Files
magpie/internal/agent/drift_shown_test.go
T
yetone 8ce32d7008 agent: the window's drift poll remembers which values are magpie's per stamp
/api/drift runs every agent's Drift every 15s while the window is up.
Each Drift asked every field's whole option list whether a value is one
of magpie's, and the model lists under them are built again once a
minute. With real accounts on mote (acgui6, w74), that was 27 MB of the
50 MB magpie allocated in three minutes with the window open.

DriftShown, which only /api/drift calls, keeps magpieValueIn's answer per
agent, field, value and settings while provider.StampNow stays the same,
5 minutes at most. Drift is unchanged for Reapply, rewiring, a launch and
/api/state, which act on the answer.

A provider resting or a model retired by its date is something the stamp
can't see. The window's drift row now shows that within 5 minutes, not 1.

Run: TestDriftShownAsksAValueOncePerStamp and the Drift tests on mica.
Broken by hand (the memo never hit): red, "five polls built the options
5 times, want 1".

Suite on mica at 6c88d61f0 + this commit: gofmt clean; go vet
internal/agent for darwin, linux and windows, internal/gui on darwin; go
test ./internal/agent ok (194.9s); ./internal/gui red only in
TestCoreScriptBudget (1269382 > 1268961). That test is red on
origin/main 6c88d61f0 as well, and #6518 is taken. This change adds no
script.
2026-11-14 09:58:49 +08:00

46 lines
1.5 KiB
Go

package agent
import (
"testing"
"github.com/yetone/magpie/internal/provider"
)
// The window's drift poll asks whether a value is one of magpie's models
// once while provider.StampNow stays the same, not on every poll: each ask
// built the field's whole option list, the agents' model lists under it
// built again once a minute. A stamp that moved asks again, and so does
// Drift itself, which Reapply and rewiring act on.
func TestDriftShownAsksAValueOncePerStamp(t *testing.T) {
asked := 0
f := Field{Key: "model", Options: func(map[string]string) []Option {
asked++
return []Option{{Value: "own-model", Ref: "fake/m1"}}
}}
a := &Agent{ID: "drift-shown-test"}
vals := map[string]string{"model": "own-model"}
var memo valueMemo
for range 5 {
if !memo.in(a, f, "own-model", vals, fieldOptions{}) {
t.Fatal("own-model is magpie's")
}
}
if asked != 1 {
t.Fatalf("five polls built the options %d times, want 1", asked)
}
// another value of the agent's settings is another question
if !memo.in(a, f, "own-model", map[string]string{"model": "own-model", "effort": "high"}, fieldOptions{}) || asked != 2 {
t.Fatalf("other settings: asked %d times, want 2", asked)
}
provider.Changed()
if !memo.in(a, f, "own-model", vals, fieldOptions{}) || asked != 3 {
t.Fatalf("after a change: asked %d times, want 3", asked)
}
for range 3 {
magpieValueIn(a, f, "own-model", vals, fieldOptions{})
}
if asked != 6 {
t.Fatalf("Drift's own asks: %d, want every one (6)", asked)
}
}