Repository navigation
test(memory): make the heartbeat freshness test deterministic - #5715
Conversation
… of racing it "keeps a lock held longer than staleMs fresh" held a lock for 400 ms with staleMs 100, so the heartbeat refreshed it every 50 ms, and a waiter judged its age against the real clock. When a refresh landed more than 50 ms late (a slow runner's disk or scheduler), the waiter saw a live lock older than staleMs and broke it. Delaying each heartbeat utimes by 120 ms reproduces the CI order [long start, waiter, long end] every time. The heartbeat itself is sound: production keeps a 15 s margin (staleMs 30 s, a beat every 15 s). The test now fakes Date and the heartbeat's interval only. It advances the clock one beat at a time, waits for that beat's refresh to land and for the waiter to look at the lock again, and holds the lock for four times staleMs before releasing it. Timeouts stay real, so the waiter retries on its own. Closes #5680 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
|
|
| Tools | Prompts | Resources | Resource templates | |
|---|---|---|---|---|
| Base | 9 | 0 | 1 | 0 |
| Head | 9 | 0 | 1 | 0 |
Updated by this run for d8715927988d122dffda10d395e5f638435bad6c.
There was a problem hiding this comment.
🟡 Changes recommended
The heartbeat test can hang indefinitely and leak fake timers and a blocked lock holder into subsequent tests because synchronization waits are unbounded and cleanup is not timeout-safe.
1 open issue
What changed in this PR
This test-only PR addresses #5680 by replacing scheduler-dependent timing in the memory server’s lock-heartbeat test with controlled clock advancement. Production code and published behavior are unchanged.
Changes:
- Fakes the clock and heartbeat interval while keeping retry timeouts real.
- Synchronizes on holder startup, heartbeat writes, and waiter reads instead of fixed sleeps.
- Verifies the lock remains held across four stale periods, with a 30-second test timeout.
| File | Description |
|---|---|
src/memory/__tests__/file-lock.test.ts |
Reworks the heartbeat freshness test to use controlled timing and observed filesystem events. |
Review effort: Balanced
Review of #5715: a heartbeat that stopped refreshing left the test waiting until Vitest's timeout, which skips the test's finally, so the fake timers and the blocked lock holder outlived it. Each wait now gives up after 10 s of real time with an error that names what it waited for. The finally releases the holder and settles both lock operations, and afterEach restores real timers for a test that times out anyway. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
|
Copilot round 1: one finding, fixed in d871592.
Re-checked after the fix: the injected-delay probe (each |
|
| Tools | Prompts | Resources | Resource templates | |
|---|---|---|---|---|
| Base | 16 | 0 | 0 | 1 |
| Head | 16 | 0 | 0 | 1 |
tools
--- base/tools.json
+++ target/tools.json
- tools[edit_file].inputSchema.properties.edits.items.properties.oldText.description: "Text to search for - must match exactly"
+ tools[edit_file].inputSchema.properties.edits.items.properties.oldText.description: "Text to search for. It matches the file's text exactly, except that line endings (LF or CRLF) never ..."Updated by this run for d8715927988d122dffda10d395e5f638435bad6c.
|
Copilot round 2 (on d871592): clean. No new findings, and the round-1 finding is marked resolved. The review loop ends here, on the first clean round. |

Closes #5680
Description
Makes the
withFileLocktest "keeps a lock held longer than staleMs fresh, so it is not broken" deterministic. The test is the only file changed.Root cause: the test's timing, not the heartbeat. The test held a lock for 400 ms with
staleMs: 100, so the heartbeat refreshed it every 50 ms, and a second waiter judged the lock's age against the real clock. If a refresh landed more than 50 ms late (a slow runner's disk or scheduler), the waiter found a live lock older thanstaleMsand broke it. That is the order CI reported:[long start, waiter, long end]. The heartbeat is sound. With the defaults (staleMs30 s, a beat every 15 s) the same failure needs a 15 s stall in the holder, which the lock protocol already assumes does not happen ("No process stalls for longer than staleMs inside a guarded section", aboveguardPathOf). So there is no product change and no changeset.The fix uses vitest fake timers for
Date,setIntervalandclearIntervalonly. The lock's age and the heartbeat now move only when the test advances them.setTimeoutstays real, so the waiter's retry loop keeps running on its own. The test:staleMs / 2, which fires exactly one heartbeat, waits for that beat'sutimesto land, then waits for the waiter to judge the lock again;4 × staleMswhile only the holder had run, then releases the holder and expects[long start, long end, waiter].Every wait is a sync point on an observed event (a spied
fs.openof the lock for reading, or a spiedfs.utimes), and none is a timed sleep. A slow runner makes the test take longer but cannot change its result. It has an explicit 30 s timeout for that reason. Each wait gives up after 10 s of real time with an error that names what it waited for, so a broken heartbeat fails the test by name (checked by mutation, below). The test'sfinallyandafterEachthen restore real timers and settle both lock operations, so nothing leaks into later tests (Copilot round 1). This follows the house style of #5634, which replaced a fixed sleep with a wait on the event itself.Server Details
src/memory/__tests__/file-lock.test.tsonly, one test. No tools, resources, prompts, configuration or source changed.Motivation and Context
The test failed intermittently in CI (
Coverage memoryon #5677, a PR that touches onlysrc/git) and once locally. See #5680.How Has This Been Tested?
This change has no client-observable surface, so the evidence is targeted probes (
pr-flowstep 5). All probes ran on macOS with Node 24, usingnpx vitest run __tests__/file-lock.test.ts -t "staleMs fresh"fromsrc/memory:yesprocesses on an 18-core machine, plus 6 parallel loops of the file under--coverage, 15 runs eachfs.utimesdelayed by 120 msexpected [ 'long start', 'waiter', 'long end' ] to deeply equal [ 'long start', 'long end', 'waiter' ]utimesdelayed by 120 ms, plus a 150 ms synchronous busy-wait that blocks the event loop on every beatrefreshLockcall replaced with a no-op inindex.ts, then reverted)Gave up waiting for heartbeat 1 to refresh the lock. The other tests that fail are exactly the heartbeat-dependent ones that fail under the same mutation on the original file, so nothing leaksThe probe copies and the mutation were temporary and are not part of the diff.
npm run local:gateexits 0 on the final head.--sequence.concurrent, which the issue suggests, does not apply to this file: its tests spy on the sharedfsmodule (vi.spyOn(fs, "open")and others), so running them concurrently makes them interfere with each other (35 of the 57 fail under--sequence.concurrent, from the shared spies, not from this test). Load and injected delays were used instead.Breaking Changes
None.
Types of changes
Checklist
npm run changeset) if this changes what a TypeScript server publishes (not applicable: test-only change; nothing published changes)Additional context
Siblings #5624 (guard clearing in
index.ts) and #5689 (missing parent directory inwithFileLock) touch the lock code at the same time. This PR does not touchindex.ts, so it does not overlap with either.🤖 Generated with Claude Code