Skip to content

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

fix(docs): correct checkpointer README schema, drop nonexistent setup()

xf gong11aaa36b3a68
diff --git a/cubepi/checkpointer/mysql/README.md b/cubepi/checkpointer/mysql/README.md
index 722b424..5a2f63a 100644
--- a/cubepi/checkpointer/mysql/README.md
+++ b/cubepi/checkpointer/mysql/README.md
@@ -4,9 +4,12 @@
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.
+reads and writes rows. Entering the async context manager
+(`async with MySQLCheckpointer(...)`) verifies the schema and raises
+`CubepiSchemaUninitialized` / `CubepiSchemaMismatch` if the expected
+migrations haven't been applied.
+
+Requires MySQL 8.0.13+ (JSON expression defaults) on InnoDB.
Install with the extra:
@@ -19,41 +22,53 @@ pip install 'cubepi[mysql]'
```python
from cubepi.checkpointer.mysql import MySQLCheckpointer
+# Entering the context manager connects the pool and verifies the schema.
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_pool_size=1,
max_pool_size=10)`. The DSN is a `mysql://user:pass@host:port/db` URL,
-parsed into `aiomysql.create_pool` kwargs.
+parsed into `aiomysql.create_pool` kwargs. Schema verification happens on
+`__aenter__`; it never creates tables.
## What the public API exposes
-Everything a host migration needs is re-exported from
-`cubepi.checkpointer.mysql`:
+The package re-exports the symbols Alembic wiring needs:
+
+```python
+from cubepi.checkpointer.mysql import cubepi_metadata, EXPECTED_SCHEMA_VERSION
+```
| 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) |
+| `EXPECTED_SCHEMA_VERSION` | The version cubepi verifies on connect (currently `2`) |
-The schema lives in `models.py`; the SQL helpers in `alembic_helpers.py`.
+The SQL helpers live in the `alembic_helpers` submodule:
+
+```python
+from cubepi.checkpointer.mysql.alembic_helpers import (
+ messages_partition_clause, # the PARTITION BY KEY (...) clause for cubepi_messages
+ add_pending_request_column_op, # v1→v2 column add
+ write_schema_version_op, # returns two ';'-separated statements
+)
+```
## Tables
+`cubepi_metadata` defines three 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 |
+| `cubepi_threads` | One row per thread; holds `pending_request JSON NULL` (HITL, v2+) |
+| `cubepi_messages` | Append-only message log — `(thread_id, seq, role, metadata, payload, created_at)`, `PARTITION BY KEY (thread_id)` into 64 partitions |
+| `cubepi_schema_version` | Single-row version marker verified on connect |
MySQL declares all partitions inline in the `CREATE TABLE` (unlike
-Postgres, which uses one child table per partition).
+Postgres, which uses one child table per partition). The `cubepi_messages`
+table has **no foreign key** to `cubepi_threads` — MySQL forbids FKs on
+partitioned tables.
## Wiring Alembic (`env.py`)
@@ -62,49 +77,36 @@ Postgres, which uses one child table per partition).
from cubepi.checkpointer.mysql import cubepi_metadata
target_metadata = cubepi_metadata
+# or, alongside your own models:
+# target_metadata = [your_metadata, cubepi_metadata]
```
-If your app has its own models too:
-
-```python
-target_metadata = [your_metadata, cubepi_metadata]
-```
-
-## Autogenerate, and what it cannot see
+## Autogenerate, and what to add by hand
```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:
+Autogenerate emits `op.create_table(...)` for all three tables **with the
+correct columns** (from `cubepi_metadata`). Two things to add 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:
+1. **KEY partitioning.** SQLAlchemy autogenerate cannot express MySQL
+ `KEY` partitioning, so append the partition clause to the
+ `cubepi_messages` table. Keep the autogenerated column list and add
+ the clause via `op.execute`:
```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()
- )
+ op.execute("ALTER TABLE cubepi_messages " + 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:
+2. **The version row.** `write_schema_version_op()` returns two
+ `;`-separated statements (DELETE then INSERT); aiomysql/pymysql run one
+ statement per call, so split before executing:
```python
from cubepi.checkpointer.mysql.alembic_helpers import write_schema_version_op
@@ -114,15 +116,15 @@ generated migration by hand:
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.
+> Do not hand-trim the `cubepi_messages` columns. The checkpointer writes
+> and reads `(thread_id, seq, role, metadata, payload)`; dropping any of
+> them causes runtime SQL errors on the first append.
## 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.
+cubepi bumps `EXPECTED_SCHEMA_VERSION` when the schema changes; the
+checkpointer refuses to connect if the row in `cubepi_schema_version`
+doesn't match. Each bump is one host migration.
**v1 → v2** (HITL): adds `pending_request JSON NULL` to `cubepi_threads`.
@@ -137,16 +139,11 @@ def upgrade():
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.
+> MySQL does not support `ADD COLUMN IF NOT EXISTS`; guard the `ALTER`
+> with an `information_schema.columns` check if the migration must be
+> re-runnable.
## See also
diff --git a/cubepi/checkpointer/postgres/README.md b/cubepi/checkpointer/postgres/README.md
index 166c782..7c91a71 100644
--- a/cubepi/checkpointer/postgres/README.md
+++ b/cubepi/checkpointer/postgres/README.md
@@ -3,9 +3,10 @@
`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.
+reads and writes rows. Entering the async context manager
+(`async with PostgresCheckpointer(...)`) verifies the schema and raises
+`CubepiSchemaUninitialized` / `CubepiSchemaMismatch` if the expected
+migrations haven't been applied.
Install with the extra:
@@ -18,38 +19,47 @@ pip install 'cubepi[postgres]'
```python
from cubepi.checkpointer.postgres import PostgresCheckpointer
+# Entering the context manager connects the pool and verifies the schema.
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_pool_size=1,
-max_pool_size=10)`. `setup()` verifies the schema and raises on a
-version mismatch; it never creates tables.
+max_pool_size=10)`. The DSN is whatever `asyncpg.create_pool` accepts.
+Schema verification happens on `__aenter__`; it never creates tables.
## What the public API exposes
-Everything a host migration needs is re-exported from
-`cubepi.checkpointer.postgres`:
+The package re-exports the symbols Alembic wiring needs:
+
+```python
+from cubepi.checkpointer.postgres import cubepi_metadata, EXPECTED_SCHEMA_VERSION
+```
| 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 |
+| `EXPECTED_SCHEMA_VERSION` | The version cubepi verifies on connect (currently `2`) |
+
+The SQL helpers live in the `alembic_helpers` submodule:
-The schema lives in `models.py`; the SQL helpers in `alembic_helpers.py`.
+```python
+from cubepi.checkpointer.postgres.alembic_helpers import (
+ 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 EXPECTED_SCHEMA_VERSION
+)
+```
## Tables
+`cubepi_metadata` defines three 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 |
+| `cubepi_threads` | One row per thread; holds `pending_request JSONB NULL` (HITL, v2+) |
+| `cubepi_messages` | Append-only message log — `(thread_id, seq, role, metadata, payload, created_at)`, **HASH-partitioned** on `thread_id` into 64 partitions |
+| `cubepi_schema_version` | Single-row version marker verified on connect |
## Wiring Alembic (`env.py`)
@@ -60,49 +70,30 @@ Point Alembic at cubepi's metadata so autogenerate sees the tables:
from cubepi.checkpointer.postgres import cubepi_metadata
target_metadata = cubepi_metadata
+# or, alongside your own models:
+# target_metadata = [your_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
+## Autogenerate, and what to add by hand
```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:
+Autogenerate emits `op.create_table(...)` for all three tables **with the
+correct columns** (they come straight from `cubepi_metadata`). Two things
+SQLAlchemy autogenerate does not reliably produce, so add them to the
+generated migration yourself:
- ```python
- op.execute(write_schema_version_op())
- ```
+1. **Child partitions.** `cubepi_messages` is HASH-partitioned on
+ `thread_id` (the model declares this via
+ `__table_args__["postgresql_partition_by"]`). Keep the autogenerated
+ `create_table` for `cubepi_messages` — including all its columns — and
+ make sure it carries `postgresql_partition_by="HASH (thread_id)"`. The
+ 64 child partitions are not autogenerated; create them with the helper.
-A complete initial `upgrade()`:
+2. **The version row.** Autogenerate creates the `cubepi_schema_version`
+ table but never inserts a row.
```python
from cubepi.checkpointer.postgres.alembic_helpers import (
@@ -111,31 +102,24 @@ from cubepi.checkpointer.postgres.alembic_helpers import (
)
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())
+ # ... autogenerated create_table() for cubepi_threads,
+ # cubepi_schema_version, and cubepi_messages (keep ALL columns;
+ # ensure cubepi_messages has postgresql_partition_by="HASH (thread_id)") ...
+ op.execute(create_message_partitions_op()) # the 64 child partitions
+ op.execute(write_schema_version_op()) # record EXPECTED_SCHEMA_VERSION
```
-> 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()`.
+> Do not hand-trim the `cubepi_messages` columns. The checkpointer writes
+> and reads `(thread_id, seq, role, metadata, payload)`; dropping any of
+> them causes runtime SQL errors on the first append.
## 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.
+cubepi bumps `EXPECTED_SCHEMA_VERSION` when the schema changes; the
+checkpointer refuses to connect if the row in `cubepi_schema_version`
+doesn't match. Each bump is one host migration.
-**v1 → v2** (HITL): adds `pending_request JSONB NULL` to
-`cubepi_threads`.
+**v1 → v2** (HITL): adds `pending_request JSONB NULL` to `cubepi_threads`.
```python
from cubepi.checkpointer.postgres.alembic_helpers import (
@@ -153,9 +137,8 @@ def downgrade():
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.
+`write_schema_version_op()` clears stale rows before inserting, so running
+it after every cubepi upgrade keeps `cubepi_schema_version` single-valued.
## See also