From 2c8af78d20d4d1f9d2a1151fc1ff15bfec847a3d Mon Sep 17 00:00:00 2001 From: Shadril Hassan Shifat <63901551+shadril238@users.noreply.github.com> Date: Sat, 7 Feb 2026 05:22:38 +0600 Subject: [PATCH] fix(hooks): replace debug console.log with proper subsystem logging in session-memory (#10730) * fix: replace debug console.log with proper subsystem logging in session-memory * fix(hooks): normalize session-memory subsystem logging --------- Co-authored-by: Tak Hoffman <781889+Takhoffman@users.noreply.github.com> --- src/hooks/bundled/session-memory/handler.ts | 47 ++++++++++++++------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/hooks/bundled/session-memory/handler.ts b/src/hooks/bundled/session-memory/handler.ts index 6f2a531f07..a0c154d84b 100644 --- a/src/hooks/bundled/session-memory/handler.ts +++ b/src/hooks/bundled/session-memory/handler.ts @@ -12,9 +12,12 @@ import { fileURLToPath } from "node:url"; import type { OpenClawConfig } from "../../../config/config.js"; import type { HookHandler } from "../../hooks.js"; import { resolveAgentWorkspaceDir } from "../../../agents/agent-scope.js"; +import { createSubsystemLogger } from "../../../logging/subsystem.js"; import { resolveAgentIdFromSessionKey } from "../../../routing/session-key.js"; import { resolveHookConfig } from "../../config.js"; +const log = createSubsystemLogger("hooks/session-memory"); + /** * Read recent messages from session file for slug generation */ @@ -69,7 +72,7 @@ const saveSessionToMemory: HookHandler = async (event) => { } try { - console.log("[session-memory] Hook triggered for /new command"); + log.debug("Hook triggered for /new command"); const context = event.context || {}; const cfg = context.cfg as OpenClawConfig | undefined; @@ -92,9 +95,11 @@ const saveSessionToMemory: HookHandler = async (event) => { const currentSessionId = sessionEntry.sessionId as string; const currentSessionFile = sessionEntry.sessionFile as string; - console.log("[session-memory] Current sessionId:", currentSessionId); - console.log("[session-memory] Current sessionFile:", currentSessionFile); - console.log("[session-memory] cfg present:", !!cfg); + log.debug("Session context resolved", { + sessionId: currentSessionId, + sessionFile: currentSessionFile, + hasCfg: Boolean(cfg), + }); const sessionFile = currentSessionFile || undefined; @@ -111,10 +116,13 @@ const saveSessionToMemory: HookHandler = async (event) => { if (sessionFile) { // Get recent conversation content sessionContent = await getRecentSessionContent(sessionFile, messageCount); - console.log("[session-memory] sessionContent length:", sessionContent?.length || 0); + log.debug("Session content loaded", { + length: sessionContent?.length ?? 0, + messageCount, + }); if (sessionContent && cfg) { - console.log("[session-memory] Calling generateSlugViaLLM..."); + log.debug("Calling generateSlugViaLLM..."); // Dynamically import the LLM slug generator (avoids module caching issues) // When compiled, handler is at dist/hooks/bundled/session-memory/handler.js // Going up ../.. puts us at dist/hooks/, so just add llm-slug-generator.js @@ -124,7 +132,7 @@ const saveSessionToMemory: HookHandler = async (event) => { // Use LLM to generate a descriptive slug slug = await generateSlugViaLLM({ sessionContent, cfg }); - console.log("[session-memory] Generated slug:", slug); + log.debug("Generated slug", { slug }); } } @@ -132,14 +140,16 @@ const saveSessionToMemory: HookHandler = async (event) => { if (!slug) { const timeSlug = now.toISOString().split("T")[1].split(".")[0].replace(/:/g, ""); slug = timeSlug.slice(0, 4); // HHMM - console.log("[session-memory] Using fallback timestamp slug:", slug); + log.debug("Using fallback timestamp slug", { slug }); } // Create filename with date and slug const filename = `${dateStr}-${slug}.md`; const memoryFilePath = path.join(memoryDir, filename); - console.log("[session-memory] Generated filename:", filename); - console.log("[session-memory] Full path:", memoryFilePath); + log.debug("Memory file path resolved", { + filename, + path: memoryFilePath.replace(os.homedir(), "~"), + }); // Format time as HH:MM:SS UTC const timeStr = now.toISOString().split("T")[1].split(".")[0]; @@ -167,16 +177,21 @@ const saveSessionToMemory: HookHandler = async (event) => { // Write to new memory file await fs.writeFile(memoryFilePath, entry, "utf-8"); - console.log("[session-memory] Memory file written successfully"); + log.debug("Memory file written successfully"); // Log completion (but don't send user-visible confirmation - it's internal housekeeping) const relPath = memoryFilePath.replace(os.homedir(), "~"); - console.log(`[session-memory] Session context saved to ${relPath}`); + log.info(`Session context saved to ${relPath}`); } catch (err) { - console.error( - "[session-memory] Failed to save session memory:", - err instanceof Error ? err.message : String(err), - ); + if (err instanceof Error) { + log.error("Failed to save session memory", { + errorName: err.name, + errorMessage: err.message, + stack: err.stack, + }); + } else { + log.error("Failed to save session memory", { error: String(err) }); + } } };