Skip to content

The agent that grows with you

test: reproduce API delegation provenance and replay loss

Teknium179790df6f0f
diff --git a/evals/api_delegation_sync_probe.py b/evals/api_delegation_sync_probe.py
new file mode 100644
index 00000000000..24fff8db963
--- /dev/null
+++ b/evals/api_delegation_sync_probe.py
@@ -0,0 +1,55 @@
+"""Model-free API delegation delivery probe; run in an isolated HERMES_HOME.
+
+Uses real request-context binding, dispatch policy and SQLite persistence. This
+is local mechanism evidence, not provider inference or native Windows evidence.
+"""
+import asyncio
+import inspect
+import json
+import os
+from pathlib import Path
+from types import SimpleNamespace
+
+
+async def probe():
+ from gateway.platforms.api_server import APIServerAdapter
+ from gateway.session_context import clear_session_vars
+ from gateway.wake import persist_delegation_delivery
+ from hermes_state import SessionDB
+ from tools.delegate_tool_dispatch import _resolve_async_wake_sid
+
+ decisions = {}
+ for name, capable in (("headerless", ""), ("explicit", "1")):
+ kw = dict(chat_id="api-parent", session_id="api-parent")
+ if "wake_capable" in inspect.signature(APIServerAdapter._bind_api_server_session).parameters:
+ kw["wake_capable"] = capable
+ tokens = APIServerAdapter._bind_api_server_session(**kw)
+ try:
+ args = ["api-parent"]
+ if len(inspect.signature(_resolve_async_wake_sid).parameters) > 1:
+ args.append(capable == "1")
+ decisions[name] = _resolve_async_wake_sid(*args)
+ finally:
+ clear_session_vars(tokens)
+ db = SessionDB(db_path=Path(os.environ["HERMES_HOME"]) / "state.db")
+ db.create_session("api-parent", source="api_server")
+ db.create_session("stranger", source="api_server")
+ adapter = SimpleNamespace(_ensure_session_db=lambda: db)
+ evt = {"type": "async_delegation", "delegation_id": "probe-unit-1"}
+ await asyncio.gather(*(persist_delegation_delivery(adapter, text="OWNER_RESULT", session_id="api-parent", evt=evt) for _ in range(2)))
+ decisions["replayed_rows"] = len(db.get_messages("api-parent"))
+ db.end_session("api-parent", "compression")
+ db.create_session("api-child", source="api_server", parent_session_id="api-parent")
+ try:
+ await persist_delegation_delivery(adapter, text="LATE_RESULT", session_id="api-parent", evt={**evt, "delegation_id": "probe-unit-2"})
+ decisions["rotation"] = "persisted"
+ except Exception as exc:
+ decisions["rotation"] = type(exc).__name__
+ decisions["child_rows"] = len(db.get_messages("api-child"))
+ decisions["stranger_rows"] = len(db.get_messages("stranger"))
+ db.close()
+ return decisions
+
+
+if __name__ == "__main__":
+ print(json.dumps(asyncio.run(probe()), indent=2))
diff --git a/tests/tools/test_delegate_apiserver_background.py b/tests/tools/test_delegate_apiserver_background.py
index 4c33cf9153b..394d8d24a4f 100644
--- a/tests/tools/test_delegate_apiserver_background.py
+++ b/tests/tools/test_delegate_apiserver_background.py
@@ -41,6 +41,10 @@ def _clean_queue_and_context(monkeypatch):
for var in sc._VAR_MAP.values():
var.set(sc._UNSET)
sc._SESSION_ASYNC_DELIVERY.set(sc._UNSET)
+ # wake-capable lives outside _VAR_MAP (no env fallback by design, #98619),
+ # so reset it explicitly — a leaked "1" would hand the next test wake
+ # authority its binder never declared.
+ sc._SESSION_WAKE_CAPABLE.set(sc._UNSET)
# set_current_session_id (invoked by the clobber-reproducing fake child
# build) writes os.environ directly — scrub it so it can't leak into
# other test modules.
@@ -108,8 +112,10 @@ def _patch_delegate(monkeypatch):
def test_apiserver_session_with_id_dispatches_background(monkeypatch):
- """async_delivery=False + a raw session id (HERMES_SESSION_ID) →
- background dispatch (the completion wakes the session via the
+ """async_delivery=False + a raw session id (HERMES_SESSION_ID) that its binder
+ DECLARED wake-capable (audited producer: explicit X-Hermes-Session-Id, a
+ native /api/sessions id, a /v1/runs id — the client can address the id
+ again) → background dispatch (the completion wakes the session via the
api_server self-post), NOT the forced-sync fallback."""
dt = _patch_delegate(monkeypatch)
monkeypatch.setenv("HERMES_SESSION_ID", "raw-sid-7")
@@ -119,6 +125,7 @@ def test_apiserver_session_with_id_dispatches_background(monkeypatch):
session_key="raw-sid-7",
session_id="raw-sid-7",
async_delivery=False,
+ wake_capable="1",
)
out = dt.delegate_task(
@@ -165,3 +172,85 @@ def test_apiserver_session_without_id_stays_synchronous(monkeypatch):
assert parsed.get("status") != "dispatched", parsed
assert "SYNCHRONOUSLY" in parsed.get("note", "")
assert process_registry.completion_queue.empty()
+
+
+# ---------------------------------------------------------------------------
+# #98619 — wake capability must be DECLARED, never assumed
+# ---------------------------------------------------------------------------
+
+
+def test_apiserver_wake_capability_omission_fails_closed(monkeypatch):
+ """#98619 public-omission regression: a binder that binds a raw session id
+ but NEVER declares wake capability (set_session_vars called without the
+ flag — exactly what a future, not-yet-audited binding path would do) must
+ NOT get background dispatch. Wake authority is proof-carrying: omitted
+ means denied, and the batch stays synchronous."""
+ dt = _patch_delegate(monkeypatch)
+ set_session_vars(
+ platform="api_server",
+ chat_id="raw-sid-undeclared",
+ session_key="raw-sid-undeclared",
+ session_id="raw-sid-undeclared",
+ async_delivery=False,
+ )
+
+ out = dt.delegate_task(
+ goal="bg undeclared", context="ctx",
+ background=True, parent_agent=_fake_parent(),
+ )
+ parsed = json.loads(out)
+ assert parsed.get("status") != "dispatched", parsed
+ assert "SYNCHRONOUSLY" in parsed.get("note", "")
+ assert process_registry.completion_queue.empty()
+
+
+def test_apiserver_derived_session_id_stays_synchronous(monkeypatch):
+ """#98619: a bound-but-derived chat id (header-less OpenAI-compatible
+ client, no X-Hermes-Session-Id) must NOT dispatch background — the wake
+ self-post would hard-fail (no API_SERVER_KEY) or land in a session whose
+ history the client never reloads. A session id merely existing is not
+ wake-capable; the binding must declare it."""
+ dt = _patch_delegate(monkeypatch)
+ set_session_vars(
+ platform="api_server",
+ chat_id="fingerprint-hex-0123456789abcdef",
+ session_key="fingerprint-hex-0123456789abcdef",
+ session_id="fingerprint-hex-0123456789abcdef",
+ wake_capable="",
+ async_delivery=False,
+ )
+
+ out = dt.delegate_task(
+ goal="bg derived", context="ctx",
+ background=True, parent_agent=_fake_parent(),
+ )
+ parsed = json.loads(out)
+ assert parsed.get("status") != "dispatched", parsed
+ assert "SYNCHRONOUSLY" in parsed.get("note", "")
+ assert process_registry.completion_queue.empty()
+
+
+def test_wake_capable_session_reader_is_default_deny(monkeypatch):
+ """#98619: wake_capable_session() True ONLY for the literal "1". _UNSET
+ (never declared) and "" (declared not capable) both fail closed, and a
+ leaked HERMES_SESSION_WAKE_CAPABLE env var must NOT grant authority —
+ the reader never falls back to os.environ."""
+ import gateway.session_context as sc
+ from gateway.session_context import wake_capable_session
+
+ # Never bound in this context: _UNSET sentinel, no env fallback even when
+ # the variable is present in the environment.
+ monkeypatch.setenv("HERMES_SESSION_WAKE_CAPABLE", "1")
+ assert wake_capable_session() is False
+
+ # Declared wake-capable (audited producer): "1".
+ tokens = set_session_vars(platform="api_server", wake_capable="1")
+ assert wake_capable_session() is True
+ sc.clear_session_vars(tokens)
+ # A cleared context has declared nothing: back to denied.
+ assert wake_capable_session() is False
+
+ # Explicitly declared NOT wake-capable (fingerprint-derived id): "".
+ tokens = set_session_vars(platform="api_server", wake_capable="")
+ assert wake_capable_session() is False
+ sc.clear_session_vars(tokens)