forked from cline/cline
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: prevent UI flicker and enable resumption after task cancellation #8986
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
638ca0e
fix: prevent UI flicker and enable resumption after task cancellation
daniel-lxs f37deb4
fix: replace JSON.parse with safeJsonParse for improved error handling
daniel-lxs 4ef7968
fix: correct isStreaming logic when cost is defined
daniel-lxs ac57bd0
fix: properly handle streaming state when api_req_started has no cost
daniel-lxs 1520618
refactor: centralize abort/streaming state reset logic and use safe J…
daniel-lxs 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
146 changes: 146 additions & 0 deletions
146
src/core/task/__tests__/Task.presentResumableAsk.abort-reset.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,146 @@ | ||
| import { describe, it, expect, vi, beforeEach, afterEach } from "vitest" | ||
| import { ProviderSettings } from "@roo-code/types" | ||
|
|
||
| import { Task } from "../Task" | ||
| import { ClineProvider } from "../../webview/ClineProvider" | ||
|
|
||
| // Mocks similar to Task.dispose.test.ts | ||
| vi.mock("../../webview/ClineProvider") | ||
| vi.mock("../../../integrations/terminal/TerminalRegistry", () => ({ | ||
| TerminalRegistry: { | ||
| releaseTerminalsForTask: vi.fn(), | ||
| }, | ||
| })) | ||
| vi.mock("../../ignore/RooIgnoreController") | ||
| vi.mock("../../protect/RooProtectedController") | ||
| vi.mock("../../context-tracking/FileContextTracker") | ||
| vi.mock("../../../services/browser/UrlContentFetcher") | ||
| vi.mock("../../../services/browser/BrowserSession") | ||
| vi.mock("../../../integrations/editor/DiffViewProvider") | ||
| vi.mock("../../tools/ToolRepetitionDetector") | ||
| vi.mock("../../../api", () => ({ | ||
| buildApiHandler: vi.fn(() => ({ | ||
| getModel: () => ({ info: {}, id: "test-model" }), | ||
| })), | ||
| })) | ||
| vi.mock("../AutoApprovalHandler") | ||
|
|
||
| // Mock TelemetryService | ||
| vi.mock("@roo-code/telemetry", () => ({ | ||
| TelemetryService: { | ||
| instance: { | ||
| captureTaskCreated: vi.fn(), | ||
| captureTaskRestarted: vi.fn(), | ||
| captureConversationMessage: vi.fn(), | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| describe("Task.presentResumableAsk abort reset", () => { | ||
| let mockProvider: any | ||
| let mockApiConfiguration: ProviderSettings | ||
| let task: Task | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
|
|
||
| mockProvider = { | ||
| context: { | ||
| globalStorageUri: { fsPath: "/test/path" }, | ||
| }, | ||
| getState: vi.fn().mockResolvedValue({ mode: "code" }), | ||
| postStateToWebview: vi.fn().mockResolvedValue(undefined), | ||
| postMessageToWebview: vi.fn().mockResolvedValue(undefined), | ||
| updateTaskHistory: vi.fn().mockResolvedValue(undefined), | ||
| log: vi.fn(), | ||
| } | ||
|
|
||
| mockApiConfiguration = { | ||
| apiProvider: "anthropic", | ||
| apiKey: "test-key", | ||
| } as ProviderSettings | ||
|
|
||
| task = new Task({ | ||
| provider: mockProvider as ClineProvider, | ||
| apiConfiguration: mockApiConfiguration, | ||
| startTask: false, | ||
| }) | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| // Ensure we don't leave event listeners dangling | ||
| task.dispose() | ||
| }) | ||
|
|
||
| it("resets abort flags and continues the loop on yesButtonClicked", async () => { | ||
| // Arrange aborted state | ||
| task.abort = true | ||
| task.abortReason = "user_cancelled" | ||
| task.didFinishAbortingStream = true | ||
| task.isStreaming = true | ||
|
|
||
| // minimal message history | ||
| task.clineMessages = [{ ts: Date.now() - 1000, type: "say", say: "text", text: "prev" } as any] | ||
|
|
||
| // Spy and stub ask + loop | ||
| const askSpy = vi.spyOn(task as any, "ask").mockResolvedValue({ response: "yesButtonClicked" }) | ||
| const loopSpy = vi.spyOn(task as any, "initiateTaskLoop").mockResolvedValue(undefined) | ||
|
|
||
| // Act | ||
| await task.presentResumableAsk() | ||
|
|
||
| // Assert ask was presented | ||
| expect(askSpy).toHaveBeenCalled() | ||
|
|
||
| // Abort flags cleared | ||
| expect(task.abort).toBe(false) | ||
| expect(task.abandoned).toBe(false) | ||
| expect(task.abortReason).toBeUndefined() | ||
| expect(task.didFinishAbortingStream).toBe(false) | ||
| expect(task.isStreaming).toBe(false) | ||
|
|
||
| // Streaming-local state cleared | ||
| expect(task.currentStreamingContentIndex).toBe(0) | ||
| expect(task.assistantMessageContent).toEqual([]) | ||
| expect(task.userMessageContentReady).toBe(false) | ||
| expect(task.didRejectTool).toBe(false) | ||
| expect(task.presentAssistantMessageLocked).toBe(false) | ||
|
|
||
| // Loop resumed | ||
| expect(loopSpy).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it("includes user feedback when resuming with messageResponse", async () => { | ||
| task.abort = true | ||
| task.clineMessages = [{ ts: Date.now() - 1000, type: "say", say: "text", text: "prev" } as any] | ||
|
|
||
| const askSpy = vi | ||
| .spyOn(task as any, "ask") | ||
| .mockResolvedValue({ response: "messageResponse", text: "Continue with this", images: undefined }) | ||
| const saySpy = vi.spyOn(task, "say").mockResolvedValue(undefined as any) | ||
| const loopSpy = vi.spyOn(task as any, "initiateTaskLoop").mockResolvedValue(undefined) | ||
|
|
||
| await task.presentResumableAsk() | ||
|
|
||
| expect(askSpy).toHaveBeenCalled() | ||
| expect(saySpy).toHaveBeenCalledWith("user_feedback", "Continue with this", undefined) | ||
| expect(loopSpy).toHaveBeenCalledTimes(1) | ||
| }) | ||
|
|
||
| it("does nothing when user clicks Terminate (noButtonClicked)", async () => { | ||
| task.abort = true | ||
| task.abortReason = "user_cancelled" | ||
| task.clineMessages = [{ ts: Date.now() - 1000, type: "say", say: "text", text: "prev" } as any] | ||
|
|
||
| vi.spyOn(task as any, "ask").mockResolvedValue({ response: "noButtonClicked" }) | ||
| const loopSpy = vi.spyOn(task as any, "initiateTaskLoop").mockResolvedValue(undefined) | ||
|
|
||
| await task.presentResumableAsk() | ||
|
|
||
| // Still aborted | ||
| expect(task.abort).toBe(true) | ||
| expect(task.abortReason).toBe("user_cancelled") | ||
| // No loop resume | ||
| expect(loopSpy).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
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.
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.