Files
ai-agent/sidecar/test_outbox_meta.py
Gabriel VidalandClaude Opus 5.5 cc6c857305 feat(work-context): past conversations on the issue, through ai-agent past
177 of 326 work conversations followed one on the same Linear issue (median
4 earlier, 15 h apart); 63 % of their early PR / issue / thread fetches
repeated one, and 5 of 192 prompts pointed at the earlier conversation.

- `ai-agent past KEY…` (cli/ai_agent.py): the conversations on a subject —
  the Linear issue (a PR counts as its issue via its head branch, a branch
  only names one the hub knows) — running now, then the 3 newest ended ones
  of the last 30 days, primary-on-it first, each with its last message that
  names the subject (a turn's answer first, whole-word match).
- Backend conversations/past.py on the related index + searchMsgs, served
  as GET /api/past-conversations on the lab.
- On a worker it goes through the outbox: a new one-shot `lookup` kind
  (`lkp_`), answered in the ack's `hub.result`; a guest's computer is
  refused. It is the only `ai-agent` command a worker run may use.
- The work-context agent runs it once per brief and adds RUNNING NOW / PAST
  sections, behind Settings → Profiles → Resources → "Past conversations in
  the brief" (resources.pastConversations, default on).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 18:18:55 +01:00

73 lines
2.9 KiB
Python

"""The outbox's `meta` kind (the `conv` CLI on a worker): queued like any
item, ended by the hub's ack (`applied`) — nothing to wait for or answer."""
import pytest
from fastapi import FastAPI, HTTPException
from fastapi.testclient import TestClient
import outbox
HUB, RUN = "hub-t0ken", "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 clients(box):
a = FastAPI()
a.include_router(outbox.make_router(box, _hub_auth, lambda: RUN))
return (TestClient(a, headers={"Authorization": f"Bearer {RUN}"}),
TestClient(a, headers={"Authorization": f"Bearer {HUB}"}))
META = {"id": "met_0123456789ab", "sessionId": "s1",
"patch": {"resources": [{"kind": "pr", "id": "o/r#1", "primary": True}]}}
def test_meta_item_is_applied_by_the_ack(clients):
run, hub = clients
r = run.post("/outbox", json={"kind": "meta", "payload": META})
assert r.status_code == 202 and r.json()["status"] == "queued"
items = hub.get("/outbox").json()["items"]
assert [i["id"] for i in items] == ["met_0123456789ab"]
assert hub.post("/outbox/met_0123456789ab/ack",
json={"hub": {"ok": True}}).json()["status"] == "applied"
assert hub.get("/outbox").json()["items"] == [] # never pending again
assert run.get("/outbox/met_0123456789ab").json()["status"] == "applied"
def test_failed_ack_and_id_kind_mismatch(clients):
run, hub = clients
run.post("/outbox", json={"kind": "meta", "payload": META})
assert hub.post("/outbox/met_0123456789ab/ack", json={"hub": {
"failed": True, "error": "that session runs elsewhere"}}).json()["status"] == "failed"
bad = run.post("/outbox", json={"kind": "meta",
"payload": {**META, "id": "ntf_0123456789ab"}})
assert bad.status_code == 400
bad = run.post("/outbox", json={"kind": "notify",
"payload": {**META, "id": "met_aaaaaaaaaaaa"}})
assert bad.status_code == 400
LOOKUP = {"id": "lkp_0123456789ab", "sessionId": "s1", "keys": ["INS-1"]}
def test_lookup_is_answered_in_the_ack(clients):
"""`ai-agent past` on a worker: one-shot, its answer is the ack's result."""
run, hub = clients
assert run.post("/outbox", json={"kind": "lookup", "payload": LOOKUP}).status_code == 202
# a lookup id on another kind, or another id on a lookup, is refused
assert run.post("/outbox", json={"kind": "notify",
"payload": {**LOOKUP, "id": "lkp_0123456789ac"}}).status_code == 400
hub.post("/outbox/lkp_0123456789ab/ack", json={"hub": {"ok": True, "result": {"subjects": []}}})
it = run.get("/outbox/lkp_0123456789ab").json()
assert it["status"] == "applied" and it["hub"]["result"] == {"subjects": []}