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
89 changes: 80 additions & 9 deletions bin/fm-crew-state.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,23 @@
# or blocker, fm-send appends a resolved event at answer time, but that event
# still does not describe the crew's current state as it resumes, fixes, or
# re-validates. This helper never infers the current state from a tail of the log:
# it reads the authoritative source (a
# no-mistakes run-step attributed under bin/fm-nm-run-lib.sh's contract, else
# the pane busy-signature) and reconciles the possibly-stale log against it.
# it reads the authoritative source (a no-mistakes run-step attributed under
# bin/fm-nm-run-lib.sh's contract, else the pane busy-signature) and reconciles
# the possibly-stale log against it. State selection is deterministic and
# read-only apart from local-only accepted-head recording below.
# A ship `done` is accepted only through the delivery gate in emit(): a clean
# worktree, complete acceptance evidence (bin/fm-receipt-check.sh), pr= recorded
# by bin/fm-pr-check.sh for the PR modes or a clean checked-out fm/<id> branch
# for local-only. For no-mistakes, it also applies the ask-user decision audit
# owned by bin/fm-nm-run-lib.sh when a recorded or attributed full run is
# available; absence of that identity does not itself refuse done. PR-ready
# registration owns the required audit before recording the PR.
# for local-only. For a local-only ready report, this helper records the branch's
# full commit id as local_done_head= in task metadata and appends the ready
# report with an [accepted local_done_head=<full commit id>] suffix to the
# status log. Re-reading that accepted report preserves the pin and refuses a
# moved head; a fresh valid ready report can replace the pin.
# Landing rechecks that exact head.
# For no-mistakes, it also applies the ask-user decision audit owned by
# bin/fm-nm-run-lib.sh when a recorded or attributed full run is available;
# absence of that identity does not itself refuse done. PR-ready registration
# owns the required audit before recording the PR.
#
# The determinism lives entirely here - only run-step / pane / log reads plus
# fixed mapping logic, no heuristics and no LLM. Output is one stable, parseable,
Expand Down Expand Up @@ -48,8 +55,8 @@
# attributed to this crew, a dead endpoint also reports unknown · none rather
# than trusting a stale status log.
#
# Read-only and side-effect free. Always exits 0 on a successful read regardless
# of state; exit 2 only on a usage error (no id).
# Always exits 0 on a successful read regardless of state; exit 2 only on a
# usage error (no id). A valid local-only ready report may pin its branch head.
set -u

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
Expand Down Expand Up @@ -122,6 +129,20 @@ emit() { # <state> <source> [detail]
;;
esac
fi
if [ "$state" = 'done' ] && [ "${KIND:-}" = ship ] && [ "$mode" = local-only ] \
&& [ "$(status_line_verb "${LOG_LINE:-}")" = 'done' ]; then
if ! status_done_line_has_artifact "$LOG_LINE" ship local-only "$ID"; then
state=parked
source=delivery-gate
detail='local-only done report must say ready in branch'
else
gate_detail=$(pin_local_done_head) || {
state=parked
source=delivery-gate
detail=$gate_detail
}
fi
fi
# The done-time ask-user decision audit: the same predicate PR-ready applies,
# against the recorded nm_run_id or, before registration, the attributed run.
if [ "$state" = 'done' ] && [ "${KIND:-}" = ship ] && [ "$mode" = no-mistakes ]; then
Expand Down Expand Up @@ -157,6 +178,52 @@ meta_value() { # <key>
grep "^$1=" "$META" 2>/dev/null | tail -1 | cut -d= -f2- || true
}

pin_local_done_head() (
local project current control lock tmp count recorded captured
project=$(meta_value project)
[ -n "$project" ] || { printf 'local-only project is missing from task metadata'; return 1; }
captured=$(cat "$META") || { printf 'task metadata could not be read'; return 1; }
[ "$LOG_LINE" = "$(log_last_line)" ] \
|| { printf 'local-only ready report changed before its head was recorded'; return 1; }
control="$STATE/.control-$ID.lock"
lock=$(fm_meta_lock_path "$META") || { printf 'task metadata lock path is invalid'; return 1; }
fm_lock_acquire_wait "$control" || { printf 'task control lock could not be acquired'; return 1; }
fm_lock_acquire_wait "$lock" || {
fm_lock_release "$control" || true
printf 'task metadata lock could not be acquired'
return 1
}
tmp=
trap '[ -z "$tmp" ] || rm -f -- "$tmp"; fm_lock_release "$lock" || true; fm_lock_release "$control" || true' EXIT
[ -f "$META" ] && [ ! -L "$META" ] && [ "$(cat "$META")" = "$captured" ] \
|| { printf 'task metadata changed before the local done head was recorded'; return 1; }
[ "$(grep '^project=' "$META" | cut -d= -f2-)" = "$project" ] \
|| { printf 'task project changed before the local done head was recorded'; return 1; }
[ "$LOG_LINE" = "$(log_last_line)" ] \
|| { printf 'local-only ready report changed before its head was recorded'; return 1; }
current=$(git -C "$project" rev-parse --verify --quiet "refs/heads/fm/$ID^{commit}" 2>/dev/null) \
|| { printf 'local-only task branch fm/%s is missing from the project' "$ID"; return 1; }
count=$(awk -F= '$1 == "local_done_head" { n++ } END { print n+0 }' "$META")
recorded=$(grep '^local_done_head=' "$META" | cut -d= -f2- || true)
case "$LOG_LINE" in
*' [accepted local_done_head='*']')
[ "$count" -eq 1 ] && [ "$recorded" = "$current" ] \
&& [ "${LOG_LINE##* [accepted local_done_head=}" = "$recorded]" ] \
|| { printf 'branch fm/%s differs from the recorded local done head; task must report ready again' "$ID"; return 1; }
return 0
;;
esac
tmp=$(mktemp "$STATE/.local-done-$ID.XXXXXX") \
|| { printf 'local done head could not be written to task metadata'; return 1; }
{ sed '/^local_done_head=/d' "$META" && printf '\nlocal_done_head=%s\n' "$current"; } > "$tmp" \
|| { printf 'local done head could not be written to task metadata'; return 1; }
mv -f -- "$tmp" "$META" \
|| { printf 'local done head could not be saved to task metadata'; return 1; }
tmp=
printf '%s [accepted local_done_head=%s]\n' "$LOG_LINE" "$current" >> "$LOG" \
|| { printf 'local done head acceptance could not be recorded in the status log'; return 1; }
)

WT=$(meta_value worktree)
KIND=$(meta_value kind)
HARNESS=$(meta_value harness)
Expand All @@ -177,6 +244,10 @@ fi
if [ -z "$WT" ] || [ ! -d "$WT" ]; then
emit unknown none "worktree gone (torn down?)"
fi
if [ "$KIND" = ship ] && [ "$MODE" = local-only ]; then
# shellcheck source=bin/fm-wake-lib.sh
. "$SCRIPT_DIR/fm-wake-lib.sh"
fi

# --- status log ------------------------------------------------------------

Expand Down
74 changes: 65 additions & 9 deletions bin/fm-merge-local.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#!/usr/bin/env bash
# Perform the approved local merge for a local-only ship task: fast-forward the
# project's default branch to the crewmate's fm/<id> branch.
# project's default branch to the exact local_done_head= recorded when the
# accepted ready report was read by bin/fm-crew-state.sh.
# Missing or malformed heads, or a moved branch, refuse landing.
# In-flight local-only tasks without a recorded head must report ready again before approval.
#
# This is firstmate's merge gate-action (the captain's merge authority applied
# locally instead of via a GitHub PR). It is the one sanctioned exception to hard
# rule #1 "never run state-changing git in projects/", and it is narrow: it only
# runs for mode=local-only tasks, only after the captain approves (or yolo=on
# auto-approves), and only as a clean fast-forward - it refuses a diverged branch
# and tells you to have the crewmate rebase. See AGENTS.md prime directives,
# project management, and task lifecycle.
# auto-approves), and only as a clean fast-forward - a diverged branch needs a
# rebase and a new approval. See AGENTS.md prime directives, project management,
# and task lifecycle.
#
# A ship task with any accepted-blocked acceptance criterion is refused before
# the fast-forward, naming the criteria and --captain-instruction, the only
Expand All @@ -28,6 +31,8 @@ STATE="${FM_STATE_OVERRIDE:-$FM_HOME/state}"
# shellcheck source=bin/fm-lease-lib.sh
. "$SCRIPT_DIR/fm-lease-lib.sh"
fm_lease_forbid_branch "local-only landing (fm-merge-local)"
# shellcheck source=bin/fm-wake-lib.sh
. "$SCRIPT_DIR/fm-wake-lib.sh"
# shellcheck source=bin/fm-pr-lib.sh
. "$SCRIPT_DIR/fm-pr-lib.sh"
# shellcheck source=bin/fm-merge-guard-lib.sh
Expand All @@ -47,12 +52,49 @@ fm_pr_task_id_valid "$ID" || { echo "error: invalid task id: $ID" >&2; exit 2; }
META="$STATE/$ID.meta"
[ -f "$META" ] || { echo "error: no meta for task $ID at $META" >&2; exit 1; }

