claude_agent/tools/
todo.rs

1//! Todo tools for task tracking.
2
3use async_trait::async_trait;
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6
7use super::SchemaTool;
8use super::context::ExecutionContext;
9use crate::session::SessionId;
10use crate::session::session_state::ToolState;
11use crate::session::types::{TodoItem, TodoStatus};
12use crate::types::ToolResult;
13
14#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
15#[schemars(deny_unknown_fields)]
16pub struct TodoInputItem {
17    #[schemars(length(min = 1))]
18    pub content: String,
19    pub status: TodoInputStatus,
20    #[serde(rename = "activeForm")]
21    #[schemars(length(min = 1))]
22    pub active_form: String,
23}
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
26#[serde(rename_all = "snake_case")]
27pub enum TodoInputStatus {
28    Pending,
29    InProgress,
30    Completed,
31}
32
33impl From<TodoInputStatus> for TodoStatus {
34    fn from(status: TodoInputStatus) -> Self {
35        match status {
36            TodoInputStatus::Pending => TodoStatus::Pending,
37            TodoInputStatus::InProgress => TodoStatus::InProgress,
38            TodoInputStatus::Completed => TodoStatus::Completed,
39        }
40    }
41}
42
43pub struct TodoWriteTool {
44    state: ToolState,
45    session_id: SessionId,
46}
47
48impl TodoWriteTool {
49    pub fn new(state: ToolState, session_id: SessionId) -> Self {
50        Self { state, session_id }
51    }
52}
53
54#[derive(Debug, Deserialize, JsonSchema)]
55#[schemars(deny_unknown_fields)]
56pub struct TodoWriteInput {
57    /// The updated todo list
58    pub todos: Vec<TodoInputItem>,
59}
60
61#[async_trait]
62impl SchemaTool for TodoWriteTool {
63    type Input = TodoWriteInput;
64
65    const NAME: &'static str = "TodoWrite";
66    const DESCRIPTION: &'static str = r#"Use this tool to create and manage a structured task list for your current coding session. This helps you track progress, organize complex tasks, and demonstrate thoroughness to the user.
67It also helps the user understand the progress of the task and overall progress of their requests.
68
69## When to Use This Tool
70Use this tool proactively in these scenarios:
71
721. Complex multi-step tasks - When a task requires 3 or more distinct steps or actions
732. Non-trivial and complex tasks - Tasks that require careful planning or multiple operations
743. User explicitly requests todo list - When the user directly asks you to use the todo list
754. User provides multiple tasks - When users provide a list of things to be done (numbered or comma-separated)
765. After receiving new instructions - Immediately capture user requirements as todos
776. When you start working on a task - Mark it as in_progress BEFORE beginning work. Ideally you should only have one todo as in_progress at a time
787. After completing a task - Mark it as completed and add any new follow-up tasks discovered during implementation
79
80## When NOT to Use This Tool
81
82Skip using this tool when:
831. There is only a single, straightforward task
842. The task is trivial and tracking it provides no organizational benefit
853. The task can be completed in less than 3 trivial steps
864. The task is purely conversational or informational
87
88NOTE that you should not use this tool if there is only one trivial task to do. In this case you are better off just doing the task directly.
89
90## Examples of When to Use the Todo List
91
92<example>
93User: I want to add a dark mode toggle to the application settings. Make sure you run the tests and build when you're done!
94Assistant: I'll help you add a dark mode toggle to your application settings. Let me create a todo list to track this implementation.
95*Creates todo list with the following items:*
961. Creating dark mode toggle component in Settings page
972. Adding dark mode state management (context/store)
983. Implementing CSS-in-JS styles for dark theme
994. Updating existing components to support theme switching
1005. Running tests and build process, addressing any failures or errors that occur
101*Begins working on the first task*
102
103<reasoning>
104The assistant used the todo list because:
1051. Adding dark mode is a multi-step feature requiring UI, state management, and styling changes
1062. The user explicitly requested tests and build be run afterward
1073. The assistant inferred that tests and build need to pass by adding "Ensure tests and build succeed" as the final task
108</reasoning>
109</example>
110
111<example>
112User: Help me rename the function getCwd to getCurrentWorkingDirectory across my project
113Assistant: Let me first search through your codebase to find all occurrences of 'getCwd'.
114*Uses grep or search tools to locate all instances of getCwd in the codebase*
115Assistant: I've found 15 instances of 'getCwd' across 8 different files. Let me create a todo list to track these changes.
116*Creates todo list with specific items for each file that needs updating*
117
118<reasoning>
119The assistant used the todo list because:
1201. First, the assistant searched to understand the scope of the task
1212. Upon finding multiple occurrences across different files, it determined this was a complex task with multiple steps
1223. The todo list helps ensure every instance is tracked and updated systematically
1234. This approach prevents missing any occurrences and maintains code consistency
124</reasoning>
125</example>
126
127
128<example>
129User: I need to implement these features for my e-commerce site: user registration, product catalog, shopping cart, and checkout flow.
130Assistant: I'll help implement these features. First, let's add all the features to the todo list.
131*Creates a todo list breaking down each feature into specific tasks based on the project architecture*
132Assistant: Let's start with user registration. This will involve creating database models, API endpoints, and frontend forms that integrate with your existing authentication system.
133
134<reasoning>
135The assistant used the todo list because:
1361. The user provided multiple complex features to implement in a comma separated list
1372. The todo list helps organize these large features into manageable tasks
1383. This approach allows for tracking progress across the entire implementation
139</reasoning>
140</example>
141
142<example>
143User: Can you help optimize my React application? It's rendering slowly and has performance issues.</user>
144Assistant: I'll help optimize your React application. First, let me examine your codebase to identify potential performance bottlenecks.
145*Reviews component structure, render patterns, state management, and data fetching*
146Assistant: After analyzing your codebase, I've identified several performance issues. Let me create a todo list to track our optimization efforts.
147*Creates todo list with items like: 1) Implementing memoization for expensive calculations in ProductList, 2) Adding virtualization for long lists in Dashboard, 3) Optimizing image loading in Gallery component, 4) Fixing state update loops in ShoppingCart, 5) Reviewing bundle size and implementing code splitting*
148Let's start by implementing memoization for the expensive calculations in your ProductList component.</assistant>
149
150<reasoning>
151The assistant used the todo list because:
1521. First, the assistant examined the codebase to identify specific performance issues
1532. Based on this analysis, it identified multiple optimization opportunities
1543. Performance optimization is a non-trivial task requiring multiple steps
1554. The todo list helps methodically track improvements across different components
1565. This systematic approach ensures all performance bottlenecks are addressed
157</reasoning>
158</example>
159
160## Examples of When NOT to Use the Todo List
161
162<example>
163User: How do I print 'Hello World' in Python?
164Assistant: In Python, you can print "Hello World" with this simple code:
165
166python
167print("Hello World")
168
169This will output the text "Hello World" to the console when executed.</assistant>
170
171<reasoning>
172The assistant did not use the todo list because this is a single, trivial task that can be completed in one step. There's no need to track multiple tasks or steps for such a straightforward request.
173</reasoning>
174</example>
175
176<example>
177User: What does the git status command do?
178Assistant: The git status command shows the current state of your working directory and staging area. It displays which changes have been staged, which haven't, and which files aren't being tracked by Git. This is a fundamental command for understanding the current state of your repository before making commits.
179
180<reasoning>
181The assistant did not use the todo list because this is an informational request with no actual coding task to complete. The user is simply asking for an explanation, not for the assistant to perform multiple steps or tasks.
182</reasoning>
183</example>
184
185<example>
186User: Can you add a comment to the calculateTotal function to explain what it does?
187Assistant: Sure, let me add a comment to the calculateTotal function to explain what it does.
188* Uses the Edit tool to add a comment to the calculateTotal function *
189
190<reasoning>
191The assistant did not use the todo list because this is a single, straightforward task confined to one location in the code. Adding a comment doesn't require tracking multiple steps or systematic organization.
192</reasoning>
193</example>
194
195<example>
196User: Run npm install for me and tell me what happens.
197Assistant: I'll run the npm install command for you.
198
199*Executes: npm install*
200
201The command completed successfully. Here's the output:
202[Output of npm install command]
203
204All dependencies have been installed according to your package.json file.
205
206<reasoning>
207The assistant did not use the todo list because this is a single command execution with immediate results. There are no multiple steps to track or organize, making the todo list unnecessary for this straightforward task.
208</reasoning>
209</example>
210
211## Task States and Management
212
2131. **Task States**: Use these states to track progress:
214   - pending: Task not yet started
215   - in_progress: Currently working on (limit to ONE task at a time)
216   - completed: Task finished successfully
217
218   **IMPORTANT**: Task descriptions must have two forms:
219   - content: The imperative form describing what needs to be done (e.g., "Run tests", "Build the project")
220   - activeForm: The present continuous form shown during execution (e.g., "Running tests", "Building the project")
221
2222. **Task Management**:
223   - Update task status in real-time as you work
224   - Mark tasks complete IMMEDIATELY after finishing (don't batch completions)
225   - Exactly ONE task must be in_progress at any time (not less, not more)
226   - Complete current tasks before starting new ones
227   - Remove tasks that are no longer relevant from the list entirely
228
2293. **Task Completion Requirements**:
230   - ONLY mark a task as completed when you have FULLY accomplished it
231   - If you encounter errors, blockers, or cannot finish, keep the task as in_progress
232   - When blocked, create a new task describing what needs to be resolved
233   - Never mark a task as completed if:
234     - Tests are failing
235     - Implementation is partial
236     - You encountered unresolved errors
237     - You couldn't find necessary files or dependencies
238
2394. **Task Breakdown**:
240   - Create specific, actionable items
241   - Break complex tasks into smaller, manageable steps
242   - Use clear, descriptive task names
243   - Always provide both forms:
244     - content: "Fix authentication bug"
245     - activeForm: "Fixing authentication bug"
246
247When in doubt, use this tool. Being proactive with task management demonstrates attentiveness and ensures you complete all requirements successfully."#;
248
249    async fn handle(&self, input: Self::Input, _context: &ExecutionContext) -> ToolResult {
250        let in_progress_count = input
251            .todos
252            .iter()
253            .filter(|t| matches!(t.status, TodoInputStatus::InProgress))
254            .count();
255
256        if in_progress_count > 1 {
257            return ToolResult::error(
258                "Only one task can be in_progress at a time. Complete the current task first.",
259            );
260        }
261
262        let todos: Vec<TodoItem> = input
263            .todos
264            .into_iter()
265            .map(|t| {
266                let mut item = TodoItem::new(self.session_id, &t.content, &t.active_form);
267                match t.status {
268                    TodoInputStatus::Pending => {}
269                    TodoInputStatus::InProgress => item.start(),
270                    TodoInputStatus::Completed => item.complete(),
271                }
272                item
273            })
274            .collect();
275
276        self.state.set_todos(todos.clone()).await;
277
278        let mut response = String::from("Todo list updated:\n");
279        for (i, todo) in todos.iter().enumerate() {
280            response.push_str(&format!(
281                "{}. {} {}\n",
282                i + 1,
283                todo.status_icon(),
284                todo.content
285            ));
286        }
287
288        ToolResult::success(response)
289    }
290}
291
292#[cfg(test)]
293mod tests {
294    use super::*;
295    use crate::tools::Tool;
296
297    #[tokio::test]
298    async fn test_todo_write() {
299        let session_id = SessionId::new();
300        let state = ToolState::new(session_id);
301        let tool = TodoWriteTool::new(state, session_id);
302        let execution_context = ExecutionContext::default();
303
304        let result = tool
305            .execute(
306                serde_json::json!({
307                    "todos": [
308                        {"content": "Fix bug", "status": "in_progress", "activeForm": "Fixing bug"},
309                        {"content": "Write tests", "status": "pending", "activeForm": "Writing tests"}
310                    ]
311                }),
312                &execution_context,
313            )
314            .await;
315
316        assert!(!result.is_error());
317        assert!(result.text().contains("Fix bug"));
318    }
319
320    #[tokio::test]
321    async fn test_multiple_in_progress_rejected() {
322        let session_id = SessionId::new();
323        let state = ToolState::new(session_id);
324        let tool = TodoWriteTool::new(state, session_id);
325        let execution_context = ExecutionContext::default();
326
327        let result = tool
328            .execute(
329                serde_json::json!({
330                    "todos": [
331                        {"content": "Task 1", "status": "in_progress", "activeForm": "Doing 1"},
332                        {"content": "Task 2", "status": "in_progress", "activeForm": "Doing 2"}
333                    ]
334                }),
335                &execution_context,
336            )
337            .await;
338
339        assert!(result.is_error());
340    }
341}