chat: give the operator their own sessions, separate from run sessions
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.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -18,13 +19,14 @@ import (
|
||||
)
|
||||
|
||||
// TestChatLiveEndToEnd drives the real brokered chat path against the installed
|
||||
// OpenCode: it opens the server's single owned endpoint, creates a scratch
|
||||
// session, and pushes chat_sessions, chat_messages, a completed chat_prompt and
|
||||
// an interrupted chat_prompt through a real /connect websocket. It is opt-in
|
||||
// (real inference) and never touches the deployed server, ntfy or any lane; it
|
||||
// runs on an in-process handler and a t.TempDir workspace. The model defaults to
|
||||
// the pipeline's own and is overridable with
|
||||
// PIPELINES_TEST_OPENCODE_CHAT_MODEL=provider/model.
|
||||
// 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")
|
||||
@@ -39,40 +41,13 @@ func TestChatLiveEndToEnd(t *testing.T) {
|
||||
defer conn.Close()
|
||||
t.Logf("owned OpenCode endpoint %s (backend %s)", conn.URL, conn.Backend)
|
||||
|
||||
provider, model := "opencode-go", "deepseek-v4.1-flash"
|
||||
if override := os.Getenv("PIPELINES_TEST_OPENCODE_CHAT_MODEL"); override != "" {
|
||||
if parts := strings.SplitN(override, "/", 2); len(parts) == 2 {
|
||||
provider, model = parts[0], parts[1]
|
||||
}
|
||||
}
|
||||
created, err := conn.Call(ctx, "post", "/api/session", map[string]any{
|
||||
"title": "pipelines chat live check",
|
||||
"location": map[string]string{"directory": dir},
|
||||
"model": map[string]string{"providerID": provider, "id": model},
|
||||
"permissions": []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}},
|
||||
})
|
||||
store, err := pipeline.Open(filepath.Join(t.TempDir(), "chat-live.db"))
|
||||
if err != nil {
|
||||
t.Fatalf("create session: %v", err)
|
||||
t.Fatal(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("created session response %s (err %v)", created, err)
|
||||
}
|
||||
sid := session.Data.ID
|
||||
defer conn.Call(context.Background(), "delete", "/api/session/"+sid, nil)
|
||||
t.Logf("scratch session %s on %s/%s", sid, provider, model)
|
||||
defer store.Close()
|
||||
|
||||
options := Options{
|
||||
Chat: opencode.NewChat(conn),
|
||||
OwnedSessions: func(context.Context) ([]pipeline.SessionOwner, error) {
|
||||
return []pipeline.SessionOwner{{ID: sid, Directory: dir}}, nil
|
||||
},
|
||||
}
|
||||
srv := httptest.NewServer(HandlerWithOptions(ctx, options))
|
||||
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 {
|
||||
@@ -80,18 +55,60 @@ func TestChatLiveEndToEnd(t *testing.T) {
|
||||
}
|
||||
defer ws.CloseNow()
|
||||
|
||||
// chat_sessions must list exactly the owned scratch session.
|
||||
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatSessions, ID: "s1"}); err != nil {
|
||||
// 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.ChatSessions || resp.ID != "s1" {
|
||||
t.Fatalf("chat_sessions response = %+v", resp)
|
||||
if resp.Type != protocol.ChatDirectories || len(resp.ChatDirectories) == 0 || resp.ChatDirectories[0] != home {
|
||||
t.Fatalf("chat_directories = %+v, want home %q first", resp, home)
|
||||
}
|
||||
if len(resp.ChatSessions) != 1 || resp.ChatSessions[0].ID != sid {
|
||||
t.Fatalf("chat_sessions = %+v, want the owned session %s", resp.ChatSessions, sid)
|
||||
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)
|
||||
}
|
||||
t.Logf("chat_sessions listed %d owned session(s): %+v", len(resp.ChatSessions), resp.ChatSessions)
|
||||
_, 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 {
|
||||
@@ -115,50 +132,72 @@ func TestChatLiveEndToEnd(t *testing.T) {
|
||||
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))
|
||||
}
|
||||
|
||||
// An interrupted turn: post a long prompt, wait for its ack, then answer it
|
||||
// with chat_interrupt and confirm the ack.
|
||||
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatPrompt, ID: "p2", ChatPrompt: &protocol.ChatPromptRequest{SessionID: sid, Text: "Write a 400 line epic poem about the sea. Output every line as it comes."}}); err != nil {
|
||||
// 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)
|
||||
}
|
||||
if _, ack = readChatResponse(t, ctx, ws); ack.Type != protocol.ChatPrompt || ack.ID != "p2" {
|
||||
t.Fatalf("second prompt ack = %+v", ack)
|
||||
_, resp := readChatResponse(t, ctx, ws)
|
||||
if resp.Type != protocol.ChatSessions {
|
||||
t.Fatalf("chat_sessions response = %+v", resp)
|
||||
}
|
||||
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatInterrupt, ID: "i1", ChatQuery: &protocol.ChatQuery{SessionID: sid}}); err != nil {
|
||||
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)
|
||||
}
|
||||
for {
|
||||
message := readChatMessage(t, ctx, ws)
|
||||
if message.Type == protocol.ChatInterrupt {
|
||||
if message.ID != "i1" {
|
||||
t.Fatalf("interrupt ack = %+v, want i1", message)
|
||||
}
|
||||
break
|
||||
}
|
||||
items, err := store.List(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// The interrupt must actually end the running turn: the session leaves the
|
||||
// V2 active set. This is the observable proof the ack alone is not.
|
||||
deadline := time.Now().Add(30 * time.Second)
|
||||
for {
|
||||
active, err := conn.Call(ctx, "get", "/api/session/active", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("active check: %v", err)
|
||||
}
|
||||
var state struct {
|
||||
Data map[string]json.RawMessage `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(active, &state); err != nil {
|
||||
t.Fatalf("active response %s (err %v)", active, err)
|
||||
}
|
||||
if _, running := state.Data[sid]; !running {
|
||||
break
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatalf("session %s is still active after chat_interrupt", sid)
|
||||
}
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
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)
|
||||
}
|
||||
t.Logf("chat_interrupt acked and the interrupted turn left the active set")
|
||||
}
|
||||
|
||||
// readChatResponse reads until a non-event response message arrives, returning
|
||||
|
||||
Reference in New Issue
Block a user