Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 35 additions & 2 deletions bin/fm-captain-hold.sh
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@
# tasks-axi --archive-body). It closes a question with `tasks-axi done` - or,
# with `--release`, lifts the hold with `tasks-axi unhold` so a captain-gated
# WORK item resumes without closing - and restores resolution-first body
# ordering. An exact retry also completes unfinished ordering normalization and
# ordering. `--release` is attended-only: while the away-posture record exists
# (state/.afk-contract, or the daemon flag state/.afk, read as away; a quiet
# record is a present captain) it is refused before anything is written, so an
# unattended supervisor cannot free held work with its own words and then steer
# it; the captain releases the hold in an attended session. A closing answer
# without `--release` is not gated by posture. An exact retry also completes
# unfinished ordering normalization and
# is idempotent only when its requested close mode
# matches the newest record; a changed decision or a mode mismatch is rejected.
# A re-held task may record a new answer on top. On a task already closed outside this script,
Expand All @@ -83,7 +89,9 @@
# identically no matter which channel the answer arrived on. The key IS the
# task id - no identity arithmetic. The optional fourth field selects the close:
# empty or `done` completes the task, `release` lifts the hold so held work
# resumes; anything else is skipped. A key that names no task, a task that is
# resumes; anything else is skipped. A `release` line while the away-posture
# record exists is reported as `refused:` under the same attended-only rule as
# `answer --release` and feeds nothing. A key that names no task, a task that is
# not held for the captain, or a task already closed is reported as `skipped:`
# and feeds nothing. A replayed delivery whose answer digest and requested
# close mode both match the newest record is reported `closed:` and is a no-op;
Expand Down Expand Up @@ -240,6 +248,9 @@ DATA="${FM_DATA_OVERRIDE:-$FM_HOME/data}"
# shellcheck source=bin/fm-parent-channel-lib.sh
# shellcheck disable=SC1091
. "$SCRIPT_DIR/fm-parent-channel-lib.sh"
# shellcheck source=bin/fm-afk-contract.sh
# shellcheck disable=SC1091
. "$SCRIPT_DIR/fm-afk-contract.sh"

