Repository navigation
fix(cleanup): read ownership from creation stamps, not transcripts - #324
Conversation
Cleanup refused its own worktrees and branches as "named by a live session", and the naming came from cleanup itself: every scan printed every worktree and branch in the repo into that session's transcript, so after one cleanup every open session named everything. A second scan in the same session also marked unrelated branches as its own. And a cleanup refused the worktree it ran in, because the session's own processes had their cwd there. A new stamp-ownership hook snapshots worktrees and branches either side of each git/gh Bash call and EnterWorktree, and stamps what is new and named by the call, inside git: a claude-session file in the worktree's admin dir, and branch.<name>.claudeSession. Git deletes both with the thing. cleanup.mjs now reads only those stamps: self is removed, another live session's is counted and never touched or printed, and an orphan (a dead session's, or unstamped) is pruned with --orphans when nothing would be lost. --stale is gone. This session's own processes (its session id in their env, or descended from CLAUDE_PID) no longer hold a worktree, and the skill calls ExitWorktree keep before removing the worktree the session is in. Co-Authored-By: Claude Opus 5.5 <[email protected]>
- The hook stamps whatever a creating call makes, first stamp wins, instead of only what the call names: naming missed ../x worktrees, gh pr checkout and a nameless EnterWorktree, and let `git commit -m fix` claim a neighbour's `fix`. It snapshots only around calls that can create (worktree, branch, switch, checkout), not every git/gh call. - Register it for PostToolUseFailure too: a creating chain that exits non-zero still created. - The stamp format moves to workflow/lib/stamps.mjs, used by the hook, cleanup.mjs and both test suites. - cleanup.mjs runs every command at `/`: its own lsof, started in the worktree, was what held the worktree a session ran in. The blanket own-process exemption goes with it, so a build still running there keeps its worktree again. - namedIn loses its unreachable name half, scan counts a live session's worktree before crediting changes in it, and prose no longer says cleanup touches nothing a running session uses. Co-Authored-By: Claude Opus 5.5 <[email protected]>
sydevs-bot
left a comment
There was a problem hiding this comment.
🧐 Adversarial review
Writing ownership into git at creation is the right shape: a git-native record beats the transcript grep it replaces, and the laptop state measured in docs/why.md#cleanup-touches-only-its-own-session justifies keeping a neighbour protection at all, rather than deleting the refusal and carrying the risk. The one-line cwd: '/' fix belongs here, and one hook plus one lib module is no larger than the question cleanup cannot answer alone.
Two things to settle. The feature rests on tool_use_id reaching a PreToolUse hook; nothing verifies that, and the failure is silent and total. The accepted-risk note also promises more than the code gives: an orphan prune can drop a neighbour's dev database.
Findings
should-fixstamp-ownership.mjs:92— unverifiedtool_use_idon PreToolUse. Absent, nothing is stamped and nothing says so.should-fixdocs/why.md:1068— "safe on GitHub" misses the dev databaseteardowndrops.should-fixStreamline ×4 —stamp-ownership.mjs:33three utilities defined elsewhere,:73branch data nothing reads,:101an unmeasured temp sweep;stamps.mjs:29hand-parsedgitdir:.
What was checked
Read whole on the branch: cleanup.mjs, stamps.mjs, stamp-ownership.mjs, hooks.json, cleanup/SKILL.md, the why.md and README hunks. The 37 new tests were listed, not run. The ladder covered the hooks.json entries and the skipped files: no dependency added. Streamliner: 35 pieces covered, 31 kept. rule-delta 94 → 94, none removed. version is bumped, README's hook count, the why.md anchor and every path the body names check out, and loop-config.json is untouched. No ## Preview link and no fixture in .claude/workflow.json, so no data check applies. Dismissed: ownerOf cannot read an unstamped branch as self, because loadSession exits without an id.
🤖 Written by the sydevs autonomous loop — see what this is
Generated by Claude Code
`adminDir` parsed `gitdir:` out of the worktree's `.git` file. `git rev-parse --path-format=absolute --git-dir --git-common-dir` answers the same question: the two paths differ only in a linked worktree, so a main checkout still gives null. It also fixes a case the hand-parse got wrong. A `.git` file whose admin directory git has already dropped still parsed, so `stampWorktree` wrote a stamp into a directory nothing reads. rev-parse fails there, and the stamp is skipped. Addresses #324 (comment) Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjZHnNizRaus1DpCe19
The hook defined `git`, `lines` and `repoOf` itself, and all three already existed: `git` in `lib/stamps.mjs`, `lines` and `repoOf` in `skills/cleanup/cleanup.mjs`. Both files already import `stamps.mjs`, so the three move there and the copies go. `cleanup.mjs` keeps its own `git`, which routes through `attempt` so a failure can be reported as a blocker. Its `repoOf` discards the error either way. Addresses #324 (comment) Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjZHnNizRaus1DpCe19
`snapshot` recorded each worktree's checked-out branch, and nothing read it: the post event compares worktree paths, and a new branch comes from `branches`. The worktrees are an array of paths now, compared with `includes`. Addresses #324 (comment) Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjZHnNizRaus1DpCe19
The hook returned unless the payload carried `tool_use_id`. Nothing verifies that a PreToolUse event sends it, and absent it the whole feature failed silently: no snapshot, no stamp, no output, in a repo whose change cannot be tested by running it. `hooks.test.mjs` wrote the field into its own payloads, so the suite could not catch it. The key is `tool_use_id ?? session_id`. Two creating calls at once then share a snapshot, which costs a stamp and never misplaces one, because the later snapshot already holds what the earlier call made. The Pre event now writes the file even when there is nothing to snapshot. A session-keyed file is reused across calls, so an abandoned call must not leave one for a later call's post event to read as its own. A test drives a creating call whose payload has no `tool_use_id`. Addresses #324 (comment) Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjZHnNizRaus1DpCe19
The Pre event swept the snapshot directory for files older than a day, at a readdir plus a stat per file on every creating call. It defended against a few hundred bytes of JSON per abandoned call in the OS temp directory, which the OS sweeps itself. A stale snapshot could never be read in any case: a `tool_use_id` key is unique to one call, and a session key is overwritten by that call's own Pre event, which now always writes. Addresses #324 (comment) Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjZHnNizRaus1DpCe19
The accepted-risk note claimed the work is safe on GitHub. A dev database is not: `remove` runs `dev-server.sh teardown` first, and `foreignDb` clears a same-repo worktree's database because its name fits the main checkout's template. So a pruned worktree's database goes with it. The note now says so, and says why teardown is not restricted to `self`: that would strand a database on every orphan prune, and the removal is the one moment teardown can still run. Addresses #324 (comment) Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Mg9rjZHnNizRaus1DpCe19
|
Six review threads, all adopted. Each reply has the detail.
Base already current with 🤖 Written by the sydevs autonomous loop — see what this is Generated by Claude Code |
There was a problem hiding this comment.
Approved for Ardnived, who wrote it: GitHub refuses an author's own approval. CI is green and the automated review has run. Enable auto-merge when you want it merged: the queue tests it against its base. 🤖 Written by the sydevs autonomous loop — see what this is
Summary
scanprinted every worktree and branch into its own transcript.Change outline
Flow — who owns a worktree or branch
The owner is now written when the thing is created, instead of inferred later from transcript text.
Behaviour — the worktree the session runs in
removerefused this worktree because its ownlsof, started inside it, listed itself as a holder. Every command now runs at/. A real process still running there, this session's included, keeps the worktree. The skill callsExitWorktree keepfirst, so the session leaves the directory before removal.Files
Test results
claude plugin validate ./workflow --strictandnode --test workflow/lib/*.test.mjs, 194 passedcleanup.test.mjs17,hooks.test.mjs21, all passedManual verification
/plugin→ update, so the hook is installed. Work in a session that creates a worktree, then run/workflow:cleanupfrom inside it while another session is open. The worktree and branch are removed, with no "a live session names it".Notes for reviewer
iffilters would remove that startup cost. They are not used, because the minimum Claude Code version that supports them is unverified.docs/why.md#cleanup-touches-only-its-own-session:database goes with it. Restricting
teardowntoselfwould instead strand a database on everyorphan prune, and the removal is the one moment
teardowncan run.git/ghtext, such as a script, leaves its item unstamped, so it becomes an orphan. That is the safe direction: it is pruned only when nothing would be lost.--resumekeeps the session id, and only--fork-sessionmints a new one. A fork's old stamps become orphans under the same safety rule.gh pr checkoutnames no branch). Theunsafeandlivechecks still apply.Detail
../xworktrees,gh pr checkoutand a namelessEnterWorktree, and thatgit commit -m fixclaimed a neighbour'sfix. The final version stamps whatever is new, and the first stamp wins.lsof, and let a still-running build lose its worktree. The exemption is gone.ps -E(/bin/sleepshows none,nodedoes). The tests therefore use a node child as the holding process.scanon an 81 MB transcript went from 21.7 s to 3.0 s, because no other session's transcript is read now.rule-delta: 94 → 94 directives, none removed.🤖 Written by the sydevs autonomous loop — see what this is
🤖 Generated with Claude Code