Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/agent/src/todos.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def manage_todos(todos: list[Todo], runtime: ToolRuntime) -> Command:
"""
Manage the current todos.
"""
# Validate supplied IDs before assigning any missing IDs or updating state.
supplied_ids = [todo["id"] for todo in todos if todo.get("id")]
if len(supplied_ids) != len(set(supplied_ids)):
raise ValueError("Todo IDs must be unique; give each item a distinct ID.")

# Ensure all todos have IDs that are unique
for todo in todos:
if "id" not in todo or not todo["id"]:
Expand Down
37 changes: 37 additions & 0 deletions apps/agent/tests/test_todo_unique_ids_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from copy import deepcopy

import pytest
from langchain.tools import ToolRuntime

from src.todos import manage_todos


def runtime():
return ToolRuntime(state={"messages": [], "todos": []}, context=None, config={},
stream_writer=lambda _: None, tool_call_id="todo-call", store=None)


def todo(id, title):
return {"id": id, "title": title, "description": "", "emoji": "", "status": "pending"}


def test_duplicate_supplied_ids_are_rejected_before_state_update():
items = [todo("", "New"), todo("same", "First"), todo("same", "Second")]
original = deepcopy(items)
with pytest.raises(ValueError, match="unique"):
manage_todos.func(items, runtime())
assert items == original


def test_unique_supplied_ids_are_stable_in_state_update():
items = [todo("one", "First"), todo("two", "Second")]
result = manage_todos.func(items, runtime())
assert result.update["todos"] == items
assert result.update["messages"][0].tool_call_id == "todo-call"


def test_new_todos_receive_distinct_nonempty_ids():
result = manage_todos.func([todo("", "First"), todo("", "Second")], runtime())
ids = [item["id"] for item in result.update["todos"]]
assert all(ids)
assert len(set(ids)) == 2