Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions bin/fm-spawn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@
# provisioned firstmate home; the default is kind=ship.
# Before a secondmate launch, the home is locally fast-forwarded to the primary
# default-branch commit when safe; skipped syncs warn and launch unchanged.
# Before a ship or scout starts in a pooled Treehouse worktree, that clean task
# worktree fetches origin, resolves the current remote default branch, and fast-forwards to its tip.
# An unreachable origin, unresolved default branch, dirty worktree, or
# non-fast-forwardable base refuses the spawn rather than risking stale history.
# Ship/scout spawns refuse to launch unless the resolved task path is a real
# git worktree root distinct from the primary project checkout.
# Batch dispatch: pass one or more `id=repo` pairs instead of a single <id> <project>, e.g.
Expand Down Expand Up @@ -1686,6 +1690,67 @@ validate_spawn_worktree() { # <source> <inspect-target>
fi
}

freshen_spawn_worktree_base() { # <worktree>
local worktree=$1 default target expected actual status current
if ! git -C "$worktree" fetch --quiet origin; then
echo "error: could not fetch origin for pooled worktree '$worktree'; refusing to launch from a potentially stale base" >&2
return 1
fi
if ! git -C "$worktree" remote set-head origin --auto >/dev/null 2>&1; then
echo "error: could not resolve origin's current default branch for pooled worktree '$worktree'; refusing to launch from a potentially stale base" >&2
return 1
fi
default=$(default_branch "$worktree") || {
echo "error: could not determine origin's default branch for pooled worktree '$worktree'; refusing to launch from a potentially stale base" >&2
return 1
}
target="origin/$default"
if ! git -C "$worktree" fetch --quiet origin "+refs/heads/$default:refs/remotes/origin/$default"; then
echo "error: could not fetch '$target' for pooled worktree '$worktree'; refusing to launch from a potentially stale base" >&2
return 1
fi
expected=$(git -C "$worktree" rev-parse --verify --quiet "$target^{commit}" 2>/dev/null) || {
echo "error: '$target' is not a commit for pooled worktree '$worktree'; refusing to launch from a potentially stale base" >&2
return 1
}
status=$(git -C "$worktree" status --porcelain) || {
echo "error: could not inspect pooled worktree '$worktree' before refreshing its base" >&2
return 1
}
if [ -n "$status" ]; then
echo "error: pooled worktree '$worktree' is not clean; refusing to discard uncommitted work while refreshing its base" >&2
return 1
fi
current=$(git -C "$worktree" rev-parse --verify --quiet HEAD 2>/dev/null) || {
echo "error: could not read pooled worktree '$worktree' HEAD before refreshing its base" >&2
return 1
}
if [ "$current" = "$expected" ]; then
return 0
fi
if ! git -C "$worktree" merge-base --is-ancestor HEAD "$target" 2>/dev/null; then
echo "error: pooled worktree '$worktree' is not fast-forwardable to '$target'; refusing to discard local commits" >&2
return 1
fi
if ! git -C "$worktree" merge --ff-only "$target" >/dev/null; then
echo "error: could not fast-forward pooled worktree '$worktree' to '$target'; refusing to launch from a potentially stale base" >&2
return 1
fi
status=$(git -C "$worktree" status --porcelain) || {
echo "error: could not inspect pooled worktree '$worktree' after refreshing its base" >&2
return 1
}
if [ -n "$status" ]; then
echo "error: pooled worktree '$worktree' is not clean after refreshing its base; refusing to launch" >&2
return 1
fi
actual=$(git -C "$worktree" rev-parse --verify --quiet HEAD 2>/dev/null || true)
if [ "$actual" != "$expected" ]; then
echo "error: pooled worktree '$worktree' is at '${actual:-unknown}', not current '$target' ('$expected'); refusing to launch" >&2
return 1
fi
}

