Skip to content

fix(agents): skip pre-prompt precheck when context engine owns compaction#95342

Merged
jalehman merged 4 commits into
openclaw:mainfrom
mpz4life:fix/skip-precheck-owns-compaction
Jun 30, 2026
Merged

fix(agents): skip pre-prompt precheck when context engine owns compaction#95342
jalehman merged 4 commits into
openclaw:mainfrom
mpz4life:fix/skip-precheck-owns-compaction

Conversation

@mpz4life

@mpz4life mpz4life commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

What Problem This Solves

The pre-prompt preemptive overflow precheck uses a built-in token estimation formula that overestimates CJK (Chinese) tool-result content by ~2.5x (TOOL_RESULT_CHARS_PER_TOKEN=2 combined with CJK 4x char inflation). This causes false-positive "Auto-compaction could not recover this turn" errors for context engines like lossless-claw that already manage context budgets accurately.

When a context engine declares ownsCompaction: true, it already:

  1. Manages context window budget in assemble() with accurate token counting
  2. Owns the compaction lifecycle (runtime auto-compaction is already disabled via shouldDisableAgentAutoCompaction)
  3. Proactively compacts in afterTurn() before context grows too large

The pre-prompt precheck is redundant for these engines and actively harmful when its estimation formula disagrees with the engine's accurate token counting.

Summary

  • Fix: Skip shouldPreemptivelyCompactBeforePrompt() when activeContextEngine?.info.ownsCompaction === true
  • Log: Emits [context-overflow-precheck] skipped: context engine "..." owns compaction for observability
  • Safety: The outer overflow-compaction retry loop in runEmbeddedAgent still handles real model API overflow errors
  • Scope: Single condition added to existing skip path + log line + regression test

Evidence

Real log output after fix (2026-06-20T22:57 local):

{
  "time": "2026-06-20T22:57:44.111+08:00",
  "logLevelName": "INFO",
  "message": "[context-overflow-precheck] skipped: context engine \"lossless-claw\" owns compaction",
  "traceId": "f2b42c377940b005e61c07e9198b373f"
}
{
  "time": "2026-06-20T22:57:50.427+08:00",
  "logLevelName": "INFO",
  "message": "[context-overflow-precheck] skipped: context engine \"lossless-claw\" owns compaction",
  "traceId": "e374769e6e3660e577a4d6c607145a7c"
}

Config (~/.openclaw/openclaw.json) confirming context engine:

{
  "plugins": {
    "slots": {
      "contextEngine": "lossless-claw"
    },
    "entries": {
      "lossless-claw": {
        "enabled": true
      }
    }
  }
}

Before fix — session 8fa61d8f (same config, lossless-claw active):

Trajectory: 4 consecutive precheck failures
  promptErrorSource: precheck
  promptError: Context overflow: prompt too large for the model (precheck).
  Model actual usage: 85,643 tokens / 128,000 context (67%)
  Precheck estimate: ~122,000 tokens (2.5x CJK overestimate)

Unit test (run.overflow-compaction.loop.test.ts):

it("recovers from real model overflow when ownsCompaction context engine skips precheck", async () => {
  mockedContextEngine.info.ownsCompaction = true;
  mockOverflowRetrySuccess({ runEmbeddedAttempt, compactDirect });
  const result = await runEmbeddedAgent(baseParams);
  expect(mockedCompactDirect).toHaveBeenCalledTimes(1);
  expect(mockedRunEmbeddedAttempt).toHaveBeenCalledTimes(2);
  expect(result.meta.error).toBeUndefined();
});
✓ agents run.overflow-compaction.loop.test.ts (27 tests) 28s
Test Files  1 passed (1)

Architectural consistency:

shouldDisableAgentAutoCompaction() at agent-settings.ts:195 already skips runtime auto-compaction for ownsCompaction: true. This PR extends the same principle to the pre-prompt precheck.

Security Impact

All No.

Compatibility

Backward compatible. Only affects sessions with an active context engine that declares ownsCompaction: true. Sessions without a context engine or with ownsCompaction: false are unchanged.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Jun 20, 2026
@clawsweeper

clawsweeper Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed June 30, 2026, 3:46 AM ET / 07:46 UTC.

Summary
The branch skips the embedded-runner generic pre-prompt overflow precheck after successful ownsCompaction context-engine assembly unless the engine opts into preassembly_may_overflow, with docs/type comments and regression coverage.

