Files
ai-agent/backend/test_conv_ids_by_session.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

60 lines
1.9 KiB
Python

"""catalog._conv_ids_by_session — the sessionId → conversation map behind
`continuedIn`, notification links and form links never resolves to a subagent
transcript, even when a subagent is the most recently written file."""
import types
import unittest
from conversations import catalog
from conversations.tree import _conv_id
from core.config import TRANSCRIPTS_DIR
ROOT = f"{TRANSCRIPTS_DIR}/-home-x/sid1.jsonl"
SUB = f"{TRANSCRIPTS_DIR}/-home-x/sid1/subagents/agent-a1.jsonl"
RESUMED = f"{TRANSCRIPTS_DIR}/-home-x--wt/sid1.jsonl"
class FakeStore:
def __init__(self, summaries, version=1):
self._s = summaries
self.summaries_version = version
def all_summaries(self):
return self._s
def deps(summaries, version):
catalog._by_sid_cache = None
return types.SimpleNamespace(store=FakeStore(summaries, version))
def summ(ended, sidechain=False):
return {"sessionId": "sid1", "messages": 3, "endedAt": ended, "isSidechain": sidechain}
class ConvIdsBySessionTest(unittest.TestCase):
def test_newer_subagent_does_not_win(self):
out = catalog._conv_ids_by_session(deps([
(ROOT, summ("2026-10-13T20:00:00Z")),
(SUB, summ("2026-10-13T21:00:00Z", sidechain=True)),
], 1))
self.assertEqual(out["sid1"], _conv_id(ROOT))
def test_subagent_path_excluded_even_unflagged(self):
out = catalog._conv_ids_by_session(deps([
(ROOT, summ("2026-10-13T20:00:00Z")),
(SUB, summ("2026-10-13T21:00:00Z")),
], 2))
self.assertEqual(out["sid1"], _conv_id(ROOT))
def test_resumed_copy_still_wins(self):
out = catalog._conv_ids_by_session(deps([
(ROOT, summ("2026-10-13T20:00:00Z")),
(RESUMED, summ("2026-10-13T20:30:00Z")),
(SUB, summ("2026-10-13T21:00:00Z", sidechain=True)),
], 3))
self.assertEqual(out["sid1"], _conv_id(RESUMED))
if __name__ == "__main__":
unittest.main()