Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

9.6 KB
Raw Blame
/**
 * Entry point for every agent hook: `claude-mem hook <agent> <event>`.
 *
 * The agent's adapter (src/cli/adapters/) turns its event payload into one
 * shared shape, the event's handler (src/cli/handlers/) talks to the worker,
 * and the result goes back in the form that agent expects. A hook must never
 * stall or break the agent's session, so every failure path exits gracefully.
 */
import { readJsonFromStdin } from './stdin-reader.js';
import { getPlatformAdapter } from './adapters/index.js';
import { AdapterRejectedInput } from './adapters/errors.js';
import { getEventHandler } from './handlers/index.js';
import type { HookResult } from './types.js';
import { HOOK_EXIT_CODES, isToolHookDisabledByEnv } from '../shared/hook-constants.js';
import {
  installHookStderrBuffer,
  emitModelContext,
  waitForModelContext,
  emitDiagnostic,
  exitGraceful,
  resetHookIoState,
  HookStdoutError,
} from '../shared/hook-io.js';
import {
  recordWorkerUnreachable,
  resetWorkerUnreachableState,
  setActiveHookType,
  getActiveHookType,
  isWorkerUnavailableError,
} from '../shared/worker-utils.js';
import { captureCliEvent } from '../services/telemetry/cli-telemetry.js';
import { settleHookSpoolNudges } from './spool-hook-event.js';
import { canonicalIntegrationId } from '../shared/integration-id.js';
import { logger } from '../utils/logger.js';
import { loadFromFileOnce } from '../shared/hook-settings.js';
import { isObserverCaptureEnabled } from '../shared/observer-capture.js';
import { isPluginDisabledInCodexConfig } from '../shared/plugin-state.js';
import { GROK_BUILD_PLATFORM_SOURCE } from '../shared/platform-source.js';

export interface HookCommandOptions {
  skipExit?: boolean;
  stdinSafetyTimeoutMs?: number;
}

/**
 * No-op result for hooks that must exit before their handler ran (adapter
 * rejected input, transcript path missing). `context` is the sole handler
 * key that produces SessionStart output on every platform; a bare
 * `{continue:true}` fallback for it — with no hookSpecificOutput — is what
 * Codex's strict SessionStart validator rejects as "invalid session start
 * JSON output" (issue #2972). Attaching the minimal valid payload keeps the
 * no-op harmless everywhere else too.
 */
export function buildNoOpResult(event: string): HookResult {
  const result: HookResult = { continue: true, suppressOutput: true };
  if (event === 'context') {
    result.hookSpecificOutput = { hookEventName: 'SessionStart', additionalContext: '' };
  }
  return result;
}

export function isNonBlockingHookInputError(error: unknown): boolean {
  const message = error instanceof Error ? error.message : String(error);
  const lower = message.toLowerCase();

  if (lower.startsWith('malformed json at stdin eof:') || lower.startsWith('incomplete json after ')) {
    return true;
  }

  return lower.includes('transcript path') &&
    (lower.includes('missing') || lower.includes('does not exist'));
}

/**
 * The host the handlers record. Grok Build loads the Claude Code plugin, so its
 * hooks arrive as `hook claude-code <event>` and keep the claude-code adapter,
 * which reads Grok's camelCase payload. Grok sets GROK_HOOK_EVENT in every hook's
 * environment, which tells its sessions apart from Claude Code's (#6419).
 */
export function hookHostPlatform(platform: string, environment: NodeJS.ProcessEnv = process.env): string {
  if (platform === 'claude-code' && environment.GROK_HOOK_EVENT) return GROK_BUILD_PLATFORM_SOURCE;
  return platform;
}

async function executeHookPipeline(
  adapter: ReturnType<typeof getPlatformAdapter>,
  handler: ReturnType<typeof getEventHandler>,
  platform: string,
  options: HookCommandOptions
): Promise<number> {
  const rawInput = await readJsonFromStdin({ safetyTimeoutMs: options.stdinSafetyTimeoutMs });
  const input = adapter.normalizeInput(rawInput);
  input.platform = hookHostPlatform(platform);
  const result = await handler.execute(input);

  // MODEL_CONTEXT: the only stdout JSON emit, via the platform adapter.
  emitModelContext(adapter, result);
  // Await the write before the handoff wait: a pipe closed meanwhile must reach the
  // HookStdoutError path, not become an unhandled rejection.
  await waitForModelContext();
  const exitCode = result.exitCode ?? HOOK_EXIT_CODES.SUCCESS;
  // A write hook spooled its event and started a nudge to the worker; let it
  // land (≤ 250 ms) so the drain starts now — process.exit would kill it.
  await settleHookSpoolNudges();
  await exitGraceful(options);
  return exitCode;
}

export async function hookCommand(rawPlatform: string, event: string, options: HookCommandOptions = {}): Promise<number> {
  const platform = canonicalIntegrationId(rawPlatform);
  resetHookIoState();
  resetWorkerUnreachableState();
  // Register the hook event for the threshold-gated hook_failed telemetry
  // (closed enum enforced inside; non-enum events just omit hook_type).
  setActiveHookType(event);

  // Install before loading settings: creation/migration/read warnings from the
  // loader must follow the same quiet-on-success discipline as handler output.
  const stderrBuffer = installHookStderrBuffer();

  // #3106: env opt-out for the high-frequency tool hooks. Checked before stdin
  // and handler work, and still emits the no-op envelope so the host gets
  // valid JSON.
  let hookDisabled: boolean;
  try {
    hookDisabled = isToolHookDisabledByEnv(event)
      // Codex runs these hooks from the user's hooks.json even with the plugin
      // switched off, so the plugin toggle is honoured here.
      || (platform === 'codex' && isPluginDisabledInCodexConfig())
      // Codex still needs the pipeline with capture off: its PostToolUse is how a
      // compacted thread gets its memory timeline back. The handler skips the write.
      || (event === 'observation' && platform !== 'codex' && !isObserverCaptureEnabled(loadFromFileOnce()));
  } catch (error) {
    stderrBuffer.restore();
    throw error;
  }
  if (hookDisabled) {
    try {
      const adapter = getPlatformAdapter(platform);
      emitModelContext(adapter, buildNoOpResult(event));
      await exitGraceful(options);
      return HOOK_EXIT_CODES.SUCCESS;
    } finally {
      stderrBuffer.restore();
    }
  }

  // Hook IO Discipline (issue #2292):
  // We BUFFER stderr during handler execution so that unsolicited writes from
  // third-party libraries don't leak into model context. Every exit path drops
  // the buffer — preserving the original "quiet on success" behavior.
  //
  // To bypass the buffer for a specific write, use emitDiagnostic from
  // src/shared/hook-io.ts. Direct process.stderr.write calls are buffered.
  const adapter = getPlatformAdapter(platform);
  const handler = getEventHandler(event);

  try {
    return await executeHookPipeline(adapter, handler, platform, options);
  } catch (error) {
    // A closed or failed stdout pipe cannot accept a replacement envelope.
    // Preserve the delivery failure instead of reporting success or double-emitting.
    if (error instanceof HookStdoutError) throw error;
    if (error instanceof AdapterRejectedInput) {
      logger.warn('HOOK', `Adapter rejected input (${error.reason}), skipping hook`);
      emitModelContext(adapter, buildNoOpResult(event));
      await exitGraceful(options);
      return HOOK_EXIT_CODES.SUCCESS;
    }
    if (isNonBlockingHookInputError(error)) {
      logger.warn('HOOK', `Hook input unavailable, skipping hook: ${error instanceof Error ? error.message : error}`);
      emitModelContext(adapter, buildNoOpResult(event));
      await exitGraceful(options);
      return HOOK_EXIT_CODES.SUCCESS;
    }
    if (isWorkerUnavailableError(error)) {
      logger.warn('HOOK', `Worker unavailable, skipping hook: ${error instanceof Error ? error.message : error}`);
      // EXIT_SIGNAL per CLAUDE.md: transient worker errors exit 0 to avoid
      // Windows Terminal tab accumulation. The fail-loud counter (worker-utils
      // recordWorkerUnreachable) never exits; when the count JUST reaches the
      // threshold it sends the hook_failed telemetry and writes a diagnostic.
      // Awaited: exitGraceful below would kill a pending POST mid-flight.
      await recordWorkerUnreachable();
      await exitGraceful(options);
      return HOOK_EXIT_CODES.SUCCESS;
    }

    const errorMessage = error instanceof Error ? error.message : String(error);
    logger.error('HOOK', `Hook error: ${errorMessage}`, {}, error instanceof Error ? error : undefined);
    // plan-17 step 2 (#3161): an unexpected claude-mem error never blocks the
    // user. This path used to exit 2, which Claude Code reads as "block":
    // UserPromptSubmit dropped the prompt, PreToolUse denied the tool, and Stop
    // re-woke the agent in a loop. Every event now gets the no-op envelope and
    // exit 0; the error reaches the log, one stderr diagnostic line, and
    // telemetry. The telemetry is awaited because exitGraceful would kill a
    // pending POST mid-flight; captureCliEvent never throws and is hard-capped
    // at 2s. Closed-enum props only: the error message itself is never sent.
    // error_mode keeps its documented 'blocking_error' value so the series
    // stays continuous, even though the hook no longer blocks.
    {
      const hookType = getActiveHookType();
      await captureCliEvent('hook_failed', {
        ...(hookType !== null ? { hook_type: hookType } : {}),
        error_mode: 'blocking_error',
        threshold_tripped: false,
      });
    }
    emitDiagnostic(`claude-mem: hook error, continuing without memory: ${errorMessage}\n`);
    emitModelContext(adapter, buildNoOpResult(event));
    await exitGraceful(options);
    return HOOK_EXIT_CODES.SUCCESS;
  } finally {
    stderrBuffer.restore();
  }
}

export { isWorkerUnavailableError } from '../shared/worker-utils.js';