From 271f6e9ad4b4ccd0f57b4d9ef27d7d986a535fca Mon Sep 17 00:00:00 2001 From: yetuge <182123929+yetuge@users.noreply.github.com> Date: Sat, 21 Nov 2026 00:53:33 +0800 Subject: [PATCH] fix(agents): make the todo completion reminder match the guard's terminal statuses (#8432) * fix(agents): make the todo completion reminder match the guard's terminal statuses The reminder told the model to finish everything and only mark items completed, while the guard also accepts blocked and waiting. A step that cannot continue in this run was retried or falsely marked completed to get past the guard. Offer blocked/waiting and name the same terminal set the exit check uses. * test(agents): state that the reminder test checks the instruction, not the response * fix(agents): include the completion reminder template in the assembly fingerprint The fixed reminder is model-visible instruction text, but release_policy_parameters hashed only the system prompt and tool description, so a wording change left AgentAssemblyDescriptor.fingerprint unchanged. Hash the template rendered with an empty item list, keeping per-run todo content out. --------- Co-authored-by: Nan Gao --- .../agents/middlewares/todo_middleware.py | 15 ++++++++--- .../tests/test_middleware_release_policy.py | 20 ++++++++++++++ backend/tests/test_todo_middleware.py | 27 +++++++++++++++++++ 3 files changed, 59 insertions(+), 3 deletions(-) diff --git a/backend/packages/harness/deerflow/agents/middlewares/todo_middleware.py b/backend/packages/harness/deerflow/agents/middlewares/todo_middleware.py index b69aec24b6..47a90f67f4 100644 --- a/backend/packages/harness/deerflow/agents/middlewares/todo_middleware.py +++ b/backend/packages/harness/deerflow/agents/middlewares/todo_middleware.py @@ -136,14 +136,20 @@ def _format_todos(todos: Iterable[Mapping[str, Any]]) -> str: def _format_completion_reminder(todos: Iterable[Mapping[str, Any]]) -> str: - """Format a completion reminder for incomplete todo items.""" + """Format a completion reminder for incomplete todo items. + + The wording must match ``_TODO_TERMINAL_STATUSES``: the guard also accepts + ``blocked`` and ``waiting``, so the reminder offers them instead of asking + the model to finish everything or mark it completed. + """ incomplete_text = _format_todos(todo for todo in todos if _stored_todo_status(todo) not in _TODO_TERMINAL_STATUSES) return ( "\n" - "You have incomplete todo items that must be finished before giving your final response:\n\n" + "You have todo items that are not completed, blocked, or waiting:\n\n" f"{incomplete_text}\n\n" "Please continue working on these tasks. Call `write_todos` to mark items as completed " - "as you finish them, and only respond when all items are done.\n" + "as you finish them. Mark an item `blocked` or `waiting` only when it cannot continue in this run, " + "and only respond when every item is completed, blocked, or waiting.\n" "" ) @@ -210,6 +216,9 @@ class TodoMiddleware(TodoListMiddleware): return { "system_prompt_hash": canonical_hash(self.system_prompt), "tool_description_hash": canonical_hash(self.tool_description), + # The fixed reminder template is model-visible instruction text too; + # hash it with an empty item list so per-run todo content stays out. + "completion_reminder_template_hash": canonical_hash(_format_completion_reminder([])), "state_channel": "todos", "skip_completion_reminder_on_length_cap": True, "skip_completion_reminder_on_safety_cap": True, diff --git a/backend/tests/test_middleware_release_policy.py b/backend/tests/test_middleware_release_policy.py index 78d90393b7..dff82cf8d5 100644 --- a/backend/tests/test_middleware_release_policy.py +++ b/backend/tests/test_middleware_release_policy.py @@ -326,6 +326,26 @@ def test_todo_hard_stop_policy_changes_assembly_identity(monkeypatch): assert _middleware_fingerprint(current) != _middleware_fingerprint(previous) +def test_todo_completion_reminder_template_changes_assembly_identity(monkeypatch): + """Model-visible reminder text belongs in the assembly fingerprint. + + The template is hashed with an empty item list, so per-run todo content + stays out while a wording change still changes the descriptor. + """ + from deerflow_extension_api import canonical_hash + + from deerflow.agents.middlewares import todo_middleware + from deerflow.agents.middlewares.todo_middleware import TodoMiddleware + + baseline_params = TodoMiddleware().release_policy_parameters() + assert baseline_params["completion_reminder_template_hash"] == canonical_hash(todo_middleware._format_completion_reminder([])) + baseline = _middleware_fingerprint(TodoMiddleware()) + + monkeypatch.setattr(todo_middleware, "_format_completion_reminder", lambda _todos: "changed") + + assert _middleware_fingerprint(TodoMiddleware()) != baseline + + def test_safety_text_only_stop_policy_changes_assembly_identity(monkeypatch): current = _make_safety_finish_reason_middleware() previous = _make_safety_finish_reason_middleware() diff --git a/backend/tests/test_todo_middleware.py b/backend/tests/test_todo_middleware.py index 294ea67b56..2d0dbd336a 100644 --- a/backend/tests/test_todo_middleware.py +++ b/backend/tests/test_todo_middleware.py @@ -1321,6 +1321,33 @@ class TestMalformedStoredTodos: assert "- [pending] Recover me" in reminder assert "Finished" not in reminder + def test_completion_reminder_describes_the_guard_terminal_statuses(self): + """The reminder must offer blocked/waiting, which the guard also accepts. + + The old text asked the model to finish everything and only mark items + completed. This test checks the instruction the model receives, not how a + particular model responds to it. + """ + reminder = _format_completion_reminder([{"content": "Needs input", "status": "in_progress"}]) + + assert "`blocked`" in reminder + assert "`waiting`" in reminder + assert "every item is completed, blocked, or waiting" in reminder + assert "only respond when all items are done" not in reminder + assert "must be finished" not in reminder + + @pytest.mark.parametrize("status", ["blocked", "waiting"]) + def test_completion_reminder_excludes_terminal_non_completed_items(self, status): + reminder = _format_completion_reminder( + [ + {"content": "Needs input", "status": status}, + {"content": "Still pending", "status": "pending"}, + ] + ) + + assert "Needs input" not in reminder + assert "Still pending" in reminder + @pytest.mark.parametrize("todos", [{"content": "x"}, "oops", 5]) def test_todos_that_are_not_a_list_allow_a_clean_exit(self, todos): mw = TodoMiddleware()