-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: Add todo validation for attempt completion #5460
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
Closed
Closed
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| import { describe, test, expect, vi } from "vitest" | ||
| import { attemptCompletionTool } from "../attemptCompletionTool" | ||
| import { Task } from "../../task/Task" | ||
| import { TodoItem } from "@roo-code/types" | ||
| import { ToolUse } from "../../../shared/tools" | ||
|
|
||
| // Mock dependencies | ||
| vi.mock("@roo-code/telemetry", () => ({ | ||
| TelemetryService: { | ||
| instance: { | ||
| captureTaskCompleted: vi.fn(), | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| vi.mock("../prompts/responses", () => ({ | ||
| formatResponse: { | ||
| toolError: vi.fn((message) => message), | ||
| }, | ||
| })) | ||
|
|
||
| describe("attemptCompletionTool", () => { | ||
| const createMockTask = (todoList?: TodoItem[]): Task => { | ||
| const task = { | ||
| todoList, | ||
| consecutiveMistakeCount: 0, | ||
| clineMessages: [], | ||
| recordToolError: vi.fn(), | ||
| sayAndCreateMissingParamError: vi.fn().mockResolvedValue("Missing parameter error"), | ||
| say: vi.fn(), | ||
| emit: vi.fn(), | ||
| getTokenUsage: vi.fn().mockReturnValue({}), | ||
| toolUsage: {}, | ||
| parentTask: null, | ||
| ask: vi.fn(), | ||
| userMessageContent: [], | ||
| } as unknown as Task | ||
| return task | ||
| } | ||
|
|
||
| const createMockToolUse = (result?: string, partial = false): ToolUse => ({ | ||
| type: "tool_use", | ||
| name: "attempt_completion", | ||
| params: { result }, | ||
| partial, | ||
| }) | ||
|
|
||
| const mockFunctions = { | ||
| askApproval: vi.fn(), | ||
| handleError: vi.fn(), | ||
| pushToolResult: vi.fn(), | ||
| removeClosingTag: vi.fn((tag, text) => text || ""), | ||
| toolDescription: vi.fn(() => "[attempt_completion]"), | ||
| askFinishSubTaskApproval: vi.fn(), | ||
| } | ||
|
|
||
| test("should block completion when there are pending todos", async () => { | ||
| const todoList: TodoItem[] = [ | ||
| { id: "1", content: "Complete task 1", status: "completed" }, | ||
| { id: "2", content: "Complete task 2", status: "pending" }, | ||
| ] | ||
| const task = createMockTask(todoList) | ||
| const toolUse = createMockToolUse("Task completed successfully") | ||
|
|
||
| await attemptCompletionTool( | ||
| task, | ||
| toolUse, | ||
| mockFunctions.askApproval, | ||
| mockFunctions.handleError, | ||
| mockFunctions.pushToolResult, | ||
| mockFunctions.removeClosingTag, | ||
| mockFunctions.toolDescription, | ||
| mockFunctions.askFinishSubTaskApproval, | ||
| ) | ||
|
|
||
| expect(task.consecutiveMistakeCount).toBe(1) | ||
| expect(task.recordToolError).toHaveBeenCalledWith("attempt_completion") | ||
| expect(mockFunctions.pushToolResult).toHaveBeenCalledWith( | ||
| expect.stringContaining("Cannot attempt completion while there are incomplete todos"), | ||
| ) | ||
| expect(mockFunctions.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("Pending todos:")) | ||
| expect(mockFunctions.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("- [ ] Complete task 2")) | ||
| }) | ||
|
|
||
| test("should block completion when there are in_progress todos", async () => { | ||
| const todoList: TodoItem[] = [ | ||
| { id: "1", content: "Complete task 1", status: "completed" }, | ||
| { id: "2", content: "Complete task 2", status: "in_progress" }, | ||
| ] | ||
| const task = createMockTask(todoList) | ||
| const toolUse = createMockToolUse("Task completed successfully") | ||
|
|
||
| await attemptCompletionTool( | ||
| task, | ||
| toolUse, | ||
| mockFunctions.askApproval, | ||
| mockFunctions.handleError, | ||
| mockFunctions.pushToolResult, | ||
| mockFunctions.removeClosingTag, | ||
| mockFunctions.toolDescription, | ||
| mockFunctions.askFinishSubTaskApproval, | ||
| ) | ||
|
|
||
| expect(task.consecutiveMistakeCount).toBe(1) | ||
| expect(task.recordToolError).toHaveBeenCalledWith("attempt_completion") | ||
| expect(mockFunctions.pushToolResult).toHaveBeenCalledWith( | ||
| expect.stringContaining("Cannot attempt completion while there are incomplete todos"), | ||
| ) | ||
| expect(mockFunctions.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("In Progress todos:")) | ||
| expect(mockFunctions.pushToolResult).toHaveBeenCalledWith(expect.stringContaining("- [-] Complete task 2")) | ||
| }) | ||
|
|
||
| test("should allow completion when all todos are completed", async () => { | ||
| const todoList: TodoItem[] = [ | ||
| { id: "1", content: "Complete task 1", status: "completed" }, | ||
| { id: "2", content: "Complete task 2", status: "completed" }, | ||
| ] | ||
| const task = createMockTask(todoList) | ||
| const toolUse = createMockToolUse("Task completed successfully") | ||
|
|
||
| // Mock the ask method to return yesButtonClicked for completion | ||
| task.ask = vi.fn().mockResolvedValue({ response: "yesButtonClicked" }) | ||
|
|
||
| await attemptCompletionTool( | ||
| task, | ||
| toolUse, | ||
| mockFunctions.askApproval, | ||
| mockFunctions.handleError, | ||
| mockFunctions.pushToolResult, | ||
| mockFunctions.removeClosingTag, | ||
| mockFunctions.toolDescription, | ||
| mockFunctions.askFinishSubTaskApproval, | ||
| ) | ||
|
|
||
| expect(task.consecutiveMistakeCount).toBe(0) | ||
| expect(task.recordToolError).not.toHaveBeenCalled() | ||
| expect(task.say).toHaveBeenCalledWith("completion_result", "Task completed successfully", undefined, false) | ||
| expect(mockFunctions.pushToolResult).toHaveBeenCalledWith("") | ||
| }) | ||
|
|
||
| test("should allow completion when no todos exist", async () => { | ||
| const task = createMockTask() // No todos | ||
| const toolUse = createMockToolUse("Task completed successfully") | ||
|
|
||
| // Mock the ask method to return yesButtonClicked for completion | ||
| task.ask = vi.fn().mockResolvedValue({ response: "yesButtonClicked" }) | ||
|
|
||
| await attemptCompletionTool( | ||
| task, | ||
| toolUse, | ||
| mockFunctions.askApproval, | ||
| mockFunctions.handleError, | ||
| mockFunctions.pushToolResult, | ||
| mockFunctions.removeClosingTag, | ||
| mockFunctions.toolDescription, | ||
| mockFunctions.askFinishSubTaskApproval, | ||
| ) | ||
|
|
||
| expect(task.consecutiveMistakeCount).toBe(0) | ||
| expect(task.recordToolError).not.toHaveBeenCalled() | ||
| expect(task.say).toHaveBeenCalledWith("completion_result", "Task completed successfully", undefined, false) | ||
| expect(mockFunctions.pushToolResult).toHaveBeenCalledWith("") | ||
| }) | ||
|
|
||
| test("should still block completion for missing result parameter", async () => { | ||
| const task = createMockTask() | ||
| const toolUse = createMockToolUse() // No result parameter | ||
|
|
||
| await attemptCompletionTool( | ||
| task, | ||
| toolUse, | ||
| mockFunctions.askApproval, | ||
| mockFunctions.handleError, | ||
| mockFunctions.pushToolResult, | ||
| mockFunctions.removeClosingTag, | ||
| mockFunctions.toolDescription, | ||
| mockFunctions.askFinishSubTaskApproval, | ||
| ) | ||
|
|
||
| expect(task.consecutiveMistakeCount).toBe(1) | ||
| expect(task.recordToolError).toHaveBeenCalledWith("attempt_completion") | ||
| expect(task.sayAndCreateMissingParamError).toHaveBeenCalledWith("attempt_completion", "result") | ||
| }) | ||
| }) |
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,86 @@ | ||
| import { describe, test, expect } from "vitest" | ||
| import { validateTodosForCompletion } from "../updateTodoListTool" | ||
| import { TodoItem } from "@roo-code/types" | ||
|
|
||
| describe("validateTodosForCompletion", () => { | ||
| test("should allow completion when no todos exist", () => { | ||
| const result = validateTodosForCompletion(undefined) | ||
| expect(result.valid).toBe(true) | ||
| expect(result.error).toBeUndefined() | ||
| }) | ||
|
|
||
| test("should allow completion when empty todo list", () => { | ||
| const result = validateTodosForCompletion([]) | ||
| expect(result.valid).toBe(true) | ||
| expect(result.error).toBeUndefined() | ||
| }) | ||
|
|
||
| test("should allow completion when all todos are completed", () => { | ||
| const todos: TodoItem[] = [ | ||
| { id: "1", content: "Task 1", status: "completed" }, | ||
| { id: "2", content: "Task 2", status: "completed" }, | ||
| ] | ||
| const result = validateTodosForCompletion(todos) | ||
| expect(result.valid).toBe(true) | ||
| expect(result.error).toBeUndefined() | ||
| }) | ||
|
|
||
| test("should block completion when there are pending todos", () => { | ||
| const todos: TodoItem[] = [ | ||
| { id: "1", content: "Task 1", status: "completed" }, | ||
| { id: "2", content: "Task 2", status: "pending" }, | ||
| ] | ||
| const result = validateTodosForCompletion(todos) | ||
| expect(result.valid).toBe(false) | ||
| expect(result.error).toContain("Cannot attempt completion while there are incomplete todos") | ||
| expect(result.error).toContain("Pending todos:") | ||
| expect(result.error).toContain("- [ ] Task 2") | ||
| expect(result.incompleteTodos?.pending).toHaveLength(1) | ||
| expect(result.incompleteTodos?.pending[0].content).toBe("Task 2") | ||
| }) | ||
|
|
||
| test("should block completion when there are in_progress todos", () => { | ||
| const todos: TodoItem[] = [ | ||
| { id: "1", content: "Task 1", status: "completed" }, | ||
| { id: "2", content: "Task 2", status: "in_progress" }, | ||
| ] | ||
| const result = validateTodosForCompletion(todos) | ||
| expect(result.valid).toBe(false) | ||
| expect(result.error).toContain("Cannot attempt completion while there are incomplete todos") | ||
| expect(result.error).toContain("In Progress todos:") | ||
| expect(result.error).toContain("- [-] Task 2") | ||
| expect(result.incompleteTodos?.inProgress).toHaveLength(1) | ||
| expect(result.incompleteTodos?.inProgress[0].content).toBe("Task 2") | ||
| }) | ||
|
|
||
| test("should block completion when there are both pending and in_progress todos", () => { | ||
| const todos: TodoItem[] = [ | ||
| { id: "1", content: "Task 1", status: "completed" }, | ||
| { id: "2", content: "Task 2", status: "pending" }, | ||
| { id: "3", content: "Task 3", status: "in_progress" }, | ||
| { id: "4", content: "Task 4", status: "pending" }, | ||
| ] | ||
| const result = validateTodosForCompletion(todos) | ||
| expect(result.valid).toBe(false) | ||
| expect(result.error).toContain("Cannot attempt completion while there are incomplete todos") | ||
| expect(result.error).toContain("Pending todos:") | ||
| expect(result.error).toContain("- [ ] Task 2") | ||
| expect(result.error).toContain("- [ ] Task 4") | ||
| expect(result.error).toContain("In Progress todos:") | ||
| expect(result.error).toContain("- [-] Task 3") | ||
| expect(result.error).toContain( | ||
| "Please complete all todos using the update_todo_list tool before attempting completion", | ||
| ) | ||
| expect(result.incompleteTodos?.pending).toHaveLength(2) | ||
| expect(result.incompleteTodos?.inProgress).toHaveLength(1) | ||
| }) | ||
|
|
||
| test("should provide helpful error message with update_todo_list tool reference", () => { | ||
| const todos: TodoItem[] = [{ id: "1", content: "Incomplete task", status: "pending" }] | ||
| const result = validateTodosForCompletion(todos) | ||
| expect(result.valid).toBe(false) | ||
| expect(result.error).toContain( | ||
| "Please complete all todos using the update_todo_list tool before attempting completion", | ||
| ) | ||
| }) | ||
| }) |
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
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
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.
User-facing error messages (e.g. in validateTodosForCompletion) are hardcoded. It is recommended to use the internationalization function to support translations consistently.
This comment was generated because it violated a code review rule: irule_C0ez7Rji6ANcGkkX.