zcode: a level's thinking carries its budget, and the user's own level options stay (#3852)
Review of d7e1e284f, two things it changed beyond its case:
- Every level that thinks was written as {"thinking":{"type":"enabled"}}
with no budget_tokens. ZCode puts a level's options in the request, so
a Claude before 4.6 (Sonnet 4.5, Haiku 4.5, a relay's) asked through
magpie at any level would be sent "enabled" without the budget
Anthropic's Messages API requires with it, a 400 it didn't get before.
Each level now carries the budget the gateway reads that effort as
(gateway.ThinkingBudget, budgetOf: low 4096 ... max 32000). The
gateway already makes it adaptive for Claude 4.6 and later
(adaptiveThinking; ZCode's output_config.effort wins over the budget)
and fits it under max_tokens (maxoutput.go).
- providerOptionsByLevel was written over on every sync. A user who put
their own there (the workaround for this very bug) lost it. A level's
options already in config.json now stay; no magpie before #3852
wrote any, so whatever is there is the user's.
Tests: TestZCodeOutputAndReasoning expects the budgets;
TestZCodeKeepsTheUsersLevelOptions keeps a hand-written high level and
fills the others.
Run on mote: go test -run TestZCode ./internal/agent, ok. Broken by
hand: the kept options ignored and the budget left out, and those two
tests fail.
Not tried: a real ZCode 3.x (this Mac has Z Code 0.9.1, a different
app; mote has none), so ZCode putting the options in the body is the
reporter's and the author's reading of zcode.cjs, not seen here.
Batch record (gua's #2833 #3052 #3205 #3867 #4107 #4247 #4245 #4113
#3852 and the three review follow-ups; #4106 left out, its test isn't
red without its fix, see the issue):
- Full run on mote before the rebase (base fa4704f06): gofmt -l clean;
go vet on darwin, GOOS=linux -tags nogui and GOOS=windows clean;
go test -count=1 -timeout 45m ./...: 85 packages ok, internal/gui red
in TestSettingsSaveKeepsWhatItDoesNotSend (#4514) and
TestMainWindowCorners/macOS_26_SDK (#3949), both already filed, and
neither in a file this batch touches.
- Rebased on origin/main 9162277b9, at this tree, on mote: gofmt clean;
go test -count=1 -timeout 45m ./internal/gateway/... ./internal/agent
./internal/library ./internal/handoff ./internal/routes, all ok
(and ./internal/redact ok at the base before, b2145eac1). Then
rebased again on ccb75030a (#4371, fastjson.go only) and
./internal/gateway/... run again at that tree: ok (636s). Pushed on 8ac1c7a56 (internal/catalog
only), where catalog, handoff and library ran whole and the batch's
gateway (Bili, TestRuleCompact, Kimi, TestClaudeMem, the two stall
tests) and agent (TestZCode) tests ran: all ok. The whole gateway
package was not run again on top of 8ac1c7a56.
- Windows (win-run): -run TestClaudeMem ./internal/gateway ok; with
claude_room.go put back, TestClaudeMemRunsFromTheCgroup hangs in
readLimit to the 5m timeout.
- Linux (wsl-run): ./internal/handoff ./internal/library ok; with the
fixes put back, TestGiveKeepsTheListenerNonblocking,
TestSaveDropsEmptyApplied and TestSyncAloneLeavesNoEmptyApplied fail.
Not tried: real Kimi, ZCode 3.x, Alma or bili accounts; #4245 is a
test-only change and was not reproduced red.
This commit is contained in:
@@ -328,3 +328,49 @@ func TestMiniMaxKeepsTheUsersDefaultEffort(t *testing.T) {
|
||||
t.Fatalf("after sync:\n%s", raw)
|
||||
}
|
||||
}
|
||||
|
||||
// ZCode's config.json: providerOptionsByLevel a user wrote for a level
|
||||
// (the workaround #3852's reporter could have had in place) stays through
|
||||
// a sync; the levels they left alone get magpie's.
|
||||
func TestZCodeKeepsTheUsersLevelOptions(t *testing.T) {
|
||||
home := keptEffortHome(t)
|
||||
path := filepath.Join(home, ".zcode", "v2", "config.json")
|
||||
os.MkdirAll(filepath.Dir(path), 0o755)
|
||||
a := zcode(home)
|
||||
if err := a.Field("provider").Set(magpieID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var doc map[string]any
|
||||
b, _ := os.ReadFile(path)
|
||||
json.Unmarshal(b, &doc)
|
||||
r := doc["provider"].(map[string]any)[magpieID].(map[string]any)["models"].(map[string]any)["think/deep"].(map[string]any)["reasoning"].(map[string]any)
|
||||
r["providerOptionsByLevel"] = map[string]any{"high": map[string]any{"thinking": map[string]any{"type": "adaptive"}}}
|
||||
b, _ = json.Marshal(doc)
|
||||
os.WriteFile(path, b, 0o644)
|
||||
if err := a.Sync(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var c struct {
|
||||
Provider map[string]struct {
|
||||
Models map[string]struct {
|
||||
Reasoning struct {
|
||||
Options map[string]struct {
|
||||
Thinking struct {
|
||||
Type string `json:"type"`
|
||||
Budget int `json:"budget_tokens"`
|
||||
} `json:"thinking"`
|
||||
} `json:"providerOptionsByLevel"`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
b, _ = os.ReadFile(path)
|
||||
json.Unmarshal(b, &c)
|
||||
o := c.Provider[magpieID].Models["think/deep"].Reasoning.Options
|
||||
if o["high"].Thinking.Type != "adaptive" || o["high"].Thinking.Budget != 0 {
|
||||
t.Fatalf("the user's high level was written over: %s", b)
|
||||
}
|
||||
if o["low"].Thinking.Type != "enabled" || o["low"].Thinking.Budget != 4096 || o["disabled"].Thinking.Type != "disabled" {
|
||||
t.Fatalf("the levels the user left alone: %s", b)
|
||||
}
|
||||
}
|
||||
|
||||
+42
-8
@@ -26,7 +26,7 @@ import (
|
||||
// "enabled":true,"source":"custom",
|
||||
// "models":{"<id>":{"name":…,"limit":{"context":…,"output":…},"modalities":{…},
|
||||
// "reasoning":{"enabled":true,"levels":[…],"defaultLevel":…,
|
||||
// "providerOptionsByLevel":{"<level>":{"thinking":{"type":"enabled"}}}}}}}}}
|
||||
// "providerOptionsByLevel":{"<level>":{"thinking":{"type":"enabled","budget_tokens":…}}}}}}}}}
|
||||
//
|
||||
// An anthropic provider is asked at baseURL + /v1/messages. The model is
|
||||
// picked per task in ZCode's own picker and kept in its window, not in a
|
||||
@@ -130,7 +130,7 @@ func zcode(home string) *Agent {
|
||||
// turned off in ZCode stays off. A sync (keep) leaves an address on another
|
||||
// machine as it found it (zcodeAddress).
|
||||
func zcodeProviderJSON(path string, keep bool) any {
|
||||
was := zcodeDefaultLevels(path)
|
||||
was, own := zcodeDefaultLevels(path), zcodeLevelOptions(path)
|
||||
ms := map[string]any{}
|
||||
for _, m := range sharedModels("zcode") {
|
||||
window := m.Context
|
||||
@@ -151,7 +151,7 @@ func zcodeProviderJSON(path string, keep bool) any {
|
||||
if levels := zcodeLevels(m.Efforts); levels != nil {
|
||||
e["reasoning"] = map[string]any{"enabled": true, "levels": levels,
|
||||
"defaultLevel": keptEffort(was[m.ID], levels, zcodeDefaultLevel(levels)),
|
||||
"providerOptionsByLevel": zcodeOptionsByLevel(levels)}
|
||||
"providerOptionsByLevel": zcodeOptionsByLevel(levels, own[m.ID])}
|
||||
}
|
||||
ms[m.ID] = e
|
||||
}
|
||||
@@ -354,14 +354,48 @@ func zcodeLevels(efforts []string) []string {
|
||||
// model's reasoning levels: thinking off for the level named "disabled",
|
||||
// on for every other (#3852 — with no provider options for a level ZCode
|
||||
// asks the model without thinking, and an always-thinking model refuses).
|
||||
func zcodeOptionsByLevel(levels []string) map[string]any {
|
||||
// Thinking on carries the level's budget, as Anthropic's Messages API asks
|
||||
// of a Claude before 4.6 (budget_tokens is required with "enabled"); the
|
||||
// gateway makes it adaptive for a later Claude and fits it under the
|
||||
// reply's limit. A level's options already in config.json are the user's
|
||||
// and stay: no magpie before #3852 wrote any.
|
||||
func zcodeOptionsByLevel(levels []string, own map[string]any) map[string]any {
|
||||
out := map[string]any{}
|
||||
for _, l := range levels {
|
||||
t := "enabled"
|
||||
if l == "disabled" {
|
||||
t = "disabled"
|
||||
if o, ok := own[l]; ok {
|
||||
out[l] = o
|
||||
continue
|
||||
}
|
||||
th := map[string]any{"type": "disabled"}
|
||||
if l != "disabled" {
|
||||
th = map[string]any{"type": "enabled", "budget_tokens": gateway.ThinkingBudget(l)}
|
||||
}
|
||||
out[l] = map[string]any{"thinking": th}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// zcodeLevelOptions are the providerOptionsByLevel each of magpie's models
|
||||
// has in config.json at path, which zcodeOptionsByLevel keeps.
|
||||
func zcodeLevelOptions(path string) map[string]map[string]any {
|
||||
var c struct {
|
||||
Provider map[string]struct {
|
||||
Models map[string]struct {
|
||||
Reasoning struct {
|
||||
Options map[string]any `json:"providerOptionsByLevel"`
|
||||
} `json:"reasoning"`
|
||||
} `json:"models"`
|
||||
} `json:"provider"`
|
||||
}
|
||||
b, _ := edit.Read(path)
|
||||
if json.Unmarshal(b, &c) != nil {
|
||||
return nil
|
||||
}
|
||||
out := map[string]map[string]any{}
|
||||
for id, m := range c.Provider[magpieID].Models {
|
||||
if len(m.Reasoning.Options) > 0 {
|
||||
out[id] = m.Reasoning.Options
|
||||
}
|
||||
out[l] = map[string]any{"thinking": map[string]any{"type": t}}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
@@ -221,8 +221,10 @@ func TestZCodeOutputAndReasoning(t *testing.T) {
|
||||
for _, want := range []string{
|
||||
`"limit":{"context":204800,"output":128000}`,
|
||||
// the reasoning keys ZCode reads (#3852): levels and defaultLevel,
|
||||
// with thinking sent per level and off for the level named disabled
|
||||
`"reasoning":{"defaultLevel":"high","enabled":true,"levels":["disabled","low","high","max"],"providerOptionsByLevel":{"disabled":{"thinking":{"type":"disabled"}},"high":{"thinking":{"type":"enabled"}},"low":{"thinking":{"type":"enabled"}},"max":{"thinking":{"type":"enabled"}}}}`,
|
||||
// with thinking sent per level, at the level's budget (a Claude
|
||||
// before 4.6 refuses "enabled" without one), and off for the level
|
||||
// named disabled
|
||||
`"reasoning":{"defaultLevel":"high","enabled":true,"levels":["disabled","low","high","max"],"providerOptionsByLevel":{"disabled":{"thinking":{"type":"disabled"}},"high":{"thinking":{"budget_tokens":24000,"type":"enabled"}},"low":{"thinking":{"budget_tokens":4096,"type":"enabled"}},"max":{"thinking":{"budget_tokens":32000,"type":"enabled"}}}}`,
|
||||
`"GLM-5-Turbo":{"reasoning":{"enabled":true,"variants":["enabled","off"],"defaultVariant":"enabled"},"limit":{"context":200000,"output":64000}}`,
|
||||
} {
|
||||
if !strings.Contains(string(b), want) {
|
||||
|
||||
@@ -921,6 +921,10 @@ func budgetOf(effort string) int {
|
||||
return 10000
|
||||
}
|
||||
|
||||
// ThinkingBudget is the Anthropic thinking budget the gateway reads an
|
||||
// effort level as, for an agent's config that has to name one (ZCode's).
|
||||
func ThinkingBudget(effort string) int { return budgetOf(effortOf(effort)) }
|
||||
|
||||
// effortOfBudget goes the other way, for Anthropic clients asking others.
|
||||
func effortOfBudget(n int) string {
|
||||
switch {
|
||||
|
||||
Reference in New Issue
Block a user