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.
This commit is contained in:
Glucksberg
2026-02-05 17:42:59 -04:00
committed by GitHub
parent 2ca78a8aed
commit 93b450349f

View File

@@ -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 };