Skip to content

fix: avoid one-token replies when context is exhausted - #161748

Closed
jacobtomlinson wants to merge 5 commits into
openclaw:mainfrom
jacobtomlinson:fix/one-token-context-overflow-161254
Closed

jacobtomlinson wants to merge 5 commits into
openclaw:mainfrom
jacobtomlinson:fix/one-token-context-overflow-161254

Conversation

@jacobtomlinson

@jacobtomlinson jacobtomlinson commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Closes #161254

What Problem This Solves

Fixes one-token length replies when an OpenAI-compatible request grows beyond its configured context window.

User Impact

When the estimated input exhausts an automatically chosen output budget, affected sessions enter context-overflow recovery and receive a visible error instead of treating a one-word partial completion as a normal reply. Deliberately requested short completions still reach the provider, including when the conservative estimate says the budget is exhausted.

Why This Change Was Made

The transport previously forced an automatically chosen remaining output budget to at least one token even when its input estimate exhausted the window. It now raises context_length_exceeded for zero or negative remaining budget unless the caller explicitly requested fewer than 16 output tokens through runtime options or supported model parameters (maxTokens, max_completion_tokens, or max_tokens). Such deliberate short requests retain the shipped one-token fallback. Positive 1–15-token budgets remain usable for non-reasoning and thinking-off requests, and the existing reasoning-enabled guard still rejects automatically clamped output below 16 tokens.

Conservative-estimate tradeoff

A conservative estimate can reject an automatic request that a provider tokenizer would fit. The loopback control exercises that case: the synthetic provider accepts the same prompt when a one-token cap is explicit, while the automatic request enters context-overflow recovery before provider I/O. This PR chooses visible, recoverable overflow for automatically exhausted requests to avoid silently treating a truncated one-token answer as a complete turn. Explicit short caps remain provider-controlled. Maintainer review of this compatibility choice is requested before merge.

Evidence

  • The exhausted automatic-budget regression failed on the original transport because no error was raised, then passed with this change. Tests cover positive 1- and 15-token budgets, explicit short caps from runtime options and all supported model-parameter names at exhausted estimates, Qwen thinking off at 15 tokens, and reasoning enabled at 15 tokens.
  • Focused transport tests passed on the current head: node scripts/run-vitest.mjs packages/ai/src/transports/openai-completions-params.test.ts packages/ai/src/transports/openai-completions-stream.integration.test.ts --maxWorkers=1 (4.64 seconds for one shard, 46 tests). The loopback HTTP control took 93 ms; it observes no provider request for an automatic exhausted budget and successful one-token replies for both runtime and configured caps.
  • An isolated real Gateway with a synthetic loopback OpenAI-compatible provider accepted a large chat.send, returned agent.wait.status=error with a visible “Context overflow: prompt too large for the model” recovery reply, and sent zero requests to the provider; no one-token request was emitted. The temporary proof harness was removed after the run. No live external provider was used.
  • The scoped changed-file gate passed on the preceding merged head. For this follow-up, the package test TypeScript graph, targeted lint and formatting, git diff --check, and focused tests passed.
  • Fresh independent Codex autoreview of the staged follow-up diff was scoped-clean with no actionable findings.

@clawsweeper

clawsweeper Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@jacobtomlinson
jacobtomlinson marked this pull request as ready for review September 30, 2026 08:11
@clawsweeper clawsweeper Bot added P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Sep 30, 2026
@clawsweeper

clawsweeper Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: blocked before merge. Reviewed October 2, 2026, 3:13 AM ET / 07:13 UTC (Revision 5).

ClawSweeper review

What this changes

The OpenAI-compatible transport rejects automatically exhausted output budgets before provider requests while preserving deliberate short caps from runtime options and model parameters, with parameter and HTTP coverage.

Merge readiness

⛔ Blocked before merge - 3 items remain

The prior configured-short-cap finding is resolved, and no remaining actionable code defect was found. The fix remains necessary, but its broader conservative-estimate refusal policy needs maintainer acceptance.

