Skip to content
This repository was archived by the owner on Nov 26, 2026. It is now read-only.

Commit 4186ff3

Browse files
committed
feat: route OpenCode permission asks to a captain decision
An OpenCode 2 permission ask renders as a modal inside the worker's own TUI. The captain never sees it and firstmate has no record of it, so a worker asking for a path outside its worktree simply blocks with no visible decision anywhere. bin/fm-opencode-permission.sh bridges one real request to the captain through the existing Discord decision path and applies the keyed answer back to exactly that request. The generated OpenCode plugin only forwards permission.asked and decides nothing: a plugin that approved anything would be the invisible grant this change exists to remove. decide refuses unless the record names that exact task and request, is still pending, carries the task's current busy generation, and the server still reports it pending with the same id, session, and action. The read-check-consume sequence is one critical section and the record becomes consumed before the POST, so a replay or a concurrent second caller refuses rather than granting twice. A captured Discord reply now also enqueues its own durable wake. The stdout contract only reaches a session when the watcher ran the poll, so an operator-run or LaunchAgent-run ingress poll captured a reply whose marker then made every later watcher poll suppress it. Capture is now the wake; the stdout contract is unchanged for the watcher's own dispatch. answer-one gains an optional occurrence guard, and the reply path passes it. The decision key already carries the occurrence the captain was actually asked at, so a reply arriving after a call was answered and re-held would otherwise resolve the newer question they were never asked. Every decision answer is then confirmed to the captain, after the keyed command succeeds. An answer with no receipt is indistinguishable from a lost reply.
1 parent 7a7f754 commit 4186ff3

14 files changed

Lines changed: 1274 additions & 5 deletions

‎.agents/skills/fmx-respond/SKILL.md‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,24 @@ Images are only for actual visual artifacts - a generated illustration, a screen
148148

149149
Before classifying an inbox item as a new mention, check whether `source` is `discord-selfhosted-decision`.
150150
Such an item is an answer to a specific pushed decision: use its `.decision` object and `.text`, and do not reply publicly or treat it as fresh work.
151-
For `trigger=captain-hold`, call `bin/fm-captain-hold.sh answer-one "$key" "$answer" "Discord reply" --source discord-selfhosted`; it folds tabs and line breaks in the answer into spaces before keyed resolution.
151+
For `trigger=captain-hold`, call `bin/fm-captain-hold.sh answer-one "$task_id" "$answer" "Discord reply" --source discord-selfhosted --expect-occurrence "$occurrence"`; it folds tabs and line breaks in the answer into spaces before keyed resolution.
152+
`$task_id` is `.decision.task_id` and `$occurrence` is the trailing integer of `.decision.key`, which has the shape `captain-hold-<task-id>-<occurrence>`; both come from the decision object, and never be derived from the reply text.
153+
`--expect-occurrence` is not optional for a Discord reply: without it a reply that arrives after the call was answered and re-held resolves the *newer* question the captain was never asked, which is a silent wrong answer rather than a visible failure.
154+
A refusal naming the occurrence is a real outcome, not an error to retry: the captain's reply answered a question that has since moved on, so leave the inbox record in place, report the refusal in the plain outcome channel, and do not resolve anything.
152155
For `trigger=ask-user` or `trigger=pr-ready`, invoke `bin/fm-send.sh <status_task_id> --resolve-key <key> "$answer"` with the captured answer as one quoted argument.
156+
For `trigger=perm-ask`, invoke `bin/fm-opencode-permission.sh decide "$task_id" "$request_id" <once|always|reject>`, where `$task_id` is `.decision.task_id` and `$request_id` is `.decision.key` with its `perm-` prefix removed.
157+
Map the captain's words to exactly one of the three options the message offered, and pass only that word: `Approve once` becomes `once`, `Approve once and remember this` becomes `always`, and `Reject the request` becomes `reject`.
158+
When the reply does not clearly select one of the three offered options, do not guess and do not apply anything: report the three options in the plain outcome channel and leave the request pending.
159+
That script is the only thing that can grant, and it refuses on its own when the request expired, was already answered, or belongs to a task generation that has moved on.
153160
These are the existing keyed-answer paths; never edit status files directly.
154161
Remove the inbox record only after the matching command confirms success.
155162
On failure leave it for retry and report the exact error.
156163

