Files
magpie/internal/agent/tune.go
T
yetone 3eb2bda4f2 settings: Codex, Claude Code and Grok Build each take a Long conversations of their own (molianyin4227 on Discord)
molianyin4227 (Discord #help): models differ, so one compaction size
for every agent fits none; they asked for Codex, Claude Code and Grok
Build to each have their own, falling back to the shared one.

- settings.AgentCompacts[agent] {At, Full} for the agents that write a
  compaction window (CompactAgents: codex, claude, grok); Settings.
  ForAgent(agent) lays it over CompactAt/FullContext for that agent's
  readers only: codexcat.entriesAt, provider codexListed, Claude Code's
  writeCompact and desktopPlan, grokModelTables, the route list's
  buildModels, Codex's window line (api.go) and Tuned for you
  (gui/tune.go, agent/tune.go). A model's or provider's own Compact at
  still comes first. An agent with none follows the shared value, so
  nothing new is written for it, and an agent outside the three has no
  such key and is refused (provider.SetAgentCompact).
- POST /api/settings/agent-compact {agent, own, full, at}; own false
  takes the agent's value away. settingsapi.Merge keeps the map.
- GUI: a row "Long conversations in <agent>" under Long conversations,
  in the agent's opened row and its sheet: Every agent / Compact at N /
  Full window (segs, no select), a size box when Compact at. It lives in
  agentcompact.js, imported when the agent's settings hold Long
  conversations, then put in synchronously (an async put-in after a
  redraw moved the page in chromium: codex-memories-settings' scroll
  check). It draws again only when its own values change. i18n in
  zh, zh-TW, ja, de.
- Tuned for you's Use on an agent with its own value changes that one,
  stashed under tune.long.compact.<agent>; Undo puts it back.
- codex-memories-settings' "no Codex" check counts Long conversations
  alone again (:not(.agent-compact)) and waits for the new row.
- docs/subsystems/agent-wiring.md: a row for it.

Core size: app.js 1,311,919 bytes, 109 under origin/main 0ee424e3e
(1,312,028): the two calls are paid for by four comments cut. The core
total is 1,534,045, still over its budget as on main (#5139), 109 less
than main's 1,534,154. No budget raised.

Run at this tree (based on origin/main 0ee424e3e, fetched just before
the commit), all on mote via mote-run:
- go test settings, settingsapi, codexcat; provider/agent/gui with
  -run "Compact|Tune|Grok|SettingsSave|CodexWindow|Long": ok.
- New tests broken by hand, each red: ForAgent taken out of codexcat
  (TestCodexCatalogCompactAt), grok.go (TestGrokWindowFollowsLong
  Conversations), claude.go (TestClaudeCompactAt); tune's per-agent key
  turned off (TestTuneAgentOwnLong: "after apply: codex {At:0
  Full:true}"); the row's import call taken out of app.js
  (agent-compact.test.cjs 4 of 4 red in chromium).
- GUI, chromium + webkit (en/zh/zh-TW/ja/de as each file has, narrow
  and wide): agent-compact, agent-prefs, compact-at, compact-one-value,
  settings-general, codex-async-compact, codex-compact-held,
  codex-memories-settings, context-tune, segs-glide, i18n-keys,
  old-webkit: 432 of 432 ok.
- gofmt -l clean; go vet darwin and GOOS=windows on mote, linux with
  -tags gtk3 on magpie-desktop (wsl-run): ok.
- TestCoreScriptBudget: red, 1,534,045 > 1,533,802, as on main (#5139),
  109 bytes lower than main.

Not tried: real Codex, Claude Code or Grok Build reading the values
(the config writers are unit-tested); the full go test ./... (after
the push). `magpie model compact` has no per-agent flag, and t3code's
route list reads the shared value.
2026-11-04 16:17:47 +08:00

564 lines
18 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package agent
// The settings tune advises (internal/tune), as each agent's config has
// them, and putting one in on the user's say: Claude Code's auto-compact
// window for a model and its cache lifetimes, and where Codex and Claude
// Code compact. What was there before is stashed, so Undo puts it back; a
// variable that takes precedence over the setting is told of, never
// written over.
//
// Where Codex compacts, and Claude Code wherever magpie's own window is
// what it runs at, is one value: Long conversations (settings.CompactAt,
// FullContext), which Sync writes into each agent's config. Tune reads and
// sets that value, so the Usage page and the agents' settings never show
// two numbers (7eleven on Discord). Before, tune wrote Codex's
// model_auto_compact_token_limit beside it; tuneMigrateCodex moves such a
// value into Long conversations. An agent with a Long conversations of
// its own (settings.AgentCompacts) has tune read and set that one.
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
)
// The settings tune knows.
const (
TuneCompact = "compact" // when the agent compacts its conversation, in tokens
TuneCache = "cache_ttl" // Claude Code: the main conversation's cache, 5m or 1h
TuneSubCache = "subagent_cache_ttl" // Claude Code: its subagents', workflows' and helpers'
// Codex (0.163 on): the share of a model's window past which it
// compacts in the background while the turn goes on, 1–100; its
// foreground limit stays where it is, as the backstop
TuneAsync = "async_compact"
)
// TuneSetting is one of those settings as an agent has it now.
type TuneSetting struct {
Value string `json:"value,omitempty"` // "" when nothing sets it
// Where Value comes from: settings (the agent's own config file),
// model (Claude Code's modelSettings for the model), env, or long
// (TuneLong: magpie's Long conversations, "" under Full window)
Source string `json:"source,omitempty"`
// Locked names the variable that takes precedence over the setting,
// which tune leaves as it is: putting the advice in would change nothing
Locked string `json:"locked,omitempty"`
// Ours: tune put Value in, and Undo puts back what was there
Ours bool `json:"ours,omitempty"`
}
// TuneLong is the Source of a window that is magpie's Long conversations,
// which Apply and Undo change in magpie's settings, not in the agent's file.
const TuneLong = "long"
// codexCompactKey is the key tune wrote Codex's window to before it went
// to Long conversations; one the user wrote comes before magpie's window.
const codexCompactKey = "model_auto_compact_token_limit"
// tuneLongKey is the stash of Long conversations as it was before tune
// put a window in it, shared by every agent's card, as the value is.
const tuneLongKey = "tune.long.compact"
type tuneLong struct {
CompactAt int `json:"compactAt"`
Full bool `json:"full"`
Set int `json:"set"`
}
// longKey is where Long conversations as agent reads it is stashed: the
// one for every agent, or the agent's own (settings.AgentCompacts), which
// a Use puts the advice in instead.
func longKey(s settings.Settings, agent string) string {
if _, own := s.AgentCompacts[agent]; own {
return tuneLongKey + "." + agent
}
return tuneLongKey
}
func tuneLongStashed(key string) (tuneLong, bool) {
raw := stashShared()[key]
var st tuneLong
if raw == "" || json.Unmarshal([]byte(raw), &st) != nil {
return tuneLong{}, false
}
return st, true
}
// longValue is Long conversations' window as the setting reads: "" under
// Full window, each model's whole one.
func longValue(s settings.Settings) string {
if n := s.Compact(); n > 0 {
return strconv.Itoa(n)
}
return ""
}
// longOurs says Long conversations, as agent reads it, still holds the
// window tune put in.
func longOurs(s settings.Settings, agent string) bool {
st, ok := tuneLongStashed(longKey(s, agent))
a := s.ForAgent(agent)
return ok && !a.FullContext && a.Compact() == st.Set
}
// longApply puts n in Long conversations, what it was kept the first time
// for Undo. Sync then writes it into every agent magpie routes.
func longApply(agent string, n int) error {
s := settings.Load()
key, a := longKey(s, agent), s.ForAgent(agent)
st, ok := tuneLongStashed(key)
if !ok {
st = tuneLong{CompactAt: a.CompactAt, Full: a.FullContext}
}
st.Set = n
b, _ := json.Marshal(st)
stash(map[string]string{key: string(b)})
if key != tuneLongKey {
return provider.SetAgentCompact(agent, &settings.AgentCompact{At: n})
}
return provider.SetCompactAt(n) // Full window off too
}
// longUndo puts Long conversations back as it was before tune, unless the
// user changed it since: then it is theirs, and only forgotten.
func longUndo(agent string) error {
s := settings.Load()
key := longKey(s, agent)
st, ok := tuneLongStashed(key)
if !ok {
return fmt.Errorf("magpie didn't set Long conversations")
}
defer forget(key)
if !longOurs(s, agent) {
return nil
}
if key != tuneLongKey {
return provider.SetAgentCompact(agent, &settings.AgentCompact{At: st.CompactAt, Full: st.Full})
}
if st.Full {
return provider.SetFullContext(true)
}
return provider.SetCompactAt(st.CompactAt)
}
// tuneMigrateCodex moves a model_auto_compact_token_limit tune wrote into
// Codex's config.toml before (tune.codex.compact) into Long conversations,
// and puts back what the file had. Long conversations at magpie's default
// takes it, as the user's own choice; one the user set takes it only when
// it is lower, which is where Codex compacted (it stops at the lesser of
// the limit and the window it is told), so nothing changes for them. A
// limit the user changed after tune wrote it is theirs and stays.
func tuneMigrateCodex(home string) {
st, ok := tuneStashed("codex", TuneCompact, "")
if !ok {
return
}
p := tunePlace{path: filepath.Join(here(home).codexHome(), "config.toml"), key: codexCompactKey, toml: true}
defer forget(tuneKey("codex", TuneCompact, ""))
v, has := p.get()
if !has || strings.Trim(v, `"'`) != st.Set {
return
}
if n, err := strconv.Atoi(st.Set); err == nil && n > 0 {
s := settings.Load().ForAgent("codex")
if s.CompactAt == 0 || s.FullContext || n < s.Compact() {
if err := longApply("codex", n); err != nil {
return
}
}
}
if st.Had {
_ = edit.SetTOMLTop(p.path, edit.KV{Path: p.key, Value: tomlRaw(st.Raw)})
return
}
_ = edit.DelTOMLTop(p.path, p.key)
}
// tuneStash is what a setting was before tune put one in.
type tuneStash struct {
Had bool `json:"had"`
Raw string `json:"raw,omitempty"` // as the file had it, JSON or TOML
Set string `json:"set"` // the value tune put in
}
func tuneKey(agent, knob, model string) string {
k := "tune." + agent + "." + knob
if model != "" {
k += "." + model
}
return k
}
func tuneHome(home string) string {
if home != "" {
return home
}
h, _ := os.UserHomeDir()
return h
}
// tunePlace is where a setting lives: the file, its key path, and the
// variables that take precedence over it, first first.
type tunePlace struct {
path string
key string
toml bool
env []string
number bool
}
func tuneWhere(home, agent, knob, model string) (tunePlace, error) {
home = tuneHome(home)
switch agent {
case "claude":
path := filepath.Join(here(home).claudeDir(), "settings.json")
switch knob {
case TuneCompact:
n := claudeName(model)
if n == "" {
return tunePlace{}, fmt.Errorf("%q is no Claude model: Claude Code's window for it is magpie's own, under Settings", model)
}
return tunePlace{path: path, key: "modelSettings." + n + ".autoCompactWindow", env: []string{claudeCompactEnv}, number: true}, nil
case TuneCache:
return tunePlace{path: path, key: "promptCacheTtl", env: []string{"FORCE_PROMPT_CACHING_5M", "CLAUDE_CODE_PROMPT_CACHE_TTL"}}, nil
case TuneSubCache:
return tunePlace{path: path, key: "subagentPromptCacheTtl", env: []string{"FORCE_PROMPT_CACHING_5M", "CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL"}}, nil
}
case "codex":
if knob == TuneCompact {
// the user's own limit in config.toml, which comes before Long
// conversations; tune itself writes only the latter
return tunePlace{path: filepath.Join(here(home).codexHome(), "config.toml"), key: codexCompactKey, toml: true, number: true}, nil
}
}
return tunePlace{}, fmt.Errorf("%s has no setting %q tune can put in", agent, knob)
}
func (p tunePlace) get() (string, bool) {
if p.toml {
return edit.GetTOMLTop(p.path, p.key)
}
return edit.GetJSON(p.path, p.key)
}
// envOf is a variable Claude Code starts with: in settings.json's env, or
// magpie's own, which a terminal it is run from most likely has too.
func (p tunePlace) envOf(k string) string {
if v, _ := edit.GetJSON(p.path, "env."+k); v != "" {
return v
}
return os.Getenv(k)
}
// TuneRead is how an agent has a setting tune advises now.
func TuneRead(home, agent, knob, model string) (TuneSetting, error) {
if agent == "codex" && knob == TuneAsync {
return asyncRead(home), nil
}
p, err := tuneWhere(home, agent, knob, model)
if err != nil {
return TuneSetting{}, err
}
if agent == "codex" && knob == TuneCompact {
tuneMigrateCodex(home)
var s TuneSetting
if v, ok := p.get(); ok {
// the user's own limit: Codex stops at it before the window
// magpie tells it
s.Value, s.Source, s.Locked = strings.Trim(v, `"'`), "settings", codexCompactKey
return s, nil
}
st := settings.Load()
return TuneSetting{Value: longValue(st.ForAgent(agent)), Source: TuneLong, Ours: longOurs(st, agent)}, nil
}
var s TuneSetting
for _, k := range p.env {
if v := p.envOf(k); v != "" && v != "0" && v != "false" {
if k == claudeCompactEnv && claudeLongEnv(p.path, v) {
// as Long conversations has it now, which Sync is writing
st := settings.Load()
return TuneSetting{Value: longValue(st.ForAgent(agent)), Source: TuneLong, Ours: longOurs(st, agent)}, nil
}
s.Locked = k
s.Value, s.Source = v, "env"
if k == "FORCE_PROMPT_CACHING_5M" {
s.Value = "5m"
}
break
}
}
if s.Locked == "" {
if v, ok := p.get(); ok && v != "auto" {
s.Value, s.Source = strings.Trim(v, `"'`), "settings"
if knob == TuneCompact && agent == "claude" {
s.Source = "model"
}
} else if agent == "claude" && knob == TuneCompact {
// the top-level window serves every model with none of its
// own: magpie's Long conversations where magpie wrote it
if v, ok := edit.GetJSON(p.path, "autoCompactWindow"); ok && v != "auto" {
if claudeLongWindow(p.path, "autoCompactWindow", "claude.compact_default") {
return TuneSetting{Value: v, Source: TuneLong, Ours: longOurs(settings.Load(), agent)}, nil
}
s.Value, s.Source = v, "settings"
}
}
}
if st, ok := tuneStashed(agent, knob, model); ok && s.Locked == "" {
v, _ := p.get()
s.Ours = strings.Trim(v, `"'`) == st.Set
}
return s, nil
}
// claudeLongEnv says the CLAUDE_CODE_AUTO_COMPACT_WINDOW Claude Code has
// is the one magpie wrote in settings.json from Long conversations
// (writeCompact), not a provider's Compact at for the main model or the
// user's own.
func claudeLongEnv(path, v string) bool {
in, ok := edit.GetJSON(path, "env."+claudeCompactEnv)
m := stashLoad()
return ok && in == v && v == m["claude.compact_window"] && m["claude.compact_long"] == "1"
}
// claudeLongWindow says the window at key in Claude Code's settings.json
// is the one magpie wrote there (its stash under stashKey) from Long
// conversations, not a provider's Compact at for the main model or the
// user's own.
func claudeLongWindow(path, key, stashKey string) bool {
v, ok := edit.GetJSON(path, key)
if !ok || v == "" || v != stashShared()[stashKey] {
return false
}
return v == longValue(settings.Load().ForAgent("claude"))
}
func tuneStashed(agent, knob, model string) (tuneStash, bool) {
raw := stashShared()[tuneKey(agent, knob, model)]
if raw == "" {
return tuneStash{}, false
}
var st tuneStash
if json.Unmarshal([]byte(raw), &st) != nil {
return tuneStash{}, false
}
return st, true
}
// TuneApply puts value in as an agent's setting, on the user's say. What
// was there before is kept, the first time only, for TuneUndo. A variable
// that takes precedence is an error: the setting would change nothing.
func TuneApply(home, agent, knob, model, value string) error {
if agent == "codex" && knob == TuneAsync {
return asyncApply(home, value)
}
p, err := tuneWhere(home, agent, knob, model)
if err != nil {
return err
}
cur, _ := TuneRead(home, agent, knob, model)
if cur.Locked != "" {
return fmt.Errorf("%s=%s is set and comes before the setting: take it out first", cur.Locked, cur.Value)
}
if cur.Source == TuneLong {
n, err := strconv.Atoi(value)
if err != nil || n <= 0 {
return fmt.Errorf("%q is no token count", value)
}
if agent == "claude" && (n < 100000 || n > 1000000) {
return fmt.Errorf("Claude Code takes a window of 100000 to 1000000 tokens, not %d", n)
}
return longApply(agent, n)
}
var v any = value
if p.number {
n, err := strconv.Atoi(value)
if err != nil || n <= 0 {
return fmt.Errorf("%q is no token count", value)
}
if agent == "claude" && (n < 100000 || n > 1000000) {
return fmt.Errorf("Claude Code takes a window of 100000 to 1000000 tokens, not %d", n)
}
v = n
} else if value != "5m" && value != "1h" {
return fmt.Errorf("the cache is kept 5m or 1h, not %q", value)
}
key := tuneKey(agent, knob, model)
if _, ok := tuneStashed(agent, knob, model); !ok {
raw, had := p.get()
b, _ := json.Marshal(tuneStash{Had: had, Raw: raw, Set: value})
stash(map[string]string{key: string(b)})
} else {
st, _ := tuneStashed(agent, knob, model)
st.Set = value
b, _ := json.Marshal(st)
stash(map[string]string{key: string(b)})
}
if p.toml {
return edit.SetTOMLTop(p.path, edit.KV{Path: p.key, Value: v})
}
return edit.SetJSON(p.path, edit.KV{Path: p.key, Value: v})
}
// TuneUndo puts back what a setting was before tune put one in, unless
// the user has changed it since: then it is theirs, and only forgotten.
func TuneUndo(home, agent, knob, model string) error {
if agent == "codex" && knob == TuneAsync {
return asyncUndo()
}
p, err := tuneWhere(home, agent, knob, model)
if err != nil {
return err
}
if cur, _ := TuneRead(home, agent, knob, model); cur.Source == TuneLong {
return longUndo(agent)
}
st, ok := tuneStashed(agent, knob, model)
if !ok {
return fmt.Errorf("magpie didn't set %s", p.key)
}
defer forget(tuneKey(agent, knob, model))
if v, _ := p.get(); strings.Trim(v, `"'`) != st.Set {
return nil
}
if st.Had {
var v any
if p.toml {
v = tomlRaw(st.Raw)
} else if json.Unmarshal([]byte(st.Raw), &v) != nil {
v = st.Raw
}
if p.toml {
return edit.SetTOMLTop(p.path, edit.KV{Path: p.key, Value: v})
}
return edit.SetJSON(p.path, edit.KV{Path: p.key, Value: v})
}
if p.toml {
return edit.DelTOMLTop(p.path, p.key)
}
if err := edit.DelJSON(p.path, p.key); err != nil {
return err
}
// an entry under modelSettings left with nothing in it goes too
if agent == "claude" && knob == TuneCompact {
n := claudeName(model)
empty := func(k string) bool {
v, ok := edit.GetJSON(p.path, k)
return ok && strings.Join(strings.Fields(v), "") == "{}"
}
if empty("modelSettings." + n) {
edit.DelJSON(p.path, "modelSettings."+n)
}
if empty("modelSettings") {
edit.DelJSON(p.path, "modelSettings")
}
}
return nil
}
// tomlRaw is a TOML value as GetTOMLTop read it, as a value to write.
func tomlRaw(raw string) any {
if n, err := strconv.Atoi(raw); err == nil {
return n
}
return strings.Trim(raw, `"'`)
}
// Codex's background compaction, as tune puts the advice in it (herman_hong
// on Discord: a foreground compaction kept them waiting, so they set 384K
// by hand; the advice's 120K is wanted in the background, the 384K kept as
// the backstop). It is Settings' Background compaction
// (settings.CodexAsyncCompact, CodexAsyncCompactPercent), which Sync writes
// into Codex's config.toml as [features.async_compaction] enabled and
// model_async_compact_threshold_percent: a share of the model's window,
// not tokens (codex-rs core/src/config, 1 to 100, Codex's own 65). The
// foreground limit, the user's model_auto_compact_token_limit or Long
// conversations, isn't touched. What Settings had before is stashed, so
// Undo puts it back unless the user changed it since.
// TuneAsyncSource is the Source of the background compaction's share.
const TuneAsyncSource = "async"
const tuneAsyncKey = "tune.codex." + TuneAsync
type tuneAsyncStash struct {
On bool `json:"on"`
Percent int `json:"percent"`
Set int `json:"set"`
}
func asyncStashed() (tuneAsyncStash, bool) {
raw := stashShared()[tuneAsyncKey]
var st tuneAsyncStash
if raw == "" || json.Unmarshal([]byte(raw), &st) != nil {
return tuneAsyncStash{}, false
}
return st, true
}
func asyncOurs(s settings.Settings) bool {
st, ok := asyncStashed()
return ok && s.CodexAsyncCompact && s.CodexAsyncCompactPercent == st.Set
}
// asyncRead is the share Codex compacts in the background at: "" when it
// is off, 65 for Codex's own. One the user has in config.toml comes first
// (codexAsyncOwn), and is Locked.
func asyncRead(home string) TuneSetting {
out := TuneSetting{Source: TuneAsyncSource}
if own := codexAsyncOwn(home); own != "" {
out.Locked = own
return out
}
s := settings.Load()
if s.CodexAsyncCompact {
p := s.CodexAsyncCompactPercent
if p == 0 {
p = 65
}
out.Value = strconv.Itoa(p)
}
out.Ours = asyncOurs(s)
return out
}
func asyncApply(home, value string) error {
n, err := strconv.Atoi(value)
if err != nil || n < 1 || n > 100 {
return fmt.Errorf("Codex compacts in the background past 1 to 100%% of a model's window, not %q", value)
}
if own := codexAsyncOwn(home); own != "" {
return fmt.Errorf("Codex's config.toml sets %s, which comes before magpie's: take it out first", own)
}
s := settings.Load()
st, ok := asyncStashed()
if !ok {
st = tuneAsyncStash{On: s.CodexAsyncCompact, Percent: s.CodexAsyncCompactPercent}
}
st.Set = n
b, _ := json.Marshal(st)
stash(map[string]string{tuneAsyncKey: string(b)})
return provider.SetCodexAsyncCompact(true, n)
}
// asyncUndo puts Background compaction back as it was before tune, unless
// the user changed it since: then it is theirs, and only forgotten.
func asyncUndo() error {
st, ok := asyncStashed()
if !ok {
return fmt.Errorf("magpie didn't set Codex's background compaction")
}
defer forget(tuneAsyncKey)
if !asyncOurs(settings.Load()) {
return nil
}
return provider.SetCodexAsyncCompact(st.On, st.Percent)
}