PARENT_HOLD_PUBLISHED=0
publish_parent_hold() { # <task-id> <occurrence> <verb> <note>
Expand Down Expand Up @@ -1244,6 +1255,20 @@ apply_pending_retained_artifact() { # <task-id>
esac
}

# A captain hold is released only in an attended session. The away daemon flag
# (state/.afk, bin/fm-wake-lib.sh fm_afk_mode) and the posture record
# (state/.afk-contract, bin/fm-afk-contract.sh mode) are both read: either one
# reading away refuses, a quiet record is a present captain, and no record is
# attended. The check runs before any write, so a refusal leaves the hold and
# the body untouched.
RELEASE_AWAY_REASON='the away-posture record exists, and a captain hold is released only by the captain in an attended session; retry --release after the return archives the record'
release_attended() {
if [ -e "$STATE/.afk" ] && [ "$(fm_afk_mode "$STATE")" = away ]; then
return 1
fi
! fm_afk_contract_away_present "$STATE"
}

close_answered() { # <task-id> <release-0-or-1>
if [ "$2" = 1 ]; then
tasks_axi unhold "$1" >/dev/null
Expand Down Expand Up @@ -1288,6 +1313,9 @@ command_answer() {
shift
done
validate_slug task-id "$id"
if [ "$release" = 1 ] && ! release_attended; then
fail "task $id stays held: $RELEASE_AWAY_REASON"
fi
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
load_decision "$decision_file"
acquire_task_control_lock "$id"
require_tasks_axi
Expand Down Expand Up @@ -1539,6 +1567,11 @@ command_answers() {
continue
;;
esac
if [ "$release_flag" = --release ] && ! release_attended; then
printf 'refused: %s (%s)\n' "$key" "$RELEASE_AWAY_REASON"
skipped=$((skipped + 1))
continue
fi
resolve_rc=0
id=$(resolve_entry "$origin" "$key" 2>"$err") || resolve_rc=$?
id=${id%% *}
Expand Down
3 changes: 3 additions & 0 deletions docs/captain-hold-lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ The `answer` subcommand records the captain's exact words and resolves the call
| `answer --release` | Frees a captain-gated work item to proceed without completing it. |

It requires a non-empty captain decision file of at most 8192 bytes.
`answer --release` is attended-only: while the away-posture record exists it is refused before anything is written, and the keyed-answer intake refuses a `release` line the same way, because only the captain in an attended session releases a captain hold.
A quiet record is a present captain and does not refuse.
It then works in this order:

1. It durably writes a resolution block carrying the decision digest and a `Resolution mode:`.
Expand Down Expand Up @@ -550,6 +552,7 @@ The suite does not test the accepted merge-to-cleanup re-hold window or asynchro
- Answer-time resolution works through a bound channel with task-id keys.
This includes the `release` mode, mode-matched replay idempotence, and the refusal of drifted, mode-mismatched, absent, unheld, and already-closed keys.
- The chat channel reaches the same intake.
- `test_release_is_attended_only` exercises the attended-only release: the away posture record and the away daemon flag each refuse `answer --release` and a `release` intake line without writing, while no record and a quiet record release, and a closing answer is not gated.
- Hold-set stamping precedes visible hold state, preserves an active lifecycle's timestamp, and resets after release.
- Interrupted answer closure retains the stamp until close and restores resolution-first ordering on retry.
- Deferral through `--until` leaves `captain_actionable` false until due.
Expand Down
72 changes: 72 additions & 0 deletions tests/fm-captain-hold-lifecycle.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,77 @@ EOF
pass "release frees held work with the captain's words recorded and the body preserved"
}

# A captain hold is released only in an attended session: the away posture
# record and the away daemon flag each refuse --release, through `answer` and
# the keyed-answer intake alike, before anything is written. No record and a
# quiet record both release, and a closing answer is not gated by posture.
test_release_is_attended_only() {
local home show out rc
home=$(make_home release-attended)
tasks_in "$home" add sample-widget "Ship the sample widget" --kind ship --repo sample >/dev/null \
|| fail "could not create the held work item"
run_captain "$home" hold sample-widget --reason "captain go needed before shipping" >/dev/null \
|| fail "could not hold the work item for the captain"
printf 'Not urgent; ship it as planned.\n' > "$home/go.txt"
FM_HOME="$home" FM_STATE_OVERRIDE="$home/state" "$ROOT/bin/fm-afk-contract.sh" enter \
--words 'merge green work' >/dev/null 2>&1 || fail "away entry failed"

rc=0
run_captain "$home" answer sample-widget --decision-file "$home/go.txt" --release \
>/dev/null 2>"$home/away.err" || rc=$?
[ "$rc" -ne 0 ] || fail "answer --release succeeded while the away record exists"
assert_grep "attended session" "$home/away.err" \
"the away refusal did not name the attended-session requirement"
show=$(tasks_in "$home" show sample-widget --full)
assert_contains "$show" "held: yes" "the away refusal lifted the hold"
assert_not_contains "$show" "Resolution mode" "the away refusal wrote a resolution record"

out=$(printf 'sample-widget\tgo\t\trelease\n' \
| run_captain "$home" answers --source "away release fixture") \
&& fail "the keyed-answer intake released held work while away"
assert_contains "$out" "refused: sample-widget" "the intake did not report the away refusal"
show=$(tasks_in "$home" show sample-widget --full)
assert_contains "$show" "held: yes" "the intake lifted the hold while away"

run_captain "$home" hold sample-question --title "Pick the widget colour" \
--reason "captain must pick the colour" --repo sample >/dev/null \
|| fail "could not hold the question for the captain"
printf 'Blue.\n' > "$home/blue.txt"
run_captain "$home" answer sample-question --decision-file "$home/blue.txt" >/dev/null \
|| fail "a closing answer was refused while away"
show=$(tasks_in "$home" show sample-question --full)
assert_contains "$show" "state: done" "a closing answer while away did not close the question"

rm -f "$home/state/.afk-contract"
printf 'away\n%s\n' "$(date +%s)" > "$home/state/.afk"
rc=0
run_captain "$home" answer sample-widget --decision-file "$home/go.txt" --release \
>/dev/null 2>&1 || rc=$?
[ "$rc" -ne 0 ] || fail "answer --release succeeded while the away daemon flag exists"
show=$(tasks_in "$home" show sample-widget --full)
assert_contains "$show" "held: yes" "the daemon-flag refusal lifted the hold"

rm -f "$home/state/.afk"
run_captain "$home" answer sample-widget --decision-file "$home/go.txt" --release >/dev/null \
|| fail "answer --release failed with no away record"
show=$(tasks_in "$home" show sample-widget --full)
assert_contains "$show" "held: no" "an attended release kept the hold"
assert_contains "$show" "Resolution mode: released" "an attended release recorded no release"

run_captain "$home" hold sample-widget --reason "second captain go needed" >/dev/null \
|| fail "could not re-hold the released work item"
printf 'quiet\n%s\n' "$(date +%s)" > "$home/state/.afk"
FM_AFK_MODE=quiet FM_HOME="$home" FM_STATE_OVERRIDE="$home/state" \
"$ROOT/bin/fm-afk-contract.sh" enter --words 'keep routine wakes off my main' >/dev/null 2>&1 \
|| fail "quiet entry failed"
printf 'Still ship it.\n' > "$home/go-quiet.txt"
run_captain "$home" answer sample-widget --decision-file "$home/go-quiet.txt" --release >/dev/null \
|| fail "answer --release failed in quiet mode"
show=$(tasks_in "$home" show sample-widget --full)
assert_contains "$show" "held: no" "a quiet-mode release kept the hold"
pass "a captain hold is released only in an attended session: away refuses, quiet and no record release"
}

# The hold-set stamp must be durable before the captain hold becomes visible.
# A wrapper observes the real tasks-axi hold boundary, and a forced stamp-write
# failure proves the command never publishes the hold without its timestamp.
Expand Down Expand Up @@ -5000,6 +5071,7 @@ test_retained_body_keeps_its_utf8_bytes
test_completion_gate_attests_and_transfers
test_answer_records_and_closes
test_release_frees_held_work
test_release_is_attended_only
test_hold_stamp_precedes_hold_visibility
test_interrupted_answer_preserves_hold_age
test_deferral_leaves_captains_call_until_due
Expand Down
Loading