Skip to content

Commit 0834c57

Browse files
committed
Clear the todos once they are all done
Signed-off-by: Djordje Lukic <djordje.lukic@docker.com>
1 parent da741b0 commit 0834c57

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

pkg/concurrent/slice.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,10 @@ func (s *Slice[V]) Update(index int, f func(V) V) bool {
8888
s.values[index] = f(s.values[index])
8989
return true
9090
}
91+
92+
func (s *Slice[V]) Clear() {
93+
s.mu.Lock()
94+
defer s.mu.Unlock()
95+
96+
s.values = nil
97+
}

pkg/tools/builtin/todo.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,32 @@ func (h *todoHandler) updateTodos(_ context.Context, params UpdateTodosArgs) (*t
157157
return tools.ResultError(output.String()), nil
158158
}
159159

160+
// Clear all todos if all are completed
161+
if h.allCompleted() {
162+
h.todos.Clear()
163+
}
164+
160165
return &tools.ToolCallResult{
161166
Output: output.String(),
162167
Meta: h.todos.All(),
163168
}, nil
164169
}
165170

171+
func (h *todoHandler) allCompleted() bool {
172+
if h.todos.Length() == 0 {
173+
return false
174+
}
175+
allDone := true
176+
h.todos.Range(func(_ int, todo Todo) bool {
177+
if todo.Status != "completed" {
178+
allDone = false
179+
return false
180+
}
181+
return true
182+
})
183+
return allDone
184+
}
185+
166186
func (h *todoHandler) listTodos(_ context.Context, _ tools.ToolCall) (*tools.ToolCallResult, error) {
167187
var output strings.Builder
168188
output.WriteString("Current todos:\n")

pkg/tools/builtin/todo_test.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ func TestTodoTool_UpdateTodos(t *testing.T) {
167167
func TestTodoTool_UpdateTodos_PartialFailure(t *testing.T) {
168168
tool := NewTodoTool()
169169

170-
// Create a single todo
171-
_, err := tool.handler.createTodo(t.Context(), CreateTodoArgs{
172-
Description: "Test todo item",
170+
// Create two todos so we can complete one without clearing the list
171+
_, err := tool.handler.createTodos(t.Context(), CreateTodosArgs{
172+
Descriptions: []string{"First todo item", "Second todo item"},
173173
})
174174
require.NoError(t, err)
175175

@@ -185,10 +185,11 @@ func TestTodoTool_UpdateTodos_PartialFailure(t *testing.T) {
185185
assert.Contains(t, result.Output, "Updated 1 todos")
186186
assert.Contains(t, result.Output, "Not found: nonexistent")
187187

188-
// Verify the existing todo was updated
188+
// Verify the existing todo was updated (list not cleared because todo_2 still pending)
189189
todos := tool.handler.todos.All()
190-
require.Len(t, todos, 1)
190+
require.Len(t, todos, 2)
191191
assert.Equal(t, "completed", todos[0].Status)
192+
assert.Equal(t, "pending", todos[1].Status)
192193
}
193194

194195
func TestTodoTool_UpdateTodos_AllNotFound(t *testing.T) {
@@ -206,6 +207,35 @@ func TestTodoTool_UpdateTodos_AllNotFound(t *testing.T) {
206207
assert.Contains(t, result.Output, "Not found: nonexistent1, nonexistent2")
207208
}
208209

210+
func TestTodoTool_UpdateTodos_ClearsWhenAllCompleted(t *testing.T) {
211+
tool := NewTodoTool()
212+
213+
// Create multiple todos
214+
_, err := tool.handler.createTodos(t.Context(), CreateTodosArgs{
215+
Descriptions: []string{"First todo item", "Second todo item"},
216+
})
217+
require.NoError(t, err)
218+
219+
// Complete all todos
220+
result, err := tool.handler.updateTodos(t.Context(), UpdateTodosArgs{
221+
Updates: []TodoUpdate{
222+
{ID: "todo_1", Status: "completed"},
223+
{ID: "todo_2", Status: "completed"},
224+
},
225+
})
226+
require.NoError(t, err)
227+
assert.Contains(t, result.Output, "Updated 2 todos")
228+
229+
// Verify all todos were cleared
230+
todos := tool.handler.todos.All()
231+
assert.Empty(t, todos)
232+
233+
// Verify Meta is also empty
234+
metaTodos, ok := result.Meta.([]Todo)
235+
require.True(t, ok, "Meta should be []Todo")
236+
assert.Empty(t, metaTodos)
237+
}
238+
209239
func TestTodoTool_OutputSchema(t *testing.T) {
210240
tool := NewTodoTool()
211241

0 commit comments

Comments
 (0)