Skip to content

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

fix(hitl): keep run_id kwarg out of save_pending_request when channel has none

xf gong2e632588a615
diff --git a/cubepi/hitl/channel.py b/cubepi/hitl/channel.py
index 5cf26c1..d0f52f1 100644
--- a/cubepi/hitl/channel.py
+++ b/cubepi/hitl/channel.py
@@ -531,9 +531,18 @@ class CheckpointedChannel(_BaseChannel):
"Use ApprovalPolicyMiddleware or ask_user_tool, or pass "
"allow_inside_custom_tool=True to opt in."
)
- await self._checkpointer.save_pending_request(
- self._thread_id, req, run_id=self._run_id
- )
+ # Only pass run_id when the caller actually set one. Third-party
+ # checkpointers that still implement the v2 contract
+ # `save_pending_request(thread_id, request)` would raise TypeError
+ # if we always passed the new kwarg. With this guard, legacy
+ # backends keep working as long as no host opts into run_id
+ # persistence on them.
+ if self._run_id is not None:
+ await self._checkpointer.save_pending_request(
+ self._thread_id, req, run_id=self._run_id
+ )
+ else:
+ await self._checkpointer.save_pending_request(self._thread_id, req)
await super()._on_pending_set(req)
async def _on_pending_cleared(
diff --git a/tests/hitl/test_checkpointed_channel_run_id.py b/tests/hitl/test_checkpointed_channel_run_id.py
index c3c6e4a..dc8fa9a 100644
--- a/tests/hitl/test_checkpointed_channel_run_id.py
+++ b/tests/hitl/test_checkpointed_channel_run_id.py
@@ -84,6 +84,44 @@ async def test_no_run_id_kwarg_leaves_run_id_none():
await ch.approve(tool_name="bash", tool_call_id="tc-2", args={})
+async def test_legacy_checkpointer_works_without_run_id_kwarg():
+ """A third-party checkpointer that still implements the v2 contract
+ ``save_pending_request(thread_id, request)`` (no run_id kwarg) must
+ keep working when the host constructs CheckpointedChannel WITHOUT
+ a run_id — the channel must not unconditionally pass the new kwarg.
+ """
+ from cubepi.hitl.types import HitlRequest
+
+ class LegacyCheckpointer:
+ """Mimics a v2-era third-party checkpointer. save_pending_request
+ intentionally lacks the run_id kwarg; calling it with run_id=...
+ would raise TypeError."""
+
+ def __init__(self) -> None:
+ self._pending: dict[str, HitlRequest | None] = {}
+
+ async def save_pending_request(
+ self, thread_id: str, request
+ ): # NO run_id kwarg
+ self._pending[thread_id] = request
+
+ async def load_pending_request(self, thread_id: str):
+ return self._pending.get(thread_id)
+
+ cp = LegacyCheckpointer()
+ # No run_id at construction → channel must call legacy two-arg signature.
+ ch = CheckpointedChannel(checkpointer=cp, thread_id="t-legacy")
+
+ async def host():
+ while await cp.load_pending_request("t-legacy") is None:
+ await asyncio.sleep(0)
+ await ch.answer(ch.pending.question_id, ApproveAnswer(decision="approve"))
+
+ asyncio.create_task(host())
+ answer = await ch.approve(tool_name="bash", tool_call_id="tc-legacy", args={})
+ assert answer.decision == "approve"
+
+
async def test_run_id_cleared_on_detach_stays_persisted():
"""On detach (HitlDetached) the pending row must remain — and so must run_id."""
cp = MemoryCheckpointer()