Skip to content

The agent that grows with you

fix(api): compressed API conversations keep accepting follow-ups

Teknium16a151078a3b
diff --git a/gateway/session_api.py b/gateway/session_api.py
index 490b8971df2..fd00284ddad 100644
--- a/gateway/session_api.py
+++ b/gateway/session_api.py
@@ -158,8 +158,14 @@ def restore_api_session(authority, session_id):
with store._lock:
store._ensure_loaded_locked()
current = store._entries.get(entry.session_key)
- if current is not None and current.session_id != session_id:
+ if current is None:
+ # Compression (``in_place=false``) advances the physical transcript, never this
+ # binding: the root stays the FIFO identity and the route resumes at the live tip.
+ entry.session_id = authority.db.get_compression_tip(session_id) or session_id
+ store._entries[entry.session_key] = entry
+ elif current.session_id == session_id:
+ store._entries[entry.session_key] = entry
+ elif current.session_id not in authority.db.get_compression_lineage(session_id):
raise RuntimeStoreError('admission_conflict')
- store._entries[entry.session_key] = entry
authority.sessions.setdefault(session_id, LiveSession(source, entry.session_key))
return SessionRef(authority.profile_id, session_id)
diff --git a/tests/gateway/test_api_cutover_contract.py b/tests/gateway/test_api_cutover_contract.py
index f1b86e9de9d..45c533ea185 100644
--- a/tests/gateway/test_api_cutover_contract.py
+++ b/tests/gateway/test_api_cutover_contract.py
@@ -108,3 +108,37 @@ def test_persisted_owner_scope_is_private_strict_and_legacy_safe(api, owner):
api, session_id='legacy-ownerless', active_run_id='run_legacy_ownerless',
user_message='old', conversation_history=[])
assert not owns_api_run(api, 'run_legacy_ownerless', scope)
+
+
[email protected]('queued_follower', [False, True], ids=['next-request', 'queued-follower'])
+def test_compressed_api_conversation_keeps_its_root_queue_and_follows_the_tip(api, owner, queued_follower):
+ """``compression.in_place=false`` moves the routing entry to the compression child; the API
+ binding stays on the root, which remains the FIFO identity. The next request on the same
+ declared conversation (and a follower already queued behind the compressing turn) must keep
+ admitting/executing on the root instead of failing ``admission_conflict``."""
+ from gateway.session_api_turn import check_api_turn
+ from hermes_state_runtime import claim_session_input, list_session_admissions
+
+ def admit(request_id):
+ return admit_api_turn(api, request_id=request_id, gateway_session_key='compressed',
+ bind_declared_conversation=True, user_message=request_id, conversation_history=[])
+ _, ref, first = admit('first')
+ if queued_follower:
+ follower = admit('follower')[2]
+ claim_session_input(owner.db, epoch=owner.epoch, session_id=ref.session_id)
+ owner.db.append_message(ref.session_id, 'user', 'first')
+ child = ref.session_id + '-child'
+ owner.db.publish_compression_child(parent_session_id=ref.session_id, child_session_id=child,
+ source='api_server', messages=[{'role': 'user', 'content': 'summary'}],
+ require_compression_lease=False)
+ route = owner.sessions[ref.session_id].route
+ assert owner.runner.session_store.advance_compression_session(route, ref.session_id, child) is not None
+ if queued_follower:
+ check_api_turn(owner, ref, follower['payload'])
+ else:
+ _, again, row = admit('second')
+ assert again == ref and row['target_session_id'] == ref.session_id
+ assert owner.runner.session_store.peek_session_id(route) == child
+ assert {r['target_session_id'] for r in list_session_admissions(owner.db, session_id=ref.session_id,
+ pending_only=False)} == {ref.session_id}
+ assert first['target_session_id'] == ref.session_id