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
2 changes: 1 addition & 1 deletion bin/fm-captain-hold.sh
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ command_hold() {
discord_summary=$reason
[ "${#discord_summary}" -le 1800 ] || discord_summary="${discord_summary:0:1800}…"
"$SCRIPT_DIR/fm-discord-notify.sh" captain-hold "$id" "captain-hold-$id-$occurrence" \
"$discord_summary" "요청대로 진행|보류 상태로 두기" >/dev/null \
"$discord_summary" "요청대로 진행|보류 상태로 두기" "보류 상태로 두기" >/dev/null \
|| printf 'actionable: captain hold %s was recorded but Discord notification failed\n' "$id" >&2
printf '%s\n' "$id"
}
Expand Down
2 changes: 1 addition & 1 deletion bin/fm-discord-notify-status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ case "$verb:$key" in
fi
[ -z "$url" ] || summary="$summary $url"
"$SCRIPT_DIR/fm-discord-notify.sh" pr-ready "$route_task_id" "$key" \
"$summary" "병합|열어 두기" "$task_id"
"$summary" "병합|열어 두기" "열어 두기" "$task_id"
;;
done:*)
note=$(status_line_note "$line")
Expand Down
16 changes: 13 additions & 3 deletions bin/fm-discord-notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { join } from "node:path";

