From 06001697c8b34add12a79b874b3413007c5493c1 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Mon, 9 Nov 2026 20:07:41 +0000 Subject: [PATCH] Add per-chat thinking levels for Codex, Claude Code and OpenCode Codex: model/list supportedReasoningEfforts per model, turn/start effort, thread reasoningEffort as the current level. Claude Code: initialize supportedEffortLevels, apply_flag_settings effortLevel at the next turn start. OpenCode v2: model variants via the session model endpoint. chat_effort sets a level; rows and chat_models carry the levels; Codex and Claude levels persist in operator_sessions.chat_effort and are re-applied before every turn. --- internal/claude/chat.go | 3 + internal/claude/claude.go | 6 + internal/claude/efforts.go | 113 +++++++++++ internal/claude/efforts_test.go | 88 ++++++++ internal/claude/hosted.go | 1 + internal/claude/process.go | 16 +- internal/claude/testdata/fake-claude.py | 12 +- internal/codex/chat.go | 9 + internal/codex/efforts.go | 59 ++++++ internal/codex/efforts_test.go | 89 +++++++++ internal/codex/models.go | 73 ++++++- internal/opencode/chat.go | 18 +- internal/opencode/efforts.go | 86 ++++++++ internal/opencode/efforts_test.go | 96 +++++++++ internal/pipeline/operator_efforts_test.go | 78 ++++++++ internal/pipeline/operator_sessions.go | 35 +++- internal/protocol/chat.go | 13 ++ internal/protocol/chat_effort.go | 53 +++++ internal/protocol/chat_effort_test.go | 65 ++++++ internal/protocol/message.go | 3 + internal/server/chat.go | 3 + internal/server/chat_efforts.go | 221 +++++++++++++++++++++ internal/server/chat_efforts_test.go | 170 ++++++++++++++++ internal/server/chat_manager.go | 17 +- internal/server/chat_stream.go | 2 +- 25 files changed, 1306 insertions(+), 23 deletions(-) create mode 100644 internal/claude/efforts.go create mode 100644 internal/claude/efforts_test.go create mode 100644 internal/codex/efforts.go create mode 100644 internal/codex/efforts_test.go create mode 100644 internal/opencode/efforts.go create mode 100644 internal/opencode/efforts_test.go create mode 100644 internal/pipeline/operator_efforts_test.go create mode 100644 internal/protocol/chat_effort.go create mode 100644 internal/protocol/chat_effort_test.go create mode 100644 internal/server/chat_efforts.go create mode 100644 internal/server/chat_efforts_test.go diff --git a/internal/claude/chat.go b/internal/claude/chat.go index 15b20d23..1de1ada3 100644 --- a/internal/claude/chat.go +++ b/internal/claude/chat.go @@ -177,6 +177,9 @@ func (c *Chat) PromptWithMessageIDModelFiles(ctx context.Context, id, text, mess if err := s.applyMode(ctx, proc); err != nil { return err } + if err := s.applyEffort(ctx, proc); err != nil { + return err + } content, err := promptContent(text, files) if err != nil { return err diff --git a/internal/claude/claude.go b/internal/claude/claude.go index 09d93cf0..ff58d953 100644 --- a/internal/claude/claude.go +++ b/internal/claude/claude.go @@ -116,6 +116,11 @@ type Chat struct { // quotas is the latest rate_limit_event quota per account (see quota.go). quotaMu sync.Mutex quotas map[string]*protocol.ChatAccountUsage + + // catalog is the latest initialize model list of any session (efforts.go), + // so a listed row names its levels without starting a CLI. + catalogMu sync.Mutex + catalog []protocol.ChatModel } func New(ctx context.Context) *Chat { @@ -319,6 +324,7 @@ type session struct { title string model string // CLI model selection (alias or full id); "" uses the CLI default mode string // permission mode id; "" uses the CLI's own default + effort string // thinking level for the next turn; "" uses the CLI default createdAt int64 // toolsOff is set after a spawn carrying the Pipelines tools failed, so // the chat keeps starting without them (chattools). diff --git a/internal/claude/efforts.go b/internal/claude/efforts.go new file mode 100644 index 00000000..2daedf20 --- /dev/null +++ b/internal/claude/efforts.go @@ -0,0 +1,113 @@ +package claude + +import ( + "context" + "errors" + "fmt" + "strings" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// effortLevels turns an initialize model's effort support into levels. A +// model without supportsEffort has none, whatever else it lists. +func effortLevels(supported bool, levels []string) []protocol.ChatEffort { + if !supported { + return nil + } + var efforts []protocol.ChatEffort + for _, level := range levels { + level = strings.TrimSpace(level) + if level != "" && !protocol.HasChatEffort(efforts, level) { + efforts = append(efforts, protocol.ChatEffort{ID: level, Name: protocol.ChatEffortName(level)}) + } + } + return efforts +} + +// rememberCatalog keeps the newest initialize model list for listing rows. +func (c *Chat) rememberCatalog(models []protocol.ChatModel) { + if len(models) == 0 { + return + } + c.catalogMu.Lock() + c.catalog = append([]protocol.ChatModel(nil), models...) + c.catalogMu.Unlock() +} + +// Efforts reports the levels Claude Code's initialize catalog lists for the +// row's model: its selection, the model a reply reported, or the CLI default +// ("default"). Before any CLI of this server initialized there is no catalog +// and so no levels; Claude Code reports no default level in its catalog. +func (c *Chat) Efforts(_ context.Context, session protocol.ChatSession) ([]protocol.ChatEffort, string, error) { + var models []protocol.ChatModel + if s := c.session(session.ID); s != nil { + s.mu.Lock() + models = append(models, s.models...) + s.mu.Unlock() + } + if len(models) == 0 { + c.catalogMu.Lock() + models = append(models, c.catalog...) + c.catalogMu.Unlock() + } + model := strings.TrimSpace(session.Model) + if model == "" { + model = "default" + } + for _, match := range []func(protocol.ChatModel) bool{ + func(entry protocol.ChatModel) bool { return entry.ID == model }, + func(entry protocol.ChatModel) bool { return entry.Resolved != "" && entry.Resolved == model }, + } { + for _, entry := range models { + if match(entry) { + return append([]protocol.ChatEffort(nil), entry.Efforts...), "", nil + } + } + } + return nil, "", nil +} + +// SetEffort records the session's thinking level. It reaches the CLI at the +// next turn's start (applyEffort), so a running turn keeps its level. +func (c *Chat) SetEffort(_ context.Context, id, effort string) error { + s := c.session(id) + if s == nil { + meta, ok := transcriptSession(id) + if !ok { + return errors.New("unknown Claude session " + id) + } + s = c.sessionFor(id, meta.Directory) + } + s.mu.Lock() + s.effort = effort + s.mu.Unlock() + return nil +} + +// applyEffort tells the CLI the session's level before a turn when it was +// told another (or none) with apply_flag_settings, the control request the +// CLI documents for session-only effort changes. A null level restores the +// CLI's own default. The setting is session-only: it never edits a settings +// file. +func (s *session) applyEffort(ctx context.Context, proc *process) error { + s.mu.Lock() + requested, current := s.effort, proc.effort + s.mu.Unlock() + if requested == current { + return nil + } + var level any + if requested != "" { + level = requested + } + setCtx, cancel := context.WithTimeout(ctx, controlRequestTimeout) + defer cancel() + if _, err := proc.request(setCtx, "apply_flag_settings", map[string]any{"settings": map[string]any{"effortLevel": level}}); err != nil { + return fmt.Errorf("set Claude thinking level: %w", err) + } + s.mu.Lock() + proc.effort = requested + s.mu.Unlock() + return nil +} diff --git a/internal/claude/efforts_test.go b/internal/claude/efforts_test.go new file mode 100644 index 00000000..616213db --- /dev/null +++ b/internal/claude/efforts_test.go @@ -0,0 +1,88 @@ +package claude + +import ( + "context" + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +func TestInitializeEffortLevelsNeedSupportsEffort(t *testing.T) { + models, _, err := parseInitialize(json.RawMessage(`{"models":[ +{"value":"opus","resolvedModel":"claude-opus-x","displayName":"Opus","supportsEffort":true,"supportedEffortLevels":["low","xhigh","max","max"]}, +{"value":"old","displayName":"Old","supportedEffortLevels":["low"]}]}`)) + if err != nil || len(models) != 2 { + t.Fatalf("models = %+v, %v", models, err) + } + want := []protocol.ChatEffort{{ID: "low", Name: "Low"}, {ID: "xhigh", Name: "Extra high"}, {ID: "max", Name: "Max"}} + if models[0].Resolved != "claude-opus-x" || len(models[0].Efforts) != 3 || models[0].Efforts[1] != want[1] || models[0].Efforts[2] != want[2] { + t.Fatalf("opus = %+v", models[0]) + } + if len(models[1].Efforts) != 0 { + t.Fatalf("a model without supportsEffort offers %+v", models[1].Efforts) + } +} + +func effortLog(t *testing.T, path string) []string { + t.Helper() + data, err := os.ReadFile(path) + if os.IsNotExist(err) { + return nil + } + if err != nil { + t.Fatal(err) + } + return strings.Fields(strings.TrimSpace(string(data))) +} + +func TestClaudeEffortIsAppliedAtTheNextTurnStart(t *testing.T) { + chat := testChat(t) + logPath := filepath.Join(t.TempDir(), "efforts.log") + t.Setenv("FAKE_CLAUDE_EFFORT_LOG", logPath) + session := newCreatedChat(t, chat) + ctx := context.Background() + // No CLI has initialized yet: the levels are unknown, so none are offered. + if efforts, _, err := chat.Efforts(ctx, session); err != nil || len(efforts) != 0 { + t.Fatalf("levels before any initialize = %+v, %v", efforts, err) + } + if err := chat.SetEffort(ctx, session.ID, "max"); err != nil { + t.Fatal(err) + } + frames := subscribe(t, chat, session.ID) + if err := chat.Prompt(ctx, session.ID, "hello"); err != nil { + t.Fatal(err) + } + waitStepEnded(t, frames) + if got := effortLog(t, logPath); len(got) != 1 || got[0] != `{"effortLevel":"max"}` { + t.Fatalf("apply_flag_settings = %v", got) + } + for model, count := range map[string]int{"": 5, "default": 5, "claude-test": 5, "sonnet": 4, "claude-sonnet-test": 4, "haiku": 0, "unknown": 0} { + efforts, fallback, err := chat.Efforts(ctx, protocol.ChatSession{ID: session.ID, Model: model}) + if err != nil || len(efforts) != count || fallback != "" { + t.Errorf("model %q levels = %+v default %q, %v", model, efforts, fallback, err) + } + } + // Another session lists its row from the catalog any CLI reported. + if efforts, _, _ := chat.Efforts(ctx, protocol.ChatSession{ID: "other", Model: "sonnet"}); len(efforts) != 4 { + t.Fatalf("cached catalog levels = %+v", efforts) + } + // An unchanged level is not sent again; a cleared one restores the default. + if err := chat.Prompt(ctx, session.ID, "again"); err != nil { + t.Fatal(err) + } + waitStepEnded(t, frames) + if err := chat.SetEffort(ctx, session.ID, ""); err != nil { + t.Fatal(err) + } + if err := chat.Prompt(ctx, session.ID, "default"); err != nil { + t.Fatal(err) + } + waitStepEnded(t, frames) + if got := effortLog(t, logPath); len(got) != 2 || got[1] != `{"effortLevel":null}` { + t.Fatalf("apply_flag_settings = %v", got) + } +} diff --git a/internal/claude/hosted.go b/internal/claude/hosted.go index 9c72288d..bf284c13 100644 --- a/internal/claude/hosted.go +++ b/internal/claude/hosted.go @@ -267,6 +267,7 @@ func (c *Chat) connectHostContext(ctx context.Context, record claudehost.Record) // Callers hold s.mu. func (s *session) adoptLocked(p *process) { s.models, s.account = p.models, p.account + s.chat.rememberCatalog(p.models) s.appliedModel = p.appliedModel if p.automations != nil { s.automations = p.automations diff --git a/internal/claude/process.go b/internal/claude/process.go index a82c1bec..b9da1e38 100644 --- a/internal/claude/process.go +++ b/internal/claude/process.go @@ -49,6 +49,9 @@ type process struct { // mode is the permission mode this process runs in, guarded by the // session's mu: set at spawn and after a confirmed set_permission_mode. mode string + // effort is the thinking level this process was told with + // apply_flag_settings, guarded by the session's mu; "" until then. + effort string // link is set when a detachable host owns the CLI (hosted.go); cmd and // stdin are then unused. @@ -172,6 +175,7 @@ func (s *session) spawnOnce(ctx context.Context) (*process, error) { s.mu.Lock() s.models = models s.account = account + s.chat.rememberCatalog(models) s.lastErr = "" s.mu.Unlock() return p, nil @@ -225,10 +229,12 @@ func (p *process) initialize(ctx context.Context) ([]protocol.ChatModel, account func parseInitialize(raw json.RawMessage) ([]protocol.ChatModel, accountIdentity, error) { var reply struct { Models []struct { - Value string `json:"value"` - ResolvedModel string `json:"resolvedModel"` - DisplayName string `json:"displayName"` - Description string `json:"description"` + Value string `json:"value"` + ResolvedModel string `json:"resolvedModel"` + DisplayName string `json:"displayName"` + Description string `json:"description"` + SupportsEffort bool `json:"supportsEffort"` + SupportedEffortLevels []string `json:"supportedEffortLevels"` } `json:"models"` Account json.RawMessage `json:"account"` } @@ -244,7 +250,7 @@ func parseInitialize(raw json.RawMessage) ([]protocol.ChatModel, accountIdentity if name == "" { name = m.Value } - models = append(models, protocol.ChatModel{ID: m.Value, Provider: Backend, Name: name}) + models = append(models, protocol.ChatModel{ID: m.Value, Provider: Backend, Name: name, Resolved: m.ResolvedModel, Efforts: effortLevels(m.SupportsEffort, m.SupportedEffortLevels)}) } if len(models) == 0 { return nil, accountIdentity{}, errors.New("Claude Code reported no models; check that the CLI is signed in") diff --git a/internal/claude/testdata/fake-claude.py b/internal/claude/testdata/fake-claude.py index e551497c..b900bc90 100644 --- a/internal/claude/testdata/fake-claude.py +++ b/internal/claude/testdata/fake-claude.py @@ -212,8 +212,10 @@ def handle(line): if subtype == "initialize": respond(value.get("request_id"), { "models": [ - {"value": "default", "displayName": "Default", "description": "Default model"}, - {"value": "sonnet", "displayName": "Sonnet", "description": "Sonnet model"}, + {"value": "default", "resolvedModel": "claude-test", "displayName": "Default", "description": "Default model", + "supportsEffort": True, "supportedEffortLevels": ["low", "medium", "high", "xhigh", "max"]}, + {"value": "sonnet", "resolvedModel": "claude-sonnet-test", "displayName": "Sonnet", "description": "Sonnet model", + "supportsEffort": True, "supportedEffortLevels": ["low", "medium", "high", "max"]}, {"value": "haiku", "displayName": "Haiku", "description": "Haiku model"}, ], "commands": [], "agents": [], @@ -229,6 +231,12 @@ def handle(line): with open(mode_log, "a", encoding="utf-8") as log: log.write(request.get("mode", "") + "\n") respond(value.get("request_id"), {"mode": request.get("mode")}) + elif subtype == "apply_flag_settings": + effort_log = os.environ.get("FAKE_CLAUDE_EFFORT_LOG") + if effort_log: + with open(effort_log, "a", encoding="utf-8") as log: + log.write(json.dumps(request.get("settings"), separators=(",", ":")) + "\n") + respond(value.get("request_id"), {}) elif subtype == "interrupt": respond(value.get("request_id"), {"still_queued": [], "cancelled": []}) if state["pending"]: diff --git a/internal/codex/chat.go b/internal/codex/chat.go index c0d64111..0b97e04b 100644 --- a/internal/codex/chat.go +++ b/internal/codex/chat.go @@ -69,6 +69,11 @@ type Chat struct { usage map[string]usageSnapshot usageOrder uint64 modes map[string]string + // Thinking levels (efforts.go): the cached model catalog and each + // thread's level for its next turn/start. + models []catalogModel + modelsAt time.Time + efforts map[string]string // Account quota (quota.go): merged native snapshot and the read schedule. quota *quotaState @@ -447,6 +452,7 @@ func (c *Chat) PromptWithMessageIDModelFiles(ctx context.Context, id, text, mess params["model"] = model } c.applyMode(id, params) + c.applyEffort(id, params) c.mu.Lock() if c.starting == nil { c.starting = map[string]bool{} @@ -980,6 +986,8 @@ type thread struct { CreatedAt int64 `json:"createdAt"` UpdatedAt int64 `json:"updatedAt"` Turns []turn `json:"turns"` + // ReasoningEffort is the thread's own current thinking level. + ReasoningEffort string `json:"reasoningEffort"` } type turn struct { @@ -1037,6 +1045,7 @@ func (t thread) chatSession() protocol.ChatSession { return protocol.ChatSession{ ID: t.ID, Title: title, Directory: t.Cwd, Model: t.Model, ModelProvider: t.ModelProvider, + Effort: t.ReasoningEffort, CreatedAt: t.CreatedAt * 1000, UpdatedAt: t.UpdatedAt * 1000, } } diff --git a/internal/codex/efforts.go b/internal/codex/efforts.go new file mode 100644 index 00000000..67e7f765 --- /dev/null +++ b/internal/codex/efforts.go @@ -0,0 +1,59 @@ +package codex + +import ( + "context" + "errors" + "strings" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// Efforts reports the reasoning efforts model/list advertises for the row's +// model (session.Model, already the saved next-turn model) and its default. +// A model the catalog does not list, or one without efforts, has none. +func (c *Chat) Efforts(ctx context.Context, session protocol.ChatSession) ([]protocol.ChatEffort, string, error) { + model := strings.TrimSpace(session.Model) + if model == "" { + return nil, "", nil + } + catalog, err := c.catalog(ctx) + if err != nil { + return nil, "", err + } + for _, entry := range catalog { + if entry.ID == model { + return append([]protocol.ChatEffort(nil), entry.Efforts...), entry.DefaultEffort, nil + } + } + return nil, "", nil +} + +// SetEffort records the thread's reasoning effort; it is sent as turn/start's +// effort, which Codex keeps for the thread's later turns too. "" sends none. +// The server has validated the level against the model's catalog entry. +func (c *Chat) SetEffort(_ context.Context, id, effort string) error { + if strings.TrimSpace(id) == "" { + return errors.New("a Codex thread id is required") + } + c.mu.Lock() + defer c.mu.Unlock() + if c.efforts == nil { + c.efforts = map[string]string{} + } + if effort == "" { + delete(c.efforts, id) + return nil + } + c.efforts[id] = effort + return nil +} + +// applyEffort adds the thread's chosen effort to turn/start params. +func (c *Chat) applyEffort(id string, params map[string]any) { + c.mu.Lock() + effort := c.efforts[id] + c.mu.Unlock() + if effort != "" { + params["effort"] = effort + } +} diff --git a/internal/codex/efforts_test.go b/internal/codex/efforts_test.go new file mode 100644 index 00000000..1d27e03c --- /dev/null +++ b/internal/codex/efforts_test.go @@ -0,0 +1,89 @@ +package codex + +import ( + "context" + "encoding/json" + "sync" + "testing" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +const effortCatalog = `{"data":[ +{"id":"a","model":"thinker","displayName":"Thinker","hidden":false,"supportedReasoningEfforts":[{"reasoningEffort":"low","description":"Quick"},{"reasoningEffort":"high","description":"Deep"},{"reasoningEffort":"xhigh","description":"Deeper"}],"defaultReasoningEffort":"high"}, +{"id":"b","model":"plain","displayName":"Plain","hidden":false,"supportedReasoningEfforts":[],"defaultReasoningEffort":"medium"} +],"nextCursor":null}` + +func TestCodexEffortsComeFromModelListAndReachTurnStart(t *testing.T) { + var mu sync.Mutex + lists, turns := 0, []map[string]any{} + chat := modelTestChat(t, func(method string, raw json.RawMessage) any { + mu.Lock() + defer mu.Unlock() + switch method { + case "thread/read": + return json.RawMessage(`{"thread":{"id":"` + fixtureThread + `","model":"thinker","modelProvider":"openai","reasoningEffort":"low","turns":[]}}`) + case "model/list": + lists++ + return json.RawMessage(effortCatalog) + case "turn/start": + var params map[string]any + _ = json.Unmarshal(raw, ¶ms) + turns = append(turns, params) + return json.RawMessage(`{"turn":{"id":"turn-1","status":"inProgress","items":[]}}`) + } + t.Errorf("unexpected method %s", method) + return map[string]any{} + }) + ctx := context.Background() + session, _, err := chat.Session(ctx, fixtureThread) + if err != nil || session.Effort != "low" { + t.Fatalf("thread's own level = %q, %v", session.Effort, err) + } + efforts, fallback, err := chat.Efforts(ctx, session) + if err != nil || fallback != "high" || len(efforts) != 3 || efforts[0] != (protocol.ChatEffort{ID: "low", Name: "Low", Description: "Quick"}) || efforts[2].Name != "Extra high" { + t.Fatalf("efforts = %+v default %q, %v", efforts, fallback, err) + } + for _, model := range []string{"plain", "unlisted", ""} { + if none, _, err := chat.Efforts(ctx, protocol.ChatSession{ID: fixtureThread, Model: model}); err != nil || len(none) != 0 { + t.Fatalf("model %q offers %+v, %v", model, none, err) + } + } + models, err := chat.Models(ctx, fixtureThread) + if err != nil || len(models) != 2 || len(models[0].Efforts) != 3 || models[0].DefaultEffort != "high" || len(models[1].Efforts) != 0 || models[1].DefaultEffort != "" { + t.Fatalf("catalog = %+v, %v", models, err) + } + if lists != 1 { + t.Fatalf("model/list read %d times; rows must share one cached catalog", lists) + } + + if err := chat.Prompt(ctx, fixtureThread, "no level"); err != nil { + t.Fatal(err) + } + if err := chat.SetEffort(ctx, fixtureThread, "xhigh"); err != nil { + t.Fatal(err) + } + if err := chat.PromptWithMessageIDModel(ctx, fixtureThread, "deep", "msg", "thinker"); err != nil { + t.Fatal(err) + } + if err := chat.SetEffort(ctx, fixtureThread, ""); err != nil { + t.Fatal(err) + } + if err := chat.Prompt(ctx, fixtureThread, "default again"); err != nil { + t.Fatal(err) + } + mu.Lock() + defer mu.Unlock() + if len(turns) != 3 { + t.Fatalf("turns = %d", len(turns)) + } + if _, sent := turns[0]["effort"]; sent { + t.Fatalf("turn without a level sent one: %v", turns[0]) + } + if turns[1]["effort"] != "xhigh" || turns[1]["model"] != "thinker" { + t.Fatalf("turn/start params = %v", turns[1]) + } + if _, sent := turns[2]["effort"]; sent { + t.Fatalf("cleared level still sent: %v", turns[2]) + } +} diff --git a/internal/codex/models.go b/internal/codex/models.go index d21ca2b2..5035716c 100644 --- a/internal/codex/models.go +++ b/internal/codex/models.go @@ -4,12 +4,26 @@ import ( "context" "encoding/json" "errors" + "time" "git.a-wan.net/alan/pipelines/internal/protocol" ) +// catalogTTL bounds how long a model/list read answers rows and choices; the +// account's catalog changes rarely, and a session list must not issue one +// model/list per row. +const catalogTTL = time.Minute + +// catalogModel is one visible model/list entry. Model.model, not the catalog +// row's opaque id, is the turn/start override. +type catalogModel struct { + ID string + Name string + Efforts []protocol.ChatEffort + DefaultEffort string +} + // Models uses the thread's actual provider and the account's paginated catalog. -// Model.model, not the catalog row's opaque id, is the turn/start override. func (c *Chat) Models(ctx context.Context, id string) ([]protocol.ChatModel, error) { session, found, err := c.Session(ctx, id) if err != nil { @@ -18,7 +32,37 @@ func (c *Chat) Models(ctx context.Context, id string) ([]protocol.ChatModel, err if !found || session.ModelProvider == "" { return nil, errors.New("Codex thread model provider is unavailable") } - models := []protocol.ChatModel{} + catalog, err := c.catalog(ctx) + if err != nil { + return nil, err + } + models := make([]protocol.ChatModel, 0, len(catalog)) + for _, model := range catalog { + models = append(models, protocol.ChatModel{ID: model.ID, Provider: session.ModelProvider, Name: model.Name, Efforts: model.Efforts, DefaultEffort: model.DefaultEffort}) + } + return models, nil +} + +// catalog returns the visible model/list entries, read at most once a minute. +func (c *Chat) catalog(ctx context.Context) ([]catalogModel, error) { + c.mu.Lock() + cached, at := c.models, c.modelsAt + c.mu.Unlock() + if cached != nil && time.Since(at) < catalogTTL { + return cached, nil + } + models, err := c.readCatalog(ctx) + if err != nil { + return nil, err + } + c.mu.Lock() + c.models, c.modelsAt = models, time.Now() + c.mu.Unlock() + return models, nil +} + +func (c *Chat) readCatalog(ctx context.Context) ([]catalogModel, error) { + models := []catalogModel{} cursor := "" seen := map[string]bool{} for page := 0; page < 20; page++ { @@ -32,9 +76,14 @@ func (c *Chat) Models(ctx context.Context, id string) ([]protocol.ChatModel, err } var response struct { Data *[]struct { - Model string `json:"model"` - DisplayName string `json:"displayName"` - Hidden bool `json:"hidden"` + Model string `json:"model"` + DisplayName string `json:"displayName"` + Hidden bool `json:"hidden"` + DefaultReasoningEffort string `json:"defaultReasoningEffort"` + SupportedReasoningEfforts []struct { + ReasoningEffort string `json:"reasoningEffort"` + Description string `json:"description"` + } `json:"supportedReasoningEfforts"` } `json:"data"` NextCursor *string `json:"nextCursor"` } @@ -42,9 +91,19 @@ func (c *Chat) Models(ctx context.Context, id string) ([]protocol.ChatModel, err return nil, errors.New("invalid Codex model catalog") } for _, model := range *response.Data { - if model.Model != "" && !model.Hidden { - models = append(models, protocol.ChatModel{ID: model.Model, Provider: session.ModelProvider, Name: model.DisplayName}) + if model.Model == "" || model.Hidden { + continue } + entry := catalogModel{ID: model.Model, Name: model.DisplayName} + for _, option := range model.SupportedReasoningEfforts { + if option.ReasoningEffort != "" && !protocol.HasChatEffort(entry.Efforts, option.ReasoningEffort) { + entry.Efforts = append(entry.Efforts, protocol.ChatEffort{ID: option.ReasoningEffort, Name: protocol.ChatEffortName(option.ReasoningEffort), Description: option.Description}) + } + } + if protocol.HasChatEffort(entry.Efforts, model.DefaultReasoningEffort) { + entry.DefaultEffort = model.DefaultReasoningEffort + } + models = append(models, entry) } if response.NextCursor == nil || *response.NextCursor == "" { return models, nil diff --git a/internal/opencode/chat.go b/internal/opencode/chat.go index c33c8dfb..72edd4da 100644 --- a/internal/opencode/chat.go +++ b/internal/opencode/chat.go @@ -49,6 +49,10 @@ type Chat struct { agents map[string]string modelMu sync.Mutex sessionModels map[string]sessionModelCache + // catalog caches /api/model for thinking levels (efforts.go). + catalogMu sync.Mutex + catalog []protocol.ChatModel + catalogAt time.Time } // NewChat brokers chat for one owned OpenCode endpoint. The connection must be @@ -182,6 +186,9 @@ func (c *Chat) Models(ctx context.Context, _ string) ([]protocol.ChatModel, erro Name string `json:"name"` Enabled bool `json:"enabled"` Status string `json:"status"` + Variants []struct { + ID string `json:"id"` + } `json:"variants"` } `json:"data"` } if err := json.Unmarshal(data, &envelope); err != nil { @@ -204,7 +211,13 @@ func (c *Chat) Models(ctx context.Context, _ string) ([]protocol.ChatModel, erro if name == "" { name = id } - models = append(models, protocol.ChatModel{ID: id, Provider: provider, Name: name}) + model := protocol.ChatModel{ID: id, Provider: provider, Name: name} + for _, variant := range item.Variants { + if variant.ID != "" && !protocol.HasChatEffort(model.Efforts, variant.ID) { + model.Efforts = append(model.Efforts, protocol.ChatEffort{ID: variant.ID, Name: protocol.ChatEffortName(variant.ID)}) + } + } + models = append(models, model) } sort.Slice(models, func(i, j int) bool { if models[i].Provider != models[j].Provider { @@ -1686,6 +1699,8 @@ type nativeChatFile struct { type nativeChatModel struct { ID string `json:"id"` ProviderID string `json:"providerID"` + // Variant is the session model's chosen variant (its thinking level). + Variant string `json:"variant"` } // nativeChatPart is one content element shared by both message envelopes. Raw @@ -1908,6 +1923,7 @@ func chatSessionFields(record nativeChatSession) protocol.ChatSession { Agent: record.Agent, Model: record.Model.ID, ModelProvider: record.Model.ProviderID, + Effort: record.Model.Variant, Directory: record.Location.Directory, ParentID: record.ParentID, CreatedAt: record.Time.Created, diff --git a/internal/opencode/efforts.go b/internal/opencode/efforts.go new file mode 100644 index 00000000..d869dbb9 --- /dev/null +++ b/internal/opencode/efforts.go @@ -0,0 +1,86 @@ +package opencode + +import ( + "context" + "errors" + "fmt" + "strings" + "time" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// catalogTTL bounds how long one /api/model read answers listed rows, so a +// session list never reads the catalog once per row. +const catalogTTL = time.Minute + +// modelCatalog is Models, cached for catalogTTL. +func (c *Chat) modelCatalog(ctx context.Context) ([]protocol.ChatModel, error) { + c.catalogMu.Lock() + cached, at := c.catalog, c.catalogAt + c.catalogMu.Unlock() + if cached != nil && time.Since(at) < catalogTTL { + return cached, nil + } + models, err := c.Models(ctx, "") + if err != nil { + return nil, err + } + c.catalogMu.Lock() + c.catalog, c.catalogAt = models, time.Now() + c.catalogMu.Unlock() + return models, nil +} + +// Efforts reports the variants the endpoint lists for the session's model. +// OpenCode stores the chosen variant on the session's model itself (the row's +// Effort); with none chosen it uses the model's base settings, so there is no +// separate default level. The v1 API has no variants. +func (c *Chat) Efforts(ctx context.Context, session protocol.ChatSession) ([]protocol.ChatEffort, string, error) { + if c.conn == nil || c.conn.Backend == "v1" || session.Model == "" || session.ModelProvider == "" { + return nil, "", nil + } + models, err := c.modelCatalog(ctx) + if err != nil { + return nil, "", err + } + for _, model := range models { + if model.ID == session.Model && model.Provider == session.ModelProvider { + return append([]protocol.ChatEffort(nil), model.Efforts...), "", nil + } + } + return nil, "", nil +} + +// SetEffort selects the variant on the session's current model with the +// documented session model endpoint, which OpenCode persists with the session +// and applies from the next turn. "" selects the model without a variant. +func (c *Chat) SetEffort(ctx context.Context, id, effort string) error { + if !sessionID.MatchString(id) { + return fmt.Errorf("invalid OpenCode session id") + } + if c.conn == nil || c.conn.Backend == "v1" { + return errors.New("this OpenCode endpoint has no model variants") + } + session, found, err := c.Session(ctx, id) + if err != nil { + return err + } + if !found || session.Model == "" || session.ModelProvider == "" { + return errors.New("the OpenCode session has no model to set a thinking level on") + } + ref := map[string]string{"id": session.Model, "providerID": session.ModelProvider} + if effort = strings.TrimSpace(effort); effort != "" { + ref["variant"] = effort + } + if evidence, err := c.call(ctx, "post", "/api/session/"+id+"/model", map[string]any{"model": ref}); err != nil { + if reason := strings.TrimSpace(string(evidence)); reason != "" { + if len(reason) > 300 { + reason = reason[:300] + } + return fmt.Errorf("%w: %s", err, reason) + } + return err + } + return nil +} diff --git a/internal/opencode/efforts_test.go b/internal/opencode/efforts_test.go new file mode 100644 index 00000000..95d4d2e8 --- /dev/null +++ b/internal/opencode/efforts_test.go @@ -0,0 +1,96 @@ +package opencode + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "sync" + "testing" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +const effortSessionList = `{"data":[{"id":"ses_1","title":"Chat","model":{"id":"thinker","providerID":"prov","variant":"high"},"location":{"directory":"/fixture"},"time":{"created":1,"updated":2}}]}` + +const effortModelList = `{"data":[ +{"modelID":"thinker","providerID":"prov","name":"Thinker","enabled":true,"status":"active","variants":[{"id":"low","settings":{"reasoningEffort":"low"}},{"id":"high","settings":{"reasoningEffort":"high"}},{"id":"xhigh"}]}, +{"modelID":"plain","providerID":"prov","name":"Plain","enabled":true,"status":"active","variants":[]} +]}` + +func TestOpenCodeEffortsAreModelVariants(t *testing.T) { + var mu sync.Mutex + modelReads := 0 + var posted []map[string]string + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + mu.Lock() + defer mu.Unlock() + w.Header().Set("Content-Type", "application/json") + switch { + case r.Method == http.MethodGet && r.URL.Path == "/api/session": + _, _ = w.Write([]byte(effortSessionList)) + case r.Method == http.MethodGet && r.URL.Path == "/api/model": + modelReads++ + _, _ = w.Write([]byte(effortModelList)) + case r.Method == http.MethodPost && r.URL.Path == "/api/session/ses_1/model": + var body struct { + Model map[string]string `json:"model"` + } + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + t.Error(err) + } + posted = append(posted, body.Model) + w.WriteHeader(http.StatusNoContent) + default: + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + } + })) + defer srv.Close() + chat := NewChat(&Connection{Backend: "v2", URL: srv.URL}) + ctx := context.Background() + session, found, err := chat.Session(ctx, "ses_1") + if err != nil || !found || session.Effort != "high" || session.Model != "thinker" { + t.Fatalf("session = %+v, %v", session, err) + } + efforts, fallback, err := chat.Efforts(ctx, session) + if err != nil || fallback != "" || len(efforts) != 3 || efforts[2] != (protocol.ChatEffort{ID: "xhigh", Name: "Extra high"}) { + t.Fatalf("efforts = %+v %q, %v", efforts, fallback, err) + } + for _, row := range []protocol.ChatSession{{ID: "ses_1", Model: "plain", ModelProvider: "prov"}, {ID: "ses_1", Model: "thinker", ModelProvider: "other"}, {ID: "ses_1"}} { + if none, _, err := chat.Efforts(ctx, row); err != nil || len(none) != 0 { + t.Fatalf("row %+v offers %+v, %v", row, none, err) + } + } + models, err := chat.Models(ctx, "ses_1") + if err != nil || len(models) != 2 || len(models[1].Efforts) != 3 || len(models[0].Efforts) != 0 { + t.Fatalf("models = %+v, %v", models, err) + } + if err := chat.SetEffort(ctx, "ses_1", "low"); err != nil { + t.Fatal(err) + } + if err := chat.SetEffort(ctx, "ses_1", ""); err != nil { + t.Fatal(err) + } + mu.Lock() + defer mu.Unlock() + // Rows share one cached catalog read; Models itself still reads live. + if modelReads != 2 { + t.Fatalf("model reads = %d", modelReads) + } + if len(posted) != 2 || posted[0]["variant"] != "low" || posted[0]["id"] != "thinker" || posted[0]["providerID"] != "prov" { + t.Fatalf("posted = %+v", posted) + } + if _, has := posted[1]["variant"]; has || posted[1]["id"] != "thinker" { + t.Fatalf("cleared level posted %+v", posted[1]) + } +} + +func TestOpenCodeV1HasNoEfforts(t *testing.T) { + chat := NewChat(&Connection{Backend: "v1"}) + if efforts, _, err := chat.Efforts(context.Background(), protocol.ChatSession{ID: "ses_1", Model: "m", ModelProvider: "p"}); err != nil || len(efforts) != 0 { + t.Fatalf("v1 efforts = %+v, %v", efforts, err) + } + if err := chat.SetEffort(context.Background(), "ses_1", "high"); err == nil { + t.Fatal("v1 accepted a thinking level") + } +} diff --git a/internal/pipeline/operator_efforts_test.go b/internal/pipeline/operator_efforts_test.go new file mode 100644 index 00000000..b25d7417 --- /dev/null +++ b/internal/pipeline/operator_efforts_test.go @@ -0,0 +1,78 @@ +package pipeline + +import ( + "context" + "database/sql" + "path/filepath" + "testing" +) + +func TestOperatorChatEffortMigratesPersistsAndFollowsNewSession(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) + } + for _, row := range [][2]string{{"codex-chat", "codex"}, {"open-chat", "opencode"}} { + if err := store.RecordOperatorSession(ctx, row[0], "/fixture", "title", "operator"); err != nil { + t.Fatal(err) + } + if err := store.SetOperatorSessionBackend(ctx, row[0], row[1]); err != nil { + t.Fatal(err) + } + } + if effort, err := store.ChatEffort(ctx, "codex-chat"); err != nil || effort != "" { + t.Fatalf("fresh effort %q: %v", effort, err) + } + // OpenCode keeps its level natively as the model variant; only Codex and + // Claude chats store one with Pipelines. + if err := store.SetChatEffort(ctx, "open-chat", "high"); err == nil { + t.Fatal("an OpenCode chat stored a Pipelines thinking level") + } + if err := store.SetChatEffort(ctx, "missing", "high"); err == nil { + t.Fatal("unregistered chat accepted a thinking level") + } + before, _, err := store.OperatorSession(ctx, "codex-chat") + if err != nil { + t.Fatal(err) + } + if err := store.SetChatEffort(ctx, "codex-chat", "xhigh"); err != nil { + t.Fatal(err) + } + store.Close() + store, err = Open(path) + if err != nil { + t.Fatal(err) + } + defer store.Close() + if effort, err := store.ChatEffort(ctx, "codex-chat"); err != nil || effort != "xhigh" { + t.Fatalf("restored effort %q: %v", effort, err) + } + after, _, err := store.OperatorSession(ctx, "codex-chat") + if err != nil || before.UpdatedAt != after.UpdatedAt { + t.Fatal("effort changed recency", err) + } + // /new keeps the chat's level on its fresh native session. + if err := store.ReplaceOperatorSession(ctx, "codex-chat", "codex-chat-2"); err != nil { + t.Fatal(err) + } + if effort, err := store.ChatEffort(ctx, "codex-chat-2"); err != nil || effort != "xhigh" { + t.Fatalf("replaced session effort %q: %v", effort, err) + } + if err := store.SetChatEffort(ctx, "codex-chat-2", ""); err != nil { + t.Fatal(err) + } + if effort, err := store.ChatEffort(ctx, "codex-chat-2"); err != nil || effort != "" { + t.Fatalf("cleared effort %q: %v", effort, err) + } +} diff --git a/internal/pipeline/operator_sessions.go b/internal/pipeline/operator_sessions.go index 14b44dc6..b9d1469e 100644 --- a/internal/pipeline/operator_sessions.go +++ b/internal/pipeline/operator_sessions.go @@ -144,7 +144,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", "chat_mode"} { + for _, column := range []string{"chat_model", "chat_model_provider", "chat_mode", "chat_effort"} { if err := s.addColumnIfMissing("operator_sessions", column, "TEXT NOT NULL DEFAULT ''"); err != nil { return err } @@ -210,8 +210,8 @@ func (s *Store) ReplaceOperatorSession(ctx context.Context, oldID, newID string) return ErrOperatorSessionReplaced } now := time.Now().UnixMilli() - if _, err := tx.ExecContext(ctx, `INSERT INTO operator_sessions(session_id,directory,title,tag,created_at,last_used_at,closed_at,starred_at,color,backend,ephemeral,chat_model,chat_model_provider,chat_mode) -SELECT ?,directory,title,tag,created_at,?,0,starred_at,color,backend,ephemeral,chat_model,chat_model_provider,chat_mode FROM operator_sessions WHERE session_id=?`, newID, now, oldID); err != nil { + if _, err := tx.ExecContext(ctx, `INSERT INTO operator_sessions(session_id,directory,title,tag,created_at,last_used_at,closed_at,starred_at,color,backend,ephemeral,chat_model,chat_model_provider,chat_mode,chat_effort) +SELECT ?,directory,title,tag,created_at,?,0,starred_at,color,backend,ephemeral,chat_model,chat_model_provider,chat_mode,chat_effort FROM operator_sessions WHERE session_id=?`, newID, now, oldID); err != nil { return err } if _, err := tx.ExecContext(ctx, `UPDATE operator_sessions SET replaced_by=? WHERE session_id=?`, newID, oldID); err != nil { @@ -268,6 +268,35 @@ func (s *Store) ChatMode(ctx context.Context, id string) (string, error) { return mode, err } +// SetChatEffort stores one Codex or Claude operator chat's thinking level for +// its later turns and restarts; "" restores the harness default. The harness +// validates the level; this only records it. +func (s *Store) SetChatEffort(ctx context.Context, id, effort string) error { + result, err := s.db.ExecContext(ctx, `UPDATE operator_sessions SET chat_effort=? WHERE session_id=? AND backend IN ('codex','claude') AND tag<>''`, effort, id) + if err != nil { + return err + } + count, err := result.RowsAffected() + if err != nil { + return err + } + if count != 1 { + return errors.New("a thinking level can only be stored on a registered Codex or Claude operator chat") + } + return nil +} + +// ChatEffort reads one operator chat's stored thinking level without touching +// recency; "" when none was chosen or the session is not such a chat. +func (s *Store) ChatEffort(ctx context.Context, id string) (string, error) { + var effort string + err := s.db.QueryRowContext(ctx, `SELECT chat_effort FROM operator_sessions WHERE session_id=? AND backend IN ('codex','claude') AND tag<>''`, id).Scan(&effort) + if errors.Is(err, sql.ErrNoRows) { + return "", nil + } + return effort, 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) diff --git a/internal/protocol/chat.go b/internal/protocol/chat.go index ed6bc132..3218a75f 100644 --- a/internal/protocol/chat.go +++ b/internal/protocol/chat.go @@ -259,6 +259,13 @@ type ChatModel struct { ID string `json:"id"` Provider string `json:"provider"` Name string `json:"name,omitempty"` + // Efforts are the thinking levels this model accepts and DefaultEffort the + // one its harness uses when none is chosen; both empty when it has none. + Efforts []ChatEffort `json:"efforts,omitempty"` + DefaultEffort string `json:"default_effort,omitempty"` + // Resolved is the concrete model an alias names (Claude Code's "opus"), + // so a model a reply reports can be matched to its entry. Never sent. + Resolved string `json:"-"` } // ChatMode is one permission mode a chat harness offers. @@ -297,6 +304,12 @@ type ChatSession struct { // ModeName is Mode's display name. With no stored mode both name the // harness default, so a client can show the mode without a chat_modes read. ModeName string `json:"mode_name,omitempty"` + // Effort is the stored thinking level ("" for the harness default), and + // Efforts/DefaultEffort the levels the row's model accepts, when the + // server knows them without starting the harness. No Efforts: no control. + Effort string `json:"effort,omitempty"` + Efforts []ChatEffort `json:"efforts,omitempty"` + DefaultEffort string `json:"default_effort,omitempty"` // InstructionStatus is the observed create-time preload outcome: applied, // unsupported, failed, or not_requested. Ordinary list rows omit it. InstructionStatus string `json:"instruction_status,omitempty"` diff --git a/internal/protocol/chat_effort.go b/internal/protocol/chat_effort.go new file mode 100644 index 00000000..2ccedc3d --- /dev/null +++ b/internal/protocol/chat_effort.go @@ -0,0 +1,53 @@ +package protocol + +import "strings" + +// ChatEffortSelect sets one chat's thinking (reasoning effort) level for its +// next and later turns. The body is ChatEffortRequest naming a level of the +// chat's current model; the reply echoes the stored level with the model's +// levels. A running turn keeps the level it started with. +const ChatEffortSelect = "chat_effort" + +// ChatEffort is one thinking level a chat's model accepts, as its harness +// names it (Codex reasoning efforts, Claude Code effort levels, OpenCode +// model variants). +type ChatEffort struct { + ID string `json:"id"` + Name string `json:"name"` + Description string `json:"description,omitempty"` +} + +// ChatEffortRequest is the chat_effort body, its reply, and the current level +// a chat_models reply reports. Effort "" means the harness's own default. +// Efforts and DefaultEffort are reply-only: the levels the chat's current (or +// saved next-turn) model accepts, and the level the harness uses when none +// was chosen, when it reports one. No Efforts means the model has no level +// the harness can set, so no control is offered. +type ChatEffortRequest struct { + SessionID string `json:"session_id"` + Effort string `json:"effort"` + Efforts []ChatEffort `json:"efforts,omitempty"` + DefaultEffort string `json:"default_effort,omitempty"` +} + +// ChatEffortName is the display name for a level id a harness reports without +// one: the id in title case, with xhigh spelled out. +func ChatEffortName(id string) string { + switch id { + case "": + return "" + case "xhigh": + return "Extra high" + } + return strings.ToUpper(id[:1]) + id[1:] +} + +// HasChatEffort reports whether id is one of efforts. +func HasChatEffort(efforts []ChatEffort, id string) bool { + for _, effort := range efforts { + if effort.ID == id { + return true + } + } + return false +} diff --git a/internal/protocol/chat_effort_test.go b/internal/protocol/chat_effort_test.go new file mode 100644 index 00000000..73038bc0 --- /dev/null +++ b/internal/protocol/chat_effort_test.go @@ -0,0 +1,65 @@ +package protocol + +import ( + "encoding/json" + "reflect" + "strings" + "testing" +) + +func TestChatEffortMessagesRoundTrip(t *testing.T) { + levels := []ChatEffort{{ID: "low", Name: "Low", Description: "Fast"}, {ID: "xhigh", Name: "Extra high"}} + for _, message := range []Message{ + {Type: ChatEffortSelect, ID: "set", EffortRequest: &ChatEffortRequest{SessionID: "s", Effort: "xhigh"}}, + {Type: ChatEffortSelect, ID: "set", EffortRequest: &ChatEffortRequest{SessionID: "s", Effort: "", Efforts: levels, DefaultEffort: "low"}}, + {Type: ChatModels, ID: "models", ChatModels: []ChatModel{{ID: "m", Provider: "p", Name: "M", Efforts: levels, DefaultEffort: "low"}}, EffortRequest: &ChatEffortRequest{SessionID: "s", Effort: "low", Efforts: levels}}, + } { + raw, err := json.Marshal(message) + if err != nil { + t.Fatal(err) + } + var decoded Message + if err := json.Unmarshal(raw, &decoded); err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(decoded, message) { + t.Fatalf("round trip\n got %+v\nwant %+v\njson %s", decoded, message, raw) + } + } + // The wire names the client reads. + raw, _ := json.Marshal(Message{Type: ChatEffortSelect, EffortRequest: &ChatEffortRequest{SessionID: "s", Effort: "high", Efforts: levels, DefaultEffort: "low"}}) + for _, want := range []string{`"type":"chat_effort"`, `"chat_effort":{"session_id":"s","effort":"high","efforts":[{"id":"low","name":"Low","description":"Fast"}`, `"default_effort":"low"`} { + if !strings.Contains(string(raw), want) { + t.Fatalf("%s lacks %s", raw, want) + } + } +} + +func TestChatSessionAndModelEffortFields(t *testing.T) { + raw, _ := json.Marshal(ChatSession{ID: "s", Effort: "max", Efforts: []ChatEffort{{ID: "max", Name: "Max"}}, DefaultEffort: "max"}) + for _, want := range []string{`"effort":"max"`, `"efforts":[{"id":"max","name":"Max"}]`, `"default_effort":"max"`} { + if !strings.Contains(string(raw), want) { + t.Fatalf("%s lacks %s", raw, want) + } + } + // A row or model without levels sends none, and Resolved never leaves. + raw, _ = json.Marshal(ChatSession{ID: "s"}) + if strings.Contains(string(raw), "effort") { + t.Fatalf("level-less row sends effort fields: %s", raw) + } + raw, _ = json.Marshal(ChatModel{ID: "opus", Provider: "claude", Resolved: "claude-opus-5-5"}) + if strings.Contains(string(raw), "claude-opus-5-5") || strings.Contains(string(raw), "effort") { + t.Fatalf("model leaks internal fields: %s", raw) + } +} + +func TestChatEffortName(t *testing.T) { + for id, want := range map[string]string{"": "", "low": "Low", "xhigh": "Extra high", "max": "Max", "minimal": "Minimal", "none": "None"} { + if got := ChatEffortName(id); got != want { + t.Errorf("ChatEffortName(%q) = %q, want %q", id, got, want) + } + } + if !HasChatEffort([]ChatEffort{{ID: "high"}}, "high") || HasChatEffort([]ChatEffort{{ID: "high"}}, "max") || HasChatEffort(nil, "") { + t.Fatal("HasChatEffort") + } +} diff --git a/internal/protocol/message.go b/internal/protocol/message.go index 65ec05e9..e2c2ba21 100644 --- a/internal/protocol/message.go +++ b/internal/protocol/message.go @@ -240,6 +240,9 @@ type Message struct { ChatModes []ChatMode `json:"chat_modes,omitempty"` // ModeRequest is the chat_mode body and echoed selection. ModeRequest *ChatModeRequest `json:"chat_mode,omitempty"` + // EffortRequest is the chat_effort body and reply, and the chat's current + // thinking level on a chat_models reply. + EffortRequest *ChatEffortRequest `json:"chat_effort,omitempty"` // ChatSubscribe attaches this connection to one session's shared stream; the // body names the session like ChatQuery. A chat_prompt response sets // ChatQueued when the prompt had to wait for an in-flight turn and was diff --git a/internal/server/chat.go b/internal/server/chat.go index 12d0e03d..03efa238 100644 --- a/internal/server/chat.go +++ b/internal/server/chat.go @@ -249,6 +249,9 @@ func handleChat(ctx context.Context, request protocol.Message, broker chatBroker case protocol.ChatModeSelect: handleChatModeSwitch(ctx, request, broker, write) return true + case protocol.ChatEffortSelect: + handleChatEffort(ctx, request, broker, write) + return true case protocol.ChatShells: handleChatShells(ctx, request, broker, registry, write) return true diff --git a/internal/server/chat_efforts.go b/internal/server/chat_efforts.go new file mode 100644 index 00000000..6182d84b --- /dev/null +++ b/internal/server/chat_efforts.go @@ -0,0 +1,221 @@ +package server + +import ( + "context" + "errors" + "strings" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// chatEfforter is the optional thinking-level surface of a chat harness. +type chatEfforter interface { + // Efforts reports the levels the row's model accepts (session.Model is + // already the saved next-turn model) and the harness default, from what + // the adapter knows without starting a process. None: no control. + Efforts(ctx context.Context, session protocol.ChatSession) ([]protocol.ChatEffort, string, error) + // SetEffort hands a level to the harness for the next turn; "" restores + // its default. A running turn keeps the level it started with. + SetEffort(ctx context.Context, sessionID, effort string) error +} + +// chatEffortRegistry stores each Codex or Claude chat's level. OpenCode keeps +// the level natively as the session model's variant. +type chatEffortRegistry interface { + SetChatEffort(ctx context.Context, id, effort string) error + ChatEffort(ctx context.Context, id string) (string, error) +} + +var ( + errChatEffortsUnsupported = errors.New("this chat harness does not support thinking levels") + errChatEffortsNone = errors.New("this chat's model has no thinking levels") +) + +// storedEffort is a model backend's saved level, "" when none. +func (m *chatManager) storedEffort(ctx context.Context, id string) string { + registry, ok := m.registry.(chatEffortRegistry) + if !ok { + return "" + } + effort, err := registry.ChatEffort(ctx, id) + if err != nil { + return "" + } + return effort +} + +// withEffort adds the row model's levels and the current level. A level the +// model does not accept is reported as the default rather than as chosen. +func (m *chatManager) withEffort(ctx context.Context, session protocol.ChatSession) protocol.ChatSession { + efforter, ok := m.adapter(session.Backend).(chatEfforter) + if !ok { + session.Effort, session.Efforts, session.DefaultEffort = "", nil, "" + return session + } + efforts, fallback, err := efforter.Efforts(ctx, session) + if err != nil || len(efforts) == 0 { + session.Effort, session.Efforts, session.DefaultEffort = "", nil, "" + return session + } + // A stored choice wins; otherwise the harness's own reported level (a + // Codex thread's reasoningEffort, an OpenCode variant) is current. + if isModelBackend(session.Backend) { + if stored := m.storedEffort(ctx, session.ID); stored != "" { + session.Effort = stored + } + } + if !protocol.HasChatEffort(efforts, session.Effort) { + session.Effort = "" + } + if !protocol.HasChatEffort(efforts, fallback) { + fallback = "" + } + session.Efforts, session.DefaultEffort = efforts, fallback + return session +} + +// EffortState is the chat's current level with its model's levels. +func (m *chatManager) EffortState(ctx context.Context, sessionID string) (protocol.ChatEffortRequest, error) { + session, found, err := m.Session(ctx, sessionID) + if err != nil { + return protocol.ChatEffortRequest{}, err + } + if !found { + return protocol.ChatEffortRequest{}, errors.New("unknown chat session " + sessionID) + } + return effortState(session), nil +} + +func effortState(session protocol.ChatSession) protocol.ChatEffortRequest { + return protocol.ChatEffortRequest{SessionID: session.ID, Effort: session.Effort, Efforts: session.Efforts, DefaultEffort: session.DefaultEffort} +} + +// SetEffort validates a level against the chat's current model, hands it to +// the harness and, for Codex and Claude, stores it so it survives a restart +// and is re-applied before every later turn. +func (m *chatManager) SetEffort(ctx context.Context, sessionID, effort string) (protocol.ChatEffortRequest, error) { + adapter, err := m.brokerFor(ctx, sessionID) + if err != nil { + return protocol.ChatEffortRequest{}, err + } + efforter, ok := adapter.(chatEfforter) + if !ok { + return protocol.ChatEffortRequest{}, errChatEffortsUnsupported + } + state, err := m.EffortState(ctx, sessionID) + if err == nil && len(state.Efforts) == 0 { + // A harness whose catalog arrives with its process (Claude Code) + // loads it on an explicit choice, never just to list a row. + if modeler, ok := adapter.(chatModeler); ok { + if _, err := modeler.Models(ctx, sessionID); err == nil { + state, err = m.EffortState(ctx, sessionID) + } + } + } + if err != nil { + return protocol.ChatEffortRequest{}, err + } + if len(state.Efforts) == 0 { + return protocol.ChatEffortRequest{}, errChatEffortsNone + } + if effort != "" && !protocol.HasChatEffort(state.Efforts, effort) { + return protocol.ChatEffortRequest{}, errors.New("thinking level " + effort + " is not offered by this chat's model") + } + if isModelBackend(m.backendFor(ctx, sessionID)) { + registry, ok := m.registry.(chatEffortRegistry) + if !ok { + return protocol.ChatEffortRequest{}, errors.New("durable chat thinking levels are not configured") + } + if err := efforter.SetEffort(ctx, sessionID, effort); err != nil { + return protocol.ChatEffortRequest{}, err + } + if err := registry.SetChatEffort(ctx, sessionID, effort); err != nil { + return protocol.ChatEffortRequest{}, err + } + } else if err := efforter.SetEffort(ctx, sessionID, effort); err != nil { + return protocol.ChatEffortRequest{}, err + } + state.Effort = effort + return state, nil +} + +// applyStoredEffort hands a Codex or Claude chat's stored level to the harness +// before a turn; it covers a restart that lost the adapter's in-memory level. +func (m *chatManager) applyStoredEffort(ctx context.Context, adapter chatBroker, sessionID string) error { + efforter, ok := adapter.(chatEfforter) + if !ok || !isModelBackend(m.backendFor(ctx, sessionID)) { + return nil + } + effort := m.storedEffort(ctx, sessionID) + if effort == "" { + return nil + } + return efforter.SetEffort(ctx, sessionID, effort) +} + +// reconcileEffort runs after a saved model switch: a stored level the new +// model does not accept becomes that model's default (or the harness's own), +// so the next turn never sends a level the model refuses. +func (m *chatManager) reconcileEffort(ctx context.Context, sessionID string) error { + stored := m.storedEffort(ctx, sessionID) + if stored == "" { + return nil + } + adapter, err := m.brokerFor(ctx, sessionID) + if err != nil { + return err + } + efforter, ok := adapter.(chatEfforter) + registry, stores := m.registry.(chatEffortRegistry) + if !ok || !stores { + return nil + } + session, found, err := m.Session(ctx, sessionID) + if err != nil || !found || protocol.HasChatEffort(session.Efforts, stored) { + return err + } + next := session.DefaultEffort + if err := efforter.SetEffort(ctx, sessionID, next); err != nil { + return err + } + return registry.SetChatEffort(ctx, sessionID, next) +} + +// handleChatEffort sets one chat's thinking level and echoes the stored level +// with the model's levels. +func handleChatEffort(ctx context.Context, request protocol.Message, broker chatBroker, write func(protocol.Message) error) { + body := request.EffortRequest + if body == nil || !validChatSessionID(body.SessionID) { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "a valid chat session id is required"}) + return + } + setter, ok := broker.(interface { + SetEffort(context.Context, string, string) (protocol.ChatEffortRequest, error) + }) + if !ok { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: errChatEffortsUnsupported.Error()}) + return + } + reply, err := setter.SetEffort(ctx, body.SessionID, strings.TrimSpace(body.Effort)) + if err != nil { + _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) + return + } + _ = write(protocol.Message{Type: protocol.ChatEffortSelect, ID: request.ID, EffortRequest: &reply}) +} + +// chatModelsEffort is the chat's current level for a chat_models reply, nil +// when the broker cannot report one. +func chatModelsEffort(ctx context.Context, broker chatBroker, sessionID string) *protocol.ChatEffortRequest { + reporter, ok := broker.(interface { + EffortState(context.Context, string) (protocol.ChatEffortRequest, error) + }) + if !ok { + return nil + } + state, err := reporter.EffortState(ctx, sessionID) + if err != nil { + return nil + } + return &state +} diff --git a/internal/server/chat_efforts_test.go b/internal/server/chat_efforts_test.go new file mode 100644 index 00000000..f5a9eafc --- /dev/null +++ b/internal/server/chat_efforts_test.go @@ -0,0 +1,170 @@ +package server + +import ( + "context" + "errors" + "path/filepath" + "testing" + + "git.a-wan.net/alan/pipelines/internal/pipeline" + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// effortedChat reports levels per model and records the levels handed to it. +type effortedChat struct { + *selectedModelChat + levels map[string][]protocol.ChatEffort + defaults map[string]string + applied []string +} + +func (c *effortedChat) Efforts(_ context.Context, session protocol.ChatSession) ([]protocol.ChatEffort, string, error) { + return c.levels[session.Model], c.defaults[session.Model], nil +} + +func (c *effortedChat) SetEffort(_ context.Context, _, effort string) error { + if effort == "refused" { + return errors.New("harness refused") + } + c.applied = append(c.applied, effort) + return nil +} + +func effortFixture(t *testing.T, path, backend string) (*pipeline.Store, *effortedChat, string) { + t.Helper() + ctx := context.Background() + store, err := pipeline.Open(path) + if err != nil { + t.Fatal(err) + } + id := "01900000-0000-7000-8000-000000000003" + if err := store.RecordOperatorSession(ctx, id, "/fixture", "chat", protocol.ChatSessionTagOperator); err != nil { + t.Fatal(err) + } + if err := store.SetOperatorSessionBackend(ctx, id, backend); err != nil { + t.Fatal(err) + } + fake := &effortedChat{selectedModelChat: &selectedModelChat{fakeChat: scriptedChat()}, + levels: map[string][]protocol.ChatEffort{"thinker": {{ID: "low", Name: "Low"}, {ID: "high", Name: "High"}}, "other": {{ID: "high", Name: "High"}, {ID: "max", Name: "Max"}}}, + defaults: map[string]string{"thinker": "high", "other": "max"}, + } + fake.models = []protocol.ChatModel{{ID: "thinker", Provider: "prov"}, {ID: "other", Provider: "prov"}, {ID: "plain", Provider: "prov"}} + fake.sessionRecord = protocol.ChatSession{ID: id, Backend: backend, Model: "thinker", ModelProvider: "prov"} + fake.sessionFound = true + fake.sessions = []protocol.ChatSession{fake.sessionRecord} + return store, fake, id +} + +func TestChatEffortIsValidatedStoredAndReappliedAfterRestart(t *testing.T) { + ctx := context.Background() + path := filepath.Join(t.TempDir(), "chat.db") + store, fake, id := effortFixture(t, path, "codex") + manager := &chatManager{codex: fake, registry: store, cache: map[string]string{}} + var reply protocol.Message + capture := func(msg protocol.Message) error { reply = msg; return nil } + + session, _, err := manager.Session(ctx, id) + if err != nil || session.Effort != "" || session.DefaultEffort != "high" || len(session.Efforts) != 2 { + t.Fatalf("fresh row = %+v, %v", session, err) + } + for _, bad := range []string{"max", "refused"} { + handleChatEffort(ctx, protocol.Message{ID: "bad", EffortRequest: &protocol.ChatEffortRequest{SessionID: id, Effort: bad}}, manager, capture) + if reply.Type != protocol.Error { + t.Fatalf("level %q accepted: %+v", bad, reply) + } + } + handleChatEffort(ctx, protocol.Message{ID: "set", EffortRequest: &protocol.ChatEffortRequest{SessionID: id, Effort: "low"}}, manager, capture) + if reply.Type != protocol.ChatEffortSelect || reply.ID != "set" || reply.EffortRequest.Effort != "low" || len(reply.EffortRequest.Efforts) != 2 || reply.EffortRequest.DefaultEffort != "high" { + t.Fatalf("set reply = %+v", reply) + } + if len(fake.applied) != 1 || fake.applied[0] != "low" || fake.overrides != 0 { + t.Fatalf("applied %v, turns %d", fake.applied, fake.overrides) + } + handleChatModels(ctx, protocol.Message{ID: "models", ChatQuery: &protocol.ChatQuery{SessionID: id}}, manager, capture) + if reply.Type != protocol.ChatModels || reply.EffortRequest == nil || reply.EffortRequest.Effort != "low" { + t.Fatalf("models reply = %+v", reply) + } + if err := store.Close(); err != nil { + t.Fatal(err) + } + + // A restarted server lists the stored level and re-applies it before turns. + store, err = pipeline.Open(path) + if err != nil { + t.Fatal(err) + } + defer store.Close() + fake.applied = nil + manager = &chatManager{codex: fake, registry: store, cache: map[string]string{}} + list, err := manager.Sessions(ctx) + if err != nil || len(list) != 1 || list[0].Effort != "low" || len(list[0].Efforts) != 2 { + t.Fatalf("restarted list = %+v, %v", list, err) + } + 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] != "low" || fake.applied[1] != "low" { + t.Fatalf("stored level not re-applied before turns: %v", fake.applied) + } + + // A model that keeps the level keeps it; one without it falls back to its + // default; one without levels clears it and offers no control. + for _, step := range []struct{ model, stored string }{{"thinker", "low"}, {"other", "max"}, {"plain", ""}} { + handleChatModelSwitch(ctx, protocol.Message{ID: "m", ModelRequest: &protocol.ChatModelRequest{SessionID: id, Provider: "prov", Model: step.model}}, manager, capture) + if reply.Type != protocol.ChatModelSelect { + t.Fatalf("switch reply = %+v", reply) + } + if stored, err := store.ChatEffort(ctx, id); err != nil || stored != step.stored { + t.Fatalf("after switching to %s stored %q, want %q (%v)", step.model, stored, step.stored, err) + } + } + session, _, err = manager.Session(ctx, id) + if err != nil || session.Effort != "" || len(session.Efforts) != 0 || session.DefaultEffort != "" { + t.Fatalf("level-less model row = %+v, %v", session, err) + } + handleChatEffort(ctx, protocol.Message{ID: "none", EffortRequest: &protocol.ChatEffortRequest{SessionID: id, Effort: "high"}}, manager, capture) + if reply.Type != protocol.Error || reply.Error != errChatEffortsNone.Error() { + t.Fatalf("level-less model accepted a level: %+v", reply) + } +} + +func TestOpenCodeEffortStaysNativeAndUnsupportedHarnessOffersNone(t *testing.T) { + ctx := context.Background() + store, fake, id := effortFixture(t, filepath.Join(t.TempDir(), "chat.db"), "opencode") + defer store.Close() + fake.sessionRecord.Effort = "high" + fake.sessions = []protocol.ChatSession{fake.sessionRecord} + manager := &chatManager{opencode: fake, registry: store, cache: map[string]string{}} + session, _, err := manager.Session(ctx, id) + if err != nil || session.Effort != "high" || len(session.Efforts) != 2 { + t.Fatalf("native level row = %+v, %v", session, err) + } + state, err := manager.SetEffort(ctx, id, "low") + if err != nil || state.Effort != "low" || len(fake.applied) != 1 { + t.Fatalf("native set = %+v, %v (applied %v)", state, err, fake.applied) + } + if stored, _ := store.ChatEffort(ctx, id); stored != "" { + t.Fatalf("OpenCode level stored by Pipelines: %q", stored) + } + // The variant is OpenCode's own state; a turn never re-sends it. + if err := manager.Prompt(ctx, id, "hi"); err != nil || len(fake.applied) != 1 { + t.Fatalf("turn re-applied a native level: %v %v", fake.applied, err) + } + + plain := scriptedChat() + plain.sessionRecord = protocol.ChatSession{ID: id, Backend: "opencode", Model: "thinker", ModelProvider: "prov", Effort: "high"} + plain.sessionFound = true + manager = &chatManager{opencode: plain, registry: store, cache: map[string]string{}} + session, _, err = manager.Session(ctx, id) + if err != nil || session.Effort != "" || len(session.Efforts) != 0 { + t.Fatalf("unsupported harness row = %+v, %v", session, err) + } + var reply protocol.Message + handleChatEffort(ctx, protocol.Message{ID: "x", EffortRequest: &protocol.ChatEffortRequest{SessionID: id, Effort: "high"}}, manager, func(msg protocol.Message) error { reply = msg; return nil }) + if reply.Type != protocol.Error || reply.Error != errChatEffortsUnsupported.Error() { + t.Fatalf("unsupported harness reply = %+v", reply) + } +} diff --git a/internal/server/chat_manager.go b/internal/server/chat_manager.go index 82548df6..79ebed25 100644 --- a/internal/server/chat_manager.go +++ b/internal/server/chat_manager.go @@ -395,7 +395,7 @@ func (m *chatManager) CreateBackend(ctx context.Context, title, directory, backe m.mu.Unlock() // The created row names its mode like a listed one, so the opened chat // shows the mode control at once rather than after the list re-reads. - return m.withMode(ctx, session), nil + return m.withEffort(ctx, m.withMode(ctx, session)), nil } func (m *chatManager) Create(ctx context.Context, title, directory string) (protocol.ChatSession, error) { @@ -414,6 +414,9 @@ func (m *chatManager) PromptWithMessageID(ctx context.Context, sessionID, text, if err := m.applyStoredMode(ctx, adapter, sessionID); err != nil { return err } + if err := m.applyStoredEffort(ctx, adapter, sessionID); err != nil { + return err + } if isModelBackend(m.backendFor(ctx, sessionID)) { model, err := m.promptModel(ctx, adapter, sessionID) if err != nil { @@ -469,6 +472,9 @@ func (m *chatManager) PromptWithMessageIDFiles(ctx context.Context, sessionID, t if err := m.applyStoredMode(ctx, adapter, sessionID); err != nil { return err } + if err := m.applyStoredEffort(ctx, adapter, sessionID); err != nil { + return err + } if isModelBackend(m.backendFor(ctx, sessionID)) { model, err := m.promptModel(ctx, adapter, sessionID) if err != nil { @@ -582,7 +588,10 @@ func (m *chatManager) SwitchModel(ctx context.Context, sessionID, provider, mode if err := modeler.SwitchModel(ctx, sessionID, provider, model); err != nil { return err } - return registry.SetChatModelSelection(ctx, sessionID, provider, model) + if err := registry.SetChatModelSelection(ctx, sessionID, provider, model); err != nil { + return err + } + return m.reconcileEffort(ctx, sessionID) } return modeler.SwitchModel(ctx, sessionID, provider, model) } @@ -609,7 +618,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 + return m.withEffort(ctx, session), nil } provider, model, err := m.chatModelSelection(ctx, session.ID) if err != nil { @@ -619,7 +628,7 @@ func (m *chatManager) withModelSelection(ctx context.Context, session protocol.C session.ModelProvider, session.Model = provider, model session.ModelSelection = "next_turn" } - return session, nil + return m.withEffort(ctx, session), nil } func (m *chatManager) Stream(ctx context.Context, sessionID string, ready func(), emit func(protocol.ChatEventFrame) error) error { diff --git a/internal/server/chat_stream.go b/internal/server/chat_stream.go index 50761799..fe6172eb 100644 --- a/internal/server/chat_stream.go +++ b/internal/server/chat_stream.go @@ -181,7 +181,7 @@ func handleChatModels(ctx context.Context, request protocol.Message, broker chat _ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: err.Error()}) return } - _ = write(protocol.Message{Type: protocol.ChatModels, ID: request.ID, ChatModels: models}) + _ = write(protocol.Message{Type: protocol.ChatModels, ID: request.ID, ChatModels: models, EffortRequest: chatModelsEffort(ctx, broker, query.SessionID)}) } // handleChatModelSwitch changes one session's model for subsequent turns.