18398dfa5b
chat_sessions used to list exactly the OpenCode sessions the pipelines own,
filtered through pipeline.SessionOwner (run_steps/run_session_owners), so an
operator-created chat was an accident of the run-ownership table and the
operator's working sessions were mixed with the internals of running pipelines.
Add a first-class operator session:
- internal/pipeline/operator_sessions.go: a dedicated operator_sessions table
(session_id PK, directory, title, tag, created_at, last_used_at) with
RecordOperatorSession (upsert preserving created_at), a one-statement
TouchOperatorSession (no-op for a non-operator id), OperatorSessions bounded to
128 rows most-recently-used first, and OperatorDirectories (bounded MRU,
deduped). run_steps/run_session_owners and SessionOwner/OwnedSessions are
untouched.
- internal/server/chat.go: chat_sessions returns operator sessions only - it
reads the registry once, intersects it with one broker session-list call, fills
directory/tag/created/updated from the registry, sets busy for a session held
by another connection, keeps MRU order, and stays fail-closed (empty list, never
the endpoint's whole list). A pipeline stage session and an untagged row can
never be listed. chat_create resolves the directory (empty/'~'/'~/...' ->
operator home; relative or missing rejected before any OpenCode call), calls
broker.Create(title, resolved) and records the row with the operator tag.
chat_directories returns home first then bounded (8) deduped recents. A
successful prompt touches the operator session without affecting pipeline
prompts.
- internal/opencode, internal/protocol, internal/server/websocket.go and
message.go: unscoped session list, Create posts {"title":...,
"location":{"directory":...}} to POST /api/session, additive protocol fields
(omitempty) and the ChatDirectories request/response, plus the OperatorRegistry
option defaulting to the first store. One OpenCode transport per server is
unchanged.
Docs and references: docs/cli.md gains a "Chat sessions" section documenting the
operator-only fail-closed list, directory defaulting/rejection, chat_directories
(home first, deduped, bounded 8) and why a dedicated request was chosen.
internal/author/skill/references/operations.md's stale "Brokered OpenCode chat"
text (chat_create unbrokered, run_steps filtering, "listed the owned scratch
session" evidence) is rewritten to the operator-session registry, the fail-closed
filter, directory defaulting and the corrected live-test wording. No new
reference file, so internal/author/install.go's inventory is unchanged.
Checked for staleness elsewhere: README.md, the docs/ topic pages and the
packaged server/TUI templates carry no chat-session ownership claims, so none
needed updating.
Verified: gofmt -l clean; go vet and go build ./... clean; go test -count=1
./internal/pipeline/... ./internal/protocol/... ./internal/opencode/...
./internal/server/... and the full go test -count=1 ./... pass.
111 lines
4.8 KiB
Go
111 lines
4.8 KiB
Go
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()
|
|
}
|