Skip to content

The agent that grows with you

fix(gateway): a local viewer's turn end republishes the agent's banner info

Teknium3429015b2a90
diff --git a/gateway/run_turn_runner.py b/gateway/run_turn_runner.py
index 5654879e2ce..ea334779f65 100644
--- a/gateway/run_turn_runner.py
+++ b/gateway/run_turn_runner.py
@@ -1258,6 +1258,8 @@ class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):
local_viewer_events.restore(agent, local_sinks)
from gateway.session_usage import publish_usage
publish_usage(self, agent, record=True) # turn end: the final API call's counters
+ from gateway.session_local_inventory import publish_settled_info
+ publish_settled_info(self, agent)
persisted_turn = _persisted_turn(agent, agent_history, result, compressions)
self._finish_stream_consumer(result, agent_history, stream_consumer)
if _finite_viewer_turn() and getattr(agent, "_codex_session", None) is not None:
diff --git a/gateway/session_local_inventory.py b/gateway/session_local_inventory.py
index a35a5da7cb9..bfbe93e1705 100644
--- a/gateway/session_local_inventory.py
+++ b/gateway/session_local_inventory.py
@@ -188,3 +188,35 @@ async def snapshot_info(authority, ref):
# A lazy session: the tools its first turn loads (the banner's own count).
info['tool_count'] = sum(len(names) for names in (inventory.get('tools') or {}).values())
return {**inventory, **info}
+
+
+def agent_inventory(agent) -> dict:
+ """The banner fields only a built agent knows (main's ``_session_info`` read them off it): the
+ tools it loaded, its cached system prompt (the ``▸ System Prompt`` row), provider and context
+ length. Cheap attribute reads, safe on the agent thread at turn end."""
+ info: dict = {}
+ with contextlib.suppress(Exception):
+ from agent.voice_turn_route import session_runtime_view
+ info["provider"] = getattr(session_runtime_view(agent), "provider", "") or ""
+ with contextlib.suppress(Exception):
+ info["tools"] = _grouped(getattr(agent, "tools", None))
+ info["system_prompt"] = getattr(agent, "_cached_system_prompt", "") or ""
+ compressor = getattr(agent, "context_compressor", None)
+ if compressor is not None and getattr(compressor, "context_length", None):
+ info["context_length"] = compressor.context_length
+ return info
+
+
+def publish_settled_info(turn, agent) -> None:
+ """Main's end-of-turn ``session.info`` (``session_workdir._emit_settled_session_info``) for a local
+ viewer: the first turn builds the agent, so this is when the banner's ``▸ System Prompt`` row, the
+ agent's tool list and provider first become known. Never breaks the turn."""
+ if agent is None:
+ return
+ with contextlib.suppress(Exception):
+ from gateway.session_contract import SessionRef
+ from gateway.session_local import local_session_info
+ authority, session_id, _generation = turn._approval_owner
+ info = {**agent_inventory(agent),
+ **local_session_info(authority, SessionRef(authority.profile_id, session_id))}
+ turn._publish_execution("session.info", info)
diff --git a/tests/gateway/test_local_viewer_usage.py b/tests/gateway/test_local_viewer_usage.py
index 68ba4c73c47..83688c3b905 100644
--- a/tests/gateway/test_local_viewer_usage.py
+++ b/tests/gateway/test_local_viewer_usage.py
@@ -120,3 +120,27 @@ def test_local_viewer_turn_end_records_stats_for_the_completion():
finally:
execution_result.reset(token)
assert captured['session_usage'] == published[-1][1]
+
+
+def test_turn_end_publishes_the_agents_banner_info():
+ """Main re-emitted ``session.info`` at every turn end; the first turn's carries the built agent's
+ ``system_prompt`` (the banner's ``▸ System Prompt — N chars`` row, FINDINGS row 2) and tools."""
+ from gateway.session_local_inventory import publish_settled_info
+ published = []
+ agent = _agent()
+ agent._cached_system_prompt = 'You are Hermes.' * 10
+ agent.tools = [{'function': {'name': 'terminal'}}]
+ turn = _turn(Platform.LOCAL, published, agent)
+ turn._approval_owner = (SimpleNamespace(profile_id='p'), 's', 1)
+ from gateway import session_local
+ original = session_local.local_session_info
+ session_local.local_session_info = lambda authority, ref: {'model': 'fake-model', 'lazy': False}
+ try:
+ publish_settled_info(turn, agent)
+ finally:
+ session_local.local_session_info = original
+ kind, info = published[-1]
+ assert kind == 'session.info'
+ assert info['system_prompt'] == agent._cached_system_prompt
+ assert info['tools'] == {'terminal': ['terminal']} and info['context_length'] == 128000
+ assert info['model'] == 'fake-model'