diff --git a/bin/fm-fleet-ledger.sh b/bin/fm-fleet-ledger.sh index c7d73bb05d0..7d830613657 100755 --- a/bin/fm-fleet-ledger.sh +++ b/bin/fm-fleet-ledger.sh @@ -11,7 +11,10 @@ # Producers: # bin/fm-brief.sh appended (in every worker's status command, # right after its unchanged plain append) -# bin/fm-spawn.sh dispatched (fresh spawns only, never relaunch) +# bin/fm-spawn.sh dispatched (fresh spawns), relaunched (every +# --relaunch, including bin/fm-control.sh +# relaunch, once the replacement is launched and +# before the control plane confirms it alive) # bin/fm-watch.sh capture, once per poll cycle # bin/fm-pr-check.sh pr_ready (a PR registered for review, not the # merge-time re-record from bin/fm-pr-merge.sh) @@ -21,6 +24,7 @@ # # Usage: # fm-fleet-ledger.sh dispatched +# fm-fleet-ledger.sh relaunched # fm-fleet-ledger.sh pr_ready # fm-fleet-ledger.sh merged pr # fm-fleet-ledger.sh merged local @@ -65,7 +69,7 @@ LOCK="$STATE/.fleet-ledger.lock" TEXT_MAX_CHARS=2000 usage() { - echo "usage: fm-fleet-ledger.sh dispatched | pr_ready | merged pr | merged local | cleaned_up | capture | appended /.status" >&2 + echo "usage: fm-fleet-ledger.sh dispatched | relaunched | pr_ready | merged pr | merged local | cleaned_up | capture | appended /.status" >&2 exit 2 } @@ -76,6 +80,7 @@ task_ok() { cmd=${1:-} case "$cmd" in dispatched) { [ "$#" -eq 6 ] && task_ok "$2"; } || usage ;; + relaunched) { [ "$#" -eq 2 ] && task_ok "$2"; } || usage ;; pr_ready) { [ "$#" -eq 3 ] && task_ok "$2" && [ -n "$3" ]; } || usage ;; merged) task_ok "${2:-}" || usage @@ -152,15 +157,16 @@ append() { # The status-line grammar belongs to bin/fm-classify-lib.sh; this only projects it. append_status() { # - local task=$1 line=$2 verb key text + local task=$1 line=$2 verb key text at status_line_verb "$line" verb case "$verb" in [a-z]*) case "$verb" in *[!a-z-]*) verb='' ;; esac ;; *) verb='' ;; esac key=$(_fm_decision_key "$line" 2>/dev/null) || key='' [ "$key" != default ] || key='' + at=$(status_line_at_epoch "$line") || at='' text=${line#*:} append task.status "$task" \ - "{state: (\$state | n), key: (\$key | n), text: \$text[0:$TEXT_MAX_CHARS]}" \ - --arg state "$verb" --arg key "$key" --arg text "$text" + "{state: (\$state | n), key: (\$key | n), text: \$text[0:$TEXT_MAX_CHARS]} + (if \$at == \"\" then {} else {at: (\$at | tonumber)} end)" \ + --arg state "$verb" --arg key "$key" --arg text "$text" --arg at "$at" } capture_task() { # ; caller holds the lock @@ -212,6 +218,10 @@ case "$cmd" in '{kind: ($kind | n), project: ($project | n), harness: ($harness | n), model: ($model | n)}' \ --arg kind "$3" --arg project "$4" --arg harness "$5" --arg model "$6" || rc=1 ;; + relaunched) + capture_task "$2" || rc=1 + append task.relaunched "$2" '{}' || rc=1 + ;; pr_ready) capture_task "$2" || rc=1 append task.pr_ready "$2" '{pr: $pr}' --arg pr "$3" || rc=1 diff --git a/bin/fm-spawn.sh b/bin/fm-spawn.sh index d1fb8225fb3..4e1fc370cb1 100755 --- a/bin/fm-spawn.sh +++ b/bin/fm-spawn.sh @@ -8533,7 +8533,13 @@ SPAWN_ACCOUNT= [ -z "$WORKER_ACCOUNT_PROVIDER" ] || SPAWN_ACCOUNT="$SPAWN_ACCOUNT account_provider=$WORKER_ACCOUNT_PROVIDER" [ -z "$OMP_PROFILE_PIN" ] || SPAWN_ACCOUNT="$SPAWN_ACCOUNT omp_profile=$OMP_PROFILE_PIN" # Opt-in fleet activity ledger (docs/fleet-ledger.md); off costs one file test. -[ ! -e "$CONFIG/fleet-ledger" ] || [ "$RELAUNCH" -eq 1 ] || FM_HOME=$FM_HOME FM_STATE_OVERRIDE=$STATE FM_CONFIG_OVERRIDE=$CONFIG "$SCRIPT_DIR/fm-fleet-ledger.sh" dispatched "$ID" "$KIND" "${PROJ_ABS##*/}" "$HARNESS" "$MODEL" || true +if [ -e "$CONFIG/fleet-ledger" ]; then + if [ "$RELAUNCH" -eq 1 ]; then + FM_HOME=$FM_HOME FM_STATE_OVERRIDE=$STATE FM_CONFIG_OVERRIDE=$CONFIG "$SCRIPT_DIR/fm-fleet-ledger.sh" relaunched "$ID" || true + else + FM_HOME=$FM_HOME FM_STATE_OVERRIDE=$STATE FM_CONFIG_OVERRIDE=$CONFIG "$SCRIPT_DIR/fm-fleet-ledger.sh" dispatched "$ID" "$KIND" "${PROJ_ABS##*/}" "$HARNESS" "$MODEL" || true + fi +fi # Opt-in Herdr project groups (docs/herdr-backend.md "Project groups") tag the # new endpoint now rather than at the next watcher poll; off costs one file test. [ ! -e "$CONFIG/herdr-project-groups" ] || [ "$BACKEND" != herdr ] || FM_HOME=$FM_HOME FM_STATE_OVERRIDE=$STATE FM_CONFIG_OVERRIDE=$CONFIG "$SCRIPT_DIR/fm-herdr-groups.sh" sync "$ID" || true diff --git a/docs/fleet-ledger.md b/docs/fleet-ledger.md index e826f2b33b5..1def7969ea7 100644 --- a/docs/fleet-ledger.md +++ b/docs/fleet-ledger.md @@ -20,7 +20,7 @@ Every record carries these members: | ------- | --------------------------------------------------------- | | `v` | Record format version, currently `1` | | `ts` | Unix time in seconds when the record was written | -| `event` | One of the five event names below | +| `event` | One of the six event names below | | `task` | The firstmate task id the record is about | Readers must ignore members and events they do not recognize, so later versions can add them without breaking existing readers. @@ -29,8 +29,9 @@ Readers must ignore members and events they do not recognize, so later versions | Event | Extra members | Written when | | ------------------ | ---------------------------------------------- | ------------ | -| `task.dispatched` | `kind`, `project`, `harness`, `model` | A new worker or second mate is launched. A relaunch of an existing task is not recorded. | -| `task.status` | `state`, `key`, `text` | A complete, nonblank line in the task's status log is captured. | +| `task.dispatched` | `kind`, `project`, `harness`, `model` | A new worker or second mate is launched. | +| `task.relaunched` | none | A replacement worker for an existing task is launched, by `bin/fm-control.sh relaunch` or directly by `bin/fm-spawn.sh --relaunch`. It is written once the launch succeeds, before the control plane confirms the worker is running, so a relaunch whose replacement then fails to come up is still recorded. | +| `task.status` | `state`, `key`, `text`, optional `at` | A complete, nonblank line in the task's status log is captured. | | `task.pr_ready` | `pr` | Firstmate records the task's PR as ready for review. | | `task.merged` | `via` (`"pr"` or `"local"`), plus `pr` when `via` is `"pr"` | The task's PR merge is recorded, or its local-only branch landed. | | `task.cleaned_up` | none | The task's worker and local copy were removed. | @@ -43,12 +44,15 @@ It is written each time firstmate records a PR for the task, so registering a re `task.status` members: `state` is the status line's leading word, such as `working`, `needs-decision`, `blocked`, `paused`, `done`, `failed`, or `resolved`, or `null` when the line has none. `key` is the line's `[key=...]` decision key, or `null`. `text` is the status line after its first colon, verbatim, capped at 2000 characters; if the line has no colon, it is the whole line. +`at` is the status line's `[at=]` stamp as Unix seconds when the stamp is valid; the member is absent when the line has no valid stamp. +This is the worker's event time, while `ts` is when the ledger recorded it. Example: ```json {"v":1,"ts":1790132857,"event":"task.dispatched","task":"fix-login","kind":"ship","project":"webapp","harness":"claude","model":null} -{"v":1,"ts":1790132870,"event":"task.status","task":"fix-login","state":"working","key":null,"text":" bug reproduced"} +{"v":1,"ts":1790132870,"event":"task.status","task":"fix-login","state":"working","key":null,"text":" bug reproduced","at":1790132865} +{"v":1,"ts":1790133000,"event":"task.relaunched","task":"fix-login"} {"v":1,"ts":1790133400,"event":"task.status","task":"fix-login","state":"done","key":null,"text":" PR https://github.com/acme/webapp/pull/7 checks green"} {"v":1,"ts":1790133410,"event":"task.pr_ready","task":"fix-login","pr":"https://github.com/acme/webapp/pull/7"} {"v":1,"ts":1790133900,"event":"task.merged","task":"fix-login","via":"pr","pr":"https://github.com/acme/webapp/pull/7"} @@ -64,10 +68,10 @@ Example: - lines from workers whose instructions predate this, or that append without running the instruction's full command; - lines a remote second mate reports, which reach this home through firstmate's relay; - lines written while the immediate record fails, for example when the ledger file cannot be written. - Recording `task.pr_ready`, `task.merged`, or `task.cleaned_up` first records that task's pending status lines. + Recording `task.relaunched`, `task.pr_ready`, `task.merged`, or `task.cleaned_up` first records that task's pending status lines. - Captured status lines are delivered at least once unless a write fails or a crash loses unflushed records: an interrupted capture can repeat records, so a reader that must not double-count should tolerate duplicates. - A status record can appear just before its task's `task.dispatched` record when the worker writes a status line in the moment between its launch and that record. -- When a home turns the ledger on, status lines already in a live task's log are recorded on that task's next capture (which may be a worker status command, PR registration, merge, cleanup, or monitor poll); tasks dispatched or cleaned up while the flag was absent have no record of that. +- When a home turns the ledger on, status lines already in a live task's log are recorded on that task's next capture (which may be a worker status command, relaunch, PR registration, merge, cleanup, or monitor poll); tasks dispatched or cleaned up while the flag was absent have no record of that. - There is no sequence number and no gap detection. - Writes are plain appends with no forced flush to disk, so a machine crash can lose the newest records. - The file is never rotated and grows until truncated. @@ -79,7 +83,6 @@ Example: These are possible follow-ups, deliberately left out of this version: - session start, away-mode, and quiet-mode events; -- relaunch events; - whether a worker is currently working or idle, and when a turn ends; see the [runtime backend event sources](architecture.md#runtime-session-backends) for native activity observations; - sequence numbers and gap detection; - rotation and continuity across rotated files; diff --git a/tests/fm-control-relaunch.test.sh b/tests/fm-control-relaunch.test.sh index ca76ffdb8e2..9de699b6def 100755 --- a/tests/fm-control-relaunch.test.sh +++ b/tests/fm-control-relaunch.test.sh @@ -392,6 +392,35 @@ test_same_harness_relaunch_keeps_identity_and_reuses_the_endpoint() { pass "fm-control relaunch: a same-harness relaunch replaces the agent in the same endpoint and worktree" } +test_relaunch_records_one_ledger_event() { + local dir out rc + dir=$(new_case ledger-relaunch rl80) + add_ship_task "$dir" rl80 claude + mkdir -p "$dir/home/config" + : > "$dir/home/config/fleet-ledger" + out=$(run_control "$dir" rl80 relaunch --note "resume the fixture task"); rc=$? + expect_code 0 "$rc" "a ledger-enabled relaunch should succeed"$'\n'"$out" + assert_equals '["task.relaunched","rl80"]' \ + "$(jq -sc '[.[] | [.event, .task]] | .[]' "$dir/home/state/fleet-ledger.jsonl")" \ + "a completed relaunch writes exactly one event for its task" + pass "fm-control relaunch records exactly one task.relaunched event" +} + +test_direct_spawn_relaunch_records_one_ledger_event() { + local dir out rc + dir=$(new_case ledger-spawn-relaunch rl81) + add_ship_task "$dir" rl81 claude + mkdir -p "$dir/home/config" + : > "$dir/home/config/fleet-ledger" + printf 'zsh' > "$dir/fake/command" + out=$(run_spawn "$dir" rl81 --relaunch); rc=$? + expect_code 0 "$rc" "a ledger-enabled direct relaunch should succeed"$'\n'"$out" + assert_equals '["task.relaunched","rl81"]' \ + "$(jq -sc '[.[] | [.event, .task]] | .[]' "$dir/home/state/fleet-ledger.jsonl")" \ + "a direct fm-spawn relaunch writes exactly one event for its task" + pass "fm-spawn --relaunch records exactly one task.relaunched event" +} + test_relaunch_refuses_before_exit_when_the_composer_holds_pending_text() { local dir out rc dir=$(new_case pending-exit rl43) @@ -3017,6 +3046,8 @@ test_relaunch_off_opencode_removes_tracked_wiring test_exit_and_relaunch_remove_the_dialog_file test_exit_removes_the_dialog_file_before_releasing_the_lock test_same_harness_relaunch_keeps_identity_and_reuses_the_endpoint +test_relaunch_records_one_ledger_event +test_direct_spawn_relaunch_records_one_ledger_event test_relaunch_refuses_before_exit_when_the_composer_holds_pending_text test_relaunch_refuses_before_exit_when_the_composer_state_is_unproven test_relaunch_from_linked_home_preserves_recorded_worktree diff --git a/tests/fm-fleet-ledger.test.sh b/tests/fm-fleet-ledger.test.sh index 33fd37cb265..7eb4201ffbd 100755 --- a/tests/fm-fleet-ledger.test.sh +++ b/tests/fm-fleet-ledger.test.sh @@ -114,7 +114,7 @@ test_flag_on_records_the_task_lifecycle() { rows=$(ledger_rows '[.event, .task] + (del(.v, .ts, .event, .task) | to_entries | map(.value))') assert_equals "$(cat </dev/null \ + || fail "a fresh dispatch recorded a relaunch" assert_absent "$HOME_DIR/state/.$TASK.fleet-ledger-offset" "cleanup left the task's ledger offset behind" pass "flag on: dispatch, polled status lines, the local merge after its task's pending lines, and cleanup are recorded in order" } @@ -208,12 +210,31 @@ test_worker_status_line_is_recorded_when_written() { "$(cat "$HOME_DIR/state/$TASK.status")" "status log" assert_equals '["task.status","needs-decision"," pick a lamp colour"]' \ "$(ledger_rows '[.event, .state, .text]')" "ledger rows right after the append" + jq -e --arg task "$TASK" 'select(.event == "task.status" and .task == $task and .state == "needs-decision" and .key == null and .text == " pick a lamp colour" and .at == 1790000000)' \ + "$HOME_DIR/state/fleet-ledger.jsonl" >/dev/null || fail "the status record lost a member or its worker event time" out=$(in_home "$ROOT/bin/fm-fleet-ledger.sh" capture 2>&1) || fail "backstop capture failed: $out" assert_equals 1 "$(wc -l < "$HOME_DIR/state/fleet-ledger.jsonl" | tr -d ' ')" \ "ledger records after the backstop capture" pass "flag on: a worker's status command records its line at once, and the watcher backstop does not record it again" } +test_status_time_is_optional_and_rejects_malformed_stamps() { + local out + make_case on-status-time on + { + printf 'working: no stamp\n' + printf 'working [at=invalid]: malformed stamp\n' + printf 'working [at=1790000001] [key=choice]: valid stamp\n' + } > "$HOME_DIR/state/$TASK.status" + out=$(in_home "$ROOT/bin/fm-fleet-ledger.sh" capture 2>&1) || fail "status capture failed: $out" + assert_equals '[["working",null," no stamp",false],["working",null," malformed stamp",false],["working","choice"," valid stamp",true]]' \ + "$(jq -sc '[.[] | [.state, .key, .text, has("at")]]' "$HOME_DIR/state/fleet-ledger.jsonl")" \ + "status records keep their existing members and add time only for a valid stamp" + assert_equals 1790000001 "$(jq -r 'select(has("at")) | .at' "$HOME_DIR/state/fleet-ledger.jsonl")" \ + "worker event time" + pass "status records carry valid worker event times without changing the existing members" +} + test_worker_status_line_is_recorded_under_a_state_override() { local out state_dir make_case on-state-override on @@ -292,6 +313,7 @@ test_flag_on_records_the_task_lifecycle test_flag_on_records_a_pr_merge_once test_flag_on_records_a_pr_registration test_worker_status_line_is_recorded_when_written +test_status_time_is_optional_and_rejects_malformed_stamps test_worker_status_line_is_recorded_under_a_state_override test_worker_status_line_is_recorded_under_a_relative_config_override test_worker_status_command_fails_when_the_append_fails