agent: preemptive context overflow detection during tool loops#29371
agent: preemptive context overflow detection during tool loops#29371keshav55 wants to merge 2 commits intoopenclaw:mainfrom
Conversation
Greptile SummaryAdded 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 Key improvements:
Note: PR includes unrelated telegram bot fix ( Confidence Score: 5/5
Last reviewed commit: 640f4b6 |
There was a problem hiding this comment.
💡 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); |
There was a problem hiding this comment.
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 👍 / 👎.
|
This pull request has been automatically marked as stale due to inactivity. |
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_endevents or the next userprompt(). 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
transformContexthook intool-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 inrun.ts.How it works (zero changes to
run.tsorattempt.ts)transformContextcompacts tool results as before (75% budget)Errorwith message"Preemptive context overflow: ..."prompt()→ caught aspromptErrorinattempt.tsrun.tsdetects it viaisLikelyContextOverflowError()(matches"context overflow:"aterrors.ts:79)isCompactionFailureError()returns false (message avoids the word "compaction")compactEmbeddedPiSessionDirect()runs full LLM-based session compactionMAX_OVERFLOW_COMPACTION_ATTEMPTS = 3prevents infinite loopsWhy 90%
Changes
src/agents/pi-embedded-runner/tool-result-context-guard.ts— addPREEMPTIVE_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 cleanisLikelyContextOverflowErrorbut NOTisCompactionFailureError