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

Main's TUI re-emitted session.info at every turn end
(_emit_settled_session_info); the first one carried the freshly built agent's
_cached_system_prompt, which is what paints the banner's
`▸ System Prompt — N chars` row (branding.tsx renders it only when
info.system_prompt is set). The canonical owner sent the inventory only on
create/resume, before any agent existed, so the row never appeared
(FINDINGS row 2).

session_local_inventory.publish_settled_info: at a local viewer's turn end the
runner publishes local_session_info plus the built agent's system_prompt,
tools, provider and context_length (agent_inventory, cheap attribute reads).
Messaging turns are unchanged.

(cherry picked from commit abeee32537a5d59c08ebc60491ec796f4e186732)
(cherry picked from commit d4420fe80770a35d8d3b4bfe99442125557a1fa7)
This commit is contained in:
Teknium
2026-10-12 20:35:31 -07:00
parent 3d8ce33aa6
commit 3429015b2a
3 changed files with 58 additions and 0 deletions

View File

@@ -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:

View File

@@ -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)

View File

@@ -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'