Skip to content

fix: preflight summary — name and recover thinking-budget exhaustion - #856

Closed
ranxianglei wants to merge 3 commits into
masterfrom
2026-09-16_preflight-reasoning-diagnosis
Closed

ranxianglei wants to merge 3 commits into
masterfrom
2026-09-16_preflight-reasoning-diagnosis

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Fixes #853

Problem

For thinking-on-by-default models (deepseek-flash / DeepSeek-V4.1-Flash), the upstream writes chain-of-thought into reasoning_content while content is often "". Reasoning tokens share the summary request's max_tokens budget; once the budget is eaten by thinking, the call returns HTTP 200 with finish_reason:"length", content:"" and a non-empty reasoning_content. bili judged "upstream returned no summary", discarded it, retried with smaller chunks (which cannot help — the thinking overhead is per-call), burned all 8 rounds and 502'd. Reported hit 4× in 2 days on 0.1.116.

Two independent defects:

  1. diagnoseEmptySummary mislabeled this as "the upstream returned a non-SSE body with no summary text" — sending operators chasing gateways/MITM instead of the real cause (output-budget exhaustion).
  2. The existing recovery path (smaller-chunk retry) structurally cannot recover these, because a single call's reasoning can exceed the whole max_tokens=8192 cap regardless of chunk size.

Changes

Commit 1 — accurate diagnosis. New diagnoseReasoningExhaustion() detects the exhaustion signature (empty content + non-empty reasoning_content / finish_reason) and names the real cause: "the model spent its output budget on reasoning/thinking … this is a budget limit, not an upstream failure." diagnoseEmptySummary() now also classifies from the raw body when the parsed JSON isn't passed in.

