Skip to content

fix(overflow): arm self-heal on ambiguous no-body 4xx errors - #215

Merged
ranxianglei merged 1 commit into
masterfrom
2026-08-23_overflow-nobody-arm
Sep 26, 2026
Merged

ranxianglei merged 1 commit into
masterfrom
2026-08-23_overflow-nobody-arm

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

fix(overflow): arm self-heal on ambiguous no-body 4xx errors

Model: GLM-5.3 (zhipuai-lb, via pi coding agent) — per AGENTS.md rule 2.
Branch: 2026-08-23_overflow-nobody-arm (based on origin/master @ 5a834ef) · Commit: c355c4e

Problem

Real incident (2026-08-23): an ACP-managed pi session dead-looped. A 50,358-char bash toolResult (~31,475 tokens, ~24% of the effective 131,072-token window) pushed the request past sglang's input + max_tokens ≤ 262,144 hard cap. Every subsequent request returned 400 status code (no body) forever:

  • The model never got a successful turn, so it could never call compress — the ACP loop never shrank the context.
  • The user's "继续" just resent the same oversized context.
  • The extension's overflow self-heal never fired: it only arms on OVERFLOW_MARKER text markers (maximum context length is N, prompt is too long, …), and a bodyless 4xx carries none of them.
  • pi itself classifies this text as overflow (pi-ai OVERFLOW_PATTERNS ends with /^4(?:00|13)\s*(?:status code)?\s*\(no body\)/i), but the extension-side marker set deliberately never matched it — for good reason: OpenAI-compatible providers return the exact same no-body 400 for non-overflow reasons (invalid model, malformed request; see src/messages.ts:80-82), so treating it as an unconditional overflow risks false positives.

Fix

