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 <thunderggnn@gmail.com>
This commit is contained in:
yetuge 2026-11-21 00:53:33 +08:00 committed by GitHub
parent e3edd05114
commit 271f6e9ad4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 59 additions and 3 deletions

View File

@ -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 (
"<system_reminder>\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"
"</system_reminder>"
)
@ -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,

View File

@ -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: "<system_reminder>changed</system_reminder>")
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()

View File

@ -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()