Skip to content

fix(agents): honor preflight compaction budget - #130562

Merged
vincentkoc merged 1 commit into
mainfrom
fix/context-engine-preflight-budget
Aug 27, 2026
Merged

vincentkoc merged 1 commit into
mainfrom
fix/context-engine-preflight-budget

Conversation

@vincentkoc

Copy link
Copy Markdown
Member

Summary

  • compact synthetic mid-turn precheck failures against the reserve-adjusted prompt budget that triggered recovery
  • retain the raw context-window budget for provider overflow, timeout recovery, and invalid preflight metadata
  • cover the narrower target, provider-originated overflow, and invalid budget values

Root cause

The mid-turn precheck preserved promptBudgetBeforeReserve, but overflow recovery always passed contextTokenBudget to forced context-engine compaction. A prompt above the reserve-adjusted budget but below the hard window could therefore be judged already under target, making recovery a deterministic no-op.

The overflow owner has both the failure provenance and the measured preflight budget. It now selects the narrower target only for a genuine synthetic mid-turn precheck failure with a finite positive budget.

Contract evidence

Sibling Codex source independently separates proactive compaction pressure from the hard provider window:

  • codex-rs/protocol/src/openai_models.rs:481
  • codex-rs/core/src/session/context_window.rs:53
  • codex-rs/core/src/session/turn.rs:1024
  • codex-rs/codex-api/src/sse/responses.rs:680

Validation

  • pre-fix regression: compaction received 200000 instead of the triggering 241616
  • focused overflow, timeout, and preflight tests: 50 passed
  • pnpm tsgo:core: passed
  • targeted lint, format, and git diff --check: passed
  • fresh post-rebase branch autoreview: clean, correctness 0.98
  • Testbox tbx_01m10c0h3eyw9jt5x0dj7q5d0w, workflow 33028988171: infrastructure blocked after more than five hours without a terminal summary; the exact task-owned lease and processes were stopped, and no replacement heavy run was started

Scope

  • production: +11 / -3, net +8
  • tests: +59 / -1, net +58
  • no context-engine contract, timeout, config, protocol, persistence, or compatibility changes

The positive production delta is the explicit provenance and validity gate needed to prevent the narrower budget from leaking into provider overflow.

This deterministic session-block fix is release-note worthy; the landing release workflow owns the changelog entry.

Fixes #130226

Reported by @HelloBias.

Use the reserve-adjusted prompt budget only for synthetic mid-turn precheck recovery while preserving the raw context budget for provider overflow and timeout recovery.\n\nFixes #130226\nReported-by: @HelloBias
@clawsweeper

clawsweeper Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@vincentkoc vincentkoc self-assigned this Aug 27, 2026
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S maintainer Maintainer-authored PR labels Aug 27, 2026
@vincentkoc
vincentkoc marked this pull request as ready for review August 27, 2026 01:17
@clawsweeper clawsweeper Bot added merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Aug 27, 2026
@clawsweeper

clawsweeper Bot commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 26, 2026, 9:25 PM ET / August 27, 2026, 01:25 UTC.

ClawSweeper review

What this changes

The PR makes synthetic mid-turn preflight overflows compact against the reserve-adjusted budget that triggered recovery while retaining the raw model window for provider overflows and timeout recovery.

Regression provenance

Possible regression — probable (reproduction; reviewed change). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 4 items remain

Keep open for normal maintainer review: the focused guard matches the recorded preflight provenance and no concrete patch defect was found, but the branch is behind main and its session-compaction behavior still needs exact-head validation.

