chat: give the operator their own sessions, separate from run sessions

chat_sessions used to list exactly the OpenCode sessions the pipelines own,
filtered through pipeline.SessionOwner (run_steps/run_session_owners), so an
operator-created chat was an accident of the run-ownership table and the
operator's working sessions were mixed with the internals of running pipelines.

Add a first-class operator session:

- internal/pipeline/operator_sessions.go: a dedicated operator_sessions table
  (session_id PK, directory, title, tag, created_at, last_used_at) with
  RecordOperatorSession (upsert preserving created_at), a one-statement
  TouchOperatorSession (no-op for a non-operator id), OperatorSessions bounded to
  128 rows most-recently-used first, and OperatorDirectories (bounded MRU,
  deduped). run_steps/run_session_owners and SessionOwner/OwnedSessions are
  untouched.
- internal/server/chat.go: chat_sessions returns operator sessions only - it
  reads the registry once, intersects it with one broker session-list call, fills
  directory/tag/created/updated from the registry, sets busy for a session held
  by another connection, keeps MRU order, and stays fail-closed (empty list, never
  the endpoint's whole list). A pipeline stage session and an untagged row can
  never be listed. chat_create resolves the directory (empty/'~'/'~/...' ->
  operator home; relative or missing rejected before any OpenCode call), calls
  broker.Create(title, resolved) and records the row with the operator tag.
  chat_directories returns home first then bounded (8) deduped recents. A
  successful prompt touches the operator session without affecting pipeline
  prompts.
- internal/opencode, internal/protocol, internal/server/websocket.go and
  message.go: unscoped session list, Create posts {"title":...,
  "location":{"directory":...}} to POST /api/session, additive protocol fields
  (omitempty) and the ChatDirectories request/response, plus the OperatorRegistry
  option defaulting to the first store. One OpenCode transport per server is
  unchanged.

Docs and references: docs/cli.md gains a "Chat sessions" section documenting the
operator-only fail-closed list, directory defaulting/rejection, chat_directories
(home first, deduped, bounded 8) and why a dedicated request was chosen.
internal/author/skill/references/operations.md's stale "Brokered OpenCode chat"
text (chat_create unbrokered, run_steps filtering, "listed the owned scratch
session" evidence) is rewritten to the operator-session registry, the fail-closed
filter, directory defaulting and the corrected live-test wording. No new
reference file, so internal/author/install.go's inventory is unchanged.

Checked for staleness elsewhere: README.md, the docs/ topic pages and the
packaged server/TUI templates carry no chat-session ownership claims, so none
needed updating.

Verified: gofmt -l clean; go vet and go build ./... clean; go test -count=1
./internal/pipeline/... ./internal/protocol/... ./internal/opencode/...
./internal/server/... and the full go test -count=1 ./... pass.
This commit is contained in:
2026-10-17 11:14:25 +01:00
parent 8692561bc7
commit 18398dfa5b
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