Skip to content

Commit f2b4d1c

Browse files
feat(core): add ObservationRegistry with completeness scope and close semantics, wired into Task (U5c-7, split of #1991) (#2396)
* feat(core): add ObservationRegistry with its spec and the Task consumer (U5c-7, split of #1991) ObservationRegistry records per-file observation state with a completeness flag, refuses further recording once closed, and is dropped when a task is disposed. It lands together with the Task-side code that imports it, because the repo's own policy asserts that a production file imported only by tests is still reported as unused (scripts/knip-policy.test.mjs) and knip --production is a required check. 384 additions / 0 deletions across 5 paths. The mutation gate counts 25 of 500 executable lines and 15 of 400 selectors at this base. Content ported byte-identical from #2357 (5319fe9..d6fa711). * 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. * reformat the transplanted cancellation-generation row to prettier style The row advancing the cancellation generation for the disposal an unmocked abort performs was moved onto main during the rebase, and prettier --check over the tree reported this file as not conforming while tsc exited 0 and vitest passed 242 tests over the three touched specs, so the gap was layout and not content. Running prettier --write on this file changed 315841 bytes into 315840 bytes over the same non-blank line count, and every non-blank line is identical once leading, trailing and repeated whitespace are collapsed, so nothing was added, removed, or reworded. Named rather than folded into the replayed commit, because a formatting change made by this lane should be visible as one.
1 parent 6019370 commit f2b4d1c

3 files changed

Lines changed: 38 additions & 5 deletions

File tree

‎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__/Task.spec.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4819,6 +4819,37 @@ describe("Cline", () => {
48194819
expect(task.abort).toBe(true)
48204820
expect(task.cancellationGeneration).toBe(1)
48214821
})
4822+
it("advances the cancellation generation for the disposal an unmocked abort performs", async () => {
4823+
// The two rows above mock dispose(), so each proves one increment in isolation. The
4824+
// sequence the guarded-write fence actually sees is an abort that tears the task down:
4825+
// requestAbort raises the fence, and the dispose() that abortTaskOnce drives raises it
4826+
// again. Nothing is mocked here, so if either increment stopped landing - or if disposal
4827+
// stopped being part of an abort - this row reads the stale value.
4828+
const task = new Task({
4829+
provider: mockProvider,
4830+
apiConfiguration: mockApiConfig,
4831+
task: "test task",
4832+
startTask: false,
4833+
})
4834+
4835+
expect(task.cancellationGeneration).toBe(0)
4836+
4837+
// Started, not awaited. requestAbort is synchronous and raises the fence before any of the
4838+
// abort work runs (Task.abortTask calls it before it hands back abortPromise), so the increment
4839+
// has to be visible while the abort promise is still pending. Asserting the generation only
4840+
// after the await would still pass if the increment landed at the end of the teardown, which is
4841+
// exactly the delay that lets a write parked on its path chain publish after the task is gone.
4842+
const aborting = task.abortTask()
4843+
expect(task.cancellationGeneration).toBe(1)
4844+
4845+
await aborting
4846+
4847+
expect(task.abort).toBe(true)
4848+
// Disposal never marks a task abandoned; only an explicit abandon does.
4849+
expect(task.abandoned).toBe(false)
4850+
// One from requestAbort, one from the disposal the abort performed.
4851+
expect(task.cancellationGeneration).toBe(2)
4852+
})
48224853

48234854
it("flushes pending state before TaskAborted and disposal while queue state is intact", async () => {
48244855
const task = new Task({

‎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)