diff --git a/internal/usage/cache_bounds_test.go b/internal/usage/cache_bounds_test.go index 02e0b850e..110b3975f 100644 --- a/internal/usage/cache_bounds_test.go +++ b/internal/usage/cache_bounds_test.go @@ -95,6 +95,14 @@ func TestUsageSameTickRewriteIsNotCached(t *testing.T) { if appendAfter { Append(Record{Time: clock, Agent: "codex", Provider: "relay", Model: "m", Input: 19}) } + if !afterSettle && !appendAfter { + // Inside the tick the read before the rewrite may + // stand, as a read a moment earlier would have had + // it; the first read once the tick settles checks + // it (#4368). + Summarize(All) + afterSettle = true + } if afterSettle { logIndexNow = func() time.Time { return tick.Add(time.Hour) } } diff --git a/internal/usage/log_hash_cost_test.go b/internal/usage/log_hash_cost_test.go index 46925fcd0..cb3454017 100644 --- a/internal/usage/log_hash_cost_test.go +++ b/internal/usage/log_hash_cost_test.go @@ -170,3 +170,53 @@ func mustStat(t *testing.T) os.FileInfo { } return info } + +// Each append wakes several readers at once (the menu bar, the pages), all +// inside the change stamp's tick. Each of them hashed the whole log again, +// and so did the read that built the snapshot and the first read once it +// settled: four times the log for one request (#4368). The read once it +// settles is the one that checks it. +func TestUsageReadersOfOneAppendHashTheLogOnce(t *testing.T) { + pageHome(t) + holdClock(t, time.Now()) + historyLog(t, 20000) + now := logIndexNow + t.Cleanup(func() { logIndexNow = now }) + later := func() time.Time { return time.Now().Add(2 * logStampSettle) } + logIndexNow = later + Summarize(Today) + hash := logRecordHash + var hashed int64 + logRecordHash = func(path string, n int64) string { + hashed += n + return hash(path, n) + } + t.Cleanup(func() { logRecordHash = hash }) + const cycles = 5 + for range cycles { + Append(Record{Agent: "codex", Provider: "relay", Model: "m", Input: 1, Status: 200}) + info, err := os.Stat(Path()) + if err != nil { + t.Fatal(err) + } + // the readers come within the tick, whatever the machine's speed + logIndexNow = func() time.Time { return logChangeTime(info) } + for range 3 { + Summarize(Today) + } + logIndexNow = later + Summarize(Today) + } + info, err := os.Stat(Path()) + if err != nil { + t.Fatal(err) + } + perCycle := hashed / cycles + t.Logf("log %d bytes: %d bytes hashed per append and four reads (%.2f× the log)", info.Size(), perCycle, float64(perCycle)/float64(info.Size())) + if perCycle > info.Size()+info.Size()/10 { + t.Fatalf("an append and its readers hashed %d bytes over a %d-byte log, more than the one check once it settles", perCycle, info.Size()) + } + if s := Summarize(Today); s.Calls != 1+cycles { + t.Fatalf("calls today: %d", s.Calls) + } +} diff --git a/internal/usage/log_index.go b/internal/usage/log_index.go index e884b016a..1e3f32228 100644 --- a/internal/usage/log_index.go +++ b/internal/usage/log_index.go @@ -111,7 +111,13 @@ func readLogSnapshot() *logSnapshot { old := logIndex.snapshot unchanged := old != nil && old.path == path && (err != nil && old.info == nil || err == nil && sameLogInfo(old.info, info)) if unchanged && info != nil { - if logChangeStamp(info) == "" || !old.settled { + if !old.settled && !settled && old.hash != "" && logChangeStamp(info) != "" { + // Still in the stamp's tick: hashing the log again proves nothing + // a read a moment earlier wouldn't have, and every reader an + // append wakes (the menu bar, the pages) hashed the whole log + // once more (#4368). The first read once it settles verifies it, + // before anything is built on it, as below. + } else if logChangeStamp(info) == "" || !old.settled { unchanged = old.hash != "" && logRecordHash(path, old.off) == old.hash if unchanged && settled { promoted := *old @@ -227,8 +233,13 @@ func readLogSnapshot() *logSnapshot { next.blocks = append(next.blocks, tail.freeze()) } next.hash, next.digest = fmt.Sprintf("%x", sum.Sum(nil)), marshalLogHash(sum) - // A rewrite during the read must not give old blocks the new content's hash. - if (!trusted || !settled) && next.hash != logRecordHash(path, next.off) { + // A rewrite during the read must not give old blocks the new content's + // hash. A read inside the stamp's tick is checked when it settles + // instead (no settled snapshot is made of it unchecked, and one + // continued from it hashes its prefix first): checked now, it hashed + // the whole log twice for each append, once now and again on + // settling (#4368). + if settled && !trusted && next.hash != logRecordHash(path, next.off) { next.hash, next.digest = "", nil next.settled = false next.full = logFull{}