refactor(gateway): extract run_sync onto TurnRunner (completes the TurnContext seam; AST-identical body)

This commit is contained in:
teknium1 2026-07-29 15:37:50 -07:00 committed by Teknium
parent 4b7f709843
commit 1a3a9de630
3 changed files with 1548 additions and 1416 deletions

File diff suppressed because it is too large Load diff

View file

@ -64,3 +64,66 @@ class TurnContext:
# send_progress_messages is scheduled) ----------------------------
_progress_metadata: Optional[dict] = None
_progress_reply_to: Optional[Any] = None
# ------------------------------------------------------------------
# run_sync extraction (second wave of the seam): the closed-over locals
# of ``_run_agent_inner`` that ``run_sync`` (and the four sibling bridge
# callbacks) captured. Same rules as above: read-only snapshots or
# shared mutable containers; ``message`` is the ONE exception — the old
# closure rebound it via ``nonlocal``, so the rebind sites now write
# ``ctx.message`` and the outer body reads ``ctx.message`` afterwards.
# ------------------------------------------------------------------
# --- the ex-``nonlocal`` turn message (rebindable) --------------------
message: Optional[str] = None
# --- turn parameters / config snapshots (read-only in run_sync) -------
history: Any = None
context_prompt: Optional[str] = None
channel_prompt: Optional[str] = None
session_id: Optional[str] = None
session_key: Optional[str] = None
run_generation: Optional[int] = None
_interrupt_depth: int = 0
event_message_id: Optional[str] = None
moa_config: Optional[dict] = None
persist_user_message: Optional[Any] = None
persist_user_timestamp: Optional[float] = None
user_config: Any = None
enabled_toolsets: Any = None
disabled_toolsets: Any = None
log_mode_enabled: bool = False
interim_assistant_messages_enabled: bool = False
needs_progress_queue: bool = False
# --- lazy-imported callables captured from the outer body -------------
AIAgent: Any = None
resolve_display_setting: Any = None
# --- mutable holder cells (shared-list pattern; outer body + the
# post-executor closures read mutations through the same objects) --
result_holder: list = field(default_factory=lambda: [None])
tools_holder: list = field(default_factory=lambda: [None])
stream_consumer_holder: list = field(default_factory=lambda: [None])
streaming_tts_consumer_holder: list = field(default_factory=lambda: [None])
# --- voice-ack wiring --------------------------------------------------
_voice_ack_fired: list = field(default_factory=lambda: [False])
_voice_ack_guild: list = field(default_factory=lambda: [None])
_voice_ack_loop: Any = None
# --- hook / status bridge wiring (published at original binding sites) -
_loop_for_step: Any = None
_hooks_ref: Any = None
_status_adapter: Any = None
_status_chat_id: Any = None
_status_thread_metadata: Optional[dict] = None
# --- extracted sibling callbacks (bound TurnRunner methods; run_sync
# reads them through the ctx exactly where it used to close over
# the sibling closures) ---------------------------------------------
progress_callback: Optional[Callable] = None
voice_ack_callback: Optional[Callable] = None
_step_callback_sync: Optional[Callable] = None
_event_callback_sync: Optional[Callable] = None
_status_callback_sync: Optional[Callable] = None

View file

@ -80,13 +80,23 @@ def test_background_and_main_agent_paths_call_refresh():
source = (
Path(__file__).resolve().parent.parent.parent / "gateway" / "run.py"
).read_text(encoding="utf-8")
assert "fallback_model=self._refresh_fallback_model()" in source
assert source.count("fallback_model=self._refresh_fallback_model()") >= 2
# The agent-construction site inside TurnRunner.run_sync (extracted from
# the old _run_agent_inner closure) references the runner as
# ``self._runner``; the background-agent site still uses bare ``self``.
_refresh_calls = (
source.count("fallback_model=self._refresh_fallback_model()")
+ source.count("fallback_model=self._runner._refresh_fallback_model()")
)
assert _refresh_calls >= 2
# The cached-agent reuse path (the load-bearing fix for a long-lived
# session in a running gateway) must apply the refreshed chain.
assert "self._apply_fallback_chain_to_agent(" in source
assert (
"self._apply_fallback_chain_to_agent(" in source
or "self._runner._apply_fallback_chain_to_agent(" in source
)
# The stale startup-snapshot form must not remain at create sites.
assert "fallback_model=self._fallback_model," not in source
assert "fallback_model=self._runner._fallback_model," not in source
def test_load_fallback_model_static_unchanged_contract(tmp_path, monkeypatch):