fix: restore persisted todos in thread history (#8459)

This commit is contained in:
Jasper 2026-11-21 04:47:45 -08:00 committed by GitHub
parent 0ebf8ce7b2
commit 1c286b9336
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 4 deletions

View File

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

View File

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

View File

@ -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.",