Skip to content

fix(agent-runtime): shrink the retained-tail fallback until it fits - #2501

Merged
vastsa merged 1 commit into
mainfrom
fix/compaction-stream-abort-2494
Nov 21, 2026
Merged

vastsa merged 1 commit into
mainfrom
fix/compaction-stream-abort-2494

Conversation

@vastsa

@vastsa vastsa commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Fixes #2494.

Root cause

The retained-tail fallback sized its window from the local token estimate, but persistCheckpoint re-estimates with the calibrated estimate. When the provider counts more tokens than the local estimate (calibration ratio ≳1.8, e.g. CJK-heavy context), every summary failure past the hard limit produced an oversized fallback, so the turn failed with CONTEXT_COMPACTION_FAILED — and every later message took the same path.

Change

  • recoverCompactionFailure halves the fallback window until the calibrated estimate fits (down to 1/128; the newest message is always kept).
  • The CONTEXT_COMPACTION_FAILED turn error now names the checkpoint failure cause.
  • CONTEXT_COMPACTION_FAILED is classified origin: "local", so it no longer carries stale providerStatus / streamMs from the previous request (the misleading providerStatus: 200, streamMs: 0 in the report).
  • Spec 02-agent-runtime.md updated.

Validation

  • New regression tests (failed before the fix): full prompt → tool loop with calibration 1.8 and a failing summary request; error message carries the cause.
  • vitest run in agent-runtime: 2083 passed; system-ca.test.ts flaked once under load and passes in isolation.
  • tsc --noEmit, biome lint, docs checks pass.

The retained-tail recovery sized its window from the local token
estimate, while the install check re-estimated with the calibrated one.
When a provider counts more tokens than the estimate, every summary
failure past the hard limit produced an oversized fallback and the turn
ended with CONTEXT_COMPACTION_FAILED, on every later message too.

The recovery now halves the window until the calibrated estimate fits,
keeping the newest message. The resulting turn error names the
checkpoint failure and is classified as local, so it no longer carries
stale stream timings or HTTP status from an earlier request.

fixes #2494
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying pi-desktop with  Cloudflare Pages  Cloudflare Pages

Latest commit: d7dbeac
Status: ✅  Deploy successful!
Preview URL: https://9336ed46.pi-desktop.pages.dev
Branch Preview URL: https://fix-compaction-stream-abort.pi-desktop.pages.dev

View logs

@vastsa
vastsa merged commit 4f5c99d into main Nov 21, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 自动上下文压缩在 0 毫秒中止,错误被标记为 retriable: false,会话永久卡死

1 participant