Skip to content

fix: apply contextTokens cap to model for auto-compaction threshold#39099

Closed
MumuTW wants to merge 1 commit intoopenclaw:mainfrom
MumuTW:fix/compaction-context-tokens-cap
Closed

fix: apply contextTokens cap to model for auto-compaction threshold#39099
MumuTW wants to merge 1 commit intoopenclaw:mainfrom
MumuTW:fix/compaction-context-tokens-cap

Conversation

@MumuTW
Copy link
Copy Markdown
Contributor

@MumuTW MumuTW commented Mar 7, 2026

Summary

  • Apply agents.defaults.contextTokens cap to the model object passed to pi-coding-agent, so the library's auto-compaction threshold uses the effective limit instead of the native model context window
  • Without this fix, when contextTokens (e.g. 160k) is much smaller than the model's native context window (e.g. 1M for Gemini Flash), the library never triggers auto-compaction because contextTokens > contextWindow - reserveTokens is never true — the safeguard extension never fires and the compaction counter stays at 0
  • Applies the same fix to both run.ts (main agent run path) and compact.ts (explicit /compact command path)

Changes

  • src/agents/pi-embedded-runner/run.ts: Create effectiveModel with capped contextWindow from resolveContextWindowInfo, pass it to runEmbeddedAttempt instead of the raw model
  • src/agents/pi-embedded-runner/compact.ts: Same pattern — compute effectiveModel early, use it for both createOpenClawCodingTools (modelContextWindowTokens) and createAgentSession
  • src/agents/pi-embedded-runner/compact.hooks.test.ts: Add missing DEFAULT_CONTEXT_TOKENS to the defaults.js mock

Test plan

  • pnpm test -- --run src/agents/pi-embedded-runner/compact.hooks.test.ts src/agents/context-window-guard.test.ts — 12 tests pass
  • pnpm test -- --run src/agents/pi-embedded-runner/run.overflow-compaction.test.ts src/agents/pi-embedded-runner/run.overflow-compaction.loop.test.ts — 16 tests pass
  • pnpm tsgo — clean

Fixes #38905

The library's auto-compaction uses `model.contextWindow` to decide when
to compact (`contextTokens > contextWindow - reserveTokens`).  When the
user sets `agents.defaults.contextTokens` (e.g. 160 000) but the model's
native context window is much larger (e.g. 1M for Gemini Flash), the
library never triggers auto-compaction because usage never exceeds the
native limit.  This means the safeguard compaction extension never fires
and the compaction counter stays at 0.

Create an `effectiveModel` with the capped `contextWindow` in both
`run.ts` and `compact.ts` so the library threshold respects the
configured limit.

Fixes openclaw#38905
@openclaw-barnacle openclaw-barnacle bot added agents Agent runtime and tooling size: XS labels Mar 7, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Mar 7, 2026

Greptile Summary

This PR fixes a bug where the pi-coding-agent library's auto-compaction mechanism would never trigger when a user-configured agents.defaults.contextTokens cap (e.g. 160k) was significantly smaller than the model's native context window (e.g. 1M for Gemini Flash). The fix constructs an effectiveModel object with contextWindow overridden to ctxInfo.tokens (the resolved effective limit) and passes it in place of the raw model to the agent session and tools, ensuring the library's internal threshold calculation uses the correct, capped value.

Key changes:

  • run.ts: Derives effectiveModel immediately after the existing resolveContextWindowInfo call and passes it to runEmbeddedAttempt instead of the raw model.
  • compact.ts: Adds a new resolveContextWindowInfo call (not previously present in the compaction path) to derive effectiveModel, and applies ctxInfo.tokens to both modelContextWindowTokens in createOpenClawCodingTools (fixing a secondary issue where model.contextWindow could be undefined) and model in createAgentSession.
  • compact.hooks.test.ts: Adds the missing DEFAULT_CONTEXT_TOKENS export to the ../defaults.js mock to prevent test failures from the newly introduced resolveContextWindowInfo call in compact.ts.
  • The effectiveModel only overrides contextWindow; all other uses of the original model (.api, .provider, .id) remain unchanged and correct.

Confidence Score: 5/5

  • This PR is safe to merge — it applies a well-scoped, minimal fix to a clear compaction threshold bug with no side-effects on other model properties.
  • The change is minimal and surgical: it only overrides contextWindow on a locally-scoped copy of the model object. All other model properties (provider, api, id) continue to come from the original model. The ?? Infinity guard correctly handles models without a declared context window. The test mock fix is accurate. Both affected code paths (run.ts and compact.ts) are handled symmetrically, and no existing logic is removed — only the downstream recipient of the model object is updated.
  • No files require special attention.

Last reviewed commit: 26f658e

@steipete
Copy link
Copy Markdown
Contributor

steipete commented Mar 7, 2026

Landed on main.

What we did:

  • Landed the compaction context-window cap fix from this PR.
  • Added changelog entry in 2026.3.7 Fixes with contributor + PR reference.
  • Ran full gate before landing:
    • pnpm lint
    • pnpm build
    • pnpm test

SHA details:

  • PR head reviewed: 26f658eef6564bd4f5476b6d8f6c454b1b7aada5
  • Landed on main: 537c97cce

Thanks @MumuTW for the fix.

@steipete steipete closed this Mar 7, 2026
mrosmarin added a commit to mrosmarin/openclaw that referenced this pull request Mar 7, 2026
* main: (133 commits)
  reduce image size, offer slim image (openclaw#38479)
  fix(security): harden install base drift cleanup
  fix(agents): respect explicit provider baseUrl in merge mode (openclaw#39103)
  fix(agents): apply contextTokens cap for compaction threshold (openclaw#39099)
  fix(exec): block dangerous override-only env pivots
  fix(security): stage installs before publish
  fix(daemon): normalise whitespace in checkTokenDrift to prevent false-positive warning (openclaw#39108)
  fix(security): harden fs-safe copy writes
  refactor: dedupe bluebubbles webhook auth test setup
  refactor: dedupe discord native command test scaffolding
  refactor: dedupe anthropic probe target test setup
  refactor: dedupe minimax provider auth test setup
  refactor: dedupe runtime snapshot test fixtures
  fix: harden zip extraction writes
  fix(tests): stabilize diffs localReq headers (supersedes openclaw#39063)
  fix: harden workspace skill path containment
  fix(agents): land openclaw#38935 from @MumuTW
  fix(models): land openclaw#38947 from @davidemanuelDEV
  fix(gateway): land openclaw#39064 from @Narcooo
  fix(models-auth): land openclaw#38951 from @MumuTW
  ...
vincentkoc pushed a commit to BryanTegomoh/openclaw-fork that referenced this pull request Mar 8, 2026
openperf pushed a commit to openperf/moltbot that referenced this pull request Mar 8, 2026
mcaxtr pushed a commit to mcaxtr/openclaw that referenced this pull request Mar 8, 2026
Saitop pushed a commit to NomiciAI/openclaw that referenced this pull request Mar 8, 2026
GordonSH-oss pushed a commit to GordonSH-oss/openclaw that referenced this pull request Mar 9, 2026
jenawant pushed a commit to jenawant/openclaw that referenced this pull request Mar 10, 2026
dhoman pushed a commit to dhoman/chrono-claw that referenced this pull request Mar 11, 2026
senw-developers pushed a commit to senw-developers/va-openclaw that referenced this pull request Mar 17, 2026
V-Gutierrez pushed a commit to V-Gutierrez/openclaw-vendor that referenced this pull request Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: XS

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Compaction issues: safeguard counter stuck at 0, default mode triggers TPM burst

2 participants