18398dfa5b
chat_sessions used to list exactly the OpenCode sessions the pipelines own,
filtered through pipeline.SessionOwner (run_steps/run_session_owners), so an
operator-created chat was an accident of the run-ownership table and the
operator's working sessions were mixed with the internals of running pipelines.
Add a first-class operator session:
- internal/pipeline/operator_sessions.go: a dedicated operator_sessions table
(session_id PK, directory, title, tag, created_at, last_used_at) with
RecordOperatorSession (upsert preserving created_at), a one-statement
TouchOperatorSession (no-op for a non-operator id), OperatorSessions bounded to
128 rows most-recently-used first, and OperatorDirectories (bounded MRU,
deduped). run_steps/run_session_owners and SessionOwner/OwnedSessions are
untouched.
- internal/server/chat.go: chat_sessions returns operator sessions only - it
reads the registry once, intersects it with one broker session-list call, fills
directory/tag/created/updated from the registry, sets busy for a session held
by another connection, keeps MRU order, and stays fail-closed (empty list, never
the endpoint's whole list). A pipeline stage session and an untagged row can
never be listed. chat_create resolves the directory (empty/'~'/'~/...' ->
operator home; relative or missing rejected before any OpenCode call), calls
broker.Create(title, resolved) and records the row with the operator tag.
chat_directories returns home first then bounded (8) deduped recents. A
successful prompt touches the operator session without affecting pipeline
prompts.
- internal/opencode, internal/protocol, internal/server/websocket.go and
message.go: unscoped session list, Create posts {"title":...,
"location":{"directory":...}} to POST /api/session, additive protocol fields
(omitempty) and the ChatDirectories request/response, plus the OperatorRegistry
option defaulting to the first store. One OpenCode transport per server is
unchanged.
Docs and references: docs/cli.md gains a "Chat sessions" section documenting the
operator-only fail-closed list, directory defaulting/rejection, chat_directories
(home first, deduped, bounded 8) and why a dedicated request was chosen.
internal/author/skill/references/operations.md's stale "Brokered OpenCode chat"
text (chat_create unbrokered, run_steps filtering, "listed the owned scratch
session" evidence) is rewritten to the operator-session registry, the fail-closed
filter, directory defaulting and the corrected live-test wording. No new
reference file, so internal/author/install.go's inventory is unchanged.
Checked for staleness elsewhere: README.md, the docs/ topic pages and the
packaged server/TUI templates carry no chat-session ownership claims, so none
needed updating.
Verified: gofmt -l clean; go vet and go build ./... clean; go test -count=1
./internal/pipeline/... ./internal/protocol/... ./internal/opencode/...
./internal/server/... and the full go test -count=1 ./... pass.
290 lines
11 KiB
Go
290 lines
11 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"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/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// TestChatLiveEndToEnd drives the real brokered chat path against the installed
|
|
// OpenCode: it opens the server's single owned endpoint, creates an operator
|
|
// session through chat_create in a chosen directory, lists it through
|
|
// chat_sessions, prompts it, and reads its history through chat_messages — all on
|
|
// a scratch store and an in-process handler. Its key proof is that a genuine
|
|
// pipeline stage session (created as a raw OpenCode session and recorded in
|
|
// run_steps/run_session_owners exactly as a run does) never appears in
|
|
// chat_sessions. It is opt-in (real inference) and never touches the deployed
|
|
// server, ntfy or any lane; it runs on a t.TempDir workspace.
|
|
func TestChatLiveEndToEnd(t *testing.T) {
|
|
if os.Getenv("PIPELINES_TEST_OPENCODE_CHAT_LIVE") != "1" {
|
|
t.Skip("set PIPELINES_TEST_OPENCODE_CHAT_LIVE=1 to run the live OpenCode chat end-to-end check")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
|
defer cancel()
|
|
dir := t.TempDir()
|
|
conn, err := opencode.OpenChat(ctx, opencode.Executable(), dir)
|
|
if err != nil {
|
|
t.Fatalf("OpenChat: %v", err)
|
|
}
|
|
defer conn.Close()
|
|
t.Logf("owned OpenCode endpoint %s (backend %s)", conn.URL, conn.Backend)
|
|
|
|
store, err := pipeline.Open(filepath.Join(t.TempDir(), "chat-live.db"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer store.Close()
|
|
|
|
srv := httptest.NewServer(HandlerWithOptions(ctx, Options{Chat: opencode.NewChat(conn), OperatorRegistry: store}))
|
|
defer srv.Close()
|
|
ws, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil)
|
|
if err != nil {
|
|
t.Fatalf("dial: %v", err)
|
|
}
|
|
defer ws.CloseNow()
|
|
|
|
// chat_directories returns the operator's home first.
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatDirectories, ID: "d1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, resp := readChatResponse(t, ctx, ws)
|
|
if resp.Type != protocol.ChatDirectories || len(resp.ChatDirectories) == 0 || resp.ChatDirectories[0] != home {
|
|
t.Fatalf("chat_directories = %+v, want home %q first", resp, home)
|
|
}
|
|
t.Logf("chat_directories = %+v", resp.ChatDirectories)
|
|
|
|
// chat_create records the operator session in the chosen directory with the
|
|
// operator tag.
|
|
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatCreate, ID: "c1", ChatCreate: &protocol.ChatCreateRequest{Title: "pipelines chat live check", Directory: dir}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, resp = readChatResponse(t, ctx, ws)
|
|
if resp.Type != protocol.ChatCreate || resp.ChatSession == nil {
|
|
t.Fatalf("chat_create response = %+v", resp)
|
|
}
|
|
sid := resp.ChatSession.ID
|
|
if sid == "" || resp.ChatSession.Directory != dir || resp.ChatSession.Tag != protocol.ChatSessionTagOperator {
|
|
t.Fatalf("created operator session = %+v, want directory %q and tag operator", resp.ChatSession, dir)
|
|
}
|
|
defer conn.Call(context.Background(), "delete", "/api/session/"+sid, nil)
|
|
t.Logf("chat_create created operator session %s in %s (tag %s)", sid, resp.ChatSession.Directory, resp.ChatSession.Tag)
|
|
|
|
// chat_sessions lists exactly the created operator session, with its registry
|
|
// directory and tag.
|
|
sessions := listLiveSessions(t, ctx, ws)
|
|
if len(sessions) != 1 || sessions[0].ID != sid || sessions[0].Tag != protocol.ChatSessionTagOperator || sessions[0].Directory != dir {
|
|
t.Fatalf("chat_sessions = %+v, want the created operator session %s", sessions, sid)
|
|
}
|
|
t.Logf("chat_sessions listed %d operator session(s): %+v", len(sessions), sessions)
|
|
|
|
// A genuine pipeline stage session is created as a raw OpenCode session and
|
|
// recorded in run_steps/run_session_owners exactly as a run does; chat_sessions
|
|
// must never list it.
|
|
stageID := createRawLiveSession(t, ctx, conn, dir)
|
|
defer conn.Call(context.Background(), "delete", "/api/session/"+stageID, nil)
|
|
recordLiveStageSession(t, ctx, store, stageID, dir)
|
|
sessions = listLiveSessions(t, ctx, ws)
|
|
for _, session := range sessions {
|
|
if session.ID == stageID {
|
|
t.Fatalf("pipeline stage session %s was listed: %+v", stageID, sessions)
|
|
}
|
|
}
|
|
if len(sessions) != 1 || sessions[0].ID != sid {
|
|
t.Fatalf("chat_sessions = %+v, want only the operator session %s", sessions, sid)
|
|
}
|
|
t.Logf("chat_sessions still listed only the operator session after recording stage session %s", stageID)
|
|
|
|
// chat_messages resumes retained/readback history without error.
|
|
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatMessages, ID: "m1", ChatQuery: &protocol.ChatQuery{SessionID: sid}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, resp = readChatResponse(t, ctx, ws)
|
|
if resp.Type != protocol.ChatMessages {
|
|
t.Fatalf("chat_messages response = %+v", resp)
|
|
}
|
|
t.Logf("chat_messages returned %d turn(s)", len(resp.ChatMessages))
|
|
|
|
// A completed turn: the ack echoes the request id and the reply arrives as
|
|
// chat_event frames with stable session/message ids.
|
|
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatPrompt, ID: "p1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: sid, Text: "Reply with exactly one word: pong"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
frames, ack := readChatResponse(t, ctx, ws)
|
|
if ack.Type != protocol.ChatPrompt || ack.ID != "p1" {
|
|
t.Fatalf("prompt ack = %+v, want the echoed request id p1", ack)
|
|
}
|
|
completed := collectUntilStepEnded(t, ctx, ws, frames)
|
|
assertLiveFrames(t, sid, completed)
|
|
t.Logf("completed turn: %d chat_event frame(s) [%s]; first text delta %q", len(completed), frameKinds(completed), firstDelta(completed))
|
|
}
|
|
|
|
// listLiveSessions issues one chat_sessions request and returns the listed
|
|
// sessions.
|
|
func listLiveSessions(t *testing.T, ctx context.Context, ws *websocket.Conn) []protocol.ChatSession {
|
|
t.Helper()
|
|
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatSessions, ID: "s1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, resp := readChatResponse(t, ctx, ws)
|
|
if resp.Type != protocol.ChatSessions {
|
|
t.Fatalf("chat_sessions response = %+v", resp)
|
|
}
|
|
return resp.ChatSessions
|
|
}
|
|
|
|
// createRawLiveSession creates a bare OpenCode session in dir through the shared
|
|
// connection, mirroring how a pipeline run creates the session a stage drives.
|
|
func createRawLiveSession(t *testing.T, ctx context.Context, conn *opencode.Connection, dir string) string {
|
|
t.Helper()
|
|
created, err := conn.Call(ctx, "post", "/api/session", map[string]any{
|
|
"title": "pipelines stage session live check",
|
|
"location": map[string]string{"directory": dir},
|
|
"permissions": []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create raw session: %v", err)
|
|
}
|
|
var session struct {
|
|
Data struct {
|
|
ID string `json:"id"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(created, &session); err != nil || session.Data.ID == "" {
|
|
t.Fatalf("raw session response %s (err %v)", created, err)
|
|
}
|
|
return session.Data.ID
|
|
}
|
|
|
|
// recordLiveStageSession records sessionID as a genuine pipeline stage session
|
|
// through the same store calls a run uses (CreateRun/BeginStep/RunSession), so it
|
|
// is present in run_steps and run_session_owners and proven to be excluded from
|
|
// chat_sessions by the operator filter, not by being unrecorded.
|
|
func recordLiveStageSession(t *testing.T, ctx context.Context, store *pipeline.Store, sessionID, dir string) {
|
|
t.Helper()
|
|
if _, err := store.Submit(ctx, "chat-live-stage", pipeline.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)
|
|
}
|
|
run, _, err := store.CreateRun(ctx, definition, "chat-live-stage-run", dir, false, nil, map[string]any{"prompt": "task"})
|
|
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, sessionID, true, "v2", dir); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// readChatResponse reads until a non-event response message arrives, returning
|
|
// that message and any interleaved chat_event frames.
|
|
func readChatResponse(t *testing.T, ctx context.Context, ws *websocket.Conn) ([]protocol.ChatEventFrame, protocol.Message) {
|
|
t.Helper()
|
|
var frames []protocol.ChatEventFrame
|
|
for {
|
|
message := readChatMessage(t, ctx, ws)
|
|
if message.Type == protocol.ChatEvent && message.ChatEvent != nil {
|
|
frames = append(frames, *message.ChatEvent)
|
|
continue
|
|
}
|
|
return frames, message
|
|
}
|
|
}
|
|
|
|
// readChatMessage reads one frame and fails on a transport error or a server
|
|
// error frame, so the test never silently accepts a failure.
|
|
func readChatMessage(t *testing.T, ctx context.Context, ws *websocket.Conn) protocol.Message {
|
|
t.Helper()
|
|
var message protocol.Message
|
|
if err := wsjson.Read(ctx, ws, &message); err != nil {
|
|
t.Fatalf("read: %v", err)
|
|
}
|
|
if message.Type == protocol.Error {
|
|
t.Fatalf("server error frame: %s", message.Error)
|
|
}
|
|
return message
|
|
}
|
|
|
|
// collectUntilStepEnded keeps reading event frames until the turn's step ends.
|
|
func collectUntilStepEnded(t *testing.T, ctx context.Context, ws *websocket.Conn, frames []protocol.ChatEventFrame) []protocol.ChatEventFrame {
|
|
t.Helper()
|
|
for {
|
|
message := readChatMessage(t, ctx, ws)
|
|
if message.Type != protocol.ChatEvent || message.ChatEvent == nil {
|
|
continue
|
|
}
|
|
frames = append(frames, *message.ChatEvent)
|
|
if last := frames[len(frames)-1]; last.Kind == protocol.ChatEventStep && last.Step != nil && last.Step.Status == protocol.ChatStepEnded {
|
|
return frames
|
|
}
|
|
}
|
|
}
|
|
|
|
// assertLiveFrames fails unless every frame carries the session id and a stable
|
|
// message id, and the turn produced at least one text frame.
|
|
func assertLiveFrames(t *testing.T, sid string, frames []protocol.ChatEventFrame) {
|
|
t.Helper()
|
|
sawText := false
|
|
for _, frame := range frames {
|
|
if frame.SessionID != sid {
|
|
t.Fatalf("frame session %q, want %s: %+v", frame.SessionID, sid, frame)
|
|
}
|
|
if frame.MessageID == "" {
|
|
t.Fatalf("frame missing a stable message id: %+v", frame)
|
|
}
|
|
if frame.Kind == protocol.ChatEventText {
|
|
sawText = true
|
|
}
|
|
}
|
|
if !sawText {
|
|
t.Fatalf("the completed turn produced no text frame: %+v", frames)
|
|
}
|
|
}
|
|
|
|
func firstDelta(frames []protocol.ChatEventFrame) string {
|
|
for _, frame := range frames {
|
|
if frame.Kind == protocol.ChatEventText && frame.Delta != "" {
|
|
return frame.Delta
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// frameKinds summarises the frame sequence (step frames carry their status) so
|
|
// the live run's observed output is legible evidence.
|
|
func frameKinds(frames []protocol.ChatEventFrame) string {
|
|
kinds := make([]string, 0, len(frames))
|
|
for _, frame := range frames {
|
|
if frame.Kind == protocol.ChatEventStep && frame.Step != nil {
|
|
kinds = append(kinds, frame.Kind+":"+frame.Step.Status)
|
|
continue
|
|
}
|
|
kinds = append(kinds, frame.Kind)
|
|
}
|
|
return strings.Join(kinds, ",")
|
|
}
|