Skip to content

Commit 94dc7c4

Browse files
test(tools): run the guarded-write cancellation rows through Task's own requestAbort
CI's compile arm typechecks the PR merge tree (log line: 'git checkout --force refs/remotes/pull/1405/merge', 'HEAD is now at 93a83aa Merge 7ea7080 into e487cd7'), and main took 5319fe9 (#2283, 2026-11-08T16:18:32Z), which makes Task.abort a get-only accessor fed by the run-state latches. Three rows in this file wrote task.abort directly - one added here, two pre-existing - so compile turned from success at 90bbdad to failure at 7ea7080 with: core/tools/__tests__/guardedWrite.spec.ts(549,10): error TS2540: Cannot assign to 'abort' core/tools/__tests__/guardedWrite.spec.ts(810,8): error TS2540: Cannot assign to 'abort' core/tools/__tests__/guardedWrite.spec.ts(824,8): error TS2540: Cannot assign to 'abort' Reproduced locally by checking out the merge tree (git merge-tree --write-tree gives 3b3f02f with no conflicts; tsc --noEmit in that tree printed those same three errors). The stand-in now sits on Task.prototype with the fields the constructor would have initialized, so requestAbort is the class's own method and the rows ask production to set the flag instead of setting it themselves. That works on both spellings: this branch's field write at Task.ts:3596 and main's latch-fed getter at Task.ts:3582. Measured: tsc --noEmit at this head 0 errors; eslint --max-warnings=0 on the file 0 messages; prettier --check clean; guardedWrite.spec.ts 41 passed (41).
1 parent 7ea7080 commit 94dc7c4

1 file changed

Lines changed: 47 additions & 12 deletions

File tree

‎src/core/tools/__tests__/guardedWrite.spec.ts‎

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ import { createIfAbsent, guardedWrite, replaceIfVersion, resetChain } from "../g
1919
import { safeWriteText, type SafeWriteTextOptions } from "../../../services/file-safety/safeWriteText"
2020
import { computeVersionToken } from "../../../utils/versionToken"
2121
import { ObservationRegistry } from "../../task/observationRegistry"
22-
import type { Task } from "../../task/Task"
22+
import { Task } from "../../task/Task"
23+
import { initialRunState, type RunState } from "../../task/run-state/runState"
24+
import type { RunRejection } from "../../task/run-state/runStateShadow"
2325

2426
// -- Mocks -------------------------------------------------------------------
2527

@@ -53,18 +55,51 @@ interface MockTaskOptions {
5355
}
5456

5557
/**
56-
* Minimal structural Task: guardedWrite only reads task.cwd and
57-
* task.observationRegistry. The real Task constructor needs the full provider
58-
* machinery, so a single documented double cast stands in for the class.
58+
* Task stand-in that runs the production cancellation path instead of re-implementing it.
59+
*
60+
* guardedWrite reads cwd, observationRegistry and abort off the task
61+
* (src/core/tools/guardedWrite.ts:289, :351, :358), and building a real Task needs the full
62+
* provider machinery: the smallest spec on this branch that does it,
63+
* src/core/task/__tests__/Task.abort-reason-race.spec.ts, carries 12 vi.mock blocks and a
64+
* 34-line provider double for exactly that reason. So this stands on Task.prototype and
65+
* supplies only the values the constructor would have initialized. requestAbort is then the
66+
* class's own method: the flag write at Task.ts:3596 and the run-state kernel it drives
67+
* (Task.ts:3592-3598, driveRunEvent at :3610-3631 over runState and runRejections) are
68+
* production code rather than a re-implementation in the double.
69+
*
70+
* Nothing here depends on which spelling of abort the tree carries. cwd is an accessor on
71+
* Task.prototype (Task.ts:6494), so it is shadowed with an own property instead of assigned;
72+
* abort is left to production - undefined until requestAbort runs, which reads the same as
73+
* false at the truthy check in guardedWrite.ts:358 - because that call site reads only abort,
74+
* while main (5319fe91f, #2283) exposes abort as a get-only accessor fed by the kernel latches
75+
* (src/core/task/run-state/runState.ts:188), where a test that wrote the field would not
76+
* compile: the CI typecheck runs on the PR merge tree, and this file writing task.abort is
77+
* what made compile fail there.
5978
*/
79+
/** The fields Task's constructor initializes that this stand-in has to supply by hand. */
80+
interface TaskDoubleFields {
81+
cwd?: string
82+
observationRegistry?: ObservationRegistry
83+
runState?: RunState
84+
runRejections?: RunRejection[]
85+
}
86+
6087
function createMockTask(options: MockTaskOptions = {}): Task {
61-
const task = {
62-
cwd: options.cwd ?? WORKSPACE,
63-
observationRegistry: options.observationRegistry ?? new ObservationRegistry(),
64-
}
88+
// Object.create is untyped in this lib: its signature takes no type argument, so the shape
89+
// comes from the structural type above rather than from a generic parameter.
90+
const task: TaskDoubleFields = Object.create(Task.prototype)
91+
Object.defineProperty(task, "cwd", {
92+
value: options.cwd ?? WORKSPACE,
93+
writable: true,
94+
configurable: true,
95+
})
96+
task.observationRegistry = options.observationRegistry ?? new ObservationRegistry()
97+
task.runState = initialRunState
98+
task.runRejections = []
99+
// The stand-in covers only the members guardedWrite touches; Task's own surface is wider and
100+
// no constructor runs here, so the double assertion stands in for that gap.
65101
return task as unknown as Task
66102
}
67-
68103
// -- Tests -------------------------------------------------------------------
69104

70105
describe("guardedWrite (S4a, epic #1375)", () => {
@@ -546,7 +581,7 @@ describe("guardedWrite (S4a, epic #1375)", () => {
546581
setImmediate(resolve)
547582
})
548583
// The second link is still queued behind the gated first one when the task goes away.
549-
task.abort = true
584+
task.requestAbort(false)
550585
releaseFirst()
551586
await first
552587
await expect(second).rejects.toThrow(/was cancelled/)
@@ -807,7 +842,7 @@ describe("task cancellation (S4a, epic #1375)", () => {
807842
await new Promise(function (resolve) {
808843
setImmediate(resolve)
809844
})
810-
task.abort = true
845+
task.requestAbort(false)
811846
releaseFirst()
812847
await first
813848
await expect(second).rejects.toThrow(/was cancelled/)
@@ -821,7 +856,7 @@ describe("task cancellation (S4a, epic #1375)", () => {
821856

822857
it("refuses an already-cancelled task's write before any I/O", async () => {
823858
const task = createMockTask()
824-
task.abort = true
859+
task.requestAbort(false)
825860
await expect(guardedWrite(task, "gone.txt", "x", "create")).rejects.toThrow(/was cancelled/)
826861
expect(mockedSafeWriteText).not.toHaveBeenCalled()
827862
expect(mockedFsAccess).not.toHaveBeenCalled()

0 commit comments

Comments
 (0)