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
This commit is contained in:
OpenClaw Bot
2026-02-16 14:09:21 +00:00
committed by Peter Steinberger
parent 767109e7d5
commit d6acd71576

View File

@@ -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,