Commit 2 — bounded budget retry. On that exact signature (finish_reason:"length" + empty content + non-empty reasoning_content), requestSummary() now retries once with a doubled max_tokens (8192 → 16384) before giving up — bounded to exactly two upstream calls per range, logged as budget-insufficient retry (#853) for grep-ability. summaryPayload() is parameterized to carry the raised cap. Per the reporter's sign-off, single-call reasoning reaches ~9500 tokens (> 8192), so chunk-splitting provably cannot save these.

Per the issue discussion we deliberately did not fall back to reading reasoning_content as the summary (it would inject CoT scratchpad into compressed blocks and degrade quality); the main loop already carries separate reasoning-truncation handling.

Scope note

The retry predicate keys off the OpenAI/Anthropic choices[] shape (the reported case). Responses-API bodies use output[] and are out of scope here — they'd need an analogous check if that protocol exhibits the same behavior.

Verification

  • npm run typecheck — clean
  • npm test — 1652 tests, 1650 pass, 0 fail, 2 skipped (e2e-gated)
  • New regression tests tests/preflight-budget-retry.test.ts: retry-once-then-succeed (asserts the budget escalates 8192 → 16384 and the summary is applied) and stop-after-one-retry (asserts no unbounded loop when the raised budget still exhausts)
  • Item-1 regression test in tests/preflight-empty-summary.test.ts
  • npm run build — success; both log markers present in dist/index.js

ework-agent added 2 commits September 16, 2026 20:20
…osis

A thinking-on-by-default model spends its shared max_tokens budget on chain-of-thought before emitting any answer, so the preflight summary call returns HTTP 200 with finish_reason:"length", content:"" and a non-empty reasoning_content. diagnoseEmptySummary fell through to "the upstream returned a non-SSE body with no summary text", sending operators chasing gateways/MITM instead of the real cause (output-budget exhaustion). Add diagnoseReasoningExhaustion to detect this signature and emit an actionable message, and let diagnoseEmptySummary classify from the raw text when the caller does not supply the parsed json.

Closes #853
…sts the summary output budget

A thinking-on-by-default model spends its shared max_tokens budget on chain-of-thought before emitting any answer, so the preflight summary call returns HTTP 200 with finish_reason:"length" and content:"". Splitting the chunk smaller cannot recover these because the thinking overhead is per-call, so the existing smaller-chunk retry path keeps failing and preflight 502s. On that exact signature (finish_reason:length + empty content + non-empty reasoning_content) requestSummary now retries ONCE with a doubled max_tokens (8192 -> 16384) before giving up, bounded to exactly two upstream calls per range and logged as "budget-insufficient retry (#853)" for grep-ability. summaryPayload is parameterized to carry the raised cap.

Refs #853
@github-actions

github-actions Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-09-16_preflight-reasoning-diagnosis (e46552f)

Option A — Install from npm PR tag (recommended)

npm install -g billion-context@pr-856

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

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr856.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 PR review complete — verified, one small defect fixed directly on the branch, one CI flake identified.

Verified (pulled 2026-09-16_preflight-reasoning-diagnosis, ran locally on Node 22):

  • npm run typecheck clean; npm test 1652 tests / 1650 pass / 0 fail / 2 skipped (e2e-gated); npm run build success; both log markers present in dist/index.js.
  • Diff is clean: 3 files, all on-topic (src/preflight.ts + 2 test files), no version/lockfile churn.
  • Retry mechanics check out: exactly two upstream calls per range (guard maxOutputTokens < MAX_SUMMARY_OUTPUT_TOKENS_RETRY), escalation 8192→16384 asserted by the new regression tests, no partial block applied when the raised budget still exhausts. Deliberately NOT reading reasoning_content as summary — agreed, correct call.
  • diagnoseEmptySummary has no callers outside src/preflight.ts, so the new parse-from-raw-text classification is contained.

Fixed directly (commit e46552f): diagnoseReasoningExhaustion() fired on any non-empty finish_reason with empty content — including finish_reason:"stop" with no reasoning at all. That mislabeled a plain empty-stop completion (refusal/moderation/gateway quirk) as "spent its output budget on reasoning/thinking … raise the preflight max_tokens" — the exact class of misleading diagnosis this PR exists to eliminate, and the advice is wrong for that shape. Now it fires only on visible reasoning or finish_reason:"length"; added the regression assertion in tests/preflight-empty-summary.test.ts. Re-ran typecheck + full suite + build after the change: all green.

CI status: test (ubuntu-latest, 24) passed; test (ubuntu-latest, 22) failed on tests/logger-rotation.test.ts → "external rename: subsequent lines land in the new file, .old frozen" (AssertionError: new bili.log must be recreated). That test comes from #210 (inode-compare rotation detection), is untouched by this PR, and depends on write/stat timing between the external rename and the next log line. It passes 8/8 locally on Node 22. Verdict: runner-timing flake, not a PR regression — please re-run the failed job (my push re-triggers the whole matrix anyway).

Two report-only findings (no action taken, your call):

  1. fix: preflight tries all viable ranges before declaring exhaustion (#569) #574 call-budget accounting. summaryCalls counts logical range attempts, not HTTP calls — the new budget retry makes the true worst case 16 upstream calls per protection regime instead of 8, and each exhausted call on a thinking model burns up to its full budget of billed reasoning tokens (~16k on the escalated attempt). It's bounded, logged, and the failure mode is terminal either way (preflight 502s regardless), and transient-error replays in requestSummaryBody already multiply outside this counter — but if you want strict fidelity to fix: preflight tries all viable ranges before declaring exhaustion (#569) #574's "bound on upstream summarization calls", the counter should charge the extra attempt (thread the attempt count out of requestSummary), or at minimum the #574 comment should document the 2× multiplier.
  2. 400 on the raised cap (openai/anthropic protocols). If an upstream accepts max_tokens:8192 but rejects 16384 with 400, the throw propagates through summarizeRange (its 400-adaptation loop only learns max_output_tokens rejections for the responses protocol) and hard-fails the whole preflight walk with HTTP 400 instead of continuing. Low probability — the reported model demonstrably emits >9.5k reasoning tokens, so its real cap is >8192 — but a cheap hardening would be catching a 400 on the escalated attempt and falling back to the first attempt's exhaustion diagnosis as {unusable}.

Scope note confirmed: Responses-API bodies (output[]) remain out of scope, consistent with the PR description.

Once CI is green after the re-trigger, this is mergeable from my side — merging stays with you.

审查结论:PR 代码正确、diff 干净、回归测试到位;我已在分支上直接补修一个诊断误报(stop+空 content 且无 reasoning 时不再误判为"预算耗尽"并给出错误的调大 max_tokens 建议,commit e46552f,改后 typecheck/全量测试/build 均绿);CI 里 ubuntu-22 的失败是无关的 logger 轮转测试(#210)在慢 runner 上的偶发失败(本地 Node 22 跑 8/8 通过),重跑该 job 全绿后即可合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 CI update: all 7 checks now pass on head e46552f — test (ubuntu-latest, 22) / test (ubuntu-latest, 24) / both Windows jobs / zero-token e2e / version-guard / build-artifact. The earlier ubuntu-22 failure was indeed the unrelated logger-rotation flake; it passed on re-run. PR is fully green and ready to merge whenever you are (merge stays with you).

CI 更新:新 head e46552f 上全部 7 项检查通过(含此前偶发失败的 ubuntu-22 logger 测试,重跑即过),PR 已全绿可合并。

@ranxianglei

Copy link
Copy Markdown
Owner Author

这个是最大值自己写小了又通过重试搞大,狗屁不通

ranxianglei added a commit to joshwapohlmann/billion-context that referenced this pull request Sep 17, 2026
…xianglei#853)

The summary call's 8192 output cap starves reasoning-on-by-default models:
reasoning_content spends the shared budget before any answer text (observed
~9.5k reasoning tokens on deepseek-flash, real ceiling 384k), guaranteeing
content:"" + finish_reason:"length".

- MAX_SUMMARY_OUTPUT_TOKENS 8192 -> 32768 (~3x headroom over observed)
- clamp per model against known ceilings below the default, from a slim
  models.dev snapshot (npm run models-dev:snapshot; caps >= 32768 are
  omitted — they can never affect the clamp; max on cross-provider id
  collisions so a low claim cannot re-starve a model)
- src/model-output-limits.ts: exact-id lookup with single namespace strip,
  same two-step resolution as the codex window lookup

Supersedes ranxianglei#856 (hard-coded cap table, closed).
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.

[Question] preflight 摘要为空就判定"上游没返回内容" —— thinking 默认模型的 reasoning_content 是否有意不参与?

1 participant