PR surface: Source +17, Tests +169, Docs +2. Total +188 across 5 files.

Reproducibility: yes. at source/proof level: current main and v2026.6.10 run the generic precheck for owning context-engine prompt submissions, and the PR body includes before-failure details plus after-fix logs from a lossless-claw setup. I did not run the contributor's live setup in this read-only review.

Review metrics: 1 noteworthy metric.

  • Context-engine contract surfaces: 2 changed. The PR changes both public docs and the exported AssembleResult.promptAuthority comment, which plugin authors may treat as an API contract.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🦞 diamond lobster
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

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

Rank-up moves:

  • Get context-engine maintainer confirmation that ownsCompaction engines should own prompt admission by default.

Risk before merge

  • [P1] Merging this changes the plugin context-engine contract: existing ownsCompaction engines that relied on OpenClaw's generic assembled-prompt precheck will submit first and rely on provider overflow recovery unless they opt into preassembly_may_overflow.

Maintainer options:

  1. Confirm owning-engine prompt admission (recommended)
    If the assigned context-engine reviewer agrees that ownsCompaction engines own prompt admission by default, merge with the docs/types/tests as the visible contract.
  2. Preserve the assembled precheck
    If maintainers want the old generic prompt-admission safeguard for existing plugins, keep an assembled-prompt gate for owning engines and only bypass the CJK-sensitive full-history estimate.
  3. Defer to estimator repair
    If maintainers prefer unchanged precheck semantics, pause or close this PR and solve the CJK false-positive through the estimator-focused work instead.

Next step before merge

  • [P2] Needs maintainer acceptance of the context-engine compatibility contract before merge; no narrow ClawSweeper repair is indicated by the current diff.

Security
Cleared: No concrete security or supply-chain concern found; the diff is limited to agent runtime control flow, docs, exported type comments, and tests.

Review details

Best possible solution:

Land only after context-engine maintainers accept that owning engines manage prompt admission by default; otherwise preserve an assembled-prompt precheck and resolve the CJK false-positive estimator path separately.

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

Yes, at source/proof level: current main and v2026.6.10 run the generic precheck for owning context-engine prompt submissions, and the PR body includes before-failure details plus after-fix logs from a lossless-claw setup. I did not run the contributor's live setup in this read-only review.

Is this the best way to solve the issue?

Yes, conditionally: this is a coherent owner-boundary fix if maintainers accept that owning engines manage prompt admission. If not, the safer alternative is to preserve an assembled-prompt precheck and fix CJK estimation separately.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P2: This is a focused agent-runtime bug fix for false overflow errors with limited blast radius, but it still affects plugin context-engine behavior.
  • merge-risk: 🚨 compatibility: Existing ownsCompaction context-engine plugins may lose the generic pre-prompt overflow safeguard unless they opt into preassembly_may_overflow.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (logs): The PR body includes before-failure details and after-fix runtime logs showing the lossless-claw owning-engine precheck skip in a real setup; those logs still match the latest head behavior.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body includes before-failure details and after-fix runtime logs showing the lossless-claw owning-engine precheck skip in a real setup; those logs still match the latest head behavior.
Evidence reviewed

PR surface:

Source +17, Tests +169, Docs +2. Total +188 across 5 files.

View PR surface stats
Area Files Added Removed Net
Source 2 37 20 +17
Tests 2 169 0 +169
Docs 1 10 8 +2
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 216 28 +188

What I checked:

Likely related people:

  • 100yenadmin: Authored the merged prompt-authority PR that made assembled context-engine output authoritative by default and defined the opt-in preassembly overflow mode this PR changes around. (role: prompt-authority contributor; confidence: high; commits: 42584964ac08, 9765a071e82f, 5a36a5f9085a; files: src/context-engine/types.ts, docs/concepts/context-engine.md, src/agents/embedded-agent-runner/run/attempt.spawn-workspace.context-engine.test.ts)
  • jalehman: Assigned on this PR, authored the latest PR-head test/repair commits, merged the prompt-authority PR, and contributed adjacent owning-compaction/runtime-settings work. (role: current reviewer and adjacent context-engine contributor; confidence: high; commits: 650b02380b7f, ee001d94d7a4, 57b61f2f9e12; files: src/agents/embedded-agent-runner/run/attempt.ts, src/agents/embedded-agent-runner/run/attempt.spawn-workspace.context-engine.test.ts, src/context-engine/types.ts)
  • fuller-stack-dev: Authored the merged context-pressure preflight PR that kept the generic guard active after context-engine loop assembly, which is the behavior this PR narrows for owning engines. (role: precheck behavior contributor; confidence: medium; commits: 2962db32fea7, f1196461d388; files: src/agents/embedded-agent-runner/run/preemptive-compaction.ts, src/agents/embedded-agent-runner/tool-result-context-guard.test.ts, src/agents/embedded-agent-runner/run/attempt.ts)
  • ragesaq: Authored recent merged runtime-settings lifecycle work that changed context-engine public API and lifecycle paths adjacent to this contract. (role: recent context-engine API contributor; confidence: medium; commits: f94a2506d218, 92a6b02f62b6, 9a19334ee56a; files: src/context-engine/types.ts, docs/concepts/context-engine.md, src/agents/embedded-agent-runner/run/attempt.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.

@mpz4life
mpz4life force-pushed the fix/skip-precheck-owns-compaction branch from ff7c508 to 0eeba50 Compare June 20, 2026 14:24
@openclaw-barnacle openclaw-barnacle Bot added size: S and removed size: XS triage: needs-pr-context Candidate: external PR body lacks required problem context or evidence. labels Jun 20, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. 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 Jun 20, 2026
@mpz4life
mpz4life force-pushed the fix/skip-precheck-owns-compaction branch 2 times, most recently from a3a7935 to 9ced576 Compare June 20, 2026 14:34
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jun 20, 2026
@mpz4life
mpz4life force-pushed the fix/skip-precheck-owns-compaction branch from 9ced576 to 6e36ce5 Compare June 20, 2026 14:47
@mpz4life mpz4life changed the title fix(agents): skip pre-prompt precheck when context engine owns compaction [AI] fix(agents): skip pre-prompt precheck when context engine owns compaction Jun 20, 2026
@mpz4life
mpz4life force-pushed the fix/skip-precheck-owns-compaction branch from 6e36ce5 to ddc69d6 Compare June 20, 2026 15:07
@mpz4life

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@mpz4life

Copy link
Copy Markdown
Contributor Author

@jalehman Could you review this when you get a chance? This fixes false-positive precheck overflow for CJK-heavy sessions when lossless-claw (ownsCompaction) is active.

@clawsweeper clawsweeper Bot added 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 Jun 20, 2026
@jalehman jalehman self-assigned this Jun 20, 2026
@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. labels Jun 21, 2026
@mpz4life

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

@mpz4life

Copy link
Copy Markdown
Contributor Author

@jalehman all checks have passed

@clawsweeper clawsweeper Bot removed the status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. label Jun 27, 2026
@clawsweeper clawsweeper Bot added the status: 🛠️ actively grinding The PR author has acted after the latest ClawSweeper review and work remains. label Jun 27, 2026
@mpz4life

Copy link
Copy Markdown
Contributor Author

@jalehman Should we skip this precheck for the LCM context engine, or fix the local token estimation?

@openclaw-barnacle openclaw-barnacle Bot added the docs Improvements or additions to documentation label Jun 30, 2026
@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: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 🛠️ actively grinding The PR author has acted after the latest ClawSweeper review and work remains. labels Jun 30, 2026
mpz4life and others added 3 commits June 30, 2026 00:26
…tion

When a context engine advertises ownsCompaction=true (e.g. lossless-claw),
skip the pre-prompt preemptive overflow precheck entirely. The engine
already manages the context budget through assemble() and its own
compaction lifecycle — the built-in precheck is redundant and causes
false-positive overflow errors for CJK-heavy sessions due to its
conservative token estimation formula.

Safety is preserved: if the model's actual context limit is exceeded,
the model API returns an error that the outer overflow-compaction retry
loop handles normally.
@jalehman
jalehman force-pushed the fix/skip-precheck-owns-compaction branch from 6a3891d to 14d361b Compare June 30, 2026 07:28
@jalehman
jalehman merged commit 444a093 into openclaw:main Jun 30, 2026
101 of 103 checks passed
@jalehman

Copy link
Copy Markdown
Contributor

Merged via squash.

xialonglee pushed a commit to xialonglee/openclaw that referenced this pull request Jun 30, 2026
…tion (openclaw#95342)

* fix(agents): skip pre-prompt precheck when context engine owns compaction

When a context engine advertises ownsCompaction=true (e.g. lossless-claw),
skip the pre-prompt preemptive overflow precheck entirely. The engine
already manages the context budget through assemble() and its own
compaction lifecycle — the built-in precheck is redundant and causes
false-positive overflow errors for CJK-heavy sessions due to its
conservative token estimation formula.

Safety is preserved: if the model's actual context limit is exceeded,
the model API returns an error that the outer overflow-compaction retry
loop handles normally.

* fix(agents): assert non-null context engine in precheck skip log

* test(context-engine): cover owning precheck contract

* fix(agents): preserve precheck after context assembly failure

---------

Co-authored-by: Josh Lehman <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 1, 2026
…tion (openclaw#95342)

* fix(agents): skip pre-prompt precheck when context engine owns compaction

When a context engine advertises ownsCompaction=true (e.g. lossless-claw),
skip the pre-prompt preemptive overflow precheck entirely. The engine
already manages the context budget through assemble() and its own
compaction lifecycle — the built-in precheck is redundant and causes
false-positive overflow errors for CJK-heavy sessions due to its
conservative token estimation formula.

Safety is preserved: if the model's actual context limit is exceeded,
the model API returns an error that the outer overflow-compaction retry
loop handles normally.

* fix(agents): assert non-null context engine in precheck skip log

* test(context-engine): cover owning precheck contract

* fix(agents): preserve precheck after context assembly failure

---------

Co-authored-by: Josh Lehman <[email protected]>
QiuYuang pushed a commit to QiuYuang/openclaw that referenced this pull request Jul 1, 2026
…tion (openclaw#95342)

* fix(agents): skip pre-prompt precheck when context engine owns compaction

When a context engine advertises ownsCompaction=true (e.g. lossless-claw),
skip the pre-prompt preemptive overflow precheck entirely. The engine
already manages the context budget through assemble() and its own
compaction lifecycle — the built-in precheck is redundant and causes
false-positive overflow errors for CJK-heavy sessions due to its
conservative token estimation formula.

Safety is preserved: if the model's actual context limit is exceeded,
the model API returns an error that the outer overflow-compaction retry
loop handles normally.

* fix(agents): assert non-null context engine in precheck skip log

* test(context-engine): cover owning precheck contract

* fix(agents): preserve precheck after context assembly failure

---------

Co-authored-by: Josh Lehman <[email protected]>
chenyangjun-xy pushed a commit to chenyangjun-xy/openclaw that referenced this pull request Jul 1, 2026
…tion (openclaw#95342)

* fix(agents): skip pre-prompt precheck when context engine owns compaction

When a context engine advertises ownsCompaction=true (e.g. lossless-claw),
skip the pre-prompt preemptive overflow precheck entirely. The engine
already manages the context budget through assemble() and its own
compaction lifecycle — the built-in precheck is redundant and causes
false-positive overflow errors for CJK-heavy sessions due to its
conservative token estimation formula.

Safety is preserved: if the model's actual context limit is exceeded,
the model API returns an error that the outer overflow-compaction retry
loop handles normally.

* fix(agents): assert non-null context engine in precheck skip log

* test(context-engine): cover owning precheck contract

* fix(agents): preserve precheck after context assembly failure

---------

Co-authored-by: Josh Lehman <[email protected]>
chenyangjun-xy pushed a commit to chenyangjun-xy/openclaw that referenced this pull request Jul 3, 2026
…tion (openclaw#95342)

* fix(agents): skip pre-prompt precheck when context engine owns compaction

When a context engine advertises ownsCompaction=true (e.g. lossless-claw),
skip the pre-prompt preemptive overflow precheck entirely. The engine
already manages the context budget through assemble() and its own
compaction lifecycle — the built-in precheck is redundant and causes
false-positive overflow errors for CJK-heavy sessions due to its
conservative token estimation formula.

Safety is preserved: if the model's actual context limit is exceeded,
the model API returns an error that the outer overflow-compaction retry
loop handles normally.

* fix(agents): assert non-null context engine in precheck skip log

* test(context-engine): cover owning precheck contract

* fix(agents): preserve precheck after context assembly failure

---------

Co-authored-by: Josh Lehman <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M 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.

2 participants