Skip to content

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

docs(website): address Codex review on PR #71

xf gonge3c9486257ad
diff --git a/website/docs/getting-started/quick-start.md b/website/docs/getting-started/quick-start.md
index a421747..a0ff7de 100644
--- a/website/docs/getting-started/quick-start.md
+++ b/website/docs/getting-started/quick-start.md
@@ -57,8 +57,8 @@ async def main():
# Subscribe BEFORE prompt() — that's how you see streaming events.
def on_event(event, signal=None):
- if event.type == "text_delta":
- print(event.delta, end="", flush=True)
+ if event.type == "message_update" and event.stream_event.type == "text_delta":
+ print(event.stream_event.delta, end="", flush=True)
elif event.type == "agent_end":
print() # final newline
diff --git a/website/docs/guides/agents/first-agent.md b/website/docs/guides/agents/first-agent.md
index 2c33250..0783d05 100644
--- a/website/docs/guides/agents/first-agent.md
+++ b/website/docs/guides/agents/first-agent.md
@@ -96,8 +96,8 @@ receives every `AgentEvent`:
```python
def on_event(event, signal=None):
- if event.type == "text_delta":
- print(event.delta, end="", flush=True)
+ if event.type == "message_update" and event.stream_event.type == "text_delta":
+ print(event.stream_event.delta, end="", flush=True)
elif event.type == "tool_execution_start":
print(f"\n→ calling {event.tool_name}({event.args})")
elif event.type == "tool_execution_end":
diff --git a/website/docs/guides/agents/streaming.md b/website/docs/guides/agents/streaming.md
index 70a7b9d..c55baf0 100644
--- a/website/docs/guides/agents/streaming.md
+++ b/website/docs/guides/agents/streaming.md
@@ -65,12 +65,17 @@ agent_end
```python
def on_event(event, signal=None):
- if event.type == "text_delta":
- print(event.delta, end="", flush=True)
+ if event.type == "message_update" and event.stream_event.type == "text_delta":
+ print(event.stream_event.delta, end="", flush=True)
unsubscribe = agent.subscribe(on_event)
```
+`agent.subscribe(...)` never receives an event whose top-level `.type`
+equals `"text_delta"` — that's a *provider* event type. The agent
+wraps every provider event in a `MessageUpdateEvent` and tucks the
+original under `event.stream_event`. Match on both fields, as above.
+
Listeners can be sync or async; async ones are awaited. The second
argument is the run-level `asyncio.Event` (the abort signal) — you can
inspect `signal.is_set()` to know if the run was cancelled.
@@ -87,20 +92,19 @@ def on_event(event, signal=None):
print(sub.delta, end="", flush=True)
```
-Equivalent shorter form, since `text_delta` is the inner provider
-event type and CubePi exposes the same `.type` and `.delta` on the
-event itself:
+Equivalent defensive form using `getattr`, since only
+`MessageUpdateEvent` carries a `stream_event` attribute:
```python
def on_event(event, signal=None):
- # message_update unwraps the inner stream_event onto the wrapper:
if getattr(event, "stream_event", None) and event.stream_event.type == "text_delta":
print(event.stream_event.delta, end="", flush=True)
```
The shape that CubePi guarantees is the one in the table above
-(`message_update.stream_event.delta`). Code defensively against the
-inner type.
+(`MessageUpdateEvent.stream_event.delta`). Always match the outer
+event's `type == "message_update"` (or check `stream_event` exists)
+before reaching into `stream_event.type`.
## Provider `StreamEvent` types
diff --git a/website/docs/guides/checkpointing/postgres.md b/website/docs/guides/checkpointing/postgres.md
index b2c5dfa..1e07e35 100644
--- a/website/docs/guides/checkpointing/postgres.md
+++ b/website/docs/guides/checkpointing/postgres.md
@@ -84,15 +84,24 @@ INSERT the schema version. Use the helper:
```python
# In a migration's upgrade():
-from cubepi.checkpointer.postgres.alembic_helpers import write_schema_version_op
+from cubepi.checkpointer.postgres.alembic_helpers import (
+ create_message_partitions_op,
+ write_schema_version_op,
+)
def upgrade():
- op.create_table(...) # auto-generated by Alembic from cubepi_metadata
- write_schema_version_op(op) # records EXPECTED_SCHEMA_VERSION
+ op.create_table(...) # auto-generated from cubepi_metadata
+ op.execute(create_message_partitions_op()) # creates the 64 hash partitions
+ op.execute(write_schema_version_op()) # records EXPECTED_SCHEMA_VERSION
```
+Both helpers return a SQL string — you pass them to `op.execute(...)`.
+`write_schema_version_op()` is idempotent: it deletes any rows from a
+prior cubepi version and inserts the current one.
+
When CubePi later upgrades and bumps `EXPECTED_SCHEMA_VERSION`, you
-generate a new revision and call `write_schema_version_op` again.
+generate a new revision and call `op.execute(write_schema_version_op())`
+again.
## Data model
diff --git a/website/docs/recipes/resumable-tasks.md b/website/docs/recipes/resumable-tasks.md
index c4c4518..0f4c832 100644
--- a/website/docs/recipes/resumable-tasks.md
+++ b/website/docs/recipes/resumable-tasks.md
@@ -77,6 +77,15 @@ async def transcode_video(tool_call_id, params: TranscodeParams, *, signal=None,
content=[TextContent(text=f"Transcoded to {output}.")],
details={"output_path": output},
)
+
+
+transcode_tool = AgentTool(
+ name="transcode_video",
+ description="Transcode a video file. Idempotent — safe to retry.",
+ parameters=TranscodeParams,
+ execute=transcode_video,
+ execution_mode="sequential", # one transcode at a time
+)
```
Now if the process dies during `run_ffmpeg`, the next agent run sees
@@ -96,7 +105,7 @@ from cubepi import Agent, Model
from cubepi.checkpointer import SQLiteCheckpointer
from cubepi.providers.anthropic import AnthropicProvider
-from tools import transcode_video # the idempotent tool from above
+from tools import transcode_tool # the wrapped AgentTool from above
async def main(thread_id: str, initial_prompt: str | None):
@@ -105,7 +114,7 @@ async def main(thread_id: str, initial_prompt: str | None):
provider=AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"]),
model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),
system_prompt="You orchestrate video transcoding jobs.",
- tools=[transcode_video],
+ tools=[transcode_tool],
checkpointer=cp,
thread_id=thread_id,
)
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/quick-start.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/quick-start.md
index 6b35709..974721c 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/quick-start.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/getting-started/quick-start.md
@@ -57,8 +57,8 @@ async def main():
# 在 prompt() 之前订阅 —— 这是看到流式事件的关键。
def on_event(event, signal=None):
- if event.type == "text_delta":
- print(event.delta, end="", flush=True)
+ if event.type == "message_update" and event.stream_event.type == "text_delta":
+ print(event.stream_event.delta, end="", flush=True)
elif event.type == "agent_end":
print() # 末尾换行
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/first-agent.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/first-agent.md
index b40f822..f46e641 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/first-agent.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/first-agent.md
@@ -92,8 +92,8 @@ agent = Agent(
```python
def on_event(event, signal=None):
- if event.type == "text_delta":
- print(event.delta, end="", flush=True)
+ if event.type == "message_update" and event.stream_event.type == "text_delta":
+ print(event.stream_event.delta, end="", flush=True)
elif event.type == "tool_execution_start":
print(f"\n→ 调用 {event.tool_name}({event.args})")
elif event.type == "tool_execution_end":
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/streaming.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/streaming.md
index eb22294..bb7d7f1 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/streaming.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/agents/streaming.md
@@ -62,12 +62,17 @@ agent_end
```python
def on_event(event, signal=None):
- if event.type == "text_delta":
- print(event.delta, end="", flush=True)
+ if event.type == "message_update" and event.stream_event.type == "text_delta":
+ print(event.stream_event.delta, end="", flush=True)
unsubscribe = agent.subscribe(on_event)
```
+`agent.subscribe(...)` 永远不会收到 `event.type == "text_delta"`
+的事件 —— 那是 *provider* 事件的 type。Agent 把每个 provider 事件
+都包成 `MessageUpdateEvent`,原事件挂在 `event.stream_event` 上。
+所以要同时匹配外层和内层。
+
Listener 可以是同步或异步,异步的会被 await。第二个参数是 run 级别
的 `asyncio.Event`(abort signal)—— 你可以查 `signal.is_set()` 判断
本次运行是否被取消。
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.3/getting-started/quick-start.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.3/getting-started/quick-start.md
index 6b35709..974721c 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.3/getting-started/quick-start.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.3/getting-started/quick-start.md
@@ -57,8 +57,8 @@ async def main():
# 在 prompt() 之前订阅 —— 这是看到流式事件的关键。
def on_event(event, signal=None):
- if event.type == "text_delta":
- print(event.delta, end="", flush=True)
+ if event.type == "message_update" and event.stream_event.type == "text_delta":
+ print(event.stream_event.delta, end="", flush=True)
elif event.type == "agent_end":
print() # 末尾换行
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.3/guides/agents/first-agent.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.3/guides/agents/first-agent.md
index b40f822..f46e641 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.3/guides/agents/first-agent.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.3/guides/agents/first-agent.md
@@ -92,8 +92,8 @@ agent = Agent(
```python
def on_event(event, signal=None):
- if event.type == "text_delta":
- print(event.delta, end="", flush=True)
+ if event.type == "message_update" and event.stream_event.type == "text_delta":
+ print(event.stream_event.delta, end="", flush=True)
elif event.type == "tool_execution_start":
print(f"\n→ 调用 {event.tool_name}({event.args})")
elif event.type == "tool_execution_end":
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.3/guides/agents/streaming.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.3/guides/agents/streaming.md
index eb22294..bb7d7f1 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.3/guides/agents/streaming.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.3/guides/agents/streaming.md
@@ -62,12 +62,17 @@ agent_end
```python
def on_event(event, signal=None):
- if event.type == "text_delta":
- print(event.delta, end="", flush=True)
+ if event.type == "message_update" and event.stream_event.type == "text_delta":
+ print(event.stream_event.delta, end="", flush=True)
unsubscribe = agent.subscribe(on_event)
```
+`agent.subscribe(...)` 永远不会收到 `event.type == "text_delta"`
+的事件 —— 那是 *provider* 事件的 type。Agent 把每个 provider 事件
+都包成 `MessageUpdateEvent`,原事件挂在 `event.stream_event` 上。
+所以要同时匹配外层和内层。
+
Listener 可以是同步或异步,异步的会被 await。第二个参数是 run 级别
的 `asyncio.Event`(abort signal)—— 你可以查 `signal.is_set()` 判断
本次运行是否被取消。
diff --git a/website/versioned_docs/version-0.3/getting-started/quick-start.md b/website/versioned_docs/version-0.3/getting-started/quick-start.md
index a421747..a0ff7de 100644
--- a/website/versioned_docs/version-0.3/getting-started/quick-start.md
+++ b/website/versioned_docs/version-0.3/getting-started/quick-start.md
@@ -57,8 +57,8 @@ async def main():
# Subscribe BEFORE prompt() — that's how you see streaming events.
def on_event(event, signal=None):
- if event.type == "text_delta":
- print(event.delta, end="", flush=True)
+ if event.type == "message_update" and event.stream_event.type == "text_delta":
+ print(event.stream_event.delta, end="", flush=True)
elif event.type == "agent_end":
print() # final newline
diff --git a/website/versioned_docs/version-0.3/guides/agents/first-agent.md b/website/versioned_docs/version-0.3/guides/agents/first-agent.md
index 2c33250..0783d05 100644
--- a/website/versioned_docs/version-0.3/guides/agents/first-agent.md
+++ b/website/versioned_docs/version-0.3/guides/agents/first-agent.md
@@ -96,8 +96,8 @@ receives every `AgentEvent`:
```python
def on_event(event, signal=None):
- if event.type == "text_delta":
- print(event.delta, end="", flush=True)
+ if event.type == "message_update" and event.stream_event.type == "text_delta":
+ print(event.stream_event.delta, end="", flush=True)
elif event.type == "tool_execution_start":
print(f"\n→ calling {event.tool_name}({event.args})")
elif event.type == "tool_execution_end":
diff --git a/website/versioned_docs/version-0.3/guides/agents/streaming.md b/website/versioned_docs/version-0.3/guides/agents/streaming.md
index 70a7b9d..c55baf0 100644
--- a/website/versioned_docs/version-0.3/guides/agents/streaming.md
+++ b/website/versioned_docs/version-0.3/guides/agents/streaming.md
@@ -65,12 +65,17 @@ agent_end
```python
def on_event(event, signal=None):
- if event.type == "text_delta":
- print(event.delta, end="", flush=True)
+ if event.type == "message_update" and event.stream_event.type == "text_delta":
+ print(event.stream_event.delta, end="", flush=True)
unsubscribe = agent.subscribe(on_event)
```
+`agent.subscribe(...)` never receives an event whose top-level `.type`
+equals `"text_delta"` — that's a *provider* event type. The agent
+wraps every provider event in a `MessageUpdateEvent` and tucks the
+original under `event.stream_event`. Match on both fields, as above.
+
Listeners can be sync or async; async ones are awaited. The second
argument is the run-level `asyncio.Event` (the abort signal) — you can
inspect `signal.is_set()` to know if the run was cancelled.
@@ -87,20 +92,19 @@ def on_event(event, signal=None):
print(sub.delta, end="", flush=True)
```
-Equivalent shorter form, since `text_delta` is the inner provider
-event type and CubePi exposes the same `.type` and `.delta` on the
-event itself:
+Equivalent defensive form using `getattr`, since only
+`MessageUpdateEvent` carries a `stream_event` attribute:
```python
def on_event(event, signal=None):
- # message_update unwraps the inner stream_event onto the wrapper:
if getattr(event, "stream_event", None) and event.stream_event.type == "text_delta":
print(event.stream_event.delta, end="", flush=True)
```
The shape that CubePi guarantees is the one in the table above
-(`message_update.stream_event.delta`). Code defensively against the
-inner type.
+(`MessageUpdateEvent.stream_event.delta`). Always match the outer
+event's `type == "message_update"` (or check `stream_event` exists)
+before reaching into `stream_event.type`.
## Provider `StreamEvent` types
diff --git a/website/versioned_docs/version-0.3/guides/checkpointing/postgres.md b/website/versioned_docs/version-0.3/guides/checkpointing/postgres.md
index b2c5dfa..1e07e35 100644
--- a/website/versioned_docs/version-0.3/guides/checkpointing/postgres.md
+++ b/website/versioned_docs/version-0.3/guides/checkpointing/postgres.md
@@ -84,15 +84,24 @@ INSERT the schema version. Use the helper:
```python
# In a migration's upgrade():
-from cubepi.checkpointer.postgres.alembic_helpers import write_schema_version_op
+from cubepi.checkpointer.postgres.alembic_helpers import (
+ create_message_partitions_op,
+ write_schema_version_op,
+)
def upgrade():
- op.create_table(...) # auto-generated by Alembic from cubepi_metadata
- write_schema_version_op(op) # records EXPECTED_SCHEMA_VERSION
+ op.create_table(...) # auto-generated from cubepi_metadata
+ op.execute(create_message_partitions_op()) # creates the 64 hash partitions
+ op.execute(write_schema_version_op()) # records EXPECTED_SCHEMA_VERSION
```
+Both helpers return a SQL string — you pass them to `op.execute(...)`.
+`write_schema_version_op()` is idempotent: it deletes any rows from a
+prior cubepi version and inserts the current one.
+
When CubePi later upgrades and bumps `EXPECTED_SCHEMA_VERSION`, you
-generate a new revision and call `write_schema_version_op` again.
+generate a new revision and call `op.execute(write_schema_version_op())`
+again.
## Data model
diff --git a/website/versioned_docs/version-0.3/recipes/resumable-tasks.md b/website/versioned_docs/version-0.3/recipes/resumable-tasks.md
index c4c4518..0f4c832 100644
--- a/website/versioned_docs/version-0.3/recipes/resumable-tasks.md
+++ b/website/versioned_docs/version-0.3/recipes/resumable-tasks.md
@@ -77,6 +77,15 @@ async def transcode_video(tool_call_id, params: TranscodeParams, *, signal=None,
content=[TextContent(text=f"Transcoded to {output}.")],
details={"output_path": output},
)
+
+
+transcode_tool = AgentTool(
+ name="transcode_video",
+ description="Transcode a video file. Idempotent — safe to retry.",
+ parameters=TranscodeParams,
+ execute=transcode_video,
+ execution_mode="sequential", # one transcode at a time
+)
```
Now if the process dies during `run_ffmpeg`, the next agent run sees
@@ -96,7 +105,7 @@ from cubepi import Agent, Model
from cubepi.checkpointer import SQLiteCheckpointer
from cubepi.providers.anthropic import AnthropicProvider
-from tools import transcode_video # the idempotent tool from above
+from tools import transcode_tool # the wrapped AgentTool from above
async def main(thread_id: str, initial_prompt: str | None):
@@ -105,7 +114,7 @@ async def main(thread_id: str, initial_prompt: str | None):
provider=AnthropicProvider(api_key=os.environ["ANTHROPIC_API_KEY"]),
model=Model(id="claude-sonnet-4-5-20250929", provider="anthropic"),
system_prompt="You orchestrate video transcoding jobs.",
- tools=[transcode_video],
+ tools=[transcode_tool],
checkpointer=cp,
thread_id=thread_id,
)