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
1 change: 1 addition & 0 deletions .agents/skills/firstmate-layout/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ state/ runtime records and signals; gitignored
<id>.reconcile-nudged epoch second of the last inventory-reconcile nudge sent to this secondmate; bin/fm-secondmate-reconcile.sh owns its per-home cooldown window
<id>.backlog-close the exact backlog transition a teardown recorded before removing the task's record, so an interrupted cleanup can still be finished at the next session start; bin/fm-backlog-transition-lib.sh owns its format and replay, and a landed transition removes it
<id>.inbox/ durable steering inbox: sequenced firstmate instruction records the worker acknowledges by moving them into its handled/ subdirectory; written by fm-send, with ordinary records re-rung and escalated by the watcher while explicit fire-and-forget records are excluded from that ladder, and removed by teardown (bin/fm-task-inbox-lib.sh)
<id>.opencode-permission/<request-id>.json durable record of one OpenCode permission request pushed to the captain as a perm-ask decision; bin/fm-opencode-permission.sh owns the schema and the ask/decide/settle lifecycle; removed by teardown
<id>.meta task metadata; each producer script's header owns its exact fields and mutation contract, with docs/configuration.md routing operator-facing backend and trace-context details
<id>.herdr-presentation quarantinable attempt and restart-binding journal for Herdr's optional visual projection; never task or endpoint authority; see docs/herdr-backend.md "Presentation spaces"
<id>.check.sh authenticated slow poll; the watcher dispatches validated PR data and the byte-identified Relay shim through trusted repository scripts, runs registered custom checks from hash-validated private snapshots, and rejects every other state check without execution
Expand Down
14 changes: 13 additions & 1 deletion .agents/skills/fmx-respond/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,24 @@ Images are only for actual visual artifacts - a generated illustration, a screen

Before classifying an inbox item as a new mention, check whether `source` is `discord-selfhosted-decision`.
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.
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.
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.
`$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.
`--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.
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.
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.
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.
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`.
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.
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.
These are the existing keyed-answer paths; never edit status files directly.
Remove the inbox record only after the matching command confirms success.
On failure leave it for retry and report the exact error.

Every decision answer is applied and then confirmed, in that order.
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.
Post it after the keyed command succeeds, never before, and never instead of applying.
Without that receipt the captain answered a decision and got no evidence it was consumed, which is indistinguishable from the reply being lost.

This is a drain over the inbox, not a single reply.
The watcher coalesces same-key `check:` wakes, so one `x-mention` wake can stand in for several pending mentions.
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.
Expand Down
41 changes: 40 additions & 1 deletion bin/fm-captain-hold.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
# fm-captain-hold.sh hold <task-id> --reason <reason> \
# [--title <title>] [--repo <repo>] [--origin <origin-id>] [--until YYYY-MM-DD]
# fm-captain-hold.sh answer <task-id> --decision-file <path> [--release]
# fm-captain-hold.sh answer-one <task-id> <answer> <label> \
# --source <provenance> [--expect-occurrence <n>]
# fm-captain-hold.sh answers [<legacy-origin> | --any-origin] --source <provenance> (keyed answers on stdin)
# fm-captain-hold.sh reconcile-requests --source-id <source-id> --source <provenance> (task ids on stdin)
# fm-captain-hold.sh bind <source-id> [<legacy-origin> | --any-origin]
Expand Down Expand Up @@ -115,6 +117,14 @@
# and pipe them here. It must never map keys to tasks, build decision records,
# choose a close mode beyond what its card declared, or close anything itself.
#
# `answer-one` is the single-key form of that same intake, for a channel that
# captured exactly one decision. `--expect-occurrence <n>` is its occurrence
# guard: pass the occurrence the decision was actually asked at, and a task
# that is no longer at that occurrence - already answered, or answered and
# re-held, or no longer captain-held - is refused before any resolution is
# written. Without the flag the guard does not run, so an existing caller that
# has no occurrence to assert keeps its current behavior.
#
# `bind`, `unbind`, and `binding` record that a captured-answer SOURCE feeds
# this intake, for any channel whose answers arrive detached from their origin
# (a process-event source id, for example). The binding is a private record
Expand Down Expand Up @@ -1933,11 +1943,12 @@ command_open() { # <task-id> [--identity] [--distinguish-absent]
}

command_answer_one() {
local key=${1:-} answer=${2:-} label=${3:-} source=''
local key=${1:-} answer=${2:-} label=${3:-} source='' occurrence=''
[ "$#" -ge 3 ] || { usage >&2; exit 2; }
shift 3
while [ "$#" -gt 0 ]; do
case "$1" in
--expect-occurrence) shift; occurrence=${1:-} ;; --expect-occurrence=*) occurrence=${1#*=} ;;
--source) shift; source=${1:-} ;;
*) usage >&2; exit 2 ;;
esac
Expand All @@ -1946,12 +1957,40 @@ command_answer_one() {
[ -n "$source" ] || fail "--source provenance is required so the durable decision records where the answer came from"
validate_slug task-id "$key"
[ "${#key}" -le 128 ] || fail "task-id must be at most 128 characters"
if [ -n "$occurrence" ]; then
require_current_occurrence "$key" "$occurrence"
fi
answer=$(sanitize_field "$answer")
label=$(sanitize_field "$label")
printf '%s\t%s\t%s\n' "$key" "$answer" "$label" \
| command_answers --any-origin --source "$source"
}

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

case "${1:-}" in
hold) shift; command_hold "$@" ;;
answer) shift; command_answer "$@" ;;
Expand Down
3 changes: 3 additions & 0 deletions bin/fm-discord-notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ function localize(value) {
"Leave it open": "열린 상태로 두기",
"Continue with the request": "요청대로 계속 진행",
"Leave it on hold": "보류 상태로 두기",
"Approve once": "이 1회만 허용",
"Approve once and remember this": "허용하고 이 경로를 기억",
"Reject the request": "요청 거부",
})[value] || value;
}

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

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
Expand All @@ -19,7 +20,7 @@ elif [ "${1:-}" = --retry-pending ]; then
[ "$#" -eq 1 ] || { echo "usage: fm-discord-notify.sh --retry-pending" >&2; exit 2; }
retry_pending=1
elif [ "$#" -lt 5 ] || [ "$#" -gt 6 ]; then
echo "usage: fm-discord-notify.sh <captain-hold|ask-user|pr-ready> <task-id> <key> <summary> <option|option...> [status-task-id]" >&2
echo "usage: fm-discord-notify.sh <captain-hold|ask-user|pr-ready|perm-ask> <task-id> <key> <summary> <option|option...> [status-task-id]" >&2
exit 2
fi
if [ "$retry_pending" -eq 0 ] && [ "$report_mode" -eq 0 ]; then
Expand Down Expand Up @@ -56,6 +57,7 @@ case "$trigger:$decision_key" in
captain-hold:captain-hold-*) ;;
ask-user:nm-*) ;;
pr-ready:pr-ready-*) ;;
perm-ask:perm-*) ;;
*) echo "fm-discord-notify: trigger does not match its decision key" >&2; exit 2 ;;
esac
case "$summary" in *$'\n'*|*$'\r'*) echo "fm-discord-notify: summary must be one line" >&2; exit 2 ;; esac
Expand Down
45 changes: 44 additions & 1 deletion bin/fm-discord-poll.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
# state/discord-watch.check.sh matches the expected byte-static identity shim.
#
# Its contract: output "x-mention <request_id>" => wake firstmate, silence => keep sleeping.
#
# A captured item is also enqueued into the durable wake queue here, so capture
# itself is the wake. The stdout contract only reaches a session when the WATCHER
# ran this script: an operator-run or LaunchAgent-run ingress poll reaches nobody
# that way, because its stdout goes to a log file while the offered marker
# written during capture then makes every later watcher poll suppress the item.
# That combination captured a captain reply and never woke anyone for it. The
# enqueue is additive, so the watcher's own validated check dispatch still sees
# and still forwards the same line.

set -u

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

exec node "$SCRIPT_DIR/fm-discord-poll.js"
# Only a token-gated run can capture anything, so the wake library is loaded
# only here: without a token this stays a hard no-op that touches no state.
if [ ! -r "$FM_ROOT/bin/fm-wake-lib.sh" ]; then
printf 'fm-discord-poll: cannot enqueue a wake (missing %s)\n' "$FM_ROOT/bin/fm-wake-lib.sh" >&2
exit 1
fi
# shellcheck source=/dev/null
FM_ROOT_OVERRIDE="$FM_ROOT" FM_HOME="$FM_HOME" STATE="$STATE" . "$FM_ROOT/bin/fm-wake-lib.sh"

OUT_FILE=$(mktemp "${TMPDIR:-/tmp}/fm-discord-poll.XXXXXX") || exit 1
trap 'rm -f "$OUT_FILE"' EXIT
node "$SCRIPT_DIR/fm-discord-poll.js" > "$OUT_FILE"
poll_rc=$?
cat "$OUT_FILE"
while IFS= read -r line || [ -n "$line" ]; do
case "$line" in
'x-mention '*) ;;
*) continue ;;
esac
request_id=${line#x-mention }
case "$request_id" in
''|.*|*[!A-Za-z0-9._-]*)
printf 'fm-discord-poll: refusing to wake on an unusable request id\n' >&2
continue
;;
esac
# The notifier's own decision-poll shim owns the same durable key, so a
# duplicate here is absorbed by the queue's per-key dedupe instead of
# surfacing the same reply twice. A failed enqueue never loses the capture:
# the inbox record is already on disk, so report it loudly and let a later
# poll's own capture retry.
fm_wake_append check "discord-$request_id" "check: $line" \
|| printf 'fm-discord-poll: captured %s but could not enqueue its wake\n' "$request_id" >&2
done < "$OUT_FILE"
exit "$poll_rc"
Loading
Loading