Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(solve): keep the remaining plan in view after each step fold

wangkaia40c6469fbdf
diff --git a/deeptutor/capabilities/solve/session.py b/deeptutor/capabilities/solve/session.py
index df4a32219..b7c885398 100644
--- a/deeptutor/capabilities/solve/session.py
+++ b/deeptutor/capabilities/solve/session.py
@@ -4,9 +4,10 @@ Unlike mastery's learning service (disk-backed, multi-session), a solve turn
is one-shot: the session lives only for the turn, keyed by the id the pipeline
injects as ``_solve_session_id``. It holds the model-authored plan, per-step
completion, and the replan budget gate — the deterministic "spine" the chat
-loop drives against. The plan also rides in the conversation (the
-``solve_plan`` tool result), so a follow-up chat turn stays grounded even
-though the session itself does not persist.
+loop drives against. The plan also rides in the conversation — the
+``solve_plan`` tool result, then :meth:`SolveSession.progress_text` as the
+checkpoint state each ``solve_finish_step`` fold keeps — so a follow-up chat
+turn stays grounded even though the session itself does not persist.
The store is a bounded, process-local dict: a solve turn runs in one process,
sessions are small and short-lived, and the bound stops a long-running server
@@ -75,6 +76,24 @@ class SolveSession:
def all_done(self) -> bool:
return bool(self.steps) and all(step.done for step in self.steps)
+ def progress_text(self) -> str:
+ """The plan as it stands — every step marked done / next / todo.
+
+ ``solve_finish_step`` hands this to the loop as checkpoint state, so the
+ plan stays in view after the step's tool chatter is folded away.
+ """
+ nxt = self.next_step()
+ lines = ["[Solve plan]"]
+ for step in self.steps:
+ mark = "done" if step.done else ("next" if step is nxt else "todo")
+ lines.append(f"- {step.id} [{mark}] {step.goal}")
+ lines.append(
+ "All steps are done: write the final answer now."
+ if nxt is None
+ else f"Work {nxt.id} now, then call solve_finish_step."
+ )
+ return "\n".join(lines)
+
_SESSIONS: "OrderedDict[str, SolveSession]" = OrderedDict()
_MAX_SESSIONS = 256
diff --git a/deeptutor/capabilities/solve/tools.py b/deeptutor/capabilities/solve/tools.py
index 91425a4bc..e603b0037 100644
--- a/deeptutor/capabilities/solve/tools.py
+++ b/deeptutor/capabilities/solve/tools.py
@@ -10,7 +10,8 @@ The active session id is injected server-side by the pipeline as
``_solve_session_id``; the model never supplies it. ``solve_finish_step`` emits
a ``_context_checkpoint`` so the loop folds the just-finished step's tool
chatter into a one-line summary — the loop-native equivalent of the old
-pipeline's per-step message reset.
+pipeline's per-step message reset — and carries the current plan as the
+checkpoint's ``state``, which each fold replaces with the latest copy.
"""
from __future__ import annotations
@@ -194,12 +195,16 @@ class SolveFinishStepTool(BaseTool):
),
}
# The checkpoint summary persists this step's outcome while the loop
- # folds its intermediate tool messages away (see AgentLoop).
+ # folds its intermediate tool messages away (see AgentLoop). The fold
+ # also drops this result and the solve_plan one, so the plan rides
+ # along as checkpoint state: the loop keeps only its latest copy.
checkpoint = f"[{step.id}] {step.goal} — done. {summary}".strip()
return _json_result(
payload,
meta_key="solve_finish_step",
- extra_meta={"_context_checkpoint": {"summary": checkpoint}},
+ extra_meta={
+ "_context_checkpoint": {"summary": checkpoint, "state": session.progress_text()}
+ },
)
diff --git a/tests/agents/chat/test_agent_loop.py b/tests/agents/chat/test_agent_loop.py
index dbd56ac12..de7e10051 100644
--- a/tests/agents/chat/test_agent_loop.py
+++ b/tests/agents/chat/test_agent_loop.py
@@ -2908,6 +2908,61 @@ async def test_context_checkpoint_state_replaces_instead_of_stacking(
assert not any(m.get("role") == "tool" for m in third_round)
+async def test_solve_checkpoint_fold_keeps_remaining_plan(monkeypatch: pytest.MonkeyPatch) -> None:
+ from deeptutor.capabilities.solve.tools import SolveFinishStepTool, SolvePlanTool
+
+ tools = {"solve_plan": SolvePlanTool(), "solve_finish_step": SolveFinishStepTool()}
+
+ class _SolveRegistry(_Registry):
+ def build_openai_schemas(self, _enabled):
+ return [tool.get_definition().to_openai_schema() for tool in tools.values()]
+
+ async def execute(self, name: str, **kwargs):
+ return await tools[name].execute(**kwargs)
+
+ def _call(call_id: str, name: str, args: dict[str, Any]) -> SimpleNamespace:
+ return _llm_chunk(tool_calls=[{"id": call_id, "name": name, "arguments": json.dumps(args)}])
+
+ goals = ["Factor x^2-5x+6", "Find both roots", "Verify by substitution"]
+ client = _ScriptedChatClient(
+ [
+ [
+ _call(
+ "c1",
+ "solve_plan",
+ {"analysis": "factor", "steps": [{"goal": g} for g in goals]},
+ )
+ ],
+ [_call("c2", "solve_finish_step", {"step_id": "S1", "summary": "(x-2)(x-3)"})],
+ [_llm_chunk(content="x = 2 or x = 3")],
+ ]
+ )
+ pipeline = AgenticChatPipeline(language="en")
+ pipeline.registry = _SolveRegistry()
+ monkeypatch.setattr(pipeline, "_compose_enabled_tools", lambda _context: list(tools))
+ monkeypatch.setattr(pipeline, "_build_openai_client", lambda: client)
+ context = UnifiedContext(
+ session_id="s1",
+ user_message="Solve x^2 - 5x + 6 = 0",
+ metadata={"solve_mode": True, "solve_session_id": "fold-keeps-plan"},
+ )
+
+ await _run(pipeline, context)
+
+ # The step's tool chatter is folded away; the remaining plan must stay in view.
+ after_fold = client.calls[2]["messages"]
+ assert not any(m.get("role") == "tool" for m in after_fold)
+ assert after_fold[-2]["content"].startswith("[Context checkpoint]\n[S1] Factor x^2-5x+6")
+ plan = after_fold[-1]["content"]
+ assert "- S1 [done] Factor x^2-5x+6" in plan
+ assert "- S2 [next] Find both roots" in plan
+ assert "- S3 [todo] Verify by substitution" in plan
+ # A follow-up turn replays the persisted model turn, so it carries the plan too.
+ model_turn = json.dumps(context.runtime.model_turn, ensure_ascii=False)
+ assert "Find both roots" in model_turn and "Verify by substitution" in model_turn
+
+
@pytest.mark.asyncio
async def test_ask_user_available_every_round(monkeypatch: pytest.MonkeyPatch) -> None:
"""The single loop offers the full tool belt — including ask_user — on
diff --git a/tests/capabilities/test_solve_capability.py b/tests/capabilities/test_solve_capability.py
index 10e19ce77..280366005 100644
--- a/tests/capabilities/test_solve_capability.py
+++ b/tests/capabilities/test_solve_capability.py
@@ -126,3 +126,45 @@ def test_session_replan_resets_steps_and_counts() -> None:
assert session.replans == 1
assert session.next_step().goal == "g2"
assert session.all_done() is False
+
+
+def test_session_progress_text_marks_each_step() -> None:
+ session = SolveSession(session_id="x")
+ session.set_plan("a", [("S1", "factor"), ("S2", "solve roots"), ("S3", "verify")])
+ session.mark_done("S1", "(x-2)(x-3)")
+
+ lines = session.progress_text().splitlines()
+
+ assert "- S1 [done] factor" in lines
+ assert "- S2 [next] solve roots" in lines
+ assert "- S3 [todo] verify" in lines
+ assert "S2" in lines[-1] and "solve_finish_step" in lines[-1]
+
+
+def test_session_progress_text_after_last_step_and_replan() -> None:
+ session = SolveSession(session_id="x", max_replans=1)
+ session.set_plan("a", [("S1", "old plan")])
+ assert session.replan("redo", [("S1", "new first"), ("S2", "new second")]) is True
+ assert "old plan" not in session.progress_text()
+ assert "- S1 [next] new first" in session.progress_text()
+
+ session.mark_done("S1", "ok")
+ session.mark_done("S2", "ok")
+ text = session.progress_text()
+ assert "[next]" not in text and "[todo]" not in text
+ assert "final answer" in text.splitlines()[-1]
+
+
+async def test_finish_step_checkpoint_carries_plan_state() -> None:
+ sid = "test-plan-state"
+ await SolvePlanTool().execute(
+ _solve_session_id=sid, analysis="a", steps=[{"goal": "factor"}, {"goal": "solve roots"}]
+ )
+
+ fin = await SolveFinishStepTool().execute(_solve_session_id=sid, step_id="S1", summary="done")
+
+ checkpoint = fin.metadata["_context_checkpoint"]
+ assert checkpoint["summary"].startswith("[S1] factor")
+ assert checkpoint["state"] == get_session(sid).progress_text()
+ assert "- S2 [next] solve roots" in checkpoint["state"]