CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.
Python agent-frameworkagent-harnessagent-runtimeagentic-aiai-agentsai-frameworkanthropicharnessharness-engineeringharness-frameworklanggraphlanggraph-alternativellmmcpopenaipi-agentpi-agent-corepythonpython-agent
feat(agent): add Agent.fork() public API
xf gong594e670ec64c
diff --git a/cubepi/agent/agent.py b/cubepi/agent/agent.pyindex 672b51d..a012ae2 100644--- a/cubepi/agent/agent.py+++ b/cubepi/agent/agent.py@@ -426,6 +426,29 @@ class Agent(Generic[TMessage]):self._state.active_run_id = Nonereturn effective_run_id+ async def fork(+ self,+ src_thread_id: str,+ new_thread_id: str,+ *,+ after_run_id: str,+ metadata: JsonObject | None = None,+ ) -> None:+ if self.checkpointer is None:+ raise RuntimeError("fork requires a checkpointer")+ if not self._run_aware:+ from cubepi.checkpointer.exceptions import CheckpointerError++ raise CheckpointerError(+ "backend does not support fork; missing claim_run / mark_run_complete"+ )+ await self.checkpointer.fork(+ src_thread_id,+ new_thread_id,+ after_run_id=after_run_id,+ metadata=metadata,+ )+async def resume(self) -> None:# Same fail-fast pattern as prompt(): lock.locked() is the atomic gate.if self._run_lock.locked() or self._state.is_streaming:diff --git a/tests/agent/test_agent_fork.py b/tests/agent/test_agent_fork.pynew file mode 100644index 0000000..798dd65--- /dev/null+++ b/tests/agent/test_agent_fork.py@@ -0,0 +1,48 @@+import pytest++from cubepi.agent.agent import Agent+from cubepi.checkpointer.memory import MemoryCheckpointer+from cubepi.providers.base import AssistantMessage, TextContent+from cubepi.providers.faux import FauxProvider+++def _ok_faux() -> FauxProvider:+ p = FauxProvider()+ p.set_responses(+ [AssistantMessage(content=[TextContent(text="ok")], stop_reason="end_turn")]+ )+ return p+++async def test_agent_fork_delegates_to_checkpointer():+ cp = MemoryCheckpointer()+ a = Agent(+ model=_ok_faux().model("faux-model"),+ checkpointer=cp,+ thread_id="src",+ )+ await a.prompt("hello", run_id="R1") # creates src + R1 marker+ await a.fork("src", "dst", after_run_id="R1")+ loaded = await cp.load("dst")+ assert loaded.parent_thread_id == "src"+++async def test_agent_fork_no_checkpointer_raises():+ a = Agent(model=_ok_faux().model("faux-model"))+ with pytest.raises(RuntimeError, match="checkpointer"):+ await a.fork("src", "dst", after_run_id="R1")+++async def test_agent_fork_does_not_mutate_self_thread_id():+ cp = MemoryCheckpointer()+ a = Agent(+ model=_ok_faux().model("faux-model"),+ checkpointer=cp,+ thread_id="src",+ )+ await a.prompt("hello", run_id="R1")+ await a.fork("src", "dst", after_run_id="R1")+ assert a.thread_id == "src"