herdr_projection_meta_field_exact() { # <meta> <key>
local meta=$1 key=$2 count
[ -f "$meta" ] && [ ! -L "$meta" ] || return 1
Expand Down Expand Up @@ -2137,6 +2202,10 @@ if [ "$KIND" != secondmate ] && [ "$BACKEND" != orca ]; then

validate_spawn_worktree "treehouse get" "$T"
fi
if [ "$KIND" != secondmate ] && [ "$BACKEND" != orca ]; then
freshen_spawn_worktree_base "$WT" || exit 1
fi

if [ "$HARNESS" = omp ] && [ "$KIND" != secondmate ]; then
OMP_ABORT_INITIAL_HEAD=$(git -C "$WT" rev-parse HEAD 2>/dev/null) || {
echo "error: OMP spawn could not bind cleanup to the initial worktree HEAD" >&2
Expand Down
2 changes: 2 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ Codex App support is recorded in `docs/codex-app-backend.md`; it is not selectab

Crewmates never intentionally touch your project clone; [treehouse](https://github.com/kunchenguid/treehouse) pools clean worktrees for tmux, herdr, zellij, and cmux tasks, while Orca creates its own worktrees for `backend=orca`.
For ship and scout work, `fm-spawn.sh` refuses to launch unless the resolved task path is a real git worktree root that is distinct from the project primary checkout.
`fm-spawn.sh` also owns the base-freshness boundary for every ship and scout in a pooled Treehouse worktree: no worker starts from that worktree until it matches the fetched tip of origin's resolved default branch, and any unsafe or unverifiable base stops the spawn.
Its header owns the exact refusal mechanics, while [`tests/fm-spawn-pool-base-freshen.test.sh`](../tests/fm-spawn-pool-base-freshen.test.sh) owns the portable regression coverage.

The firstmate repo has one extra exposure because it can dispatch crewmates to work on itself.
Its operating checkout (`FM_ROOT`) and the disposable crewmate worktrees are all linked git worktrees of the same repository, so the valid discriminator is branch state, not whether the checkout is linked.
Expand Down
2 changes: 2 additions & 0 deletions tests/fm-backend-autodetect-smoke.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ git -C "$PROJ" init -q
printf '# scratch\n' > "$PROJ/README.md"
git -C "$PROJ" add README.md
git -C "$PROJ" -c user.name='Firstmate Tests' -c user.email='tests@example.invalid' commit -qm initial
git clone --quiet --bare "$PROJ" "$PROJ.origin.git"
git -C "$PROJ" remote add origin "file://$PROJ.origin.git"

# --- spawn with NO explicit backend config; HERDR_ENV=1 is the only marker --

Expand Down
2 changes: 2 additions & 0 deletions tests/fm-backend-herdr-launcher-workspace-e2e.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ make_scratch_project() { # <dir>
printf '# scratch\n' > "$dir/README.md"
git -C "$dir" add README.md
git -C "$dir" -c user.name='Firstmate Tests' -c user.email='tests@example.invalid' commit -qm initial
git clone --quiet --bare "$dir" "$dir.origin.git"
git -C "$dir" remote add origin "file://$dir.origin.git"
}

# make_workspace <label> -> "<workspace_id> <tab_id> <root_pane_id>"
Expand Down
2 changes: 2 additions & 0 deletions tests/fm-backend-herdr-presentation-e2e.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ make_project() { # <dir>
printf '# Herdr projection E2E fixture\n' > "$dir/README.md"
git -C "$dir" add README.md
git -C "$dir" -c user.name='Firstmate Tests' -c user.email='tests@example.invalid' commit -qm initial
git clone --quiet --bare "$dir" "$dir.origin.git"
git -C "$dir" remote add origin "file://$dir.origin.git"
}

spawn_task() { # <id> <home> <project>
Expand Down
2 changes: 2 additions & 0 deletions tests/fm-backend-herdr-workspace-per-home-e2e.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ make_scratch_project() { # <dir>
printf '# scratch\n' > "$dir/README.md"
git -C "$dir" add README.md
git -C "$dir" -c user.name='Firstmate Tests' -c user.email='tests@example.invalid' commit -qm initial
git clone --quiet --bare "$dir" "$dir.origin.git"
git -C "$dir" remote add origin "file://$dir.origin.git"
}

PROJ1="$TMP_ROOT/scratch-project-1"; make_scratch_project "$PROJ1"
Expand Down
1 change: 1 addition & 0 deletions tests/fm-backend-orca.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ test_spawn_writes_orca_metadata_and_launches_harness() {
state="$TMP_ROOT/spawn-state"
config="$TMP_ROOT/spawn-config"
fm_git_worktree "$proj" "$wt" "fm/$id"
git -C "$wt" remote remove origin
mkdir -p "$data/$id" "$state" "$config"
printf 'brief\n' > "$data/$id/brief.md"
touch "$state/.last-watcher-beat"
Expand Down
1 change: 1 addition & 0 deletions tests/fm-gate-refuse.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ test_spawn_refuses_and_admits() {
local home proj fakebin wt out rc
home="$TMP/spawn-home"; mkdir -p "$home/data"
proj=$(make_normal_repo "$TMP/spawn-proj")
fm_git_add_origin "$proj" "$TMP/spawn-origin.git"
fakebin=$(make_spawn_fakebin "$TMP/spawn-fake")
wt="$TMP/spawn-wt"
git -C "$proj" worktree add -q --detach "$wt" >/dev/null 2>&1
Expand Down
19 changes: 10 additions & 9 deletions tests/fm-spawn-dispatch-profile.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ make_spawn_case() {
fakebin=$(make_spawn_fakebin "$case_dir/fake")
mkdir -p "$home/data" "$home/projects" "$home/state" "$home/config"
printf '%s\n' "$harness" > "$home/config/crew-harness"
printf 'off\n' > "$home/config/herdr-presentation-spaces"
fm_git_worktree "$proj" "$wt" "wt-$name"
touch "$home/state/.last-watcher-beat"
for id in "$@"; do
Expand Down Expand Up @@ -715,7 +716,7 @@ test_omp_threads_exact_identity_model_and_every_thinking_level() {
read_case_record "$rec"
export FM_TEST_OMP_ACK="$HOME_DIR/state/$id.omp-started"

out=$(run_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR" \
out=$(run_ship_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR" \
--model openai-codex/gpt-5.6-sol --effort "$effort")
status=$?
expect_code 0 "$status" "OMP spawn with $effort thinking should succeed"
Expand Down Expand Up @@ -743,7 +744,7 @@ test_omp_herdr_worker_and_scout_launch_with_exact_identity_and_ack() {
rec=$(make_spawn_case "profile-omp-herdr-$kind" omp "$id")
read_case_record "$rec"
export FM_TEST_OMP_ACK="$HOME_DIR/state/$id.omp-started"
flag=
flag='--mode no-mistakes --yolo off'
[ "$kind" != scout ] || flag=--scout

out=$(run_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR" \
Expand Down Expand Up @@ -774,7 +775,7 @@ test_omp_refuses_unverified_backends_before_endpoint_creation() {
: > "$endpoint_log"

out=$(FM_FAKE_ENDPOINT_LOG="$endpoint_log" \
run_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR" --backend "$backend")
run_ship_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR" --backend "$backend")
status=$?
expect_code 1 "$status" "OMP should refuse unverified backend $backend"
assert_contains "$out" "verified only on backend=tmux or backend=herdr" \
Expand Down Expand Up @@ -834,7 +835,7 @@ test_omp_whitespace_identity_paths_refuse_before_endpoint() {
chmod +x "$spaced/$mode"
path="$spaced:$FAKEBIN_DIR"

out=$(run_spawn "$HOME_DIR" "$WT_DIR" "$path" "$LAUNCH_LOG" "$id" "$PROJ_DIR")
out=$(run_ship_spawn "$HOME_DIR" "$WT_DIR" "$path" "$LAUNCH_LOG" "$id" "$PROJ_DIR")
status=$?
expect_code 1 "$status" "OMP should refuse a whitespace-bearing $mode identity"
assert_contains "$out" 'canonical executable paths without whitespace' \
Expand Down Expand Up @@ -866,7 +867,7 @@ test_omp_missing_binary_or_capability_refuses_before_endpoint_and_metadata() {
FM_SPAWN_NO_GUARD=1 FM_FAKE_PANE_PATH="$WT_DIR" TMUX="fake,1,0" \
FM_FAKE_ENDPOINT_LOG="$endpoint_log" FM_FAKE_LAUNCH_LOG="$LAUNCH_LOG" \
PATH="$FAKEBIN_DIR:/usr/bin:/bin:/usr/sbin:/sbin" \
"$SPAWN" "$id" "$PROJ_DIR" 2>&1)
"$SPAWN" "$id" "$PROJ_DIR" --mode no-mistakes --yolo off 2>&1)
status=$?
expect_code 1 "$status" "OMP $mode should refuse before launch"
assert_contains "$out" "omp" "OMP preflight refusal did not name the selected runtime"
Expand All @@ -884,7 +885,7 @@ test_omp_launch_requires_observable_turn_start_acknowledgement() {
read_case_record "$rec"

out=$(FM_OMP_LAUNCH_ACK_POLLS=2 FM_OMP_LAUNCH_ACK_INTERVAL=0.01 \
run_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR")
run_ship_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR")
status=$?
expect_code 1 "$status" "unacknowledged OMP launch should fail"
assert_contains "$out" "initial instruction was not acknowledged" \
Expand All @@ -906,7 +907,7 @@ test_omp_herdr_unacked_launch_cleans_owned_endpoint_worktree_and_artifacts() {
read_case_record "$rec"

out=$(FM_OMP_LAUNCH_ACK_POLLS=2 FM_OMP_LAUNCH_ACK_INTERVAL=0.01 \
run_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR" --backend herdr)
run_ship_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR" --backend herdr)
status=$?
expect_code 1 "$status" "unacknowledged OMP Herdr launch should fail"
assert_contains "$out" "initial instruction was not acknowledged" \
Expand All @@ -929,7 +930,7 @@ test_omp_herdr_refused_close_preserves_worktree_and_artifacts() {

out=$(FM_OMP_LAUNCH_ACK_POLLS=2 FM_OMP_LAUNCH_ACK_INTERVAL=0.01 \
FM_TEST_HERDR_REFUSE_CLOSE=1 \
run_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR" --backend herdr)
run_ship_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR" --backend herdr)
status=$?
expect_code 1 "$status" "unacknowledged OMP Herdr launch should fail"
assert_contains "$out" "could not confirm its owned endpoint stopped" \
Expand All @@ -953,7 +954,7 @@ test_omp_ack_cleanup_preserves_artifacts_when_ownership_changes() {

out=$(FM_OMP_LAUNCH_ACK_POLLS=2 FM_OMP_LAUNCH_ACK_INTERVAL=0.01 \
FM_TEST_OMP_META_TAMPER="$HOME_DIR/state/$id.meta" \
run_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR")
run_ship_spawn "$HOME_DIR" "$WT_DIR" "$FAKEBIN_DIR" "$LAUNCH_LOG" "$id" "$PROJ_DIR")
status=$?
expect_code 1 "$status" "ownership-changed OMP launch should fail"
assert_contains "$out" "could not prove ownership" \
Expand Down
Loading