Repository navigation
feat(bin): surface OpenCode permission asks as captain decisions - #76
Merged
Merged
Conversation
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.
Two review findings, both real. A notification that failed once was never retried. The existing-record path in ask returned before the push, so a Discord outage left the captain never asked while the worker stayed blocked. The push is idempotent per (trigger, task, key), so it now runs on that path too, and the captured record is still left untouched. The question rendered only resources[0] and flattened the save list on spaces. Approving a request grants every resource it names, so the captain could approve scope he was never shown, and a path containing a space lost its boundary. Both lists are now carried as compact JSON and rendered whole. Each is proven by a negative control: reverting the retry fails the push-count assertion, and reverting the rendering fails the full-list assertion.
Ivory2024
added a commit
that referenced
this pull request
Oct 7, 2026
* 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. * fix: retry a failed captain push and show every requested path Two review findings, both real. A notification that failed once was never retried. The existing-record path in ask returned before the push, so a Discord outage left the captain never asked while the worker stayed blocked. The push is idempotent per (trigger, task, key), so it now runs on that path too, and the captured record is still left untouched. The question rendered only resources[0] and flattened the save list on spaces. Approving a request grants every resource it names, so the captain could approve scope he was never shown, and a path containing a space lost its boundary. Both lists are now carried as compact JSON and rendered whole. Each is proven by a negative control: reverting the retry fails the push-count assertion, and reverting the rendering fails the full-list assertion. * no-mistakes(document): Add missing opencode-permission state path to layout reference
Ivory2024
added a commit
that referenced
this pull request
Oct 7, 2026
* 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. * fix: retry a failed captain push and show every requested path Two review findings, both real. A notification that failed once was never retried. The existing-record path in ask returned before the push, so a Discord outage left the captain never asked while the worker stayed blocked. The push is idempotent per (trigger, task, key), so it now runs on that path too, and the captured record is still left untouched. The question rendered only resources[0] and flattened the save list on spaces. Approving a request grants every resource it names, so the captain could approve scope he was never shown, and a path containing a space lost its boundary. Both lists are now carried as compact JSON and rendered whole. Each is proven by a negative control: reverting the retry fails the push-count assertion, and reverting the rendering fails the full-list assertion. * no-mistakes(document): Add missing opencode-permission state path to layout reference
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Intent
이거 나한테 선택창이 안보여 근본적으로 해결해 이런일이 자주 생길거잖아
The captain never sees the OpenCode permission choice dialog, and such asks recur constantly because any tool call reaching outside a worker's disposable worktree raises one. He wants this fixed at the root: the ask must reach him as a real decision, and his answer must be applied to exactly the request he was asked about.
What was built, and the decisions behind it:
An OpenCode 2 permission ask renders as a modal inside the worker's own TUI, and firstmate had no path from that modal to any captain-visible surface. A new bin/fm-opencode-permission.sh owns one real request end to end. It reads the request from the live server, requires the returned id and session to match what it was asked about, and records it durably under the task's own state. The captain decision travels the existing Discord decision path under a new perm-ask trigger, so there is no second bot and no second approval ledger. The generated OpenCode plugin only forwards permission.asked and decides nothing at all: a plugin that approved anything would be exactly the invisible grant this change exists to remove.
The apply side is where the safety lives. It 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 the request pending with the same id, session, and action. The read-check-consume sequence runs as one critical section and the record becomes consumed before the API write, so a replayed or concurrent second caller refuses rather than granting twice. Every refusal is a nonzero exit with no API write. Duplicate polls, expired requests, stale task generations, another task's id, and a decision outside the server's own enum all fail closed.
Deliberate choices a reviewer should not read as oversights:
Autonomous in-process approval of in-worktree asks was considered and rejected. It would grant without the captain ever seeing the ask, which is the precise failure being closed. Every ask is his decision. The recommendation in the message favours the one-shot option, and taking it is his call.
The OpenCode launch flags were left alone. Switching to a per-worker server would make a blanket allow effective and silently remove every ask, including the captain's decision surface. The brief forbids that unless the captain-visible decision requirement is still met, and it would not be.
The captain-bottleneck audit's proposal to auto-close a captain call after 48 hours was not implemented. A timer that closes a credential, physical-action, or explicitly-paused call takes his decision away from him. The proposal to structurally gate escalation on an authority label the same pipeline emits was also rejected as a self-approving loop.
The pre-existing saved external-directory grants on this machine are untouched. Revoking access the captain granted is his call, not a side effect of this change.
Discord reply latency was in scope alongside the permission surface, and its root cause is separate from the permission ask. A captured reply is now also enqueued into the durable wake queue, so capture itself is the wake. Previously the reply only reached a session when the watcher ran the poll; an operator-run or LaunchAgent-run ingress poll captured the reply, wrote the single-fire marker, and sent its own output to a log file, so every later watcher poll suppressed an item nobody had been told about. Measured capture-to-resolution in the independent scout's findings was up to roughly 44 minutes. The poll's stdout contract is unchanged, so the watcher's own validated dispatch still forwards the same line.
The keyed captain answer now also confirms itself to the captain after it is applied. Before this, a Discord decision answer produced no receipt at all, which is indistinguishable from the reply having been lost. An optional occurrence guard on the single-key answer intake closes a related silent-wrong-answer hole: a reply arriving after a call was answered and re-held would otherwise resolve the newer question the captain was never asked. The guard is optional, so existing callers are unchanged.
Verification: nine suites plus lint and the documentation audience check. Every concurrency and rendering guarantee is proven by a negative control, not by reading the code: removing the lock makes the two-post race appear, reverting the push retry drops the retry count, and reverting the full-list rendering loses the second resource. The skill's documented reply derivation is executed as written in a test, so a drift between the documented command and the applier's interface fails rather than misrouting a grant. No live OpenCode permission request was created, replied to, or probed, and no Discord message was sent. One saved grant was created by an early test version that had not yet been given its recorder shadow, over a deleted temp test directory; it was removed through the server's own endpoint and the permission table was verified back at its prior row count, and the corrected test re-run changes nothing. That disclosure is recorded in the task report.
Known limit: the bridge only takes effect on an updated home. The deployed home's spawn script still writes the older OpenCode plugin shape, so every firstmate OpenCode plugin fails to load there. That is already fixed on the default branch and is a fleet-update matter, not part of this change.
What Changed
bin/fm-opencode-permission.sh, owning a real OpenCode permission request end to end: it reads the ask from the live server, requires the returned id/session to match what it was asked about, records it durably under the task's state, and pushes it to the captain through the existing Discord decision path under a newperm-asktrigger (no second bot, no second approval ledger). The apply side runs a read-check-consume critical section that refuses unless the record names the exact task/request, is still pending, matches the task's current busy generation, and the server still reports the same pending id/session/action, so a replay or concurrent second caller cannot grant twice; every refusal exits nonzero with no API write.bin/fm-spawn.shwrites a new generated OpenCode plugin that only forwardspermission.askedevents tofm-opencode-permission.sh askand decides nothing itself;bin/fm-teardown.shremoves the per-taskopencode-permissionstate directory on teardown.bin/fm-discord-poll.shnow enqueues a captured reply into the durable wake queue at capture time instead of relying on the watcher's own poll to notice it, andbin/fm-captain-hold.shretries a failed captain push and reports every requested path.bin/fm-discord-notify.sh/.jsadd a post-apply confirmation receipt for keyed captain answers and an optional occurrence guard on single-key answer intake.firstmate-layout,fmx-respond,docs/scripts.md) and test coverage (tests/fm-opencode-permission.test.sh,tests/fm-opencode-permission-plugin.test.sh,tests/fm-captain-hold-lifecycle.test.sh,tests/fm-discord-decision-push.test.sh,tests/fm-teardown.test.sh) for the new script, plugin contract, push retry, and teardown cleanup.🤖 Generated with Claude Code
Risk Assessment
Testing
Every file this branch touches (the new bin/fm-opencode-permission.sh, its fm-spawn.sh-generated plugin, the fm-teardown.sh cleanup line, fm-discord-notify.sh/js, fm-discord-poll.sh, and the fm-captain-hold.sh occurrence guard) has a targeted test suite that drives the real script live against fake external servers; all passed, and I confirmed two of the fixed defects' regression tests are real (not vacuous) by reverting each fix and watching the test fail.
Evidence: fm-opencode-permission.test.sh full pass
Evidence: negative control: reverted failed-push retry fails as expected
Evidence: negative control: reverted full-resource-list rendering fails as expected
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
✅ **Rebase** - passed
✅ No issues found.
bin/fm-captain-hold.sh:1960- The new occurrence guard (require_current_occurrence, called fromcommand_answer_oneat line 1960) reads task state and compares the occurrence WITHOUT holdingacquire_task_control_lock. The task is only locked much later, insidecommand_answer(line 1030), which is reached indirectly throughcommand_answers(a separate, unlocked loop starting ~1227) spawning"$0" answeras a new subprocess.command_answeritself never re-validates the originally-expected occurrence; it only computesoccurrence = resolution_record_count+1for its own record and writes a resolution unconditionally once state==pending/hold_kind==captain. Concrete sequence: captain replies to the decision pushed for occurrence N; beforerequire_current_occurrence's unlocked read is consumed by the eventual locked write, a worker concurrently callsholdagain (e.g. a new ask on the same task), bumping the task to occurrence N+1 and reopening it;require_current_occurrencealready returned success for N, so the stale reply proceeds throughcommand_answersand is applied viacommand_answerto the now-different, newer question the captain was never shown — exactly the silent-wrong-answer scenario this guard exists to close (per the user intent: "a reply arriving after a call was answered and re-held would otherwise resolve the newer question the captain was never asked"). No test in this diff exercises concurrent hold/answer timing (the added testtest_answer_one_occurrence_guard_refuses_a_superseded_questionis fully sequential), so the gap is untested. Smallest honest remedy: move the occurrence comparison inside the same locked critical section that performs the resolution (e.g. thread the expected occurrence intocommand_answerand re-check it underacquire_task_control_lockimmediately before writing, mirroring the lock-then-check-then-consume pattern already used in bin/fm-opencode-permission.sh'scommand_decide), rather than checking once, early, and unlocked.bin/fm-opencode-permission.sh:210-push_captain_decision's summary truncation ([ "${#summary}" -le 1800 ] || summary="${summary:0:1800}…") is an unchanged blind byte-slice carried over verbatim from the pre-fix version (identical in commit 4186ff3), despite the round-1 authorized fix instructions explicitly requiring: "if truncation is still needed, cut only after the arrays are rendered and never in a way that drops an entry silently." For a request naming enough resources/save paths to push the rendered summary past 1800 characters, this slice can land in the middle of theresources=[...]or['...']JSON (or mid path-string), producing a mangled/incomplete list shown to the captain — silently misrepresenting the exact scope being approved, which is the same class of defect review-2 was meant to close. Smallest honest remedy: truncate by dropping whole trailing resource/save entries (re-serializing valid, complete JSON) rather than slicing the rendered string, or omit/summarize entries explicitly (e.g. "+N more") instead of a raw character cut.✅ **Test** - passed
✅ No issues found.
bash tests/fm-opencode-permission.test.sh(20/20 ok) — ask/decide/settle against a fake opencode server: server-confirmed-only recording, identity mismatch refusal, stale-generation refusal, foreign-task refusal, unknown-decision refusal, server-refusal receipt, idempotent ask, failed-push retry, full resource/save list with comma- and space-bearing paths, concurrent decides post exactly once, documented reply-derivationbash tests/fm-opencode-permission-plugin.test.sh(1/1 ok) — realfm-spawn.shgenerates the OpenCode plugin into a worktree, plugin loaded in a real Node host and fed a real permission.asked event, forwards only the complete identity and grants nothingbash tests/fm-discord-decision-push.test.sh(20/20 ok) — perm-ask trigger validation, perm- key routing through fm-discord-poll.sh into x-inbox, durable wake-queue enqueue on capture (operator/LaunchAgent-poll latency fix)bash tests/fm-captain-hold-lifecycle.test.sh(full suite, 0 not-ok) — includes the two new occurrence-guard tests: refuses a stale reply to a superseded/re-held occurrence, refuses a non-captain-held task and a non-numeric occurrencebash tests/fm-teardown.test.sh(87/87 ok) — includes the new test proving teardown removes a retired task's OpenCode permission-request recordsManual negative control: reverted the failed-push-retry fix in bin/fm-opencode-permission.sh and reran the suite — the retry test failed as expected, then restored the file (git diff clean)Manual negative control: reverted the full-resource-list rendering to resources[0]-only and reran the suite — the rendering test failed as expected, then restored the file (git diff clean)✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.