From e610e71e151f9434cb20589ad7de467db4c9b102 Mon Sep 17 00:00:00 2001 From: irene Date: Mon, 14 Sep 2026 22:38:46 +0900 Subject: [PATCH] fix(bin): reap inactive terminal crew endpoints and processes --- bin/fm-inactive-reconcile.sh | 33 +++++++++++++++++++++++++++-- tests/fm-inactive-reconcile.test.sh | 12 ++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/bin/fm-inactive-reconcile.sh b/bin/fm-inactive-reconcile.sh index 5cbaf9e63d2..553ac262397 100755 --- a/bin/fm-inactive-reconcile.sh +++ b/bin/fm-inactive-reconcile.sh @@ -475,6 +475,30 @@ report_child() { # report_child_ledger_locked "$id" "$meta" } +reap_terminal_child_locked() { # + 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() { # 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 @@ -504,7 +528,7 @@ reconcile_direct_child_locked() { # diff --git a/tests/fm-inactive-reconcile.test.sh b/tests/fm-inactive-reconcile.test.sh index 0c57b55691e..7a86902f496 100755 --- a/tests/fm-inactive-reconcile.test.sh +++ b/tests/fm-inactive-reconcile.test.sh @@ -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 @@ -101,7 +102,7 @@ run_reconcile() { # [--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 @@ -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 @@ -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"