#22901: fix: guard against NaN reserveTokens in compaction safeguard
agents
size: XS
Cluster:
Memory Management Enhancements
## Summary
- Adds `|| 0` fallback for `preparation.settings.reserveTokens` in both occurrences within `compaction-safeguard.ts`
- When `reserveTokens` is undefined, `Math.floor(undefined)` returns `NaN`, and `Math.max(1, NaN)` also returns `NaN`, causing summarization to fail silently
- With the fallback, `Math.max(1, Math.floor(0))` correctly resolves to `1`
Fixes #9279
## Local Validation
- [x] Both occurrences of `preparation.settings.reserveTokens` are guarded (lines 278 and 300)
- [x] `|| 0` correctly handles `undefined`, `null`, `NaN`, and `0` inputs
## Scope
XS — 2-line change in one file
## AI Assistance
Used Claude Code for issue investigation and fix implementation.
## Author
@miloudbelarebia
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
<!-- greptile_comment -->
<h3>Greptile Summary</h3>
Minimal 2-line defensive fix in `compaction-safeguard.ts` that guards against `NaN` propagation when `preparation.settings.reserveTokens` is `undefined`. Without the fix, `Math.floor(undefined)` produces `NaN`, and `Math.max(1, NaN)` also returns `NaN`, which would be passed to `summarizeInStages` causing summarization to fail silently. The `|| 0` fallback ensures `Math.max(1, Math.floor(0))` correctly resolves to `1` — a safe minimum.
- Both occurrences of `preparation.settings.reserveTokens` (lines 278 and 300) are now consistently guarded
- The fix correctly handles `undefined`, `null`, `NaN`, and `0` inputs
- No issues found during review
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge — it's a minimal defensive fix with no behavioral change when reserveTokens is already a valid number.
- The change is a 2-line fix in a single file, adding a standard JS falsy fallback. The logic is straightforward: `|| 0` guards against `undefined`/`NaN`, and the outer `Math.max(1, ...)` ensures a minimum of 1. Both occurrences are handled consistently. No new code paths, no API changes, no risk of regression.
- No files require special attention.
<sub>Last reviewed commit: 11e1133</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#22859: fix: guard against undefined fileOps properties in compaction safeg...
by miloudbelarebia · 2026-02-21
79.3%
#19878: fix: Handle compaction when fallback model has smaller context window
by gaurav10gg · 2026-02-18
73.7%
#22786: fix: guard against undefined file.path in system-prompt-report
by miloudbelarebia · 2026-02-21
72.4%
#12254: fix: add minimum token guard to prevent double compaction after flush
by Yida-Dev · 2026-02-09
71.8%
#17864: fix(compaction): pass model through runtime + reduce chunk ratio to...
by battman21 · 2026-02-16
71.5%
#20790: fix: Math.max/min with spread operator can cause stack overflow on ...
by coygeek · 2026-02-19
71.3%
#10711: fix: cancel compaction instead of truncating history when summariza...
by DukeDeSouth · 2026-02-06
71.2%
#20799: fix: replace Math.max/min spread with reduce to prevent stack overflow
by mariusorani · 2026-02-19
71.1%
#22711: fix: post-compaction audit skips non-existent required files
by taw0002 · 2026-02-21
70.7%
#9620: fix: increase auto-compaction reserve buffer to 40k tokens
by Arlo83963 · 2026-02-05
70.7%