Repository navigation
feat(file-safety): guard machinery in the writer, with the identity and rights refusals (U5-B, split 1/2 of #1914, #1375) #2357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d52c161
725b8a3
a1d9d24
b51aa56
cefc893
59550e9
01b41f9
6809e11
1998619
7c2a7bd
839c6cf
1f0d7a4
8ae1974
aae67b0
549899c
e1d6b6c
80f1dc3
b11c6cc
837f92a
e9ab519
d7baa2c
8ed2f4c
d0bc9b7
f1090c1
fbd87ac
2013167
a8ea1b4
79c25fe
e38258a
e0f2186
3fd1af2
4a20286
cb969ed
ee0317d
f1e50ad
c918c93
3638716
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,7 @@ import { t } from "../../i18n" | |
|
|
||
| import { defaultModeSlug, getModeBySlug } from "../../shared/modes" | ||
| import type { AutoApprovalContext, ToolResponse, ToolUse, McpToolUse } from "../../shared/tools" | ||
| import type { ToolCallbacks } from "../tools/BaseTool" | ||
|
|
||
| import { buildAutoDenyReason } from "../auto-approval" | ||
|
|
||
|
|
@@ -71,6 +72,51 @@ export function toTelemetryToolName( | |
| return "invalid_tool_call" | ||
| } | ||
|
|
||
| /** | ||
| * The one tool instance the parse-failure teardown can act on for a dropped call. | ||
| * | ||
| * write_to_file is the only tool that overrides onParameterParseFailure(); every other tool | ||
| * inherits the base no-op that reports nothing and tears nothing down, so resolving those | ||
| * names here would only add a call with no effect. Unknown, MCP, and custom-tool names | ||
| * resolve to no tool. | ||
| */ | ||
| function parseFailureTeardownTool(toolName: string) { | ||
| return toolName === "write_to_file" ? writeToFileTool : undefined | ||
| } | ||
|
|
||
| /** | ||
| * Run the parse-failure teardown for a known tool whose call never reached handle(). | ||
| * | ||
| * The dispatch-scope callbacks do not exist at the point where the call is dropped, so this | ||
| * builds the ToolCallbacks shape the teardown expects and points both report paths at the | ||
| * caller instead of pushing a second payload: the caller keeps owning the single tool_result | ||
| * the dropped call must carry, and the returned text tells it which error to put in it. The | ||
| * write_to_file override reports through handleError; pushToolResult records a string result | ||
| * the same way so a later override cannot have its report dropped on the floor. | ||
| * | ||
| * @returns the error text the teardown reported, or undefined when it reported nothing | ||
| */ | ||
| async function reportedErrorFromParseFailureTeardown( | ||
| cline: Task, | ||
| toolCallId: string, | ||
| toolName: string, | ||
| errorMessage: string, | ||
| ): Promise<string | undefined> { | ||
| const tool = parseFailureTeardownTool(toolName) | ||
| if (!tool) { | ||
| return undefined | ||
| } | ||
| let reported: string | undefined | ||
| const callbacks: ToolCallbacks = { | ||
| askApproval: async () => false, | ||
| handleError: async (_action: string, error: Error) => void (reported = error.message), | ||
| pushToolResult: (content: ToolResponse) => (reported ??= typeof content === "string" ? content : undefined), | ||
| toolCallId, | ||
| } | ||
| await tool.onParameterParseFailure(cline, callbacks, new Error(errorMessage)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Finalize the partial ask before invoking the parse-failure hook. After successful partial streaming, a final call without Call and await
|
||
| return reported | ||
| } | ||
|
|
||
| /** | ||
| * Processes and presents assistant message content to the user interface. | ||
| * | ||
|
|
@@ -577,12 +623,26 @@ async function presentAssistantMessageBlock(cline: Task, policySnapshot: Request | |
| // Best-effort only | ||
| } | ||
|
|
||
| // The call is dropped before dispatch, so the tool never sees it: the parse-failure | ||
| // boundary in BaseTool - write_to_file's only teardown for the partial ask, the abort | ||
| // listener, the streaming failure mark, and any diff preview a delta already opened - | ||
| // would not run either. Route the failure through that same boundary rather than | ||
| // writing a parallel cleanup here. When the teardown already surfaced the streaming | ||
| // error a delta captured, that is the error the user can act on, so it replaces the | ||
| // generic missing-nativeArgs text in the one tool_result below instead of being pushed | ||
| // as a second error for the same tool_use_id. | ||
| const teardownReport = await reportedErrorFromParseFailureTeardown( | ||
| cline, | ||
| toolCallId, | ||
| block.name, | ||
| errorMessage, | ||
| ) | ||
| // Push tool_result directly without setting didAlreadyUseTool so streaming can | ||
| // continue gracefully. | ||
| cline.pushToolResultToUserContent({ | ||
| type: "tool_result", | ||
| tool_use_id: sanitizeToolUseId(toolCallId), | ||
| content: formatResponse.toolError(errorMessage), | ||
| content: formatResponse.toolError(teardownReport ?? errorMessage), | ||
| is_error: true, | ||
| }) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,7 +46,13 @@ export async function readFileWithMissingRetry(filePath: string): Promise<string | |
| return await fs.readFile(filePath, "utf8") | ||
| } catch (retryError) { | ||
| if (getErrorCode(retryError) !== "ENOENT") throw retryError | ||
| if (!(await recoverMissingJsonFile(filePath))) throw retryError | ||
| // A recovery that rejects failed for its own reasons - a readdir error, a lock it could | ||
| // not take - and jsonFileRecovery traces that failure with its code before throwing. | ||
| // It is not the reader's failure: the caller can only classify the read it asked for, | ||
| // so an unreachable recovery counts as "nothing recovered" and the ENOENT read above | ||
| // is what the caller receives. | ||
| const recovered = await recoverMissingJsonFile(filePath).catch(() => false) | ||
| if (!recovered) throw retryError | ||
|
Comment on lines
+54
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return fs.readFile(filePath, "utf8") | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exercise registration before asserting listener identity.
The fixture inserts
abortCleanupdirectly into the map, sotask.oncenever runs. This test still passes if production registers a different callback.Create the state through the real
getTaskPartialStreamStatemethod. Assert thattask.onceandtask.offreceive the same storedabortCleanupreference. Keep the disposal-order assertion.As per path instructions, “For listener registration and removal, assert the same function reference was added and removed.”
Source: Path instructions