fix: emit session:reset hook before transcript archival

This commit is contained in:
Saurabh 2026-03-20 13:03:33 +05:30
parent a54be14070
commit 9cabdeff38

View File

@ -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) {