Skip to content

The agent that grows with you

fix(gateway): remap local compression tip ids on session.resume

Chinmayab275e5e9192c
diff --git a/gateway/session_controls.py b/gateway/session_controls.py
index 0477b2d8042..3cc964de71d 100644
--- a/gateway/session_controls.py
+++ b/gateway/session_controls.py
@@ -508,7 +508,10 @@ class AuthorityConnection:
# Another viewer's refresh mid-cleanup has committed a policy it has not published yet.
await refresh_settled(self.authority, ref.session_id)
row = self.authority.db.get_session(ref.session_id)
- if row and row['source'] in LEGACY_LOCAL_SOURCES:
+ # Canonical LOCAL compression/reset segments keep the receipt on their logical
+ # owner, not on the physical transcript ID the history picker / Desktop supplies
+ # after list_sessions projects the tip (#139306).
+ if row and row['source'] in LEGACY_LOCAL_SOURCES | {Platform.LOCAL.value}:
ref = resolve_local_target(self.authority, self.actor,
self.authority.db.get_compression_tip(ref.session_id) or ref.session_id)
if params.get('editor') is not None:
diff --git a/gateway/session_local_migration.py b/gateway/session_local_migration.py
index 08e766b3aaf..31513443521 100644
--- a/gateway/session_local_migration.py
+++ b/gateway/session_local_migration.py
@@ -54,6 +54,11 @@ def resolve_local_target(authority, actor, sid):
from hermes_state_local_migration import LEGACY_PREFIX
bound = conn.execute('SELECT 1 FROM state_meta WHERE key IN (?,?)',
(POLICY_PREFIX + (row.get('chat_id') or sid), LEGACY_PREFIX + (row.get('chat_id') or sid))).fetchone()
+ if bound and row.get('chat_id'):
+ from hermes_state_local import local_lineage_owner
+ # A copied chat_id is not evidence that this row belongs to the owner's lineage.
+ if local_lineage_owner(conn, sid) != row['chat_id']:
+ raise RuntimeStoreError('storage_unavailable')
if bound or str(row.get('chat_id') or '').startswith('local-'):
ref = SessionRef(authority.profile_id, row['chat_id'])
authority.authorize(actor, ref, 'session:read')
diff --git a/tests/gateway/test_session_resume_compression_tip.py b/tests/gateway/test_session_resume_compression_tip.py
new file mode 100644
index 00000000000..a7662c8aef9
--- /dev/null
+++ b/tests/gateway/test_session_resume_compression_tip.py
@@ -0,0 +1,106 @@
+"""Desktop lists compression tips; session.resume must remap tip -> owner receipt."""
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+
+from gateway.session_contract import SessionRef
+from gateway.session_controls import AuthorityConnection
+
+
+def _attach_snapshot(owner_ref):
+ return SimpleNamespace(
+ subscription_id='sub',
+ history=(),
+ pending=(),
+ prompts=(),
+ replay_epoch='e',
+ last_sequence=0,
+ handle=SimpleNamespace(
+ ref=owner_ref,
+ execution_state='idle',
+ authority_epoch=1,
+ revision=1,
+ execution_generation=0,
+ ),
+ )
+
+
+async def test_session_resume_remaps_local_source_compression_tip(monkeypatch):
+ """Session lists project the tip id after compression. Cold resume must remap
+ ``source=local`` tips to the owner via resolve_local_target — not only
+ LEGACY_LOCAL_SOURCES — or restore_local_session raises storage_unavailable
+ (no policy receipt on the child id)."""
+ owner_id = 'local-owner-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
+ tip_id = '20261015_221307_d93723'
+ owner_ref = SessionRef('fixture', owner_id)
+ resolved = []
+
+ monkeypatch.setattr(
+ 'gateway.session_local_migration.resolve_local_target',
+ lambda authority, actor, sid: resolved.append(sid) or owner_ref)
+
+ row = {
+ 'id': tip_id,
+ 'source': 'local',
+ 'chat_id': owner_id,
+ 'parent_session_id': owner_id,
+ }
+ authority = SimpleNamespace(
+ profile_id='fixture',
+ instance_id='one',
+ events={},
+ db=SimpleNamespace(
+ get_session=lambda sid: row if sid == tip_id else None,
+ get_compression_tip=lambda sid: tip_id if sid in (tip_id, owner_id) else None,
+ ),
+ sessions={owner_id: SimpleNamespace(source=SimpleNamespace(platform=None))},
+ authorize=lambda *a, **k: None,
+ attach=AsyncMock(return_value=_attach_snapshot(owner_ref)),
+ )
+ connection = AuthorityConnection(
+ authority, object(),
+ {'user_id': 'owner', 'capabilities': [
+ 'session:create', 'session:read', 'session:control']},
+ )
+ result = await connection.resume(SessionRef('fixture', tip_id), {})
+ assert resolved == [tip_id]
+ assert result['session_id'] == owner_id
+ assert result['stored_session_id'] == owner_id
+
+
+async def test_session_resume_still_remaps_legacy_cli_source(monkeypatch):
+ """Pre-authority cli/tui/gui rows keep the previous remap path."""
+ owner_id = 'legacy-cli-owner'
+ tip_id = 'legacy-cli-tip'
+ owner_ref = SessionRef('fixture', owner_id)
+ resolved = []
+
+ monkeypatch.setattr(
+ 'gateway.session_local_migration.resolve_local_target',
+ lambda authority, actor, sid: resolved.append(sid) or owner_ref)
+
+ authority = SimpleNamespace(
+ profile_id='fixture',
+ instance_id='one',
+ events={},
+ db=SimpleNamespace(
+ get_session=lambda sid: {
+ 'id': tip_id, 'source': 'cli', 'chat_id': owner_id,
+ } if sid == tip_id else None,
+ get_compression_tip=lambda sid: tip_id,
+ ),
+ sessions={owner_id: SimpleNamespace(source=SimpleNamespace(platform=None))},
+ authorize=lambda *a, **k: None,
+ attach=AsyncMock(return_value=_attach_snapshot(owner_ref)),
+ )
+ connection = AuthorityConnection(
+ authority, object(),
+ {'user_id': 'owner', 'capabilities': [
+ 'session:create', 'session:read', 'session:control']},
+ )
+ result = await connection.resume(SessionRef('fixture', tip_id), {})
+ assert resolved == [tip_id]
+ assert result['session_id'] == owner_id