Skip to content

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

docs(checkpointer): add Postgres/MySQL host-integration READMEs

xf gongd29345471bfd
diff --git a/cubepi/checkpointer/mysql/README.md b/cubepi/checkpointer/mysql/README.md
new file mode 100644
index 0000000..dd03854
--- /dev/null
+++ b/cubepi/checkpointer/mysql/README.md
@@ -0,0 +1,155 @@
+# MySQL checkpointer — host integration
+
+`MySQLCheckpointer` (aiomysql) is the MySQL counterpart of the Postgres
+checkpointer — same model, MySQL-compatible types, and `KEY`
+partitioning on `thread_id`. **It never issues DDL at runtime** — your
+application owns the schema lifecycle through Alembic, and cubepi only
+reads and writes rows. At startup `setup()` runs verification probes and
+raises `CubepiSchemaUninitialized` / `CubepiSchemaMismatch` if the
+expected migrations haven't been applied.
+
+Install with the extra:
+
+```bash
+pip install 'cubepi[mysql]'
+```
+
+## Using the checkpointer
+
+```python
+from cubepi.checkpointer.mysql import MySQLCheckpointer
+
+async with MySQLCheckpointer("mysql://user:pass@host:3306/db") as cp:
+ await cp.setup() # verifies schema; never creates tables
+ agent = Agent(..., checkpointer=cp, thread_id="user-42")
+```
+
+Constructor: `MySQLCheckpointer(dsn, *, min_size=1, max_size=10,
+schema_version_check=True)`. The DSN accepts `mysql://`,
+`mysql+aiomysql://`, or `mysql+pymysql://` and is normalized to the
+aiomysql form.
+
+## What the public API exposes
+
+Everything a host migration needs is re-exported from
+`cubepi.checkpointer.mysql`:
+
+| Symbol | Use |
+|---|---|
+| `cubepi_metadata` | SQLAlchemy `MetaData` — set as Alembic `target_metadata` |
+| `EXPECTED_SCHEMA_VERSION` | The version cubepi verifies at startup (currently `2`) |
+| `messages_partition_clause()` | The `PARTITION BY KEY (...)` clause for `cubepi_messages` |
+| `add_pending_request_column_op()` | v1→v2 column add |
+| `write_schema_version_op()` | Sets `cubepi_schema_version` (returns two `;`-separated statements) |
+
+The schema lives in `models.py`; the SQL helpers in `alembic_helpers.py`.
+
+## Tables
+
+| Table | Purpose |
+|---|---|
+| `cubepi_threads` | One row per thread; holds `pending_request JSON` (HITL, v2+) |
+| `cubepi_runs` | One row per run; status tracking |
+| `cubepi_messages` | Append-only message log — `PARTITION BY KEY (thread_id)`, 64 partitions |
+| `cubepi_schema_version` | Single-row version marker verified at startup |
+
+MySQL declares all partitions inline in the `CREATE TABLE` (unlike
+Postgres, which uses one child table per partition).
+
+## Wiring Alembic (`env.py`)
+
+```python
+# alembic/env.py
+from cubepi.checkpointer.mysql import cubepi_metadata
+
+target_metadata = cubepi_metadata
+```
+
+If your app has its own models too:
+
+```python
+target_metadata = [your_metadata, cubepi_metadata]
+```
+
+## Autogenerate, and what it cannot see
+
+```bash
+alembic revision --autogenerate -m "add cubepi checkpointer"
+```
+
+Autogenerate captures the four `CREATE TABLE`s, but **SQLAlchemy
+`MetaData` does not model two things cubepi needs**, so add them to the
+generated migration by hand:
+
+1. **Partitioning.** Append the KEY-partition clause to the generated
+ `cubepi_messages` table. The simplest path is to drop the
+ autogenerated `create_table("cubepi_messages", ...)` and emit raw DDL:
+
+ ```python
+ from cubepi.checkpointer.mysql.alembic_helpers import messages_partition_clause
+
+ op.execute(
+ "CREATE TABLE cubepi_messages ("
+ " thread_id VARCHAR(255) NOT NULL,"
+ " seq BIGINT NOT NULL,"
+ " message_json TEXT NOT NULL,"
+ " created_at BIGINT NOT NULL,"
+ " PRIMARY KEY (thread_id, seq)"
+ ") " + messages_partition_clause()
+ )
+ ```
+
+ `KEY` (not `HASH`) is required because `thread_id` is a `VARCHAR` —
+ MySQL `HASH` partitioning only accepts integer expressions.
+
+2. **The version row.** Autogenerate creates `cubepi_schema_version` but
+ never inserts a row. `write_schema_version_op()` returns two
+ `;`-separated statements and aiomysql/pymysql run one statement per
+ call, so split before executing:
+
+ ```python
+ from cubepi.checkpointer.mysql.alembic_helpers import write_schema_version_op
+
+ for stmt in write_schema_version_op().split(";"):
+ if stmt.strip():
+ op.execute(stmt)
+ ```
+
+> Tip: `models.create_all_sql()` dumps the MySQL dialect DDL as a string
+> for eyeballing. Remember to append `messages_partition_clause()` to the
+> messages table.
+
+## Upgrading schema versions
+
+cubepi bumps `EXPECTED_SCHEMA_VERSION` when the schema changes. The
+checkpointer refuses to start if the row in `cubepi_schema_version`
+doesn't match, so each bump is one host migration.
+
+**v1 → v2** (HITL): adds `pending_request JSON NULL` to `cubepi_threads`.
+
+```python
+from cubepi.checkpointer.mysql.alembic_helpers import (
+ add_pending_request_column_op,
+ write_schema_version_op,
+)
+
+def upgrade():
+ op.execute(add_pending_request_column_op()) # ALTER TABLE ... ADD COLUMN
+ for stmt in write_schema_version_op().split(";"):
+ if stmt.strip():
+ op.execute(stmt)
+
+def downgrade():
+ op.execute("ALTER TABLE cubepi_threads DROP COLUMN pending_request")
+ op.execute("DELETE FROM cubepi_schema_version")
+ op.execute("INSERT INTO cubepi_schema_version (version) VALUES (1)")
+```
+
+> MySQL does **not** support `ADD COLUMN IF NOT EXISTS`. If you need the
+> migration to be re-runnable, guard the `ALTER` with an
+> `information_schema.columns` check in your migration.
+
+## See also
+
+- User-facing guide: `website/docs/guides/checkpointing/mysql.md`
+- Postgres counterpart: `../postgres/README.md`
diff --git a/cubepi/checkpointer/postgres/README.md b/cubepi/checkpointer/postgres/README.md
new file mode 100644
index 0000000..28414f7
--- /dev/null
+++ b/cubepi/checkpointer/postgres/README.md
@@ -0,0 +1,163 @@
+# Postgres checkpointer — host integration
+
+`PostgresCheckpointer` (asyncpg) persists agent state for multi-replica,
+high-concurrency deployments. **It never issues DDL at runtime** — your
+application owns the schema lifecycle through Alembic, and cubepi only
+reads and writes rows. At startup `setup()` runs verification probes and
+raises `CubepiSchemaUninitialized` / `CubepiSchemaMismatch` if the
+expected migrations haven't been applied.
+
+Install with the extra:
+
+```bash
+pip install 'cubepi[postgres]'
+```
+
+## Using the checkpointer
+
+```python
+from cubepi.checkpointer.postgres import PostgresCheckpointer
+
+async with PostgresCheckpointer("postgresql://user:pass@host/db") as cp:
+ await cp.setup() # verifies schema; never creates tables
+ agent = Agent(..., checkpointer=cp, thread_id="user-42")
+```
+
+Constructor: `PostgresCheckpointer(dsn, *, min_size=1, max_size=10,
+schema_version_check=True)`. Pass `schema_version_check=False` only if
+you deliberately run ahead of the row in `cubepi_schema_version`.
+
+## What the public API exposes
+
+Everything a host migration needs is re-exported from
+`cubepi.checkpointer.postgres`:
+
+| Symbol | Use |
+|---|---|
+| `cubepi_metadata` | SQLAlchemy `MetaData` — set as Alembic `target_metadata` |
+| `EXPECTED_SCHEMA_VERSION` | The version cubepi verifies at startup (currently `2`) |
+| `create_message_partitions_op()` | DDL for the 64 hash partitions of `cubepi_messages` |
+| `add_pending_request_column_op()` | v1→v2 column add (idempotent `IF NOT EXISTS`) |
+| `write_schema_version_op()` | Sets `cubepi_schema_version` to the current version |
+
+The schema lives in `models.py`; the SQL helpers in `alembic_helpers.py`.
+
+## Tables
+
+| Table | Purpose |
+|---|---|
+| `cubepi_threads` | One row per thread; holds `pending_request JSONB` (HITL, v2+) |
+| `cubepi_runs` | One row per run; status tracking |
+| `cubepi_messages` | Append-only message log — **64 hash partitions** on `thread_id` |
+| `cubepi_schema_version` | Single-row version marker verified at startup |
+
+## Wiring Alembic (`env.py`)
+
+Point Alembic at cubepi's metadata so autogenerate sees the tables:
+
+```python
+# alembic/env.py
+from cubepi.checkpointer.postgres import cubepi_metadata
+
+target_metadata = cubepi_metadata
+```
+
+If your app has its own models too, combine them:
+
+```python
+target_metadata = [your_metadata, cubepi_metadata]
+```
+
+## Autogenerate, and what it cannot see
+
+```bash
+alembic revision --autogenerate -m "add cubepi checkpointer"
+```
+
+Autogenerate captures the four `CREATE TABLE`s from `cubepi_metadata`,
+but **SQLAlchemy `MetaData` does not model two things cubepi needs**, so
+you must add them to the generated migration by hand:
+
+1. **Partitioning.** The generated `cubepi_messages` is a plain table.
+ Replace it with a partitioned parent + children:
+
+ ```python
+ # in the autogenerated upgrade(), instead of the plain create_table:
+ op.execute(
+ "CREATE TABLE cubepi_messages ("
+ " thread_id TEXT NOT NULL,"
+ " seq BIGINT NOT NULL,"
+ " message_json TEXT NOT NULL,"
+ " created_at BIGINT NOT NULL,"
+ " PRIMARY KEY (thread_id, seq)"
+ ") PARTITION BY HASH (thread_id)"
+ )
+ op.execute(create_message_partitions_op()) # the 64 children
+ ```
+
+2. **The version row.** Autogenerate creates `cubepi_schema_version` but
+ never inserts a row. Append:
+
+ ```python
+ op.execute(write_schema_version_op())
+ ```
+
+A complete initial `upgrade()`:
+
+```python
+from cubepi.checkpointer.postgres.alembic_helpers import (
+ create_message_partitions_op,
+ write_schema_version_op,
+)
+
+def upgrade():
+ # ... autogenerated create_table() for threads / runs / schema_version ...
+ op.execute(
+ "CREATE TABLE cubepi_messages ("
+ " thread_id TEXT NOT NULL, seq BIGINT NOT NULL,"
+ " message_json TEXT NOT NULL, created_at BIGINT NOT NULL,"
+ " PRIMARY KEY (thread_id, seq)"
+ ") PARTITION BY HASH (thread_id)"
+ )
+ op.execute(create_message_partitions_op())
+ op.execute(write_schema_version_op())
+```
+
+> Tip: `models.create_all_sql()` and `models.initial_migration_sql()`
+> dump the dialect DDL as strings — handy for eyeballing or pasting a
+> baseline. Partition children still come from
+> `create_message_partitions_op()`.
+
+## Upgrading schema versions
+
+cubepi bumps `EXPECTED_SCHEMA_VERSION` when the schema changes. The
+checkpointer refuses to start if the row in `cubepi_schema_version`
+doesn't match, so each bump is one host migration.
+
+**v1 → v2** (HITL): adds `pending_request JSONB NULL` to
+`cubepi_threads`.
+
+```python
+from cubepi.checkpointer.postgres.alembic_helpers import (
+ add_pending_request_column_op,
+ write_schema_version_op,
+)
+
+def upgrade():
+ op.execute(add_pending_request_column_op()) # ADD COLUMN IF NOT EXISTS
+ op.execute(write_schema_version_op()) # bump to EXPECTED_SCHEMA_VERSION
+
+def downgrade():
+ op.execute("ALTER TABLE cubepi_threads DROP COLUMN IF EXISTS pending_request")
+ op.execute("DELETE FROM cubepi_schema_version")
+ op.execute("INSERT INTO cubepi_schema_version (version) VALUES (1)")
+```
+
+`write_schema_version_op()` clears stale rows before inserting, so
+running it after every cubepi upgrade keeps `cubepi_schema_version`
+single-valued and consistent.
+
+## See also
+
+- User-facing guide: `website/docs/guides/checkpointing/postgres.md`
+- MySQL counterpart: `../mysql/README.md`