Skip to content

fix(spawn): accept only a genuine isolated worktree in the treehouse cwd poll - #3

Merged
kostas-nt merged 2 commits into
mainfrom
fix-spawn-worktree-poll-transient
Jul 14, 2026
Merged

kostas-nt merged 2 commits into
mainfrom
fix-spawn-worktree-poll-transient

Conversation

@kostas-nt

@kostas-nt kostas-nt commented Jul 13, 2026 •

Copy link
Copy Markdown
Owner

Intent

Fix a race in bin/fm-spawn.sh's treehouse-worktree cwd poll that deterministically broke every ship/scout spawn on hosts with slow pane-shell startup: a freshly created pane transiently reports $HOME as #{pane_current_path} before its shell finishes initializing (traced live 2026-07-13: tick 1 = /home/kostas, tick 2 = project dir, tick 5 = treehouse worktree), and the poll accepted the FIRST reading that differed from the project, so WT resolved to $HOME and validate_spawn_worktree refused the launch, 3/3 reproductions. The fix: the poll now accepts a candidate only when it is itself a git toplevel physically distinct from the project. Deliberate decisions: (1) the accept predicate was extracted as fm_isolated_worktree into bin/fm-tangle-lib.sh (the prevention/detection lib for exactly this worktree-tangle guard family) and is shared by both the poll and validate_spawn_worktree per the repo's one-owner rule; validate_spawn_worktree is deliberately kept after the loop as defense in depth. (2) real_path_or_raw was removed from fm-spawn.sh as dead code - both former consumers now go through the predicate. (3) The poll-timeout error message was reworded to 'did not yield an isolated worktree within Ns' because it now also covers 'entered a dir that never became a valid worktree', sharing the load-bearing phrase with validate_spawn_worktree's error. (4) FM_SPAWN_WT_POLLS is a deliberate test-only override of the poll count (default 60, unchanged); tests set it to 3 because reject-and-retry abort cases now run to the timeout instead of aborting on the first different reading. (5) One fix covers tmux/herdr/zellij/cmux since they all route through the same spawn_current_path loop; orca and secondmate spawns never enter it; the poll keeps targeting the stable WT_TARGET window id per upstream kunchenguid#134. This branch was deliberately REBUILT on the post-fork-sync origin/main (b708731) by cherry-picking only the fix commit and its doc-sync commit, resolving conflicts with upstream's kunchenguid#134 window-id targeting and the new GUARD 1c window-construction tests, because the fork history it originally shipped on was reset; the PR branch was force-with-lease updated with explicit authorization, and the gate's stale pre-rebuild branch ref was cleared so this run's gate push is a clean create. Tests extend tests/fm-tangle-guard.test.sh (the suite owning the fm-spawn isolation seam): unit rows for the predicate plus an e2e regression test replaying the live-traced pane-cwd sequence via a sequence-capable fake tmux; the regression test was verified to fail against pre-fix fm-spawn.sh and pass with the fix. shellcheck bin/.sh bin/backends/.sh tests/.sh is clean. KNOWN PRE-EXISTING, UNRELATED FAILURE ON THIS HOST: tests/fm-watcher-lock.test.sh's 'restart reports healthy peer' case (added upstream in kunchenguid#375) fails identically on a pristine origin/main export on this machine (status 124, arm starts a fresh watcher instead of reporting the healthy TERM-resistant peer); this branch does not touch any watcher/lock code (bin/fm-watch.sh, bin/fm-wake-lib.sh, tests/fm-watcher-lock.test.sh are byte-identical to origin/main), so that failure must not be attributed to or fixed by this change.

What Changed

  • Fixed a race in bin/fm-spawn.sh's treehouse-worktree cwd poll where a freshly created pane transiently reports $HOME as its current path before the shell finishes initializing; the poll previously accepted the first reading that merely differed from the project dir, so WT resolved to $HOME and validate_spawn_worktree refused the launch on hosts with slow pane-shell startup. The poll now accepts a candidate only when it is itself a git toplevel physically distinct from the project.
  • Extracted the accept predicate as fm_isolated_worktree in bin/fm-tangle-lib.sh, shared by both the poll and validate_spawn_worktree (kept after the loop as defense in depth); removed the now-dead real_path_or_raw from fm-spawn.sh and reworded the poll-timeout error to "did not yield an isolated worktree within Ns".
  • Extended tests/fm-tangle-guard.test.sh with unit rows for the predicate and an e2e regression test replaying the live-traced pane-cwd sequence via a sequence-capable fake tmux (verified to fail pre-fix, pass post-fix); tests use the test-only FM_SPAWN_WT_POLLS override since reject-and-retry cases now run to timeout. The Test stage reports one failure in tests/fm-watcher-lock.test.sh, a pre-existing host-specific failure in code this branch does not touch.

Risk Assessment

✅ Low: Well-bounded, faithful extraction of an existing isolation check into a shared predicate that is strictly more restrictive than the prior poll condition; it is covered by new unit and e2e regression tests, matches every stated intent decision, and introduces no regression path.

Testing

Completed 1 recorded test check.

  • Outcome: ⚠️ 1 error across 1 run (6m12s)

Pipeline

Updates from git push no-mistakes

✅ **intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

✅ **Review** - passed

✅ No issues found.

⚠️ **Test** - 1 error
  • 🚨 tests failed with exit code 1
  • 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"
✅ **Document** - passed

✅ No issues found.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…cwd poll

A freshly created pane transiently reports $HOME as its cwd before its
shell finishes initializing, and the worktree poll grabbed the first
reading that differed from the project, so every ship/scout spawn failed
on hosts with slow pane-shell startup. Poll with the same accept
condition the launch guard enforces - the path is its own git toplevel,
physically distinct from the project - via a shared fm_isolated_worktree
predicate in fm-tangle-lib.sh, keeping validate_spawn_worktree as
defense in depth.
@kostas-nt
kostas-nt force-pushed the fix-spawn-worktree-poll-transient branch from c9fe287 to 503676d Compare July 14, 2026 11:07
@kostas-nt
kostas-nt merged commit 08922d4 into main Jul 14, 2026
4 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.

1 participant