#10997: fix: enable cache-ttl pruning on first load after restart
agents
stale
Cluster:
Session Pruning Enhancements
## Summary
Fixes context overflow on first request after gateway restart when cache-ttl pruning is enabled.
## Problem
When `lastCacheTouchAt` was null (on first load after restart), the cache-ttl pruning logic would skip pruning entirely. This caused sessions with large histories to immediately overflow before pruning could occur.
## Solution
Modified the TTL check to only skip pruning when:
- `lastCacheTouchAt` is NOT null AND
- TTL hasn't expired
Now pruning runs on first load (when `lastCacheTouchAt` is null) and after TTL expires, then sets `lastCacheTouchAt` for future TTL checks.
## Changes
- **File**: `src/agents/pi-extensions/context-pruning/extension.ts`
- Changed condition from `if (!lastTouch || ttlMs <= 0)` to `if (lastTouch !== null && ttlMs > 0 && Date.now() - lastTouch < ttlMs)`
- Added explanatory comments
- **Tests**: `src/agents/pi-extensions/context-pruning.test.ts`
- Added new test: `cache-ttl prunes on first load when lastCacheTouchAt is null`
- Verifies pruning happens on first load
- Verifies `lastCacheTouchAt` is set after first prune
## Testing
✅ All existing tests pass
✅ New test validates first-load pruning behavior
✅ Full `pnpm check` passes (lint, format, typecheck)
Fixes #10986
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
- Adjusts `cache-ttl` context-pruning gating so pruning runs on first request after restart (when `lastCacheTouchAt` is `null`) and continues to be skipped only while the TTL window is still active.
- Keeps existing pruning flow unchanged: `pruneContextMessages(...)` runs and, if it produced changes, updates `runtime.lastCacheTouchAt` to start/reset the TTL window.
- Adds a regression test covering the “first load with null lastCacheTouchAt” case and asserts that `lastCacheTouchAt` is set after the first prune.
<h3>Confidence Score: 5/5</h3>
- This PR looks safe to merge and addresses the reported first-load pruning skip without changing pruning behavior outside cache-ttl gating.
- Change is narrowly scoped to the cache-ttl early-return condition, maintains existing behavior for ttlMs<=0 and within-TTL requests, and includes a targeted regression test that exercises the previously failing scenario (null lastCacheTouchAt) plus existing ttl-window tests.
- No files require special attention
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
<!-- /greptile_comment -->
Most Similar PRs
#14879: fix: persist session metadata to sessions.json after context pruning
by skylarkoo7 · 2026-02-12
79.9%
#14913: fix: update context pruning to notify session metadata after prunin...
by ScreenTechnicals · 2026-02-12
76.8%
#15515: agents: add always mode for context pruning
by TsekaLuk · 2026-02-13
75.3%
#13877: perf: Comprehensive performance optimizations - caching, model rout...
by trevorgordon981 · 2026-02-11
74.5%
#23639: fix(agents): stop re-resizing session history images on every turn ...
by yinghaosang · 2026-02-22
74.1%
#12414: fix(slack): do not cache API failures in thread_ts resolver
by Yida-Dev · 2026-02-09
74.0%
#16987: fix(config): add skipCache to updateSessionStoreEntry and updateLas...
by AI-Reviewer-QS · 2026-02-15
73.9%
#19878: fix: Handle compaction when fallback model has smaller context window
by gaurav10gg · 2026-02-18
73.8%
#12195: fix(agents): sync config fallback for lookupContextTokens cold-star...
by mcaxtr · 2026-02-09
73.5%
#14744: fix(context): key MODEL_CACHE by provider/modelId to prevent collis...
by lailoo · 2026-02-12
73.4%