Skip to content

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

docs(0.7): document main-branch additions — compaction tracing + anthropic empty-assistant fix

xf gonga41a38423fff
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ec7ffa0..13bdcb5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -43,6 +43,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
runs (`stop_reason in ("error", "aborted")`) skip the hook.
- HITL-interrupted runs (HitlDetached / HitlAborted) also skip the hook —
the conversation is paused, not finished.
+- **Compaction summarizer tracing** — `CompactionMiddleware`'s summary call is
+ now first-class in the trace tree. A `cubepi.compaction.summarize` parent
+ span (carrying `cubepi.compaction.message_count`) wraps the summarizer LLM
+ call, and the recorder auto-subscribes the middleware's `summary_provider`
+ so its `chat` span lands as a child:
+ ```
+ invoke_agent
+ └── cubepi.turn
+ ├── cubepi.compaction.summarize
+ │ └── chat <summary-model>
+ └── chat <main-model>
+ ```
+ Previously summarizer calls bypassed the trace entirely.
+- **`Middleware.providers()` protocol** — middleware authors can override this
+ method to expose any extra `BaseProvider` instances the middleware owns;
+ `Recorder.attach()` walks `agent._middleware` and wires its listener registry
+ on each one (id-deduped against `agent._provider`). Default is empty, so
+ existing custom middleware needs no change.
### Changed
@@ -99,6 +117,30 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `Tracer.oneshot()` now closes chat spans on failure/cancellation, awaits
stream completion and flushes, forwards abort signals, and surfaces silent
producer failures.
+- **Anthropic empty-assistant recovery** — a persisted
+ `AssistantMessage(content=[], stop_reason="error")` (e.g. from a transient
+ provider failure that the checkpointer recorded) no longer poisons the
+ conversation:
+ - Trailing empty assistant turns are dropped before send (the next request
+ regenerates from the preceding user prompt instead of treating the
+ placeholder as an assistant prefill).
+ - Mid-history empty assistant content is replaced with an `[empty response]`
+ text block so the wire payload satisfies the Anthropic API's
+ `messages.N: all messages must have non-empty content` rule.
+ - The trim happens in `stream()` before the cache policy resolves
+ breakpoints, so the user's last-message `cache_control` marker survives
+ on retry paths.
+- **Tracing root attribution under middleware-driven providers** — when a
+ middleware provider (e.g. `CompactionMiddleware.summary_provider`) issues
+ the first chat span of a run before the agent's main call, the root
+ `invoke_agent` span's `gen_ai.provider.name` / `cubepi.agent.system_prompt_sha256`
+ / `cubepi.agent.tools` are no longer overwritten by the middleware's values.
+ The recorder now gates root attribution by model identity (not listener
+ identity), and falls back to first-call-wins when a middleware declares the
+ same `(provider, model)` pair as the agent's main.
+- The compaction summarizer's wrapper span now installs `turn_span` as the
+ OTel current span, so `cubepi.compaction.summarize` lands as a child of
+ `cubepi.turn` instead of becoming an orphan root.
### Removed
diff --git a/website/docs/guides/middleware/compaction.md b/website/docs/guides/middleware/compaction.md
index 5c96073..3891bd1 100644
--- a/website/docs/guides/middleware/compaction.md
+++ b/website/docs/guides/middleware/compaction.md
@@ -70,6 +70,30 @@ outputs or user corrections are especially important. Increase
`max_summary_tokens` for long-running research or coding sessions where the
summary needs more detail.
+## Tracing
+
+When `cubepi.tracing` is attached to the agent, the summarizer call is
+first-class in the trace tree. `summarize()` opens a
+`cubepi.compaction.summarize` parent span (tagged with
+`cubepi.compaction.message_count`) around the LLM call, and the recorder
+automatically subscribes the summarizer provider so its `chat` span lands
+inside:
+
+```
+invoke_agent
+└── cubepi.turn
+ ├── cubepi.compaction.summarize
+ │ └── chat <summary-model>
+ └── chat <main-model>
+```
+
+The wrapper span is a no-op context manager when OpenTelemetry isn't
+installed, so the middleware works the same on minimal installs. The
+root `invoke_agent` span's `gen_ai.provider.name` /
+`cubepi.agent.system_prompt_sha256` / `cubepi.agent.tools` continue to
+reflect the agent's main provider/model, not the summarizer's — even
+when summarization runs first.
+
## Failure behavior
If the summary provider fails, CubePi logs a warning and continues with the
diff --git a/website/docs/guides/tracing/overview.md b/website/docs/guides/tracing/overview.md
index d6fa591..6632045 100644
--- a/website/docs/guides/tracing/overview.md
+++ b/website/docs/guides/tracing/overview.md
@@ -63,6 +63,14 @@ Each layer carries standard `gen_ai.*` attributes — `gen_ai.operation.name`,
(`cubepi.tags = ("beta-arm",)`, `cubepi.metadata.user_id = "u-42"`)
via a contextvar-scoped block. Concurrent agents each see their own
values.
+- **Middleware-owned providers traced automatically** — middleware that
+ exposes extra `BaseProvider` instances via `Middleware.providers()` has
+ its listener registry wired by `Recorder.attach()` so those providers'
+ `chat` spans land in the same trace as the agent's main call.
+ `CompactionMiddleware` uses this to surface its summarizer LLM call as
+ a `chat <summary-model>` span nested under
+ `cubepi.compaction.summarize` — see the [compaction
+ guide](../middleware/compaction#tracing).
## What it costs
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/compaction.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/compaction.md
index 5b5bff5..9319731 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/compaction.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/middleware/compaction.md
@@ -63,6 +63,26 @@ CompactionMiddleware(
`max_tokens_before_compact`。如果最近工具输出或用户修正很重要,可以提高
`keep_recent_messages`。长时间研究或编码会话可提高 `max_summary_tokens`。
+## Tracing
+
+挂上 `cubepi.tracing` 时,摘要调用是 trace 树里的一等公民。`summarize()`
+在 LLM 调用外包一个 `cubepi.compaction.summarize` 父 span(标签
+`cubepi.compaction.message_count`),同时 recorder 自动订阅 summary
+provider,所以它的 `chat` span 也落在里面:
+
+```
+invoke_agent
+└── cubepi.turn
+ ├── cubepi.compaction.summarize
+ │ └── chat <summary-model>
+ └── chat <main-model>
+```
+
+没装 OpenTelemetry 时,wrapper span 退化为 no-op context manager,中间件
+行为不变。根 `invoke_agent` span 的 `gen_ai.provider.name` /
+`cubepi.agent.system_prompt_sha256` / `cubepi.agent.tools` 始终归属
+agent 的主 provider/model,不会被先跑的 summarizer 覆盖。
+
## 失败行为
如果摘要 provider 失败,CubePi 会记录 warning,并继续使用之前的压缩视图或
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/tracing/overview.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/tracing/overview.md
index 4b48459..20bb738 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/tracing/overview.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/current/guides/tracing/overview.md
@@ -60,6 +60,13 @@ trace
- **`tracing_context()`** —— 通过 contextvar 作用域块为单次运行设置标签和
元数据(`cubepi.tags = ("beta-arm",)`、`cubepi.metadata.user_id = "u-42"`),
并发 agent 各自看到独立的值。
+- **中间件自有 provider 自动接入 trace** —— 中间件通过
+ `Middleware.providers()` 暴露自己持有的 `BaseProvider`,
+ `Recorder.attach()` 会自动给这些 provider 接上 listener 注册表,它们的
+ `chat` span 就和 agent 主调用落进同一条 trace。`CompactionMiddleware`
+ 就用这个机制把摘要 LLM 调用呈现为嵌在
+ `cubepi.compaction.summarize` 下的 `chat <summary-model>` —— 详见
+ [compaction 指南](../middleware/compaction#tracing)。
## 开销
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/compaction.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/compaction.md
index 5b5bff5..9319731 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/compaction.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/middleware/compaction.md
@@ -63,6 +63,26 @@ CompactionMiddleware(
`max_tokens_before_compact`。如果最近工具输出或用户修正很重要,可以提高
`keep_recent_messages`。长时间研究或编码会话可提高 `max_summary_tokens`。
+## Tracing
+
+挂上 `cubepi.tracing` 时,摘要调用是 trace 树里的一等公民。`summarize()`
+在 LLM 调用外包一个 `cubepi.compaction.summarize` 父 span(标签
+`cubepi.compaction.message_count`),同时 recorder 自动订阅 summary
+provider,所以它的 `chat` span 也落在里面:
+
+```
+invoke_agent
+└── cubepi.turn
+ ├── cubepi.compaction.summarize
+ │ └── chat <summary-model>
+ └── chat <main-model>
+```
+
+没装 OpenTelemetry 时,wrapper span 退化为 no-op context manager,中间件
+行为不变。根 `invoke_agent` span 的 `gen_ai.provider.name` /
+`cubepi.agent.system_prompt_sha256` / `cubepi.agent.tools` 始终归属
+agent 的主 provider/model,不会被先跑的 summarizer 覆盖。
+
## 失败行为
如果摘要 provider 失败,CubePi 会记录 warning,并继续使用之前的压缩视图或
diff --git a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/overview.md b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/overview.md
index 4b48459..20bb738 100644
--- a/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/overview.md
+++ b/website/i18n/zh-Hans/docusaurus-plugin-content-docs/version-0.7/guides/tracing/overview.md
@@ -60,6 +60,13 @@ trace
- **`tracing_context()`** —— 通过 contextvar 作用域块为单次运行设置标签和
元数据(`cubepi.tags = ("beta-arm",)`、`cubepi.metadata.user_id = "u-42"`),
并发 agent 各自看到独立的值。
+- **中间件自有 provider 自动接入 trace** —— 中间件通过
+ `Middleware.providers()` 暴露自己持有的 `BaseProvider`,
+ `Recorder.attach()` 会自动给这些 provider 接上 listener 注册表,它们的
+ `chat` span 就和 agent 主调用落进同一条 trace。`CompactionMiddleware`
+ 就用这个机制把摘要 LLM 调用呈现为嵌在
+ `cubepi.compaction.summarize` 下的 `chat <summary-model>` —— 详见
+ [compaction 指南](../middleware/compaction#tracing)。
## 开销
diff --git a/website/versioned_docs/version-0.7/guides/middleware/compaction.md b/website/versioned_docs/version-0.7/guides/middleware/compaction.md
index 5c96073..3891bd1 100644
--- a/website/versioned_docs/version-0.7/guides/middleware/compaction.md
+++ b/website/versioned_docs/version-0.7/guides/middleware/compaction.md
@@ -70,6 +70,30 @@ outputs or user corrections are especially important. Increase
`max_summary_tokens` for long-running research or coding sessions where the
summary needs more detail.
+## Tracing
+
+When `cubepi.tracing` is attached to the agent, the summarizer call is
+first-class in the trace tree. `summarize()` opens a
+`cubepi.compaction.summarize` parent span (tagged with
+`cubepi.compaction.message_count`) around the LLM call, and the recorder
+automatically subscribes the summarizer provider so its `chat` span lands
+inside:
+
+```
+invoke_agent
+└── cubepi.turn
+ ├── cubepi.compaction.summarize
+ │ └── chat <summary-model>
+ └── chat <main-model>
+```
+
+The wrapper span is a no-op context manager when OpenTelemetry isn't
+installed, so the middleware works the same on minimal installs. The
+root `invoke_agent` span's `gen_ai.provider.name` /
+`cubepi.agent.system_prompt_sha256` / `cubepi.agent.tools` continue to
+reflect the agent's main provider/model, not the summarizer's — even
+when summarization runs first.
+
## Failure behavior
If the summary provider fails, CubePi logs a warning and continues with the
diff --git a/website/versioned_docs/version-0.7/guides/tracing/overview.md b/website/versioned_docs/version-0.7/guides/tracing/overview.md
index d6fa591..6632045 100644
--- a/website/versioned_docs/version-0.7/guides/tracing/overview.md
+++ b/website/versioned_docs/version-0.7/guides/tracing/overview.md
@@ -63,6 +63,14 @@ Each layer carries standard `gen_ai.*` attributes — `gen_ai.operation.name`,
(`cubepi.tags = ("beta-arm",)`, `cubepi.metadata.user_id = "u-42"`)
via a contextvar-scoped block. Concurrent agents each see their own
values.
+- **Middleware-owned providers traced automatically** — middleware that
+ exposes extra `BaseProvider` instances via `Middleware.providers()` has
+ its listener registry wired by `Recorder.attach()` so those providers'
+ `chat` spans land in the same trace as the agent's main call.
+ `CompactionMiddleware` uses this to surface its summarizer LLM call as
+ a `chat <summary-model>` span nested under
+ `cubepi.compaction.summarize` — see the [compaction
+ guide](../middleware/compaction#tracing).
## What it costs