164+
Every decision answer is applied and then confirmed, in that order.
165+
Confirmation is what tells the captain their reply landed, and it is the only receipt they get: `bin/fm-discord-notify.sh --report <channel_id> "<outcome>"`, using `.channel_id` from the decision object and stating plainly which decision was applied and what it enabled.
166+
Post it after the keyed command succeeds, never before, and never instead of applying.
167+
Without that receipt the captain answered a decision and got no evidence it was consumed, which is indistinguishable from the reply being lost.
168+
157169
This is a drain over the inbox, not a single reply.
158170
The watcher coalesces same-key `check:` wakes, so one `x-mention` wake can stand in for several pending mentions.
159171
Treat `state/x-inbox/` as the source of truth and process **every** file you find there, not just the `request_id` named in the wake.

‎bin/fm-captain-hold.sh‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
# fm-captain-hold.sh hold <task-id> --reason <reason> \
2424
# [--title <title>] [--repo <repo>] [--origin <origin-id>] [--until YYYY-MM-DD]
2525
# fm-captain-hold.sh answer <task-id> --decision-file <path> [--release]
26+
# fm-captain-hold.sh answer-one <task-id> <answer> <label> \
27+
# --source <provenance> [--expect-occurrence <n>]
2628
# fm-captain-hold.sh answers [<legacy-origin> | --any-origin] --source <provenance> (keyed answers on stdin)
2729
# fm-captain-hold.sh reconcile-requests --source-id <source-id> --source <provenance> (task ids on stdin)
2830
# fm-captain-hold.sh bind <source-id> [<legacy-origin> | --any-origin]
@@ -115,6 +117,14 @@
115117
# and pipe them here. It must never map keys to tasks, build decision records,
116118
# choose a close mode beyond what its card declared, or close anything itself.
117119
#
120+
# `answer-one` is the single-key form of that same intake, for a channel that
121+
# captured exactly one decision. `--expect-occurrence <n>` is its occurrence
122+
# guard: pass the occurrence the decision was actually asked at, and a task
123+
# that is no longer at that occurrence - already answered, or answered and
124+
# re-held, or no longer captain-held - is refused before any resolution is
125+
# written. Without the flag the guard does not run, so an existing caller that
126+
# has no occurrence to assert keeps its current behavior.
127+
#
118128
# `bind`, `unbind`, and `binding` record that a captured-answer SOURCE feeds
119129
# this intake, for any channel whose answers arrive detached from their origin
120130
# (a process-event source id, for example). The binding is a private record
@@ -1933,11 +1943,12 @@ command_open() { # <task-id> [--identity] [--distinguish-absent]
19331943
}
19341944

19351945
command_answer_one() {
1936-
local key=${1:-} answer=${2:-} label=${3:-} source=''
1946+
local key=${1:-} answer=${2:-} label=${3:-} source='' occurrence=''
19371947
[ "$#" -ge 3 ] || { usage >&2; exit 2; }
19381948
shift 3
19391949
while [ "$#" -gt 0 ]; do
19401950
case "$1" in
1951+
--expect-occurrence) shift; occurrence=${1:-} ;; --expect-occurrence=*) occurrence=${1#*=} ;;
19411952
--source) shift; source=${1:-} ;;
19421953
*) usage >&2; exit 2 ;;
19431954
esac
@@ -1946,12 +1957,40 @@ command_answer_one() {
19461957
[ -n "$source" ] || fail "--source provenance is required so the durable decision records where the answer came from"
19471958
validate_slug task-id "$key"
19481959
[ "${#key}" -le 128 ] || fail "task-id must be at most 128 characters"
1960+
if [ -n "$occurrence" ]; then
1961+
require_current_occurrence "$key" "$occurrence"
1962+
fi
19491963
answer=$(sanitize_field "$answer")
19501964
label=$(sanitize_field "$label")
19511965
printf '%s\t%s\t%s\n' "$key" "$answer" "$label" \
19521966
| command_answers --any-origin --source "$source"
19531967
}
19541968

