Skip to content

Build and run agents you can see, understand and trust.

fix(plan): trigger hooks when recovering historical plans (#1279)

QianCyrus2e4bb109c621
diff --git a/src/agentscope/plan/_plan_notebook.py b/src/agentscope/plan/_plan_notebook.py
index b7277fa22..db6812a3d 100644
--- a/src/agentscope/plan/_plan_notebook.py
+++ b/src/agentscope/plan/_plan_notebook.py
@@ -815,6 +815,7 @@ class PlanNotebook(StateModule):
],
)
self.current_plan = historical_plan
+ await self._trigger_plan_change_hooks()
return res
def list_tools(
diff --git a/tests/plan_test.py b/tests/plan_test.py
index 788696a27..a3610af89 100644
--- a/tests/plan_test.py
+++ b/tests/plan_test.py
@@ -782,3 +782,42 @@ Subtask at index 2:
notebook.remove_plan_change_hook("test")
with self.assertRaises(ValueError):
notebook.remove_plan_change_hook("bad_hook")
+
+ async def test_recover_historical_plan_triggers_hook(self) -> None:
+ """Test recovering a historical plan triggers plan change hooks."""
+ notebook = PlanNotebook()
+ hook_calls: list[str | None] = []
+
+ def hook(_nb: PlanNotebook, plan: Plan | None) -> None:
+ hook_calls.append(plan.name if plan else None)
+
+ notebook.register_plan_change_hook("recover_hook", hook)
+
+ await notebook.create_plan(
+ "P1",
+ "desc",
+ "outcome",
+ [SubTask(name="t1", description="d", expected_outcome="e")],
+ )
+ await notebook.finish_plan("done", "final")
+
+ self.assertEqual(
+ len(hook_calls),
+ 2,
+ )
+ self.assertEqual(
+ hook_calls,
+ ["P1", None],
+ )
+
+ historical_plan = (await notebook.storage.get_plans())[0]
+ await notebook.recover_historical_plan(historical_plan.id)
+
+ self.assertEqual(
+ len(hook_calls),
+ 3,
+ )
+ self.assertEqual(
+ hook_calls[-1],
+ "P1",
+ )