From 792c17aac70b7456e139daaef7d8cf42cc3b4d95 Mon Sep 17 00:00:00 2001 From: Dotta <34892728+cryppadotta@users.noreply.github.com> Date: Thu, 12 Nov 2026 11:17:56 -0600 Subject: [PATCH] fix(ui): freeze the clock in the persisted PR-read cache test (#21147) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The board UI saves approved reads to local storage. A reload can then show them at once. > - The test `bounds saved PR reads and retains their original age through later saves` checks the limit of 100 saved PR reads. > - The test reads `Date.now()` one time and uses that value as the cache clock. Then `seed()` writes the session and company list with the real clock. > - If the real clock moves 1 ms or more between these two steps, the saved session has a time in the future. The cache correctly rejects that session, so it restores no data. > - This failed CI 5 times in 48 hours on 5 unrelated pull requests. Each failure was `expected [] to have a length of 100 but got +0`. > - This pull request freezes the clock in that test, as the sibling tests in the same file already do. > - The benefit is a deterministic test, and no more unrelated red CI runs. ## Linked Issues or Issue Description **What happened?** The UI test `ui/src/lib/persisted-query-cache.test.ts > persisted read cache > bounds saved PR reads and retains their original age through later saves` failed in CI 5 times in 48 hours on 5 unrelated branches. The error was `AssertionError: expected [] to have a length of 100 but got +0` at line 82. **Expected behavior** The test passes on every run, independent of machine load. **Steps to reproduce** 1. Start two CPU busy loops (`node -e 'while(true){}'`). 2. Run `pnpm --filter @paperclipai/ui exec vitest run src/lib/persisted-query-cache.test.ts -t "bounds saved PR reads"` 30 times, two at a time. 3. Before this change: 0 pass, 30 fail, all at line 82. Root cause: the test injects a fixed clock (`now`) into the cache, but `seed()` calls `setQueryData` without `updatedAt`. TanStack Query then uses the real `Date.now()`. When the real clock passes the fixed clock, `fresh()` sees a session read from the future. The flush then does not save the session, and the restore aborts. The product code is correct. The test clock was not consistent. ## What Changed - Call `vi.useFakeTimers()` at the start of `bounds saved PR reads and retains their original age through later saves`. `Date.now()` is now frozen, so the seeded reads and the injected clock agree. The other clock-driven tests in this file already do this. - Call `vi.useFakeTimers()` in `discards snapshots after %s changes`. It had the same race. There, the race did not fail the test, but it let the test pass without the discard path under test. ## Verification - Deterministic red/green: I added a real 5 ms sleep (`Atomics.wait`) after `let now = Date.now()` in a temporary copy of the test. Without the fix: `expected [] to have a length of 100 but got +0`. With the fix: pass. I removed the temporary copies. - Loop under load (two CPU busy loops, two parallel vitest processes, 30 runs each): - Before: 0 pass, 30 fail (all at `persisted-query-cache.test.ts:82`). - After: 30 pass, 0 fail. - Full file: `pnpm --filter @paperclipai/ui exec vitest run src/lib/persisted-query-cache.test.ts`: 54 passed. ## Risks - Low risk. The change is test-only. Fake timers are reset in the existing `afterEach` (`vi.useRealTimers()`). ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude Code with tool use (shell, file edit) and reasoning. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 --- ui/src/lib/persisted-query-cache.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ui/src/lib/persisted-query-cache.test.ts b/ui/src/lib/persisted-query-cache.test.ts index 0ff2ab40e55..2bca4eed8d3 100644 --- a/ui/src/lib/persisted-query-cache.test.ts +++ b/ui/src/lib/persisted-query-cache.test.ts @@ -72,6 +72,7 @@ describe("persisted read cache", () => { }); it("bounds saved PR reads and retains their original age through later saves", () => { + vi.useFakeTimers(); let now = Date.now(); const first = setup(undefined, () => now); seed(first.client); for (let i = 0; i < 120; i += 1) first.client.setQueryData( @@ -529,6 +530,7 @@ describe("persisted read cache", () => { }); it.each(["build", "expiry"])("discards snapshots after %s changes", kind => { + vi.useFakeTimers(); const startedAt = Date.now(); const first = setup(undefined, () => startedAt); seed(first.client); first.handle.flush(); const restored = setup(first.storage, () => kind === "expiry" ? startedAt + QUERY_CACHE_MAX_AGE + 1 : startedAt, undefined, kind === "build" ? "build-two" : "build-one");