feat(chat): per-chat permission modes for Claude, Codex and OpenCode
This commit is contained in:
@@ -203,6 +203,12 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker
|
||||
case protocol.ChatModelSelect:
|
||||
handleChatModelSwitch(ctx, request, broker, write)
|
||||
return true
|
||||
case protocol.ChatModes:
|
||||
handleChatModes(ctx, request, broker, write)
|
||||
return true
|
||||
case protocol.ChatModeSelect:
|
||||
handleChatModeSwitch(ctx, request, broker, write)
|
||||
return true
|
||||
case protocol.ChatShells:
|
||||
handleChatShells(ctx, request, broker, registry, write)
|
||||
return true
|
||||
|
||||
@@ -179,6 +179,9 @@ func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.applyStoredMode(ctx, adapter, sessionID); err != nil {
|
||||
return err
|
||||
}
|
||||
if isModelBackend(m.backendFor(ctx, sessionID)) {
|
||||
model, err := m.promptModel(ctx, adapter, sessionID)
|
||||
if err != nil {
|
||||
@@ -231,6 +234,9 @@ func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, t
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.applyStoredMode(ctx, adapter, sessionID); err != nil {
|
||||
return err
|
||||
}
|
||||
if isModelBackend(m.backendFor(ctx, sessionID)) {
|
||||
model, err := m.promptModel(ctx, adapter, sessionID)
|
||||
if err != nil {
|
||||
@@ -349,6 +355,7 @@ func (m *chatManager) chatModelSelection(ctx context.Context, id string) (string
|
||||
}
|
||||
|
||||
func (m *chatManager) withModelSelection(ctx context.Context, session protocol.ChatSession) (protocol.ChatSession, error) {
|
||||
session = m.withMode(ctx, session)
|
||||
if !isModelBackend(session.Backend) {
|
||||
return session, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// chatModer is the optional permission-mode surface of a chat harness.
|
||||
type chatModer interface {
|
||||
Modes(ctx context.Context, sessionID string) ([]protocol.ChatMode, error)
|
||||
SetMode(ctx context.Context, sessionID, mode string) error
|
||||
}
|
||||
|
||||
// chatModeRegistry stores each operator chat's chosen mode.
|
||||
type chatModeRegistry interface {
|
||||
SetChatMode(ctx context.Context, id, mode string) error
|
||||
ChatMode(ctx context.Context, id string) (string, error)
|
||||
}
|
||||
|
||||
var errChatModesUnsupported = errors.New("this chat harness does not support changing modes")
|
||||
|
||||
// Modes lists the modes the session's harness offers.
|
||||
func (m *chatManager) Modes(ctx context.Context, sessionID string) ([]protocol.ChatMode, error) {
|
||||
adapter, err := m.brokerFor(ctx, sessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
moder, ok := adapter.(chatModer)
|
||||
if !ok {
|
||||
return nil, errChatModesUnsupported
|
||||
}
|
||||
return moder.Modes(ctx, sessionID)
|
||||
}
|
||||
|
||||
// SetMode applies a mode to the harness now and stores it, so it survives a
|
||||
// restart and is re-applied before every later turn.
|
||||
func (m *chatManager) SetMode(ctx context.Context, sessionID, mode string) error {
|
||||
adapter, err := m.brokerFor(ctx, sessionID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
moder, ok := adapter.(chatModer)
|
||||
if !ok {
|
||||
return errChatModesUnsupported
|
||||
}
|
||||
registry, ok := m.registry.(chatModeRegistry)
|
||||
if !ok {
|
||||
return errors.New("durable chat modes are not configured")
|
||||
}
|
||||
if err := moder.SetMode(ctx, sessionID, mode); err != nil {
|
||||
return err
|
||||
}
|
||||
return registry.SetChatMode(ctx, sessionID, mode)
|
||||
}
|
||||
|
||||
// CurrentMode is the session's stored mode, "" for the harness default.
|
||||
func (m *chatManager) CurrentMode(ctx context.Context, sessionID string) string {
|
||||
registry, ok := m.registry.(chatModeRegistry)
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
mode, err := registry.ChatMode(ctx, sessionID)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return mode
|
||||
}
|
||||
|
||||
// withMode names the session's mode on a listed row: the stored mode, else the
|
||||
// harness default (listed first). A harness without modes leaves both empty.
|
||||
func (m *chatManager) withMode(ctx context.Context, session protocol.ChatSession) protocol.ChatSession {
|
||||
moder, ok := m.adapter(session.Backend).(chatModer)
|
||||
if !ok {
|
||||
return session
|
||||
}
|
||||
modes, err := moder.Modes(ctx, session.ID)
|
||||
if err != nil || len(modes) == 0 {
|
||||
return session
|
||||
}
|
||||
stored := m.CurrentMode(ctx, session.ID)
|
||||
chosen := modes[0]
|
||||
for _, mode := range modes {
|
||||
if mode.ID == stored {
|
||||
chosen = mode
|
||||
}
|
||||
}
|
||||
session.Mode, session.ModeName = chosen.ID, chosen.Name
|
||||
return session
|
||||
}
|
||||
|
||||
// applyStoredMode hands the stored mode to the harness before a turn. Adapters
|
||||
// treat a repeat as a no-op, and this covers a server restart that lost the
|
||||
// adapter's in-memory choice.
|
||||
func (m *chatManager) applyStoredMode(ctx context.Context, adapter chatBroker, sessionID string) error {
|
||||
moder, ok := adapter.(chatModer)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
mode := m.CurrentMode(ctx, sessionID)
|
||||
if mode == "" {
|
||||
return nil
|
||||
}
|
||||
return moder.SetMode(ctx, sessionID, mode)
|
||||
}
|
||||
|
||||
// handleChatModes lists a session's modes and reports the current one.
|
||||
func handleChatModes(ctx context.Context, request protocol.Message, broker chatBroker, write func(protocol.Message) error) {
|
||||
query := request.ChatQuery
|
||||
if query == nil || !validChatSessionID(query.SessionID) {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid chat session id is required"})
|
||||
return
|
||||
}
|
||||
moder, ok := broker.(chatModer)
|
||||
if !ok {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: errChatModesUnsupported.Error()})
|
||||
return
|
||||
}
|
||||
modes, err := moder.Modes(ctx, query.SessionID)
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
current := protocol.ChatModeRequest{SessionID: query.SessionID}
|
||||
if reporter, ok := broker.(interface {
|
||||
CurrentMode(context.Context, string) string
|
||||
}); ok {
|
||||
current.Mode = reporter.CurrentMode(ctx, query.SessionID)
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.ChatModes, ID: request.ID, ChatModes: modes, ModeRequest: ¤t})
|
||||
}
|
||||
|
||||
// handleChatModeSwitch changes one session's mode and echoes the stored mode.
|
||||
func handleChatModeSwitch(ctx context.Context, request protocol.Message, broker chatBroker, write func(protocol.Message) error) {
|
||||
body := request.ModeRequest
|
||||
if body == nil || !validChatSessionID(body.SessionID) {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid chat session id is required"})
|
||||
return
|
||||
}
|
||||
mode := strings.TrimSpace(body.Mode)
|
||||
if mode == "" {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a mode is required"})
|
||||
return
|
||||
}
|
||||
moder, ok := broker.(chatModer)
|
||||
if !ok {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: errChatModesUnsupported.Error()})
|
||||
return
|
||||
}
|
||||
if err := moder.SetMode(ctx, body.SessionID, mode); err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()})
|
||||
return
|
||||
}
|
||||
reply := protocol.ChatModeRequest{SessionID: body.SessionID, Mode: mode}
|
||||
_ = write(protocol.Message{Type: protocol.ChatModeSelect, ID: request.ID, ModeRequest: &reply})
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// modedChat records the modes the manager hands to the harness.
|
||||
type modedChat struct {
|
||||
*fakeChat
|
||||
applied []string
|
||||
}
|
||||
|
||||
func (c *modedChat) Modes(context.Context, string) ([]protocol.ChatMode, error) {
|
||||
return []protocol.ChatMode{{ID: "default", Name: "Ask"}, {ID: "auto", Name: "Auto"}}, nil
|
||||
}
|
||||
|
||||
func (c *modedChat) SetMode(_ context.Context, _, mode string) error {
|
||||
if mode != "default" && mode != "auto" {
|
||||
return errors.New("unknown mode " + mode)
|
||||
}
|
||||
c.applied = append(c.applied, mode)
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestChatModeIsStoredAppliedAndSurvivesRestart(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "chat.db")
|
||||
store, err := pipeline.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
id := "01900000-0000-7000-8000-000000000002"
|
||||
if err := store.RecordOperatorSession(ctx, id, "/fixture", "chat", protocol.ChatSessionTagOperator); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.SetOperatorSessionBackend(ctx, id, "claude"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fake := &modedChat{fakeChat: scriptedChat()}
|
||||
fake.sessionRecord = protocol.ChatSession{ID: id, Backend: "claude"}
|
||||
fake.sessionFound = true
|
||||
fake.sessions = []protocol.ChatSession{fake.sessionRecord}
|
||||
manager := &chatManager{claude: fake, registry: store, cache: map[string]string{}}
|
||||
var reply protocol.Message
|
||||
capture := func(msg protocol.Message) error { reply = msg; return nil }
|
||||
|
||||
if session, _, err := manager.Session(ctx, id); err != nil || session.Mode != "default" || session.ModeName != "Ask" {
|
||||
t.Fatalf("fresh session names %q/%q, want the harness default: %v", session.Mode, session.ModeName, err)
|
||||
}
|
||||
handleChatModes(ctx, protocol.Message{ID: "list", ChatQuery: &protocol.ChatQuery{SessionID: id}}, manager, capture)
|
||||
if reply.Type != protocol.ChatModes || len(reply.ChatModes) != 2 || reply.ModeRequest == nil || reply.ModeRequest.Mode != "" {
|
||||
t.Fatalf("modes reply = %+v", reply)
|
||||
}
|
||||
handleChatModeSwitch(ctx, protocol.Message{ID: "bad", ModeRequest: &protocol.ChatModeRequest{SessionID: id, Mode: "yolo"}}, manager, capture)
|
||||
if reply.Type != protocol.Error {
|
||||
t.Fatalf("unknown mode accepted: %+v", reply)
|
||||
}
|
||||
handleChatModeSwitch(ctx, protocol.Message{ID: "auto", ModeRequest: &protocol.ChatModeRequest{SessionID: id, Mode: "auto"}}, manager, capture)
|
||||
if reply.Type != protocol.ChatModeSelect || reply.ModeRequest.Mode != "auto" || len(fake.applied) != 1 {
|
||||
t.Fatalf("switch reply = %+v, applied %v", reply, fake.applied)
|
||||
}
|
||||
if err := store.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store, err = pipeline.Open(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
// A restarted server forgot the harness state; the stored mode is listed and
|
||||
// re-applied before the next turn.
|
||||
fake.applied = nil
|
||||
manager = &chatManager{claude: fake, registry: store, cache: map[string]string{}}
|
||||
session, found, err := manager.Session(ctx, id)
|
||||
if err != nil || !found || session.Mode != "auto" || session.ModeName != "Auto" {
|
||||
t.Fatalf("restarted session = %+v, %v", session, err)
|
||||
}
|
||||
list, err := manager.Sessions(ctx)
|
||||
if err != nil || len(list) != 1 || list[0].Mode != "auto" {
|
||||
t.Fatalf("restarted list = %+v, %v", list, err)
|
||||
}
|
||||
handleChatModes(ctx, protocol.Message{ID: "again", ChatQuery: &protocol.ChatQuery{SessionID: id}}, manager, capture)
|
||||
if reply.ModeRequest == nil || reply.ModeRequest.Mode != "auto" {
|
||||
t.Fatalf("current mode after restart = %+v", reply)
|
||||
}
|
||||
if err := manager.Prompt(ctx, id, "hello"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := manager.PromptWithMessageIDFiles(ctx, id, "again", "msg", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(fake.applied) != 2 || fake.applied[0] != "auto" || fake.applied[1] != "auto" {
|
||||
t.Fatalf("stored mode not re-applied before turns: %v", fake.applied)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user