fix(compaction): guard malformed token estimation#2207
Open
BingqingLyu wants to merge 4 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mainsessions could crash before provider dispatch when compaction token estimation hit malformed replay history andestimateTokens()read missing.lengthfields.estimateMessageTokens()path insrc/agents/compaction.tsand switched preemptive compaction plus embedded compaction metrics/sanity checks to reuse it.@mariozechner/pi-coding-agent; it only hardens OpenClaw’s local compaction estimation path.Change Type (select all)
Scope (select all touched areas)
Linked Issue/PR
Closes Main session prompt crash: Cannot read properties of undefined (reading 'length') in compaction token estimation openclaw/openclaw#63612
This likely also addresses the malformed-history /
reading 'length'Telegram manifestation discussed in [Bug]: Telegram direct lane repeatedly throws 'Cannot read properties of undefined (reading "length")' on 2026.4.9 openclaw/openclaw#64053, and may partially reduce the Telegram crash facet mentioned in [Bug]: 2026.4.9 multi-channel degradation on Linux: Discord direct-session overflow + slash lag + Telegram 'reading length' lane crashes openclaw/openclaw#64034, but it does not address the broader Discord overflow/lag symptoms tracked there.This PR fixes a bug or regression
Root Cause (if applicable)
.lengthreads.estimateTokens()call sites.mainsessions exercise pre-prompt compaction on every turn, so one malformed history block could repeatedly fail the recovery path itself.Regression Test Plan (if applicable)
src/agents/compaction.test.ts,src/agents/pi-embedded-runner/run/preemptive-compaction.test.tsUser-visible / Behavior Changes
Long-lived sessions with malformed replay history now fail soft in compaction token estimation instead of crashing before reply generation.
Diagram (if applicable)
Security Impact (required)
No)No)No)No)No)Yes, explain risk + mitigation:Repro + Verification
Environment
mainsessionSteps
Expected
Actual
Cannot read properties of undefined (reading 'length')and abort the reply before provider dispatch.Evidence
Human Verification (required)
pnpm test src/agents/compaction.test.ts,pnpm test src/agents/pi-embedded-runner/run/preemptive-compaction.test.ts, andpnpm check.Review Conversations
Compatibility / Migration
Yes)No)No)Risks and Mitigations
estimateTokens()for malformed messages.