8. The Session Envelope: QueryEngine.ts and Transcript Persistence
Status: written. Reverse-engineered from the Claude Code 2.1.88 source snapshot (see the Prologue (
prologue-how-to-read-a-1900-file-agent.md) for snapshot caveats).Layer: Layer 2 (The Agent Engine) Prerequisites: Chapter 01 (
bootstrap/state.ts,FileStateCache), Chapter 02 (ToolUseContext), Chapter 03 (processUserInput,normalizeMessages), Chapter 04 (asSystemPrompt), Chapter 05 (cost-tracker.ts), Chapter 06 (runTools,ContentReplacementState), Chapter 07 (query()) Primary Source Files:•
QueryEngine.ts— conversation-lifetime envelope wrappingquery(), SDK framing, persistence timing, and guardrails (1,295 lines)•
utils/queryHelpers.ts— SDK normalization,isResultSuccessful, orphaned-permission replay, and resume cache extraction (552 lines)•
utils/sessionStorage.ts— lazy-materializedProjectJSONL writer,parentUuidDAG, read-side repair, and scale guards (5,105 lines)•
utils/sessionRestore.ts— resume hydration, worktree restoration, and--fork-sessionorchestration (551 lines)•
utils/conversationRecovery.ts— resume loader, turn-interruption repair, and skill-state replay (597 lines)•
utils/fileHistory.ts— three-phase path-hashed file checkpointing and/rewinddisk rollback (1,115 lines)
Why This Layer Exists
Chapter 07's query() runs one prompt to a Terminal exit. Four production problems remain above it:
- Cross-turn state & SDK framing: Retaining
mutableMessages,readFileState,permissionDenials,totalUsage, guardrails, andSDKMessageframing. - Append-only durability: Persisting JSONL without empty-file leaks, kill-before-flush loss, or 150 MB OOMs.
- Crash recovery & forking: Repairing truncated lines and interrupted turns while isolating
--fork-sessionfrom parent worktrees/caches. - Coherent
/rewind: Rolling back conversation history and disk edits together.
These six files (plus utils/json.ts and utils/sessionStoragePortable.ts) close Layer 2.
1. The Session Envelope: QueryEngine vs. query() (QueryEngine.ts)
The Problem
query() holds only a turn-local State.messages slice and knows nothing about SDK wire types, JSONL flushing, budget caps, or post-compaction GC. Worse, Desktop and Cowork kill the CLI subprocess milliseconds after reading result—or seconds after a user hits Stop before the API replies.
The Real Implementation (QueryEngine.ts)
QueryEngine (QueryEngine.ts:184-1177) owns nine instance fields (QueryEngine.ts:184-207):
// QueryEngine.ts:184-207export class QueryEngine { private config: QueryEngineConfig private mutableMessages: Message[] private abortController: AbortController private permissionDenials: SDKPermissionDenial[] private totalUsage: NonNullableUsage private hasHandledOrphanedPermission = false private readFileState: FileStateCache // Turn-scoped skill discovery tracking (feeds was_discovered on // tengu_skill_tool_invocation). Must persist across the two // processUserInputContext rebuilds inside submitMessage, but is cleared // at the start of each submitMessage to avoid unbounded growth across // many turns in SDK mode. private discoveredSkillNames = new Set<string>() private loadedNestedMemoryPaths = new Set<string>() constructor(config: QueryEngineConfig) { this.config = config this.mutableMessages = config.initialMessages ?? [] this.abortController = config.abortController ?? createAbortController() this.permissionDenials = [] this.readFileState = config.readFileCache this.totalUsage = EMPTY_USAGE }Two lifetime carve-outs stand out immediately:
discoveredSkillNamesis turn-scoped (QueryEngine.ts:197,QueryEngine.ts:238): Cleared at eachsubmitMessage()start to cap SDK memory (unlikescreens/REPL.tsx:1958-1963).loadedNestedMemoryPaths(QueryEngine.ts:198) stays session-wide becausereadFileStateis a bounded LRU that evicts nestedCLAUDE.mdpaths.abortControllernever resets (QueryEngine.ts:187,QueryEngine.ts:203,QueryEngine.ts:1158-1160): Onceinterrupt()aborts an engine instance, it stays aborted across turns.
submitMessage() (QueryEngine.ts:209-1156) drives each turn in nine phases:
- Turn setup & denial capture (
QueryEngine.ts:213-271): ClearsdiscoveredSkillNames, setscwd, snapshotspersistSession = !isSessionPersistenceDisabled()(QueryEngine.ts:240), and wrapscanUseToolto record denials undersdkCompatToolName(tool.name)(Agent→Taskfor#19647,utils/messages/systemInit.ts:19-25). - Prompt, coordinator & schema hooks (
QueryEngine.ts:273-333): Resolves model and thinking, callsfetchSystemPromptParts()(utils/queryContext.ts:44-74), and merges coordinator context underfeature('COORDINATOR_MODE')(QueryEngine.ts:112-117,QueryEngine.ts:302-308, passinggetScratchpadDir()behind GrowthBook'tengu_scratch'viaisScratchpadEnabled()).
When customPrompt !== undefined && hasAutoMemPathOverride() (CLAUDE_COWORK_MEMORY_PATH_OVERRIDE at memdir/paths.ts:161-166), it appends Cowork's memory prompt (QueryEngine.ts:316-319), brands asSystemPrompt([...]), and registers Stop schema enforcement when jsonSchema and StructuredOutput are present (QueryEngine.ts:328-333).
- Context #1 & orphaned permission (
QueryEngine.ts:335-408): BuildsprocessUserInputContextwith livesetMessages(QueryEngine.ts:344-346) and same-refupdateFileHistoryState(QueryEngine.ts:376-384), then runshandleOrphanedPermission()once viahasHandledOrphanedPermission. - User input & pre-query durability (
QueryEngine.ts:410-463): CallsprocessUserInput({ ..., querySource: 'sdk' }), pushes returned messages intothis.mutableMessages, snapshots localmessages, and persists before callingquery():
// QueryEngine.ts:450-463if (persistSession && messagesFromUserInput.length > 0) { const transcriptPromise = recordTranscript(messages) if (isBareMode()) { void transcriptPromise } else { await transcriptPromise if ( isEnvTruthy(process.env.CLAUDE_CODE_EAGER_FLUSH) || isEnvTruthy(process.env.CLAUDE_CODE_IS_COWORK) ) { await flushSessionStorage() } }} Pre-query persistence plus eager flushing keeps Cowork --resume from failing with "No conversation found" on early Stop. Under --bare / CLAUDE_CODE_SIMPLE (isBareMode(), utils/envUtils.ts:60-65), scripted calls fire-and-forget, saving ~4–30 ms.
- Context #2 &
system/init(QueryEngine.ts:465-554): SelectsmessagesToAckvia lazymessageSelector().selectableUserMessagesFilter(QueryEngine.ts:87-89,QueryEngine.ts:471), rebuildsprocessUserInputContextwith no-opsetMessages, loads cache-only skills/plugins, and yieldssystem/init(buildSystemInitMessage(),utils/messages/systemInit.ts:53-96;SDKSystemMessageSchema,entrypoints/sdk/coreSchemas.ts:1457-1494). !shouldQueryfast path (QueryEngine.ts:556-639): Local slash commands skipquery().system/local_commandoutput becomes a syntheticSDKAssistantMessage(utils/messages/mappers.ts:196-215) because Android andapi-goonly parseinitandcompact_boundarysystem subtypes.- Pre-loop snapshots & watermark (
QueryEngine.ts:641-673): Firesvoid fileHistoryMakeSnapshot(...)whenfileHistoryEnabled() && persistSession, and captures reference-basederrorLogWatermark = getInMemoryErrors().at(-1)(QueryEngine.ts:669). query()consumer loop (QueryEngine.ts:675-1049): Iteratesfor await (const message of query({...}))and discardsquery()'sTerminalreturn value. Before recordingcompact_boundary, it flushesthis.mutableMessages.slice(0, tailIdx + 1)up topreservedSegment.tailUuid(QueryEngine.ts:701-715). Core writes then split sync vs. async:
// QueryEngine.ts:717-732if (persistSession) { // Fire-and-forget for assistant messages. claude.ts yields one // assistant message per content block, then mutates the last // one's message.usage/stop_reason on message_delta — relying on // the write queue's 100ms lazy jsonStringify. Awaiting here // blocks ask()'s generator, so message_delta can't run until // every block is consumed; the drain timer (started at block 1) // elapses first. Interactive CC doesn't hit this because // useLogMessages.ts fire-and-forgets. enqueueWrite is // order-preserving so fire-and-forget here is safe. if (message.type === 'assistant') { void recordTranscript(messages) } else { await recordTranscript(messages) }} In switch (message.type) (QueryEngine.ts:757-969), tombstone is skipped (QueryEngine.ts:758-760), stream_event accumulates totalUsage on message_stop (QueryEngine.ts:810-816), and progress/attachment call inline void recordTranscript (QueryEngine.ts:773-781, QueryEngine.ts:831-835) so unrecorded rows never stall prefix dedup.
On snipReplay (QueryEngine.ts:905-915) and compact_boundary (QueryEngine.ts:918-942), QueryEngine splices pre-compaction messages out of this.mutableMessages in place for GC.
- Guardrails &
resultframing (QueryEngine.ts:842-874,QueryEngine.ts:972-1156): EnforcesmaxTurns(error_max_turns),maxBudgetUsd(getTotalCost()→error_max_budget_usd), andprocess.env.MAX_STRUCTURED_OUTPUT_RETRIES || '5'(error_max_structured_output_retries).
All six exit/pre-query sites eager-flush under CLAUDE_CODE_EAGER_FLUSH || CLAUDE_CODE_IS_COWORK (QueryEngine.ts:457-461, QueryEngine.ts:611-615, QueryEngine.ts:845-849, QueryEngine.ts:975-979, QueryEngine.ts:1018-1022, QueryEngine.ts:1075-1079). On failure, error_during_execution slices getInMemoryErrors() from all.lastIndexOf(errorLogWatermark) + 1 (QueryEngine.ts:1106-1115), staying turn-scoped even when .shift() rotates the 100-entry ring buffer (utils/log.ts:66-77).
Hidden Invariants & Architectural Reality Today
ask()andHISTORY_SNIP(QueryEngine.ts:1186-1295):export async function* ask(...)creates a freshQueryEngineper call, sharesmutableMessages, clonesreadFileCacheand writes it back infinally(QueryEngine.ts:1292-1294), and injectssnipReplaybehindfeature('HISTORY_SNIP')(QueryEngine.ts:122-127,QueryEngine.ts:1276-1284) using snapshot-absentservices/compact/snipCompact.jsandservices/compact/snipProjection.js.- Unfinished caller migration: Despite
QueryEngine.ts:176-182's docstring,cli/print.tsis the sole production caller (cli/print.ts:91,cli/print.ts:2147), hoistingmutableMessages,readFileState+pendingSeeds(cli/print.ts:1145-1167,cli/print.ts:2168-2181), and a freshabortControllerper turn (cli/print.ts:2133) aroundask(). Interactivescreens/REPL.tsxkeeps its own refs (screens/REPL.tsx:1958-1977) and callsquery()directly (screens/REPL.tsx:146,screens/REPL.tsx:2793-2803).
2. SDK Framing, Orphaned Permissions & Resume Cache Extraction (utils/queryHelpers.ts)
The Problem
Translating internal messages for SDK clients and recovering restarted headless processes introduces four edge cases: subagent attribution, silent end_turn completions, out-of-band permission responses after reconnects, and an empty FileStateCache on restart.
The Real Implementation (utils/queryHelpers.ts)
normalizeMessage()(utils/queryHelpers.ts:102-222): Converts internalassistant,progress, andusermessages intoSDKMessages vianormalizeMessages()andisNotEmptyMessage(). It unwrapsagent_progress/skill_progresswithparent_tool_use_id: message.parentToolUseID(utils/queryHelpers.ts:121-156) and mergesmcpMetaintotool_use_result.
For bash_progress/powershell_progress (utils/queryHelpers.ts:157-201), it emits tool_progress only when CLAUDE_CODE_REMOTE or CLAUDE_CODE_CONTAINER_ID is set, throttled by TOOL_PROGRESS_THROTTLE_MS = 30000 (30 s) in a 100-entry toolProgressLastSentTime Map (utils/queryHelpers.ts:98-100). (Stale comments: utils/queryHelpers.ts:161 says "one per minute" and utils/queryHelpers.ts:178 says "LRU eviction", but Map.set() on an existing key preserves insertion order—making eviction FIFO at 30 s.)
isResultSuccessful()(utils/queryHelpers.ts:56-94): Paired withQueryEngine.ts:1051-1060's#23537fix (messages.findLast(m => m.type === 'assistant' || m.type === 'user')so trailing stop-hook progress cannot blank-p), this check accepts assistanttext/thinking/redacted_thinkingblocks, all-tool_resultuser messages, or the zero-blockend_turncarve-out (stopReason === 'end_turn',utils/queryHelpers.ts:83-93).
On task_notification drain turns or post-StructuredOutput turns (services/api/claude.ts:2346-2350; comment at utils/queryHelpers.ts:85 cites stale line 2026 of services/api/claude.ts), zero assistant blocks are legitimate; without this carve-out, QueryEngine emits error_during_execution.
handleOrphanedPermission()(utils/queryHelpers.ts:224-343) &PermissionPromptTool(utils/queryHelpers.ts:39-42): When a remote client sends a late permission response (cli/print.ts:5241-5304),handleOrphanedPermission()appliesupdatedInputon'allow'with a warn fallback (utils/queryHelpers.ts:262-276), guardsmutableMessagesbefore yielding, and replays[finalToolUseBlock]viarunTools()(utils/queryHelpers.ts:322-342):
// utils/queryHelpers.ts:299-319const alreadyPresent = mutableMessages.some( m => m.type === 'assistant' && Array.isArray(m.message.content) && m.message.content.some( b => b.type === 'tool_use' && 'id' in b && b.id === toolUseID, ),)if (!alreadyPresent) { mutableMessages.push(assistantMessage) if (persistSession) { await recordTranscript(mutableMessages) }}const sdkAssistantMessage: SDKMessage = { ...assistantMessage, session_id: getSessionId(), parent_tool_use_id: null,} as SDKMessageyield sdkAssistantMessage Deduplicating by tool_use.id—not assistantMessage.message.id—avoids both traps: streaming splits [text, tool_use] under one message.id and recovery can drop tool_use. Checking message.id orphans tool_result; blind pushes hit HTTP 400 "tool_use ids must be unique" after normalizeMessagesForAPI merges assistants.
Companion PermissionPromptTool (utils/queryHelpers.ts:39-42) wraps utils/permissions/PermissionPromptToolResultSchema.ts:15-79 (.catch(...) → undefined for #29440 at utils/permissions/PermissionPromptToolResultSchema.ts:48-59, vs. decisionClassificationField's .catch(undefined) at utils/permissions/PermissionPromptToolResultSchema.ts:37-42; mobile {} updatedInput keeps original input at utils/permissions/PermissionPromptToolResultSchema.ts:107-111).
- Resume cache extractors (
utils/queryHelpers.ts:346-552): extractReadFilesFromMessages()(utils/queryHelpers.ts:346-501, defaultASK_READ_FILE_STATE_CACHE_SIZE = 10, overridden atcli/print.ts:1154,screens/REPL.tsx:1972) runs two passes for last-winsFileStateCachehydration.
Pass 1 indexes non-ranged Read/Write/Edit calls; Pass 2 replays tool_results, skips FILE_UNCHANGED_STUB (utils/queryHelpers.ts:428), strips <system-reminder> tags/line prefixes, and re-reads Edit targets from live disk via readFileSyncWithMetadata (utils/queryHelpers.ts:477-494).
extractBashToolsFromMessages()(utils/queryHelpers.ts:507-552) stripsVAR=valand'sudo'(STRIPPED_COMMANDS/extractCliName) to rebuild used CLI binaries.
3. JSONL Transcript Persistence, the parentUuid DAG & Scale Guards (utils/sessionStorage.ts, utils/sessionStoragePortable.ts)
The Problem
Transcripts must survive sudden kills without leaking empty .jsonl files on --version or /exit, avoid syscall thrashing during streaming, represent rewinds and compactions in an append-only log, and stay fast when sessions exceed 150 MB (inc-3930).
The Write Path: Lazy Project, Batched Queues & Sidechain Isolation
Singleton class Project (utils/sessionStorage.ts:532-1384, returned by getProject() at utils/sessionStorage.ts:443-467) enforces five invariants:
- Lazy materialization (
utils/sessionStorage.ts:549-552,utils/sessionStorage.ts:960-991):sessionFilestartsnull, buffering early rows inpendingEntries. The firstuser/assistantmessage runsmaterializeSessionFile(), checkingshouldSkipPersistence()(NODE_ENV === 'test' && !TEST_ENABLE_SESSION_PERSISTENCE,cleanupPeriodDays === 0,isSessionPersistenceDisabled(),CLAUDE_CODE_SKIP_PROMPT_HISTORY), resolvinggetOriginalCwd()lazily (utils/sessionStorage.ts:108-112), and drainingpendingEntries. - Dual async/sync queues (
utils/sessionStorage.ts:530,utils/sessionStorage.ts:567-568,utils/sessionStorage.ts:606-686,utils/sessionStorage.ts:2572-2729): Transcript rows batch viaFLUSH_INTERVAL_MS(100ms;REMOTE_FLUSH_INTERVAL_MS = 10ms remotely) inMAX_CHUNK_BYTES(100MB) chunks (0o600files,0o700dirs). Metadata (saveCustomTitle,saveTag) writes synchronously viaappendEntryToFile().
On exit, registerCleanup (utils/sessionStorage.ts:449-462) flushes and calls reAppendSessionMetadata() (utils/sessionStorage.ts:721-839), tail-reading 64 KiB (readFileTailSync) with col-0 startsWith so external SDK renames win inside the tail window.
- In-place 64 KiB tombstoning & 50 MB caps (
inc-3930):removeMessageByUuid()(utils/sessionStorage.ts:871-951) searches the last 64 KiB for"uuid":"<id>"(avoidingparentUuidhits) and splices viafh.truncate(). Its slow-path full rewrite caps atMAX_TOMBSTONE_REWRITE_BYTES(50MB,utils/sessionStorage.ts:123), matchingMAX_TRANSCRIPT_READ_BYTES(utils/sessionStorage.ts:229). - Main-file-authoritative
messageSet(inc-4718,utils/sessionStorage.ts:1224-1262): Agent sidechains write to<sessionId>/subagents/agent-<agentId>.jsonland share inherited parent UUIDs. Deduping against mainmessageSetdropped fork context, while adding sidechain UUIDs or posting to Session Ingress (ENABLE_SESSION_PERSISTENCE,utils/sessionStorage.ts:1327) caused dangling links and 409 shutdowns—soappendEntry()bypasses dedup,messageSet.add, andpersistToRemotefor sidechains. - DAG links & post-spread session re-stamp (
utils/sessionStorage.ts:1031-1068):tool_results overrideparentUuidwithsourceToolAssistantUUID;compact_boundarysetsparentUuid: nulland preserveslogicalParentUuid; onlyisChainParticipant(m.type !== 'progress',utils/sessionStorage.ts:154-156) advancesparentUuid. Session-stamp fields intentionally follow...message:
// utils/sessionStorage.ts:1039-1064 const transcriptMessage: TranscriptMessage = { parentUuid: isCompactBoundary ? null : effectiveParentUuid, logicalParentUuid: isCompactBoundary ? parentUuid : undefined, isSidechain, teamName: teamInfo?.teamName, agentName: teamInfo?.agentName, promptId: message.type === 'user' ? (getPromptId() ?? undefined) : undefined, agentId, ...message, // Session-stamp fields MUST come after the spread. On --fork-session // and --resume, messages arrive as SerializedMessage (carries source // sessionId/cwd/etc. because removeExtraFields only strips parentUuid // and isSidechain). If sessionId isn't re-stamped, FRESH.jsonl ends up // with messages stamped sessionId=A but content-replacement entries // stamped sessionId=FRESH (from insertContentReplacement), and // loadFullLog's sessionId-keyed contentReplacements lookup misses → // replacement records lost → FROZEN misclassification. userType: getUserType(), entrypoint: getEntrypoint(), cwd: getCwd(), sessionId, version: VERSION, gitBranch, slug, }recordTranscript() (utils/sessionStorage.ts:1408-1449) runs cleanMessagesForLogging() (utils/sessionStorage.ts:4450-4461): isLoggableMessage() (utils/sessionStorage.ts:4351-4367) drops all progress unconditionally (utils/sessionStorage.ts:4352), and under getUserType() !== 'ant' (process.env.USER_TYPE, utils/sessionStorage.ts:419-421) drops attachments except hook_additional_context when CLAUDE_CODE_SAVE_HOOK_ADDITIONAL_CONTEXT is truthy (utils/sessionStorage.ts:4357-4365).
That same getUserType() !== 'ant' gate calls transformMessagesForExternalTranscript() (utils/sessionStorage.ts:4396-4448, utils/sessionStorage.ts:4455-4460), stripping REPL_TOOL_NAME pairs and promoting isVirtual; tracking startingParentUuid only over an unbroken prefix (!seenNewMessage) prevents post-compact chain rewiring.
Separately, EPHEMERAL_PROGRESS_TYPES / isEphemeralToolProgress() (utils/sessionStorage.ts:186-196, gating 'sleep_progress' on feature('PROACTIVE') || feature('KAIROS') at utils/sessionStorage.ts:190-192) drives UI replace-in-place in screens/REPL.tsx:2608, not cleanMessagesForLogging().
Read-Side Repair Pipeline & Three Scale Guards
On load, loadTranscriptFile() (utils/sessionStorage.ts:3472-3813) and buildConversationChain() (utils/sessionStorage.ts:2069-2094) run five deterministic repairs, followed by checkResumeConsistency() on resume (utils/conversationRecovery.ts:553):
| Stage | Exact Symbol & Citation | What It Repairs |
|---|---|---|
| 1 | isLegacyProgressEntry() (utils/sessionStorage.ts:169-178), progressBridge (utils/sessionStorage.ts:3616-3645) | Bridges pre-#24099 legacy progress entries (#14373, #23537) to the nearest non-progress ancestor. |
| 2 | applyPreservedSegmentRelinks (utils/sessionStorage.ts:1839-1956) | Rewires compaction-preserved head→anchor, zeroes stale pre-compact usage, validates tail→head (tengu_relink_walk_broken), prunes pre-boundary noise. |
| 3 | applySnipRemovals (utils/sessionStorage.ts:1982-2039) | Structurally inspects snipMetadata?.removedUuids (no ant-only string leak), deletes snipped entries, path-compressed relinks surviving children. |
| 4 | Leaf pruning (utils/sessionStorage.ts:3716-3790) | Walks terminal nodes to nearest user/assistant with cycle guard; under GrowthBook 'tengu_pebble_leaf_prune' (default false, utils/sessionStorage.ts:3731), prunes ancestors already having a user/assistant child. |
| 5 | recoverOrphanedParallelToolResults (utils/sessionStorage.ts:2118-2206) | Re-attaches sibling 1-block AssistantMessages (same message.id) and their tool_results orphaned by single-parent walks (tengu_chain_parallel_tr_recovered). |
| 6 | checkResumeConsistency (utils/sessionStorage.ts:2224-2243) | Checks latest turn_duration checkpoint's messageCount against reconstructed index (tengu_resume_consistency_delta, catching #22453 truncation). |
Three scale guards paired with utils/sessionStoragePortable.ts (793 lines) bound latency and RSS:
> 5 MiBfd-level pre-compact streaming (utils/sessionStorage.ts:3536-3556,utils/sessionStoragePortable.ts:480,utils/sessionStoragePortable.ts:717-793): AboveSKIP_PRECOMPACT_THRESHOLD(5 * 1024 * 1024),readTranscriptForLoad()streams 1 MB chunks, dropping staleattribution-snapshotlines and resetting at non-preservedcompact_boundarylines (cutting a 151 MB log from ~316 MB to ~155 MB RSS,utils/sessionStorage.ts:3525-3529).
scanPreBoundaryMetadata() (utils/sessionStorage.ts:3157-3224) recovers pre-boundary metadata via raw Buffer.includes.
- Byte-level dead-fork pre-filter (
walkChainBeforeParse(),utils/sessionStorage.ts:3306-3466): Above5 MiB(when!opts?.keepAllLeaves && !hasPreservedSegment), it indexes{"parentUuid":lines and"uuid":"<36>","timestamp":"(disambiguating nested hits via brace-depth-1pickDepthOneUuidCandidate()atutils/sessionStorage.ts:3275-3304) to walk the live chain inlatin1beforeJSON.parse:
// utils/sessionStorage.ts:3419-3444 const seen = new Set<number>() const chain = new Set<number>() let chainBytes = 0 let slot: number | undefined = leafSlot while (slot !== undefined) { if (seen.has(slot)) break seen.add(slot) chain.add(msgIdx[slot]!) chainBytes += msgIdx[slot + 1]! - msgIdx[slot]! const parentStart = msgIdx[slot + 2]! if (parentStart < 0) break const parent = buf.toString('latin1', parentStart, parentStart + UUID_LEN) slot = uuidToSlot.get(parent) } // parseJSONL cost scales with bytes, not entry count. A session can have // thousands of dead entries by count but only single-digit-% of bytes if // the dead branches are short turns and the live chain holds the fat // assistant responses (measured: 107 MB session, 69% dead entries, 30% // dead bytes - index+concat overhead exceeded parse savings). Gate on // bytes: only stitch if we would drop at least half the buffer. Metadata // is tiny so len - chainBytes approximates dead bytes closely enough. // Near break-even the concat memcpy (copying chainBytes into a fresh // allocation) dominates, so a conservative 50% gate stays safely on the // winning side. if (len - chainBytes < len >> 1) return buf Setting CLAUDE_CODE_DISABLE_PRECOMPACT_SKIP (utils/sessionStorage.ts:3536, utils/sessionStorage.ts:3575) disables both the > 5 MiB pre-compact skip and walkChainBeforeParse().
- Two-phase progressive listing (
utils/sessionStorage.ts:4739-5105):getSessionFilesLite()(utils/sessionStorage.ts:4975-5016) is strictlystat-only (zero file reads), returningmtime-sorted stubs immediately.
Phase 2 (enrichLogs(), utils/sessionStorage.ts:5077-5105) accepts caller count—defaulting to INITIAL_ENRICH_COUNT = 50 (utils/sessionStorage.ts:4577; callers utils/sessionStorage.ts:3973, utils/sessionStorage.ts:4020, utils/sessionStorage.ts:4076, utils/sessionStorage.ts:4089).
It reuses one LITE_READ_BUF_SIZE = 65536 (64 KiB, utils/sessionStoragePortable.ts:17) buffer via readLiteMetadata() (utils/sessionStorage.ts:4739-4813) → readHeadAndTail() (utils/sessionStoragePortable.ts:215-242) for 64 KiB head/tail reads. (Stale utils/sessionStorage.ts:2810 and utils/sessionStorage.ts:5049 comments say "16KB".)
4. Crash Recovery, Resume & Session Forking (utils/json.ts, utils/conversationRecovery.ts, utils/sessionRestore.ts)
The Problem
Hard kills damage two layers: truncated trailing JSONL lines, and turns ending in dangling tool_use, orphaned thinking, or unanswered prompts. Resume and --fork-session must repair both without corrupting parent state.
Two-Level Crash Resilience (utils/json.ts, utils/conversationRecovery.ts)
- Syntactic partial-write recovery (
utils/json.ts, 277 lines):parseJSONL()(utils/json.ts:182-190) runs nativeBun.JSONL.parseChunk(parseJSONLBun(),utils/json.ts:102-127). On a mid-stream error (result.error), it keeps already-parsedresult.values, skips past the next\n(0x0a), and resumesparse(data, offset).
Fallbacks parseJSONLBuffer() (utils/json.ts:129-153) and parseJSONLString() (utils/json.ts:155-175) strip UTF-8 BOMs and skip bad lines via per-line try/catch, while readJSONLFile() (utils/json.ts:192-226) bounds raw tail reads at MAX_JSONL_READ_BYTES (100 MB).
- Semantic interruption repair (
utils/conversationRecovery.ts:164-373):deserializeMessagesWithInterruptDetection()returns{ messages, turnInterruptionState }(DeserializeResult/TurnInterruptionState,utils/conversationRecovery.ts:139-146,utils/conversationRecovery.ts:247) across six stages: - Migrates legacy attachments (
migrateLegacyAttachmentTypes(),utils/conversationRecovery.ts:77-132), strips unknownpermissionModestrings, and filters unresolvedtool_uses, orphaned thinking-only blocks, and whitespace-only assistant messages. - Runs
detectTurnInterruption()(utils/conversationRecovery.ts:272-333), skipping trailingsystem,progress, andisApiErrorMessageassistants. For trailingtool_results,isTerminalToolResult()(utils/conversationRecovery.ts:348-373) recognizes brief/Kairos terminal tools—presenttools/BriefTool/prompt.ts(feature('KAIROS') || feature('KAIROS_BRIEF'),utils/conversationRecovery.ts:55-66) and absenttools/SendUserFileTool/prompt.js(feature('KAIROS'),utils/conversationRecovery.ts:67-71)—fixing#20467. - Turns
interrupted_turnintointerrupted_promptby appending syntheticisMeta: true'Continue from where you left off.', plus aNO_RESPONSE_REQUESTEDassistant sentinel so history stays API-valid even without auto-continue.
Resume Loading, Hydration & --fork-session (utils/conversationRecovery.ts, utils/sessionRestore.ts)
loadConversationForResume() (utils/conversationRecovery.ts:456-597) resolves --continue (skipping live --bg/daemon sessions via absent utils/udsClient.js behind feature('BG_SESSIONS'), utils/conversationRecovery.ts:492-506), .jsonl paths, UUIDs, or LogOptions.
It then runs loadFullLog, copyPlanForResume, void copyFileHistoryForResume(log) (utils/conversationRecovery.ts:550), checkResumeConsistency(), restoreSkillStateFromMessages() (utils/conversationRecovery.ts:382-403, replaying invoked_skills and suppressing duplicate skill_listing), deserializeMessagesWithInterruptDetection(), and processSessionStartHooks('resume').
restoreSessionStateFromLog() (utils/sessionRestore.ts:99-150) hydrates fileHistory via fileHistoryRestoreStateFromLog() (utils/sessionRestore.ts:104-108), attribution (feature('COMMIT_ATTRIBUTION')), and legacy SDK todos (!isTodoV2Enabled(), utils/tasks.ts:133-139, bypassed by CLAUDE_CODE_ENABLE_TASKS).
Under feature('CONTEXT_COLLAPSE') (utils/sessionRestore.ts:127-136), it calls absent services/contextCollapse/persist.js's restoreFromEntries() even on empty arrays so /resume clears stale collapse state.
processResumedConversation() (utils/sessionRestore.ts:409-551) runs coordinator sync (feature('COORDINATOR_MODE')), restoreAgentFromSession(), non-fork worktree chdir (restoreWorktreeForResume(), utils/sessionRestore.ts:479; exitRestoredWorktree() at utils/sessionRestore.ts:380-400 runs only on mid-session /resume, screens/REPL.tsx:1878), a second CONTEXT_COLLAPSE restore (utils/sessionRestore.ts:494-503), and the in-place vs. --fork-session split:
// utils/sessionRestore.ts:436-463 if (!opts.forkSession) { const sid = opts.sessionIdOverride ?? result.sessionId if (sid) { // When resuming from a different project directory (git worktrees, // cross-project), transcriptPath points to the actual file; its dirname // is the project dir. Otherwise the session lives in the current project. switchSession( asSessionId(sid), opts.transcriptPath ? dirname(opts.transcriptPath) : null, ) // Rename asciicast recording to match the resumed session ID so // getSessionRecordingPaths() can discover it during /share await renameRecordingForSession() await resetSessionFilePointer() restoreCostStateForSession(sid) } } else if (result.contentReplacements?.length) { // --fork-session keeps the fresh startup session ID. useLogMessages will // copy source messages into the new JSONL via recordTranscript, but // content-replacement entries are a separate entry type only written by // recordContentReplacement (which query.ts calls for newlyReplaced, never // the pre-loaded records). Without this seed, `claude -r {newSessionId}` // finds source tool_use_ids in messages but no matching replacement records // → they're classified as FROZEN → full content sent (cache miss, permanent // overage). insertContentReplacement stamps sessionId = getSessionId() = // the fresh ID, so loadTranscriptFile's keyed lookup will match. await recordContentReplacement(result.contentReplacements) }5. Rolling Back Conversation & Disk Together: /rewind (utils/fileHistory.ts)
The Problem
Rewinding messages without disk rollback desyncs files from memory. Naive snapshotting blocks reducers with I/O, OOMs via readFileSync+writeFileSync, overwrites v1 backups, and loses backups on session expiry.
The Real Implementation (utils/fileHistory.ts)
utils/fileHistory.ts (1,115 lines; zero feature(), USER_TYPE, or GrowthBook gates) solves all five hazards:
- Dual interactive vs. SDK gate (
utils/fileHistory.ts:63-78): Interactive mode defaults on unlessfileCheckpointingEnabled === falseorCLAUDE_CODE_DISABLE_FILE_CHECKPOINTINGis truthy. SDK/non-interactive mode defaults off, requiringCLAUDE_CODE_ENABLE_SDK_FILE_CHECKPOINTINGtruthy andCLAUDE_CODE_DISABLE_FILE_CHECKPOINTINGfalsy. - Three-phase async protocol (
fileHistoryTrackEdit()atutils/fileHistory.ts:86-193,fileHistoryMakeSnapshot()atutils/fileHistory.ts:198-342): - Phase 1 (Same-ref capture): Calls
updateFileHistoryState(state => { captured = state; return state }), letting same-ref wrappers (QueryEngine.ts:376-384,screens/REPL.tsx:2442-2454) skip notifications and no-op stubs (utils/forkedAgent.ts:432) bail cleanly. InfileHistoryTrackEdit(), Phase 1 guardsmostRecent.trackedFileBackups[trackingPath](utils/fileHistory.ts:99-118) so repeat edits never corrupt{hash}@v1. - Phase 2 (Async disk I/O): Runs
stat(),checkOriginFileChanged(), andcreateBackup()outside store updaters. - Phase 3 (Fresh re-read commit): Inherits files tracked during Phase 2's async window (
utils/fileHistory.ts:290-297), enforcesMAX_SNAPSHOTS = 100(utils/fileHistory.ts:54,utils/fileHistory.ts:305-313) with monotonicsnapshotSequence, and persists viarecordFileHistorySnapshot(). - Path-hashed backups & stat-first creation sentinels (
utils/fileHistory.ts:725-798):getBackupFileName()computes{sha256(filePath).slice(0, 16)}@v{version}under{configDir}/file-history/{sessionId}/—hashing the path, not contents (correcting imprecise"content hash"comments intools/FileEditTool/FileEditTool.ts:433andtools/FileWriteTool/FileWriteTool.ts:257).createBackup()keeps source-missing distinct from dir-missing and avoids heap OOMs:
// utils/fileHistory.ts:759-786 // Stat first: if the source is missing, record a null backup and skip the // copy. Separates "source missing" from "backup dir missing" cleanly — // sharing a catch for both meant a file deleted between copyFile-success // and stat would leave an orphaned backup with a null state record. let srcStats: Stats try { srcStats = await stat(filePath) } catch (e: unknown) { if (isENOENT(e)) { return { backupFileName: null, version, backupTime: new Date() } } throw e } // copyFile preserves content and avoids reading the whole file into the JS // heap (which the previous readFileSync+writeFileSync pipeline did, OOMing // on large tracked files). Lazy mkdir: 99% of calls hit the fast path // (directory already exists); on ENOENT, mkdir then retry. try { await copyFile(filePath, backupPath) } catch (e: unknown) { if (!isENOENT(e)) throw e await mkdir(dirname(backupPath), { recursive: true }) await copyFile(filePath, backupPath) } // Preserve file permissions on the backup. await chmod(backupPath, srcStats.mode)- Dual-keyed DAG-branch replay (
utils/fileHistory.ts:172-175,utils/fileHistory.ts:319-322,utils/sessionStorage.ts:2248-2272): Turn-startfileHistoryMakeSnapshot()writesentry.messageId = userMessage.uuidwithisSnapshotUpdate: false. Mid-turnfileHistoryTrackEdit()attachesv1tomostRecentSnapshot(keepingsnapshot.messageId = userMessage.uuid) while keying the JSONL entry on the assistantparentMessage.uuidwithisSnapshotUpdate: true.
On load, buildFileHistorySnapshotChain() replays only entries along the active parentUuid branch, updating snapshots[existingIndex] in place so abandoned rewind branches never leak into active history.
Under fileHistoryEnabled(), fileHistoryRestoreStateFromLog() (utils/fileHistory.ts:888-917; callers utils/sessionRestore.ts:104-108, hooks/useFileHistorySnapshotInit.ts:16-24) hydrates FileHistoryState: migrating legacy absolute paths via maybeShortenFilePath() (utils/fileHistory.ts:867-876), rebuilding trackedFiles: Set<string>, and setting snapshotSequence: snapshots.length.
- Pure-disk rewind + append-only conversation rewind (
utils/fileHistory.ts:347-397,utils/fileHistory.ts:537-672,utils/fileHistory.ts:847-862):fileHistoryRewind()never mutatesFileHistoryState(allowing repeated forward/backward rewinds) and runsapplySnapshot()across every path instate.trackedFiles.
For targets predating a file's first edit, getBackupFileNameFirstVersion() checks version === 1 (failing safe if MAX_SNAPSHOTS = 100 evicted it). null calls unlink(); non-null calls restoreBackup() only when checkOriginFileChanged() / compareStatsAndContent() (utils/fileHistory.ts:600-672) detects existence, mode/size, mtimeMs fast-path, or UTF-8 content diffs.
In parallel, interactive /rewind (screens/REPL.tsx:3661-3707) slices messages and triggers a same-head shrink in useLogMessages (hooks/useLogMessages.ts:53-57, hooks/useLogMessages.ts:102-114). JSONL is never truncated—the next prompt chains off the rewound leaf, leaving an abandoned fork that walkChainBeforeParse() skips.
(Forward seams: pre-edit fileHistoryTrackEdit() callers in FileEditTool, FileWriteTool, NotebookEditTool, and BashTool's applySedEdit()—not general shell commands—belong to Chapter 12; MessageSelector /rewind belongs to UI chapters.)
Putting Layer 2 Together
How This Snaps Into the Next Layer
Chapter 08 closes Layer 2 (The Agent Engine). Chapter 09 opens Layer 3 (Long-Horizon Context & Memory) directly on top of Chapter 08's persistence hooks:
services/compact/compact.ts:598,services/compact/compact.ts:1014, andservices/compact/sessionMemoryCompact.ts:447emitcompact_boundaryentries (withservices/compact/compact.ts:711andservices/compact/compact.ts:1057callingreAppendSessionMetadata()) beforeutils/sessionStorage.ts:1040-1041seversparentUuidand saveslogicalParentUuid.query.ts:376-535runs five pre-API stages: tool-result budget (recordContentReplacement,query.ts:376-394), snip (feature('HISTORY_SNIP'),query.ts:401-410), microcompact (query.ts:413-426; post-APImicrocompact_boundaryviafeature('CACHED_MICROCOMPACT'),query.ts:870-892), collapse (feature('CONTEXT_COLLAPSE')→ snapshot-absentcontextCollapse.applyCollapsesIfNeeded(),query.ts:440-447), and autocompact (query.ts:454-535).- Zero-caller
recordContextCollapseCommit()/recordContextCollapseSnapshot()(utils/sessionStorage.ts:1541-1581) define themarble-origami-commit/marble-origami-snapshotwriters;QueryEngine.ts:701-715flushespreservedSegment.tailUuid, andQueryEngine.ts:918-942splices pre-boundary history for GC. services/compact/postCompactCleanup.ts:76callsclearSessionMessagesCache()(utils/sessionStorage.ts:3854-3856) to cleargetSessionMessages's UUID dedup cache.
Reading Exercises & Code Checkpoints
- Trace conversation-lifetime state in
QueryEnginevs. single-turnquery():
rg -n 'export (class QueryEngine|async function\* ask)|private (mutableMessages|readFileState|permissionDenials|totalUsage|hasHandledOrphanedPermission|discoveredSkillNames|loadedNestedMemoryPaths)|async \*submitMessage\(|for await \(const message of query\(' QueryEngine.ts Verify why discoveredSkillNames clears at QueryEngine.ts:238 while loadedNestedMemoryPaths persists, and why cli/print.ts:2133 creates a fresh AbortController before every ask() call.
- Locate JSONL scale guards and two-level crash-recovery entry points:
rg -n 'MAX_TRANSCRIPT_READ_BYTES|SKIP_PRECOMPACT_THRESHOLD|walkChainBeforeParse|CLAUDE_CODE_DISABLE_PRECOMPACT_SKIP|deserializeMessagesWithInterruptDetection|handleOrphanedPermission' utils/sessionStorage.ts utils/sessionStoragePortable.ts utils/conversationRecovery.ts utils/queryHelpers.ts Trace why walkChainBeforeParse() gates on 50% dropped bytes (len - chainBytes < len >> 1) instead of entry counts, and compare syntactic recovery in utils/json.ts:102-127 with semantic repair in utils/conversationRecovery.ts:164-252.
- Audit file-checkpointing gates, pre-edit tracking sites, and
/rewindcallers:
rg -n 'fileHistory(TrackEdit|MakeSnapshot|Rewind)|CLAUDE_CODE_(DISABLE|ENABLE_SDK)_FILE_CHECKPOINTING' utils/fileHistory.ts utils/handlePromptSubmit.ts QueryEngine.ts tools/ screens/REPL.tsx cli/print.ts Confirm why SDK mode defaults checkpointing off while interactive mode defaults it on, and verify which tools invoke fileHistoryTrackEdit() (noting why general BashTool commands stay untracked outside applySedEdit()).
- Verify the Layer 2 → Layer 3 compaction persistence seam and snapshot-absent restore hook:
rg -n 'logicalParentUuid|applyPreservedSegmentRelinks|applySnipRemovals|clearSessionMessagesCache|contextCollapse/persist\.js' utils/sessionStorage.ts utils/sessionRestore.ts services/compact/postCompactCleanup.ts Follow a compact_boundary from utils/sessionStorage.ts:1040-1041 through applyPreservedSegmentRelinks() (utils/sessionStorage.ts:1839-1956) and clearSessionMessagesCache() (services/compact/postCompactCleanup.ts:76) to prepare for Chapter 09.