Repository navigation
fix(write-to-file): clean partial state on missing-param and rooignore denial + integrate the #1066 split series (6/6 + FINAL) #1928
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
Open
easonLiangWorldedtech
wants to merge
2
commits into
Zoo-Code-Org:main
Choose a base branch
from
easonLiangWorldedtech:p1066/u7-early-return-denial-cleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
src/core/assistant-message/__tests__/presentAssistantMessage-missing-native-args.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| // npx vitest src/core/assistant-message/__tests__/presentAssistantMessage-missing-native-args.spec.ts | ||
|
|
||
| import { describe, it, expect, beforeEach, vi } from "vitest" | ||
| import { presentAssistantMessage } from "../presentAssistantMessage" | ||
| import { DispatchState } from "../../task/dispatchState" | ||
| import { type RequestPolicySnapshot } from "../../prompts/tools/effective-tool-policy" | ||
| import { isValidToolName, validateToolUse } from "../../tools/validateToolUse" | ||
|
|
||
| // Tool policy is frozen at request build and the presenter refuses to run without the | ||
| // snapshot; these tests only exercise the presenter's bypass exits, so the empty policy is | ||
| // enough (validateToolUse itself is mocked). | ||
| const baseSnapshot: RequestPolicySnapshot = { | ||
| disabledTools: [], | ||
| customModes: [], | ||
| } | ||
|
|
||
| const mockTeardown = vi.hoisted(() => vi.fn()) | ||
| const mockWriteHandle = vi.hoisted(() => vi.fn()) | ||
|
|
||
| vi.mock("../../task/Task") | ||
| vi.mock("../../tools/validateToolUse", () => ({ | ||
| validateToolUse: vi.fn(), | ||
| isValidToolName: vi.fn(() => true), | ||
| })) | ||
| vi.mock("../../tools/WriteToFileTool", () => ({ | ||
| writeToFileTool: { handle: mockWriteHandle, teardownAbandonedStream: mockTeardown }, | ||
| })) | ||
| vi.mock("@roo-code/telemetry", () => ({ | ||
| TelemetryService: { | ||
| instance: { | ||
| captureToolUsage: vi.fn(), | ||
| captureConsecutiveMistakeError: vi.fn(), | ||
| captureException: vi.fn(), | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| type ToolResultBlock = { type: string; tool_use_id: string; content: string; is_error: boolean } | ||
|
|
||
| describe("presentAssistantMessage - finalized block without nativeArgs", () => { | ||
| // The presenter reads only a subset of Task. A Record keeps the double honest without | ||
| // an `any`; the single cast at the call site is the documented boundary. | ||
| let mockTask: Record<string, unknown> | ||
| let userMessageContent: ToolResultBlock[] | ||
|
|
||
| beforeEach(() => { | ||
| mockTeardown.mockReset() | ||
| mockWriteHandle.mockReset() | ||
| userMessageContent = [] | ||
| mockTask = { | ||
| taskId: "test-task-id", | ||
| instanceId: "test-instance", | ||
| abort: false, | ||
| // main renamed the dispatcher fields (src/core/task/dispatchState.ts): the presenter reads | ||
| // dispatchState and pendingWork, not the old lock/flag pair. | ||
| dispatchState: DispatchState.IDLE, | ||
| pendingWork: null, | ||
| currentStreamingContentIndex: 0, | ||
| assistantMessageContent: [ | ||
| { | ||
| type: "tool_use", | ||
| name: "write_to_file", | ||
| params: {}, | ||
| partial: false, | ||
| // Streaming JSON never parsed: Task completes the block with no nativeArgs. | ||
| id: "toolu_1", | ||
| nativeArgs: undefined, | ||
| }, | ||
| ], | ||
| userMessageContent, | ||
| didCompleteReadingStream: false, | ||
| // The presenter keeps per-tool-call context in this map (added on main since this | ||
| // spec was written); the bypass exits below run after its first use. | ||
| activeToolContexts: new Map(), | ||
| didRejectTool: false, | ||
| didAlreadyUseTool: false, | ||
| consecutiveMistakeCount: 0, | ||
| consecutiveMistakeLimit: 3, | ||
| apiConfiguration: { apiProvider: "test-provider" }, | ||
| clineMessages: [], | ||
| getTaskMode: vi.fn().mockResolvedValue("code"), | ||
| api: { getModel: () => ({ id: "test-model", info: {} }) }, | ||
| recordToolUsage: vi.fn(), | ||
| recordToolError: vi.fn(), | ||
| toolRepetitionDetector: { check: vi.fn().mockReturnValue({ allowExecution: true }) }, | ||
| providerRef: { | ||
| deref: () => ({ getState: vi.fn().mockResolvedValue({ mode: "code", customModes: [] }) }), | ||
| }, | ||
| say: vi.fn().mockResolvedValue(undefined), | ||
| ask: vi.fn().mockResolvedValue({ response: "yesButtonClicked" }), | ||
| pushToolResultToUserContent: vi.fn((toolResult: ToolResultBlock) => { | ||
| userMessageContent.push(toolResult) | ||
| return true | ||
| }), | ||
| } | ||
| }) | ||
|
|
||
| it("tears the write_to_file stream state down instead of dispatching the tool", async () => { | ||
| await presentAssistantMessage( | ||
| mockTask as unknown as Parameters<typeof presentAssistantMessage>[0], | ||
| baseSnapshot, | ||
| ) | ||
|
|
||
| // The malformed call must not be executed, and exactly one error tool_result is | ||
| // emitted for the provider. | ||
| expect(isValidToolName).toHaveBeenCalled() | ||
| expect(mockWriteHandle).not.toHaveBeenCalled() | ||
| expect(mockTask.pushToolResultToUserContent).toHaveBeenCalledTimes(1) | ||
| expect(userMessageContent).toEqual([ | ||
| expect.objectContaining({ | ||
| type: "tool_result", | ||
| tool_use_id: "toolu_1", | ||
| is_error: true, | ||
| content: expect.stringContaining("missing nativeArgs"), | ||
| }), | ||
| ]) | ||
| // The guard bypasses handle(), so the per-task stream state has to be released here: | ||
| // otherwise the entry, its TaskAborted listener and any streamed diff view leak into | ||
| // the next API request of the same task. | ||
| expect(mockTeardown).toHaveBeenCalledTimes(1) | ||
| expect(mockTeardown).toHaveBeenCalledWith(mockTask) | ||
| }) | ||
|
|
||
| it("tears the stream state down when tool validation rejects the call", async () => { | ||
| // A mode file restriction (or any validateToolUse failure) lands here. The block | ||
| // streamed partial deltas - handlePartial ran - and this exit bypasses handle(), so | ||
| // without the teardown the per-task stream state, its TaskAborted listener and any | ||
| // streamed diff view leak into the next API request. | ||
| mockTask.assistantMessageContent = [ | ||
| { | ||
| type: "tool_use", | ||
| name: "write_to_file", | ||
| params: { path: "restricted.ts", content: "partial model output" }, | ||
| partial: false, | ||
| id: "toolu_2", | ||
| nativeArgs: { path: "restricted.ts", content: "partial model output" }, | ||
| }, | ||
| ] | ||
| vi.mocked(validateToolUse).mockImplementationOnce(() => { | ||
| throw new Error("write_to_file is not allowed to write restricted.ts in this mode") | ||
| }) | ||
|
|
||
| await presentAssistantMessage( | ||
| mockTask as unknown as Parameters<typeof presentAssistantMessage>[0], | ||
| baseSnapshot, | ||
| ) | ||
|
|
||
| expect(mockWriteHandle).not.toHaveBeenCalled() | ||
| expect(userMessageContent).toEqual([ | ||
| expect.objectContaining({ | ||
| type: "tool_result", | ||
| tool_use_id: "toolu_2", | ||
| is_error: true, | ||
| content: expect.stringContaining("restricted.ts"), | ||
| }), | ||
| ]) | ||
| expect(mockTeardown).toHaveBeenCalledTimes(1) | ||
| expect(mockTeardown).toHaveBeenCalledWith(mockTask) | ||
| }) | ||
|
|
||
| it("tears the stream state down when the tool repetition limit stops the call", async () => { | ||
| mockTask.assistantMessageContent = [ | ||
| { | ||
| type: "tool_use", | ||
| name: "write_to_file", | ||
| params: { path: "same.ts", content: "same content" }, | ||
| partial: false, | ||
| id: "toolu_3", | ||
| nativeArgs: { path: "same.ts", content: "same content" }, | ||
| }, | ||
| ] | ||
| // The detector's result is a tagged union now (allow / soft_block / hard_block): only the | ||
| // hard block asks the user and stops the call, so that is the exit under test. | ||
| vi.mocked(mockTask.toolRepetitionDetector as unknown as { check: unknown }).check = vi.fn().mockReturnValue({ | ||
| action: "hard_block", | ||
| askUser: { messageKey: "tool_repetition", messageDetail: "write_to_file" }, | ||
| }) | ||
|
|
||
| await presentAssistantMessage( | ||
| mockTask as unknown as Parameters<typeof presentAssistantMessage>[0], | ||
| baseSnapshot, | ||
| ) | ||
|
|
||
| expect(mockWriteHandle).not.toHaveBeenCalled() | ||
| // The repetition exit reports through the local pushToolResult, which wraps the | ||
| // message in the tool-error envelope rather than the provider is_error flag. | ||
| expect(userMessageContent).toEqual([ | ||
| expect.objectContaining({ | ||
| type: "tool_result", | ||
| tool_use_id: "toolu_3", | ||
| content: expect.stringContaining("repetition limit reached"), | ||
| }), | ||
| ]) | ||
| expect(mockTeardown).toHaveBeenCalledTimes(1) | ||
| expect(mockTeardown).toHaveBeenCalledWith(mockTask) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.