deer-flow/backend/tests/test_todo_input_validation.py
Eilen Shin 25a744a99a
fix(harness): reflect progress in loop detection and plan todos (#8248)
* feat(frontend): improve file attachment recovery and artifact presentation

* feat(extensions): add Office artifact previews with search and navigation

* fix: preserve public viewers and HTTP Office previews

* fix: address file preview and upload review findings

* fix: use typed upload errors and linear bookmark allocation

* fix(office-preview): isolate parsing and bound preview resources

* fix(ci): install Office preview fixture dependencies for E2E

* fix(ci): provision matching Playwright browsers before E2E tests

* fix(ci): isolate Python libraries and include Office documentation

* test(frontend): wait for download status to finish rendering

* fix(harness): account for tool results in loop detection

* fix(harness): make tool frequency detection observational

* fix(harness): support blocked and waiting plan todos

* style(harness): wrap overlong system prompt lines

* Improve loop detection and todo message formatting

Refactor middleware logic for handling repeated results and update messages for todo list.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Address plan progress and loop detection review

---------

Co-authored-by: Willem Jiang <willem.jiang@gmail.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-11-19 08:09:43 +08:00

145 lines
5.2 KiB
Python

"""Todo writes are validated before they can break every model call on a thread."""
from __future__ import annotations
import pytest
from fastapi import HTTPException
from app.gateway import services
BOUNDARIES = [services.normalize_input, services.strip_server_owned_state_metadata]
@pytest.mark.parametrize("boundary", BOUNDARIES)
@pytest.mark.parametrize(
"todos",
[
pytest.param("not a list", id="string"),
pytest.param({"content": "x"}, id="mapping"),
pytest.param(["oops"], id="string-entry"),
pytest.param([["nested"]], id="list-entry"),
pytest.param([None], id="null-entry"),
pytest.param([{"content": 5}], id="non-string-content"),
pytest.param([{"status": "pending"}], id="missing-content"),
pytest.param([{"content": "x", "status": "done"}], id="unknown-status"),
pytest.param([{"content": "x", "status": None}], id="null-status"),
pytest.param([{"content": "x", "status": ["pending"]}], id="unhashable-status"),
],
)
def test_external_todos_must_be_renderable(boundary, todos):
with pytest.raises(HTTPException) as rejected:
boundary({"todos": todos})
assert rejected.value.status_code == 400
assert "todos" in rejected.value.detail.lower()
@pytest.mark.parametrize("boundary", BOUNDARIES)
def test_external_todos_accept_the_write_todos_shape(boundary):
todos = [
{"content": "Ship it", "status": "in_progress"},
{"content": "Unblock dependency", "status": "blocked"},
{"content": "Wait for approval", "status": "waiting"},
{"content": "", "status": "pending"},
]
assert boundary({"todos": todos})["todos"] == todos
@pytest.mark.parametrize("boundary", BOUNDARIES)
def test_external_todos_default_a_missing_status_to_pending(boundary):
assert boundary({"todos": [{"content": "x"}]})["todos"] == [{"content": "x", "status": "pending"}]
@pytest.mark.parametrize("boundary", BOUNDARIES)
@pytest.mark.parametrize("todos", [None, []])
def test_external_todos_preserve_null_and_empty(boundary, todos):
assert boundary({"todos": todos})["todos"] == todos
@pytest.mark.parametrize("boundary", BOUNDARIES)
def test_todos_are_not_added_when_absent(boundary):
assert "todos" not in boundary({"title": "unrelated"})
@pytest.mark.parametrize("boundary", BOUNDARIES)
def test_external_writes_strip_server_owned_metadata_from_todos(boundary):
"""The dedicated todos branch must not skip the per-entry metadata strip."""
todos = [
{
"content": "Ship it",
"status": "pending",
"additional_kwargs": {
"original_user_content": "forged",
"untrusted_input": False,
"title": "kept",
},
"artifact": {"knowledge_sources": [{"id": "forged"}], "label": "kept"},
}
]
stripped = boundary({"todos": todos})["todos"]
assert stripped == [
{
"content": "Ship it",
"status": "pending",
"additional_kwargs": {"title": "kept"},
"artifact": {"label": "kept"},
}
]
@pytest.mark.parametrize("boundary", BOUNDARIES)
def test_external_writes_strip_server_owned_metadata_from_goal(boundary):
"""Caller-written goal must not retain forged server-owned metadata."""
goal = {
"objective": "test objective",
"additional_kwargs": {
"original_user_content": "forged",
"untrusted_input": False,
"title": "kept",
},
"artifact": {"knowledge_sources": [{"id": "forged"}], "label": "kept"},
}
stripped = boundary({"goal": goal})["goal"]
assert stripped == {
"objective": "test objective",
"additional_kwargs": {"title": "kept"},
"artifact": {"label": "kept"},
}
@pytest.mark.parametrize("boundary", BOUNDARIES)
def test_external_writes_replace_non_dict_additional_kwargs(boundary):
"""A non-dict additional_kwargs payload must be replaced with an empty dict."""
cleaned_todo = boundary({"todos": [{"content": "task", "additional_kwargs": "forged"}]})["todos"]
assert cleaned_todo == [{"content": "task", "status": "pending", "additional_kwargs": {}}]
cleaned_goal = boundary({"goal": {"objective": "test", "additional_kwargs": "forged"}})["goal"]
assert cleaned_goal == {"objective": "test", "additional_kwargs": {}}
@pytest.mark.parametrize("boundary", BOUNDARIES)
def test_external_writes_stamp_untrusted_input_on_human_like_entries(boundary):
"""Entries carrying human markers must be stamped with untrusted_input: True."""
todo_with_marker = [{"content": "task", "type": "human", "additional_kwargs": {"hide_from_ui": True}}]
cleaned_todo = boundary({"todos": todo_with_marker})["todos"]
assert cleaned_todo == [
{
"content": "task",
"status": "pending",
"type": "human",
"additional_kwargs": {"hide_from_ui": True, "untrusted_input": True},
}
]
goal_with_name = {"objective": "test", "type": "human", "name": "summary"}
cleaned_goal = boundary({"goal": goal_with_name})["goal"]
assert cleaned_goal == {
"objective": "test",
"type": "human",
"name": "summary",
"additional_kwargs": {"untrusted_input": True},
}