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
33 changes: 31 additions & 2 deletions bin/fm-inactive-reconcile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,30 @@ report_child() { # <id>
report_child_ledger_locked "$id" "$meta"
}

reap_terminal_child_locked() { # <id> <meta>
local id=$1 meta=$2 backend target pids pid
[ -f "$meta" ] && [ ! -L "$meta" ] || return 0
backend=$(clean_field "$(meta_field "$meta" backend)")
[ -n "$backend" ] || backend=tmux
target=$(clean_field "$(meta_field "$meta" window)")
[ -n "$target" ] || return 0
if [ "$backend" = tmux ] && command -v tmux >/dev/null 2>&1; then
pids=$(tmux list-panes -t "$target" -F '#{pane_pid}' 2>/dev/null || true)
for pid in $pids; do
if [ -n "$pid" ] && [ "$pid" -gt 1 ] 2>/dev/null; then
kill -TERM -"$pid" 2>/dev/null || kill -TERM "$pid" 2>/dev/null || true
fi
done
fi
if [ -f "$SCRIPT_DIR/fm-backend.sh" ]; then
# shellcheck source=bin/fm-backend.sh
. "$SCRIPT_DIR/fm-backend.sh"
fm_backend_kill "$backend" "$target" 2>/dev/null || true
elif [ "$backend" = tmux ] && command -v tmux >/dev/null 2>&1; then
tmux kill-window -t "$target" 2>/dev/null || true
fi
}

reconcile_direct_child_locked() { # <id> <meta> <secondmate-id-or-empty> <timeout>
local id=$1 meta=$2 self=${3:-} timeout=$4 status turn last age state_line state pr incarnation fingerprint outcome_key payload kind state_rc=0
[ -f "$meta" ] && [ ! -L "$meta" ] || return 0
Expand Down Expand Up @@ -504,7 +528,7 @@ reconcile_direct_child_locked() { # <id> <meta> <secondmate-id-or-empty> <timeou
'state: failed '*) state='failed' ;;
*) return 0 ;;
esac
pr=$(pr_for_task "$meta")
pr=$(pr_for_task "$meta" "$status")
incarnation=$(meta_incarnation "$meta")
fingerprint=$(sha256_text "$incarnation|$id|$state|$pr|$(clean_field "$last")")
if [ -n "$self" ]; then
Expand All @@ -513,20 +537,25 @@ reconcile_direct_child_locked() { # <id> <meta> <secondmate-id-or-empty> <timeou
outcome_key="inactive-outcome-main-$id-$state"
fi
ensure_record "$fingerprint" "$id" "$incarnation" "$state" "$outcome_key" direct "upstream" "$pr" "$(sha256_text "$last")" || return 1
[ -n "$RECORD_PENDING" ] || return 0
if [ -z "$RECORD_PENDING" ]; then
reap_terminal_child_locked "$id" "$meta" || true
return 0
fi
if [ -n "$self" ]; then
if report_to_parent "$id" "$state" "$outcome_key" "$fingerprint" "$pr"; then
mark_reported "$RECORD_PENDING" || return 1
else
notice_parent_report_failed "$RECORD_PENDING" "$fingerprint" \
"inactive terminal outcome needs parent report: child=$id state=$state"
fi
reap_terminal_child_locked "$id" "$meta" || true
return 0
fi
record_phase_set "$RECORD_PENDING" presentation || return 1
payload="inactive terminal outcome awaiting captain presentation: child=$id state=$state"
[ -z "$pr" ] || payload="$payload pr=$pr"
queue_presentation "$RECORD_PENDING" "$fingerprint" "$payload" || true
reap_terminal_child_locked "$id" "$meta" || true
}

reconcile_direct_child() { # <id> <meta> <secondmate-id-or-empty> <timeout>
Expand Down
12 changes: 11 additions & 1 deletion tests/fm-inactive-reconcile.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ SH
case "${1:-}" in
display-message) printf '%%1\n' ;;
capture-pane) printf 'idle\n> \n' ;;
kill-window) [ -z "${FM_TMUX_KILL_LOG:-}" ] || printf '%s\n' "$*" >> "$FM_TMUX_KILL_LOG" ;;
esac
SH
local tool
Expand Down Expand Up @@ -101,7 +102,7 @@ run_reconcile() { # <home> [--startup]
PATH="$WORLD/fakebin:$PATH" FM_ROOT_OVERRIDE="$WORLD/root" FM_HOME="$home" \
FM_STATE_OVERRIDE="$home/state" FM_DATA_OVERRIDE="$home/data" FM_CONFIG_OVERRIDE="$home/config" \
FM_INACTIVE_RECONCILE_SECS=60 FM_INACTIVE_CREW_STATE_BIN="$WORLD/fakebin/fm-crew-state.sh" \
FM_FORGE_LOG="$WORLD/forge.log" "$RECON" scan ${option:+"$option"}
FM_FORGE_LOG="$WORLD/forge.log" FM_TMUX_KILL_LOG="${FM_TMUX_KILL_LOG:-}" "$RECON" scan ${option:+"$option"}
}

# The teardown-side entry point: deliver one child's terminal ledger line for a
Expand Down Expand Up @@ -894,6 +895,14 @@ SH
pass "reconciliation state reads set no-forge mode"
}

test_inactive_terminal_child_endpoint_is_reaped() {
local kill_log="$WORLD/tmux_kill.log"
make_world reaper; write_child "$MAIN" dead-child 'done: finished'
FM_TMUX_KILL_LOG="$kill_log" FM_FAKE_CREW_STATE='done' run_reconcile "$MAIN" --startup
grep -q 'firstmate:=fm-dead-child' "$kill_log" 2>/dev/null || fail "terminal child endpoint was not reaped"
pass "inactive terminal child endpoint is reaped"
}

test_main_direct_terminal_presentation_receipt
test_local_secondmate_delivers_terminal_ledger_line
test_secondmate_multiline_terminal_outcome_is_delivered_once
Expand Down Expand Up @@ -926,5 +935,6 @@ test_notice_recovery_does_not_duplicate_wake
test_missing_parent_binding_names_itself
test_reconciliation_never_calls_forge
test_reconciliation_sets_no_forge_mode_for_state_read
test_inactive_terminal_child_endpoint_is_reaped

echo "all inactive reconciliation tests passed"
Loading