thedotmack/claude-mem · error

Corrupt hooks.json at

Error message

Corrupt hooks.json at ${WINDSURF_HOOKS_JSON_PATH}, refusing to overwrite

What it means

mergeAndWriteHooksJson reads the existing Windsurf hooks.json and merges new hook entries into it. If the file exists but cannot be parsed as JSON (corrupt), it logs and throws this error instead of overwriting the user's file, protecting unknown/ manual hook configuration from data loss.

Solutions

  1. Open WINDSURF_HOOKS_JSON_PATH and fix the JSON syntax (or delete/rename the file so the installer can recreate it).
  2. Validate the file with a JSON linter or `bun -e 'JSON.parse(require("fs").readFileSync(path))'` to find the syntax error.
  3. Back up the file, delete it, re-run the Windsurf hooks setup, then re-apply custom hooks manually.
  4. Re-run installation after fixing — mergeAndWriteHooksJson only proceeds when the existing file parses.

Example fix

// before (hooks.json, corrupt)
{ "hooks": { "on_workspace_start": , }
// after
{ "hooks": { "on_workspace_start": [] } }
Defensive patterns

Strategy: try-catch

Validate before calling

const fs = require('fs');
if (fs.existsSync(hooksPath)) {
  try { JSON.parse(fs.readFileSync(hooksPath, 'utf8')); } catch (e) { /* fix or move the file first */ }
}

Try / catch

try {
  await installer.writeWindsurfHooksAndSetupContext(bunPath);
} catch (err) {
  if (String(err).includes('Corrupt hooks.json')) {
    // prompt user to fix or back up hooks.json, then retry
  }
}

Prevention

When it happens

Trigger: writeWindsurfHooksAndSetupContext invokes mergeAndWriteHooksJson while ~/.codeium/windsurf/hooks.json (WINDSURF_HOOKS_JSON_PATH) exists but contains invalid JSON — truncated write, merge-conflict markers, trailing commas, or hand-edited syntax errors.

Common situations: A previous crash or power loss left a partially written hooks.json; the user edited the file manually and broke the syntax; a version upgrade changed the format.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


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

Appendix: source

Thrown at src/services/integrations/WindsurfHooksInstaller.ts:143

  workerServicePath: string,
  workingDirectory: string,
): void {
  mkdirSync(WINDSURF_HOOKS_DIR, { recursive: true });

  let existingConfig: WindsurfHooksJson = { hooks: {} };
  if (existsSync(WINDSURF_HOOKS_JSON_PATH)) {
    try {
      existingConfig = parseJsonWithBom<WindsurfHooksJson>(readFileSync(WINDSURF_HOOKS_JSON_PATH, 'utf-8'));
      if (!existingConfig.hooks) {
        existingConfig.hooks = {};
      }
    } catch (error) {
      if (error instanceof Error) {
        logger.error('WORKER', 'Corrupt hooks.json, refusing to overwrite', { path: WINDSURF_HOOKS_JSON_PATH }, error);
      } else {
        logger.error('WORKER', 'Corrupt hooks.json, refusing to overwrite', { path: WINDSURF_HOOKS_JSON_PATH }, new Error(String(error)));
      }
      throw new Error(`Corrupt hooks.json at ${WINDSURF_HOOKS_JSON_PATH}, refusing to overwrite`);
    }
  }

  for (const eventName of WINDSURF_HOOK_EVENTS) {
    const command = buildHookCommand(bunPath, workerServicePath, eventName);

    const hookEntry: WindsurfHookEntry = {
      command,
      show_output: false,
      working_directory: workingDirectory,
    };

    const existingHooks = (existingConfig.hooks[eventName] ?? []).filter(
      (hook) => !hook.command.includes('worker-service') || !hook.command.includes('windsurf')
    );

    existingConfig.hooks[eventName] = [...existingHooks, hookEntry];
  }

View on GitHub (pinned to d8bc9755e7)