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
+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)
}
}