From 0bf9d77d3f1cd70eb6cd79023632cf3b41b19ebe Mon Sep 17 00:00:00 2001 From: yetone Date: Wed, 7 Oct 2026 03:03:06 +0800 Subject: [PATCH] agent tests: TestDriftBypassed's request reaching the gateway is kept to its run, so -count=2 passes The test stood for a request with usage.Saw("codex"). Saw's record lasts the process, which the gateway relies on (LastSeen answers from it before a request is written to the log), so a second run started with a request seconds old, newer than its prompt five minutes back, and found nothing bypassed ("bypassed: ", also on origin/main). Drift reads LastSeen through a package variable the test replaces with its own clock; production still reads usage.LastSeen. That Saw is LastSeen's answer at once moves to usage's own TestSawIsLastSeen, which removes its record afterwards. -run 'TestDrift|TestSawIsLastSeen' -race -count=5 passes; before, -count=2 failed. --- internal/agent/applied.go | 7 ++++++- internal/agent/applied_test.go | 8 ++++++-- internal/usage/usage_test.go | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/internal/agent/applied.go b/internal/agent/applied.go index aacab5c8..f2d79e69 100644 --- a/internal/agent/applied.go +++ b/internal/agent/applied.go @@ -201,7 +201,7 @@ func (a *Agent) Drift() *Drift { } } if a.LastUsed != nil && onMagpie { - if used := a.LastUsed(); bypassed(used, rec.At, usage.LastSeen(a.ID)) { + if used := a.LastUsed(); bypassed(used, rec.At, lastSeen(a.ID)) { return &Drift{Kind: "bypassed", Field: on.Key, Now: vals[on.Key], Want: vals[on.Key], Detail: a.Name + " was used at " + used.Format("15:04") + " but none of its requests reached magpie — one started before magpie set it up still runs on its old config: restart it"} } @@ -235,6 +235,11 @@ func (a *Agent) theInstalled() bool { // bypassed: the agent was used — while this gateway was up and after magpie // last set it — and no request of it arrived since. A request leaves within // moments of the prompt; a little grace keeps one in flight from counting. +// lastSeen is when the agent's last request reached this process's gateway +// (usage.LastSeen). What usage.Saw records lasts as long as the process, so +// a test that stands for a request gives its own here. +var lastSeen = usage.LastSeen + func bypassed(used, applied, seen time.Time) bool { const grace = 30 * time.Second return !used.IsZero() && used.After(started) && used.After(applied) && diff --git a/internal/agent/applied_test.go b/internal/agent/applied_test.go index a96958fb..fe140fe0 100644 --- a/internal/agent/applied_test.go +++ b/internal/agent/applied_test.go @@ -10,7 +10,6 @@ import ( "time" "github.com/yetone/magpie/internal/gateway" - "github.com/yetone/magpie/internal/usage" ) // sameGroup compares two values of a field, one of them read back from the @@ -94,6 +93,11 @@ func TestDriftBypassed(t *testing.T) { cx := codex(home) defer func(s time.Time) { started = s }(started) started = time.Now().Add(-time.Hour) + // a request reaching the gateway, kept to this run: usage.Saw's record + // lasts the process, so the next run (-count=2) would start with it + var saw time.Time + defer func(f func(string) time.Time) { lastSeen = f }(lastSeen) + lastSeen = func(string) time.Time { return saw } if err := cx.Apply("model", "fake/m1"); err != nil { t.Fatal(err) } @@ -111,7 +115,7 @@ func TestDriftBypassed(t *testing.T) { if d := cx.Drift(); d == nil || d.Kind != "bypassed" || d.Want != "fake/m1" { t.Fatalf("bypassed: %+v", d) } - usage.Saw("codex") + saw = time.Now() if d := cx.Drift(); d != nil { t.Fatalf("a request arrived: %+v", d) } diff --git a/internal/usage/usage_test.go b/internal/usage/usage_test.go index d45cd237..f694907f 100644 --- a/internal/usage/usage_test.go +++ b/internal/usage/usage_test.go @@ -296,3 +296,18 @@ func TestFormatCost(t *testing.T) { } } } + +// A request Saw note is LastSeen's answer at once, before any record of it +// is written. +func TestSawIsLastSeen(t *testing.T) { + const agent = "saw-is-last-seen" + t.Cleanup(func() { seen.Delete(agent) }) // the record lasts the process + if at := LastSeen(agent); !at.IsZero() { + t.Fatalf("before any request: %v", at) + } + before := time.Now() + Saw(agent) + if at := LastSeen(agent); at.Before(before) || time.Since(at) > time.Minute { + t.Fatalf("after a request: %v", at) + } +}