Source Code Reference

Hard-coded constants, data flow, feature gates — verified against the Claude Code source.

Hard-Coded Constants

ConstantValuePurpose
MEMORY.md line cap200 linesIndex truncation
MEMORY.md byte cap25,000 bytesIndex truncation (for long-line indexes)
Per-file recall lines200 linesTopic file truncation when surfaced
Per-file recall bytes4,096 bytes~4KB per surfaced memory file
Session memory budget61,440 bytes60KB total for all surfaced memories
Max files scanned200 filesMemory directory scan limit
Max selected files5 filesHard-coded in Sonnet selector prompt
@import depth5 levelsPrevents circular reference loops
Frontmatter scan depth30 linesHow deep to look for YAML frontmatter
CLAUDE.md "large" flag40,000 chars/doctor warning only — not enforced
Auto-compact buffer13,000 tokensTriggers when window - usage < this
MCP description cap2,048 charsTool descriptions truncated beyond this
Dream time gate24 hoursMinimum time between consolidations
Dream session gate5 sessionsMinimum transcript files since last run
Dream lock stale1 hourLock PID considered stale after this

Complete Memory Data Flow

1
System Prompt Load

buildMemoryPrompt() reads MEMORY.md → dual truncation (200 lines / 25KB) → warning appended if truncated → injected into system prompt via getUserContext().

2
Per-Query Recall

User input → findRelevantMemories() (Sonnet scores filename+description, picks up to 5) → readMemoriesForSurfacing() (truncate 200 lines / 4KB each, 60KB budget) → injected as <system-reminder> attachments.

3
Memory Saving

End of turn → forked agent with restricted tools (Read/Grep/Glob + write only in memory dir) → pre-injected manifest → updates topic files + MEMORY.md. Throttled to every N turns. Skipped if main agent already wrote.

4
Dream Consolidation

Time gate (24h) + session gate (5) + PID lock → forked dream agent → reviews logs → merges into topic files → prunes MEMORY.md.

5
Session Memory (separate)

Periodic background sub-agent → extracts key session info into .session/memory.md → triggered at ~30K token init, ~20K between updates. Within-session only.

System Prompt Assembly Order

  1. Attribution header (billing)
  2. CLI prefix
  3. Introduction section
  4. System section (tools, compression, hooks)
  5. Doing tasks section
  6. Tools section (per-tool instructions)
  7. SYSTEM_PROMPT_DYNAMIC_BOUNDARY — cache scope split
  8. User/project context (CLAUDE.md, memory) — not globally cached
  9. Output style, language, MCP instructions
  10. Append system prompt

Content before the boundary is globally cacheable (shared across users). Content after (your CLAUDE.md, memory) is re-processed every API call.

All 27 Hook Events

Can block (exit code 2): PreToolUse, PermissionRequest, UserPromptSubmit, Stop, SubagentStop, TaskCreated, TaskCompleted, ConfigChange, Elicitation, WorktreeCreate, TeammateIdle

Informational: PostToolUse, PostToolUseFailure, SessionStart, SessionEnd, Notification, SubagentStart, CwdChanged, FileChanged, PreCompact, PostCompact, InstructionsLoaded, WorktreeRemove, Setup, PermissionDenied, ElicitationResult, StopFailure

Settings Precedence

Lowest to highest priority:

  1. userSettings: ~/.claude/settings.json
  2. projectSettings: .claude/settings.json (shared, committed)
  3. localSettings: .claude/settings.local.json (gitignored)
  4. flagSettings: --settings CLI flag
  5. policySettings: managed (remote API → MDM → managed-settings.json → drop-ins)

Arrays are concatenated and deduplicated. Objects are deep-merged. Scalars: higher priority overwrites.

Feature Gates

GatePurpose
KAIROSDaily log mode (perpetual sessions)
TEAMMEMTeam memory (shared via VCS)
tengu_passport_quailEnable memory extraction
tengu_bramble_lintelThrottle extraction frequency
tengu_onyx_ploverDream scheduling thresholds
tengu_moth_copseSkip MEMORY.md index in extraction prompt

Memory-Related Settings

KeyTypePurpose
autoMemoryEnabledbooleanToggle auto memory (default: true)
autoMemoryDirectorystringCustom path (supports ~/). Ignored in projectSettings for security.
autoDreamEnabledbooleanUser override for dream consolidation
claudeMdExcludesstring[]Glob patterns to skip CLAUDE.md files
disableAllHooksbooleanKill switch for all hooks