CONTROL_LOCK="$STATE/.control-$ID.lock"
META_LOCK=$(fm_meta_lock_path "$META") || exit 1
PROJECT_LOCK=
cleanup_locks() {
[ -z "$PROJECT_LOCK" ] || fm_lock_release "$PROJECT_LOCK" || true
[ -z "$META_LOCK" ] || fm_lock_release "$META_LOCK" || true
[ -z "$CONTROL_LOCK" ] || fm_lock_release "$CONTROL_LOCK" || true
}
trap cleanup_locks EXIT
fm_lock_acquire_wait "$CONTROL_LOCK"
fm_lock_acquire_wait "$META_LOCK"

PROJ=$(grep '^project=' "$META" | cut -d= -f2-)
MODE=$(grep '^mode=' "$META" | cut -d= -f2- || true)
[ "$MODE" = local-only ] || { echo "error: task $ID is mode=$MODE, not local-only; merge PR tasks with bin/fm-pr-merge.sh <id> <PR url> after approval" >&2; exit 1; }

local_done_head_count=$(awk -F= '$1 == "local_done_head" { n++ } END { print n+0 }' "$META")
local_done_head=$(grep '^local_done_head=' "$META" | cut -d= -f2- || true)
case ${#local_done_head} in
40|64) ;;
*)
echo "error: missing or malformed local done head for task $ID; refusing; task must report ready again before approval" >&2
exit 1
;;
esac
case "$local_done_head" in
*[!0-9a-f]*)
echo "error: missing or malformed local done head for task $ID; refusing; task must report ready again before approval" >&2
exit 1
;;
esac
[ "$local_done_head_count" -eq 1 ] || {
echo "error: missing or malformed local done head for task $ID; refusing; task must report ready again before approval" >&2
exit 1
}

BRANCH="fm/$ID"
git -C "$PROJ" rev-parse --verify --quiet "refs/heads/$BRANCH" >/dev/null || { echo "error: branch $BRANCH does not exist in $PROJ" >&2; exit 1; }
project_git_dir=$(git -C "$PROJ" rev-parse --absolute-git-dir) || {
echo "error: cannot determine the project checkout's Git directory" >&2
exit 1
}
PROJECT_LOCK="$project_git_dir/fm-local-landing.lock"
fm_lock_acquire_wait "$PROJECT_LOCK"

DEFAULT=$("$FM_ROOT/bin/fm-local-default.sh" "$PROJ") \
|| { echo "error: cannot determine default branch for $PROJ; expected a local origin/HEAD branch, main, or master" >&2; exit 1; }
Expand All @@ -66,18 +108,32 @@ if [ -n "$(git -C "$PROJ" status --porcelain 2>/dev/null | head -1)" ]; then
exit 1
fi

# Clean fast-forward only: DEFAULT must be an ancestor of BRANCH.
if ! git -C "$PROJ" merge-base --is-ancestor "$DEFAULT" "$BRANCH"; then
branch_head=$(git -C "$PROJ" rev-parse --verify --quiet "refs/heads/$BRANCH^{commit}" 2>/dev/null || true)
[ -n "$branch_head" ] || { echo "error: branch $BRANCH does not exist in $PROJ" >&2; exit 1; }
[ "$branch_head" = "$local_done_head" ] || {
echo "error: branch $BRANCH differs from the recorded local done head; refusing; task $ID must report ready again before landing" >&2
exit 1
}

# Clean fast-forward only: DEFAULT must be an ancestor of the accepted commit.
if ! git -C "$PROJ" merge-base --is-ancestor "$DEFAULT" "$local_done_head"; then
echo "REFUSED: $BRANCH is not a fast-forward of $DEFAULT (it has diverged)." >&2
echo "Have the crewmate rebase $BRANCH onto $DEFAULT, then retry." >&2
echo "Have the crewmate rebase $BRANCH onto $DEFAULT, then report ready again." >&2
exit 1
fi

