Skip to content

fix(platform): steer chat tool budget and derive limits from catalog - #2980

Merged
larryro merged 1 commit into
mainfrom
fix/chat-budget-steer-catalog-limits
Aug 15, 2026
Merged

larryro merged 1 commit into
mainfrom
fix/chat-budget-steer-catalog-limits

Conversation

@larryro

@larryro larryro commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

Problem

Two hardcoded ceilings degraded document-heavy chat turns (repro: 491-page BGB.pdf, a five-topic "详细说说" question):

  1. A turn that hit MAX_TOOL_ROUNDS ended without an answer. The loop withholds the tools on the forced final round, but nothing told the model — mid-plan, it announced its next lookup ("现在让我搜索…") and stopped. The unit test masked this: its fake model conveniently answers when tools vanish; real models don't. The rag_fetch keep-fetching steer made it worse on huge documents.
  2. The reply ceiling was a constant 4096, whatever the catalog declared. deepseek-v4-flash declares 1M context / 384k output on OpenRouter and still got max_tokens: 4096 — the seven-section answer cut off mid-sentence. The flat 96k history cap was the same class of hardcode.

Change

Steer the round budget instead of enforcing it silently (turn.ts, context.ts):

  • Two wire-only user-role notices (the truncation-notice pattern; never persisted): the last offered round tells the model to batch every remaining lookup as parallel calls in that response; the forced round tells it the budget is spent, to answer from what it read, and to state coverage honestly.
  • One tool-docs line declares the per-reply lookup budget and parallel rounds up front.

Derive capability limits from the model catalog (effort.ts, budget.ts, context.ts, turn_action.ts):

  • Default and thinking maxTokens follow the declared maxOutputTokens; 4096 survives only as the undeclared-entry fallback. The one structural rule left is the output share — half the window (the precedent the thinking budget clamp already used) — so a huge declared output can never starve the history slice.
  • New fitSamplingToWindow re-applies that share when governance shrinks the effective window, shrinking a thinking budget alongside so the maxTokens > budgetTokens wire invariant survives.
  • The flat 96k history cap is deleted. The history slice is the effective window minus output reserve and system prompt; cost control belongs to the explicit governance maxContextTokens cap, not a hidden constant. Note the cost surface: long threads on 1M-window models now replay what the window allows.

Verification

  • 75031 platform unit tests, typecheck, oxlint, opengrep green; new tests lock the notices (wire-only, right rounds, never persisted), the declaration-driven ceilings, and the governance re-fit.
  • Live on the BGB.pdf thread (deepseek-v4-flash-0731 via OpenRouter): the capped turn batches six parallel rag_fetch reads in its last round and ends with a complete, cited seven-section answer plus an honest "not read" caveat, instead of a dangling narration. After the ceiling change the wire requests 393216 output tokens instead of 4096, and the previously truncated §§ 355–361 exposition completes with a proper ending; the model's long deliberation lands in the thinking accordion, not the visible text.

A chat turn that hit MAX_TOOL_ROUNDS withheld the tools silently, so the
model announced its next lookup and stopped instead of answering, and the
reply ceiling was a hardcoded 4096 whatever the model declared.

Steer the round budget instead of enforcing it silently:

- Wire-only user-role notices (the truncation-notice pattern, never
  persisted): the last offered round tells the model to batch every
  remaining lookup as parallel calls, the forced round tells it the budget
  is spent and to answer from what it read, stating coverage.
- One tool-docs line declares the per-reply lookup budget up front.

Derive capability limits from the model catalog instead of constants:

- Default and thinking maxTokens follow the declared maxOutputTokens
  (4096 only as the undeclared fallback), under an output share of half
  the window so the reply never starves the history slice.
- fitSamplingToWindow re-applies the share when governance shrinks the
  effective window, shrinking a thinking budget alongside to keep the
  maxTokens > budgetTokens wire invariant.
- The flat 96k history cap is gone: the history slice is the effective
  window minus reserve and system prompt; cost control belongs to the
  governance maxContextTokens cap alone.

Verified live against a 491-page BGB.pdf: the capped turn now batches six
parallel rag_fetch reads in its last round and ends with a complete cited
answer, and deepseek-v4-flash (1M window / 384k output declared) requests
393216 output tokens instead of 4096, finishing the previously truncated
section with a proper ending.
@larryro
larryro merged commit e433304 into main Aug 15, 2026
50 checks passed
@larryro
larryro deleted the fix/chat-budget-steer-catalog-limits branch August 15, 2026 06:07
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