From c1457f2eecff20413c731cbe775956e152ce7014 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 1 Nov 2026 06:30:15 +0800 Subject: [PATCH] agent: Drift makes a field's options once a look Drift asked magpieValue up to three times a field: once to find the field on magpie's models, then for magpie's record and the value now. Each ask made the field's options anew, and Claude Code's (its own models and claudeViaMagpie) are long. fieldOptions keeps each field's options, by key, for one Drift; magpieValue is unchanged for the other callers. Run on mote at this tree: - gofmt clean; go vet ./internal/agent ok on darwin, GOOS=windows and GOOS=linux. - TestDriftMakesAFieldsOptionsOnce ok 3 of 3 alone. Broken by hand (opts left nil in Drift): "one Drift made the field's options 3 times, want once". - go test ./internal/agent: ok (324.6s). Real accounts (sandbox copy of the owner's config, network blocked, 40 rounds of /api/drift, /api/state, /api/providers), v0.1.2488 tree against this one: - Drift 54.3 -> 45.3 MB, magpieValue 47.1 -> 42.6 MB, claudeIn.func57 39.7 -> 33.8 MB; total 613 -> 587 MB (totals move +-25 MB between runs). - /api/state and /api/drift byte for byte the same. - footprint peak 138 MB, RSS 180 MB: unchanged. Not run: the gui suite (gui is not touched). Run at 573b21f4b; picked onto 3f2faef62 after it (11 commits, in internal/agent only dsh.go and a Responses test), not run again there. --- internal/agent/applied.go | 31 +++++++++++++++++++++--- internal/agent/drift_options_test.go | 35 ++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 internal/agent/drift_options_test.go diff --git a/internal/agent/applied.go b/internal/agent/applied.go index 9a67e7b10..65b938f29 100644 --- a/internal/agent/applied.go +++ b/internal/agent/applied.go @@ -175,11 +175,12 @@ func (a *Agent) Drift() *Drift { return nil } vals := a.Values() + opts := fieldOptions{} // the field on one of magpie's models: where drift shows, and what // setting it again sets on, onMagpie := a.Fields[0], false for _, f := range a.Fields { - if magpieValue(a, f, vals[f.Key], vals) { + if magpieValueIn(a, f, vals[f.Key], vals, opts) { on, onMagpie = f, true break } @@ -213,7 +214,7 @@ func (a *Agent) Drift() *Drift { if now == "" && f.Follows != "" { now = vals[f.Follows] } - if !ok || picked || now == want || !magpieValue(a, f, want, vals) || magpieValue(a, f, now, vals) || sameGroup(want, now) { + if !ok || picked || now == want || !magpieValueIn(a, f, want, vals, opts) || magpieValueIn(a, f, now, vals, opts) || sameGroup(want, now) { continue } return &Drift{Kind: "replaced", Field: f.Key, Now: vals[f.Key], Want: want, @@ -296,6 +297,30 @@ func bypassed(used, applied, seen time.Time) bool { // its suffix after the model (SplitSuffix) aside; a list of models is the // user's own. func magpieValue(a *Agent, f Field, v string, vals map[string]string) bool { + return magpieValueIn(a, f, v, vals, nil) +} + +// fieldOptions keeps each field's options, by its key, for one look at an +// agent's values: Drift asks magpieValue up to three times a field, and +// Claude Code's options (claudeViaMagpie, its own models) were made anew +// each time, 35 MB of what forty polls with real accounts allocated. +type fieldOptions map[string][]Option + +func (o fieldOptions) of(f Field, vals map[string]string) []Option { + if o == nil { + return f.Options(vals) + } + opts, ok := o[f.Key] + if !ok { + opts = f.Options(vals) + o[f.Key] = opts + } + return opts +} + +// magpieValueIn is magpieValue reading a field's options from opts, made +// for the same vals; nil reads them anew. +func magpieValueIn(a *Agent, f Field, v string, vals map[string]string, opts fieldOptions) bool { if v == "" { return false } @@ -314,7 +339,7 @@ func magpieValue(a *Agent, f Field, v string, vals map[string]string) bool { if f.Options == nil { return false } - for _, o := range f.Options(vals) { + for _, o := range opts.of(f, vals) { if o.Value == v { return o.Ref != "" } diff --git a/internal/agent/drift_options_test.go b/internal/agent/drift_options_test.go new file mode 100644 index 000000000..025984101 --- /dev/null +++ b/internal/agent/drift_options_test.go @@ -0,0 +1,35 @@ +package agent + +import "testing" + +// Drift asked a field's options up to three times, once a field to find +// the one on magpie and twice to compare it with magpie's record, and +// Claude Code's were made anew each time (35 MB of forty polls with real +// accounts). They are made once a look now. +func TestDriftMakesAFieldsOptionsOnce(t *testing.T) { + v, made := "", 0 + a := &Agent{ID: "drift-options", Name: "X", Fields: []Field{{Key: "model", Label: "Model", + Get: func() string { return v }, + Set: func(s string) error { v = s; return nil }, + Options: func(map[string]string) []Option { + made++ + return []Option{ + {Value: "nowhere-relay/m1", Ref: "nowhere-relay/m1"}, + {Value: "nowhere-relay/m2", Ref: "nowhere-relay/m2"}, + } + }, + }}} + if err := a.Apply("model", "nowhere-relay/m1"); err != nil { + t.Fatal(err) + } + // another of magpie's models, picked in the agent: not drift, and every + // comparison read the options + v = "nowhere-relay/m2" + made = 0 + if d := a.Drift(); d != nil { + t.Fatalf("one magpie model for another drifted: %+v", d) + } + if made != 1 { + t.Fatalf("one Drift made the field's options %d times, want once", made) + } +}