From 1f75e4797edbe37a43d1b3bb1ec0400405b50461 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 17 Oct 2026 20:14:11 +0100 Subject: [PATCH] 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 [--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. --- cmd/pipelines-server/main.go | 41 ++- docs/cli.md | 34 +++ .../author/skill/references/operations.md | 50 ++++ internal/cli/app.go | 17 ++ internal/cli/chat_adopt_test.go | 58 ++++ internal/cli/commands.go | 2 + internal/cli/help.go | 1 + internal/installation/service.go | 4 +- internal/opencode/chat.go | 21 ++ internal/opencode/connection.go | 41 ++- internal/opencode/endpoint.go | 153 ++++++++++ internal/opencode/endpoint_test.go | 267 ++++++++++++++++++ internal/pipeline/settings.go | 43 +++ internal/pipeline/settings_test.go | 42 +++ internal/pipeline/store.go | 4 + internal/protocol/chat.go | 16 +- internal/protocol/chat_test.go | 5 +- internal/protocol/message.go | 1 + internal/server/chat.go | 60 ++++ internal/server/chat_test.go | 200 +++++++++++++ 20 files changed, 1027 insertions(+), 33 deletions(-) create mode 100644 internal/cli/chat_adopt_test.go create mode 100644 internal/opencode/endpoint.go create mode 100644 internal/opencode/endpoint_test.go create mode 100644 internal/pipeline/settings.go create mode 100644 internal/pipeline/settings_test.go diff --git a/cmd/pipelines-server/main.go b/cmd/pipelines-server/main.go index 1771635d..48cb00ae 100644 --- a/cmd/pipelines-server/main.go +++ b/cmd/pipelines-server/main.go @@ -21,6 +21,11 @@ import ( func main() { addr := flag.String("addr", "127.0.0.1:8080", "HTTP/WebSocket listen address") + // -opencode-endpoint opts in to brokering chat from an operator-supplied + // OpenCode service instead of the spawned default. Its default is the + // environment variable, so both are the same opt-in; the recorded choice (set + // by a previous run) is used when neither is present. + endpoint := flag.String("opencode-endpoint", os.Getenv(opencode.EndpointEnvKey), "Broker chat from this OpenCode endpoint instead of spawning one") defaultDB, err := paths.Database() if err != nil { log.Fatal(err) @@ -42,14 +47,38 @@ func main() { // harness; the summariser falls back to the prompt's first line, so title // generation never blocks or fails run creation. options := server.Options{ArtifactsRoot: artifactsRoot, RunTitles: summarise.GenerateTitle, StepVerdicts: summarise.GenerateVerdictReason} - // Chat brokers OpenCode over one owned endpoint. OpenChat detects the + // Chat brokers OpenCode over one endpoint. By default OpenChat detects the // installed CLI and starts the single owned serve process for it: the native // wire for v1, and the V2 API for the installed V2 CLI (which exposes the - // authenticated SSE firehose at /api/event). Only a URL-bearing, - // SSE-capable connection enables chat; otherwise chat stays disabled and - // every chat_* request is answered with a protocol error naming the missing - // configuration rather than hanging. - if conn, err := opencode.OpenChat(ctx, opencode.Executable(), ""); err != nil { + // authenticated SSE firehose at /api/event). When the operator configured an + // endpoint (flag, environment or a previously recorded choice) chat is + // brokered from that endpoint instead: it is validated once, an unreachable or + // non-OpenCode endpoint is a clear refusal and never falls back to a spawned + // instance, and nothing is started locally. Only a URL-bearing, SSE-capable + // connection enables chat; otherwise chat stays disabled and every chat_* + // request is answered with a protocol error naming the missing configuration + // rather than hanging. + recorded := "" + if value, err := store.OpenCodeEndpoint(ctx); err != nil { + log.Printf("OpenCode endpoint setting unreadable: %v", err) + } else { + recorded = value + } + reference := opencode.ResolveEndpointRef(*endpoint, os.Getenv(opencode.EndpointEnvKey), recorded) + if reference != "" { + password, err := opencode.ReadEndpointCredentialFile(os.Getenv(opencode.EndpointPasswordFileEnvKey)) + if err != nil { + log.Printf("OpenCode chat unavailable: %v", err) + } else if conn, err := opencode.OpenEndpoint(ctx, reference, password); err != nil { + log.Printf("OpenCode chat unavailable: %v", err) + } else { + options.Chat = opencode.NewChat(conn) + defer conn.Close() + if err := store.RecordOpenCodeEndpoint(ctx, reference); err != nil { + log.Printf("record OpenCode endpoint: %v", err) + } + } + } else if conn, err := opencode.OpenChat(ctx, opencode.Executable(), ""); err != nil { log.Printf("OpenCode chat unavailable: %v", err) } else if conn.URL == "" { conn.Close() diff --git a/docs/cli.md b/docs/cli.md index 7f8ab4ef..026199bb 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -314,6 +314,40 @@ sessions pipeline runs create. The protocol types are: (currently 8). It is a dedicated request so the create dialog can load the choices without first listing sessions; `chat_sessions` stays a pure session list. +- `chat_adopt` takes a `session_id` that already exists on the configured + endpoint and records it in the operator session registry, using the endpoint's + own directory and title (never a guess or the server's working directory). The + response echoes the adopted session as `chat_session` and, like `chat_create`, + its registry row in `chat_sessions`, so `chat_sessions` then lists it and + `chat_prompt` can continue it. A nil payload or an invalid/blank id 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 the home or working directory. The CLI + equivalent is `pipelines chat-adopt ` (`--json` for the full + response; otherwise a short `Adopted · ` line). + +### Choosing the endpoint + +By default chat brokers from the single `opencode serve` instance the server +spawns for itself, unchanged. To broker from the operator's own OpenCode service +instead, set the opt-in `-opencode-endpoint` server flag or the +`PIPELINES_OPENCODE_ENDPOINT` environment variable. Precedence is explicit flag, +then explicit environment, then the endpoint recorded on a previous run; with +none set the spawned default is used. The reference must be an http(s) URL with a +host and no credentials, query, fragment or path. It is validated once at +startup: the backend is detected from the service's own `/api/info` (V2) or +`/global/health` (native) contract, and a streamable `text/event-stream` event +path is required. A malformed reference, an unreachable host, a reachable +non-OpenCode service, or a healthy service with no SSE each disable chat with an +error naming the endpoint — never a silent fallback to another instance. An +endpoint that authenticates accepts an optional password through the private file +`PIPELINES_OPENCODE_ENDPOINT_PASSWORD_FILE` (a regular 0600 file; never an +inline secret), and `PIPELINES_OPENCODE_ENDPOINT_USERNAME` overrides the detected +default user. The validated choice is recorded so a restart with no flag or +environment keeps brokering from the same place; an unreachable recorded endpoint +disables chat with a clear message and no fallback. The endpoint choice is +preserved by `pipelines service enable` autostart (the variables, with the +password file as a reference). The stored tag is exactly `operator`; the display label `Operator` is derived from it, so there is no second tag vocabulary. Chat uses the server's one OpenCode diff --git a/internal/author/skill/references/operations.md b/internal/author/skill/references/operations.md index ab000c95..cc7e1d65 100644 --- a/internal/author/skill/references/operations.md +++ b/internal/author/skill/references/operations.md @@ -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 ` (`--json` prints the full +response, otherwise a short `Adopted · ` 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 diff --git a/internal/cli/app.go b/internal/cli/app.go index d8caf7be..ca5f4796 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -955,6 +955,23 @@ func (a App) Run(ctx context.Context, args []string) error { } _, err = fmt.Fprintf(a.Out, "Renamed run #%d to %s\n", r.RunDetail.ID, r.RunDetail.Label()) return err + case "chat-adopt": + id, err := one() + if err != nil { + return err + } + r, err := call(protocol.Message{Type: protocol.ChatAdopt, ChatAdopt: &protocol.ChatAdoptRequest{SessionID: id}}) + if err != nil { + return err + } + if o.json { + return a.output(r) + } + if r.ChatSession == nil { + return fmt.Errorf("server did not return the adopted session") + } + _, err = fmt.Fprintf(a.Out, "Adopted %s · %s\n", r.ChatSession.ID, r.ChatSession.Directory) + return err case "rpc": path, err := one() if err != nil { diff --git a/internal/cli/chat_adopt_test.go b/internal/cli/chat_adopt_test.go new file mode 100644 index 00000000..94121b55 --- /dev/null +++ b/internal/cli/chat_adopt_test.go @@ -0,0 +1,58 @@ +package cli + +import ( + "bytes" + "context" + "encoding/json" + "strings" + "sync/atomic" + "testing" + "time" + + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/wstest" +) + +// TestCLIChatAdoptSendsOneRequest pins the CLI parity for chat_adopt: exactly one +// chat_adopt request carrying the id travels over the shared protocol client, +// --json prints the response, and the human line is concise. +func TestCLIChatAdoptSendsOneRequest(t *testing.T) { + var calls int32 + var got protocol.Message + session := protocol.ChatSession{ID: "ses_abc", Title: "mine", Directory: "/work/mine", Tag: protocol.ChatSessionTagOperator} + endpoint := wstest.Server(t, func(request protocol.Message) protocol.Message { + atomic.AddInt32(&calls, 1) + got = request + return protocol.Message{Type: protocol.ChatAdopt, ChatSession: &session, ChatSessions: []protocol.ChatSession{session}} + }) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + var out, errOut bytes.Buffer + app := App{Out: &out, Err: &errOut, In: strings.NewReader("")} + if err := app.Run(ctx, []string{"--url", endpoint, "chat-adopt", "ses_abc", "--json"}); err != nil { + t.Fatal(err) + } + if n := atomic.LoadInt32(&calls); n != 1 { + t.Fatalf("chat-adopt sent %d requests, want exactly one", n) + } + if got.Type != protocol.ChatAdopt || got.ChatAdopt == nil || got.ChatAdopt.SessionID != "ses_abc" { + t.Fatalf("wire request = %+v, want chat_adopt with the id", got) + } + var decoded protocol.Message + if err := json.Unmarshal(out.Bytes(), &decoded); err != nil { + t.Fatalf("stdout is not parseable JSON: %v\n%s", err, out.String()) + } + if decoded.ChatSession == nil || decoded.ChatSession.ID != "ses_abc" { + t.Fatalf("stdout lost the adopted session: %+v", decoded.ChatSession) + } + + var humanOut, humanErr bytes.Buffer + human := App{Out: &humanOut, Err: &humanErr, In: strings.NewReader("")} + if err := human.Run(ctx, []string{"--url", endpoint, "chat-adopt", "ses_abc"}); err != nil { + t.Fatal(err) + } + if !strings.Contains(humanOut.String(), "ses_abc") || !strings.Contains(humanOut.String(), "/work/mine") { + t.Fatalf("human line = %q, want the id and the endpoint directory", humanOut.String()) + } +} diff --git a/internal/cli/commands.go b/internal/cli/commands.go index aec2945f..2c22ba22 100644 --- a/internal/cli/commands.go +++ b/internal/cli/commands.go @@ -25,6 +25,7 @@ var usage = map[string]string{ "retry": "retry [--json [--wait [--wait-timeout DURATION]]]", "title": "title \"\" [--json]", "debug": "debug <run-id> [--json | --no-open]", + "chat-adopt": "chat-adopt <session-id> [--json]", "connection": "connection [--json]", "ping": "ping [--json]", "version": "version [--json]", @@ -52,6 +53,7 @@ var notes = map[string]string{ "decide": "Choose an option for a run waiting on a human stage. The run must be `waiting`; the option must be one of that stage's non-error edges. The stage is inferred from the run's current stage unless named. The choice and optional --feedback are recorded as the step's evidence (visible in inspect and the TUI) and the run resumes along the chosen edge. An unknown option is refused with the valid list; a repeated decision for an already-decided visit does not double-apply. Works across a server restart because the wait is persisted.", "debug": "Open a fresh configured harness conversation to investigate a run using inspect/show/runs CLI queries. --harness opencode|pi|nvim chooses another configured harness or read-only nvim packet. Does not resume implementation or retry. --json returns the prompt without launching anything.", "rpc": "Accepts a JSON protocol envelope; '-' reads stdin. Every TUI server operation is available here too. Mutating commands are not dry runs. The omni-search command is `search` (fields query, optional kind and limit) and answers with ranked `search_results`.", + "chat-adopt": "Adopt a session that already exists on the configured OpenCode endpoint so it appears in chat_sessions and can be continued. The id must be one the endpoint reports; its directory and title are taken from the endpoint's own record (never guessed), and a missing id records nothing. Adopting is always explicit and does not change the server's endpoint choice.", "retention": "Local, explicit run-history retention; never automatic and never a side effect of another command. `report` is strictly read-only and prints what a policy would remove; `apply` removes it after approval (--yes for scripted use). Policy: --older-than DURATION selects runs created before now minus that rolling UTC duration (Go durations plus d/w; not a calendar boundary), --keep N keeps the newest N runs by run id and selects older ranks (--keep 0 keeps none). Both together are the conservative intersection; at least one is required. Protected and never removed: queued/running runs, runs waiting on a person (waiting run or step), runs with an active cleanup agent, and any run referenced by a retained run's retry provenance (transitively). A run that becomes protected before deletion aborts the whole apply. Artifacts at <root>/<run> are removed with their run and kept with protected/retained runs; the database is VACUUMed and its WAL checkpointed, with measured reclaimed bytes. Definitions and versions are immutable and never touched. --db must name an existing database (default: the standard data directory); --artifacts defaults to artifacts/ beside --db. Works without a running server.", "search": "One read-only query over pipelines, versions, runs, steps, field text and artifacts. QUERY is free text, a bare status word (queued, running, succeeded, failed, canceled, interrupted) selecting those runs, or a lone date expression (today, yesterday, this week [Monday 00:00 local], or YYYY-MM-DD) selecting the runs created in that window. --type is the only narrowing mechanism and takes one kind: pipeline, version, run, step, field or artifact; an unknown kind is an error. Ranking: an exact name match (3) beats a name substring (2) beats a body-text match (1); equal tiers break by recency, then a stable kind/target tie-break, and every hit reports score, matched_field and recency so a client can re-rank. Bounds: the result cap defaults to 50 and clamps to 200 (0 = default); every kind scans at most 2000 rows and field-text scanning is additionally capped at 2000 steps / 1 MiB; Truncated plus scanned_steps, scanned_bytes and scan_bound_bytes report when a bound applied. Targets are stable: pipeline:<id>, pipeline:<id>:v<version>, run:<id>, run:<id>:<sequence>, run:<id>:<sequence>:<field>, or an artifact URL.", } diff --git a/internal/cli/help.go b/internal/cli/help.go index 7f7f6f7c..309eac5f 100644 --- a/internal/cli/help.go +++ b/internal/cli/help.go @@ -31,6 +31,7 @@ EXECUTE retry <run-id> Restart a failed run with its original input and directory title <run-id> "<title>" Rename a run after the fact; input/steps unchanged debug <run-id> Investigate a run with a configured harness or nvim + chat-adopt <session-id> Adopt an existing OpenCode session into chat_sessions draft-delete <draft.md> Delete a selected retained draft and its metadata connection Server and public phone connection (no secrets) status Server/ntfy health, ports and server computer IPs diff --git a/internal/installation/service.go b/internal/installation/service.go index 69ed95b8..bc489906 100644 --- a/internal/installation/service.go +++ b/internal/installation/service.go @@ -78,7 +78,7 @@ func (d serviceDefinition) render(platform string) (string, error) { // Only explicitly supported settings are captured, never the complete environment // or provider credentials. File-based notification credentials remain references. -var serviceEnvironmentKeys = []string{"PATH", "HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "XDG_CACHE_HOME", "PIPELINES_OPENCODE", "PIPELINES_NTFY_URL", "PIPELINES_NTFY_PUBLIC_URL", "PIPELINES_NTFY_TOKEN_FILE"} +var serviceEnvironmentKeys = []string{"PATH", "HOME", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_STATE_HOME", "XDG_CACHE_HOME", "PIPELINES_OPENCODE", "PIPELINES_OPENCODE_ENDPOINT", "PIPELINES_OPENCODE_ENDPOINT_PASSWORD_FILE", "PIPELINES_OPENCODE_ENDPOINT_USERNAME", "PIPELINES_NTFY_URL", "PIPELINES_NTFY_PUBLIC_URL", "PIPELINES_NTFY_TOKEN_FILE"} func envValues(env map[string]string) []string { var values []string @@ -224,7 +224,7 @@ func ServiceCommand(ctx context.Context, executable string, args []string, out, d.Environment[key] = value } } - for _, key := range []string{"PIPELINES_OPENCODE", "PIPELINES_NTFY_TOKEN_FILE"} { + for _, key := range []string{"PIPELINES_OPENCODE", "PIPELINES_OPENCODE_ENDPOINT_PASSWORD_FILE", "PIPELINES_NTFY_TOKEN_FILE"} { if value := d.Environment[key]; value != "" && (key != "PIPELINES_OPENCODE" || strings.ContainsRune(value, filepath.Separator)) { absolute, err := filepath.Abs(value) if err != nil { diff --git a/internal/opencode/chat.go b/internal/opencode/chat.go index e91efe0f..41a7b25c 100644 --- a/internal/opencode/chat.go +++ b/internal/opencode/chat.go @@ -154,6 +154,27 @@ func (c *Chat) Create(ctx context.Context, title, directory string) (protocol.Ch return decodeChatSession(data) } +// Session returns the endpoint's own record of one session so a caller can adopt +// an existing session with the directory and title the endpoint reports, never a +// guessed directory. found is false when the endpoint does not list id. It +// reuses Sessions/decodeChatSessions, so no second session decoder or endpoint is +// introduced; it performs at most one bounded endpoint read. +func (c *Chat) Session(ctx context.Context, id string) (protocol.ChatSession, bool, error) { + if !ValidSessionID(id) { + return protocol.ChatSession{}, false, fmt.Errorf("invalid OpenCode session id") + } + sessions, err := c.Sessions(ctx) + if err != nil { + return protocol.ChatSession{}, false, err + } + for _, session := range sessions { + if session.ID == id { + return session, true, nil + } + } + return protocol.ChatSession{}, false, nil +} + // Reply posts a permission decision, mirroring the documented endpoint. func (c *Chat) Reply(ctx context.Context, request protocol.ChatReplyRequest) error { if !sessionID.MatchString(request.SessionID) { diff --git a/internal/opencode/connection.go b/internal/opencode/connection.go index 2b63eae0..9a868d84 100644 --- a/internal/opencode/connection.go +++ b/internal/opencode/connection.go @@ -28,16 +28,20 @@ type Connection struct { Backend, Executable, Directory string URL string password string - process *exec.Cmd - done chan error - models map[string]map[string]string - responses map[string][]byte - formatChecked bool - formatSupported bool - FormatProbe *FormatProbe - LastHTTPStatus int - LastHTTPOperation string - MessageReadbackDiagnostic string + // username overrides the backend-default basic-auth user for an + // operator-supplied endpoint (PIPELINES_OPENCODE_ENDPOINT_USERNAME). It is + // empty for the owned serve connection, which keeps the detected default. + username string + process *exec.Cmd + done chan error + models map[string]map[string]string + responses map[string][]byte + formatChecked bool + formatSupported bool + FormatProbe *FormatProbe + LastHTTPStatus int + LastHTTPOperation string + MessageReadbackDiagnostic string } // Reference records only our ephemeral loopback service credentials, never provider auth. @@ -204,6 +208,9 @@ func newConnection(ctx context.Context, executable, directory, backend string) ( // authenticates the fixed "opencode" user regardless of // OPENCODE_SERVER_USERNAME, verified against the installed 2.0.20 binary. func (c *Connection) serverUser() string { + if c.username != "" { + return c.username + } if c.Backend == "v2" { return "opencode" } @@ -280,22 +287,12 @@ func (c *Connection) startServe(ctx context.Context) error { // healthy one. func (c *Connection) checkHealth(ctx context.Context) error { if c.Backend == "v2" { - info, err := c.Native(ctx, "get", "/api/info", nil) - var result struct { - Version string `json:"version"` - URLs []string `json:"urls"` - } - if err != nil || json.Unmarshal(info, &result) != nil || result.Version == "" { + if err := c.probeHealth(ctx, "v2"); err != nil { return fmt.Errorf("OpenCode serve did not return the expected V2 info contract") } return nil } - health, err := c.Native(ctx, "get", "/global/health", nil) - var result struct { - Healthy bool `json:"healthy"` - Version string `json:"version"` - } - if err != nil || json.Unmarshal(health, &result) != nil || !result.Healthy || result.Version == "" { + if err := c.probeHealth(ctx, "v1"); err != nil { return fmt.Errorf("OpenCode serve did not return the expected native health contract") } return nil diff --git a/internal/opencode/endpoint.go b/internal/opencode/endpoint.go new file mode 100644 index 00000000..9828c492 --- /dev/null +++ b/internal/opencode/endpoint.go @@ -0,0 +1,153 @@ +package opencode + +import ( + "context" + "encoding/json" + "fmt" + "net/url" + "os" + "strings" + "syscall" + "time" +) + +// Environment keys for the operator-supplied OpenCode endpoint. The endpoint +// itself is opt-in: when none of the flag, these variables or the recorded +// setting is present the server keeps today's spawned default. The password is +// accepted only through a private file reference (never inline), mirroring the +// ntfy token-file rule; an optional username overrides the detected default. +const ( + EndpointEnvKey = "PIPELINES_OPENCODE_ENDPOINT" + EndpointPasswordFileEnvKey = "PIPELINES_OPENCODE_ENDPOINT_PASSWORD_FILE" + EndpointUsernameEnvKey = "PIPELINES_OPENCODE_ENDPOINT_USERNAME" +) + +// endpointProbeTimeout bounds each startup probe (health and SSE) so an +// unreachable endpoint fails fast instead of hanging server startup. +const endpointProbeTimeout = 5 * time.Second + +// endpointPasswordFileLimit bounds the credential file read. +const endpointPasswordFileLimit = 4096 + +// ResolveEndpointRef picks the operator's OpenCode endpoint reference by +// precedence: explicit flag > explicit environment > the endpoint recorded on a +// previous run. An empty result means no endpoint is configured, so the caller +// keeps today's spawned-instance default. It is the single precedence resolver +// used by the server and its tests so the choice cannot drift. +func ResolveEndpointRef(flagValue, envValue, recorded string) string { + for _, value := range []string{flagValue, envValue, recorded} { + if value = strings.TrimSpace(value); value != "" { + return value + } + } + return "" +} + +// NormalizeEndpointURL validates an operator-supplied endpoint the way the other +// endpoint references are validated: an http(s) URL with a non-empty host, no +// userinfo, no query, no fragment and an empty or "/" path. Anything else is a +// clear error naming the endpoint rather than a silently guessed base. +func NormalizeEndpointURL(raw string) (string, error) { + u, err := url.Parse(strings.TrimSpace(raw)) + if err != nil || u.Hostname() == "" || (u.Scheme != "http" && u.Scheme != "https") || + u.User != nil || u.RawQuery != "" || u.Fragment != "" || (u.Path != "" && u.Path != "/") { + return "", fmt.Errorf("invalid OpenCode endpoint %q: use an http(s) URL with a host and no credentials, query, fragment or path", raw) + } + return strings.TrimRight(u.String(), "/"), nil +} + +// ReadEndpointCredentialFile reads the optional endpoint password from a private +// file reference, mirroring the ntfy token-file rule: a regular, single-owner +// 0600 file with a bounded read. An empty path yields no password, so an endpoint +// that authenticates with the default user only keeps working. +func ReadEndpointCredentialFile(path string) (string, error) { + if strings.TrimSpace(path) == "" { + return "", nil + } + info, err := os.Lstat(path) + if err != nil { + return "", fmt.Errorf("cannot read the OpenCode endpoint password file") + } + if !info.Mode().IsRegular() || info.Mode().Perm() != 0600 || info.Size() > endpointPasswordFileLimit { + return "", fmt.Errorf("invalid OpenCode endpoint password file") + } + if stat, ok := info.Sys().(*syscall.Stat_t); !ok || stat.Uid != uint32(os.Getuid()) { + return "", fmt.Errorf("the OpenCode endpoint password file belongs to another user") + } + data, err := os.ReadFile(path) + if err != nil { + return "", fmt.Errorf("cannot read the OpenCode endpoint password file") + } + return strings.TrimSpace(string(data)), nil +} + +// OpenEndpoint opens an operator-supplied OpenCode endpoint without owning a +// process: the returned Connection has no owned process (Close is a no-op), so +// nothing local is ever spawned, and it is never used to reach a different +// instance. The endpoint reference is validated once here (URL shape, then the +// backend-specific health contract, then a streamable event path) and every +// failure names the endpoint; there is no fallback and no retry loop. +func OpenEndpoint(ctx context.Context, rawURL, password string) (*Connection, error) { + base, err := NormalizeEndpointURL(rawURL) + if err != nil { + return nil, err + } + conn := &Connection{ + URL: base, + password: password, + username: strings.TrimSpace(os.Getenv(EndpointUsernameEnvKey)), + models: map[string]map[string]string{}, + responses: map[string][]byte{}, + } + probeCtx, cancel := context.WithTimeout(ctx, endpointProbeTimeout) + defer cancel() + switch { + case conn.probeHealth(probeCtx, "v2") == nil: + conn.Backend = "v2" + case conn.probeHealth(probeCtx, "v1") == nil: + conn.Backend = "v1" + default: + return nil, fmt.Errorf("OpenCode endpoint %s is unreachable or is not an OpenCode service", base) + } + // Require a streamable event endpoint before the endpoint is accepted: a + // healthy service without SSE cannot broker chat, so it is a clear refusal, + // never a silent fallback to another instance. + streamCtx, cancelStream := context.WithTimeout(ctx, endpointProbeTimeout) + defer cancelStream() + body, err := conn.EventStream(streamCtx) + if err != nil { + return nil, fmt.Errorf("OpenCode endpoint %s exposes no streamable event endpoint: %w", base, err) + } + body.Close() + return conn, nil +} + +// probeHealth checks one backend family's readiness contract through the same +// authenticated request path the adapter uses, so a reachable but unauthorized +// endpoint is not mistaken for a healthy one. checkHealth and OpenEndpoint share +// it, so the endpoint contract is defined once. +func (c *Connection) probeHealth(ctx context.Context, backend string) error { + previous := c.Backend + c.Backend = backend + defer func() { c.Backend = previous }() + if backend == "v2" { + info, err := c.nativeRequest(ctx, "get", "/api/info", nil, "") + var result struct { + Version string `json:"version"` + URLs []string `json:"urls"` + } + if err != nil || json.Unmarshal(info, &result) != nil || result.Version == "" { + return fmt.Errorf("no V2 info contract") + } + return nil + } + health, err := c.nativeRequest(ctx, "get", "/global/health", nil, "") + var result struct { + Healthy bool `json:"healthy"` + Version string `json:"version"` + } + if err != nil || json.Unmarshal(health, &result) != nil || !result.Healthy || result.Version == "" { + return fmt.Errorf("no native health contract") + } + return nil +} diff --git a/internal/opencode/endpoint_test.go b/internal/opencode/endpoint_test.go new file mode 100644 index 00000000..464f7bdf --- /dev/null +++ b/internal/opencode/endpoint_test.go @@ -0,0 +1,267 @@ +package opencode + +import ( + "context" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + "time" +) + +// TestResolveEndpointRef pins the precedence the server and its tests share: +// explicit flag beats explicit environment beats the recorded setting, and an +// all-empty input means no endpoint is configured (today's spawned default). +func TestResolveEndpointRef(t *testing.T) { + cases := []struct { + flag, env, recorded, want string + }{ + {"http://flag", "http://env", "http://recorded", "http://flag"}, + {"", "http://env", "http://recorded", "http://env"}, + {"", "", "http://recorded", "http://recorded"}, + {" ", " ", " ", ""}, + {"", "", "", ""}, + } + for _, tc := range cases { + if got := ResolveEndpointRef(tc.flag, tc.env, tc.recorded); got != tc.want { + t.Fatalf("ResolveEndpointRef(%q,%q,%q) = %q, want %q", tc.flag, tc.env, tc.recorded, got, tc.want) + } + } +} + +// TestNormalizeEndpointURLRejectsMalformed pins the validation idiom: only an +// http(s) URL with a host and no credentials, query, fragment or path is +// accepted, and every rejection names the endpoint. +func TestNormalizeEndpointURLRejectsMalformed(t *testing.T) { + bad := []string{ + "ftp://host", "host:1234", "http://", "http:///path", + "http://user:pass@host", "http://host/?q=1", "http://host/#frag", + "http://host/base", "http://host/base/", "", + } + for _, raw := range bad { + if got, err := NormalizeEndpointURL(raw); err == nil { + t.Fatalf("NormalizeEndpointURL(%q) = %q, want error", raw, got) + } else if raw != "" && !strings.Contains(err.Error(), raw) && !strings.Contains(err.Error(), "invalid OpenCode endpoint") { + // The error always names the endpoint or states the rule. + t.Fatalf("NormalizeEndpointURL(%q) error %v does not name the endpoint", raw, err) + } + } + for raw, want := range map[string]string{ + "http://host:1234": "http://host:1234", + "https://host/": "https://host", + "http://127.0.0.1:4096/": "http://127.0.0.1:4096", + } { + got, err := NormalizeEndpointURL(raw) + if err != nil || got != want { + t.Fatalf("NormalizeEndpointURL(%q) = %q,%v want %q", raw, got, err, want) + } + } +} + +// v2Endpoint serves the V2 contract: /api/info and a text/event-stream +// /api/event. user/pass assert the fixed basic-auth user unless overridden. +func v2Endpoint(t *testing.T, wantUser string) *httptest.Server { + t.Helper() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if wantUser != "" { + if user, _, _ := r.BasicAuth(); user != wantUser { + w.WriteHeader(http.StatusUnauthorized) + return + } + } + switch r.URL.Path { + case "/api/info": + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"version":"2.0.20","urls":[]}`)) + case "/api/event": + w.Header().Set("Content-Type", "text/event-stream") + w.WriteHeader(http.StatusOK) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + t.Cleanup(srv.Close) + return srv +} + +// v1Endpoint serves the native contract: /global/health and /event. +func v1Endpoint(t *testing.T) *httptest.Server { + t.Helper() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case "/global/health": + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"healthy":true,"version":"1.0.0"}`)) + case "/event": + w.Header().Set("Content-Type", "text/event-stream") + w.WriteHeader(http.StatusOK) + default: + w.WriteHeader(http.StatusNotFound) + } + })) + t.Cleanup(srv.Close) + return srv +} + +// TestOpenEndpointDetectsBackendsAndOwnsNoProcess pins that the explicit path +// returns a connection with no owned process (Close is a no-op and no local +// serve is ever spawned), detecting each backend from its own health contract. +func TestOpenEndpointDetectsBackendsAndOwnsNoProcess(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + for _, tc := range []struct { + name string + srv *httptest.Server + backend string + }{ + {"v2", v2Endpoint(t, "opencode"), "v2"}, + {"v1", v1Endpoint(t), "v1"}, + } { + t.Run(tc.name, func(t *testing.T) { + conn, err := OpenEndpoint(ctx, tc.srv.URL, "") + if err != nil { + t.Fatalf("OpenEndpoint: %v", err) + } + if conn.Backend != tc.backend || conn.URL != tc.srv.URL { + t.Fatalf("connection = %+v, want backend %s at %s", conn, tc.backend, tc.srv.URL) + } + if conn.process != nil { + t.Fatalf("an operator endpoint owns a process: %+v", conn.process) + } + // startServe sets both process and done; neither may be set on the + // explicit path, so no local serve was ever spawned. + if conn.done != nil { + t.Fatalf("an operator endpoint owns a process waiter: %+v", conn.done) + } + conn.Close() // must be a no-op + }) + } +} + +// TestOpenEndpointValidationFailuresAreClearAndNeverFallBack pins that +// unreachable hosts, a reachable non-OpenCode service, a health-ok endpoint +// without SSE and an auth mismatch each fail naming the endpoint and do not +// silently return a usable connection. +func TestOpenEndpointValidationFailuresAreClearAndNeverFallBack(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + + nonOpenCode := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") + _, _ = w.Write([]byte("<html>not opencode</html>")) + })) + t.Cleanup(nonOpenCode.Close) + + noSSE := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/api/info" { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"version":"2.0.20"}`)) + return + } + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"ok":true}`)) + })) + t.Cleanup(noSSE.Close) + + authMismatch := v2Endpoint(t, "someone-else") + + // A port that is closed: two backends fail fast (bounded by the probe context). + unreachable := "http://127.0.0.1:1" + + for _, tc := range []struct { + name string + url string + want string + }{ + {"non-opencode", nonOpenCode.URL, nonOpenCode.URL}, + {"no sse", noSSE.URL, noSSE.URL}, + {"auth mismatch", authMismatch.URL, authMismatch.URL}, + {"unreachable", unreachable, unreachable}, + {"malformed", "ftp://host", "ftp://host"}, + } { + t.Run(tc.name, func(t *testing.T) { + conn, err := OpenEndpoint(ctx, tc.url, "") + if err == nil { + t.Fatalf("OpenEndpoint(%q) succeeded: %+v", tc.url, conn) + } + if !strings.Contains(err.Error(), tc.want) { + t.Fatalf("error %v does not name the endpoint %q", err, tc.want) + } + if conn != nil { + t.Fatalf("a failed endpoint returned a connection: %+v", conn) + } + }) + } +} + +// TestOpenEndpointUsernameOverride pins that PIPELINES_OPENCODE_ENDPOINT_USERNAME +// overrides the detected default user on an operator endpoint. +func TestOpenEndpointUsernameOverride(t *testing.T) { + t.Setenv(EndpointUsernameEnvKey, "custom-user") + srv := v2Endpoint(t, "custom-user") + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + conn, err := OpenEndpoint(ctx, srv.URL, "s3cret") + if err != nil { + t.Fatalf("OpenEndpoint with username override: %v", err) + } + if conn.serverUser() != "custom-user" { + t.Fatalf("serverUser = %q, want custom-user", conn.serverUser()) + } +} + +// TestChatSessionReadsEndpointRecord pins that Chat.Session reuses the existing +// session list/decode: it returns the endpoint's own record for a listed id and +// found=false for an id the endpoint does not report, without inventing a +// directory. +func TestChatSessionReadsEndpointRecord(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet || r.URL.Path != "/api/session" { + w.WriteHeader(http.StatusNotFound) + return + } + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"data":[{"id":"ses_abc","title":"mine","location":{"directory":"/work/mine"}}]}`)) + })) + defer srv.Close() + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + session, found, err := chat.Session(ctx, "ses_abc") + if err != nil || !found { + t.Fatalf("Session(ses_abc) = %+v,%v,%v want found", session, found, err) + } + if session.Directory != "/work/mine" || session.Title != "mine" { + t.Fatalf("session record = %+v, want the endpoint's own directory/title", session) + } + if _, found, err := chat.Session(ctx, "ses_missing"); err != nil || found { + t.Fatalf("Session(ses_missing) = found %v, err %v; want not found", found, err) + } + if _, _, err := chat.Session(ctx, "not-a-session"); err == nil { + t.Fatal("Session accepted a foreign-shaped id") + } +} + +// TestReadEndpointCredentialFile pins the file-reference rule: an empty path +// yields no password, a 0600 regular file is read, and a broader permission or a +// directory is refused. +func TestReadEndpointCredentialFile(t *testing.T) { + if got, err := ReadEndpointCredentialFile(""); err != nil || got != "" { + t.Fatalf("empty path = %q,%v want no password", got, err) + } + path := filepath.Join(t.TempDir(), "endpoint-password") + if err := os.WriteFile(path, []byte("secret\n"), 0600); err != nil { + t.Fatal(err) + } + if got, err := ReadEndpointCredentialFile(path); err != nil || got != "secret" { + t.Fatalf("read = %q,%v want secret", got, err) + } + if err := os.Chmod(path, 0644); err != nil { + t.Fatal(err) + } + if _, err := ReadEndpointCredentialFile(path); err == nil { + t.Fatal("a world-readable password file was accepted") + } +} diff --git a/internal/pipeline/settings.go b/internal/pipeline/settings.go new file mode 100644 index 00000000..98009aec --- /dev/null +++ b/internal/pipeline/settings.go @@ -0,0 +1,43 @@ +package pipeline + +import ( + "context" + "database/sql" +) + +// openCodeEndpointSetting is the single server_settings key that remembers the +// operator's chosen OpenCode endpoint across restarts. +const openCodeEndpointSetting = "opencode_endpoint" + +// initServerSettings creates the additive single-row settings table that records +// operator choices that must survive a restart (currently the OpenCode +// endpoint). It is its own table rather than a column elsewhere so no existing +// schema or ownership table is touched. +func (s *Store) initServerSettings() error { + _, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS server_settings ( +key TEXT PRIMARY KEY, value TEXT NOT NULL);`) + return err +} + +// RecordOpenCodeEndpoint records the operator's chosen OpenCode endpoint in one +// bounded upsert, so a restart with no flag or environment keeps brokering from +// the same place. It is only called after OpenEndpoint validated the endpoint. +func (s *Store) RecordOpenCodeEndpoint(ctx context.Context, url string) error { + _, err := s.db.ExecContext(ctx, `INSERT INTO server_settings(key,value) VALUES(?,?) +ON CONFLICT(key) DO UPDATE SET value=excluded.value`, openCodeEndpointSetting, url) + return err +} + +// OpenCodeEndpoint returns the recorded OpenCode endpoint, or an empty string +// when none was ever recorded. +func (s *Store) OpenCodeEndpoint(ctx context.Context) (string, error) { + var value string + err := s.db.QueryRowContext(ctx, `SELECT value FROM server_settings WHERE key=?`, openCodeEndpointSetting).Scan(&value) + if err == sql.ErrNoRows { + return "", nil + } + if err != nil { + return "", err + } + return value, nil +} diff --git a/internal/pipeline/settings_test.go b/internal/pipeline/settings_test.go new file mode 100644 index 00000000..d2e84341 --- /dev/null +++ b/internal/pipeline/settings_test.go @@ -0,0 +1,42 @@ +package pipeline + +import ( + "context" + "path/filepath" + "testing" +) + +// TestOpenCodeEndpointRoundTrip pins that the operator's chosen endpoint +// survives a store Close/Open (a restart), and that an unset value reads empty. +func TestOpenCodeEndpointRoundTrip(t *testing.T) { + ctx := context.Background() + path := filepath.Join(t.TempDir(), "settings.db") + store, err := Open(path) + if err != nil { + t.Fatal(err) + } + if got, err := store.OpenCodeEndpoint(ctx); err != nil || got != "" { + t.Fatalf("unset endpoint = %q,%v want empty", got, err) + } + if err := store.RecordOpenCodeEndpoint(ctx, "http://127.0.0.1:4096"); err != nil { + t.Fatal(err) + } + if err := store.Close(); err != nil { + t.Fatal(err) + } + reopened, err := Open(path) + if err != nil { + t.Fatal(err) + } + defer reopened.Close() + if got, err := reopened.OpenCodeEndpoint(ctx); err != nil || got != "http://127.0.0.1:4096" { + t.Fatalf("recorded endpoint = %q,%v want the same endpoint after reopen", got, err) + } + // A later choice replaces the row rather than appending a second one. + if err := reopened.RecordOpenCodeEndpoint(ctx, "https://opencode.example"); err != nil { + t.Fatal(err) + } + if got, err := reopened.OpenCodeEndpoint(ctx); err != nil || got != "https://opencode.example" { + t.Fatalf("updated endpoint = %q,%v want the new endpoint", got, err) + } +} diff --git a/internal/pipeline/store.go b/internal/pipeline/store.go index 17be1942..46466d51 100644 --- a/internal/pipeline/store.go +++ b/internal/pipeline/store.go @@ -62,6 +62,10 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU db.Close() return nil, err } + if err := s.initServerSettings(); err != nil { + db.Close() + return nil, err + } return s, nil } diff --git a/internal/protocol/chat.go b/internal/protocol/chat.go index cc7a6611..bd39f8f0 100644 --- a/internal/protocol/chat.go +++ b/internal/protocol/chat.go @@ -16,8 +16,12 @@ import "encoding/json" // brokered values match the service exactly; all list fields are optional so // older peers ignore them. const ( - ChatSessions = "chat_sessions" - ChatCreate = "chat_create" + ChatSessions = "chat_sessions" + ChatCreate = "chat_create" + // ChatAdopt records a session that already exists on the configured OpenCode + // endpoint in the operator-session registry, using the endpoint's own + // directory and title, so it appears in chat_sessions and can be continued. + ChatAdopt = "chat_adopt" ChatPrompt = "chat_prompt" ChatMessages = "chat_messages" ChatInterrupt = "chat_interrupt" @@ -122,6 +126,14 @@ type ChatCreateRequest struct { Directory string `json:"directory,omitempty"` } +// ChatAdoptRequest is the chat_adopt request body: the id of a session that +// already exists on the configured endpoint. The server records it in the +// operator-session registry with the endpoint's own directory and title and +// echoes the adopted session (and the registry list row) like chat_create. +type ChatAdoptRequest struct { + SessionID string `json:"session_id"` +} + // ChatQuery is the session-scoped request body for chat_messages and // chat_interrupt, which name their target session through it; Limit caps the // returned history (server default when zero). diff --git a/internal/protocol/chat_test.go b/internal/protocol/chat_test.go index 38f376cb..5c02e6cc 100644 --- a/internal/protocol/chat_test.go +++ b/internal/protocol/chat_test.go @@ -23,6 +23,7 @@ func chatSamples() []struct { }}, {"session_busy", ChatSession{ID: "ses_2", Title: "held", Busy: true}}, {"create", ChatCreateRequest{Title: "demo", Directory: "/work"}}, + {"adopt", ChatAdoptRequest{SessionID: "ses_1"}}, {"query", ChatQuery{SessionID: "ses_1", Limit: 50}}, {"prompt", ChatPromptRequest{SessionID: "ses_1", Text: "hello", Model: "m", Agent: "build", Files: []string{"file:///tmp/a.png"}}}, {"message", ChatMessage{ @@ -155,6 +156,7 @@ func TestChatEnvelopeRoundTrip(t *testing.T) { ChatSessions: []ChatSession{session}, ChatSession: &session, ChatCreate: &ChatCreateRequest{Title: "new"}, + ChatAdopt: &ChatAdoptRequest{SessionID: "ses_1"}, ChatQuery: &ChatQuery{SessionID: "ses_1", Limit: 10}, ChatPrompt: &ChatPromptRequest{SessionID: "ses_1", Text: "hi"}, ChatMessages: []ChatMessage{{ID: "msg_1", SessionID: "ses_1", Role: ChatRoleUser, Parts: []ChatPart{{Type: ChatPartText, Text: "hi"}}}}, @@ -179,6 +181,7 @@ func TestChatEnvelopeRoundTrip(t *testing.T) { if !reflect.DeepEqual(decoded.ChatSessions, message.ChatSessions) || !reflect.DeepEqual(decoded.ChatSession, message.ChatSession) || !reflect.DeepEqual(decoded.ChatCreate, message.ChatCreate) || + !reflect.DeepEqual(decoded.ChatAdopt, message.ChatAdopt) || !reflect.DeepEqual(decoded.ChatQuery, message.ChatQuery) || !reflect.DeepEqual(decoded.ChatPrompt, message.ChatPrompt) || !reflect.DeepEqual(decoded.ChatMessages, message.ChatMessages) || @@ -195,7 +198,7 @@ func TestChatFieldsOmittedWhenUnused(t *testing.T) { if err != nil { t.Fatal(err) } - for _, absent := range []string{"chat_sessions", "chat_session", "chat_create", "chat_query", "chat_prompt", "chat_messages", "chat_event", "chat_permission", "chat_reply", "chat_directories"} { + for _, absent := range []string{"chat_sessions", "chat_session", "chat_create", "chat_adopt", "chat_query", "chat_prompt", "chat_messages", "chat_event", "chat_permission", "chat_reply", "chat_directories"} { if strings.Contains(string(minimal), absent) { t.Fatalf("absent %q must be omitted: %s", absent, minimal) } diff --git a/internal/protocol/message.go b/internal/protocol/message.go index 2f177a0f..0ca03db2 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -159,6 +159,7 @@ type Message struct { ChatSessions []ChatSession `json:"chat_sessions,omitempty"` ChatSession *ChatSession `json:"chat_session,omitempty"` ChatCreate *ChatCreateRequest `json:"chat_create,omitempty"` + ChatAdopt *ChatAdoptRequest `json:"chat_adopt,omitempty"` ChatQuery *ChatQuery `json:"chat_query,omitempty"` ChatPrompt *ChatPromptRequest `json:"chat_prompt,omitempty"` ChatMessages []ChatMessage `json:"chat_messages,omitempty"` diff --git a/internal/server/chat.go b/internal/server/chat.go index dd67fd53..8baa51db 100644 --- a/internal/server/chat.go +++ b/internal/server/chat.go @@ -33,6 +33,10 @@ type chatBroker interface { Sessions(ctx context.Context) ([]protocol.ChatSession, error) // Create creates one OpenCode session in the given directory and returns it. Create(ctx context.Context, title, directory string) (protocol.ChatSession, error) + // Session returns the endpoint's own record of one existing session so an + // adopted session carries the endpoint's directory and title; found is false + // when the endpoint does not list sessionID. + Session(ctx context.Context, sessionID string) (protocol.ChatSession, bool, error) // Permissions lists the session's pending OpenCode approval requests so the // handler can push chat_permission frames. Permissions(ctx context.Context, sessionID string) ([]protocol.ChatPermissionFrame, error) @@ -474,6 +478,9 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker case protocol.ChatCreate: handleChatCreate(ctx, request, broker, registry, write) return true + case protocol.ChatAdopt: + handleChatAdopt(ctx, request, broker, registry, write) + return true case protocol.ChatDirectories: handleChatDirectories(ctx, request, registry, write) return true @@ -591,6 +598,59 @@ func handleChatCreate(ctx context.Context, request protocol.Message, broker chat _ = write(response) } +// handleChatAdopt records a session that already exists on the endpoint in the +// operator registry, using the endpoint's own directory and title verbatim, so +// it appears in chat_sessions and can be continued. It refuses a nil broker, a +// nil registry, a nil payload and an invalid/blank id before any broker call; an +// id the endpoint does not report is an error naming the id and records nothing; +// an endpoint record with an empty directory is refused rather than substituting +// the server's working directory or the operator home. The response echoes the +// adopted session and its registry row like chat_create. +func handleChatAdopt(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry, write func(protocol.Message) error) { + if broker == nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"}) + return + } + if registry == nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"}) + return + } + if request.ChatAdopt == nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a chat_adopt session id is required"}) + return + } + id := strings.TrimSpace(request.ChatAdopt.SessionID) + if !opencode.ValidSessionID(id) { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) + return + } + session, found, err := broker.Session(ctx, id) + if err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + if !found { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + id + " was not found on the OpenCode endpoint"}) + return + } + directory := strings.TrimSpace(session.Directory) + if directory == "" { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + id + " has no directory on the OpenCode endpoint"}) + return + } + if err := registry.RecordOperatorSession(ctx, session.ID, directory, session.Title, protocol.ChatSessionTagOperator); err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was found but could not be recorded: " + err.Error()}) + return + } + session.Directory = directory + session.Tag = protocol.ChatSessionTagOperator + response := protocol.Message{Type: protocol.ChatAdopt, ID: request.ID, ChatSession: &session} + // The same registry row is echoed on the list field so a client can splice it + // into chat_sessions without a re-list. + response.ChatSessions = []protocol.ChatSession{session} + _ = write(response) +} + // handleChatDirectories answers chat_directories with the operator's home first, // then the directories of previous operator sessions most-recently-used first, // deduplicated against the home directory and capped at operatorDirectoryLimit. diff --git a/internal/server/chat_test.go b/internal/server/chat_test.go index 8ca2a728..7c11801e 100644 --- a/internal/server/chat_test.go +++ b/internal/server/chat_test.go @@ -71,6 +71,11 @@ type fakeChat struct { createDirectory string created protocol.ChatSession createErr error + sessionByIDCalls int + sessionByID string + sessionRecord protocol.ChatSession + sessionFound bool + sessionByIDErr error } func (f *fakeChat) Prompt(context.Context, string, string) error { @@ -214,6 +219,34 @@ func (f *fakeChat) createQuery() (int, string, string) { return f.createCalls, f.createTitle, f.createDirectory } +// Session returns the seeded endpoint record of one session (or "not found") +// and counts each call, so the adopt handler can be asserted to read the +// endpoint exactly once and never invent a directory. +func (f *fakeChat) Session(_ context.Context, sessionID string) (protocol.ChatSession, bool, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.sessionByIDCalls++ + f.sessionByID = sessionID + if f.sessionByIDErr != nil { + return protocol.ChatSession{}, false, f.sessionByIDErr + } + if !f.sessionFound { + return protocol.ChatSession{}, false, nil + } + session := f.sessionRecord + if session.ID == "" { + session.ID = sessionID + } + return session, true, nil +} + +// sessionByIDQuery reports the recorded chat_adopt read count and last id. +func (f *fakeChat) sessionByIDQuery() (int, string) { + f.mu.Lock() + defer f.mu.Unlock() + return f.sessionByIDCalls, f.sessionByID +} + // replyQuery reports the recorded chat_reply count and the last forwarded // request, so a test can assert the exact session/request/decision. func (f *fakeChat) replyQuery() (int, protocol.ChatReplyRequest) { @@ -1676,3 +1709,170 @@ func TestChatPermissionWriteFailureDoesNotDropRequest(t *testing.T) { t.Fatalf("push attempts = %d, want the request re-pushed after the failure", attempts) } } + +// TestChatAdoptRecordsEndpointSession is the chat_adopt acceptance test: the +// endpoint's own record supplies the directory and title, the handler records +// the row with the operator tag, and the response echoes the session and its +// registry row so the client can splice it into chat_sessions without a re-list. +func TestChatAdoptRecordsEndpointSession(t *testing.T) { + ctx := context.Background() + store := newOperatorStore(t) + broker := scriptedChat() + broker.sessionFound = true + broker.sessionRecord = protocol.ChatSession{ID: "ses_existing", Title: "my own work", Directory: "/work/existing"} + _, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store}) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatAdopt, ID: "a1", ChatAdopt: &protocol.ChatAdoptRequest{SessionID: "ses_existing"}}); err != nil { + t.Fatal(err) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.ChatAdopt || response.ID != "a1" || response.ChatSession == nil { + t.Fatalf("chat_adopt response = %+v", response) + } + if response.ChatSession.ID != "ses_existing" || response.ChatSession.Directory != "/work/existing" || response.ChatSession.Tag != protocol.ChatSessionTagOperator { + t.Fatalf("adopted session = %+v", response.ChatSession) + } + if len(response.ChatSessions) != 1 || response.ChatSessions[0].ID != "ses_existing" || response.ChatSessions[0].Tag != protocol.ChatSessionTagOperator { + t.Fatalf("adopted session not echoed as a registry row: %+v", response.ChatSessions) + } + if calls, id := broker.sessionByIDQuery(); calls != 1 || id != "ses_existing" { + t.Fatalf("broker.Session = %d/%q, want 1/ses_existing", calls, id) + } + recorded, err := store.OperatorSessions(ctx) + if err != nil { + t.Fatal(err) + } + if len(recorded) != 1 || recorded[0].ID != "ses_existing" || recorded[0].Directory != "/work/existing" || recorded[0].Title != "my own work" || recorded[0].Tag != protocol.ChatSessionTagOperator { + t.Fatalf("registry row = %+v", recorded) + } + // chat_sessions now lists the adopted session (the endpoint still reports it), + // carrying the registry directory and operator tag. + broker.mu.Lock() + broker.sessions = []protocol.ChatSession{{ID: "ses_existing", Title: "endpoint title", Directory: "/endpoint/elsewhere"}} + broker.mu.Unlock() + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatSessions, ID: "s1"}); err != nil { + t.Fatal(err) + } + var listed protocol.Message + if err := wsjson.Read(ctx, conn, &listed); err != nil { + t.Fatal(err) + } + if listed.Type != protocol.ChatSessions || len(listed.ChatSessions) != 1 || listed.ChatSessions[0].ID != "ses_existing" || listed.ChatSessions[0].Directory != "/work/existing" || listed.ChatSessions[0].Tag != protocol.ChatSessionTagOperator { + t.Fatalf("chat_sessions did not list the adopted session with its registry directory: %+v", listed.ChatSessions) + } + // chat_prompt can continue the adopted session: TouchOperatorSession updates + // the recorded row, so it moves to the front of the list. + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "p1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_existing", Text: "continue"}}); err != nil { + t.Fatal(err) + } + var ack protocol.Message + if err := wsjson.Read(ctx, conn, &ack); err != nil { + t.Fatal(err) + } + if ack.Type != protocol.ChatPrompt || ack.ID != "p1" { + t.Fatalf("adopted session prompt ack = %+v", ack) + } +} + +// TestChatAdoptMissingIDRecordsNothing pins that an id the endpoint does not +// report is refused naming the id and leaves the registry unchanged. +func TestChatAdoptMissingIDRecordsNothing(t *testing.T) { + ctx := context.Background() + store := newOperatorStore(t) + // Seed one unrelated operator row so the "unchanged" claim compares the + // stored rows before and after, not merely an empty registry. + if err := store.RecordOperatorSession(ctx, "ses_other", "/work/other", "other", protocol.ChatSessionTagOperator); err != nil { + t.Fatal(err) + } + before, err := store.OperatorSessions(ctx) + if err != nil { + t.Fatal(err) + } + broker := scriptedChat() + broker.sessionFound = false + _, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store}) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatAdopt, ID: "a1", ChatAdopt: &protocol.ChatAdoptRequest{SessionID: "ses_missing"}}); err != nil { + t.Fatal(err) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.Error || !strings.Contains(response.Error, "ses_missing") { + t.Fatalf("missing id not refused naming the id: %+v", response) + } + after, err := store.OperatorSessions(ctx) + if err != nil { + t.Fatal(err) + } + if len(before) != 1 || len(after) != 1 || after[0].ID != before[0].ID { + t.Fatalf("missing id changed the registry: before %+v after %+v", before, after) + } +} + +// TestChatAdoptRefusesEmptyDirectory pins the no-guessing rule: an endpoint +// record with an empty directory is refused and records nothing, never falling +// back to the server's working directory or the operator home. +func TestChatAdoptRefusesEmptyDirectory(t *testing.T) { + ctx := context.Background() + store := newOperatorStore(t) + broker := scriptedChat() + broker.sessionFound = true + broker.sessionRecord = protocol.ChatSession{ID: "ses_nodir"} + _, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store}) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatAdopt, ID: "a1", ChatAdopt: &protocol.ChatAdoptRequest{SessionID: "ses_nodir"}}); err != nil { + t.Fatal(err) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.Error || !strings.Contains(response.Error, "directory") { + t.Fatalf("empty directory not refused: %+v", response) + } + if recorded, err := store.OperatorSessions(ctx); err != nil || len(recorded) != 0 { + t.Fatalf("empty directory recorded a session: %+v %v", recorded, err) + } +} + +// TestChatAdoptRejectsInvalidInputBeforeBroker pins that a nil payload, a blank +// or foreign-shaped id, a nil registry and a nil broker are each refused before +// any endpoint call. +func TestChatAdoptRejectsInvalidInputBeforeBroker(t *testing.T) { + ctx := context.Background() + cases := []struct { + name string + request protocol.Message + options Options + wantCall int + }{ + {name: "nil payload", request: protocol.Message{Type: protocol.ChatAdopt, ID: "a1"}, options: Options{Chat: scriptedChat(), OperatorRegistry: newOperatorStore(t)}, wantCall: 0}, + {name: "blank id", request: protocol.Message{Type: protocol.ChatAdopt, ID: "a1", ChatAdopt: &protocol.ChatAdoptRequest{SessionID: " "}}, options: Options{Chat: scriptedChat(), OperatorRegistry: newOperatorStore(t)}, wantCall: 0}, + {name: "foreign id", request: protocol.Message{Type: protocol.ChatAdopt, ID: "a1", ChatAdopt: &protocol.ChatAdoptRequest{SessionID: "not-a-session"}}, options: Options{Chat: scriptedChat(), OperatorRegistry: newOperatorStore(t)}, wantCall: 0}, + {name: "nil broker", request: protocol.Message{Type: protocol.ChatAdopt, ID: "a1", ChatAdopt: &protocol.ChatAdoptRequest{SessionID: "ses_1"}}, options: Options{OperatorRegistry: newOperatorStore(t)}, wantCall: 0}, + {name: "nil registry", request: protocol.Message{Type: protocol.ChatAdopt, ID: "a1", ChatAdopt: &protocol.ChatAdoptRequest{SessionID: "ses_1"}}, options: Options{Chat: scriptedChat()}, wantCall: 0}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + broker, _ := tc.options.Chat.(*fakeChat) + _, _, conn := dialChatOptions(t, tc.options) + if err := wsjson.Write(ctx, conn, protocol.Message(tc.request)); err != nil { + t.Fatal(err) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.Error { + t.Fatalf("%s not refused: %+v", tc.name, response) + } + if broker != nil { + if calls, _ := broker.sessionByIDQuery(); calls != tc.wantCall { + t.Fatalf("%s reached the endpoint %d times, want %d", tc.name, calls, tc.wantCall) + } + } + }) + } +}