#6653: fix: persist archived session entry on /new or /reset
Cluster:
Session Management Improvements
## Problem
When users trigger `/new` or `/reset`, the previous session entry was being overwritten in `sessions.json` because the store is keyed by `sessionKey` (e.g., `agent:main:webchat`), not by `sessionId`.
This caused old sessions to disappear from the sessions list in the UI, even though their transcript files (`.jsonl`) still existed on disk. Users could see 7 transcript files but only 1 entry in `sessions.json`.
## Solution
Archive the previous session entry with a unique key (`${sessionKey}:session:${previousSessionId}`) before saving the new session. This preserves the old session metadata while still allowing the new session to be the "active" one at the original `sessionKey`.
## Changes
- Modified `src/auto-reply/reply/session.ts` to persist `previousSessionEntry` to the store when `resetTriggered` is true
## Testing
- ✅ All 14 tests in `src/auto-reply/reply/session.test.ts` pass
- ✅ All 41 tests in `src/config/sessions` pass
- ✅ Build completes successfully (`pnpm build`)
## AI-Assisted Disclosure
This fix was identified and implemented with AI assistance (Claude). The bug was traced through analysis of the session initialization flow, where `previousSessionEntry` was captured but never persisted to the store during a reset.
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
This PR updates `initSessionState` to persist (archive) the previous `SessionEntry` into `sessions.json` under a derived key when `/new` or `/reset` is triggered, preventing the previous session’s metadata from being overwritten at the primary `sessionKey` while a new active session starts.
The change integrates into the existing session initialization flow by hooking into the same `updateSessionStore` write that updates `store[sessionKey]`, adding an extra write of `previousSessionEntry` under an archive key before storing the new entry.
<h3>Confidence Score: 3/5</h3>
- This PR is likely safe to merge, with a small risk of session-history loss in repeated reset scenarios due to archive key collisions.
- The change is localized and follows existing store update patterns, but the archive key derivation can overwrite an existing archived entry if the same previous sessionId is archived again (e.g., repeated `/reset` without the active sessionId changing), which undermines the stated goal of preserving historical entries.
- src/auto-reply/reply/session.ts
<!-- greptile_other_comments_section -->
<sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub>
**Context used:**
- Context from `dashboard` - CLAUDE.md ([source](https://app.greptile.com/review/custom-context?memory=fd949e91-5c3a-4ab5-90a1-cbe184fd6ce8))
- Context from `dashboard` - AGENTS.md ([source](https://app.greptile.com/review/custom-context?memory=0d0c8278-ef8e-4d6c-ab21-f5527e322f13))
<!-- /greptile_comment -->
Most Similar PRs
#14358: fix: preserve session overrides (verbose, model) across /new or /rese…
by WyattSanders · 2026-02-12
83.2%
#14018: fix: auto-remind AI to check workspace context files on session reset
by WalterSumbon · 2026-02-11
80.8%
#11613: fix: clear stale model metadata on /new and /reset
by mcaxtr · 2026-02-08
80.6%
#4664: fix: per-session metadata files to eliminate lock contention
by tsukhani · 2026-01-30
80.3%
#7537: fix(sessions): reset token counts on /new for channel sessions
by SoniAssist · 2026-02-02
80.1%
#19313: fix(session-memory): preserve rotated transcript selection on /new
by ayanesakura · 2026-02-17
80.0%
#15888: fix: store relative session file paths instead of absolute
by devAnon89 · 2026-02-14
79.4%
#16061: fix(sessions): tolerate invalid sessionFile metadata
by haoyifan · 2026-02-14
79.4%
#6315: fix(cron): persist isolated sessions (fixes #6217) - attempt 2
by ChaitanyaSai-Meka · 2026-02-01
78.7%
#20188: fix: Update sessionFile path when rolling to new session in cron jobs
by jriff · 2026-02-18
78.7%