Skip to content

feat(source-control): reap plugin install records from a WorktreeRemove hook - #10167

Merged
melodic-automation-lanes[bot] merged 3 commits into
mainfrom
work-resolve-issue/issue-7457-40261933669-1
Oct 26, 2026
Merged

melodic-automation-lanes[bot] merged 3 commits into
mainfrom
work-resolve-issue/issue-7457-40261933669-1

Conversation

@melodic-automation-lanes

Copy link
Copy Markdown
Contributor

Closes #7457

Summary

Opened by the work-resolve-issue lane. The run's own summary:

Adds a WorktreeRemove hook (hooks/worktree-remove-gate.sh). It runs when Claude Code removes a worktree this plugin's WorktreeCreate hook created, reaps that worktree's project- and local-scope plugin install records through scripts/reap-project-plugin-records.sh, and then removes the worktree itself with plain git worktree remove. Harness-removed worktrees no longer leave stale records. The hook leaves the worktree on disk and exits non-zero when it finds uncommitted or untracked work, an operation in progress, submodules, a detached HEAD holding commits no ref holds, a lock naming another session, or a failed reap. The audit Step 2b doc, README, prerequisites and the changelog record the change; item 2 of the issue had already shipped in #8022.

Not verified

  • The issue's test checks (scripts/run-test-file.sh plugins/source-control/hooks/worktree-remove-gate.test.sh and scripts/run-test-file.sh plugins/source-control/scripts/reap-project-plugin-records.test.sh) were not run: this lane's Bash runs only the named wrappers, and check-affected runs without --
  • No live Claude Code probe was run, because the lane cannot start one. Unconfirmed in practice: which session_id the WorktreeRemove payload carries after /clear, a resume, a background-session delete or a remote-control cleanup. The hook unlocks only a lock naming that session and otherwise refuses,
  • CI-only repository gates were not run locally: killswitch-hoist, hook-exec-form, hook-slow-shapes, hooks-description, test-tmp-cleanup, silent-skips, declared-prerequisites and the rest of the list check-affected prints. The new files were checked against them only by reading; CI runs them.
  • Hook branch deletion is not done: the hooks docs say Claude Code never deletes a hook-created worktree's branch. This hook keeps it on purpose (deleting it would destroy commits nothing else holds), so branches of harness-removed worktrees still accumulate, as they did before. The issue does not ask

Fix

  • plugins/source-control/hooks/worktree-remove-gate.sh: new WorktreeRemove hook. Guards, in order: absolute path, not a symlink, a linked git worktree; no merge, rebase, cherry-pick, revert, sequencer or bisect in progress; a clean git status; no submodules; no detached HEAD holding commits no ref hol
  • plugins/source-control/hooks/worktree-remove-gate.test.sh: new suite with throwaway git fixtures and a stubbed claude CLI. Cases: a clean worktree has its records reaped and is removed, with its git entry gone and its branch kept. Uncommitted and untracked changes refuse with nothing reaped. A reap
  • plugins/source-control/hooks/hooks.json: registers the WorktreeRemove hook (exec form through exec-bash.mjs, timeout 300) and extends the top-level description.
  • plugins/source-control/prerequisites.json: adds hook:worktree-remove-gate.sh to jq's users and fixes jq's degrade text (this hook refuses instead of skipping); declares the claude CLI as required for the hook.
  • scripts/prerequisites-baseline.txt: drops the 'source-control claude' gap row, which declaring claude retires (the gate fails on a stale baseline row).
  • plugins/source-control/skills/worktree/context/audit.md: Step 2b says harness-removed worktrees (a --worktree exit, ExitWorktree, a background-session delete) are reaped by the WorktreeRemove hook, and that subagent and workflow-agent worktrees are not, with a pointer to the hooks docs section, an a
  • plugins/source-control/skills/pull-request/reference/create.md: the 'Why here (not WorktreeRemove hook)' line no longer says a WorktreeRemove hook cannot block removal; it says such a hook cannot prompt and that this plugin's compares no carried file.
  • plugins/source-control/README.md: new worktree-remove-gate section under Hooks, plus a Requirements bullet for the claude CLI.
  • .changes/source-control/feat-worktree-remove-reap-7457a1c3.md: minor changelog fragment (Added: the hook; Changed: a dirty harness-removed worktree now stays on disk). Written by hand because new-changelog-fragment refuses on a detached HEAD.

Verification

The runner ran each check from the base copy against 998436f:

  • scripts/check-changelog-fragments.sh --check: passed

