Skip to content

Commit 89c1bd8

Browse files
test(write-to-file): pin the directory release against the reset on the early exits
The row asked for the removal-before-reset ordering on the teardowns that did not assert it: the cancellation teardown and the setup-window catch. What they can assert is not what the early-exit and validation-rejection paths can, and the reason is in the code. releaseEarlyDirectories() returns early while a session is editing (WriteToFileTool.ts:170-172). The early-exit release runs the removal and then the reset (:225 before :226) and the validation-rejection release does the same (:148 before :149), so there the pair really co-occurs and the order is the claim. teardownCancelledPreview() resets inside the isEditing branch (:197) and only then reaches the guarded release (:199), and the setup-window catch releases at :784 and resets only inside the same isEditing branch (:786-788). On those two paths the removal and the reset belong to opposite states: the reset runs only while the session edits, the removal only while it does not. What can be pinned there is that the tool never reaches past a live session for a list its provider still owns - which is now asserted at writeToFileTool.spec.ts:1160, :1193, :1762 and :1802, joining :1834, which already did it for the state-rejection variant of the setup window. The order itself goes where it is observable and was uncovered: the missing-parameter exit. The two existing missing-parameter tests (:1009, :1050) both run with a session editing, so the removal never runs there. The new test at :1081 drives the same exit with no session and a non-empty adopted list, and asserts the removal once, the reset once, removal.invocationCallOrder[0] < reset.invocationCallOrder[0] (:1119), no discard on that branch, the stream state released and the model still receiving the missing-parameter result. Controls, each installed alone and restored byte-equal (restore byte-equal reported true for both): - Order flipped: swapping :225 and :226 in WriteToFileTool.ts so the reset precedes the release gives 3 failed | 73 passed | 5 skipped, and every failure is an ordering comparison - "expected 526 to be less than 525", "expected 555 to be less than 554" (this commit's assertion at :1119) and "expected 698 to be less than 697". Nothing failed on a count, a message or an incidental mismatch. - Removal allowed to run after the reset: deleting the guard at :170-172 gives 5 failed | 71 passed | 5 skipped - the two cancellation tests, the two setup-window tests and :1834's test - each with "expected \u0022vi.fn()\u0022 to not be called at all, but actually been called 1 times". That is the shape of a release that runs after the reset has already dropped the list. Production code is untouched: this commit changes only src/core/tools/__tests__/writeToFileTool.spec.ts. Gates on the tree as committed: prettier --check exit 0, eslint --max-warnings=0 exit 0 on the spec, tsc --noEmit -p tsconfig.json exit 0, focused spec 76 passed | 5 skipped (was 75 | 5). The core vitest shard was re-run against this identical tree after the commit; its result is in the round log rather than here. Local Stryker preflight before this push: not run.
1 parent 1670ac2 commit 89c1bd8

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,13 +1078,65 @@ describe("writeToFileTool", () => {
10781078
}
10791079
})
10801080

