Skip to content

fix(agents): preserve preflight overflow token counts into recovery budgeting#101181

Merged
jalehman merged 1 commit into
mainfrom
b62.5/preflight-overflow-budgeting
Jul 6, 2026
Merged

fix(agents): preserve preflight overflow token counts into recovery budgeting#101181
jalehman merged 1 commit into
mainfrom
b62.5/preflight-overflow-budgeting

Conversation

@jalehman

@jalehman jalehman commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

When a rendered prompt overflows preflight token checks, the overflow token counts observed during preflight were dropped before recovery ran. Compaction engines then compacted against a stale/underestimated size instead of the prompt OpenClaw actually rendered, and context-engine assembly was not budgeted against the compaction reserve plus rendered prompt pressure before the precheck.

Why This Change Was Made

Recovery decisions should use the best token evidence available at the moment of failure:

  • A buildPreflightRecoveryBudgetSnapshot helper captures estimated prompt tokens, pre-reserve prompt budget, and observed overflow tokens into every token-pressure-driven preflightRecovery assignment.
  • Overflow recovery falls back observedOverflowTokens → preflightEstimatedPromptTokens → minimally-over-budget, and logs preflightEstimatedTokens= for diagnosis.
  • Context-engine assembly budgets tokenBudget as context budget minus compaction reserve minus rendered prompt pressure, passing the reserve as maxOutputTokens.

The aggregate tool-result char-pressure recovery site keeps no snapshot deliberately — no token estimate exists in that scope; the optional fields plus the fallback chain cover it.

User Impact

Agents recovering from prompt overflow compact against realistic sizes, reducing repeated overflow/compaction loops on long sessions. No config or API changes; the new preflightRecovery fields are optional.

Evidence

  • New synthetic-overflow test in run.overflow-compaction.test.ts and a context-engine assembly budget test in run/attempt.spawn-workspace.context-engine.test.ts; 120 tests pass across the two touched test files.
  • tsgo core + core-test lanes clean; oxfmt/oxlint clean on all six touched files.
  • Structured review (codex/gpt-5.5) clean, zero findings.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S maintainer Maintainer-authored PR labels Jul 6, 2026
Budget context engine assembly against the reserve and rendered prompt
pressure, and carry the preflight estimated prompt tokens, prompt budget,
and overflow tokens into the outer overflow recovery loop so compaction
engines compact against the prompt OpenClaw actually rendered instead of
a minimally over-budget guess.
@RomneyDa
RomneyDa force-pushed the b62.5/preflight-overflow-budgeting branch from d3b227c to 94d9056 Compare July 6, 2026 21:00
@jalehman

jalehman commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

ClawSweeper status: review started.

I am starting a fresh review of this pull request: fix(agents): preserve preflight overflow token counts into recovery budgeting This is item 1/1 in the current shard. Shard 0/1.

This placeholder means the worker is alive and reading the current context. I will edit this same comment with the actual review when the claws are done clicking.

Crustacean status: shell secured, claws on keyboard, evidence pebbles being sorted.

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jul 6, 2026
@clawsweeper

clawsweeper Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 6, 2026, 5:19 PM ET / 21:19 UTC.

Summary
The branch carries preflight token-budget snapshots into overflow recovery, reduces context-engine assemble budgets by reserve/rendered prompt pressure, and adds focused regression coverage.

PR surface: Source +81, Tests +69. Total +150 across 6 files.

Reproducibility: yes. source-reproducible: current main computes precheck budget counts, then drops them from preflightRecovery before the outer overflow compaction path chooses currentTokenCount. I did not run a live near-full session in this read-only review.

Review metrics: 1 noteworthy metric.

  • Context-engine budget contract: 1 changed call contract: tokenBudget reduced, maxOutputTokens newly populated. This is plugin-facing runtime behavior, so maintainers should notice the compatibility implication before merge.

Merge readiness
Overall: 🦪 silver shellfish
Proof: 🦪 silver shellfish
Patch quality: 🐚 platinum hermit
Result: blocked until real behavior proof from a real setup is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

Proof guidance:

  • [P1] Needs real behavior proof before merge: The PR body lists synthetic tests, tsgo, lint, and structured review, but no redacted real setup proof such as terminal/live logs from a near-full recovery path. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Risk before merge

Maintainer options:

  1. Confirm the context-engine budget contract (recommended)
    Require maintainer-visible confirmation and proof that existing context engines should see the reduced message budget during assemble before merge.
  2. Accept the plugin behavior change
    Maintainers can intentionally accept that context engines may assemble less history because the new budget better protects prompt admission.
  3. Defer the assemble-budget change
    Pause or narrow this PR to the count-propagation fix if the context-engine contract should be settled in the API-focused follow-up.

Next step before merge

  • [P1] Human review is needed because the PR has a protected maintainer label, lacks real behavior proof, and changes plugin-facing context-engine budgeting; no narrow automated repair is indicated.

