Repository navigation
fix(bin): stop secondmate relaunch failing when watcher scratch files vanish - #4900
Merged
Merged
Conversation
Relaunch refused when find(1) exited non-zero while listing a secondmate home's state directory. A live watcher can delete scratch files between readdir and processing, which is not evidence that child *.meta records are unreadable. Prove the directory is listable from its mode and keep the existing readable-meta loop as the child-record guarantee. Fixes kunchenguid#4765.
…en running as root
Owner
|
Speaking as Kun's firstmate: this is merged. Thank you @tiago-peixoto — really appreciate you taking the time on this. Contract-class: restore. Tip vs main: removed the |
BohnBawerick
added a commit
to BohnBawerick/firstmate
that referenced
this pull request
Sep 23, 2026
Intent
Fixes #4765
Observed:
bin/fm-control.shrelaunch for a secondmate runsfind "$WT/state" -mindepth 1 -maxdepth 1 -printand refuses if find exits non-zero ("child records cannot be traversed"). On a live home whose watcher continuously writes.hash-*,.count-*,.last-*scratch files, find can exit non-zero when an entry disappears between directory read and processing. The same directory is readable immediately afterwards. Re-running restart alone succeeds with nothing changed. The loop below that probe already opens every*.metachild record and refuses on an unreadable regular file, which is the actual guarantee.Expected: restart should refuse when child
*.metarecords cannot be read, not when a watcher scratch file vanishes mid-find.Consequence: a live second mate keeps running stale instructions after it already persisted open work, and the update reads as mostly successful.
What Changed
bin/fm-control.sh: secondmate relaunch no longer runsfindover$WT/stateand no longer refuses with "child records cannot be traversed". Before this change, a.hash-*,.count-*, or.last-*scratch file that the watcher removed mid-scan madefindexit non-zero and blocked the relaunch.Now the check only requires
state/to be a directory the relaunch can read and enter (-d,-r,-x). The existing per-*.metaloop still refuses when any child record is not a readable regular file.tests/fm-control-relaunch.test.sh: adds a test with a fakefindthat fails on the state directory with a vanished.hash-0error. It checks that relaunch still succeeds and that both child metas are recorded in the journal (children=2).000state directory instead of the removedfindfailure path. It expects the "no readable state directory" refusal and checks that the secondmate keeps running. This test is skipped when run as root, because mode000does not restrict root.🤖 Generated with Claude Code
Risk Assessment
✅ Low: The change is small: it drops the find(1) probe that could fail when a scratch file vanished mid-scan, replaces it with -d/-r/-x checks on the state directory, and leaves the per-*.meta readability loop that intent names as the real guarantee unchanged; the tests cover both the race regression and the refusal cases.
Testing
I ran the targeted relaunch test file, and all 58 cases passed. I then ranfm-control.sh <id> relaunchon a secondmate home built with the suite's hermetic tmux harness, which stands in for the agent endpoint. Each case ran against both the base commit and the fix. The first case reproduces the reported race by making find fail on state/ while a background loop keeps creating and deleting scratch files. The base script refuses ("child records cannot be traversed"). The fix relaunches and counts both child records (children=2). With the real find and live churn, the fix relaunched 20 out of 20 times. The refusal cases still hold on the fix: an unreadable child .meta, a directory named *.meta, an unlistable state/ and a missing state/ all exit 1 and leave the secondmate running. The real GNU find did not fail under churn on this Linux host (0 of 3000 runs), so the race itself was reproduced by forcing the find error (the error the issue reports) rather than waiting for it to happen naturally. There is no UI change, so there are no screenshots; the evidence is the CLI transcripts.Evidence: Before/after: find fails on vanished scratch file during relaunch
Source: Before/after: find fails on vanished scratch file during relaunch
Evidence: Fixed relaunch under real scratch churn, 20 runs
Source: Fixed relaunch under real scratch churn, 20 runs
Evidence: Refusal: unreadable child meta
Source: Refusal: unreadable child meta
Evidence: Refusal: directory named *.meta
Source: Refusal: directory named *.meta
Evidence: Refusal: unlistable state dir
Source: Refusal: unlistable state dir
Evidence: Refusal: missing state dir
Source: Refusal: missing state dir
Evidence: Driver script
Source: Driver script
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
🔧 **Review** - 1 issue found → auto-fixed ✅
tests/fm-control-relaunch.test.sh:1414- The new unlistable-state assertion useschmod 000 "$dir/smhome/state"and expects relaunch to exit 1. If the suite runs as root (UID 0), which is common in containers,[ -r ]/[ -x ]in bin/fm-control.sh:752 return true for a mode-000 directory. Root can also list the directory, so the glob finds c1.meta and relaunch succeeds with rc 0, andexpect_code 1fails. The version this replaces used a fakefindand a directory named as a .meta, so it did not depend on the caller's UID. Other suites already guard the same way (for example tests/fm-claude-trust.test.sh:508 and tests/fm-procevent.test.sh:2237 checkid -u). Fix: skip or branch this sub-case when$(id -u)is 0, so the test does not fail when run as root.🔧 Fix applied.
✅ Re-checked - no issues remain.
✅ **Test** - passed
✅ No issues found.
bash tests/fm-control-relaunch.test.sh: all 58 cases pass, including the new vanished-scratch test and the unlistable-state test that is skipped as root (run here as a non-root user)live-driver.sh vanish: base 1bb72cc vs fix ef8a25b, with an injected find failure on state/ while a background loop keeps creating and deleting .hash-/.count-/.last-* fileslive-driver.sh churnx20: fixed fm-control.sh relaunch with the real find while scratch files churnlive-driver.sh unreadable|metadir|unlistable|nostate: refusal cases on base and fixProbe: 3000 runs of the real GNU find-mindepth 1 -maxdepth 1 -printunder scratch-file churn (0 failures on this Linux host)✅ **Document** - passed
✅ No issues found.
🔧 **Lint** - 1 issue found → no changes applied (2) ✅
🔧 No changes applied.
1 warning still open:
🔧 No changes applied.
✅ Re-checked - no issues remain.
✅ **Push** - passed
✅ No issues found.