The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): carry turn-end session_usage and completion metadata to local viewers
yoniebans7acb3070aa76
diff --git a/gateway/run_turn_runner.py b/gateway/run_turn_runner.pyindex d1483a4f798..42448b087c2 100644--- a/gateway/run_turn_runner.py+++ b/gateway/run_turn_runner.py@@ -1315,6 +1315,13 @@ class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):**usage,**({"persisted_turn": persisted_turn} if persisted_turn else {}),}+ was_delivered = getattr(agent, "_interim_text_was_delivered", None)+ previewed = bool(result.get("response_previewed") or (+ callable(was_delivered) and bool(final_response) and was_delivered(final_response) is True))+ viewer_meta = {+ **({"billing_block": result["billing_block"]} if isinstance(result.get("billing_block"), dict) else {}),+ **({"warning": result["warning"]} if isinstance(result.get("warning"), str) and result["warning"] else {}),+ }if not final_response:final_response = _normalize_empty_agent_response(result, final_response or "", history_len=len(agent_history))final_response = _sanitize_gateway_final_response(ctx.source.platform, final_response)@@ -1322,13 +1329,19 @@ class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):final_response = t("gateway.shared.warn_passthrough", error=result["error"]) if result.get("error") else ""# NOTE: deliberately omits agent_persisted/last_reasoning/response_* — the caller# defaults agent_persisted differently when the key is absent.+ if local_sinks is not None:+ return {+ "final_response": final_response, **common, **viewer_meta,+ "response_previewed": previewed,+ "response_transformed": bool(result.get("response_transformed")),+ }return {"final_response": final_response, **common}final_response = self._append_auto_media_tags(final_response, result, agent_history, history_media_paths)# Auto-titling runs at TURN START (agent/turn_context.py) from the user's message alone, so a# failed/interrupted turn is still titled.return {- "final_response": final_response, "last_reasoning": result.get("last_reasoning"), **common,- "response_previewed": result.get("response_previewed", False),+ "final_response": final_response, "last_reasoning": result.get("last_reasoning"), **common, **viewer_meta,+ "response_previewed": previewed,"response_transformed": result.get("response_transformed", False),"response_reused": result.get("response_reused", False),# Lets the persistence block tell whether the codex app-server path self-persisted (itdiff --git a/gateway/session_ingress.py b/gateway/session_ingress.pyindex d4d656f41e0..b2efffe1dfd 100644--- a/gateway/session_ingress.py+++ b/gateway/session_ingress.py@@ -272,7 +272,9 @@ async def execute_admission(authority, ref, row):'error': response or 'The admitted turn failed.'}# The drain commits this under the stream lock so no viewer reads `terminal`# before the completion event exists in the replay ring.- authority.pending_results[row['admission_id']] = {'result': result, 'usage': captured.get('usage', {})}+ authority.pending_results[row['admission_id']] = {+ 'result': result, 'usage': captured.get('usage', {}),+ **({'session_usage': captured['session_usage']} if 'session_usage' in captured else {})}if not is_api and response:adapter = authority.runner._adapter_for_source(event.source)if adapter is not None:diff --git a/gateway/session_settlement_recovery.py b/gateway/session_settlement_recovery.pyindex 54f8989bf9a..f61a0c89e1f 100644--- a/gateway/session_settlement_recovery.py+++ b/gateway/session_settlement_recovery.py@@ -49,6 +49,16 @@ def completion_payload(row, settled, response, captured):captured_result = (captured or {}).get('result') or {}if response and captured_result.get('response_reused'):complete['response_reused'] = True+ if captured_result.get('response_previewed'):+ complete['response_previewed'] = True+ if captured_result.get('response_transformed'):+ complete['response_transformed'] = True+ if isinstance(captured_result.get('warning'), str) and captured_result['warning']:+ complete['warning'] = captured_result['warning']+ if isinstance(captured_result.get('billing_block'), dict):+ complete['billing'] = captured_result['billing_block']+ if isinstance(captured_result.get('failure_reason'), str) and captured_result['failure_reason']:+ complete['failure_reason'] = captured_result['failure_reason']# The committed row addresses of the turn: a viewer binds the streamed reply to# its stored row, so a transcript read racing this frame never paints it twice.# ``submission_id`` names whose turn it is: the sending viewer binds its optimistic