Repository navigation
feat(log): list and search default to the current repo's sessions (--all-repos to widen) - #329
Conversation
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Audit round 0: requirements and deletion, PR #329 @
|
| # | requirement | author of record |
|---|---|---|
| R1 | log list and log search cover the current repo's sessions by default |
Zach. When asked how a session-log search in one repo should be kept from pulling in other repos' (including client) content, he chose the option that puts the default in the cairn client, so every caller gets the guard |
| R2 | --all-repos is required to widen |
Zach (the same choice) |
| R3 | refuse with exit 2 outside a git repo, or when --repo is not a repo |
agent (fail-closed, which follows from R1) |
| R4 | the scope is every worktree from git worktree list, not a path prefix |
agent ("the repos use sibling-dir worktrees") |
| R5 | the match is at a path-component boundary, written once in Go and once in SQL | agent (needed for R1 to be correct) |
| R6 | the scoped request is a POST with a form body, served by two new routes with a body bound | agent (needed for R4's size, about 31 KB against nginx's 8 KB request line) |
| R7 | the listener echoes cwd_under, and an answer without the echo exits 3 |
agent |
| R8 | the client rechecks every hit or row against the scope, and an outside one exits 5 unprinted | agent |
| R9 | a 405 on the POST exits 3 and names the flags that work around it | agent |
| R10 | a cairn: sessions=… line on stderr |
agent |
| R11 | --cwd with --all-repos is a usage error |
agent |
| R12 | nested worktrees are deduplicated, with a cap of 1024 per request | agent |
| R13 | log read SESSION stays unscoped |
agent (unchanged behaviour) |
I raise no deletion candidate on the grounds that nobody asked for something. R3–R13 are design choices made in service of R1 and R2.
Q1: the scope is per SESSION, keyed on its first cwd. It is not per record.
transcript_sessions.cwd is the first cwd seen (internal/pgstore/transcriptindex.go:117-123), and both SQL filters test that column (:293-294, :454-455). So all the content from a session that was launched in repo A and then worked in repo B, including a client repo, sits inside A's default scope.
Measured on the live listener with the installed client, using --cwd for one repo's base clone and --since 2026-11-01 --limit 50. I counted lines only, so no snippet was read:
- Positive control (the repo's own name): 50 hits in 4 sessions.
- A term specific to one client repo: 50 hits (the cap) in 6 sessions.
- The client org's name: 50 hits in 8 sessions.
That shows the term is present. It does not show the hits are client content, which I deliberately did not read. The structural point stands either way. The ask was about content from other repos, and the PR delivers a guard at session level. Closing the gap would mean indexing a cwd per record, which is a capture/index change that this PR's shape does not extend to.
D1: delete the cwd_under echo and its exit-3 branch
The two CwdUnder echo fields are at transcriptwire.go:151 and :197, the echo comparison is at log.go:312 in breach, and the echo tests go with them.
The case the echo guards (an older listener that ignores the filter) can no longer happen. The scoped request is POST-only, and a listener from before this PR has no POST route, so it answers 405 and R9 already exits 3. The leak half is covered without it, too: R8's per-hit check refuses any outside session that any listener prints. The only thing the echo adds is choosing between exit 3 and exit 5 for a future listener that serves the POST and drops the filter. Keep R8 and R9.
D2: simplify R4 and R6 by sending the main worktree and the current toplevel, as a GET
The premise behind R4 is that sessions live in sibling worktrees. Measured on the live index:
- For this repo, all 38 sessions whose cwd starts with the base-clone path have the base clone itself as their cwd. None starts in a sibling worktree.
- Across every session under
/home/zach/from 2026-11-01 to 2026-12-04 (219 sessions, with no week reaching the 200 cap), no first cwd is a sibling worktree directory. They are base clones, scratch directories or non-git agent directories.
Sessions start in the base clone and move into worktrees. Nested worktrees and subagent streams belong to the root session's cwd. With one or two paths, the request fits a GET, so the two POST routes, the body bound, the POST-with-query 400 and the deploy-order hazard can all go. If D1 is not taken, the echo would then become load-bearing.
What this costs: a session launched directly inside a sibling worktree would be missed. None appears in the sample, and the scope above is the only scope I measured.
Alternatives I considered and did not recommend:
- A server-side git-common-dir identity, or sending one common-dir path. The server cannot map a cwd to a repo (the index stores a cwd only), so this needs new capture-time data. It is larger, not simpler. It would also fix the removed-worktree limit, but per the measurement above that limit is immaterial.
- Filtering only on the client. This needs no server change, but
--limitand top-N would be taken from every repo's hits before filtering. - Raising nginx's buffer instead of using POST. Every worktree path would then sit in access logs.
Not raised here
- The
log querySQL verb from feat(log): read-only SQL over the transcript index, single-user (v1 Phase 2) #328 is unscoped. That is a separate decision, not a gap in this requirement. feat(log): list and search default to the current repo's sessions (--all-repos to widen) #329 does not depend on it, and feat(log): read-only SQL over the transcript index, single-user (v1 Phase 2) #328 is still a draft. If the default is meant to be "every caller gets the guard", that premise belongs in feat(log): read-only SQL over the transcript index, single-user (v1 Phase 2) #328's own round 0. - Deploy order (cairn-ui before the client) is an operational note, not a premise.
D1 — delete the cwd_under echo (transcriptwire echo fields, the breach() echo compare, the missing-echo exit-3 branch, its tests): POST-only + pre-PR listener ⇒ 405 already exits 3, and the per-hit outside-scope check already blocks any leak
D2 — replace git-worktree-list enumeration + the two POST routes/body bound with main-worktree (+ current toplevel) cwd_under over GET: measured 0 sessions whose first cwd is a sibling worktree (cairn 38/38 = base clone; 219 sessions Nov-01..Dec-04 under /home/zach)
Q1 — the default scope is per SESSION keyed on its FIRST cwd, so a session launched in repo A that then works in repo B (incl. a client repo) is fully inside A's default; is session-granular scope acceptable for the operator's ask, or must the guard be per-record (per-unit cwd in the index, a capture/index change)? · forcing: user
|
Operator answer to the round-0 premise (relayed from the session where the operator was asked): |
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
5f9e69c to
0985d29
Compare
…n place Operator decision on #329's round-0 Q1: `cwd_under` and `cwd` keep RECORDS, not sessions. The index kept one cwd per session (its first), so a session launched in repo A that then worked in client repo B answered B's records to A's search. - Each indexed unit now carries its own record's cwd (`IndexUnit.Cwd`): a Claude Code record naming none takes the last one its stream named (tracked over the whole stream, so a batch starting mid-stream carries it); an opencode unit takes its session's directory; a unit with none matches no directory filter. - Search filters `transcript_units.cwd`, and a hit's cwd is its unit's. - List reads a new `transcript_session_cwds` table, one row per (session, directory), merged across batches: a session is listed when ANY directory passes, answering the earliest-first passing directory, ITS first prompt and the span over every passing one. No unit is scanned. - `--cwd` is per record the same way (a prefix): a behaviour change, documented. - Migration 5 adds the column (no rewrite) and the table and zeroes every index cursor, so the indexer's startup backfill re-reads every stream and replaces rows by key. Until a root is re-read its units carry cwd '' and the scoped search answers less, never more; `--all-repos` is unaffected. - Round-0 D1: the `cwd_under` echo and its exit-3 branch are deleted. A 405 on the scoped POST stays exit 3, and the client's per-hit/per-row check stays exit 5 (exact now that the listener filters per record). - `log query` (#328) is left unscoped (a separate decision). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
|
Audit round 1 (read-only, head 0985d29), 0 🔴, so the ladder ends. Findings were handed to the operator. Summary: 🟡 the indexer now map-decodes every record before the cursor on every pass (index.go:727). 🟡 opencode per-unit cwd is untested: the mutant that sets it to "" survives. 🟡 the default scoped search falls to the all-time step and Partial much more often for small repos. 🟢 the v4 rollback |
`cairn log list|search` now print only sessions whose cwd is, or lies below, a worktree of the git repository --repo names (default: the working directory). `--cwd PREFIX` replaces that default, `--all-repos` removes it, the two together are exit 2, and outside a git repo with neither the verb refuses (exit 2) instead of searching everything. stderr names what was covered: `cairn: sessions=repo:<main worktree> worktrees=<N>`, `sessions=cwd:<PREFIX>` or `sessions=all-repos`. `log read` is unchanged. Why: `--repo` picked the instance and filtered nothing, so a search from one repo printed other repos' transcript content into the calling session's own (captured) transcript. Mechanism: the stored records carry `cwd` and `gitBranch` and nothing that identifies a repository, so the client resolves the set itself with `git worktree list --porcelain` and sends one `cwd_under` per outermost worktree. The listener keeps a session whose cwd IS one of them or lies below one (transcriptwire.CwdUnderAny, at a path-component boundary; the same rule in pgstore SQL, pinned by the Postgres tier) and echoes the set. A plain prefix cannot work: worktrees are sibling directories, and the prefix that names them also names unrelated `<repo>-*` repos. The scoped request is a POST with a form body (new rows in the client listener's route ledger): one path per worktree is tens of KiB on a repo with hundreds of worktrees, past a proxied GET request line. Fail closed against an older listener: a 405 on the POST, or an answer without the `cwd_under` echo, is exit 3 with nothing printed; an echoed answer naming a session outside the set is exit 5, nothing printed. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The ledger-class probe ran `log search` with no scope flag, which outside a git checkout (the nix sandbox, the mutant copies) is now the exit-2 refusal; it passes --all-repos, since it measures the ledger class, not the scope. The binary's Postgres-tier test counts the listener's 11 routes and drives a POSTed cwd_under search through the real listener and SQL. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…n place Operator decision on #329's round-0 Q1: `cwd_under` and `cwd` keep RECORDS, not sessions. The index kept one cwd per session (its first), so a session launched in repo A that then worked in client repo B answered B's records to A's search. - Each indexed unit now carries its own record's cwd (`IndexUnit.Cwd`): a Claude Code record naming none takes the last one its stream named (tracked over the whole stream, so a batch starting mid-stream carries it); an opencode unit takes its session's directory; a unit with none matches no directory filter. - Search filters `transcript_units.cwd`, and a hit's cwd is its unit's. - List reads a new `transcript_session_cwds` table, one row per (session, directory), merged across batches: a session is listed when ANY directory passes, answering the earliest-first passing directory, ITS first prompt and the span over every passing one. No unit is scanned. - `--cwd` is per record the same way (a prefix): a behaviour change, documented. - Migration 5 adds the column (no rewrite) and the table and zeroes every index cursor, so the indexer's startup backfill re-reads every stream and replaces rows by key. Until a root is re-read its units carry cwd '' and the scoped search answers less, never more; `--all-repos` is unaffected. - Round-0 D1: the `cwd_under` echo and its exit-3 branch are deleted. A 405 on the scoped POST stays exit 3, and the client's per-hit/per-row check stays exit 5 (exact now that the listener filters per record). - `log query` (#328) is left unscoped (a separate decision). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…d search to its sessions - 🟡1 The carried cwd is stored WITH each stream's cursor (`Cursor.Cwd`, `transcript_index_cursors.cwd`, added to the still-unreleased migration 5), so a pass decodes only the records after its cursor instead of map-decoding every earlier record to relearn the directory. - 🟡2 Opencode per-record cwd is tested (transcript + Postgres tier): the mutant that blanks it now dies. - 🟡3 A scoped search first names its sessions from transcript_session_cwds (`scopeRoots`) and both steps read only their units through the (root, at) index; a scope whose records all lie in the window (no untimed unit) is answered by the window step alone, never Partial. EXPLAIN ANALYZE on a 300k-unit fixture: the scoped all-time step went 19.7 ms (parallel seq scan, ~300k rows removed by filter) -> 1.25 ms (bitmap on (root, at)); no index on the per-unit cwd is needed. - 🟢4 The documented index DROP recipe includes transcript_session_cwds, and a Postgres-tier test runs it as written. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
0985d29 to
f9fc141
Compare
What
cairn log listandcairn log searchnow default to the current repository's records, across every git worktree of it, and widen only with the new--all-repos. The scope is per RECORD (operator decision on round-0 Q1): a search keeps only the hits whose own record ran in the repo. A list names the sessions with any record there.Why:
--repopicked the instance and filtered nothing, and the index kept ONE cwd per session (its first). A search from one repo therefore returned other repos' records, client content included, and printed them into the calling session's own captured transcript. Per-session scoping alone would still have answered repo B's records to repo A whenever a session launched in A and then worked in B.Rebased onto
mainafter #328 (log query). ⚠log queryis unscoped: its SQL reads every session of the caller. Scoping it is a separate decision and is deliberately not in this PR.Final CLI contract
list/search)cairn: instance=…when printed)--repo P)git worktree listnames for that repocairn: sessions=repo:<main worktree path> worktrees=<N>--cwd PREFIXcairn: sessions=cwd:<PREFIX>--all-reposcairn: sessions=all-repos--all-reposwith--cwd--reponaming a non-repo--cwd PREFIXand--all-reposcwd=is now the hit record's own directory.--all-repos) list is unchanged.--all-repos.--repo Psets the repo and still routes the instance.log read SESSIONis unchanged.log queryis unscoped.no matchesincluded)Nothing is printed on stdout in the 3 and 5 cases this PR adds.
cmd >out 2>err; echo $?: exit 3, 0 bytes of stdout, on both the installed client and this branch. It was already the documented 3, so nothing changed there.Mechanism
internal/transcript/index.go).IndexUnit.Cwdis the record's own cwd. The Claude indexer tracks the cwd in force over every record of the stream, including records at or before the cursor, so a batch that starts mid-stream still carries the directory an earlier batch saw.transcript_units.cwdstores it. Search filtersu.cwd, bothcwd_under(transcriptwire.CwdUnderAny/cwdUnderSQL, a path-component boundary) andcwd(a prefix), and a hit's cwd is its unit's.transcript_session_cwdstable has one row per (session, directory): the first human prompt of the main stream recorded there, plus the first and last record times there. It is a handful of rows per session. Each batch's rows are merged into it: the first prompt is kept once set, and the span only widens. A filtered list usesDISTINCT ON (root)over the passing rows withmin/maxwindow functions, and never touchestranscript_units.cwd_underper outermost worktree fromgit worktree list --porcelain. With per-record cwd, records written inside sibling worktrees now actually show those paths. The request isPOST /client/v1/log/{list,search}with a form body, because hundreds of worktrees would overflow a proxied GET request line.cwd_underecho and its missing-echo exit-3 branch are deleted. A listener older than this PR has no POST route, so it answers 405 and the client exits 3. The client still re-checks every hit and list row against the scope (exit 5), and that check is exact now that the listener filters per record.Deploy order and the backfill (operator steps)
cairn-uiimage first. On start it applies migration 5:ALTER TABLE transcript_units ADD COLUMN cwd … DEFAULT '', a catalogue-only change with no table rewrite;CREATE TABLE transcript_session_cwds;UPDATE transcript_index_cursors SET through = 0.DefaultUnitsPerSecond), like the original backfill. While a root waits, its units carrycwd = '', which no directory filter keeps. So the scoped search fails closed and answers less, never more, and--all-reposanswers everything throughout. Track progress on the primary:pendingshould fall to the units whose records name no directory at all.log list|searchfrom the new client exits 3 (405) with nothing printed.--cwdand--all-reposare GET and keep working, but against an old listener--cwdis still session-level.Rollback:
DELETE FROM schema_migrations WHERE version = 5;lets an older build start. The column and table stay and are ignored. Re-upgrading zeroes the cursors again and re-reads everything (refuseFromTheFutureinmigrate.go).Tests: red at 5f9e69c (the previous head), green at this head
I ran the new tests against 5f9e69c's production code and its own (session-level) index test double.
transcriptTestTheRepoScopeIsPerRecord. The session launched in/w/aand worked in/w/b-client: A's search returns only A-cwd records, B's returns only B-cwd records (including one that inherits its cwd), and a name-prefix sibling is excluded. List semantics (in-scope dir, its prompt, span) and--cwdper record are covered too.cwd=/w/a, and B's search missed the session entirelycairn-uibinary, Postgres tier: one session's two records (/work/alpha, then/work/bravo-client) POSTed through the real listener and real SQL; each directory returns only its own record; list under B shows B's prompt/work/alphasearch answered the client-tree record, labelledcwd=/work/alphapgstoreTestTheRepoScopeIsPerRecordInSQL (search and list per record, and the merge of per-directory rows across batches)pgstoreTestMigrationFiveReindexesAVersionFourDatabaseInPlace (a v4 row gets cwd''and cursor 0; the scoped search returns nothing while the unscoped one still answers; the re-read by key leaves one row, now in scope)transcriptTestMigrationFiveReindexesInPlace (the same backfill on the indexer: no duplicates, and fail-closed beforehand)transcriptTestTheRecordCwdCarriesAcrossPassesAndTheFirstPromptStaysgo testpasses for./internal/client/ ./internal/clientapi/ ./internal/transcript/... ./internal/transcriptwire/, both in a checkout and in a copy with no.git. The full Postgres tier for./internal/pgstore/ ./cmd/cairn-ui/passes on a throwaway unix-socket Postgres 18 (its own PID recorded and stopped). Other changes: the pgtest declaration ledger, the Guard C surface ledger (new unexported row fields, no memo text), and the route counts after #328 (11 without the query role, 12 with it).Mutation results (Go and SQL; each restored; positive control included)
s-idx-ab#4@with an empty cwd)cwd_underons.cwd--cwdprefix ons.cwds.cwd, not the per-directory rowscursor is 7, want 0)RepoWorktreeskeeps only the first worktreeRound 1 fixes (audit 7414863049; no 🔴 — not re-audited, each tested and mutation-checked)
Rebased onto
mainafter #330 (5f362a4); the only conflict wasCHANGELOG.md(both rows kept, this one on top).transcript.Cursor{Through, Cwd},transcript_index_cursors.cwd, added to migration 5, which has not been applied outside tests), so a pass decodes only the records after its cursor: O(new records)TestAPassResumesTheCarriedCwdFromItsCursor: a pass decodes only record 2 and takes its directory from the stored cursor. Mutants: decode before the cursor skip →decoded [1 2]; ignore the cursor cwd → killed (with the carry test). pg tier: the cursor stores/work/alphapast a cwd-less record (mutant "cursor cwd not stored" killed)TestAnOpencodeUnitIsScopedByItsSessionsDirectory(transcript), and scoped searches over the opencode and Claude sessions in the pg-tier real-index testscopeRootsreads the scope's sessions fromtranscript_session_cwds, and both steps addu.root = ANY($11), so they read only those units through the(root, at)index. A scope whose passing directories all start inside the window, and which holds no untimed unit, is answered by the window step alone. A scope naming no session answers nothing without running the text searchTestASmallRepoScopedSearchIsAnsweredByTheWindowStep, with the all-time step stalled: the scoped search returns its hit, not Partial, from the window step alone narrowed to its session (control: unscoped → Partial).TestAScopedAllTimeStepReadsOnlyTheScopesSessions: an inconsistent "liar" session is kept out only by the narrowing; an untimed unit forces the all-time step. Mutants killed: no root narrowing, never window-only, ignore untimed, roots not passedtranscript_session_cwdsfirstTestTheDocumentedIndexDropSucceedsAndReUpgradesruns the recipe as written, then re-upgradesEXPLAIN ANALYZE (PG 18; 300k units in 3,000 sessions; 10 sessions in the scoped repo; a word in 60% of units), scoped all-time step:
(root, at), 1,000 rows, 400 removed by the cwd filter)The per-unit
cwdcolumn needs no index. The narrowing inside the window step's session sub-select is performance only, because the outer clause already enforces it, so no test pins it.Operator note, migration 5 (deploy step 1): it also adds
transcript_index_cursors.cwd. The progress query and the rollback are unchanged. The DROP recipe now includestranscript_session_cwds.Remaining limits
--all-repos.--cwdanswers against the prefix, so--cwdagainst an old listener stays session-level until step 1 above.🤖 Generated with Claude Code