Priority: P1
Reviewed head: 36e79d1855b55176ae8266eb7c2cc04fd8af24c3
Owner decision: Required. See Decision needed.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused repair with meaningful production-boundary observations and the prior finding resolved; the remaining blocker is an explicit compatibility-policy choice.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): Recorded isolated Gateway chat.send observations show the changed budget owner producing visible overflow guidance without provider I/O, while the real HTTP transport control shows successful runtime and configured short-cap replies. This proves refusal and visible failure, not successful compaction or external tokenizer accuracy; no stored-data contract changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (live_output): Recorded isolated Gateway chat.send observations show the changed budget owner producing visible overflow guidance without provider I/O, while the real HTTP transport control shows successful runtime and configured short-cap replies. This proves refusal and visible failure, not successful compaction or external tokenizer accuracy; no stored-data contract changes.
Evidence reviewed 9 items Pinned introduced change: Reviewed the verified merge-base-to-head delta and the original production blob. The new refusal is confined to managed proxy-like requests; direct request behavior is unchanged. The stale test merge was not used to infer removal of current-main behavior.
Previous blocker resolved: The follow-up carries explicit provenance from runtime maxTokens and the shared resolver for maxTokens, max_completion_tokens, and max_tokens. Current coverage checks each configured alias and demonstrates successful runtime and configured one-token requests through the real HTTP transport.
Current main still needs the fix: Current main still floors exhausted estimated budgets at one token and only throws for clamped thinking-enabled requests below the existing useful-output floor. Non-reasoning and thinking-off exhaustion remains affected.
Findings None None.
Security None None.

How this fits together

The OpenAI-compatible transport converts conversation context and model settings into provider requests. Its output-budget decision determines whether a request reaches the provider or enters existing context-overflow recovery.

flowchart TD
  A[Conversation and tools] --> C[Request preparation]
  B[Model settings and output cap] --> C
  C --> D[Estimate remaining context]
  D --> E{Exhausted automatic budget?}
  E -->|Yes| F[Overflow recovery or visible error]
  E -->|No or deliberate short cap| G[Provider request and reply]
Loading

Decision needed

Question Recommendation
Should automatically exhausted non-reasoning and thinking-off proxy requests be refused using the conservative estimate by default, even when the provider could fit them? Preserve compatibility and offer strict refusal: Retain shipped provider-controlled behavior by default and authorize an explicit strict policy with coverage for both modes.

Why: The control demonstrates a real compatibility tradeoff, and the prior recorded acceptance was limited to thinking-enabled stalls.

Before merge

  • Resolve merge risk (P1) - The 1.25-margin estimate can reject an automatic non-reasoning or thinking-off request that its provider could accept; an existing setup may begin compacting or fail visibly after upgrade. The provider-fitting control documents this tradeoff, but maintainer acceptance of the broader default remains outstanding.
  • Complete next step (P2) - Have the transport owner decide whether to accept conservative automatic refusal as the default or preserve compatibility with an opt-in strict policy.
  • Resolve maintainer decision - Resolve the maintainer decision shown above before merge.
Agent review details

Security

None.

PR surface

Source +19, Tests +97. Total +116 across 3 files.

View PR surface stats
Area Files Added Removed Net
Source 1 28 9 +19
Tests 2 106 9 +97
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 3 134 18 +116

Review metrics

Metric Value Why it matters
Exhaustion policy 1 default behavior changed; 0 policy options added Existing proxy setups receive the broader refusal automatically, making owner acceptance material before merge.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #161254
Summary: This PR is a candidate fix for its linked report; related work covers the same exhaustion mechanism with a broader estimator policy.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Merge-risk options

Maintainer options:

  1. Keep provider-controlled defaults (recommended)
    Authorize an opt-in strict policy and verify both the compatibility default and strict refusal before merging that direction.
  2. Accept conservative refusal
    Record explicit owner acceptance that provider-fitting automatic requests may compact or fail visibly under the new default.

Technical review

Best possible solution:

Keep overflow handling with the existing recovery owner; prefer preserving provider-controlled compatibility by default and making conservative refusal opt-in unless maintainers explicitly accept the broader default.

Do we have a high-confidence way to reproduce the issue?

Yes, source establishes the current-main path: a non-reasoning proxy model with a 1000-token context and a 3200-character system prompt receives a one-token cap. This review did not execute it; the linked reports and recorded Gateway observations supply runtime context.

Is this the best way to solve the issue?

