utils — Core Utility Library

Core Utility Library

Massive 300+ utility library providing foundational functions for every other module. Categories include: git operations, file system helpers, token counting/estimation, permissions engine, sandbox isolation, config management, session storage, telemetry, MCP validation, shell execution, diff processing, and more.

30
Categories
564
Files
~180k
Lines
TypeScript
Language

🔵 Foundation Library 🟢 Git Operations 🟡 Permission Engine 🔴 Sandbox Isolation

🏭 Think of it this way: If Claude Code is a modern workshop, then utils is every tool, jig, and piece of equipment on the walls. Need to cut wood? Grab a saw from git/. Need to measure twice? Use the calipers in tokens.ts. Need to lock the door while you work? permissions/ is the deadbolt. No other module manufactures its own tools — they all reach for the ones hanging here. The workshop doesn't build the furniture itself, but nothing gets built without it.

Architecture

The utils module is organized into cohesive category clusters, each serving specific consumer modules throughout the application.

flowchart LR
  subgraph Consumers["Consumer Modules"]
    Tools["Tool Execution
(BashTool, FileEditTool)"] Session["Session Manager"] API["API Client"] UI["UI / Renderer"] end subgraph UtilsCore["utils — Core Categories"] direction TB subgraph FSCluster["File System"] file["file.ts"] fsOps["fsOperations.ts"] path["path.ts"] glob["glob.ts"] end subgraph ShellCluster["Shell Execution"] Shell["Shell.ts"] ShellCmd["ShellCommand.ts"] process["process.ts"] end subgraph ConfigCluster["Configuration"] config["config.ts"] env["env.ts"] settings["settings/"] end subgraph AuthCluster["Auth & Security"] auth["auth.ts"] perms["permissions/"] secure["secureStorage/"] sandbox["sandbox/"] end subgraph MsgCluster["Messages & Tokens"] messages["messages.ts"] tokens["tokens.ts"] context["analyzeContext.ts"] end subgraph GitCluster["Git Operations"] git["git.ts"] worktree["worktree.ts"] gitDiff["gitDiff.ts"] end end subgraph Infra["Infrastructure"] telemetry["telemetry/"] memory["memory/ & claudemd.ts"] model["model/"] mcp["mcp/"] end Tools --> ShellCluster Tools --> AuthCluster Tools --> FSCluster Session --> ConfigCluster Session --> MsgCluster API --> MsgCluster API --> model UI --> memory UI --> telemetry ShellCluster --> FSCluster AuthCluster --> ConfigCluster MsgCluster --> ConfigCluster GitCluster --> FSCluster

Data Flow — Git Operation Trace

Trace a findGitRoot() call from a tool invocation through the utils git cluster, showing memoization and filesystem probing.

sequenceDiagram
    participant Tool as BashTool
    participant Git as git.ts
    participant Cache as LRU Cache
    participant FS as fsOperations.ts
    participant Path as path.ts
    participant Telem as telemetry/

    Tool->>Git: findGitRoot(cwd)
    Git->>Cache: lookup(cwd)
    alt Cache HIT
        Cache-->>Git: cachedRoot
        Git-->>Tool: "/repo/root"
    else Cache MISS
        Cache-->>Git: null
        Git->>Path: normalize(cwd)
        Path-->>Git: normalizedPath
        loop Walk up directory tree
            Git->>FS: stat(dir + "/.git")
            FS-->>Git: exists / not found
        end
        Git->>Cache: store(cwd, foundRoot)
        Git->>Telem: logEvent("git_root_found", duration)
        Git-->>Tool: "/repo/root"
    end

🚶 Step-by-Step Narrated Walkthrough

Follow a single file permission check as it flows through the utils permission engine:

  1. Tool Invocation — FileEditTool receives a request to write /etc/hosts. Before any I/O, it must consult permissions/.
  2. Rule Set Loading — permissions/ruleLoader.ts reads the active rule set from settings/ (managed, project, and user-level rules merged by priority).
  3. Tool Name Matching — The engine matches the tool identifier (file_edit) against rule patterns — exact matches are tried first, then wildcards.
  4. Path Pattern Evaluation — Each rule that matched the tool is tested against the target path /etc/hosts. Glob patterns like /etc/** are evaluated via glob.ts.
  5. Priority Resolution — Rules are sorted by priority (managed > project > user > default). The first matching rule wins — in this case, a managed rule blocks /etc/* writes.
  6. Dangerous Pattern Detection — Even without an explicit rule, the classifier checks a built-in deny list (e.g., rm -rf /, writes to /etc). This is a safety net.
  7. Decision Assembly — The engine builds a result: { allowed: false, reason: "Managed rule blocks /etc/** writes", source: "managed" }.
  8. Telemetry Emission — telemetry/ records the decision: tool name, path, outcome, rule source, and latency (~2 ms typical).
  9. User Notification — The decision propagates back to the tool, which surfaces a permission-denied message to the user with the reason string.
  10. Audit Trail — sessionStorage.ts appends the permission event to the session log for post-session review and compliance reporting.

Key Components

#ComponentFileRole
1ShellShell.tsShell initialization, command execution, session management, provider selection
2ShellCommandShellCommand.tsCommand wrapping, streaming output, exit code handling (~14k lines)
3getConfig()config.tsGlobal/project config loading with re-entrancy guards; imported by 60+ files
4normalizeMessagesForAPI()messages.tsConverts internal messages to Anthropic SDK format; handles tool results
5analyzeContextWindow()analyzeContext.tsToken budget estimation, context compaction trigger, tool overhead calculation
6getSecureStorage()secureStorage/Platform-adaptive credential storage (macOS Keychain → plaintext fallback)
7findGitRoot()git.tsCached git root detection; handles worktrees and submodules
8parseClaudeMarkdown()claudemd.tsMemory file parsing with include directives and priority hierarchy
9loadSessionState()sessionStorage.tsSession recovery, persistent state deserialization (~5k lines)
10memoizeWithTTL()memoize.tsWrite-through cache with background async refresh (stale-while-revalidate)
11ripgrepCommand()ripgrep.tsResolves ripgrep executable (system / builtin / embedded modes)
12findExecutable()findExecutable.tsPATH-based executable lookup with platform-specific logic
13safeParseJSON()json.tsLRU-bounded JSONC parsing with 50-entry cache and error handling
14authauth.tsOAuth / API key management, token refresh, credential verification
15permissions enginepermissions/24-file rule engine: pattern matching, classifier, mode enforcement

Sub-Modules

git/

Git operations, repository detection, worktree management, and diff generation.

  • Cached findGitRoot() with LRU memoization
  • Worktree activation / deactivation lifecycle
  • Structured diff generation with context lines
  • Shallow clone detection and branch operations

permissions/

Fine-grained permission rule engine governing tool and file access control.

  • 24 files — rule loading, pattern matching, classifier
  • Priority-based resolution (managed → project → user)
  • Dangerous-pattern deny list as safety net
  • Audit trail integration with telemetry

sandbox/

Sandbox adapter for secure, isolated command execution environments.

  • Process isolation boundary enforcement
  • Adapter pattern for pluggable sandbox backends
  • Integration with permissions engine
  • Resource limit configuration

model/

Model options, capabilities, cost estimation, and provider selection.

  • 16 files covering model metadata and pricing
  • Provider-aware capability detection
  • Cost estimation for token budgeting

shell/

Shell provider abstraction — Bash, Zsh, PowerShell detection and configuration.

  • Platform-adaptive shell selection algorithm
  • Output size limits and stream management
  • Subprocess environment setup

telemetry/

Event logging, tracing (Perfetto format), session tracing, and analytics export.

  • Structured event emission with attributes
  • Perfetto-compatible trace generation
  • Statistics collection and BigQuery export

Configuration

Setting / ConstantLocationDefaultPurpose
TOOL_TOKEN_COUNT_OVERHEADanalyzeContext.ts500Fixed token cost added when tools are present
PARSE_CACHE_MAX_KEY_BYTESjson.ts8 KBMax JSON string length eligible for LRU cache
KILL_RING_MAX_SIZECursor.ts10Maximum entries in Emacs-style kill ring buffer
DEFAULT_TIMEOUTShell.ts30 minMaximum shell command execution time
MAX_OUTPUT_SIZEshell/0.25 MBMaximum file output capture size
CHECK_INTERVAL_MScron.ts1000 msCron scheduler check frequency
AUTOCOMPACT_BUFFERanalyzeContext.ts12%Context window threshold triggering auto-compaction
cacheLifetimeMsmemoize.ts5 minDefault TTL for memoized function results
LRU cache sizejson.ts50 entriesBounded JSON parse cache to prevent memory growth
CLAUDE_CODE_SHELLenv.ts(unset)Override shell provider selection

Directory Map

Source tree for utils (30 categories, 564 files)
utils/
├── git/                        ← Git filesystem ops, cached reads, worktree paths
│   ├── git.ts                  ← Root detection, branch/commit operations (926 lines)
│   ├── worktree.ts             ← Worktree management, activation (1519 lines)
│   ├── gitDiff.ts              ← Diff generation
│   └── gitSettings.ts          ← Git configuration reading
├── permissions/                ← Permission rule engine (24 files)
│   ├── ruleLoader.ts           ← Rule set loading and merging
│   ├── classifier.ts           ← ML-based operation classifier
│   └── patterns.ts             ← Glob pattern matching for rules
├── sandbox/                    ← Sandbox adapter for secure execution
├── model/                      ← Model options, costs, providers (16 files)
├── bash/                       ← Bash-specific specs (15 files + specs/)
├── shell/                      ← Shell provider abstraction (10 files)
│   ├── bashProvider.ts         ← Bash shell provider
│   └── powershellProvider.ts   ← PowerShell provider
├── mcp/                        ← Model Context Protocol support
├── memory/                     ← Memory type definitions (User, Project, Local)
├── settings/                   ← Settings loader, MDM, per-source tracking (16 files)
├── telemetry/                  ← Event logging, Perfetto tracing (9 files)
├── background/                 ← Background task management
├── hooks/                      ← Hook utilities
├── plugins/                    ← Plugin system, loading, validation (44 files)
├── swarm/                      ← Agent swarm management (13 files)
├── secureStorage/              ← Platform-specific credential storage (6 files)
├── computerUse/                ← Computer use tool integration (15 files)
├── task/                       ← Task persistence, output management (5 files)
├── deepLink/                   ← Deep link parsing and handling (6 files)
├── teleport/                   ← Session teleporting (4 files)
├── filePersistence/            ← Persistent file storage (2 files)
├── messages/                   ← Message mappers, utilities (2 files)
├── config.ts                   ← Global/project config (1817 lines)
├── env.ts                      ← Environment variables (10k lines)
├── auth.ts                     ← OAuth/API key management (2002 lines)
├── messages.ts                 ← Message normalization (5512 lines)
├── sessionStorage.ts           ← Session persistence (5105 lines)
├── claudemd.ts                 ← Memory file loading (1479 lines)
├── Shell.ts                    ← Shell management (1530 lines)
├── ShellCommand.ts             ← Command wrapping (14k lines)
├── analyzeContext.ts           ← Token budget analysis (1382 lines)
├── file.ts                     ← Core file operations (1519 lines)
├── memoize.ts                  ← TTL/LRU memoization
├── json.ts                     ← Safe JSONC parsing with LRU cache
├── diff.ts                     ← Structured diff generation (4.8k lines)
├── ripgrep.ts                  ← Ripgrep integration (679 lines)
└── ...                         ← 91+ additional utility primitives

Design Patterns

PatternExamplePurpose
Factory FunctiongetSecureStorage(), Shell.create()Platform/context-dependent object creation
LRU CachememoizeWithLRU(), fileReadCache.tsBounded memory caching with eviction
Stale-While-RevalidatememoizeWithTTL()Return instantly with cached value, refresh async
Provider PatternShellProvider, SecureStoragePluggable implementations behind interfaces
Fallback ChainKeychain → plaintext in secureStorage/Graceful degradation across platforms
Re-entrancy GuardinsideGetConfig flag in config.tsPrevent infinite recursion in config loading
Discriminated Union{ ok: true, value } vs { ok: false }Type-safe error handling without exceptions