From 93b450349f77fa2521c7948a724a6ffdaeaeb09f Mon Sep 17 00:00:00 2001 From: Glucksberg <80581902+Glucksberg@users.noreply.github.com> Date: Thu, 5 Feb 2026 17:42:59 -0400 Subject: [PATCH] fix: clear stale token metrics on /new and /reset (#8929) When starting a new session via /new or /reset, the token usage fields (totalTokens, inputTokens, outputTokens, contextTokens) survived from the previous session via the spread pattern in session init. This caused /status to display misleading context usage from the old session. Clear all four token metrics explicitly in the isNewSession block, alongside the existing compactionCount reset. Also add diagnostic logging for session forking via ParentSessionKey to help trace context inheritance. --- src/auto-reply/reply/session.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/auto-reply/reply/session.ts b/src/auto-reply/reply/session.ts index 895c4d07e0..d3de9ef3fb 100644 --- a/src/auto-reply/reply/session.ts +++ b/src/auto-reply/reply/session.ts @@ -313,6 +313,10 @@ export async function initSessionState(params: { parentSessionKey !== sessionKey && sessionStore[parentSessionKey] ) { + console.warn( + `[session-init] forking from parent session: parentKey=${parentSessionKey} → sessionKey=${sessionKey} ` + + `parentTokens=${sessionStore[parentSessionKey].totalTokens ?? "?"}`, + ); const forked = forkSessionFromParent({ parentEntry: sessionStore[parentSessionKey], }); @@ -320,6 +324,7 @@ export async function initSessionState(params: { sessionId = forked.sessionId; sessionEntry.sessionId = forked.sessionId; sessionEntry.sessionFile = forked.sessionFile; + console.warn(`[session-init] forked session created: file=${forked.sessionFile}`); } } if (!sessionEntry.sessionFile) { @@ -333,6 +338,12 @@ export async function initSessionState(params: { sessionEntry.compactionCount = 0; sessionEntry.memoryFlushCompactionCount = undefined; sessionEntry.memoryFlushAt = undefined; + // Clear stale token metrics from previous session so /status doesn't + // display the old session's context usage after /new or /reset. + sessionEntry.totalTokens = undefined; + sessionEntry.inputTokens = undefined; + sessionEntry.outputTokens = undefined; + sessionEntry.contextTokens = undefined; } // Preserve per-session overrides while resetting compaction state on /new. sessionStore[sessionKey] = { ...sessionStore[sessionKey], ...sessionEntry };