Treat pi's bodyless 4xx ... (no body) error text as a possible overflow signal, and arm the emergency self-heal (forced ≥95% usage → kernel emergency nudge + tool-result truncate) only when at least one false-positive guard holds:

  1. Ratio guard: ACP's current sent-view token estimate ≥ 50% (NO_BODY_ARM_RATIO) of the effective limit — the same pct basis already logged per turn (tokenCount / config.modelContextLimit, recorded per request in the context event, before the armed boost so the guard sees the real estimate).
  2. Consecutive guard: it is the ≥2nd consecutive no-body 4xx since the last successful assistant turn. Count resets on a successful assistant turn and at session start (per-session OverflowEpisode), recovering the dead-loop even when the estimate under-reports (the incident ran at ~24%: the sent-view estimate cannot see the provider's input+max_tokens accounting).

When the body has no parseable limit number (always, for a bodyless error), no window is learned — the armed emergency uses the already-resolved effective limit, so arming never fails for lack of a parsed number.

The classic text-marker arm path (inspectOverflowMessage → learn window + arm) is unchanged.

Files changed

File Change
src/overflow-selfheal.ts NO_BODY_4XX_MARKER + isNoBody4xxError() (mirrors pi-ai's anchored regex); NO_BODY_ARM_RATIO = 0.5; OverflowEpisode.noteSentView/noteSuccess/onNoBody4xx() (returns {arm, consecutive, ratio}); reset() clears the new state. Guard state is runtime/in-memory like the armed flag — not persisted to acp.json (justified in comment: a resumed session re-establishes both within one turn; a persisted count would arm against a fresh session whose first request may succeed).
src/index.ts wireContextTransform: record noteSentView(tokenCount, config.modelContextLimit) before the armed boost. wireOverflowSelfHeal (message_end): non-error assistant turns call noteSuccess(); error turns run the classic marker path unchanged, else the no-body path with the two guards; arms + logs no-body-arm (consecutive, ratio) + UI notify.
tests/overflow-selfheal.test.ts 10 new tests (21 → 31 in file).
CHANGELOG.md Unreleased entry.

Tests

npm run typecheck ✅ · full suite npm test ✅ 422 pass / 0 fail (was 412; +10):

  • isNoBody4xxError matches 400/413 status code (no body), 400 (no body), 413(no body); rejects 429/500/404 (no body), quota text, empty, and prefixed text (anchored, like pi-ai).
  • no-body at low estimate (~24%, incident scale) + first occurrence → NO arm.
  • no-body at ≥50% estimate → arm on first occurrence (incl. exact-threshold >= case).
  • first no-body at low estimate, then a second consecutive no-body → arm (dead-loop recovery).
  • classic maximum context length is 262144 ... → still arms via the text-marker path, window parsed (regression).
  • successful turn resets the consecutive count; OverflowEpisode.reset() clears sent-view + count; fresh episode (no recorded sent view) does not arm on the first hit.

Notes

@github-actions

github-actions Bot commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Extension Artifact

Branch: 2026-08-23_overflow-nobody-arm (819e415)

Option A — Install from npm PR tag (recommended)

pi install npm:billion-context-pi@pr-215

Each push to this PR publishes a new version under the pr-215 npm tag.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pi-pr215.tgz
pi install ./package

This comment is automatically updated on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

Review round 1 (agent reviewer — verdict: ship):

  • Regex matches pi's real surface form: openai-completions.js:451 passes the openai SDK's verbatim 400 status code (no body) with no prefix; pi-ai's own classifier uses the identical anchored regex. 401/403/429 can't match.
  • Healthy path unchanged: success branch only adds noteSuccess(); nothing outbound; state in-memory only.
  • Emergency fires only during genuine recovery (armed consumed once per arm; success resets; no healthy-path rewrite of old messages).

Minor:

  • False-positive surface is real but gated: persistent non-overflow bodyless 400s are documented in-repo (GLM empty-assistant quirk, messages.ts:81; body-stripping relays). ≥2-consecutive arms → per-turn view truncation + nudge spam until the first success; originals survive in the session file. Accepted trade for the dead-loop it fixes.
  • No wiring-level test pinning noteSentView before the armed boost (property exists only as a comment).
  • Merge order vs fix(arbitration): prefer provider-anchored usage over calibrated estimate #214 (guaranteed src/index.ts conflict): place ov.noteSentView(...) after fix(arbitration): prefer provider-anchored usage over calibrated estimate #214's anchored max-merge and before the armed boost — then the ratio guard sees the provider-anchored basis and arms on the first no-body error in an incident (anchored ~100% ≥ 50% immediately), instead of waiting for the second.

@ranxianglei

Copy link
Copy Markdown
Owner Author

分诊(队列清理 #484): KEEP-VALID。

缺口在 master 上仍真实存在:src/overflow-selfheal.ts:48-51 的 inspectOverflowMessage 在 body 为空时直接返回 {isOverflow:false} —— 对 sglang/vLLM 空 body 的 400/413 无能为力,master 无任何 no-body 4xx 佐证逻辑(grep NO_BODY_4XX|isNoBody4xxError 零命中)。

设计上对误报的防备是周全的:仅当 N 次连续 no-body 4xx(自上次成功 turn 起)+ 与记录的 sent-view 估算互证后才 arm,arm 后 boost 前仍查 guard。

rebase 风险:重。 wireContextTransform(原 noteSentView 记录点)已被 #257/#455/#289 的 floors/calibration 重写,PR 所 patch 的 calibrateTokens(sentTokens, runtime.density…) 行已不存在;wireOverflowSelfHeal 移到了 src/index.ts:823+。两个钩子需按新代码手工重放。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

✅ Rebased onto current master and force-pushed — branch 2026-08-23_overflow-nobody-arm is now 55e549f on top of 72d8771 (was c355c4e on stale base 5a834ef). mergeable: true, no conflicts; GitHub CI 9/9 green (test ×4 OS/node matrix, e2e ×2, pr-validation, docker, build-artifact).

How the two hooks were replayed (manual re-application per triage):

  1. wireContextTransform sent-view recording — the original patch point (calibrateTokens(sentTokens, runtime.density…)) no longer exists after the floors/calibration rewrite (估算错误导致紧急压缩从不触发 #257/[Bug] Context size accounting diverges from provider usage 1.5-2x and never re-anchors: first-sight-mass stuck on, emergency state persists after context recovered below limit #455/[Bug] Token 计数与压缩后状态不同步,导致低收益及零收益的连续压缩 #289). ov.noteSentView(...) is now called right after the view-recount block, recording a guard basis = max(calibrated sent-view, provider-usage floor) — deliberately excluding the armed 95% floor. That floor is an artifact of a prior arm; feeding it back would let the ratio guard re-arm unconditionally right after an emergency, defeating the corroboration design. The provider-usage floor stays in the basis because it's real size evidence. Placement is pre-armed-boost as originally intended (fix(arbitration): prefer provider-anchored usage over calibrated estimate #214's anchored-max ordering guidance no longer applies — that PR closed unmerged).
  2. wireOverflowSelfHeal message_end — relocated to its new position in src/index.ts; restructure applied verbatim from c355c4e: a successful turn resets the consecutive counter, and no-body 4xx arms only through onNoBody4xx() corroboration (ratio ≥ 0.5 against the recorded sent-view, or second consecutive hit).

Verification on the rebased head:

  • typecheck clean
  • full suite: failure set byte-identical to clean master (82 pre-existing local-environment failures, identical before/after this change, unrelated to it); overflow-selfheal.test.ts 36/36 pass including the 10 new tests
  • production build OK (self-contained bundle)
  • GitHub CI: 9/9 green

PR is ready for human merge.

中文摘要:已按分诊把 #215 的两个钩子手工重放到重写后的 master(sent-view 记录点改用排除 armed floor 的 guard basis,避免紧急压缩后无条件再 arm;message_end 逻辑原样重放到新位置),本地测试与干净 master 失败集一致、新增 10 个用例全过、GitHub CI 全绿,可以合并。

Rebased onto master @ 72d8771 (queue cleanup #484 triage: KEEP-VALID).
Both hooks manually replayed against the rewritten code:

- wireContextTransform (#257/#455/#289 floors/calibration): noteSentView
  now records a guard basis = max(calibrated sent-view, provider-usage
  floor) — deliberately WITHOUT the armed 95% floor, so the ratio guard
  sees real size evidence and cannot re-arm unconditionally right after
  an emergency (original intent of "recorded before the armed boost").
- wireOverflowSelfHeal (moved to src/index.ts): message_end restructure
  verbatim from c355c4e — success resets the consecutive count,
  no-body 4xx arms only via OverflowEpisode.onNoBody4xx() corroboration.

Verified on rebased head: typecheck clean; full suite failure set
byte-identical to clean master (82 pre-existing sandbox failures from
read-only /tmp mounts); +10 new overflow tests all pass; build OK.
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.

1 participant