Maintainer decision needed

  • Question: Should context-engine assemble() now receive the remaining message budget, or should tokenBudget remain the full prompt budget while headroom is exposed through a separate contract?
  • Rationale: The count-propagation part is narrow, but changing assemble tokenBudget semantics can affect existing context-engine plugins and overlaps the separate currentTokenCount API proposal.
  • Likely owner: steipete — Recent reserve-cap and context-overflow triage history make this the best available routing owner for the compatibility decision.
  • Options:
    • Confirm reduced assemble budget (recommended): Land this PR only after real behavior proof and maintainer confirmation that reduced tokenBudget plus maxOutputTokens is the intended context-engine contract.
    • Keep tokenBudget stable: Preserve tokenBudget as the full prompt budget and expose remaining headroom through a separate optional field or runtime setting.
    • Split to count propagation: Land only the measured-count recovery subset and leave context-engine headroom semantics to the existing API-focused follow-up.

Security
Cleared: The diff touches internal agent runner code and tests only; I found no concrete security or supply-chain concern.

Review details

Best possible solution:

Land one reconciled fix after redacted real recovery proof and maintainer confirmation of the context-engine assemble budget contract.

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

Yes, source-reproducible: current main computes precheck budget counts, then drops them from preflightRecovery before the outer overflow compaction path chooses currentTokenCount. I did not run a live near-full session in this read-only review.

Is this the best way to solve the issue?

Yes for the count-propagation path: carrying existing measured fields through the existing recovery object is narrow and keeps provider-reported counts first. The context-engine assemble budget change is plausible but needs maintainer contract confirmation because it changes plugin-facing semantics.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against c7295e417d5d.

Label changes

Label changes:

  • add P2: The PR targets a bounded agent-runtime overflow recovery bug that can affect near-full conversations but has limited blast radius.
  • add merge-risk: 🚨 compatibility: Merging changes context-engine assemble budgeting semantics that existing plugins may rely on when selecting history.
  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body lists synthetic tests, tsgo, lint, and structured review, but no redacted real setup proof such as terminal/live logs from a near-full recovery path. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.

Label justifications:

  • P2: The PR targets a bounded agent-runtime overflow recovery bug that can affect near-full conversations but has limited blast radius.
  • merge-risk: 🚨 compatibility: Merging changes context-engine assemble budgeting semantics that existing plugins may rely on when selecting history.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🐚 platinum hermit.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs real behavior proof before merge: The PR body lists synthetic tests, tsgo, lint, and structured review, but no redacted real setup proof such as terminal/live logs from a near-full recovery path. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, the PR author or someone with repository write access can comment @clawsweeper re-review.
Evidence reviewed

PR surface:

Source +81, Tests +69. Total +150 across 6 files.

View PR surface stats
Area Files Added Removed Net
Source 4 88 7 +81
Tests 2 70 1 +69
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 158 8 +150

What I checked:

Likely related people:

  • jalehman: Prior merged context-engine host capability work and co-author/reviewer history on runtime settings and ownsCompaction behavior make this person closely tied to the contract this PR changes. (role: recent context-engine contributor and reviewer; confidence: high; commits: cff5244a5b25, f94a2506d218, 444a09359377; files: src/context-engine/types.ts, src/agents/harness/context-engine-lifecycle.ts, src/agents/embedded-agent-runner/run/attempt.ts)
  • mpz4life: Authored the merged ownsCompaction precheck-skip PR that changed the same context-engine/precheck boundary this PR now adjusts. (role: recent precheck behavior contributor; confidence: high; commits: 444a09359377; files: src/agents/embedded-agent-runner/run/attempt.ts, src/agents/embedded-agent-runner/run/attempt.spawn-workspace.context-engine.test.ts)
  • yetval: Authored the merged pre-prompt overflow estimator fix for bashExecution and summary turns, which shares the measured-token invariant this PR carries into recovery. (role: recent token-estimation contributor; confidence: medium; commits: 24626e5266b7; files: src/agents/embedded-agent-runner/run/preemptive-compaction.ts, src/agents/embedded-agent-runner/run/preemptive-compaction.bashexec.test.ts)
  • steipete: Authored the recent effective reserve cap and closed the broader pre-send context-estimation issue after implementation review, making them a likely reviewer for the reserve/budgeting side. (role: recent adjacent contributor and triage owner; confidence: medium; commits: 62d957634c53; files: src/agents/agent-settings.ts, src/agents/embedded-agent-runner/run/preemptive-compaction.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

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.

How this review workflow works
  • 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.

@jalehman
jalehman merged commit d5e9b4d into main Jul 6, 2026
128 of 134 checks passed
@jalehman
jalehman deleted the b62.5/preflight-overflow-budgeting branch July 6, 2026 22:58
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 7, 2026
…openclaw#101181)

Budget context engine assembly against the reserve and rendered prompt
pressure, and carry the preflight estimated prompt tokens, prompt budget,
and overflow tokens into the outer overflow recovery loop so compaction
engines compact against the prompt OpenClaw actually rendered instead of
a minimally over-budget guess.
giodl73-repo pushed a commit to giodl73-repo/openclaw that referenced this pull request Jul 8, 2026
…openclaw#101181)

Budget context engine assembly against the reserve and rendered prompt
pressure, and carry the preflight estimated prompt tokens, prompt budget,
and overflow tokens into the outer overflow recovery loop so compaction
engines compact against the prompt OpenClaw actually rendered instead of
a minimally over-budget guess.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant