The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): reuse a session's context breakdown until its transcript moves
Tekniuma7e69984ce97
diff --git a/gateway/session_authority.py b/gateway/session_authority.pyindex a3b7011b029..d85b652650b 100644--- a/gateway/session_authority.py+++ b/gateway/session_authority.py@@ -58,6 +58,9 @@ class LiveSession:# (a contended SQLite writer must not stall loop-side lock takers); readers holding that lock# still see the claim as started, so no snapshot shows a terminal row without its completion.settling: dict = field(default_factory=dict)+ # ``(revision key, payload)`` of the last ``session.context_breakdown``: Desktop re-polls it on+ # every idle mount and reconnect, and a rebuild walks the whole stored transcript (#140667).+ context_breakdown: tuple | None = None# A viewer attached while a guarded deferred pick awaits consent: the next boundary asks again.reask_pick: bool = False# A historic messaging row mounted for reading after its lane moved on (``/new``): ``route`` isdiff --git a/gateway/session_tui_reads.py b/gateway/session_tui_reads.pyindex 6ec610044d4..a5b7b1399bc 100644--- a/gateway/session_tui_reads.py+++ b/gateway/session_tui_reads.py@@ -163,8 +163,25 @@ async def context_breakdown(connection, ref, params) -> dict:from agent.context_breakdown import compute_session_context_breakdownfrom agent.context_file_sources import context_file_sources_for_agentwith owner_scope(authority), _session_workspace(authority.runner, live.source):+ # A revision probe is two indexed reads; the rebuild walks the whole transcript and the+ # system prompt. Reuse the payload while neither the transcript nor the agent moved.+ key = (id(agent), _agent_context_marks(agent),+ authority.db.conversation_revision(stored, include_ancestors=True))+ cached = live.context_breakdown+ if cached is not None and cached[0] == key:+ return dict(cached[1])conversation = authority.db.get_messages_as_conversation(stored, include_ancestors=True)payload = compute_session_context_breakdown(agent, conversation)payload['context_files'] = context_file_sources_for_agent(agent)- return payload+ live.context_breakdown = (key, payload)+ return dict(payload)return await asyncio.to_thread(build)+++def _agent_context_marks(agent) -> tuple:+ """What the breakdown reads off the agent besides the transcript: model, toolset size, window,+ measured prompt tokens, compressions and the usage anchors."""+ comp = getattr(agent, 'context_compressor', None)+ return (getattr(agent, 'model', None), getattr(agent, 'provider', None), len(getattr(agent, 'tools', None) or ()),+ *(getattr(comp, name, None) for name in ('context_length', 'last_prompt_tokens', 'compression_count')),+ repr(getattr(agent, '_turn_base_usage_anchor', None)), repr(getattr(agent, '_usage_anchor', None)))diff --git a/hermes_state_messages.py b/hermes_state_messages.pyindex 299e8b59d46..172dfb0d14f 100644--- a/hermes_state_messages.py+++ b/hermes_state_messages.py@@ -1532,6 +1532,16 @@ class SessionMessagesMixin(SessionDetachedResultsMixin):repair_alternation=repair_alternation, include_row_ids=include_row_ids,include_summary_markers=repair_alternation)+ def conversation_revision(self, session_id: str, include_ancestors: bool = False) -> tuple:+ """A cheap fingerprint of what ``get_messages_as_conversation`` (live rows) would load: the+ lineage ids plus the active rows' count and highest id. Any append, rewind, undo or compaction+ moves it, so a payload derived from the transcript can be reused while it holds."""+ ids = self._resume_lineage_ids(session_id) if include_ancestors else [session_id]+ row = self._read_one(+ f"SELECT COUNT(*), COALESCE(MAX(id), 0) FROM messages WHERE session_id IN ({_placeholders(ids)})"+ " AND active = 1", tuple(ids))+ return (tuple(ids), int(row[0]), int(row[1]))+def _dedupe_replayed_user(self, messages, msg, exact_user_clones) -> tuple[bool, Any]:"""Ancestor-lineage dedupe of one decoded user *msg* -> ``(skip, exact_clone_key)``. Rotationcolumn-clones the concurrent tail into the child, so copies need not be adjacent: the exactdiff --git a/tests/gateway/test_tui_session_reads.py b/tests/gateway/test_tui_session_reads.pyindex c63b92a8b66..663213b8a6c 100644--- a/tests/gateway/test_tui_session_reads.py+++ b/tests/gateway/test_tui_session_reads.py@@ -137,3 +137,35 @@ async def test_context_breakdown_verb_serves_the_owner_agents_categories(owner,monkeypatch.setattr(context_file_sources, 'context_file_sources_for_agent', lambda a: [])live = await owner.call('session.context_breakdown', session_id=sid)assert live['categories'][0]['tokens'] == 3000 and live['model'] == 'fake-model', live+++async def test_context_breakdown_is_reused_until_the_transcript_moves(owner, monkeypatch):+ """#140667: Desktop re-polls ``session.context_breakdown`` on every idle mount and reconnect,+ and each poll rebuilt the prompt over the whole stored transcript (seconds on a 9,501-row+ session). The owner reuses the payload while the transcript revision is unchanged and+ recomputes once a row lands."""+ sid = (await owner.call('session.create', request_id='r', source='cli', cwd=str(owner.workdir)))['session_id']+ from agent import context_breakdown, context_file_sources+ agent = SimpleNamespace(model='fake-model', provider='custom')+ owner.authority.runner._cached_agent_for = lambda route: agent+ computed = []++ def compute(a, messages):+ computed.append(len(messages))+ return {'categories': [{'id': 'conversation', 'label': 'Conversation', 'tokens': len(messages),+ 'color': '#fff'}], 'context_max': 128000, 'context_used': 1,+ 'context_percent': 0, 'estimated_total': len(messages), 'context_estimated': True,+ 'context_source': 'local_estimate', 'model': 'fake-model'}+ monkeypatch.setattr(context_breakdown, 'compute_session_context_breakdown', compute)+ monkeypatch.setattr(context_file_sources, 'context_file_sources_for_agent', lambda a: [])+ first = await owner.call('session.context_breakdown', session_id=sid)+ again = await owner.call('session.context_breakdown', session_id=sid)+ assert computed == [0] and again == first+ from gateway.session_tui_reads import _stored_id+ stored = _stored_id(owner.authority, SimpleNamespace(session_id=sid))+ owner.authority.db.append_message(stored, 'user', 'hello')+ moved = await owner.call('session.context_breakdown', session_id=sid)+ assert computed == [0, 1] and moved['estimated_total'] == 1+ await owner.call('session.context_breakdown', session_id=sid)+ assert computed == [0, 1]