|
| 1 | +# OpenCode's server surface, measured for the driver |
| 2 | + |
| 3 | +2026-08-23, OpenCode 1.18.19 (`/home/schickling/.nix-profile/bin/opencode`), Linux, isolated |
| 4 | +`XDG_DATA_HOME`/`XDG_CONFIG_HOME`, headless `opencode serve --port 43123 --print-logs`. The free |
| 5 | +anonymous model (`opencode/big-pickle`) answered prompts with no credentials, so every claim below |
| 6 | +is reproducible without an API key. |
| 7 | + |
| 8 | +## What was established |
| 9 | + |
| 10 | +**The TUI is a server.** `opencode` (TUI, the default command) starts a server on `--port` / |
| 11 | +`--hostname` exactly like `opencode serve`; `opencode attach <url>` exists for the reverse |
| 12 | +direction. A driver therefore launches the interactive seat with a wrapper-allocated loopback port |
| 13 | +and speaks HTTP to its own child — no screen scraping anywhere in the driver path. |
| 14 | + |
| 15 | +**Observation** rides `GET /event` (SSE). First event is `server.connected`, `server.heartbeat` is |
| 16 | +periodic, and a connect replays ~45 `plugin.added` events — subscribers must tolerate noise and |
| 17 | +duplicates. The API self-describes at `GET /doc` (OpenAPI 3.1, 94 event schemas). Measured and |
| 18 | +schema-verified signals, as projected by the driver: |
| 19 | + |
| 20 | +- `session.status` with a three-arm status union `busy | idle | retry` (measured firing at turn |
| 21 | + start and end; `retry` carries `attempt`/`next`/`message`); |
| 22 | +- `session.idle` fires beside the idle status (measured); |
| 23 | +- `permission.asked` / `permission.replied` and `question.asked` / `question.replied|rejected` |
| 24 | + carry stable `^per` / `^que` ids — the blocked-on-human exit edge is id-matched, with none of the |
| 25 | + Claude batching ambiguity (schema-verified; a live `permission.asked` capture is still owed — |
| 26 | + with `{"permission":{"bash":"ask"}}` PATCHed into config, the free model's bash ran without |
| 27 | + asking in one run and emitted no tool part in another); |
| 28 | +- `session.error` is an eight-arm union; `ProviderAuthError` is terminal for the seat, the others |
| 29 | + leave the session promptable; |
| 30 | +- `GET /session/status` returns `{sessionID: status}` and **omits idle sessions** — measured `{}` |
| 31 | + when idle, so absence-of-key is the idle proof only over a proven-live server. |
| 32 | + |
| 33 | +**Delivery** is `POST /session/{id}/prompt_async` (measured: returns 200 immediately, empty body), |
| 34 | +which accepts a caller-supplied `messageID` (`^msg`) — idempotent and receipt-correlatable. The |
| 35 | +receipt is the message read back (`GET /session/{id}/message/{messageID}` / the `message.updated` |
| 36 | +event). Prompts sent mid-turn queue natively. **`/tui/append-prompt` and `/tui/submit-prompt` |
| 37 | +returned `true` on a headless server with no TUI attached** — they are broadcast, not receipt, and |
| 38 | +must never count as delivery. Auth is `OPENCODE_SERVER_PASSWORD` + basic auth (user `opencode`), |
| 39 | +unsecured by default on loopback. |
| 40 | + |
| 41 | +**Sessions** are `ses_*`; storage moved to sqlite (`$XDG_DATA_HOME/opencode/opencode.db`, |
| 42 | +`opencode db` exists) — the API is the only sane read path. Exact resume is `--session <id>` / |
| 43 | +`--continue`, forking is `--fork`. No native incarnation concept: st2's runtime generation, the |
| 44 | +pinned port, and the wrapper pid supply fencing. |
| 45 | + |
| 46 | +**Pinning**: `opencode --version` prints the bare version; `Session.version` also rides the wire. |
| 47 | +Because the server serves its own OpenAPI document, a live `/doc` subset check at wrapper start |
| 48 | +covers the shape while a version list covers the semantics — the hybrid of the Codex |
| 49 | +`SUPPORTED_CODEX_CLI_VERSIONS` pattern and the pi type-check pattern. |
| 50 | + |
| 51 | +## Reproduction |
| 52 | + |
| 53 | +``` |
| 54 | +XDG_DATA_HOME=$S/data XDG_CONFIG_HOME=$S/config opencode serve --port 43123 --print-logs |
| 55 | +curl -s http://127.0.0.1:43123/doc | jq '.paths | keys' |
| 56 | +curl -sN http://127.0.0.1:43123/event # SSE capture |
| 57 | +curl -s -XPOST http://127.0.0.1:43123/session # create ses_… |
| 58 | +curl -s -XPOST http://127.0.0.1:43123/session/<id>/prompt_async \ |
| 59 | + -H 'content-type: application/json' \ |
| 60 | + -d '{"messageID":"msg0000000000000000000000000","parts":[{"type":"text","text":"hi"}]}' |
| 61 | +curl -s http://127.0.0.1:43123/session/<id>/message/msg0000000000000000000000000 |
| 62 | +curl -s http://127.0.0.1:43123/session/status # {} idle · {"ses_…":{"type":"busy"}} mid-turn |
| 63 | +curl -s -XPOST http://127.0.0.1:43123/tui/append-prompt -d '{"text":"x"}' # true, no TUI attached |
| 64 | +``` |
| 65 | + |
| 66 | +Original captures: session `ses_fd078983affefGxfpkGr2u44LJ`, files `serve.log`, `openapi.json`, |
| 67 | +`events{,2,3,4}.sse`, `session.json`, `prompt-response.json` (session scratchpad, not committed). |
| 68 | + |
| 69 | +## Follow-up capture: the blocked-on-human pairs, live (2026-08-23, second run) |
| 70 | + |
| 71 | +The permission prompt fires headless after all — the first run's failure was the *write path*, not |
| 72 | +the surface: permissions set via `PATCH /config` did not take effect for asks, while the same |
| 73 | +`{"permission":{"bash":"ask","edit":"ask","webfetch":"ask"}}` in `$XDG_CONFIG_HOME/opencode/ |
| 74 | +opencode.json` asks reliably with the free model and no TUI. |
| 75 | + |
| 76 | +Reproduction (isolated env as above, port 43217; session `ses_fd0241376ffe3KDznnEB55qvKi`): |
| 77 | + |
| 78 | +``` |
| 79 | +# config file (not PATCH) carries the ask settings, then: |
| 80 | +curl -s -XPOST :43217/session/<id>/prompt_async -d '{"parts":[{"type":"text", |
| 81 | + "text":"Use the bash tool to run exactly: echo capture-test-42. Do not answer without running it."}]}' |
| 82 | +curl -s :43217/permission # pending: [{"id":"per_02fdc246b001BB5pclAd62tzpJ","permission":"bash",…}] |
| 83 | +curl -s -XPOST :43217/permission/per_…/reply -d '{"reply":"once"}' # → true; pending clears; turn completes |
| 84 | +# question: prompt "you MUST use your question tool…", then |
| 85 | +curl -s :43217/question # pending: [{"id":"que_02fdd3e83001GwptE1fgJam0jB",…}] |
| 86 | +curl -s -XPOST :43217/question/que_…/reply -d '{"answers":[["Yes"]]}' |
| 87 | +``` |
| 88 | + |
| 89 | +Captured event frames (verbatim, now fixture tests in `src/opencode_session.rs`): |
| 90 | + |
| 91 | +``` |
| 92 | +data: {"id":"evt_02fdc246b0020Xw65txB3nXBC4","type":"permission.asked","properties":{"id":"per_02fdc246b001BB5pclAd62tzpJ","sessionID":"ses_fd0241376ffe3KDznnEB55qvKi","permission":"bash","patterns":["echo capture-test-42"],"metadata":{"command":"echo capture-test-42"},"always":["echo *"],"tool":{"messageID":"msg_02fdc0989001nfz93uTCTLeO6O","callID":"call_6614fd927fe74d86ab089078"}}} |
| 93 | +data: {"id":"evt_02fdc8342001TQBwhszchZw1U6","type":"permission.replied","properties":{"sessionID":"ses_fd0241376ffe3KDznnEB55qvKi","requestID":"per_02fdc246b001BB5pclAd62tzpJ","reply":"once"}} |
| 94 | +data: {"type":"question.asked","properties":{"id":"que_02fdd3e83001GwptE1fgJam0jB",…}} |
| 95 | +data: {"type":"question.replied","properties":{"sessionID":"…","requestID":"que_02fdd3e83001GwptE1fgJam0jB","answers":[["Yes"]]}} |
| 96 | +``` |
| 97 | + |
| 98 | +**Two corrections to the schema-derived design, both shipped:** |
| 99 | + |
| 100 | +1. **Exit events spell the id `requestID`.** Entry events carry `properties.id`; `permission.replied` |
| 101 | + and `question.replied|rejected` carry `properties.requestID`. The extraction that only knew `/id` |
| 102 | + would have held `blockedOn: human` forever after a real grant. |
| 103 | +2. **`GET /event` over HTTP/1.1 is `Transfer-Encoding: chunked`** — chunk-size lines interleave into |
| 104 | + the line-oriented SSE read and a `data:` line can split across chunks (silent event loss). The |
| 105 | + same server streams raw SSE over an HTTP/1.0 request, so the producer requests HTTP/1.0. |
| 106 | + JSON endpoints (`/config`, and `/doc` at 478 KB) responded `Content-Length` in every probe, so |
| 107 | + the one-shot request path is unaffected. |
| 108 | + |
| 109 | +Also measured while live: `prompt_async` with a repeated caller `messageID` yields **one** user |
| 110 | +message (read-back receipt correlation holds; no duplicate delivery), but the second POST appends |
| 111 | +its `parts` again into that message — a resend after a *transiently failed* read-back duplicates |
| 112 | +text inside the message, not the message. The pump's read-back-before-resend rule is therefore |
| 113 | +load-bearing, not just polite. |
| 114 | + |
| 115 | +## Limits |
| 116 | + |
| 117 | +- A v2 surface (`/api/event`, `/api/session/{id}/wait`, `permission.v2.*`) coexists with the |
| 118 | + legacy one probed here; the driver pins the legacy arms via the `/doc` check. |
| 119 | +- Docs move fast (the site showed "Last updated Aug 23, 2026"); the `/doc` gate is the defense. |
| 120 | +- The chunked/1.0 behavior and the `requestID` spelling are measured on 1.18.19 only; both sit |
| 121 | + behind `SUPPORTED_OPENCODE_VERSIONS` and the `/doc` subset gate. |
| 122 | + |
| 123 | +## VRS Impact |
| 124 | + |
| 125 | +Resolves `DQ-H6` in full: the OpenCode producer is evented (server SSE), uniquely offers an |
| 126 | +id-matched blocked-on-human exit edge, and both blocked pairs are now captured live with the two |
| 127 | +wire corrections above landed as code plus verbatim fixture tests. Feeds the OpenCode producer |
| 128 | +section of `spec.md` (mapping table, aggregate-session rule, receipt semantics, the two-gate |
| 129 | +fail-closed rule) and requirement `OHS-R08`. |
0 commit comments