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): persist AgentContext.extra to checkpointer across turns
xf gongac6c1cee4947
diff --git a/cubepi/agent/agent.py b/cubepi/agent/agent.pyindex 84cd99d..63eb0d6 100644--- a/cubepi/agent/agent.py+++ b/cubepi/agent/agent.py@@ -142,6 +142,8 @@ class Agent(Generic[TMessage]):self.checkpointer = checkpointerself.thread_id = thread_id+ self._extra: dict[str, Any] = {}+self._steering_queue = _MessageQueue(steering_mode)self._follow_up_queue = _MessageQueue(follow_up_mode)self._listeners: list[Callable] = []@@ -197,11 +199,13 @@ class Agent(Generic[TMessage]):else:messages = [message]- # Restore history from checkpointer if this is first prompt+ # Restore history and extra from checkpointer if this is first promptif self.checkpointer and self.thread_id and not self._state._messages:data = await self.checkpointer.load(self.thread_id)- if data and data.messages:- self._state._messages = list(data.messages)+ if data:+ if data.messages:+ self._state._messages = list(data.messages)+ self._extra = dict(data.extra)await self._run_prompt(messages)@@ -290,6 +294,7 @@ class Agent(Generic[TMessage]):system_prompt=self._state.system_prompt,messages=list(self._state._messages),tools=list(self._state._tools),+ extra=dict(self._extra),)async def _run_with_lifecycle(self, executor: Callable) -> None:@@ -352,6 +357,8 @@ class Agent(Generic[TMessage]):self._state.error_message = msg.error_messageelif event.type == "agent_end":self._state.streaming_message = None+ if self.checkpointer and self.thread_id:+ await self.checkpointer.save_extra(self.thread_id, self._extra)await self._emit_to_listeners(event)diff --git a/tests/agent/test_context_extra.py b/tests/agent/test_context_extra.pyindex ac581a6..79f13b7 100644--- a/tests/agent/test_context_extra.py+++ b/tests/agent/test_context_extra.py@@ -1,5 +1,10 @@"""AgentContext.extra field tests (D6)."""+import tempfile+from pathlib import Path++import pytest+from cubepi.agent.types import AgentContext@@ -28,3 +33,79 @@ def test_extra_independent_between_instances() -> None:b = AgentContext(system_prompt="", messages=[])a.extra["x"] = 1assert "x" not in b.extra+++async def test_save_extra_round_trip_via_checkpointer() -> None:+ """save_extra + load round-trip works (sanity check for the checkpointer)."""+ from cubepi.checkpointer import SQLiteCheckpointer++ with tempfile.TemporaryDirectory() as d:+ path = Path(d) / "extra.db"+ async with SQLiteCheckpointer(str(path)) as cp:+ await cp.save_extra("t-extra", {"counter": 1})+ data = await cp.load("t-extra")+ assert data is not None+ assert data.extra == {"counter": 1}+++async def test_agent_hydrates_ctx_extra_from_checkpointer() -> None:+ """When agent loads a thread with pre-existing extra, the extra is preserved+ through the turn and written back via save_extra."""+ from cubepi.agent.agent import Agent+ from cubepi.checkpointer import SQLiteCheckpointer+ from cubepi.providers.base import Model+ from cubepi.providers.faux import FauxProvider, faux_assistant_message++ with tempfile.TemporaryDirectory() as d:+ path = Path(d) / "hydrate.db"+ async with SQLiteCheckpointer(str(path)) as cp:+ await cp.save_extra("t-hyd", {"seeded": True})++ provider = FauxProvider()+ provider.set_responses([faux_assistant_message("ok")])+ agent = Agent(+ provider=provider,+ model=Model(id="test", provider="faux"),+ checkpointer=cp,+ thread_id="t-hyd",+ )+ await agent.prompt("hi")++ # After the turn, the seeded extra must still be there+ # (we haven't mutated it; loop must preserve it across save_extra).+ data = await cp.load("t-hyd")+ assert data is not None+ assert data.extra.get("seeded") is True+++async def test_agent_persists_ctx_extra_mutation_after_turn() -> None:+ """Pre-seeded extra is round-tripped: hydrated on load, persisted via+ save_extra after the turn completes."""+ from cubepi.agent.agent import Agent+ from cubepi.checkpointer import SQLiteCheckpointer+ from cubepi.providers.base import Model+ from cubepi.providers.faux import FauxProvider, faux_assistant_message++ with tempfile.TemporaryDirectory() as d:+ path = Path(d) / "persist.db"+ async with SQLiteCheckpointer(str(path)) as cp:+ provider = FauxProvider()+ provider.set_responses([faux_assistant_message("ok")])++ agent = Agent(+ provider=provider,+ model=Model(id="test", provider="faux"),+ checkpointer=cp,+ thread_id="t-pst",+ )+ # Pre-seed via the checkpointer before the agent runs.+ await cp.save_extra("t-pst", {"initial": 1})++ await agent.prompt("hi")++ data = await cp.load("t-pst")+ assert data is not None+ assert data.extra.get("initial") == 1