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>
156 lines
6.3 KiB
Python
156 lines
6.3 KiB
Python
"""Public conversation ids: UUIDv7 per transcript (conversations/ids.py) and
|
|
the one-shot move of the JSON stores off the old path ids (id_migration.py)."""
|
|
|
|
import json
|
|
import pathlib
|
|
import uuid
|
|
|
|
import pytest
|
|
|
|
from conversations import id_migration, ids
|
|
from db import Store
|
|
|
|
REL = "-home-gabrielvidal-projects-secret/2b1f0c7e-6f1d-4f61-9a3e-0c7f2b0f9d11.jsonl"
|
|
SUB = "-home-gabrielvidal-projects-secret/2b1f0c7e-6f1d-4f61-9a3e-0c7f2b0f9d11/subagents/agent-a1b2.jsonl"
|
|
|
|
|
|
def _write(root: pathlib.Path, rel: str, ts: str | None) -> pathlib.Path:
|
|
p = root / rel
|
|
p.parent.mkdir(parents=True, exist_ok=True)
|
|
recs = [{"type": "summary"}]
|
|
if ts:
|
|
recs.append({"type": "user", "timestamp": ts, "message": {"content": "hi"}})
|
|
p.write_text("\n".join(json.dumps(r) for r in recs) + "\n")
|
|
return p
|
|
|
|
|
|
@pytest.fixture
|
|
def env(tmp_path, monkeypatch):
|
|
root = tmp_path / "transcripts"
|
|
root.mkdir()
|
|
store = Store(str(tmp_path / "ai.db"))
|
|
reg = ids.Registry()
|
|
monkeypatch.setattr(ids, "REGISTRY", reg)
|
|
monkeypatch.setattr(ids, "_root", lambda: root)
|
|
reg.bind(store, root)
|
|
return root, store, reg
|
|
|
|
|
|
def test_derive_is_a_uuidv7_carrying_the_start():
|
|
uid = ids.derive(REL, 1_762_000_000_123)
|
|
u = uuid.UUID(uid)
|
|
assert u.version == 7 and u.variant == uuid.RFC_4122
|
|
assert ids.uid_time(uid) == pytest.approx(1_762_000_000.123)
|
|
assert ids.derive(REL, 1_762_000_000_123) == uid # deterministic
|
|
assert ids.derive(SUB, 1_762_000_000_123) != uid # per path
|
|
assert "secret" not in uid and "home" not in uid
|
|
|
|
|
|
def test_ids_sort_by_start(env):
|
|
root, _store, reg = env
|
|
_write(root, "-a/11111111-1111-4111-8111-111111111111.jsonl", "2026-10-01T10:00:00.000Z")
|
|
_write(root, "-b/22222222-2222-4222-8222-222222222222.jsonl", "2026-09-01T10:00:00.000Z")
|
|
_write(root, "-c/33333333-3333-4333-8333-333333333333.jsonl", "2026-11-01T10:00:00Z")
|
|
got = {rel[1]: reg.uid(rel) for rel in (
|
|
"-a/11111111-1111-4111-8111-111111111111.jsonl",
|
|
"-b/22222222-2222-4222-8222-222222222222.jsonl",
|
|
"-c/33333333-3333-4333-8333-333333333333.jsonl")}
|
|
assert sorted(got, key=got.get) == ["b", "a", "c"]
|
|
|
|
|
|
def test_registry_persists_and_resolves(env, tmp_path):
|
|
root, store, reg = env
|
|
_write(root, REL, "2026-11-04T09:00:00Z")
|
|
uid = ids.uid_for_path(root / REL)
|
|
assert ids.rel_for(uid) == REL
|
|
assert ids.rel_for(REL) == REL # legacy spelling still resolves
|
|
assert ids.rel_for("../etc/passwd.jsonl") is None
|
|
assert ids.rel_for("nope") is None
|
|
assert ids.canonical(REL) == uid
|
|
# a fresh process re-reads the same uid from the DB
|
|
reg2 = ids.Registry()
|
|
reg2.bind(Store(str(tmp_path / "ai.db")), root)
|
|
assert reg2.rel(uid) == REL and reg2.uid(REL) == uid
|
|
|
|
|
|
def test_lost_registry_rederives_same_ids(env, tmp_path):
|
|
root, _store, reg = env
|
|
_write(root, REL, "2026-11-04T09:00:00Z")
|
|
uid = reg.uid(REL)
|
|
fresh = ids.Registry()
|
|
fresh.bind(Store(str(tmp_path / "other.db")), root)
|
|
assert fresh.uid(REL) == uid
|
|
|
|
|
|
def test_no_timestamp_falls_back_to_first_seen(env):
|
|
root, _store, reg = env
|
|
_write(root, REL, None)
|
|
uid = reg.uid(REL)
|
|
assert ids.is_uid(uid) and ids.uid_time(uid) > 1_700_000_000
|
|
|
|
|
|
def test_rewrite_text_swaps_only_known_ids():
|
|
uid = ids.derive(REL, 1)
|
|
mapping = {REL: uid, SUB: ids.derive(SUB, 1)}
|
|
src = json.dumps({
|
|
"conversationId": REL,
|
|
"sub": SUB,
|
|
"known": [f"{REL}#2026-11-03T00:33:17Z#0"],
|
|
"url": f"https://ai-agent.lab.gabvdl.xyz/conversation/{REL}",
|
|
"diff": "/diff/conversation/" + REL.replace("/", "%2F"),
|
|
"file": f"/home/gabrielvidal/.claude/projects/{REL}", # a file: kept
|
|
"unknown": "-home-x/44444444-4444-4444-8444-444444444444.jsonl",
|
|
# a synced PWA store: JSON inside a JSON string
|
|
"ui": json.dumps({"state": {"byConv": {REL: {"viewed": {}}}}}),
|
|
})
|
|
out, n = id_migration.rewrite_text(src, mapping)
|
|
d = json.loads(out)
|
|
assert d["conversationId"] == uid
|
|
assert d["sub"] == mapping[SUB]
|
|
assert d["known"] == [f"{uid}#2026-11-03T00:33:17Z#0"]
|
|
assert d["url"] == f"https://ai-agent.lab.gabvdl.xyz/conversation/{uid}"
|
|
assert d["diff"] == f"/diff/conversation/{uid}"
|
|
assert d["file"] == f"/home/gabrielvidal/.claude/projects/{REL}"
|
|
assert d["unknown"].endswith(".jsonl")
|
|
assert list(json.loads(d["ui"])["state"]["byConv"]) == [uid]
|
|
assert n == 6
|
|
|
|
|
|
def test_migrate_rewrites_stores_and_sweeps_again_each_start(env, tmp_path):
|
|
root, store, reg = env
|
|
_write(root, REL, "2026-11-04T09:00:00Z")
|
|
_write(root, SUB, "2026-11-04T09:01:00Z")
|
|
data = tmp_path
|
|
(data / "scheduled.json").write_text(json.dumps([{"conversationId": REL}]))
|
|
(data / "auth.json").write_text(json.dumps({"x": REL}))
|
|
store.set_short_title(REL, "Short", "Long")
|
|
out = id_migration.migrate(store, root, data)
|
|
uid = reg.known(REL)
|
|
assert out["minted"] == 2 and out["files"] == {"scheduled.json": 1}
|
|
assert json.loads((data / "scheduled.json").read_text()) == [{"conversationId": uid}]
|
|
assert json.loads((data / "auth.json").read_text()) == {"x": REL} # skipped
|
|
assert list(data.glob("scheduled.json.bak-uids-*"))
|
|
assert store.get_short_title(uid)[0] == "Short"
|
|
assert store.get_short_title(REL) is None
|
|
# nothing left: the next start only walks
|
|
again = id_migration.migrate(store, root, data)
|
|
assert (again["minted"], again["files"], again["shortTitles"]) == (0, {}, 0)
|
|
# …but what an old container wrote in between (a blue-green deploy) is swept
|
|
(data / "scheduled.json").write_text(json.dumps([{"conversationId": REL}]))
|
|
assert id_migration.migrate(store, root, data)["files"] == {"scheduled.json": 1}
|
|
assert json.loads((data / "scheduled.json").read_text()) == [{"conversationId": uid}]
|
|
|
|
|
|
def test_two_processes_agree_on_a_minted_uid(env, tmp_path):
|
|
"""No timestamp to derive from: each would mint off its own clock. The
|
|
second adopts what the first stored, and finds the first's by uid."""
|
|
root, store, reg = env
|
|
_write(root, REL, None)
|
|
a = reg.uid(REL)
|
|
other = ids.Registry()
|
|
other.bind(Store(str(tmp_path / "ai.db")), root)
|
|
other._by_rel.clear(); other._by_uid.clear() # started before the mint
|
|
assert other.rel(a) == REL
|
|
other._by_rel.clear(); other._by_uid.clear()
|
|
assert other.uid(REL) == a
|