Skip to content

The agent that grows with you

fix(tui): an owner error after its commit is unconfirmed, not "nothing was sent"

David Dudok de Wit819888e07dd6
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 141bec39e52..d87d83e9c9a 100644
--- a/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py
+++ b/tests/tui_gateway/test_legacy_submit_forwards_to_owner.py
@@ -283,7 +283,49 @@ def test_a_receipt_lost_after_the_owner_queued_it_is_unconfirmed_not_resendable(
db.close()
[email protected]("case", ["owner_refused", "rewind"])
+def test_an_owner_error_after_its_commit_is_unconfirmed_not_resendable(monkeypatch, tmp_path):
+ """The owner publishes its pending list AFTER the durable write, and a SQLite error there reaches
+ serve as a structured ``storage_unavailable``, the same answer a refused write gets. The prompt is
+ in the owner's FIFO and survives a restart, so "Nothing was sent. Send it again" would queue a
+ second copy under a fresh submission id: only a refusal raised before the write may say so."""
+ import sqlite3
+ 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)
+ publish, failed = authority._publish_pending, []
+
+ def publish_fails_once(ref): # the admission's publication, after its commit
+ if not failed:
+ failed.append(ref)
+ raise sqlite3.OperationalError("disk I/O error")
+ return publish(ref)
+ monkeypatch.setattr(authority, "_publish_pending", publish_fails_once)
+
+ 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 failed 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()
+
+
[email protected]("case", ["owner_refused", "owner_queue_full", "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,
with a transient reason (not SESSION_NOT_OWNED, whose card offers a new chat)."""
@@ -305,13 +347,18 @@ def test_a_prompt_the_owner_cannot_queue_is_refused_with_why_and_when(monkeypatc
raise owner_forward.OwnerUnavailable("the gateway answered with another protocol")
yield
monkeypatch.setattr(owner_forward, "connect_owner", refused)
+ elif case == "owner_queue_full":
+ # Sent, and refused by the owner's ledger inside its write: nothing was queued.
+ import hermes_state_runtime
+ monkeypatch.setattr(hermes_state_runtime, "ADMISSION_QUEUE_MAX", 0)
else:
params.update(truncate_before_user_ordinal=0, confirm_truncate=True)
reply = server.handle_request({"id": "p", "method": "prompt.submit", "params": params})
assert reply["error"]["code"] == 4090 and reply["error"]["data"] == {"reason": "SESSION_OWNER_BUSY"}
message = reply["error"]["message"]
assert "when the current turn ends" in message and "Nothing was sent" in message
- assert ("another protocol" in message) if case == "owner_refused" else ("rewind" in message)
+ assert {"owner_refused": "another protocol", "owner_queue_full": "the gateway refused it",
+ "rewind": "rewind"}[case] in message
assert len(list_session_admissions(db, session_id=key)) == 1 and turns == []
finally:
server._sessions.pop(sid, None)
diff --git a/tui_gateway/owner_forward.py b/tui_gateway/owner_forward.py
index 8c1f7e21e63..d864b289b10 100644
--- a/tui_gateway/owner_forward.py
+++ b/tui_gateway/owner_forward.py
@@ -42,6 +42,13 @@ RELAY_FINALIZE_POLL_S = 1.0
#: nothing re-reads the stored transcript at ``message.complete``, which brings the user's own row
#: (written by the owner, not by this backend) and the reply in their committed order.
_RELAYED = frozenset({"message.start", "message.complete"})
+#: The owner's ``prompt.submit`` refusals raised before its durable write (dispatcher, ``authorize``,
+#: ``submission``, the admission gate, and the ledger's own checks inside the rolled-back write): only
+#: these prove nothing was queued. ``storage_unavailable`` and an internal error can also come from the
+#: pending publication AFTER the commit (``SessionAuthority._admitted``), so they are never listed.
+_REFUSED_BEFORE_COMMIT = frozenset({
+ "invalid_params", "runtime_draining", "runtime_retired", "profile_mismatch", "permission_denied",
+ "not_found", "session_superseded", "stale_epoch", "admission_conflict", "queue_full"})
class OwnerUnavailable(RuntimeError):
@@ -139,16 +146,19 @@ class _Relay:
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)."""
+ refusal the owner raises before its write (``_REFUSED_BEFORE_COMMIT``) proves nothing was
+ queued: a lost connection, a missing answer or any other error may follow the owner's durable
+ commit (a gateway restart mid-forward, a storage error publishing the pending list), 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:
+ # The owner names its refusal in ``data.reason``; ``message`` may be a display string.
+ reason = exc.data.get("reason", str(exc)) if isinstance(exc, GatewayRPCError) else None
+ if isinstance(reason, str) and reason in _REFUSED_BEFORE_COMMIT:
+ raise
raise OwnerUnconfirmed("the gateway did not confirm it") from exc
def _pending(self, params: dict, admission_id: str) -> None:
@@ -204,7 +214,7 @@ def forward_prompt(sid: str, session: dict, text: str, *, emit=None, home: Path
Raises ``OwnerNotRunning`` when no gateway serves the home, ``OwnerUnavailable`` (or the
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."""
+ be queued there: the receipt timed out, or the owner was lost or failed after the request was sent."""
from tui_gateway import server
key = str(session.get("session_key") or "")
if not key: