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
+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.