Skip to content

fix(bin): base local-only spawns and reviews on the local default branch - #4284

Open
phorel wants to merge 8 commits into
kunchenguid:mainfrom
phorel:fm/pool-base-gate
Open

phorel wants to merge 8 commits into
kunchenguid:mainfrom
phorel:fm/pool-base-gate

Conversation

@phorel

@phorel phorel commented Sep 12, 2026 •

Copy link
Copy Markdown

The bug

freshen_spawn_worktree_base in bin/fm-spawn.sh reset every pooled worktree to origin/<default>.

That is correct for a project whose approved work reaches the remote. It is wrong for a local-only project, where bin/fm-merge-local.sh fast-forwards the local default branch and never pushes. Every subsequent spawn then hands the worker a base missing exactly the landed-but-unpushed commits, and the branch that worker produces proposes reverting them.

This is not theoretical and not a race. It reproduced on the first task dispatched after the first local landing: the resulting branch would have reverted a feature that had just been approved, and the deletions were invisible inside an otherwise ordinary feature diff. It compounds — each further local merge widens the gap.

The fix

Base selection is keyed on the task's own recorded delivery mode, not on the project registry. The registry records a standing posture; whether a given piece of work lands locally is a per-task fact, and bin/fm-merge-local.sh already gates the local landing on mode= in the task's metadata.

With an origin:

  • mode=local-only and origin/<default> is an ancestor of the local default branch → prefer the local default branch.
  • any other mode, no recorded mode, or a local branch that is behind or diverged → origin/<default> stays authoritative, exactly as before.

With no origin at all: the local default branch is the only possible authority, so the worktree refreshes to it in every mode. This was the one shape left unrefreshed by the original fix, and the one where local is unambiguously correct.

The refresh detaches HEAD rather than resetting in place, so it can never move a branch reference another task owns — in an origin-less repository those commits exist nowhere else. Every pre-existing refusal is preserved: a dirty worktree still refuses rather than discarding uncommitted work, and the refresh additionally refuses when no default branch can be determined rather than launching from an unverified base.

bin/fm-review-diff.sh carried the same assumption and gets the same rule, so what is reviewed matches what will actually be delivered. Without that, a PR-delivered project whose local default is ahead of origin would have the review silently under-report the diff.

Known limit

An origin-less repository whose default branch is named neither main nor master is not supported by the pooled base path; it refuses rather than guessing. An earlier revision resolved this from the repository's own HEAD, which was dropped: it created a second definition of "the default branch" disagreeing with default_branch in bin/fm-ff-lib.sh, the one every other consumer uses — and spawn would then accept a base that bin/fm-merge-local.sh refuses at landing time, so the task could never ship. If that support is ever wanted, it belongs in default_branch so every consumer gains it at once.

Tests

tests/fm-spawn-pool-base-freshen.test.sh and tests/fm-review-diff.test.sh cover local ahead, equal, behind, diverged, origin-less, and dirty, plus the mode distinction in both directions: a local-only task on a no-mistakes-registered project prefers local, and a no-mistakes task on a local-only-registered project keeps origin. They run against real git repositories rather than mocks — the original defect was in what git actually reported, and a mocked ancestor check would have passed while the real one failed.

bin/fm-test-run.sh routes edits to the new library to the spawn suite; without that, an edit to the library alone would have run none of its proof.

… pooled base

A pooled worktree was always reset to origin/<default> before launch. That is
right for a project whose landed work reaches origin, and wrong for a
`local-only` project: bin/fm-merge-local.sh merges approved work into the LOCAL
default branch and never pushes, so origin's tip is missing work the captain has
already approved. The next task then started from a base predating that merge,
and its branch read as a revert of the landed work - silent, because the
worker's own change was correct and the deletions hid inside its diff, and
compounding, because every further local landing widened the gap. It is
guaranteed rather than occasional: every `local-only` project has this shape.

The base now follows where landed work actually lives, by cases:

- origin is an ancestor of the local default branch (strictly ahead): use the
  local branch. It holds everything origin has plus the locally landed work, so
  nothing can be lost by starting there. This is the safe case precisely because
  containment is proven, not assumed.
- equal: the same commit, so origin stands and behaviour is unchanged.
- local behind origin: origin carries work the local branch lacks, so origin
  remains authoritative.
- diverged, with origin not an ancestor: origin remains authoritative. A local
  branch that does not contain origin's tip is not something to silently build
  on.

Everything else is untouched: origin is still fetched, origin's default branch
is still resolved, a worktree with no origin still returns early, and every
existing refusal - dirty worktree, stale submodule pin, unreachable origin,
unresolved remote default, post-reset verification - still refuses. The
preference only picks between two verified commits.

fm-review-diff.sh carried the same assumption in the same shape: with an origin
configured it always reviewed against origin/<default>, so a local-only branch
would be reviewed against a base missing the landed work, reporting that work as
part of the branch's own change. It now applies the same rule, pointing at the
spawn helper as the owner of the reasoning. bin/fm-teardown.sh and
bin/fm-fleet-sync.sh were checked and are not affected: teardown already tests
local-only work against the local default branch, and fleet sync only
fast-forwards, so a local-ahead clone is skipped rather than rewound.

Tests build real git repositories rather than mocking the ancestor check, since
the defect was in what git actually reported about origin and local refs.
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