fix(state): retiring a deleted target strips its text from every mutation receipt naming it

f7dd0aedab stripped target_message/summary only from mutation receipts whose
result.session_id was a deleted id. A local owner's receipts are keyed by the
logical id, but rewind copies the rewound turn from the PHYSICAL target (a
reset child). Deleting just that child (legacy delete_session / dashboard row)
left the logical owner alive, so its gateway.mutation.v1 rewind receipt still
held the deleted turn and an exact retry returned it.

Retirement now matches a receipt by every session id its result names
(session_id, target_message.session_id, target_session_id,
previous_target_session_id), and also nulls a user-set title (rename/sidebar),
which otherwise outlived a full delete in its receipt.

Raw state.db / -wal bytes still hold the deleted rows until SQLite reuses the
freed pages; main's delete_session behaves the same (secure_delete off, PASSIVE
checkpoints) and that is left as a policy decision.

Review: https://github.com/NousResearch/hermes-agent/pull/106742#issuecomment-6073624820
(cherry picked from commit 0a4b3e550b7f2192d363bf2e5661d51713a052a0)
This commit is contained in:
Teknium
2026-10-08 22:54:35 -07:00
parent c4d1ba0e2f
commit b178f6f6fc
2 changed files with 51 additions and 6 deletions

View File

@@ -98,19 +98,26 @@ def retire_sessions(conn, session_ids):
retire_local_receipts(conn, session_ids)
# Mutation-receipt fields that copy transcript content: the rewound message and a compaction summary.
_MUTATION_TRANSCRIPT_FIELDS = ('target_message', 'summary')
# Mutation-receipt fields that copy user content: the rewound message, a compaction summary and
# a user-set title (rename/sidebar).
_MUTATION_TRANSCRIPT_FIELDS = ('target_message', 'summary', 'title')
# Every session a receipt result can name. A local owner's receipt is keyed by its logical id, but
# its rewind/compress content comes from the PHYSICAL target (a reset child): deleting only that
# child must strip it too, or the surviving owner's exact retry replays the deleted text.
_RECEIPT_SESSION_PATHS = ('$.result.session_id', '$.result.target_message.session_id',
'$.result.target_session_id', '$.result.previous_target_session_id')
def retire_mutation_receipts(conn, session_ids):
"""Keep each exact-retry mutation receipt (digest, ids, revision) but drop the transcript
copies a rewind/compress result carries, so a retry after the delete cannot read them back."""
"""Keep each exact-retry mutation receipt (digest, ids, revision) but drop the user content
a rewind/compress/rename result carries, so a retry after the delete cannot read it back."""
if not session_ids:
return
named = ' OR '.join(f"json_extract(value,'{path}') IN (SELECT value FROM json_each(?1))"
for path in _RECEIPT_SESSION_PATHS)
rows = conn.execute(
"SELECT key,value FROM state_meta WHERE key GLOB 'gateway.mutation.v1.*' AND "
"CASE WHEN json_valid(value) THEN json_extract(value,'$.result.session_id') END "
"IN (SELECT value FROM json_each(?))", (json.dumps(list(session_ids)),)).fetchall()
f"CASE WHEN json_valid(value) THEN ({named}) END", (json.dumps(list(session_ids)),)).fetchall()
for key, raw in rows:
receipt = json.loads(raw)
result = receipt.get('result')

View File

@@ -271,3 +271,41 @@ def test_deleted_history_is_not_recoverable_from_worker_or_mutation_receipts(tmp
terminal_worker_receipt(db, execution_id='worker', session_id='gone', generation=row['generation'],
sequence=1, adoption_secret='proof', payload_digest=rt.admission_fingerprint(
canonical_target='gone', payload={'operation': 'compression.history', 'payload': history}))
def test_mutation_receipts_naming_a_deleted_physical_target_drop_its_text(tmp_path):
"""D7 / pastels M07: a local owner's rewind receipt is keyed by the logical id but copies the
rewound turn from the physical reset child. Deleting only that child (``hermes sessions delete``
on the dashboard row) must still strip it, and a full delete also drops the user-set title."""
import json
from tests.hermes_state.test_target_advance_fence import _local_session
def args(db, sid, operation, request_id, payload):
snap = db.get_session(sid)
return dict(principal_id='human', session_id=sid, request_id=request_id, operation=operation,
payload=payload, expected_revision=snap['runtime_revision'],
expected_generation=snap['runtime_generation'])
with SessionDB(tmp_path / 'state.db') as db:
epoch = rt.begin_runtime_epoch(db, instance_id='owner')
sid = _local_session(db, epoch, tmp_path)
child = rt.mutate_runtime_session(db, epoch=epoch, **args(db, sid, 'reset', 'reset', {}))['target_session_id']
db.append_message(child, 'user', 'first')
db.append_message(child, 'assistant', 'a1')
target = db.append_message(child, 'user', 'SECRET_CHILD_TURN')
db.append_message(child, 'assistant', 'a2')
rewind = args(db, sid, 'rewind', 'rw', {'target_message_id': target})
assert 'SECRET_CHILD_TURN' in json.dumps(rt.mutate_runtime_session(db, epoch=epoch, **rewind))
rename = args(db, sid, 'rename', 'rn', {'title': 'SECRET_TITLE'})
rt.mutate_runtime_session(db, epoch=epoch, **rename)
assert db.delete_session(child) and db.get_session(sid) is not None
def blobs():
with db._read_ctx() as c:
return ''.join(v for (v,) in c.execute('SELECT value FROM state_meta'))
assert 'SECRET_CHILD_TURN' not in blobs()
replay = rt.mutate_runtime_session(db, epoch=epoch, **rewind)
assert replay['target_message'] is None and replay['rewound_count'] == 2
rt.mutate_runtime_session(db, epoch=epoch, **args(db, sid, 'delete', 'del', {}))
assert 'SECRET_TITLE' not in blobs()
assert rt.mutate_runtime_session(db, epoch=epoch, **rename)['title'] is None