Skip to content

Commit 6768ccf

Browse files
author
easonLiangWorldedtech
committed
fix(apply-patch): recheck cancellation before publication starts
A task can be aborted while the guard awaits its preflight, so the publish started for a task the caller had already reported as gone. Cancellation is now rechecked after the awaited preflight and immediately before publication.
1 parent ddece31 commit 6768ccf

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,24 @@ describe("guardedWrite (S4a, epic #1375)", () => {
793793
expect(mockedSafeWriteText).not.toHaveBeenCalled()
794794
})
795795

796+
it("re-checks cancellation after the awaited preflight, before publication starts", async () => {
797+
// The abort lands while the version token is being computed. The guard has to
798+
// refuse on the way to the publish, not write for a task that is already gone.
799+
mockedComputeVersionToken.mockImplementation(async () => {
800+
task.abort = true
801+
return "v1"
802+
})
803+
const reg = new ObservationRegistry()
804+
reg.observe(abs("a.txt"), "v1", true)
805+
const task = createMockTask({ observationRegistry: reg })
806+
807+
await expect(guardedWrite(task, "a.txt", "content", "update")).rejects.toThrow(
808+
"Task was cancelled before this write published -- nothing was written.",
809+
)
810+
811+
expect(mockedSafeWriteText).not.toHaveBeenCalled()
812+
})
813+
796814
it("names the caller's path, not the resolved absolute path, in a model-facing rejection", async () => {
797815
// The guard key stays absolute, but the message and the error field go to the
798816
// model, so they must not carry a user-specific absolute path.

‎src/core/tools/guardedWrite.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ export async function createIfAbsent(
162162
// A real I/O failure (EACCES, EIO, ...) -- not a guard verdict.
163163
throw error
164164
}
165+
// Immediately before publication starts.
166+
cancelledBeforePublish(absolutePath, displayPath, isCancelled)
165167
await safeWriteText(absolutePath, content)
166168
// Read the new token under the same lock, otherwise a peer lock-using
167169
// writer can publish in the gap and the caller records that writer's
@@ -230,7 +232,14 @@ export async function replaceIfVersion(
230232
throw error
231233
}
232234

235+
// Re-checked after the awaited preflight: the task can be aborted while this
236+
// operation waits, and a publish that starts after that is a write the caller
237+
// has already reported as not performed.
238+
cancelledBeforePublish(absolutePath, displayPath, isCancelled)
239+
233240
if (currentVersion === expectedVersion) {
241+
// Immediately before publication starts.
242+
cancelledBeforePublish(absolutePath, displayPath, isCancelled)
234243
await safeWriteText(absolutePath, content)
235244
// Read the new token under the same lock, otherwise a peer lock-using
236245
// writer can publish in the gap and the caller records that writer's

0 commit comments

Comments
 (0)