Skip to content

The agent that grows with you

Merge origin/main — resolve todo-state seam onto the todo_list rename (accept legacy alias)

Teknium7b3c9f86ed18
diff --cc tui_gateway/server.py
index 3d6889173f3,887994458a8..5ba4b8fbed9
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@@ -7603,6 -7604,95 +7604,95 @@@ def _tool_summary(name: str, result: st
return f"{text}{suffix}" if text else None
+ def _normalize_todo_state(value: object) -> dict | None:
+ """Return a client-safe full todo snapshot or ``None`` when malformed."""
+ if not isinstance(value, dict) or not isinstance(value.get("todos"), list):
+ return None
+ try:
+ revision = max(0, int(value.get("revision") or 0))
+ except (TypeError, ValueError):
+ return None
+ return {"todos": list(value["todos"]), "revision": revision}
+
+
+ def _session_todo_state(session: dict) -> dict | None:
+ """Return the newest live/cached todo snapshot for a runtime session."""
+ cached = _normalize_todo_state(session.get("todo_state"))
+ live = None
+ agent = session.get("agent")
+ store = getattr(agent, "_todo_store", None)
+ snapshot = getattr(store, "snapshot", None)
+ if callable(snapshot):
+ try:
+ live = _normalize_todo_state(snapshot())
+ except Exception:
+ logger.debug("failed to read live todo state", exc_info=True)
+
+ if live is not None and (
+ cached is None or live["revision"] >= cached["revision"]
+ ):
+ cached = live
+ if cached is not None:
+ session["todo_state"] = cached
+ return cached
+
+
+ def _attach_todo_state(payload: dict, session: dict) -> dict:
+ """Attach the authoritative todo snapshot to a session response."""
+ state = _session_todo_state(session)
+ if state is not None:
+ payload["todo_state"] = state
+ return payload
+
+
+ def _todo_state_from_history(history) -> dict | None:
+ """Derive the latest todo snapshot from an already-loaded transcript.
+
+ Used by resume paths that answer before an AIAgent (and its live
+ TodoStore) exists. The canonical todo tool results already persist in
+ conversation history as ordinary tool messages, so the latest one paired
+ with an assistant ``todo`` tool call IS the durable snapshot — no side
+ table and no extra transcript read (each resume path passes the history
+ it already loaded).
+ """
+ if not isinstance(history, list) or not history:
+ return None
+ try:
+ from tools.todo_tool import MAX_TODO_RESULT_CHARS
+
+ todo_call_ids: set[str] = set()
+ for msg in history:
+ if not isinstance(msg, dict):
+ continue
+ for call in msg.get("tool_calls") or []:
- if (call.get("function") or {}).get("name") == "todo":
++ if (call.get("function") or {}).get("name") in ("todo_list", "todo"):
+ cid = call.get("id")
+ if cid:
+ todo_call_ids.add(cid)
+ if not todo_call_ids:
+ return None
+ for msg in reversed(history):
+ if not isinstance(msg, dict) or msg.get("role") != "tool":
+ continue
+ if msg.get("tool_call_id") not in todo_call_ids:
+ continue
+ content = msg.get("content", "")
+ if (
+ not isinstance(content, str)
+ or len(content) > MAX_TODO_RESULT_CHARS
+ or '"todos"' not in content
+ ):
+ continue
+ try:
+ return _normalize_todo_state(json.loads(content))
+ except Exception:
+ continue
+ return None
+ except Exception:
+ logger.debug("failed to derive todo state from history", exc_info=True)
+ return None
+
+
def _on_tool_start(sid: str, tool_call_id: str, name: str, args: dict):
session = _sessions.get(sid)
if session is not None:
@@@ -7659,13 -7749,15 +7749,15 @@@ def _on_tool_complete(sid: str, tool_ca
result_text = _tool_result_text(result)
if result_text:
payload["result_text"] = result_text
- if name == "todo_list":
- try:
- data = json.loads(result)
- if isinstance(data, dict) and isinstance(data.get("todos"), list):
- payload["todos"] = data.get("todos")
- except Exception:
- pass
+ todo_state = None
- if name == "todo":
++ if name in ("todo_list", "todo"): # legacy alias: pre-rename replays
+ todo_state = _normalize_todo_state(payload.get("result"))
+ if todo_state is not None:
+ payload.update(todo_state)
+ if session is not None:
+ cached = _normalize_todo_state(session.get("todo_state"))
+ if cached is None or todo_state["revision"] >= cached["revision"]:
+ session["todo_state"] = todo_state
try:
from agent.display import render_edit_diff_with_delta
@@@ -7680,8 -7772,18 +7772,18 @@@
payload["inline_diff"] = "\n".join(rendered)
except Exception:
pass
- if _tool_progress_enabled(sid) or payload.get("inline_diff") or _tool_lifecycle_required_for_ui(name):
+ if (
+ _tool_progress_enabled(sid)
+ or payload.get("inline_diff")
+ or _tool_lifecycle_required_for_ui(name)
- or name == "todo"
++ or name in ("todo_list", "todo")
+ ):
_emit("tool.complete", sid, payload)
+ # Task state is application data, not optional tool-progress chrome. A
+ # dedicated full-snapshot event lets every client reconcile immediately
+ # without interpreting provider text or partial merge arguments.
+ if todo_state is not None:
+ _emit("todo.updated", sid, todo_state)
def _on_tool_progress(