Skip to content

agent: preemptive context overflow detection during tool loops#29371

Open
keshav55 wants to merge 2 commits intoopenclaw:mainfrom
keshav55:keshav55/preemptive-context-overflow
Open

agent: preemptive context overflow detection during tool loops#29371
keshav55 wants to merge 2 commits intoopenclaw:mainfrom
keshav55:keshav55/preemptive-context-overflow

Conversation

@keshav55
Copy link
Contributor

Summary

Fixes #24800 — auto-compaction is not triggered during tool-use loops.

During long tool loops (LLM call → tool exec → tool result → repeat), context grows continuously but compaction only triggers on agent_end events or the next user prompt(). If context fills the window mid-loop, the API rejects the request — wasting the call and surfacing an error to the user.

This PR adds a preemptive overflow check to the transformContext hook in tool-result-context-guard.ts, which already runs before every LLM call (even mid-tool-loop). After the existing tool-result compaction (targets 75% of the context window), we re-estimate context size. If it still exceeds 90% of the window, we throw a context overflow error that triggers the existing overflow recovery cascade in run.ts.

How it works (zero changes to run.ts or attempt.ts)

  1. transformContext compacts tool results as before (75% budget)
  2. New: Post-enforcement check — if context still exceeds 90%, throws Error with message "Preemptive context overflow: ..."
  3. Error propagates through prompt() → caught as promptError in attempt.ts
  4. run.ts detects it via isLikelyContextOverflowError() (matches "context overflow:" at errors.ts:79)
  5. isCompactionFailureError() returns false (message avoids the word "compaction")
  6. Takes the explicit compaction branch → compactEmbeddedPiSessionDirect() runs full LLM-based session compaction
  7. On success, retries the prompt with compacted context
  8. MAX_OVERFLOW_COMPACTION_ATTEMPTS = 3 prevents infinite loops

Why 90%

  • The guard already aggressively compacts tool results at 75%
  • If context is still at 90%+ after that, non-tool-result content (assistant messages, thinking blocks) dominates — only full LLM compaction can reduce those
  • Better to compact proactively at 90% than waste a failing API call at ~100%

Changes

  • src/agents/pi-embedded-runner/tool-result-context-guard.ts — add PREEMPTIVE_OVERFLOW_RATIO (0.9), PREEMPTIVE_CONTEXT_OVERFLOW_MESSAGE, and post-enforcement overflow check (+19 lines)
  • src/agents/pi-embedded-runner/tool-result-context-guard.test.ts — 3 new tests: throws at 90%+, doesn't throw under 90%, verifies tool results are compacted before overflow check (+60 lines)

Test plan

  • pnpm test src/agents/pi-embedded-runner/tool-result-context-guard.test.ts — 11/11 pass (8 existing + 3 new)
  • pnpm test src/agents/pi-embedded-runner/run.overflow-compaction.loop.test.ts — 11/11 pass (unchanged)
  • pnpm test src/agents/pi-embedded-helpers.isbillingerrormessage.test.ts — 41/41 pass (error classifier tests)
  • pnpm check — format, typecheck, lint all clean
  • Hand-verified all 8 existing tests stay under 90% threshold after enforcement (no false triggers)
  • Verified error message matches isLikelyContextOverflowError but NOT isCompactionFailureError

@openclaw-barnacle openclaw-barnacle bot added channel: telegram Channel integration: telegram agents Agent runtime and tooling size: S labels Feb 28, 2026
@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 28, 2026

Greptile Summary

Added preemptive context overflow detection during tool loops to prevent API call failures. After tool-result compaction targets 75% of context window, a new check triggers full session compaction if context still exceeds 90%. Integrates seamlessly with existing overflow recovery cascade in run.ts by throwing an error that matches isLikelyContextOverflowError but avoids "compaction" keywords.

Key improvements:

  • Prevents wasted API calls when context grows too large mid-tool-loop
  • 90% threshold chosen appropriately after aggressive 75% tool-result compaction
  • Well-tested with 3 new test cases covering threshold behavior
  • Error message format correctly integrates with existing error detection patterns

Note: PR includes unrelated telegram bot fix (hasBotMention word-boundary check) in separate commit df61383, which prevents cross-bot mention collisions (e.g., @gaian matching @gaianchat_bot).

Confidence Score: 5/5

  • Safe to merge - well-designed fix with comprehensive test coverage
  • Clean implementation that integrates seamlessly with existing error handling. Logic is sound (75% tool-result compaction → 90% overflow check → full LLM compaction). Test coverage is thorough, error message format is correct, and calculations are verified. No edge cases or bugs identified.
  • No files require special attention

Last reviewed commit: 640f4b6

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 640f4b619b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

const text = (msg.text ?? msg.caption ?? "").toLowerCase();
if (text.includes(`@${botUsername}`)) {
const mention = `@${botUsername}`;
const idx = text.indexOf(mention);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Scan all mention occurrences before falling back to entities

hasBotMention now checks only the first @<botUsername> occurrence via indexOf, so text like @gaianbot ... @gaian is treated as not mentioning the bot when entities/caption_entities are absent, even though a valid mention exists later in the same message. This regresses the previous includes behavior and can cause mention-gated group messages to be dropped in updates where Telegram does not provide mention entities.

Useful? React with 👍 / 👎.

@openclaw-barnacle
Copy link

This pull request has been automatically marked as stale due to inactivity.
Please add updates or it will be closed.

@openclaw-barnacle openclaw-barnacle bot added the stale Marked as stale due to inactivity label Mar 5, 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 channel: telegram Channel integration: telegram size: S stale Marked as stale due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Auto-compaction not triggered during tool-use loops, leading to unrecoverable context overflow

1 participant