Skip to content

feat(self-fix): worktree-based self-fix flow for firstmate's own tracked code - #451

Closed
chanjdaniel wants to merge 5 commits into
kunchenguid:mainfrom
Chan-Software-Services:fix/fm-selffix-brief-fmhome-b7
Closed

chanjdaniel wants to merge 5 commits into
kunchenguid:mainfrom
Chan-Software-Services:fix/fm-selffix-brief-fmhome-b7

Conversation

@chanjdaniel

Copy link
Copy Markdown

Intent

Fix bin/fm-self-fix.sh brief-existence precheck to honor FM_HOME. When FM_HOME is set to a directory other than the repo root, firstmate's operational data (including briefs) lives under FM_HOME/data/, not FM_ROOT/data/. The precheck was checking FM_ROOT/data/ and false-refusing even when the brief existed at FM_HOME/data/. The fix resolves FM_HOME and DATA the same way fm-spawn.sh and fm-brief.sh do (FM_HOME="${FM_HOME:-${FM_ROOT_OVERRIDE:-$FM_ROOT}}", DATA="${FM_DATA_OVERRIDE:-$FM_HOME/data}"), then checks the brief at $DATA/$ID/brief.md and prints the resolved path in the error message. The scratch worktree root stays outside FM_HOME - unchanged behavior. A regression test at tests/fm-self-fix.test.sh covers the FM_HOME-differs case and the missing-brief-diagnostic-message case. Shellcheck clean on both changed files.

What Changed

  • Added bin/fm-self-fix.sh, which creates an isolated plain git worktree of the firstmate repo on a fix/<id> branch rooted outside any FM_HOME and launches a crewmate into it via fm-spawn.sh --worktree, so the primary checkout never leaves its default branch while a shared-material fix still ships through the gate; the brief-existence precheck now resolves FM_HOME/DATA the same way fm-spawn.sh and fm-brief.sh do and reports the resolved $DATA/<id>/brief.md path on failure.
  • Reworked fm-spawn.sh to assert the resolved worktree is a genuine isolated worktree distinct from the primary checkout before launch, added the --worktree self-fix path (recorded as selffix=1 in meta), and taught fm-teardown.sh to remove a self-fix worktree with git worktree remove.
  • Added tests/fm-self-fix.test.sh covering the FM_HOME-differs and missing-brief-diagnostic cases (made hermetic via a worktree guard), extended tests/lib.sh/tests/fm-gotmp.test.sh to clear operational-home selector env vars, and updated AGENTS.md, CONTRIBUTING.md, and docs to document the self-fix flow and FM_SELFFIX_ROOT.

Risk Assessment

✅ Low: Well-bounded change (a small FM_HOME-aware precheck, a --worktree spawn path, and matching teardown/test coverage); the prior hermeticity finding is fixed, and the one remaining issue is a defensive test-isolation gap, not a product-behavior risk.

Testing

The baseline suite (all tests/*.test.sh) already passed, and I ran the targeted tests/fm-self-fix.test.sh (both cases green). For product-level evidence I built an end-to-end CLI transcript running the pre-fix script against the fixed one under an FM_HOME that differs from the repo root with the brief under $FM_HOME/data/: the old version false-refuses with "no brief at data/...", the fixed version finds the brief and advances past the precheck (stopping at the later worktree guard), and the missing-brief case now names the resolved FM_HOME path. This is a CLI/bin change with no UI surface, so the appropriate artifact is the command transcript rather than a screenshot. Worktree left clean.

Evidence: fm-self-fix FM_HOME precheck before/after transcript

### BEFORE fix (v1b16a82): precheck read $FM_ROOT/data/ -> false refusal error: no brief at data/fix-typo-x7/brief.md (author it first) exit=1 ### AFTER fix (current): resolves $FM_HOME/data/ -> brief found, passes precheck error: worktree path already exists: .../selffix-scratch/fix-typo-x7 exit=1 ### Missing-brief diagnostic names the resolved FM_HOME path error: no brief at .../other-home/data/missing-b2/brief.md (author it first) exit=1

## fm-self-fix.sh brief precheck honors FM_HOME  (bug fix, commit d845527)

Setup: FM_HOME=<ops-home> distinct from repo root; brief authored at $FM_HOME/data/fix-typo-x7/brief.md

### BEFORE fix (v1b16a82): precheck read $FM_ROOT/data/ -> false refusal
error: no brief at data/fix-typo-x7/brief.md (author it first)
exit=1

### AFTER fix (current): resolves $FM_HOME/data/ -> brief found, passes precheck
error: worktree path already exists: /tmp/tmp.HQ7x0fNADN/selffix-scratch/fix-typo-x7
exit=1

### Missing-brief diagnostic names the resolved FM_HOME path
error: no brief at /tmp/tmp.HQ7x0fNADN/other-home/data/missing-b2/brief.md (author it first)
exit=1

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

⚠️ **Rebase** - 1 warning

Push main to origin, or rebase your branch onto origin/main, before gating.

⚠️ **Review** - 1 warning
  • ⚠️ tests/fm-self-fix.test.sh:33 - tests/fm-self-fix.test.sh: test_brief_found_with_fmhome_set is not hermetic. With the brief present, fm-self-fix.sh passes the precheck and proceeds to git -C &#34;$FM_ROOT&#34; worktree add &#34;$WT&#34; -b fix/selffix-brief-a1 ... against the REAL repo ($ROOT), then execs fm-spawn.sh --backend tmux. So merely running this test creates a persistent fix/selffix-brief-a1 branch plus a git worktree admin entry in the repo under review and launches a real tmux spawn as a side effect. The temp-dir cleanup (rm -rf) removes the worktree directory but not the branch or the git worktree registration, so it leaks state; a second run then fails at the branch-exists check (still with no 'no brief' text, so the assertion silently still passes). This runs in the pipeline's test step inside a worktree of this same repo, where the leaked branch/worktree/tmux window can interfere. The test should short-circuit before the worktree-add/exec (e.g. stub or intercept fm-spawn.sh, or assert the precheck outcome without reaching side effects) rather than exercising the full spawn.

🔧 Fix: make self-fix FM_HOME test hermetic via worktree guard
1 warning still open:

  • ⚠️ tests/lib.sh:33 - The new hermetic-isolation unset clears FM_HOME, FM_ROOT_OVERRIDE, FM_STATE_OVERRIDE, and FM_CONFIG_OVERRIDE, but omits FM_DATA_OVERRIDE (and FM_PROJECTS_OVERRIDE), which are also operational-home selectors read by the scripts under test. This directly undermines the fix's own regression test: fm-self-fix.sh resolves DATA="${FM_DATA_OVERRIDE:-$FM_HOME/data}", so both new tests in tests/fm-self-fix.test.sh assert on a path computed from FM_HOME only when FM_DATA_OVERRIDE is unset. If a live firstmate session (or CI env) exports FM_DATA_OVERRIDE, test_brief_found_with_fmhome_set reads the wrong data dir and its 'no brief' assertion fails, and test_brief_missing_prints_resolved_path could even find a real brief. The comment claims to 'Clear the operational-home selectors up front' but leaves the very selector the self-fix brief precheck keys on. Add FM_DATA_OVERRIDE (and FM_PROJECTS_OVERRIDE for consistency) to the unset list here and in tests/fm-gotmp.test.sh:18.
✅ **Test** - passed

✅ No issues found.

  • command -v tmux >/dev/null || { echo "tmux is required for e2e tests" >&2; exit 1; }; tmux -V; rc=0; for t in tests/*.test.sh; do echo "== $t =="; bash "$t" || rc=1; done; exit "$rc"
  • bash tests/fm-self-fix.test.sh - both cases pass (finds brief under FM_HOME; missing brief prints resolved FM_HOME path)
  • E2E before/after transcript: ran pre-fix (git show 1b16a82:bin/fm-self-fix.sh) vs current bin/fm-self-fix.sh with FM_HOME set to a separate ops-home and the brief authored at $FM_HOME/data/&lt;id&gt;/brief.md
  • Verified missing-brief diagnostic prints the resolved $FM_HOME/data/&lt;id&gt;/brief.md path
  • git status --porcelain after cleanup - worktree clean, no transient files left behind
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

chanjdaniel and others added 5 commits July 11, 2026 10:56
…ect path (#1)

* fix(spawn): wait for genuine isolated worktree, not just any non-project path

The treehouse worktree wait loop was breaking on the first pane cwd
that was not the project directory. Right after tmux new-window, the
pane transiently reports the tmux server default (/home/danielc) before
treehouse get runs, so the loop breaks immediately with WT=/home/danielc.
The isolation guard then correctly refuses, aborting every spawn.

Hoist the isolation invariant (real git worktree root, resolved
toplevel equals itself, distinct from project checkout) into a
reusable is_isolated_worktree() function and use it as the loop's
break condition. This skips transient pre-chdir states and waits
for the real worktree to appear.

validate_spawn_worktree() now delegates to the same function as a
redundant final assertion.

* no-mistakes(test): make failing tests hermetic against ambient FM_HOME and system node

* no-mistakes(document): mark herdr worktree-isolation symlink gap resolved in docs

* no-mistakes(lint): fix SC2015 in fm-spawn is_isolated_worktree helper
…ked code (#2)

* feat(self-fix): worktree-based self-fix flow so firstmate can ship its own fixes through the gate

Add bin/fm-self-fix.sh, which creates an isolated git worktree of this repo on a
fix branch (rooted outside any FM_HOME) and dispatches a crewmate into it via a
new fm-spawn --worktree mode - so firstmate can fix its own tracked code through
the no-mistakes gate without treehouse pooling and without moving the primary
checkout off its default branch.

- fm-spawn.sh: --worktree <path> skips treehouse get, uses the pre-created
  isolated worktree (same isolation assertion), starts the tmux window in it, and
  records selffix=1 in meta. All paths are byte-unchanged when the flag is absent.
- fm-teardown.sh: selffix=1 tasks are cleaned with git worktree remove (after the
  unchanged landed-work safety check), not a treehouse return.

Scoped to the tmux backend; other backends are a later follow-up.

* no-mistakes(review): enforce tmux-only self-fix and skip fleet-sync on selffix teardown

* no-mistakes(document): document worktree-based self-fix flow in scripts, AGENTS, architecture docs
bin/fm-self-fix.sh checked the brief at $FM_ROOT/data/$ID/brief.md,
but firstmate's operational data lives under FM_HOME, which can differ
from the repo root. Resolve FM_HOME/DATA the same way fm-spawn.sh and
fm-brief.sh do, then check the brief there.

Add regression test covering the FM_HOME-differs case and the
missing-brief-diagnostic-message case.
@chanjdaniel
chanjdaniel force-pushed the fix/fm-selffix-brief-fmhome-b7 branch from c81cf71 to ad6f077 Compare July 11, 2026 03:08
@chanjdaniel

Copy link
Copy Markdown
Author

Closing - opened against the upstream parent by mistake; this work belongs only on our own fork (chanjdaniel/firstmate). Re-landing there.

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.

1 participant