feat(chat): per-chat permission modes for Claude, Codex and OpenCode

This commit is contained in:
2026-10-24 18:16:10 +01:00
parent ef575898f2
commit 6d430948a6
29 changed files with 1512 additions and 3 deletions
+58
View File
@@ -0,0 +1,58 @@
package pipeline
import (
"context"
"database/sql"
"path/filepath"
"testing"
)
func TestOperatorChatModeMigratesAndPersists(t *testing.T) {
ctx := context.Background()
path := filepath.Join(t.TempDir(), "old.db")
db, err := sql.Open("sqlite", path)
if err != nil {
t.Fatal(err)
}
_, err = db.Exec(`CREATE TABLE 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)`)
if err != nil {
t.Fatal(err)
}
db.Close()
store, err := Open(path)
if err != nil {
t.Fatal(err)
}
if err := store.RecordOperatorSession(ctx, "chat", "/fixture", "title", "operator"); err != nil {
t.Fatal(err)
}
if mode, err := store.ChatMode(ctx, "chat"); err != nil || mode != "" {
t.Fatalf("fresh mode %q: %v", mode, err)
}
if err := store.SetChatMode(ctx, "missing", "auto"); err == nil {
t.Fatal("unregistered chat accepted a mode")
}
before, _, err := store.OperatorSession(ctx, "chat")
if err != nil {
t.Fatal(err)
}
if err := store.SetChatMode(ctx, "chat", "auto"); err != nil {
t.Fatal(err)
}
store.Close()
store, err = Open(path)
if err != nil {
t.Fatal(err)
}
defer store.Close()
if mode, err := store.ChatMode(ctx, "chat"); err != nil || mode != "auto" {
t.Fatalf("restored mode %q: %v", mode, err)
}
if mode, err := store.ChatMode(ctx, "missing"); err != nil || mode != "" {
t.Fatalf("missing chat mode %q: %v", mode, err)
}
after, _, err := store.OperatorSession(ctx, "chat")
if err != nil || before.UpdatedAt != after.UpdatedAt {
t.Fatal("mode changed recency", err)
}
}
+29 -1
View File
@@ -139,7 +139,7 @@ func (s *Store) AdvanceChatCreate(ctx context.Context, requestID, from, to strin
// row valid and open (0), so an upgrade is idempotent across repeated Open calls
// and no session is silently reported as closed.
func (s *Store) migrateOperatorSessions() error {
for _, column := range []string{"chat_model", "chat_model_provider"} {
for _, column := range []string{"chat_model", "chat_model_provider", "chat_mode"} {
if err := s.addColumnIfMissing("operator_sessions", column, "TEXT NOT NULL DEFAULT ''"); err != nil {
return err
}
@@ -183,6 +183,34 @@ func (s *Store) SetChatModelSelection(ctx context.Context, id, provider, model s
return nil
}
// SetChatMode stores one operator chat's permission mode. The harness validates
// the mode id; this only records it for later turns and restarts.
func (s *Store) SetChatMode(ctx context.Context, id, mode string) error {
result, err := s.db.ExecContext(ctx, `UPDATE operator_sessions SET chat_mode=? WHERE session_id=? AND tag<>''`, mode, id)
if err != nil {
return err
}
count, err := result.RowsAffected()
if err != nil {
return err
}
if count != 1 {
return errors.New("a mode can only be set on a registered operator chat")
}
return nil
}
// ChatMode reads one operator chat's stored mode without touching recency; ""
// when none was chosen or the session is not an operator chat.
func (s *Store) ChatMode(ctx context.Context, id string) (string, error) {
var mode string
err := s.db.QueryRowContext(ctx, `SELECT chat_mode FROM operator_sessions WHERE session_id=? AND tag<>''`, id).Scan(&mode)
if errors.Is(err, sql.ErrNoRows) {
return "", nil
}
return mode, err
}
// ChatModelSelection reads a durable next-turn choice without touching recency.
func (s *Store) ChatModelSelection(ctx context.Context, id string) (provider, model string, err error) {
err = s.db.QueryRowContext(ctx, `SELECT chat_model_provider,chat_model FROM operator_sessions WHERE session_id=? AND backend IN ('codex','claude') AND tag<>''`, id).Scan(&provider, &model)