Repository navigation
fix(spawn): lease and reuse crew worktrees via treehouse instead of pane-cwd discovery - #7
Conversation
SchokoShake
left a comment
There was a problem hiding this comment.
Reviewed with Lookout.
| path_real=$(cd "$path" 2>/dev/null && pwd -P) || path_real=$path | ||
| [ "$path_real" = "$want_real" ] && return 0 | ||
| done <<EOF | ||
| $( (cd "$PROJ_ABS" && treehouse status) 2>/dev/null || true ) |
There was a problem hiding this comment.
A failed treehouse status is indistinguishable here from a status that answered "not reserved": 2>/dev/null || true collapses a missing binary, an unreadable pool, a non-zero exit, and a genuine available into the same empty table, so treehouse_holds_worktree returns 1 either way.
The caller then prints a specific factual claim — treehouse no longer reserves the worktree recorded for <id> (line 1083) — that it has no evidence for, and the reuse proceeds regardless, so the only cost is a false alarm. But it is a false alarm on every respawn for as long as treehouse is unavailable, which is exactly the "spurious warning on every reuse of a genuinely-held pool worktree" that test_spawn_reuse_reads_real_treehouse_status_shape's own comment identifies as "the noise that teaches a captain to ignore the warning".
Consider distinguishing the three states — capture the status rc separately and, when the command itself failed, either stay silent or say "could not ask treehouse whether is still reserved" rather than asserting it is not.
| # reads, instead of a second warning naming a command that just repeats the | ||
| # refusal. Every other failure is a real un-released lease and must be loud. | ||
| case "$out" in | ||
| *'not managed by treehouse'*) return 0 ;; |
There was a problem hiding this comment.
This branch keys on treehouse's English error text. If a future treehouse rewords not managed by treehouse, the match silently stops firing and every isolation-guard abort gains a second, spurious could not release the treehouse lease ... line telling the captain to hand-run a command that just repeats the refusal — precisely the outcome test_spawn_lease_release_warning_is_signal was written to prevent, and that test would still pass because its stub hard-codes today's wording.
Same class of coupling as the treehouse status table parse at 307-317.
Not obviously worth fixing — there is no machine-readable alternative in v2.0.0, and both couplings fail loud-but-wrong rather than silently-dangerous. Flagging it so the version assumption is a conscious one: both sites name v2.0.0 in a comment, but nothing detects the day that stops being true.
| if [ -z "$WT" ]; then | ||
| echo "error: treehouse get did not enter a worktree within 60s; inspect window $T" >&2 | ||
| exit 1 | ||
| WT=$( cd "$PROJ_ABS" && treehouse get --lease --lease-holder "fm-$ID" ) || { |
There was a problem hiding this comment.
The lease is taken with --lease-holder "fm-$ID", and treehouse status's table carries the holder back as (held by <holder>) — the shape treehouse_holds_worktree already parses past (its trailing _). But the reuse check only asks is this path reserved, never is it reserved by this task.
The identity gate upstream makes that safe today: a meta that survived is this task's own, so a held path is held by this task. It is a free strengthening though — matching the holder against fm-$ID would turn a worktree that treehouse re-leased to a different task (after, say, a partial teardown that removed the lease but left the meta) from a silent reuse into a caught one. Today that case falls into the same silent-reuse path as a genuinely-still-held worktree.
Optional; the data is already in hand.
| # no startup-transient race. Sent to the stable window id (WT_TARGET) for the | ||
| # same rename-safety reason bin/backends/tmux.sh pins it, and buffered by the | ||
| # pane exactly like the GOTMPDIR export and launch command sent below. | ||
| spawn_send_text_line "$WT_TARGET" "cd $(shell_quote "$WT")" |
There was a problem hiding this comment.
This is the one safety property the change gives up, so it is worth signing off deliberately rather than inheriting.
The old poll hard-failed after 60s if the pane never left the project, which incidentally made it a readiness barrier: it absorbed the ~0.5-1s of shell startup that this PR's own root-cause analysis documents (docs/tmux-backend.md, "Worktree capture"). The new code fires this cd and then sends export GOTMPDIR plus the launch command shortly after, into a pane whose login shell may not have exec'd yet.
Send ordering through the pty is preserved, so the normal case is fine. The failure mode is narrow — the buffered cd being dropped, e.g. an rc-file stty flushing pending input — and it is caught downstream by the brief's isolation assertion (bin/fm-brief.sh:336, blocked: launched in primary checkout), so it is loud rather than silent. But nothing in fm-spawn detects the divergence, and meta would record the leased worktree while the agent actually started in $PROJ_ABS.
No change requested — confirming this is a deliberate trade rather than an incidental consequence of deleting the poll.
| # abort BETWEEN the lease and that write would strand the pool slot forever with | ||
| # nothing left naming it. Every guard above is exactly such an abort. This pins | ||
| # that fm-spawn releases the lease itself on those paths. | ||
| test_spawn_releases_lease_on_abort() { |
There was a problem hiding this comment.
This pins the lease release on the two guard aborts (the $FM_HOME backstop and the generic isolation refusal) and on the success path. The contract the trap actually claims is broader — bin/fm-spawn.sh:265-271 says it covers any abort before the meta write — and there is a real, unexercised gap between the two.
The turn-end hook block (fm-spawn.sh:1107-1180) runs after the lease is armed (1060) and before it is disarmed (1264), and does mkdir / cat > / mktemp under set -e. A failure in any of those, or in the meta write itself, is a leaked durable lease with no meta left to name it — the exact leak class this trap exists for, and the one the PR's own review called out as new to this change.
A case that makes $WT/.claude unwritable (or points GROK_AUTH_DIR somewhere mktemp fails) and asserts treehouse return --force still lands would close it. As written, a regression that moved LEASE_ABORT_CLEANUP=0 above the hook block would pass the whole suite.
…ainst $FM_HOME
Every projects/* spawn recorded worktree=$FM_HOME (the primary checkout) in
state/<id>.meta instead of the crew's real ~/.treehouse/... worktree, forcing a
manual repair each time and risking teardown/review on the wrong path. The crew
itself was always fine; firstmate-self spawns were immune.
Root cause: worktree discovery sent `treehouse get` into the pane and polled
#{pane_current_path} until it differed from the project dir, taking that as the
worktree. Right after `tmux new-window -c <project>`, the pane process is still
the forked tmux-server child (the shell has not yet exec'd and chdir'd), so
#{pane_current_path} transiently reports the tmux SERVER's own cwd - $FM_HOME
(the server was started there) - before settling on the -c dir a moment later.
The poll's "any path != project dir" exit latched that transient $FM_HOME on the
first iteration of a projects/* spawn ($FM_HOME != $FM_HOME/projects/<repo>),
before `treehouse get` even ran; a firstmate-self spawn ($FM_HOME == project dir)
kept polling to the real worktree - the exact projects/*-only signature. kunchenguid#134's
stable-window-id targeting could not help: the correctly-targeted pane
transiently reports the server cwd.
Fix (bin/fm-spawn.sh):
- Capture authoritatively: lease from fm-spawn itself with `treehouse get
--lease` (prints only the ~/.treehouse/... path to stdout, banners to stderr)
and cd the pane into that exact path. No poll, no startup-transient race.
worktree= and the turn-end hook use the leased path; fm-teardown's existing
`treehouse return --force <worktree=>` releases it, same as before. Consistent
with fm-home-seed.sh's existing lease pattern. Applies to every non-orca
backend and to batch (which re-execs the single path per pair).
- Loud backstop in validate_spawn_worktree: assert the worktree is not the
primary checkout ($FM_HOME/$FM_ROOT). Because firstmate is a git repo of
itself, $FM_HOME is a valid git toplevel that cleared every prior clause
silently; now a regression aborts loudly instead of corrupting meta.
Tests updated to the lease-based capture; new cases pin real-worktree capture
(never $FM_HOME) and the $FM_HOME abort. Docs: tmux-backend.md gains a
root-cause/verification section; architecture.md and herdr-backend.md corrected.
Root cause reproduced with tmux 3.6; fix verified end-to-end against real
treehouse (worktree= records a real .treehouse/... path, pane cd's into it).
Refs fm-spawn-wt-batch-x5.
…ding PROJ_ABS_REAL Review (decision A, captain rule: fix the root cause, do not soften) found the reframed test_spawn_symlinked_project_prefix_avoids_false_refusal no longer exercised the PROJ_ABS_REAL canonicalization its comment claimed to guard: its leased worktree was entirely distinct from the project, so validate_spawn_worktree's only project-dependent clause (wt_real == proj_real) was never near-triggered and the test would pass even if PROJ_ABS_REAL regressed to a logical PROJ_ABS. Add a tangle case: the treehouse stub leases the PROJECT ITSELF reached back through the symlinked prefix (physical path equal to the project), and the spawn must be REFUSED with "did not yield an isolated worktree". That refusal holds ONLY because validate_spawn_worktree resolves both sides with pwd -P; were PROJ_ABS_REAL the logical PROJ_ABS the physical worktree path would compare unequal and the tangle would be wrongly allowed. Test-only; bin/fm-spawn.sh unchanged. Refs fm-spawn-wt-batch-x5.
…op dead current_path ops
…refresh stale adapter docs
…and lease reservation
…e capture and respawn
…awn reuse Address the review on PR #7: - treehouse_reservation replaces the boolean holds-check and captures the `treehouse status` exit code separately, so a treehouse outage reports could-not-ask instead of a false "no longer reserves" on every respawn (#1). - Match the leased `(held by <holder>)` against this task's fm-<id>, so a worktree treehouse re-leased to a different task is caught, not silently adopted; an in-use row carries no holder, so the gate only strengthens (#3). - Cover the lease-release trap for a turn-end hook-block failure between the lease and the meta write, the leak class new to this change (#2). - Record the deliberate readiness-barrier trade at the cd site, marking the brief's isolation assertion load-bearing and forbidding a re-added pane probe (#4). - Make the two treehouse human-text couplings a conscious v2.0.0 assumption; v2.0.0 has no --json/--porcelain and both fail loud-but-wrong (#5). The test treehouse stub now records the real --lease-holder so status renders the caller's fm-<id>, exercising the holder match faithfully.
9585828 to
e3d8f20
Compare
What Changed
treehouse get --leasereservation, guarded by an abort-cleanup that releases the lease when a spawn fails beforestate/<id>.metais written, so an aborted spawn no longer strands a pool slot.project=/kind=/backend=(orca-boundary) identity check and atreehouse statusreservation probe that distinguishes held / held-by-other / available / unknown, so recovering a husk task keeps its branch, commits, and uncommitted work reachable.--helprange and strengthened tangle/symlinked-prefix tests).Risk Assessment
✅ Low: Touches the sensitive worktree-capture path, but the change is well-bounded, adds more safety guards than it removes, documents its tradeoffs in-code, and lands with extensive targeted test coverage; no material defect was substantiated.
Testing
Completed 1 recorded test check.
Pipeline
Updates from git push no-mistakes
⏭️ **intent** - skipped
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
✅ **Review** - passed
✅ No issues found.
✅ **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"✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.