diff --git a/backend/app/gateway/routers/threads.py b/backend/app/gateway/routers/threads.py index f32d9a1398..917285c799 100644 --- a/backend/app/gateway/routers/threads.py +++ b/backend/app/gateway/routers/threads.py @@ -2132,8 +2132,9 @@ async def get_thread_history( Only the latest (first) checkpoint carries the ``messages`` key to avoid duplicating the complete conversation across every entry. It also - carries an active ``goal`` and an achieved ``goal_outcome``, because - ``useStream`` rebuilds thread values from this head after every run. + carries the persisted ``todos`` list, an active ``goal`` and an achieved + ``goal_outcome``, because ``useStream`` rebuilds thread values from this + head after every run. """ checkpointer = get_checkpointer(request) try: @@ -2165,6 +2166,8 @@ async def get_thread_history( values["thread_data"] = thread_data if is_latest_checkpoint: + if isinstance(todos := materialized_values.get("todos"), list): + values["todos"] = [todo for todo in todos if isinstance(todo, dict) and isinstance(todo.get("content"), str)] if is_active_goal(goal := materialized_values.get("goal")): values["goal"] = goal outcome = materialized_values.get(GOAL_OUTCOME_CHANNEL) diff --git a/backend/tests/test_threads_router.py b/backend/tests/test_threads_router.py index 85f329eead..9ed538dd7b 100644 --- a/backend/tests/test_threads_router.py +++ b/backend/tests/test_threads_router.py @@ -4167,6 +4167,45 @@ def _create_extension_thread(client, checkpointer, custom_factory, mode, thread_ asyncio.run(_seed_extension_source(checkpointer, custom_factory, mode, thread_id)) +@pytest.mark.parametrize("mode", ["full", "delta"]) +@pytest.mark.parametrize( + ("latest_todos", "expected_todos"), + [ + ([{"content": "Verify the result", "status": "completed"}], [{"content": "Verify the result", "status": "completed"}]), + ([], []), + (None, None), + ([None, {"content": {"invalid": True}}, {"content": "Keep this task", "status": "pending"}], [{"content": "Keep this task", "status": "pending"}]), + ], +) +def test_history_head_preserves_current_todos(monkeypatch, mode, latest_todos, expected_todos) -> None: + app, _store, checkpointer = _build_thread_app() + custom_factory = _wire_extension_agent(monkeypatch, app, checkpointer, mode) + thread_id = f"todo-head-{mode}" + + async def write_todos(): + accessor = CheckpointStateAccessor.bind(custom_factory(), checkpointer, mode=mode) + config = {"configurable": {"thread_id": thread_id, "checkpoint_ns": ""}} + await accessor.aupdate(config, {"todos": [{"content": "Older task", "status": "pending"}]}, as_node="model") + await accessor.aupdate(config, {"todos": latest_todos}, as_node="model") + + with TestClient(app) as client: + _create_extension_thread(client, checkpointer, custom_factory, mode, thread_id) + if latest_todos is not None: + asyncio.run(write_todos()) + head_response = client.post(f"/api/threads/{thread_id}/history", json={"limit": 1}) + history_response = client.post(f"/api/threads/{thread_id}/history", json={"limit": 10}) + + assert head_response.status_code == history_response.status_code == 200 + history = history_response.json() + assert head_response.json() == history[:1] + if expected_todos is None: + assert "todos" not in history[0]["values"] + else: + assert history[0]["values"]["todos"] == expected_todos + assert len(history) > 1 + assert all("todos" not in entry["values"] for entry in history[1:]) + + @pytest.mark.parametrize("mode", ["full", "delta"]) def test_history_head_carries_goal_state_without_messages(monkeypatch, mode) -> None: """useStream rebuilds thread values from the head, so a goal set on /chats/new diff --git a/contracts/thread_goal_contract.json b/contracts/thread_goal_contract.json index 22d3e7c406..9c040db52a 100644 --- a/contracts/thread_goal_contract.json +++ b/contracts/thread_goal_contract.json @@ -1,6 +1,6 @@ { "version": 1, - "description": "Cross-language contract fixture for the chat goal state. When auto-continue stands down, the worker stores one of stand_down_reason_codes in goal.last_evaluation.stand_down_reason (backend/packages/harness/deerflow/runtime/runs/worker.py); check_failure_codes are the ones where the goal could not be checked. Clearing a met goal writes the goal_outcome channel in the same checkpoint, and every other goal write removes it (runtime/goal.py write_thread_goal); clients cannot write it. The POST /api/threads/{thread_id}/history head carries at most history_head_keys, projecting goal only while it is active and goal_outcome only when achieved. host_reason_texts are the English last_evaluation.reason values the host writes itself instead of the evaluator; the evaluator_failed reason embeds an exception type name and is covered by check_failure_codes. The frontend reads the same file so it never shows a raw code and never labels host text as the evaluator's.", + "description": "Cross-language contract fixture for the chat goal state. When auto-continue stands down, the worker stores one of stand_down_reason_codes in goal.last_evaluation.stand_down_reason (backend/packages/harness/deerflow/runtime/runs/worker.py); check_failure_codes are the ones where the goal could not be checked. Clearing a met goal writes the goal_outcome channel in the same checkpoint, and every other goal write removes it (runtime/goal.py write_thread_goal); clients cannot write it. The POST /api/threads/{thread_id}/history head carries at most history_head_keys, projecting goal only while it is active, goal_outcome only when achieved, and todos when the persisted value is a list (including an explicit empty list), keeping only objects with string content. host_reason_texts are the English last_evaluation.reason values the host writes itself instead of the evaluator; the evaluator_failed reason embeds an exception type name and is covered by check_failure_codes. The frontend reads the same file so it never shows a raw code and never labels host text as the evaluator's.", "stand_down_reason_codes": [ "blocked:missing_evidence", "blocked:needs_user_input", @@ -37,7 +37,7 @@ "reply_message_id" ] }, - "history_head_keys": ["title", "thread_data", "messages", "goal", "goal_outcome"], + "history_head_keys": ["title", "thread_data", "messages", "goal", "goal_outcome", "todos"], "host_reason_texts": [ "No visible assistant evidence is available yet.", "The turn ended on a question to the user that has not been answered.",