1969+
# Refuse a keyed answer that names a superseded occurrence of a still-open
1970+
# captain call. The Discord decision key carries the occurrence
1971+
# `captain-hold-<task-id>-<occurrence>` that was actually put in front of the
1972+
# captain, so a reply that arrives after the call was answered and re-held
1973+
# targets an occurrence the captain was never asked about. Resolution is
1974+
# refused rather than forwarded, so a stale reply cannot silently answer a
1975+
# newer question. A closed task, an unreadable body, or an absent task fails
1976+
# closed here exactly as it does in the resolution path itself.
1977+
require_current_occurrence() { # <task-id> <expected-occurrence>
1978+
local key=$1 expected=$2 show body hold_kind count
1979+
case "$expected" in ''|*[!0-9]*|0) fail "--expect-occurrence must be a positive integer" ;; esac
1980+
task_show "$key" || fail "no captain-held task $key in this home's configured backlog (data directory $DATA)"
1981+
show=$TASK_SHOW_OUTPUT
1982+
body=$(show_field_value "$show" body)
1983+
hold_kind=$(show_field_value "$show" hold_kind)
1984+
count=$(resolution_record_count "$body") || count=''
1985+
case "$count" in ''|*[!0-9]*) count=0 ;; esac
1986+
if [ "$hold_kind" != captain ]; then
1987+
fail "refusing the answer: $key is not currently held for the captain (hold_kind=$hold_kind), so its occurrence $((count + 1)) cannot be confirmed"
1988+
fi
1989+
if [ "$((count + 1))" != "$expected" ]; then
1990+
fail "refusing the answer: $key is at occurrence $((count + 1)), not the occurrence $expected this decision was asked at"
1991+
fi
1992+
}
1993+
19551994
case "${1:-}" in
19561995
hold) shift; command_hold "$@" ;;
19571996
answer) shift; command_answer "$@" ;;

‎bin/fm-discord-notify.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ function localize(value) {
5959
"Leave it open": "열린 상태로 두기",
6060
"Continue with the request": "요청대로 계속 진행",
6161
"Leave it on hold": "보류 상태로 두기",
62+
"Approve once": "이 1회만 허용",
63+
"Approve once and remember this": "허용하고 이 경로를 기억",
64+
"Reject the request": "요청 거부",
6265
})[value] || value;
6366
}
6467

‎bin/fm-discord-notify.sh‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env bash
22
# Send one firstmate-initiated decision to the configured self-hosted Discord channel.
3-
# Usage: fm-discord-notify.sh <captain-hold|ask-user|pr-ready> <task-id> <key> <summary> <option|option...> [status-task-id]
3+
# `perm-ask` carries a real OpenCode permission request; its key is `perm-<request-id>`.
4+
# Usage: fm-discord-notify.sh <captain-hold|ask-user|pr-ready|perm-ask> <task-id> <key> <summary> <option|option...> [status-task-id]
45
set -u
56

67
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@@ -19,7 +20,7 @@ elif [ "${1:-}" = --retry-pending ]; then
1920
[ "$#" -eq 1 ] || { echo "usage: fm-discord-notify.sh --retry-pending" >&2; exit 2; }
2021
retry_pending=1
2122
elif [ "$#" -lt 5 ] || [ "$#" -gt 6 ]; then
22-
echo "usage: fm-discord-notify.sh <captain-hold|ask-user|pr-ready> <task-id> <key> <summary> <option|option...> [status-task-id]" >&2
23+
echo "usage: fm-discord-notify.sh <captain-hold|ask-user|pr-ready|perm-ask> <task-id> <key> <summary> <option|option...> [status-task-id]" >&2
2324
exit 2
2425
fi
2526
if [ "$retry_pending" -eq 0 ] && [ "$report_mode" -eq 0 ]; then
@@ -56,6 +57,7 @@ case "$trigger:$decision_key" in
5657
captain-hold:captain-hold-*) ;;
5758
ask-user:nm-*) ;;
5859
pr-ready:pr-ready-*) ;;
60+
perm-ask:perm-*) ;;
5961
*) echo "fm-discord-notify: trigger does not match its decision key" >&2; exit 2 ;;
6062
esac
6163
case "$summary" in *$'\n'*|*$'\r'*) echo "fm-discord-notify: summary must be one line" >&2; exit 2 ;; esac

