feat(chat): per-chat permission modes for Claude, Codex and OpenCode
This commit is contained in:
@@ -67,6 +67,7 @@ type Chat struct {
|
||||
endedTurns map[string]string
|
||||
usage map[string]usageSnapshot
|
||||
usageOrder uint64
|
||||
modes map[string]string
|
||||
}
|
||||
|
||||
type pendingForm struct {
|
||||
@@ -407,6 +408,7 @@ func (c *Chat) PromptWithMessageIDModelFiles(ctx context.Context, id, text, mess
|
||||
if model != "" {
|
||||
params["model"] = model
|
||||
}
|
||||
c.applyMode(id, params)
|
||||
c.mu.Lock()
|
||||
if c.starting == nil {
|
||||
c.starting = map[string]bool{}
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package codex
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
// codexMode is one approval/sandbox preset, applied as turn/start overrides
|
||||
// that Codex keeps for the thread's later turns too.
|
||||
type codexMode struct {
|
||||
protocol.ChatMode
|
||||
approval string
|
||||
sandbox string
|
||||
}
|
||||
|
||||
// The presets mirror Codex's own: ask in the workspace sandbox (the thread
|
||||
// default chats start with), act without asking inside it, or full access.
|
||||
var codexModes = []codexMode{
|
||||
{protocol.ChatMode{ID: "ask", Name: "Ask for approval", Description: "Work in the project sandbox and ask when an action needs more."}, "on-request", "workspaceWrite"},
|
||||
{protocol.ChatMode{ID: "auto", Name: "Auto", Description: "Work in the project sandbox without asking."}, "never", "workspaceWrite"},
|
||||
{protocol.ChatMode{ID: "full", Name: "Full access", Description: "No sandbox and no approvals. Everything runs."}, "never", "dangerFullAccess"},
|
||||
}
|
||||
|
||||
// Modes lists the approval/sandbox presets a Codex chat can switch to.
|
||||
func (c *Chat) Modes(context.Context, string) ([]protocol.ChatMode, error) {
|
||||
modes := make([]protocol.ChatMode, len(codexModes))
|
||||
for index, mode := range codexModes {
|
||||
modes[index] = mode.ChatMode
|
||||
}
|
||||
return modes, nil
|
||||
}
|
||||
|
||||
// SetMode records the thread's preset; it is sent with the next turn/start.
|
||||
func (c *Chat) SetMode(_ context.Context, id, mode string) error {
|
||||
if _, ok := codexModeFor(mode); !ok {
|
||||
return fmt.Errorf("unknown Codex mode %q", mode)
|
||||
}
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
if c.modes == nil {
|
||||
c.modes = map[string]string{}
|
||||
}
|
||||
c.modes[id] = mode
|
||||
return nil
|
||||
}
|
||||
|
||||
func codexModeFor(id string) (codexMode, bool) {
|
||||
for _, mode := range codexModes {
|
||||
if mode.ID == id {
|
||||
return mode, true
|
||||
}
|
||||
}
|
||||
return codexMode{}, false
|
||||
}
|
||||
|
||||
// applyMode adds the thread's preset to turn/start params, if one was chosen.
|
||||
func (c *Chat) applyMode(id string, params map[string]any) {
|
||||
c.mu.Lock()
|
||||
chosen := c.modes[id]
|
||||
c.mu.Unlock()
|
||||
mode, ok := codexModeFor(chosen)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
params["approvalPolicy"] = mode.approval
|
||||
params["sandboxPolicy"] = map[string]any{"type": mode.sandbox}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package codex
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestModePresetRidesOnTheNextTurn(t *testing.T) {
|
||||
var mu sync.Mutex
|
||||
var turns []map[string]any
|
||||
chat := modelTestChat(t, func(method string, raw json.RawMessage) any {
|
||||
if method != "turn/start" {
|
||||
t.Errorf("unexpected operation: %s", method)
|
||||
return map[string]any{}
|
||||
}
|
||||
var params map[string]any
|
||||
_ = json.Unmarshal(raw, ¶ms)
|
||||
mu.Lock()
|
||||
turns = append(turns, params)
|
||||
mu.Unlock()
|
||||
return json.RawMessage(`{"turn":{"id":"turn-fixture","status":"inProgress","items":[]}}`)
|
||||
})
|
||||
ctx := context.Background()
|
||||
if err := chat.SetMode(ctx, fixtureThread, "yolo"); err == nil {
|
||||
t.Fatal("an unlisted mode was accepted")
|
||||
}
|
||||
if err := chat.Prompt(ctx, fixtureThread, "plain"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := chat.SetMode(ctx, fixtureThread, "auto"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := chat.Prompt(ctx, fixtureThread, "auto"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := chat.SetMode(ctx, fixtureThread, "full"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := chat.Prompt(ctx, fixtureThread, "full"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if len(turns) != 3 {
|
||||
t.Fatalf("turns = %d", len(turns))
|
||||
}
|
||||
if turns[0]["approvalPolicy"] != nil || turns[0]["sandboxPolicy"] != nil {
|
||||
t.Fatalf("no mode chosen, yet overrides were sent: %v", turns[0])
|
||||
}
|
||||
sandbox := func(turn map[string]any) any { return turn["sandboxPolicy"].(map[string]any)["type"] }
|
||||
if turns[1]["approvalPolicy"] != "never" || sandbox(turns[1]) != "workspaceWrite" {
|
||||
t.Fatalf("auto turn = %v", turns[1])
|
||||
}
|
||||
if turns[2]["approvalPolicy"] != "never" || sandbox(turns[2]) != "dangerFullAccess" {
|
||||
t.Fatalf("full turn = %v", turns[2])
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user