Skip to content

The agent that grows with you

fix(gateway): a Desktop resume that omits messages reads no compression lineage

Teknium08f9f47d018c
diff --git a/gateway/platforms/api_server_authority_runs.py b/gateway/platforms/api_server_authority_runs.py
index 136e49f6c6a..ef229d97453 100644
--- a/gateway/platforms/api_server_authority_runs.py
+++ b/gateway/platforms/api_server_authority_runs.py
@@ -126,7 +126,7 @@ async def respond_run(adapter, run_id, body, *, kind):
ref = SessionRef(authority.profile_id, row['target_session_id'])
actor = Principal('api', authority.profile_id,
frozenset({'session:read', 'session:approve', 'session:respond'}), 'api-control:' + uuid.uuid4().hex)
- snapshot = await authority.attach(actor, ref)
+ snapshot = await authority.attach(actor, ref, history=False) # prompts only
try:
if not any(p['prompt_id'] == prompt_id and p['kind'] == kind for p in snapshot.prompts):
raise RuntimeStoreError('approval_not_pending')
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index e14caa9b57f..a14e5fa7283 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -347,10 +347,14 @@ class SessionAuthority:
raise RuntimeStoreError('not_found') from None
raise
- async def attach(self, actor, ref):
+ async def attach(self, actor, ref, *, history=True):
+ """Join *ref*'s event stream and snapshot it. ``history=False``: the snapshot carries no transcript
+ (``history == ()``), so the compression lineage is never read; for a viewer that pages the
+ transcript itself (Desktop over REST) or reads only the prompts and handle."""
self.authorize(actor, ref, 'session:read')
- from gateway.session_local_recovery import local_history, prepare_history
- prepared = await prepare_history(self, ref) # the lineage read stays off the loop and the lock
+ from gateway.session_local_recovery import local_history, prepare_history, transcript_target
+ # The lineage read stays off the loop and the lock; without history only the route is restored.
+ prepared = await prepare_history(self, ref) if history else (transcript_target(self, ref), None)
from gateway.session_onboarding import keep_from_discard
# The last await before the viewer joins: a fresh launch's claim is taken back with no yield
# before the join, and a delete already past it answers not_found below.
@@ -376,7 +380,7 @@ class SessionAuthority:
active_generation = handle.execution_generation if handle.execution_state == "running" else None
prompts = live.controls.snapshot(ref.session_id, active_generation)
epoch, sequence = live.event_stream.watermark()
- history = self.unless_deleted(ref, lambda: tuple(local_history(self, ref, prepared)))
+ history = self.unless_deleted(ref, lambda: tuple(local_history(self, ref, prepared))) if history else ()
snapshot = SubscriptionSnapshot(subscription, handle, epoch, sequence, history,
tuple(self._pending_receipt(r) for r in pending_rows(self, ref)), prompts)
from gateway.session_viewer_close import cancel_persisted_close
diff --git a/gateway/session_contract.py b/gateway/session_contract.py
index dda3763a395..c109ac26acf 100644
--- a/gateway/session_contract.py
+++ b/gateway/session_contract.py
@@ -85,7 +85,7 @@ class SubscriptionSnapshot:
class SessionAuthority(Protocol):
async def create(self, actor: Principal, request: SessionCreate) -> SessionHandle: ...
async def resolve(self, actor: Principal, ref: SessionRef) -> SessionHandle: ...
- async def attach(self, actor: Principal, ref: SessionRef) -> SubscriptionSnapshot: ...
+ async def attach(self, actor: Principal, ref: SessionRef, *, history: bool = True) -> SubscriptionSnapshot: ...
async def detach(self, actor: Principal, subscription_id: str, *, end_reason: str | None = None) -> None: ...
async def submit(self, actor: Principal, request: Submission) -> AdmissionReceipt: ...
async def receipt(self, actor: Principal, ref: SessionRef, admission_id: str) -> AdmissionReceipt: ...
diff --git a/gateway/session_controls.py b/gateway/session_controls.py
index d029c12dd1c..369703898c8 100644
--- a/gateway/session_controls.py
+++ b/gateway/session_controls.py
@@ -110,6 +110,21 @@ def model_only(row):
return isinstance(metadata, dict) and bool(metadata.get(MODEL_ONLY_DISPLAY_METADATA_KEY))
+async def _resume_transcript(authority, ref, history, omit):
+ """A resume reply's transcript fields. *omit* (``omit_messages``): no rows and ``messages_omitted``, the
+ count and the todo plan from bounded reads of the physical tip, as the stdio resume answers it."""
+ # Its home module, not tui_gateway.server: importing the stdio sidecar points sys.stdout at stderr.
+ from tui_gateway.tool_progress import _todo_state_from_history
+ if not omit:
+ # The newest todo result in the transcript, the snapshot the session's agent hydrates
+ # from and its ``todo.updated`` frames carried: a viewer attaching shows the plan now.
+ return {'messages': history, 'message_count': len(history), 'todo_state': _todo_state_from_history(history)}
+ from gateway.session_local_recovery import omitted_history_state, transcript_target
+ todo_state, count = await asyncio.to_thread(
+ omitted_history_state, authority.db, transcript_target(authority, ref))
+ return {'messages': [], 'messages_omitted': True, 'message_count': count, 'todo_state': todo_state}
+
+
class AuthorityConnection:
def __init__(self, authority, transport, identity, *, operator=False):
self.authority = authority
@@ -637,7 +652,10 @@ class AuthorityConnection:
# whose earlier open found it busy (Desktop's warm `session.activate` resumes on one connection).
from gateway.session_policy_refresh import refresh_resumed_policy
await refresh_resumed_policy(self.authority, ref, attached=ref.session_id in self.subscriptions)
- snapshot = await self.authority.attach(self.actor, ref)
+ # ``omit_messages``: the client pages the transcript itself (Desktop over REST), so the attach reads no
+ # compression lineage (a whole-lineage read here made every open of a long chat cost seconds).
+ omit = params.get('omit_messages') is True
+ snapshot = await self.authority.attach(self.actor, ref, history=not omit)
self.subscriptions[ref.session_id] = snapshot.subscription_id
self.handed_back.discard(ref.session_id)
self._spawn_notice(ref)
@@ -651,15 +669,13 @@ class AuthorityConnection:
info = await snapshot_info(self.authority, snapshot.handle.ref)
from gateway.session_connections import pending_connection
from gateway.session_server_requests import open_requests
- # Its home module, not tui_gateway.server: importing the stdio sidecar points sys.stdout at stderr.
- from tui_gateway.tool_progress import _todo_state_from_history
history, display = list(snapshot.history), {}
if params.get('display_projection'):
# A micro-compaction merge stays in ``messages`` (an undo target); its originals are what shows.
display['display_messages'] = display_projection(
[row for row in history if not model_only(row)], self.authority.profile_id)
return {'session_id': ref.session_id, 'stored_session_id': ref.session_id,
- 'messages': history, **display, 'message_count': len(history),
+ **await _resume_transcript(self.authority, ref, history, omit), **display,
'running': snapshot.handle.execution_state == 'running',
'authority_epoch': snapshot.handle.authority_epoch,
'replay_epoch': snapshot.replay_epoch, 'last_sequence': snapshot.last_sequence,
@@ -670,10 +686,7 @@ class AuthorityConnection:
'pending_connection': pending_connection(self.authority, ref.session_id),
# Still-waiting server→client requests: the event ring cannot say "still open".
'open_requests': open_requests(self.authority.sessions[snapshot.handle.ref.session_id].event_stream,
- snapshot.handle.ref.session_id),
- # The newest todo result in the transcript, the snapshot the session's agent hydrates
- # from and its ``todo.updated`` frames carried: a viewer attaching shows the plan now.
- 'todo_state': _todo_state_from_history(history)}
+ snapshot.handle.ref.session_id)}
def _spawn_notice(self, ref):
"""Low-disk auto-prune question for this viewer, off the resume reply's path."""
diff --git a/gateway/session_hosted_rpc.py b/gateway/session_hosted_rpc.py
index 4b9fe830860..571fb75eed8 100644
--- a/gateway/session_hosted_rpc.py
+++ b/gateway/session_hosted_rpc.py
@@ -247,7 +247,7 @@ class HostedRoomAuthorityRPC:
'task_id': current[1].task_id if current else None,
'execution_generation': current[2] if current else None,
'status': current[0]['status'] if current else 'idle'}
- snapshot = await self.authority.attach(self.principal, self.ref)
+ snapshot = await self.authority.attach(self.principal, self.ref, history=False) # prompts only
if snapshot.prompts:
prompt = next((p for p in snapshot.prompts if p.get('kind') == 'approval'), None)
if prompt:
@@ -291,7 +291,7 @@ class HostedRoomAuthorityRPC:
async def _approve(self, params):
if params['choice'] not in {'once', 'deny'}:
raise RuntimeStoreError('invalid_params')
- snapshot = await self.authority.attach(self.principal, self.ref)
+ snapshot = await self.authority.attach(self.principal, self.ref, history=False) # prompts only
prompt = next((p for p in snapshot.prompts if p.get('prompt_id') == params['request_id']
and p.get('kind') == 'approval'), None)
if prompt is None:
diff --git a/gateway/session_local_recovery.py b/gateway/session_local_recovery.py
index a6590ff1dd9..e975e8bc202 100644
--- a/gateway/session_local_recovery.py
+++ b/gateway/session_local_recovery.py
@@ -265,6 +265,14 @@ async def prepare_history(authority, ref):
return target, await asyncio.to_thread(display_rows, authority.db, target)
+def omitted_history_state(db, target):
+ """``(todo_state, message_count)`` of a resume that omits its transcript: bounded, indexed reads of the
+ physical *target* (the newest todo result, the stored count), never the compression lineage."""
+ from tui_gateway.tool_progress import _todo_state_from_db
+ row = db.get_session(target) or {}
+ return _todo_state_from_db(db, target), int(row.get('message_count') or 0)
+
+
def _merge_live_in_stored_order(display, live):
"""The live rows in stored (``row_id``) order, each archived display row placed before the live
row it preceded in display order.
diff --git a/tests/gateway/test_resume_omit_messages.py b/tests/gateway/test_resume_omit_messages.py
new file mode 100644
index 00000000000..496cc6624b0
--- /dev/null
+++ b/tests/gateway/test_resume_omit_messages.py
@@ -0,0 +1,70 @@
+"""A resume that asks ``omit_messages`` (Desktop, which pages the transcript over REST) gets no transcript rows,
+so opening a long compressed chat no longer reads its whole compression lineage; a resume without it still
+gets the full display history, and both report the same todo plan.
+
+Real store, authority, local session, compaction verb and ``AuthorityConnection.resume``; the runner is a
+stand-in with no cached agent, and the resume ``info`` block is stubbed out.
+"""
+import json
+from types import SimpleNamespace
+
+import pytest
+
+from agent.context_compressor import SUMMARY_PREFIX
+
+TODOS = {'todos': [{'id': '1', 'content': 'ship it', 'status': 'in_progress'}]}
+
+
+async def _local_session(tmp_path, monkeypatch):
+ from gateway import run, session_local
+ from gateway.config import GatewayConfig
+ from gateway.session import SessionStore
+ from gateway.session_authority import initialize_session_authority
+ from gateway.session_contract import Principal
+ from hermes_state_local import local_receipt
+ monkeypatch.setattr(run, '_load_gateway_config', dict)
+ monkeypatch.setattr(session_local, 'local_session_info', lambda *a: {})
+ store = SessionStore(tmp_path / 'sessions', GatewayConfig())
+ runner = SimpleNamespace(session_store=store, _session_db=store._db, adapters={}, _draining=False,
+ _cached_agent_for=lambda *a, **k: None, _evict_cached_agent=lambda *a, **k: None)
+ runner._adapter_for_source = lambda source: runner.adapters.get(source.platform)
+ authority = await initialize_session_authority(runner, profile_id='fixture', instance_id='first')
+ actor = Principal('owner', 'fixture', frozenset({'session:create', 'session:read', 'session:control'}), 'sock')
+ ref = session_local.create_local_session(authority, actor, dict(
+ request_id='resume', source='gui', cwd=str(tmp_path), model='frozen', toolsets=[]))
+ return authority, actor, ref, local_receipt(authority.db, ref.session_id)['entry']['session_id']
+
+
+async def _resume(authority, actor, ref, **params):
+ from gateway.session_controls import AuthorityConnection
+ conn = AuthorityConnection(authority, object(), {'user_id': 'owner'})
+ conn.actor = actor
+ authority.events[actor.transport_id] = object()
+ return await conn.resume(ref, {'session_id': ref.session_id, **params})
+
+
+def _seed_compacted(db, physical):
+ for n in range(3):
+ db.append_message(physical, 'user', f'OLD_Q{n}')
+ db.append_message(physical, 'assistant', f'OLD_A{n}')
+ db.append_message(physical, 'assistant', None, tool_calls=[
+ {'id': 'todo-1', 'type': 'function', 'function': {'name': 'todo', 'arguments': '{}'}}])
+ db.append_message(physical, 'tool', json.dumps(TODOS), tool_name='todo', tool_call_id='todo-1')
+ db.append_message(physical, 'user', 'TAIL_Q')
+ db.append_message(physical, 'assistant', 'TAIL_A')
+ db.archive_and_compact(physical, [{'role': 'user', 'content': SUMMARY_PREFIX + '\nsummary'},
+ {'role': 'user', 'content': 'TAIL_Q'},
+ {'role': 'assistant', 'content': 'TAIL_A'}], tail_count=2)
+
+
+async def test_omit_messages_resume_reads_no_transcript(tmp_path, monkeypatch):
+ authority, actor, ref, physical = await _local_session(tmp_path, monkeypatch)
+ _seed_compacted(authority.db, physical)
+ full = await _resume(authority, actor, ref)
+ assert any(row.get('archived') for row in full['messages']) # the default resume still shows archived turns
+ omitted = await _resume(authority, actor, ref, omit_messages=True)
+ assert omitted['messages'] == [] and omitted['messages_omitted'] is True
+ assert omitted['message_count'] == authority.db.get_session(physical)['message_count']
+ # Both paths report the same plan: the omitted one reads it from the store's bounded todo lookup.
+ assert omitted['todo_state'] == full['todo_state'] and omitted['todo_state']