thedotmack/claude-mem · error

Missing cwd in FileEdit hook input for session

Error message

Missing cwd in FileEdit hook input for session ${sessionId}, file ${filePath}

What it means

The FileEdit hook handler requires the `cwd` field on the normalized hook input so it can decide whether the project is tracked and record the edit against the right project. claude-mem throws this error when the hook input arrives without a working directory, because an edit cannot be attributed to any project. It is an input-contract failure of the hook pipeline, not a runtime condition the handler can recover from.

Solutions

  1. Ensure the hook invocation payload includes the cwd field (the directory of the Claude session).
  2. If building the input programmatically, pass `cwd: process.cwd()` or the session's project directory into the handler.
  3. Check for a mismatched claude-mem version where the hook entry script and handler expect different input shapes; upgrade both via npm run build-and-sync.
  4. Catch this in the hook dispatcher and skip observation instead of failing the hook.

Example fix

// before
await fileEditHandler.execute({ sessionId, filePath, edits });
// after
await fileEditHandler.execute({ sessionId, cwd: process.cwd(), filePath, edits });
Defensive patterns

Strategy: validation

Validate before calling

if (!input.cwd) throw new TypeError('fileEditHandler requires cwd; pass the session working directory');

Type guard

function hasCwd(input: Partial<NormalizedHookInput>): input is NormalizedHookInput & { cwd: string } { return typeof input.cwd === 'string' && input.cwd.length > 0; }

Try / catch

try { await fileEditHandler.execute(input); } catch (e) { if (String(e.message).includes('Missing cwd')) { logger.warn('skipping file-edit observation: no cwd'); } else throw e; }

Prevention

When it happens

Trigger: fileEditHandler.execute() is invoked with a NormalizedHookInput whose `cwd` is undefined/empty (falsy), while `filePath` is present — e.g. a hook payload constructed by a platform integration that omits cwd.

Common situations: Custom or third-party platform adapters calling the internal handler directly with a partially populated input; a hook JSON envelope missing the cwd key; a broken/old hook invocation script that drops the cwd field.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of thedotmack/claude-mem@d8bc9755e7 (2026-09-17). Data as JSON: /api/errors/12f5d41337280215. Report an issue: GitHub.

Appendix: source

Thrown at src/cli/handlers/file-edit.ts:23

import { HOOK_EXIT_CODES } from '../../shared/hook-constants.js';
import { normalizePlatformSource } from '../../shared/platform-source.js';
import { shouldTrackProject } from '../../shared/should-track-project.js';

export const fileEditHandler: EventHandler = {
  async execute(input: NormalizedHookInput): Promise<HookResult> {
    const { sessionId, cwd, filePath, edits } = input;
    const platformSource = normalizePlatformSource(input.platform);

    if (!filePath) {
      throw new Error('fileEditHandler requires filePath');
    }

    logger.dataIn('HOOK', `FileEdit: ${filePath}`, {
      editCount: edits?.length ?? 0
    });

    if (!cwd) {
      throw new Error(`Missing cwd in FileEdit hook input for session ${sessionId}, file ${filePath}`);
    }

    if (!shouldTrackProject(cwd)) {
      logger.debug('HOOK', 'Project excluded from tracking, skipping file edit observation', { cwd, filePath });
      return { continue: true, suppressOutput: true, exitCode: HOOK_EXIT_CODES.SUCCESS };
    }

    const result = await executeWithWorkerFallback<{ status?: string }>(
      '/api/sessions/observations',
      'POST',
      {
        contentSessionId: sessionId,
        platformSource,
        tool_name: 'write_file',
        tool_input: { filePath, edits },
        tool_response: { success: true },
        cwd,
      },

View on GitHub (pinned to d8bc9755e7)