Skip to content

Commit a8d6e3b

Browse files
test(core): gate the metadata retry and isolate the abort-path release
The retry assertion needed a gate: with the retry settling as soon as it started, the call counts passed whether or not dispose() awaited it. The gate holds the retry open so the test can observe disposal still waiting, and the drain loop fails if the awaited call is dropped. The two abort tests now disable disposal's own release trigger on the way into dispose(), so the release they count can only be the stream-abort path's; without the swap either path could produce the same count.
1 parent 9ebacb5 commit a8d6e3b

1 file changed

Lines changed: 71 additions & 1 deletion

File tree

‎src/core/task/__tests__/Task.teardown.spec.ts‎

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,20 @@ describe("Task teardown, dispose-time metadata repair and diff-view release", ()
431431
// already on disk.
432432
const taskDir = path.join(os.tmpdir(), "test-storage", "tasks", "00000000-0000-7000-8000-000000000000")
433433
fsReal.mkdirSync(taskDir, { recursive: true })
434+
// The retry is held open by a gate. Ungated it settles the moment it starts, so the
435+
// call-count assertions below would pass whether or not dispose() awaited it. Holding
436+
// it open is what lets the test observe disposal still waiting on the retry.
437+
let releaseRetry: (() => void) | undefined
438+
const retryGate = new Promise<void>((resolve) => {
439+
releaseRetry = resolve
440+
})
434441
const historySpy = vi
435442
.spyOn(mockProvider, "updateTaskHistory")
436443
.mockRejectedValueOnce(new Error("history stage unavailable"))
444+
.mockImplementation(async () => {
445+
await retryGate
446+
return []
447+
})
437448

438449
const task = new Task({
439450
provider: mockProvider,
@@ -448,10 +459,31 @@ describe("Task teardown, dispose-time metadata repair and diff-view release", ()
448459
expect(historySpy).toHaveBeenCalledTimes(1)
449460
expect(consoleErrorSpy).toHaveBeenCalledWith("Failed to save task metadata:", expect.any(Error))
450461

451-
await task.dispose()
462+
let disposalSettled = false
463+
const disposalFinished = task.dispose().then(() => {
464+
disposalSettled = true
465+
})
466+
467+
try {
468+
// Drain until disposal settles or the cap is reached. The gate is the only thing holding the
469+
// retry open, so a disposal that awaits the retry cannot settle inside this loop; a disposal
470+
// that dropped the awaited call runs the loop out having settled on its own.
471+
for (let tick = 0; tick < 2000 && !disposalSettled; tick++) {
472+
await new Promise((wake) => setImmediate(wake))
473+
}
474+
expect(historySpy).toHaveBeenCalledTimes(2)
475+
// dispose() has not resolved while the retry it awaits is still open. Turning the
476+
// awaited call into a floating one fails here instead of going unnoticed.
477+
expect(disposalSettled).toBe(false)
478+
} finally {
479+
releaseRetry?.()
480+
}
481+
482+
await disposalFinished
452483

453484
// dispose() awaited the retry: by the time it resolves the metadata stage has run
454485
// again with the recomputed history item for this task.
486+
expect(disposalSettled).toBe(true)
455487
expect(historySpy).toHaveBeenCalledTimes(2)
456488
// The history record is keyed by `id` (the task id), not a taskId field.
457489
expect(historySpy.mock.calls[1][0]).toEqual(expect.objectContaining({ id: task.taskId }))
@@ -669,13 +701,32 @@ describe("Task teardown, dispose-time metadata repair and diff-view release", ()
669701
const discardSpy = vi.spyOn(task.diffViewProvider, "discardUnapprovedStream").mockResolvedValue(undefined)
670702
task.diffViewProvider.isEditing = true
671703
task.diffViewProvider.editType = "create"
704+
// Isolation: disposal's own release trigger is gated on isStreaming, so clearing that flag
705+
// on the way into dispose() leaves the stream-abort path as the only one left that can start
706+
// a release here. Without the swap the two paths cannot be told apart - abortTaskOnce() voids
707+
// dispose(), and the call count below could be reporting either one of them.
708+
const disposeReal = task.dispose.bind(task)
709+
let disposeCalls = 0
710+
const disposeSpy = vi.spyOn(task, "dispose").mockImplementation(() => {
711+
disposeCalls++
712+
task.isStreaming = false
713+
return disposeReal.call(task)
714+
})
672715

673716
await task.recursivelyMakeClineRequests([{ type: "text", text: "cancel this stream" }]).catch(() => {})
674717

718+
// Disposal really ran, so the swap above was exercised rather than vacuous, and every entry
719+
// into it had its release trigger disabled: the release counted below is the abort path's own.
720+
expect(disposeCalls).toBeGreaterThan(0)
721+
const sharedRelease = task["diffReversionPromise"]
722+
expect(sharedRelease).toBeDefined()
723+
// It had already settled before this line, so disposal never got the chance to start it.
724+
await sharedRelease
675725
expect(discardSpy).toHaveBeenCalledTimes(1)
676726
expect(revertSpy).not.toHaveBeenCalled()
677727
revertSpy.mockRestore()
678728
discardSpy.mockRestore()
729+
disposeSpy.mockRestore()
679730
})
680731

681732
it("reverts a modify preview from the stream abort path, whose restore is the content on disk", async () => {
@@ -703,13 +754,32 @@ describe("Task teardown, dispose-time metadata repair and diff-view release", ()
703754
const discardSpy = vi.spyOn(task.diffViewProvider, "discardUnapprovedStream").mockResolvedValue(undefined)
704755
task.diffViewProvider.isEditing = true
705756
task.diffViewProvider.editType = "modify"
757+
// Isolation: disposal's own release trigger is gated on isStreaming, so clearing that flag
758+
// on the way into dispose() leaves the stream-abort path as the only one left that can start
759+
// a release here. Without the swap the two paths cannot be told apart - abortTaskOnce() voids
760+
// dispose(), and the call count below could be reporting either one of them.
761+
const disposeReal = task.dispose.bind(task)
762+
let disposeCalls = 0
763+
const disposeSpy = vi.spyOn(task, "dispose").mockImplementation(() => {
764+
disposeCalls++
765+
task.isStreaming = false
766+
return disposeReal.call(task)
767+
})
706768

707769
await task.recursivelyMakeClineRequests([{ type: "text", text: "cancel this stream" }]).catch(() => {})
708770

771+
// Disposal really ran, so the swap above was exercised rather than vacuous, and every entry
772+
// into it had its release trigger disabled: the release counted below is the abort path's own.
773+
expect(disposeCalls).toBeGreaterThan(0)
774+
const sharedRelease = task["diffReversionPromise"]
775+
expect(sharedRelease).toBeDefined()
776+
// It had already settled before this line, so disposal never got the chance to start it.
777+
await sharedRelease
709778
expect(revertSpy).toHaveBeenCalledTimes(1)
710779
expect(discardSpy).not.toHaveBeenCalled()
711780
revertSpy.mockRestore()
712781
discardSpy.mockRestore()
782+
disposeSpy.mockRestore()
713783
})
714784

715785
it("starts one release per diff view and hands every later caller the same promise", async () => {

0 commit comments

Comments
 (0)