The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): let profile retirement stop a running /btw
Tekniumf17a567ac935
diff --git a/agent/side_question.py b/agent/side_question.pyindex 735afb62891..e84640abd26 100644--- a/agent/side_question.py+++ b/agent/side_question.py@@ -9,7 +9,7 @@ the agent): a rendered transcript through :func:`agent.oneshot.run_oneshot`."""import logging-from typing import Any, Dict, List, Optional+from typing import Any, Callable, Dict, List, Optionalfrom agent.background_review import _msg_text@@ -99,11 +99,14 @@ def _side_question_task_config() -> dict[str, Any]:return task if isinstance(task, dict) else {}-def _answer_via_fork(parent_agent: Any, question: str, history: Optional[list[dict[str, Any]]]) -> str:+def _answer_via_fork(parent_agent: Any, question: str, history: Optional[list[dict[str, Any]]],+ on_fork: Optional[Callable[[Any], None]] = None) -> str:"""Answer via a cache-parity fork of ``parent_agent`` on the calling thread.An empty thread-scoped tool whitelist denies every tool call at dispatch: ``tools[]``stays byte-identical for cache parity, but the side question can never mutate anything.+ ``on_fork(fork)`` publishes the built fork so an owner can interrupt it, and+ ``on_fork(None)`` follows its teardown."""from agent.background_review import (_digest_history, _record_review_usage_to_parent, _snapshot_review_usage, build_cache_parity_fork,@@ -112,6 +115,8 @@ def _answer_via_fork(parent_agent: Any, question: str, history: Optional[list[difork, _rt, routed = build_cache_parity_fork(parent_agent, _side_question_task_config(),max_iterations=_FORK_MAX_ITERATIONS, write_origin="side_question")+ if on_fork is not None:+ on_fork(fork)try:set_thread_tool_whitelist(set(), deny_msg_fmt=("Side question (/btw) denied tool call: {tool_name}. "@@ -132,6 +137,8 @@ def _answer_via_fork(parent_agent: Any, question: str, history: Optional[list[distep()except Exception:pass+ if on_fork is not None:+ on_fork(None)def _answer_via_oneshot(question: str, history: Optional[list[dict[str, Any]]], **run_kwargs: Any) -> str:@@ -148,22 +155,29 @@ def _answer_via_oneshot(question: str, history: Optional[list[dict[str, Any]]],def answer_side_question(question: str, history: Optional[list[dict[str, Any]]], *, parent_agent: Any = None,main_runtime: Optional[dict[str, Any]] = None, max_tokens: int = 2048, temperature: Optional[float] = 0.3,- timeout: float = 180.0,+ timeout: float = 180.0, on_fork: Optional[Callable[[Any], None]] = None,+ should_stop: Optional[Callable[[], bool]] = None,) -> str:"""Fork when ``parent_agent`` is live, else (or on empty answer / failure) the one-shot- digest. Raises on failure — callers surface the error on their own UI."""+ digest. Raises on failure — callers surface the error on their own UI.++ ``on_fork`` receives the fork once built (and ``None`` after its teardown) so the caller can+ interrupt it; ``should_stop()`` true after the fork skips the one-shot fallback."""question = (question or "").strip()if not question:raise ValueError("answer_side_question requires a non-empty question")if parent_agent is not None:try:- answer = _answer_via_fork(parent_agent, question, history)+ answer = (_answer_via_fork(parent_agent, question, history, on_fork) if on_fork is not None+ else _answer_via_fork(parent_agent, question, history))if answer:return answerlogger.warning("/btw fork returned an empty answer; falling back to one-shot")except Exception:logger.warning("/btw cache-parity fork failed; falling back to one-shot", exc_info=True)+ if should_stop is not None and should_stop():+ raise RuntimeError("side question stopped")return _answer_via_oneshot(question, history, main_runtime=main_runtime, max_tokens=max_tokens,temperature=temperature, timeout=timeout)diff --git a/gateway/session_runtime_workers.py b/gateway/session_runtime_workers.pyindex 4b949c01c35..841e6272610 100644--- a/gateway/session_runtime_workers.py+++ b/gateway/session_runtime_workers.py@@ -25,8 +25,11 @@ def uncounted_runtime_work(runner):def side_agents(authority):"""``(thread, holder)`` for each detached side agent (/bg, /btw, preview restart) whose thread- has not unwound (``gateway/session_tui_side.py`` ``_spawn``)."""- return list(getattr(authority, '_side_agents', {}).items())+ has not unwound (``gateway/session_tui_side.py`` ``_spawn``). A stopped ``/btw`` whose fork+ is gone (``_SideQuestionStop.abandoned``) is no longer owner work."""+ from gateway.session_tui_side import _abandoned+ return [(thread, holder) for thread, holder in list(getattr(authority, '_side_agents', {}).items())+ if not _abandoned(holder)]def persist_work_count(authority, _task=None):diff --git a/gateway/session_tui_side.py b/gateway/session_tui_side.pyindex e286b020aee..88764aec469 100644--- a/gateway/session_tui_side.py+++ b/gateway/session_tui_side.py@@ -105,6 +105,11 @@ def _side_agent(authority, policy, live, task_id, **overrides):return AIAgent(**kwargs)+def _abandoned(holder):+ abandoned = getattr(holder[0], 'abandoned', None) if holder else None+ return bool(abandoned and abandoned())++def _spawn(authority, ref, task_id, event_type, body, extra=None, holder=None):"""Run ``body()`` on a daemon thread; publish its text (or ``error: <exc>``) on the session.@@ -132,7 +137,7 @@ def _spawn(authority, ref, task_id, event_type, body, extra=None, holder=None):logger.warning('Side agent %s failed', task_id, exc_info=True)text = f'error: {getattr(exc, "reason", None) or exc}'live = authority.sessions.get(ref.session_id)- if live is not None:+ if live is not None and not _abandoned(holder):live.event_stream.publish(ref.session_id, {'task_id': task_id, **extra, 'text': str(text or '')},event_type=event_type)finally:@@ -186,6 +191,8 @@ async def btw(connection, ref, params):from gateway.session_local_recovery import transcript_targettarget = None if snapshot else transcript_target(authority, ref)+ handle = _SideQuestionStop()+def body():from agent.side_question import answer_side_questionhistory = snapshot or authority.db.get_messages_as_conversation(target)@@ -193,9 +200,40 @@ async def btw(connection, ref, params):model, runtime = _resolve_runtime(authority, policy)main_runtime = {'model': model, 'session_id': ref.session_id,**{key: runtime.get(key) for key in ('provider', 'base_url', 'api_key', 'api_mode')}}- return answer_side_question(text, history, parent_agent=agent, main_runtime=main_runtime) or ''-- return _spawn(authority, ref, task_id, 'btw.complete', body, extra={'question': text})+ return answer_side_question(text, history, parent_agent=agent, main_runtime=main_runtime,+ on_fork=handle.set_fork, should_stop=handle.stopped.is_set) or ''++ return _spawn(authority, ref, task_id, 'btw.complete', body, extra={'question': text}, holder=[handle])+++class _SideQuestionStop:+ """``/btw``'s holder: retirement and shutdown interrupt its cache-parity fork, and once stopped+ the read-only one-shot fallback (an uncancellable aux call of up to 180 s that writes nothing)+ no longer holds owner work, so it cannot keep a retiring profile's reservation."""++ def __init__(self):+ self.stopped = threading.Event()+ self._lock = threading.Lock()+ self._fork = None++ def set_fork(self, fork):+ with self._lock:+ self._fork = fork+ if fork is not None and self.stopped.is_set():+ self.hard_interrupt('Profile runtime is stopping')++ def hard_interrupt(self, message=None):+ self.stopped.set()+ with self._lock:+ fork = self._fork+ if fork is not None:+ from agent.interrupt_compat import request_hard_interrupt+ request_hard_interrupt(fork, message)++ def abandoned(self):+ """Stopped with no fork running: what is left publishes nothing and writes nothing."""+ with self._lock:+ return self.stopped.is_set() and self._fork is Noneasync def preview_restart(connection, ref, params):diff --git a/tests/gateway/test_runtime_worker_shutdown.py b/tests/gateway/test_runtime_worker_shutdown.pyindex bdc145ed2d9..ad6ba3cd3f5 100644--- a/tests/gateway/test_runtime_worker_shutdown.py+++ b/tests/gateway/test_runtime_worker_shutdown.py@@ -239,3 +239,61 @@ async def test_side_agent_thread_killed_by_a_base_exception_releases_owner_work(assert not threads[0].is_alive() and failures == [exit_type]assert uncounted_runtime_work(runner) == 0 and (not persisted or persisted[-1] == 0)await join_authority_work(authority, 0)++[email protected]('path', ['fork', 'oneshot'])+async def test_btw_side_question_does_not_hold_profile_retirement(monkeypatch, path):+ """``/btw`` is counted owner work (``_spawn``), so retirement must be able to stop it too.++ Its fork is interrupted through the holder; the one-shot fallback (``agent/side_question.py``,+ ``timeout=180.0``) is an uncancellable aux call that never mutates anything, so retirement+ abandons it instead of keeping the profile's reservation for the full settle window."""+ import contextlib+ from gateway import session_tui_side+ from gateway.session_contract import SessionRef+ from gateway.session_runtime_workers import join_authority_work, uncounted_runtime_work+ from agent import side_question++ started, release, interrupts, events = threading.Event(), threading.Event(), [], []++ class Fork:+ def hard_interrupt(self, message=None):+ interrupts.append(message)+ release.set()++ def answer(question, history, *, parent_agent=None, main_runtime=None, on_fork=None, should_stop=None):+ if path == 'fork':+ on_fork(Fork())+ started.set()+ release.wait(30) # interrupted through the holder+ on_fork(None) # _answer_via_fork's teardown+ return 'interrupted'+ started.set()+ release.wait(30) # the 180 s one-shot aux call, which nothing can interrupt+ return 'late answer'++ live = SimpleNamespace(task=None, event_stream=SimpleNamespace(execution={},+ publish=lambda sid, payload, event_type: events.append((event_type, payload))))+ authority = SimpleNamespace(sessions={'s': live}, pending_stops={}, adopted={},+ agent=lambda ref: SimpleNamespace(_session_messages=[{'role': 'user', 'content': 'hi'}]))+ runner = SimpleNamespace(_running_agents={}, session_authority=authority, _persist_active_agents=lambda: None)+ authority.runner = runner+ monkeypatch.setattr(session_tui_side, '_admit', lambda *_: (None, None, 'why?', 'btw_x'))+ monkeypatch.setattr(session_tui_side, '_side_scope', lambda *_: contextlib.nullcontext())+ monkeypatch.setattr(session_tui_side, '_resolve_runtime', lambda *_: ('m', {}))+ monkeypatch.setattr(side_question, 'answer_side_question', answer)+ try:+ await session_tui_side.btw(SimpleNamespace(authority=authority), SessionRef('p', 's'), {})+ assert await asyncio.to_thread(started.wait, 5)+ thread = next(t for t in threading.enumerate() if t.name == 'side-agent-btw_x')+ assert uncounted_runtime_work(runner) == 1+ await join_authority_work(authority, 5)+ assert uncounted_runtime_work(runner) == 0+ if path == 'fork':+ assert interrupts and set(interrupts) == {'Profile runtime is stopping'}+ release.set()+ await asyncio.to_thread(thread.join, 5)+ assert not thread.is_alive() and events == [] # a stopped /btw publishes no late answer+ finally:+ release.set()