Broker chat from an operator's own OpenCode endpoint and adopt existing sessions

Add an opt-in way to broker chat from an operator-supplied OpenCode service
instead of the instance the server spawns for itself, and a way to adopt a
session that already exists on that endpoint so it appears in chat_sessions and
can be continued from the app.

- Endpoint selection: the `-opencode-endpoint` server flag (defaulting to
  `PIPELINES_OPENCODE_ENDPOINT`) opts in; precedence is explicit flag >
  explicit environment > the endpoint recorded on a previous run, with the
  spawned instance as the default. opencode.OpenEndpoint validates the
  reference once (http(s), host, no userinfo/query/fragment/path), detects the
  backend from its own /api/info (v2) or /global/health (v1) contract, and
  requires a streamable text/event-stream event path. It owns no process
  (Close is a no-op) and never calls startServe; every failure names the
  endpoint and disables chat rather than silently falling back. An
  authenticated endpoint takes an optional password only through the private
  file PIPELINES_OPENCODE_ENDPOINT_PASSWORD_FILE, with
  PIPELINES_OPENCODE_ENDPOINT_USERNAME overriding the detected default.
- Persistence: the validated choice is recorded in an additive single-row
  server_settings table so a restart with no flag/environment reuses it; an
  unreachable recorded endpoint disables chat with a clear message and no
  fallback.
- chat_adopt: a new protocol request (ChatAdoptRequest) with handleChatAdopt in
  internal/server/chat.go. It refuses a nil broker/registry/payload and a
  blank or foreign-shaped id before any endpoint call, reads the endpoint via
  the shared Chat.Session (which reuses Sessions/decodeChatSessions), records
  through the existing RecordOperatorSession with the endpoint's directory and
  title verbatim, and echoes the adopted session plus its registry row like
  chat_create. A missing id is refused naming the id and records nothing; an
  endpoint record with an empty directory is refused rather than substituting
  the server's working directory or home.
- CLI: `pipelines chat-adopt <session-id> [--json]` (usage, notes, help
  inventory, dispatch) with docs/cli.md updated under Chat sessions.
- Autostart preserves the endpoint variables (and the password-file reference,
  never an inline secret) via serviceEnvironmentKeys.

Checks before committing:
- Package location: the change is user-facing chat behaviour, so the packaged
  authoring skill reference internal/author/skill/references/operations.md is
  updated in the same change (Brokered OpenCode chat section) with a dated
  verification-evidence entry and the not-deployed state. No new reference file
  is added, so internal/author/install.go's managed inventory is unchanged.
- Packaged templates: this repository packages no template set for this
  behaviour and none references the chat endpoint, so no template needs
  updating.
- Verification: `go build ./...`, `go vet` on the touched packages, `gofmt -l`
  clean and `go test ./...` all pass. The unit tests cover the endpoint
  validation/precedence, the adopt happy path and refusals, the recorded
  endpoint round-trip, the protocol round-trip and the CLI dispatch. The live
  scratch-server check against the operator's real endpoint was not run in this
  change.

