Repository navigation
feat(opencode): answer OpenCode 2 permission requests from a card - #4515
Conversation
OpenCode 2's service holds each permission request for the session
(GET /api/session/{id}/permission) and takes its answer over HTTP
(POST .../permission/{requestID}/reply). The pane's pending request is now
an approval card read from that list: the shell command, the edit's patch,
the fetched URL or the MCP tool with its input, answered Allow once, Always
allow (only when OpenCode would save a pattern) or Deny. The request leaves
the list once the TUI answers it, and the card with it; a reply OpenCode no
longer holds answers prompt_changed.
The routes are experimental in OpenCode's OpenAPI, so only a verified 2.x
service is asked, and a list holding anything of another shape or session
is no answer: the fallback card stays, as before.
Recorded fixture: server/fixtures/opencode-permission-2.2.2.json, the
permission.asked/replied events of a real OpenCode 2.2.2.
|
No actionable comments were generated in the recent review. Summary
Merge Risk:
|
… the fallback card A card cuts its body at 12,000 characters; a longer command, patch or MCP input on an approval card would have its user allow what they never saw. Such a request gets no card, as Claude's held approvals do.
….x service - An edit whose files do not each carry a patch gets no card: part of a patch would have its user allow the rest unseen. - The pending list a card is read from is the latest read's: an older read finishing late no longer brings back a request the TUI already answered, and an answered request leaves it at once. - Only a 2.x.y service is listed or answered, checked again right before the reply. - Tests assert decisions, saved patterns and card values, not wording.
|
Reviews on this PR and how each finding was resolved: Dori omo review @ 231c75e (BLOCK)
Gate reviewer (omo-native-gate-reviewer) @ 231c75e (BLOCK), fixed in 6fc0708. Each fix has a test that fails on 231c75e:
Dori omo review @ 6fc0708: CLEAR. Its LOW notes are not changed in this PR:
CodeRabbit reviewed 231c75e with no actionable comments. Unresolved review threads: 0. |
|
Merged through Dori omo review CLEAR at 6fc0708 (findings and fixes in the PR comment); CodeRabbit reviewed only the first head, no review of this head; merged on the lead's word under the owner's standing rule for CodeRabbit-limited PRs. |
…aude as its first adapter (#4516) ## Change #4480 added Claude Code's `PermissionRequest` hook, written for Claude alone: the socket, the per-launch key, the held requests, the card ids and the answers lived in `ClaudePermissionHooks`, and `agent-launch.ts`, `prompt.ts`, `index.ts` and `dispatch.ts` called it by name. Every agent that moves its approvals to its own hook would have edited those same files. This PR turns that code into one approval hub for every agent, with a small adapter per agent. Claude is the only adapter, and its behaviour does not change. ### The hub (`server/approvals/hub.ts`) It owns everything about a request once it arrives: - the listener: a unix socket in a 0700 folder of the state directory (as before), or on Windows a named pipe; - a key per launch, naming the pane the agent was started in; - the held requests, their ids, releasing them, and answering them. A held request is kept apart from any screen. `held(paneId)` lists a pane's requests and `settle(paneId, id, decision)` answers one by its id. The screen binding that Claude's cards use today (`shown`, `holds`, `answer`: a request binds only to a dialog that shows its whole command) sits on top of those. A later PR can build a card from the hook's request alone, with screen reading as the fallback, without changing the hub. ### The adapter (`ApprovalAdapter`) Each agent fills in only these, in its own file: - `supports(version)`: an unknown version, or one that cannot be read, gets no hook (keys, as before #4480); - `launchArgs(args, hook, dir)`: its arguments with the hook added, files only in the hub's own folder. It never edits the user's settings; - `toCard(event)`: the agent's request as a card shows it, or null for one the hub gives no decision on; - `decision(label)`: which card options are a decision; the rest still go as keys; - `toAnswer(decision | null)`: what the hook prints. The hub applies before `agentLaunch` chooses between a shell-started agent and one herdr starts, so a later adapter for dsh-tui or omo needs no change there. `server/approvals/claude.ts` is #4480's code moved: the `--settings` merge, the `Bash|Monitor` matcher, Bash events only, Yes = `allow`, No = `deny` + `interrupt`. It supports Claude Code 2.1.286 and later, the first version that draws a Bash dialog's command between dashed rules, which is what a held request binds to. An older Claude would bind nothing. Which version: the agent catalog's own version read (`AgentDetector`, the agent's `--version`, kept per executable). `agentLaunch` waits for it before it starts the agent; a version that is not read means no hook. ### The channel, on Windows too The hook runs `ovis-server approval-hook --socket <address> --launch <key>` (`server/approvals/relay.ts`, renamed from `claude-hook`; same Bun config and no-preload guards). The wire is one JSON line each way over `node:net`, the same on a unix socket and a named pipe (as `server/ssh.ts` already does for askpass): - the request carries the key's sha256, a fresh nonce, and the event sealed with the key (AES-256-GCM, under a sealing key derived from the launch key, with the side and nonce as associated data). It never carries the key; - the answer comes back sealed the same way and bound to the request's nonce. The relay prints only an answer that opens. So a listener that does not hold the key can neither read the command nor answer it. On Linux and macOS the 0700 folder already kept other accounts out. On Windows anyone can create a pipe of a free name. The pipe's name is random for each server run, and the sealing covers another account that takes the name once Ovis stops (the reason #4480 refused a loopback port) or connects to the hub's pipe. Before this PR, a Claude on Windows had no hook. The hub validates each line before it touches it (a JSON object, a known launch, a well-formed nonce, a seal that opens, at most 256 KiB of UTF-8 as main's `Content-Length` cap). Anything else closes that one connection with no answer, and the hub goes on. **Red first.** - The old relay, pointed at a squatter on its socket that answers `allow` to anything, printed the squatter's `allow` and sent the squatter the key. The new relay prints nothing, and the squatter gets neither the key nor the command. The hub test `takes no answer from a listener at its address that does not hold the key, and tells it neither the key nor the command` keeps this. - At this PR's first commit, a `null` line on the hub's socket threw an uncaught `TypeError` in the server. Now that line closes only its connection; covered by `holds nothing for a request not sealed with the launch's key, nor for a line that is no request`, which then answers a valid request. ### Anything the hub cannot be sure of is the terminal's No adapter, an unsupported or unread version, an event the adapter does not take, an unsigned request, a command the card could not show whole or as it is (#4480's rules, unchanged), and any channel failure: no decision, and the agent's own dialog stands. ### Other - The state folder is now `approval hooks/`; the old `claude hooks/` folder (a dead socket and settings files whose keys no run answers) is removed at start. - No HTTP/WS contract the browser uses changes: the card keeps its shape, and a bound card's id is still `<screen id>.<16 hex>`. - No other agent's adapter is in this PR, and no screen reading is removed. ## Validation - **Fast checks:** `bun run check fast` passed: workflow syntax, generated types and themes fresh, typecheck, build, unit tests. - **Hub, Claude and launch unit tests:** `bun test --rerun-each 10 ./server/approvals/ ./server/agent-launch.test.ts ./server/core/pane-vacated.test.ts ./server/entries.test.ts ./server/conventions.test.ts`: 900 pass, 0 fail. #4480's test file is moved to `server/approvals/claude.test.ts` with every case and expectation kept. Two changes: the calls name the agent and its version, and a bound card now also says its `kind`. The "no hook on Windows" case is replaced by the Windows address test. - **New hub tests** (`server/approvals/hub.test.ts`): - no hook for an agent without an adapter, nor for a version that is unsupported or unread; - the Windows address is a fresh named pipe for each hub; - a request is held apart from any screen and settled by its id; - an event the adapter does not take gets no decision at once; - unsealed, mis-sealed, replayed-nonce, malformed and oversized lines are not held, and the hub keeps answering; - no event over 256 KiB of UTF-8; - a squatter can neither answer nor learn the key or the command. - **`agentLaunch`** (`server/agent-launch.test.ts`): a hub adapter's hook reaches a shell-started agent (omo, gjc, dsh-tui) as it does one herdr starts, at the version read. It failed on the first commit, which hooked only herdr-started agents. - **Contract tests** (isolated herdr session): `prompt.contract` and `compiled-server.contract`, 24 pass, 0 fail. These cover the real answer route on a bound card, and the compiled `approval-hook` in a folder with a planted `bunfig.toml` and `.env`. - **Real Claude Code 2.1.330 (Haiku)**, isolated herdr session, started through `agentLaunch` with the hub, the catalog's version read (`2.1.330`) and the prompt routes: - Bash `touch hook-allow.txt`: the card bound to the held request (body `Create empty file hook-allow.txt\ntouch hook-allow.txt`). Yes answered 200, and the file was created. - Bash `touch hook-deny.txt`: the card bound; No answered 200. Claude showed `Interrupted · What should Claude do instead?`, and the file was not created. - **Native Windows** (winvm, Windows 11, Bun 1.4.2, a fresh `%TEMP%` clone at 6d1489f, deleted after): `bun test ./server/approvals/hub.test.ts ./server/approvals/claude.test.ts ./server/windows-native.test.ts`, 46 pass, 0 fail, 5 skip. The skips are the POSIX file-mode and link cases of `launchArgs`. The relay tests ran over a real named pipe: a held request answered allow and deny, an unknown launch, a stopped hub, malformed lines and a squatter at the pipe's name. Not covered: a second Windows account and a real Claude Code on Windows. These test files are not in CI's Windows job, which this PR does not change. - **Reviews before opening:** one gate reviewer (BLOCK at the first commit, with two HIGHs: the relay sent the command before it knew the listener, and malformed input could throw in the hub; MEDIUMs: shell-started agents were not hooked, the event cap was in UTF-16 units). A Codex review (gpt-6.2-sol-fast, xhigh) found that a failed bind's `error` event had no handler yet. All fixed in the second commit with the tests above. Its output was cut by its provider's content filter before a written report, so its other notes are not listed. - **After opening:** origin/main merged (one conflict in `server/prompt.ts`'s imports beside #4515's OpenCode permission card). CodeRabbit's one thread (a version read finishing after `stop()` reopened the listener) is fixed in 24171d1 with a test that fails on the previous hub. On the merged tree: `bun run check fast` passed (24,106 unit tests), and the prompt, OpenCode, omo-prompt, prompt-watch and compiled-server contract files passed (74 tests). CI is green at 24171d1. - **Skipped:** `bun run check full` (CI runs it); macOS (not run on eamac).
…on card (#4533) ## What An OpenCode 2 permission card for a shell command now shows the whole command that will run, byte for byte, instead of the pieces OpenCode's permission rules match. OpenCode 2.2.2 lists those pieces in `resources`. `a > f && b >> f && c` arrives as `["a > f", "b", "c"]` (the second redirect is gone). A heredoc arrives as its first line plus the next command. The card (#4515) joined those pieces, so a long command, a heredoc or a quoted chain showed something other than what ran. In the approval-path measurement this happened in 10 of 25 approvals. The request names its tool call: `source: { type: "tool", messageID, id }`. OpenCode's own TUI resolves the call through that field. It reads the call's input from the message (`state.input.command`, unless the input is still streaming) and shows `$ <command>`. Ovis now does the same: - `server/opencode-service.ts`: `listPendingPermissions` keeps `source`. `readOpenCodeToolInput` reads `GET /api/session/{id}/message/{messageID}` and takes the tool part with that call id. `withShellInput` attaches the command to the first pending shell request (the one the card shows). A later read of the same request reuses the command when the request still names the same message and call, because a running call's input does not change. - `server/dialogs/opencode.ts`: a shell card shows `$ ${input.command}`. A shell request whose call could not be read (message gone, input still streaming, an older 2.x without the route) gets no card and keeps the fallback card. An edit without its patch already works this way: the card never asks someone to allow something they cannot see in full. - `server/prompt.ts`: the OpenCode branch reads the call after the pending list. The browser contract is unchanged: the same card, same options, same reply route. Only the body text is now correct. ## Validation - Red first: 12 new or changed tests in `server/dialogs/opencode.test.ts` and `server/opencode-answer.test.ts` fail on `main`'s code and pass with this change. - New fixture `server/fixtures/opencode-permission-command-2.2.2.json`: recorded from a real, isolated OpenCode 2.2.2 (`opencode serve`, own HOME/XDG, model dsapi/deepseek-v4.1-flash). It holds the short, long (`&&`, `>>`), heredoc and quoted (Korean, `$`, `|`, `&`) commands. Each case has the pending request, the message its call is in, and the `bash -c` script strace saw run. The directory is scrubbed. - End to end with this branch's own functions (`listPendingPermissions` → `withShellInput` → `openCodePermissionPrompt` → `replyOpenCodePermission`) against an isolated OpenCode 2.2.2 serve. These are the same five commands as the approval-path measurement, with ground truth from strace for shell commands and file bytes for writes: - 25 of 25 cards match exactly what ran (short 5, long 5, multi 5, quote 5, write 5; a 6th write was also exact). 15 of the 20 shell requests came in more than one piece. - 5 extra write turns got no write approval: the model first asked to run `od -c` on the input file, the driver denied it, and the prompt tells the model to stop on a denial. Those turns are left out, as in the original measurement. - Before this change, the same path gave 15 of 25. - `bun run check fast`: typecheck, build and generated files ok. Unit tests: 24382 pass, 1 fail. The failure is `server/ssh-account-add.test.ts`: its unix socket path goes over the length limit under this lane's long TMPDIR. It fails the same way without this change, and passes (3/3) with a short TMPDIR. - Not run: full check, browser regressions (the card component is unchanged).
What
OpenCode 2's permission prompts (shell command, file edit, web fetch, MCP tool) get a real approval card instead of the generic "Waiting for input" Enter/Esc card.
server/opencode-service.ts:listPendingPermissionsreadsGET /api/session/{id}/permissionfrom the verified background service (the same one the question forms already use).replyOpenCodePermissionposts{decision}toPOST /api/session/{id}/permission/{requestID}/reply. The form reply and the permission reply share one helper.server/dialogs/opencode.ts:openCodePermissionPromptbuilds the card the way OpenCode's TUI presents a request (tui/src/util/permission.tsat v2.2.2):$ commandforshell, the patch foredit, the URL forwebfetch, and the tool name plus its JSON input for MCP tools (<server>_<tool>) and anything else. Options: Allow once / Always allow / Deny. Always allow shows only when the request hassavepatterns, as in the TUI, and its description says what it saves.server/prompt.ts: the OpenCode branch checks permissions next to forms, and a pending permission comes first. The answer branch checks again that herdr still reports that session in the pane, then replies. 404 (already answered in the TUI or elsewhere) givesprompt_changed. No reply givesreply_unconfirmed(504), the same as forms. No keys are ever typed. The herdr re-check moved intoopenCodePaneOn, which the form path uses too.null: wrong field types, a non-perid, or another session's request. Withnull, the pane keeps today's fallback card.--standalone,--server, OpenCode 1), so nothing was removed.The browser contract is unchanged:
kind: "approval",option_indexanswers, and newown_textkeys with ko/ja/zh entries. I did not touch the approval-hub files (server/claude-*.ts, the held-permission part ofprompt.ts,agent-launch.ts).CARD_BODY_MAX, the same limitfinishPromptcuts at).state.permissions, ordered byissued/appliedlike forms), so an older read that finishes late does not restore a request the TUI already answered. A request answered from the card leaves that list immediately.Screens (1280, light, Korean, isolated OpenCode 2.2.2)
Before (main): generic fallback card

After: permission card

Validation
server/opencode-answer.test.tsandserver/dialogs/opencode.test.tsfailed before the change and pass after it.server/fixtures/opencode-permission-2.2.2.json: realpermission.asked/permission.repliedevents from OpenCode 2.2.2 (model dsapi/deepseek-v4.1-flash, isolated HOME/XDG) forshell,edit,webfetchand an MCP tool (ovisfix_echo). The directory is scrubbed.bun run check fastat the head (short TMPDIR): all green, unit tests 24095 pass, 0 fail.2.unknownversion, a service that becomes 3.0.0 between the binding check and the reply, and an older read that finishes after a newer one.ovis-testherdr session, with an Ovis server from this branch. In the browser:touch allowed.txt→ 한 번 허용 →204, file created.touch denied.txt→ 거부 → answer 200, TUI shows "The user declined this tool call", no file.touch terminal.txt→ Enter in the TUI → the card was gone 123 ms later and the file was created.Notes
approvalScopecue for every "Deny" option, and omp's card has it too. Not changed here.