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
171 changes: 142 additions & 29 deletions bin/fm-pending-reply-lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@
# ack_lines= count of correlated working:/paused: lines seen
# ack_line_epoch= when the latest of those lines was first seen
# escalated_epoch=
# escalation_wake_due_epoch=
# when a quiet missed or unreported escalation becomes
# a wake; empty once woken or when it woke at once
# escalation_closed_epoch=
# when the durable status decision opened by that
# escalation was closed again (see the escalation
Expand Down Expand Up @@ -108,6 +111,18 @@
# (bin/fm-backlog-handoff.sh's receiver wake) stayed refused forever once the
# watcher escalated between the lost transport and the next resume.
#
# Quiet escalation: a missed or unreported escalation for a delivered request is
# bookkeeping until proven otherwise - the mate is usually still working or has
# answered where main already saw it, and every wake costs a supervisor turn that
# re-reads the whole conversation. So the keyed blocked decision is published
# with the home's own self-announced append (no wake) and still lists in the next
# drain's OPEN DECISIONS. Only a request still unresolved after
# FM_PENDING_REPLY_WAKE_HOLD_SECS gets one wake: a plain reminder line under the
# same key (escalation_wake_due_epoch holds the due time; the reminder clears it).
# A reply landing in between resolves the record and closes the decision without
# any wake. Delivery-unknown and recovery-delivery escalations report a transport
# fault main must act on, so they wake at once as before.
#
# Sourced by bin/fm-send.sh, bin/fm-watch.sh, bin/fm-secondmate-report.sh, and
# tests. No side effects on source. set -u / set -e safe.
#
Expand All @@ -119,6 +134,11 @@
# FM_PENDING_REPLY_ACK_SECS default 1800; how long an acknowledged request
# may go without a new correlated line before
# the pending-reply-unreported escalation
# FM_PENDING_REPLY_WAKE_HOLD_SECS
# default 3600; how long a missed or unreported
# escalation stays a quiet status decision before
# it wakes the supervisor (see the quiet
# escalation note above); 0 wakes at once
# FM_PENDING_REPLY_DIR_OVERRIDE override the pending-replies directory (tests)
# FM_PENDING_REPLY_SEND_HOOK optional command template for recovery delivery
# (tests); receives task_id and full message as args
Expand Down Expand Up @@ -146,6 +166,7 @@ FM_PENDING_REPLY_SCHEMA='fm-pending-reply.v1'
FM_PENDING_REPLY_CORR_RE='corr=[A-Fa-f0-9]{16}'
FM_PENDING_REPLY_GRACE_DEFAULT=120
FM_PENDING_REPLY_ACK_DEFAULT=1800
FM_PENDING_REPLY_WAKE_HOLD_DEFAULT=3600

fm_pending_reply_now() {
if [ -n "${FM_PENDING_REPLY_NOW:-}" ]; then
Expand All @@ -171,6 +192,14 @@ fm_pending_reply_ack_secs() {
printf '%s' "$a"
}

fm_pending_reply_wake_hold_secs() {
local h=${FM_PENDING_REPLY_WAKE_HOLD_SECS:-$FM_PENDING_REPLY_WAKE_HOLD_DEFAULT}
case "$h" in
''|*[!0-9]*) h=$FM_PENDING_REPLY_WAKE_HOLD_DEFAULT ;;
esac
printf '%s' "$h"
}