Unclear pending policy acceptance. The revised provenance handling is a narrow repair, but conservative default refusal can reject provider-fitting requests; preserving compatibility with an opt-in strict policy is safer for upgrades.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 9af47211ed1e.

Labels

Label changes:

No label changes.

Label justifications:

  • P1: Reported growing sessions repeatedly return unusable one-token replies and block agent task progress.
  • merge-risk: 🚨 compatibility: The new automatic refusal can stop provider-fitting requests in existing non-reasoning and thinking-off setups.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (live_output): Recorded isolated Gateway chat.send observations show the changed budget owner producing visible overflow guidance without provider I/O, while the real HTTP transport control shows successful runtime and configured short-cap replies. This proves refusal and visible failure, not successful compaction or external tokenizer accuracy; no stored-data contract changes.
  • proof: sufficient: Contributor real behavior proof is sufficient. Recorded isolated Gateway chat.send observations show the changed budget owner producing visible overflow guidance without provider I/O, while the real HTTP transport control shows successful runtime and configured short-cap replies. This proves refusal and visible failure, not successful compaction or external tokenizer accuracy; no stored-data contract changes.

Evidence

What I checked:

Likely related people:

  • steipete: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)
  • obviyus: Suggested for follow-up; no historical authorship or introduction is verified. (role: unverified routing candidate; confidence: low)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (4 earlier review cycles)
  • reviewed 2026-09-30T08:17:58.321Z sha 6e9743b :: blocked before merge. :: [P1] Preserve working short requests outside the exhausted case
  • reviewed 2026-09-30T17:43:15.082Z sha 6c8b24c :: blocked before merge. :: [P1] Preserve explicitly requested short completions
  • reviewed 2026-09-30T17:59:10.664Z sha 19d5f39 :: blocked before merge. :: [P1] Preserve explicitly requested short completions
  • reviewed 2026-10-01T15:11:07.760Z sha 76e6f68 :: blocked before merge. :: [P1] Preserve deliberate short caps supplied through model parameters

@jacobtomlinson

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. labels Sep 30, 2026
@jacobtomlinson

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@jacobtomlinson

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Oct 2, 2026
steipete added a commit that referenced this pull request Oct 9, 2026
Apply the existing useful-output floor to every context-driven clamp,
including non-reasoning and thinking-off requests. Keep deliberately
short caps when they fit and report exhausted room as context overflow.

Builds on the investigations in #161748 and #158161.
Refs #161254, #105729, #158159.

Co-authored-by: Jacob Tomlinson <[email protected]>
Co-authored-by: Alex Mittell <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 10, 2026
…67878)

Apply the existing useful-output floor to every context-driven clamp,
including non-reasoning and thinking-off requests. Keep deliberately
short caps when they fit and report exhausted room as context overflow.

Builds on the investigations in openclaw#161748 and openclaw#158161.
Refs openclaw#161254, openclaw#105729, openclaw#158159.

Co-authored-by: Jacob Tomlinson <[email protected]>
Co-authored-by: Alex Mittell <[email protected]>
RomneyDa pushed a commit that referenced this pull request Oct 10, 2026
Apply the existing useful-output floor to every context-driven clamp,
including non-reasoning and thinking-off requests. Keep deliberately
short caps when they fit and report exhausted room as context overflow.

Builds on the investigations in #161748 and #158161.
Refs #161254, #105729, #158159.

Co-authored-by: Jacob Tomlinson <[email protected]>
Co-authored-by: Alex Mittell <[email protected]>
(cherry picked from commit e05ca71)
@steipete

Copy link
Copy Markdown
Contributor

Thanks for this, and for the investigation behind it. The behavior this PR targets (oversized OpenAI-compatible requests returning one-token length replies instead of entering context-overflow recovery, #161254) is now fixed on main by #167878 (e05ca71), which closed #161254. That change keeps the estimator unchanged and removes the one-token clamp while preserving automatic output caps that genuinely fit, a slightly narrower shape than this PR's. Your work is credited there with Co-authored-by.

Closing this as superseded. If you see a one-token reply or a missing overflow error on a current build, please open an issue (or reopen this) with the request details and we'll take another look.

@steipete steipete closed this Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P1 High-priority user-facing bug, regression, or broken workflow. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Oversized OpenAI-compatible requests return one-token replies instead of context errors

2 participants