Merge lane-chatsrv: keep the lane's reviewed work

This commit is contained in:
2026-10-17 11:14:52 +01:00
16 changed files with 1258 additions and 219 deletions
+34
View File
@@ -291,6 +291,40 @@ aggregates. See `internal/protocol/message.go` for JSON envelopes. Mutating RPCs
have real effects; they are not previews. TUI-only animation, scrolling, and
transient client logs are presentation, not separate server capabilities.
## Chat sessions
The brokered OpenCode chat exposes the operator's own sessions, separate from the
sessions pipeline runs create. The protocol types are:
- `chat_sessions` lists the operator's own sessions only, ordered
most-recently-used first. Each carries its `directory`, its `tag`, its `busy`
state (another connection currently holds it) and its recency. A session with
no recorded tag is never listed, and no pipeline stage session is ever listed:
stage sessions are still recorded in `run_steps`/`run_session_owners`
for the run that owns them. The list is fail-closed: with no registry and no
store it returns an empty list, never the endpoint's whole session list.
- `chat_create` takes a `title` (default "New chat") and a `directory`
(default `~`, the operator's home; `~`, an omitted or empty value and a `~/...`
value all resolve to the home directory). A relative or missing directory is
rejected before any OpenCode call. The created session is recorded with the
stored tag `operator` and returned as the response's `chat_session`.
- `chat_directories` returns the operator's home first, then the directories of
previous operator sessions most-recently-used first, deduplicated and bounded
(currently 8). It is a dedicated request so the create dialog can load the
choices without first listing sessions; `chat_sessions` stays a pure session
list.
The stored tag is exactly `operator`; the display label `Operator` is derived from
it, so there is no second tag vocabulary. Chat uses the server's one OpenCode
transport, and every existing chat guarantee (retained-state bounds, the
permission/approval flow, interruption, one broker call per list) is unchanged.
The operator-session registry, filter, directory defaulting and the list bound are
covered by the `pipeline`, `opencode` and `server` unit tests. The live end-to-end
check is opt-in (`PIPELINES_TEST_OPENCODE_CHAT_LIVE=1`); it runs against a scratch
store and never touches the deployed server. Android rendering of the tag is out
of scope for the server change; the wire carries `tag: "operator"`.
## Scripted runs
Every run records a short, single-line `title` saying what it was for. `pipelines
+45 -19
View File
@@ -227,11 +227,9 @@ traversal, absolute paths, symlink escapes and unknown runs/steps.
## Brokered OpenCode chat
The protocol declares the brokered `chat_*` surface (`chat_sessions`,
`chat_create`, `chat_prompt`, `chat_messages`, `chat_interrupt`, `chat_permission`,
`chat_reply`). The server dispatches `chat_sessions`, `chat_prompt`,
`chat_messages`, `chat_interrupt` and `chat_reply`, answers the declared
`chat_create` with an explicit protocol error because it is not yet brokered, and
pushes `chat_event` frames derived from OpenCode's global SSE firehose
`chat_create`, `chat_directories`, `chat_prompt`, `chat_messages`,
`chat_interrupt`, `chat_permission`, `chat_reply`). The server dispatches all of
them, and pushes `chat_event` frames derived from OpenCode's global SSE firehose
(`/event` on the native serve, `/api/event` on the V2 API), with retained
per-session/per-message state so a reconnecting client re-syncs instead of
replaying deltas.
@@ -248,16 +246,39 @@ events it does not surface before decoding their data, so an unrelated event
whose `delta` has a different shape (`session.instructions.updated` carries an
object) never fails the stream.
`chat_sessions` is brokered but restricted: OpenCode's `GET /api/session` lists
every session on the machine, so the server filters it to the sessions recorded
in `run_steps` as owned by this server (deduped by session id), fills any missing
directory from the owning run's workspace, and fails closed to an empty list when
no ownership source is configured rather than exposing the operator's whole
environment. A session another client currently holds (an in-server claim,
released when the stream ends or the client disconnects) or that this server
records as an active run is marked `busy` so the phone can show it read-only; a
second client's `chat_prompt` for a held session is rejected before any OpenCode
call. `chat_create` remains unbrokered.
`chat_sessions` lists the operator's own chat sessions, not the sessions pipeline
runs create. Operator sessions are recorded in the dedicated `operator_sessions`
table (`session_id` primary key, plus `directory`, `title`, `tag`, `created_at`,
`last_used_at`), separate from `run_steps`/`run_session_owners`, so an operator
chat is a first-class thing and a pipeline stage session — recorded as run
ownership, never in the operator registry — is never listed. The handler reads the
registry once (bounded to 128 rows, most-recently-used first), calls OpenCode's
session list once, and keeps only registry rows the endpoint still reports, so a
session with no recorded tag is never listed and a deleted upstream session does
not linger. It fills each session's `directory`, `tag` and `created_at`/
`updated_at` from the registry and marks `busy` when another client currently
holds the session (an in-server claim, released when the stream ends or the client
disconnects); a second client's `chat_prompt` for a held session is rejected
before any OpenCode call. It is fail-closed: with no registry configured it
returns an empty list, never the endpoint's whole session list. The OpenCode
session list is read unscoped by directory (the V2 `GET /api/session` otherwise
defaults to the caller's directory), because an operator session may live in any
directory.
`chat_create` creates one operator session and records it in the same registry
with the stored tag `operator` (the display label `Operator` is derived from it,
so there is no second tag vocabulary). It takes a `title` (default "New chat") and
a `directory` (default `~`): an omitted or empty value, `~`, and any `~/...` value
all resolve to `os.UserHomeDir`, never the server's working directory; a relative
path or a missing directory is rejected before any OpenCode call, and a session
created but not recordable is reported as an error naming its id. Create posts the
verified V2 shape `POST /api/session` `{"title":...,"location":{"directory":...}}`.
`chat_directories` returns the operator's home first, then the directories of
previous operator sessions most-recently-used first, deduplicated and bounded (8);
it is a dedicated request so the create dialog can load the choices without first
listing sessions and `chat_sessions` stays a pure, fail-closed session list. A
successful `chat_prompt` touches the session's `last_used_at` in one UPDATE, so it
moves to the front of the list; a non-operator session id touches no row.
While a `chat_prompt` subscription is open the server also polls
`GET /api/permission/request` on a fixed small interval (first poll immediately) and
@@ -288,10 +309,15 @@ Verified against the installed OpenCode 2.0.20 on 2026-10-17:
plus `session.step.*` and `session.usage.updated`; the V2 API server
authenticates the fixed `opencode` basic-auth user.
- The opt-in live end-to-end test (`PIPELINES_TEST_OPENCODE_CHAT_LIVE=1 go test
./internal/server/... -run TestChatLiveEndToEnd`) opened an owned V2 endpoint,
listed the owned scratch session, streamed a completed turn as
`[step:started,text,step:streamed,step:ended]` (first delta `"pong"`), and
interrupted a running turn so the session left the V2 active set.
./internal/server -run TestChatLiveEndToEnd -v`) opened an owned V2 endpoint,
created an operator session through `chat_create` in a chosen directory, listed
exactly that session through `chat_sessions` (with its directory and tag
`operator`), recorded a genuine pipeline stage session in
`run_steps`/`run_session_owners` and showed it never appeared in the list, and
streamed a completed turn as `[step:started,text,step:streamed,step:ended]`
(first delta `"pong"`). The operator-session registry, the filter, directory
defaulting and the recents bound are also covered by the `pipeline`, `opencode`
and `server` unit tests, which run without the live gate.
- Not verified live: a genuine pending-approval round trip and any backend other
than the installed 2.0.20. The permission wire shapes are unit-tested over an
httptest fixture, not against a live pending request.
+79 -17
View File
@@ -115,20 +115,45 @@ func (c *Chat) Messages(ctx context.Context, id string, limit int) ([]protocol.C
// Sessions lists the OpenCode sessions on the endpoint. On the V2 HTTP API that
// is GET /api/session; the native v1 serve translation maps it to GET /session.
// It decodes the verified {"data":[...]} envelope (tolerating a bare array like
// chatMessageRecords), lifts the nested location.directory and
// time{created,updated} into protocol.ChatSession, drops records without a valid
// session id, and orders newest-updated first so the list is deterministic. The
// service's list is global; the caller is responsible for restricting it to the
// sessions this server owns.
// It reads the list unscoped by the connection's directory (see
// Connection.SessionList): the V2 list is otherwise restricted to the
// connection's own directory, while an operator session may live in any
// directory, and the caller is responsible for restricting the result to the
// operator's recorded sessions. It decodes the verified {"data":[...]} envelope
// (tolerating a bare array like chatMessageRecords), lifts the nested
// location.directory and time{created,updated} into protocol.ChatSession, drops
// records without a valid session id, and orders newest-updated first so the list
// is deterministic.
func (c *Chat) Sessions(ctx context.Context) ([]protocol.ChatSession, error) {
data, err := c.call(ctx, "get", "/api/session", nil)
c.mu.Lock()
data, err := c.conn.SessionList(ctx)
c.mu.Unlock()
if err != nil {
return nil, err
}
return decodeChatSessions(data)
}
// Create creates one OpenCode session in the given directory, mirroring the
// verified V2 shape POST /api/session {"title":...,"location":{"directory":...}}.
// It deliberately sends no model or permission override here: the endpoint's own
// defaults apply, exactly as the live check creates a session. The created
// session is returned with its directory lifted from location.directory.
func (c *Chat) Create(ctx context.Context, title, directory string) (protocol.ChatSession, error) {
body := struct {
Title string `json:"title"`
Location struct {
Directory string `json:"directory"`
} `json:"location"`
}{Title: title}
body.Location.Directory = directory
data, err := c.call(ctx, "post", "/api/session", body)
if err != nil {
return protocol.ChatSession{}, err
}
return decodeChatSession(data)
}
// Reply posts a permission decision, mirroring the documented endpoint.
func (c *Chat) Reply(ctx context.Context, request protocol.ChatReplyRequest) error {
if !sessionID.MatchString(request.SessionID) {
@@ -629,6 +654,39 @@ func chatSessionRecords(data []byte) ([]nativeChatSession, error) {
return records, nil
}
// chatSessionRecord reads one created session, accepting the {"data":{...}}
// envelope the V2 API returns and tolerating a bare object. An unrecognised body
// is an error, never an empty session.
func chatSessionRecord(data []byte) (nativeChatSession, error) {
var response struct {
Data *nativeChatSession `json:"data"`
}
if err := json.Unmarshal(data, &response); err == nil && response.Data != nil {
return *response.Data, nil
}
var record nativeChatSession
if err := json.Unmarshal(data, &record); err != nil || record.ID == "" {
return nativeChatSession{}, fmt.Errorf("invalid OpenCode session response")
}
return record, nil
}
// chatSessionFields lifts the nested location.directory and time{created,updated}
// of one native session record onto protocol.ChatSession. It is the single field
// mapping shared by the list decoder and Create, so the two cannot drift.
func chatSessionFields(record nativeChatSession) protocol.ChatSession {
return protocol.ChatSession{
ID: record.ID,
Title: record.Title,
Agent: record.Agent,
Model: record.Model.ID,
Directory: record.Location.Directory,
ParentID: record.ParentID,
CreatedAt: record.Time.Created,
UpdatedAt: record.Time.Updated,
}
}
// decodeChatSessions maps the listed sessions onto protocol.ChatSession, dropping
// every record whose id is not a valid OpenCode session id (an empty or foreign
// id cannot be addressed) and ordering by UpdatedAt then id so the result is
@@ -643,16 +701,7 @@ func decodeChatSessions(data []byte) ([]protocol.ChatSession, error) {
if !sessionID.MatchString(record.ID) {
continue
}
sessions = append(sessions, protocol.ChatSession{
ID: record.ID,
Title: record.Title,
Agent: record.Agent,
Model: record.Model.ID,
Directory: record.Location.Directory,
ParentID: record.ParentID,
CreatedAt: record.Time.Created,
UpdatedAt: record.Time.Updated,
})
sessions = append(sessions, chatSessionFields(record))
}
sort.SliceStable(sessions, func(i, j int) bool {
if sessions[i].UpdatedAt != sessions[j].UpdatedAt {
@@ -662,3 +711,16 @@ func decodeChatSessions(data []byte) ([]protocol.ChatSession, error) {
})
return sessions, nil
}
// decodeChatSession maps one created session, rejecting an empty or foreign id
// (an unaddressable session is an error, never a silent success).
func decodeChatSession(data []byte) (protocol.ChatSession, error) {
record, err := chatSessionRecord(data)
if err != nil {
return protocol.ChatSession{}, err
}
if !sessionID.MatchString(record.ID) {
return protocol.ChatSession{}, fmt.Errorf("invalid OpenCode session response")
}
return chatSessionFields(record), nil
}
+70 -2
View File
@@ -502,15 +502,21 @@ func TestDecodeChatMessagesSkipsUnknownPartsAndRoles(t *testing.T) {
// TestChatSessionsListsAndLiftsShape loads the verified V2 session list
// (GET /api/session: {"data":[{id,...,location.directory,time{created,updated},
// parentID}]}) and asserts the adapter reads it exactly once, lifts the nested
// directory/time onto protocol.ChatSession, and orders newest-updated first.
// directory/time onto protocol.ChatSession, orders newest-updated first, and does
// not scope the list to the connection's own directory (so operator sessions in
// other directories are returned for the caller to filter).
func TestChatSessionsListsAndLiftsShape(t *testing.T) {
var reads atomic.Int32
var scoped bool
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet || r.URL.Path != "/api/session" {
t.Errorf("unexpected request %s %s", r.Method, r.URL.Path)
return
}
reads.Add(1)
if r.URL.Query().Get("directory") != "" {
scoped = true
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"location":{"directory":"/work"},"data":[
{"id":"ses_old","title":"older","agent":"build","model":{"id":"m1"},"parentID":"ses_new","location":{"directory":"/proj"},"time":{"created":10,"updated":20}},
@@ -519,7 +525,7 @@ func TestChatSessionsListsAndLiftsShape(t *testing.T) {
}))
defer srv.Close()
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL, Directory: "/repo"})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
sessions, err := chat.Sessions(ctx)
@@ -529,6 +535,9 @@ func TestChatSessionsListsAndLiftsShape(t *testing.T) {
if reads.Load() != 1 {
t.Fatalf("session endpoint read %d times, want 1", reads.Load())
}
if scoped {
t.Fatal("session list was scoped to the connection directory; it must list every directory")
}
if len(sessions) != 2 {
t.Fatalf("sessions = %+v, want 2", sessions)
}
@@ -631,6 +640,65 @@ func TestChatStreamRequiresEndpoint(t *testing.T) {
}
}
// TestChatCreatePostsSessionEndpoint pins the outbound create call: the adapter
// must POST exactly once to /api/session with the verified V2 body
// {"title":...,"location":{"directory":...}} (no invented mechanism, no model or
// permission override), read exactly that one path, and lift the nested
// location.directory from the {"data":{...}} response.
func TestChatCreatePostsSessionEndpoint(t *testing.T) {
type request struct{ method, path, body string }
var mu sync.Mutex
var requests []request
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, _ := io.ReadAll(r.Body)
mu.Lock()
requests = append(requests, request{method: r.Method, path: r.URL.Path, body: string(data)})
mu.Unlock()
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"data":{"id":"ses_new","title":"demo","location":{"directory":"/work/proj"},"time":{"created":11,"updated":22}}}`))
}))
defer srv.Close()
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
session, err := chat.Create(ctx, "demo", "/work/proj")
if err != nil {
t.Fatalf("create: %v", err)
}
mu.Lock()
got := append([]request(nil), requests...)
mu.Unlock()
if len(got) != 1 {
t.Fatalf("create requests = %d, want 1: %+v", len(got), got)
}
if got[0].method != http.MethodPost || got[0].path != "/api/session" {
t.Fatalf("create request = %+v", got[0])
}
if got[0].body != `{"title":"demo","location":{"directory":"/work/proj"}}` {
t.Fatalf("create body = %q", got[0].body)
}
if session.ID != "ses_new" || session.Title != "demo" || session.Directory != "/work/proj" || session.CreatedAt != 11 || session.UpdatedAt != 22 {
t.Fatalf("created session = %+v", session)
}
}
// TestChatCreateRejectsMalformedSession pins that a response without a valid
// session id is an error, not a silent empty success.
func TestChatCreateRejectsMalformedSession(t *testing.T) {
for _, raw := range []string{`{"data":{}}`, `{"data":{"id":""}}`, `{"data":{"id":"msg_not_a_session"}}`, `not json`} {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(raw))
}))
chat := NewChat(&Connection{Backend: "v2", URL: srv.URL})
if _, err := chat.Create(context.Background(), "demo", "/work"); err == nil {
t.Fatalf("response %q must be an error, not empty success", raw)
}
srv.Close()
}
}
func TestNormalizeChatEventWithoutMessageID(t *testing.T) {
for _, payload := range []string{
`{"type":"session.text.delta","data":{"sessionID":"ses_1","ordinal":0,"delta":"x"}}`,
+12 -3
View File
@@ -316,15 +316,24 @@ func (c *Connection) Close() {
}
func (c *Connection) Native(ctx context.Context, method, path string, body any) ([]byte, error) {
return c.nativeRequest(ctx, method, path, body, c.Directory)
}
// nativeRequest is the shared Native request path. An empty directory omits the
// ?directory= query parameter; the V2 session list then returns sessions across
// every directory instead of only the connection's own.
func (c *Connection) nativeRequest(ctx context.Context, method, path string, body any, directory string) ([]byte, error) {
c.LastHTTPStatus = 0
c.LastHTTPOperation = strings.ToUpper(method) + " " + path
u, err := url.Parse(c.URL + path)
if err != nil {
return nil, err
}
query := u.Query()
query.Set("directory", c.Directory)
u.RawQuery = query.Encode()
if directory != "" {
query := u.Query()
query.Set("directory", directory)
u.RawQuery = query.Encode()
}
var data []byte
if body != nil {
data, err = json.Marshal(body)
+17
View File
@@ -63,6 +63,23 @@ func messageText(data []byte) (string, error) {
return text, nil
}
// SessionList reads the endpoint's whole OpenCode session list without the
// connection's directory scope, so the brokered chat handler can filter it to
// the operator's own sessions across every directory. The V2 HTTP API scopes
// GET /api/session to ?directory= when given; omitting it lists every session,
// which is required because an operator session may live in any directory. The
// native serve list is /session. It reuses the same authenticated request path
// (no second transport or endpoint); only the directory query is omitted.
func (c *Connection) SessionList(ctx context.Context) ([]byte, error) {
if c.Backend == "v1" {
return c.nativeRequest(ctx, "get", "/session", nil, "")
}
if c.URL == "" {
return nil, fmt.Errorf("native adapter requires the serve backend")
}
return c.nativeRequest(ctx, "get", "/api/session", nil, "")
}
// Call adapts the pipeline's existing V2 command contract to the native stable API.
// Sessions still use the same account/database; only our loopback serve process is owned.
// A connection whose backend already speaks the /api HTTP surface forwards the path
+110
View File
@@ -0,0 +1,110 @@
package pipeline
import (
"context"
"time"
)
// operatorSessionListLimit bounds one OperatorSessions read so a long-lived
// database with unbounded operator-session history cannot make one chat_sessions
// request scan it all. Rows are read most-recently-used first, so the bound only
// ever drops the oldest sessions, never one the operator just used. It mirrors
// the bounded newest-first convention of sessionOwnerScanLimit.
const operatorSessionListLimit = 128
// OperatorSession is one recorded operator chat session: a chat the person
// created through chat_create, tagged with its kind and carrying the directory
// it lives in. CreatedAt is when the row was first recorded; UpdatedAt is the
// last-used epoch-millisecond timestamp that drives most-recently-used ordering.
type OperatorSession struct {
ID string
Directory string
Title string
Tag string
CreatedAt int64
UpdatedAt int64
}
// initOperatorSessions creates the operator-session registry. It is a table of
// its own rather than a kind column on run_steps so a pipeline run's session
// ownership (run_session_owners, SessionOwner) is untouched and the two
// vocabularies cannot be confused. session_id is the primary key, so lookups and
// the upsert use the index; operator_sessions_recent serves the recency order.
func (s *Store) initOperatorSessions() error {
_, err := s.db.Exec(`CREATE TABLE IF NOT EXISTS operator_sessions (
session_id TEXT PRIMARY KEY, directory TEXT NOT NULL, title TEXT NOT NULL DEFAULT '',
tag TEXT NOT NULL DEFAULT '', created_at INTEGER NOT NULL, last_used_at INTEGER NOT NULL);
CREATE INDEX IF NOT EXISTS operator_sessions_recent ON operator_sessions(last_used_at DESC);`)
return err
}
// RecordOperatorSession upserts one operator chat session in a single statement:
// a new id is inserted with its created_at and last_used_at set to now, and a
// repeated id updates directory/title/tag and moves last_used_at to now while
// preserving the original created_at. CreatedAt is therefore stable across
// re-records.
func (s *Store) RecordOperatorSession(ctx context.Context, sessionID, directory, title, tag string) error {
now := time.Now().UnixMilli()
_, err := s.db.ExecContext(ctx, `INSERT INTO operator_sessions(session_id,directory,title,tag,created_at,last_used_at)
VALUES(?,?,?,?,?,?)
ON CONFLICT(session_id) DO UPDATE SET directory=excluded.directory,title=excluded.title,tag=excluded.tag,last_used_at=excluded.last_used_at`,
sessionID, directory, title, tag, now, now)
return err
}
// TouchOperatorSession records that an operator session was just used, in one
// UPDATE. A session id that is not an operator session updates no row and is not
// an error: the statement is itself the presence check, so no second read is
// needed.
func (s *Store) TouchOperatorSession(ctx context.Context, sessionID string) error {
_, err := s.db.ExecContext(ctx, "UPDATE operator_sessions SET last_used_at=? WHERE session_id=?", time.Now().UnixMilli(), sessionID)
return err
}
// OperatorSessions reports the operator's chat sessions, most-recently-used
// first (ties broken by session id for a deterministic order), capped at
// operatorSessionListLimit. Rows with no tag are omitted: a session with no
// recorded tag is not an operator session and must never be listed. It is one
// bounded statement; recency is never mutated here.
func (s *Store) OperatorSessions(ctx context.Context) ([]OperatorSession, error) {
rows, err := s.db.QueryContext(ctx, `SELECT session_id,directory,title,tag,created_at,last_used_at
FROM operator_sessions WHERE tag <> '' ORDER BY last_used_at DESC, session_id LIMIT ?`, operatorSessionListLimit)
if err != nil {
return nil, err
}
defer rows.Close()
sessions := []OperatorSession{}
for rows.Next() {
var session OperatorSession
if err := rows.Scan(&session.ID, &session.Directory, &session.Title, &session.Tag, &session.CreatedAt, &session.UpdatedAt); err != nil {
return nil, err
}
sessions = append(sessions, session)
}
return sessions, rows.Err()
}
// OperatorDirectories reports the distinct directories of the operator's
// sessions, most-recently-used first, deduplicated in SQL and capped at limit.
// It is one bounded statement, newest use per directory wins the ordering, and
// it never mutates recency. A non-positive limit returns no directories.
func (s *Store) OperatorDirectories(ctx context.Context, limit int) ([]string, error) {
if limit <= 0 {
return nil, nil
}
rows, err := s.db.QueryContext(ctx, `SELECT directory FROM operator_sessions WHERE tag <> ''
GROUP BY directory ORDER BY MAX(last_used_at) DESC, directory LIMIT ?`, limit)
if err != nil {
return nil, err
}
defer rows.Close()
directories := []string{}
for rows.Next() {
var directory string
if err := rows.Scan(&directory); err != nil {
return nil, err
}
directories = append(directories, directory)
}
return directories, rows.Err()
}
+193
View File
@@ -0,0 +1,193 @@
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])
}
}
+4
View File
@@ -46,6 +46,10 @@ CREATE TABLE IF NOT EXISTS submissions (id INTEGER PRIMARY KEY, name TEXT NOT NU
db.Close()
return nil, err
}
if err := s.initOperatorSessions(); err != nil {
db.Close()
return nil, err
}
return s, nil
}
+20
View File
@@ -22,6 +22,13 @@ const (
ChatMessages = "chat_messages"
ChatInterrupt = "chat_interrupt"
ChatReply = "chat_reply"
// ChatDirectories is the request/response type for the operator's directory
// choices: the home directory plus the directories of previous operator
// sessions, most recently used first, deduplicated and bounded. It is a
// dedicated request (not a field on chat_sessions) so the create dialog can
// load the choices without first listing sessions and chat_sessions stays a
// pure, fail-closed operator-session list.
ChatDirectories = "chat_directories"
// ChatEvent and ChatPermission are the server-pushed frames; they are the
// only types the server sends without a matching request.
ChatEvent = "chat_event"
@@ -69,6 +76,15 @@ const (
ChatDecisionReject = "reject"
)
// ChatSession.Tag values. The stored tag vocabulary is exactly
// ChatSessionTagOperator; the display label the app shows is
// ChatSessionTagOperatorDisplay, derived from the wire tag rather than carried
// as a second field so there is no duplicate mapping to drift.
const (
ChatSessionTagOperator = "operator"
ChatSessionTagOperatorDisplay = "Operator"
)
// ChatMessage.Role values.
const (
ChatRoleUser = "user"
@@ -85,6 +101,10 @@ type ChatSession struct {
Model string `json:"model,omitempty"`
Directory string `json:"directory,omitempty"`
ParentID string `json:"parent_id,omitempty"`
// Tag marks the session kind. The only stored value is "operator" (a chat
// the person created); pipeline stage sessions are never tagged and are never
// listed by chat_sessions. Additive/omitempty so older peers ignore it.
Tag string `json:"tag,omitempty"`
CreatedAt int64 `json:"created_at,omitempty"`
UpdatedAt int64 `json:"updated_at,omitempty"`
// Busy marks a session another client currently holds (or one the server
+21 -4
View File
@@ -19,7 +19,7 @@ func chatSamples() []struct {
}{
{"session", ChatSession{
ID: "ses_1", Title: "demo", Agent: "build", Model: "deepseek-v4.1-flash",
Directory: "/work", ParentID: "ses_0", CreatedAt: 1792112366711, UpdatedAt: 1792112367999,
Directory: "/work", ParentID: "ses_0", Tag: ChatSessionTagOperator, CreatedAt: 1792112366711, UpdatedAt: 1792112367999,
}},
{"session_busy", ChatSession{ID: "ses_2", Title: "held", Busy: true}},
{"create", ChatCreateRequest{Title: "demo", Directory: "/work"}},
@@ -162,7 +162,8 @@ func TestChatEnvelopeRoundTrip(t *testing.T) {
ChatPermission: &ChatPermissionFrame{
SessionID: "ses_1", RequestID: "per_1", Action: "bash", Resources: []string{"rm -rf /"},
},
ChatReply: &ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: ChatDecisionReject},
ChatReply: &ChatReplyRequest{SessionID: "ses_1", RequestID: "req_1", Decision: ChatDecisionReject},
ChatDirectories: []string{"/home/operator", "/work/project"},
}
encoded, err := json.Marshal(message)
if err != nil {
@@ -183,7 +184,8 @@ func TestChatEnvelopeRoundTrip(t *testing.T) {
!reflect.DeepEqual(decoded.ChatMessages, message.ChatMessages) ||
!reflect.DeepEqual(decoded.ChatEvent, message.ChatEvent) ||
!reflect.DeepEqual(decoded.ChatPermission, message.ChatPermission) ||
!reflect.DeepEqual(decoded.ChatReply, message.ChatReply) {
!reflect.DeepEqual(decoded.ChatReply, message.ChatReply) ||
!reflect.DeepEqual(decoded.ChatDirectories, message.ChatDirectories) {
t.Fatalf("chat payloads lost: %+v", decoded)
}
}
@@ -193,9 +195,24 @@ func TestChatFieldsOmittedWhenUnused(t *testing.T) {
if err != nil {
t.Fatal(err)
}
for _, absent := range []string{"chat_sessions", "chat_session", "chat_create", "chat_query", "chat_prompt", "chat_messages", "chat_event", "chat_permission", "chat_reply"} {
for _, absent := range []string{"chat_sessions", "chat_session", "chat_create", "chat_query", "chat_prompt", "chat_messages", "chat_event", "chat_permission", "chat_reply", "chat_directories"} {
if strings.Contains(string(minimal), absent) {
t.Fatalf("absent %q must be omitted: %s", absent, minimal)
}
}
}
// TestChatOperatorTagVocabulary pins that the stored tag and the display label
// are one mapping (a wire tag plus a derived label), not a second vocabulary.
func TestChatOperatorTagVocabulary(t *testing.T) {
if ChatSessionTagOperator != "operator" || ChatSessionTagOperatorDisplay != "Operator" {
t.Fatalf("operator tag vocabulary = %q/%q, want operator/Operator", ChatSessionTagOperator, ChatSessionTagOperatorDisplay)
}
encoded, err := json.Marshal(ChatSession{ID: "ses_1", Tag: ChatSessionTagOperator})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(encoded), `"tag":"operator"`) {
t.Fatalf("operator tag not on the wire: %s", encoded)
}
}
+5
View File
@@ -165,6 +165,11 @@ type Message struct {
ChatEvent *ChatEventFrame `json:"chat_event,omitempty"`
ChatPermission *ChatPermissionFrame `json:"chat_permission,omitempty"`
ChatReply *ChatReplyRequest `json:"chat_reply,omitempty"`
// ChatDirectories carries the operator's directory choices on a
// chat_directories response (home first, then recent operator-session
// directories, deduplicated and bounded). Additive/omitempty so older peers
// ignore it.
ChatDirectories []string `json:"chat_directories,omitempty"`
// RunEvent is the server-pushed run state delta (see runevents.go) and
// RunIDs the optional subscribe scope, so a client asks for the runs it
// displays. Both are optional so older peers ignore them.
+192 -51
View File
@@ -6,6 +6,8 @@ import (
"encoding/hex"
"errors"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
@@ -27,14 +29,27 @@ type chatBroker interface {
Interrupt(ctx context.Context, sessionID string) error
Messages(ctx context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error)
// Sessions lists the endpoint's OpenCode sessions so the handler can
// restrict them to the ones this server owns.
// restrict them to the operator's own recorded sessions.
Sessions(ctx context.Context) ([]protocol.ChatSession, error)
// Create creates one OpenCode session in the given directory and returns it.
Create(ctx context.Context, title, directory string) (protocol.ChatSession, error)
// Permissions lists the session's pending OpenCode approval requests so the
// handler can push chat_permission frames.
Permissions(ctx context.Context, sessionID string) ([]protocol.ChatPermissionFrame, error)
Reply(ctx context.Context, request protocol.ChatReplyRequest) error
}
// operatorRegistry is the server-side operator-session seam. *pipeline.Store
// satisfies it. It is a table of its own, separate from the pipeline stage
// ownership (SessionOwner/run_session_owners) that settles a run: chat_sessions
// lists operator sessions only and never consults stage ownership.
type operatorRegistry interface {
RecordOperatorSession(ctx context.Context, sessionID, directory, title, tag string) error
TouchOperatorSession(ctx context.Context, sessionID string) error
OperatorSessions(ctx context.Context) ([]pipeline.OperatorSession, error)
OperatorDirectories(ctx context.Context, limit int) ([]string, error)
}
// chatPermissionPollInterval is the fixed interval between permission polls: one
// GET per poll through the shared Connection bounds, never a busy loop. The first
// poll runs immediately so a request pending when the prompt is sent is surfaced
@@ -55,6 +70,12 @@ const chatRetainedLimit = opencode.MaxResponse
// least-recently-updated turn is evicted once the cap is exceeded.
const chatMaxTurns = 128
// operatorDirectoryLimit bounds the chat_directories choices: the operator home
// plus recent operator-session directories, capped at this small fixed number so
// a long history is never returned. It matches the posture of the other small
// fixed list bounds.
const operatorDirectoryLimit = 8
// chatClientToken returns an unguessable per-connection identity. It attributes
// a session claim so two clients on the same server cannot silently write the
// same conversation; a fresh token per connection means a reconnected client is
@@ -433,10 +454,10 @@ func (h *chatHub) forgetPermission(sessionID, requestID string) {
// the generic response). Frames are pushed after the ack, each through the
// connection-scoped write function so the read loop and the stream goroutine
// never interleave partial websocket writes.
func handleChat(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, owners func(context.Context) ([]pipeline.SessionOwner, error), clientID string, write func(protocol.Message) error) bool {
func handleChat(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) bool {
switch request.Type {
case protocol.ChatPrompt:
handleChatPrompt(ctx, request, broker, hub, clientID, write)
handleChatPrompt(ctx, request, broker, hub, registry, clientID, write)
return true
case protocol.ChatMessages:
handleChatMessages(ctx, request, broker, hub, write)
@@ -448,90 +469,202 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
handleChatSimple(ctx, request, broker, hub, write)
return true
case protocol.ChatSessions:
handleChatSessions(ctx, request, broker, hub, owners, clientID, write)
handleChatSessions(ctx, request, broker, hub, registry, clientID, write)
return true
case protocol.ChatCreate:
// chat_create remains declared for the client but is not implemented yet.
// Answer explicitly so the client never mistakes the generic Pong for a
// chat reply, and the boundary stays visible instead of silently
// dropping.
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "creating a chat is not implemented yet"})
handleChatCreate(ctx, request, broker, registry, write)
return true
case protocol.ChatDirectories:
handleChatDirectories(ctx, request, registry, write)
return true
}
return false
}
// handleChatSessions answers chat_sessions with the OpenCode sessions this
// server owns, marking any another client currently holds (or an active run) as
// Busy. FAIL-CLOSED: when no ownership source is configured the result is empty,
// never the whole endpoint list. broker.Sessions is called at most once per
// request.
func handleChatSessions(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, owners func(context.Context) ([]pipeline.SessionOwner, error), clientID string, write func(protocol.Message) error) {
// handleChatSessions answers chat_sessions with the operator's own sessions,
// marking any another client currently holds as Busy. The registry is the single
// source of truth for which sessions are listed and for their directory, tag,
// created/recency, so a pipeline stage session (recorded in run_steps, never in
// the operator registry) is always excluded and a session with no recorded tag
// is never listed. FAIL-CLOSED: when no registry is configured the result is
// empty, never the whole endpoint list. broker.Sessions is called at most once
// per request, and the registry once.
func handleChatSessions(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) {
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
owned, err := ownedSessionSet(ctx, owners)
response := protocol.Message{Type: protocol.ChatSessions, ID: request.ID}
// No registry (no store and no injection): list nothing rather than the whole
// OpenCode machine.
if registry == nil {
_ = write(response)
return
}
operatorSessions, err := registry.OperatorSessions(ctx)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
// OperatorSessions is already most-recently-used first with a deterministic
// tie-break; keep that order and only include a registry session the broker
// still reports, so a session the operator deleted upstream is not listed.
registered := make(map[string]struct{}, len(operatorSessions))
for _, session := range operatorSessions {
if session.ID != "" {
registered[session.ID] = struct{}{}
}
}
sessions, err := broker.Sessions(ctx)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
response := protocol.Message{Type: protocol.ChatSessions, ID: request.ID}
present := make(map[string]protocol.ChatSession, len(sessions))
for _, session := range sessions {
owner, ok := owned[session.ID]
if _, ok := registered[session.ID]; ok {
present[session.ID] = session
}
}
for _, operator := range operatorSessions {
session, ok := present[operator.ID]
if !ok {
continue
}
if session.Directory == "" {
session.Directory = owner.Directory
}
if owner.Active || hub.busyForOther(session.ID, clientID) {
session.Busy = true
if operator.Title != "" {
session.Title = operator.Title
}
session.Directory = operator.Directory
session.Tag = operator.Tag
session.CreatedAt = operator.CreatedAt
session.UpdatedAt = operator.UpdatedAt
session.Busy = hub.busyForOther(operator.ID, clientID)
response.ChatSessions = append(response.ChatSessions, session)
}
_ = write(response)
}
// ownedSessionSet reads the ownership source once and returns a set keyed by
// session id, deduped in favour of an active owner and a non-empty directory.
// A nil source (no store and no injection) yields an empty set: chat_sessions
// then lists nothing rather than the whole OpenCode machine.
func ownedSessionSet(ctx context.Context, owners func(context.Context) ([]pipeline.SessionOwner, error)) (map[string]pipeline.SessionOwner, error) {
set := map[string]pipeline.SessionOwner{}
if owners == nil {
return set, nil
// handleChatCreate creates one operator session in a resolved directory and
// records it in the operator registry: the OpenCode session id, the exact
// resolved directory, the title and the operator tag. A blank or omitted
// directory defaults to the operator's home, never the server's working
// directory. A registry write failure answers an error naming the created id, so
// an orphaned session is visible rather than silently reported as success.
func handleChatCreate(ctx context.Context, request protocol.Message, broker chatBroker, registry operatorRegistry, write func(protocol.Message) error) {
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
}
list, err := owners(ctx)
if registry == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"})
return
}
title := ""
directory := ""
if request.ChatCreate != nil {
title = strings.TrimSpace(request.ChatCreate.Title)
directory = strings.TrimSpace(request.ChatCreate.Directory)
}
if title == "" {
title = "New chat"
}
resolved, err := resolveChatDirectory(directory)
if err != nil {
return nil, err
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
for _, owner := range list {
if owner.ID == "" {
continue
}
existing, ok := set[owner.ID]
if !ok {
set[owner.ID] = owner
continue
}
if owner.Active && !existing.Active {
existing.Active = true
}
if existing.Directory == "" {
existing.Directory = owner.Directory
}
set[owner.ID] = existing
session, err := broker.Create(ctx, title, resolved)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
return set, nil
if err := registry.RecordOperatorSession(ctx, session.ID, resolved, title, protocol.ChatSessionTagOperator); err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "session " + session.ID + " was created but could not be recorded: " + err.Error()})
return
}
session.Directory = resolved
session.Tag = protocol.ChatSessionTagOperator
response := protocol.Message{Type: protocol.ChatCreate, ID: request.ID, ChatSession: &session}
// The same registry row is echoed on the list field so a client can splice it
// into chat_sessions without a re-list.
response.ChatSessions = []protocol.ChatSession{session}
_ = write(response)
}
func handleChatPrompt(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, clientID string, write func(protocol.Message) error) {
// handleChatDirectories answers chat_directories with the operator's home first,
// then the directories of previous operator sessions most-recently-used first,
// deduplicated against the home directory and capped at operatorDirectoryLimit.
func handleChatDirectories(ctx context.Context, request protocol.Message, registry operatorRegistry, write func(protocol.Message) error) {
if registry == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "operator session registry is not configured on this server"})
return
}
home, err := OperatorHomeDirectory()
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
directories := []string{home}
seen := map[string]struct{}{home: {}}
recent, err := registry.OperatorDirectories(ctx, operatorDirectoryLimit)
if err != nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
for _, directory := range recent {
if len(directories) >= operatorDirectoryLimit {
break
}
if directory == "" {
continue
}
if _, ok := seen[directory]; ok {
continue
}
seen[directory] = struct{}{}
directories = append(directories, directory)
}
_ = write(protocol.Message{Type: protocol.ChatDirectories, ID: request.ID, ChatDirectories: directories})
}
// resolveChatDirectory resolves a chat_create directory to an absolute path: an
// omitted/empty value, "~" or a "~/..." value resolves to the operator's home
// (os.UserHomeDir) and never the server's working directory; any other value
// must be an absolute, existing directory, so a relative path cannot silently
// create a session relative to wherever the server was started.
func resolveChatDirectory(directory string) (string, error) {
directory = strings.TrimSpace(directory)
if directory == "" || directory == "~" || strings.HasPrefix(directory, "~/") {
return OperatorHomeDirectory()
}
if !filepath.IsAbs(directory) {
return "", errors.New("chat directory must be absolute or start with ~")
}
info, err := os.Stat(directory)
if err != nil {
return "", errors.New("chat directory " + directory + " is not available: " + err.Error())
}
if !info.IsDir() {
return "", errors.New("chat directory " + directory + " is not a directory")
}
return directory, nil
}
// OperatorHomeDirectory returns the operator's home directory, the default chat
// location. It is resolved through the standard library so it never falls back
// to the process working directory.
func OperatorHomeDirectory() (string, error) {
home, err := os.UserHomeDir()
if err != nil {
return "", errors.New("resolve operator home directory: " + err.Error())
}
if home == "" {
return "", errors.New("operator home directory is empty")
}
return home, nil
}
func handleChatPrompt(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, registry operatorRegistry, clientID string, write func(protocol.Message) error) {
if broker == nil {
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "chat is not configured on this server"})
return
@@ -612,6 +745,14 @@ func handleChatPrompt(ctx context.Context, request protocol.Message, broker chat
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
return
}
// Recency is one UPDATE: it moves the operator session to the front of
// chat_sessions. A session id that is not an operator session updates no row
// and is not an error, so pipeline stage prompts are unaffected.
if registry != nil {
if err := registry.TouchOperatorSession(ctx, sessionID); err != nil {
log.Printf("touch operator session %s: %v", sessionID, err)
}
}
// Acknowledge the accepted prompt with the echoed request id, then stream the
// reply. The prompt is sent exactly once and never re-sent on resume.
if err := write(protocol.Message{Type: protocol.ChatPrompt, ID: request.ID}); err != nil {
+120 -81
View File
@@ -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
+323 -30
View File
@@ -6,6 +6,8 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
@@ -20,6 +22,19 @@ import (
"git.a-wan.net/alan/pipelines/internal/protocol"
)
// newOperatorStore opens a scratch store for the operator-session registry, so
// the chat tests exercise the real SQL-backed registry (record, list, delete)
// rather than a second fake of it.
func newOperatorStore(t *testing.T) *pipeline.Store {
t.Helper()
store, err := pipeline.Open(filepath.Join(t.TempDir(), "operator.db"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { store.Close() })
return store
}
// fakeChat is a scripted chatBroker. It counts calls and either emits a fixed
// frame sequence or blocks until the caller's context is canceled.
type fakeChat struct {
@@ -51,6 +66,11 @@ type fakeChat struct {
subscribed bool
promptBeforeSubscribe bool
interruptErr error
createCalls int
createTitle string
createDirectory string
created protocol.ChatSession
createErr error
}
func (f *fakeChat) Prompt(context.Context, string, string) error {
@@ -168,6 +188,32 @@ func (f *fakeChat) sessionQuery() int {
return f.sessionCalls
}
// Create records the title and directory the handler forwarded and returns the
// seeded created session. A blank created id defaults to "ses_created" so a test
// only asserts what it cares about.
func (f *fakeChat) Create(_ context.Context, title, directory string) (protocol.ChatSession, error) {
f.mu.Lock()
defer f.mu.Unlock()
f.createCalls++
f.createTitle = title
f.createDirectory = directory
if f.createErr != nil {
return protocol.ChatSession{}, f.createErr
}
session := f.created
if session.ID == "" {
session.ID = "ses_created"
}
return session, nil
}
// createQuery reports the recorded chat_create call count, title and directory.
func (f *fakeChat) createQuery() (int, string, string) {
f.mu.Lock()
defer f.mu.Unlock()
return f.createCalls, f.createTitle, f.createDirectory
}
// replyQuery reports the recorded chat_reply count and the last forwarded
// request, so a test can assert the exact session/request/decision.
func (f *fakeChat) replyQuery() (int, protocol.ChatReplyRequest) {
@@ -965,38 +1011,214 @@ func TestChatPromptRejectsEmptyText(t *testing.T) {
}
}
// TestChatCreateAnswersExplicitly pins the declared-but-unbrokered boundary:
// chat_create is answered with a protocol error rather than falling through to
// the generic Pong. chat_sessions is now brokered and covered by
// TestChatSessionsListsOnlyOwned and TestChatSessionsBusyRejectsSecondClient.
func TestChatCreateAnswersExplicitly(t *testing.T) {
// TestChatCreateRecordsOperatorSession is the chat_create acceptance test: the
// broker creates the session in the resolved directory, the handler records it in
// the operator registry with the operator tag, and the response carries the
// created session and the registry row (directory, tag). The registry can then
// list it.
func TestChatCreateRecordsOperatorSession(t *testing.T) {
store := newOperatorStore(t)
broker := scriptedChat()
ctx, _, conn := dialChat(t, broker)
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatCreate, ID: "c1"}); err != nil {
broker.created = protocol.ChatSession{ID: "ses_new", Title: "demo"}
dir := t.TempDir()
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatCreate, ID: "c1", ChatCreate: &protocol.ChatCreateRequest{Title: "demo", Directory: dir}}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := wsjson.Read(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.Error || response.ID != "c1" {
t.Fatalf("chat_create did not answer with an explicit error: %+v", response)
if response.Type != protocol.ChatCreate || response.ID != "c1" || response.ChatSession == nil {
t.Fatalf("chat_create response = %+v", response)
}
if response.ChatSession.ID != "ses_new" || response.ChatSession.Directory != dir || response.ChatSession.Tag != protocol.ChatSessionTagOperator {
t.Fatalf("created session = %+v", response.ChatSession)
}
if len(response.ChatSessions) != 1 || response.ChatSessions[0].ID != "ses_new" || response.ChatSessions[0].Tag != protocol.ChatSessionTagOperator {
t.Fatalf("created session not echoed as a registry row: %+v", response.ChatSessions)
}
if calls, title, directory := broker.createQuery(); calls != 1 || title != "demo" || directory != dir {
t.Fatalf("broker.Create = %d/%q/%q, want 1/demo/%q", calls, title, directory, dir)
}
recorded, err := store.OperatorSessions(ctx)
if err != nil {
t.Fatal(err)
}
if len(recorded) != 1 || recorded[0].ID != "ses_new" || recorded[0].Directory != dir || recorded[0].Tag != protocol.ChatSessionTagOperator || recorded[0].Title != "demo" {
t.Fatalf("registry row = %+v", recorded)
}
}
// TestChatSessionsListsOnlyOwned is the task evidence for ownership: the broker
// lists two OpenCode sessions (one owned by this server, one foreign) and
// chat_sessions returns exactly the owned one, asking the broker once.
func TestChatSessionsListsOnlyOwned(t *testing.T) {
// TestChatCreateDirectoryResolution pins directory defaulting and rejection: an
// omitted/empty/"~"/"~/..." directory resolves to the operator's home (never the
// server's working directory), an explicit existing directory is recorded
// unchanged, and a relative or missing directory is rejected before any OpenCode
// call.
func TestChatCreateDirectoryResolution(t *testing.T) {
home, err := os.UserHomeDir()
if err != nil {
t.Fatal(err)
}
explicit := t.TempDir()
cases := []struct {
name string
omitted bool
directory string
wantDirectory string
wantErr string
}{
{name: "omitted", omitted: true, wantDirectory: home},
{name: "empty", directory: "", wantDirectory: home},
{name: "tilde", directory: "~", wantDirectory: home},
{name: "tilde path", directory: "~/project", wantDirectory: home},
{name: "explicit", directory: explicit, wantDirectory: explicit},
{name: "relative", directory: "relative/dir", wantErr: "absolute"},
{name: "missing", directory: filepath.Join(t.TempDir(), "missing"), wantErr: "not available"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
store := newOperatorStore(t)
broker := scriptedChat()
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
request := protocol.Message{Type: protocol.ChatCreate, ID: "c1"}
if !tc.omitted {
request.ChatCreate = &protocol.ChatCreateRequest{Title: "demo", Directory: tc.directory}
}
if err := wsjson.Write(ctx, conn, request); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := wsjson.Read(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if tc.wantErr != "" {
if response.Type != protocol.Error || !strings.Contains(response.Error, tc.wantErr) {
t.Fatalf("directory %q not rejected: %+v", tc.directory, response)
}
if calls, _, _ := broker.createQuery(); calls != 0 {
t.Fatalf("rejected directory still reached OpenCode: %d calls", calls)
}
if recorded, err := store.OperatorSessions(ctx); err != nil || len(recorded) != 0 {
t.Fatalf("rejected directory recorded a session: %+v %v", recorded, err)
}
return
}
if response.Type != protocol.ChatCreate || response.ChatSession == nil || response.ChatSession.Directory != tc.wantDirectory {
t.Fatalf("directory %q resolved wrong: %+v", tc.directory, response)
}
if calls, _, directory := broker.createQuery(); calls != 1 || directory != tc.wantDirectory {
t.Fatalf("broker.Create = %d/%q, want 1/%q", calls, directory, tc.wantDirectory)
}
})
}
}
// TestChatDirectoriesHomeThenBoundedRecents pins chat_directories: the operator's
// home first, then the directories of previous operator sessions
// most-recently-used first, deduplicated and capped at the fixed bound even with
// more directory history seeded than the cap.
func TestChatDirectoriesHomeThenBoundedRecents(t *testing.T) {
home, err := os.UserHomeDir()
if err != nil {
t.Fatal(err)
}
store := newOperatorStore(t)
ctx, _, conn := dialChatOptions(t, Options{OperatorRegistry: store})
// Seed more directories than the bound, oldest first.
seeded := make([]string, 0, operatorDirectoryLimit+4)
for i := 0; i < operatorDirectoryLimit+4; i++ {
directory := filepath.Join(t.TempDir(), fmt.Sprintf("d%02d", i))
seeded = append(seeded, directory)
if err := store.RecordOperatorSession(ctx, fmt.Sprintf("ses_%02d", i), directory, "s", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
time.Sleep(2 * time.Millisecond)
}
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatDirectories, ID: "d1"}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := wsjson.Read(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.ChatDirectories || response.ID != "d1" {
t.Fatalf("chat_directories response = %+v", response)
}
if len(response.ChatDirectories) != operatorDirectoryLimit {
t.Fatalf("directories = %+v, want the bound of %d", response.ChatDirectories, operatorDirectoryLimit)
}
if response.ChatDirectories[0] != home {
t.Fatalf("home not first: %+v", response.ChatDirectories)
}
// The remainder are the most recently recorded directories, MRU first.
for i := 1; i < len(response.ChatDirectories); i++ {
want := seeded[len(seeded)-i]
if response.ChatDirectories[i] != want {
t.Fatalf("directory %d = %q, want %q (MRU order): %+v", i, response.ChatDirectories[i], want, response.ChatDirectories)
}
}
}
// TestChatSessionsListsOperatorSessionsOnly is the task evidence for the
// operator-session filter: the broker lists an operator-registered session, a
// genuine pipeline stage session recorded in run_steps/run_session_owners, and a
// foreign session; chat_sessions returns exactly the operator session, carrying
// its registry directory and operator tag, and asks the broker once. The stage
// session is proven to be genuinely recorded through store.OwnedSessions.
func TestChatSessionsListsOperatorSessionsOnly(t *testing.T) {
ctx := context.Background()
store := newOperatorStore(t)
if err := store.RecordOperatorSession(ctx, "ses_op", "/work/op", "Operator chat", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
if _, err := store.Submit(ctx, "chatsrv", 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, "stage-run", "/work/stage", 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, "ses_stage", true, "v2", "/work/stage"); err != nil {
t.Fatal(err)
}
// Prove ses_stage is a real owned pipeline session, so its exclusion is the
// operator filter at work, not an unrecorded id.
owned, err := store.OwnedSessions(ctx)
if err != nil {
t.Fatal(err)
}
sawStage := false
for _, owner := range owned {
if owner.ID == "ses_stage" {
sawStage = true
}
if owner.ID == "ses_op" {
t.Fatalf("operator session leaked into pipeline ownership: %+v", owner)
}
}
if !sawStage {
t.Fatalf("stage session was not recorded in run_steps: %+v", owned)
}
broker := scriptedChat()
broker.sessions = []protocol.ChatSession{
{ID: "ses_owned", Title: "ours", Directory: "/work"},
{ID: "ses_op", Title: "upstream", Directory: "/upstream"},
{ID: "ses_stage", Title: "pipeline internal", Directory: "/work/stage"},
{ID: "ses_foreign", Title: "someone else's", Directory: "/elsewhere"},
}
owners := func(context.Context) ([]pipeline.SessionOwner, error) {
return []pipeline.SessionOwner{{ID: "ses_owned", Directory: "/work"}}, nil
}
ctx, _, conn := dialChatOptions(t, Options{Chat: broker, OwnedSessions: owners})
_, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatSessions, ID: "s1"}); err != nil {
t.Fatal(err)
}
@@ -1007,20 +1229,86 @@ func TestChatSessionsListsOnlyOwned(t *testing.T) {
if response.Type != protocol.ChatSessions || response.ID != "s1" {
t.Fatalf("chat_sessions response = %+v", response)
}
if len(response.ChatSessions) != 1 || response.ChatSessions[0].ID != "ses_owned" {
t.Fatalf("foreign session leaked into the list: %+v", response.ChatSessions)
if len(response.ChatSessions) != 1 || response.ChatSessions[0].ID != "ses_op" {
t.Fatalf("non-operator session leaked into the list: %+v", response.ChatSessions)
}
if response.ChatSessions[0].Busy {
t.Fatalf("inactive owned session marked busy: %+v", response.ChatSessions[0])
got := response.ChatSessions[0]
if got.Directory != "/work/op" || got.Tag != protocol.ChatSessionTagOperator {
t.Fatalf("operator session shape = %+v", got)
}
if got.Busy {
t.Fatalf("inactive operator session marked busy: %+v", got)
}
if calls := broker.sessionQuery(); calls != 1 {
t.Fatalf("broker.Sessions called %d times, want 1", calls)
}
}
// TestChatSessionsFailsClosedWithoutOwnership pins the default when no ownership
// source is available: chat_sessions returns an empty list, never the full
// OpenCode machine.
// TestChatSessionsNeverListsUntagged pins that a row with no recorded tag is not
// an operator session and is never listed, even when the broker still reports it.
func TestChatSessionsNeverListsUntagged(t *testing.T) {
ctx := context.Background()
store := newOperatorStore(t)
if err := store.RecordOperatorSession(ctx, "ses_untagged", "/work/untagged", "no tag", ""); err != nil {
t.Fatal(err)
}
broker := scriptedChat()
broker.sessions = []protocol.ChatSession{{ID: "ses_untagged"}}
_, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatSessions, ID: "s1"}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := wsjson.Read(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if response.Type != protocol.ChatSessions || len(response.ChatSessions) != 0 {
t.Fatalf("untagged session was listed: %+v", response)
}
}
// TestChatSessionsMostRecentlyUsedFirst pins the list order: sessions come back
// most-recently-used first by registry recency (later creation wins), with the
// directory and tag from the registry.
func TestChatSessionsMostRecentlyUsedFirst(t *testing.T) {
ctx := context.Background()
store := newOperatorStore(t)
if err := store.RecordOperatorSession(ctx, "ses_old", "/work/old", "old", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
time.Sleep(3 * time.Millisecond)
if err := store.RecordOperatorSession(ctx, "ses_new", "/work/new", "new", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
broker := scriptedChat()
// The broker lists them oldest-updated-first, so the handler's registry order
// is what decides the result.
broker.sessions = []protocol.ChatSession{
{ID: "ses_old", UpdatedAt: 100},
{ID: "ses_new", UpdatedAt: 50},
}
_, _, conn := dialChatOptions(t, Options{Chat: broker, OperatorRegistry: store})
if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatSessions, ID: "s1"}); err != nil {
t.Fatal(err)
}
var response protocol.Message
if err := wsjson.Read(ctx, conn, &response); err != nil {
t.Fatal(err)
}
if len(response.ChatSessions) != 2 {
t.Fatalf("chat_sessions = %+v, want 2", response.ChatSessions)
}
if response.ChatSessions[0].ID != "ses_new" || response.ChatSessions[1].ID != "ses_old" {
t.Fatalf("not most-recently-used first: %+v", response.ChatSessions)
}
if response.ChatSessions[0].Directory != "/work/new" || response.ChatSessions[0].Tag != protocol.ChatSessionTagOperator {
t.Fatalf("registry fields not applied: %+v", response.ChatSessions[0])
}
}
// TestChatSessionsFailsClosedWithoutOwnership pins the default when no operator
// registry (store or injection) is available: chat_sessions returns an empty list
// without consulting the broker, never the full OpenCode machine.
func TestChatSessionsFailsClosedWithoutOwnership(t *testing.T) {
broker := scriptedChat()
broker.sessions = []protocol.ChatSession{{ID: "ses_1"}, {ID: "ses_2"}}
@@ -1033,7 +1321,10 @@ func TestChatSessionsFailsClosedWithoutOwnership(t *testing.T) {
t.Fatal(err)
}
if response.Type != protocol.ChatSessions || len(response.ChatSessions) != 0 {
t.Fatalf("unowned sessions were exposed: %+v", response)
t.Fatalf("unregistered sessions were exposed: %+v", response)
}
if calls := broker.sessionQuery(); calls != 0 {
t.Fatalf("fail-closed list consulted the broker %d times, want 0", calls)
}
}
@@ -1043,14 +1334,16 @@ func TestChatSessionsFailsClosedWithoutOwnership(t *testing.T) {
// broker.Prompt call; after A disconnects the claim is released and a later list
// is no longer busy.
func TestChatSessionsBusyRejectsSecondClient(t *testing.T) {
ctx := context.Background()
store := newOperatorStore(t)
if err := store.RecordOperatorSession(ctx, "ses_1", "/work", "operator chat", protocol.ChatSessionTagOperator); err != nil {
t.Fatal(err)
}
broker := scriptedChat()
broker.block = true
broker.done = make(chan struct{})
broker.sessions = []protocol.ChatSession{{ID: "ses_1"}}
owners := func(context.Context) ([]pipeline.SessionOwner, error) {
return []pipeline.SessionOwner{{ID: "ses_1"}}, nil
}
ctx, _, conns := dialChatConns(t, Options{Chat: broker, OwnedSessions: owners}, 2)
ctx, _, conns := dialChatConns(t, Options{Chat: broker, OperatorRegistry: store}, 2)
connA, connB := conns[0], conns[1]
if err := wsjson.Write(ctx, connA, protocol.Message{Type: protocol.ChatPrompt, ID: "a1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: "ses_1", Text: "hi"}}); err != nil {
+13 -12
View File
@@ -27,11 +27,12 @@ type Options struct {
// chat_* request with a protocol error; a configured server passes an
// *opencode.Chat bound to the owned endpoint.
Chat chatBroker
// OwnedSessions reports the OpenCode sessions this server owns so
// chat_sessions is restricted to them. When nil, HandlerWithOptions derives
// it from the first store; when neither is available chat_sessions returns an
// empty list rather than the endpoint's whole session list.
OwnedSessions func(context.Context) ([]pipeline.SessionOwner, error)
// OperatorRegistry is the operator-session registry chat_sessions lists and
// chat_create/chat_directories read. When nil, HandlerWithOptions derives it
// from the first store; when neither is available chat_sessions returns an
// empty list (never the endpoint's whole session list) and chat_create is
// refused. Purely pipeline stage ownership is unaffected.
OperatorRegistry operatorRegistry
// RunTitles generates the operator-facing title stored with each new run.
// When nil, run creation stores the prompt-derived fallback without a model
// call, which keeps tests and unconfigured servers deterministic.
@@ -78,12 +79,12 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
go runs.scheduleLoop()
}
}
// chat_sessions is restricted to the sessions this server owns. Prefer an
// injected source; otherwise derive it from the store. With neither, the
// chat_sessions lists the operator's own recorded sessions. Prefer an
// injected registry; otherwise derive it from the store. With neither, the
// handler fails closed to an empty list.
owned := options.OwnedSessions
if owned == nil && len(stores) > 0 {
owned = stores[0].OwnedSessions
registry := options.OperatorRegistry
if registry == nil && len(stores) > 0 {
registry = stores[0]
}
mux.HandleFunc("GET /artifacts/{run}/{step}/{name...}", func(w http.ResponseWriter, r *http.Request) {
if runs == nil {
@@ -154,7 +155,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
return
}
log.Printf("command received: client=%s type=%q id=%q pipeline=%d version=%d", r.RemoteAddr, request.Type, request.ID, request.PipelineID, request.Version)
if handleChat(connCtx, request, options.Chat, hub, owned, clientID, write) {
if handleChat(connCtx, request, options.Chat, hub, registry, clientID, write) {
continue
}
if handleRunEvents(request, events, runs, clientID, write) {
@@ -243,7 +244,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
response.Error = err.Error()
}
}
} else if runs != nil && (request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun || request.Type == protocol.ListRuns || request.Type == protocol.GetRun || request.Type == protocol.CancelRun || request.Type == protocol.DecideRun || request.Type == protocol.SetRunTitle || request.Type == protocol.GetRunEvidence) {
} else if runs != nil && (request.Type == protocol.RunPipeline || request.Type == protocol.RetryRun || request.Type == protocol.ListRuns || request.Type == protocol.GetRun || request.Type == protocol.CancelRun || request.Type == protocol.DecideRun || request.Type == protocol.SetRunTitle || request.Type == protocol.GetRunEvidence) {
var err error
switch request.Type {
case protocol.GetRunEvidence: