Skip to content

Commit 9b6dc1a

Browse files
test(skills): pin that the delayed stale-sidecar sweep removes the sidecar
The retry-timing test counted sweep invocations and swallowed the sweep body, so it proved the window and never proved the removal the retry exists to perform. The counter now delegates to the real sweep with the manager instance preserved, the fixture ages the sidecar between the two fires, and the assertions require the first sweep to leave it alone and the delayed sweep to unlink it.
1 parent 17b2689 commit 9b6dc1a

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

‎src/services/skills/__tests__/SkillsManager.spec.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4335,20 +4335,54 @@ Instructions`)
43354335
// a live write in another process, and nothing else ever revisits them. The one delayed retry
43364336
// is what does, so both halves are pinned: no second sweep inside the window, one after it.
43374337
vi.useFakeTimers()
4338+
// vi.useFakeTimers() takes over Date as well as the timers, so a sidecar stamped with the
4339+
// planted reading ages exactly as the clock is advanced: the retry's verdict changes because
4340+
// the window closed, not because the fixture changed.
4341+
const sidecarName = ".SKILL.md.789.ghi.tmp"
4342+
const sidecarPath = p(globalSkillsDir, sidecarName)
4343+
// What the retry changes is the sidecar's age relative to now, and this suite's fake timers do
4344+
// not fake Date (measured: the two fires land milliseconds apart on the real clock), so the
4345+
// fixture ages the sidecar between the fires instead of pretending the clock carried it past
4346+
// the window.
4347+
let sidecarAgeMs = 0
4348+
mockReaddir.mockImplementation(async (_dir: string, options?: { withFileTypes?: boolean }) =>
4349+
options?.withFileTypes ? [] : [sidecarName],
4350+
)
4351+
mockStat.mockImplementation(async (pathArg: string) =>
4352+
pathArg.endsWith(sidecarName) ? { mtimeMs: Date.now() - sidecarAgeMs } : Promise.reject(enoent()),
4353+
)
4354+
mockUnlink.mockResolvedValue(undefined)
4355+
const unlinkedSoFar = () => mockUnlink.mock.calls.map((call) => String(call[0]))
43384356
let sweeps = 0
43394357
const originalSweep = skillsManager["sweepStaleSkillTempFiles"]
43404358
skillsManager["sweepStaleSkillTempFiles"] = async () => {
43414359
sweeps += 1
4360+
// Count the fires and let the real sweep run beneath the counter: a counter that swallowed
4361+
// the body would pin the timing without proving the removal the retry exists to perform.
4362+
// call() rather than originalSweep(): the captured method is unbound, and an unbound call
4363+
// loses the manager instance, throws inside the sweep, and gets swallowed by the retry's own
4364+
// catch - which is how the first version of this test passed while never removing anything.
4365+
await originalSweep.call(skillsManager)
4366+
if (sweeps === 1) {
4367+
// The retry only makes sense once the freshness window has closed on this sidecar.
4368+
sidecarAgeMs = 10 * 60 * 1000
4369+
}
43424370
}
43434371
try {
43444372
await skillsManager.initialize()
43454373
expect(sweeps).toBe(1)
4374+
expect(unlinkedSoFar(), "the first sweep leaves a fresh sidecar alone").not.toContain(sidecarPath)
43464375
// SKILL_TEMP_SIDECAR_STALE_MS is ten minutes and module-private, so the window is written
43474376
// here and the assertions below pin which side of it fires.
43484377
await vi.advanceTimersByTimeAsync(10 * 60 * 1000 - 1)
43494378
expect(sweeps).toBe(1)
4379+
expect(unlinkedSoFar()).not.toContain(sidecarPath)
43504380
await vi.advanceTimersByTimeAsync(1)
43514381
expect(sweeps).toBe(2)
4382+
// The removal itself: the retry is what turns a skipped sidecar into a deleted one.
4383+
expect(unlinkedSoFar(), "the delayed sweep removes the sidecar the first sweep skipped").toContain(
4384+
sidecarPath,
4385+
)
43524386
} finally {
43534387
skillsManager["sweepStaleSkillTempFiles"] = originalSweep
43544388
vi.useRealTimers()

0 commit comments

Comments
 (0)