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) + } +}