Priority: P2
Reviewed head: 2f435942de3dc78c5769aecaf6579d4f91e65efb

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) The patch is focused and source-consistent, with confidence limited by pending exact-head session-recovery validation.
Proof confidence 🌊 off-meta tidepool Not applicable: This MEMBER-authored PR is exempt from the external-contributor proof gate; its reported focused test results remain supplemental evidence.
Patch quality 🦐 gold shrimp (3/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This MEMBER-authored PR is exempt from the external-contributor proof gate; its reported focused test results remain supplemental evidence.
Evidence reviewed 7 items Preflight owner: The preflight calculation clamps the model context budget and subtracts the effective reserve, then selects compaction only when the rendered prompt exceeds that reserve-adjusted target.
Failure provenance: The prompt phase records the mid-turn recovery metadata and stamps the synthetic prompt error as precheck before terminal normalization.
Patched decision and callee: The new guard requires a precheck terminal source, mid-turn metadata, and a finite positive budget; the canonical helper forwards the chosen budget to ContextEngine.compact with compactionTarget budget.
Findings None None.
Security None None.

Live Verification

Command: pnpm test src/agents/embedded-agent-runner/run.overflow-context-recovery.test.ts

Result: PASS (completed)

$ node --import tsx scripts/test-projects.mts src/agents/embedded-agent-runner/run.overflow-context-recovery.test.ts
[test] starting test/vitest/vitest.agents-embedded-agent.config.ts

 RUN  v4.1.11 /tmp/clawsweeper-live-proof-130562-KYkDdT/target

 ✓  agents-embedded-agent  src/agents/embedded-agent-runner/run.overflow-context-recovery.test.ts (30 tests) 73ms

 Test Files  1 passed (1)
      Tests  30 passed (30)
   Start at  01:26:22
   Duration  7.13s (transform 5.56s, setup 843ms, import 5.91s, tests 73ms, environment 0ms)

[test] passed 1 Vitest shard in 12.19s
$ node --import tsx scripts/test-projects.mts src/agents/embedded-agent-runner/run.overflow-context-recovery.test.ts
[test] starting test/vitest/vitest.agents-embedded-agent.config.ts

 RUN  v4.1.11 /tmp/clawsweeper-live-proof-130562-KYkDdT/target

 ✓  agents-embedded-agent  src/agents/embedded-agent-runner/run.overflow-context-recovery.test.ts (30 tests) 67ms

 Test Files  1 passed (1)
      Tests  30 passed (30)
   Start at  01:26:35
   Duration  3.38s (transform 1.87s, setup 392ms, import 2.72s, tests 67ms, environment 0ms)

[test] passed 1 Vitest shard in 8.43s

Assertions:

  • PASS expect_output: Test Files

How this fits together

Embedded agent runs measure prompt pressure before submission and, on overflow, ask a context engine to compact the active session before retrying. This change selects the recovery target from the recorded preflight failure provenance.

flowchart LR
A[Rendered session prompt] --> B[Reserve-aware preflight]
B --> C[Recorded mid-turn failure]
C --> D[Overflow recovery]
D --> E[Select compaction target]
E --> F[Context engine compaction]
F --> G[Retry current transcript]
Loading

Before merge

  • Resolve merge risk (P2) - This changes how an existing session is compacted; the focused tests mock the compaction boundary, so an exact-head context-engine recovery run should confirm both the reserve-aware precheck path and the raw-budget provider/timeout paths.
  • Resolve merge risk (P1) - The branch is behind main and checks were still pending in the supplied state; rebase and validate the exact resulting head before landing.
  • Resolve merge risk (P1) - The latest ClawSweeper review text was not present in the supplied comment payload and GitHub was unreachable, so its rank-up moves must be checked before landing.
  • Complete next step (P2) - This active MEMBER-authored, maintainer-labeled PR needs ordinary exact-head rebase, CI, and session-boundary validation rather than an automated replacement.
Agent review details

Security

None.

PR surface

Source +8, Tests +58. Total +66 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 11 3 +8
Tests 1 59 1 +58
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 70 4 +66

Review metrics

Metric Value Why it matters
Production versus test delta production +8 net; tests +58 net The added production guard is limited to recorded precheck provenance and is accompanied by focused path-isolation coverage.

Stored data model

Persistent data-model change detected: vector/embedding metadata: src/agents/embedded-agent-runner/run.overflow-context-recovery.test.ts. Confirm migration or upgrade compatibility proof before merge.

Root-cause cluster

Relationship: fixed_by_candidate
Canonical: #130226
Summary: This PR is a focused candidate fix for the reported reserve-adjusted compaction-target mismatch.

Members:

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

Merge-risk options

Maintainer options:

  1. Validate the narrowed recovery target after rebase (recommended)
    Rebase and run a context-engine-boundary case proving the synthetic precheck uses the reserve-adjusted target while provider overflow and timeout recovery retain the raw target.
  2. Pause for session-recovery verification
    Defer landing if the exact-head recovery proof cannot show that the lower target does not alter unrelated session recovery paths.

Technical review

Best possible solution:

Rebase onto current main, preserve the narrow provenance gate, and demonstrate through the real context-engine boundary that only the synthetic precheck compacts to the reserve-adjusted target.

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

Yes, from source: a mid-turn precheck records a reserve-adjusted budget while the previous overflow recovery always forwarded the raw context budget. The supplied report also provides a concrete affected-session scenario, but no live run was executed in this read-only review.

Is this the best way to solve the issue?

Yes, subject to exact-head boundary proof: selecting the target at the overflow owner from the closed terminal provenance prevents the narrower budget from leaking into provider or timeout recovery.

AGENTS.md: found and applied where relevant.

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

Labels

Label changes:

  • add P2: This is a bounded repair for deterministic context-compaction recovery in affected sessions.
  • add merge-risk: 🚨 session-state: The PR intentionally changes the compaction target for an existing session, which can alter transcript reduction behavior if provenance is misclassified.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🌊 off-meta tidepool and patch quality is 🦐 gold shrimp.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This MEMBER-authored PR is exempt from the external-contributor proof gate; its reported focused test results remain supplemental evidence.

Label justifications:

  • P2: This is a bounded repair for deterministic context-compaction recovery in affected sessions.
  • merge-risk: 🚨 session-state: The PR intentionally changes the compaction target for an existing session, which can alter transcript reduction behavior if provenance is misclassified.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🌊 off-meta tidepool and patch quality is 🦐 gold shrimp.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This MEMBER-authored PR is exempt from the external-contributor proof gate; its reported focused test results remain supplemental evidence.

Evidence

What I checked:

Likely related people:

  • Peter Steinberger: He authored the budget-consolidation and subsequent context-engine recovery fixes that dominate this path's history. (role: recent recovery-path owner; confidence: high; commits: 475ea69f2c35, 65fca84d7c68, e530b58b44b8; files: src/agents/embedded-agent-runner/run/overflow-context-recovery.ts, src/agents/embedded-agent-runner/run/timeout-context-recovery.ts)
  • Josh Avant: History identifies the synthetic-overflow work as an earlier contribution in the same recovery area. (role: adjacent feature contributor; confidence: medium; commits: e07de4c561; files: src/agents/embedded-agent-runner/run/overflow-context-recovery.ts)

Rank-up moves

Optional improvements that raise the rating; they are not merge blockers.

  • Rebase and run a real context-engine recovery case covering the reserve-aware precheck, provider overflow, and timeout branches.
  • Check the latest ClawSweeper review comment and address or explicitly skip its rank-up moves before landing.

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.

@vincentkoc

Copy link
Copy Markdown
Member Author

ClawSweeper rank-up moves:

  • Rebase: skipped. The opened PR has no conflict, its exact head 2f435942de3dc78c5769aecaf6579d4f91e65efb passed the full exact-head CI run, and repo policy does not rebase solely because main advanced. Native prepare/merge verification will check relevant drift.
  • Real context-engine recovery replay: not claimed. The task-owned Testbox run hung and was stopped without a second run. Focused owner-boundary coverage proves the reserve-aware precheck, provider-overflow, timeout, and invalid-budget branches; direct Codex source inspection and exact-head CI provide the remaining contract validation.

No ClawSweeper code findings remain.

@vincentkoc
vincentkoc merged commit 3cb0dd4 into main Aug 27, 2026
651 of 653 checks passed
@vincentkoc
vincentkoc deleted the fix/context-engine-preflight-budget branch August 27, 2026 01:48
@vincentkoc

Copy link
Copy Markdown
Member Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Aug 27, 2026
Use the reserve-adjusted prompt budget only for synthetic mid-turn precheck recovery while preserving the raw context budget for provider overflow and timeout recovery.\n\nFixes openclaw#130226\nReported-by: @HelloBias
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: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. 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]: Forced context-engine compaction can no-op above the reserve-adjusted prompt budget

1 participant