Skip to content

Commit d0496c5

Browse files
fix(core): answer the review nits on the observation registry unit
- observationRegistry.spec.ts: restore real timers from an afterEach on the describe block, so a failing assertion in the fake-timer test cannot leave the rest of the file running on fake timers. - Task.dispose.test.ts: await the second Task's disposal in the finally block instead of firing it and forgetting it, so teardown is deterministic and a rejection surfaces. - Task.ts: describe cancellationGeneration in repository terms. The run-state kernel has no transition that clears latches.abort, so abort stays true across resumeAfterDelegation() and only the counter keeps a cancelled run apart from the resumed one; the guarded write the comment documents has no production reader yet, so the comment no longer claims a comparison that does not happen. The production change is comment-only: the mutation gate still prices 25 counted executable lines and 15 selectors for this unit.
1 parent b9b85de commit d0496c5

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

‎src/core/task/Task.ts‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,14 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
397397

398398
providerRef: WeakRef<ClineProvider>
399399
private readonly globalStoragePath: string
400-
// Monotonic cancellation counter (branch-side guard machinery; main's run-state
401-
// kernel supplies the abort getter, so the plain abort field from the branch is
402-
// dropped in favor of main's `get abort()` over runState). requestAbort() and
403-
// requestDispose() advance this fence, and the guarded-write path compares the
404-
// value it captured when the write was queued with the value at publish time,
405-
// so a queued write cannot publish after the task was cancelled or disposed.
400+
// Monotonic cancellation fence. `requestAbort()` and `requestDispose()` advance it
401+
// on every cancellation and it never goes backwards: the run-state kernel has no
402+
// transition that clears `latches.abort`, so `abort` stays true across
403+
// `resumeAfterDelegation()`, and only this counter keeps a cancelled run apart
404+
// from the run that resumes afterwards. A guarded write would compare the value it
405+
// captured when the write was queued with the value at publish time; nothing
406+
// publishes through such a path today, so the counter has no production reader yet
407+
// and only its tests pin this behavior.
406408
cancellationGeneration: number = 0
407409
currentRequestAbortController?: AbortController
408410
/**
@@ -3607,13 +3609,12 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
36073609
if (isAbandoned) {
36083610
this.abandoned = true
36093611
}
3610-
// The branch-side write guard compares this counter with the value a queued
3611-
// write captured when it started, so every cancellation must advance it; it
3612-
// must never go backwards because resumeAfterDelegation() clears abort via the
3613-
// kernel (the flag alone cannot keep the cancelled run and the resumed run
3614-
// apart). Main's driveRunEvent below is the single cancellation entry point;
3615-
// the branch's direct `this.abort = true` assignment is dropped because main
3616-
// exposes abort as a getter over the run-state kernel.
3612+
// Every cancellation must advance the fence, and it must never go backwards: the
3613+
// kernel never clears `latches.abort`, so a resumed run keeps `abort` true and
3614+
// only the counter separates the cancelled run from the resumed one.
3615+
// `driveRunEvent` below is the single cancellation entry point, and the
3616+
// guarded-write consumer documented at the field is what would compare this
3617+
// value across a queue hop.
36173618
this.cancellationGeneration++
36183619
this.driveRunEvent({ tag: "abortRequested", abandoned: isAbandoned })
36193620
}
@@ -3624,10 +3625,10 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
36243625
}
36253626

36263627
requestDispose(): void {
3627-
// A host teardown never calls abortTask(): disposal has to raise the same
3628+
// A host teardown never calls `abortTask()`: disposal has to raise the same
36283629
// fence, or a write still parked on its path chain publishes after the task
3629-
// is gone. Same doctrine as requestAbort: main's disposeRequested event drives
3630-
// abort through the kernel; the branch's `this.abort = true` is dropped.
3630+
// is gone. Same doctrine as `requestAbort`: the `disposeRequested` event below
3631+
// drives `abort` through the kernel, which never releases the latch.
36313632
this.cancellationGeneration++
36323633
this.driveRunEvent({ tag: "disposeRequested" })
36333634
}

‎src/core/task/__tests__/Task.dispose.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ describe("Task dispose method", () => {
119119
expect(disposalComplete).toBe(true)
120120
})
121121

122-
test("owns a per-Task observation registry", () => {
122+
test("owns a per-Task observation registry", async () => {
123123
// ReadFileTool and the guarded-write path reach the registry only through the Task,
124124
// so a Task that never built one - or shared one across Tasks - would silently drop
125125
// read observations. The registry's own tests only exercise standalone instances, and
@@ -142,7 +142,7 @@ describe("Task dispose method", () => {
142142
expect(task.observationRegistry.has("/workspace/a.ts")).toBe(false)
143143
expect(task.observationRegistry.size).toBe(0)
144144
} finally {
145-
void other.dispose().catch(() => {})
145+
await other.dispose()
146146
}
147147
})
148148

‎src/core/task/__tests__/observationRegistry.spec.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
import { describe, it, expect, vi } from "vitest"
1+
import { afterEach, describe, it, expect, vi } from "vitest"
22

33
import { ObservationRegistry } from "../observationRegistry"
44

55
describe("ObservationRegistry", () => {
6+
afterEach(() => {
7+
vi.useRealTimers()
8+
})
9+
610
it("observe → get returns the recorded version and observedAt", () => {
711
const reg = new ObservationRegistry()
812
reg.observe("/a/b/c.ts", "1:2:300:4000000000:5000000000")
@@ -25,8 +29,6 @@ describe("ObservationRegistry", () => {
2529
const second = reg.get("/a/b/c.ts")!
2630
expect(second.version).toBe("v2")
2731
expect(second.observedAt).toBeGreaterThan(first.observedAt)
28-
29-
vi.useRealTimers()
3032
})
3133

3234
it("has returns true for observed paths, false otherwise", () => {

0 commit comments

Comments
 (0)