Skip to content

Commit 13a943d

Browse files
S4b (Zoo-Code-Org#1375): a committed write that is not durable keeps its authorization
The rename is the point of no return for the content, and the observation was not following it. When the commit landed but the parent-directory fsync did not, PostCommitDurabilityError unwound past guardedWrite's post-publish refresh, so the observation stayed at the version the read recorded. The next edit of that same file - content the tool itself had just written - was then refused as stale against bytes that no longer existed, and ApplyPatchTool's finally sweep withdrew the read entirely because the publish had not returned. Withdrawal is the right disposition for a publish that did not happen and the wrong one for content that is on disk. PostCommitDurabilityError now carries committedVersion: the version token the commit landed, read best-effort with the same derivation every guard uses, and the message says what to do with it - "committed, not durable: do not re-issue the write; read the file again before editing it" - naming the version so the instruction is checkable rather than a style preference. When the post-commit stat itself fails, the field stays undefined and the message says the same thing without inventing a version. guardedWrite treats that error as a publish that succeeded with a caveat: the token the error carries feeds the same refresh a successful publish gets - keeping a partial observation partial, because the branch that sets that never ran - and the durability failure is thrown after the refresh, so the caller sees the accepted state and the caveat together. A failure before the rename still unwinds untouched: nothing was committed to record. That ordering is what ApplyPatchTool's sweep depends on. It forgets a tool-recorded read only when the stored entry is still the one it recorded, so an entry the refresh re-earned survives, and the withdrawal still applies to every read whose publish never landed. ApplyPatchTool needed no production change for that - the sweep was already keyed to the entry rather than the path - so its half of this commit is the row that pins the property. This commit touches services/file-safety/safeWriteText.ts and core/tools/guardedWrite.ts in production, and three spec lanes. It is deliberately one commit: the token is meaningless without the refresh that reads it, and the refresh is meaningless without the error that carries it. Gates: safeWriteText 77 passed (2 rows new), guardedWrite 73 passed (4 rows new), applyPatchTool.execute 34 passed (1 row new), applyPatchTool.partial and the applyDiff/apply-patch lanes green, write-path callers (writeToFileTool, editFileTool, editTool, searchReplaceTool) green, task abort races 9 passed, DiffViewProvider 168 passed, safeWriteJson + fileLock 81 passed | 4 skipped. Together the blast-radius run is 14 files, 530 passed | 5 skipped. eslint --max-warnings=0 exit 0 on all seven touched files; --prune-suppressions run afterwards and src/eslint-suppressions.json restored to HEAD (a narrow prune pass also drops records for files outside the selection, so it is never committed). prettier --check clean. tsc --noEmit -p . shows only activate/registerCommands.ts(88,102) TS2739, the local junction artifact. No it.runIf(win32) gate added anywhere; the parent-directory fsync path is POSIX-only by construction and the rows drive it with the platform option, not with an OS gate. Controls, each failing only for the reason its row describes: not recognizing the committed durability error -> the three durability rows report 'expected v1 to be v2'; refreshing but swallowing the failure -> the same three rows report 'promise resolved instead of rejecting'; not mirroring the partial flag -> only the partial row, 'expected true to be false'; not reading the committed version -> only the token row, 'expected undefined to be 7:8:4:9:10'; keying the sweep to the path instead of the entry -> only the apply_patch row, 'expected undefined to be committed-token'. The row that pins the pre-commit case (a publish refused before the rename leaves the read's version) passes under every one of those controls, which is the point of writing it as its own row. Three spec files had to expose the real error class through their module mock of safeWriteText (guardedWrite.spec, DiffViewProvider.spec and this lane's stubs): without it 37 provider-lane rows failed with 'No "PostCommitDurabilityError" export is defined on the mock', and a locally defined stand-in class would have let rows pass against a shape production does not build, so the factories now pull the real class through importOriginal while keeping the publish primitive stubbed. Review rows 4507764645 and 4507851715.
1 parent 802352d commit 13a943d

6 files changed

Lines changed: 327 additions & 76 deletions

File tree

‎src/core/tools/__tests__/applyPatchTool.execute.spec.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ObservationRegistry } from "../../task/observationRegistry"
1111
import { guardedWrite } from "../guardedWrite"
1212
import { ApplyPatchTool } from "../ApplyPatchTool"
1313
import { CommittedWriteBufferConflictError } from "../../../integrations/editor/committedWriteBufferConflictError"
14+
import { PostCommitDurabilityError } from "../../../services/file-safety/safeWriteText"
1415

