Files
pipelines/internal/opencode/chat_v2_test.go
T
alan e9d902213e Preserve background shell visibility across readback races
Reconcile authoritative shell snapshots with bounded lifecycle journals so concurrent exits and creates cannot be overwritten, coalesce observation setup, and retain verified ownership across reconnects. Add deterministic reconciliation and cancellation regressions plus stable limited-visibility handling.\n\nChecked packaged templates and authoring skill references: none need updating. The task-owned docs note records the stale shared chat documentation for docs-fold; shared documentation was not edited.
2026-10-18 12:54:36 +01:00

440 lines
20 KiB
Go

package opencode
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"time"
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// TestConnectionServerUser pins the owned serve basic-auth user per backend: the
// native serve authenticates "pipelines", the V2 API server authenticates
// "opencode" (verified against the installed 2.0.20 binary, which ignores
// OPENCODE_SERVER_USERNAME). A wrong user is an unauthenticated endpoint, so it
// is asserted rather than assumed.
func TestConnectionServerUser(t *testing.T) {
if got := (&Connection{Backend: "v1"}).serverUser(); got != "pipelines" {
t.Fatalf("v1 server user = %q, want pipelines", got)
}
if got := (&Connection{Backend: "v2"}).serverUser(); got != "opencode" {
t.Fatalf("v2 server user = %q, want opencode", got)
}
}
// TestChatV2PromptShapeAndAuth pins the outbound V2 prompt: a single "text"
// field (the installed service's PromptInput schema requires "text" and rejects
// a bare "parts" array) sent with the "opencode" basic-auth user. The same body
// also drives the v1 translation, so no second request format exists.
func TestChatV2PromptShapeAndAuth(t *testing.T) {
type request struct {
method, path, user, pass, body string
}
var got request
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data := make([]byte, r.ContentLength)
_, _ = r.Body.Read(data)
user, pass, _ := r.BasicAuth()
got = request{method: r.Method, path: r.URL.Path, user: user, pass: pass, body: string(data)}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":{"id":"msg_1","sessionID":"ses_1","type":"user"}}`))
}))
defer srv.Close()
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL, password: "deadbeef"})
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 got.method != http.MethodPost || got.path != "/api/session/ses_1/prompt" {
t.Fatalf("prompt request = %+v", got)
}
if got.body != `{"text":"hello"}` {
t.Fatalf("prompt body = %q, want the V2 text shape", got.body)
}
if got.user != "opencode" || got.pass != "deadbeef" {
t.Fatalf("prompt auth = %q/%q, want opencode/deadbeef", got.user, got.pass)
}
}
// TestChatStreamRealV2Shapes replays the exact event frames the installed 2.0.20
// service emitted on /api/event during a live turn (captured, not documented):
// a step start, token text delta, usage and a step end whose data carries no
// step index. It asserts the existing firehose mapper surfaces them with stable
// session/message ids and that the usage frame inherits the retained message id.
func TestChatStreamRealV2Shapes(t *testing.T) {
frames := []string{
`{"id":"evt_0","created":0,"type":"session.instructions.updated","data":{"sessionID":"ses_1","delta":{"core/instructions":"abc","core/date":"def"}}}`,
`{"id":"evt_1","created":1,"type":"session.step.started","data":{"sessionID":"ses_1","agent":"build","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go","variant":"default"},"assistantMessageID":"msg_1","started":1}}`,
`{"id":"evt_2","created":2,"type":"session.text.started","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0}}`,
`{"id":"evt_3","created":3,"type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"Where granite meets"}}`,
`{"id":"evt_4","created":4,"type":"session.usage.updated","data":{"sessionID":"ses_1","cost":0.0005,"tokens":{"input":94,"output":304}}}`,
`{"id":"evt_5","created":5,"type":"session.step.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","finish":"stop","tokens":{"input":94,"output":304},"cost":0.0005}}`,
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || r.URL.Path != "/api/event" {
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
return
}
w.Header().Set("Content-Type", "text/event-stream")
for _, frame := range frames {
_, _ = w.Write([]byte("data: " + frame + "\n\n"))
}
}))
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
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)
}
// text.started carries no delta and is skipped; the four remaining frames
// (step start, text delta, usage, step end) must all surface.
if len(got) != 4 {
t.Fatalf("frames = %d, want 4: %+v", len(got), got)
}
for _, frame := range got {
if frame.SessionID != "ses_1" || frame.MessageID != "msg_1" {
t.Fatalf("frame missing stable ids: %+v", frame)
}
}
if got[0].Kind != protocol.ChatEventStep || got[0].Step == nil || got[0].Step.Status != protocol.ChatStepStarted {
t.Fatalf("step start frame = %+v", got[0])
}
if got[1].Kind != protocol.ChatEventText || got[1].Delta != "Where granite meets" {
t.Fatalf("text frame = %+v", got[1])
}
if got[2].Kind != protocol.ChatEventUsage || got[2].Usage == nil || got[2].Usage.InputTokens != 94 || got[2].Usage.OutputTokens != 304 {
t.Fatalf("usage frame = %+v", got[2])
}
if got[3].Kind != protocol.ChatEventStep || got[3].Step == nil || got[3].Step.Status != protocol.ChatStepEnded {
t.Fatalf("step end frame = %+v", got[3])
}
}
// TestChatDeltaPartIDsNeverCollide pins the broker's part-id namespace: OpenCode
// numbers one message's text and reasoning parts independently from 0, so a
// message's first text delta and its first reasoning delta share an ordinal. The
// two must still land on distinct part ids — otherwise the client appends the
// answer into the thinking block and the server's retained state re-collides on a
// resume. The id is the shared ChatDeltaPartID derivation, so it is deterministic
// across a reconnect.
func TestChatDeltaPartIDsNeverCollide(t *testing.T) {
payloads := map[string]string{
"text": `{"type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"answer"}}`,
"reasoning": `{"type":"session.reasoning.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"think"}}`,
}
ids := map[string]string{}
for kind, payload := range payloads {
frame, ok, err := NormalizeChatEvent([]byte(payload), "ses_1", &ChatStreamState{})
if err != nil || !ok {
t.Fatalf("%s delta: ok=%v err=%v", kind, ok, err)
}
if frame.Part == nil {
t.Fatalf("%s delta carried no part", kind)
}
if frame.Part.Type != kind {
t.Fatalf("%s delta part type = %q", kind, frame.Part.Type)
}
ids[kind] = frame.Part.ID
}
if ids["text"] == ids["reasoning"] {
t.Fatalf("text and reasoning share part id %q", ids["text"])
}
if ids["text"] != protocol.ChatDeltaPartID("msg_1", protocol.ChatPartText, 0) {
t.Fatalf("text part id = %q, want the stable derivation", ids["text"])
}
if ids["reasoning"] != protocol.ChatDeltaPartID("msg_1", protocol.ChatPartReasoning, 0) {
t.Fatalf("reasoning part id = %q, want the stable derivation", ids["reasoning"])
}
}
// TestChatToolLifecycleCarriesNameAndInput pins what the broker learns from each
// verified tool frame: input.started carries the name, input.ended carries the
// arguments as its `text` string, called carries the parsed input object, and
// progress/success carry neither — so those frames must surface no name and no
// input, leaving the retention to the consumer instead of blanking the field.
func TestChatToolLifecycleCarriesNameAndInput(t *testing.T) {
frames := []struct {
name string
payload string
wantStatus string
wantName string
wantInput string // the JSON encoding expected, "" for absent
}{
{
name: "input.started names the call",
payload: `{"type":"session.tool.input.started","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","name":"shell"}}`,
wantStatus: protocol.ChatToolStreaming,
wantName: "shell",
},
{
name: "input.ended carries the arguments as text",
payload: `{"type":"session.tool.input.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","text":"{\"command\":\"ls\"}"}}`,
wantStatus: protocol.ChatToolStreaming,
wantInput: `{"command":"ls"}`,
},
{
name: "called carries the parsed input object",
payload: `{"type":"session.tool.called","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","input":{"command":"ls"}}}`,
wantStatus: protocol.ChatToolRunning,
wantInput: `{"command":"ls"}`,
},
{
name: "progress carries metadata only",
payload: `{"type":"session.tool.progress","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","metadata":{"exit":0}}}`,
wantStatus: protocol.ChatToolRunning,
},
{
name: "success carries the output and neither name nor input",
payload: `{"type":"session.tool.success","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","content":[{"type":"text","text":"ok"}],"metadata":{"exit":0},"executed":true}}`,
wantStatus: protocol.ChatToolCompleted,
},
}
for _, tc := range frames {
t.Run(tc.name, func(t *testing.T) {
frame, ok, err := NormalizeChatEvent([]byte(tc.payload), "ses_1", &ChatStreamState{})
if err != nil || !ok {
t.Fatalf("ok=%v err=%v", ok, err)
}
if frame.Part == nil || frame.Part.State == nil {
t.Fatalf("tool frame = %+v", frame)
}
if frame.Part.State.Status != tc.wantStatus {
t.Fatalf("status = %q, want %q", frame.Part.State.Status, tc.wantStatus)
}
if frame.Part.Name != tc.wantName {
t.Fatalf("name = %q, want %q", frame.Part.Name, tc.wantName)
}
if tc.wantInput == "" {
if len(frame.Part.State.Input) != 0 {
t.Fatalf("input = %s, want absent", frame.Part.State.Input)
}
return
}
if !json.Valid(frame.Part.State.Input) {
t.Fatalf("input %s is not valid JSON", frame.Part.State.Input)
}
var want, got any
if err := json.Unmarshal([]byte(tc.wantInput), &want); err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(frame.Part.State.Input, &got); err != nil {
t.Fatalf("input %s: %v", frame.Part.State.Input, err)
}
if fmt.Sprintf("%v", want) != fmt.Sprintf("%v", got) {
t.Fatalf("input = %s, want %s", frame.Part.State.Input, tc.wantInput)
}
})
}
}
// TestChatToolInputFromPartialText pins the streaming case: input.ended's text
// that is not yet a complete JSON value must still surface, carried as a JSON
// string so the frame stays valid and the client can show the partial arguments.
func TestChatToolInputFromPartialText(t *testing.T) {
frame, ok, err := NormalizeChatEvent([]byte(`{"type":"session.tool.input.ended","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","id":"call_1","text":"{\"command\":\"l"}}`), "ses_1", &ChatStreamState{})
if err != nil || !ok {
t.Fatalf("ok=%v err=%v", ok, err)
}
if frame.Part == nil || frame.Part.State == nil {
t.Fatalf("frame = %+v", frame)
}
if !json.Valid(frame.Part.State.Input) {
t.Fatalf("partial input %s is not valid JSON", frame.Part.State.Input)
}
var got string
if err := json.Unmarshal(frame.Part.State.Input, &got); err != nil {
t.Fatalf("partial input is not a JSON string: %s (%v)", frame.Part.State.Input, err)
}
if got != `{"command":"l` {
t.Fatalf("partial input text = %q", got)
}
}
// TestChatShellEventMapping pins the shell mapping: a shell.created belonging to
// the session surfaces a ChatEventShell frame carrying the shell's id, status and
// command, and the matching shell.exited carries its id, status and exit code.
// The frames carry no message id (a shell outlives the tool call that started
// it), and the exit is a pointer so an exit of 0 is not mistaken for "absent".
func TestChatShellEventMapping(t *testing.T) {
state := &ChatStreamState{}
created, ok, err := NormalizeChatEvent([]byte(`{"type":"shell.created","data":{"info":{"id":"sh_1","status":"running","command":"npm run dev","cwd":"/work","metadata":{"sessionID":"ses_1"}}}}`), "ses_1", state)
if err != nil || !ok {
t.Fatalf("shell.created: ok=%v err=%v", ok, err)
}
if created.Kind != protocol.ChatEventShell || created.Shell == nil {
t.Fatalf("created frame = %+v", created)
}
if created.MessageID != "" {
t.Fatalf("shell frame carried a message id: %q", created.MessageID)
}
if created.Shell.ID != "sh_1" || created.Shell.Status != protocol.ChatShellRunning || created.Shell.Command != "npm run dev" {
t.Fatalf("created shell = %+v", created.Shell)
}
exited, ok, err := NormalizeChatEvent([]byte(`{"type":"shell.exited","data":{"id":"sh_1","exit":0,"status":"exited"}}`), "ses_1", state)
if err != nil || !ok {
t.Fatalf("shell.exited: ok=%v err=%v", ok, err)
}
if exited.Shell == nil || exited.Shell.ID != "sh_1" || exited.Shell.Status != protocol.ChatShellExited {
t.Fatalf("exited shell = %+v", exited.Shell)
}
if exited.Shell.Exit == nil || *exited.Shell.Exit != 0 {
t.Fatalf("exit code = %v, want a present 0", exited.Shell.Exit)
}
// A second exit for the same shell is not this session's any more: the id was
// forgotten once its exit surfaced.
if _, ok, err := NormalizeChatEvent([]byte(`{"type":"shell.exited","data":{"id":"sh_1","exit":0,"status":"exited"}}`), "ses_1", state); err != nil || ok {
t.Fatalf("repeated exit produced a frame: ok=%v err=%v", ok, err)
}
}
// TestChatShellEventForeignSessionIgnored pins the session scoping the shell
// events need: they are location-scoped, so the session that started a shell is
// named only in the created event's metadata. A shell started by another session
// is ignored, and its exit — which names no session at all — is ignored too,
// because the id was never remembered for this session.
func TestChatShellEventForeignSessionIgnored(t *testing.T) {
state := &ChatStreamState{}
foreign := `{"type":"shell.created","data":{"info":{"id":"sh_foreign","status":"running","command":"sleep 120","metadata":{"sessionID":"ses_other"}}}}`
if frame, ok, err := NormalizeChatEvent([]byte(foreign), "ses_1", state); err != nil || ok {
t.Fatalf("another session's shell produced a frame: %+v ok=%v err=%v", frame, ok, err)
}
if _, ok, err := NormalizeChatEvent([]byte(`{"type":"shell.exited","data":{"id":"sh_foreign","exit":0,"status":"exited"}}`), "ses_1", state); err != nil || ok {
t.Fatalf("another session's shell exit produced a frame: ok=%v err=%v", ok, err)
}
// An unattributed shell (no metadata.sessionID) is not this session's either.
unattributed := `{"type":"shell.created","data":{"info":{"id":"sh_anon","status":"running","command":"ls","metadata":{}}}}`
if _, ok, err := NormalizeChatEvent([]byte(unattributed), "ses_1", state); err != nil || ok {
t.Fatalf("unattributed shell produced a frame: ok=%v err=%v", ok, err)
}
}
// TestChatMalformedShellEventFailsStream pins that a shell event which cannot be
// keyed (no shell id) fails the stream instead of producing a frame the client
// cannot attribute.
func TestChatMalformedShellEventFailsStream(t *testing.T) {
cases := []string{
`{"type":"shell.created","data":{"info":{"status":"running","metadata":{"sessionID":"ses_1"}}}}`,
`{"type":"shell.exited","data":{"exit":0,"status":"exited"}}`,
}
for _, payload := range cases {
if _, ok, err := NormalizeChatEvent([]byte(payload), "ses_1", &ChatStreamState{}); err == nil || ok {
t.Fatalf("malformed shell event %s: ok=%v err=%v", payload, ok, err)
}
}
}
// TestChatStreamSurfacesShellLifecycle drives the whole mapping through the SSE
// read loop: a shell started for the session surfaces as a shell frame, a shell
// started by another session is dropped, and the matching exit is surfaced only
// for the session's own shell. It pins that the shell context the exit needs is
// carried across frames by the stream's retained state, not by one payload.
func TestChatStreamSurfacesShellLifecycle(t *testing.T) {
frames := []string{
`{"id":"evt_1","type":"shell.created","data":{"info":{"id":"sh_1","status":"running","command":"npm run dev","metadata":{"sessionID":"ses_1"}}}}`,
`{"id":"evt_2","type":"shell.created","data":{"info":{"id":"sh_other","status":"running","command":"sleep 120","metadata":{"sessionID":"ses_other"}}}}`,
`{"id":"evt_3","type":"session.text.delta","data":{"sessionID":"ses_1","assistantMessageID":"msg_1","ordinal":0,"delta":"hi"}}`,
`{"id":"evt_4","type":"shell.exited","data":{"id":"sh_other","exit":0,"status":"exited"}}`,
`{"id":"evt_5","type":"shell.exited","data":{"id":"sh_1","exit":0,"status":"exited"}}`,
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
for _, frame := range frames {
_, _ = w.Write([]byte("data: " + frame + "\n\n"))
}
}))
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
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)
}
var shellIDs []string
for _, frame := range got {
if frame.Kind == protocol.ChatEventShell && frame.Shell != nil {
shellIDs = append(shellIDs, frame.Shell.ID+":"+frame.Shell.Status)
}
}
if len(shellIDs) != 2 || shellIDs[0] != "sh_1:running" || shellIDs[1] != "sh_1:exited" {
t.Fatalf("shell frames = %v, want only this session's created and exited", shellIDs)
}
}
// TestOpenStreamRejectsUnknownBackend pins that the shared construction path
// validates the backend before any process is started, so OpenStream never
// launches a serve for an unknown backend.
func TestOpenStreamRejectsUnknownBackend(t *testing.T) {
if _, err := OpenStream(context.Background(), "opencode", t.TempDir(), "v3"); err == nil {
t.Fatal("an unknown backend must be rejected")
}
}
// TestOpenChatRejectsUnrunnableCLI pins that OpenChat surfaces detection failure
// (an executable that cannot run) instead of opening a half-configured endpoint.
func TestOpenChatRejectsUnrunnableCLI(t *testing.T) {
missing := filepath.Join(t.TempDir(), "no-such-opencode")
if _, err := OpenChat(context.Background(), missing, ""); err == nil {
t.Fatal("a missing CLI must fail detection, not open an endpoint")
}
}
// TestChatShellReadbackSeedsReplacementStream proves an authoritative shell
// readback survives a later stream replacement: shell.exited has no session id,
// so it must still clear the verified shell after reconnect.
func TestChatShellReadbackSeedsReplacementStream(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/api/shell":
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":[{"id":"sh_1","status":"running","command":"sleep 30","metadata":{"sessionID":"ses_1"}},{"id":"sh_other","status":"running","command":"sleep 30","metadata":{"sessionID":"ses_other"}}]}`))
case "/api/event":
w.Header().Set("Content-Type", "text/event-stream")
_, _ = w.Write([]byte("data: {\"type\":\"shell.exited\",\"data\":{\"id\":\"sh_1\",\"status\":\"exited\",\"exit\":0}}\n\n"))
}
}))
defer srv.Close()
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
shells, err := chat.Shells(context.Background(), "ses_1")
if err != nil || len(shells) != 1 || shells[0].ID != "sh_1" {
t.Fatalf("Shells = %+v, %v", shells, err)
}
var got []protocol.ChatEventFrame
if err := chat.Stream(context.Background(), "ses_1", nil, func(frame protocol.ChatEventFrame) error { got = append(got, frame); return nil }); err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].Shell == nil || got[0].Shell.ID != "sh_1" || got[0].Shell.Status != protocol.ChatShellExited {
t.Fatalf("exit after readback = %+v", got)
}
}
func TestChatReleaseShellsBoundsDisconnectedState(t *testing.T) {
chat := NewChat(&Connection{})
state := chat.streamState("ses_1")
state.markShell("sh_1")
chat.ReleaseShells("ses_1")
chat.shellMu.Lock()
_, retained := chat.shells["ses_1"]
chat.shellMu.Unlock()
if retained {
t.Fatal("released session shell state was retained")
}
}