‎bin/fm-discord-poll.sh‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
# state/discord-watch.check.sh matches the expected byte-static identity shim.
88
#
99
# Its contract: output "x-mention <request_id>" => wake firstmate, silence => keep sleeping.
10+
#
11+
# A captured item is also enqueued into the durable wake queue here, so capture
12+
# itself is the wake. The stdout contract only reaches a session when the WATCHER
13+
# ran this script: an operator-run or LaunchAgent-run ingress poll reaches nobody
14+
# that way, because its stdout goes to a log file while the offered marker
15+
# written during capture then makes every later watcher poll suppress the item.
16+
# That combination captured a captain reply and never woke anyone for it. The
17+
# enqueue is additive, so the watcher's own validated check dispatch still sees
18+
# and still forwards the same line.
1019

1120
set -u
1221

@@ -30,4 +39,38 @@ export FM_DISCORD_EXCLUDES="${FM_DISCORD_EXCLUDES-}"
3039
export FM_DISCORD_ALLOW_DMS="${FM_DISCORD_DMS:-true}"
3140
export FM_DISCORD_AUTHORIZED_USER_IDS="${FM_DISCORD_AUTHORIZED_USERS:-}"
3241

33-
exec node "$SCRIPT_DIR/fm-discord-poll.js"
42+
# Only a token-gated run can capture anything, so the wake library is loaded
43+
# only here: without a token this stays a hard no-op that touches no state.
44+
if [ ! -r "$FM_ROOT/bin/fm-wake-lib.sh" ]; then
45+
printf 'fm-discord-poll: cannot enqueue a wake (missing %s)\n' "$FM_ROOT/bin/fm-wake-lib.sh" >&2
46+
exit 1
47+
fi
48+
# shellcheck source=/dev/null
49+
FM_ROOT_OVERRIDE="$FM_ROOT" FM_HOME="$FM_HOME" STATE="$STATE" . "$FM_ROOT/bin/fm-wake-lib.sh"
50+
51+
OUT_FILE=$(mktemp "${TMPDIR:-/tmp}/fm-discord-poll.XXXXXX") || exit 1
52+
trap 'rm -f "$OUT_FILE"' EXIT
53+
node "$SCRIPT_DIR/fm-discord-poll.js" > "$OUT_FILE"
54+
poll_rc=$?
55+
cat "$OUT_FILE"
56+
while IFS= read -r line || [ -n "$line" ]; do
57+
case "$line" in
58+
'x-mention '*) ;;
59+
*) continue ;;
60+
esac
61+
request_id=${line#x-mention }
62+
case "$request_id" in
63+
''|.*|*[!A-Za-z0-9._-]*)
64+
printf 'fm-discord-poll: refusing to wake on an unusable request id\n' >&2
65+
continue
66+
;;
67+
esac
68+
# The notifier's own decision-poll shim owns the same durable key, so a
69+
# duplicate here is absorbed by the queue's per-key dedupe instead of
70+
# surfacing the same reply twice. A failed enqueue never loses the capture:
71+
# the inbox record is already on disk, so report it loudly and let a later
72+
# poll's own capture retry.
73+
fm_wake_append check "discord-$request_id" "check: $line" \
74+
|| printf 'fm-discord-poll: captured %s but could not enqueue its wake\n' "$request_id" >&2
75+
done < "$OUT_FILE"
76+
exit "$poll_rc"

0 commit comments

Comments
 (0)