Files
ai-agent/backend/test_continued_in.py
gabrielvidalandClaude Opus 5.5 de07e009f1 feat(conversations): UUIDv7 conversation ids — the transcript path stays inside
A conversation (and every subagent) was addressed by its transcript's path
under the archive, which put the owner's home dir, project and branch names in
every URL a guest saw. Now each archived transcript has a UUIDv7:

- timestamp = the transcript's first timestamped record (ids sort by start,
  past conversations included); the other 74 bits = sha256(rel path), so a
  lost registry re-derives the same ids
- registry: conv_uids table in ai-agent.db (conversations/ids.py), bound at
  startup; tree._conv_id is the one door responses go through
- legacy path ids and bare session ids still resolve (_public_id) — old links,
  deploy.sh, the mail trigger, worker terminals
- access.session_of / catalog._meta_key resolve a uid before deriving the sid;
  TRANSCRIPTS_DIR / <id> joins replaced by ids.path_of
- SSE transcript events carry uids + a roots map (subagent → root); the guest
  filter trims it
- thread detail: owner-only `transcript` (the archived copy's host path)
- id_migration: at every start, mint missing uids and swap every known path id
  in /data/*.json for its uid (raw text, .bak-uids-<stamp> copies), re-key
  short titles + their side-call refs; cross-process safe for blue-green

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 10:04:20 +01:00

81 lines
3.3 KiB
Python

"""GET /api/conversation `continuedIn` — a root thread follows a resumed copy
of its session, but a subagent (which shares its parent's sessionId) is its
own thread and never points back at the parent.
Imports ``main`` against throwaway paths (no startup event → no threads)."""
import json
import os
import pathlib
import tempfile
import unittest
TMP = pathlib.Path(tempfile.mkdtemp())
for k, v in {"DB_PATH": TMP / "x.db", "TRANSCRIPTS_DIR": TMP / "t",
"WORKSPACE": TMP, "REPO_DIR": TMP, "ARCHIVE_DIR": TMP / "arch",
"META_PATH": TMP / "meta.json", "WORKERS_PATH": TMP / "w.json",
"PROFILES_PATH": TMP / "profiles.json",
"WORKERS_TRANSCRIPTS_DIR": TMP / "mirror",
"CRON_PATH": TMP / "cron.json", "NOTIFY_PATH": TMP / "n.json",
"FORMS_PATH": TMP / "f.json", "UI_STATE_PATH": TMP / "ui.json",
"NOTIF_READ_PATH": TMP / "nr.json",
"MAIL_TRIGGER_SENDERS_PATH": TMP / "mt.json",
"USERS_PATH": TMP / "users.json", "AUTH_PATH": TMP / "auth.json",
"NEW_PROJECTS_PATH": TMP / "np.json", "PROJECTS_DIR": TMP / "projects",
"INTERNAL_API_TOKEN": "tok", "SIDECAR_TOKEN": "",
"PI_TRANSCRIPTS_DIR": ""}.items():
os.environ[k] = str(v)
from fastapi.testclient import TestClient # noqa: E402
import main # noqa: E402
import conversations.routes as conv_routes # noqa: E402
from conversations import ids # noqa: E402
SID = "11111111-2222-4333-8444-555555555555"
ROOT = f"-home-x/{SID}.jsonl"
SUB = f"-home-x/{SID}/subagents/agent-a1.jsonl"
RESUMED = f"-home-x--wt/{SID}.jsonl"
def write(rel: str, sidechain: bool) -> None:
p = TMP / "arch" / rel
p.parent.mkdir(parents=True, exist_ok=True)
extra = {"isSidechain": True, "agentId": "a1"} if sidechain else {}
p.write_text("\n".join(json.dumps(o) for o in [
{"type": "user", "uuid": "u1", "sessionId": SID, "timestamp": "2026-10-13T20:00:00Z",
"message": {"role": "user", "content": "hi"}, **extra},
{"type": "assistant", "uuid": "a1", "sessionId": SID, "timestamp": "2026-10-13T20:00:01Z",
"message": {"role": "assistant", "model": "claude-opus-5-5",
"content": [{"type": "text", "text": "hello"}]}, **extra},
]) + "\n")
class ContinuedInTest(unittest.TestCase):
def setUp(self):
for rel, side in ((ROOT, False), (SUB, True), (RESUMED, False)):
write(rel, side)
# The session map itself is covered by test_conv_ids_by_session; here
# the resumed copy is the newest file of the session.
self._orig = conv_routes._conv_ids_by_session
conv_routes._conv_ids_by_session = lambda deps: {SID: ids.uid_for_rel(RESUMED)}
self.c = TestClient(main.app, headers={"x-internal-auth": "tok"})
def tearDown(self):
conv_routes._conv_ids_by_session = self._orig
def continued_in(self, rel: str):
r = self.c.get("/api/conversation", params={"id": rel})
self.assertEqual(r.status_code, 200, r.text)
return r.json().get("continuedIn")
def test_root_follows_resumed_copy(self):
self.assertEqual(self.continued_in(ROOT), ids.uid_for_rel(RESUMED))
def test_subagent_is_never_redirected(self):
self.assertIsNone(self.continued_in(SUB))
if __name__ == "__main__":
unittest.main()