Skip to content

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

feat(checkpointer): add CheckpointerError runtime hierarchy

xf gong197dc596229a
diff --git a/cubepi/checkpointer/exceptions.py b/cubepi/checkpointer/exceptions.py
index d939adf..2061379 100644
--- a/cubepi/checkpointer/exceptions.py
+++ b/cubepi/checkpointer/exceptions.py
@@ -32,3 +32,70 @@ class CubepiSchemaMismatch(CubepiSchemaError):
super().__init__(msg)
self.expected = expected
self.actual = actual
+
+
+class CheckpointerError(Exception):
+ """Base class for cubepi checkpointer runtime errors.
+
+ Distinct from ``CubepiSchemaError`` (schema-vs-library incompatibility).
+ ``CheckpointerError`` covers runtime operation outcomes — missing
+ thread, lock timeout, run state, etc.
+ """
+
+
+class ThreadNotFoundError(CheckpointerError):
+ pass
+
+
+class ThreadAlreadyExistsError(CheckpointerError):
+ pass
+
+
+class RunNotCompletedError(CheckpointerError):
+ """The cubepi_runs row for (thread_id, run_id) does not exist, or
+ exists with completed_at IS NULL (paused, abandoned, or in flight)."""
+
+
+class RunNotClaimedError(CheckpointerError):
+ """mark_run_complete() called but no cubepi_runs row exists for
+ (thread_id, run_id). Indicates an agent-loop logic bug."""
+
+
+class RunAlreadyClaimedError(CheckpointerError):
+ """claim_run() found an existing row with completed_at IS NULL.
+ Another process is currently running this run_id; retry with a
+ different run_id."""
+
+
+class RunAlreadyCompletedError(CheckpointerError):
+ """claim_run() found an existing row with completed_at IS NOT NULL.
+ Runs are append-only; start a new run with a different run_id.
+
+ NOT raised by mark_run_complete() — that path is idempotent on
+ already-completed rows (spec §3.6.2).
+ """
+
+
+class CheckpointerLockTimeoutError(CheckpointerError):
+ """Backend writer lock not acquired within the configured timeout
+ (SQLite busy_timeout, etc.)."""
+
+
+class CompletionMarkerFailedError(CheckpointerError):
+ """mark_run_complete() failed AFTER the run's final append succeeded.
+ Carries `run_id` so callers using prompt(run_id=None) can recover
+ the cubepi-generated value (spec §3.6.2)."""
+
+ def __init__(
+ self,
+ *,
+ thread_id: str,
+ run_id: str,
+ cause: BaseException,
+ ) -> None:
+ super().__init__(
+ f"mark_run_complete failed for ({thread_id}, {run_id}): {cause}"
+ )
+ self.thread_id = thread_id
+ self.run_id = run_id
+ self.__cause__ = cause
diff --git a/tests/checkpointer/test_exceptions.py b/tests/checkpointer/test_exceptions.py
new file mode 100644
index 0000000..f8a87c2
--- /dev/null
+++ b/tests/checkpointer/test_exceptions.py
@@ -0,0 +1,54 @@
+import pytest
+
+from cubepi.checkpointer.exceptions import (
+ CheckpointerError,
+ CheckpointerLockTimeoutError,
+ CompletionMarkerFailedError,
+ CubepiSchemaError,
+ RunAlreadyClaimedError,
+ RunAlreadyCompletedError,
+ RunNotClaimedError,
+ RunNotCompletedError,
+ ThreadAlreadyExistsError,
+ ThreadNotFoundError,
+)
+
+
+ "exc_cls",
+ [
+ ThreadNotFoundError,
+ ThreadAlreadyExistsError,
+ RunNotCompletedError,
+ RunNotClaimedError,
+ RunAlreadyClaimedError,
+ RunAlreadyCompletedError,
+ CheckpointerLockTimeoutError,
+ ],
+)
+def test_runtime_errors_inherit_checkpointer_error(exc_cls):
+ assert issubclass(exc_cls, CheckpointerError)
+ assert issubclass(exc_cls, Exception)
+
+
+def test_checkpointer_error_separate_from_schema_error():
+ assert not issubclass(CheckpointerError, CubepiSchemaError)
+ assert not issubclass(CubepiSchemaError, CheckpointerError)
+
+
+def test_completion_marker_failed_error_carries_run_id():
+ cause = RuntimeError("db timeout")
+ exc = CompletionMarkerFailedError(
+ thread_id="t1", run_id="r1", cause=cause
+ )
+ assert exc.thread_id == "t1"
+ assert exc.run_id == "r1"
+ assert exc.__cause__ is cause
+ assert "t1" in str(exc) and "r1" in str(exc)
+
+
+def test_runtime_errors_constructable_with_kwargs():
+ e = ThreadNotFoundError("missing thread t1")
+ assert "t1" in str(e)
+ e = RunNotCompletedError("thread=t1 run=r1")
+ assert "r1" in str(e)