usage: the fine-clock hash test times each read from the append's stamp, not the wall clock (#3511)

TestUsageReadAfterAppendHashesNothingOnAFineClock/whole-second_stamp was
red on CI on macOS and ubuntu at f02390544 (run 40802792343), and 2 to 3
of 20 with -count=20 on the Mac mini. It truncated the log's change time
to whole seconds and read at time.Now()+150ms, expecting every read to
fall inside the stamp's 1s settle. An append at x.86s or later has stamp
x.0 and its read at x+1.01s is past it, so the snapshot counted as
settled and nothing was hashed. Under load (#3551, WSL) the read's
time.Now() drifts later the same way.

The reader is right, not a bug: settledLogStamp takes the read's time
before the stat and calls a whole-second stamp c settled only when the
read is past c+1s. Any write at or after that read has a stamp of c+1s
or later, so an equal stamp can't hide a later write on a filesystem
with 1s mtimes. The test's assumption was wrong.

Each read is now stamp + tc.after, the stamp taken from the history log
and then from each append's stat (the one the fine case already took for
its skip). No wall clock is read on the path. A third case pins the
other side: a whole-second stamp read 1.15s later (the next second)
hashes nothing. ggbdpq's patch on #3511 proposed the same stamp-relative
read.

Run (mote = Mac mini, via mote-run; base f02390544):
- Old test, wall clock injected at x.95 of its second (logIndexNow =
  time.Now().Truncate(1s)+950ms+150ms), -count=10: 10 of 10 red with
  CI's "0 bytes hashed over 5 appends".
- New test, -count=50 -v: 150 of 150 subtests pass.
- Broken by hand: logStampSettle = 100ms turns whole-second_stamp red
  (3/3); logStampSettle = 2s turns the next-second case red (3/3, 54MB
  hashed). The next-second case first used logStampSettle+150ms and
  stayed green under the 2s break, so it is pinned to time.Second.
- mote: go vet ./internal/usage; -count=50 of the test; go test
  -count=1 -timeout 45m ./internal/usage/...: ok.
- Linux (wsl-run, magpie-desktop): go test ./internal/usage/...: ok.
- Windows (win-run, xiaomi-gpu): the three subtests pass (-count=3);
  the package is red only on TestDesktopSessionAccountMetadata ("Code
  tab account mapping"), which fails the same at untouched f02390544
  and is #1356.
- gofmt -l internal/usage: clean.

Not tried: a real filesystem with whole-second mtimes (HFS+/FAT); the
test fakes the truncation through logChangeTime as before.

Issues: #3511, #3551 (its duplicate)

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
This commit is contained in:
yetone
2026-10-29 10:13:06 +08:00
co-authored by ggbdpq
parent f02390544d
commit 3142a55594
+18 -5
View File
@@ -104,12 +104,23 @@ func TestUsagePartialLineThenCompletion(t *testing.T) {
// logFineSettle, so a read that long after an append continues the
// snapshot from the appends it noted, hashing nothing again; one in whole
// seconds still waits out logStampSettle (#3394).
//
// Each read is timed from the append's change stamp, not the wall clock: a
// whole-second stamp is up to a second behind the append, so 150 ms after
// an append late in its second is already the next second, past the
// stamp's tick, where nothing is hashed, rightly (#3511). The last case
// holds that: any later write has a later stamp by then.
func TestUsageReadAfterAppendHashesNothingOnAFineClock(t *testing.T) {
for _, tc := range []struct {
name string
fine bool
after time.Duration // from the stamp to the read
hashes bool
}{{"fine stamp", true, false}, {"whole-second stamp", false, true}} {
}{
{"fine stamp", true, 150 * time.Millisecond, false},
{"whole-second stamp", false, 150 * time.Millisecond, true},
{"whole-second stamp read the next second", false, time.Second + 150*time.Millisecond, false},
} {
t.Run(tc.name, func(t *testing.T) {
pageHome(t)
holdClock(t, time.Now())
@@ -119,8 +130,9 @@ func TestUsageReadAfterAppendHashesNothingOnAFineClock(t *testing.T) {
if !tc.fine {
logChangeTime = func(info os.FileInfo) time.Time { return changeTime(info).Truncate(time.Second) }
}
// each read comes 150 ms after the append before it
logIndexNow = func() time.Time { return time.Now().Add(150 * time.Millisecond) }
// each read comes tc.after past the stamp of the append before it
stamp := logChangeTime(mustStat(t))
logIndexNow = func() time.Time { return stamp.Add(tc.after) }
Summarize(Today)
hash := logRecordHash
var hashed int64
@@ -132,7 +144,8 @@ func TestUsageReadAfterAppendHashesNothingOnAFineClock(t *testing.T) {
const cycles = 5
for range cycles {
Append(Record{Agent: "codex", Provider: "relay", Model: "m", Input: 1, Status: 200})
if c := logChangeTime(mustStat(t)); tc.fine && c.Nanosecond() == 0 {
stamp = logChangeTime(mustStat(t))
if tc.fine && stamp.Nanosecond() == 0 {
t.Skip("this file system keeps change times in whole seconds")
}
Summarize(Today)
@@ -143,7 +156,7 @@ func TestUsageReadAfterAppendHashesNothingOnAFineClock(t *testing.T) {
}
t.Logf("%d bytes hashed over %d appends", hashed, cycles)
if tc.hashes != (hashed > 0) {
t.Fatalf("hashed %d bytes over %d appends and reads 150 ms after each", hashed, cycles)
t.Fatalf("hashed %d bytes over %d appends and reads %v past each one's stamp", hashed, cycles, tc.after)
}
})
}