-
Notifications
You must be signed in to change notification settings - Fork 281
fix(history): prompt on workspace mismatch #1660
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
PierrunoYT
wants to merge
5
commits into
Zoo-Code-Org:main
Choose a base branch
from
PierrunoYT:fix/1602-history-workspace-mismatch
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.
+371
−3
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eb83c61
fix(history): prompt on workspace mismatch
ampagent 113996b
fix(history): make workspace reset recoverable
PierrunoYT 0964f34
Merge branch 'main' into fix/1602-history-workspace-mismatch
PierrunoYT 5a28429
Merge branch 'main' into fix/1602-history-workspace-mismatch
PierrunoYT 2e7054d
fix(webview): isolate checkpoint cleanup failures
PierrunoYT 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
239 changes: 239 additions & 0 deletions
239
src/core/webview/__tests__/ClineProvider.history-workspace.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,239 @@ | ||
| import fs from "fs/promises" | ||
| import os from "os" | ||
| import path from "path" | ||
| import * as vscode from "vscode" | ||
|
|
||
| import type { HistoryItem } from "@roo-code/types" | ||
|
|
||
| import { ClineProvider } from "../ClineProvider" | ||
|
|
||
| const historyItem = (workspace: string): HistoryItem => ({ | ||
| id: "task-1602", | ||
| number: 1, | ||
| ts: 1, | ||
| task: "Continue in another worktree", | ||
| tokensIn: 0, | ||
| tokensOut: 0, | ||
| cacheWrites: 0, | ||
| cacheReads: 0, | ||
| totalCost: 0, | ||
| workspace, | ||
| }) | ||
|
|
||
| const createProvider = (workspace: string) => { | ||
| const provider = Object.create(ClineProvider.prototype) as ClineProvider | ||
| Object.defineProperty(provider, "currentWorkspacePath", { value: workspace, writable: true }) | ||
| return provider | ||
| } | ||
|
|
||
| describe("ClineProvider historical workspace selection", () => { | ||
| afterEach(() => { | ||
| vi.restoreAllMocks() | ||
| }) | ||
|
|
||
| it("keeps a history item unchanged when it already belongs to the current workspace", async () => { | ||
| const provider = createProvider("/current/workspace") | ||
| const prompt = vi.spyOn(vscode.window, "showWarningMessage") | ||
| const item = historyItem("/current/workspace") | ||
|
|
||
| await expect(provider.prepareHistoryItemForResume(item)).resolves.toBe(item) | ||
| expect(prompt).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("cancels restoration without mutating history when the mismatch prompt is dismissed", async () => { | ||
| const provider = createProvider("/current/workspace") | ||
| vi.spyOn(vscode.window, "showWarningMessage").mockResolvedValue(undefined) | ||
| provider["resetTaskCheckpointsForWorkspaceChange"] = vi.fn() | ||
| provider.updateTaskHistory = vi.fn() | ||
|
|
||
| await expect(provider.prepareHistoryItemForResume(historyItem("/old/worktree"))).resolves.toBeUndefined() | ||
| expect(provider["resetTaskCheckpointsForWorkspaceChange"]).not.toHaveBeenCalled() | ||
| expect(provider.updateTaskHistory).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("opens the original workspace in a new window without restoring the task", async () => { | ||
| const provider = createProvider("/current/workspace") | ||
| const prompt = vi | ||
| .spyOn(vscode.window, "showWarningMessage") | ||
| .mockResolvedValue({ title: "Open Original Workspace" }) | ||
| const executeCommand = vi.spyOn(vscode.commands, "executeCommand").mockResolvedValue(undefined) | ||
|
|
||
| await expect(provider.prepareHistoryItemForResume(historyItem("/old/worktree"))).resolves.toBeUndefined() | ||
| expect(prompt).toHaveBeenCalledWith( | ||
| expect.any(String), | ||
| { modal: true }, | ||
| { title: "Use Current Workspace" }, | ||
| { title: "Open Original Workspace" }, | ||
| ) | ||
| expect(executeCommand).toHaveBeenCalledWith( | ||
| "vscode.openFolder", | ||
| expect.objectContaining({ fsPath: "/old/worktree" }), | ||
| { forceNewWindow: true }, | ||
| ) | ||
| }) | ||
|
|
||
| it("moves the task to the current workspace and resets workspace-specific checkpoints", async () => { | ||
| const provider = createProvider("/current/workspace") | ||
| vi.spyOn(vscode.window, "showWarningMessage").mockResolvedValue({ title: "Use Current Workspace" }) | ||
| provider["resetTaskCheckpointsForWorkspaceChange"] = vi.fn().mockResolvedValue(undefined) | ||
| provider.updateTaskHistory = vi.fn().mockResolvedValue([]) | ||
| const item = historyItem("/old/worktree") | ||
|
|
||
| await expect(provider.prepareHistoryItemForResume(item)).resolves.toEqual({ | ||
| ...item, | ||
| workspace: "/current/workspace", | ||
| }) | ||
| expect(provider["resetTaskCheckpointsForWorkspaceChange"]).toHaveBeenCalledWith(item, { | ||
| ...item, | ||
| workspace: "/current/workspace", | ||
| }) | ||
| }) | ||
|
|
||
| it("restores history with the workspace selected by the mismatch prompt", async () => { | ||
| const provider = createProvider("/current/workspace") | ||
| const original = historyItem("/old/worktree") | ||
| const prepared = { ...original, workspace: "/current/workspace" } | ||
| provider.getCurrentTask = vi.fn().mockReturnValue(undefined) | ||
| provider.getTaskWithId = vi.fn().mockResolvedValue({ historyItem: original }) | ||
| provider.prepareHistoryItemForResume = vi.fn().mockResolvedValue(prepared) | ||
| provider.createTaskWithHistoryItem = vi.fn().mockResolvedValue({}) | ||
| provider.postMessageToWebview = vi.fn().mockResolvedValue(true) | ||
|
|
||
| await provider.showTaskWithId(original.id) | ||
|
|
||
| expect(provider.createTaskWithHistoryItem).toHaveBeenCalledWith(prepared) | ||
| expect(provider.postMessageToWebview).toHaveBeenCalledWith({ | ||
| type: "action", | ||
| action: "chatButtonClicked", | ||
| }) | ||
| }) | ||
|
|
||
| it("does not restore or reveal a task when workspace selection is cancelled", async () => { | ||
| const provider = createProvider("/current/workspace") | ||
| const original = historyItem("/old/worktree") | ||
| provider.getCurrentTask = vi.fn().mockReturnValue(undefined) | ||
| provider.getTaskWithId = vi.fn().mockResolvedValue({ historyItem: original }) | ||
| provider.prepareHistoryItemForResume = vi.fn().mockResolvedValue(undefined) | ||
| provider.createTaskWithHistoryItem = vi.fn() | ||
| provider.postMessageToWebview = vi.fn() | ||
|
|
||
| await provider.showTaskWithId(original.id) | ||
|
|
||
| expect(provider.createTaskWithHistoryItem).not.toHaveBeenCalled() | ||
| expect(provider.postMessageToWebview).not.toHaveBeenCalled() | ||
| }) | ||
|
|
||
| it("removes the old checkpoint repository and checkpoint-only chat rows", async () => { | ||
| const storagePath = await fs.mkdtemp(path.join(os.tmpdir(), "zoo-history-workspace-")) | ||
| const taskDir = path.join(storagePath, "tasks", "task-1602") | ||
| const checkpointsDir = path.join(taskDir, "checkpoints") | ||
| await fs.mkdir(checkpointsDir, { recursive: true }) | ||
| await fs.writeFile(path.join(checkpointsDir, "HEAD"), "old checkpoint") | ||
| await fs.writeFile( | ||
| path.join(taskDir, "ui_messages.json"), | ||
| JSON.stringify([ | ||
| { type: "say", say: "task", ts: 1, text: "Continue" }, | ||
| { type: "say", say: "checkpoint_saved", ts: 2, text: "old-hash" }, | ||
| { type: "say", say: "text", ts: 3, text: "Still useful" }, | ||
| ]), | ||
| ) | ||
|
|
||
| const provider = createProvider("/current/workspace") | ||
| Object.defineProperty(provider, "contextProxy", { | ||
| value: { globalStorageUri: { fsPath: storagePath } }, | ||
| }) | ||
| const original = historyItem("/old/worktree") | ||
| const updated = { ...original, workspace: "/current/workspace" } | ||
| provider.updateTaskHistory = vi.fn().mockResolvedValue([]) | ||
| Object.defineProperty(provider, "taskHistoryStore", { value: { get: vi.fn().mockReturnValue(original) } }) | ||
|
|
||
| try { | ||
| await provider["resetTaskCheckpointsForWorkspaceChange"](original, updated) | ||
|
|
||
| await expect(fs.stat(checkpointsDir)).rejects.toMatchObject({ code: "ENOENT" }) | ||
| const messages = JSON.parse(await fs.readFile(path.join(taskDir, "ui_messages.json"), "utf8")) | ||
| expect(messages).toMatchObject([ | ||
| { type: "say", say: "task", text: "Continue" }, | ||
| { type: "say", say: "text", text: "Still useful" }, | ||
| ]) | ||
| expect(provider.updateTaskHistory).toHaveBeenCalledWith(updated) | ||
| } finally { | ||
| await fs.rm(storagePath, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| it("restores checkpoint files and messages when workspace persistence fails", async () => { | ||
| const storagePath = await fs.mkdtemp(path.join(os.tmpdir(), "zoo-history-workspace-rollback-")) | ||
| const taskDir = path.join(storagePath, "tasks", "task-1602") | ||
| const checkpointsDir = path.join(taskDir, "checkpoints") | ||
| const messagesPath = path.join(taskDir, "ui_messages.json") | ||
| const originalMessages = [ | ||
| { type: "say", say: "task", ts: 1, text: "Continue" }, | ||
| { type: "say", say: "checkpoint_saved", ts: 2, text: "old-hash" }, | ||
| ] | ||
| await fs.mkdir(checkpointsDir, { recursive: true }) | ||
| await fs.writeFile(path.join(checkpointsDir, "HEAD"), "old checkpoint") | ||
| await fs.writeFile(messagesPath, JSON.stringify(originalMessages)) | ||
|
|
||
| const provider = createProvider("/current/workspace") | ||
| const original = historyItem("/old/worktree") | ||
| const updated = { ...original, workspace: "/current/workspace" } | ||
| Object.defineProperty(provider, "contextProxy", { | ||
| value: { globalStorageUri: { fsPath: storagePath } }, | ||
| }) | ||
| Object.defineProperty(provider, "taskHistoryStore", { value: { get: vi.fn().mockReturnValue(original) } }) | ||
| provider.updateTaskHistory = vi.fn().mockRejectedValue(new Error("history write failed")) | ||
|
|
||
| try { | ||
| await expect(provider["resetTaskCheckpointsForWorkspaceChange"](original, updated)).rejects.toThrow( | ||
| "history write failed", | ||
| ) | ||
| await expect(fs.readFile(path.join(checkpointsDir, "HEAD"), "utf8")).resolves.toBe("old checkpoint") | ||
| expect(JSON.parse(await fs.readFile(messagesPath, "utf8"))).toMatchObject(originalMessages) | ||
| } finally { | ||
| await fs.rm(storagePath, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
|
|
||
| it("keeps committed history when checkpoint backup cleanup fails", async () => { | ||
| const storagePath = await fs.mkdtemp(path.join(os.tmpdir(), "zoo-history-workspace-cleanup-")) | ||
| const taskDir = path.join(storagePath, "tasks", "task-1602") | ||
| const checkpointsDir = path.join(taskDir, "checkpoints") | ||
| const messagesPath = path.join(taskDir, "ui_messages.json") | ||
| await fs.mkdir(checkpointsDir, { recursive: true }) | ||
| await fs.writeFile(path.join(checkpointsDir, "HEAD"), "old checkpoint") | ||
| await fs.writeFile( | ||
| messagesPath, | ||
| JSON.stringify([ | ||
| { type: "say", say: "task", ts: 1, text: "Continue" }, | ||
| { type: "say", say: "checkpoint_saved", ts: 2, text: "old-hash" }, | ||
| ]), | ||
| ) | ||
|
|
||
| const provider = createProvider("/current/workspace") | ||
| const original = historyItem("/old/worktree") | ||
| const updated = { ...original, workspace: "/current/workspace" } | ||
| Object.defineProperty(provider, "contextProxy", { | ||
| value: { globalStorageUri: { fsPath: storagePath } }, | ||
| }) | ||
| Object.defineProperty(provider, "taskHistoryStore", { value: { get: vi.fn().mockReturnValue(updated) } }) | ||
| provider.updateTaskHistory = vi.fn().mockResolvedValue([]) | ||
| provider["log"] = vi.fn() | ||
| vi.spyOn(fs, "rm").mockRejectedValueOnce(new Error("backup cleanup failed")) | ||
|
|
||
| try { | ||
| await expect(provider["resetTaskCheckpointsForWorkspaceChange"](original, updated)).resolves.toBeUndefined() | ||
|
|
||
| expect(provider.updateTaskHistory).toHaveBeenCalledOnce() | ||
| expect(provider.updateTaskHistory).toHaveBeenCalledWith(updated) | ||
| expect(provider["log"]).toHaveBeenCalledWith(expect.stringContaining("backup cleanup failed")) | ||
| await expect(fs.stat(checkpointsDir)).rejects.toMatchObject({ code: "ENOENT" }) | ||
| expect(JSON.parse(await fs.readFile(messagesPath, "utf8"))).toMatchObject([ | ||
| { type: "say", say: "task", text: "Continue" }, | ||
| ]) | ||
| } finally { | ||
| vi.mocked(fs.rm).mockRestore() | ||
| await fs.rm(storagePath, { recursive: true, force: true }) | ||
| } | ||
| }) | ||
| }) |
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,33 @@ | ||
| import * as vscode from "vscode" | ||
|
|
||
| import type { HistoryItem } from "@roo-code/types" | ||
|
|
||
| import { API } from "../api" | ||
| import type { ClineProvider } from "../../core/webview/ClineProvider" | ||
|
|
||
| const historyItem = { | ||
| id: "task-1602", | ||
| task: "Resume task", | ||
| tokensIn: 0, | ||
| tokensOut: 0, | ||
| totalCost: 0, | ||
| } as HistoryItem | ||
|
|
||
| describe("API.resumeTask", () => { | ||
| it("does not restore a task when workspace selection is cancelled", async () => { | ||
| const provider = { | ||
| viewLaunched: true, | ||
| context: {}, | ||
| getTaskWithId: vi.fn().mockResolvedValue({ historyItem }), | ||
| prepareHistoryItemForResume: vi.fn().mockResolvedValue(undefined), | ||
| createTaskWithHistoryItem: vi.fn(), | ||
| on: vi.fn(), | ||
| } as unknown as ClineProvider | ||
| const outputChannel = { appendLine: vi.fn() } as unknown as vscode.OutputChannel | ||
| const api = new API(outputChannel, provider) | ||
|
|
||
| await api.resumeTask(historyItem.id) | ||
|
|
||
| expect(provider.createTaskWithHistoryItem).not.toHaveBeenCalled() | ||
| }) | ||
| }) |
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.
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: Zoo-Code-Org/Zoo-Code
Length of output: 12061
Test cancellation at both workspace-resume entry points.
When
prepareHistoryItemForResumereturnsundefined, neither entry point may restore or reveal the task. Add separate caller-level tests:ClineProvider.showTaskWithId: assert thatcreateTaskWithHistoryItemand thechatButtonClickedaction are not called.api.resumeTask: assert thatcreateTaskWithHistoryItemis not called.The existing helper tests cover preparation cancellation only. They do not exercise either caller's early-return behavior.
🧰 Tools
🪛 GitHub Check: mutation-diff
[warning] 2309-2309: Mutation test advisory
src/core/webview/ClineProvider.ts:2309: Survived ConditionalExpression mutant (replacement: false). See the job summary for the complete list and resolution guidance.
🤖 Prompt for AI Agents