#15294: fix(cron): reset token counters when creating new isolated session
size: XS
Cluster:
Cron Job Enhancements
## Summary
Fixes stale token counters appearing in Sessions view after a cron session is deleted and recreated.
Fixes #13730
### Problem
After deleting an isolated cron session and letting the cron run again:
- A new session is created with a new `sessionId`
- But the Sessions view still shows the old token count (e.g., `272000 / 272000`) instead of the fresh usage (~23k)
### Root Cause
`resolveCronSession()` was preserving `contextTokens` from the old session entry. While `totalTokens` wasn't explicitly preserved, this could lead to confusing state where old context values persisted.
### Solution
Explicitly clear all token-related fields when creating a new cron session:
- `contextTokens`: Don't preserve from old entry — will be set during the run based on actual model
- `inputTokens`, `outputTokens`, `totalTokens`: Set to `undefined`
- `totalTokensFresh`: Set to `false` until actual usage is recorded
This ensures the Sessions view shows accurate "unknown" state until the run completes with real usage data.
### Testing
- [x] `pnpm run lint` passes
- [x] `pnpm vitest run src/cron` — all 128 tests pass
### AI Disclosure
🤖 This PR was AI-assisted (Claude via OpenClaw).
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This change updates cron isolated-session creation (`resolveCronSession`) to avoid carrying over stale token counters when an isolated session is deleted and later recreated. The new session entry now explicitly clears `contextTokens`, `inputTokens`, `outputTokens`, and `totalTokens`, and sets `totalTokensFresh` to `false` so downstream consumers treat usage as unknown until the run records real token usage.
This fits the existing session metadata model (`SessionEntry`) and its freshness helpers (`resolveFreshSessionTotalTokens`), preventing misleading “old” utilization in the Sessions view after session recreation.
<h3>Confidence Score: 5/5</h3>
- This PR is safe to merge.
- Single-file, narrowly-scoped change that clears optional token fields on new cron session creation and uses existing freshness semantics to prevent stale usage values from being displayed. Aligns with the SessionEntry type and helper functions.
- No files require special attention
<sub>Last reviewed commit: 795aec3</sub>
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#23562: feat: add sessionFreshness config for isolated cron jobs (#23539)
by MunemHashmi · 2026-02-22
83.7%
#23699: fix(cron): isolated sessions must generate fresh UUID on every run ...
by echoVic · 2026-02-22
83.2%
#23501: fix(cron): force new session ID for isolated cron jobs (#23470)
by stakeswky · 2026-02-22
83.1%
#6315: fix(cron): persist isolated sessions (fixes #6217) - attempt 2
by ChaitanyaSai-Meka · 2026-02-01
81.7%
#20188: fix: Update sessionFile path when rolling to new session in cron jobs
by jriff · 2026-02-18
81.6%
#6801: docs: add token optimization guidance for isolated cron sessions
by ProofOfReach · 2026-02-02
79.5%
#7537: fix(sessions): reset token counts on /new for channel sessions
by SoniAssist · 2026-02-02
78.0%
#19385: fix: pass authProfileId from cron session to runEmbeddedPiAgent
by gigi-trifle · 2026-02-17
75.9%
#16511: feat(cron): support custom session IDs and auto-bind to current ses...
by kkhomej33-netizen · 2026-02-14
75.7%
#22387: fix: session_status context tracking undercount for cached providers
by 1ucian · 2026-02-21
75.5%