FM_MERGE_GUARD_DATA="${FM_DATA_OVERRIDE:-$FM_HOME/data}"
fm_merge_guard_check_accepted_blocked "$ID" "$META"
fm_merge_guard_resolve "$ID" fm-merge-local "$BRANCH" "$CAPTAIN_INSTRUCTION" || exit 1

# Recheck after approval guards, then land the immutable accepted object rather
# than resolving the mutable branch name again.
branch_head=$(git -C "$PROJ" rev-parse --verify --quiet "refs/heads/$BRANCH^{commit}" 2>/dev/null || true)
[ "$branch_head" = "$local_done_head" ] || {
echo "error: branch $BRANCH differs from the recorded local done head; refusing; task $ID must report ready again before landing" >&2
exit 1
}
before=$(git -C "$PROJ" rev-parse --short "$DEFAULT")
git -C "$PROJ" merge --ff-only "$BRANCH" >/dev/null
git -C "$PROJ" merge --ff-only "$local_done_head" >/dev/null
after=$(git -C "$PROJ" rev-parse --short "$DEFAULT")
echo "merged $BRANCH into local $DEFAULT ($before -> $after) in $PROJ"
2 changes: 1 addition & 1 deletion bin/fm-nm-run-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Shared no-mistakes axi run attribution primitives.
#
# ONE owner for the no-mistakes run-attribution primitives used by
# fm-crew-state.sh (read-only current-state reporting and the done-time
# fm-crew-state.sh (current-state reporting and the done-time
# ask-user decision audit), fm-teardown.sh (pre-teardown run abort, see its
# "Fix 1" header comment), and fm-pr-check.sh (PR-ready run identity and the
# decision-evidence audit). Teardown uses only strict branch-and-head identity;
Expand Down
3 changes: 3 additions & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Outside away mode, idle-open-work probes, terminal and nonterminal stable-hash t
The done check clears stale wedge aging at triage and timer expiry; it does not suppress newly appended captain-relevant status events, and resumed work remains subject to the current-state supervision rules.
`tests/fm-watch-triage.test.sh` covers the run-step-done classifier, idle-probe absorption, timer-expiry absorption, new decision delivery, and resumed-work probing.
Generated briefs bind each crewmate to report a terminal or `paused:` status within that bound while its board row remains in flight, and a `done:` claim is accepted only with its delivery artifact - a canonical PR URL for the PR modes, `ready in branch` for local-only, or the report path for a scout - with ship done acceptance enforced by `bin/fm-crew-state.sh`'s delivery gate (complete acceptance evidence from `bin/fm-receipt-check.sh` plus the PR registered by `bin/fm-pr-check.sh` or a clean ready branch) and final cleanup enforced by `bin/fm-teardown.sh`.
For local-only ship work, `bin/fm-crew-state.sh` freezes the accepted branch's full commit id until a fresh valid ready report is accepted; its header owns the metadata and status-log recording mechanics.
`bin/fm-merge-local.sh` lands that immutable commit, refusing a missing or malformed pin or a branch-head mismatch while holding the task control, task metadata, and project landing locks across verification and fast-forward.
In-flight local-only tasks must report ready again before approval.
Repeated provably-working stale escalations on the same unchanged pane add an escalation count to the wake reason and, at `FM_WEDGE_DEMAND_INSPECT_COUNT`, a `demand-deep-inspection` marker.
Pane quietness plus the run step can miss a crew writing source, tests, or documentation behind a static pane, so a file newer than the start of the current quiet window in that task's recorded worktree defers the escalation.
That deferral re-surfaces on the same `FM_PAUSE_RESURFACE_SECS` cadence as a declared wait, with a reason naming the write evidence rather than a wedge, and it is bounded to one pruned, depth-bounded, wall-clock-bounded walk (`FM_WORKTREE_WRITE_PRUNE`, `FM_WORKTREE_WRITE_MAXDEPTH`, `FM_WORKTREE_WRITE_TIMEOUT`) taken only in the branch that was about to escalate, never on every poll.
Expand Down
44 changes: 41 additions & 3 deletions tests/fm-crew-state.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1637,11 +1637,12 @@ test_pr_modes_done_requires_registered_pr() {

test_local_only_done_requires_clean_task_branch() {
reset_fakes
local d out id=delivery-gate-local
local d out id=delivery-gate-local accepted revised base rc
d=$(new_case "$id")
make_repo_on_branch "$d/wt" other-branch
make_fakebin "$d" >/dev/null
fm_write_meta "$d/state/$id.meta" "window=fm:fm-$id" "worktree=$d/wt" "kind=ship" "harness=claude" "mode=local-only"
fm_write_meta "$d/state/$id.meta" "window=fm:fm-$id" "worktree=$d/wt" "project=$d/wt" \
"kind=ship" "harness=claude" "mode=local-only"
printf 'done: ready in branch fm/%s\n' "$id" > "$d/state/$id.status"
FM_FAKE_AXI_STATUS=
FM_FAKE_RUNS_LIST=
Expand All @@ -1651,10 +1652,47 @@ test_local_only_done_requires_clean_task_branch() {
assert_contains "$out" "state: parked" "local-only done on another branch was accepted"
assert_contains "$out" "source: delivery-gate" "local-only branch gate did not name itself"
assert_contains "$out" "branch fm/$id is not checked out" "local-only branch gate did not say what is missing"
git -C "$d/wt" branch -f main HEAD
base=$(git -C "$d/wt" rev-parse main)
git -C "$d/wt" checkout -q -b "fm/$id"
git -C "$d/wt" commit -q --allow-empty -m approved
out=$(run_crew_state "$d" "$id")
assert_contains "$out" "state: done" "local-only done on the clean task branch was refused"
pass "local-only done requires the clean fm/<id> branch and no PR"
accepted=$(git -C "$d/wt" rev-parse HEAD)
assert_grep "local_done_head=$accepted" "$d/state/$id.meta" \
"accepted local-only ready report did not persist its full branch head"
git -C "$d/wt" commit -q --allow-empty -m unapproved
out=$(run_crew_state "$d" "$id")
assert_contains "$out" "state: parked" "a post-report commit did not invalidate the accepted head"
assert_contains "$out" "must report ready again" "a moved branch did not require a fresh ready report"
assert_grep "local_done_head=$accepted" "$d/state/$id.meta" \
"ordinary state reads replaced the accepted head"
revised=$(git -C "$d/wt" rev-parse HEAD)
git -C "$d/wt" checkout -q main
rc=0
env -u FM_TASK_ID FM_HOME="$d" bash "$ROOT/bin/fm-merge-local.sh" "$id" \
> "$d/merge-output" 2>&1 || rc=$?
expect_code 1 "$rc" "post-report commit was allowed to land"
assert_grep 'must report ready again' "$d/merge-output" "merge did not request a fresh report"
[ "$(git -C "$d/wt" rev-parse main)" = "$base" ] || fail "refusal moved main"
git -C "$d/wt" checkout -q "fm/$id"
printf 'done: implemented\n' >> "$d/state/$id.status"
out=$(run_crew_state "$d" "$id")
assert_contains "$out" "state: parked" "invalid fresh report was accepted"
assert_grep "local_done_head=$accepted" "$d/state/$id.meta" "invalid report replaced the pin"
printf 'done: ready in branch fm/%s\n' "$id" >> "$d/state/$id.status"
out=$(run_crew_state "$d" "$id")
assert_contains "$out" "state: done" "fresh ready report could not replace the pin"
assert_grep "local_done_head=$revised" "$d/state/$id.meta" "fresh report did not pin B"
out=$(run_crew_state "$d" "$id")
assert_contains "$out" "state: done" "repeated read rejected the accepted report"
git -C "$d/wt" checkout -q main
rc=0
env -u FM_TASK_ID FM_HOME="$d" bash "$ROOT/bin/fm-merge-local.sh" "$id" \
> "$d/merge-output" 2>&1 || rc=$?
expect_code 0 "$rc" "freshly accepted head could not land: $(cat "$d/merge-output")"
[ "$(git -C "$d/wt" rev-parse main)" = "$revised" ] || fail "landing did not reach B"
pass "local-only done freezes each accepted head until a fresh valid ready report"
}

test_done_time_ask_user_audit_uses_recorded_run() {
Expand Down
Loading
Loading