1516
// The vi.mock factory exposes the fs/promises functions under a `default`
1617
// property (matching the SUT's default import), which the static module type
@@ -310,6 +311,40 @@ describe("ApplyPatchTool.execute - guarded write (S4b, epic #1375)", () => {
310311
expect(mockHandleError).not.toHaveBeenCalled()
311312
})
312313

314+
// Row 4507764645: the finally sweep withdraws the read behind a publish that never
315+
// landed. When the guarded commit landed and only the durability step failed, the guard
316+
// re-records at the committed token, and the sweep must leave that entry alone - the
317+
// stored entry is no longer the one the tool recorded, so the withdrawal does not fit.
318+
it("update: a publish that committed without durability keeps the re-recorded observation", async () => {
319+
const targetKey = path.resolve("/workspace/project", "src/thing.ts")
320+
const reg = mockTask.observationRegistry
321+
// The double stands in for the guard's own pair of actions - re-record at the
322+
// committed token, then report the durability failure - so this row tests the sweep's
323+
// identity key, which is ApplyPatchTool's half of the rule.
324+
mockSaveDirectly.mockImplementationOnce(async () => {
325+
reg.observe(targetKey, "committed-token", false)
326+
throw new PostCommitDurabilityError(
327+
targetKey,
328+
new Error("EBADF: bad file descriptor"),
329+
null,
330+
"committed-token",
331+
)
332+
})
333+
334+
await tool.execute({ patch: updatePatch }, mockTask as Task, {
335+
askApproval: mockAskApproval,
336+
handleError: mockHandleError,
337+
pushToolResult: mockPushToolResult,
338+
toolCallId: "test-tool-call-id",
339+
})
340+
341+
expect(mockHandleError).toHaveBeenCalledWith(
342+
"apply patch",
343+
expect.objectContaining({ name: "PostCommitDurabilityError" }),
344+
)
345+
expect(reg.get(targetKey)?.version).toBe("committed-token")
346+
expect(mockPushToolResult).not.toHaveBeenCalled()
347+
})
313348
it("update: keeps a partial observation partial so a later full-file write is still rejected", async () => {
314349
// The hunk read is the tool's own read, not a model read. Upgrading the
315350
// model's partial view to complete here would let a later write_to_file

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

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import * as path from "path"
1515
import { describe, expect, it, beforeEach, vi } from "vitest"
1616

1717
import { createIfAbsent, guardedWrite, replaceIfVersion, resetChain, GuardRejectedError } from "../guardedWrite"
18-
import { safeWriteText } from "../../../services/file-safety/safeWriteText"
18+
import { PostCommitDurabilityError, safeWriteText } from "../../../services/file-safety/safeWriteText"
1919
import { computeVersionToken } from "../../../utils/versionToken"
2020
import { withFileLock } from "../../../utils/fileLock"
2121
import { resolveLockKey } from "../../../services/file-safety/safeWriteText"
@@ -35,10 +35,17 @@ vi.mock("../../../utils/versionToken", () => ({
3535
computeVersionToken: vi.fn(),
3636
}))
3737

38-
vi.mock("../../../services/file-safety/safeWriteText", () => ({
39-
safeWriteText: vi.fn(),
40-
resolveLockKey: vi.fn(async (p: string) => p),
41-
}))
38+
vi.mock("../../../services/file-safety/safeWriteText", async (importOriginal) => {
39+
// The durability branch is matched by name, so the class has to be the real one - a
40+
// local stand-in would let the rows below pass against a shape production does not
41+
// build. The publish primitive stays a stub.
42+
const actual = await importOriginal<typeof import("../../../services/file-safety/safeWriteText")>()
43+
return {
44+
safeWriteText: vi.fn(),
45+
resolveLockKey: vi.fn(async (p: string) => p),
46+
PostCommitDurabilityError: actual.PostCommitDurabilityError,
47+
}
48+
})
4249

4350
vi.mock("../../../utils/fileLock", () => ({
4451
withFileLock: vi.fn(),
@@ -1332,4 +1339,86 @@ describe("guardedWrite (S4a, epic #1375)", () => {
13321339
expect(staleError?.path).toBe("src/thing.ts")
13331340
expect(mockedSafeWriteText).not.toHaveBeenCalled()
13341341
})
1342+
1343+
describe("post-commit durability failure", () => {
1344+
// Row 4507764645 / 4507851715: the rename is the point of no return for the
1345+
// observation as well as for the content. A write whose commit landed but whose
1346+
// parent-directory fsync did not has to leave the caller authorized against the bytes
1347+
// that are actually on disk, while the durability failure is still reported.
1348+
const durabilityError = (target: string, committedVersion?: string): PostCommitDurabilityError =>
1349+
new PostCommitDurabilityError(target, new Error("EBADF: bad file descriptor"), null, committedVersion)
1350+
1351+
it("keeps the observation on the committed version and still reports the failure", async () => {
1352+
const target = abs("durability.txt")
1353+
const task = createMockTask()
1354+
task.observationRegistry.observe(target, "v1", true)
1355+
mockedComputeVersionToken.mockResolvedValue("v1") // the version check passes
1356+
const failure = durabilityError(target, "v2")
1357+
mockedSafeWriteText.mockRejectedValueOnce(failure)
1358+
1359+
await expect(guardedWrite(task, "durability.txt", "new", "update")).rejects.toBe(failure)
1360+
1361+
// The content on disk is v2's bytes; recording v1 would refuse the next edit of a
1362+
// file this very write changed, and recording nothing would withdraw authority the
1363+
// tool earned by writing the content itself.
1364+
expect(task.observationRegistry.get(target)?.version).toBe("v2")
1365+
expect(failure.message).toContain("committed, not durable")
1366+
expect(failure.committedVersion).toBe("v2")
1367+
})
1368+
1369+
it("accepts a later write that matches the committed version", async () => {
1370+
const target = abs("second-write.txt")
1371+
const task = createMockTask()
1372+
task.observationRegistry.observe(target, "v1", true)
1373+
mockedComputeVersionToken.mockResolvedValue("v1")
1374+
mockedSafeWriteText.mockRejectedValueOnce(durabilityError(target, "v2"))
1375+
1376+
const first = await guardedWrite(task, "second-write.txt", "first", "update").catch(
1377+
(error: unknown) => error,
1378+
)
1379+
expect(first).toBeInstanceOf(PostCommitDurabilityError)
1380+
1381+
// The second write compares against the recorded version, which the refresh moved
1382+
// to the committed token: it runs, and its own publish is not refused as stale.
1383+
mockedComputeVersionToken.mockResolvedValue("v2")
1384+
mockedSafeWriteText.mockResolvedValue(undefined)
1385+
await guardedWrite(task, "second-write.txt", "second", "update")
1386+
1387+
expect(mockedSafeWriteText).toHaveBeenCalledTimes(2)
1388+
expect(task.observationRegistry.get(target)?.version).toBe("v2")
1389+
})
1390+
1391+
it("leaves the observation at the read version when nothing was committed", async () => {
1392+
const target = abs("pre-commit.txt")
1393+
const task = createMockTask()
1394+
task.observationRegistry.observe(target, "v1", true)
1395+
mockedComputeVersionToken.mockResolvedValue("v1")
1396+
const enospc = Object.assign(new Error("ENOSPC: no space left on device"), { code: "ENOSPC" })
1397+
mockedSafeWriteText.mockRejectedValueOnce(enospc)
1398+
1399+
await expect(guardedWrite(task, "pre-commit.txt", "new", "update")).rejects.toBe(enospc)
1400+
1401+
// The other half of the rule: only a commit re-records. A failure before the rename
1402+
// leaves the read's version as the only true one.
1403+
expect(task.observationRegistry.get(target)?.version).toBe("v1")
1404+
})
1405+
1406+
it("keeps a partial observation partial when the publish is remembered", async () => {
1407+
const target = abs("partial-durability.txt")
1408+
const task = createMockTask()
1409+
task.observationRegistry.observe(target, "v1", false)
1410+
mockedComputeVersionToken.mockResolvedValue("v1")
1411+
mockedSafeWriteText.mockRejectedValueOnce(durabilityError(target, "v2"))
1412+
1413+
await expect(guardedWrite(task, "partial-durability.txt", "new", "edit")).rejects.toBeInstanceOf(
1414+
PostCommitDurabilityError,
1415+
)
1416+
1417+
// The branch that sets this never ran on the durability path: an edit authorized by
1418+
// a slice must not become authority for a full-file replacement of content the model
1419+
// never read.
1420+
expect(task.observationRegistry.get(target)?.complete).toBe(false)
1421+
expect(task.observationRegistry.get(target)?.version).toBe("v2")
1422+
})
1423+
})
13351424
})

‎src/core/tools/guardedWrite.ts‎

Lines changed: 93 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import * as fs from "fs/promises"
2525
import * as path from "path"
2626

27-
import { safeWriteText } from "../../services/file-safety/safeWriteText"
27+
import { PostCommitDurabilityError, safeWriteText } from "../../services/file-safety/safeWriteText"
2828
import { computeVersionToken } from "../../utils/versionToken"
2929
import { withFileLock } from "../../utils/fileLock"
3030
import { resolveLockKey } from "../../services/file-safety/safeWriteText"
@@ -591,72 +591,95 @@ export async function guardedWrite(
591591
// this write published rather than a peer writer's.
592592
let publishedToken: string | undefined
593593

594-
if (kind === "edit") {
595-
// Edit-style writes require a prior read: no observation, no write.
596-
// A targeted edit only authorizes the view the model saw, so a
597-
// partial observation is valid for the edit itself; the version
598-
// check still rejects a file that moved since the read.
599-
if (obs === undefined) {
600-
await unobservedEditGuard(absolutePath, displayPath)
594+
// Set when the commit landed and only the durability step failed: the publish is
595+
// kept, the observation is refreshed at the token the error carries, and the
596+
// failure is reported after that refresh so the caller sees both facts.
597+
let committedButNotDurable: PostCommitDurabilityError | undefined
598+
try {
599+
if (kind === "edit") {
600+
// Edit-style writes require a prior read: no observation, no write.
601+
// A targeted edit only authorizes the view the model saw, so a
602+
// partial observation is valid for the edit itself; the version
603+
// check still rejects a file that moved since the read.
604+
if (obs === undefined) {
605+
await unobservedEditGuard(absolutePath, displayPath)
606+
} else {
607+
publishedToken = await replaceIfVersion(
608+
absolutePath,
609+
obs.version,
610+
content,
611+
displayPath,
612+
() => task.abort,
613+
verifyTarget,
614+
)
615+
staysPartial = obs.complete === false
616+
}
601617
} else {
602-
publishedToken = await replaceIfVersion(
603-
absolutePath,
604-
obs.version,
605-
content,
606-
displayPath,
607-
() => task.abort,
608-
verifyTarget,
609-
)
610-
staysPartial = obs.complete === false
611-
}
612-
} else {
613-
// "create" or "update" publish a full file built on the model's
614-
// content. A replacement of an existing target -- an "update", or a
615-
// "create" whose target is still on disk -- therefore requires a
616-
// complete observation: a slice, range, truncated, or
617-
// indentation-block read only authorizes the view the model saw, and
618-
// publishing over the existing file would silently drop everything
619-
// the model never read, so the guard fails closed with a
620-
// re-read-the-whole-file remediation. A fresh create (absent target)
621-
// needs no prior read and stays allowed.
622-
const absent = kind === "create" && (await fileIsAbsent(absolutePath))
623-
if (!absent && obs !== undefined && obs.complete === false) {
624-
throw new GuardRejectedError(
625-
"File was only partially read (line slice, range, truncated view, or indentation block) -- " +
626-
"a full-file replacement needs the complete content; re-read the whole file, then retry.",
627-
displayPath,
628-
)
629-
}
618+
// "create" or "update" publish a full file built on the model's
619+
// content. A replacement of an existing target -- an "update", or a
620+
// "create" whose target is still on disk -- therefore requires a
621+
// complete observation: a slice, range, truncated, or
622+
// indentation-block read only authorizes the view the model saw, and
623+
// publishing over the existing file would silently drop everything
624+
// the model never read, so the guard fails closed with a
625+
// re-read-the-whole-file remediation. A fresh create (absent target)
626+
// needs no prior read and stays allowed.
627+
const absent = kind === "create" && (await fileIsAbsent(absolutePath))
628+
if (!absent && obs !== undefined && obs.complete === false) {
629+
throw new GuardRejectedError(
630+
"File was only partially read (line slice, range, truncated view, or indentation block) -- " +
631+
"a full-file replacement needs the complete content; re-read the whole file, then retry.",
632+
displayPath,
633+
)
634+
}
630635

631-
if (obs === undefined) {
632-
// Never read: only an absent target may be created.
633-
publishedToken = await createIfAbsent(
634-
absolutePath,
635-
content,
636-
displayPath,
637-
() => task.abort,
638-
verifyTarget,
639-
)
640-
} else if (absent) {
641-
// A "create" on a file that vanished after the read recreates it.
642-
publishedToken = await createIfAbsent(
643-
absolutePath,
644-
content,
645-
displayPath,
646-
() => task.abort,
647-
verifyTarget,
648-
)
636+
if (obs === undefined) {
637+
// Never read: only an absent target may be created.
638+
publishedToken = await createIfAbsent(
639+
absolutePath,
640+
content,
641+
displayPath,
642+
() => task.abort,
643+
verifyTarget,
644+
)
645+
} else if (absent) {
646+
// A "create" on a file that vanished after the read recreates it.
647+
publishedToken = await createIfAbsent(
648+
absolutePath,
649+
content,
650+
displayPath,
651+
() => task.abort,
652+
verifyTarget,
653+
)
654+
} else {
655+
// The version recorded at read time must still match the on-disk
656+
// token.
657+
publishedToken = await replaceIfVersion(
658+
absolutePath,
659+
obs.version,
660+
content,
661+
displayPath,
662+
() => task.abort,
663+
verifyTarget,
664+
)
665+
}
666+
}
667+
} catch (error: unknown) {
668+
// The rename committed; only the parent-directory fsync failed. The content on disk
669+
// is the content this write published, so the token the error carries authorizes the
670+
// same refresh any successful publish gets below, and the durability failure is
671+
// rethrown after it. Withdrawing the observation here would leave the caller holding
672+
// a token for bytes that are no longer on disk and refusing the next edit of a file
673+
// this very write changed. Anything else - a guard refusal, a pre-commit I/O failure -
674+
// unwinds untouched, because there is nothing committed to record.
675+
if (error instanceof PostCommitDurabilityError && typeof error.committedVersion === "string") {
676+
publishedToken = error.committedVersion
677+
committedButNotDurable = error
678+
// The branch that would have set this never ran: an edit built on a partial
679+
// observation stays partial even when the publish is remembered.
680+
staysPartial = kind === "edit" && obs?.complete === false
649681
} else {
650-
// The version recorded at read time must still match the on-disk
651-
// token.
652-
publishedToken = await replaceIfVersion(
653-
absolutePath,
654-
obs.version,
655-
content,
656-
displayPath,
657-
() => task.abort,
658-
verifyTarget,
659-
)
682+
throw error
660683
}
661684
}
662685

@@ -673,6 +696,12 @@ export async function guardedWrite(
673696
// publish content built from the slice alone.
674697
task.observationRegistry.observe(absolutePath, publishedToken, completeOverride ?? !staysPartial)
675698
}
699+
700+
// The refresh above is what keeps the committed content authorized; the durability
701+
// caveat is not swallowed by it.
702+
if (committedButNotDurable !== undefined) {
703+
throw committedButNotDurable
704+
}
676705
})
677706
}
678707

‎src/integrations/editor/__tests__/DiffViewProvider.spec.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,17 @@ vi.mock("fs/promises", () => ({
4444
}))
4545

4646
// Mock safeWriteText (used by saveDirectly)
47-
vi.mock("../../../services/file-safety/safeWriteText", () => ({
48-
safeWriteText: vi.fn().mockResolvedValue(undefined),
49-
resolveLockKey: vi.fn(async (p: string) => p),
50-
}))
47+
vi.mock("../../../services/file-safety/safeWriteText", async (importOriginal) => {
48+
// guardedWrite matches the durability branch by name, so the class has to come from
49+
// the real module: a stand-in would let rows pass against a shape production does not
50+
// build. The publish primitive stays a stub.
51+
const actual = await importOriginal<typeof import("../../../services/file-safety/safeWriteText")>()
52+
return {
53+
...actual,
54+
safeWriteText: vi.fn(),
55+
resolveLockKey: vi.fn(async (p: string) => p),
56+
}
57+
})
5158

5259
// Mock the S1 version token (used by the S4 guarded write); the real
5360
// computeVersionToken needs fs.stat, which is not part of the fs/promises mock above.

0 commit comments

Comments
 (0)