const retryPending = process.argv[2] === "--retry-pending";
const reportMode = process.argv[2] === "--report";
const [trigger, taskId, key, summary, channelId, statusTaskId, ...options] = process.argv.slice(retryPending ? 3 : reportMode ? 3 : 2);
const [trigger, taskId, key, summary, recommendation, channelId, statusTaskId, ...options] = process.argv.slice(retryPending ? 3 : reportMode ? 3 : 2);
const token = process.env.FM_DISCORD_BOT_TOKEN;
const home = process.env.FM_HOME || process.env.FM_ROOT || ".";
const stateDir = process.env.FM_STATE_OVERRIDE || join(home, "state");
Expand Down Expand Up @@ -76,7 +76,7 @@ async function sendRecord(path, record, botId, recover) {
const sending = { ...record, state: "sending", attempted_at: Math.floor(Date.now() / 1000) };
saveRecord(path, sending);
const payload = {
content: `작업: ${record.task_id}\n${localize(record.summary)}\n선택지: ${record.options.map(localize).join(" / ")}\n이 메시지에 바로 답장해 주세요.`,
content: `작업: ${record.task_id}\n왜 연락했나: ${localize(record.summary)}\n필요한 결정: ${decisionPrompt(record.trigger)}\n선택지: ${record.options.map(localize).join(" / ")}\n권장안: ${localize(record.recommendation || record.options[0])}\n답장으로 선택해 주세요.`,
allowed_mentions: { parse: [] },
nonce: record.nonce,
enforce_nonce: true,
Expand All @@ -100,6 +100,15 @@ async function sendRecord(path, record, botId, recover) {
}
}

function decisionPrompt(trigger) {
return ({
"captain-hold": "보류된 작업을 어떻게 진행할지",
"ask-user": "제안된 변경을 승인할지",
"pr-ready": "풀 리퀘스트를 병합할지",
"perm-ask": "OpenCode 요청에 권한을 줄지",
})[trigger] || "어떻게 진행할지";
}

async function main() {
if (reportMode) {
const [reportChannelId, reportMessage] = process.argv.slice(3);
Expand Down Expand Up @@ -156,7 +165,7 @@ async function main() {
}
return;
}
if (!token || !trigger || !taskId || !key || !summary || !channelId || !statusTaskId || options.length === 0) {
if (!token || !trigger || !taskId || !key || !summary || !recommendation || !channelId || !statusTaskId || options.length === 0) {
console.error("fm-discord-notify: required configuration or notification data is missing");
process.exitCode = 2;
return;
Expand Down Expand Up @@ -195,6 +204,7 @@ async function main() {
key,
status_task_id: statusTaskId,
summary,
recommendation,
options,
channel_id: channelId,
nonce,
Expand Down
18 changes: 12 additions & 6 deletions bin/fm-discord-notify.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
# Send one firstmate-initiated decision to the configured self-hosted Discord channel.
# `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]
# Usage: fm-discord-notify.sh <captain-hold|ask-user|pr-ready|perm-ask> <task-id> <key> <summary> <option|option...> <recommendation> [status-task-id]
set -u

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
Expand All @@ -19,13 +19,13 @@ if [ "${1:-}" = --report ]; then
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|perm-ask> <task-id> <key> <summary> <option|option...> [status-task-id]" >&2
elif [ "$#" -lt 6 ] || [ "$#" -gt 7 ]; then
echo "usage: fm-discord-notify.sh <captain-hold|ask-user|pr-ready|perm-ask> <task-id> <key> <summary> <option|option...> <recommendation> [status-task-id]" >&2
exit 2
fi
if [ "$retry_pending" -eq 0 ] && [ "$report_mode" -eq 0 ]; then
trigger=$1 task_id=$2 decision_key=$3 summary=$4 options=$5
status_task_id=${6:-$task_id}
trigger=$1 task_id=$2 decision_key=$3 summary=$4 options=$5 recommendation=$6
status_task_id=${7:-$task_id}
fi
fm_discord_load_config
[ -n "${FM_DISCORD_TOKEN:-}" ] || {
Expand Down Expand Up @@ -61,6 +61,7 @@ case "$trigger:$decision_key" in
*) 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
case "$recommendation" in ''|*$'\n'*|*$'\r'*) echo "fm-discord-notify: recommendation must be one line" >&2; exit 2 ;; esac

channel_id=${FM_DISCORD_CHANNELS%%,*}
case "$channel_id" in ''|*[!0-9]*) echo "fm-discord-notify: configured channel id is invalid" >&2; exit 2 ;; esac
Expand All @@ -69,7 +70,12 @@ IFS='|' read -r -a option_list <<< "$options"
for option in "${option_list[@]}"; do
[ -n "$option" ] || { echo "fm-discord-notify: options must not be empty" >&2; exit 2; }
done
recommendation_found=0
for option in "${option_list[@]}"; do
[ "$option" = "$recommendation" ] && recommendation_found=1
done
[ "$recommendation_found" -eq 1 ] || { echo "fm-discord-notify: recommendation must match one offered option" >&2; exit 2; }

export FM_HOME FM_STATE_OVERRIDE="$STATE" FM_DISCORD_BOT_TOKEN="$FM_DISCORD_TOKEN"
exec node "$SCRIPT_DIR/fm-discord-notify.js" "$trigger" "$task_id" "$decision_key" \
"$summary" "$channel_id" "$status_task_id" "${option_list[@]}"
"$summary" "$recommendation" "$channel_id" "$status_task_id" "${option_list[@]}"
2 changes: 1 addition & 1 deletion bin/fm-opencode-permission.sh
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ push_captain_decision() { # <task-id> <request-id> <action> <resources-json> <s
[ "${#summary}" -le 1800 ] || summary="${summary:0:1800}…"
"$SCRIPT_DIR/fm-discord-notify.sh" "$TRIGGER" "$task_id" "perm-$request_id" \
"$summary" \
"Approve once|Approve once and remember this|Reject the request" >/dev/null \
"Approve once|Approve once and remember this|Reject the request" "Approve once" >/dev/null \
|| printf 'actionable: the permission request was recorded but the captain decision push failed\n' >&2
}

Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ Quiet-mode status reports also use the first configured channel; the report comm

When the self-hosted connector is enabled, firstmate also posts captain decisions to the first configured channel: a newly recorded captain hold (including a genuinely escalated `nm-` ask-user gate, held with the real question in its reason per `ask-user-authority`) or a pull request ready for review when `yolo=off`.
A raw `nm-` ask-user gate observed in a worker's status log never posts by itself - most gates are decided in-scope with no captain involvement, so only the captain-hold recorded at actual escalation time reaches Discord.
Each message includes the task id, a plain-language summary carrying the actual decision content, and the available choices.
Each message states why contact is needed, what decision is required, the available choices, and a recommended choice that must match one of them, plus the task id.
Pull-request review messages name the repository or project when the URL is an accepted GitHub or GitLab merge-request URL, so the captain can identify it before opening it.
Keep the configured channel private to the captain and trusted operators; only IDs in `FM_DISCORD_AUTHORIZED_USER_IDS` can answer decisions, and replies from other channel members are ignored.
Reply directly to a decision message.
Expand Down
24 changes: 20 additions & 4 deletions tests/fm-discord-decision-push.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ test_no_token_is_inert() {
home="$TMP_ROOT/no-token"
mkdir -p "$home"
out=$(PATH="$BASE_PATH" FM_HOME="$home" FM_DISCORD_BOT_TOKEN='' \
"$ROOT/bin/fm-discord-notify.sh" captain-hold task-a captain-hold-task-a-1 "Needs a decision" "Continue|Pause")
"$ROOT/bin/fm-discord-notify.sh" captain-hold task-a captain-hold-task-a-1 "Needs a decision" "Continue|Pause" "Pause")
rc=$?
expect_code 0 "$rc" "missing token is inert"
[ -z "$out" ] || fail "missing token printed output: $out"
Expand Down Expand Up @@ -145,7 +145,7 @@ test_notify_records_reply_binding() {
PATH="$home/fake-bin:$BASE_PATH" FM_HOME="$home" FM_STATE_OVERRIDE="$home/state" \
FM_DISCORD_BOT_TOKEN=fake-token FM_DISCORD_CHANNEL_ID=1000000000000000001 \
"$ROOT/bin/fm-discord-notify.sh" captain-hold task-a captain-hold-task-a-1 \
"Choose how to proceed" "Continue|Pause" >/dev/null \
"Choose how to proceed" "Continue|Pause" "Pause" >/dev/null \
|| fail "notification post failed"
record=$(find "$home/state/x-context" -maxdepth 1 -name 'discord-notify-*.json' -print -quit)
assert_present "$record" "notification binding is persisted"
Expand All @@ -158,9 +158,24 @@ test_notify_records_reply_binding() {
assert_contains "$body" "Choose how to proceed" "message includes summary"
assert_contains "$body" "Continue" "message includes options"
assert_contains "$body" "Pause" "message includes all options"
assert_contains "$body" "필요한 결정: 보류된 작업을 어떻게 진행할지" "message states required decision"
assert_contains "$body" "권장안: Pause" "message shows its recommendation"
pass "proactive Discord post stores the task and reply binding"
}

test_recommendation_must_be_an_offered_option() {
local home output rc
home="$TMP_ROOT/invalid-recommendation"
mkdir -p "$home"
output=$(FM_HOME="$home" FM_DISCORD_BOT_TOKEN=fake-token FM_DISCORD_CHANNEL_ID=1000000000000000001 \
"$ROOT/bin/fm-discord-notify.sh" ask-user task-a nm-run-review \
"A decision is needed" "Approve|Decline" "Maybe" 2>&1); rc=$?
expect_code 2 "$rc" "recommendation outside the offered choices is rejected"
assert_equals "fm-discord-notify: recommendation must match one offered option" "$output" "recommendation validation diagnostic"
assert_absent "$home/state/x-context" "invalid recommendation cannot create a pending decision"
pass "recommendation must match an offered decision option"
}

test_failed_notification_retries_from_durable_outbox() {
local home record log state
home="$TMP_ROOT/retry-failed"
Expand All @@ -172,7 +187,7 @@ test_failed_notification_retries_from_durable_outbox() {
PATH="$home/fake-bin:$BASE_PATH" FM_HOME="$home" FM_STATE_OVERRIDE="$home/state" \
FM_DISCORD_BOT_TOKEN=fake-token FM_DISCORD_CHANNEL_ID=1000000000000000001 \
"$ROOT/bin/fm-discord-notify.sh" ask-user task-retry nm-run42-review \
"A decision is needed" "Approve|Decline" >/dev/null 2>&1; then
"A decision is needed" "Approve|Decline" "Decline" >/dev/null 2>&1; then
fail "a rejected Discord send reported success"
fi
record=$(find "$home/state/x-context" -maxdepth 1 -name 'discord-notify-*.json' -print -quit)
Expand Down Expand Up @@ -203,7 +218,7 @@ test_profile_failure_keeps_retryable_intent() {
PATH="$home/fake-bin:$BASE_PATH" FM_HOME="$home" FM_STATE_OVERRIDE="$home/state" \
FM_DISCORD_BOT_TOKEN=fake-token FM_DISCORD_CHANNEL_ID=1000000000000000001 \
"$ROOT/bin/fm-discord-notify.sh" ask-user task-profile nm-run43-review \
"A decision is needed" "Approve|Decline" >/dev/null 2>&1; then
"A decision is needed" "Approve|Decline" "Decline" >/dev/null 2>&1; then
fail "a rejected profile lookup reported success"
fi
record=$(find "$home/state/x-context" -maxdepth 1 -name 'discord-notify-*.json' -print -quit)
Expand Down Expand Up @@ -599,6 +614,7 @@ test_report_requires_token
test_report_helper_refuses_non_quiet_mode
test_report_helper_sends_bearings_snapshot
test_notify_records_reply_binding
test_recommendation_must_be_an_offered_option
test_failed_notification_retries_from_durable_outbox
test_profile_failure_keeps_retryable_intent
test_stale_sending_notification_recovers_without_duplicate_post
Expand Down
Loading