From 9fa09e6ce8953e8b0ecb281684648b77fd64b378 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 16 Oct 2026 03:01:55 +0100 Subject: [PATCH 1/7] Add brokered OpenCode chat wire types Add the internal/protocol types the phone needs to talk to OpenCode chat through the Pipelines server (route B): the chat_* request/response type constants (sessions, create, prompt, messages, interrupt, reply) and the server-pushed chat_event frame, plus the part/status/role/tool-state/decision constant sets and payload structs modelled on the verified OpenCode shapes - an assistant turn is a list of parts (text | reasoning | tool), and a tool part carries name, executed, state.status (streaming | running | completed | error), state.input, state.content, state.metadata, state.error and epoch-millisecond timings. Add eight optional omitempty chat fields to the shared Message envelope, so older peers ignore them, and a golden round-trip test covering every new type, an exact-JSON pin of a tool chat_event frame, envelope preservation and omitempty. go test ./internal/protocol/... passes. --- internal/protocol/chat.go | 211 +++++++++++++++++++++++++++++++++ internal/protocol/chat_test.go | 163 +++++++++++++++++++++++++ internal/protocol/message.go | 10 ++ 3 files changed, 384 insertions(+) create mode 100644 internal/protocol/chat.go create mode 100644 internal/protocol/chat_test.go diff --git a/internal/protocol/chat.go b/internal/protocol/chat.go new file mode 100644 index 00000000..9d411210 --- /dev/null +++ b/internal/protocol/chat.go @@ -0,0 +1,211 @@ +package protocol + +import "encoding/json" + +// Chat requests and responses are brokered by the Pipelines server (route B in +// the OpenCode chat research): the phone keeps talking only to /connect and the +// server holds the OpenCode credentials, translating each type below to the +// native service. Like every other exchange, a client sends +// Message{Type: ChatXxx, ID: } and the server echoes the ID on the +// response. +// +// The payloads mirror the verified OpenCode shapes: an assistant turn is a list +// of parts (text | reasoning | tool), and a tool part carries name, state.status, +// state.input, state.content, state.metadata, state.error and epoch-millisecond +// timings. Timings stay integer milliseconds rather than RFC3339 strings so the +// brokered values match the service exactly; all list fields are optional so +// older peers ignore them. +const ( + ChatSessions = "chat_sessions" + ChatCreate = "chat_create" + ChatPrompt = "chat_prompt" + ChatMessages = "chat_messages" + ChatInterrupt = "chat_interrupt" + ChatReply = "chat_reply" + // ChatEvent is the server-pushed frame; it is the only type the server + // sends without a matching request. + ChatEvent = "chat_event" +) + +// Content part types carried by ChatPart.Type and ChatEvent.Kind. +const ( + ChatPartText = "text" + ChatPartReasoning = "reasoning" + ChatPartTool = "tool" +) + +// ChatEvent.Kind values pushed on the chat_event stream. Step and usage carry +// no ChatPart; they report turn progress and the turn's totals. +const ( + ChatEventText = ChatPartText + ChatEventReasoning = ChatPartReasoning + ChatEventTool = ChatPartTool + ChatEventStep = "step" + ChatEventUsage = "usage" +) + +// ChatStep.Status values, mirroring the OpenCode session.step events. +const ( + ChatStepStarted = "started" + ChatStepStreamed = "streamed" + ChatStepEnded = "ended" +) + +// ChatToolState.Status values, mirroring OpenCode's ToolState union. +const ( + ChatToolStreaming = "streaming" + ChatToolRunning = "running" + ChatToolCompleted = "completed" + ChatToolError = "error" +) + +// ChatReplyRequest.Decision values, mirroring the OpenCode permission reply body. +const ( + ChatDecisionOnce = "once" + ChatDecisionAlways = "always" + ChatDecisionReject = "reject" +) + +// ChatMessage.Role values. +const ( + ChatRoleUser = "user" + ChatRoleAssistant = "assistant" +) + +// ChatSession is one createable/listed OpenCode session, flattened for the +// phone: the nested location.directory and time{created,updated} fields are +// lifted to Directory, CreatedAt and UpdatedAt. +type ChatSession struct { + ID string `json:"id"` + Title string `json:"title,omitempty"` + Agent string `json:"agent,omitempty"` + Model string `json:"model,omitempty"` + Directory string `json:"directory,omitempty"` + ParentID string `json:"parent_id,omitempty"` + CreatedAt int64 `json:"created_at,omitempty"` + UpdatedAt int64 `json:"updated_at,omitempty"` +} + +// ChatCreateRequest is the chat_create request body: a title and an optional +// working directory (the server's configured location when empty). The response +// carries the created session in Message.ChatSession. +type ChatCreateRequest struct { + Title string `json:"title,omitempty"` + Directory string `json:"directory,omitempty"` +} + +// 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). +type ChatQuery struct { + SessionID string `json:"session_id"` + Limit int `json:"limit,omitempty"` +} + +// ChatPromptRequest is the chat_prompt request body: the text to send, with an +// optional model/agent override and attached file URIs. The matching response is +// the inbox envelope, which the server folds into a ChatMessage. +type ChatPromptRequest struct { + SessionID string `json:"session_id"` + Text string `json:"text"` + Model string `json:"model,omitempty"` + Agent string `json:"agent,omitempty"` + Files []string `json:"files,omitempty"` +} + +// ChatMessage is one stored turn returned by chat_messages. A user turn is a +// single text part; an assistant turn holds the text, reasoning and tool parts +// the phone renders. Parts corresponds to the OpenCode content array. +type ChatMessage struct { + ID string `json:"id"` + SessionID string `json:"session_id"` + Role string `json:"role"` + Model string `json:"model,omitempty"` + Agent string `json:"agent,omitempty"` + CreatedAt int64 `json:"created_at,omitempty"` + Parts []ChatPart `json:"parts"` +} + +// ChatPart is one element of a ChatMessage. Type selects the meaning: text and +// reasoning use Text and ID, tool uses ID, Name, Executed, State and Time. +type ChatPart struct { + Type string `json:"type"` + ID string `json:"id,omitempty"` + Text string `json:"text,omitempty"` + Name string `json:"name,omitempty"` + Executed bool `json:"executed,omitempty"` + State *ChatToolState `json:"state,omitempty"` + Time *ChatTime `json:"time,omitempty"` +} + +// ChatToolState is the lifecycle of a tool call. Input is a raw JSON value: a +// partial-JSON string while streaming, an object once running or completed. +// Content is the tool's textual/file result, Metadata its provider stats, +// Error the structured failure for status "error". +type ChatToolState struct { + Status string `json:"status"` + Input json.RawMessage `json:"input,omitempty"` + Content []ChatToolContent `json:"content,omitempty"` + Metadata map[string]any `json:"metadata,omitempty"` + Error *ChatError `json:"error,omitempty"` +} + +// ChatToolContent is one returned tool result: either text or a file/uri pair +// (so a tool can return an image the phone renders). +type ChatToolContent struct { + Type string `json:"type"` + Text string `json:"text,omitempty"` + URI string `json:"uri,omitempty"` + Mime string `json:"mime,omitempty"` + Name string `json:"name,omitempty"` +} + +// ChatError is the structured error attached to a failed tool call. +type ChatError struct { + Type string `json:"type,omitempty"` + Message string `json:"message,omitempty"` +} + +// ChatTime is the epoch-millisecond timing envelope of a tool call. +type ChatTime struct { + Created int64 `json:"created,omitempty"` + Ran int64 `json:"ran,omitempty"` + Completed int64 `json:"completed,omitempty"` +} + +// ChatEventFrame is the server-pushed chat_event frame. Kind selects the +// payload: text and reasoning carry an incremental Delta plus the target Part, +// tool carries a full Part with its state, step marks turn progress and usage +// reports the turn's totals. +type ChatEventFrame struct { + SessionID string `json:"session_id"` + MessageID string `json:"message_id,omitempty"` + Kind string `json:"kind"` + Delta string `json:"delta,omitempty"` + Part *ChatPart `json:"part,omitempty"` + Step *ChatStep `json:"step,omitempty"` + Usage *ChatUsage `json:"usage,omitempty"` +} + +// ChatStep is a progress marker for the current step of a turn. +type ChatStep struct { + Status string `json:"status"` + Index int `json:"index,omitempty"` +} + +// ChatUsage is the token and cost total of a turn. +type ChatUsage struct { + InputTokens int `json:"input_tokens,omitempty"` + OutputTokens int `json:"output_tokens,omitempty"` + Cost float64 `json:"cost,omitempty"` +} + +// ChatReplyRequest is the chat_reply request body: the phone's decision on a +// pending permission request, mirroring +// POST /api/session/{id}/permission/{requestID}/reply. +type ChatReplyRequest struct { + SessionID string `json:"session_id"` + RequestID string `json:"request_id"` + Decision string `json:"decision"` + Message string `json:"message,omitempty"` +} diff --git a/internal/protocol/chat_test.go b/internal/protocol/chat_test.go new file mode 100644 index 00000000..f4dbc4bd --- /dev/null +++ b/internal/protocol/chat_test.go @@ -0,0 +1,163 @@ +package protocol + +import ( + "encoding/json" + "reflect" + "strings" + "testing" +) + +// chatSamples covers every new chat type with a populated value, plus a text and +// a tool chat_event, so a round-trip loss in any field fails the test. +func chatSamples() []struct { + name string + value any +} { + return []struct { + name string + value any + }{ + {"session", ChatSession{ + ID: "ses_1", Title: "demo", Agent: "build", Model: "deepseek-v4.1-flash", + Directory: "/work", ParentID: "ses_0", CreatedAt: 1792112366711, UpdatedAt: 1792112367999, + }}, + {"create", ChatCreateRequest{Title: "demo", Directory: "/work"}}, + {"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{ + ID: "msg_1", SessionID: "ses_1", Role: ChatRoleAssistant, Model: "m", Agent: "build", CreatedAt: 1792112366711, + Parts: []ChatPart{ + {Type: ChatPartText, ID: "p1", Text: "done"}, + {Type: ChatPartReasoning, ID: "p2", Text: "thinking"}, + {Type: ChatPartTool, ID: "call_1", Name: "shell", Executed: true, + State: &ChatToolState{ + Status: ChatToolCompleted, + Input: json.RawMessage(`{"command":"ls"}`), + Content: []ChatToolContent{{Type: "text", Text: "app-debug.apk"}, {Type: "file", URI: "file:///tmp/x.png", Mime: "image/png", Name: "x.png"}}, + Metadata: map[string]any{"exit": "0"}, + }, + Time: &ChatTime{Created: 1, Ran: 2, Completed: 3}, + }, + {Type: ChatPartTool, ID: "call_2", Name: "edit", + State: &ChatToolState{Status: ChatToolError, Input: json.RawMessage(`{}`), Error: &ChatError{Type: "tool", Message: "boom"}}, + }, + }, + }}, + {"event_text", ChatEventFrame{ + SessionID: "ses_1", MessageID: "msg_1", Kind: ChatEventText, Delta: "hel", + Part: &ChatPart{Type: ChatPartText, ID: "p1"}, + }}, + {"event_tool", ChatEventFrame{ + SessionID: "ses_1", MessageID: "msg_1", Kind: ChatEventTool, + Part: &ChatPart{Type: ChatPartTool, ID: "call_1", Name: "shell", Executed: true, + State: &ChatToolState{Status: ChatToolRunning, Input: json.RawMessage(`{"command":"ls"}`)}, + }, + }}, + {"event_step", ChatEventFrame{SessionID: "ses_1", Kind: ChatEventStep, Step: &ChatStep{Status: ChatStepEnded, Index: 2}}}, + {"event_usage", ChatEventFrame{SessionID: "ses_1", Kind: ChatEventUsage, Usage: &ChatUsage{InputTokens: 12, OutputTokens: 34, Cost: 0.001}}}, + {"reply", ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: ChatDecisionOnce, Message: "ok"}}, + } +} + +func TestChatTypesGoldenRoundTrip(t *testing.T) { + for _, sample := range chatSamples() { + encoded, err := json.Marshal(sample.value) + if err != nil { + t.Fatalf("%s: marshal: %v", sample.name, err) + } + target := reflect.New(reflect.TypeOf(sample.value)) + if err := json.Unmarshal(encoded, target.Interface()); err != nil { + t.Fatalf("%s: unmarshal: %v", sample.name, err) + } + decoded := target.Elem().Interface() + if !reflect.DeepEqual(decoded, sample.value) { + t.Fatalf("%s: fields lost\n got %+v\nwant %+v", sample.name, decoded, sample.value) + } + again, err := json.Marshal(decoded) + if err != nil { + t.Fatalf("%s: re-marshal: %v", sample.name, err) + } + if string(again) != string(encoded) { + t.Fatalf("%s: wire shape not stable\n got %s\nwant %s", sample.name, again, encoded) + } + } +} + +// TestChatEventGoldenJSON pins the exact wire shape of a pushed frame so a JSON +// tag change cannot silently alter what the phone parses. +func TestChatEventGoldenJSON(t *testing.T) { + event := ChatEventFrame{ + SessionID: "ses_1", MessageID: "msg_1", Kind: ChatEventTool, + Part: &ChatPart{Type: ChatPartTool, ID: "call_1", Name: "shell", Executed: true, + State: &ChatToolState{ + Status: ChatToolStreaming, + Input: json.RawMessage(`"{\"command\":\"ls\""`), + Content: nil, + }, + Time: &ChatTime{Created: 10, Ran: 11, Completed: 12}, + }, + } + encoded, err := json.Marshal(event) + if err != nil { + t.Fatal(err) + } + want := `{"session_id":"ses_1","message_id":"msg_1","kind":"tool","part":{"type":"tool","id":"call_1","name":"shell","executed":true,"state":{"status":"streaming","input":"{\"command\":\"ls\""},"time":{"created":10,"ran":11,"completed":12}}}` + if string(encoded) != want { + t.Fatalf("golden mismatch\n got %s\nwant %s", encoded, want) + } + var decoded ChatEventFrame + if err := json.Unmarshal([]byte(want), &decoded); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(decoded, event) { + t.Fatalf("golden did not decode back: %+v", decoded) + } +} + +func TestChatEnvelopeRoundTrip(t *testing.T) { + session := ChatSession{ID: "ses_1", Title: "demo"} + message := Message{ + Type: ChatSessions, ID: "r1", + ChatSessions: []ChatSession{session}, + ChatSession: &session, + ChatCreate: &ChatCreateRequest{Title: "new"}, + 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"}}}}, + ChatEvent: &ChatEventFrame{SessionID: "ses_1", Kind: ChatEventText, Delta: "hi"}, + ChatReply: &ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: ChatDecisionReject}, + } + encoded, err := json.Marshal(message) + if err != nil { + t.Fatal(err) + } + var decoded Message + if err := json.Unmarshal(encoded, &decoded); err != nil { + t.Fatal(err) + } + if decoded.Type != ChatSessions || decoded.ID != "r1" { + t.Fatalf("envelope lost: %+v", decoded) + } + if !reflect.DeepEqual(decoded.ChatSessions, message.ChatSessions) || + !reflect.DeepEqual(decoded.ChatSession, message.ChatSession) || + !reflect.DeepEqual(decoded.ChatCreate, message.ChatCreate) || + !reflect.DeepEqual(decoded.ChatQuery, message.ChatQuery) || + !reflect.DeepEqual(decoded.ChatPrompt, message.ChatPrompt) || + !reflect.DeepEqual(decoded.ChatMessages, message.ChatMessages) || + !reflect.DeepEqual(decoded.ChatEvent, message.ChatEvent) || + !reflect.DeepEqual(decoded.ChatReply, message.ChatReply) { + t.Fatalf("chat payloads lost: %+v", decoded) + } +} + +func TestChatFieldsOmittedWhenUnused(t *testing.T) { + minimal, err := json.Marshal(Message{Type: List, ID: "r1"}) + 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_reply"} { + 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 da7c2925..f80556fa 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -133,4 +133,14 @@ type Message struct { WritePaths []string `json:"write_paths,omitempty"` RequestKey string `json:"request_key,omitempty"` Workspace string `json:"workspace,omitempty"` + // Chat is the brokered OpenCode chat traffic (route B). See chat.go for the + // per-type payload structs and the wire shapes they mirror. + ChatSessions []ChatSession `json:"chat_sessions,omitempty"` + ChatSession *ChatSession `json:"chat_session,omitempty"` + ChatCreate *ChatCreateRequest `json:"chat_create,omitempty"` + ChatQuery *ChatQuery `json:"chat_query,omitempty"` + ChatPrompt *ChatPromptRequest `json:"chat_prompt,omitempty"` + ChatMessages []ChatMessage `json:"chat_messages,omitempty"` + ChatEvent *ChatEventFrame `json:"chat_event,omitempty"` + ChatReply *ChatReplyRequest `json:"chat_reply,omitempty"` } From b8b545accdc1cff931925a839ee22e1d4b608b19 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 16 Oct 2026 04:09:14 +0100 Subject: [PATCH 2/7] Stream brokered OpenCode chat replies over /connect Add the server-side half of the brokered chat path (route B): the opencode.Chat adapter forwards chat_prompt to POST /api/session/{id}/prompt through the owned Connection.Call translation and derives chat_event frames from the authenticated /api/event SSE firehose (text and reasoning deltas, tool parts with their status transitions, step and usage). Every pushed frame carries a stable session and message id, with a last-message-id fallback so step/tool events that omit the assistant message id stay attributable. The /connect handler keeps per-session retained state (chatHub) capped at 1 MiB with least-recently-updated eviction at 128 turns, so a reconnecting client re-syncs through chat_messages instead of replaying deltas the OpenCode per-session log never resends. Prompt dispatch subscribes before posting and buffers pre-ack frames to preserve order, and cancels the stream on disconnect so no goroutine leaks. chat_sessions and chat_create are declared but not yet brokered: they answer with an explicit protocol error rather than the generic Pong. Reuse the existing transport, validation and normalization; no second OpenCode transport or credential path is introduced. Tests: protocol golden round-trip, adapter normalization + v1/v2 Call translation, and handler ack/stream/validation/resume/disconnect, plus a real-adapter no-tool evidence harness. --- cmd/pipelines-server/main.go | 22 +- .../author/skill/references/operations.md | 15 + internal/opencode/chat.go | 355 +++++++++++ internal/opencode/chat_test.go | 292 +++++++++ internal/opencode/connection.go | 37 ++ internal/opencode/native.go | 21 +- internal/server/chat.go | 458 ++++++++++++++ internal/server/chat_test.go | 563 ++++++++++++++++++ internal/server/websocket.go | 33 +- 9 files changed, 1786 insertions(+), 10 deletions(-) create mode 100644 internal/opencode/chat.go create mode 100644 internal/opencode/chat_test.go create mode 100644 internal/server/chat.go create mode 100644 internal/server/chat_test.go diff --git a/cmd/pipelines-server/main.go b/cmd/pipelines-server/main.go index 0ffa3cb0..1d2e4408 100644 --- a/cmd/pipelines-server/main.go +++ b/cmd/pipelines-server/main.go @@ -12,6 +12,7 @@ import ( "syscall" "time" + "git.a-wan.net/alan/pipelines/internal/opencode" "git.a-wan.net/alan/pipelines/internal/paths" "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/server" @@ -36,7 +37,26 @@ func main() { ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() artifactsRoot := filepath.Join(filepath.Dir(*db), "artifacts") - srv := &http.Server{Addr: *addr, Handler: server.HandlerWithOptions(ctx, server.Options{ArtifactsRoot: artifactsRoot}, store), ReadHeaderTimeout: 5 * time.Second} + options := server.Options{ArtifactsRoot: artifactsRoot} + // Chat brokers OpenCode over the owned endpoint. Streaming needs a + // URL-bearing, SSE-capable backend. Detect may select "v2" (the CLI + // advertises `api`), but Open never starts a serve process for v2 and so + // returns no event-stream URL: this build enables chat only when a + // connection carries a streamable endpoint (the owned v1 serve process). + // Otherwise chat stays disabled and every chat_* request is answered with a + // protocol error naming the missing configuration rather than hanging. + if backend, err := opencode.Detect(ctx, opencode.Executable()); err != nil { + log.Printf("OpenCode chat unavailable: %v", err) + } else if conn, err := opencode.Open(ctx, opencode.Executable(), "", backend); err != nil { + log.Printf("OpenCode chat unavailable: %v", err) + } else if conn.URL == "" { + conn.Close() + log.Printf("OpenCode chat unavailable: backend %q exposes no streamable event endpoint", backend) + } else { + options.Chat = opencode.NewChat(conn) + defer conn.Close() + } + srv := &http.Server{Addr: *addr, Handler: server.HandlerWithOptions(ctx, options, store), ReadHeaderTimeout: 5 * time.Second} defer server.WaitRuns(srv.Handler) go func() { <-ctx.Done() diff --git a/internal/author/skill/references/operations.md b/internal/author/skill/references/operations.md index bb716717..2359e8b5 100644 --- a/internal/author/skill/references/operations.md +++ b/internal/author/skill/references/operations.md @@ -175,6 +175,21 @@ traversal, absolute paths, symlink escapes and unknown runs/steps. - Do not invent artifact files or claim a stage produced one without the recorded evidence; the server observes the directory after the stage. +## Brokered OpenCode chat (unshipped) + +The protocol declares the brokered `chat_*` surface (`chat_sessions`, +`chat_create`, `chat_prompt`, `chat_messages`, `chat_interrupt`, `chat_reply`). The +server dispatches `chat_prompt`, `chat_messages`, `chat_interrupt` and `chat_reply`, +answers the declared `chat_sessions`/`chat_create` with an explicit protocol error +because they are not yet brokered, and pushes `chat_event` frames derived from +OpenCode's global `/api/event` firehose, with retained per-session/per-message state +so a reconnecting client re-syncs instead of replaying deltas. This is an internal, unshipped capability: no client consumes it, +and chat is enabled only when the OpenCode connection exposes a URL-bearing SSE +endpoint (the owned v1 serve process). On a V2 CLI (`opencode api`, no owned serve) +that endpoint is absent, so every dispatched `chat_*` request returns a protocol error +naming the missing configuration. Do not describe brokered chat as a finished or +user-facing feature. + ## Debug and retry Debug opens a new conversation with retained failure context, exact definition and diff --git a/internal/opencode/chat.go b/internal/opencode/chat.go new file mode 100644 index 00000000..dd0cc386 --- /dev/null +++ b/internal/opencode/chat.go @@ -0,0 +1,355 @@ +package opencode + +import ( + "bufio" + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "strings" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// Chat is the brokered OpenCode chat adapter (route B in the research): the +// Pipelines server owns the OpenCode endpoint and credentials and turns the v2 +// HTTP API into protocol.ChatEventFrame frames. It reuses the owned-endpoint +// semantics, the session-id shape and the 64 KiB / 1 MiB bounds of Connection so +// no second transport or credential path is introduced. +type Chat struct { + conn *Connection +} + +// NewChat brokers chat for one owned OpenCode endpoint. The connection must be +// able to reach the /api HTTP surface: an owned serve connection (translated by +// Connection.Call) or a connection that already carries the v2 API URL. +func NewChat(conn *Connection) *Chat { return &Chat{conn: conn} } + +// ValidSessionID reports whether id has the OpenCode session-id shape the +// native adapter already accepts. It is the single validation shared by the +// chat adapter and callers that must reject a request before any OpenCode call. +func ValidSessionID(id string) bool { return sessionID.MatchString(id) } + +// Prompt forwards one chat prompt through the existing Call translation +// (authenticated, bounded, directory-scoped). The endpoint returns the inbox +// envelope immediately; the reply arrives on Stream. +func (c *Chat) Prompt(ctx context.Context, id, text string) error { + if !sessionID.MatchString(id) { + return fmt.Errorf("invalid OpenCode session id") + } + if strings.TrimSpace(text) == "" { + return fmt.Errorf("empty chat prompt") + } + body := map[string]any{"parts": []map[string]string{{"type": "text", "text": text}}} + if _, err := c.conn.Call(ctx, "post", "/api/session/"+id+"/prompt", body); err != nil { + return err + } + return nil +} + +// Interrupt aborts the session's active run, reusing the same translated surface. +func (c *Chat) Interrupt(ctx context.Context, id string) error { + if !sessionID.MatchString(id) { + return fmt.Errorf("invalid OpenCode session id") + } + if _, err := c.conn.Call(ctx, "post", "/api/session/"+id+"/interrupt", map[string]any{}); err != nil { + return err + } + return nil +} + +// Messages reads one session's stored turns. On the owned serve backend it +// reuses Connection.Messages, including its read-only OutputFormat readback +// fallback, instead of duplicating that path. It maps the verified assistant +// content parts (text | reasoning | tool) onto protocol.ChatMessage. +func (c *Chat) Messages(ctx context.Context, id string, limit int) ([]protocol.ChatMessage, error) { + if !sessionID.MatchString(id) { + return nil, fmt.Errorf("invalid OpenCode session id") + } + var data []byte + var err error + if c.conn.Backend == "v1" { + data, err = c.conn.Messages(ctx, id) + } else { + path := "/api/session/" + id + "/message" + if limit > 0 { + path += fmt.Sprintf("?limit=%d", limit) + } + data, err = c.conn.Call(ctx, "get", path, nil) + } + if err != nil { + return nil, err + } + return decodeChatMessages(id, data) +} + +// 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) { + return fmt.Errorf("invalid OpenCode session id") + } + if request.RequestID == "" { + return fmt.Errorf("missing permission request id") + } + decision := request.Decision + if decision != protocol.ChatDecisionOnce && decision != protocol.ChatDecisionAlways && decision != protocol.ChatDecisionReject { + return fmt.Errorf("invalid permission decision %q", decision) + } + body := map[string]any{"decision": decision} + if request.Message != "" { + body["message"] = request.Message + } + path := "/api/session/" + request.SessionID + "/permission/" + request.RequestID + "/reply" + if _, err := c.conn.Call(ctx, "post", path, body); err != nil { + return err + } + return nil +} + +// Stream subscribes to the OpenCode event firehose and invokes emit for every +// frame belonging to sessionID. It reads one bounded SSE frame at a time and +// never polls the per-session message log; the global firehose is the only +// source of token-level deltas on this build. ready, when non-nil, is called +// once the subscription is attached and before the first frame is read, so a +// caller can post its prompt only after the stream is listening (the firehose +// has no replay cursor). Canceling ctx cancels the subscription and returns +// without leaking a connection. +func (c *Chat) Stream(ctx context.Context, id string, ready func(), emit func(protocol.ChatEventFrame) error) error { + if !sessionID.MatchString(id) { + return fmt.Errorf("invalid OpenCode session id") + } + body, err := c.conn.EventStream(ctx) + if err != nil { + return err + } + defer body.Close() + if ready != nil { + ready() + } + return scanChatEvents(ctx, body, id, emit) +} + +// scanChatEvents turns an SSE body into protocol frames, one bounded frame at a +// time. Heartbeat comments, blank lines and unrelated event types are ignored; +// malformed JSON fails the stream instead of producing a malformed frame. The +// last assistant message id is retained so a later tool/step event that omits it +// still attributes every frame to a stable message. +func scanChatEvents(ctx context.Context, body io.Reader, sessionID string, emit func(protocol.ChatEventFrame) error) error { + scanner := bufio.NewScanner(body) + scanner.Buffer(make([]byte, 0, 64<<10), MaxResponse) + lastMessageID := "" + for scanner.Scan() { + if err := ctx.Err(); err != nil { + return err + } + line := bytes.TrimSpace(scanner.Bytes()) + if !bytes.HasPrefix(line, []byte("data:")) { + continue + } + payload := bytes.TrimSpace(line[len("data:"):]) + if len(payload) == 0 { + continue + } + frame, ok, err := NormalizeChatEvent(payload, sessionID, &lastMessageID) + if err != nil { + return err + } + if !ok { + continue + } + if frame.MessageID != "" { + lastMessageID = frame.MessageID + } + if err := emit(frame); err != nil { + return err + } + } + if err := scanner.Err(); err != nil { + if ctx.Err() != nil { + return ctx.Err() + } + return fmt.Errorf("OpenCode event stream read failed") + } + return ctx.Err() +} + +// chatEvent is the subset of the verified firehose envelope this adapter reads: +// {"type":"session.text.delta","data":{...}}. Fields absent from a given event +// stay zero so one struct covers every type. +type chatEvent struct { + SessionID string `json:"sessionID"` + MessageID string `json:"assistantMessageID"` + MessageIDAlt string `json:"messageID"` + Delta string `json:"delta"` + ID string `json:"id"` + Name string `json:"name"` + Input json.RawMessage `json:"input"` + Content []protocol.ChatToolContent `json:"content"` + Metadata map[string]any `json:"metadata"` + Error *protocol.ChatError `json:"error"` + Ordinal int `json:"ordinal"` + Index int `json:"index"` + Step int `json:"step"` + InputTokens int `json:"inputTokens"` + OutputTokens int `json:"outputTokens"` + Cost float64 `json:"cost"` + Tokens struct { + Input int `json:"input"` + Output int `json:"output"` + } `json:"tokens"` +} + +// NormalizeChatEvent maps one OpenCode firehose `data:` payload to a +// protocol.ChatEventFrame. ok is false for an event type this adapter does not +// surface or one belonging to a different session, so the caller can skip it +// without emitting a frame. Malformed JSON returns an error and never a frame. +func NormalizeChatEvent(payload []byte, sessionID string, lastMessageID *string) (protocol.ChatEventFrame, bool, error) { + var outer struct { + Type string `json:"type"` + Data json.RawMessage `json:"data"` + } + if err := json.Unmarshal(payload, &outer); err != nil { + return protocol.ChatEventFrame{}, false, fmt.Errorf("malformed OpenCode event: %w", err) + } + if outer.Type == "" || len(outer.Data) == 0 { + return protocol.ChatEventFrame{}, false, nil + } + var data chatEvent + if err := json.Unmarshal(outer.Data, &data); err != nil { + return protocol.ChatEventFrame{}, false, fmt.Errorf("malformed OpenCode event data: %w", err) + } + if data.SessionID != sessionID { + return protocol.ChatEventFrame{}, false, nil + } + messageID := data.MessageID + if messageID == "" { + messageID = data.MessageIDAlt + } + if messageID == "" && lastMessageID != nil { + messageID = *lastMessageID + } + // Every pushed frame must carry a stable message id so the client can + // attribute it; without one this is not a chat frame. + if messageID == "" { + return protocol.ChatEventFrame{}, false, nil + } + frame := protocol.ChatEventFrame{SessionID: sessionID, MessageID: messageID} + switch { + case outer.Type == "session.text.delta": + frame.Kind = protocol.ChatEventText + frame.Delta = data.Delta + frame.Part = &protocol.ChatPart{Type: protocol.ChatPartText, ID: fmt.Sprintf("%s#%d", messageID, data.Ordinal)} + case outer.Type == "session.reasoning.delta": + frame.Kind = protocol.ChatEventReasoning + frame.Delta = data.Delta + frame.Part = &protocol.ChatPart{Type: protocol.ChatPartReasoning, ID: fmt.Sprintf("%s#%d", messageID, data.Ordinal)} + case outer.Type == "session.step.started" || outer.Type == "session.step.streamed" || outer.Type == "session.step.ended": + frame.Kind = protocol.ChatEventStep + frame.Step = &protocol.ChatStep{Status: strings.TrimPrefix(outer.Type, "session.step."), Index: stepIndex(data)} + case strings.HasPrefix(outer.Type, "session.tool."): + status, ok := toolStatus(outer.Type) + if !ok { + return protocol.ChatEventFrame{}, false, nil + } + frame.Kind = protocol.ChatEventTool + state := &protocol.ChatToolState{Status: status, Input: data.Input, Content: data.Content, Metadata: data.Metadata, Error: data.Error} + frame.Part = &protocol.ChatPart{Type: protocol.ChatPartTool, ID: data.ID, Name: data.Name, State: state} + case outer.Type == "session.usage.updated": + frame.Kind = protocol.ChatEventUsage + input, output := data.InputTokens, data.OutputTokens + if data.Tokens.Input != 0 || data.Tokens.Output != 0 { + input, output = data.Tokens.Input, data.Tokens.Output + } + frame.Usage = &protocol.ChatUsage{InputTokens: input, OutputTokens: output, Cost: data.Cost} + default: + return protocol.ChatEventFrame{}, false, nil + } + return frame, true, nil +} + +// toolStatus maps the verified tool event types onto the ToolState lifecycle. +func toolStatus(eventType string) (string, bool) { + switch eventType { + case "session.tool.input.started", "session.tool.input.ended": + return protocol.ChatToolStreaming, true + case "session.tool.called", "session.tool.progress": + return protocol.ChatToolRunning, true + case "session.tool.success": + return protocol.ChatToolCompleted, true + case "session.tool.failed": + return protocol.ChatToolError, true + } + return "", false +} + +func stepIndex(data chatEvent) int { + if data.Step > 0 { + return data.Step + } + return data.Index +} + +// decodeChatMessages maps the native /message response onto the protocol chat +// history shape. Assistant content parts are text, reasoning and tool; a user +// turn is the prompt text. It never re-derives provider shapes. +type nativeChatMessage struct { + Info struct { + ID string `json:"id"` + Role string `json:"role"` + ModelID string `json:"modelID"` + Agent string `json:"agent"` + CreatedAt int64 `json:"created"` + } `json:"info"` + Parts []struct { + Type string `json:"type"` + ID string `json:"id"` + Text string `json:"text"` + Name string `json:"name"` + Executed bool `json:"executed"` + State *protocol.ChatToolState `json:"state"` + Time *protocol.ChatTime `json:"time"` + } `json:"parts"` +} + +// chatMessageRecords accepts both message envelopes the two surfaces return: +// the V2 HTTP API wraps them in {"data":[...]} while the native serve readback +// returns a bare array. An unrecognised body is an error, never empty history. +func chatMessageRecords(data []byte) ([]nativeChatMessage, error) { + var response struct { + Data []nativeChatMessage `json:"data"` + } + if err := json.Unmarshal(data, &response); err == nil && response.Data != nil { + return response.Data, nil + } + var records []nativeChatMessage + if err := json.Unmarshal(data, &records); err != nil { + return nil, fmt.Errorf("invalid OpenCode message response") + } + return records, nil +} + +func decodeChatMessages(sessionID string, data []byte) ([]protocol.ChatMessage, error) { + records, err := chatMessageRecords(data) + if err != nil { + return nil, err + } + messages := make([]protocol.ChatMessage, 0, len(records)) + for _, message := range records { + role := message.Info.Role + if role != protocol.ChatRoleUser && role != protocol.ChatRoleAssistant { + continue + } + out := protocol.ChatMessage{ID: message.Info.ID, SessionID: sessionID, Role: role, Model: message.Info.ModelID, Agent: message.Info.Agent, CreatedAt: message.Info.CreatedAt} + for _, part := range message.Parts { + switch part.Type { + case protocol.ChatPartText, protocol.ChatPartReasoning: + out.Parts = append(out.Parts, protocol.ChatPart{Type: part.Type, ID: part.ID, Text: part.Text}) + case protocol.ChatPartTool: + out.Parts = append(out.Parts, protocol.ChatPart{Type: protocol.ChatPartTool, ID: part.ID, Name: part.Name, Executed: part.Executed, State: part.State, Time: part.Time}) + } + } + messages = append(messages, out) + } + return messages, nil +} diff --git a/internal/opencode/chat_test.go b/internal/opencode/chat_test.go new file mode 100644 index 00000000..ac78a477 --- /dev/null +++ b/internal/opencode/chat_test.go @@ -0,0 +1,292 @@ +package opencode + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "sync/atomic" + "testing" + "time" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +func TestNormalizeChatEvent(t *testing.T) { + last := "msg_prev" + cases := []struct { + name string + payload string + last *string + wantOK bool + wantKind string + wantMsg string + wantDelta string + wantStatus string + wantStep string + wantUsage *protocol.ChatUsage + }{ + {name: "text delta", payload: `{"type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"hel"}}`, wantOK: true, wantKind: protocol.ChatEventText, wantMsg: "msg_1", wantDelta: "hel"}, + {name: "reasoning delta", payload: `{"type":"session.reasoning.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":1,"delta":"hmm"}}`, wantOK: true, wantKind: protocol.ChatEventReasoning, wantMsg: "msg_1", wantDelta: "hmm"}, + {name: "step ended", payload: `{"type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","step":2}}`, wantOK: true, wantKind: protocol.ChatEventStep, wantStep: protocol.ChatStepEnded}, + {name: "tool streaming", payload: `{"type":"session.tool.input.started","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","input":"{\"command\""}}`, wantOK: true, wantKind: protocol.ChatEventTool, wantStatus: protocol.ChatToolStreaming}, + {name: "tool running", payload: `{"type":"session.tool.called","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","input":{"command":"ls"}}}`, wantOK: true, wantKind: protocol.ChatEventTool, wantStatus: protocol.ChatToolRunning}, + {name: "tool completed", payload: `{"type":"session.tool.success","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","content":[{"type":"text","text":"ok"}]}}`, wantOK: true, wantKind: protocol.ChatEventTool, wantStatus: protocol.ChatToolCompleted}, + {name: "tool failed", payload: `{"type":"session.tool.failed","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","error":{"type":"tool","message":"boom"}}}`, wantOK: true, wantKind: protocol.ChatEventTool, wantStatus: protocol.ChatToolError}, + {name: "usage", payload: `{"type":"session.usage.updated","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","tokens":{"input":12,"output":34},"cost":0.001}}`, wantOK: true, wantKind: protocol.ChatEventUsage, wantUsage: &protocol.ChatUsage{InputTokens: 12, OutputTokens: 34, Cost: 0.001}}, + {name: "message id fallback", payload: `{"type":"session.step.started","data":{"sessionID":"ses_1","index":1}}`, last: &last, wantOK: true, wantKind: protocol.ChatEventStep, wantMsg: "msg_prev", wantStep: protocol.ChatStepStarted}, + {name: "foreign session skipped", payload: `{"type":"session.text.delta","data":{"sessionID":"ses_other","delta":"x"}}`, wantOK: false}, + {name: "unknown type skipped", payload: `{"type":"session.created","data":{"sessionID":"ses_1"}}`, wantOK: false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + lastID := tc.last + frame, ok, err := NormalizeChatEvent([]byte(tc.payload), "ses_1", lastID) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if ok != tc.wantOK { + t.Fatalf("ok = %v, want %v", ok, tc.wantOK) + } + if !ok { + return + } + if frame.SessionID != "ses_1" || frame.Kind != tc.wantKind { + t.Fatalf("frame = %+v", frame) + } + if tc.wantMsg != "" && frame.MessageID != tc.wantMsg { + t.Fatalf("messageID = %q, want %q", frame.MessageID, tc.wantMsg) + } + if tc.wantDelta != "" && frame.Delta != tc.wantDelta { + t.Fatalf("delta = %q, want %q", frame.Delta, tc.wantDelta) + } + if tc.wantStatus != "" && (frame.Part == nil || frame.Part.State == nil || frame.Part.State.Status != tc.wantStatus) { + t.Fatalf("tool state = %+v, want status %q", frame.Part, tc.wantStatus) + } + if tc.wantStep != "" && (frame.Step == nil || frame.Step.Status != tc.wantStep) { + t.Fatalf("step = %+v, want %q", frame.Step, tc.wantStep) + } + if tc.wantUsage != nil && (frame.Usage == nil || *frame.Usage != *tc.wantUsage) { + t.Fatalf("usage = %+v, want %+v", frame.Usage, tc.wantUsage) + } + }) + } +} + +func TestNormalizeChatEventRejectsMalformedJSON(t *testing.T) { + if _, _, err := NormalizeChatEvent([]byte(`{"type":`), "ses_1", nil); err == nil { + t.Fatal("malformed event must return an error, not a frame") + } +} + +func TestChatStreamFiltersFrames(t *testing.T) { + var prompts atomic.Int32 + frames := []string{ + `{"id":"evt_1","type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"hello "}}`, + `{"id":"evt_2","type":"session.text.delta","data":{"sessionID":"ses_other","assistantMessageID":"msg_x","ordinal":0,"delta":"ignore"}}`, + `{"id":"evt_3","type":"session.tool.called","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","input":{"command":"ls"}}}`, + `{"id":"evt_4","type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","step":1}}`, + } + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodPost && r.URL.Path == "/api/session/ses_1/prompt": + prompts.Add(1) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`)) + case r.Method == http.MethodGet && r.URL.Path == "/api/event": + w.Header().Set("Content-Type", "text/event-stream") + for _, frame := range frames { + _, _ = w.Write([]byte("data: " + frame + "\n\n")) + } + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + } + })) + defer srv.Close() + + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + var got []protocol.ChatEventFrame + err := chat.Stream(ctx, "ses_1", nil, func(frame protocol.ChatEventFrame) error { + got = append(got, frame) + return nil + }) + if err != nil { + t.Fatalf("stream: %v", err) + } + if len(got) != 3 { + t.Fatalf("got %d frames, want 3 (foreign session filtered): %+v", len(got), got) + } + if got[0].Kind != protocol.ChatEventText || got[0].Delta != "hello " || got[0].MessageID != "msg_1" || got[0].Part == nil || got[0].Part.ID == "" { + t.Fatalf("text frame = %+v", got[0]) + } + if got[1].Kind != protocol.ChatEventTool || got[1].Part == nil || got[1].Part.State == nil || got[1].Part.State.Status != protocol.ChatToolRunning { + t.Fatalf("tool frame = %+v", got[1]) + } + if got[2].Kind != protocol.ChatEventStep || got[2].Step == nil || got[2].Step.Status != protocol.ChatStepEnded { + t.Fatalf("step frame = %+v", got[2]) + } + for _, frame := range got { + if frame.SessionID != "ses_1" || frame.MessageID == "" { + t.Fatalf("frame missing stable ids: %+v", frame) + } + } + + if err := chat.Prompt(ctx, "ses_1", "hello"); err != nil { + t.Fatalf("prompt: %v", err) + } + if prompts.Load() != 1 { + t.Fatalf("prompt posts = %d, want 1", prompts.Load()) + } +} + +func TestChatRejectsInvalidSessionWithoutCalling(t *testing.T) { + var calls atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls.Add(1) + })) + defer srv.Close() + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + ctx := context.Background() + if err := chat.Prompt(ctx, "not-a-session", "hi"); err == nil { + t.Fatal("invalid session id must be rejected") + } + if err := chat.Stream(ctx, "", nil, func(protocol.ChatEventFrame) error { return nil }); err == nil { + t.Fatal("empty session id must be rejected") + } + if err := chat.Interrupt(ctx, "bad id"); err == nil { + t.Fatal("invalid interrupt session id must be rejected") + } + if calls.Load() != 0 { + t.Fatalf("invalid requests reached the service: %d", calls.Load()) + } +} + +func TestDecodeChatMessages(t *testing.T) { + raw := []byte(`{"data":[{"info":{"id":"msg_1","role":"assistant","modelID":"m","agent":"build","created":10},"parts":[{"type":"text","id":"p1","text":"hi"},{"type":"reasoning","id":"p2","text":"hmm"},{"type":"tool","id":"call_1","name":"shell","executed":true,"state":{"status":"completed","input":{"command":"ls"},"content":[{"type":"text","text":"ok"}]},"time":{"created":1,"ran":2,"completed":3}}]}]}`) + messages, err := decodeChatMessages("ses_1", raw) + if err != nil { + t.Fatal(err) + } + if len(messages) != 1 || messages[0].Role != protocol.ChatRoleAssistant || len(messages[0].Parts) != 3 { + t.Fatalf("messages = %+v", messages) + } + tool := messages[0].Parts[2] + if tool.Type != protocol.ChatPartTool || tool.Name != "shell" || tool.State == nil || tool.State.Status != protocol.ChatToolCompleted || tool.Time == nil { + t.Fatalf("tool part = %+v", tool) + } +} + +func TestChatStreamRejectsMalformedEvent(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: {\"type\":\"session.text.delta\",\"data\":{\"sessionID\":\"ses_1\"\n\n")) + })) + defer srv.Close() + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + emitted := 0 + err := chat.Stream(context.Background(), "ses_1", nil, func(protocol.ChatEventFrame) error { + emitted++ + return nil + }) + if err == nil { + t.Fatal("malformed event JSON must fail the stream") + } + if emitted != 0 { + t.Fatalf("malformed event produced %d frames", emitted) + } +} + +func TestChatStreamRequiresEndpoint(t *testing.T) { + chat := NewChat(&Connection{Backend: "v2"}) + err := chat.Stream(context.Background(), "ses_1", nil, func(protocol.ChatEventFrame) error { return nil }) + if err == nil || !strings.Contains(err.Error(), "event-stream endpoint") { + t.Fatalf("expected endpoint error, got %v", err) + } +} + +func TestNormalizeChatEventWithoutMessageID(t *testing.T) { + for _, payload := range []string{ + `{"type":"session.text.delta","data":{"sessionID":"ses_1","ordinal":0,"delta":"x"}}`, + `{"type":"session.tool.called","data":{"sessionID":"ses_1","id":"call_1","name":"shell","input":{"command":"ls"}}}`, + `{"type":"session.usage.updated","data":{"sessionID":"ses_1","tokens":{"input":1,"output":2}}}`, + } { + if _, ok, err := NormalizeChatEvent([]byte(payload), "ses_1", nil); err != nil || ok { + t.Fatalf("event without a message id produced a frame: ok=%v err=%v", ok, err) + } + } +} + +// TestChatV1ConnectionDrivesThroughCall drives the adapter through a real +// (serve-shaped) Connection so the /api -> native translation and the owned +// endpoint are exercised, not a direct v2 URL shortcut. +func TestChatV1ConnectionDrivesThroughCall(t *testing.T) { + var sawPrompt, sawMessages bool + var promptBody string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodPost && r.URL.Path == "/session/ses_1/message": + // Call must have translated /api/session/{id}/prompt to this path. + sawPrompt = true + body, _ := io.ReadAll(r.Body) + promptBody = string(body) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"ok":true}`)) + case r.Method == http.MethodGet && r.URL.Path == "/session/ses_1/message": + sawMessages = true + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`[{"info":{"id":"msg_1","role":"assistant","modelID":"m","agent":"build","created":10},"parts":[{"type":"text","id":"p1","text":"hi"}]}]`)) + case r.Method == http.MethodGet && r.URL.Path == "/event": + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: {\"type\":\"session.text.delta\",\"data\":{\"sessionID\":\"ses_1\",\"assistantMessageID\":\"msg_1\",\"ordinal\":0,\"delta\":\"hi\"}}\n\n")) + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + } + })) + defer srv.Close() + + chat := NewChat(&Connection{Backend: "v1", URL: srv.URL, Directory: "/repo", models: map[string]map[string]string{}, responses: map[string][]byte{}}) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := chat.Prompt(ctx, "ses_1", "hello"); err != nil { + t.Fatalf("prompt: %v", err) + } + if !sawPrompt { + t.Fatal("prompt was not translated to the native path") + } + // The translated body must carry the prompt text: the chat adapter sends the + // V2 `parts` shape and the v1 translation has to preserve it, or the native + // serve process receives an empty prompt. + var sent struct { + Parts []struct { + Type string `json:"type"` + Text string `json:"text"` + } `json:"parts"` + } + if err := json.Unmarshal([]byte(promptBody), &sent); err != nil { + t.Fatalf("prompt body: %v (%s)", err, promptBody) + } + if len(sent.Parts) != 1 || sent.Parts[0].Type != protocol.ChatPartText || sent.Parts[0].Text != "hello" { + t.Fatalf("prompt text not carried through the v1 translation: %s", promptBody) + } + var got []protocol.ChatEventFrame + if err := chat.Stream(ctx, "ses_1", nil, func(frame protocol.ChatEventFrame) error { + got = append(got, frame) + return nil + }); err != nil { + t.Fatalf("stream: %v", err) + } + if len(got) != 1 || got[0].Kind != protocol.ChatEventText || got[0].MessageID != "msg_1" { + t.Fatalf("stream frames = %+v", got) + } + messages, err := chat.Messages(ctx, "ses_1", 0) + if err != nil { + t.Fatalf("messages: %v", err) + } + if !sawMessages || len(messages) != 1 || messages[0].ID != "msg_1" { + t.Fatalf("messages = %+v", messages) + } +} diff --git a/internal/opencode/connection.go b/internal/opencode/connection.go index ac01b802..fca80cb3 100644 --- a/internal/opencode/connection.go +++ b/internal/opencode/connection.go @@ -277,3 +277,40 @@ func (c *Connection) Native(ctx context.Context, method, path string, body any) } return data, nil } + +// EventStream opens the authenticated SSE firehose for the connection's owned +// endpoint using the same URL and credentials as Native, so chat never builds a +// parallel HTTP client. The path follows the backend surface: the native serve +// stream is /event, the V2 HTTP API is /api/event. The caller closes the body. +func (c *Connection) EventStream(ctx context.Context) (io.ReadCloser, error) { + if c == nil || c.URL == "" { + return nil, fmt.Errorf("OpenCode chat requires an event-stream endpoint") + } + path := "/api/event" + if c.Backend == "v1" { + path = "/event" + } + request, err := http.NewRequestWithContext(ctx, http.MethodGet, strings.TrimSuffix(c.URL, "/")+path, nil) + if err != nil { + return nil, err + } + request.Header.Set("Accept", "text/event-stream") + request.SetBasicAuth("pipelines", c.password) + response, err := http.DefaultClient.Do(request) + if err != nil { + if ctx.Err() != nil { + return nil, ctx.Err() + } + return nil, fmt.Errorf("OpenCode event stream request failed") + } + if response.StatusCode < 200 || response.StatusCode >= 300 { + evidence, _ := io.ReadAll(io.LimitReader(response.Body, 16<<10)) + response.Body.Close() + return nil, fmt.Errorf("OpenCode event stream failed (HTTP %d): %s", response.StatusCode, strings.TrimSpace(string(evidence))) + } + if !strings.HasPrefix(response.Header.Get("Content-Type"), "text/event-stream") { + response.Body.Close() + return nil, fmt.Errorf("OpenCode event stream returned a non-SSE response") + } + return response.Body, nil +} diff --git a/internal/opencode/native.go b/internal/opencode/native.go index 6f8842b9..5a4ed4fe 100644 --- a/internal/opencode/native.go +++ b/internal/opencode/native.go @@ -65,9 +65,14 @@ func messageText(data []byte) (string, error) { // Call adapts the pipeline's existing V2 command contract to the native stable API. // Sessions still use the same account/database; only our loopback serve process is owned. +// A connection whose backend already speaks the /api HTTP surface forwards the path +// unchanged: only the v1 serve surface needs the /api -> native translation below. func (c *Connection) Call(ctx context.Context, method, path string, body any) ([]byte, error) { if c.Backend != "v1" { - return nil, fmt.Errorf("native adapter requires the serve backend") + if c.URL == "" { + return nil, fmt.Errorf("native adapter requires the serve backend") + } + return c.Native(ctx, method, path, body) } if path == "/api/session" && method == "post" { data, _ := json.Marshal(body) @@ -152,6 +157,9 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([ Prompt string `json:"prompt"` Text string `json:"text"` Format json.RawMessage `json:"format"` + Parts []struct { + Text string `json:"text"` + } `json:"parts"` } if err := json.Unmarshal(data, &request); err != nil { return nil, err @@ -160,6 +168,17 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([ if text == "" { text = request.Text } + // The brokered chat adapter sends the V2 prompt shape (a parts array); + // accept it so the native translation carries the text unchanged instead + // of posting an empty prompt. + if text == "" { + for _, part := range request.Parts { + if part.Text != "" { + text = part.Text + break + } + } + } payload := map[string]any{"model": c.models[id], "parts": []map[string]string{{"type": "text", "text": text}}} if len(request.Format) > 0 { supported, err := c.supportsFormat(ctx, request.Format) diff --git a/internal/server/chat.go b/internal/server/chat.go new file mode 100644 index 00000000..08b579cc --- /dev/null +++ b/internal/server/chat.go @@ -0,0 +1,458 @@ +package server + +import ( + "context" + "log" + "strings" + "sync" + "time" + + "git.a-wan.net/alan/pipelines/internal/opencode" + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// chatBroker is the server-side OpenCode chat seam. *opencode.Chat implements +// it; tests substitute a scripted broker. Keeping it an interface means the +// /connect handler never builds a second OpenCode transport. +type chatBroker interface { + Prompt(ctx context.Context, sessionID, text string) error + // Stream subscribes to the session's OpenCode event stream. ready is called + // once the subscription is attached, before any frame is read, so the caller + // can order the prompt after the subscription. + Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error + Interrupt(ctx context.Context, sessionID string) error + Messages(ctx context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error) + Reply(ctx context.Context, request protocol.ChatReplyRequest) error +} + +// chatRetainedLimit bounds the state one session may retain so a stalled reader +// or a runaway turn cannot grow memory without limit. It matches the OpenCode +// response bound (1 MiB). +const chatRetainedLimit = opencode.MaxResponse + +// chatMaxTurns bounds how many sessions retain turn state. A long-lived server +// cannot accumulate state for every session ever prompted, so the +// least-recently-updated turn is evicted once the cap is exceeded. +const chatMaxTurns = 128 + +// chatHub retains the accumulated turn state per session, independently of any +// single connection, so a reconnecting client can re-sync what it missed. State +// is keyed by session and then by message id (a session holds many turns), and +// is capped at chatRetainedLimit. +type chatHub struct { + mu sync.Mutex + turns map[string]*chatTurn + now func() time.Time +} + +// chatPart is the retained form of one part. Text and reasoning deltas append to +// text (amortized O(1)); a tool frame replaces part outright. +type chatPart struct { + part protocol.ChatPart + text []byte +} + +// chatMessage is one retained assistant turn. Its parts keep the exact ids the +// supporting chat_event frames carried, so a resume preserves attribution. +type chatMessage struct { + id string + order []string + parts map[string]*chatPart +} + +type chatTurn struct { + sessionID string + order []string + messages map[string]*chatMessage + step *protocol.ChatStep + usage *protocol.ChatUsage + size int + overflow bool + updated time.Time +} + +func newChatHub() *chatHub { + return &chatHub{turns: map[string]*chatTurn{}, now: time.Now} +} + +func (h *chatHub) clock() time.Time { + if h.now != nil { + return h.now() + } + return time.Now() +} + +// apply folds one pushed frame into the retained state. Text and reasoning +// deltas append to their message's part; a tool frame upserts the full part +// (accounting for its whole size) so the latest lifecycle status, input, +// content, metadata, error and time are retained under the frame's message id. +// Step and usage frames update the session's latest progress. +func (h *chatHub) apply(frame protocol.ChatEventFrame) { + if frame.SessionID == "" { + return + } + h.mu.Lock() + defer h.mu.Unlock() + turn := h.turns[frame.SessionID] + if turn == nil { + h.evictLocked() + turn = &chatTurn{sessionID: frame.SessionID, messages: map[string]*chatMessage{}} + h.turns[frame.SessionID] = turn + } + turn.updated = h.clock() + switch frame.Kind { + case protocol.ChatEventStep: + turn.step = frame.Step + return + case protocol.ChatEventUsage: + turn.usage = frame.Usage + return + } + if frame.MessageID == "" { + return + } + message := turn.messages[frame.MessageID] + if message == nil { + message = &chatMessage{id: frame.MessageID, parts: map[string]*chatPart{}} + turn.order = append(turn.order, frame.MessageID) + turn.messages[frame.MessageID] = message + } + switch frame.Kind { + case protocol.ChatEventText, protocol.ChatEventReasoning: + id := "" + if frame.Part != nil { + id = frame.Part.ID + } + if id == "" || frame.Delta == "" { + return + } + if !turn.retain(len(frame.Delta)) { + return + } + part, ok := message.parts[id] + if !ok { + part = &chatPart{part: protocol.ChatPart{Type: frame.Kind, ID: id}} + message.order = append(message.order, id) + message.parts[id] = part + } + part.text = append(part.text, frame.Delta...) + case protocol.ChatEventTool: + if frame.Part == nil || frame.Part.ID == "" || turn.overflow { + return + } + // The tool part is retained whole, so size the complete part (input, + // content, metadata, error and time) and charge the difference against + // the cap on every upsert, not just the first-seen name. + incoming := chatPartBytes(frame.Part) + part, ok := message.parts[frame.Part.ID] + if !ok { + if !turn.retain(incoming) { + return + } + part = &chatPart{} + message.order = append(message.order, frame.Part.ID) + message.parts[frame.Part.ID] = part + } else if previous := chatPartBytes(&part.part); incoming > previous { + if !turn.retain(incoming - previous) { + return + } + } else { + turn.shrink(previous - incoming) + } + part.part = *frame.Part + } +} + +// chatPartBytes estimates the retained size of a part so the per-session cap +// accounts for its whole state instead of only the first-seen name. It walks the +// fields rather than re-marshalling, so the cost matches the bytes it stores. +func chatPartBytes(part *protocol.ChatPart) int { + if part == nil { + return 0 + } + n := len(part.Type) + len(part.ID) + len(part.Name) + if part.State == nil { + return n + } + n += len(part.State.Status) + len(part.State.Input) + for _, content := range part.State.Content { + n += len(content.Type) + len(content.Text) + len(content.URI) + len(content.Mime) + len(content.Name) + } + if part.State.Error != nil { + n += len(part.State.Error.Type) + len(part.State.Error.Message) + } + for key, value := range part.State.Metadata { + n += len(key) + if text, ok := value.(string); ok { + n += len(text) + } else { + n += 8 + } + } + return n +} + +// evictLocked drops the least-recently-updated turn when the session cap is +// reached, keeping retained memory bounded across session churn. +func (h *chatHub) evictLocked() { + if len(h.turns) < chatMaxTurns { + return + } + oldestID := "" + var oldest time.Time + for id, turn := range h.turns { + if oldestID == "" || turn.updated.Before(oldest) { + oldestID, oldest = id, turn.updated + } + } + if oldestID != "" { + delete(h.turns, oldestID) + } +} + +// retain records n more bytes and reports whether the turn is still within the +// bound. The first overflow records one diagnostic and then drops further state +// instead of growing without limit. +func (t *chatTurn) retain(n int) bool { + if t.overflow { + return false + } + if t.size+n > chatRetainedLimit { + t.overflow = true + log.Printf("chat session %s retained state exceeds %d bytes; dropping further state", t.sessionID, chatRetainedLimit) + return false + } + t.size += n + return true +} + +// shrink releases n bytes when a tool upsert replaces a larger retained part. +func (t *chatTurn) shrink(n int) { + t.size -= n + if t.size < 0 { + t.size = 0 + } +} + +// snapshot returns the accumulated session as one assistant message per +// retained message id, with the same stable part ids, or nil when nothing is +// retained. It is the resume source: a reconnecting client re-syncs the whole +// session once instead of replaying deltas, and each turn keeps its own id. +func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage { + h.mu.Lock() + defer h.mu.Unlock() + turn := h.turns[sessionID] + if turn == nil { + return nil + } + turn.updated = h.clock() + var messages []protocol.ChatMessage + for _, id := range turn.order { + message := turn.messages[id] + if message == nil || len(message.order) == 0 { + continue + } + out := protocol.ChatMessage{ID: id, SessionID: sessionID, Role: protocol.ChatRoleAssistant} + for _, partID := range message.order { + if part, ok := message.parts[partID]; ok { + piece := part.part + piece.Text = string(part.text) + out.Parts = append(out.Parts, piece) + } + } + messages = append(messages, out) + } + return messages +} + +// handleChat dispatches one chat_* request. It returns true when the request was +// a chat type and has been fully answered (the caller must not fall through to +// the generic response). Frames are pushed after the ack, each through the +// connection-scoped write function so the read loop and the stream goroutine +// never interleave partial websocket writes. +func handleChat(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) bool { + switch request.Type { + case protocol.ChatPrompt: + handleChatPrompt(ctx, request, broker, hub, write) + return true + case protocol.ChatMessages: + handleChatMessages(ctx, request, broker, hub, write) + return true + case protocol.ChatInterrupt: + handleChatSimple(ctx, request, broker, hub, write) + return true + case protocol.ChatReply: + handleChatSimple(ctx, request, broker, hub, write) + return true + case protocol.ChatSessions, protocol.ChatCreate: + // Declared in the protocol for the client but not yet brokered. Answer + // explicitly so the client never mistakes the generic Pong for a chat + // reply, and the boundary stays visible instead of silently dropping. + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat_sessions and chat_create are not yet brokered"}) + return true + } + return false +} + +func handleChatPrompt(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, 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 + } + prompt := request.ChatPrompt + if prompt == nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a chat prompt payload is required"}) + return + } + if !opencode.ValidSessionID(prompt.SessionID) { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) + return + } + if strings.TrimSpace(prompt.Text) == "" { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a nonempty chat prompt is required"}) + return + } + // Reject an oversized prompt before any OpenCode call; the bound matches the + // OpenCode request limit. + if len(prompt.Text) > opencode.MaxRequest { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat prompt exceeds 64 KiB"}) + return + } + + // Subscribe to the OpenCode firehose before posting the prompt: the stream + // has no replay cursor (research 2.4), so a delta emitted between the POST + // and the subscription would otherwise be lost. Frames that arrive before the + // ack is written are buffered and flushed in order after it. + sessionID := prompt.SessionID + streamCtx, streamCancel := context.WithCancel(ctx) + attached := make(chan struct{}) + var attachOnce sync.Once + streamResult := make(chan error, 1) + var bufferMu sync.Mutex + buffering := true + var buffered []protocol.ChatEventFrame + go func() { + streamResult <- broker.Stream(streamCtx, sessionID, func() { + attachOnce.Do(func() { close(attached) }) + }, func(frame protocol.ChatEventFrame) error { + bufferMu.Lock() + if buffering { + buffered = append(buffered, frame) + bufferMu.Unlock() + return nil + } + bufferMu.Unlock() + hub.apply(frame) + return write(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}) + }) + }() + + select { + case <-attached: + case err := <-streamResult: + streamCancel() + if err != nil && ctx.Err() == nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat stream: " + err.Error()}) + } + return + case <-ctx.Done(): + streamCancel() + return + } + + if err := broker.Prompt(ctx, sessionID, prompt.Text); err != nil { + streamCancel() + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + // Acknowledge the accepted prompt with the echoed request id, then stream the + // reply. The prompt is sent exactly once and never re-sent on resume. + if err := write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID}); err != nil { + streamCancel() + return + } + // Flush frames buffered while the prompt was accepted, then resume live + // delivery. Holding bufferMu preserves order: the stream goroutine either + // buffered before this flush or waits and writes after it. + bufferMu.Lock() + buffering = false + pending := buffered + buffered = nil + for _, frame := range pending { + hub.apply(frame) + if err := write(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}); err != nil { + bufferMu.Unlock() + streamCancel() + return + } + } + bufferMu.Unlock() + go drainChatStream(ctx, streamResult, streamCancel, sessionID, write) +} + +// drainChatStream reports the terminal error of a chat subscription and releases +// its context once the stream has ended. +func drainChatStream(ctx context.Context, result <-chan error, cancel context.CancelFunc, sessionID string, write func(protocol.Message) error) { + defer cancel() + if err := <-result; err != nil && ctx.Err() == nil { + log.Printf("chat stream session=%s: %v", sessionID, err) + _ = write(protocol.Message{Type: protocol.Error, Error: "chat stream: " + err.Error()}) + } +} + +func handleChatMessages(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) { + query := request.ChatQuery + if query == nil || !opencode.ValidSessionID(query.SessionID) { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) + return + } + response := protocol.Message{Type: protocol.ChatMessages, ID: request.ID} + // Resume from retained server state first: a reconnecting client gets the + // accumulated turns with the same stable ids without re-reading OpenCode. + if messages := hub.snapshot(query.SessionID); len(messages) > 0 { + response.ChatMessages = messages + _ = write(response) + return + } + if broker == nil { + _ = write(response) + return + } + messages, err := broker.Messages(ctx, query.SessionID, query.Limit) + if err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + response.ChatMessages = messages + _ = write(response) +} + +func handleChatSimple(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, 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 request.Type == protocol.ChatInterrupt { + query := request.ChatQuery + if query == nil || !opencode.ValidSessionID(query.SessionID) { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) + return + } + if err := broker.Interrupt(ctx, query.SessionID); err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + _ = write(protocol.Message{Type: protocol.ChatInterrupt, ID: request.ID}) + return + } + reply := request.ChatReply + if reply == nil || !opencode.ValidSessionID(reply.SessionID) { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) + return + } + if err := broker.Reply(ctx, *reply); err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + _ = write(protocol.Message{Type: protocol.ChatReply, ID: request.ID}) +} diff --git a/internal/server/chat_test.go b/internal/server/chat_test.go new file mode 100644 index 00000000..f300575b --- /dev/null +++ b/internal/server/chat_test.go @@ -0,0 +1,563 @@ +package server + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "sync" + "sync/atomic" + "testing" + "time" + + "github.com/coder/websocket" + "github.com/coder/websocket/wsjson" + + "git.a-wan.net/alan/pipelines/internal/opencode" + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// fakeChat is a scripted chatBroker. It counts calls and either emits a fixed +// frame sequence or blocks until the caller's context is canceled. +type fakeChat struct { + mu sync.Mutex + promptCalls int + interrupts int + replies int + messages []protocol.ChatMessage + script []protocol.ChatEventFrame + started chan struct{} + done chan struct{} + startOnce sync.Once + block bool + subscribed bool + promptBeforeSubscribe bool +} + +func (f *fakeChat) Prompt(context.Context, string, string) error { + f.mu.Lock() + defer f.mu.Unlock() + f.promptCalls++ + if !f.subscribed { + f.promptBeforeSubscribe = true + } + return nil +} + +func (f *fakeChat) Stream(ctx context.Context, _ string, ready func(), emit func(protocol.ChatEventFrame) error) error { + f.mu.Lock() + f.subscribed = true + f.mu.Unlock() + if ready != nil { + ready() + } + if f.started != nil { + f.startOnce.Do(func() { close(f.started) }) + } + if f.block { + <-ctx.Done() + if f.done != nil { + close(f.done) + } + return ctx.Err() + } + for _, frame := range f.script { + if err := ctx.Err(); err != nil { + return err + } + if err := emit(frame); err != nil { + return err + } + } + return nil +} + +func (f *fakeChat) Interrupt(context.Context, string) error { + f.mu.Lock() + defer f.mu.Unlock() + f.interrupts++ + return nil +} + +func (f *fakeChat) Messages(context.Context, string, int) ([]protocol.ChatMessage, error) { + return f.messages, nil +} + +func (f *fakeChat) Reply(context.Context, protocol.ChatReplyRequest) error { + f.mu.Lock() + defer f.mu.Unlock() + f.replies++ + return nil +} + +func (f *fakeChat) counts() (int, int, int) { + f.mu.Lock() + defer f.mu.Unlock() + return f.promptCalls, f.interrupts, f.replies +} + +func chatFrame(kind, delta string, part *protocol.ChatPart, step *protocol.ChatStep) protocol.ChatEventFrame { + return protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: kind, Delta: delta, Part: part, Step: step} +} + +func scriptedChat() *fakeChat { + return &fakeChat{script: []protocol.ChatEventFrame{ + chatFrame(protocol.ChatEventText, "hello ", &protocol.ChatPart{Type: protocol.ChatPartText, ID: "msg_1#0"}, nil), + chatFrame(protocol.ChatEventText, "world", &protocol.ChatPart{Type: protocol.ChatPartText, ID: "msg_1#0"}, nil), + chatFrame(protocol.ChatEventTool, "", &protocol.ChatPart{Type: protocol.ChatPartTool, ID: "call_1", Name: "shell", State: &protocol.ChatToolState{Status: protocol.ChatToolCompleted, Input: []byte(`{"command":"ls"}`)}}, nil), + chatFrame(protocol.ChatEventStep, "", nil, &protocol.ChatStep{Status: protocol.ChatStepEnded, Index: 1}), + }} +} + +func dialChat(t *testing.T, broker chatBroker) (context.Context, context.CancelFunc, *websocket.Conn) { + t.Helper() + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + srv := httptest.NewServer(HandlerWithOptions(ctx, Options{Chat: broker})) + t.Cleanup(func() { srv.Close(); cancel() }) + conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { conn.CloseNow() }) + return ctx, cancel, conn +} + +func readChatTurn(t *testing.T, ctx context.Context, conn *websocket.Conn) []protocol.ChatEventFrame { + t.Helper() + var frames []protocol.ChatEventFrame + for { + var message protocol.Message + if err := wsjson.Read(ctx, conn, &message); err != nil { + t.Fatal(err) + } + if message.Type == protocol.Error { + t.Fatalf("unexpected error frame: %s", message.Error) + } + if message.ChatEvent == nil { + continue + } + frames = append(frames, *message.ChatEvent) + if message.ChatEvent.Kind == protocol.ChatEventStep && message.ChatEvent.Step != nil && message.ChatEvent.Step.Status == protocol.ChatStepEnded { + return frames + } + } +} + +func TestChatPromptStreamsEvents(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); 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 != "r1" { + t.Fatalf("ack = %+v", ack) + } + frames := readChatTurn(t, ctx, conn) + if len(frames) != 4 { + t.Fatalf("got %d frames, want 4", len(frames)) + } + sawText, sawStep := false, false + for _, frame := range frames { + if frame.SessionID != "ses_1" || frame.MessageID != "msg_1" { + t.Fatalf("frame missing stable ids: %+v", frame) + } + switch frame.Kind { + case protocol.ChatEventText: + sawText = true + case protocol.ChatEventStep: + sawStep = true + if frame.Step == nil || frame.Step.Status != protocol.ChatStepEnded { + t.Fatalf("step frame = %+v", frame) + } + } + } + if !sawText || !sawStep { + t.Fatalf("missing text or step frame: %+v", frames) + } + if prompts, _, _ := broker.counts(); prompts != 1 { + t.Fatalf("prompt forwarded %d times, want 1", prompts) + } +} + +func TestChatPromptRejectsInvalidSession(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + for _, id := range []string{"", "not-a-session", "msg_1"} { + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "bad", ChatPrompt: &protocol.ChatPromptRequest{SessionID: id, Text: "hi"}}); 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 || response.ID != "bad" { + t.Fatalf("session %q not rejected: %+v", id, response) + } + } + if prompts, _, _ := broker.counts(); prompts != 0 { + t.Fatalf("invalid sessions reached OpenCode: %d", prompts) + } +} + +func TestChatPromptRejectsOversizedText(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + oversized := strings.Repeat("x", opencode.MaxRequest+1) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "big", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: oversized}}); 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, "64 KiB") { + t.Fatalf("oversized prompt not rejected: %+v", response) + } + if prompts, _, _ := broker.counts(); prompts != 0 { + t.Fatalf("oversized prompt reached OpenCode: %d", prompts) + } +} + +func TestChatPromptWithoutBroker(t *testing.T) { + ctx, _, conn := dialChat(t, nil) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); 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, "not configured") { + t.Fatalf("unconfigured chat not rejected: %+v", response) + } +} + +func TestChatMessagesResumesRetainedState(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil { + t.Fatal(err) + } + var ack protocol.Message + if err := wsjson.Read(ctx, conn, &ack); err != nil { + t.Fatal(err) + } + readChatTurn(t, ctx, conn) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "r2", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); 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.ChatMessages || response.ID != "r2" || len(response.ChatMessages) != 1 { + t.Fatalf("resume response = %+v", response) + } + message := response.ChatMessages[0] + if message.ID != "msg_1" || message.Role != protocol.ChatRoleAssistant || len(message.Parts) != 2 { + t.Fatalf("resumed message = %+v", message) + } + if message.Parts[0].Type != protocol.ChatPartText || message.Parts[0].ID != "msg_1#0" || message.Parts[0].Text != "hello world" { + t.Fatalf("resumed text part = %+v", message.Parts[0]) + } + if message.Parts[1].Type != protocol.ChatPartTool || message.Parts[1].State == nil || message.Parts[1].State.Status != protocol.ChatToolCompleted { + t.Fatalf("resumed tool part = %+v", message.Parts[1]) + } +} + +func TestChatDisconnectCancelsStream(t *testing.T) { + broker := scriptedChat() + broker.block = true + broker.started = make(chan struct{}) + broker.done = make(chan struct{}) + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil { + t.Fatal(err) + } + var ack protocol.Message + if err := wsjson.Read(ctx, conn, &ack); err != nil { + t.Fatal(err) + } + select { + case <-broker.started: + case <-time.After(2 * time.Second): + t.Fatal("stream never started") + } + conn.CloseNow() + // The server's read loop returns on disconnect, canceling the connection + // context the subscription observes; no goroutine may keep running. + select { + case <-broker.done: + case <-time.After(3 * time.Second): + t.Fatal("chat stream was not canceled on disconnect") + } + if prompts, _, _ := broker.counts(); prompts != 1 { + t.Fatalf("prompt forwarded %d times, want 1", prompts) + } +} + +func TestChatInterruptAndReply(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatInterrupt, ID: "i1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); 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.ChatInterrupt || response.ID != "i1" { + t.Fatalf("interrupt response = %+v", response) + } + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatReply, ID: "p1", ChatReply: &protocol.ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: protocol.ChatDecisionOnce}}); err != nil { + t.Fatal(err) + } + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.ChatReply || response.ID != "p1" { + t.Fatalf("reply response = %+v", response) + } + if _, interrupts, replies := broker.counts(); interrupts != 1 || replies != 1 { + t.Fatalf("interrupts=%d replies=%d, want 1/1", interrupts, replies) + } +} + +func TestChatHubAccumulatesAndEvicts(t *testing.T) { + tick := 0 + hub := newChatHub() + hub.now = func() time.Time { tick++; return time.Unix(int64(tick), 0) } + for _, delta := range []string{"a", "b", "c"} { + hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventText, Delta: delta, Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "msg_1#0"}}) + } + messages := hub.snapshot("ses_1") + if len(messages) != 1 || len(messages[0].Parts) != 1 || messages[0].Parts[0].Text != "abc" { + t.Fatalf("snapshot = %+v", messages) + } + for i := 0; i < chatMaxTurns+5; i++ { + id := fmt.Sprintf("ses_%d", i) + hub.apply(protocol.ChatEventFrame{SessionID: id, MessageID: "m", Kind: protocol.ChatEventText, Delta: "x", Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: id + "#0"}}) + } + if len(hub.turns) != chatMaxTurns { + t.Fatalf("retained %d turns, want %d", len(hub.turns), chatMaxTurns) + } +} + +// TestChatHubKeepsMessagesSeparate pins per-message retention: a second prompt +// in one session must not fold its parts onto the first turn's message id. +func TestChatHubKeepsMessagesSeparate(t *testing.T) { + hub := newChatHub() + hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventText, Delta: "one", Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "msg_1#0"}}) + hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_2", Kind: protocol.ChatEventText, Delta: "two", Part: &protocol.ChatPart{Type: protocol.ChatPartText, ID: "msg_2#0"}}) + messages := hub.snapshot("ses_1") + if len(messages) != 2 { + t.Fatalf("messages = %+v", messages) + } + if messages[0].ID != "msg_1" || messages[0].Parts[0].ID != "msg_1#0" || messages[0].Parts[0].Text != "one" { + t.Fatalf("first message = %+v", messages[0]) + } + if messages[1].ID != "msg_2" || messages[1].Parts[0].ID != "msg_2#0" || messages[1].Parts[0].Text != "two" { + t.Fatalf("second message = %+v", messages[1]) + } +} + +// TestChatHubChargesToolStateBytes pins the retained-state cap: a tool part's +// input/content bytes count against it, and an upsert that would exceed the cap +// is dropped with the overflow diagnostic rather than growing memory. +func TestChatHubChargesToolStateBytes(t *testing.T) { + hub := newChatHub() + big := strings.Repeat("x", chatRetainedLimit/2) + hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventTool, Part: &protocol.ChatPart{Type: protocol.ChatPartTool, ID: "call_1", Name: "shell", State: &protocol.ChatToolState{Status: protocol.ChatToolStreaming, Input: json.RawMessage(`"` + big + `"`)}}}) + hub.mu.Lock() + size := hub.turns["ses_1"].size + hub.mu.Unlock() + if size < len(big) { + t.Fatalf("tool input not charged: size=%d want >= %d", size, len(big)) + } + huge := strings.Repeat("y", chatRetainedLimit) + hub.apply(protocol.ChatEventFrame{SessionID: "ses_1", MessageID: "msg_1", Kind: protocol.ChatEventTool, Part: &protocol.ChatPart{Type: protocol.ChatPartTool, ID: "call_1", Name: "shell", State: &protocol.ChatToolState{Status: protocol.ChatToolCompleted, Input: json.RawMessage(`"` + huge + `"`)}}}) + messages := hub.snapshot("ses_1") + if len(messages) != 1 || len(messages[0].Parts) != 1 || messages[0].Parts[0].State == nil || messages[0].Parts[0].State.Status != protocol.ChatToolStreaming { + t.Fatalf("overflowing tool state was retained: %+v", messages) + } +} + +// TestChatPromptSubscribesBeforePrompt pins the subscribe-before-prompt order: +// the firehose has no replay cursor, so the subscription must be attached before +// the prompt is posted or deltas emitted in between are lost. +func TestChatPromptSubscribesBeforePrompt(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil { + t.Fatal(err) + } + var ack protocol.Message + if err := wsjson.Read(ctx, conn, &ack); err != nil { + t.Fatal(err) + } + readChatTurn(t, ctx, conn) + broker.mu.Lock() + before := broker.promptBeforeSubscribe + broker.mu.Unlock() + if before { + t.Fatal("prompt was posted before the event subscription was attached") + } +} + +// realAdapterChat posts a chat_prompt through the real opencode.Chat broker and +// HandlerWithOptions against a v2 HTTP fixture that replays frames, so the +// acceptance path forwards to an actual OpenCode-shaped stream (not a scripted +// fake) and returns the chat_event frames the server pushed. It pins exactly one +// prompt POST and one event subscription per chat_prompt, then asserts the ack +// echoes the request id; callers assert the frames themselves. +func realAdapterChat(t *testing.T, frames []string) []protocol.ChatEventFrame { + t.Helper() + var prompts, streams atomic.Int32 + fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodPost && r.URL.Path == "/api/session/ses_1/prompt": + prompts.Add(1) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`)) + case r.Method == http.MethodGet && r.URL.Path == "/api/event": + streams.Add(1) + w.Header().Set("Content-Type", "text/event-stream") + for _, frame := range frames { + _, _ = w.Write([]byte("data: " + frame + "\n\n")) + } + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + } + })) + defer fixture.Close() + + broker := opencode.NewChat(&opencode.Connection{Backend: "v2", URL: fixture.URL}) + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "ping"}}); 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 != "r1" { + t.Fatalf("ack = %+v", ack) + } + framesIn := readChatTurn(t, ctx, conn) + if prompts.Load() != 1 || streams.Load() != 1 { + t.Fatalf("prompts=%d streams=%d, want 1/1", prompts.Load(), streams.Load()) + } + return framesIn +} + +// assertStableFrameIDs fails unless every frame carries the fixture's session +// and message ids: a client cannot attribute a frame without them. +func assertStableFrameIDs(t *testing.T, frames []protocol.ChatEventFrame) { + t.Helper() + for _, frame := range frames { + if frame.SessionID != "ses_1" || frame.MessageID != "msg_1" { + t.Fatalf("frame missing stable ids: %+v", frame) + } + } +} + +// TestChatPromptThroughRealAdapter drives a text, tool and step turn end to end. +func TestChatPromptThroughRealAdapter(t *testing.T) { + framesIn := realAdapterChat(t, []string{ + `{"id":"evt_1","type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"pong"}}`, + `{"id":"evt_2","type":"session.tool.called","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell","input":{"command":"ls"}}}`, + `{"id":"evt_3","type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","step":1}}`, + }) + assertStableFrameIDs(t, framesIn) + sawText, sawTool := false, false + for _, frame := range framesIn { + if frame.Kind == protocol.ChatEventText && frame.Delta == "pong" { + sawText = true + } + if frame.Kind == protocol.ChatEventTool && frame.Part != nil && frame.Part.Name == "shell" { + sawTool = true + } + } + if !sawText || !sawTool { + t.Fatalf("missing text or tool frame: %+v", framesIn) + } +} + +// TestChatPromptNoToolThroughRealAdapter is the task's evidence harness: it posts +// a trivial no-tool prompt to the real opencode.Chat adapter over a v2 SSE +// fixture that emits only a text delta and a step, and asserts both arrive as +// chat_event frames with stable session/message ids. It reuses dialChat, +// readChatTurn and realAdapterChat so it pins the same ack/stream contract as the +// other chat tests, and it pins exactly one prompt POST and one event subscription. +func TestChatPromptNoToolThroughRealAdapter(t *testing.T) { + framesIn := realAdapterChat(t, []string{ + `{"id":"evt_1","type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"pong"}}`, + `{"id":"evt_2","type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","step":1}}`, + }) + assertStableFrameIDs(t, framesIn) + sawText, sawStep, sawTool := false, false, false + for _, frame := range framesIn { + switch frame.Kind { + case protocol.ChatEventText: + sawText = frame.Delta == "pong" + case protocol.ChatEventStep: + sawStep = frame.Step != nil && frame.Step.Status == protocol.ChatStepEnded + case protocol.ChatEventTool: + sawTool = true + } + } + if !sawText || !sawStep { + t.Fatalf("missing text or step frame: %+v", framesIn) + } + if sawTool { + t.Fatalf("a trivial no-tool prompt produced a tool frame: %+v", framesIn) + } +} + +// TestChatPromptRejectsEmptyText pins the whitespace guard: an empty or +// whitespace-only prompt is rejected before any OpenCode call. +func TestChatPromptRejectsEmptyText(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + for _, text := range []string{"", " ", "\t\n"} { + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "empty", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: text}}); 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 || response.ID != "empty" { + t.Fatalf("empty text %q not rejected: %+v", text, response) + } + } + if prompts, _, _ := broker.counts(); prompts != 0 { + t.Fatalf("empty prompts reached OpenCode: %d", prompts) + } +} + +// TestChatSessionsAndCreateAnswerExplicitly pins the declared-but-unbrokered +// boundary: chat_sessions and chat_create are answered with a protocol error +// rather than falling through to the generic Pong. +func TestChatSessionsAndCreateAnswerExplicitly(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + for _, typ := range []string{protocol.ChatSessions, protocol.ChatCreate} { + if err := wsjson.Write(ctx, conn, protocol.Message{Type: typ, ID: "c1"}); 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 || response.ID != "c1" { + t.Fatalf("%s did not answer with an explicit error: %+v", typ, response) + } + } +} diff --git a/internal/server/websocket.go b/internal/server/websocket.go index 7d071be3..e237010a 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "strings" + "sync" "time" "github.com/coder/websocket" @@ -21,6 +22,10 @@ type Options struct { // ArtifactsRoot overrides where stage artifacts live. Empty uses the store's // directory (the database's parent), matching the data-directory default. ArtifactsRoot string + // Chat brokers OpenCode chat traffic over /connect. Nil answers every + // chat_* request with a protocol error; a configured server passes an + // *opencode.Chat bound to the owned endpoint. + Chat chatBroker } // Handler serves the persistent connection with default options. Canceling ctx @@ -34,6 +39,7 @@ func Handler(ctx context.Context, stores ...*pipeline.Store) http.Handler { func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipeline.Store) http.Handler { build := buildinfo.Current() started := time.Now() + hub := newChatHub() mux := http.NewServeMux() var runs *runner var runErr error @@ -70,16 +76,27 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin } defer conn.CloseNow() conn.SetReadLimit(2 << 20) + // connCtx is canceled when this connection's read loop returns, so a + // chat subscription started for the client stops on disconnect. A single + // write mutex serializes the read loop's responses and the chat stream + // goroutine, which must never interleave partial websocket writes. + connCtx, connCancel := context.WithCancel(ctx) + defer connCancel() + var writeMu sync.Mutex + write := func(message protocol.Message) error { + writeMu.Lock() + defer writeMu.Unlock() + writeCtx, cancel := context.WithTimeout(connCtx, 5*time.Second) + defer cancel() + return wsjson.Write(writeCtx, conn, message) + } if len(stores) > 0 { items, err := stores[0].List(ctx) if err != nil { log.Printf("list pipelines: %v", err) return } - writeCtx, cancel := context.WithTimeout(ctx, 5*time.Second) - err = wsjson.Write(writeCtx, conn, protocol.Message{Type: protocol.PipelineList, Pipelines: items, NotificationEndpoints: notificationEndpoints()}) - cancel() - if err != nil { + if err := write(protocol.Message{Type: protocol.PipelineList, Pipelines: items, NotificationEndpoints: notificationEndpoints()}); err != nil { return } } @@ -89,6 +106,9 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin return } log.Printf("command received: client=%s type=%q id=%q pipeline=%d version=%d", r.RemoteAddr, request.Type, request.ID, request.PipelineID, request.Version) + if handleChat(connCtx, request, options.Chat, hub, write) { + continue + } response := protocol.Message{Type: protocol.Pong, ID: request.ID} if request.Type == protocol.List || request.Type == protocol.Submit { response.NotificationEndpoints = notificationEndpoints() @@ -196,10 +216,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin response.Error = "execution unavailable: " + runErr.Error() } } - writeCtx, cancel := context.WithTimeout(ctx, 5*time.Second) - err := wsjson.Write(writeCtx, conn, response) - cancel() - if err != nil { + if err := write(response); err != nil { log.Printf("write response: %v", err) return } From 915016df0b3113f1f79d458280a55d2b993db4f8 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 16 Oct 2026 05:24:00 +0100 Subject: [PATCH 3/7] Expose OpenCode conversation history in chat_messages decodeChatMessages only understood the native-serve {info,parts} envelope, so records from the V2 HTTP API (GET /api/session/{id}/message) decoded with an empty role and were silently dropped, leaving chat_messages with empty history. Teach nativeChatMessage to read the V2 top-level shape (id/type/agent/model/time, parts under "content", user prompt as a bare string) while keeping the native {info,parts} path via role/id/model/agent/ created accessors and a contentParts fallback. Add seeded-session evidence: a decoder table test covering both envelopes (user + assistant with text, reasoning and a completed tool part carrying input, content and status) plus handler tests that drive chat_messages through the broker fallthrough and through the real adapter. --- internal/opencode/chat.go | 111 +++++++++++++++++++---- internal/opencode/chat_test.go | 131 +++++++++++++++++++++++++-- internal/server/chat_test.go | 157 ++++++++++++++++++++++++++++++++- 3 files changed, 374 insertions(+), 25 deletions(-) diff --git a/internal/opencode/chat.go b/internal/opencode/chat.go index dd0cc386..75a89f2c 100644 --- a/internal/opencode/chat.go +++ b/internal/opencode/chat.go @@ -290,26 +290,98 @@ func stepIndex(data chatEvent) int { return data.Index } -// decodeChatMessages maps the native /message response onto the protocol chat -// history shape. Assistant content parts are text, reasoning and tool; a user -// turn is the prompt text. It never re-derives provider shapes. +// nativeChatMessage is one stored turn. The two surfaces return different +// envelopes for the same session and both decode into this one struct, so the +// mapping below reads whichever fields a record populated: +// +// - the V2 HTTP API (GET /api/session/{id}/message) puts id/type/agent/model, +// the timing and the parts array at the message top level, with the parts +// under "content" and a user turn's text as a bare string; +// - the native serve readback nests the same fields under "info" (role as +// "role", model as "modelID", timing as "created") with the parts under +// "parts". type nativeChatMessage struct { - Info struct { + ID string `json:"id"` + Type string `json:"type"` + Agent string `json:"agent"` + Model nativeChatModel `json:"model"` + Time *protocol.ChatTime `json:"time"` + Text string `json:"text"` + Content []nativeChatPart `json:"content"` + Info struct { ID string `json:"id"` Role string `json:"role"` ModelID string `json:"modelID"` Agent string `json:"agent"` CreatedAt int64 `json:"created"` } `json:"info"` - Parts []struct { - Type string `json:"type"` - ID string `json:"id"` - Text string `json:"text"` - Name string `json:"name"` - Executed bool `json:"executed"` - State *protocol.ChatToolState `json:"state"` - Time *protocol.ChatTime `json:"time"` - } `json:"parts"` + Parts []nativeChatPart `json:"parts"` +} + +// nativeChatModel is the nested Model.Ref a V2 turn carries ({id, providerID, +// variant}); only the model id is surfaced in the protocol message. +type nativeChatModel struct { + ID string `json:"id"` +} + +// nativeChatPart is one content element shared by both message envelopes. +type nativeChatPart struct { + Type string `json:"type"` + ID string `json:"id"` + Text string `json:"text"` + Name string `json:"name"` + Executed bool `json:"executed"` + State *protocol.ChatToolState `json:"state"` + Time *protocol.ChatTime `json:"time"` +} + +// role, parts and the accessor methods below read the V2 top-level fields first +// and fall back to the native serve "info"/"parts" fields, so one record maps +// regardless of which surface produced it. + +func (m nativeChatMessage) role() string { + if m.Info.Role != "" { + return m.Info.Role + } + return m.Type +} + +func (m nativeChatMessage) messageID() string { + if m.Info.ID != "" { + return m.Info.ID + } + return m.ID +} + +func (m nativeChatMessage) modelID() string { + if m.Info.ModelID != "" { + return m.Info.ModelID + } + return m.Model.ID +} + +func (m nativeChatMessage) agent() string { + if m.Info.Agent != "" { + return m.Info.Agent + } + return m.Agent +} + +func (m nativeChatMessage) createdAt() int64 { + if m.Info.CreatedAt != 0 { + return m.Info.CreatedAt + } + if m.Time != nil { + return m.Time.Created + } + return 0 +} + +func (m nativeChatMessage) contentParts() []nativeChatPart { + if m.Parts != nil { + return m.Parts + } + return m.Content } // chatMessageRecords accepts both message envelopes the two surfaces return: @@ -335,13 +407,13 @@ func decodeChatMessages(sessionID string, data []byte) ([]protocol.ChatMessage, return nil, err } messages := make([]protocol.ChatMessage, 0, len(records)) - for _, message := range records { - role := message.Info.Role + for _, record := range records { + role := record.role() if role != protocol.ChatRoleUser && role != protocol.ChatRoleAssistant { continue } - out := protocol.ChatMessage{ID: message.Info.ID, SessionID: sessionID, Role: role, Model: message.Info.ModelID, Agent: message.Info.Agent, CreatedAt: message.Info.CreatedAt} - for _, part := range message.Parts { + out := protocol.ChatMessage{ID: record.messageID(), SessionID: sessionID, Role: role, Model: record.modelID(), Agent: record.agent(), CreatedAt: record.createdAt()} + for _, part := range record.contentParts() { switch part.Type { case protocol.ChatPartText, protocol.ChatPartReasoning: out.Parts = append(out.Parts, protocol.ChatPart{Type: part.Type, ID: part.ID, Text: part.Text}) @@ -349,6 +421,11 @@ func decodeChatMessages(sessionID string, data []byte) ([]protocol.ChatMessage, out.Parts = append(out.Parts, protocol.ChatPart{Type: protocol.ChatPartTool, ID: part.ID, Name: part.Name, Executed: part.Executed, State: part.State, Time: part.Time}) } } + // A V2 user turn carries its prompt text as a message-level string rather + // than a content array, so surface it as the turn's single text part. + if len(out.Parts) == 0 && role == protocol.ChatRoleUser && record.Text != "" { + out.Parts = append(out.Parts, protocol.ChatPart{Type: protocol.ChatPartText, Text: record.Text}) + } messages = append(messages, out) } return messages, nil diff --git a/internal/opencode/chat_test.go b/internal/opencode/chat_test.go index ac78a477..fca96c4d 100644 --- a/internal/opencode/chat_test.go +++ b/internal/opencode/chat_test.go @@ -165,18 +165,135 @@ func TestChatRejectsInvalidSessionWithoutCalling(t *testing.T) { } } +// TestDecodeChatMessages loads a seeded user + assistant session in both +// response envelopes the two surfaces return and asserts the turns come back +// with role, id, part order, part ids and the completed tool's input, content +// and status intact. The V2 HTTP API (GET /api/session/{id}/message) wraps the +// messages in {"data":[...]} with the fields at the message top level and the +// parts under "content"; the native serve readback returns a bare array with the +// fields under "info" and the parts under "parts". func TestDecodeChatMessages(t *testing.T) { - raw := []byte(`{"data":[{"info":{"id":"msg_1","role":"assistant","modelID":"m","agent":"build","created":10},"parts":[{"type":"text","id":"p1","text":"hi"},{"type":"reasoning","id":"p2","text":"hmm"},{"type":"tool","id":"call_1","name":"shell","executed":true,"state":{"status":"completed","input":{"command":"ls"},"content":[{"type":"text","text":"ok"}]},"time":{"created":1,"ran":2,"completed":3}}]}]}`) - messages, err := decodeChatMessages("ses_1", raw) + cases := []struct { + name string + raw string + wantUserPartID string + wantUserPartTxt string + wantTextPartID string + wantReasonPartID string + }{ + { + name: "v2 data envelope", + raw: `{"data":[ + {"id":"msg_user","time":{"created":10},"type":"user","text":"list the files"}, + {"id":"msg_assistant","time":{"created":20,"streamed":25,"completed":30},"type":"assistant","agent":"build","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go"},"content":[ + {"type":"text","text":"here you go"}, + {"type":"reasoning","text":"thinking"}, + {"type":"tool","id":"call_1","name":"shell","executed":true,"state":{"status":"completed","input":{"command":"ls"},"content":[{"type":"text","text":"ok"}],"metadata":{"exit":0}},"time":{"created":1,"ran":2,"completed":3}} + ]} + ],"cursor":{"next":null}}`, + wantUserPartID: "", + wantUserPartTxt: "list the files", + wantTextPartID: "", + wantReasonPartID: "", + }, + { + name: "v1 bare array", + raw: `[ + {"info":{"id":"msg_user","role":"user","created":10},"parts":[{"type":"text","id":"p0","text":"list the files"}]}, + {"info":{"id":"msg_assistant","role":"assistant","modelID":"deepseek-v4.1-flash","agent":"build","created":20},"parts":[ + {"type":"text","id":"p1","text":"here you go"}, + {"type":"reasoning","id":"p2","text":"thinking"}, + {"type":"tool","id":"call_1","name":"shell","executed":true,"state":{"status":"completed","input":{"command":"ls"},"content":[{"type":"text","text":"ok"}],"metadata":{"exit":0}},"time":{"created":1,"ran":2,"completed":3}} + ]} + ]`, + wantUserPartID: "p0", + wantUserPartTxt: "list the files", + wantTextPartID: "p1", + wantReasonPartID: "p2", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + messages, err := decodeChatMessages("ses_1", []byte(tc.raw)) + if err != nil { + t.Fatalf("decode: %v", err) + } + if len(messages) != 2 { + t.Fatalf("got %d messages, want user+assistant: %+v", len(messages), messages) + } + user, assistant := messages[0], messages[1] + if user.ID != "msg_user" || user.Role != protocol.ChatRoleUser || user.SessionID != "ses_1" { + t.Fatalf("user turn = %+v", user) + } + if len(user.Parts) != 1 || user.Parts[0].Type != protocol.ChatPartText || user.Parts[0].ID != tc.wantUserPartID || user.Parts[0].Text != tc.wantUserPartTxt { + t.Fatalf("user parts = %+v", user.Parts) + } + if assistant.ID != "msg_assistant" || assistant.Role != protocol.ChatRoleAssistant || assistant.SessionID != "ses_1" { + t.Fatalf("assistant turn = %+v", assistant) + } + if assistant.Model != "deepseek-v4.1-flash" || assistant.Agent != "build" { + t.Fatalf("assistant model/agent = %q/%q", assistant.Model, assistant.Agent) + } + if len(assistant.Parts) != 3 { + t.Fatalf("got %d assistant parts, want text+reasoning+tool: %+v", len(assistant.Parts), assistant.Parts) + } + if p := assistant.Parts[0]; p.Type != protocol.ChatPartText || p.ID != tc.wantTextPartID || p.Text != "here you go" { + t.Fatalf("text part = %+v", p) + } + if p := assistant.Parts[1]; p.Type != protocol.ChatPartReasoning || p.ID != tc.wantReasonPartID || p.Text != "thinking" { + t.Fatalf("reasoning part = %+v", p) + } + tool := assistant.Parts[2] + if tool.Type != protocol.ChatPartTool || tool.ID != "call_1" || tool.Name != "shell" || !tool.Executed { + t.Fatalf("tool part = %+v", tool) + } + if tool.State == nil || tool.State.Status != protocol.ChatToolCompleted { + t.Fatalf("tool state = %+v", tool.State) + } + var input struct { + Command string `json:"command"` + } + if err := json.Unmarshal(tool.State.Input, &input); err != nil || input.Command != "ls" { + t.Fatalf("tool input = %s (err %v)", tool.State.Input, err) + } + if len(tool.State.Content) != 1 || tool.State.Content[0].Type != "text" || tool.State.Content[0].Text != "ok" { + t.Fatalf("tool content = %+v", tool.State.Content) + } + if tool.Time == nil || tool.Time.Completed != 3 { + t.Fatalf("tool time = %+v", tool.Time) + } + }) + } +} + +// TestDecodeChatMessagesSkipsUnknownPartsAndRoles pins that a non user/assistant +// turn and an unrecognised part type are skipped instead of failing the whole +// history, and that an unparseable body is an error rather than empty history. +func TestDecodeChatMessagesSkipsUnknownPartsAndRoles(t *testing.T) { + raw := `{"data":[ + {"id":"msg_system","type":"system","content":[{"type":"text","text":"ignore me"}]}, + {"id":"msg_user","time":{"created":1},"type":"user","text":"hi"}, + {"id":"msg_assistant","time":{"created":2},"type":"assistant","content":[ + {"type":"text","text":"ok"}, + {"type":"file","text":"ignored"}, + {"type":"step","text":"ignored"} + ]} + ]}` + messages, err := decodeChatMessages("ses_1", []byte(raw)) if err != nil { t.Fatal(err) } - if len(messages) != 1 || messages[0].Role != protocol.ChatRoleAssistant || len(messages[0].Parts) != 3 { - t.Fatalf("messages = %+v", messages) + if len(messages) != 2 { + t.Fatalf("got %d messages, want the system turn skipped: %+v", len(messages), messages) } - tool := messages[0].Parts[2] - if tool.Type != protocol.ChatPartTool || tool.Name != "shell" || tool.State == nil || tool.State.Status != protocol.ChatToolCompleted || tool.Time == nil { - t.Fatalf("tool part = %+v", tool) + if messages[0].Role != protocol.ChatRoleUser || len(messages[0].Parts) != 1 || messages[0].Parts[0].Text != "hi" { + t.Fatalf("user turn = %+v", messages[0]) + } + if messages[1].Role != protocol.ChatRoleAssistant || len(messages[1].Parts) != 1 || messages[1].Parts[0].Text != "ok" { + t.Fatalf("assistant turn = %+v", messages[1]) + } + if _, err := decodeChatMessages("ses_1", []byte("not json")); err == nil { + t.Fatal("an unparseable body must be an error, not empty history") } } diff --git a/internal/server/chat_test.go b/internal/server/chat_test.go index f300575b..38778702 100644 --- a/internal/server/chat_test.go +++ b/internal/server/chat_test.go @@ -26,6 +26,9 @@ type fakeChat struct { promptCalls int interrupts int replies int + messageCalls int + messageSessionID string + messageLimit int messages []protocol.ChatMessage script []protocol.ChatEventFrame started chan struct{} @@ -81,10 +84,23 @@ func (f *fakeChat) Interrupt(context.Context, string) error { return nil } -func (f *fakeChat) Messages(context.Context, string, int) ([]protocol.ChatMessage, error) { +func (f *fakeChat) Messages(_ context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.messageCalls++ + f.messageSessionID = sessionID + f.messageLimit = limit return f.messages, nil } +// messageQuery reports the recorded chat_messages call count, session id and +// limit so a test can assert the handler forwarded the query. +func (f *fakeChat) messageQuery() (int, string, int) { + f.mu.Lock() + defer f.mu.Unlock() + return f.messageCalls, f.messageSessionID, f.messageLimit +} + func (f *fakeChat) Reply(context.Context, protocol.ChatReplyRequest) error { f.mu.Lock() defer f.mu.Unlock() @@ -271,6 +287,145 @@ func TestChatMessagesResumesRetainedState(t *testing.T) { } } +// seededChatMessages is the stored user + assistant + tool session the +// chat_messages evidence tests assert on, mirroring the OpenCode V2 shape of an +// assistant turn's completed tool part (input object, text content, status). +func seededChatMessages() []protocol.ChatMessage { + return []protocol.ChatMessage{ + {ID: "msg_user", SessionID: "ses_1", Role: protocol.ChatRoleUser, + Parts: []protocol.ChatPart{{Type: protocol.ChatPartText, Text: "list the files"}}}, + {ID: "msg_assistant", SessionID: "ses_1", Role: protocol.ChatRoleAssistant, Model: "deepseek-v4.1-flash", Agent: "build", + Parts: []protocol.ChatPart{ + {Type: protocol.ChatPartText, ID: "p1", Text: "here you go"}, + {Type: protocol.ChatPartReasoning, ID: "p2", Text: "thinking"}, + {Type: protocol.ChatPartTool, ID: "call_1", Name: "shell", Executed: true, + State: &protocol.ChatToolState{Status: protocol.ChatToolCompleted, Input: json.RawMessage(`{"command":"ls"}`), Content: []protocol.ChatToolContent{{Type: "text", Text: "ok"}}}, + Time: &protocol.ChatTime{Created: 1, Ran: 2, Completed: 3}}, + }}, + } +} + +// assertSeededChatMessages fails unless the user + assistant + tool session came +// back with both roles and the completed tool part's input, content and status. +func assertSeededChatMessages(t *testing.T, messages []protocol.ChatMessage) { + t.Helper() + if len(messages) != 2 { + t.Fatalf("got %d messages, want user+assistant: %+v", len(messages), messages) + } + user, assistant := messages[0], messages[1] + if user.ID != "msg_user" || user.Role != protocol.ChatRoleUser || len(user.Parts) != 1 || user.Parts[0].Type != protocol.ChatPartText || user.Parts[0].Text != "list the files" { + t.Fatalf("user turn = %+v", user) + } + if assistant.ID != "msg_assistant" || assistant.Role != protocol.ChatRoleAssistant || len(assistant.Parts) != 3 { + t.Fatalf("assistant turn = %+v", assistant) + } + if p := assistant.Parts[0]; p.Type != protocol.ChatPartText || p.Text != "here you go" { + t.Fatalf("assistant text part = %+v", p) + } + tool := assistant.Parts[2] + if tool.Type != protocol.ChatPartTool || tool.ID != "call_1" || tool.Name != "shell" || tool.State == nil || tool.State.Status != protocol.ChatToolCompleted { + t.Fatalf("assistant tool part = %+v", tool) + } + var input struct { + Command string `json:"command"` + } + if err := json.Unmarshal(tool.State.Input, &input); err != nil || input.Command != "ls" { + t.Fatalf("tool input = %s (err %v)", tool.State.Input, err) + } + if len(tool.State.Content) != 1 || tool.State.Content[0].Text != "ok" { + t.Fatalf("tool content = %+v", tool.State.Content) + } +} + +// TestChatMessagesReturnsBrokerHistory drives chat_messages through the handler +// with an empty hub, so it must fall through to broker.Messages and return the +// seeded user + assistant + tool session, forwarding the query limit unchanged. +func TestChatMessagesReturnsBrokerHistory(t *testing.T) { + broker := scriptedChat() + broker.messages = seededChatMessages() + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "r2", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1", Limit: 7}}); 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.ChatMessages || response.ID != "r2" { + t.Fatalf("response = %+v", response) + } + assertSeededChatMessages(t, response.ChatMessages) + if calls, sessionID, limit := broker.messageQuery(); calls != 1 || sessionID != "ses_1" || limit != 7 { + t.Fatalf("broker query = %d/%q/%d, want 1/ses_1/7", calls, sessionID, limit) + } +} + +// TestChatMessagesThroughRealAdapter loads a seeded session through the real +// opencode.Chat broker: the chat_messages handler reads the V2 +// GET /api/session/{id}/message fixture once and returns the user + assistant + +// tool turns decoded from it, exercising the full handler -> adapter -> decode +// path with no scripted broker. +func TestChatMessagesThroughRealAdapter(t *testing.T) { + var messageReads atomic.Int32 + fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet || r.URL.Path != "/api/session/ses_1/message" { + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + return + } + messageReads.Add(1) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"data":[ + {"id":"msg_user","time":{"created":10},"type":"user","text":"list the files"}, + {"id":"msg_assistant","time":{"created":20,"completed":30},"type":"assistant","agent":"build","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go"},"content":[ + {"type":"text","text":"here you go"}, + {"type":"reasoning","text":"thinking"}, + {"type":"tool","id":"call_1","name":"shell","executed":true,"state":{"status":"completed","input":{"command":"ls"},"content":[{"type":"text","text":"ok"}]},"time":{"created":1,"ran":2,"completed":3}} + ]} + ],"cursor":{"next":null}}`)) + })) + defer fixture.Close() + + broker := opencode.NewChat(&opencode.Connection{Backend: "v2", URL: fixture.URL}) + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "r2", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); 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.ChatMessages || response.ID != "r2" { + t.Fatalf("response = %+v", response) + } + assertSeededChatMessages(t, response.ChatMessages) + if messageReads.Load() != 1 { + t.Fatalf("message endpoint read %d times, want 1", messageReads.Load()) + } +} + +// TestChatMessagesRejectsInvalidSession pins that a missing or malformed session +// id is rejected before any broker call. +func TestChatMessagesRejectsInvalidSession(t *testing.T) { + broker := scriptedChat() + broker.messages = seededChatMessages() + ctx, _, conn := dialChat(t, broker) + for _, query := range []*protocol.ChatQuery{nil, {SessionID: ""}, {SessionID: "not-a-session"}} { + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "bad", ChatQuery: query}); 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 || response.ID != "bad" { + t.Fatalf("query %+v not rejected: %+v", query, response) + } + } + if calls, _, _ := broker.messageQuery(); calls != 0 { + t.Fatalf("invalid chat_messages reached the broker: %d", calls) + } +} + func TestChatDisconnectCancelsStream(t *testing.T) { broker := scriptedChat() broker.block = true From b28f88be6624e86ccd1592f23a91fc0749ce7624 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 16 Oct 2026 06:43:02 +0100 Subject: [PATCH 4/7] Add tests proving chat_interrupt stops a running chat turn The chat_interrupt path was already wired end to end (protocol type, server dispatch through handleChatSimple to broker.Interrupt, and the adapter POST /api/session/{id}/interrupt), but nothing exercised it against a live stream. Add the missing coverage: - internal/opencode/chat_test.go: pin the adapter's outbound call exactly (POST /api/session/ses_1/interrupt, empty JSON body, one request, nil on 200) plus a non-nil error on HTTP 500. - internal/server/chat_test.go: drive the real opencode.Chat adapter over an httptest SSE fixture that holds the turn open until the interrupt arrives, then assert the run ends, the interrupt is acknowledged, no error frame is sent, and the prompt/interrupt posts and interrupt path match. Also cover invalid-session rejection, the unconfigured-broker error, and broker-failure reporting, adding a spare interruptErr hook to the scripted fakeChat. Production code is unchanged: verification found no gap in the existing interrupt transport. Tests use httptest loopback fixtures only and do not touch the live OpenCode service. --- internal/opencode/chat_test.go | 54 ++++++++++ internal/server/chat_test.go | 180 ++++++++++++++++++++++++++++++++- 2 files changed, 233 insertions(+), 1 deletion(-) diff --git a/internal/opencode/chat_test.go b/internal/opencode/chat_test.go index fca96c4d..63edb2b4 100644 --- a/internal/opencode/chat_test.go +++ b/internal/opencode/chat_test.go @@ -7,6 +7,7 @@ import ( "net/http" "net/http/httptest" "strings" + "sync" "sync/atomic" "testing" "time" @@ -165,6 +166,59 @@ func TestChatRejectsInvalidSessionWithoutCalling(t *testing.T) { } } +// TestChatInterruptPostsInterruptEndpoint pins the outbound interrupt call: the +// adapter must POST an empty JSON body to /api/session/{id}/interrupt exactly +// once and return nil, and it must surface a non-nil error when the endpoint +// fails. One interrupter request, no probing beyond what the call needs. +func TestChatInterruptPostsInterruptEndpoint(t *testing.T) { + t.Run("success", func(t *testing.T) { + type request struct{ method, path, body string } + var mu sync.Mutex + var requests []request + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + data, _ := io.ReadAll(r.Body) + mu.Lock() + requests = append(requests, request{method: r.Method, path: r.URL.Path, body: string(data)}) + mu.Unlock() + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{}`)) + })) + defer srv.Close() + + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := chat.Interrupt(ctx, "ses_1"); err != nil { + t.Fatalf("interrupt: %v", err) + } + mu.Lock() + got := append([]request(nil), requests...) + mu.Unlock() + if len(got) != 1 { + t.Fatalf("interrupt requests = %d, want 1: %+v", len(got), got) + } + if got[0].method != http.MethodPost || got[0].path != "/api/session/ses_1/interrupt" { + t.Fatalf("interrupt request = %+v", got[0]) + } + if got[0].body != "{}" { + t.Fatalf("interrupt body = %q, want {}", got[0].body) + } + }) + + t.Run("failure", func(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + })) + defer srv.Close() + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := chat.Interrupt(ctx, "ses_1"); err == nil { + t.Fatal("a failing interrupt endpoint must return an error") + } + }) +} + // TestDecodeChatMessages loads a seeded user + assistant session in both // response envelopes the two surfaces return and asserts the turns come back // with role, id, part order, part ids and the completed tool's input, content diff --git a/internal/server/chat_test.go b/internal/server/chat_test.go index 38778702..7516faa1 100644 --- a/internal/server/chat_test.go +++ b/internal/server/chat_test.go @@ -37,6 +37,7 @@ type fakeChat struct { block bool subscribed bool promptBeforeSubscribe bool + interruptErr error } func (f *fakeChat) Prompt(context.Context, string, string) error { @@ -81,7 +82,7 @@ func (f *fakeChat) Interrupt(context.Context, string) error { f.mu.Lock() defer f.mu.Unlock() f.interrupts++ - return nil + return f.interruptErr } func (f *fakeChat) Messages(_ context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error) { @@ -484,6 +485,183 @@ func TestChatInterruptAndReply(t *testing.T) { } } +// TestChatInterruptEndsRunningPrompt is the task evidence: it starts a +// long-running turn through the real opencode.Chat adapter over an httptest SSE +// fixture, sends chat_interrupt, and asserts the run ends. The fixture records +// the actual POST /api/session/ses_1/interrupt and only closes the SSE +// subscription once that request arrives, so the run genuinely terminates rather +// than completing on its own. +func TestChatInterruptEndsRunningPrompt(t *testing.T) { + var ( + mu sync.Mutex + prompts int + interrupts int + interruptPath string + ) + interrupted := make(chan struct{}) + var interruptOnce sync.Once + fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch { + case r.Method == http.MethodPost && r.URL.Path == "/api/session/ses_1/prompt": + mu.Lock() + prompts++ + mu.Unlock() + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`)) + case r.Method == http.MethodGet && r.URL.Path == "/api/event": + flusher, ok := w.(http.Flusher) + if !ok { + t.Errorf("event stream response is not flushable") + return + } + w.Header().Set("Content-Type", "text/event-stream") + _, _ = w.Write([]byte("data: " + `{"id":"evt_1","type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"working"}}` + "\n\n")) + flusher.Flush() + // Hold the turn open until the interrupt arrives so the test exercises + // a running run, not a stream that already completed. + select { + case <-interrupted: + case <-r.Context().Done(): + return + } + _, _ = w.Write([]byte("data: " + `{"id":"evt_2","type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","step":1}}` + "\n\n")) + flusher.Flush() + case r.Method == http.MethodPost && r.URL.Path == "/api/session/ses_1/interrupt": + mu.Lock() + interrupts++ + interruptPath = r.URL.Path + mu.Unlock() + interruptOnce.Do(func() { close(interrupted) }) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{}`)) + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + } + })) + defer fixture.Close() + + broker := opencode.NewChat(&opencode.Connection{Backend: "v2", URL: fixture.URL}) + ctx, _, conn := dialChat(t, broker) + + // Start a long-running turn. + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "start a long-running task"}}); 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 != "r1" { + t.Fatalf("prompt ack = %+v", ack) + } + // The first text delta proves the turn is live and the subscription attached. + var first protocol.Message + if err := wsjson.Read(ctx, conn, &first); err != nil { + t.Fatal(err) + } + if first.Type != protocol.ChatEvent || first.ChatEvent == nil || first.ChatEvent.Kind != protocol.ChatEventText || first.ChatEvent.Delta != "working" { + t.Fatalf("expected a live text delta, got %+v", first) + } + + // Stop the running turn. + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatInterrupt, ID: "i1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil { + t.Fatal(err) + } + + // Read until both the interrupt ack and the step.ended frame arrive, in + // whichever order the server emits them; any error frame fails the test. + var acked, ended bool + for !acked || !ended { + var message protocol.Message + if err := wsjson.Read(ctx, conn, &message); err != nil { + t.Fatal(err) + } + if message.Type == protocol.Error { + t.Fatalf("unexpected error frame: %s", message.Error) + } + if message.Type == protocol.ChatInterrupt { + if message.ID != "i1" { + t.Fatalf("interrupt ack id = %q, want i1", message.ID) + } + acked = true + } + if event := message.ChatEvent; event != nil && event.SessionID == "ses_1" && event.Kind == protocol.ChatEventStep && event.Step != nil && event.Step.Status == protocol.ChatStepEnded { + ended = true + } + } + mu.Lock() + gotPrompts, gotInterrupts, gotPath := prompts, interrupts, interruptPath + mu.Unlock() + if gotPrompts != 1 { + t.Fatalf("prompt posts = %d, want 1", gotPrompts) + } + if gotInterrupts != 1 { + t.Fatalf("interrupt posts = %d, want 1", gotInterrupts) + } + if gotPath != "/api/session/ses_1/interrupt" { + t.Fatalf("interrupt path = %q, want /api/session/ses_1/interrupt", gotPath) + } +} + +// TestChatInterruptRejectsInvalidSession pins that a missing, empty or malformed +// session id is rejected with a protocol.Error before any broker call. +func TestChatInterruptRejectsInvalidSession(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + for _, query := range []*protocol.ChatQuery{nil, {}, {SessionID: "not-a-session"}} { + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatInterrupt, ID: "bad", ChatQuery: query}); 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 || response.ID != "bad" { + t.Fatalf("query %+v not rejected: %+v", query, response) + } + } + if _, interrupts, _ := broker.counts(); interrupts != 0 { + t.Fatalf("invalid interrupts reached the broker: %d", interrupts) + } +} + +// TestChatInterruptWithoutBroker pins that an unconfigured server answers +// chat_interrupt with the existing "not configured" error. +func TestChatInterruptWithoutBroker(t *testing.T) { + ctx, _, conn := dialChat(t, nil) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatInterrupt, ID: "i1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); 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, "not configured") { + t.Fatalf("unconfigured interrupt not rejected: %+v", response) + } +} + +// TestChatInterruptErrorReported pins that a broker failure is surfaced to the +// client as a protocol.Error frame carrying the echoed request id. +func TestChatInterruptErrorReported(t *testing.T) { + broker := scriptedChat() + broker.interruptErr = fmt.Errorf("run is already finished") + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatInterrupt, ID: "i1", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); 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 || response.ID != "i1" || !strings.Contains(response.Error, "already finished") { + t.Fatalf("interrupt failure not reported: %+v", response) + } + if _, interrupts, _ := broker.counts(); interrupts != 1 { + t.Fatalf("interrupts = %d, want 1", interrupts) + } +} + func TestChatHubAccumulatesAndEvicts(t *testing.T) { tick := 0 hub := newChatHub() From 3ff5cd8838310b76f4908defb28b0cf787bfb295 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 16 Oct 2026 08:06:39 +0100 Subject: [PATCH 5/7] Poll OpenCode permission requests and forward chat_reply --- .../author/skill/references/operations.md | 44 ++- internal/opencode/chat.go | 134 +++++++- internal/opencode/chat_test.go | 148 ++++++++ internal/opencode/connection.go | 5 + internal/opencode/native.go | 15 + internal/protocol/chat.go | 36 +- internal/protocol/chat_test.go | 41 ++- internal/protocol/message.go | 17 +- internal/server/chat.go | 165 ++++++++- internal/server/chat_test.go | 324 +++++++++++++++++- 10 files changed, 889 insertions(+), 40 deletions(-) diff --git a/internal/author/skill/references/operations.md b/internal/author/skill/references/operations.md index 2359e8b5..e1a5f039 100644 --- a/internal/author/skill/references/operations.md +++ b/internal/author/skill/references/operations.md @@ -178,17 +178,39 @@ traversal, absolute paths, symlink escapes and unknown runs/steps. ## Brokered OpenCode chat (unshipped) The protocol declares the brokered `chat_*` surface (`chat_sessions`, -`chat_create`, `chat_prompt`, `chat_messages`, `chat_interrupt`, `chat_reply`). The -server dispatches `chat_prompt`, `chat_messages`, `chat_interrupt` and `chat_reply`, -answers the declared `chat_sessions`/`chat_create` with an explicit protocol error -because they are not yet brokered, and pushes `chat_event` frames derived from -OpenCode's global `/api/event` firehose, with retained per-session/per-message state -so a reconnecting client re-syncs instead of replaying deltas. This is an internal, unshipped capability: no client consumes it, -and chat is enabled only when the OpenCode connection exposes a URL-bearing SSE -endpoint (the owned v1 serve process). On a V2 CLI (`opencode api`, no owned serve) -that endpoint is absent, so every dispatched `chat_*` request returns a protocol error -naming the missing configuration. Do not describe brokered chat as a finished or -user-facing feature. +`chat_create`, `chat_prompt`, `chat_messages`, `chat_interrupt`, `chat_permission`, +`chat_reply`). The server dispatches `chat_prompt`, `chat_messages`, `chat_interrupt` +and `chat_reply`, answers the declared `chat_sessions`/`chat_create` with an explicit +protocol error because they are not yet brokered, and pushes `chat_event` frames +derived from OpenCode's global `/api/event` firehose, with retained +per-session/per-message state so a reconnecting client re-syncs instead of replaying +deltas. + +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 +request id so a repeated poll never re-pushes the same request. The app answers with +`chat_reply`, which the server forwards as +`POST /api/session/{id}/permission/{requestID}/reply` with +`{"decision":"once"|"always"|"reject"}`. A missing session/request id or an invalid +decision is rejected before any OpenCode call, and the poller stops when the stream +ends or the client disconnects. + +The permission list and reply are implemented against the V2 HTTP API only: their +field names match the service's `Permission.Request`/`Permission.Reply` schemas +(verified against the installed service's `/openapi.json`), and the native v1 serve +translation rejects them explicitly instead of guessing an endpoint. Because this +build enables brokered chat only when the OpenCode connection exposes a URL-bearing +SSE endpoint (the owned v1 serve process), the approval flow is not reachable on the +backend that currently enables chat; on that backend the permission poller stops +quietly rather than reporting the documented limitation as a per-prompt error. This is +an internal, unshipped capability: no client consumes it, and on a V2 CLI +(`opencode api`, no owned serve) that endpoint is absent, so every dispatched `chat_*` +request returns a protocol error naming the missing configuration. The live +end-to-end approval flow has NOT been tested against a real OpenCode service; the wire +shapes and the exact reply path/body are covered by unit tests over an httptest +fixture, not against a live pending request. Do not describe brokered chat as a +finished or user-facing feature. ## Debug and retry diff --git a/internal/opencode/chat.go b/internal/opencode/chat.go index 75a89f2c..130ee793 100644 --- a/internal/opencode/chat.go +++ b/internal/opencode/chat.go @@ -5,13 +5,22 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" "strings" + "sync" "git.a-wan.net/alan/pipelines/internal/protocol" ) +// ErrPermissionUnsupported reports that the connected OpenCode backend does not +// expose the brokered approval surface. The native v1 serve translation returns +// it; callers treat it as a documented limitation rather than an operational +// failure, so a chat prompt does not emit a permission error frame on a backend +// that cannot answer one. +var ErrPermissionUnsupported = errors.New("OpenCode permission approval is not implemented for the native serve backend") + // Chat is the brokered OpenCode chat adapter (route B in the research): the // Pipelines server owns the OpenCode endpoint and credentials and turns the v2 // HTTP API into protocol.ChatEventFrame frames. It reuses the owned-endpoint @@ -19,6 +28,11 @@ import ( // no second transport or credential path is introduced. type Chat struct { conn *Connection + // mu serializes the adapter's HTTP calls. Connection records its last + // operation and status on the shared connection, so two calls must not + // overlap; the permission poller runs alongside the read loop's + // prompt/interrupt/reply calls. + mu sync.Mutex } // NewChat brokers chat for one owned OpenCode endpoint. The connection must be @@ -31,6 +45,15 @@ func NewChat(conn *Connection) *Chat { return &Chat{conn: conn} } // chat adapter and callers that must reject a request before any OpenCode call. func ValidSessionID(id string) bool { return sessionID.MatchString(id) } +// call serializes the adapter's HTTP calls and forwards them through the shared +// Connection, so no second transport is introduced and Connection's recorded +// last operation/status is never written by two goroutines at once. +func (c *Chat) call(ctx context.Context, method, path string, body any) ([]byte, error) { + c.mu.Lock() + defer c.mu.Unlock() + return c.conn.Call(ctx, method, path, body) +} + // Prompt forwards one chat prompt through the existing Call translation // (authenticated, bounded, directory-scoped). The endpoint returns the inbox // envelope immediately; the reply arrives on Stream. @@ -42,7 +65,7 @@ func (c *Chat) Prompt(ctx context.Context, id, text string) error { return fmt.Errorf("empty chat prompt") } body := map[string]any{"parts": []map[string]string{{"type": "text", "text": text}}} - if _, err := c.conn.Call(ctx, "post", "/api/session/"+id+"/prompt", body); err != nil { + if _, err := c.call(ctx, "post", "/api/session/"+id+"/prompt", body); err != nil { return err } return nil @@ -53,7 +76,7 @@ func (c *Chat) Interrupt(ctx context.Context, id string) error { if !sessionID.MatchString(id) { return fmt.Errorf("invalid OpenCode session id") } - if _, err := c.conn.Call(ctx, "post", "/api/session/"+id+"/interrupt", map[string]any{}); err != nil { + if _, err := c.call(ctx, "post", "/api/session/"+id+"/interrupt", map[string]any{}); err != nil { return err } return nil @@ -69,6 +92,7 @@ func (c *Chat) Messages(ctx context.Context, id string, limit int) ([]protocol.C } var data []byte var err error + c.mu.Lock() if c.conn.Backend == "v1" { data, err = c.conn.Messages(ctx, id) } else { @@ -78,6 +102,7 @@ func (c *Chat) Messages(ctx context.Context, id string, limit int) ([]protocol.C } data, err = c.conn.Call(ctx, "get", path, nil) } + c.mu.Unlock() if err != nil { return nil, err } @@ -101,12 +126,108 @@ func (c *Chat) Reply(ctx context.Context, request protocol.ChatReplyRequest) err body["message"] = request.Message } path := "/api/session/" + request.SessionID + "/permission/" + request.RequestID + "/reply" - if _, err := c.conn.Call(ctx, "post", path, body); err != nil { + if _, err := c.call(ctx, "post", path, body); err != nil { return err } return nil } +// Permissions lists the session's pending OpenCode approval requests, mirroring +// GET /api/permission/request. The endpoint is global, so the decoded list is +// filtered to sessionID; a malformed response is an error, never an empty +// success. It reuses Connection.Call so the request keeps the shared bounds, +// auth and translation, and reuses ValidSessionID for the same rejection the +// other chat calls apply. +func (c *Chat) Permissions(ctx context.Context, id string) ([]protocol.ChatPermissionFrame, error) { + if !sessionID.MatchString(id) { + return nil, fmt.Errorf("invalid OpenCode session id") + } + data, err := c.call(ctx, "get", "/api/permission/request", nil) + if err != nil { + return nil, err + } + return decodeChatPermissions(id, data) +} + +// nativeChatPermission is one pending approval request matching the V2 +// Permission.Request schema verified against the service's /openapi.json: id, +// sessionID, action and resources are required; save, metadata, source and +// message are optional, so the decoder tolerates any of them being absent. +type nativeChatPermission struct { + ID string `json:"id"` + SessionID string `json:"sessionID"` + Action string `json:"action"` + Resources []string `json:"resources"` + Save []string `json:"save"` + Metadata map[string]any `json:"metadata"` + Source *nativeChatPermissionSource `json:"source"` + Message string `json:"message"` +} + +// nativeChatPermissionSource is the incoming Permission.Source union; its single +// (tool) member carries type plus the message id and call id (the protocol frame +// renames messageID to message_id). +type nativeChatPermissionSource struct { + Type string `json:"type"` + MessageID string `json:"messageID"` + ID string `json:"id"` +} + +// chatEnvelope unwraps the {"data":[...]} envelope the V2 HTTP API returns and +// reports whether the body was that envelope with a non-nil data array; a bare +// array or a malformed body returns ok=false. It is the single envelope rule the +// message and permission decoders share instead of each re-deriving it. +func chatEnvelope[T any](data []byte) ([]T, bool) { + var response struct { + Data []T `json:"data"` + } + if err := json.Unmarshal(data, &response); err != nil || response.Data == nil { + return nil, false + } + return response.Data, true +} + +// chatPermissionRecords reads the {"data":[...]} envelope the V2 endpoint +// returns. An unparseable body or a missing data array is an error, never an +// empty list. +func chatPermissionRecords(data []byte) ([]nativeChatPermission, error) { + records, ok := chatEnvelope[nativeChatPermission](data) + if !ok { + return nil, fmt.Errorf("invalid OpenCode permission response") + } + return records, nil +} + +// decodeChatPermissions maps the pending-request list onto protocol frames, +// keeping only the requested session's requests (a request with no id cannot be +// answered and is dropped). +func decodeChatPermissions(sessionID string, data []byte) ([]protocol.ChatPermissionFrame, error) { + records, err := chatPermissionRecords(data) + if err != nil { + return nil, err + } + frames := make([]protocol.ChatPermissionFrame, 0, len(records)) + for _, record := range records { + if record.SessionID != sessionID || record.ID == "" { + continue + } + frame := protocol.ChatPermissionFrame{ + SessionID: sessionID, + RequestID: record.ID, + Action: record.Action, + Resources: record.Resources, + Save: record.Save, + Metadata: record.Metadata, + Message: record.Message, + } + if record.Source != nil { + frame.Source = &protocol.ChatPermissionSource{Type: record.Source.Type, MessageID: record.Source.MessageID, ID: record.Source.ID} + } + frames = append(frames, frame) + } + return frames, nil +} + // Stream subscribes to the OpenCode event firehose and invokes emit for every // frame belonging to sessionID. It reads one bounded SSE frame at a time and // never polls the per-session message log; the global firehose is the only @@ -388,11 +509,8 @@ func (m nativeChatMessage) contentParts() []nativeChatPart { // the V2 HTTP API wraps them in {"data":[...]} while the native serve readback // returns a bare array. An unrecognised body is an error, never empty history. func chatMessageRecords(data []byte) ([]nativeChatMessage, error) { - var response struct { - Data []nativeChatMessage `json:"data"` - } - if err := json.Unmarshal(data, &response); err == nil && response.Data != nil { - return response.Data, nil + if records, ok := chatEnvelope[nativeChatMessage](data); ok { + return records, nil } var records []nativeChatMessage if err := json.Unmarshal(data, &records); err != nil { diff --git a/internal/opencode/chat_test.go b/internal/opencode/chat_test.go index 63edb2b4..b3c5a579 100644 --- a/internal/opencode/chat_test.go +++ b/internal/opencode/chat_test.go @@ -3,6 +3,7 @@ package opencode import ( "context" "encoding/json" + "errors" "io" "net/http" "net/http/httptest" @@ -161,11 +162,158 @@ func TestChatRejectsInvalidSessionWithoutCalling(t *testing.T) { if err := chat.Interrupt(ctx, "bad id"); err == nil { t.Fatal("invalid interrupt session id must be rejected") } + if _, err := chat.Permissions(ctx, "not-a-session"); err == nil { + t.Fatal("invalid permission session id must be rejected") + } + if err := chat.Reply(ctx, protocol.ChatReplyRequest{SessionID: "not-a-session", RequestID: "req_1", Decision: protocol.ChatDecisionOnce}); err == nil { + t.Fatal("invalid reply session id must be rejected") + } + if err := chat.Reply(ctx, protocol.ChatReplyRequest{SessionID: "ses_1", RequestID: "", Decision: protocol.ChatDecisionOnce}); err == nil { + t.Fatal("empty reply request id must be rejected") + } + if err := chat.Reply(ctx, protocol.ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: "maybe"}); err == nil { + t.Fatal("invalid reply decision must be rejected") + } if calls.Load() != 0 { t.Fatalf("invalid requests reached the service: %d", calls.Load()) } } +// TestChatPermissionsListsAndFilters loads a pending-request fixture in the +// verified V2 envelope (GET /api/permission/request) and asserts the adapter +// returns the requested session's request with its fields intact, filters out a +// foreign session's request, and reads the endpoint once. +func TestChatPermissionsListsAndFilters(t *testing.T) { + var reads atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet || r.URL.Path != "/api/permission/request" { + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + return + } + reads.Add(1) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"location":{"directory":"/work"},"data":[ + {"id":"per_1","sessionID":"ses_1","action":"bash","resources":["rm -rf /tmp"],"save":["bash:rm -rf /tmp"],"metadata":{"command":"rm -rf /tmp"},"source":{"type":"tool","messageID":"msg_1","id":"call_1"},"message":"wants to run a command"}, + {"id":"per_2","sessionID":"ses_other","action":"edit","resources":["/etc/hosts"]} + ]}`)) + })) + defer srv.Close() + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + frames, err := chat.Permissions(ctx, "ses_1") + if err != nil { + t.Fatalf("permissions: %v", err) + } + if reads.Load() != 1 { + t.Fatalf("permission endpoint read %d times, want 1", reads.Load()) + } + if len(frames) != 1 { + t.Fatalf("frames = %+v, want the foreign request filtered", frames) + } + got := frames[0] + if got.SessionID != "ses_1" || got.RequestID != "per_1" || got.Action != "bash" { + t.Fatalf("frame = %+v", got) + } + if len(got.Resources) != 1 || got.Resources[0] != "rm -rf /tmp" || len(got.Save) != 1 || got.Message != "wants to run a command" { + t.Fatalf("frame detail = %+v", got) + } + if got.Metadata["command"] != "rm -rf /tmp" { + t.Fatalf("frame metadata = %+v", got.Metadata) + } + if got.Source == nil || got.Source.Type != "tool" || got.Source.MessageID != "msg_1" || got.Source.ID != "call_1" { + t.Fatalf("frame source = %+v", got.Source) + } +} + +// TestChatPermissionsRejectsMalformedList pins that a response without the data +// array is an error, not an empty success. +func TestChatPermissionsRejectsMalformedList(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"location":{"directory":"/work"}}`)) + })) + defer srv.Close() + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + if _, err := chat.Permissions(context.Background(), "ses_1"); err == nil { + t.Fatal("a malformed permission response must be an error, not empty success") + } +} + +// TestChatPermissionReplyPostsReplyEndpoint pins the outbound approval reply: the +// adapter must POST exactly once to +// /api/session/{id}/permission/{requestID}/reply with the exact decision body, +// accept the documented 204 No Content success, and surface a non-2xx failure. +func TestChatPermissionReplyPostsReplyEndpoint(t *testing.T) { + for _, decision := range []string{protocol.ChatDecisionOnce, protocol.ChatDecisionAlways, protocol.ChatDecisionReject} { + t.Run(decision, func(t *testing.T) { + type request struct{ method, path, body string } + var mu sync.Mutex + var requests []request + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + data, _ := io.ReadAll(r.Body) + mu.Lock() + requests = append(requests, request{method: r.Method, path: r.URL.Path, body: string(data)}) + mu.Unlock() + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if err := chat.Reply(ctx, protocol.ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: decision}); err != nil { + t.Fatalf("reply: %v", err) + } + mu.Lock() + got := append([]request(nil), requests...) + mu.Unlock() + if len(got) != 1 { + t.Fatalf("reply requests = %d, want 1: %+v", len(got), got) + } + if got[0].method != http.MethodPost || got[0].path != "/api/session/ses_1/permission/req_1/reply" { + t.Fatalf("reply request = %+v", got[0]) + } + if got[0].body != `{"decision":"`+decision+`"}` { + t.Fatalf("reply body = %q, want decision %q", got[0].body, decision) + } + }) + } + + t.Run("failure", func(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.WriteHeader(http.StatusInternalServerError) + })) + defer srv.Close() + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + if err := chat.Reply(context.Background(), protocol.ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: protocol.ChatDecisionOnce}); err == nil { + t.Fatal("a failing reply endpoint must return an error") + } + }) +} + +// TestChatPermissionV1TranslationGated pins the declared backend limitation: the +// permission list and reply are implemented against the V2 HTTP API, and the +// native serve translation rejects them explicitly (an actionable error, no +// guessed endpoint, no HTTP call) so the caller can see the limitation. +func TestChatPermissionV1TranslationGated(t *testing.T) { + var calls atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + calls.Add(1) + })) + defer srv.Close() + chat := NewChat(&Connection{Backend: "v1", URL: srv.URL, Directory: "/repo", models: map[string]map[string]string{}, responses: map[string][]byte{}}) + ctx := context.Background() + if _, err := chat.Permissions(ctx, "ses_1"); !errors.Is(err, ErrPermissionUnsupported) { + t.Fatalf("v1 permission list must return the gate sentinel, got %v", err) + } + if err := chat.Reply(ctx, protocol.ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: protocol.ChatDecisionOnce}); !errors.Is(err, ErrPermissionUnsupported) { + t.Fatalf("v1 permission reply must return the gate sentinel, got %v", err) + } + if calls.Load() != 0 { + t.Fatalf("gated permission requests reached the service: %d", calls.Load()) + } +} + // TestChatInterruptPostsInterruptEndpoint pins the outbound interrupt call: the // adapter must POST an empty JSON body to /api/session/{id}/interrupt exactly // once and return nil, and it must surface a non-nil error when the endpoint diff --git a/internal/opencode/connection.go b/internal/opencode/connection.go index fca80cb3..c5cd7074 100644 --- a/internal/opencode/connection.go +++ b/internal/opencode/connection.go @@ -262,6 +262,11 @@ func (c *Connection) Native(ctx context.Context, method, path string, body any) evidence, _ := io.ReadAll(io.LimitReader(response.Body, 16<<10)) return evidence, fmt.Errorf("OpenCode %s %s failed (HTTP %d)", method, path, response.StatusCode) } + // A successful no-content operation (the permission-reply endpoint returns + // 204) carries no JSON body to validate. + if response.StatusCode == http.StatusNoContent { + return nil, nil + } if !strings.HasPrefix(response.Header.Get("Content-Type"), "application/json") { return nil, fmt.Errorf("OpenCode %s %s returned a non-JSON API response", method, path) } diff --git a/internal/opencode/native.go b/internal/opencode/native.go index 5a4ed4fe..e613a381 100644 --- a/internal/opencode/native.go +++ b/internal/opencode/native.go @@ -116,6 +116,16 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([ return json.Marshal(map[string]any{"data": map[string]string{"id": session.ID}}) } clean := strings.SplitN(path, "?", 2)[0] + // The brokered approval surface is implemented against the V2 HTTP API: the + // documented endpoints are GET /api/permission/request and POST + // /api/session/{id}/permission/{requestID}/reply. This native serve + // translation does not implement them, so answer with an actionable gate + // error instead of the generic unsupported-operation error; the permission + // poller surfaces that limitation rather than silently reporting no pending + // requests. + if clean == "/api/permission/request" { + return nil, fmt.Errorf("permission listing: %w", ErrPermissionUnsupported) + } parts := strings.Split(strings.TrimPrefix(clean, "/api/session/"), "/") if strings.HasPrefix(clean, "/api/experimental/session/") { parts = strings.Split(strings.TrimPrefix(clean, "/api/experimental/session/"), "/") @@ -124,6 +134,11 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([ return nil, fmt.Errorf("unsupported native OpenCode operation") } id := parts[0] + // The approval reply is V2-only in this translation (see the permission-list + // gate above); the V2 path has four trailing segments. + if len(parts) == 4 && parts[1] == "permission" && parts[3] == "reply" { + return nil, fmt.Errorf("permission reply: %w", ErrPermissionUnsupported) + } if len(parts) == 1 && method == "delete" { data, err := c.Native(ctx, "delete", "/session/"+id, nil) if err != nil { diff --git a/internal/protocol/chat.go b/internal/protocol/chat.go index 9d411210..2d848f19 100644 --- a/internal/protocol/chat.go +++ b/internal/protocol/chat.go @@ -22,9 +22,12 @@ const ( ChatMessages = "chat_messages" ChatInterrupt = "chat_interrupt" ChatReply = "chat_reply" - // ChatEvent is the server-pushed frame; it is the only type the server - // sends without a matching request. + // ChatEvent and ChatPermission are the server-pushed frames; they are the + // only types the server sends without a matching request. ChatEvent = "chat_event" + // ChatPermission carries one pending OpenCode approval request the phone + // answers with chat_reply. + ChatPermission = "chat_permission" ) // Content part types carried by ChatPart.Type and ChatEvent.Kind. @@ -200,6 +203,35 @@ type ChatUsage struct { Cost float64 `json:"cost,omitempty"` } +// ChatPermissionFrame is the server-pushed chat_permission payload: one pending +// OpenCode permission request the phone must approve or reject. It is a distinct +// Message field (like ChatEvent) rather than a ChatEvent.Kind, so it does not +// overload the turn-progress kinds. Its field names match the service's +// Permission.Request schema, verified against the installed service's +// /openapi.json (id, sessionID, action and resources required; save, metadata, +// source and message optional), with id -> RequestID and sessionID -> SessionID. +// The wire shape is spec-verified; a live pending request has not been observed. +// Every optional field is omitempty so older peers ignore it. +type ChatPermissionFrame struct { + SessionID string `json:"session_id"` + RequestID string `json:"request_id"` + Action string `json:"action,omitempty"` + Resources []string `json:"resources,omitempty"` + Save []string `json:"save,omitempty"` + Metadata map[string]any `json:"metadata,omitempty"` + Source *ChatPermissionSource `json:"source,omitempty"` + Message string `json:"message,omitempty"` +} + +// ChatPermissionSource identifies what raised a permission request, matching the +// Permission.Source union (currently only a tool call: type "tool" plus the +// message and call id) verified against the service's /openapi.json. +type ChatPermissionSource struct { + Type string `json:"type,omitempty"` + MessageID string `json:"message_id,omitempty"` + ID string `json:"id,omitempty"` +} + // ChatReplyRequest is the chat_reply request body: the phone's decision on a // pending permission request, mirroring // POST /api/session/{id}/permission/{requestID}/reply. diff --git a/internal/protocol/chat_test.go b/internal/protocol/chat_test.go index f4dbc4bd..8b71040c 100644 --- a/internal/protocol/chat_test.go +++ b/internal/protocol/chat_test.go @@ -55,6 +55,13 @@ func chatSamples() []struct { }}, {"event_step", ChatEventFrame{SessionID: "ses_1", Kind: ChatEventStep, Step: &ChatStep{Status: ChatStepEnded, Index: 2}}}, {"event_usage", ChatEventFrame{SessionID: "ses_1", Kind: ChatEventUsage, Usage: &ChatUsage{InputTokens: 12, OutputTokens: 34, Cost: 0.001}}}, + {"permission", ChatPermissionFrame{ + SessionID: "ses_1", RequestID: "per_1", Action: "bash", + Resources: []string{"rm -rf /tmp"}, Save: []string{"bash:rm -rf /tmp"}, + Metadata: map[string]any{"command": "rm -rf /tmp"}, + Source: &ChatPermissionSource{Type: "tool", MessageID: "msg_1", ID: "call_1"}, + Message: "wants to run a command", + }}, {"reply", ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: ChatDecisionOnce, Message: "ok"}}, } } @@ -114,6 +121,32 @@ func TestChatEventGoldenJSON(t *testing.T) { } } +// TestChatPermissionGoldenJSON pins the exact wire shape of the pushed +// chat_permission frame so a JSON tag change cannot silently alter what the +// phone parses. +func TestChatPermissionGoldenJSON(t *testing.T) { + frame := ChatPermissionFrame{ + SessionID: "ses_1", RequestID: "per_1", Action: "bash", + Resources: []string{"rm -rf /tmp"}, + Source: &ChatPermissionSource{Type: "tool", MessageID: "msg_1", ID: "call_1"}, + } + encoded, err := json.Marshal(frame) + if err != nil { + t.Fatal(err) + } + want := `{"session_id":"ses_1","request_id":"per_1","action":"bash","resources":["rm -rf /tmp"],"source":{"type":"tool","message_id":"msg_1","id":"call_1"}}` + if string(encoded) != want { + t.Fatalf("golden mismatch\n got %s\nwant %s", encoded, want) + } + var decoded ChatPermissionFrame + if err := json.Unmarshal([]byte(want), &decoded); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(decoded, frame) { + t.Fatalf("golden did not decode back: %+v", decoded) + } +} + func TestChatEnvelopeRoundTrip(t *testing.T) { session := ChatSession{ID: "ses_1", Title: "demo"} message := Message{ @@ -125,7 +158,10 @@ func TestChatEnvelopeRoundTrip(t *testing.T) { ChatPrompt: &ChatPromptRequest{SessionID: "ses_1", Text: "hi"}, ChatMessages: []ChatMessage{{ID: "msg_1", SessionID: "ses_1", Role: ChatRoleUser, Parts: []ChatPart{{Type: ChatPartText, Text: "hi"}}}}, ChatEvent: &ChatEventFrame{SessionID: "ses_1", Kind: ChatEventText, Delta: "hi"}, - ChatReply: &ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: ChatDecisionReject}, + ChatPermission: &ChatPermissionFrame{ + SessionID: "ses_1", RequestID: "per_1", Action: "bash", Resources: []string{"rm -rf /"}, + }, + ChatReply: &ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: ChatDecisionReject}, } encoded, err := json.Marshal(message) if err != nil { @@ -145,6 +181,7 @@ func TestChatEnvelopeRoundTrip(t *testing.T) { !reflect.DeepEqual(decoded.ChatPrompt, message.ChatPrompt) || !reflect.DeepEqual(decoded.ChatMessages, message.ChatMessages) || !reflect.DeepEqual(decoded.ChatEvent, message.ChatEvent) || + !reflect.DeepEqual(decoded.ChatPermission, message.ChatPermission) || !reflect.DeepEqual(decoded.ChatReply, message.ChatReply) { t.Fatalf("chat payloads lost: %+v", decoded) } @@ -155,7 +192,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_reply"} { + for _, absent := range []string{"chat_sessions", "chat_session", "chat_create", "chat_query", "chat_prompt", "chat_messages", "chat_event", "chat_permission", "chat_reply"} { 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 f80556fa..7b63b470 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -135,12 +135,13 @@ type Message struct { Workspace string `json:"workspace,omitempty"` // Chat is the brokered OpenCode chat traffic (route B). See chat.go for the // per-type payload structs and the wire shapes they mirror. - ChatSessions []ChatSession `json:"chat_sessions,omitempty"` - ChatSession *ChatSession `json:"chat_session,omitempty"` - ChatCreate *ChatCreateRequest `json:"chat_create,omitempty"` - ChatQuery *ChatQuery `json:"chat_query,omitempty"` - ChatPrompt *ChatPromptRequest `json:"chat_prompt,omitempty"` - ChatMessages []ChatMessage `json:"chat_messages,omitempty"` - ChatEvent *ChatEventFrame `json:"chat_event,omitempty"` - ChatReply *ChatReplyRequest `json:"chat_reply,omitempty"` + ChatSessions []ChatSession `json:"chat_sessions,omitempty"` + ChatSession *ChatSession `json:"chat_session,omitempty"` + ChatCreate *ChatCreateRequest `json:"chat_create,omitempty"` + ChatQuery *ChatQuery `json:"chat_query,omitempty"` + ChatPrompt *ChatPromptRequest `json:"chat_prompt,omitempty"` + ChatMessages []ChatMessage `json:"chat_messages,omitempty"` + ChatEvent *ChatEventFrame `json:"chat_event,omitempty"` + ChatPermission *ChatPermissionFrame `json:"chat_permission,omitempty"` + ChatReply *ChatReplyRequest `json:"chat_reply,omitempty"` } diff --git a/internal/server/chat.go b/internal/server/chat.go index 08b579cc..ecc759db 100644 --- a/internal/server/chat.go +++ b/internal/server/chat.go @@ -2,6 +2,7 @@ package server import ( "context" + "errors" "log" "strings" "sync" @@ -22,9 +23,22 @@ type chatBroker interface { Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error Interrupt(ctx context.Context, sessionID string) error Messages(ctx context.Context, sessionID string, limit int) ([]protocol.ChatMessage, 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) Reply(ctx context.Context, request protocol.ChatReplyRequest) error } +// chatPermissionPollInterval is the fixed interval between permission polls: one +// GET per poll through the shared Connection bounds, never a busy loop. The first +// poll runs immediately so a request pending when the prompt is sent is surfaced +// without waiting a full interval. +const chatPermissionPollInterval = 2 * time.Second + +// chatPermissionRetainedLimit bounds the dedup set of already-pushed permission +// request ids one session retains, so a long turn cannot grow it without limit. +const chatPermissionRetainedLimit = 256 + // chatRetainedLimit bounds the state one session may retain so a stalled reader // or a runaway turn cannot grow memory without limit. It matches the OpenCode // response bound (1 MiB). @@ -69,6 +83,11 @@ type chatTurn struct { size int overflow bool updated time.Time + // permissions holds the request ids already pushed to the client so a + // repeated poll cannot push the same pending request twice; permissionOrder + // bounds it to chatPermissionRetainedLimit. + permissions map[string]struct{} + permissionOrder []string } func newChatHub() *chatHub { @@ -93,13 +112,7 @@ func (h *chatHub) apply(frame protocol.ChatEventFrame) { } h.mu.Lock() defer h.mu.Unlock() - turn := h.turns[frame.SessionID] - if turn == nil { - h.evictLocked() - turn = &chatTurn{sessionID: frame.SessionID, messages: map[string]*chatMessage{}} - h.turns[frame.SessionID] = turn - } - turn.updated = h.clock() + turn := h.turnLocked(frame.SessionID) switch frame.Kind { case protocol.ChatEventStep: turn.step = frame.Step @@ -210,6 +223,21 @@ func (h *chatHub) evictLocked() { } } +// turnLocked returns the session's retained turn, creating it (and evicting the +// least-recently-updated turn first when the cap is reached) and refreshing its +// update time. It is the shared turn-initialization path for apply and +// seenPermission, so the two cannot drift. Callers must hold h.mu. +func (h *chatHub) turnLocked(sessionID string) *chatTurn { + turn := h.turns[sessionID] + if turn == nil { + h.evictLocked() + turn = &chatTurn{sessionID: sessionID, messages: map[string]*chatMessage{}} + h.turns[sessionID] = turn + } + turn.updated = h.clock() + return turn +} + // retain records n more bytes and reports whether the turn is still within the // bound. The first overflow records one diagnostic and then drops further state // instead of growing without limit. @@ -265,6 +293,55 @@ func (h *chatHub) snapshot(sessionID string) []protocol.ChatMessage { return messages } +// seenPermission records requestID as pushed for the session and reports whether +// it is newly seen (true) or a duplicate (false). The retained ids are bounded: +// the oldest is dropped once the session exceeds chatPermissionRetainedLimit, so +// the same pending request is pushed at most once while the state stays bounded. +func (h *chatHub) seenPermission(sessionID, requestID string) bool { + if sessionID == "" || requestID == "" { + return false + } + h.mu.Lock() + defer h.mu.Unlock() + turn := h.turnLocked(sessionID) + if turn.permissions == nil { + turn.permissions = map[string]struct{}{} + } + if _, ok := turn.permissions[requestID]; ok { + return false + } + turn.permissions[requestID] = struct{}{} + turn.permissionOrder = append(turn.permissionOrder, requestID) + if len(turn.permissionOrder) > chatPermissionRetainedLimit { + oldest := turn.permissionOrder[0] + turn.permissionOrder = turn.permissionOrder[1:] + delete(turn.permissions, oldest) + } + return true +} + +// forgetPermission releases a request id reserved by seenPermission when its +// frame could not be written, so a later poll re-pushes it instead of the id +// staying "seen" while the phone never received the request. +func (h *chatHub) forgetPermission(sessionID, requestID string) { + if sessionID == "" || requestID == "" { + return + } + h.mu.Lock() + defer h.mu.Unlock() + turn := h.turns[sessionID] + if turn == nil { + return + } + delete(turn.permissions, requestID) + for i, id := range turn.permissionOrder { + if id == requestID { + turn.permissionOrder = append(turn.permissionOrder[:i], turn.permissionOrder[i+1:]...) + break + } + } +} + // handleChat dispatches one chat_* request. It returns true when the request was // a chat type and has been fully answered (the caller must not fall through to // the generic response). Frames are pushed after the ack, each through the @@ -388,6 +465,70 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat } bufferMu.Unlock() go drainChatStream(ctx, streamResult, streamCancel, sessionID, write) + // Poll for pending approval requests alongside the event stream, sharing + // streamCtx so the poller stops when the stream ends or the client + // disconnects. The first poll runs immediately. + go pollChatPermissions(streamCtx, broker, hub, sessionID, write) +} + +// pollChatPermissions polls the broker's pending-approval list on a fixed +// interval, sharing the chat subscription's context so it stops on disconnect or +// when the turn's stream ends. The hub dedups the request ids, so each pending +// request is pushed at most once; a poll failure is surfaced once through the +// same write closure instead of staying silent. A backend that does not expose +// the approval surface (opencode.ErrPermissionUnsupported) stops the poller +// quietly, so the documented limitation is not reported as a per-prompt error. +func pollChatPermissions(ctx context.Context, broker chatBroker, hub *chatHub, sessionID string, write func(protocol.Message) error) { + ticker := time.NewTicker(chatPermissionPollInterval) + defer ticker.Stop() + reported := false + for { + if err := pushChatPermissions(ctx, broker, hub, sessionID, write); err != nil { + if ctx.Err() != nil { + return + } + if errors.Is(err, opencode.ErrPermissionUnsupported) { + log.Printf("chat permission poll session=%s: %v", sessionID, err) + return + } + if reported { + log.Printf("chat permission poll session=%s: %v", sessionID, err) + } else { + reported = true + _ = write(protocol.Message{Type: protocol.Error, Error: "chat permission poll: " + err.Error()}) + } + } + select { + case <-ctx.Done(): + return + case <-ticker.C: + } + } +} + +// pushChatPermissions performs one poll and writes a chat_permission frame for +// every newly seen request of the session, through the same connection-scoped +// write closure as the chat stream. It never re-pushes a request id, and releases +// the reservation on a write failure so the frame is not silently dropped. +func pushChatPermissions(ctx context.Context, broker chatBroker, hub *chatHub, sessionID string, write func(protocol.Message) error) error { + pending, err := broker.Permissions(ctx, sessionID) + if err != nil { + return err + } + for _, request := range pending { + if request.SessionID != sessionID || request.RequestID == "" { + continue + } + if !hub.seenPermission(sessionID, request.RequestID) { + continue + } + frame := request + if err := write(protocol.Message{Type: protocol.ChatPermission, ChatPermission: &frame}); err != nil { + hub.forgetPermission(sessionID, request.RequestID) + return err + } + } + return nil } // drainChatStream reports the terminal error of a chat subscription and releases @@ -450,6 +591,16 @@ func handleChatSimple(ctx context.Context, request protocol.Message, broker chat _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid OpenCode session id is required"}) return } + if strings.TrimSpace(reply.RequestID) == "" { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a permission request id is required"}) + return + } + switch reply.Decision { + case protocol.ChatDecisionOnce, protocol.ChatDecisionAlways, protocol.ChatDecisionReject: + default: + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid permission decision is required"}) + return + } if err := broker.Reply(ctx, *reply); err != nil { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) return diff --git a/internal/server/chat_test.go b/internal/server/chat_test.go index 7516faa1..d7184520 100644 --- a/internal/server/chat_test.go +++ b/internal/server/chat_test.go @@ -26,10 +26,19 @@ type fakeChat struct { promptCalls int interrupts int replies int + replyRequest protocol.ChatReplyRequest + replyErr error messageCalls int messageSessionID string messageLimit int messages []protocol.ChatMessage + permissionCalls int + permissionSessionID string + permissions []protocol.ChatPermissionFrame + permissionErr error + permBlock bool + permDone chan struct{} + permOnce sync.Once script []protocol.ChatEventFrame started chan struct{} done chan struct{} @@ -102,11 +111,49 @@ func (f *fakeChat) messageQuery() (int, string, int) { return f.messageCalls, f.messageSessionID, f.messageLimit } -func (f *fakeChat) Reply(context.Context, protocol.ChatReplyRequest) error { +func (f *fakeChat) Reply(_ context.Context, request protocol.ChatReplyRequest) error { f.mu.Lock() defer f.mu.Unlock() f.replies++ - return nil + f.replyRequest = request + return f.replyErr +} + +// Permissions returns the seeded pending requests, or blocks until the caller's +// context is canceled when permBlock is set (used to prove the poller stops on +// disconnect). Every call is counted so a test can assert polling happened. +func (f *fakeChat) Permissions(ctx context.Context, sessionID string) ([]protocol.ChatPermissionFrame, error) { + f.mu.Lock() + f.permissionCalls++ + f.permissionSessionID = sessionID + frames := append([]protocol.ChatPermissionFrame(nil), f.permissions...) + err := f.permissionErr + block := f.permBlock + done := f.permDone + f.mu.Unlock() + if block { + <-ctx.Done() + if done != nil { + f.permOnce.Do(func() { close(done) }) + } + return nil, ctx.Err() + } + return frames, err +} + +// permissionQuery reports the recorded permission poll count and session id. +func (f *fakeChat) permissionQuery() (int, string) { + f.mu.Lock() + defer f.mu.Unlock() + return f.permissionCalls, f.permissionSessionID +} + +// 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) { + f.mu.Lock() + defer f.mu.Unlock() + return f.replies, f.replyRequest } func (f *fakeChat) counts() (int, int, int) { @@ -508,6 +555,9 @@ func TestChatInterruptEndsRunningPrompt(t *testing.T) { mu.Unlock() w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`)) + case r.Method == http.MethodGet && r.URL.Path == "/api/permission/request": + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"location":{"directory":"/work"},"data":[]}`)) case r.Method == http.MethodGet && r.URL.Path == "/api/event": flusher, ok := w.(http.Flusher) if !ok { @@ -758,6 +808,9 @@ func realAdapterChat(t *testing.T, frames []string) []protocol.ChatEventFrame { prompts.Add(1) w.Header().Set("Content-Type", "application/json") _, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`)) + case r.Method == http.MethodGet && r.URL.Path == "/api/permission/request": + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"location":{"directory":"/work"},"data":[]}`)) case r.Method == http.MethodGet && r.URL.Path == "/api/event": streams.Add(1) w.Header().Set("Content-Type", "text/event-stream") @@ -894,3 +947,270 @@ func TestChatSessionsAndCreateAnswerExplicitly(t *testing.T) { } } } + +// TestChatPromptPushesPermissionAndForwardsReply is the task evidence: with a +// blocking scripted stream keeping the subscription open, a seeded pending +// approval request is pushed as a chat_permission frame carrying the seeded +// session/request id after the prompt ack, and a chat_reply with Decision "once" +// is forwarded to the broker with the matching SessionID/RequestID/Decision. +// +// The live end-to-end approval flow against the real OpenCode service is NOT +// tested: this broker is the same interface seam the production handler uses, and +// the adapter's exact reply path/body are covered by the opencode package tests +// over an httptest fixture. +func TestChatPromptPushesPermissionAndForwardsReply(t *testing.T) { + broker := scriptedChat() + broker.block = true + broker.started = make(chan struct{}) + broker.permissions = []protocol.ChatPermissionFrame{{ + SessionID: "ses_1", RequestID: "req_1", Action: "bash", Resources: []string{"rm -rf /tmp"}, + }} + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "run it"}}); 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 != "r1" { + t.Fatalf("prompt ack = %+v", ack) + } + var permission protocol.Message + if err := wsjson.Read(ctx, conn, &permission); err != nil { + t.Fatal(err) + } + if permission.Type == protocol.Error { + t.Fatalf("unexpected error frame: %s", permission.Error) + } + if permission.Type != protocol.ChatPermission || permission.ChatPermission == nil { + t.Fatalf("expected a chat_permission frame, got %+v", permission) + } + if permission.ChatPermission.SessionID != "ses_1" || permission.ChatPermission.RequestID != "req_1" || permission.ChatPermission.Action != "bash" { + t.Fatalf("permission frame = %+v", permission.ChatPermission) + } + + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatReply, ID: "p1", ChatReply: &protocol.ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: protocol.ChatDecisionOnce}}); 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.ChatReply || response.ID != "p1" { + t.Fatalf("reply response = %+v", response) + } + count, forwarded := broker.replyQuery() + if count != 1 { + t.Fatalf("reply forwarded %d times, want 1", count) + } + if forwarded.SessionID != "ses_1" || forwarded.RequestID != "req_1" || forwarded.Decision != protocol.ChatDecisionOnce { + t.Fatalf("forwarded reply = %+v", forwarded) + } +} + +// TestChatPermissionDedupesRepeatedPolls pins that the poller pushes each +// pending request at most once even though it re-reads the same list, and that a +// foreign session's request is never surfaced. +func TestChatPermissionDedupesRepeatedPolls(t *testing.T) { + broker := scriptedChat() + broker.permissions = []protocol.ChatPermissionFrame{ + {SessionID: "ses_1", RequestID: "req_1", Action: "bash"}, + {SessionID: "ses_other", RequestID: "req_x", Action: "edit"}, + } + hub := newChatHub() + var mu sync.Mutex + var pushed []protocol.Message + write := func(message protocol.Message) error { + mu.Lock() + pushed = append(pushed, message) + mu.Unlock() + return nil + } + for i := 0; i < 3; i++ { + if err := pushChatPermissions(context.Background(), broker, hub, "ses_1", write); err != nil { + t.Fatal(err) + } + } + mu.Lock() + defer mu.Unlock() + if len(pushed) != 1 { + t.Fatalf("pushed %d permission frames, want 1: %+v", len(pushed), pushed) + } + if pushed[0].Type != protocol.ChatPermission || pushed[0].ChatPermission == nil || pushed[0].ChatPermission.RequestID != "req_1" { + t.Fatalf("pushed frame = %+v", pushed[0]) + } + if calls, sessionID := broker.permissionQuery(); calls != 3 || sessionID != "ses_1" { + t.Fatalf("permission polls = %d/%q, want 3/ses_1", calls, sessionID) + } +} + +// TestChatDisconnectCancelsPermissionPoller pins that the permission poller is +// canceled on disconnect (reusing the stream's context), so it cannot leak a +// goroutine after the client goes away. +func TestChatDisconnectCancelsPermissionPoller(t *testing.T) { + broker := scriptedChat() + broker.block = true + broker.started = make(chan struct{}) + broker.done = make(chan struct{}) + broker.permBlock = true + broker.permDone = make(chan struct{}) + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil { + t.Fatal(err) + } + var ack protocol.Message + if err := wsjson.Read(ctx, conn, &ack); err != nil { + t.Fatal(err) + } + select { + case <-broker.started: + case <-time.After(2 * time.Second): + t.Fatal("stream never started") + } + select { + case <-broker.permDone: + t.Fatal("permission poller returned before disconnect") + default: + } + conn.CloseNow() + select { + case <-broker.permDone: + case <-time.After(3 * time.Second): + t.Fatal("permission poller was not canceled on disconnect") + } + select { + case <-broker.done: + case <-time.After(3 * time.Second): + t.Fatal("chat stream was not canceled on disconnect") + } +} + +// TestChatPermissionPollErrorSurfaced pins that a malformed permission list is +// reported to the client as an error frame rather than silence. +func TestChatPermissionPollErrorSurfaced(t *testing.T) { + broker := scriptedChat() + broker.block = true + broker.started = make(chan struct{}) + broker.permissionErr = fmt.Errorf("invalid OpenCode permission response") + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil { + t.Fatal(err) + } + var ack protocol.Message + if err := wsjson.Read(ctx, conn, &ack); 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, "permission poll") { + t.Fatalf("malformed permission list not surfaced: %+v", response) + } +} + +// TestChatReplyRejectsInvalidInput pins that a missing session id, an empty +// request id and an invalid decision are rejected with a protocol.Error before +// any broker call. +func TestChatReplyRejectsInvalidInput(t *testing.T) { + broker := scriptedChat() + ctx, _, conn := dialChat(t, broker) + cases := []*protocol.ChatReplyRequest{ + nil, + {SessionID: "", RequestID: "req_1", Decision: protocol.ChatDecisionOnce}, + {SessionID: "not-a-session", RequestID: "req_1", Decision: protocol.ChatDecisionOnce}, + {SessionID: "ses_1", RequestID: "", Decision: protocol.ChatDecisionOnce}, + {SessionID: "ses_1", RequestID: "req_1", Decision: ""}, + {SessionID: "ses_1", RequestID: "req_1", Decision: "maybe"}, + } + for _, reply := range cases { + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatReply, ID: "bad", ChatReply: reply}); 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 || response.ID != "bad" { + t.Fatalf("reply %+v not rejected: %+v", reply, response) + } + } + if count, _ := broker.replyQuery(); count != 0 { + t.Fatalf("invalid replies reached the broker: %d", count) + } +} + +// TestChatReplyWithoutBroker pins that an unconfigured server answers chat_reply +// with the existing "not configured" error. +func TestChatReplyWithoutBroker(t *testing.T) { + ctx, _, conn := dialChat(t, nil) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatReply, ID: "p1", ChatReply: &protocol.ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: protocol.ChatDecisionOnce}}); 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, "not configured") { + t.Fatalf("unconfigured reply not rejected: %+v", response) + } +} + +// TestChatPermissionUnsupportedBackendIsQuiet pins that a backend which does not +// expose the approval surface (the documented native v1 limitation, reported as +// opencode.ErrPermissionUnsupported) stops the poller without writing a +// per-prompt error frame, so a working prompt is not misread as a failure. +func TestChatPermissionUnsupportedBackendIsQuiet(t *testing.T) { + broker := scriptedChat() + broker.block = true + broker.started = make(chan struct{}) + broker.permissionErr = opencode.ErrPermissionUnsupported + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatPrompt, ID: "r1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); 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 != "r1" { + t.Fatalf("prompt ack = %+v", ack) + } + // No error frame may follow: a read with a short deadline must time out. + readCtx, cancel := context.WithTimeout(ctx, 300*time.Millisecond) + defer cancel() + var unexpected protocol.Message + if err := wsjson.Read(readCtx, conn, &unexpected); err == nil { + t.Fatalf("unsupported permission surface must not write a frame, got %+v", unexpected) + } + if calls, _ := broker.permissionQuery(); calls != 1 { + t.Fatalf("permission polls = %d, want 1 (the poller must stop on the documented limitation)", calls) + } +} + +// TestChatPermissionWriteFailureDoesNotDropRequest pins that a request id is not +// retained as "seen" when its frame could not be written, so the next poll +// re-pushes it instead of silently dropping the approval request. +func TestChatPermissionWriteFailureDoesNotDropRequest(t *testing.T) { + broker := scriptedChat() + broker.permissions = []protocol.ChatPermissionFrame{{SessionID: "ses_1", RequestID: "req_1", Action: "bash"}} + hub := newChatHub() + attempts := 0 + write := func(protocol.Message) error { + attempts++ + if attempts == 1 { + return fmt.Errorf("connection closed") + } + return nil + } + if err := pushChatPermissions(context.Background(), broker, hub, "ses_1", write); err == nil { + t.Fatal("a failed write must surface an error") + } + if err := pushChatPermissions(context.Background(), broker, hub, "ses_1", write); err != nil { + t.Fatalf("retry after a failed write: %v", err) + } + if attempts != 2 { + t.Fatalf("push attempts = %d, want the request re-pushed after the failure", attempts) + } +} From 1818a06d2ff5dce8fd7210af72078cd04833f6c2 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 16 Oct 2026 09:21:50 +0100 Subject: [PATCH 6/7] Restrict chat_sessions to server-owned sessions chat_sessions now brokers GET /api/session but filters the global list to the sessions this server recorded in run_steps, deduped by session id, and fails closed to an empty list when no ownership source is configured. A session another client holds (in-server claim, released on stream end or disconnect) or an active run is marked busy; a second client's chat_prompt for a held session is rejected before any OpenCode call. chat_create stays unbrokered. --- .../author/skill/references/operations.md | 23 +- internal/opencode/chat.go | 86 ++++++++ internal/opencode/chat_test.go | 104 +++++++++ internal/opencode/native.go | 7 + internal/pipeline/runs.go | 44 +++- internal/pipeline/runs_test.go | 63 ++++++ internal/protocol/chat.go | 5 + internal/protocol/chat_test.go | 1 + internal/server/chat.go | 198 +++++++++++++++-- internal/server/chat_test.go | 201 ++++++++++++++++-- internal/server/websocket.go | 17 +- 11 files changed, 711 insertions(+), 38 deletions(-) diff --git a/internal/author/skill/references/operations.md b/internal/author/skill/references/operations.md index e1a5f039..44273aba 100644 --- a/internal/author/skill/references/operations.md +++ b/internal/author/skill/references/operations.md @@ -179,12 +179,23 @@ traversal, absolute paths, symlink escapes and unknown runs/steps. The protocol declares the brokered `chat_*` surface (`chat_sessions`, `chat_create`, `chat_prompt`, `chat_messages`, `chat_interrupt`, `chat_permission`, -`chat_reply`). The server dispatches `chat_prompt`, `chat_messages`, `chat_interrupt` -and `chat_reply`, answers the declared `chat_sessions`/`chat_create` with an explicit -protocol error because they are not yet brokered, and pushes `chat_event` frames -derived from OpenCode's global `/api/event` firehose, with retained -per-session/per-message state so a reconnecting client re-syncs instead of replaying -deltas. +`chat_reply`). The server dispatches `chat_sessions`, `chat_prompt`, +`chat_messages`, `chat_interrupt` and `chat_reply`, answers the declared +`chat_create` with an explicit protocol error because it is not yet brokered, and +pushes `chat_event` frames derived from OpenCode's global `/api/event` firehose, +with retained per-session/per-message state so a reconnecting client re-syncs +instead of replaying deltas. + +`chat_sessions` is brokered but restricted: OpenCode's `GET /api/session` lists +every session on the machine, so the server filters it to the sessions recorded +in `run_steps` as owned by this server (deduped by session id), fills any missing +directory from the owning run's workspace, and fails closed to an empty list when +no ownership source is configured rather than exposing the operator's whole +environment. A session another client currently holds (an in-server claim, +released when the stream ends or the client disconnects) or that this server +records as an active run is marked `busy` so the phone can show it read-only; a +second client's `chat_prompt` for a held session is rejected before any OpenCode +call. `chat_create` remains unbrokered. While a `chat_prompt` subscription is open the server also polls `GET /api/permission/request` on a fixed small interval (first poll immediately) and diff --git a/internal/opencode/chat.go b/internal/opencode/chat.go index 130ee793..ce20f0f6 100644 --- a/internal/opencode/chat.go +++ b/internal/opencode/chat.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "io" + "sort" "strings" "sync" @@ -109,6 +110,22 @@ func (c *Chat) Messages(ctx context.Context, id string, limit int) ([]protocol.C return decodeChatMessages(id, data) } +// Sessions lists the OpenCode sessions on the endpoint. On the V2 HTTP API that +// is GET /api/session; the native v1 serve translation maps it to GET /session. +// It decodes the verified {"data":[...]} envelope (tolerating a bare array like +// chatMessageRecords), lifts the nested location.directory and +// time{created,updated} into protocol.ChatSession, drops records without a valid +// session id, and orders newest-updated first so the list is deterministic. The +// service's list is global; the caller is responsible for restricting it to the +// sessions this server owns. +func (c *Chat) Sessions(ctx context.Context) ([]protocol.ChatSession, error) { + data, err := c.call(ctx, "get", "/api/session", nil) + if err != nil { + return nil, err + } + return decodeChatSessions(data) +} + // 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) { @@ -548,3 +565,72 @@ func decodeChatMessages(sessionID string, data []byte) ([]protocol.ChatMessage, } return messages, nil } + +// nativeChatSession is one listed session in the verified V2 shape +// ({id,title,agent,model,cost,tokens,outcome,time,title,location.directory, +// parentID,permissions}); only the fields the protocol surfaces are read. The +// native v1 serve readback of the same list is not covered by the research, so +// its records decode through the same tolerant envelope but any additional +// nesting is not asserted. +type nativeChatSession struct { + ID string `json:"id"` + Title string `json:"title"` + Agent string `json:"agent"` + Model nativeChatModel `json:"model"` + ParentID string `json:"parentID"` + Location struct { + Directory string `json:"directory"` + } `json:"location"` + Time struct { + Created int64 `json:"created"` + Updated int64 `json:"updated"` + } `json:"time"` +} + +// chatSessionRecords accepts both session-list envelopes: the V2 HTTP API wraps +// them in {"data":[...]} while the native serve readback returns a bare array. +// An unrecognised body is an error, never an empty list. +func chatSessionRecords(data []byte) ([]nativeChatSession, error) { + if records, ok := chatEnvelope[nativeChatSession](data); ok { + return records, nil + } + var records []nativeChatSession + if err := json.Unmarshal(data, &records); err != nil { + return nil, fmt.Errorf("invalid OpenCode session response") + } + return records, nil +} + +// decodeChatSessions maps the listed sessions onto protocol.ChatSession, dropping +// every record whose id is not a valid OpenCode session id (an empty or foreign +// id cannot be addressed) and ordering by UpdatedAt then id so the result is +// deterministic. +func decodeChatSessions(data []byte) ([]protocol.ChatSession, error) { + records, err := chatSessionRecords(data) + if err != nil { + return nil, err + } + sessions := make([]protocol.ChatSession, 0, len(records)) + for _, record := range records { + if !sessionID.MatchString(record.ID) { + continue + } + sessions = append(sessions, protocol.ChatSession{ + ID: record.ID, + Title: record.Title, + Agent: record.Agent, + Model: record.Model.ID, + Directory: record.Location.Directory, + ParentID: record.ParentID, + CreatedAt: record.Time.Created, + UpdatedAt: record.Time.Updated, + }) + } + sort.SliceStable(sessions, func(i, j int) bool { + if sessions[i].UpdatedAt != sessions[j].UpdatedAt { + return sessions[i].UpdatedAt > sessions[j].UpdatedAt + } + return sessions[i].ID < sessions[j].ID + }) + return sessions, nil +} diff --git a/internal/opencode/chat_test.go b/internal/opencode/chat_test.go index b3c5a579..53181de7 100644 --- a/internal/opencode/chat_test.go +++ b/internal/opencode/chat_test.go @@ -499,6 +499,110 @@ func TestDecodeChatMessagesSkipsUnknownPartsAndRoles(t *testing.T) { } } +// TestChatSessionsListsAndLiftsShape loads the verified V2 session list +// (GET /api/session: {"data":[{id,...,location.directory,time{created,updated}, +// parentID}]}) and asserts the adapter reads it exactly once, lifts the nested +// directory/time onto protocol.ChatSession, and orders newest-updated first. +func TestChatSessionsListsAndLiftsShape(t *testing.T) { + var reads atomic.Int32 + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet || r.URL.Path != "/api/session" { + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + return + } + reads.Add(1) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"location":{"directory":"/work"},"data":[ + {"id":"ses_old","title":"older","agent":"build","model":{"id":"m1"},"parentID":"ses_new","location":{"directory":"/proj"},"time":{"created":10,"updated":20}}, + {"id":"ses_new","title":"newer","agent":"general","model":{"id":"m2"},"location":{"directory":"/proj"},"time":{"created":30,"updated":50}} + ],"cursor":{"next":null}}`)) + })) + defer srv.Close() + + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + sessions, err := chat.Sessions(ctx) + if err != nil { + t.Fatalf("sessions: %v", err) + } + if reads.Load() != 1 { + t.Fatalf("session endpoint read %d times, want 1", reads.Load()) + } + if len(sessions) != 2 { + t.Fatalf("sessions = %+v, want 2", sessions) + } + newer, older := sessions[0], sessions[1] + if newer.ID != "ses_new" || older.ID != "ses_old" { + t.Fatalf("not ordered newest-updated first: %+v", sessions) + } + if newer.Title != "newer" || newer.Agent != "general" || newer.Model != "m2" || newer.Directory != "/proj" || newer.CreatedAt != 30 || newer.UpdatedAt != 50 { + t.Fatalf("lifted fields wrong: %+v", newer) + } + if older.ParentID != "ses_new" || older.UpdatedAt != 20 || older.Title != "older" || older.Model != "m1" { + t.Fatalf("older session fields wrong: %+v", older) + } +} + +// TestChatSessionsTolerantEnvelopes pins the decoder's edge cases: a bare array +// (the native serve readback) is accepted, records without a valid session id +// are dropped, and an unparseable body or a data:null envelope is an error, not +// a silent empty success. +func TestChatSessionsTolerantEnvelopes(t *testing.T) { + sessions, err := decodeChatSessions([]byte(`[ + {"id":"ses_1","title":"kept","time":{"updated":5}}, + {"id":"","title":"dropped empty"}, + {"id":"msg_not_a_session","title":"dropped invalid"} + ]`)) + if err != nil { + t.Fatalf("bare array decode: %v", err) + } + if len(sessions) != 1 || sessions[0].ID != "ses_1" || sessions[0].Title != "kept" { + t.Fatalf("bare array mapping wrong: %+v", sessions) + } + if sessions, err := decodeChatSessions([]byte(`{"data":[]}`)); err != nil || len(sessions) != 0 { + t.Fatalf("empty data array must decode to no sessions: %+v %v", sessions, err) + } + for _, raw := range []string{`{"data":null}`, `not json`, `{"location":{"directory":"/work"}}`} { + if _, err := decodeChatSessions([]byte(raw)); err == nil { + t.Fatalf("body %q must be an error, not empty success", raw) + } + } +} + +// TestChatSessionsV1Translation pins the native serve path: GET /api/session is +// translated to the native GET /session (a bare array), so chat_sessions works +// on the backend that currently enables brokered chat. The native list shape is +// not covered by the research; this asserts the translated endpoint and the +// shared tolerant decode, not an unverified service shape. +func TestChatSessionsV1Translation(t *testing.T) { + var sawList bool + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet || r.URL.Path != "/session" { + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + return + } + sawList = true + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`[{"id":"ses_1","title":"native","time":{"created":1,"updated":2}}]`)) + })) + defer srv.Close() + + chat := NewChat(&Connection{Backend: "v1", URL: srv.URL, Directory: "/repo", models: map[string]map[string]string{}, responses: map[string][]byte{}}) + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + sessions, err := chat.Sessions(ctx) + if err != nil { + t.Fatalf("sessions: %v", err) + } + if !sawList { + t.Fatal("GET /api/session was not translated to the native GET /session") + } + if len(sessions) != 1 || sessions[0].ID != "ses_1" || sessions[0].Title != "native" || sessions[0].UpdatedAt != 2 { + t.Fatalf("native session list mapped wrong: %+v", sessions) + } +} + func TestChatStreamRejectsMalformedEvent(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/event-stream") diff --git a/internal/opencode/native.go b/internal/opencode/native.go index e613a381..4b195d53 100644 --- a/internal/opencode/native.go +++ b/internal/opencode/native.go @@ -115,6 +115,13 @@ func (c *Connection) Call(ctx context.Context, method, path string, body any) ([ c.models[session.ID] = map[string]string{"providerID": request.Model["providerID"], "modelID": request.Model["id"]} return json.Marshal(map[string]any{"data": map[string]string{"id": session.ID}}) } + // The brokered chat session list is GET /api/session on the V2 HTTP API; the + // native serve surface lists sessions at /session and returns a bare array, + // which the adapter's tolerant decoder accepts. This keeps chat_sessions + // working on the backend that currently enables brokered chat. + if path == "/api/session" && method == "get" { + return c.Native(ctx, "get", "/session", nil) + } clean := strings.SplitN(path, "?", 2)[0] // The brokered approval surface is implemented against the V2 HTTP API: the // documented endpoints are GET /api/permission/request and POST diff --git a/internal/pipeline/runs.go b/internal/pipeline/runs.go index 5707b375..03c8ca6f 100644 --- a/internal/pipeline/runs.go +++ b/internal/pipeline/runs.go @@ -26,6 +26,7 @@ input_json TEXT NOT NULL, output_json TEXT NOT NULL DEFAULT '{}', diagnostic TEX session_id TEXT NOT NULL DEFAULT '', agent_active INTEGER NOT NULL DEFAULT 0, transcript TEXT NOT NULL DEFAULT '', started_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP, finished_at TEXT, PRIMARY KEY(run_id,sequence)); CREATE INDEX IF NOT EXISTS run_latest ON pipeline_runs(id DESC); +CREATE INDEX IF NOT EXISTS run_steps_session ON run_steps(session_id); CREATE TABLE IF NOT EXISTS step_notifications ( run_id INTEGER NOT NULL, sequence INTEGER NOT NULL, receipts_json TEXT NOT NULL, PRIMARY KEY(run_id,sequence), FOREIGN KEY(run_id,sequence) REFERENCES run_steps(run_id,sequence)); @@ -335,6 +336,8 @@ type SessionOwner struct { // RunID is the owning run, used to tell an orphaned session (no active run) // from one owned by a run that is active right now. RunID int64 + // Active is true while the owning step records the session as agent-active. + Active bool } func (s *Store) RunSessionOwner(ctx context.Context, id int64, sequence int) (SessionOwner, error) { @@ -343,19 +346,54 @@ func (s *Store) RunSessionOwner(ctx context.Context, id int64, sequence int) (Se return owner, err } +// sessionOwnerScanLimit bounds the ownership scan so a long-lived database with +// thousands of historical sessions cannot make one chat_sessions request scan +// them all. Rows are read newest-run first, so the bound only ever drops the +// oldest owners, never the sessions a client can still be writing. +const sessionOwnerScanLimit = 1024 + +// ActiveSessionOwners reports every currently active agent session, deduped by +// session id (newest owning run wins). func (s *Store) ActiveSessionOwners(ctx context.Context) ([]SessionOwner, error) { - rows, err := s.db.QueryContext(ctx, `SELECT s.session_id,COALESCE(o.backend,'v2'),r.workspace,COALESCE(o.reference,''),s.run_id -FROM run_steps s JOIN pipeline_runs r ON r.id=s.run_id LEFT JOIN run_session_owners o ON o.run_id=s.run_id AND o.sequence=s.sequence WHERE s.agent_active=1 AND s.session_id<>''`) + return s.sessionOwners(ctx, true) +} + +// OwnedSessions reports every session the server ever recorded in run_steps, +// deduped by session id (newest owning run wins) and including inactive ones, so +// chat_sessions can be restricted to sessions this server created. +func (s *Store) OwnedSessions(ctx context.Context) ([]SessionOwner, error) { + return s.sessionOwners(ctx, false) +} + +// sessionOwners is the single ownership query both ActiveSessionOwners and +// OwnedSessions run, so the SQL is not duplicated. It reads one bounded, +// newest-run-first window and drops repeated session ids in favour of the most +// recent owning run's directory and active flag. +func (s *Store) sessionOwners(ctx context.Context, activeOnly bool) ([]SessionOwner, error) { + query := `SELECT s.session_id,COALESCE(o.backend,'v2'),r.workspace,COALESCE(o.reference,''),s.run_id,s.agent_active +FROM run_steps s JOIN pipeline_runs r ON r.id=s.run_id LEFT JOIN run_session_owners o ON o.run_id=s.run_id AND o.sequence=s.sequence WHERE s.session_id<>''` + if activeOnly { + query += " AND s.agent_active=1" + } + query += " ORDER BY s.run_id DESC LIMIT ?" + rows, err := s.db.QueryContext(ctx, query, sessionOwnerScanLimit) if err != nil { return nil, err } defer rows.Close() + seen := map[string]struct{}{} var owners []SessionOwner for rows.Next() { var owner SessionOwner - if err = rows.Scan(&owner.ID, &owner.Backend, &owner.Directory, &owner.Reference, &owner.RunID); err != nil { + var active int + if err = rows.Scan(&owner.ID, &owner.Backend, &owner.Directory, &owner.Reference, &owner.RunID, &active); err != nil { return nil, err } + owner.Active = active != 0 + if _, ok := seen[owner.ID]; ok { + continue + } + seen[owner.ID] = struct{}{} owners = append(owners, owner) } return owners, rows.Err() diff --git a/internal/pipeline/runs_test.go b/internal/pipeline/runs_test.go index bd1a281c..ec165034 100644 --- a/internal/pipeline/runs_test.go +++ b/internal/pipeline/runs_test.go @@ -149,3 +149,66 @@ func TestActiveAgentSessionsAreScopedToRun(t *testing.T) { t.Fatalf("second run's own session was affected: %v %v", sessions, err) } } + +// TestOwnedSessionsListsRecordedWithActiveFlag proves OwnedSessions returns every +// session the server ever recorded (active or not) in run_steps, deduped by +// session id in favour of the newest owning run, with that run's workspace +// directory and the active flag. +func TestOwnedSessionsListsRecordedWithActiveFlag(t *testing.T) { + ctx := context.Background() + store, err := Open(filepath.Join(t.TempDir(), "owned.db")) + if err != nil { + t.Fatal(err) + } + defer store.Close() + if _, err := store.Submit(ctx, "owned", Template([]string{"one"}, true)); err != nil { + t.Fatal(err) + } + items, err := store.List(ctx) + if err != nil { + t.Fatal(err) + } + definition, err := store.Get(ctx, items[0].ID, 1) + if err != nil { + t.Fatal(err) + } + record := func(key, workspace, session string, active bool) { + t.Helper() + run, _, err := store.CreateRun(ctx, definition, key, workspace, false, nil, nil) + if err != nil { + t.Fatal(err) + } + if err := store.BeginStep(ctx, run, 1, "one", map[string]any{"prompt": "task"}); err != nil { + t.Fatal(err) + } + if err := store.RunSession(ctx, run, 1, session, active, "v2", "/repo"); err != nil { + t.Fatal(err) + } + } + record("owned-a", "/work/a", "ses_active", true) + record("owned-b", "/work/b", "ses_idle", false) + // The same session recorded by a newer run must dedupe to one entry, taking + // the newest run's workspace and active flag. + record("owned-c", "/work/c", "ses_idle", true) + + owners, err := store.OwnedSessions(ctx) + if err != nil { + t.Fatal(err) + } + if len(owners) != 2 { + t.Fatalf("owned sessions = %+v, want 2 deduped", owners) + } + byID := map[string]SessionOwner{} + for _, owner := range owners { + byID[owner.ID] = owner + } + if owner, ok := byID["ses_active"]; !ok || !owner.Active || owner.Directory != "/work/a" { + t.Fatalf("active owner = %+v", owner) + } + if owner, ok := byID["ses_idle"]; !ok || !owner.Active || owner.Directory != "/work/c" { + t.Fatalf("deduped owner should come from the newest run: %+v", owner) + } + if active, err := store.ActiveSessionOwners(ctx); err != nil || len(active) != 2 { + t.Fatalf("ActiveSessionOwners = %+v %v, want both now-active sessions", active, err) + } +} diff --git a/internal/protocol/chat.go b/internal/protocol/chat.go index 2d848f19..33ce57e0 100644 --- a/internal/protocol/chat.go +++ b/internal/protocol/chat.go @@ -87,6 +87,11 @@ type ChatSession struct { ParentID string `json:"parent_id,omitempty"` CreatedAt int64 `json:"created_at,omitempty"` UpdatedAt int64 `json:"updated_at,omitempty"` + // Busy marks a session another client currently holds (or one the server + // itself owns as an active run), so the phone can show it read-only instead + // of letting two clients silently write the same conversation. It is + // additive and optional: older peers ignore it. + Busy bool `json:"busy,omitempty"` } // ChatCreateRequest is the chat_create request body: a title and an optional diff --git a/internal/protocol/chat_test.go b/internal/protocol/chat_test.go index 8b71040c..046a9ced 100644 --- a/internal/protocol/chat_test.go +++ b/internal/protocol/chat_test.go @@ -21,6 +21,7 @@ func chatSamples() []struct { ID: "ses_1", Title: "demo", Agent: "build", Model: "deepseek-v4.1-flash", Directory: "/work", ParentID: "ses_0", CreatedAt: 1792112366711, UpdatedAt: 1792112367999, }}, + {"session_busy", ChatSession{ID: "ses_2", Title: "held", Busy: true}}, {"create", ChatCreateRequest{Title: "demo", Directory: "/work"}}, {"query", ChatQuery{SessionID: "ses_1", Limit: 50}}, {"prompt", ChatPromptRequest{SessionID: "ses_1", Text: "hello", Model: "m", Agent: "build", Files: []string{"file:///tmp/a.png"}}}, diff --git a/internal/server/chat.go b/internal/server/chat.go index ecc759db..4541cf4b 100644 --- a/internal/server/chat.go +++ b/internal/server/chat.go @@ -2,6 +2,8 @@ package server import ( "context" + "crypto/rand" + "encoding/hex" "errors" "log" "strings" @@ -9,6 +11,7 @@ import ( "time" "git.a-wan.net/alan/pipelines/internal/opencode" + "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) @@ -23,6 +26,9 @@ type chatBroker interface { Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error Interrupt(ctx context.Context, sessionID string) error Messages(ctx context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error) + // Sessions lists the endpoint's OpenCode sessions so the handler can + // restrict them to the ones this server owns. + Sessions(ctx context.Context) ([]protocol.ChatSession, 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) @@ -49,6 +55,18 @@ const chatRetainedLimit = opencode.MaxResponse // least-recently-updated turn is evicted once the cap is exceeded. const chatMaxTurns = 128 +// chatClientToken returns an unguessable per-connection identity. It attributes +// a session claim so two clients on the same server cannot silently write the +// same conversation; a fresh token per connection means a reconnected client is +// a new writer and must re-claim. +func chatClientToken() string { + var b [8]byte + if _, err := rand.Read(b[:]); err != nil { + return "conn-unknown" + } + return "conn-" + hex.EncodeToString(b[:]) +} + // chatHub retains the accumulated turn state per session, independently of any // single connection, so a reconnecting client can re-sync what it missed. State // is keyed by session and then by message id (a session holds many turns), and @@ -57,6 +75,10 @@ type chatHub struct { mu sync.Mutex turns map[string]*chatTurn now func() time.Time + // claims maps a session to the client currently writing it so a second + // client is rejected; claimOrder bounds the map like chatMaxTurns. + claims map[string]string + claimOrder []string } // chatPart is the retained form of one part. Text and reasoning deltas append to @@ -223,6 +245,70 @@ func (h *chatHub) evictLocked() { } } +// claim reserves sessionID for clientID. It reports true when the session is +// free or already held by the same client (so a client may re-prompt its own +// session) and false when another client holds it. The claim map is bounded by +// chatMaxTurns with oldest-eviction so a stuck client cannot grow it without +// limit. +func (h *chatHub) claim(sessionID, clientID string) bool { + if sessionID == "" || clientID == "" { + return false + } + h.mu.Lock() + defer h.mu.Unlock() + if h.claims == nil { + h.claims = map[string]string{} + } + if holder, ok := h.claims[sessionID]; ok { + return holder == clientID + } + h.evictClaimsLocked() + h.claims[sessionID] = clientID + h.claimOrder = append(h.claimOrder, sessionID) + return true +} + +// release drops the same client's claim on sessionID; a claim held by a +// different client is left untouched so a stale release cannot free a live one. +func (h *chatHub) release(sessionID, clientID string) { + h.mu.Lock() + defer h.mu.Unlock() + if holder, ok := h.claims[sessionID]; !ok || holder != clientID { + return + } + delete(h.claims, sessionID) + for i, id := range h.claimOrder { + if id == sessionID { + h.claimOrder = append(h.claimOrder[:i], h.claimOrder[i+1:]...) + break + } + } +} + +// busyForOther reports whether another client currently holds sessionID. +func (h *chatHub) busyForOther(sessionID, clientID string) bool { + h.mu.Lock() + defer h.mu.Unlock() + holder, ok := h.claims[sessionID] + return ok && holder != clientID +} + +// evictClaimsLocked drops the oldest claim when the cap is reached, keeping the +// claim map bounded like the retained turns. Callers must hold h.mu. +func (h *chatHub) evictClaimsLocked() { + if len(h.claims) < chatMaxTurns { + return + } + for len(h.claimOrder) > 0 { + oldest := h.claimOrder[0] + h.claimOrder = h.claimOrder[1:] + if _, ok := h.claims[oldest]; ok { + delete(h.claims, oldest) + return + } + } +} + // turnLocked returns the session's retained turn, creating it (and evicting the // least-recently-updated turn first when the cap is reached) and refreshing its // update time. It is the shared turn-initialization path for apply and @@ -347,10 +433,10 @@ func (h *chatHub) forgetPermission(sessionID, requestID string) { // the generic response). Frames are pushed after the ack, each through the // connection-scoped write function so the read loop and the stream goroutine // never interleave partial websocket writes. -func handleChat(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) bool { +func handleChat(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, owners func(context.Context) ([]pipeline.SessionOwner, error), clientID string, write func(protocol.Message) error) bool { switch request.Type { case protocol.ChatPrompt: - handleChatPrompt(ctx, request, broker, hub, write) + handleChatPrompt(ctx, request, broker, hub, clientID, write) return true case protocol.ChatMessages: handleChatMessages(ctx, request, broker, hub, write) @@ -361,17 +447,91 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker case protocol.ChatReply: handleChatSimple(ctx, request, broker, hub, write) return true - case protocol.ChatSessions, protocol.ChatCreate: - // Declared in the protocol for the client but not yet brokered. Answer - // explicitly so the client never mistakes the generic Pong for a chat - // reply, and the boundary stays visible instead of silently dropping. - _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat_sessions and chat_create are not yet brokered"}) + case protocol.ChatSessions: + handleChatSessions(ctx, request, broker, hub, owners, clientID, write) + return true + case protocol.ChatCreate: + // chat_create remains declared for the client but not yet brokered. + // Answer explicitly so the client never mistakes the generic Pong for a + // chat reply, and the boundary stays visible instead of silently + // dropping. + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat_create is not yet brokered"}) return true } return false } -func handleChatPrompt(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, write func(protocol.Message) error) { +// handleChatSessions answers chat_sessions with the OpenCode sessions this +// server owns, marking any another client currently holds (or an active run) as +// Busy. FAIL-CLOSED: when no ownership source is configured the result is empty, +// never the whole endpoint list. broker.Sessions is called at most once per +// request. +func handleChatSessions(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, owners func(context.Context) ([]pipeline.SessionOwner, error), clientID string, 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 + } + owned, err := ownedSessionSet(ctx, owners) + if err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + sessions, err := broker.Sessions(ctx) + if err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + response := protocol.Message{Type: protocol.ChatSessions, ID: request.ID} + for _, session := range sessions { + owner, ok := owned[session.ID] + if !ok { + continue + } + if session.Directory == "" { + session.Directory = owner.Directory + } + if owner.Active || hub.busyForOther(session.ID, clientID) { + session.Busy = true + } + response.ChatSessions = append(response.ChatSessions, session) + } + _ = write(response) +} + +// ownedSessionSet reads the ownership source once and returns a set keyed by +// session id, deduped in favour of an active owner and a non-empty directory. +// A nil source (no store and no injection) yields an empty set: chat_sessions +// then lists nothing rather than the whole OpenCode machine. +func ownedSessionSet(ctx context.Context, owners func(context.Context) ([]pipeline.SessionOwner, error)) (map[string]pipeline.SessionOwner, error) { + set := map[string]pipeline.SessionOwner{} + if owners == nil { + return set, nil + } + list, err := owners(ctx) + if err != nil { + return nil, err + } + for _, owner := range list { + if owner.ID == "" { + continue + } + existing, ok := set[owner.ID] + if !ok { + set[owner.ID] = owner + continue + } + if owner.Active && !existing.Active { + existing.Active = true + } + if existing.Directory == "" { + existing.Directory = owner.Directory + } + set[owner.ID] = existing + } + return set, nil +} + +func handleChatPrompt(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, clientID string, 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 @@ -395,12 +555,19 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat prompt exceeds 64 KiB"}) return } + // Claim the session before subscribing or prompting: a second client must be + // rejected here, before any broker call, so two clients cannot silently write + // the same conversation. The same client may re-prompt its own session. + sessionID := prompt.SessionID + if !hub.claim(sessionID, clientID) { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + sessionID + " is busy with another client"}) + return + } // Subscribe to the OpenCode firehose before posting the prompt: the stream // has no replay cursor (research 2.4), so a delta emitted between the POST // and the subscription would otherwise be lost. Frames that arrive before the // ack is written are buffered and flushed in order after it. - sessionID := prompt.SessionID streamCtx, streamCancel := context.WithCancel(ctx) attached := make(chan struct{}) var attachOnce sync.Once @@ -428,17 +595,20 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat case <-attached: case err := <-streamResult: streamCancel() + hub.release(sessionID, clientID) if err != nil && ctx.Err() == nil { _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat stream: " + err.Error()}) } return case <-ctx.Done(): streamCancel() + hub.release(sessionID, clientID) return } if err := broker.Prompt(ctx, sessionID, prompt.Text); err != nil { streamCancel() + hub.release(sessionID, clientID) _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) return } @@ -446,6 +616,7 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat // reply. The prompt is sent exactly once and never re-sent on resume. if err := write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID}); err != nil { streamCancel() + hub.release(sessionID, clientID) return } // Flush frames buffered while the prompt was accepted, then resume live @@ -460,11 +631,12 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat if err := write(protocol.Message{Type: protocol.ChatEvent, ChatEvent: &frame}); err != nil { bufferMu.Unlock() streamCancel() + hub.release(sessionID, clientID) return } } bufferMu.Unlock() - go drainChatStream(ctx, streamResult, streamCancel, sessionID, write) + go drainChatStream(ctx, streamResult, streamCancel, hub, sessionID, clientID, write) // Poll for pending approval requests alongside the event stream, sharing // streamCtx so the poller stops when the stream ends or the client // disconnects. The first poll runs immediately. @@ -532,9 +704,11 @@ func pushChatPermissions(ctx context.Context, broker chatBroker, hub *chatHub, s } // drainChatStream reports the terminal error of a chat subscription and releases -// its context once the stream has ended. -func drainChatStream(ctx context.Context, result <-chan error, cancel context.CancelFunc, sessionID string, write func(protocol.Message) error) { +// its context and the session claim once the stream has ended, so a later client +// is no longer blocked and nothing leaks. +func drainChatStream(ctx context.Context, result <-chan error, cancel context.CancelFunc, hub *chatHub, sessionID, clientID string, write func(protocol.Message) error) { defer cancel() + defer hub.release(sessionID, clientID) if err := <-result; err != nil && ctx.Err() == nil { log.Printf("chat stream session=%s: %v", sessionID, err) _ = write(protocol.Message{Type: protocol.Error, Error: "chat stream: " + err.Error()}) diff --git a/internal/server/chat_test.go b/internal/server/chat_test.go index d7184520..e4c58fbb 100644 --- a/internal/server/chat_test.go +++ b/internal/server/chat_test.go @@ -16,6 +16,7 @@ import ( "github.com/coder/websocket/wsjson" "git.a-wan.net/alan/pipelines/internal/opencode" + "git.a-wan.net/alan/pipelines/internal/pipeline" "git.a-wan.net/alan/pipelines/internal/protocol" ) @@ -36,6 +37,9 @@ type fakeChat struct { permissionSessionID string permissions []protocol.ChatPermissionFrame permissionErr error + sessionCalls int + sessions []protocol.ChatSession + sessionErr error permBlock bool permDone chan struct{} permOnce sync.Once @@ -148,6 +152,22 @@ func (f *fakeChat) permissionQuery() (int, string) { return f.permissionCalls, f.permissionSessionID } +// Sessions returns the seeded session list and counts each call so a test can +// assert the list handler asks the broker at most once per request. +func (f *fakeChat) Sessions(context.Context) ([]protocol.ChatSession, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.sessionCalls++ + return f.sessions, f.sessionErr +} + +// sessionQuery reports the recorded chat_sessions call count. +func (f *fakeChat) sessionQuery() int { + f.mu.Lock() + defer f.mu.Unlock() + return f.sessionCalls +} + // 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) { @@ -176,16 +196,33 @@ func scriptedChat() *fakeChat { } func dialChat(t *testing.T, broker chatBroker) (context.Context, context.CancelFunc, *websocket.Conn) { + t.Helper() + return dialChatOptions(t, Options{Chat: broker}) +} + +func dialChatOptions(t *testing.T, options Options) (context.Context, context.CancelFunc, *websocket.Conn) { + t.Helper() + ctx, cancel, conns := dialChatConns(t, options, 1) + return ctx, cancel, conns[0] +} + +// dialChatConns starts one server and dials n connections to the same hub, so a +// test can exercise two clients contending for one session. +func dialChatConns(t *testing.T, options Options, n int) (context.Context, context.CancelFunc, []*websocket.Conn) { t.Helper() ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - srv := httptest.NewServer(HandlerWithOptions(ctx, Options{Chat: broker})) + srv := httptest.NewServer(HandlerWithOptions(ctx, options)) t.Cleanup(func() { srv.Close(); cancel() }) - conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil) - if err != nil { - t.Fatal(err) + conns := make([]*websocket.Conn, 0, n) + for i := 0; i < n; i++ { + conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil) + if err != nil { + t.Fatal(err) + } + t.Cleanup(func() { conn.CloseNow() }) + conns = append(conns, conn) } - t.Cleanup(func() { conn.CloseNow() }) - return ctx, cancel, conn + return ctx, cancel, conns } func readChatTurn(t *testing.T, ctx context.Context, conn *websocket.Conn) []protocol.ChatEventFrame { @@ -928,23 +965,155 @@ func TestChatPromptRejectsEmptyText(t *testing.T) { } } -// TestChatSessionsAndCreateAnswerExplicitly pins the declared-but-unbrokered -// boundary: chat_sessions and chat_create are answered with a protocol error -// rather than falling through to the generic Pong. -func TestChatSessionsAndCreateAnswerExplicitly(t *testing.T) { +// TestChatCreateAnswersExplicitly pins the declared-but-unbrokered boundary: +// chat_create is answered with a protocol error rather than falling through to +// the generic Pong. chat_sessions is now brokered and covered by +// TestChatSessionsListsOnlyOwned and TestChatSessionsBusyRejectsSecondClient. +func TestChatCreateAnswersExplicitly(t *testing.T) { broker := scriptedChat() ctx, _, conn := dialChat(t, broker) - for _, typ := range []string{protocol.ChatSessions, protocol.ChatCreate} { - if err := wsjson.Write(ctx, conn, protocol.Message{Type: typ, ID: "c1"}); err != nil { + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatCreate, ID: "c1"}); 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 || response.ID != "c1" { + t.Fatalf("chat_create did not answer with an explicit error: %+v", response) + } +} + +// TestChatSessionsListsOnlyOwned is the task evidence for ownership: the broker +// lists two OpenCode sessions (one owned by this server, one foreign) and +// chat_sessions returns exactly the owned one, asking the broker once. +func TestChatSessionsListsOnlyOwned(t *testing.T) { + broker := scriptedChat() + broker.sessions = []protocol.ChatSession{ + {ID: "ses_owned", Title: "ours", Directory: "/work"}, + {ID: "ses_foreign", Title: "someone else's", Directory: "/elsewhere"}, + } + owners := func(context.Context) ([]pipeline.SessionOwner, error) { + return []pipeline.SessionOwner{{ID: "ses_owned", Directory: "/work"}}, nil + } + ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OwnedSessions: owners}) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatSessions, ID: "s1"}); 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.ChatSessions || response.ID != "s1" { + t.Fatalf("chat_sessions response = %+v", response) + } + if len(response.ChatSessions) != 1 || response.ChatSessions[0].ID != "ses_owned" { + t.Fatalf("foreign session leaked into the list: %+v", response.ChatSessions) + } + if response.ChatSessions[0].Busy { + t.Fatalf("inactive owned session marked busy: %+v", response.ChatSessions[0]) + } + if calls := broker.sessionQuery(); calls != 1 { + t.Fatalf("broker.Sessions called %d times, want 1", calls) + } +} + +// TestChatSessionsFailsClosedWithoutOwnership pins the default when no ownership +// source is available: chat_sessions returns an empty list, never the full +// OpenCode machine. +func TestChatSessionsFailsClosedWithoutOwnership(t *testing.T) { + broker := scriptedChat() + broker.sessions = []protocol.ChatSession{{ID: "ses_1"}, {ID: "ses_2"}} + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatSessions, ID: "s1"}); 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.ChatSessions || len(response.ChatSessions) != 0 { + t.Fatalf("unowned sessions were exposed: %+v", response) + } +} + +// TestChatSessionsBusyRejectsSecondClient is the task evidence for busy state: +// while client A holds ses_1 with a blocking prompt, client B's chat_sessions +// reports ses_1 Busy=true and B's chat_prompt for ses_1 is rejected before any +// broker.Prompt call; after A disconnects the claim is released and a later list +// is no longer busy. +func TestChatSessionsBusyRejectsSecondClient(t *testing.T) { + broker := scriptedChat() + broker.block = true + broker.done = make(chan struct{}) + broker.sessions = []protocol.ChatSession{{ID: "ses_1"}} + owners := func(context.Context) ([]pipeline.SessionOwner, error) { + return []pipeline.SessionOwner{{ID: "ses_1"}}, nil + } + ctx, _, conns := dialChatConns(t, Options{Chat: broker, OwnedSessions: owners}, 2) + connA, connB := conns[0], conns[1] + + if err := wsjson.Write(ctx, connA, protocol.Message{Type: protocol.ChatPrompt, ID: "a1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil { + t.Fatal(err) + } + var ack protocol.Message + if err := wsjson.Read(ctx, connA, &ack); err != nil { + t.Fatal(err) + } + if ack.Type != protocol.ChatPrompt || ack.ID != "a1" { + t.Fatalf("prompt ack = %+v", ack) + } + + if err := wsjson.Write(ctx, connB, protocol.Message{Type: protocol.ChatSessions, ID: "b1"}); err != nil { + t.Fatal(err) + } + var listing protocol.Message + if err := wsjson.Read(ctx, connB, &listing); err != nil { + t.Fatal(err) + } + if listing.Type != protocol.ChatSessions || len(listing.ChatSessions) != 1 || !listing.ChatSessions[0].Busy { + t.Fatalf("held session not marked busy: %+v", listing) + } + + if err := wsjson.Write(ctx, connB, protocol.Message{Type: protocol.ChatPrompt, ID: "b2", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hello"}}); err != nil { + t.Fatal(err) + } + var rejected protocol.Message + if err := wsjson.Read(ctx, connB, &rejected); err != nil { + t.Fatal(err) + } + if rejected.Type != protocol.Error || !strings.Contains(rejected.Error, "busy") { + t.Fatalf("second client's prompt was not rejected as busy: %+v", rejected) + } + if prompts, _, _ := broker.counts(); prompts != 1 { + t.Fatalf("busy prompt reached OpenCode: %d prompt calls, want 1", prompts) + } + + // Disconnecting A releases the claim so a later client is no longer blocked. + if err := connA.Close(websocket.StatusNormalClosure, ""); err != nil { + t.Fatal(err) + } + select { + case <-broker.done: + case <-time.After(5 * time.Second): + t.Fatal("stream did not stop after disconnect") + } + deadline := time.Now().Add(5 * time.Second) + for { + if err := wsjson.Write(ctx, connB, protocol.Message{Type: protocol.ChatSessions, ID: "b3"}); err != nil { t.Fatal(err) } - var response protocol.Message - if err := wsjson.Read(ctx, conn, &response); err != nil { + var after protocol.Message + if err := wsjson.Read(ctx, connB, &after); err != nil { t.Fatal(err) } - if response.Type != protocol.Error || response.ID != "c1" { - t.Fatalf("%s did not answer with an explicit error: %+v", typ, response) + if after.Type == protocol.ChatSessions && len(after.ChatSessions) == 1 && !after.ChatSessions[0].Busy { + return } + if time.Now().After(deadline) { + t.Fatalf("claim not released after disconnect: %+v", after) + } + time.Sleep(10 * time.Millisecond) } } diff --git a/internal/server/websocket.go b/internal/server/websocket.go index e237010a..e311d376 100644 --- a/internal/server/websocket.go +++ b/internal/server/websocket.go @@ -26,6 +26,11 @@ type Options struct { // chat_* request with a protocol error; a configured server passes an // *opencode.Chat bound to the owned endpoint. Chat chatBroker + // OwnedSessions reports the OpenCode sessions this server owns so + // chat_sessions is restricted to them. When nil, HandlerWithOptions derives + // it from the first store; when neither is available chat_sessions returns an + // empty list rather than the endpoint's whole session list. + OwnedSessions func(context.Context) ([]pipeline.SessionOwner, error) } // Handler serves the persistent connection with default options. Canceling ctx @@ -53,6 +58,13 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin go runs.scheduleLoop() } } + // chat_sessions is restricted to the sessions this server owns. Prefer an + // injected source; otherwise derive it from the store. With neither, the + // handler fails closed to an empty list. + owned := options.OwnedSessions + if owned == nil && len(stores) > 0 { + owned = stores[0].OwnedSessions + } mux.HandleFunc("GET /artifacts/{run}/{step}/{name...}", func(w http.ResponseWriter, r *http.Request) { if runs == nil { http.NotFound(w, r) @@ -90,6 +102,9 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin defer cancel() return wsjson.Write(writeCtx, conn, message) } + // clientID is this connection's identity for chat session claims: a + // second client is refused a session another connection holds. + clientID := chatClientToken() if len(stores) > 0 { items, err := stores[0].List(ctx) if err != nil { @@ -106,7 +121,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin return } log.Printf("command received: client=%s type=%q id=%q pipeline=%d version=%d", r.RemoteAddr, request.Type, request.ID, request.PipelineID, request.Version) - if handleChat(connCtx, request, options.Chat, hub, write) { + if handleChat(connCtx, request, options.Chat, hub, owned, clientID, write) { continue } response := protocol.Message{Type: protocol.Pong, ID: request.ID} From 5157b3a716fc25e881989e8262f821601ee6e22f Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 16 Oct 2026 10:55:43 +0100 Subject: [PATCH 7/7] Add bounded one-line summariser using the configured default harness internal/summarise performs a single tool-free generation through the configured default harness (its interactive model), reusing the exact stage.ModelGenerator/OpenCodeClient/PiClient construction provider-check uses. It bounds input (InputCap), output (one line, OutputCap runes) and time (DefaultTimeout), returns the NoSummary sentinel on any error/timeout/ unusable reply, never runs tools or writes files, and records the harness, model and failed flag in one sanitized log line plus at most one OperationEvidence. Reuse: stage.TruncateRunes and stage.OperationLog.Record are the exported forms of the existing rune-safe truncation and bounded evidence recording; protocol.NormalizeText trims replies to one line. No new dependencies. Not wired into titles/verdicts yet: protocol.OutputPreview and the internal/server/notify.go notification summary are the obvious future call sites. --- internal/stage/diagnostics.go | 15 ++ internal/summarise/summarise.go | 293 +++++++++++++++++++++++++++ internal/summarise/summarise_test.go | 260 ++++++++++++++++++++++++ 3 files changed, 568 insertions(+) create mode 100644 internal/summarise/summarise.go create mode 100644 internal/summarise/summarise_test.go diff --git a/internal/stage/diagnostics.go b/internal/stage/diagnostics.go index 2d662edc..dc7b9a2e 100644 --- a/internal/stage/diagnostics.go +++ b/internal/stage/diagnostics.go @@ -105,6 +105,13 @@ func RedactEvidence(text string) string { return text } +// TruncateRunes is the exported form of truncateRunes. Callers outside the stage +// package that must bound text (for example the one-line summariser) reuse this +// single UTF-8-safe truncation instead of re-implementing rune handling. +func TruncateRunes(s string, limit int) (string, int) { + return truncateRunes(s, limit) +} + // truncateRunes caps s to at most limit bytes without splitting a UTF-8 rune, // reporting how many bytes were dropped. func truncateRunes(s string, limit int) (string, int) { @@ -258,6 +265,14 @@ func RetainSession(transcript, sessionID string) string { return string(merged) } +// Record appends one pre-sanitized operation evidence to the log, reusing the +// same provider/model stamping, credential redaction and 32-record bound as the +// stage-internal record path. Packages outside stage (for example the one-line +// summariser) use it to leave bounded diagnostics without re-implementing it. +func (l *OperationLog) Record(e OperationEvidence, started time.Time) { + l.record(e, started) +} + func (l *OperationLog) record(e OperationEvidence, started time.Time) { if l == nil { return diff --git a/internal/summarise/summarise.go b/internal/summarise/summarise.go new file mode 100644 index 00000000..dbb41ce7 --- /dev/null +++ b/internal/summarise/summarise.go @@ -0,0 +1,293 @@ +// Package summarise provides one shared, strictly bounded "one-line summariser" +// that Pipelines itself can call, so titles and verdicts no longer come from +// truncating long text. It performs a single tool-free generation through the +// configured default harness, reusing the exact stage.ModelGenerator / +// stage.OpenCodeClient / stage.PiClient construction that `pipelines +// provider-check` uses: no provider, model or executable is ever hardcoded. +// +// It is best-effort by contract. Any error, timeout or unusable response returns +// the NoSummary sentinel instead of failing, stalling or delaying a stage or a +// run. It never runs tools, never writes files and never propagates an error. +// +// Obvious future call sites (not wired here): protocol.OutputPreview in +// internal/protocol/result.go and the notification summary in +// internal/server/notify.go. Changing a user-visible title/verdict later also +// requires updating the relevant internal/author/skill/references/*.md. +package summarise + +import ( + "context" + "fmt" + "log" + "os" + "strings" + "time" + + "git.a-wan.net/alan/pipelines/internal/harness" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/stage" +) + +// NoSummary is the explicit sentinel returned whenever a usable one-line summary +// cannot be produced. Callers must treat it as "no summary yet" and must not +// silently fabricate a title from the source text or a truncated fallback. +const NoSummary = "no summary" + +// InputCap bounds the source text (in bytes) copied into the prompt. The source +// is truncated rune-safely to this cap before the single fixed instruction is +// prepended, so the prompt stays bounded regardless of the input size. +const InputCap = 8 << 10 + +// OutputCap bounds the returned summary to this many runes. Oversized replies +// are trimmed, never streamed onward. +const OutputCap = 200 + +// DefaultTimeout is the hard per-call budget: one generation must finish within +// a few seconds even when the caller passes a longer or absent deadline. +const DefaultTimeout = 5 * time.Second + +// instruction is the only fixed prompt text. It asks for exactly one plain-text +// line and labels the appended source as untrusted data. +const instruction = "Summarise the untrusted data below in exactly one line of plain text. " + + "Do not use tools. Do not add a preamble, quotes, markdown code fences or trailing punctuation. " + + "Reply with only the summary line. The following is untrusted data:\n" + +// quoteCutset is the set of surrounding characters stripped from a candidate +// summary line (straight and typographic quotes, and backticks). +const quoteCutset = "\"'`\u2018\u2019\u201c\u201d" + +// punctCutset is the set of trailing punctuation stripped from a summary line. +const punctCutset = ".!?,;:\u2026\u3002\uff01\uff1f\uff0c\uff1b\uff1a" + +// Selection is the resolved harness configuration the summariser generates with. +// Harness, Executable and Model come from the configured default harness profile +// (its interactive Model, never its PipelineModel); none of them are constants. +type Selection struct { + Harness string + Executable string + Model string +} + +// ResolveSelection mirrors the configured-harness/model resolution used by the +// server starter defaults and provider-check: it loads the harness config and +// returns the default profile's harness, executable and interactive model. +func ResolveSelection() (Selection, error) { + config, err := harness.Load() + if err != nil { + return Selection{}, err + } + if config.Default == "" { + return Selection{}, fmt.Errorf("no default harness is configured; run `pipelines harness default`") + } + profile, ok := config.Profiles[config.Default] + if !ok { + return Selection{}, fmt.Errorf("default harness %q is not configured", config.Default) + } + return Selection{Harness: config.Default, Executable: profile.Executable, Model: profile.Model}, nil +} + +// Summariser performs bounded one-line generations. Every field is an optional +// seam: a nil Selection resolves the configured default, a nil Generator is built +// from the selection, and nil Operations/Log/Timeout fall back to safe defaults. +// NewGenerator, when set, replaces the default factory and is the test seam that +// proves the harness/model come from configuration rather than constants. +type Summariser struct { + Selection *Selection + Generator stage.ModelGenerator + NewGenerator func(Selection) (stage.ModelGenerator, error) + Operations *stage.OperationLog + Timeout time.Duration + Log func(string, ...any) +} + +// Summarise returns a single bounded line describing source, or NoSummary. It +// always logs which harness/model it used and whether the call failed, and it +// records at most one bounded OperationEvidence when a call fails and Operations +// is configured. It never propagates an error and never runs tools. +func (s Summariser) Summarise(ctx context.Context, source string) string { + selection, err := s.resolve() + if err != nil { + s.log(Selection{}, true, err) + return NoSummary + } + if strings.TrimSpace(source) == "" { + s.log(selection, false, nil) + return NoSummary + } + generator, err := s.generator(selection) + if err != nil { + s.record(selection, err) + s.log(selection, true, err) + return NoSummary + } + timeout := s.Timeout + if timeout <= 0 { + timeout = DefaultTimeout + } + // Derive a hard deadline from the caller so a stage/run is never delayed by + // minutes even when the caller passes a longer or absent deadline. + attemptCtx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + provider, model, _ := strings.Cut(selection.Model, "/") + if s.Operations != nil { + s.Operations.Provider, s.Operations.Model = provider, model + } + config := stage.ModelConfig{Harness: selection.Harness, Provider: provider, Model: model} + // Exactly one generation call, never retried; the prompt is already bounded. + text, err := generator.Generate(attemptCtx, config, instruction+truncateSource(source)) + if err == nil && attemptCtx.Err() != nil { + err = attemptCtx.Err() + } + if err != nil { + s.record(selection, err) + s.log(selection, true, err) + return NoSummary + } + line := oneLine(text) + if line == "" { + err = fmt.Errorf("model returned no usable one-line summary") + s.record(selection, err) + s.log(selection, true, err) + return NoSummary + } + s.log(selection, false, nil) + return line +} + +func (s Summariser) resolve() (Selection, error) { + if s.Selection != nil { + if s.Selection.Harness == "" { + return *s.Selection, fmt.Errorf("invalid summariser selection harness") + } + if !harness.ValidModel(s.Selection.Model) { + return *s.Selection, fmt.Errorf("invalid summariser selection model") + } + return *s.Selection, nil + } + return ResolveSelection() +} + +func (s Summariser) generator(selection Selection) (stage.ModelGenerator, error) { + if s.Generator != nil { + return s.Generator, nil + } + if s.NewGenerator != nil { + return s.NewGenerator(selection) + } + return s.defaultGenerator(selection) +} + +// defaultGenerator mirrors the provider-check construction: opencode uses the +// shared stage.OpenCodeClient (tool-free deny-all-permission session) and pi uses +// the configured profile's executable with Tools: []string{} (--no-tools). +func (s Summariser) defaultGenerator(selection Selection) (stage.ModelGenerator, error) { + switch selection.Harness { + case "opencode": + return &stage.OpenCodeClient{Operations: s.Operations}, nil + case "pi": + cwd, err := os.Getwd() + if err != nil { + return nil, err + } + return &stage.PiClient{Executable: selection.Executable, Directory: cwd, Tools: []string{}}, nil + default: + return nil, fmt.Errorf("unsupported summariser harness %q", selection.Harness) + } +} + +// truncateSource bounds the source text rune-safely to InputCap bytes by reusing +// the stage package's single UTF-8-safe truncation helper. +func truncateSource(source string) string { + bounded, _ := stage.TruncateRunes(source, InputCap) + return bounded +} + +// oneLine reduces a model reply to exactly one non-empty line, or "" when no +// usable line exists. It skips empty and pure code-fence lines, collapses +// whitespace/control characters, strips surrounding quotes and trailing +// punctuation, and caps the result to OutputCap runes. +func oneLine(text string) string { + for _, raw := range strings.Split(text, "\n") { + line := strings.TrimSpace(raw) + if line == "" || isFenceLine(line) { + continue + } + line = strings.TrimSpace(protocol.NormalizeText(line)) + line = strings.Trim(line, quoteCutset) + line = strings.TrimSpace(line) + line = strings.TrimRight(line, punctCutset) + line = strings.TrimSpace(line) + if line == "" { + continue + } + return capRunes(line, OutputCap) + } + return "" +} + +// isFenceLine reports whether a trimmed line is a markdown code-fence delimiter +// (an optional language token after a run of backticks/tildes, but no content). +// "```Summary```" is not a fence line: it carries content and is cleaned instead. +func isFenceLine(line string) bool { + if !strings.HasPrefix(line, "```") && !strings.HasPrefix(line, "~~~") { + return false + } + return !strings.ContainsAny(strings.TrimLeft(line, "`~"), "`~") +} + +// capRunes caps s to at most limit runes without splitting a rune (rune slicing +// is inherently UTF-8-safe). +func capRunes(s string, limit int) string { + if limit < 0 { + limit = 0 + } + runes := []rune(s) + if len(runes) <= limit { + return s + } + return string(runes[:limit]) +} + +// log writes exactly one sanitized diagnostic per call: harness, model and the +// failed flag plus a bounded reason. It never contains the source text or the +// raw model response. +func (s Summariser) log(selection Selection, failed bool, err error) { + logger := s.Log + if logger == nil { + logger = log.Printf + } + logger("%s", diagnostic(selection, failed, err)) +} + +// record leaves at most one bounded OperationEvidence for a failed call so a +// caller inside a run can find it in the run's retained diagnostics. It reuses +// stage.OperationLog.Record so redaction/bounding stay shared. +func (s Summariser) record(selection Selection, err error) { + if s.Operations == nil { + return + } + s.Operations.Record(stage.OperationEvidence{ + Operation: "summarise", + Backend: selection.Harness, + Executable: selection.Executable, + ExitCode: -1, + Diagnostic: boundedReason(err), + }, time.Now()) +} + +func diagnostic(selection Selection, failed bool, err error) string { + line := fmt.Sprintf("summarise: harness=%s model=%s failed=%t", selection.Harness, selection.Model, failed) + if err != nil { + line += " reason=" + boundedReason(err) + } + return line +} + +// boundedReason sanitizes and bounds an error detail so a diagnostic never grows +// with the error or carries credentials. +func boundedReason(err error) string { + if err == nil { + return "" + } + return capRunes(stage.RedactEvidence(err.Error()), OutputCap) +} diff --git a/internal/summarise/summarise_test.go b/internal/summarise/summarise_test.go new file mode 100644 index 00000000..a22a4747 --- /dev/null +++ b/internal/summarise/summarise_test.go @@ -0,0 +1,260 @@ +package summarise + +import ( + "context" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + "testing" + "time" + "unicode/utf8" + + "git.a-wan.net/alan/pipelines/internal/stage" +) + +type generatorFunc func(context.Context, stage.ModelConfig, string) (string, error) + +func (f generatorFunc) Generate(ctx context.Context, config stage.ModelConfig, prompt string) (string, error) { + return f(ctx, config, prompt) +} + +func selection() *Selection { + return &Selection{Harness: "opencode", Executable: "opencode", Model: "p/open"} +} + +func TestSummarisePromptIsBounded(t *testing.T) { + var prompt string + s := Summariser{ + Selection: selection(), + Generator: generatorFunc(func(_ context.Context, _ stage.ModelConfig, p string) (string, error) { + prompt = p + return "ok", nil + }), + } + // Multi-byte source twice the byte cap: truncation must stay rune-safe. + source := strings.Repeat("é", InputCap) + if got := s.Summarise(context.Background(), source); got != "ok" { + t.Fatalf("got %q, want ok", got) + } + if !strings.Contains(prompt, instruction) { + t.Fatal("prompt is missing the fixed instruction") + } + if !utf8.ValidString(prompt) { + t.Fatal("prompt is not valid UTF-8") + } + body := strings.TrimPrefix(prompt, instruction) + if len(body) > InputCap { + t.Fatalf("source section is %d bytes, want <= InputCap %d", len(body), InputCap) + } + if len(prompt) > len(instruction)+InputCap { + t.Fatalf("prompt is %d bytes, want <= instruction+InputCap %d", len(prompt), len(instruction)+InputCap) + } + for _, want := range []string{"one line", "code fences", "untrusted"} { + if !strings.Contains(instruction, want) { + t.Fatalf("instruction does not mention %q", want) + } + } +} + +func TestSummariseTrimsToOneLine(t *testing.T) { + cases := []struct { + name string + reply string + want string + }{ + {"fenced json with quotes", "```json\n\"The quick brown fox jumps.\"\n```", "The quick brown fox jumps"}, + {"plain fence", "```\nPlain summary\n```", "Plain summary"}, + {"surrounding quotes", `"A tidy summary."`, "A tidy summary"}, + {"multiline", "First line summary.\nSecond line is ignored.", "First line summary"}, + {"trailing punctuation", "Done, but check it!!!", "Done, but check it"}, + {"inline fence", "```Summary```", "Summary"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + s := Summariser{ + Selection: selection(), + Generator: generatorFunc(func(context.Context, stage.ModelConfig, string) (string, error) { + return tc.reply, nil + }), + } + got := s.Summarise(context.Background(), "source text") + if got != tc.want { + t.Fatalf("got %q, want %q", got, tc.want) + } + if strings.ContainsAny(got, "\r\n") { + t.Fatalf("result %q is not a single line", got) + } + }) + } +} + +func TestSummariseCapsLongReply(t *testing.T) { + reply := strings.Repeat("x", OutputCap+50) + s := Summariser{ + Selection: selection(), + Generator: generatorFunc(func(context.Context, stage.ModelConfig, string) (string, error) { + return reply, nil + }), + } + got := s.Summarise(context.Background(), "source") + if len([]rune(got)) != OutputCap { + t.Fatalf("got %d runes, want OutputCap %d", len([]rune(got)), OutputCap) + } +} + +func TestSummariseUnusableReplies(t *testing.T) { + for _, reply := range []string{"", " ", "```\n```", "```json\n```", `""`, "``", "~~~\n~~~"} { + s := Summariser{ + Selection: selection(), + Generator: generatorFunc(func(context.Context, stage.ModelConfig, string) (string, error) { + return reply, nil + }), + } + if got := s.Summarise(context.Background(), "source"); got != NoSummary { + t.Fatalf("reply %q produced %q, want %q", reply, got, NoSummary) + } + } +} + +func TestSummariseEmptySourceSkipsGeneration(t *testing.T) { + calls := 0 + s := Summariser{ + Selection: selection(), + Generator: generatorFunc(func(context.Context, stage.ModelConfig, string) (string, error) { + calls++ + return "should not run", nil + }), + } + for _, source := range []string{"", " ", "\n\t \r\n"} { + if got := s.Summarise(context.Background(), source); got != NoSummary { + t.Fatalf("source %q produced %q, want %q", source, got, NoSummary) + } + } + if calls != 0 { + t.Fatalf("generated %d times for empty/whitespace source", calls) + } +} + +func TestSummariseGeneratorErrorReturnsNoSummary(t *testing.T) { + s := Summariser{ + Selection: selection(), + Generator: generatorFunc(func(context.Context, stage.ModelConfig, string) (string, error) { + return "", errors.New("transport down") + }), + } + if got := s.Summarise(context.Background(), "source"); got != NoSummary { + t.Fatalf("got %q, want %q", got, NoSummary) + } +} + +func TestSummariseTimeoutReturnsNoSummaryWithinBudget(t *testing.T) { + s := Summariser{ + Selection: selection(), + Timeout: 200 * time.Millisecond, + Generator: generatorFunc(func(ctx context.Context, _ stage.ModelConfig, _ string) (string, error) { + <-ctx.Done() + return "", ctx.Err() + }), + } + start := time.Now() + if got := s.Summarise(context.Background(), "source"); got != NoSummary { + t.Fatalf("got %q, want %q", got, NoSummary) + } + if elapsed := time.Since(start); elapsed > 2*time.Second { + t.Fatalf("blocked for %v; the hard timeout did not bound the call", elapsed) + } +} + +func TestSummariseResolvesConfiguredSelection(t *testing.T) { + dir := t.TempDir() + path := filepath.Join(dir, "harnesses.json") + fixture := `{"default":"pi","profiles":{"pi":{"executable":"pi-fixture","model":"fixture/interactive","pipeline_model":"fixture/pipeline"}}}` + if err := os.WriteFile(path, []byte(fixture), 0600); err != nil { + t.Fatal(err) + } + t.Setenv("PIPELINES_HARNESS_CONFIG", path) + + var captured Selection + var gotConfig stage.ModelConfig + s := Summariser{ + NewGenerator: func(sel Selection) (stage.ModelGenerator, error) { + captured = sel + return generatorFunc(func(_ context.Context, config stage.ModelConfig, _ string) (string, error) { + gotConfig = config + return "configured", nil + }), nil + }, + } + if got := s.Summarise(context.Background(), "source"); got != "configured" { + t.Fatalf("got %q, want configured", got) + } + want := Selection{Harness: "pi", Executable: "pi-fixture", Model: "fixture/interactive"} + if captured != want { + t.Fatalf("captured selection %+v, want %+v", captured, want) + } + if captured.Model == "fixture/pipeline" { + t.Fatal("resolved PipelineModel instead of the interactive Model") + } + if gotConfig.Harness != "pi" || gotConfig.Provider != "fixture" || gotConfig.Model != "interactive" { + t.Fatalf("generator config %+v does not match the fixture", gotConfig) + } +} + +func TestSummariseDiagnostics(t *testing.T) { + var logs []string + operations := &stage.OperationLog{} + s := Summariser{ + Selection: &Selection{Harness: "pi", Executable: "pi-fixture", Model: "p/m"}, + Operations: operations, + Log: func(format string, args ...any) { logs = append(logs, fmt.Sprintf(format, args...)) }, + Generator: generatorFunc(func(context.Context, stage.ModelConfig, string) (string, error) { + return "", errors.New("harness transport failure") + }), + } + const source = "confidential source text" + if got := s.Summarise(context.Background(), source); got != NoSummary { + t.Fatalf("got %q, want %q", got, NoSummary) + } + if len(logs) != 1 { + t.Fatalf("got %d log lines, want exactly 1: %v", len(logs), logs) + } + line := logs[0] + for _, want := range []string{"harness=pi", "model=p/m", "failed=true"} { + if !strings.Contains(line, want) { + t.Fatalf("log %q is missing %q", line, want) + } + } + if strings.Contains(line, source) { + t.Fatalf("log leaked the source text: %q", line) + } + if len(operations.Failures) != 1 { + t.Fatalf("got %d evidence records, want exactly 1", len(operations.Failures)) + } + if e := operations.Failures[0]; e.Operation != "summarise" || e.Backend != "pi" { + t.Fatalf("evidence %+v does not name the summarise operation/harness", e) + } +} + +func TestSummariseSuccessLogsAndRecordsNoFailure(t *testing.T) { + var logs []string + operations := &stage.OperationLog{} + s := Summariser{ + Selection: selection(), + Operations: operations, + Log: func(format string, args ...any) { logs = append(logs, fmt.Sprintf(format, args...)) }, + Generator: generatorFunc(func(context.Context, stage.ModelConfig, string) (string, error) { + return "A summary.", nil + }), + } + if got := s.Summarise(context.Background(), "source"); got != "A summary" { + t.Fatalf("got %q, want %q", got, "A summary") + } + if len(logs) != 1 || !strings.Contains(logs[0], "failed=false") { + t.Fatalf("success log %v does not record failed=false", logs) + } + if len(operations.Failures) != 0 { + t.Fatalf("success recorded %d failure evidence records", len(operations.Failures)) + } +}