Part III · Surviving Long Horizon: Context Management & Memory
Chapter 10
Memory Systems: CLAUDE.md, Auto-Memory, Session Memory, and Auto-Dream
Layer: Layer 3 (Long-Horizon Context & Memory)
Prerequisites: Chapter 01, Chapter 02, Chapter 04, Chapter 07, Chapter 08, Chapter 09
Primary Source Files:
utils/claudemd.ts—CLAUDE.mddiscovery, HTML stripping,@include,.claude/rules/, caches (1,479 lines)utils/attachments.ts,query.ts,bootstrap/state.ts— nested memory injection,loadedNestedMemoryPaths,using-scopedstartRelevantMemoryPrefetch(), andcachedClaudeMdContentmemdir/memdir.ts,memdir/memoryTypes.ts,memdir/memoryScan.ts,memdir/findRelevantMemories.ts—MEMORY.mdtruncation, 4-type taxonomy, frontmatter scan, and Sonnet relevance selectormemdir/paths.ts,memdir/memoryAge.ts,memdir/teamMemPaths.ts,memdir/teamMemPrompts.ts— enablement & path trust, staleness caveats, symlink containment, and team promptsservices/SessionMemory/sessionMemory.ts,services/SessionMemory/sessionMemoryUtils.ts,services/SessionMemory/prompts.ts— post-sampling scratchpad feeding Chapter 09services/extractMemories/:services/extractMemories/extractMemories.ts,services/extractMemories/prompts.ts— turn-end topic extraction, coalescing, andcreateAutoMemCanUseTool()services/autoDream/autoDream.ts,services/autoDream/config.ts,services/autoDream/consolidationLock.ts,services/autoDream/consolidationPrompt.ts— 3-gate consolidation, PID+mtime lock, and leaf configservices/teamMemorySync/:services/teamMemorySync/index.ts,services/teamMemorySync/secretScanner.ts,services/teamMemorySync/teamMemSecretGuard.ts,services/teamMemorySync/types.ts,services/teamMemorySync/watcher.ts— OAuth delta sync, gitleaks scanning, and circuit-broken watcherutils/forkedAgent.ts(Ch 18 seam) andtasks/DreamTask/DreamTask.ts(Ch 17 seam)- (Snapshot absences — call sites only, never linked:
memdir/memoryShapeTelemetry.js,types/message.js,constants/querySource.js)
Why This Layer Exists
Chapter 09 showed how Claude Code forgets turns to fit a finite window. Memory solves the symmetric problem across five horizons: CLAUDE.md/.claude/rules/, persistent memdir/ with optional relevance prefetch, live SessionMemory (summary.md), turn-end extractMemories plus autoDream consolidation, and OAuth teamMemorySync.
Every tier upholds three invariants: preserve the prompt cache (Chapter 04), keep latency off the critical path, and prevent arbitrary writes or credential leaks.
1. Hierarchical CLAUDE.md, Conditional Rules, and Nested Injection (utils/claudemd.ts, utils/attachments.ts)
The Problem
Chapter 04 covered getUserContext() (context.ts:155-189), honoring CLAUDE_CODE_DISABLE_CLAUDE_MDS and --bare before getMemoryFiles() → filterInjectedMemoryFiles() → getClaudeMds(). Beneath it sit six challenges: priority scopes, nested worktrees, HTML comments, recursive @includes, path-scoped rules, and Chapter 01's 100-entry LRU FileStateCache evicting injected CLAUDE.md files.
The Mechanism: Reverse-Priority Walk, Single-Pass Lexing, and 4-Phase Nested Injection
getMemoryFiles() (utils/claudemd.ts:790-1075) memoizes an async walker loading scopes in reverse priority so local rules sit closest to model attention (docblock at utils/claudemd.ts:9-10):
- Managed (
utils/claudemd.ts:804-823): Always loaded (ungated byisSettingSourceEnabled). Reads platform<managedDir>/CLAUDE.md(utils/settings/managedPath.ts:8-25) and unconditional.claude/rules/*.md. - User (
utils/claudemd.ts:826-847): Gated onuserSettings. Loads~/.claude/CLAUDE.mdand unconditional~/.claude/rules/*.md(conditionalRule: false,utils/claudemd.ts:844) withincludeExternal = true(trusted outsideoriginalCwd). - Root-to-CWD Project & Local (
utils/claudemd.ts:850-934): Walks upward fromgetOriginalCwd()stopping before root (!== parse(currentDir).root), reverses root → CWD, and loadsCLAUDE.md,.claude/CLAUDE.md, unconditional.claude/rules/*.md(projectSettings), andCLAUDE.local.md(localSettings). - Opt-in
--add-dir(utils/claudemd.ts:940-977): Gated onCLAUDE_CODE_ADDITIONAL_DIRECTORIES_CLAUDE_MD; bypassesprojectSettings(explicit user intent; SDK defaultssettingSourcesto[]). AutoMem&TeamMementrypoints (utils/claudemd.ts:980-1007): WhenisAutoMemoryEnabled()(and underfeature('TEAMMEM'),isTeamMemoryEnabled()), readsMEMORY.mdandteam/MEMORY.mdvia directsafelyReadMemoryFileAsync()—skipping@includeandclaudeMdExcludes.
Dedup never inspects stat.ino. processMemoryFile() (utils/claudemd.ts:618-685) tracks processedPaths: Set<string> via normalizePathForComparison() (utils/file.ts:565-577) plus safeResolvePath()'s symlink target (utils/fsOperations.ts:138-178, blocking UNC paths and FIFOs/sockets before realpathSync). processMdRules() (utils/claudemd.ts:697-788) adds visitedDirs to break directory-symlink cycles.
parseMemoryFileContent() (utils/claudemd.ts:343-400) rejects binary files outside TEXT_FILE_EXTENSIONS (utils/claudemd.ts:96-227) and parses YAML paths: via parseFrontmatterPaths() (utils/claudemd.ts:254-279, using quoteProblematicValues() at utils/frontmatterParser.ts:85-121 and brace-aware splitPathInFrontmatter() at utils/frontmatterParser.ts:189-232). It lexes once for stripHtmlCommentsFromTokens() (utils/claudemd.ts:303-334) and extractIncludePathsFromTokens() (utils/claudemd.ts:451-535):
// utils/claudemd.ts:356-388
const { content: withoutFrontmatter, paths } =
parseFrontmatterPaths(rawContent)
// Lex once so strip and @include-extract share the same tokens. gfm:false
// is required by extract (so ~/path doesn't tokenize as strikethrough) and
// doesn't affect strip (html blocks are a CommonMark rule).
const hasComment = withoutFrontmatter.includes('<!--')
const tokens =
hasComment || includeBasePath !== undefined
? new Lexer({ gfm: false }).lex(withoutFrontmatter)
: undefined
// Only rebuild via tokens when a comment actually needs stripping —
// marked normalises \r\n during lex, so round-tripping a CRLF file
// through token.raw would spuriously flip contentDiffersFromDisk.
const strippedContent =
hasComment && tokens
? stripHtmlCommentsFromTokens(tokens).content
: withoutFrontmatter
const includePaths =
tokens && includeBasePath !== undefined
? extractIncludePathsFromTokens(tokens, includeBasePath)
: []
// Truncate MEMORY.md entrypoints to the line AND byte caps
let finalContent = strippedContent
if (type === 'AutoMem' || type === 'TeamMem') {
finalContent = truncateEntrypointContent(strippedContent).content
}
// Covers frontmatter strip, HTML comment strip, and MEMORY.md truncation
const contentDiffersFromDisk = finalContent !== rawContent
stripHtmlCommentsFromTokens() strips closed block html tokens (<!--..-->), preserving code blocks, inline comments, unclosed typos, and trailing type-2 text. @include caps depth at MAX_INCLUDE_DEPTH = 5 (utils/claudemd.ts:537) and flags non-User includes outside CWD via getExternalClaudeMdIncludes() (utils/claudemd.ts:1404-1414). Three filters shape injection:
isClaudeMdExcluded()(utils/claudemd.ts:547-612): Matchessettings.claudeMdExcludesviapicomatchonUser/Project/Localonly, resolving prefixes throughrealpathSync(resolveExcludePatterns()) for macOS/tmp.filterInjectedMemoryFiles()(utils/claudemd.ts:1142-1151): StripsAutoMem/TeamMemunder GrowthBooktengu_moth_copse.getClaudeMds()(utils/claudemd.ts:1153-1195): OmitsProject/Localunder GrowthBooktengu_paper_halyard, wrapsTeamMemin<team-memory-content source="shared">(feature('TEAMMEM')), and leavesCLAUDE.mduntruncated (MAX_MEMORY_CHARACTER_COUNT = 40000atutils/claudemd.ts:92,getLargeMemoryFiles()atutils/claudemd.ts:1132-1134advisory).
For lazy discovery, FileReadTool appends read paths to nestedMemoryAttachmentTriggers (tools/FileReadTool/FileReadTool.ts:848, tools/FileReadTool/FileReadTool.ts:870, tools/FileReadTool/FileReadTool.ts:1038). getAttachments() awaits @-mentions first (utils/attachments.ts:819) before draining triggers (utils/attachments.ts:872, skipped under CLAUDE_CODE_DISABLE_ATTACHMENTS/CLAUDE_CODE_SIMPLE, utils/attachments.ts:753-754), and getOpenedFileFromIDE() calls discovery directly (utils/attachments.ts:1878).
getNestedMemoryAttachmentsForFile() (utils/attachments.ts:1792-1862) checks pathInAllowedWorkingPath(), then runs four phases: (1) Managed/User conditional rules (utils/claudemd.ts:1205-1238), (2) getDirectoriesToProcess() (utils/attachments.ts:1656-1689: nestedDirs CWD→target, cwdLevelDirs root→CWD). Phases (3) nested dirs (utils/claudemd.ts:1249-1318) and (4) ancestor rules (utils/claudemd.ts:1329-1397, ignore()) both honor GrowthBook tengu_paper_halyard (utils/attachments.ts:1823-1826, utils/attachments.ts:1834, utils/attachments.ts:1851).
memoryFilesToAttachments() (utils/attachments.ts:1710-1775) decouples session-wide deduplication from edit-safety caching:
// utils/attachments.ts:1718-1750
for (const memoryFile of memoryFiles) {
// Dedup: loadedNestedMemoryPaths is a non-evicting Set; readFileState
// is a 100-entry LRU that drops entries in busy sessions, so relying
// on it alone re-injects the same CLAUDE.md on every eviction cycle.
if (toolUseContext.loadedNestedMemoryPaths?.has(memoryFile.path)) {
continue
}
if (!toolUseContext.readFileState.has(memoryFile.path)) {
attachments.push({
type: 'nested_memory',
path: memoryFile.path,
content: memoryFile,
displayPath: relative(getCwd(), memoryFile.path),
})
toolUseContext.loadedNestedMemoryPaths?.add(memoryFile.path)
// Mark as loaded in readFileState — this provides cross-function and
// cross-turn dedup via the .has() check above.
//
// When the injected content doesn't match disk (stripped HTML comments,
// stripped frontmatter, truncated MEMORY.md), cache the RAW disk bytes
// with `isPartialView: true`. Edit/Write see the flag and require a real
// Read first; getChangedFiles sees real content + undefined offset/limit
// so mid-session change detection still works.
toolUseContext.readFileState.set(memoryFile.path, {
content: memoryFile.contentDiffersFromDisk
? (memoryFile.rawContent ?? memoryFile.content)
: memoryFile.content,
timestamp: Date.now(),
offset: undefined,
limit: undefined,
isPartialView: memoryFile.contentDiffersFromDisk,
})
Hidden Invariants & Production Scars
- Non-evicting
Setvs. 100-entry LRU:FileStateCachecaps at100entries /25 MB(utils/fileStateCache.ts:18-22). Storing paths inloadedNestedMemoryPaths(Tool.ts:222, owned byQueryEngine.ts:198andscreens/REPL.tsx:1967) stops eviction re-injection; compaction (services/compact/compact.ts:522,services/compact/compact.ts:921) and/clear(commands/clear/conversation.ts:132) clear it. - Silent vs. hook-arming cache resets: Worktrees, settings sync, and
/memorycall silentclearMemoryFileCaches()(utils/claudemd.ts:1119-1122). Reloads callresetGetMemoryFilesCache('compact')(utils/claudemd.ts:1124-1130), armingshouldFireHook = truealongsidegetUserContext.cache.clear?.()in Chapter 09'sservices/compact/postCompactCleanup.ts:59-60. - Nested git worktree dedup (
#29599,utils/claudemd.ts:859-934): Inside.claude/worktrees/<name>/,skipProjectskips checked-inProjectfiles above the worktree while keeping gitignoredCLAUDE.local.mdfrom main. unconditionalProcessedPathsclone scar (utils/claudemd.ts:1288-1315):processMdRules()marks every visited.mdinprocessedPathsbefore filteringconditionalRule; cloningprocessedPathsfor the unconditional pass avoids silencing sibling conditional rules.- Stale docblock ordering scar (
utils/claudemd.ts:23,utils/claudemd.ts:616vs.utils/claudemd.ts:663-682): Both docblocks claim@included children precede their parent, yet code pushes the parent first (result.push(memoryFile)) and appends children after.
2. Persistent Auto-Memory & Team Memory Layout (memdir/)
The Problem
Writable cross-session memory raises four hazards: hostile repos redirecting writes to ~/.ssh, unbounded MEMORY.md indexes bloating turn-1 context, models saving derivable code or stale file:line claims, and symlink traversal in shared team memory.
The Mechanism: Path Trust, Dual Caps, 4-Type Taxonomy, and Two-Pass Containment
memdir/paths.ts resolves enablement via 5-level isAutoMemoryEnabled() (memdir/paths.ts:30-55): (1) tri-state CLAUDE_CODE_DISABLE_AUTO_MEMORY (truthy off, defined-falsy forces on); (2) CLAUDE_CODE_SIMPLE (--bare off); (3) CLAUDE_CODE_REMOTE off unless CLAUDE_CODE_REMOTE_MEMORY_DIR is set; (4) settings.autoMemoryEnabled; (5) default true.
getAutoMemPath() (memdir/paths.ts:223-235) memoizes per getProjectRoot() (skipping four parseSettingsFile calls per render) and shares one dir across worktrees via findCanonicalGitRoot() (memdir/paths.ts:203-205, #24382). getAutoMemPathSetting() (memdir/paths.ts:179-186) reads autoMemoryDirectory from policy, flag, local, and user settings—excluding projectSettings. validateMemoryPath() (memdir/paths.ts:109-150) rejects relative paths, bare ~/~/, drive roots, UNC prefixes, and null bytes.
Path Trust Boundary (Ch 10 → Ch 11): Because
~/.claude/sits insideDANGEROUS_DIRECTORIES,utils/permissions/filesystem.ts:1572-1581grants a silent pre-safety write carve-out when!hasAutoMemPathOverride() && isAutoMemPath(normalizedPath). ExcludingprojectSettingsblocks hostile repos from redirecting writes to~/.ssh;CLAUDE_COWORK_MEMORY_PATH_OVERRIDE(memdir/paths.ts:161-166) setshasAutoMemPathOverride()(memdir/paths.ts:194-196)true, withholding silent writes from SDK mounts.
Both utils/claudemd.ts:384 and subagent buildMemoryPrompt() (memdir/memdir.ts:272-316) bound MEMORY.md via truncateEntrypointContent() (memdir/memdir.ts:57-103) at MAX_ENTRYPOINT_LINES = 200 and MAX_ENTRYPOINT_BYTES = 25_000 (memdir/memdir.ts:35-38). It checks wasByteTruncated on original trimmed.length (UTF-16 units, unlike services/teamMemorySync/index.ts:435's Buffer.byteLength) and slices at lastIndexOf('\n', MAX_ENTRYPOINT_BYTES) so links never split mid-line.
loadMemoryPrompt() (memdir/memdir.ts:419-507) feeds Chapter 04's systemPromptSection('memory', ...) (constants/prompts.ts:495). In Team and Individual modes (not KAIROS), it calls ensureMemoryDirExists() (memdir/memdir.ts:129-147) so DIR_EXISTS_GUIDANCE / DIRS_EXIST_GUIDANCE (memdir/memdir.ts:116-119) avoids ls/mkdir -p turns, threading CLAUDE_COWORK_MEMORY_EXTRA_GUIDELINES (memdir/memdir.ts:441-446) into both builders. Mode precedence:
feature('KAIROS') && autoEnabled && getKairosActive()(memdir/memdir.ts:432-438):buildAssistantDailyLogPrompt()(memdir/memdir.ts:327-370) overrides team memory, skipsensureMemoryDirExists()(instructing first-write creation atmemdir/memdir.ts:348), and emits literal<autoMemPath>/logs/YYYY/MM/YYYY-MM-DD.mdto survive midnight rollover.feature('TEAMMEM') && isTeamMemoryEnabled()(memdir/memdir.ts:448-473): Requires auto-memory + GrowthBooktengu_herring_clock(memdir/teamMemPaths.ts:73-78), callsensureMemoryDirExists(teamDir)(memdir/memdir.ts:459, creatingautoDirvia recursivemkdir), and returnsbuildCombinedMemoryPrompt()(memdir/teamMemPrompts.ts:22-100).- Individual auto-memory (
memdir/memdir.ts:475-490): CallsensureMemoryDirExists(autoDir)(memdir/memdir.ts:479) and returnsbuildMemoryLines()(memdir/memdir.ts:199-266).
All three builders append GrowthBook tengu_coral_fern search guidance via buildSearchingPastContextSection() (memdir/memdir.ts:375-407, called at memdir/memdir.ts:366, memdir/teamMemPrompts.ts:96, memdir/memdir.ts:263).
memdir/memoryTypes.ts defines the closed 4-type taxonomy ('user' | 'feedback' | 'project' | 'reference', memdir/memoryTypes.ts:14-21) and WHAT_NOT_TO_SAVE_SECTION (memdir/memoryTypes.ts:183-195).
memdir/memoryAge.ts formats "today"/"yesterday"/"N days ago" via memoryAge() (memdir/memoryAge.ts:15-20); memoryFreshnessText() (memdir/memoryAge.ts:33-42) emits "This memory is N days old..." caveats for files > 1 day old, wrapped in <system-reminder> by memoryFreshnessNote() (memdir/memoryAge.ts:49-53, tools/FileReadTool/FileReadTool.ts:747-753).
For <autoMemPath>/team/, memdir/teamMemPaths.ts enforces PSR M22186/M22187 via sanitizePathKey() (memdir/teamMemPaths.ts:22-64), blocking null bytes, URL-encoded traversals, NFKC fullwidth ../, backslashes, and leading /. validateTeamMemWritePath() (memdir/teamMemPaths.ts:228-256) and validateTeamMemKey() (memdir/teamMemPaths.ts:265-284) pair trailing-sep containment with realpathDeepestExisting() (memdir/teamMemPaths.ts:109-171), trapping dangling symlinks, ELOOP, and EACCES/EIO.
Hidden Invariants & Production Scars
- 197 KB
MEMORY.mdp100 scar (memdir/memdir.ts:36-38): Long single-line paragraphs hit 197 KB under 200 lines, forcing the dual cap and original-length check. - Eval-validated wording (
memdir/memoryTypes.ts:192-194,memdir/memoryTypes.ts:204-222,memdir/memoryTypes.ts:224-256): Tightening save exclusions jumped 0/2 → 3/3;#22856added the ignore-memory rule (memdir/memoryTypes.ts:220); renaming"Trusting what you recall"to"## Before recommending from memory"(same body) jumped 0/3 → 3/3 (0/3 gap on slash commands like/fork).
3. Latency-Hidden Relevant Memory Prefetch with using (memdir/memoryScan.ts, memdir/findRelevantMemories.ts, utils/attachments.ts, query.ts)
The Problem
Eager MEMORY.md injection crowds context as topic files grow, yet synchronous relevance selection adds ~500–1,500 ms to time-to-first-token.
The Mechanism: Four-Subsystem tengu_moth_copse Switch & Disposable Zero-Wait Prefetch
One GrowthBook flag, tengu_moth_copse (default false), switches four subsystems: filterInjectedMemoryFiles() (utils/claudemd.ts:1142-1151) strips AutoMem/TeamMem MEMORY.md, both memdir/memdir.ts:422-425 (inside loadMemoryPrompt()) and services/extractMemories/extractMemories.ts:366-369 set skipIndex = true, and startRelevantMemoryPrefetch() (utils/attachments.ts:2361-2424) activates in queryLoop.
findRelevantMemories() (memdir/findRelevantMemories.ts:39-75) calls scanMemoryFiles() (memdir/memoryScan.ts:35-77, split for circular import #25372), scanning up to 200 newest .md files (30 header lines + mtimeMs in one readFileInRange() call). Pre-filtering !alreadySurfaced, selectRelevantMemories() (memdir/findRelevantMemories.ts:77-141) builds formatMemoryManifest() (memdir/memoryScan.ts:84-94) and runs Sonnet sideQuery (max_tokens: 256, json_schema, up to 5 files, recentTools suppression at utils/attachments.ts:2465-2503).
Snapshot Absence (
memdir/memoryShapeTelemetry.js): Underfeature('MEMORY_SHAPE_TELEMETRY'),memdir/findRelevantMemories.ts:66-72requires./memoryShapeTelemetry.jsand callslogMemoryRecallShape(memories, selected)even when empty;utils/sessionFileAccessHooks.ts:38-40andutils/sessionFileAccessHooks.ts:210-224calllogMemoryWriteShape(). Absent from this snapshot (call sites only).
To hide sideQuery latency behind streaming and tools, query.ts:297-304 binds using pendingMemoryPrefetch = startRelevantMemoryPrefetch(state.messages, state.toolUseContext) once per turn before while (true). Four cheap guards—isAutoMemoryEnabled() + tengu_moth_copse, non-meta prompt, >= 2 words, and collectSurfacedMemories(messages).totalBytes < 60 * 1024 (utils/attachments.ts:2383-2386, RELEVANT_MEMORIES_CONFIG.MAX_SESSION_BYTES at utils/attachments.ts:279-289)—return undefined (using no-op); otherwise it returns a handle:
// utils/attachments.ts:2390-2423
const controller = createChildAbortController(toolUseContext.abortController)
const firedAt = Date.now()
const promise = getRelevantMemoryAttachments(
input,
toolUseContext.options.agentDefinitions.activeAgents,
toolUseContext.readFileState,
collectRecentSuccessfulTools(messages, lastUserMessage),
controller.signal,
surfaced.paths,
).catch(e => {
if (!isAbortError(e)) {
logError(e)
}
return []
})
const handle: MemoryPrefetch = {
promise,
settledAt: null,
consumedOnIteration: -1,
[Symbol.dispose]() {
controller.abort()
logEvent('tengu_memdir_prefetch_collected', {
hidden_by_first_iteration:
handle.settledAt !== null && handle.consumedOnIteration === 0,
consumed_on_iteration: handle.consumedOnIteration,
latency_ms: (handle.settledAt ?? Date.now()) - firedAt,
})
},
}
void promise.finally(() => {
handle.settledAt = Date.now()
})
return handle
At queryLoop's post-tools stage (query.ts:1592-1614), collection never waits: it polls settledAt !== null && consumedOnIteration === -1, skipping if still running. When ready, filterDuplicateMemoryAttachments() (utils/attachments.ts:2520-2541) filters survivors, yields <system-reminder> attachments (utils/messages.ts:3708-3722), and sets consumedOnIteration = turnCount - 1.
Hidden Invariants & Production Scars
- Deterministic disposal across 12
queryLoopexits: TC39usingensures[Symbol.dispose]()runs on all 12return { reason: ... }exits (query.ts:646throughquery.ts:1711), throws, and.return(), cancelling orphaned side-queries and emittingtengu_memdir_prefetch_collected.createChildAbortController()(utils/abortController.ts:68-99) propagates Escape downward, isolates child abort, and cleans up viaWeakRef. - Precomputed
memoryHeadercache-bust scar (utils/attachments.ts:505-514,utils/attachments.ts:2327-2332): Formatting"saved N days ago"at render time (utils/messages.ts:3715) flips across midnight and busts cache; freezingheaderat creation stays byte-stable. - Mark-after-filter scar (
utils/attachments.ts:2506-2541): Writing paths toreadFileStateduringreadMemoriesForSurfacing()(utils/attachments.ts:2279-2321, capped at200lines /4096bytes atutils/attachments.ts:269-277) madefilterDuplicateMemoryAttachments()drop 100% of prefetched memories. Writing survivors after filtering breaks the cycle. - Self-healing 60 KB budget (
utils/attachments.ts:2251-2266): ScanningmessagesincollectSurfacedMemories()instead of a mutable counter lets Chapter 09 compaction restore the budget automatically.
4. Cache-Sharing Background Agents: SessionMemory and extractMemories (services/SessionMemory/, services/extractMemories/, utils/forkedAgent.ts)
The Problem
Background memory agents must read live history without cold cache_creation penalties or uncontrolled repo edits.
The Mechanism: runForkedAgent() Prefix Reuse, Mid-Turn Scratchpads, and Turn-End Extraction
SessionMemory, extractMemories, and autoDream run via utils/forkedAgent.ts (forward seam to Chapter 18). runForkedAgent() (utils/forkedAgent.ts:489-626) takes createCacheSafeParams(context) (utils/forkedAgent.ts:131-141) to preserve exact systemPrompt, userContext, systemContext, toolUseContext, and forkContextMessages.
createSubagentContext() (utils/forkedAgent.ts:345-462) clones readFileState and contentReplacementState, starts fresh loadedNestedMemoryPaths (utils/forkedAgent.ts:383), and stubs setAppState while keeping setAppStateForTasks. (Both constants/querySource.js at utils/forkedAgent.ts:14 and types/message.js at utils/forkedAgent.ts:26 are absent from this snapshot; call sites only.)
A. Live Session Scratchpad (services/SessionMemory/)
initSessionMemory() (services/SessionMemory/sessionMemory.ts:357-375) runs inside !isBareMode() (setup.ts:293, calling initSessionMemory() at setup.ts:294), exits on remote mode (services/SessionMemory/sessionMemory.ts:358), logs tengu_session_memory_init under USER_TYPE === 'ant' (services/SessionMemory/sessionMemory.ts:363-367), exits if !isAutoCompactEnabled() (services/SessionMemory/sessionMemory.ts:369-371), and calls registerPostSamplingHook() (utils/hooks/postSamplingHooks.ts:31-33, fired at query.ts:1000-1009).
- FIFO queue & thresholds:
sequential()(utils/sequential.ts:19-56) serializes ticks. AfterquerySource === 'repl_main_thread'and lazy GrowthBooktengu_session_memory/tengu_sm_config(> 0overrides),shouldExtractMemory()(services/SessionMemory/sessionMemory.ts:134-181) triggers at>= 10000init tokens, then+5000tokens with>= 3tool calls or a tool-free break. - Atomic init & Edit-only sandbox:
setupSessionMemoryFile()(services/SessionMemory/sessionMemory.ts:183-233) createssummary.mdviaflag: 'wx'(O_CREAT|O_EXCL,0o600), seeds the 10-section template (services/SessionMemory/prompts.ts:11-41), and deletesmemoryPathfromreadFileStatebeforeFileReadTool.callto prevent afile_unchangedstub.createMemoryFileCanUseTool(memoryPath)(services/SessionMemory/sessionMemory.ts:460-482) permits onlyFILE_EDIT_TOOL_NAMEon exactmemoryPath. - Safe cursor:
updateLastSummarizedMessageIdIfSafe()(services/SessionMemory/sessionMemory.ts:488-495) advanceslastSummarizedMessageIdonly when!hasToolCallsInLastAssistantTurn(messages)so Chapter 09'strySessionMemoryCompaction()(services/compact/sessionMemoryCompact.ts:514-630, callingcalculateMessagesToKeepIndex()atservices/compact/sessionMemoryCompact.ts:571-574) never splits a tool pair. - Leaf utils:
services/SessionMemory/sessionMemoryUtils.tsisolateswaitForSessionMemoryExtraction()(15swait /60sstale) fromrunAgent, whileservices/SessionMemory/prompts.tsexportsisSessionMemoryEmpty()andtruncateSessionMemoryForCompact()(services/SessionMemory/prompts.ts:256-324).
B. Turn-End Topic Extraction (services/extractMemories/)
Because Bun's feature() tree-shakes direct if checks only (docblock at memdir/paths.ts:65-67), feature('EXTRACT_MEMORIES') sits at three caller boundaries—never inside services/extractMemories/extractMemories.ts: utils/backgroundHousekeeping.ts:34-36, query/stopHooks.ts:141-153, and cli/print.ts:967-969.
At turn end, query/stopHooks.ts:136 checks !isBareMode(), and query/stopHooks.ts:141-153 checks !agentId plus isExtractModeActive() (memdir/paths.ts:69-77, GrowthBook tengu_passport_quail + non-interactive GrowthBook tengu_slate_thimble); headless exit calls drainPendingExtraction() (services/extractMemories/extractMemories.ts:611-615) via its 60s .unref() drainer closure (services/extractMemories/extractMemories.ts:579-586).
runExtraction() (services/extractMemories/extractMemories.ts:329-523) skips via hasMemoryWritesSince() (services/extractMemories/extractMemories.ts:121-148) after main-agent writes, throttles via GrowthBook tengu_bramble_lintel (services/extractMemories/extractMemories.ts:377-386), and coalesces overlapping turns into one trailing pendingContext pass (services/extractMemories/extractMemories.ts:503-522, services/extractMemories/extractMemories.ts:557-564).
The fork runs with skipTranscript: true, maxTurns: 5, 2-turn batching (services/extractMemories/prompts.ts:29-154), compaction-safe cursor fallback (services/extractMemories/extractMemories.ts:103-108), and createAutoMemCanUseTool() (services/extractMemories/extractMemories.ts:171-222):
// services/extractMemories/extractMemories.ts:171-222
export function createAutoMemCanUseTool(memoryDir: string): CanUseToolFn {
return async (tool: Tool, input: Record<string, unknown>) => {
// Allow REPL — when REPL mode is enabled (ant-default), primitive tools
// are hidden from the tool list so the forked agent calls REPL instead.
// REPL's VM context re-invokes this canUseTool for each inner primitive
// (toolWrappers.ts createToolWrapper), so the Read/Bash/Edit/Write checks
// below still gate the actual file and shell operations. Giving the fork a
// different tool list would break prompt cache sharing (tools are part of
// the cache key — see CacheSafeParams in forkedAgent.ts).
if (tool.name === REPL_TOOL_NAME) {
return { behavior: 'allow' as const, updatedInput: input }
}
// ...
if (tool.name === BASH_TOOL_NAME) {
const parsed = tool.inputSchema.safeParse(input)
if (parsed.success && tool.isReadOnly(parsed.data)) {
return { behavior: 'allow' as const, updatedInput: input }
}
// ...
}
if (
(tool.name === FILE_EDIT_TOOL_NAME ||
tool.name === FILE_WRITE_TOOL_NAME) &&
'file_path' in input
) {
const filePath = input.file_path
if (typeof filePath === 'string' && isAutoMemPath(filePath)) {
return { behavior: 'allow' as const, updatedInput: input }
}
}
// ...
}
}
Hidden Invariants & Production Scars
The
REPL_TOOL_NAMECache-Key Scar (services/extractMemories/extractMemories.ts:173-182): Becausetools[]hashes first in the prompt cache (Chapter 04), forks cannot strip tools without busting cache. Claude Code sends the full tool pool (allowingREPL_TOOL_NAMEinant-default REPL mode) and enforces least privilege incanUseToolas REPL wraps innerRead, read-onlyBash,Edit, andWrite.
- Missing
try/finallyscar inextractSessionMemory: Manual/summary(services/SessionMemory/sessionMemory.ts:450-452) callsmarkExtractionCompleted()infinally, whereas backgroundextractSessionMemoryruns unguarded (services/SessionMemory/sessionMemory.ts:272-350), callingmarkExtractionCompleted()atservices/SessionMemory/sessionMemory.ts:349—relying onEXTRACTION_STALE_THRESHOLD_MS = 60000(services/SessionMemory/sessionMemoryUtils.ts:13,services/SessionMemory/sessionMemoryUtils.ts:93) on throws.
5. Cross-Session Consolidation: autoDream's Three-Gate Lock (services/autoDream/, tasks/DreamTask/DreamTask.ts)
The Problem
Per-turn notes accumulate contradictions, relative dates, and index bloat. Consolidating every turn wastes tokens; unlocked consolidation lets concurrent terminals corrupt rewrites.
The Mechanism: Cheapest-First Funnel, PID+mtime Lock, and Symmetric Rollback
Initialized at utils/backgroundHousekeeping.ts:37 and fired at query/stopHooks.ts:154-156 (inside !isBareMode(), query/stopHooks.ts:136, excluding subagents), isGateOpen() (services/autoDream/autoDream.ts:95-100) checks !getKairosActive(), !getIsRemoteMode(), isAutoMemoryEnabled(), and isAutoDreamEnabled().
isAutoDreamEnabled() lives in 21-line services/autoDream/config.ts (services/autoDream/config.ts:13-21), where settings.autoDreamEnabled overrides GrowthBook tengu_onyx_plover.enabled. (isForced() at services/autoDream/autoDream.ts:105-107 returns false.)
runAutoDream() (services/autoDream/autoDream.ts:125-272) bounds per-turn cost to at most two cached GrowthBook reads when settings.autoDreamEnabled is unset (getConfig() at services/autoDream/autoDream.ts:126 and isAutoDreamEnabled() via isGateOpen() at services/autoDream/autoDream.ts:128) plus one stat via a cheapest-first three-gate funnel (services/autoDream/autoDream.ts:126-190, DEFAULTS = { minHours: 24, minSessions: 5 } at services/autoDream/autoDream.ts:63-66):
- Time gate (1
stat):readLastConsolidatedAt()(services/autoDream/consolidationLock.ts:29-36) reads.consolidate-lock'smtime(0if missing); returns unlesshoursSince >= cfg.minHours. - Scan throttle + Session gate: Enforces a 10-minute
SESSION_SCAN_INTERVAL_MScooldown (services/autoDream/autoDream.ts:56,services/autoDream/autoDream.ts:144-151) beforelistSessionsTouchedSince(lastAt)(services/autoDream/consolidationLock.ts:118-124, usingmtimesinceext4birthtimeis0and excludingagent-*.jsonl);runAutoDream()then excludesgetSessionId()(services/autoDream/autoDream.ts:164-165) and requires>= cfg.minSessions. - Cross-process lock:
tryAcquireConsolidationLock()(services/autoDream/consolidationLock.ts:46-84) writes the holder PID into.consolidate-lock, whosemtimedoubles aslastConsolidatedAt:
// services/autoDream/consolidationLock.ts:60-84
if (mtimeMs !== undefined && Date.now() - mtimeMs < HOLDER_STALE_MS) {
if (holderPid !== undefined && isProcessRunning(holderPid)) {
logForDebugging(
`[autoDream] lock held by live PID ${holderPid} (mtime ${Math.round((Date.now() - mtimeMs) / 1000)}s ago)`,
)
return null
}
// Dead PID or unparseable body — reclaim.
}
// Memory dir may not exist yet.
await mkdir(getAutoMemPath(), { recursive: true })
await writeFile(path, String(process.pid))
// Two reclaimers both write → last wins the PID. Loser bails on re-read.
let verify: string
try {
verify = await readFile(path, 'utf8')
} catch {
return null
}
if (parseInt(verify.trim(), 10) !== process.pid) return null
return mtimeMs ?? 0
}
Once locked, runAutoDream() calls registerDreamTask() in tasks/DreamTask/DreamTask.ts (Ch 17 seam, setAppStateForTasks ?? setAppState, services/autoDream/autoDream.ts:200-208). It builds the 4-phase prompt (services/autoDream/consolidationPrompt.ts:10-65: Orient → Gather signal → Consolidate → Prune and index) and calls runForkedAgent() (services/autoDream/autoDream.ts:224-233) with createAutoMemCanUseTool(), skipTranscript: true, and makeDreamProgressWatcher() (services/autoDream/autoDream.ts:281-313).
Hidden Invariants & Production Scars
- 1-hour PID-reuse guard & rollback rewind (
services/autoDream/consolidationLock.ts:19,services/autoDream/consolidationLock.ts:91-108):HOLDER_STALE_MS = 60 * 60 * 1000stops recycled PIDs from deadlocking consolidation. On failure,rollbackConsolidationLock(priorMtime)unlinks ifpriorMtime === 0or runswriteFile(path, '')beforeutimes()so our live PID clears the lock. - No double-rollback on user kill (
services/autoDream/autoDream.ts:258-271,tasks/DreamTask/DreamTask.ts:136-156):DreamTask.killabortsabortControllerand callsrollbackConsolidationLock(priorMtime);if (abortController.signal.aborted) returnprevents double-rollback or overwriting'killed'. - 21-line leaf config (
services/autoDream/config.ts:1-21): Lets UI components queryisAutoDreamEnabled()without importingrunForkedAgentorDreamTask.
6. Team Memory Sync, Secret Scanning, and File Watcher (services/teamMemorySync/)
The Problem
Syncing <autoMemPath>/team/ across collaborators adds three hazards: concurrent edits must not lose local work or hit HTTP 413s, leaked credentials reach the whole org, and naive watchers exhaust fds or spin on permanent auth errors.
The Mechanism: Asymmetric Delta Sync, Dual-Layer Secret Scanning, and Pull-Before-Watch
services/teamMemorySync/index.ts requires first-party OAuth (isUsingOAuth(), services/teamMemorySync/index.ts:151-161), builds URLs in getTeamMemorySyncEndpoint() (services/teamMemorySync/index.ts:163-167: TEAM_MEMORY_SYNC_URL or getOauthConfig().BASE_API_URL), and uses lazy Zod schemas in services/teamMemorySync/types.ts.
SyncState (services/teamMemorySync/index.ts:100-119) tracks { lastKnownChecksum, serverChecksums, serverMaxEntries } (stale "watcher suppression" docblock at services/teamMemorySync/index.ts:22). Sync splits conflict policy between pull and push:
- Server-wins parallel pull (
pullTeamMemory(),services/teamMemorySync/index.ts:770-867): SendsIf-None-Matchwith exponential backoff infetchTeamMemory()(services/teamMemorySync/index.ts:387-410,MAX_RETRIES = 3).writeRemoteEntriesToLocal()(services/teamMemorySync/index.ts:689-755) validates keys (validateTeamMemKey()), caps at250_000bytes, skips unchanged files, and writes in parallel (fixing ~22s p99 pulls).pullTeamMemory()callsclearMemoryFileCaches()(services/teamMemorySync/index.ts:851-855) whenfilesWritten > 0. - Local-wins delta push (
pushTeamMemory()atservices/teamMemorySync/index.ts:889-1146): Read-only on disk. Packssha256:deltas into sorted200_000-bytePUTbatches viabatchDeltaByBytes()(services/teamMemorySync/index.ts:426-460, avoiding gateway HTML 413s), advancingserverChecksumsandlastKnownChecksumafter each200. - 412 hash probes & dynamic 413 caps: On
412,fetchTeamMemoryHashes()(GET ...&view=hashes,services/teamMemorySync/index.ts:315-385) refreshes hashes without bodies (MAX_CONFLICT_RETRIES = 2). On structured413(TeamMemoryTooManyEntriesSchema,services/teamMemorySync/index.ts:533-542),pushTeamMemory()cachesstate.serverMaxEntries(services/teamMemorySync/index.ts:1053-1059) soreadLocalTeamMemory()truncates sorted keys deterministically (services/teamMemorySync/index.ts:654-671).
Credential defense (PSR M22174) uses 36 gitleaks regexes in services/teamMemorySync/secretScanner.ts (ANT_KEY_PFX = ['sk', 'ant', 'api'].join('-'), services/teamMemorySync/secretScanner.ts:46; SECRET_RULES, services/teamMemorySync/secretScanner.ts:48-224) at two gates:
- Upstream fail-closed write guard:
checkTeamMemSecrets()(services/teamMemorySync/teamMemSecretGuard.ts,services/teamMemorySync/teamMemSecretGuard.ts:15-44,feature('TEAMMEM')) runs invalidateInputoncontent(tools/FileWriteTool/FileWriteTool.ts:157-160) andnew_stringonly (tools/FileEditTool/FileEditTool.ts:144-147)—not insidecreateAutoMemCanUseTool()—blocking writes pre-disk. - Push-time safety net:
readLocalTeamMemory()(services/teamMemorySync/index.ts:567-673) runs full-filescanForSecrets()(services/teamMemorySync/index.ts:599-615), catching Bash, external, and split-edit credentials and returningskippedSecretswhile pushing clean files.pushTeamMemory()logs rule IDs only viatengu_team_mem_secret_skipped(services/teamMemorySync/index.ts:935-944).
services/teamMemorySync/watcher.ts (started inside !isBareMode(), setup.ts:336, behind feature('TEAMMEM'), setup.ts:365-369) avoids pull-echo structurally: startTeamMemoryWatcher() (services/teamMemorySync/watcher.ts:252-305) finishes pullTeamMemory() before startFileWatcher(), watching 404 repos via fs.watch({ recursive: true }) (O(1) FSEvents vs. 500+ kqueue fds in chokidar 4+).
Writes coalesce behind a 2-second DEBOUNCE_MS timer (services/teamMemorySync/watcher.ts:35), backed by PostToolUse notifyTeamMemoryWrite() (utils/sessionFileAccessHooks.ts:201, utils/sessionFileAccessHooks.ts:205) and final flush in stopTeamMemoryWatcher() (services/teamMemorySync/watcher.ts:327-352).
Hidden Invariants & Production Scars
- 167K-event
no_oauthstorm & unlink-cleared circuit breaker: Exponential backoff lives infetchTeamMemory(), notwatcher.ts. Instead,watcher.tslatchespushSuppressedReasonon permanent failure, clearing it only on unlink (stat()→ENOENT,services/teamMemorySync/watcher.ts:187-205) or restart:
// services/teamMemorySync/watcher.ts:45-73
// Set after a push fails for a reason that can't self-heal on retry.
// Prevents watch events from other sessions' writes to the shared team
// dir driving an infinite retry loop (BQ Mar 14-16: one no_oauth device
// emitted 167K push events over 2.5 days). Cleared on unlink — file deletion
// is a recovery action for the too-many-entries case, and for no_oauth the
// suppression persisting until session restart is correct.
let pushSuppressedReason: string | null = null
/**
* Permanent = retry without user action will fail the same way.
* - no_oauth / no_repo: pre-request client checks, no status code
* - 4xx except 409/429: client error (404 missing repo, 413 too many
* entries, 403 permission). 409 is a transient conflict — server state
* changed under us, a fresh push after next pull can succeed. 429 is a
* rate limit — watcher-driven backoff is fine.
*/
export function isPermanentFailure(r: TeamMemorySyncPushResult): boolean {
if (r.errorType === 'no_oauth' || r.errorType === 'no_repo') return true
if (
r.httpStatus !== undefined &&
r.httpStatus >= 400 &&
r.httpStatus < 500 &&
r.httpStatus !== 409 &&
r.httpStatus !== 429
) {
return true
}
return false
}
Putting Layer 3 Together
Memory Subsystem Horizon, Trigger & Write Boundary Matrix
+-----------------+---------------+----------------------+-------------------+
| Subsystem | Trigger Seam | Gate / Throttle | Write Sandbox |
+-----------------+---------------+----------------------+-------------------+
| CLAUDE.md/rules | Turn 1 + Read | Always on; Set dedup | RO; isPartialView |
| Memory Prefetch | query() using | tengu_moth_copse | RO sideQuery <=5 |
| SessionMemory | Post-sampling | tengu_session_memory | Edit summary.md |
| extractMemories | Stop hook | EXTRACT_MEMORIES | RO/Bash + AutoMem |
| autoDream | Stop hook | 24h/5ses + PID lock | RO/Bash + AutoMem |
| teamMemorySync | Setup + watch | TEAMMEM + OAuth; 2s | team/ + 36 rules |
+-----------------+---------------+----------------------+-------------------+
flowchart TD
CMD["utils/claudemd.ts: hierarchical discovery & @include"]
ATT["utils/attachments.ts: loadedNestedMemoryPaths & prefetch"]
Q["query.ts: using pendingMemoryPrefetch & post-sampling"]
STOP["query/stopHooks.ts: handleStopHooks()"]
SM["services/SessionMemory/: summary.md scratchpad"]
EXT["services/extractMemories/: turn-end topic extraction"]
DRM["services/autoDream/: 3-gate lock & consolidation"]
FORK["utils/forkedAgent.ts: runForkedAgent() + CacheSafeParams"]
SUM["session-memory/summary.md (Ch 09)"]
GUARD["FileWriteTool & FileEditTool: checkTeamMemSecrets()"]
MEM["memdir/: private memory/ & memory/team/"]
SYNC["services/teamMemorySync/: watcher & OAuth delta sync"]
PERM["Ch 11: utils/permissions/ carve-outs & classifier"]
CMD -->|"getUserContext() (Ch 04)"| Q
CMD -->|"nested rules"| ATT
MEM -->|"scanMemoryFiles()"| ATT
ATT -->|"non-blocking poll"| Q
Q -->|"executePostSamplingHooks()"| SM
Q -->|"end of turn"| STOP
STOP -->|"EXTRACT_MEMORIES"| EXT
STOP -->|"tengu_onyx_plover"| DRM
SM -->|"createMemoryFileCanUseTool()"| FORK
EXT -->|"createAutoMemCanUseTool()"| FORK
DRM -->|"createAutoMemCanUseTool()"| FORK
FORK -->|"SessionMemory: Edit only"| SUM
FORK -->|"extractMemories / autoDream"| GUARD
GUARD --> MEM
MEM <--> SYNC
CMD -.->|"cachedClaudeMdContent"| PERM
MEM -.->|"isAutoMemPath()"| PERM
How This Snaps Into the Next Layer
Every memory write boundary and policy signal plugs into Layer 4 in Chapter 11:
- Forked-agent callbacks:
createMemoryFileCanUseTool()(services/SessionMemory/sessionMemory.ts:460-482) andcreateAutoMemCanUseTool()(services/extractMemories/extractMemories.ts:171-222) implementCanUseToolFnwithout mutating cached tool schemas. - Filesystem helpers & carve-outs:
utils/permissions/filesystem.tsownsgetSessionMemoryDir()/isSessionMemoryPath()(utils/permissions/filesystem.ts:261-278), the!hasAutoMemPathOverride() && isAutoMemPath(normalizedPath)write carve-out (utils/permissions/filesystem.ts:1572-1581) withholding silent writes fromCLAUDE_COWORK_MEMORY_PATH_OVERRIDE, and read carve-outs (utils/permissions/filesystem.ts:1620-1629,utils/permissions/filesystem.ts:1716-1725). - Auto-mode classifier seam:
cachedClaudeMdContent(bootstrap/state.ts:121-123,bootstrap/state.ts:1207-1213) breaks theclaudemd → filesystem → permissions → yoloClassifiercycle sobuildClaudeMdMessage()(utils/permissions/yoloClassifier.ts:460-477) feedsCLAUDE.mdintoclassifyYoloAction()(utils/permissions/permissions.ts:102-105,utils/permissions/permissions.ts:693-699). - Layer 6 forward seams:
tasks/DreamTask/DreamTask.tsplugs into Chapter 17's background task runtime, andutils/forkedAgent.tsopens Chapter 18's subagent runtime.
Reading Exercises & Code Checkpoints
- Trace
loadedNestedMemoryPathsacross attachments, compaction, and forks: Runrg -n "loadedNestedMemoryPaths" utils/attachments.ts services/compact/compact.ts utils/forkedAgent.tsand verify why a non-evictingSetpairs with LRUreadFileStateand clears on compaction. - Trace
tengu_moth_copseacross all four memory subsystems: Runrg -n "startRelevantMemoryPrefetch|tengu_moth_copse" query.ts utils/attachments.ts utils/claudemd.ts memdir/memdir.ts services/extractMemories/extractMemories.tsto confirm how one flag swaps staticMEMORY.mdforusingprefetch andskipIndex = true. - Audit background sandboxes and upstream secret scanning:
Run
rg -n "createMemoryFileCanUseTool|createAutoMemCanUseTool|checkTeamMemSecrets|isAutoMemPath" services/SessionMemory/sessionMemory.ts services/extractMemories/extractMemories.ts services/autoDream/autoDream.ts services/teamMemorySync/teamMemSecretGuard.ts tools/FileWriteTool/FileWriteTool.ts tools/FileEditTool/FileEditTool.ts utils/permissions/filesystem.tsand confirm whycheckTeamMemSecretsruns invalidateInput. - Verify
autoDream's 3-gate lock, symmetric rollback, and leaf config: Runrg -n "isAutoDreamEnabled|tryAcquireConsolidationLock|rollbackConsolidationLock|registerDreamTask|tengu_onyx_plover" services/autoDream/ tasks/DreamTask/DreamTask.tsand trace both rollbacks (services/autoDream/autoDream.ts:270,tasks/DreamTask/DreamTask.ts:154) plusabortController.signal.aborted.
Manuscript chapters/10-memory-systems-claudemd-auto-memory-and-dream.md from AtlantisPleb/cc at 5aa286a, synced Nov 24, 2026.