Skip to content

fix(bin): fold path spellings in worktree and watcher identity checks - #2705

Open
chappellerj wants to merge 3 commits into
kunchenguid:mainfrom
chappellerj:fm/fmcasefix-t4
Open

chappellerj wants to merge 3 commits into
kunchenguid:mainfrom
chappellerj:fm/fmcasefix-t4

Conversation

@chappellerj

Copy link
Copy Markdown

Summary

Two safety checks in firstmate's own tooling compared paths as raw strings, so one directory reached under two spellings read as two different locations. Both defeat checks that protect the never-write-to-projects prime directive.

Defect 1: case-variant home path defeats identity checks

A finding that departs from the original framing: the task prescribed normalizing with pwd -P, and fm-spawn already did that (landed in #294, a month before the incident). pwd -P resolves symlinks but preserves the caller's spelling of each component:

cd /x/Home && pwd -P   ->  /x/Home
cd /x/home && pwd -P   ->  /x/home     # same inode, different string
realpath /x/home       ->  /x/Home     # folds to the on-disk name

Perl's Cwd::realpath and Python's os.path.realpath behave like pwd -P, so the new helper deliberately shells out to realpath(1) despite an existing perl-realpath idiom elsewhere in the repo.

Reproduced the near-miss: a worktree pointed at the primary clone with a case-variant project path was ACCEPTED before the fix, REFUSED after.

New fm_canonical_path / fm_canonical_file_path in fm-wake-lib.sh are applied on both sides of the spawn isolation assertion and both watcher-lock identity comparisons (fm-wake-lib.sh and fm-watch-arm.sh shared the weakness). The guard failure produced false "watcher down" alarms after a session resume.

Defect 2: pool slot reuse leaves stale meta worktree pointers

fm-spawn now records the canonical path the lease actually settled on. fm-teardown refuses when the recorded worktree holds a different task's fm/<id> branch, before the step that deletes that branch and resets the worktree.

Deliberate design decisions

  • The spawn-side double-lease check warns rather than refuses. A lingering record is usually a finished task awaiting cleanup, so refusing would block routine spawns on stale metadata. The destructive end refuses.
  • The teardown refusal requires positive proof: a detached HEAD (how a landed task legitimately ends) or a non-fm/ branch name does not block cleanup.
  • --force deliberately does not override the refusal: it authorizes discarding this task's own work, never another task's.

Review round

The review gate caught that the guard covered only the non-orca destructive path while the orca branch ran the identical checkout --detach + branch -D sequence unguarded. Extended to orca, plus canonical_existing_dir (feeding orca's path-match equality check) had the same pwd -P weakness and could cause a spurious refusal on a legitimate orca teardown. The hazard class is now closed on every supported backend.

Verification

  • bin/fm-lint.sh passes (pinned ShellCheck 0.11.0, actionlint 1.7.12).
  • New tests are proven to fail without their fix and pass with it.
  • Tests skip on case-sensitive filesystems, where the variant path cannot exist.
  • Four pre-existing test failures (fm-teardown herdr-preflight, fm-watch-arm, fm-wake-queue, fm-backend-orca metadata-write) were each confirmed failing identically on the clean baseline via git stash / git checkout — unrelated to this change.

Two safety checks compared paths as raw strings, so one directory reached
under two spellings read as two different locations.

Worktree isolation: fm-spawn's assertion canonicalized with `cd … && pwd -P`,
which resolves symlinks but preserves the caller's spelling of each component.
On a case-insensitive filesystem a session opened as .../Firstmate and a
treehouse path spelled .../firstmate are the same directory, so the guard
compared them as distinct and accepted the primary clone as an isolated
worktree. realpath(1) folds the spelling to the on-disk name; Perl's
Cwd::realpath and Python's os.path.realpath share pwd -P's behavior and are
unsuitable. fm_canonical_path now owns this and is used on both sides of the
isolation assertion and of the watcher lock's home and watcher-path identity
comparison, which produced false "watcher down" alarms after a session resume.

Pool slot reuse: a treehouse slot is a reusable lease, so the recorded
worktree= can name a slot already re-leased to another task. fm-spawn now
records the canonical path the lease actually settled on and warns when
another task's metadata claims the same slot; cleanup refuses outright when
the recorded worktree holds a different task's fm/<id> branch, since its next
steps delete that branch and reset the worktree. The refusal needs positive
proof of a conflict, so a detached HEAD or a non-fm/ branch does not block
routine cleanup, and --force does not override it: --force authorizes
discarding this task's own work, never another task's.

Tests cover the case-variant identity match (with an unrelated home still
rejected), fm_canonical_path's folding, and the cleanup refusal, all skipping
where the filesystem is case-sensitive and the case cannot arise.
@kunchenguid

Copy link
Copy Markdown
Owner

Speaking as Kun's firstmate:

Scheduled firstmate-oss pass. Diff reviewed against main (head 562267bdb16ded3e7a997ebc31d001b22b9abb7c). No security risk. Approving the pending first-time fork workflow runs.

Classification: corrective. Physical-dir identity was comparing caller spelling (pwd -P) so a case-variant primary checkout was accepted as an isolated worktree, and watcher lock identity produced false "watcher down" after resume. fm_canonical_path / fm_canonical_file_path fold via realpath(1) (documented why Perl/Python realpath and pwd -P are insufficient). Isolation assertion, watcher lock home/script compare, spawn worktree= recording, and orca canonical_existing_dir all use it. Recycled pool-slot teardown now refuses on positive proof of another task's fm/<id> branch; --force does not override. Spawn double-lease warns rather than refuses (stale meta must not block routine spawns; damage is at the destructive end).

VISION:

  • One captain, one interface: aligns. Removes false watcher-down noise after resume.
  • Authority is explicit and never inferred: aligns. Strengthens the never-write-to-primary isolation guard; does not add capability.
  • Scripts own the mechanics, agents own the judgment: aligns. Path identity is scripted.
  • A restart is a non-event: aligns. Watcher lock identity survives session-resume spelling drift.
  • Delegation with a spine: aligns. Isolation refusal and "unlanded work is never torn down" both get stronger (foreign fm/<id> checkout refused, including under --force).
  • The fleet outlives any vendor: aligns. Applied on tmux/treehouse and orca teardown.
  • Scope: aligns. Command-layer safety of firstmate's own spawn/teardown/watcher.

Merge-eligible: YES. Same physical-dir class as earlier #2692.

Overlap: teardown identity matches only fm/*. If #2706's config/branch-prefix lands first, a ryan-fm/<other-id> recycled slot would not be recognized as foreign. Also both this PR and #2700 patch bin/fm-spawn.sh.

@kunchenguid kunchenguid left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Speaking as Kun's firstmate: reviewed the full diff. No security risk. Corrective path-identity / pool-slot teardown guards. Approve pending first-time fork CI.

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