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

4.5 KB
Raw Blame
// CLAUDE_MEM_SKIP_BASH_PATTERNS matching, shared by every capture path so the
// setting means the same thing whichever runtime the user picked: the worker
// ingest path (src/services/worker/http/shared.ts) and the PostToolUse hook's
// server-runtime branch (src/cli/handlers/observation.ts), which sends events
// straight to the server and never passes through worker ingest.

import { logger } from '../utils/logger.js';

// Compile each CLAUDE_MEM_SKIP_BASH_PATTERNS value once, not per observation:
// ingestObservation runs on the hot path. A cached `null` marks a value that
// failed to compile, so an invalid regex warns once instead of on every Bash
// command until the setting is fixed.
const bashPatternCache = new Map<string, RegExp | null>();

function getBashSkipPattern(pattern: string): RegExp | null {
  const cached = bashPatternCache.get(pattern);
  if (cached !== undefined) return cached;

  let compiled: RegExp | null = null;
  try {
    compiled = new RegExp(pattern);
  } catch (error) {
    logger.warn('INGEST', 'Invalid CLAUDE_MEM_SKIP_BASH_PATTERNS regex — ignoring', {
      pattern,
      error: error instanceof Error ? error.message : String(error),
    });
  }
  bashPatternCache.set(pattern, compiled);
  return compiled;
}

// This is deliberately not a shell parser. When syntax is ambiguous across
// Bash and PowerShell, retain the observation rather than drop part of it.
function simpleCommandSegments(command: string): string[] | null {
  const segments: string[] = [];
  let start = 0;
  let quote: "'" | '"' | null = null;
  for (let i = 0; i < command.length; i++) {
    const char = command[i];
    // cmd.exe expands %VAR% even inside quotes; ^ and ! also have shell-
    // specific meanings. Without the actual shell, don't guess their effect.
    if ('%^!'.includes(char)) return null;
    if (quote === "'") {
      // cmd.exe does not treat single quotes as quoting. Shell operators or
      // a double quote here could change command boundaries or redirect IO.
      if ('&|;\n\r"<>(){}'.includes(char)) return null;
      if (char === "'") quote = null;
      continue;
    }
    if (char === '`' || char === '\\' || char === '$') return null;
    if (quote === '"') {
      if (char === '\n' || char === '\r') return null;
      if (char === '"') quote = null;
      continue;
    }
    if (char === "'" || char === '"') {
      quote = char;
      continue;
    }
    if ('<>(){}#'.includes(char)) return null;
    if (';|&\n\r'.includes(char)) {
      const segment = command.slice(start, i).trim();
      if (segment) segments.push(segment);
      if ((char === '|' || char === '&') && command[i + 1] === char) i++;
      start = i + 1;
    }
  }
  if (quote) return null;
  const last = command.slice(start).trim();
  if (last) segments.push(last);
  return segments.length ? segments : null;
}

// The shell command CLAUDE_MEM_SKIP_BASH_PATTERNS is matched against. Claude
// Code sends `Bash` + `command` (Cursor and Windsurf adapters normalize to the
// same shape); the Codex transcript watcher sends `exec_command` + `cmd`; on
// Windows the primary shell arrives as `PowerShell` + `command` (issue #4745 —
// without this branch the setting silently did nothing there); Antigravity's
// agy sends `run_command`, and its adapter copies `CommandLine` to `command`.
function shellCommandOf(toolName: string, toolInput: unknown): string {
  if (!toolInput || typeof toolInput !== 'object') return '';
  const input = toolInput as { command?: unknown; cmd?: unknown };
  const command =
    toolName === 'Bash' || toolName === 'PowerShell' || toolName === 'run_command' ? input.command
    : toolName === 'exec_command' ? input.cmd
    : undefined;
  return typeof command === 'string' ? command : '';
}

export function isShellCommandSkipped(
  toolName: string,
  toolInput: unknown,
  settings: { CLAUDE_MEM_SKIP_BASH_PATTERNS?: string; CLAUDE_MEM_SKIP_BASH_PATTERNS_MATCH_EVERY_SEGMENT?: string },
): boolean {
  const skipBashPatterns = (settings.CLAUDE_MEM_SKIP_BASH_PATTERNS ?? '').trim();
  const command = skipBashPatterns ? shellCommandOf(toolName, toolInput) : '';
  if (!command) return false;
  // A bad user regex never throws here — getBashSkipPattern returns null, so the
  // command is captured as if no pattern was set.
  const pattern = getBashSkipPattern(skipBashPatterns);
  const segments = settings.CLAUDE_MEM_SKIP_BASH_PATTERNS_MATCH_EVERY_SEGMENT === 'true'
    ? simpleCommandSegments(command)
    : [command];
  return !!pattern && pattern.test(command) && !!segments && segments.every(segment => pattern.test(segment));
}