Skip to content

The agent that grows with you

perf(gateway): check a local receipt's entry once when nothing was adopted

unsupportedpastelsc128ab432b96
diff --git a/gateway/session_local_recovery.py b/gateway/session_local_recovery.py
index d24ef30c30d..0a263a338cf 100644
--- a/gateway/session_local_recovery.py
+++ b/gateway/session_local_recovery.py
@@ -94,10 +94,11 @@ def restore_local_session(authority, sid):
store = authority.runner.session_store
route = store._generate_session_key(source)
# The catch-up writes, so the receipt as stored must already be this owner's (#140060).
- _checked_entry(authority, receipt, source, route)
- receipt = _with_untracked_compression(authority, sid, receipt)
- policy = restore_policy(receipt['policy'])
entry = _checked_entry(authority, receipt, source, route)
+ adopted = _with_untracked_compression(authority, sid, receipt)
+ policy = restore_policy(adopted['policy'])
+ if adopted is not receipt: # re-read after a catch-up: check the copy that will be served
+ receipt, entry = adopted, _checked_entry(authority, adopted, source, route)
target = entry.session_id
from hermes_state_local_lineage import validate_local_lineage
with authority.db._read_ctx() as conn:
diff --git a/tests/gateway/test_local_receipt_untracked_compression.py b/tests/gateway/test_local_receipt_untracked_compression.py
index 3beeca89be6..5428c149aa2 100644
--- a/tests/gateway/test_local_receipt_untracked_compression.py
+++ b/tests/gateway/test_local_receipt_untracked_compression.py
@@ -200,6 +200,26 @@ async def test_a_compacted_secondary_receipt_missing_its_origin_profile_restores
process_ownership.release(home)
+async def test_a_restore_with_nothing_to_adopt_checks_the_stored_entry_once(tmp_path, monkeypatch):
+ """The catch-up re-checks the receipt only when it moved it. Every LOCAL turn claim restores, so a
+ receipt with no untracked step costs one read of its stored session row, not two."""
+ from gateway.session_local import create_local_session
+ from gateway.session_local_recovery import restore_local_session
+
+ authority, connection = await _owner(tmp_path, monkeypatch, 'first')
+ try:
+ ref = create_local_session(authority, connection.actor, {'request_id': 'r', 'source': 'tui',
+ 'cwd': str(tmp_path), 'model': 'm', 'toolsets': []})
+ reads, get_session = [], authority.db.get_session
+ monkeypatch.setattr(authority.db, 'get_session', lambda sid, *a, **k: reads.append(sid) or get_session(sid, *a, **k))
+ assert restore_local_session(authority, ref.session_id) == ref
+ assert reads == [ref.session_id]
+ finally:
+ await connection.close()
+ authority.db.close()
+
+
@pytest.mark.asyncio
async def test_the_resume_hint_the_tui_prints_opens_the_session(tmp_path, monkeypatch):
"""The TUI's exit epilogue prints ``_short_resume_id`` (``local-`` + 16 hex); in-TUI ``/resume``