fix(compaction): guard malformed token estimation#63636
Conversation
Greptile SummaryThis PR introduces Confidence Score: 5/5Safe to merge; the fix is well-scoped, the fallback path is correct, and tests cover the targeted failure modes. All findings are P2. The only notable point is that the try/catch in summarizeCompactionMessages is now functionally dead after switching to the non-throwing estimateMessageTokens, leaving tokenEstimationFailed unreachable. This is a cleanup/clarity issue, not a bug. src/agents/pi-embedded-runner/compact.ts — dead tokenEstimationFailed guard in summarizeCompactionMessages worth cleaning up.
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c5a7008237
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6b9362efba
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b1565bafbd
ℹ️ 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".
c463a8f to
83f468b
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 83f468bc88
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a8de51ae7d
ℹ️ 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".
9c2e902 to
aa179b2
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: aa179b2410
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a89bd1e0c3
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: d5902dfce4
ℹ️ 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".
d5902df to
050272b
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 050272b15d
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 674baac000
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 63005a39d0
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 107c939667
ℹ️ 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".
|
Thanks for the context here. I swept through the related work, and this is now duplicate or superseded. Keep open. The PR targets a real malformed-history availability bug, but it is not merge-ready: current main has partially changed the pre-prompt estimator, the branch still needs to be ported to the current agent-core/embedded-runner layout, one known fallback shape remains undercounted, and real behavior proof is still missing. Canonical path: Close this stale PR. The latest review rated it F, the branch still lacks merge-ready proof, and there has been no human follow-up after the durable review. So I’m closing this here because the remaining work is already tracked in the canonical issue. Review detailsBest possible solution: Close this stale PR. The latest review rated it F, the branch still lacks merge-ready proof, and there has been no human follow-up after the durable review. Do we have a high-confidence way to reproduce the issue? Yes. The linked report provides a concrete stack for the original pre-prompt crash, and current source still shows unchecked Is this the best way to solve the issue? No. A guarded estimator is the right direction, but this branch is stale against current main and still misses a known reasoning block shape; the best fix is a current-main port at the shared estimator boundary with proof. Security review: Security review cleared: The diff changes token estimation and tests only; I found no new permissions, network calls, dependency sources, secret handling, or code-execution surface. AGENTS.md: found and applied where relevant. What I checked:
Likely related people:
Codex review notes: model internal, reasoning high; reviewed against 37e3e895b020. |
|
ClawSweeper PR egg 🎁 Pass real behavior proof to wake the egg and unlock a hatchable treat. Where did the egg go?
|
|
This pull request has been automatically marked as stale due to inactivity. |
|
ClawSweeper applied the proposed close for this PR.
|
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 #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 #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 #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.