Skip to content

fix(bin): stop claude spawn from overwriting a project's .claude/settings.local.json - #2

Merged
doitdigital0495 merged 4 commits into
mainfrom
fm/fm-settings-clobber
Aug 20, 2026
Merged

doitdigital0495 merged 4 commits into
mainfrom
fm/fm-settings-clobber

Conversation

@doitdigital0495

Copy link
Copy Markdown
Owner

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_TEAMS env entry, a permissions.allow list, an enabledPlugins entry) with just the hooks block. Two consequences, both real:

  1. The project's committed settings were silently destroyed in that worktree for the life of the task.
  2. Because the file is tracked, every spawn left the worktree permanently dirty, which blocked bin/fm-teardown.sh after 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.json with a Stop hook of its own: all three marker files were created - the project's own hook, plus the Stop and UserPromptSubmit hooks supplied only through --settings. So --settings merges 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 to state/<id>.claude-settings.json and handed to the launch through --settings. Dropped the mkdir -p "$WT/.claude" and the .git/info/exclude entry. A secondmate arms no busy contract, so the flag is omitted for kind=secondmate rather than pointed at a file that does not exist.
  • bin/fm-control-lib.sh - claude's per-task relaunch wiring path follows to state/.
  • bin/fm-teardown.sh - three fixes:
    • No longer removes <worktree>/.claude/settings.local.json at any of its four cleanup sites. That removal was unconditional and ran for every harness, not just claude.
    • The dirty check no longer filters untracked paths under .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 from git status by 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 own fm-busy-event.sh command, so a tracked or project-authored file is provably never touched.
  • Docs - a fact row and two corrected claims in the harness-adapters skill; dated evidence and the refresh command in docs/verification/supervision.md.

Tests

Both directions, as required:

  • tests/fm-busy-adapter-wiring.test.sh - a project that tracks .claude/settings.local.json with real content (landed on the default branch so the spawn's base refresh brings it in) survives a real fm-spawn byte for byte, git status stays empty, the launch command carries --settings pointing 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 via FM_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.sh was added to the live-harness-optin family in bin/fm-test-run.sh so 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.sh aborts 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 commit 1cb900c from 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_teardown and test_legacy_firstmate_claude_leftover_is_removed never 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, where main has 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_path call appended .claude/settings.local.json to each project's shared .git/info/exclude. That line outlives teardown and hides a project's own untracked settings file from git status repo-wide. It is not removed automatically because there is no way to prove firstmate wrote a given exclude line rather than the operator.

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.
@doitdigital0495
doitdigital0495 merged commit 610ccbc into main Aug 20, 2026
11 of 13 checks passed
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.

2 participants