Skip to content

The agent that grows with you

fix(tui): a forwarded prompt lost after send is unconfirmed, not "nothing was sent"

Tekniumc2c767cf32f9
diff --git a/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py b/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py
index 73aea35833e..cc3bf758103 100644
--- a/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py
+++ b/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py
@@ -213,6 +213,76 @@ def test_an_unconfirmed_forward_is_never_reported_as_not_sent(monkeypatch, tmp_p
db.close()
+def test_a_receipt_lost_after_the_owner_queued_it_is_unconfirmed_not_resendable(monkeypatch, tmp_path):
+ """The owner COMMITS the forwarded admission, then its socket drops before the receipt reaches
+ serve (a gateway restart mid-forward). The prompt sits in the owner's durable FIFO and survives
+ the restart, so "Nothing was sent. Send it again" would run a second copy: the answer is
+ SESSION_OWNER_UNCONFIRMED (check the chat, no Retry). Wire-level: a real loopback websockets
+ server runs the real ``AuthorityConnection``; the relay uses the real ``GatewayClient``."""
+ import json
+ from gateway.session_controls import AuthorityConnection
+ from hermes_cli.gateway_client import GatewayClient
+ from websockets.asyncio.client import connect
+ from websockets.asyncio.server import serve
+ from tui_gateway import owner_forward
+
+ db = SessionDB(db_path=tmp_path / "state.db")
+ sid, key, turns, _emitted = _legacy(monkeypatch, db)
+ try:
+ db.create_session(key, "test")
+ epoch = begin_runtime_epoch(db, instance_id="gateway")
+ admit_session_input(db, epoch=epoch, principal_id="bot", session_id=key, request_id="dm-1",
+ payload={"text": "a DM"})
+ claim_session_input(db, epoch=epoch, session_id=key)
+ authority = _owner(monkeypatch, db, key, epoch)
+ seen = []
+
+ @contextlib.asynccontextmanager
+ async def wire_owner(_home, _endpoint):
+ loop = asyncio.get_running_loop()
+
+ async def handler(ws):
+ class _Socket:
+ def write(self, frame):
+ loop.call_soon_threadsafe(lambda: asyncio.ensure_future(ws.send(json.dumps(frame))))
+ return True
+
+ def close(self):
+ return None
+ conn = AuthorityConnection(authority, _Socket(), {"user_id": "desktop"})
+ async for raw in ws:
+ request = json.loads(raw)
+ reply = await conn.dispatch(request)
+ seen.append(request["method"])
+ if request["method"] == "prompt.submit":
+ await ws.close() # the gateway restarts after the admission committed
+ return
+ await ws.send(json.dumps(reply))
+ async with serve(handler, "127.0.0.1", 0) as listener:
+ port = listener.sockets[0].getsockname()[1]
+ async with connect(f"ws://127.0.0.1:{port}", proxy=None) as ws:
+ async with GatewayClient(ws) as client:
+ yield client
+ monkeypatch.setattr(owner_forward, "connect_owner", wire_owner)
+
+ reply = server.handle_request({"id": "p", "method": "prompt.submit",
+ "params": {"session_id": sid, "text": "my question"}})
+ mine = [row for row in list_session_admissions(db, session_id=key)
+ if row["payload"].get("text") == "my question"]
+ assert seen == ["session.resume", "prompt.submit"] and len(mine) == 1 # the owner holds it
+ recover_session_inputs(db, epoch=begin_runtime_epoch(db, instance_id="gateway-2"))
+ after = {row["admission_id"]: row["status"] for row in list_session_admissions(db, session_id=key)}
+ assert after[mine[0]["admission_id"]] == "queued" # it survives the restart and will run
+ assert reply["error"]["code"] == 4090
+ assert reply["error"]["data"] == {"reason": "SESSION_OWNER_UNCONFIRMED"}
+ message = reply["error"]["message"]
+ assert "Nothing was sent" not in message and "check the chat" in message
+ assert turns == []
+ finally:
+ server._sessions.pop(sid, None)
+ db.close()
+
+
@pytest.mark.parametrize("case", ["owner_refused", "rewind"])
def test_a_prompt_the_owner_cannot_queue_is_refused_with_why_and_when(monkeypatch, tmp_path, case):
"""Where admission is impossible the refusal names why and says to send when the turn ends,
diff --git a/tui_gateway/owner_forward.py b/tui_gateway/owner_forward.py
index 4bb4dbb6031..8c1f7e21e63 100644
--- a/tui_gateway/owner_forward.py
+++ b/tui_gateway/owner_forward.py
@@ -128,8 +128,7 @@ class _Relay:
async with connect_owner(self.home, self.endpoint) as client:
attached = await client.rpc("session.resume", session_id=self.key)
target = str(attached.get("session_id") or self.key)
- receipt = await client.rpc("prompt.submit", session_id=target, text=self.text,
- submission_id=self.request_id)
+ receipt = await self._submit(client, target)
admission_id = receipt.get("admission_id")
if not isinstance(admission_id, str) or receipt.get("status") not in ("queued", "started", "terminal"):
raise OwnerUnavailable("the gateway did not admit it")
@@ -138,6 +137,20 @@ class _Relay:
self.key, admission_id)
await self._relay(client, admission_id)
+ async def _submit(self, client, target: str) -> dict:
+ """``prompt.submit`` on the owner. Once the request is handed to the socket, only a received
+ refusal (``GatewayRPCError``) proves nothing was queued: a lost connection or a missing answer
+ may follow the owner's durable commit (a gateway restart mid-forward), so it is unconfirmed,
+ never "nothing was sent" (a resend would run a second copy)."""
+ from hermes_cli.gateway_client import GatewayRPCError
+ try:
+ return await client.rpc("prompt.submit", session_id=target, text=self.text,
+ submission_id=self.request_id)
+ except GatewayRPCError:
+ raise
+ except Exception as exc:
+ raise OwnerUnconfirmed("the gateway did not confirm it") from exc
+
def _pending(self, params: dict, admission_id: str) -> None:
"""The owner's pending list as this wire's ``pending_submissions``, this admission only: the
Desktop's queued-row card while it waits, retired once the owner starts it."""
@@ -190,8 +203,8 @@ def forward_prompt(sid: str, session: dict, text: str, *, emit=None, home: Path
"""Admit ``text`` into the owner's FIFO for this session; the owner's receipt.
Raises ``OwnerNotRunning`` when no gateway serves the home, ``OwnerUnavailable`` (or the
- client's refusal) when the owner did not take it; nothing was admitted unless the receipt
- timed out, which is reported as such."""
+ client's refusal) when the owner did not take it, and ``OwnerUnconfirmed`` when the prompt may
+ be queued there: the receipt timed out, or the owner was lost after the request was sent."""
from tui_gateway import server
key = str(session.get("session_key") or "")
if not key: