From d6acd71576cda5a319abbcc244614eb5be91045d Mon Sep 17 00:00:00 2001 From: OpenClaw Bot Date: Mon, 16 Feb 2026 14:09:21 +0000 Subject: [PATCH] fix: session-memory hook finds previous session file after /new/reset When /new or /reset is triggered, the session file gets rotated before the hook runs. The hook was reading the new (empty) file instead of the previous session content. This fix: 1. Checks if the session file looks like a reset file (.reset.) 2. Falls back to finding the most recent non-reset .jsonl file 3. Logs debug info about which file was used Fixes openclaw/openclaw#18088 --- src/hooks/bundled/session-memory/handler.ts | 25 ++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/hooks/bundled/session-memory/handler.ts b/src/hooks/bundled/session-memory/handler.ts index c8f59fb5141..08213c757c3 100644 --- a/src/hooks/bundled/session-memory/handler.ts +++ b/src/hooks/bundled/session-memory/handler.ts @@ -133,12 +133,35 @@ const saveSessionToMemory: HookHandler = async (event) => { const dateStr = now.toISOString().split("T")[0]; // YYYY-MM-DD // Generate descriptive slug from session using LLM + // Prefer previousSessionEntry (old session before /new) over current (which may be empty) const sessionEntry = (context.previousSessionEntry || context.sessionEntry || {}) as Record< string, unknown >; + let currentSessionFile = (sessionEntry.sessionFile as string) || undefined; + + // If sessionFile is empty or looks like a new/reset file, try to find the previous session file + if (!currentSessionFile || currentSessionFile.includes(".reset.")) { + // Look for previous session file in the sessions directory + const sessionsDir = path.dirname(currentSessionFile || ""); + if (sessionsDir) { + try { + const files = await fs.readdir(sessionsDir); + const sessionFiles = files + .filter((f) => f.endsWith(".jsonl") && !f.includes(".reset.")) + .toSorted() + .toReversed(); + if (sessionFiles.length > 0) { + currentSessionFile = path.join(sessionsDir, sessionFiles[0]); + log.debug("Found previous session file", { file: currentSessionFile }); + } + } catch { + // Ignore errors reading directory + } + } + } + const currentSessionId = sessionEntry.sessionId as string; - const currentSessionFile = sessionEntry.sessionFile as string; log.debug("Session context resolved", { sessionId: currentSessionId,