Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(agent): discover Grok reasoning effort catalog (#7252)

Bowser1cff79d2562f
diff --git a/server/pkg/agent/grok_test.go b/server/pkg/agent/grok_test.go
index e8f73b8686..7c41013729 100644
--- a/server/pkg/agent/grok_test.go
+++ b/server/pkg/agent/grok_test.go
@@ -77,7 +77,7 @@ while IFS= read -r line; do
printf '{"jsonrpc":"2.0","id":%s,"error":{"code":-32000,"message":"authenticate must complete first"}}\n' "$id"
exit 0
fi
- printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"ses_new","models":{"availableModels":[{"modelId":"grok-4.6","name":"Grok 4.6","description":""},{"modelId":"grok-4.5","name":"Grok 4.5","description":""},{"modelId":"grok-composer-2.5-fast","name":"Grok Composer 2.5 Fast","description":""}],"currentModelId":"grok-4.6"}}}\n' "$id"
+ printf '{"jsonrpc":"2.0","id":%s,"result":{"sessionId":"ses_new","models":{"availableModels":[{"modelId":"grok-4.6","name":"Grok 4.6","_meta":{"supportsReasoningEffort":true,"reasoningEfforts":[{"id":"xhigh","value":"xhigh","label":"Extra High Effort","default":false},{"id":"high","value":"high","label":"High Effort","default":true},{"id":"medium","value":"medium","label":"Medium Effort","default":false},{"id":"low","value":"low","label":"Low Effort","default":false}]}},{"modelId":"grok-4.5","name":"Grok 4.5","_meta":{"supportsReasoningEffort":true,"reasoningEfforts":[{"id":"high","value":"high","label":"High Effort","default":true},{"id":"medium","value":"medium","label":"Medium Effort","default":false},{"id":"low","value":"low","label":"Low Effort","default":false}]}},{"modelId":"grok-composer-2.5-fast","name":"Grok Composer 2.5 Fast"}],"currentModelId":"grok-4.6"}}}\n' "$id"
;;
*'"method":"session/load"'*)
if [ -z "$authenticated" ]; then
@@ -753,18 +753,21 @@ func TestDiscoverGrokModelsWaitsForAdvertisedAuth(t *testing.T) {
if len(catalog.Models) != 3 || catalog.Models[0].ID != "grok-4.6" {
t.Fatalf("unexpected models: %+v", catalog.Models)
}
- if catalog.Models[0].Thinking == nil {
- t.Fatal("discovered grok-4.6 must advertise documented effort levels")
+ if catalog.Fallback {
+ t.Error("a successful ACP discovery must not be marked Fallback")
}
- got := make([]string, 0, len(catalog.Models[0].Thinking.SupportedLevels))
- for _, level := range catalog.Models[0].Thinking.SupportedLevels {
- got = append(got, level.Value)
+ byID := make(map[string]*ModelThinking, len(catalog.Models))
+ for _, model := range catalog.Models {
+ byID[model.ID] = model.Thinking
}
- if strings.Join(got, ",") != "low,medium,high,xhigh" {
- t.Fatalf("discovered grok-4.6 levels = %v, want low/medium/high/xhigh", got)
+ if got := thinkingValues(byID["grok-4.6"]); strings.Join(got, ",") != "xhigh,high,medium,low" || byID["grok-4.6"].DefaultLevel != "high" {
+ t.Fatalf("grok-4.6 thinking = %+v, want advertised catalog with high default", byID["grok-4.6"])
}
- if catalog.Fallback {
- t.Error("a successful ACP discovery must not be marked Fallback")
+ if got := thinkingValues(byID["grok-4.5"]); strings.Join(got, ",") != "high,medium,low" || byID["grok-4.5"].DefaultLevel != "high" {
+ t.Fatalf("grok-4.5 thinking = %+v, want advertised catalog with high default", byID["grok-4.5"])
+ }
+ if byID["grok-composer-2.5-fast"] != nil {
+ t.Fatalf("model without vendor reasoning metadata got %+v", byID["grok-composer-2.5-fast"])
}
raw, err := os.ReadFile(requestsFile)
if err != nil {
@@ -911,20 +914,6 @@ func TestGrokSelectAuthMethod(t *testing.T) {
}
}
-func TestGrokIsKnownThinkingValue(t *testing.T) {
- t.Parallel()
- for _, level := range []string{"", "low", "medium", "high", "xhigh"} {
- if !IsKnownThinkingValue("grok", level) {
- t.Errorf("IsKnownThinkingValue(grok, %q) = false", level)
- }
- }
- for _, level := range []string{"none", "minimal", "bogus", "max"} {
- if IsKnownThinkingValue("grok", level) {
- t.Errorf("IsKnownThinkingValue(grok, %q) = true, want rejected", level)
- }
- }
-}
-
func grokMustFindModel(t *testing.T, models []Model, id string) Model {
t.Helper()
for _, model := range models {
diff --git a/server/pkg/agent/models.go b/server/pkg/agent/models.go
index 60279fb10a..4b61256035 100644
--- a/server/pkg/agent/models.go
+++ b/server/pkg/agent/models.go
@@ -2338,6 +2338,7 @@ func discoverGrokModels(ctx context.Context, runtimeCmd Command) (Catalog, error
clientName: "multica-model-discovery",
tmpdirPrefix: "multica-grok-discovery-",
acpArgs: []string{"--no-auto-update", "agent", "--always-approve", "stdio"},
+ annotate: annotateGrokThinkingFromACP,
selectAuthMethod: func(initResult json.RawMessage, childEnv []string) (string, error) {
return selectGrokAuthMethod(extractACPAuthMethods(initResult), envHasNonEmpty(childEnv, "XAI_API_KEY"))
},
@@ -2354,7 +2355,6 @@ func discoverGrokModels(ctx context.Context, runtimeCmd Command) (Catalog, error
models[i].Provider = "xai"
}
}
- annotateGrokThinking(models)
return Catalog{Models: models}, nil
}
@@ -2373,13 +2373,14 @@ func grokStaticModels() []Model {
// annotateGrokThinking attaches only capabilities confirmed by xAI's
// per-model reasoning documentation and Grok Build's `--effort` flag.
-// session/new does not advertise effort catalogs on the versions this
-// table covers, so unknown and composer models deliberately keep Thinking
-// nil instead of exposing values that may fail at runtime.
+// Unknown and composer models deliberately keep Thinking nil instead of
+// exposing values that may fail at runtime. Successful discovery replaces
+// these fallback catalogs with the installed CLI's advertised values.
//
// grok-4.6 documents and accepts `xhigh` (docs.x.ai/developers/grok-4-6,
-// grok 1.0.5 `--effort`). grok-4.5 does not; the server enum lets the
-// token through and ValidateThinkingLevel still fails it closed for 4.5.
+// grok 1.0.5 `--effort`). grok-4.5 does not; the server's dynamic literal
+// gate lets the token through and ValidateThinkingLevel still fails it closed
+// for 4.5 using the per-model catalog.
func annotateGrokThinking(models []Model) {
for i := range models {
switch models[i].ID {
@@ -2403,6 +2404,65 @@ func grokThinkingCatalog(includeXHigh bool) *ModelThinking {
return &ModelThinking{SupportedLevels: levels}
}
+// annotateGrokThinkingFromACP fills in each model's effort catalog from the
+// xAI vendor `_meta` block on its `session/new` entry:
+//
+// {"modelId": "grok-4.6", "_meta": {"supportsReasoningEffort": true,
+// "reasoningEfforts": [{"value": "high", "label": "High Effort", "default": true}, ...]}}
+//
+// This extension is outside the core ACP schema, so the parse stays narrow:
+// models whose entry does not advertise it keep Thinking nil, which hides the
+// picker instead of offering levels the CLI may reject.
+func annotateGrokThinkingFromACP(models []Model, sessionResult json.RawMessage) {
+ var resp struct {
+ Models struct {
+ AvailableModels []struct {
+ ModelID string `json:"modelId"`
+ Meta struct {
+ SupportsReasoningEffort bool `json:"supportsReasoningEffort"`
+ ReasoningEfforts []struct {
+ Value string `json:"value"`
+ Label string `json:"label"`
+ Default bool `json:"default"`
+ } `json:"reasoningEfforts"`
+ } `json:"_meta"`
+ } `json:"availableModels"`
+ } `json:"models"`
+ }
+ if err := json.Unmarshal(sessionResult, &resp); err != nil {
+ return
+ }
+ thinkingByModel := map[string]*ModelThinking{}
+ for _, entry := range resp.Models.AvailableModels {
+ if !entry.Meta.SupportsReasoningEffort {
+ continue
+ }
+ thinking := &ModelThinking{}
+ seen := map[string]bool{}
+ for _, effort := range entry.Meta.ReasoningEfforts {
+ value := strings.TrimSpace(effort.Value)
+ if value == "" || seen[value] || !isValidDynamicThinkingValue(value) {
+ continue
+ }
+ seen[value] = true
+ label := strings.TrimSpace(effort.Label)
+ if label == "" {
+ label = value
+ }
+ thinking.SupportedLevels = append(thinking.SupportedLevels, ThinkingLevel{Value: value, Label: label})
+ if effort.Default {
+ thinking.DefaultLevel = value
+ }
+ }
+ if len(thinking.SupportedLevels) > 0 {
+ thinkingByModel[strings.TrimSpace(entry.ModelID)] = thinking
+ }
+ }
+ for i := range models {
+ models[i].Thinking = thinkingByModel[models[i].ID]
+ }
+}
+
// discoverCursorModels runs `cursor-agent --list-models` and parses
// the `id - Label` rows. Cursor's catalog changes often and ships
// many variants of the same base model (thinking / fast / max
diff --git a/server/pkg/agent/thinking.go b/server/pkg/agent/thinking.go
index 8418ab7f5e..e5287887c9 100644
--- a/server/pkg/agent/thinking.go
+++ b/server/pkg/agent/thinking.go
@@ -773,16 +773,6 @@ var providerThinkingEnums = map[string]map[string]bool{
"xhigh": true,
"max": true,
},
- // Grok CLI --effort vocabulary (grok 1.0.5: low, medium, high, xhigh).
- // Grok cannot disable reasoning and does not accept none, minimal, or
- // max. xhigh is grok-4.6-only; the daemon's per-model catalog rejects
- // it for grok-4.5 after this server-side literal gate.
- "grok": {
- "low": true,
- "medium": true,
- "high": true,
- "xhigh": true,
- },
// Pi owns a fixed CLI vocabulary; RPC discovery narrows this universe to
// the exact subset supported by each model before execution.
"pi": {
@@ -801,8 +791,12 @@ var providerThinkingEnums = map[string]map[string]bool{
// server accepts any well-formed token for them and lets the daemon's
// per-model check decide before execution.
var thinkingDynamicCatalogProviders = map[string]bool{
- "codex": true,
- "dsh": true,
+ "codex": true,
+ "dsh": true,
+ // Grok advertises each model's effort catalog through session/new, so the
+ // server does not maintain a provider-wide fixed enum. The daemon applies
+ // the selected effort with `--effort`, not session/set_config_option.
+ "grok": true,
"opencode": true,
"kimi": true,
}
diff --git a/server/pkg/agent/thinking_test.go b/server/pkg/agent/thinking_test.go
index 5114fec2cd..2738818568 100644
--- a/server/pkg/agent/thinking_test.go
+++ b/server/pkg/agent/thinking_test.go
@@ -520,14 +520,15 @@ func TestIsKnownThinkingValue(t *testing.T) {
// literal gate opens for both.
{"hermes", "low", true},
{"grok", "", true},
+ {"grok", "none", true},
+ {"grok", "minimal", true},
{"grok", "low", true},
{"grok", "medium", true},
{"grok", "high", true},
{"grok", "xhigh", true},
- {"grok", "none", false},
- {"grok", "minimal", false},
- {"grok", "max", false},
- {"grok", "bogus", false},
+ {"grok", "future-level", true}, // exact support is checked against the daemon catalog
+ {"grok", ".hidden", false},
+ {"grok", "bad value", false},
}
for _, tc := range tests {
if got := IsKnownThinkingValue(tc.provider, tc.value); got != tc.want {