Skip to content

The agent that grows with you

fix(gateway): model-only tip read and prompt-aware breakdown cache

unsupportedpastels6b11a8eca54e
diff --git a/gateway/session_tui_reads.py b/gateway/session_tui_reads.py
index fc16f1ee14b..dddf5c514ae 100644
--- a/gateway/session_tui_reads.py
+++ b/gateway/session_tui_reads.py
@@ -43,6 +43,13 @@ def _context_id(authority, ref) -> str:
return authority.db.get_compression_tip(stored) or stored
+def _model_history(db, tip) -> list:
+ """The tip's working context: its active rows as live replay loads them, minus the ``session_meta``
+ bookkeeping the gateway strips before the model sees history. Superseded segments are not read."""
+ return [m for m in db.get_messages_as_conversation(tip, repair_alternation=True, include_row_ids=True)
+ if m['role'] != 'session_meta']
+
+
def _status(authority, ref, live, policy, agent, arg) -> str:
"""``tui_gateway/methods_session.py`` ``session.status`` on main, for the owner's session."""
from hermes_cli.status_report import build_status_fields, status_lines
@@ -84,7 +91,7 @@ def _context(authority, ref, live, policy, agent, arg) -> str:
with contextlib.suppress(Exception):
from agent.context_breakdown import (
compute_context_details, compute_session_context_breakdown, render_context_breakdown_lines)
- payload = compute_session_context_breakdown(agent, authority.db.get_resume_conversations(stored)[0])
+ payload = compute_session_context_breakdown(agent, _model_history(authority.db, stored))
details = None
if arg.strip().lower() in {'all', 'full', 'details'}:
with contextlib.suppress(Exception):
@@ -152,17 +159,19 @@ async def context_breakdown(connection, ref, params) -> dict:
from agent.context_file_sources import context_file_sources_for_agent
with owner_scope(authority), _session_workspace(authority.runner, live.source):
tip = _context_id(authority, ref)
+ # Agentless, the categories come from the frozen prompt and tools pin, which a compute worker
+ # can rewrite (session.prompt / session.tools) without moving the transcript.
+ row = {} if agent is not None else authority.db.get_session(tip) or {}
# 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(tip))
+ key = (id(agent), _agent_context_marks(agent), authority.db.conversation_revision(tip),
+ row.get('system_prompt'), row.get('tool_names'))
cached = live.context_breakdown
if cached is not None and cached[0] == key:
return dict(cached[1])
- # The display history keeps superseded compression segments; only the model projection
- # (the tip's active rows, no session_meta) is the working context.
- conversation = authority.db.get_resume_conversations(tip)[0]
+ conversation = _model_history(authority.db, tip)
if agent is None:
- payload = _stored_breakdown(authority.db.get_session(tip) or {}, conversation, usage)
+ payload = _stored_breakdown(row, conversation, usage)
else:
payload = compute_session_context_breakdown(agent, conversation)
payload['context_files'] = context_file_sources_for_agent(agent)
diff --git a/tests/gateway/test_tui_context_breakdown_scope.py b/tests/gateway/test_tui_context_breakdown_scope.py
index 34301d27482..78e235265a0 100644
--- a/tests/gateway/test_tui_context_breakdown_scope.py
+++ b/tests/gateway/test_tui_context_breakdown_scope.py
@@ -38,6 +38,26 @@ async def test_context_breakdown_without_a_resident_agent_estimates_the_stored_f
assert actual['estimated_total'] == expected['estimated_total']
+async def test_agentless_breakdown_refreshes_when_the_stored_prompt_or_tools_change(owner):
+ """A compute worker can freeze the prompt or tools pin without touching the transcript; the cached
+ agentless breakdown must not outlive either write."""
+ from gateway.session_tui_reads import _stored_id
+
+ sid = (await owner.call('session.create', request_id='refreeze', source='tui', cwd=str(owner.workdir)))['session_id']
+ db = owner.authority.db
+ stored = _stored_id(owner.authority, SimpleNamespace(session_id=sid))
+ db.append_message(stored, 'user', 'hello')
+ before = await owner.call('session.context_breakdown', session_id=sid)
+ db.update_system_prompt(stored, 'You are Hermes. ' * 50)
+ prompted = await owner.call('session.context_breakdown', session_id=sid)
+ assert prompted['estimated_total'] > before['estimated_total'], prompted
+ db.update_session_tool_names(stored, {'version': 'v', 'tools': [
+ {'type': 'function', 'function': {'name': 'terminal', 'parameters': {'type': 'object'}}}]})
+ tooled = await owner.call('session.context_breakdown', session_id=sid)
+ assert 'tool_definitions' in {c['id'] for c in tooled['categories']}, tooled
+
+
@pytest.mark.asyncio
async def test_context_breakdown_measures_only_the_compression_tips_model_history(owner, monkeypatch):
"""The runtime id still names the compressed parent; neither its superseded segment nor the tip's