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:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user