Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix(todo): tolerate malformed persisted status

guozirui14178c2741d28
diff --git a/jiuwenswarm/common/todo_snapshot.py b/jiuwenswarm/common/todo_snapshot.py
index 85647f3167..201d6d64d0 100644
--- a/jiuwenswarm/common/todo_snapshot.py
+++ b/jiuwenswarm/common/todo_snapshot.py
@@ -52,8 +52,12 @@ def format_todos_for_frontend(todos_data: list[Any]) -> list[dict[str, Any]]:
active_form = getattr(item, "activeForm", None)
status_value = getattr(status, "value", status)
- status_key = str(status_value or "pending").lower()
- if status in _CANCELLED_STATUSES or status_key in _CANCELLED_STATUSES:
+ status_key = (
+ status_value.lower()
+ if isinstance(status_value, str) and status_value
+ else "pending"
+ )
+ if status_key in _CANCELLED_STATUSES:
continue
if todo_id is None or todo_id == "":
continue
@@ -62,7 +66,7 @@ def format_todos_for_frontend(todos_data: list[Any]) -> list[dict[str, Any]]:
"id": str(todo_id),
"content": content_text,
"activeForm": active_form if isinstance(active_form, str) else content_text,
- "status": _STATUS_TO_FRONTEND.get(status, _STATUS_TO_FRONTEND.get(status_key, "pending")),
+ "status": _STATUS_TO_FRONTEND.get(status_key, "pending"),
})
return formatted
diff --git a/tests/unit_tests/server/test_todo_snapshot.py b/tests/unit_tests/server/test_todo_snapshot.py
index 143ed63088..2451f0dc34 100644
--- a/tests/unit_tests/server/test_todo_snapshot.py
+++ b/tests/unit_tests/server/test_todo_snapshot.py
@@ -21,6 +21,20 @@ def test_format_todos_keeps_completed_and_drops_cancelled():
assert [item["id"] for item in format_todos_for_frontend(items)] == ["a", "b"]
+def test_format_todos_defaults_malformed_status_to_pending():
+ items = [
+ {"id": "dict", "content": "dict status", "status": {}},
+ {"id": "list", "content": "list status", "status": []},
+ {"id": "none", "content": "none status", "status": None},
+ ]
+
+ assert [item["status"] for item in format_todos_for_frontend(items)] == [
+ "pending",
+ "pending",
+ "pending",
+ ]
+
+
def test_load_snapshot_accepts_json_and_rejects_unsafe_session(tmp_path, monkeypatch):
root = tmp_path / "todo"
snapshot = root / "web_session_1" / "todo.json"