From 9cabdeff38e5882ae095157885d66b981d0f2a4d Mon Sep 17 00:00:00 2001 From: Saurabh Date: Fri, 20 Mar 2026 13:03:33 +0530 Subject: [PATCH] fix: emit session:reset hook before transcript archival --- src/auto-reply/reply/session.ts | 36 +++++++++++++++++---------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/src/auto-reply/reply/session.ts b/src/auto-reply/reply/session.ts index bfe2f8fd5c7..b5f005335ae 100644 --- a/src/auto-reply/reply/session.ts +++ b/src/auto-reply/reply/session.ts @@ -558,6 +558,25 @@ export async function initSessionState(params: { }, ); + // When a session is auto-reset (idle timeout or daily reset), emit a + // session:reset internal hook so the session-memory hook can persist + // the outgoing session context — just as it does for manual /new and /reset. + // This MUST fire before archiveSessionTranscripts() so the handler can still + // read the original transcript file (which archival renames). + if (isNewSession && !resetTriggered && previousSessionEntry) { + const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId); + const autoResetEvent = createInternalHookEvent("session", "reset", sessionKey, { + sessionEntry, + previousSessionEntry, + commandSource: "auto-reset", + workspaceDir, + cfg, + }); + void triggerInternalHook(autoResetEvent).catch((err) => { + log.debug("session:reset hook error (auto-reset)", { error: String(err) }); + }); + } + // Archive old transcript so it doesn't accumulate on disk (#14869). if (previousSessionEntry?.sessionId) { archiveSessionTranscripts({ @@ -586,23 +605,6 @@ export async function initSessionState(params: { IsNewSession: isNewSession ? "true" : "false", }; - // When a session is auto-reset (idle timeout or daily reset), emit a - // session:reset internal hook so the session-memory hook can persist - // the outgoing session context — just as it does for manual /new and /reset. - if (isNewSession && !resetTriggered && previousSessionEntry) { - const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId); - const autoResetEvent = createInternalHookEvent("session", "reset", sessionKey, { - sessionEntry, - previousSessionEntry, - commandSource: "auto-reset", - workspaceDir, - cfg, - }); - void triggerInternalHook(autoResetEvent).catch((err) => { - log.debug("session:reset hook error (auto-reset)", { error: String(err) }); - }); - } - // Run session plugin hooks (fire-and-forget) const hookRunner = getGlobalHookRunner(); if (hookRunner && isNewSession) {