Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

feat(agent): claim_run pre-flight + legacy degraded-mode fallback

xf gong0efb0b0294b7
diff --git a/cubepi/agent/agent.py b/cubepi/agent/agent.py
index a3bbb9b..a86d5f8 100644
--- a/cubepi/agent/agent.py
+++ b/cubepi/agent/agent.py
@@ -210,6 +210,11 @@ class Agent(Generic[TMessage]):
self.tool_execution = tool_execution
self.checkpointer = checkpointer
self.thread_id = thread_id
+ self._run_aware = (
+ self.checkpointer is not None
+ and hasattr(self.checkpointer, "claim_run")
+ and hasattr(self.checkpointer, "mark_run_complete")
+ )
self._channel = channel
# _bind_emit is a _BaseChannel internal, not part of the HitlChannel
# protocol. Third-party channels that only implement the public
@@ -340,6 +345,9 @@ class Agent(Generic[TMessage]):
# mutation so the supplied run_id remains reusable (Task 25 will add
# claim_run, which must not be called for a rejected prompt).
self._validate_input_run_ids(message, effective_run_id)
+ if self._run_aware and self.thread_id is not None:
+ assert self.checkpointer is not None # narrowed by _run_aware
+ await self.checkpointer.claim_run(self.thread_id, effective_run_id)
self._state.active_run_id = effective_run_id
try:
async with self._run_lock:
diff --git a/tests/agent/test_agent_claim_run.py b/tests/agent/test_agent_claim_run.py
new file mode 100644
index 0000000..0c240d0
--- /dev/null
+++ b/tests/agent/test_agent_claim_run.py
@@ -0,0 +1,83 @@
+import pytest
+
+from cubepi.agent.agent import Agent
+from cubepi.checkpointer.exceptions import (
+ RunAlreadyClaimedError,
+ RunAlreadyCompletedError,
+)
+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
+
+
+def _agent(**kw):
+ return Agent(model=_ok_faux().model("faux-model"), **kw)
+
+
+async def test_prompt_calls_claim_run_before_append():
+ cp = MemoryCheckpointer()
+ a = _agent(checkpointer=cp, thread_id="t")
+ await a.prompt("hi", run_id="R1")
+ # Run row exists.
+ assert "R1" in cp._runs["t"]
+
+
+async def test_prompt_rejects_already_claimed_run_id():
+ cp = MemoryCheckpointer()
+ await cp.claim_run("t", "R1")
+ a = _agent(checkpointer=cp, thread_id="t")
+ with pytest.raises(RunAlreadyClaimedError):
+ await a.prompt("hi", run_id="R1")
+
+
+async def test_prompt_rejects_already_completed_run_id():
+ cp = MemoryCheckpointer()
+ await cp.claim_run("t", "R1")
+ await cp.mark_run_complete("t", "R1")
+ a = _agent(checkpointer=cp, thread_id="t")
+ with pytest.raises(RunAlreadyCompletedError):
+ await a.prompt("hi", run_id="R1")
+
+
+async def test_no_checkpointer_no_claim_call():
+ a = _agent() # checkpointer=None
+ got = await a.prompt("hi")
+ assert isinstance(got, str) # works fine; no claim attempted
+
+
+async def test_degraded_mode_v3_only_checkpointer():
+ class _V3Only:
+ async def load(self, thread_id):
+ return None
+
+ async def append(self, thread_id, msgs):
+ pass
+
+ async def save_extra(self, thread_id, extra):
+ pass
+
+ async def save_pending_request(self, thread_id, req, *, run_id=None):
+ pass
+
+ async def load_pending_request(self, thread_id):
+ return None
+
+ # No claim_run / mark_run_complete.
+
+ a = _agent(checkpointer=_V3Only(), thread_id="t")
+ # Vanilla prompt works (degraded mode skips claim).
+ got = await a.prompt("hi")
+ assert isinstance(got, str)