Python: support checkpoints for workflow orchestrations and sub-workflows (#863)

* Magentic checkpoint wip

* Magentic checkpoint updates

* Support checkpointing for magentic orchestration.

* Checkpointing for sub-workflows

* Use _execute_contexts instead of _pending_requests

* Remove unnecessary type ignores

* Support checkpoints for other orchestrations, refactor some code.

* Regenerate uv.lock
This commit is contained in:
Evan Mattson
2025-09-26 11:21:06 +09:00
committed by GitHub
Unverified
parent 4b743ea62a
commit 2cd7ab342b
22 changed files with 2005 additions and 277 deletions
@@ -21,6 +21,7 @@ from agent_framework import (
WorkflowStatusEvent,
handler,
)
from agent_framework._workflow._checkpoint import InMemoryCheckpointStorage
class _EchoAgent(BaseAgent):
@@ -114,3 +115,46 @@ async def test_sequential_with_custom_executor_summary() -> None:
assert msgs[0].role == Role.USER
assert msgs[1].role == Role.ASSISTANT and "A1 reply" in msgs[1].text
assert msgs[2].role == Role.ASSISTANT and msgs[2].text.startswith("Summary of users:")
@pytest.mark.asyncio
async def test_sequential_checkpoint_resume_round_trip() -> None:
storage = InMemoryCheckpointStorage()
initial_agents = (_EchoAgent(id="agent1", name="A1"), _EchoAgent(id="agent2", name="A2"))
wf = SequentialBuilder().participants(list(initial_agents)).with_checkpointing(storage).build()
baseline_output: list[ChatMessage] | None = None
async for ev in wf.run_stream("checkpoint sequential"):
if isinstance(ev, WorkflowOutputEvent):
baseline_output = ev.data # type: ignore[assignment]
if isinstance(ev, WorkflowStatusEvent) and ev.state == WorkflowRunState.IDLE:
break
assert baseline_output is not None
checkpoints = await storage.list_checkpoints()
assert checkpoints
checkpoints.sort(key=lambda cp: cp.timestamp)
resume_checkpoint = next(
(cp for cp in checkpoints if (cp.metadata or {}).get("checkpoint_type") == "superstep"),
checkpoints[-1],
)
resumed_agents = (_EchoAgent(id="agent1", name="A1"), _EchoAgent(id="agent2", name="A2"))
wf_resume = SequentialBuilder().participants(list(resumed_agents)).with_checkpointing(storage).build()
resumed_output: list[ChatMessage] | None = None
async for ev in wf_resume.run_stream_from_checkpoint(resume_checkpoint.checkpoint_id):
if isinstance(ev, WorkflowOutputEvent):
resumed_output = ev.data # type: ignore[assignment]
if isinstance(ev, WorkflowStatusEvent) and ev.state in (
WorkflowRunState.IDLE,
WorkflowRunState.IDLE_WITH_PENDING_REQUESTS,
):
break
assert resumed_output is not None
assert [m.role for m in resumed_output] == [m.role for m in baseline_output]
assert [m.text for m in resumed_output] == [m.text for m in baseline_output]