Nothing unrelated is staged; docs left stale (README and other shared documents)
are not edited here and are for a single folding writer.
This commit is contained in:
2026-10-17 20:14:11 +01:00
parent 50af9dbc62
commit 1f75e4797e
20 changed files with 1027 additions and 33 deletions
@@ -277,6 +277,25 @@ events it does not surface before decoding their data, so an unrelated event
whose `delta` has a different shape (`session.instructions.updated` carries an
object) never fails the stream.
The operator can instead broker chat from their own OpenCode service with the
opt-in `-opencode-endpoint` flag or `PIPELINES_OPENCODE_ENDPOINT`, with precedence
flag > environment > the endpoint recorded on a previous run and the spawned
instance as the default. `opencode.OpenEndpoint` validates the reference once
(the established http(s)/host/no-credentials/no-query/no-path idiom), detects the
backend from the service's own `/api/info` (V2) or `/global/health` (native)
contract and requires a streamable `text/event-stream` event path; it returns a
connection with **no owned process** and never calls `startServe`. A malformed
reference, an unreachable host, a reachable non-OpenCode service or a
health-ok-but-no-SSE service each disable chat with an error naming the endpoint
and never fall back to the spawned instance. An authenticating endpoint accepts an
optional password only through the private file
`PIPELINES_OPENCODE_ENDPOINT_PASSWORD_FILE` (regular, 0600, single-owner), and
`PIPELINES_OPENCODE_ENDPOINT_USERNAME` overrides the detected default user. The
validated choice is recorded in the additive `server_settings` table so a restart
with no flag/environment reuses it; an unreachable recorded endpoint disables chat
with a clear message and no fallback. `pipelines service enable` preserves the
endpoint and the password-file reference in autostart.
`chat_sessions` lists the operator's own chat sessions, not the sessions pipeline
runs create. Operator sessions are recorded in the dedicated `operator_sessions`
table (`session_id` primary key, plus `directory`, `title`, `tag`, `created_at`,
@@ -311,6 +330,20 @@ listing sessions and `chat_sessions` stays a pure, fail-closed session list. A
successful `chat_prompt` touches the session's `last_used_at` in one UPDATE, so it
moves to the front of the list; a non-operator session id touches no row.
`chat_adopt` records a session that already exists on the configured endpoint in
the same operator registry, using the endpoint's own directory and title
verbatim (never a guess, the server's working directory or the home directory).
It reads the endpoint once through the shared `Chat.Session` (which reuses
`Sessions`/`decodeChatSessions`), records through the existing
`RecordOperatorSession` with the `operator` tag, and echoes the adopted session
plus its registry row like `chat_create`, so `chat_sessions` lists it and
`chat_prompt` continues it. A nil payload, a blank or foreign-shaped id, a nil
broker or a nil registry is refused before any endpoint call; an id the endpoint
does not report is refused naming the id and records nothing; an endpoint record
with an empty directory is refused rather than substituting a directory. The CLI
equivalent is `pipelines chat-adopt <session-id>` (`--json` prints the full
response, otherwise a short `Adopted <id> · <directory>` line).
While a `chat_prompt` subscription is open the server also polls
`GET /api/permission/request` on a fixed small interval (first poll immediately) and
pushes one `chat_permission` frame per newly pending approval request, deduped by
@@ -352,6 +385,23 @@ Verified against the installed OpenCode 2.0.20 on 2026-10-17:
- Not verified live: a genuine pending-approval round trip and any backend other
than the installed 2.0.20. The permission wire shapes are unit-tested over an
httptest fixture, not against a live pending request.
- Operator-supplied endpoint selection and `chat_adopt` (added 2026-10-17):
`opencode.OpenEndpoint` and the flag/env/recorded precedence, the URL-shape
validation failures (scheme, empty host, userinfo, query, fragment, non-empty
path), the unreachable/non-OpenCode/no-SSE/auth-mismatch failures, the
no-owned-process property (`conn.process == nil`) and the password-file rule
are unit-tested over httptest fixtures in `internal/opencode/endpoint_test.go`.
`chat_adopt` (endpoint directory/title recorded with the `operator` tag, echo,
missing id refused naming the id and recording nothing, nil payload/blank id/nil
broker/nil registry refused before any endpoint call, empty endpoint directory
refused) is unit-tested over the server fake broker in
`internal/server/chat_test.go`; the recorded-endpoint round-trip across a store
Close/Open is in `internal/pipeline/settings_test.go`; the protocol round-trip
and the CLI dispatch (`pipelines chat-adopt`) are covered in
`internal/protocol/chat_test.go` and `internal/cli/chat_adopt_test.go`. The
live scratch-server check against the operator's real endpoint (adopt, list,
prompt) was **not** run in this change; only the unit fixtures above were
executed.
Deployment state: not deployed. This is reviewed and committed locally only;
deploying it (and restarting the server) is the operator's step. Do not describe