{"record":{"id":"be0169063427dc1e","repo":"thedotmack/claude-mem","slug":"missing-cwd-in-posttooluse-hook-input-for-session","errorCode":null,"errorMessage":"Missing cwd in PostToolUse hook input for session ${sessionId}, tool ${toolName}","messagePattern":"Missing cwd in PostToolUse hook input for session (.+?), tool (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cli/handlers/observation.ts","lineNumber":56,"sourceCode":"  logger.debug('HOOK', 'Observation sent successfully via worker', { toolName: input.toolName });\n  return { continue: true, suppressOutput: true };\n}\n\nexport const observationHandler: EventHandler = {\n  async execute(input: NormalizedHookInput): Promise<HookResult> {\n    const { sessionId, cwd, toolName, toolInput, toolResponse } = input;\n    const platformSource = normalizePlatformSource(input.platform);\n\n    if (!toolName) {\n      return { continue: true, suppressOutput: true, exitCode: HOOK_EXIT_CODES.SUCCESS };\n    }\n\n    const toolStr = logger.formatTool(toolName, toolInput);\n\n    logger.dataIn('HOOK', `PostToolUse: ${toolStr}`, {});\n\n    if (!cwd) {\n      throw new Error(`Missing cwd in PostToolUse hook input for session ${sessionId}, tool ${toolName}`);\n    }\n\n    if (!shouldTrackProject(cwd)) {\n      logger.debug('HOOK', 'Project excluded from tracking, skipping observation', { cwd, toolName });\n      return { continue: true, suppressOutput: true };\n    }\n\n    const runtime = resolveRuntimeContext();\n    // Phase 1a (cmem-sdk rename): `runtime.runtime` is the canonical `'server'`\n    // value. `runtime-selector.selectRuntime()` continues to accept the legacy\n    // `'server-beta'` literal in settings.json and normalizes it to `'server'`.\n    if (runtime.runtime === 'server') {\n      const event: ServerRecordEventRequest = {\n        projectId: runtime.projectId,\n        contentSessionId: sessionId,\n        platformSource,\n        sourceType: 'hook',\n        eventType: 'tool_use',","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/d8bc9755e74915e5c3b999181e10a67c889bce2a/src/cli/handlers/observation.ts#L38-L74","documentation":"The PostToolUse hook handler needs `cwd` to decide whether to track the project and to record the tool observation. It throws when the normalized hook input has no cwd, since a tool event without a working directory cannot be attributed to a project. Like the FileEdit variant, this is an input-contract failure.","triggerScenarios":"The PostToolUse handler execute() receives input where `toolName`/`toolInput` are present but `cwd` is undefined or empty — the hook caller omitted cwd from the payload.","commonSituations":"A hook transport layer (SDK or JSON stdin) that forwards tool events without the project directory; custom integrations calling the handler directly; stale plugin code producing inputs in an older format that lacked cwd.","solutions":["Make sure the PostToolUse hook payload includes cwd (the session's project directory).","When invoking the handler directly, supply cwd explicitly rather than relying on defaults.","Rebuild/re-sync the plugin so hook entry points and handlers agree on the input schema.","Guard in the dispatcher: if input.cwd is falsy, skip observation gracefully."],"exampleFix":"// before\nawait observationHandler.execute({ sessionId, toolName, toolInput });\n// after\nawait observationHandler.execute({ sessionId, cwd: process.cwd(), toolName, toolInput });","handlingStrategy":"validation","validationCode":"if (!input.cwd) { logger.warn('PostToolUse input missing cwd; skipping observation'); return { continue: true, suppressOutput: true }; }","typeGuard":"const hasCwd = (i: Partial<NormalizedHookInput>): i is NormalizedHookInput & { cwd: string } => typeof i.cwd === 'string' && i.cwd.length > 0;","tryCatchPattern":"try { await handler.execute(input); } catch (e) { if (e instanceof Error && e.message.startsWith('Missing cwd')) return { continue: true, suppressOutput: true }; throw e; }","preventionTips":["Forward the full hook envelope (including cwd) from your transport layer","Add a pre-dispatch input validator that rejects missing cwd early with a clear log","Re-sync the plugin after upgrades so entry points and handlers share the schema"],"tags":["hooks","missing-field","input-validation"],"backgroundTag":"missing-required-argument","analyzedSha":"d8bc9755e74915e5c3b999181e10a67c889bce2a","analyzedAt":"2026-09-17T16:40:26.182Z","contentChangedAt":"2026-09-17T16:40:26.182Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}