Module session_backfill

Module session_backfill 

Source
Expand description

Backfill the registry record session_id from each agent’s provider transcript, so a cross-channel open --resumes the original conversation instead of starting a fresh session.

Background: the registry session_id is read on launch (surfaced to the picker, which passes it as --resume <sid> on the first turn of a reattached block — agent_handlers.rs) but was never written by production code, so it was always null. A fresh-build / cross-channel open therefore had no sid to resume and spawned a brand-new session, which then shadowed the original. See docs/retro/retro-cross-channel-conversation-continuity-regression-2026-06-16.md.

Once populated, --resume keeps the same session id across turns, so a single idempotent startup pass keeps continuity solid without per-turn wiring.

Functions§

backfill_session_ids
Populate session_id for registry records that lack one, from the agent’s largest provider session. Idempotent (skips records that already carry a non-empty id). Returns the number populated. Best-effort per record — a record with no source_agents_base or no transcript is skipped, never fatal.
encode_project_slug
Encode an absolute workspace path to a Claude project-dir slug. Claude Code replaces each of / \ : . with - (lossy — a literal - inside a segment is indistinguishable from a separator; this matches the CLI’s own scheme and the decode_project_path direction in history::claude_adapter).
largest_session_id
The provider session id (jsonl stem) of the agent’s largest session across the candidate project roots (the account-wide default and, for identity-bound agents, the identity bundle). We pick the largest, not the newest, on purpose: an accidental fresh session (one started when a blank cross-channel open failed to resume) must never win over the real conversation. None when there are no session files under any root.
largest_with_size 🔒
The largest (size, session-id) directly under projects_dir/<slug>, or None if the dir doesn’t exist / has no session files.