hermes_state.py becomes the stand-in plugins import, the rest of the legacy store and its test folder go, and the per-process modules the backend imported from main move under persistence/sqlite/_state/. The backend checks PID namespaces and lease holders through process_identity and its WAL setup through sqlite_support, and its error module takes the shared predicates and classification from storage_errors, so neither helper set has a second copy. The backend, sqlite_support.py and the tests that pin those modules reach their final imports, and the backend-import guard allows no file outside persistence/. The modules no caller imports any more go with it: the agent's onboarding rules (now in session_rules) and gateway/session_admission.py. The stand-in's SessionDB releases its store on close() and, used again afterwards, opens it again with a warning, as the old class reopened its connection, unless the file at its path was replaced or the store is latched corrupt; a read-only one raises. A close during a call releases the store when the call returns. A named source wins over a source list in its session listing and count. An agent given a writable stand-in SessionDB as session_db= and no persistence= takes the store handle that SessionDB forwards to as its persistence, so its turn flush, token counts, compression and session row writes go through the storage interface, as an agent given the old SessionDB wrote through it. The backend-import guard also holds the backend's remaining reads of the process or task home, each with its reason, and refuses any other and any import of the stand-in inside persistence/sqlite/. Plugin context engines get the agent's store as persistence= beside the deprecated session_db=, in each hook whose signature accepts that keyword, including the rebind after /new, /resume and /branch, and the context engine guide and the plugin guide's replacement table name it. A test fails once the running release reaches the stand-in's removal release, listing what goes with the stand-in. release_unheld_media and collect_unheld_api_images take a store only: their SessionDB branches, which no caller reaches, go with the gateway's own copies of the admission and transcript queries.
139 lines
7.9 KiB
Python
139 lines
7.9 KiB
Python
import pytest
|
|
import sqlite3
|
|
|
|
from persistence.core.errors import AuthorityRefusal
|
|
from persistence.sqlite.authority._admission_store import admit_session_input, claim_session_input, recover_session_inputs
|
|
from persistence.sqlite.authority._runtime_ends import begin_runtime_epoch
|
|
from persistence.sqlite.conversation._store.messages import append_message, get_messages
|
|
from persistence.sqlite.conversation._store.sessions import create_session, end_session, get_session
|
|
from persistence.sqlite.workers import _executions
|
|
from persistence.sqlite.workers._executions import adopt_worker_execution, register_worker_execution
|
|
from persistence.sqlite.workers._receipt import mutate_worker_execution
|
|
|
|
|
|
def setup_worker(store):
|
|
assert callable(getattr(_executions, 'register_worker_execution', None)), 'canonical worker receipts missing'
|
|
holder = store.holder()
|
|
create_session(holder, 's', 'test')
|
|
epoch = begin_runtime_epoch(holder, instance_id='boot')
|
|
assignment = dict(execution_id='worker', session_id='s', generation=0)
|
|
register_worker_execution(holder, epoch=epoch, **assignment, kind='compute', adoption_secret='private-fixture')
|
|
return holder, epoch, assignment
|
|
|
|
|
|
def append(holder, *, epoch, execution_id, session_id, generation, sequence, content, role='assistant'):
|
|
"""The worker's production text write: one ``transcript.append`` receipt."""
|
|
return mutate_worker_execution(holder, epoch=epoch, execution_id=execution_id, session_id=session_id,
|
|
generation=generation, sequence=sequence, operation='transcript.append',
|
|
payload={'messages': [{'role': role, 'content': content}]})
|
|
|
|
|
|
def finish(holder, *, epoch, execution_id, session_id, generation, sequence):
|
|
return mutate_worker_execution(holder, epoch=epoch, execution_id=execution_id, session_id=session_id,
|
|
generation=generation, sequence=sequence, operation='execution.finish', payload={})
|
|
|
|
|
|
def test_worker_receipts_require_exact_sequence_assignment_and_adoption(backend, scope_a):
|
|
store = backend.open(scope_a)
|
|
holder, epoch, assignment = setup_worker(store)
|
|
assert claim_session_input(holder, epoch=epoch, session_id='s') is None
|
|
args = dict(epoch=epoch, **assignment, sequence=1, content='kept output')
|
|
result = append(holder, **args)
|
|
assert append(holder, **args) == result
|
|
assert len(get_messages(holder, 's')) == 1
|
|
for change, reason in [({'content': 'different'}, 'admission_conflict'), ({'sequence': 3}, 'invalid_params'), ({'session_id': 'other'}, 'permission_denied'), ({'generation': 1}, 'stale_generation')]:
|
|
with pytest.raises(AuthorityRefusal, match=reason):
|
|
append(holder, **(args | change))
|
|
new_epoch = begin_runtime_epoch(holder, instance_id='replacement')
|
|
recover_session_inputs(holder, epoch=new_epoch)
|
|
with pytest.raises(AuthorityRefusal, match='stale_epoch'):
|
|
append(holder, **args)
|
|
with pytest.raises(AuthorityRefusal, match='stale_epoch'):
|
|
append(holder, **(args | {'epoch': new_epoch}))
|
|
with pytest.raises(AuthorityRefusal, match='permission_denied'):
|
|
adopt_worker_execution(holder, epoch=new_epoch, **assignment, adoption_secret='wrong')
|
|
adopted = adopt_worker_execution(holder, epoch=new_epoch, **assignment, adoption_secret='private-fixture')
|
|
assert adopted['owner_epoch'] == new_epoch and 'adoption_digest' not in adopted
|
|
assert append(holder, **(args | {'epoch': new_epoch})) == result
|
|
append(holder, **(args | {'epoch': new_epoch, 'sequence': 2, 'content': 'next'}))
|
|
finished = finish(holder, epoch=new_epoch, **assignment, sequence=3)
|
|
backend.reopen()
|
|
holder = store.holder()
|
|
before = holder._conn.total_changes
|
|
assert finish(holder, epoch=new_epoch, **assignment, sequence=3) == finished
|
|
assert append(holder, **(args | {'epoch': new_epoch})) == result
|
|
assert holder._conn.total_changes == before
|
|
assert finished == {'status': 'terminal'}
|
|
with pytest.raises(AuthorityRefusal, match='admission_conflict'):
|
|
append(holder, **(args | {'epoch': new_epoch, 'content': 'changed'}))
|
|
with pytest.raises(AuthorityRefusal, match='stale_generation'):
|
|
adopt_worker_execution(holder, epoch=new_epoch, **assignment, adoption_secret='private-fixture')
|
|
with pytest.raises(AuthorityRefusal, match='stale_generation'):
|
|
append(holder, **(args | {'epoch': new_epoch, 'sequence': 4}))
|
|
assert [r['content'] for r in get_messages(holder, 's')] == ['kept output', 'next']
|
|
admit_session_input(holder, epoch=new_epoch, principal_id='human', session_id='s', request_id='next', payload={})
|
|
assert claim_session_input(holder, epoch=new_epoch, session_id='s')['generation'] > assignment['generation']
|
|
with pytest.raises(AuthorityRefusal, match='stale_generation'):
|
|
append(holder, **(args | {'epoch': new_epoch}))
|
|
with pytest.raises(AuthorityRefusal, match='stale_generation'):
|
|
finish(holder, epoch=new_epoch, **assignment, sequence=3)
|
|
|
|
|
|
@pytest.mark.parametrize('content', ['once', 'a' + chr(0xD800) + 'b'])
|
|
def test_worker_mutation_and_receipt_roll_back_together(backend, scope_a, content):
|
|
store = backend.open(scope_a)
|
|
holder, epoch, assignment = setup_worker(store)
|
|
holder._conn.execute("CREATE TRIGGER reject_receipt BEFORE INSERT ON worker_receipts BEGIN SELECT RAISE(ABORT, 'receipt fixture'); END")
|
|
create_session(holder, 'direct', 'test')
|
|
append_message(holder, 'direct', 'assistant', content)
|
|
expected_content = get_messages(holder, 'direct')[0]['content']
|
|
args = dict(epoch=epoch, **assignment, sequence=1, content=content)
|
|
with pytest.raises(sqlite3.IntegrityError, match='receipt fixture'):
|
|
append(holder, **args)
|
|
assert get_messages(holder, 's') == []
|
|
assert get_session(holder, 's')['message_count'] == 0
|
|
assert holder._conn.execute("SELECT last_sequence FROM worker_executions WHERE execution_id='worker'").fetchone()[0] == 0
|
|
holder._conn.execute('DROP TRIGGER reject_receipt')
|
|
receipt = append(holder, **args)
|
|
backend.reopen()
|
|
holder = store.holder()
|
|
assert append(holder, **args) == receipt
|
|
assert get_messages(holder, 's')[0]['content'] == expected_content
|
|
if content != expected_content:
|
|
with pytest.raises(AuthorityRefusal, match='admission_conflict'):
|
|
append(holder, **(args | {'content': expected_content}))
|
|
assert len(get_messages(holder, 's')) == get_session(holder, 's')['message_count'] == 1
|
|
|
|
|
|
@pytest.mark.parametrize('closed', [False, True])
|
|
def test_worker_append_preserves_compression_write_guards(backend, scope_a, closed):
|
|
from persistence.sqlite._state.errors import CompressionSessionClosedError
|
|
|
|
store = backend.open(scope_a)
|
|
holder, epoch, assignment = setup_worker(store)
|
|
args = dict(epoch=epoch, **assignment, sequence=1, content='before closure')
|
|
receipt = append(holder, **args)
|
|
assert store.coordination.try_acquire_compression_lock(session_id='s', holder='compressor')
|
|
if closed:
|
|
end_session(holder, 's', end_reason='compression')
|
|
create_session(holder, 'tip', 'test', parent_session_id='s')
|
|
before = get_session(holder, 's')
|
|
messages = get_messages(holder, 's')
|
|
next_args = args | {'sequence': 2, 'content': 'new output'}
|
|
if closed:
|
|
with pytest.raises(CompressionSessionClosedError):
|
|
append_message(holder, 's', 'assistant', 'ordinary')
|
|
with pytest.raises(CompressionSessionClosedError):
|
|
append(holder, **next_args)
|
|
assert get_session(holder, 's') == before
|
|
assert get_messages(holder, 's') == messages
|
|
assert holder._conn.execute('SELECT COUNT(*) FROM worker_receipts').fetchone()[0] == 1
|
|
assert holder._conn.execute('SELECT last_sequence FROM worker_executions').fetchone()[0] == 1
|
|
else:
|
|
append_message(holder, 's', 'assistant', 'ordinary')
|
|
append(holder, **next_args)
|
|
assert [r['content'] for r in get_messages(holder, 's')] == ['before closure', 'ordinary', 'new output']
|
|
backend.reopen()
|
|
holder = store.holder()
|
|
assert append(holder, **args) == receipt
|