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
20 changes: 15 additions & 5 deletions bin/fm-fleet-ledger.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -21,6 +24,7 @@
#
# Usage:
# fm-fleet-ledger.sh dispatched <task> <kind> <project> <harness> <model>
# fm-fleet-ledger.sh relaunched <task>
# fm-fleet-ledger.sh pr_ready <task> <url>
# fm-fleet-ledger.sh merged <task> pr <url>
# fm-fleet-ledger.sh merged <task> local
Expand Down Expand Up @@ -65,7 +69,7 @@ LOCK="$STATE/.fleet-ledger.lock"
TEXT_MAX_CHARS=2000

usage() {
echo "usage: fm-fleet-ledger.sh dispatched <task> <kind> <project> <harness> <model> | pr_ready <task> <url> | merged <task> pr <url> | merged <task> local | cleaned_up <task> | capture | appended <config> <state>/<task>.status" >&2
echo "usage: fm-fleet-ledger.sh dispatched <task> <kind> <project> <harness> <model> | relaunched <task> | pr_ready <task> <url> | merged <task> pr <url> | merged <task> local | cleaned_up <task> | capture | appended <config> <state>/<task>.status" >&2
exit 2
}

Expand All @@ -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
Expand Down Expand Up @@ -152,15 +157,16 @@ append() {

# The status-line grammar belongs to bin/fm-classify-lib.sh; this only projects it.
append_status() { # <task> <status-line>
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() { # <task>; caller holds the lock
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion bin/fm-spawn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions docs/fleet-ledger.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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. |
Expand All @@ -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=<epoch>]` 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"}
Expand All @@ -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.
Expand All @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions tests/fm-control-relaunch.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion tests/fm-fleet-ledger.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF
["task.dispatched","$TASK","ship","sample","claude",null]
["task.status","$TASK","working",null," setup done"]
["task.status","$TASK","working",null," setup done",1790000000]
["task.status","$TASK","needs-decision","pick-one"," choose \"a\"\\\\b or c"]
["task.status","$TASK","resolved","pick-one"," [key=pick-one] chose a"]
["task.status","$TASK",null,null,"partial line without its newline finished"]
Expand All @@ -125,6 +125,8 @@ EOF
)" "$rows" "ledger rows"
assert_not_contains "$LEDGER_AFTER_POLL" "partial line" "the poll recorded a line before its newline arrived"
assert_contains "$LEDGER_AFTER_POLL" '"state":"needs-decision"' "the watcher poll did not record the status lines"
jq -e -s 'any(.[]; .event == "task.relaunched") | not' "$HOME_DIR/state/fleet-ledger.jsonl" >/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"
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading