Skip to content

Commit 753c22f

Browse files
author
easonLiangWorldedtech
committed
fix(tools): report the captured stream failure on the parse-failure path
The unit captured streamError for exactly one reason - when the finalized block never reaches execute(), the authoritative filesystem retry in execute() never happens, and the captured error is the only failure the user can act on. Nothing read it: the hook released the state but BaseTool still reported the incidental parse error, so the capture was dead and the real failure was hidden behind a parse message. clearTaskStreamState becomes releaseStreamStateOnParseFailure(task, callbacks): it releases THIS task's state, restores the diff document the stream opened (revert before reset, as the other teardowns do), and when a streamError was captured reports it with the same 'writing file' context execute()'s catch uses, returning true so BaseTool suppresses the generic parse error. The failure therefore surfaces exactly once. With no captured error the generic parse error is still reported, and the default hook for other tools keeps returning false. execute()'s early returns (missing path, missing content, rooignore denial) returned before the success/catch cleanup, leaving the per-task state and its TaskAborted listener behind for the task's lifetime - and a retained streamFailed suppresses the diff preview of every later write_to_file in that task. They now release only this task's state. Tests: 'reports the captured streaming failure once instead of the incidental parse error' (handleError called once with the captured error, not with the parse error, state released, revertChanges + reset ran) and 'releases the per-task stream state when execute() returns early on a denied path'. Pins: returning false instead of reporting fails the first; removing the release in the rooignore branch fails the second. Local: core/tools + writeToFileTool.spec + assistant-message + core/task = 1420 passed / 5 skipped; tsc 0; eslint 0 err / 0 warn on all three files.
1 parent 7752664 commit 753c22f

3 files changed

Lines changed: 102 additions & 20 deletions

File tree

