mirror of
https://github.com/openclaw/openclaw.git
synced 2026-02-19 18:39:20 -05:00
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:
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user