Skip to content

The agent that grows with you

fix(gateway): queue during restart_wait only for the interactive WS

Teknium5f797bbd71fe
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index 17e3ec7d43d..3da20636edd 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -431,17 +431,22 @@ class SessionAuthority:
results[sid] = exc.reason
return results
- async def submit(self, actor: Principal, request: Submission, *, _authorize_write=None):
+ async def submit(self, actor: Principal, request: Submission, *, _authorize_write=None, queue_while_restarting=False):
+ """``queue_while_restarting``: the caller is an interactive client attached to a restart's
+ after-turn wait, which commits the input queued for the next owner. Machine callers (a2a,
+ kanban, hosted rooms) keep the plain ``runtime_draining`` refusal and retry against the next
+ owner with their own stable request id; none of them reads a released waiter as queued."""
self.authorize(actor, request.ref, 'session:submit')
async with self.sessions[request.ref.session_id].mutation_lock:
# Deletion can retire this session while the caller waits on its writer.
self.authorize(actor, request.ref, 'session:submit')
from gateway.session_ingress_media import capture_lease
with capture_lease(): # captured bytes are held from capture until commit or refusal
- return await self._submit(actor, request, _authorize_write=_authorize_write)
+ return await self._submit(actor, request, _authorize_write=_authorize_write,
+ queue_only=queue_while_restarting is True)
- async def _submit(self, actor, request, *, _authorize_write=None):
- self._require_admission_open(queue_only=True)
+ async def _submit(self, actor, request, *, _authorize_write=None, queue_only=False):
+ self._require_admission_open(queue_only=queue_only)
if (request.intent != 'queue' or not {'text'} <= set(request.payload) <= {
'text', 'attachments', 'finite', 'unattended', 'surface', 'voice_context', 'interrupted',
'voice_turn', 'display_kind', 'title_preview', 'turn_author', 'api_text', 'model_override', 'clarify_batch'}
@@ -476,7 +481,7 @@ class SessionAuthority:
return admit_session_input(self.db, epoch=self.epoch, principal_id=actor.subject,
session_id=request.ref.session_id, request_id=request.request_id,
payload=payload, intent=request.intent,
- _authorize_write=self._admission_gate(_authorize_write, queue_only=True))
+ _authorize_write=self._admission_gate(_authorize_write, queue_only=queue_only))
except Exception:
self._release_refused_capture(request, payload)
raise
diff --git a/gateway/session_controls.py b/gateway/session_controls.py
index b12525d6d2e..80d322f8762 100644
--- a/gateway/session_controls.py
+++ b/gateway/session_controls.py
@@ -581,7 +581,9 @@ class AuthorityConnection:
**submit_display_fields(params), **submit_cli_turn_fields(params)}
if 'attachments' in params:
payload['attachments'] = params['attachments']
- receipt = await self.authority.submit(self.actor, Submission(request_id, ref, payload, 'queue'))
+ # The interactive WS is the one surface a restart's after-turn wait admits (#139041).
+ receipt = await self.authority.submit(self.actor, Submission(request_id, ref, payload, 'queue'),
+ queue_while_restarting=self._operator is True)
return asdict(receipt)
async def mutate(self, ref, params):
diff --git a/tests/gateway/test_restart_wait_prompt_reach.py b/tests/gateway/test_restart_wait_prompt_reach.py
index da6ef2b7ade..543e28f79e5 100644
--- a/tests/gateway/test_restart_wait_prompt_reach.py
+++ b/tests/gateway/test_restart_wait_prompt_reach.py
@@ -64,7 +64,8 @@ async def test_restart_wait_commits_new_input_queued_and_never_claims_it(tmp_pat
authority.sessions["s"] = LiveSession(SessionSource(platform=Platform.TELEGRAM, chat_id="c", user_id="human"), "route")
actor = Principal("human", "p", frozenset({"session:submit"}), "t")
try:
- receipt = await authority.submit(actor, Submission("r1", SessionRef("p", "s"), {"text": "next"}, "queue"))
+ receipt = await authority.submit(actor, Submission("r1", SessionRef("p", "s"), {"text": "next"}, "queue"),
+ queue_while_restarting=True)
task = authority.sessions["s"].task
if task is not None:
await asyncio.wait_for(task, 10)
@@ -74,7 +75,8 @@ async def test_restart_wait_commits_new_input_queued_and_never_claims_it(tmp_pat
# stop() withdraws the window: input is refused, as before.
del runner.session_runtime_descriptor["drain_reason"]
with pytest.raises(RuntimeStoreError, match="runtime_draining"):
- await authority.submit(actor, Submission("r2", SessionRef("p", "s"), {"text": "late"}, "queue"))
+ await authority.submit(actor, Submission("r2", SessionRef("p", "s"), {"text": "late"}, "queue"),
+ queue_while_restarting=True)
finally:
db.close()
@@ -153,3 +155,30 @@ async def test_restart_wait_ws_serves_the_parked_turn_and_refuses_every_other_ve
await viewer.close()
db.close()
+
+async def test_restart_wait_queues_only_interactive_input(tmp_path, monkeypatch):
+ """Only the interactive WS submit commits queued input during the wait; a2a, kanban and hosted
+ rooms call ``authority.submit`` directly and keep the plain refusal, so none of them can read a
+ released waiter as failure and resubmit the same input under a new request id."""
+ from gateway.config import Platform
+ from gateway.session import SessionSource
+ from gateway.session_authority import LiveSession, SessionAuthority
+ from gateway.session_contract import Principal, SessionRef, Submission
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ db = SessionDB(tmp_path / "state.db")
+ db.create_session("s", source="telegram")
+ runner = SimpleNamespace(_draining=True, _running=True, config=SimpleNamespace(multiplex_profiles=False),
+ session_runtime_descriptor={"state": "draining", "drain_reason": "restart_wait"},
+ _adapter_for_source=lambda source: None, _handle_message=None)
+ authority = SessionAuthority(runner, profile_id="p", instance_id="owner", db=db,
+ epoch=begin_runtime_epoch(db, instance_id="owner"))
+ authority.sessions["s"] = LiveSession(SessionSource(platform=Platform.TELEGRAM, chat_id="c", user_id="peer"), "route")
+ peer = Principal("peer", "p", frozenset({"session:submit"}), "t")
+ try:
+ with pytest.raises(RuntimeStoreError, match="runtime_draining"):
+ await authority.submit(peer, Submission("a2a:1", SessionRef("p", "s"), {"text": "hi"}, "queue"))
+ assert list_session_admissions(db, session_id="s") == []
+ finally:
+ db.close()