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: <nil>", 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.
This commit is contained in:
yetone committed 2026-10-07 03:03:06 +08:00
1 parent 06420a7121
commit 0bf9d77d3f
3 files changed
+27 -3

No files matched your search

+6 -1
View File
@@ -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) &&
+6 -2
View File
@@ -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)
}
+15
View File
@@ -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)
}
}