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

7.6 KB
Raw Blame
// IO discipline (see src/shared/hook-io.ts): this handler is PURE. It returns a
// HookResult and MUST NOT call process.stderr.write / process.stdout.write /
// console.* / process.exit. logger.* calls are DIAGNOSTIC; thrown errors are
// caught by hookCommand, logged, and answered with a no-op (never exit 2).
import type { EventHandler, NormalizedHookInput, HookResult } from '../types.js';
import { spoolHookEvent } from '../spool-hook-event.js';
import { logger } from '../../utils/logger.js';
import { redactForLog } from '../../utils/redaction.js';
import { HOOK_EXIT_CODES } from '../../shared/hook-constants.js';
import { shouldTrackProject } from '../../shared/should-track-project.js';
import { shouldSkipAgentObservation } from '../../shared/should-skip-agent-observation.js';
import { loadFromFileOnce } from '../../shared/hook-settings.js';
import { isToolSkipped } from '../../shared/is-tool-skipped.js';
import { isShellCommandSkipped } from '../../shared/shell-command-skip.js';
import { normalizePlatformSource } from '../../shared/platform-source.js';
import { resolveRuntimeContext, logServerFallback } from '../../services/hooks/runtime-selector.js';
import { isServerClientError, type ServerRecordEventRequest } from '../../services/hooks/server-client.js';
import { isRecursiveMemoryTool, isExplicitMemoryWrite } from '../../shared/memory-retrieval-tools.js';
import { workStatePlatform } from '../../shared/work-state-telemetry.js';
import { boundObservationDispatch, scanMediaFields } from '../../shared/media-ingress.js';

// Derived from the Codex PostToolUse wire timeout of 5 s (`timeout: 5` on
// 'PostToolUse.0.0' in scripts/build-hooks.js). Measured hook startup plus
// spool write is ~0.3 s warm and ~0.5 s on a first run (Apple Silicon Mac,
// 2026-10-25); Windows cold start was not measured. 5 s minus that cost
// leaves ~4.5 s; 2 s for the server attempt keeps more than half of that
// as margin for slow starts, so a stalled server still falls back to the
// local spool before Codex kills the hook. Change both values together.
const SERVER_OBSERVATION_TIMEOUT_MS = 2_000;

function spoolObservation(input: NormalizedHookInput, platformSource: string): HookResult {
  // Same pre-dispatch bound as the server path: image bodies survive for
  // worker-side capture unless they would push the event past the JSON cap.
  const bounded = boundObservationDispatch({ tool_input: input.toolInput, tool_response: input.toolResponse });
  spoolHookEvent('observation', {
    contentSessionId: input.sessionId,
    platformSource,
    telemetryPlatformSource: workStatePlatform(input.platform),
    toolName: input.toolName!,
    nativeToolName: input.nativeToolName,
    toolInput: bounded.tool_input,
    toolResponse: bounded.tool_response,
    cwd: input.cwd,
    agentId: input.agentId,
    agentType: input.agentType,
    toolUseId: input.toolUseId,
    // The hook's own clock: this tool use is happening now.
    eventOccurredAtEpochMs: Date.now(),
  });
  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 };
    }

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

    // A Bash command line or URL can carry a secret; logs get the redacted form.
    const toolStr = redactForLog(logger.formatTool(toolName, scanMediaFields(toolInput, undefined, 'disabled').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 };
    }

    // #2736 — drop subagent observations BEFORE any worker HTTP call or provider
    // request. Placed ahead of the runtime branch so it covers both the worker
    // and server runtimes. Saves the round-trip and the provider tokens, and
    // prevents Dynamic Workflows fan-out from exhausting provider quota.
    const skip = shouldSkipAgentObservation(input.agentId, input.agentType, loadFromFileOnce());
    if (skip.skip) {
      logger.debug('HOOK', `Skipping observation: ${skip.reason}`, {
        toolName,
        agentId: input.agentId,
        agentType: input.agentType,
      });
      return { continue: true, suppressOutput: true, exitCode: HOOK_EXIT_CODES.SUCCESS };
    }

    // Save echoes go through the worker spool so ingest can retain a write
    // receipt, without generating a second observation of the explicit note.
    if (isExplicitMemoryWrite(toolName)) {
      return spoolObservation(input, platformSource);
    }

    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') {
      // Server events never pass through worker ingest, which is where
      // CLAUDE_MEM_SKIP_TOOLS applies in worker runtime, so apply it here.
      if (isToolSkipped(toolName, loadFromFileOnce().CLAUDE_MEM_SKIP_TOOLS, input.nativeToolName)) {
        logger.debug('HOOK', 'Server runtime: tool excluded by CLAUDE_MEM_SKIP_TOOLS', { toolName });
        return { continue: true, suppressOutput: true, exitCode: HOOK_EXIT_CODES.SUCCESS };
      }
      // CLAUDE_MEM_SKIP_BASH_PATTERNS is applied there too, for the same reason.
      if (isShellCommandSkipped(toolName, toolInput, loadFromFileOnce())) {
        logger.debug('HOOK', 'Server runtime: command excluded by CLAUDE_MEM_SKIP_BASH_PATTERNS', { toolName });
        return { continue: true, suppressOutput: true, exitCode: HOOK_EXIT_CODES.SUCCESS };
      }
      const event: ServerRecordEventRequest = {
        projectId: runtime.projectId,
        contentSessionId: sessionId,
        platformSource,
        sourceType: 'hook',
        eventType: 'tool_use',
        occurredAtEpoch: Date.now(),
        payload: boundObservationDispatch({
          tool_name: toolName,
          tool_input: toolInput,
          tool_response: toolResponse,
          cwd,
          agentId: input.agentId,
          agentType: input.agentType,
          platformSource,
          tool_use_id: input.toolUseId,
        }),
      };
      try {
        await runtime.client.recordEvent(
          event,
          input.platform === 'codex' ? { timeoutMs: SERVER_OBSERVATION_TIMEOUT_MS } : undefined,
        );
        logger.debug('HOOK', 'Observation sent successfully via server', { toolName });
        return { continue: true, suppressOutput: true };
      } catch (error: unknown) {
        if (isServerClientError(error) && error.isFallbackEligible()) {
          logServerFallback(error.kind, { status: error.status, message: error.message, route: '/v1/events' });
          // fall through to the worker spool
        } else {
          logger.error('HOOK', 'Server event failed (non-recoverable)', {
            error: error instanceof Error ? error.message : String(error),
          });
          return { continue: true, suppressOutput: true, exitCode: HOOK_EXIT_CODES.SUCCESS };
        }
      }
    }

    return spoolObservation(input, platformSource);
  },
};