# Directory holding durable pending-reply records for <state-dir>.
fm_pending_reply_dir() { # <state-dir>
local state=$1
Expand Down Expand Up @@ -378,6 +407,7 @@ recovery_delivery_outcome=
recovery_turn_seen_busy=0
recovery_turn_completed_epoch=
escalated_epoch=
escalation_wake_due_epoch=
resolved_epoch=
resolved_via=
wrong_home_hits=0
Expand Down Expand Up @@ -1234,6 +1264,26 @@ fm_pending_reply_escalation_line() { # <status-file> <record-path> <corr_id>
printf '%s' "$found"
}

# True while <escalation-line>'s keyed decision is still open with that exact
# note in bin/fm-classify-lib.sh's fold, so nothing reopens or closes a
# decision another writer has since closed or taken over.
_fm_pending_reply_escalation_open() { # <status-file> <escalation-line>
local key note open_line open_key open_note
key=$(_fm_decision_key "$2") || key=''
note=$(status_line_note "$2")
while IFS= read -r open_line; do
[ -n "$open_line" ] || continue
open_key=${open_line%%$'\t'*}
[ "$open_key" = "$key" ] || continue
open_note=${open_line#*$'\t'}
open_note=${open_note#*$'\t'}
[ "$open_note" = "$note" ] && return 0
done <<EOF
$(status_open_decisions "$1")
EOF
return 1
}

# Close the durable status decision a previous escalation opened for <corr_id>.
# Idempotent, and safe to retry until it succeeds: it appends the closing line
# only while that exact keyed decision is still open in
Expand Down Expand Up @@ -1261,8 +1311,8 @@ fm_pending_reply_close_escalation() { # <state-dir> <corr_id>
}

_fm_pending_reply_close_escalation_locked() { # <state-dir> <corr_id>
local state=$1 corr=$2 rec escalated closed parent_status escalation key note
local open_line open_key open_note now close_line close_rc _task _via
local state=$1 corr=$2 rec escalated closed parent_status escalation key
local now close_line close_rc _task _via
rec=$(fm_pending_reply_path "$state" "$corr")
[ -f "$rec" ] || return 1
[ "$(fm_pending_reply_get "$rec" phase)" = resolved ] || return 0
Expand All @@ -1273,32 +1323,21 @@ _fm_pending_reply_close_escalation_locked() { # <state-dir> <corr_id>
parent_status=$(fm_pending_reply_get "$rec" parent_status)
[ -n "$parent_status" ] || return 1
escalation=$(fm_pending_reply_escalation_line "$parent_status" "$rec" "$corr")
if [ -n "$escalation" ]; then
if [ -n "$escalation" ] && _fm_pending_reply_escalation_open "$parent_status" "$escalation"; then
key=$(_fm_decision_key "$escalation") || key=''
note=$(status_line_note "$escalation")
while IFS= read -r open_line; do
[ -n "$open_line" ] || continue
open_key=${open_line%%$'\t'*}
[ "$open_key" = "$key" ] || continue
open_note=${open_line#*$'\t'}
open_note=${open_note#*$'\t'}
[ "$open_note" = "$note" ] || continue
# This close is the home's own bookkeeping, written by the same resolve
# or tick that already consumed the reply, so it uses the guarded
# self-announced append (bin/fm-wake-lib.sh, sourced by this function's
# wrappers) and does not wake the home that wrote it; the escalation
# OPEN above stays a plain append because a new blocker must wake.
_task=$(fm_pending_reply_get "$rec" task_id)
_via=$(fm_pending_reply_get "$rec" resolved_via)
close_line="resolved [key=${key}]: $(fm_pending_reply_resolved_note "$_task" "$corr" "$_via")"
close_rc=0
fm_wake_status_append_self_announced "${parent_status%/*}" "$parent_status" "$close_line" \
2>/dev/null || close_rc=$?
[ "$close_rc" -ne 2 ] || return 1
break
done <<EOF
$(status_open_decisions "$parent_status")
EOF
# This close is the home's own bookkeeping, written by the same resolve
# or tick that already consumed the reply, so it uses the guarded
# self-announced append (bin/fm-wake-lib.sh, sourced by this function's
# wrappers) and does not wake the home that wrote it; only a quiet
# escalation opens the same way, and every other escalation open stays a
# plain append because a new blocker must wake.
_task=$(fm_pending_reply_get "$rec" task_id)
_via=$(fm_pending_reply_get "$rec" resolved_via)
close_line="resolved [key=${key}]: $(fm_pending_reply_resolved_note "$_task" "$corr" "$_via")"
close_rc=0
fm_wake_status_append_self_announced "${parent_status%/*}" "$parent_status" "$close_line" \
2>/dev/null || close_rc=$?
[ "$close_rc" -ne 2 ] || return 1
fi
now=$(fm_pending_reply_now)
fm_pending_reply_set "$rec" escalation_closed_epoch "$now"
Expand Down Expand Up @@ -1331,7 +1370,7 @@ fm_pending_reply_maybe_escalate() { # <state-dir> <corr_id>
_fm_pending_reply_maybe_escalate_locked() { # <state-dir> <corr_id>
local state=$1 corr=$2
local rec phase completed now payload parent_status line kind first display
local delivered task_id meta sm_home remote_host grace age
local delivered task_id meta sm_home remote_host grace age hold quiet append_rc
rec=$(fm_pending_reply_path "$state" "$corr")
[ -f "$rec" ] || return 1
phase=$(fm_pending_reply_get "$rec" phase)
Expand Down Expand Up @@ -1403,15 +1442,88 @@ _fm_pending_reply_maybe_escalate_locked() { # <state-dir> <corr_id>
[ -n "$parent_status" ] || return 1
mkdir -p "$(dirname "$parent_status")" 2>/dev/null || return 1
line="blocked [key=$(fm_pending_reply_escalation_key "$corr")]: $payload"
hold=$(fm_pending_reply_wake_hold_secs)
quiet=0
case "$kind" in
missed|unreported) [ "$hold" -gt 0 ] && [ -n "$delivered" ] && quiet=1 ;;
esac
if ! status_event_recorded "$parent_status" "$line"; then
printf '%s\n' "$(status_stamp_line "$line")" >> "$parent_status" 2>/dev/null || return 1
if [ "$quiet" = 1 ]; then
# Rc 1 (appended, marker left alone) still records the owned range that
# keeps the signal gate quiet, and where it cannot the watcher wakes
# as before; either way the reminder is armed, so a miss is never dropped.
append_rc=0
fm_wake_status_append_self_announced "${parent_status%/*}" "$parent_status" "$line" \
2>/dev/null || append_rc=$?
[ "$append_rc" -ne 2 ] || return 1
else
printf '%s\n' "$(status_stamp_line "$line")" >> "$parent_status" 2>/dev/null || return 1
fi
fi
now=$(fm_pending_reply_now)
if [ "$quiet" = 1 ]; then
fm_pending_reply_set "$rec" escalation_wake_due_epoch "$((now + hold))" || return 1
fi
fm_pending_reply_set "$rec" escalated_epoch "$now" || return 1
fm_pending_reply_set "$rec" phase escalated || return 1
return 0
}

# Wake once for a quiet escalation that is still unresolved after the hold.
# Appends a plain reminder under the escalation's own key, so the reserved-key
# fold replaces the open note and a later resolve closes it like any escalation.
# Retryable: the due epoch clears only after the reminder is on disk.
fm_pending_reply_wake_held_escalation() { # <state-dir> <corr_id>
# Serialized per correlation, like the escalation and its close; see
# fm_pending_reply_maybe_escalate for why the wake-lib globals are local.
local state=$1 corr=$2 lock rc=0
local STATE FM_WAKE_QUEUE FM_WAKE_QUEUE_LOCK
STATE=$state
lock="$state/.pending-reply-$corr.lock"
# Deliberately undirected, as in fm_pending_reply_maybe_escalate.
. "$_FM_PENDING_REPLY_LIB_DIR/fm-wake-lib.sh"
fm_lock_acquire_wait "$lock" || return 1
_fm_pending_reply_wake_held_escalation_locked "$@" || rc=$?
fm_lock_release "$lock"
return "$rc"
}

_fm_pending_reply_wake_held_escalation_locked() { # <state-dir> <corr_id>
local state=$1 corr=$2 rec due escalated now parent_status escalation note suffix
rec=$(fm_pending_reply_path "$state" "$corr")
[ -f "$rec" ] || return 1
[ "$(fm_pending_reply_get "$rec" phase)" = escalated ] || return 0
due=$(fm_pending_reply_get "$rec" escalation_wake_due_epoch)
escalated=$(fm_pending_reply_get "$rec" escalated_epoch)
case "$due" in ''|*[!0-9]*) return 0 ;; esac
case "$escalated" in ''|*[!0-9]*) return 0 ;; esac
now=$(fm_pending_reply_now)
[ "$now" -ge "$due" ] || return 0
parent_status=$(fm_pending_reply_get "$rec" parent_status)
[ -n "$parent_status" ] || return 1
# One fresh read first: a reply that landed during the hold ends it quietly.
if _fm_pending_reply_try_resolve_locked "$state" "$corr" "$parent_status"; then
return 0
fi
[ "$(fm_pending_reply_get "$rec" phase)" = escalated ] || return 1
# Only a decision still open as this library left it is reminded; one the
# supervisor already closed (fm-send --resolve-key) or that has left the
# status file needs no further turn.
escalation=$(fm_pending_reply_escalation_line "$parent_status" "$rec" "$corr")
if [ -n "$escalation" ] && _fm_pending_reply_escalation_open "$parent_status" "$escalation"; then
note=$(status_line_note "$escalation")
suffix=" - still unresolved after $(((due - escalated) / 60))m"
case "$note" in
*"$suffix") ;;
*)
printf '%s\n' "$(status_stamp_line "blocked [key=$(fm_pending_reply_escalation_key "$corr")]: $note$suffix")" \
>> "$parent_status" 2>/dev/null || return 1
;;
esac
fi
fm_pending_reply_set "$rec" escalation_wake_due_epoch ""
}

# Detect a correlated report written under the secondmate home (wrong home)
# without treating it as acknowledgement. A remote route's
# parent-replies.status is its parent channel, not a stranded self-home file.
Expand Down Expand Up @@ -1664,6 +1776,7 @@ fm_pending_reply_tick() { # <state-dir>
if fm_pending_reply_try_resolve "$state" "$corr"; then
continue
fi
fm_pending_reply_wake_held_escalation "$state" "$corr" 2>/dev/null || true
if [ -f "$meta" ]; then
sm_home=$(fm_meta_get "$meta" home)
if [ -n "$sm_home" ]; then
Expand Down
1 change: 1 addition & 0 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ A third bounded section, RECORD DIVERGENCE, prints on the same drains for the op
A failed read, output, or concurrent-replacement check prevents the snapshot cursor from advancing across uncertain bytes, and teardown retires a task's manifest row before that task ID can be reused.
The explicit resolution is written by the actor that answers, not the busy worker: `fm-send`'s `--resolve-key` appends the closing `resolved` line to this home's own copy of the ledger at answer time, which covers crewmates, local secondmates, and remote secondmates identically because a remote mate's escalations reach that local copy through the parent-replies ingest and only the answer message itself crosses the transport.
This home's answerer close, pending-reply escalation close, and captain-held transfer use the provenance-guarded append owned by `bin/fm-wake-lib.sh`, which records the exact byte range it appended so a later wake scan can tell this home's own growth from a foreign write instead of waking on it.
<!-- Fork-only: quiet pending-reply escalation --> A quiet pending-reply escalation open uses the same guarded append.
The watcher marker advances past those bytes only when every earlier byte was already classified by the watcher or listed as an open decision by the OPEN DECISIONS fold; any other earlier line, including a worker line the fold read but never listed, and any interleaved foreign write, fails toward an ordinary wake.
A turn-ended-only queue row omits its historical status annotation when that status file exactly matches the same seen marker.
Any direct or remaining historical annotation prints every status line unread at the presentation cursor instead of replaying only the latest line.
Expand Down
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2506,6 +2506,7 @@ FM_SEND_SLEEP=0.4 # seconds between fm-send typed-plane submit checks
FM_SEND_SETTLE=1 # seconds fm-send waits after a successful typed-plane submit; 0 disables
FM_PENDING_REPLY_GRACE_SECS=120 # seconds after the request turn completes without a correlated parent report before its one recovery repost is eligible, and after the recovery turn completes before the missed-report escalation is eligible; never counted from delivery
FM_PENDING_REPLY_ACK_SECS=1800 # seconds a request acknowledged only by a correlated working: or paused: line may go without a new correlated line before its pending-reply-unreported escalation (bin/fm-pending-reply-lib.sh)
FM_PENDING_REPLY_WAKE_HOLD_SECS=3600 # seconds a missed or unreported pending-reply escalation stays a quiet open decision (listed in the next drain, no wake) before it wakes once; 0 wakes at once (bin/fm-pending-reply-lib.sh)
# sub-supervisor (bin/fm-supervise-daemon.sh); presence-gated via /afk
FM_SUPERVISOR_BACKEND= # optional supervisor pane backend override; tmux/herdr only, otherwise detects $TMUX_PANE then HERDR_ENV/HERDR_PANE_ID before tmux fallback
FM_SUPERVISOR_TARGET= # optional supervisor pane target override; tmux target or herdr <session>:<pane-id>, otherwise auto-detected
Expand Down
Loading