Skip to content

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

test(hitl): cover answer ledger replay paths

xf gong2d24d66cd407
diff --git a/tests/checkpointer/test_hitl_answer_ledger_unit.py b/tests/checkpointer/test_hitl_answer_ledger_unit.py
new file mode 100644
index 0000000..fb92c75
--- /dev/null
+++ b/tests/checkpointer/test_hitl_answer_ledger_unit.py
@@ -0,0 +1,169 @@
+from __future__ import annotations
+
+import json
+
+import pytest
+
+from cubepi.checkpointer.mysql.checkpointer import MySQLCheckpointer
+from cubepi.checkpointer.postgres.checkpointer import PostgresCheckpointer
+
+
+class _AsyncCtx:
+ def __init__(self, value):
+ self._value = value
+
+ async def __aenter__(self):
+ return self._value
+
+ async def __aexit__(self, exc_type, exc, tb):
+ return False
+
+
+class _PgConn:
+ def __init__(self, row=None):
+ self.row = row
+ self.exec_calls: list[tuple[str, tuple]] = []
+
+ async def execute(self, sql, *args):
+ self.exec_calls.append((sql, args))
+
+ async def fetchrow(self, sql, *args):
+ self.exec_calls.append((sql, args))
+ return self.row
+
+ def transaction(self):
+ return _AsyncCtx(None)
+
+
+class _PgPool:
+ def __init__(self, conn: _PgConn):
+ self.conn = conn
+
+ def acquire(self):
+ return _AsyncCtx(self.conn)
+
+
+class _MyCursor:
+ def __init__(self, conn: "_MyConn", row=None):
+ self.conn = conn
+ self.row = row
+
+ async def __aenter__(self):
+ return self
+
+ async def __aexit__(self, exc_type, exc, tb):
+ return False
+
+ async def execute(self, sql, params):
+ self.conn.exec_calls.append((sql, params))
+
+ async def fetchone(self):
+ return self.row
+
+
+class _MyConn:
+ def __init__(self, row=None):
+ self.row = row
+ self.exec_calls: list[tuple[str, tuple]] = []
+ self.begin_calls = 0
+
+ async def begin(self):
+ self.begin_calls += 1
+
+ def cursor(self):
+ return _AsyncCtx(_MyCursor(self, self.row))
+
+ async def commit(self):
+ return None
+
+ async def rollback(self):
+ return None
+
+
+class _MyPool:
+ def __init__(self, conn: _MyConn):
+ self.conn = conn
+
+ def acquire(self):
+ return _AsyncCtx(self.conn)
+
+
+async def test_postgres_hitl_answer_ledger_crud() -> None:
+ cp = PostgresCheckpointer("postgresql://unused")
+ conn = _PgConn(row={"answer": {"decision": "approve"}})
+ cp._pool = _PgPool(conn)
+
+ await cp.save_hitl_answer("t-1", "q-1", {"decision": "approve"}, run_id="r-1")
+ assert await cp.load_hitl_answer("t-1", "q-1", run_id="r-1") == {
+ "decision": "approve"
+ }
+ await cp.clear_hitl_answers("t-1", ["q-1"], run_id="r-1")
+ await cp.clear_hitl_answers("t-1", run_id="r-1")
+
+ sqls = [sql for sql, _args in conn.exec_calls]
+ assert any("cubepi_hitl_answers" in sql for sql in sqls)
+ assert any("ON CONFLICT (thread_id, run_id, question_id)" in sql for sql in sqls)
+ assert any("DELETE FROM cubepi_hitl_answers" in sql for sql in sqls)
+
+
+async def test_postgres_hitl_answer_ledger_load_from_json_string() -> None:
+ cp = PostgresCheckpointer("postgresql://unused")
+ conn = _PgConn(row={"answer": json.dumps({"decision": "deny"})})
+ cp._pool = _PgPool(conn)
+
+ assert await cp.load_hitl_answer("t-1", "q-1", run_id="r-1") == {
+ "decision": "deny"
+ }
+ assert await cp.load_hitl_answer("t-1", "q-missing", run_id="r-1") == {
+ "decision": "deny"
+ }
+
+
+async def test_postgres_hitl_answer_ledger_returns_none_when_missing() -> None:
+ cp = PostgresCheckpointer("postgresql://unused")
+ conn = _PgConn(row=None)
+ cp._pool = _PgPool(conn)
+
+ assert await cp.load_hitl_answer("t-1", "q-1", run_id="r-1") is None
+
+
+async def test_mysql_hitl_answer_ledger_crud() -> None:
+ cp = MySQLCheckpointer("mysql://unused")
+ conn = _MyConn(row=({"decision": "approve"},))
+ cp._pool = _MyPool(conn)
+
+ await cp.save_hitl_answer("t-1", "q-1", {"decision": "approve"}, run_id="r-1")
+ assert await cp.load_hitl_answer("t-1", "q-1", run_id="r-1") == {
+ "decision": "approve"
+ }
+ await cp.clear_hitl_answers("t-1", ["q-1"], run_id="r-1")
+ await cp.clear_hitl_answers("t-1", run_id="r-1")
+
+ sqls = [sql for sql, _params in conn.exec_calls]
+ assert any("cubepi_hitl_answers" in sql for sql in sqls)
+ assert any("ON DUPLICATE KEY UPDATE" in sql for sql in sqls)
+ assert any("DELETE FROM cubepi_hitl_answers" in sql for sql in sqls)
+
+
+async def test_mysql_hitl_answer_ledger_load_from_json_string() -> None:
+ cp = MySQLCheckpointer("mysql://unused")
+ conn = _MyConn(row=("{}".format(json.dumps({"decision": "deny"})),))
+ cp._pool = _MyPool(conn)
+
+ assert await cp.load_hitl_answer("t-1", "q-1", run_id="r-1") == {
+ "decision": "deny"
+ }
+
+
+async def test_mysql_hitl_answer_ledger_returns_none_when_missing() -> None:
+ cp = MySQLCheckpointer("mysql://unused")
+ conn = _MyConn(row=None)
+ cp._pool = _MyPool(conn)
+
+ assert await cp.load_hitl_answer("t-1", "q-1", run_id="r-1") is None
diff --git a/tests/hitl/test_checkpointed_channel.py b/tests/hitl/test_checkpointed_channel.py
index 2532c99..d48181b 100644
--- a/tests/hitl/test_checkpointed_channel.py
+++ b/tests/hitl/test_checkpointed_channel.py
@@ -101,3 +101,15 @@ def test_checkpointed_requires_hitl_methods_on_checkpointer():
with pytest.raises(HitlError):
CheckpointedChannel(checkpointer=_BareCheckpointer(), thread_id="t-1")
+
+
+def test_checkpointed_requires_hitl_answer_ledger_methods():
+ class _PartialCheckpointer:
+ async def save_pending_request(self, thread_id, request, *, run_id=None):
+ return None
+
+ async def load_pending_request(self, thread_id):
+ return None
+
+ with pytest.raises(HitlError, match="save_hitl_answer"):
+ CheckpointedChannel(checkpointer=_PartialCheckpointer(), thread_id="t-1")
diff --git a/tests/hitl/test_checkpointer_pending_request.py b/tests/hitl/test_checkpointer_pending_request.py
index 27e29d8..3179abd 100644
--- a/tests/hitl/test_checkpointer_pending_request.py
+++ b/tests/hitl/test_checkpointer_pending_request.py
@@ -114,3 +114,11 @@ async def test_sqlite_hitl_answer_ledger_crud(sqlite_cp):
assert await sqlite_cp.load_hitl_answer("t-1", "q-1", run_id="r-2") == {
"decision": "edit"
}
+
+
+async def test_sqlite_hitl_answer_ledger_clear_empty_iterable(sqlite_cp):
+ await sqlite_cp.save_hitl_answer("t-1", "q-1", {"decision": "approve"})
+ await sqlite_cp.clear_hitl_answers("t-1", [], run_id=None)
+ assert await sqlite_cp.load_hitl_answer("t-1", "q-1") == {
+ "decision": "approve"
+ }
diff --git a/tests/hitl/test_edge_cases.py b/tests/hitl/test_edge_cases.py
index 4c918dd..af7ec07 100644
--- a/tests/hitl/test_edge_cases.py
+++ b/tests/hitl/test_edge_cases.py
@@ -114,6 +114,31 @@ def test_respond_raises_when_checkpointer_lacks_hitl():
)
+def test_respond_raises_when_checkpointer_lacks_hitl_answer_store():
+ class _PartialCP: # pragma: no cover — minimal stub
+ async def load(self, tid):
+ return None
+
+ async def load_pending(self, tid):
+ return (
+ HitlRequest(
+ question_id="tc-1",
+ thread_id=tid,
+ payload=ApproveRequest(
+ tool_name="bash", tool_call_id="tc-1", args={}
+ ),
+ created_at=0.0,
+ ),
+ "R1",
+ )
+
+ agent = _agent(channel=InMemoryChannel(), checkpointer=_PartialCP(), thread_id="t-1")
+ with pytest.raises(HitlError, match="save_hitl_answer"):
+ asyncio.get_event_loop().run_until_complete(
+ agent.respond(answer=ApproveAnswer(decision="approve"))
+ )
+
+
def test_respond_raises_no_pending():
cp = MemoryCheckpointer()
ch = InMemoryChannel()
diff --git a/tests/hitl/test_in_memory_channel.py b/tests/hitl/test_in_memory_channel.py
index eda0085..04cd840 100644
--- a/tests/hitl/test_in_memory_channel.py
+++ b/tests/hitl/test_in_memory_channel.py
@@ -10,7 +10,11 @@ from cubepi.hitl import (
HitlTimedOut,
Question,
)
-from cubepi.hitl.channel import InMemoryChannel
+from cubepi.hitl.channel import InMemoryChannel, _BaseChannel
+
+
+class _BareChannel(_BaseChannel):
+ pass
async def test_ask_resolves_via_answer():
@@ -229,6 +233,61 @@ async def test_answer_ledger_short_circuits_replayed_approve():
assert ch.pending is None
+async def test_answer_ledger_replays_raw_dict_as_approve_answer():
+ ch = InMemoryChannel()
+
+ async def host():
+ while ch.pending is None:
+ await asyncio.sleep(0)
+ await ch.answer(ch.pending.question_id, {"decision": "approve"})
+
+ asyncio.create_task(host())
+ first = await ch.approve(tool_name="bash", tool_call_id="tc-8", args={})
+ assert first == {"decision": "approve"}
+
+ replayed = await ch.approve(
+ tool_name="bash", tool_call_id="tc-8", args={}, timeout=0.01
+ )
+ assert replayed.decision == "approve"
+
+
+async def test_answer_ledger_replay_emits_hitl_answer_event():
+ from cubepi.agent.types import HitlAnswerEvent
+
+ emitted: list[object] = []
+ ch = InMemoryChannel()
+ ch._bind_emit(lambda e: emitted.append(e))
+
+ async def host():
+ while ch.pending is None:
+ await asyncio.sleep(0)
+ await ch.answer(ch.pending.question_id, ApproveAnswer(decision="approve"))
+
+ asyncio.create_task(host())
+ await ch.approve(tool_name="bash", tool_call_id="tc-9", args={})
+ emitted.clear()
+
+ replayed = await ch.approve(
+ tool_name="bash", tool_call_id="tc-9", args={}, timeout=0.01
+ )
+ assert replayed.decision == "approve"
+ answer_events = [e for e in emitted if isinstance(e, HitlAnswerEvent)]
+ assert len(answer_events) == 1
+ assert answer_events[0].question_id == "tc-9"
+
+
+async def test_base_channel_ledger_hooks_default_to_noop():
+ ch = _BareChannel()
+ assert await ch._load_answer("q-1") is None
+ assert await ch._save_answer("q-1", {"answer": True}) is None
+
+
+async def test_cancel_with_stale_qid_raises():
+ ch = InMemoryChannel()
+ with pytest.raises(HitlStaleAnswer):
+ await ch.cancel("not-pending")
+
+
async def test_resume_short_circuit_emits_hitl_answer_event():
"""Resume short-circuit must emit HitlAnswerEvent so subscribers
(e.g. IM outbound tailers) learn the question was answered."""