thedotmack/claude-mem · error

Missing cwd in PostToolUse hook input for session

Error message

Missing cwd in PostToolUse hook input for session ${sessionId}, tool ${toolName}

What it means

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.

Solutions

  1. Make sure the PostToolUse hook payload includes cwd (the session's project directory).
  2. When invoking the handler directly, supply cwd explicitly rather than relying on defaults.
  3. Rebuild/re-sync the plugin so hook entry points and handlers agree on the input schema.
  4. Guard in the dispatcher: if input.cwd is falsy, skip observation gracefully.

Example fix

// before
await observationHandler.execute({ sessionId, toolName, toolInput });
// after
await observationHandler.execute({ sessionId, cwd: process.cwd(), toolName, toolInput });
Defensive patterns

Strategy: validation

Validate before calling

if (!input.cwd) { logger.warn('PostToolUse input missing cwd; skipping observation'); return { continue: true, suppressOutput: true }; }

Type guard

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

Try / catch

try { await handler.execute(input); } catch (e) { if (e instanceof Error && e.message.startsWith('Missing cwd')) return { continue: true, suppressOutput: true }; throw e; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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/be0169063427dc1e. Report an issue: GitHub.

Appendix: source

Thrown at src/cli/handlers/observation.ts:56

  logger.debug('HOOK', 'Observation sent successfully via worker', { toolName: input.toolName });
  return { continue: true, suppressOutput: true };
}

export const observationHandler: EventHandler = {
  async execute(input: NormalizedHookInput): Promise<HookResult> {
    const { sessionId, cwd, toolName, toolInput, toolResponse } = input;
    const platformSource = normalizePlatformSource(input.platform);

    if (!toolName) {
      return { continue: true, suppressOutput: true, exitCode: HOOK_EXIT_CODES.SUCCESS };
    }

    const toolStr = logger.formatTool(toolName, toolInput);

    logger.dataIn('HOOK', `PostToolUse: ${toolStr}`, {});

    if (!cwd) {
      throw new Error(`Missing cwd in PostToolUse hook input for session ${sessionId}, tool ${toolName}`);
    }

    if (!shouldTrackProject(cwd)) {
      logger.debug('HOOK', 'Project excluded from tracking, skipping observation', { cwd, toolName });
      return { continue: true, suppressOutput: true };
    }

    const runtime = resolveRuntimeContext();
    // Phase 1a (cmem-sdk rename): `runtime.runtime` is the canonical `'server'`
    // value. `runtime-selector.selectRuntime()` continues to accept the legacy
    // `'server-beta'` literal in settings.json and normalizes it to `'server'`.
    if (runtime.runtime === 'server') {
      const event: ServerRecordEventRequest = {
        projectId: runtime.projectId,
        contentSessionId: sessionId,
        platformSource,
        sourceType: 'hook',
        eventType: 'tool_use',

View on GitHub (pinned to d8bc9755e7)