Repository navigation
fix(bin): keep claude crewmate hooks out of the project worktree - #1627
lochlann-bit wants to merge 5 commits into
Conversation
fm-spawn installed a claude crewmate's turn-end and busy-state hooks by writing <worktree>/.claude/settings.local.json wholesale. In a project that tracks that path, the committed content was destroyed: on 2026-08-03 a real clone had 300 committed lines of permission rules replaced by a single hook line, every claude crewmate there started on a modified tracked file, and teardown could refuse because the worktree was dirty. Writing into a project's tracked content also violates firstmate's own prime directive. The hooks now live in state/<id>.claude-settings.json, outside the worktree, and reach the session through `claude --settings <file>` on the launch command - the same shape pi already uses for its extension, and the same outside-the-worktree principle grok and kimi already follow. Nothing is written into the project, so there is no project file to parse, merge, or unpick later, and no unparseable-file case to guess at. Verified live on Claude Code 2.1.220 that --settings hooks fire in a real interactive session and load additively: the project's own .claude settings still apply to the crewmate, which the previous wholesale write had silently taken away. A secondmate gets no per-task hooks (its own home's tracked .claude/settings.json owns them), so its launch template omits the flag. A raw claude launch command, the unverified-adapter escape hatch, cannot receive the flag by substitution, so fm-spawn now warns loudly and names the argument to add rather than starting a worker whose turn-end wake never fires. Teardown retires the new state artifact and no longer deletes the worktree path unconditionally. A task launched before this change can still carry the old file, so cleanup removes it only when it is untracked in that worktree AND its content names this task's own firstmate hook wiring; a project's own file, tracked or not, is left alone. Compatibility reviewed across every supported harness and runtime backend. codex (launch-command notify=), pi and pi-signed (extension in state/), grok and kimi (firstmate-named worktree pointers) already keep their artifacts clear of standard project paths; opencode writes a firstmate-named plugin file. tmux, herdr, zellij, orca, and cmux are unaffected: the change is a launch-string argument and a file location, both backend-agnostic, and orca's own teardown path uses the same guarded removal.
|
Withdrawing this PR - moving the work to our own fork instead. No action needed from maintainers. |
|
Reopening - this was withdrawn briefly while we sorted out our own repository setup. The change is unchanged and the no-mistakes signature is intact. Ready for review whenever a maintainer can release the workflow approval. |
|
Automated reminder: thanks for the PR! This branch currently has a merge conflict with the base branch. When you get a chance, please rebase onto (or merge) the latest base branch, resolve the conflict, and push. After that, checks will re-run and the PR will get looked at again. Noted for firstmate#1627 at |
|
Speaking as Kun's firstmate: closing this as stale. It has been waiting on a contributor update for 14+ days with no author push or comment. Reopen if you want to pick it back up. |
Intent
Fix a defect in bin/fm-spawn.sh: launching a claude crewmate destroyed a project's tracked .claude/settings.local.json.
The defect. For a claude crewmate, fm-spawn installed the per-task turn-end and busy-state hooks by writing /.claude/settings.local.json wholesale. Many projects TRACK that path, and in one affected repository 300 committed lines of permission rules were replaced by a single hook line. Three consequences followed: the committed content was destroyed in the worktree, every claude crewmate in such a project started work on a modified tracked file, and fm-teardown could then refuse to clean up because the worktree was dirty. It also breaks firstmate's own first rule, that firstmate reads projects and only crewmates change them.
The fix, and why this shape. Two approaches were on the table: merge into the existing JSON and unpick only the added hook at teardown, or move the hook out of the worktree entirely the way the other adapters already do. This change takes the second. The hooks now live in state/.claude-settings.json and reach the session through 'claude --settings ' on the launch command. That mirrors what pi already does with '-e state/.pi-ext.ts', and follows the same outside-the-worktree principle as grok and kimi, which keep a global hook plus a small firstmate-named worktree pointer. Claude was the odd one out. Merging would have meant owning a JSON parser, an unparseable-file failure path, and a partial unpick at teardown; writing nothing into the project removes all three. That is why the diff contains no JSON-merge code and no 'existing file is unparseable' branch: the requirement to fail rather than guess on an unparseable file is satisfied by never reading or writing the project's file at all.
Verified live against the installed Claude Code 2.1.220, in a real interactive tmux pane rather than print mode: hooks supplied through --settings do fire, and --settings is additive, so the project's own .claude settings still load alongside them. The second property means this also restores something the old wholesale write had been silently removing, since the project's own permission rules now reach the crewmate instead of being overwritten.
Decisions a reviewer reading only the diff would not know:
Verification already performed: the original clobber was reproduced end to end with the real fm-spawn before any fix; the fix was then demonstrated against a real affected repository, a read-only clone carrying its real 300 committed lines, leaving that file byte-identical and the worktree clean, rather than only against a synthetic fixture; the new tests were confirmed to fail against the pre-fix scripts so they are not vacuous; bin/fm-lint.sh passes on pinned ShellCheck 0.11.0 with every touched bin/*.sh clean; bin/fm-doc-audience-check.sh passes. Two test failures in the development environment, tests/fm-gotmp.test.sh and one Orca metadata-write case in tests/fm-backend-orca.test.sh, reproduce on unmodified HEAD and are unrelated to this change.
Tests are colocated in tests/ and extend existing scripts rather than adding a new runner: fm-busy-adapter-wiring covers the tracked file staying untouched, an untracked file being preserved, the launch command carrying --settings, the Stop hook still touching the turn-end marker, and the raw-launch path warning while staying unarmed and classifying unknown; fm-teardown covers the state artifact being retired, a tracked project file being kept, and a legacy firstmate file being removed while a foreign one is preserved; fm-secondmate-harness covers a secondmate launch carrying no --settings; fm-spawn-dispatch-profile has its exact launch-string expectations updated. One new file is added deliberately: tests/fm-claude-settings-hook-live-e2e.test.sh, an opt-in credentialed live guard registered in the existing live-harness-optin family and gated on the existing FM_CLAUDE_LIVE_E2E variable, so no new knob. A vendor-behaviour dependency like --settings needs a live guard that fails loudly naming the harness and version; it is the command that refreshes the dated evidence in docs/verification/supervision.md after a Claude Code upgrade.
Compatibility was reviewed across every supported harness and runtime backend rather than only the ones currently in use. codex carries its turn-end on the launch command through -c notify=, pi and pi-signed already keep their extension in state/, and grok and kimi use firstmate-named worktree pointers, .fm-grok-turnend and .fm-kimi-turnend, so none of them writes to a standard project path. opencode writes a firstmate-named plugin file, .opencode/plugins/fm-busy-state.js, which no project would track, so it is not the same class of defect. tmux, herdr, zellij, orca, and cmux are unaffected after inspection: this is a launch-string argument and a file location, both backend-agnostic, and orca's own teardown branch uses the same guarded removal.
Repo conventions followed: one sentence per line in tracked Markdown, plain dash rather than an em dash, no agent name as a commit co-author, and documented behaviour changes went into the owning script header and the harness-adapters skill rather than into AGENTS.md.
What Changed
bin/fm-spawn.shno longer writes a claude crewmate's turn-end and busy-state hooks into<worktree>/.claude/settings.local.json, which overwrote that file's committed content in projects that track it. The hooks now go tostate/<id>.claude-settings.jsonand reach the session throughclaude --settings <file>on the templated launch, mirroring how pi already passes-e state/<id>.pi-ext.ts. Because--settingsis additive, the project's own.claudesettings still load alongside the per-task hooks. A claude secondmate launch omits the flag, since no per-task hook file is written for it.--settingsinto, sofm-spawnnow warns and names the exact argument to add, declines to arm the busy-state contract for that spawn (it classifies unknown rather than being seeded busy with nothing able to clear it), and writes a hook file holding only the turn-end touch.bin/fm-teardown.shreplaces its unconditionalrmof<worktree>/.claude/settings.local.jsonwithremove_legacy_claude_worktree_settings, which only removes the path when it is untracked in that worktree and its content names firstmate's own hook wiring; it also retires the newstate/<id>.claude-settings.jsonartifact. Coverage was added intests/fm-busy-adapter-wiring,fm-teardown,fm-secondmate-harness, andfm-spawn-dispatch-profile, plus a new opt-in live guardtests/fm-claude-settings-hook-live-e2e.test.shgated on the existingFM_CLAUDE_LIVE_E2Evariable, and the--settingsrequirement for manual resume or relaunch is recorded in the harness-adapters skill.Risk Assessment
✅ Low: The change removes the clobbering write entirely rather than patching a symptom, every intent-required behavior is verifiable in source (out-of-worktree hook file, additive --settings on the launch, secondmate omission, warned-and-unarmed raw launch, doubly-gated legacy removal, artifact retirement at both cleanup sites), downstream busy-state consumers already tolerate the unarmed case, and the only finding is informational.
Testing
Ran the four targeted test files touched by this change (fm-busy-adapter-wiring, fm-spawn-dispatch-profile, fm-secondmate-harness, fm-teardown) and every new and changed case passes, including tracked-file-untouched, launch-carries---settings, raw-launch-warns-and-stays-unarmed, legacy-teardown-removal, and secondmate-no---settings. Beyond the unit level I built a before/after end-to-end transcript running the real fm-spawn.sh from the base commit and from this branch against an identical project tracking a 302-line .claude/settings.local.json: the base commit destroys it (302 lines to 1, worktree dirty, real fm-teardown then REFUSES on uncommitted changes) while this branch leaves it byte-identical with a clean worktree, writes the hooks to state/<id>.claude-settings.json, types --settings into the launch command, fires the turn-end marker from that file's Stop hook, and tears down cleanly. I also ran the opt-in live guard and captured the real Claude 2.1.221 crewmate pane, where firstmate's --settings hooks and the project's own tracked Stop hook both fired and the tracked file was unmodified, and captured the operator-visible raw-launch warning naming the exact --settings argument to add. The only failure seen was herdr-preflight-missing-adapter in fm-teardown.test.sh, which reproduces identically on unmodified base commit 733a504 and is unrelated to this change; the working tree was left clean with all evidence in the dedicated evidence directory.Evidence: Before/after end-to-end: claude crewmate spawn + teardown against a project tracking a 302-line .claude/settings.local.json
=== BEFORE the fix (base commit 733a504) === project file : .claude/settings.local.json (TRACKED, 302 lines) lines after spawn : 1 sha256 before : 694c9775f7caeec777b839847e9d4d48f0343686c89cb0c3a3bb8bd442323e9b sha256 after : 4ae9aeb4c77834e1fcd3e256cd8d9b2d125c619f90b35e5dbfc627dc715a5737 verdict : DESTROYED - committed content was overwritten git status of the crewmate worktree: M .claude/settings.local.json launch command typed into the crewmate pane: CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION=false claude --dangerously-skip-permissions "$(...encode launch-brief < .../brief.md)" --- real fm-teardown of the same task --- teardown exit status: 1 REFUSED: worktree .../before-fix/wt has uncommitted changes. uncommitted changes present === AFTER the fix (branch fm/fm-spawn-clobbers-settings) === project file : .claude/settings.local.json (TRACKED, 302 lines) lines after spawn : 302 sha256 before : 694c9775f7caeec777b839847e9d4d48f0343686c89cb0c3a3bb8bd442323e9b sha256 after : 694c9775f7caeec777b839847e9d4d48f0343686c89cb0c3a3bb8bd442323e9b verdict : UNCHANGED - the committed content survived git status of the crewmate worktree: (clean) per-task hook file outside the worktree (state/): .../home/state/demo-task.claude-settings.json hooks: ["SessionEnd","Stop","StopFailure","UserPromptSubmit"] launch command typed into the crewmate pane: CLAUDE_CODE_ENABLE_PROMPT_SUGGESTION=false claude --dangerously-skip-permissions --settings '.../home/state/demo-task.claude-settings.json' "$(...encode launch-brief < .../brief.md)" Stop hook from that file executed -> turn-end marker present: yes --- real fm-teardown of the same task --- teardown exit status: 0 teardown demo-task complete (window firstmate:fm-demo-task, worktree .../after-fix/wt) per-task hook file left in state/ after teardown: no (retired) project's tracked file after teardown: 694c9775f7caeec777b839847e9d4d48f0343686c89cb0c3a3bb8bd442323e9bEvidence: Live Claude 2.1.221 crewmate pane launched with --settings (real tmux capture-pane)
--- Claude Code v2.1.221 --- Welcome back Lochlann! Opus 5 with high effort - Claude Max /.../T/fm-claude-pane.GfXJvb/project > reply with the single word ok * ok bypass permissions on (shift+tab to cycle) Hook markers left behind: firstmate UserPromptSubmit (from --settings) : FIRED firstmate Stop / turn-end wake (--settings) : FIRED the project's OWN Stop hook (tracked file) : FIRED <- --settings is additive The project's tracked .claude/settings.local.json: sha256 before session : 9a02708915072a07baa9ebe2b94f0ea399093b1d5f405da490d806333d1cd8d6 sha256 after session : 9a02708915072a07baa9ebe2b94f0ea399093b1d5f405da490d806333d1cd8d6 git status : (clean)Evidence: Opt-in live guard against the installed Claude Code
$ claude --version 2.1.221 (Claude Code) $ FM_CLAUDE_LIVE_E2E=1 bash tests/fm-claude-settings-hook-live-e2e.test.sh ok - Claude 2.1.221 (Claude Code): --settings hooks fired the turn-end wake, loaded additively beside the project's own tracked settings, and left that tracked file unmodified exit=0Evidence: Operator-visible warning for the raw claude launch escape hatch
$ fm-spawn.sh raw-task <project> --launch "claude --dangerously-skip-permissions 'do the thing'" warning: this raw claude launch command carries no --settings argument, so raw-task will run with no turn-end wake and its busy state stays unknown; add --settings '.../home/state/raw-task.claude-settings.json' to the command to restore the turn-end wake spawned raw-task harness=claude kind=ship mode=no-mistakes yolo=off window=firstmate:fm-raw-task worktree=.../wt busy-state contract armed for this spawn : no (stays unarmed, so it classifies unknown - never a stranded busy) hook file still written, ready for a hand-added --settings: {"hooks":{"Stop":[{"hooks":[{"type":"command","command":"touch '.../home/state/raw-task.turn-ended'"}]}]}}Evidence: Evidence-producing script: before/after spawn + teardown demo
Evidence: Evidence-producing script: live Claude pane capture
Evidence: Evidence-producing script: raw-launch warning demo
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-teardown.sh:510- The second content gate in remove_legacy_claude_worktree_settings (grep -qF 'fm-busy-event.sh') is redundant for its stated purpose and quietly wider than the comment describes. Both legacy shapes fm-spawn ever wrote embeddedtouch '<STATE>/<id>.turn-ended'in the Stop hook (the busy-state shape prefixed the busy call onto that same touch), so gate 1 at :509 already matches both; the comment's "Both legacy shapes are covered" rationale is therefore inaccurate. What gate 2 actually adds is matching a firstmate legacy file belonging to a different task id - plausibly useful for a reused treehouse pool worktree, but undocumented and unexercised by the new tests (the legacy fixture usestask-x1.turn-ended, which matches gate 1). Residual cost: any untracked .claude/settings.local.json mentioning fm-busy-event.sh is removed regardless of task, narrow but reachable in the firstmate repo itself, the one project whose files name that script.tests/fm-teardown.test.sh:1582- tests/fm-teardown.test.sh fails atherdr-preflight-missing-adapter("the retryable pre-return refusal was not explained visibly"), so the file exits 1. This is NOT caused by this change: running the same test file from the unmodified base commit 733a504 (extracted withgit archiveinto a temp dir) produces the identical single failure. Flagged only so the non-zero exit of a test file this change touches is not mistaken for a regression; deciding whether to fix it is out of scope here.bash tests/fm-busy-adapter-wiring.test.sh- all pass, including the four new claude cases (tracked file untouched, untracked file preserved, launch loads out-of-tree settings, raw launch warns/unarmed/unknown)bash tests/fm-spawn-dispatch-profile.test.sh- all pass, exact claude launch strings now include--settings '<state>/<id>.claude-settings.json'bash tests/fm-secondmate-harness.test.sh- all pass, includingtest_spawn_claude_secondmate_launches_without_per_task_settingsbash tests/fm-teardown.test.sh- both new claude cases pass; one pre-existing unrelated failure (herdr-preflight-missing-adapter)cd /tmp/fm-base-30223 && bash tests/fm-teardown.test.sh(base commit 733a504 extracted viagit archive) - sameherdr-preflight-missing-adapterfailure, confirming it is pre-existingManual before/after end-to-end: realbin/fm-spawn.shfrom base commit vs branch against a project tracking a 302-line.claude/settings.local.json, comparing sha256, line count,git status --porcelain, the tmuxsend-keyslaunch command, and the written hook file (clobber-e2e-demo.sh)Manual end-to-end continuation: realbin/fm-teardown.shon both spawned tasks - base case REFUSES on uncommitted changes, fixed case completes and retiresstate/<id>.claude-settings.jsonFM_CLAUDE_LIVE_E2E=1 bash tests/fm-claude-settings-hook-live-e2e.test.shagainst installed Claude Code 2.1.221 - passesManual live pane capture: launched a real Claude session in an isolated tmux server with the exact launch shape fm-spawn builds, capturedtmux capture-pane -p, and checked firstmate's--settingshooks, the project's own Stop hook, and the tracked file's sha256 (live-pane-capture.sh)Manual raw-launch escape hatch: realfm-spawn.shwith a hand-written claude launch command, capturing the operator-visible warning, the unarmed busy contract, and the turn-end-only hook file (raw-launch-warning-demo.sh)docs/fm-test-portable-shards.md:73- docs/fm-test-portable-shards.md's portable-serial shard table (15/18/17/19 = 69 scripts, dated 2026-08-02) no longer matches the derived lane, which now holds 82 scripts across 16/21/23/22. This change contributes exactly one of that gap (tests/fm-claude-settings-hook-live-e2e.test.sh lands in portable-serial-4of4); the other 12 predate it. Refreshing the table correctly requires the per-shard CI timing artifacts named in that document's own refresh procedure, which are not available here, and editing only the counts while leaving stale durations would misrepresent the balance evidence. Proposed follow-up: refresh the hints and table together from a green CI run. Coverage is unaffected - membership is derived and the coverage guard keeps the partition complete.✅ **Push** - passed
✅ No issues found.