1081+
it("removes the adopted directories before resetting on the missing-parameter exit", async () => {
1082+
// releaseEarlyExitState() releases the directories no editor took over and only then
1083+
// resets the view, and reset() drops the recorded list without touching disk, so the order
1084+
// is the claim (WriteToFileTool.ts:225 before :226). The two missing-parameter tests
1085+
// above run with a session editing, where releaseEarlyDirectories returns early
1086+
// (:170-172) and the removal never runs; this drives the same exit with no session and
1087+
// a non-empty adopted list, the shape where a reversed order would leave the
1088+
// directories behind for a write that never happened.
1089+
const adoptedDirs = ["/mock-workspace/test/nested"]
1090+
mockedCreateDirectoriesForFile.mockResolvedValue(adoptedDirs)
1091+
mockCline.diffViewProvider.isEditing = false
1092+
await executeWriteFileTool({ content: "" }, { isPartial: true })
1093+
await executeWriteFileTool({ content: "" }, { isPartial: true })
1094+
// No diff view ever opened, so nothing else owns these directories.
1095+
expect(mockCline.diffViewProvider.open).not.toHaveBeenCalled()
1096+
expect(mockCline.diffViewProvider.adoptCreatedDirectories).toHaveBeenCalledWith(adoptedDirs)
1097+
mockCline.diffViewProvider.removeAdoptedDirectories.mockClear()
1098+
mockCline.diffViewProvider.reset.mockClear()
1099+
1100+
const block = {
1101+
type: "tool_use",
1102+
name: "write_to_file",
1103+
params: { path: "a.ts" },
1104+
nativeArgs: { path: "a.ts" },
1105+
partial: false,
1106+
} as ToolUse<"write_to_file">
1107+
1108+
try {
1109+
await writeToFileTool.handle(mockCline, block, {
1110+
askApproval: mockAskApproval,
1111+
handleError: mockHandleError,
1112+
pushToolResult: mockPushToolResult,
1113+
toolCallId: "test-tool-call-id",
1114+
})
1115+
1116+
expect(mockCline.diffViewProvider.removeAdoptedDirectories).toHaveBeenCalledTimes(1)
1117+
expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1)
1118+
// Ordering, not mere occurrence: reversed, reset() would clear the list the removal reads.
1119+
expect(mockCline.diffViewProvider.removeAdoptedDirectories.mock.invocationCallOrder[0]).toBeLessThan(
1120+
mockCline.diffViewProvider.reset.mock.invocationCallOrder[0],
1121+
)
1122+
// Nothing was shown on this branch, so there is no unapproved preview to discard.
1123+
expect(mockCline.diffViewProvider.discardUnapprovedStream).not.toHaveBeenCalled()
1124+
expect(writeToFileTool["taskPartialStreamState"].size).toBe(0)
1125+
expect(mockPushToolResult).toHaveBeenCalledWith("Missing param error")
1126+
} finally {
1127+
mockedCreateDirectoriesForFile.mockResolvedValue([])
1128+
writeToFileTool["taskPartialStreamState"].clear()
1129+
}
1130+
})
10811131
it("tears the preview down when cancellation lands while update() is in flight", async () => {
10821132
// The abort handler can release this task's state while VS Code's edit application
10831133
// is still awaiting, and its own teardown may have checked isEditing before this
10841134
// delta's streamed lines landed. No later delta runs for a released stream, so
10851135
// this delta must discard and reset what it streamed into - returning silently
10861136
// here left the preview active on the disposal path.
10871137
mockCline.diffViewProvider.isEditing = true
1138+
// Counted from here: a previous test's release must not leak into the assertion below.
1139+
mockCline.diffViewProvider.removeAdoptedDirectories.mockClear()
10881140
mockCline.diffViewProvider.update = vi.fn().mockImplementation(async () => {
10891141
writeToFileTool["clearTaskState"](mockCline)
10901142
})
@@ -1100,6 +1152,12 @@ describe("writeToFileTool", () => {
11001152
expect(mockCline.diffViewProvider.update).toHaveBeenCalledTimes(1)
11011153
expect(mockCline.diffViewProvider.discardUnapprovedStream).toHaveBeenCalledTimes(1)
11021154
expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1)
1155+
// This teardown resets inside the isEditing branch (:197) and only then reaches the guarded
1156+
// release (:199). While the session edits the provider owns the directories and its own
1157+
// discard above already removed them, so the release has to stay a no-op here
1158+
// (WriteToFileTool.ts:170-172); running it after this reset would mean reaching past a live
1159+
// session for a list the reset had already dropped.
1160+
expect(mockCline.diffViewProvider.removeAdoptedDirectories).not.toHaveBeenCalled()
11031161
expect(writeToFileTool["taskPartialStreamState"].size).toBe(0)
11041162
} finally {
11051163
writeToFileTool["taskPartialStreamState"].clear()
@@ -1116,6 +1174,7 @@ describe("writeToFileTool", () => {
11161174
writeToFileTool["clearTaskState"](mockCline)
11171175
})
11181176
mockCline.diffViewProvider.discardUnapprovedStream.mockRejectedValue(new Error("restore failed"))
1177+
mockCline.diffViewProvider.removeAdoptedDirectories.mockClear()
11191178

11201179
try {
11211180
await executeWriteFileTool({}, { isPartial: true })
@@ -1129,6 +1188,9 @@ describe("writeToFileTool", () => {
11291188
// Teardown completes despite the failed discard: reset still runs and the
11301189
// state stays released.
11311190
expect(mockCline.diffViewProvider.reset).toHaveBeenCalledTimes(1)
1191+
// And it completes without removing anything: the session owns those directories while it
1192+
// edits (:170-172), and this path's reset (:197) sits ahead of the guarded release (:199).
1193+
expect(mockCline.diffViewProvider.removeAdoptedDirectories).not.toHaveBeenCalled()
11321194
expect(writeToFileTool["taskPartialStreamState"].size).toBe(0)
11331195
} finally {
11341196
writeToFileTool["taskPartialStreamState"].clear()
@@ -1677,6 +1739,7 @@ describe("writeToFileTool", () => {
16771739
getState: vi.fn().mockRejectedValue(new Error("provider state unavailable")),
16781740
})
16791741
mockCline.diffViewProvider.isEditing = true
1742+
mockCline.diffViewProvider.removeAdoptedDirectories.mockClear()
16801743
mockCline.diffViewProvider.discardUnapprovedStream.mockRejectedValue(
16811744
new Error("EPERM: operation not permitted"),
16821745
)
@@ -1693,6 +1756,10 @@ describe("writeToFileTool", () => {
16931756
"error",
16941757
expect.stringContaining("EPERM: operation not permitted"),
16951758
)
1759+
// The discard failed while a session was editing, so the directories are still the provider's
1760+
// to remove: the setup-window release (WriteToFileTool.ts:784) is guarded by :170-172 and must
1761+
// not clear the list that discard depended on.
1762+
expect(mockCline.diffViewProvider.removeAdoptedDirectories).not.toHaveBeenCalled()
16961763
const reported = mockHandleError.mock.calls.find(
16971764
([context]) => context === "handling partial write_to_file",
16981765
)?.[1] as Error
@@ -1716,6 +1783,7 @@ describe("writeToFileTool", () => {
17161783
new Error("EPERM: operation not permitted"),
17171784
)
17181785
mockCline.say.mockRejectedValue(new Error("task aborted"))
1786+
mockCline.diffViewProvider.removeAdoptedDirectories.mockClear()
17191787
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {})
17201788

17211789
try {
@@ -1729,6 +1797,9 @@ describe("writeToFileTool", () => {
17291797
"error",
17301798
expect.stringContaining("could not discard the unapproved preview after the failed stream"),
17311799
)
1800+
// Same rule with the report itself undeliverable: the setup-window release at :784 stays a
1801+
// no-op while the session owns the directories.
1802+
expect(mockCline.diffViewProvider.removeAdoptedDirectories).not.toHaveBeenCalled()
17321803
} finally {
17331804
errorSpy.mockRestore()
17341805
mockCline.say.mockResolvedValue(undefined)

0 commit comments

Comments
 (0)