Files
pipelines/internal/pipeline/operator_sessions_test.go
T
alan 18398dfa5b 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.
2026-10-17 11:14:25 +01:00

194 lines
6.1 KiB
Go

package pipeline
import (
"context"
"path/filepath"
"testing"
"time"
)
// TestOperatorSessionsRecordListAndUpsert proves the operator-session registry
// contract: rows persist the id, directory, title and tag; the list is
// most-recently-used first; and a second RecordOperatorSession for the same id
// updates directory/title/tag/last_used_at while preserving the original
// created_at.
func TestOperatorSessionsRecordListAndUpsert(t *testing.T) {
ctx := context.Background()
store, err := Open(filepath.Join(t.TempDir(), "operator.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if err := store.RecordOperatorSession(ctx, "ses_a", "/work/a", "A", "operator"); err != nil {
t.Fatal(err)
}
time.Sleep(3 * time.Millisecond)
if err := store.RecordOperatorSession(ctx, "ses_b", "/work/b", "B", "operator"); err != nil {
t.Fatal(err)
}
sessions, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if len(sessions) != 2 {
t.Fatalf("sessions = %+v, want 2", sessions)
}
// Most-recently-used first: ses_b was recorded after ses_a.
if sessions[0].ID != "ses_b" || sessions[1].ID != "ses_a" {
t.Fatalf("not ordered most-recently-used first: %+v", sessions)
}
if sessions[1].Directory != "/work/a" || sessions[1].Title != "A" || sessions[1].Tag != "operator" || sessions[1].CreatedAt == 0 || sessions[1].UpdatedAt == 0 {
t.Fatalf("recorded row missing fields: %+v", sessions[1])
}
originalCreated := sessions[1].CreatedAt
time.Sleep(3 * time.Millisecond)
if err := store.RecordOperatorSession(ctx, "ses_a", "/work/a2", "A2", "operator"); err != nil {
t.Fatal(err)
}
sessions, err = store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if sessions[0].ID != "ses_a" {
t.Fatalf("upsert did not move the session to the front: %+v", sessions)
}
updated := sessions[0]
if updated.Directory != "/work/a2" || updated.Title != "A2" || updated.Tag != "operator" {
t.Fatalf("upsert did not update directory/title/tag: %+v", updated)
}
if updated.CreatedAt != originalCreated {
t.Fatalf("upsert changed created_at: got %d want %d", updated.CreatedAt, originalCreated)
}
if updated.UpdatedAt <= originalCreated {
t.Fatalf("upsert did not move last_used_at: %+v", updated)
}
}
// TestOperatorSessionsIgnoresUntagged proves an untagged row is not an operator
// session: it is never listed and never contributes a directory choice.
func TestOperatorSessionsIgnoresUntagged(t *testing.T) {
ctx := context.Background()
store, err := Open(filepath.Join(t.TempDir(), "untagged.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
if err := store.RecordOperatorSession(ctx, "ses_untagged", "/work/untagged", "no tag", ""); err != nil {
t.Fatal(err)
}
sessions, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if len(sessions) != 0 {
t.Fatalf("untagged session was listed: %+v", sessions)
}
directories, err := store.OperatorDirectories(ctx, 8)
if err != nil {
t.Fatal(err)
}
if len(directories) != 0 {
t.Fatalf("untagged session contributed a directory: %+v", directories)
}
}
// TestOperatorDirectoriesDedupeBoundAndOrder proves OperatorDirectories returns
// distinct directories most-recently-used first, deduplicated and bounded: more
// directories are seeded than the requested limit.
func TestOperatorDirectoriesDedupeBoundAndOrder(t *testing.T) {
ctx := context.Background()
store, err := Open(filepath.Join(t.TempDir(), "directories.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
record := func(id, directory string) {
t.Helper()
if err := store.RecordOperatorSession(ctx, id, directory, id, "operator"); err != nil {
t.Fatal(err)
}
}
record("ses_1", "/work/one")
record("ses_2", "/work/one") // duplicate directory for a newer session
record("ses_3", "/work/two")
record("ses_4", "/work/three")
record("ses_5", "/work/four")
// Set the recency explicitly so the ordering assertion is deterministic.
set := func(id string, at int64) {
t.Helper()
if _, err := store.db.ExecContext(ctx, "UPDATE operator_sessions SET last_used_at=? WHERE session_id=?", at, id); err != nil {
t.Fatal(err)
}
}
set("ses_1", 10)
set("ses_2", 50) // the duplicate dir's newest use wins the ordering
set("ses_3", 40)
set("ses_4", 30)
set("ses_5", 20)
directories, err := store.OperatorDirectories(ctx, 2)
if err != nil {
t.Fatal(err)
}
if len(directories) != 2 {
t.Fatalf("directories = %+v, want the requested bound of 2", directories)
}
if directories[0] != "/work/one" || directories[1] != "/work/two" {
t.Fatalf("directories = %+v, want the two most recently used distinct dirs", directories)
}
all, err := store.OperatorDirectories(ctx, 8)
if err != nil {
t.Fatal(err)
}
if len(all) != 4 {
t.Fatalf("directories = %+v, want 4 deduplicated", all)
}
if all[0] != "/work/one" || all[1] != "/work/two" || all[2] != "/work/three" || all[3] != "/work/four" {
t.Fatalf("directories not MRU-ordered: %+v", all)
}
}
// TestOperatorSessionsSurviveRestart proves a recorded operator session is read
// back after Close and re-Open of the same SQLite path, with its
// directory/tag/recency intact and no re-registration.
func TestOperatorSessionsSurviveRestart(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "restart.db")
store, err := Open(path)
if err != nil {
t.Fatal(err)
}
if err := store.RecordOperatorSession(ctx, "ses_restart", "/work/restart", "Restart", "operator"); err != nil {
t.Fatal(err)
}
before, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if err := store.Close(); err != nil {
t.Fatal(err)
}
store, err = Open(path)
if err != nil {
t.Fatal(err)
}
defer store.Close()
after, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if len(after) != 1 || len(before) != 1 {
t.Fatalf("sessions before/after restart = %+v / %+v", before, after)
}
if after[0] != before[0] {
t.Fatalf("session changed across restart:\n got %+v\nwant %+v", after[0], before[0])
}
if after[0].Directory != "/work/restart" || after[0].Tag != "operator" || after[0].UpdatedAt == 0 {
t.Fatalf("restarted row missing fields: %+v", after[0])
}
}