← Back to PRs

#18724: fix(compaction): add defensive guards for undefined session.messages

by mdlmarkham open 2026-02-17 00:52 View on GitHub →
agents size: XS
## Summary - Add null guard in `sanitizeSessionHistory()` for undefined messages param - Add `session.messages ?? []` guards in `compact.ts` before destructive operations - Prevents `Cannot read properties of undefined (reading 'filter')` crash during compaction ## Problem When running `/compact` or when auto-compaction triggers, the following crash can occur: ``` TypeError: Cannot read properties of undefined (reading 'filter') ``` This happens when `session.messages` is undefined in edge cases (e.g., corrupted session file, SDK edge case). The undefined value flows through to `defaultConvertToLlm` in pi-agent-core which calls `.filter()` on it without a null check. ## Root Cause The compaction flow accesses `session.messages` directly without guarding against undefined: 1. `sanitizeSessionHistory({ messages: session.messages, ... })` 2. `[...session.messages]` spread operation 3. `session.messages.length` property access If `session.messages` is undefined at any point, the crash occurs. ## Fix Add defensive null coalescing (`?? []`) at: 1. **google.ts**: `sanitizeSessionHistory` - guard against undefined `params.messages` 2. **compact.ts**: Guard all `session.messages` accesses with null coalescing ## Related - Similar to #3225 which fixed `Cannot read properties of undefined (reading 'length')` in `limitHistoryTurns` - This PR adds the same pattern to additional code paths ## Test Plan - [x] Lint passes (`pnpm lint`) - [x] No TypeScript errors - [ ] Manual testing with session that has undefined messages - [ ] Existing compaction tests should continue to pass <!-- greptile_comment --> <h3>Greptile Summary</h3> Adds defensive null-coalescing guards (`?? []`) for `session.messages` in the compaction flow to prevent `TypeError: Cannot read properties of undefined (reading 'filter')` crashes when session data is corrupted or `messages` is unexpectedly undefined. - **`compact.ts`**: Guards `session.messages` accesses before and after compaction by caching into `sessionMessages` and `postCompactionMessages` local variables with `?? []` fallback - **`google.ts`**: Guards `params.messages` inside `sanitizeSessionHistory()`, which protects all callers (including `run/attempt.ts`) from undefined messages at the function boundary - Import reordering in both files (likely automated formatter output) <h3>Confidence Score: 5/5</h3> - This PR is safe to merge — it adds purely defensive null guards with no behavior change for the normal case. - The changes are minimal and defensive: null-coalescing operators (`?? []`) that fall through to the existing behavior when `session.messages` is defined (the normal case). When `session.messages` is undefined (the bug case), the code now gracefully handles it with an empty array instead of crashing. Import reordering is cosmetic. No logic changes, no new dependencies, no risk of regression. - No files require special attention. <sub>Last reviewed commit: efc65f7</sub> <!-- greptile_other_comments_section --> <sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub> <!-- /greptile_comment -->

Most Similar PRs