Gabriel's call (2026-11-18 form): Claude Code on the Orus MacBook drives the
hub through the MCP server, not the `ai-agent` CLI carried by the worker's
outbox (0226ffa..9031162, 2026-11-16).
- An owner MCP connection can be bound to one of his workers (grant.worker,
PATCH /api/mcp/connections/{id} {worker}, "" unbinds; logged mcp_bound).
Its loopback calls carry X-AIA-As-Worker + X-AIA-As-Worker-Mode (run for a
full connection, read otherwise) and the gate answers them as
workers/bound.py worker_caller: the owner narrowed to that worker's
profile, its routes, runs on that machine only. Its tools are
WORKER_TOOLS; browse keeps scheduled / models / workers / usage / budget.
- The narrowing the CLI path introduced (access.py, runs / scheduled /
conversation-meta pinning) stays, moved to workers/bound.py.
- Removed for every worker: the `cli` outbox item and CliJobs, the per-worker
`cli` switch, the CLI's outbox forwarding. On a worker the CLI keeps only
`past` (the outbox lookup, still narrowed to the worker's profile) and its
refusal points to the MCP tools. The installer keeps the notify shims and
deletes its old `ai-agent` shim.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
177 lines
7.3 KiB
Python
177 lines
7.3 KiB
Python
"""The outbox: a run queues, the hub collects + answers, the run's wait returns."""
|
|
|
|
import threading
|
|
import time
|
|
|
|
import pytest
|
|
from fastapi import FastAPI, HTTPException
|
|
from fastapi.testclient import TestClient
|
|
|
|
import outbox
|
|
|
|
HUB = "hub-t0ken"
|
|
RUN = "run-t0ken"
|
|
|
|
|
|
def _hub_auth(h):
|
|
if h != f"Bearer {HUB}":
|
|
raise HTTPException(401, "unauthorized")
|
|
|
|
|
|
@pytest.fixture()
|
|
def box(tmp_path):
|
|
return outbox.Outbox(tmp_path / "outbox.json")
|
|
|
|
|
|
@pytest.fixture()
|
|
def app(box):
|
|
a = FastAPI()
|
|
a.include_router(outbox.make_router(box, _hub_auth, lambda: RUN))
|
|
return a
|
|
|
|
|
|
@pytest.fixture()
|
|
def run(app):
|
|
return TestClient(app, headers={"Authorization": f"Bearer {RUN}"})
|
|
|
|
|
|
@pytest.fixture()
|
|
def hub(app):
|
|
return TestClient(app, headers={"Authorization": f"Bearer {HUB}"})
|
|
|
|
|
|
ASK = {"id": "ask_0123456789ab", "question": "Ship?", "options": ["Yes", "No"]}
|
|
|
|
|
|
def test_run_token_is_minted_once_and_private(tmp_path):
|
|
t1 = outbox.run_token(tmp_path)
|
|
t2 = outbox.run_token(tmp_path)
|
|
assert t1 == t2 and len(t1) >= 32
|
|
assert oct((tmp_path / "outbox-token").stat().st_mode & 0o777) == "0o600"
|
|
|
|
|
|
def test_put_needs_run_token_and_a_cli_id(run, app):
|
|
anon = TestClient(app)
|
|
assert anon.post("/outbox", json={"kind": "ask", "payload": ASK}).status_code == 401
|
|
r = run.post("/outbox", json={"kind": "ask", "payload": {"id": "nope"}})
|
|
assert r.status_code == 400
|
|
r = run.post("/outbox", json={"kind": "ask", "payload": ASK})
|
|
assert r.status_code == 202 and r.json() == {"queued": True, "id": ASK["id"],
|
|
"status": "queued"}
|
|
# the same id can't be queued twice
|
|
assert run.post("/outbox", json={"kind": "ask", "payload": ASK}).status_code == 400
|
|
|
|
|
|
def test_hub_collects_acks_and_answers(run, hub, box):
|
|
run.post("/outbox", json={"kind": "ask", "payload": ASK})
|
|
# the run can't list the queue, the hub can
|
|
assert run.get("/outbox").status_code == 401
|
|
items = hub.get("/outbox").json()["items"]
|
|
assert [i["id"] for i in items] == [ASK["id"]] and items[0]["status"] == "queued"
|
|
|
|
hub.post(f"/outbox/{ASK['id']}/ack", json={"hub": {"ok": True, "id": ASK["id"]}})
|
|
it = run.get(f"/outbox/{ASK['id']}").json()
|
|
assert it["status"] == "sent" and it["hub"]["ok"] is True
|
|
# still listed (open) so the hub keeps checking for the answer
|
|
assert hub.get("/outbox").json()["count"] == 1
|
|
|
|
r = hub.post(f"/outbox/{ASK['id']}/answer",
|
|
json={"status": "answered", "answer": "Yes", "answerIndex": 0})
|
|
assert r.json()["status"] == "answered"
|
|
it = run.get(f"/outbox/{ASK['id']}").json()
|
|
assert (it["status"], it["answer"], it["answerIndex"]) == ("answered", "Yes", 0)
|
|
assert hub.get("/outbox").json()["count"] == 0 # done — off the list
|
|
assert box.get(ASK["id"])["endedAt"]
|
|
|
|
|
|
def test_wait_returns_the_instant_the_answer_lands(box):
|
|
box.put("ask", ASK)
|
|
box.ack(ASK["id"], {"ok": True})
|
|
got = {}
|
|
|
|
def waiter():
|
|
got["it"] = box.wait(ASK["id"], 10)
|
|
th = threading.Thread(target=waiter)
|
|
t0 = time.monotonic()
|
|
th.start()
|
|
time.sleep(0.2)
|
|
box.answer(ASK["id"], {"status": "answered", "answer": "No", "answerIndex": 1})
|
|
th.join(5)
|
|
assert got["it"]["answer"] == "No" and time.monotonic() - t0 < 3
|
|
|
|
|
|
def test_failed_ack_ends_the_item(run, hub):
|
|
run.post("/outbox", json={"kind": "form", "payload": {"id": "frm_0123456789ab",
|
|
"title": "", "fields": []}})
|
|
hub.post("/outbox/frm_0123456789ab/ack", json={"hub": {"failed": True,
|
|
"error": "need a title"}})
|
|
it = run.get("/outbox/frm_0123456789ab").json()
|
|
assert it["status"] == "failed" and it["hub"]["error"] == "need a title"
|
|
assert hub.get("/outbox").json()["count"] == 0
|
|
|
|
|
|
def test_cancel_before_and_after_collection(run, hub):
|
|
a, b = dict(ASK, id="ask_aaaaaaaaaaaa"), dict(ASK, id="ask_bbbbbbbbbbbb")
|
|
run.post("/outbox", json={"kind": "ask", "payload": a})
|
|
run.post("/outbox", json={"kind": "ask", "payload": b})
|
|
# a: cancelled while still queued → simply gone from the hub's view
|
|
assert run.post(f"/outbox/{a['id']}/cancel").json()["status"] == "cancelled"
|
|
assert [i["id"] for i in hub.get("/outbox").json()["items"]] == [b["id"]]
|
|
# b: collected, then cancelled → the hub is told once, then it's off the list
|
|
hub.post(f"/outbox/{b['id']}/ack", json={"hub": {"ok": True}})
|
|
run.post(f"/outbox/{b['id']}/cancel")
|
|
items = hub.get("/outbox").json()["items"]
|
|
assert items and items[0]["id"] == b["id"] and items[0]["cancelRequested"] is True
|
|
hub.post(f"/outbox/{b['id']}/ack", json={"hub": {"cancelled": True}})
|
|
assert hub.get("/outbox").json()["count"] == 0
|
|
|
|
|
|
def test_persists_across_a_restart(tmp_path):
|
|
b1 = outbox.Outbox(tmp_path / "o.json")
|
|
b1.put("notify", {"id": "ntf_0123456789ab", "title": "t", "message": "m"})
|
|
b2 = outbox.Outbox(tmp_path / "o.json")
|
|
assert b2.get("ntf_0123456789ab")["status"] == "queued"
|
|
assert [i["id"] for i in b2.pending()] == ["ntf_0123456789ab"]
|
|
|
|
|
|
def test_form_secret_is_readable_by_the_run_only(run, hub, box):
|
|
fid = "frm_0123456789ab"
|
|
run.post("/outbox", json={"kind": "form", "payload": {"id": fid, "title": "T",
|
|
"fields": []}})
|
|
hub.post(f"/outbox/{fid}/ack", json={"hub": {"ok": True, "id": fid}})
|
|
hub.post(f"/outbox/{fid}/answer", json={
|
|
"status": "submitted", "answers": {"tok": {"secret": True, "chars": 3}},
|
|
"secrets": {"tok": "abc"}})
|
|
# `notify wait` reads the item: the answers' placeholder, never the value
|
|
it = run.get(f"/outbox/{fid}").json()
|
|
assert it["answers"] == {"tok": {"secret": True, "chars": 3}}
|
|
assert "abc" not in str(it)
|
|
# only the run's token reads it — not the hub's, not an anonymous caller
|
|
assert hub.post(f"/outbox/{fid}/secret", json={"key": "tok"}).status_code == 401
|
|
r = run.post(f"/outbox/{fid}/secret", json={"key": "tok"})
|
|
assert r.status_code == 200 and r.json()["value"] == "abc"
|
|
assert run.post(f"/outbox/{fid}/secret", json={"key": "nope"}).status_code == 410
|
|
# forget drops it; expiry too
|
|
run.post(f"/outbox/{fid}/secret", json={"key": "tok", "forget": True})
|
|
assert run.post(f"/outbox/{fid}/secret", json={"key": "tok"}).status_code == 410
|
|
box.answer(fid, {"status": "submitted", "secrets": {"tok": "abc"}})
|
|
box._find(fid)["secretsExpire"] = time.time() - 1
|
|
assert box.secret(fid, "tok") is None and "secrets" not in box.get(fid)
|
|
assert "abc" not in box.path.read_text()
|
|
|
|
|
|
def test_askform_event_is_one_shot(box, hub):
|
|
"""An `ask-form serve` event (askform.py) is applied on collection: the
|
|
ack ends it, nothing waits on it."""
|
|
it = box.put("askform", {"id": "afe_0123456789ab", "event": "created",
|
|
"form": {"id": "project-setup_k3f9q2"}})
|
|
assert it["status"] == "queued"
|
|
assert [i["id"] for i in box.pending()] == ["afe_0123456789ab"]
|
|
hub.post("/outbox/afe_0123456789ab/ack", json={"hub": {"ok": True}})
|
|
assert box.get("afe_0123456789ab")["status"] == "applied"
|
|
assert box.pending() == []
|
|
with pytest.raises(ValueError):
|
|
box.put("askform", {"id": "ntf_0123456789ab"}) # the wrong prefix
|
|
with pytest.raises(ValueError):
|
|
box.put("notify", {"id": "afe_0123456789ab"})
|