Files
hermes-agent/tests/hermes_state/test_runtime_schema.py
teknium1 caa329904c Merge remote-tracking branch 'origin/main' into fix/106742-thread-resolution
# Conflicts:
#	apps/desktop/src/app/session/hooks/use-message-stream/steer-arrival-order.test.tsx
#	apps/desktop/src/store/composer-queue.ts
#	apps/desktop/src/types/hermes.ts
#	apps/shared/src/json-rpc-gateway.ts
#	cron/jobs.py
#	cron/scheduler.py
#	gateway/hosted_room_policy_checkpoint.py
#	gateway/run.py
#	hermes_cli/env_loader.py
#	tests/acp_adapter/test_catalog_read_only.py
#	tests/acp_adapter/test_gateway_cancel_admission.py
#	tests/acp_adapter/test_gateway_catalog_cwd.py
#	tests/acp_adapter/test_gateway_client_boundary.py
#	tests/acp_adapter/test_gateway_controls.py
#	tests/acp_adapter/test_gateway_editor_cwd.py
#	tests/acp_adapter/test_gateway_editor_mcp.py
#	tests/acp_adapter/test_gateway_editor_policy.py
#	tests/acp_adapter/test_gateway_editor_v4a.py
#	tests/acp_adapter/test_gateway_editor_wire.py
#	tests/acp_adapter/test_gateway_entry_lifecycle.py
#	tests/acp_adapter/test_gateway_new_cold.py
#	tests/acp_adapter/test_gateway_prompt_content.py
#	tests/acp_adapter/test_gateway_sessions.py
#	tests/acp_adapter/test_gateway_terminal_projection.py
#	tests/acp_adapter/test_gateway_tool_projection.py
#	tests/acp_adapter/test_gateway_wire_controls.py
#	tests/acp_adapter/test_policy_identity_compat.py
#	tests/acp_adapter/test_session_mcp_frozen.py
#	tests/acp_adapter/test_session_mcp_policy.py
#	tests/cron/test_scheduler.py
#	tests/hermes_state/test_ledger_delete_consistency.py
#	tests/hermes_state/test_runtime_admission.py
#	tests/hermes_state/test_runtime_adoption.py
#	tests/hermes_state/test_runtime_destructive_mutations.py
#	tests/hermes_state/test_runtime_import_mutations.py
#	tests/hermes_state/test_runtime_mutation_actions.py
#	tests/hermes_state/test_runtime_mutation_retired_import.py
#	tests/hermes_state/test_runtime_mutations.py
#	tests/hermes_state/test_runtime_recovery.py
#	tests/hermes_state/test_runtime_rewind_mutations.py
#	tests/hermes_state/test_runtime_schema.py
#	tests/hermes_state/test_runtime_worker.py
#	tests/hermes_state/test_runtime_worker_compaction.py
#	tests/hermes_state/test_runtime_worker_compression.py
#	tests/hermes_state/test_runtime_worker_compression_admission.py
#	tests/hermes_state/test_runtime_worker_compression_annotations.py
#	tests/hermes_state/test_runtime_worker_compression_cleanup.py
#	tests/hermes_state/test_runtime_worker_compression_flush.py
#	tests/hermes_state/test_runtime_worker_context.py
#	tests/hermes_state/test_runtime_worker_history.py
#	tests/hermes_state/test_runtime_worker_lifecycle.py
#	tests/hermes_state/test_runtime_worker_metadata.py
#	tests/hermes_state/test_runtime_worker_operations.py
#	tests/hermes_state/test_runtime_worker_rotation_automation.py
#	tests/hermes_state/test_runtime_worker_title_retry.py
#	tests/hermes_state/test_runtime_worker_turn_cleanup.py
#	tests/hermes_state/test_runtime_worker_usage.py
#	tests/hermes_state/test_terminal_retirement.py
#	tests/tui_gateway/test_bot_relay_methods.py
#	tools/async_delegation.py
#	tools/bot_relay.py
#	tui_gateway/server.py
#	ui-tui/src/app/createGatewayEventHandler.ts
#	ui-tui/src/app/useSessionLifecycle.ts
#	ui-tui/src/app/useSubmission.ts
#	ui-tui/src/gatewayClient.ts
#	ui-tui/src/gatewayTypes.ts
#	ui-tui/src/types.ts
#	web/src/lib/api.ts
#	website/docs/developer-guide/gateway-classic-cli.md
#	website/docs/developer-guide/gateway-local-sessions.md
#	website/docs/user-guide/sessions.md
2026-09-13 12:27:55 -07:00

57 lines
2.9 KiB
Python

"""Real SQLite schema reconciliation and storage-failure controls."""
import sqlite3
import pytest
from hermes_state import SessionDB
from hermes_state_common import SCHEMA_VERSION
import hermes_state_runtime as rt
def test_schema_reconciles_existing_sessions_without_losing_history(tmp_path):
path = tmp_path / 'state.db'
db = SessionDB(db_path=path)
db.create_session('existing', source='test')
db.append_message('existing', role='user', content='retained')
db.close()
with sqlite3.connect(path) as legacy:
legacy.execute('ALTER TABLE sessions DROP COLUMN runtime_generation')
legacy.execute('ALTER TABLE sessions DROP COLUMN runtime_revision')
for table in ('worker_receipts', 'worker_executions', 'session_admissions', 'runtime_epoch'):
legacy.execute(f'DROP TABLE {table}')
legacy.execute('UPDATE schema_version SET version=?', (SCHEMA_VERSION - 1,))
db = SessionDB(db_path=path)
try:
assert db.get_session('existing')['runtime_revision'] == 0
assert db.get_session('existing')['runtime_generation'] == 0
assert [r['content'] for r in db.get_messages('existing')] == ['retained']
assert db._conn.execute('SELECT version FROM schema_version').fetchone()[0] == SCHEMA_VERSION
epoch = rt.begin_runtime_epoch(db, instance_id='boot')
assert rt.admit_session_input(db, epoch=epoch, principal_id='human', session_id='existing', request_id='r', payload={'text': 'next'})['status'] == 'queued'
assert db._conn.execute('PRAGMA foreign_key_check').fetchall() == []
finally:
db.close()
def test_real_sqlite_full_aborts_admission_and_epoch_failure_is_atomic(tmp_path):
db = SessionDB(db_path=tmp_path / 'state.db')
try:
db.create_session('s', source='test')
epoch = rt.begin_runtime_epoch(db, instance_id='boot')
db._conn.execute("CREATE TRIGGER refuse_epoch BEFORE UPDATE ON runtime_epoch BEGIN SELECT RAISE(ABORT, 'epoch fixture'); END")
with pytest.raises(sqlite3.IntegrityError, match='epoch fixture'):
rt.begin_runtime_epoch(db, instance_id='refused')
assert tuple(db._conn.execute('SELECT epoch,instance_id FROM runtime_epoch').fetchone()) == (epoch, 'boot')
db._conn.execute('DROP TRIGGER refuse_epoch')
pages = db._conn.execute('PRAGMA page_count').fetchone()[0]
db._conn.execute(f'PRAGMA max_page_count={pages}')
args = dict(epoch=epoch, principal_id='human', session_id='s', request_id='full', payload={'text': 'x' * (2 * 1024 * 1024)})
with pytest.raises(sqlite3.OperationalError) as exc:
rt.admit_session_input(db, **args)
assert exc.value.sqlite_errorcode == sqlite3.SQLITE_FULL
assert rt.list_session_admissions(db, session_id='s') == []
db._conn.execute('PRAGMA max_page_count=2147483646')
assert rt.admit_session_input(db, **args)['status'] == 'queued'
finally:
db.close()