{"record":{"id":"37463fdb47b98679","repo":"thedotmack/claude-mem","slug":"adapter-rejected-input-invalid-cwd-claude-code","errorCode":null,"errorMessage":"adapter rejected input: invalid_cwd","messagePattern":"adapter rejected input: invalid_cwd","errorType":"validation","errorClass":"AdapterRejectedInput","httpStatus":null,"severity":"error","filePath":"src/cli/adapters/claude-code.ts","lineNumber":16,"sourceCode":"import type { PlatformAdapter, NormalizedHookInput, HookResult } from '../types.js';\nimport { AdapterRejectedInput, isValidCwd } from './errors.js';\nimport { resolveHookProjectPath } from '../../utils/project-name.js';\n\nconst MAX_AGENT_FIELD_LEN = 128;\nconst pickAgentField = (v: unknown): string | undefined =>\n  typeof v === 'string' && v.length > 0 && v.length <= MAX_AGENT_FIELD_LEN ? v : undefined;\nconst pickStringField = (v: unknown): string | undefined =>\n  typeof v === 'string' ? v : undefined;\n\nexport const claudeCodeAdapter: PlatformAdapter = {\n  normalizeInput(raw) {\n    const r = (raw ?? {}) as any;\n    const inputCwd = r.cwd ?? process.cwd();\n    if (!isValidCwd(inputCwd)) {\n      throw new AdapterRejectedInput('invalid_cwd');\n    }\n    const cwd = resolveHookProjectPath(inputCwd);\n    if (!isValidCwd(cwd)) {\n      throw new AdapterRejectedInput('invalid_cwd');\n    }\n    return {\n      sessionId: r.session_id ?? r.id ?? r.sessionId,\n      cwd,\n      prompt: r.prompt,\n      toolName: r.tool_name,\n      toolInput: r.tool_input,\n      toolResponse: r.tool_response,\n      toolUseId: typeof r.tool_use_id === 'string' ? r.tool_use_id : undefined,\n      transcriptPath: r.transcript_path,\n      reason: pickStringField(r.reason),\n      agentId: pickAgentField(r.agent_id),\n      agentType: pickAgentField(r.agent_type),\n    };","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/thedotmack/claude-mem/blob/d8bc9755e74915e5c3b999181e10a67c889bce2a/src/cli/adapters/claude-code.ts#L1-L34","documentation":"claudeCodeAdapter.normalizeInput throws AdapterRejectedInput('invalid_cwd') at the first check when the raw input cwd (or process.cwd() fallback) is not a non-empty string. This is the pre-normalization gate before resolveHookProjectPath is applied.","triggerScenarios":"Invoking the claude-code adapter with raw where r.cwd is undefined, null, empty string, or a non-string type. Since process.cwd() is the fallback, the failure occurs when r.cwd is explicitly an invalid value (empty/non-string) or process.cwd() itself fails/returns empty.","commonSituations":"Claude Code hook payloads with \"cwd\": null; scripts passing process.env.CLAUDE_PROJECT_DIR which is empty string (empty string does not fall through ?? because '' is not nullish, so the empty value is used); misconfigured hook command templates substituting an empty variable.","solutions":["Supply a valid non-empty directory string as cwd in the hook input","Check that the hook template does not substitute an empty variable for cwd (e.g. ${CLAUDE_PROJECT_DIR:-$PWD} instead of ${CLAUDE_PROJECT_DIR})","Remove the cwd key to rely on process.cwd() fallback","Catch AdapterRejectedInput and inspect .reason === 'invalid_cwd' to report the misconfigured hook"],"exampleFix":"// before\nclaudeCodeAdapter.normalizeInput({ cwd: process.env.CLAUDE_PROJECT_DIR ?? '' }); // '' if unset\n// after\nclaudeCodeAdapter.normalizeInput({ cwd: process.env.CLAUDE_PROJECT_DIR || process.cwd() });","handlingStrategy":"validation","validationCode":"if (typeof raw?.cwd !== 'string' || raw.cwd.length === 0) raw = { ...raw, cwd: process.cwd() };","typeGuard":"function hasValidCwd(v: unknown): v is string { return typeof v === 'string' && v.length > 0; }","tryCatchPattern":"try { adapter.normalizeInput(raw); } catch (e) { if (e instanceof AdapterRejectedInput && e.reason === 'invalid_cwd') { /* retry with process.cwd() */ } else throw e; }","preventionTips":["Use ${VAR:-fallback} shell expansion in hook templates so empty env vars never reach cwd","Pass absolute paths only","Omit cwd rather than passing null/empty string"],"tags":["input-validation","adapter","cwd"],"backgroundTag":"invalid-argument-value","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"}