Repository navigation
fix(bin): stop claude spawn from overwriting a project's .claude/settings.local.json - #2
Merged
Merged
Conversation
The claude branch wrote its per-task busy-state hooks wholesale into <worktree>/.claude/settings.local.json. On a project that tracks that path, the spawn destroyed the project's committed settings for the life of the task and left the tracked file permanently modified, which then blocked teardown and re-escalated the finished task on every watcher pass. Follow the grok/kimi/pi precedent and keep the hooks out of the worktree entirely: they now live in state/<id>.claude-settings.json and reach the worker through claude's own --settings flag. Verified live on Claude Code 2.1.237 that --settings hooks fire and MERGE with the project's own settings rather than replacing them, so a project's committed hooks keep working. Teardown no longer rm -f's that project-owned path, and its dirty check no longer ignores everything untracked under .claude/ - firstmate writes nothing there, so anything present belongs to the project or the worker and must be able to refuse teardown. A narrow, provably-firstmate-only removal cleans up leftovers from tasks already under way. Regression tests cover both directions: the project's tracked settings survive a spawn with the worktree clean and the hooks armed, and teardown leaves .claude/ exactly as it found it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this fixes
Spawning a Claude crewmate wrote firstmate's busy-state hooks wholesale into
<worktree>/.claude/settings.local.json- a path the project owns and frequently tracks.Observed 2026-08-20 on a project that tracks that file: the spawn replaced its committed contents (a
CLAUDE_CODE_EXPERIMENTAL_AGENT_TEAMSenv entry, apermissions.allowlist, anenabledPluginsentry) with just the hooks block. Two consequences, both real:bin/fm-teardown.shafter the work had merged and produced an endless stale-wake loop - the watcher re-escalated the finished task every few minutes until it was cleared by hand.Why hooks moved out of the worktree instead of merging into the file
Two designs were on the table: merge firstmate's hooks into the existing file and restore it on teardown, or follow the existing Grok/Kimi/Pi precedent and keep the hooks outside the worktree entirely.
The choice was settled by what Claude Code actually supports, not preference.
claude --settings <file>is a documented flag that loads additional settings from an arbitrary path. Verified live against Claude Code 2.1.237 in a throwaway workspace holding its own.claude/settings.local.jsonwith aStophook of its own: all three marker files were created - the project's own hook, plus theStopandUserPromptSubmithooks supplied only through--settings. So--settingsmerges with the project's settings rather than replacing them, and the project's file is untouched.That makes the merge-into-the-file design strictly worse: it would still touch a project file, still need restore-on-teardown, still need absent / empty / malformed / read-only handling, and would still leave a tracked file dirty mid-task. Firstmate now writes zero files into the worktree for claude.
Changes
bin/fm-spawn.sh- hooks are written tostate/<id>.claude-settings.jsonand handed to the launch through--settings. Dropped themkdir -p "$WT/.claude"and the.git/info/excludeentry. A secondmate arms no busy contract, so the flag is omitted forkind=secondmaterather than pointed at a file that does not exist.bin/fm-control-lib.sh- claude's per-task relaunch wiring path follows tostate/.bin/fm-teardown.sh- three fixes:<worktree>/.claude/settings.local.jsonat any of its four cleanup sites. That removal was unconditional and ran for every harness, not just claude..claude/. Firstmate writes nothing there, so anything present belongs to the project or the worker and must be able to refuse teardown like any other dirty file. This was a second latent bug: any untracked file a worker wrote under.claude/was previously discarded silently.remove_legacy_claude_settings- a one-time migration for tasks already under way at update time, whose pre-fix file is hidden fromgit statusby the exclude entry the old spawn also wrote and whose stale hooks would otherwise fire for the next task in a pooled worktree. It removes the file only when it is untracked and contains firstmate's ownfm-busy-event.shcommand, so a tracked or project-authored file is provably never touched.harness-adaptersskill; dated evidence and the refresh command indocs/verification/supervision.md.Tests
Both directions, as required:
tests/fm-busy-adapter-wiring.test.sh- a project that tracks.claude/settings.local.jsonwith real content (landed on the default branch so the spawn's base refresh brings it in) survives a realfm-spawnbyte for byte,git statusstays empty, the launch command carries--settingspointing outside the worktree, and the hooks still drive the busy classifier.tests/fm-teardown.test.sh- the tracked file survives teardown unchanged; an untracked file under.claude/now refuses teardown instead of being discarded; a pre-fix firstmate leftover is removed while a neighbouring project file under.claude/is not.tests/fm-claude-settings-live-e2e.test.sh(new, opt-in viaFM_CLAUDE_SETTINGS_LIVE_E2E=1, self-skipping) - the live guard for the vendor fact this design rests on. It exercises the installed binary and fails naming its version, per the harness-dependent-check rule that a vendor-emitted fact must be proven end to end against the real harness rather than a stub.bin/fm-spawn.shwas added to thelive-harness-optinfamily inbin/fm-test-run.shso a spawn change re-selects it.The three new teardown cases were verified in CI, not in a plain local run
tests/fm-teardown.test.shaborts at its first failure, and two failures precede the new cases on the development machine:content-landed: teardown should succeed when content is already in the default branch- verified identical on the untouched base commit1cb900cfrom an extracted base tree, so it is not caused by this change.herdr-child-preflight: refusal did not explain its non-mutating boundary- also present on the untouched base, with herdr 0.8.0 installed locally.Because of those,
test_teardown_leaves_the_projects_claude_settings_alone,test_untracked_claude_file_refuses_teardownandtest_legacy_firstmate_claude_leftover_is_removednever execute in a full-file local run. They were verified locally by running a trimmed copy of the same script, and they run normally in CI, wheremainhas been green on each of the recent pushes. Anyone picking up a follow-up on those two pre-existing failures has the reproduction above; neither is demonstrably a defect in the code under test rather than a local-environment artifact.Other harnesses
Audited every write site in the spawn path. No other harness has this flaw. opencode, grok, kimi, muse, cursor and pi write either firstmate-named files (
.fm-grok-turnend,.fm-kimi-turnend,fm-busy-state.js) or nothing inside the worktree at all, and kimi's edit of its config under the user's home is genuinely marker-delimited and atomic.One unrelated observation, not fixed here: grok writes its global turn-end hook files non-atomically on every spawn, without the validation guard kimi has.
Known residue, deliberately not auto-cleaned
The old
exclude_pathcall appended.claude/settings.local.jsonto each project's shared.git/info/exclude. That line outlives teardown and hides a project's own untracked settings file fromgit statusrepo-wide. It is not removed automatically because there is no way to prove firstmate wrote a given exclude line rather than the operator.