‎src/core/tools/BaseTool.ts‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,21 @@ export abstract class BaseTool<TName extends ToolName> {
103103
* execute(). No-op for tools without per-task state; the scope is a single task
104104
* because tool instances are singletons shared by concurrent tasks.
105105
*/
106-
protected clearTaskStreamState(_task: Task): void {}
106+
/**
107+
* Teardown boundary for the handle() parse-failure path, where execute() never
108+
* runs. Default: there is no per-task streaming state to release, so the generic
109+
* parse error is what the user sees. A tool that keeps per-task stream state may
110+
* release it, restore any diff document a stream opened, and report a more specific
111+
* failure - returning true suppresses the incidental parse error so the failure is
112+
* reported exactly once. Per-task only: a global teardown would clobber another task
113+
* that is still streaming through this singleton.
114+
*/
115+
protected async releaseStreamStateOnParseFailure(
116+
_task: Task,
117+
_callbacks: ToolCallbacks,
118+
): Promise<boolean> {
119+
return false
120+
}
107121

108122
/**
109123
* Main entry point for tool execution.
@@ -164,13 +178,14 @@ export abstract class BaseTool<TName extends ToolName> {
164178
} catch (error) {
165179
console.error(`Error parsing parameters:`, error)
166180
const errorMessage = `Failed to parse ${this.name} parameters: ${error instanceof Error ? error.message : String(error)}`
167-
await callbacks.handleError(`parsing ${this.name} args`, new Error(errorMessage))
168181
// execute() never runs on this path, so a tool that keeps per-task streaming
169-
// state must still release THIS task's state; a completed block that failed
170-
// finalization would otherwise leave a failure flag suppressing later previews
171-
// for the same task. Per-task only: a global teardown would clobber another
172-
// task that is still streaming through this singleton.
173-
this.clearTaskStreamState(task)
182+
// state must still release THIS task's state and restore any diff document the
183+
// stream opened. If a streaming delta already hit a fatal error, the tool reports
184+
// that (the actionable failure) and the incidental parse error is suppressed.
185+
const reportedStreamFailure = await this.releaseStreamStateOnParseFailure(task, callbacks)
186+
if (!reportedStreamFailure) {
187+
await callbacks.handleError(`parsing ${this.name} args`, new Error(errorMessage))
188+
}
174189
// Note: handleError already emits a tool_result via formatResponse.toolError in the caller.
175190
// Do NOT call pushToolResult here to avoid duplicate tool_result payloads.
176191
return

‎src/core/tools/WriteToFileTool.ts‎

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,22 +160,38 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
160160
}
161161

162162
/**
163-
* Teardown boundary for the handle() parse-failure path, where execute() never
164-
* runs and therefore its finally (resetTaskPartialState) never runs either.
163+
* Teardown for the handle() parse-failure path, where execute() never runs and its
164+
* cleanup never runs either.
165165
*
166-
* Tears down the per-task stream state: otherwise the abort listener leaks for
167-
* the task's lifetime, and when a streaming delta had failed, the streamFailed
168-
* guard would suppress the diff preview of every later write_to_file in this
169-
* task. Restores the diff document: streaming may have opened it with
170-
* unapproved partial content, and execute()'s error cleanup (revert + reset)
171-
* never fires on this path, so a user save could persist the content without
172-
* the teardown here. When a streaming delta already hit a fatal filesystem
173-
* error, that error is what the user can act on, so report it with the same
174-
* "writing file" context execute()'s catch uses, and suppress the incidental
175-
* parse error.
166+
* Releases the per-task stream state: otherwise the abort listener leaks for the task's
167+
* lifetime, and a failed streaming delta leaves the streamFailed guard suppressing the
168+
* diff preview of every later write_to_file in this task. Restores the diff document:
169+
* streaming may have opened it with unapproved partial content, and execute()'s error
170+
* cleanup (revert + reset) never fires here, so a user save could persist it. When a
171+
* streaming delta already hit a fatal filesystem error, THAT is the failure the user can
172+
* act on, so it is reported with the same "writing file" context execute()'s catch uses,
173+
* and true is returned to suppress the incidental parse error - the failure surfaces
174+
* exactly once.
176175
*/
177-
protected override clearTaskStreamState(task: Task): void {
176+
protected override async releaseStreamStateOnParseFailure(
177+
task: Task,
178+
callbacks: ToolCallbacks,
179+
): Promise<boolean> {
180+
const state = this.taskPartialStreamState.get(this.getPartialStreamFailureKey(task))
181+
if (!state) {
182+
return false
183+
}
184+
178185
this.resetTaskPartialState(task)
186+
await this.revertDiffChangesBeforeReset(task)
187+
await this.resetDiffViewAfterWrite(task)
188+
189+
if (!state.streamError) {
190+
return false
191+
}
192+
193+
await callbacks.handleError("writing file", state.streamError)
194+
return true
179195
}
180196

181197
override resetPartialState(): void {
@@ -197,6 +213,10 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
197213
if (!relPath) {
198214
task.consecutiveMistakeCount++
199215
task.recordToolError("write_to_file")
216+
// No execute() cleanup on this early return: release THIS task's stream state
217+
// (and only this task's) so the abort listener and the streamFailed guard do not
218+
// outlive the call.
219+
this.resetTaskPartialState(task)
200220
pushToolResult(await task.sayAndCreateMissingParamError("write_to_file", "path"))
201221
await task.diffViewProvider.reset()
202222
return
@@ -205,6 +225,10 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
205225
if (newContent === undefined) {
206226
task.consecutiveMistakeCount++
207227
task.recordToolError("write_to_file")
228+
// No execute() cleanup on this early return: release THIS task's stream state
229+
// (and only this task's) so the abort listener and the streamFailed guard do not
230+
// outlive the call.
231+
this.resetTaskPartialState(task)
208232
pushToolResult(await task.sayAndCreateMissingParamError("write_to_file", "content"))
209233
await task.diffViewProvider.reset()
210234
return
@@ -214,6 +238,7 @@ export class WriteToFileTool extends BaseTool<"write_to_file"> {
214238

215239
if (!accessAllowed) {
216240
await task.say("rooignore_error", relPath)
241+
this.resetTaskPartialState(task)
217242
pushToolResult(formatResponse.rooIgnoreError(relPath))
218243
return
219244
}

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,48 @@ describe("writeToFileTool", () => {
610610
await executeWriteFileTool({}, { isPartial: true })
611611
expect(mockCline.ask).toHaveBeenCalledTimes(1)
612612
})
613+
it("reports the captured streaming failure once instead of the incidental parse error", async () => {
614+
// A streaming delta hit a fatal filesystem error and the finalized block then
615+
// arrives without nativeArgs: execute() never runs, so its authoritative retry of
616+
// the same filesystem operation never happens either. The captured error is the one
617+
// the user can act on, and it must surface exactly once.
618+
const state = writeToFileTool["getTaskPartialStreamState"](mockCline as never)
619+
state.streamFailed = true
620+
const streamFailure = new Error("EACCES: stream open failed")
621+
state.streamError = streamFailure
622+
623+
const block = {
624+
type: "tool_use",
625+
name: "write_to_file",
626+
params: {},
627+
partial: false,
628+
} as ToolUse<"write_to_file">
629+
await writeToFileTool.handle(mockCline, block, {
630+
askApproval: mockAskApproval,
631+
handleError: mockHandleError,
632+
pushToolResult: mockPushToolResult,
633+
})
634+
635+
expect(mockHandleError).toHaveBeenCalledTimes(1)
636+
expect(mockHandleError).toHaveBeenCalledWith("writing file", streamFailure)
637+
expect(mockHandleError).not.toHaveBeenCalledWith("parsing write_to_file args", expect.any(Error))
638+
expect(writeToFileTool["taskPartialStreamState"].size).toBe(0)
639+
// The stream may have left a diff view open with content that was never approved.
640+
expect(mockCline.diffViewProvider.revertChanges).toHaveBeenCalled()
641+
expect(mockCline.diffViewProvider.reset).toHaveBeenCalled()
642+
})
643+
644+
it("releases the per-task stream state when execute() returns early on a denied path", async () => {
645+
// The rooignore branch returns before execute()'s success/catch cleanup; without
646+
// this the abort listener and the streamFailed guard outlive the call and suppress
647+
// the diff preview of every later write_to_file in this task.
648+
writeToFileTool["getTaskPartialStreamState"](mockCline as never).streamFailed = true
649+
650+
await executeWriteFileTool({}, { accessAllowed: false })
651+
652+
expect(writeToFileTool["taskPartialStreamState"].size).toBe(0)
653+
expect(mockCline.off).toHaveBeenCalledWith(RooCodeEventName.TaskAborted, expect.any(Function))
654+
})
613655
})
614656

615657
describe("per-task stream state isolation", () => {

0 commit comments

Comments
 (0)