From 5422306fac7327beefe5afef854c9588a0cbb111 Mon Sep 17 00:00:00 2001 From: Snowyy <142957559+Snowyyyyyy1@users.noreply.github.com> Date: Sat, 3 Oct 2026 02:19:11 +0800 Subject: [PATCH] usage: guard cache transitions and Windows long-path reads (#598) --- .github/workflows/test.yml | 2 +- internal/usage/cache_bounds_test.go | 31 +++++++++++++++++++++ internal/usage/log_change_windows.go | 9 +++++- internal/usage/log_change_windows_test.go | 34 +++++++++++++++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 27f7a62cb..2c98f37a5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -71,7 +71,7 @@ jobs: # ChangeTime checks rather than only cross-compiling the Windows API. - name: Windows usage cache if: runner.os == 'Windows' - run: go test -v -count=1 ./internal/usage -run '^(TestWindows|TestLogChange|TestLogFileInfoPreservesIdentity$|TestUsageSameSizeAndMtimeRewrite$|TestLogGrowingRewriteThenAppend$)' + run: go test -v -count=1 ./internal/usage -run '^(TestWindows|TestLogChange|TestLogFileInfoPreservesIdentity$|TestUsageSameSizeAndMtimeRewrite$|TestLogGrowingRewriteThenAppend$|TestUsagePageCacheAfterUncachedQuery$)' cli: name: cli (nogui) diff --git a/internal/usage/cache_bounds_test.go b/internal/usage/cache_bounds_test.go index 95095e721..7238251bc 100644 --- a/internal/usage/cache_bounds_test.go +++ b/internal/usage/cache_bounds_test.go @@ -122,3 +122,34 @@ func TestUsageLargeSummaryIsCompleteButNotCached(t *testing.T) { t.Fatal("oversized Sessions retained in summary cache") } } + +// Rebuilding an uncached snapshot with a larger budget keeps its version and +// page key. The preceding query must leave an initialized page map to write. +func TestUsagePageCacheAfterUncachedQuery(t *testing.T) { + pageHome(t) + historyLog(t, 1034) + normalBudget := requestCacheBytes + cacheBudget(t, 128) + QueryPage(All, Filter{}, 0, 100) + logIndex.Lock() + uncached := logIndex.snapshot.uncached + logIndex.Unlock() + if !uncached { + t.Fatal("fixture must exceed the tiny budget") + } + requestCache.Lock() + key := requestCache.key + requestCache.Unlock() + + requestCacheBytes = normalBudget + got := QueryPage(All, Filter{}, 0, 100) + equalPage(t, got, pageFromLedger(All, Filter{}, 0, 100, LedgerOf(All, Filter{}))) + requestCache.Lock() + defer requestCache.Unlock() + if requestCache.key != key { + t.Fatal("page key changed instead of exercising the same cache") + } + if _, ok := requestCache.pages[pageKey{All, Filter{}, 0, 100}]; !ok { + t.Fatal("page not cached after rebuilding within budget") + } +} diff --git a/internal/usage/log_change_windows.go b/internal/usage/log_change_windows.go index 6b113a1fe..1a7bd53c9 100644 --- a/internal/usage/log_change_windows.go +++ b/internal/usage/log_change_windows.go @@ -42,7 +42,14 @@ func statLogFile(path string) (os.FileInfo, error) { 0, ) if err != nil { - return nil, err + // Go's opener handles long paths on Windows versions where the raw + // API requires an extended path prefix. Keep the same-handle stamp. + f, err := os.Open(path) + if err != nil { + return nil, err + } + defer f.Close() + return statLogHandle(f) } f := os.NewFile(uintptr(h), path) if f == nil { diff --git a/internal/usage/log_change_windows_test.go b/internal/usage/log_change_windows_test.go index 79b99525b..7ef87c666 100644 --- a/internal/usage/log_change_windows_test.go +++ b/internal/usage/log_change_windows_test.go @@ -5,6 +5,8 @@ package usage import ( "bytes" "os" + "path/filepath" + "strings" "testing" "time" "unsafe" @@ -152,6 +154,9 @@ func TestLogChangeTrustedAppendReuse(t *testing.T) { t.Fatal("append did not record trusted change-time states") } after := readLogSnapshot() + if after.hash != "" { + t.Fatal("trusted append scanned the historical prefix") + } if after.blocks[0] != before.blocks[0] { t.Fatal("in-process append rebuilt the sealed prefix") } @@ -160,3 +165,32 @@ func TestLogChangeTrustedAppendReuse(t *testing.T) { t.Fatalf("summary stale after trusted append: got %+v want %+v", got.Totals, want.Totals) } } + +func TestLogChangeLongPath(t *testing.T) { + pageHome(t) + dir := t.TempDir() + for len(dir) < 320 { + dir = filepath.Join(dir, strings.Repeat("nested", 8)) + } + t.Setenv("XDG_CONFIG_HOME", dir) + historyLog(t, 1034) + if len(Path()) <= 260 { + t.Fatal("fixture must exceed the legacy Windows path limit") + } + info, err := statLogFile(Path()) + if err != nil { + t.Fatal(err) + } + native, err := os.Stat(Path()) + if err != nil { + t.Fatal(err) + } + if !sameLogFile(info, native) || info.Size() != native.Size() { + t.Fatal("long-path stat lost file identity or size") + } + requireChangeStamp(t, Path()) + got, want := Summarize(All), summarize(All, time.Now(), Load(time.Time{})) + if got.Totals != want.Totals || got.Calls != 1034 { + t.Fatalf("long-path history missing: got %+v want %+v", got.Totals, want.Totals) + } +}