The run reports:

  • read https: //code.claude.com/docs/en/hooks (WorktreeRemove and WorktreeRemove input sections): the event fires only for a worktree a WorktreeCreate hook created (an interactive worktree session exit, a background-session delete, ExitWorktree, claude remote-control cleanups). It does not fire when a
  • /home/runner/work/claude-code-plugins/claude-code-plugins/.lane/checks/bin/check-changelog-fragments: all 6 fragments valid
  • /home/runner/work/claude-code-plugins/claude-code-plugins/.lane/checks/bin/check-affected: 10 passed, 6 skipped, 0 failed over 9 changed files (markdown, shellcheck, shell-portability, exec-bit, changelog-fragments, stale-base-overlap, validate-plugins, spoke-plugin-root, plugin-options-docs and cha
  • /home/runner/work/claude-code-plugins/claude-code-plugins/.lane/verify/bin/cant-fail-scan --file plugins/source-control/hooks/worktree-remove-gate.test.sh --check: PASS, no gating findings
  • fresh-context review subagent over the whole diff: traced every test assertion and found each would pass on Linux with git >= 2.36, and found that the six named lint gates would not flag the change. I fixed its confirmed findings: submodule worktrees reaped and then refused by git, sequencer/ mis
  • The lane run: https://github.com/melodic-software/claude-code-plugins/actions/runs/40261933669

Left out: the items under Not verified.

Related

#7457

Lane-Activity: work-resolve-issue/resolve-issue
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

claude-security-review reviewed cf621bf9a1a63f19429a3a0a1118cbd33035ee08: 0 finding(s), 0 as inline comments.

No security issues found.

critical-findings: 0

Scope. I read all 9 changed files and the PR context. The change adds a WorktreeRemove hook, plugins/source-control/hooks/worktree-remove-gate.sh, plus its registration, tests, docs and prerequisites. No file on the trusted-path list changed. I also read reap-project-plugin-records.sh and worktree_reason_is_ours in scripts/lib/worktree-facts.sh, which the hook calls.

Manifests. Neither .claude-plugin/plugin.json nor marketplace.json changed, so there are no manifest <field> lines. Installed users do get one new hook on update: hooks/hooks.json registers worktree-remove-gate.sh through the existing exec-bash.mjs launcher. The script is in this repository and uses ${CLAUDE_PLUGIN_ROOT}, so there is no external source and no unpinned reference.

Hook.

  • Path handling. worktree_path comes from the Claude Code WorktreeRemove payload. jq -r '... | strings' reads it, so no shell expansion happens. Every later use is quoted and passed as an argument (git -C "$path", bash "$reap" --worktree-path "$path"), and the path is passed as a value rather than parsed as an option.
  • What it will remove. Before it removes anything, the hook requires an absolute path that is not a symlink. The path must be a directory with a .git file, and git rev-parse must report a git dir that differs from the common dir. A main working tree, a plain directory or a relative path is refused with exit 1 and left on disk. The only thing it deletes is a linked worktree, and it uses plain git worktree remove with no --force.
  • Data safety. Uncommitted or untracked files, an operation in progress, submodules, and a detached HEAD holding commits no ref holds all make it refuse. This removes a data-loss path that existed before, when Claude Code's git fallback discarded dirty work. Gitignored files are not compared, and the change documents that.
  • Lock handling. The hook unlocks a worktree only if the lock reason contains session <session_id> since. The session_id is inside a quoted pattern in worktree_reason_is_ours, so it matches literally with no glob injection, and an empty session_id never matches. A lock held by another session or with no reason is refused, and the lock is restored if git worktree remove then fails.
  • Reap ordering. All refusal checks run before the reap. A failed reap leaves the directory in place so the records stay reachable. The reap helper does its own main-working-tree refusal.
  • Secrets and network. The change adds no token handling and makes no network calls.

Instruction-surface edits. The create.md and audit.md edits reword prose. The create.md edit drops "WorktreeRemove hooks cannot block removal"; that sentence is now false for this hook, which does block removal. The .worktreeinclude sync it describes is a user-confirmed checkpoint and is not enforced by a deletable rule, so nothing is left unchecked. I did not consult the instruction exception register.

Cost. The change adds no paid plan or billing setting. The hook runs locally with a 300-second timeout.

@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

claude-review reviewed cf621bf9a1a63f19429a3a0a1118cbd33035ee08: 0 finding(s), 0 as inline comments.

No blocking or advisory findings on this PR. I read the whole diff and the PR context. Beyond that I read only the code the new hook calls: scripts/lib/worktree-facts.sh (worktree_reason_is_ours), the --worktree-path and settings-restore logic in scripts/reap-project-plugin-records.sh, and the existing WorktreeCreate registration in hooks.json. I ran no tests or commands, so the PR's "checks passed" lines are author-claimed and unverified by me.

Closing issue #7457. The criteria are met.

  • hooks.json registers a WorktreeRemove hook.
  • The new test file covers a reaped and removed worktree with its git entry gone, a dirty or untracked worktree left on disk with a non-zero exit, and a reap failure that leaves the directory and exits non-zero.
  • audit.md Step 2b now says harness-removed worktrees are reaped by the hook and subagent worktrees are not.
  • The changelog fragment is present and its bump is minor.
  • Item 2 shipped earlier in feat(source-control): script the worktree audit Step 2b recreate-and-reap remedy #8022.
  • The unchanged reap test is the PR's own claim and I did not run it.

What I traced in the hook.

  • The reap helper's --worktree-path interface and its restore of a tracked, unmodified .claude/settings.json exist as the hook assumes. The hook's "plain removal still succeeds" path therefore holds for the tracked case.
  • worktree_reason_is_ours matches session <sid> since exactly, so a lock naming s10 is not claimed by s1.
  • Every guard refuses before the reap runs, apart from the post-reap unlock and remove failures, which the hook reports as "records already reaped".
  • Prerequisite declarations and the removed baseline row are consistent with each other.

Residual risks, not counted as findings. The PR states these itself or they fail safe.

  • Which session_id the WorktreeRemove payload carries after /clear, a resume or a remote-control cleanup is unverified live. A mismatch with a lock the create gate took leaves the worktree on disk, so it fails safe but would leave stale records.
  • Users with no claude CLI on the hooks' PATH (some desktop or IDE setups) now keep every harness-removed worktree on disk until /source-control:worktree cleanup. Previously Claude Code removed it with git. The README, the changelog and prerequisites.json all document this.
  • Gitignored files are not compared before the removal, which the hook header states as intended.

Manifest items. None. No plugin.json or marketplace.json change; the release uses a changelog fragment with bump: minor, which fits a new hook.

Spend. Nothing in this change draws on a paid plan or billing setting. The new hook timeout of 300 seconds applies to each worktree removal and is local compute only.

Comment thread plugins/source-control/hooks/hooks.json Resolved
Comment thread scripts/prerequisites-baseline.txt Resolved
@melodic-automation-lanes

Copy link
Copy Markdown
Contributor Author

pr-unblock / address-feedback finished round 1 of max 4 on 3f4c79c.

@melodic-automation-lanes melodic-automation-lanes Bot added the needs-human Human-in-the-loop required; autonomous sessions must not resolve items carrying this. label Oct 26, 2026
@melodic-automation-lanes

Copy link
Copy Markdown
Contributor Author

The pr-unblock lane stopped on this pull request and needs a person: a disputed review finding is one only a person decides (a security-review finding, a thread a person directed, or one an adjudicator found stands and the next round left open again).

  • Reason: feedback-disputed
  • Resume key: 3f4c79cb7466fbc9ad610e9f0bac61cb0a289881 (the head the lane stopped on)

To resume, deal with the reason above, remove the needs-human label (and do-not-merge, if set), then dispatch the pr-unblock workflow with pr-number=10167 and activity=address-feedback:

gh workflow run pr-unblock.yml -R melodic-software/claude-code-plugins -f pr-number=10167 -f activity=address-feedback

@kyle-sexton kyle-sexton removed the needs-human Human-in-the-loop required; autonomous sessions must not resolve items carrying this. label Oct 26, 2026
@melodic-automation-lanes

Copy link
Copy Markdown
Contributor Author

The unattended merge path armed native auto-merge at head cf621bf9a1a63f19429a3a0a1118cbd33035ee08.

@melodic-automation-lanes
melodic-automation-lanes Bot added this pull request to the merge queue Oct 26, 2026
Merged via the queue into main with commit 387aa94 Oct 26, 2026
87 of 90 checks passed
@melodic-automation-lanes
melodic-automation-lanes Bot deleted the work-resolve-issue/issue-7457-40261933669-1 branch October 26, 2026 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(source-control): reap project-scope records when Claude Code removes a worktree, and script the Step 2b backlog remedy

1 participant