Files
pipelines/internal/opencode/chat_test.go
T
alan 915016df0b 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.
2026-10-16 05:24:00 +01:00

410 lines
18 KiB
Go

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())
}
}
// 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) {
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) != 2 {
t.Fatalf("got %d messages, want the system turn skipped: %+v", len(messages), messages)
}
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")
}
}
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)
}
}