Part I · Foundations: Atoms, State, and Contracts
Chapter 03
The Command & Message Contracts: User Input Meets the Model
Layer: Layer 1 (Core Capability & Input Contracts)
Prerequisites: Chapter 00, Chapter 01, Chapter 02
Primary Source Files:
types/command.ts— three-variantCommandunion,LocalJSXCommandContext,CommandAvailability, and leaf helpers (216 lines)commands.ts—COMMANDSthunk, memoizedloadAllCommands, livegetCommandsfiltering, skill projections, and remote/bridge allowlists (754 lines)utils/slashCommandParsing.ts— zero-import slash parser with two-token(MCP)glue (60 lines)utils/processUserInput/processUserInput.ts— multi-modal input router, image resizing,bridgeOrigingate,ULTRAPLANrewrite, attachments, andUserPromptSubmithooks (605 lines)utils/processUserInput/processTextPrompt.ts— plain-text prompt assembly,promptId, interaction spans, and OTel logging (100 lines)utils/messages.ts— dual UI/API projections, Stage 1normalizeMessagesForAPI, thinking-block passes,stripSignatureBlocks, andensureToolResultPairing(5,512 lines)
Why This Layer Exists
Chapter 02 built the model-facing half of Layer 1 (Tool, ToolUseContext, assembleToolPool). Before Layer 2 can build a system prompt and call the LLM, Claude Code needs the human-facing half:
- One contract for user-invocable commands:
/compact,/model,/commit,/my-skill, and/server:prompt (MCP)share one/prefix across host actions, Ink dialogs, inline prompt expansions, and forked subagents. - One multi-modal input funnel: Terminal strings, SDK/VS Code
ContentBlockParam[]arrays, pasted images,!shell escapes, IDE selections, attachments, Remote Control bridge messages, andUserPromptSubmithooks must collapse into oneshouldQueryenvelope. - Two projections over one transcript: Ink wants 1-block rows, spinners, retry rows, and compaction dividers; the Anthropic API requires strictly alternating
user/assistantturns, valid thinking signatures, and pairedtool_use/tool_resultIDs.
1. The Three-Variant Command Contract & Registry
The Problem
Separate registries for CLI commands, Ink pickers, disk skills, plugins, workflows, and MCP prompts would fracture autocomplete, SkillTool, and remote-safety checks. Eagerly importing ~100 command modules or reading auth state at module load would also hurt startup latency and break mid-session /login switches.
The Real Implementation
types/command.ts unifies every command into Command = CommandBase & (PromptCommand | LocalCommand | LocalJSXCommand) (types/command.ts:205-206). Every import at types/command.ts:1-14 is import type; only getCommandName and isCommandEnabled (types/command.ts:209-216) emit runtime code.
PromptCommand(type: 'prompt',types/command.ts:25-57) unifies built-in prompt commands (/init,/review,/insights), bundled skills (skills/bundledSkills.ts:75-95), disk skills and.claude/commands/(skills/loadSkillsDir.ts:467,skills/loadSkillsDir.ts:608), plugins (utils/plugins/loadPluginCommands.ts:300-325), workflows (feature('WORKFLOW_SCRIPTS')), and MCP prompts (services/mcp/client.ts:2056-2095), carryinghooks,skillRoot,allowedTools,model,effort,paths, and inline/fork controls:
// types/command.ts:42-56
// Execution context: 'inline' (default) or 'fork' (run as sub-agent)
// 'inline' = skill content expands into the current conversation
// 'fork' = skill runs in a sub-agent with separate context and token budget
context?: 'inline' | 'fork'
// Agent type to use when forked (e.g., 'Bash', 'general-purpose')
// Only applicable when context is 'fork'
agent?: string
effort?: EffortValue
// Glob patterns for file paths this skill applies to
// When set, the skill is only visible after the model touches matching files
paths?: string[]
getPromptForCommand(
args: string,
context: ToolUseContext,
): Promise<ContentBlockParam[]>
LocalCommand(type: 'local',types/command.ts:74-78) lazy-loads host code viaload: () => Promise<LocalCommandModule>and returnsLocalCommandResult(types/command.ts:16-23:'text','compact', or'skip').LocalJSXCommand(type: 'local-jsx',types/command.ts:144-152) lazy-loads an Ink dialog mounted viasetToolJSX, completing throughLocalJSXCommandOnDone(types/command.ts:117-126:display?: 'skip' | 'system' | 'user',shouldQuery?,metaMessages?,nextInput?,submitNextInput?).
Ch 02 → Ch 03 seam: Tool.ts:11 imports type { Command } into ToolUseContext.options.commands (Tool.ts:158-160), while types/command.ts:6 extends ToolUseContext into LocalJSXCommandContext (types/command.ts:80-98, adding setMessages, canUseTool, theme, onChangeAPIKey, resume). Both local and local-jsx receive LocalJSXCommandContext; import type breaks the bidirectional cycle. On CommandBase (types/command.ts:175-203), static auth/provider availability?: ('claude-ai' | 'console')[] (types/command.ts:169-173) is kept strictly separate from dynamic isEnabled?: () => boolean.
commands.ts splits discovery across three volatility tiers:
- Deferred built-ins (
COMMANDS,commands.ts:258-346): A privatelodash-es/memoizethunk becauseisUsing3PServices()andlogin()read config and auth unavailable at module load. Heavy/insights(commands/insights.ts, ~113 KB) uses a lazy shim (usageReport,commands.ts:190-202), andINTERNAL_ONLY_COMMANDS(commands.ts:225-254) spreads only whenprocess.env.USER_TYPE === 'ant' && !process.env.IS_DEMO(commands.ts:343-345). - Memoized
loadAllCommands(cwd)vs. livegetCommands(cwd)(commands.ts:449-517):
// commands.ts:449-485
const loadAllCommands = memoize(async (cwd: string): Promise<Command[]> => {
const [
{ skillDirCommands, pluginSkills, bundledSkills, builtinPluginSkills },
pluginCommands,
workflowCommands,
] = await Promise.all([
getSkills(cwd),
getPluginCommands(),
getWorkflowCommands ? getWorkflowCommands(cwd) : Promise.resolve([]),
])
return [
...bundledSkills,
...builtinPluginSkills,
...skillDirCommands,
...workflowCommands,
...pluginCommands,
...pluginSkills,
...COMMANDS(),
]
})
// ...
export async function getCommands(cwd: string): Promise<Command[]> {
const allCommands = await loadAllCommands(cwd)
// Get dynamic skills discovered during file operations
const dynamicSkills = getDynamicSkills()
// Build base commands without dynamic skills
const baseCommands = allCommands.filter(
_ => meetsAvailabilityRequirement(_) && isCommandEnabled(_),
)
Running meetsAvailabilityRequirement (commands.ts:417-443; 'console' requires !isClaudeAISubscriber() && !isUsing3PServices() && isFirstPartyAnthropicBaseUrl(), excluding Bedrock/Vertex/Foundry and gateways) and isCommandEnabled outside loadAllCommands lets /login take effect immediately. getCommands then deduplicates getDynamicSkills() and splices them right before built-in COMMANDS() (commands.ts:505-516), preserving findCommand order (commands.ts:688-698).
- MCP & skill projections: Reactive MCP commands live in
AppState.mcp.commands(services/mcp/client.ts:2054-2096) and merge viauseMergedCommands(hooks/useMergedCommands.ts:5-15); behindfeature('MCP_SKILLS'),getMcpSkillCommands(commands.ts:547-559) filtersloadedFrom === 'mcp'(its loaderskills/mcpSkills.jsis absent from this snapshot). Two memoized selectors project distinct skill slices:getSkillToolCommands(commands.ts:563-581) returns strictly model-invocable prompt commands (!cmd.disableModelInvocationatcommands.ts:569), whereasgetSlashCommandToolSkills(commands.ts:586-608) selects skill-shaped prompt commands and explicitly includescmd.disableModelInvocation(commands.ts:598). Human-facing labels (formatDescriptionWithSource,commands.ts:728-754) stay strictly in the UI layer. - Remote & bridge allowlists:
REMOTE_SAFE_COMMANDS(commands.ts:619-637, 17 local-TUI built-ins) feedsfilterCommandsForRemoteMode(commands.ts:684-686). For Remote Control mobile/web input,BRIDGE_SAFE_COMMANDS(commands.ts:651-660) andisBridgeSafeCommand(commands.ts:672-676) relax PR #19134's blanket bridge block (originally added after iOS/modelpopped the local terminal Ink picker,commands.ts:666-670):'local-jsx'stays blocked (false),'prompt'is safe by construction (true), and'local'fails closed against six allowlisted commands (compact,clear,cost,summary,releaseNotes,files).
Hidden Invariants & Failure Modes
- Split cache invalidation (
commands.ts:523-539):clearCommandMemoizationCaches()clearsloadAllCommands,getSkillToolCommands,getSlashCommandToolSkills, and—behindfeature('EXPERIMENTAL_SKILL_SEARCH')(commands.ts:96-100)—clearSkillIndexCache?.()(outer memoization in absentservices/skillSearch/localSearch.js) without callingclearSkillCaches(), becauseutils/skills/skillChangeDetector.ts:92-97invokes it right after discovering dynamic skills. FullclearCommandsCache()wipes everything on/reload-plugins. Neither clearsCOMMANDS.cache, andcommands.ts:1disables import sorting to protect ANT-ONLY strip markers.
Gates & Snapshot Absences in
commands.ts: Sixteen compile-timefeature('...')flags (commands.ts:63-122,commands.ts:401) andprocess.env.USER_TYPE === 'ant'(commands.ts:49) gate conditionalrequire()imports, while a seventeenth flag (feature('MCP_SKILLS'),commands.ts:550) gatesgetMcpSkillCommands()runtime filtering andprocess.env.USER_TYPE === 'ant' && !process.env.IS_DEMO(commands.ts:343) gates spreadingINTERNAL_ONLY_COMMANDSintoCOMMANDS(). Thirteen conditionalrequire()targets (commands/agents-platform/index.js,commands/proactive.js,commands/assistant/index.js,commands/remoteControlServer/index.js,commands/force-snip.js,commands/workflows/index.js,services/skillSearch/localSearch.js,commands/subscribe-pr.js,commands/torch.js,commands/peers/index.js,commands/fork/index.js,commands/buddy/index.js,tools/WorkflowTool/createWorkflowCommand.js) are absent from this snapshot (call-site evidence only); 5feature()-gated commands (commands/brief.ts,commands/bridge/index.ts,commands/voice/index.ts,commands/remote-setup/index.ts,commands/ultraplan.tsx) are present; and 18 inert.jsstub files back 19 of the 28INTERNAL_ONLY_COMMANDSentries (17 one-line{ isEnabled: () => false, isHidden: true, name: 'stub' }default stubs plus 4-linecommands/reset-limits/index.jsexporting bothresetLimitsandresetLimitsNonInteractive).
2. Slash Parsing & Runtime Dispatch
The Problem
A leading / could be a built-in command, an MCP prompt with a space before (MCP), a mistyped skill with long arguments, or a POSIX path like /etc/hosts. Once resolved, each variant must protect UI focus, redact secrets, preserve compaction timestamps, and prevent local output from baiting the model into replying next turn.
The Real Implementation
parseSlashCommand (utils/slashCommandParsing.ts:25-60) is a zero-import parser returning ParsedSlashCommand | null ({ commandName, args, isMcp }, utils/slashCommandParsing.ts:5-9). Non-/ input or a bare '/' returns null; otherwise it splits strictly on ' ' and glues words[1] === '(MCP)' into commandName + ' (MCP)' to match services/mcp/client.ts:2066-2070 ("{client.name}:{prompt.name} (MCP)").
Because POSIX paths like /usr/bin/env also start with /, semantic disambiguation happens inside utils/processUserInput/processSlashCommand.tsx (922 lines: 921 pre-compiled .tsx lines + line 922 inline sourcemap, with build-time folded "external" === 'ant' checks at utils/processUserInput/processSlashCommand.tsx:276, utils/processUserInput/processSlashCommand.tsx:430, and utils/processUserInput/processSlashCommand.tsx:498). When !hasCommand() (utils/processUserInput/processSlashCommand.tsx:333-381), it probes stat('/' + commandName) and tests looksLikeCommand(commandName) (!/[^a-zA-Z0-9:\-_]/.test(commandName), utils/processUserInput/processSlashCommand.tsx:304-308). Only looksLikeCommand && !isFilePath rejects with Unknown skill: ... (shouldQuery: false), emitting a UI-only warning Args from unknown skill: ... (gh-32591, utils/processUserInput/processSlashCommand.tsx:355-357) so typed arguments survive; real filesystem paths fall through as plain prompts (shouldQuery: true).
For known commands, getMessagesForSlashCommand (utils/processUserInput/processSlashCommand.tsx:525-777) blocks userInvocable === false and switches on command.type:
local-jsx(utils/processUserInput/processSlashCommand.tsx:551-656): Settles asynchronously viaonDone. On theoptions?.display === 'system'branch (utils/processUserInput/processSlashCommand.tsx:591-593), it skips' dismissed'scrollback entries when fullscreen mode is enabled (isFullscreenEnvEnabled(), gated byCLAUDE_CODE_NO_FLICKER/process.env.USER_TYPE === 'ant'outsidetmux -CCatutils/fullscreen.ts:112-129). Two explicit deadlock guards protect the input queue (utils/processUserInput/processSlashCommand.tsx:622-655):doneWasCalledstops an early-exitonDone()plus returned JSX from re-armingisLocalJSXCommand, and.catch()clears JSX and resolves so thrown errors never wedgequeryGuard.local(utils/processUserInput/processSlashCommand.tsx:657-722): Redacts arguments to'***'whenisSensitiveis set and wraps'text'results increateCommandInputMessage(system/local_command). For'compact'results, it bumpsdisplayText's timestamp byDate.now() + 100(utils/processUserInput/processSlashCommand.tsx:689) so--resumeselects it as the latest leaf, then callsresetMicrocompactState()(utils/processUserInput/processSlashCommand.tsx:699).prompt(utils/processUserInput/processSlashCommand.tsx:723-921):- Forked (
context === 'fork',utils/processUserInput/processSlashCommand.tsx:62-295): The synchronous path streamsrunAgent({ isAsync: false, querySource: 'agent:custom', ... })and returnsshouldQuery: false. Behindfeature('KAIROS') && (await context.getAppState()).kairosEnabled(utils/processUserInput/processSlashCommand.tsx:102-183), it instead launches an asyncrunAgentwith a standalonebgAbortControllerand capturedspawnTimeWorkload = getWorkload(). Before running, the background task polls every200ms up toMCP_SETTLE_TIMEOUT_MS(10_000ms,utils/processUserInput/processSlashCommand.tsx:56-57) until no MCP client is'pending', callsrefreshTools?.(), and re-enqueues<scheduled-task-result>(isMeta: true, skipSlashCommands: true). - Inline (
getMessagesForPromptSlashCommand,utils/processUserInput/processSlashCommand.tsx:827-921, shared bySkillToolviaprocessPromptSlashCommandatutils/processUserInput/processSlashCommand.tsx:817-826): Behindfeature('COORDINATOR_MODE') && isEnvTruthy(process.env.CLAUDE_CODE_COORDINATOR_MODE) && !context.agentId(utils/processUserInput/processSlashCommand.tsx:837-868), main-thread coordinators receive a delegation stub while workers (agentIdset) expand the prompt normally:
- Forked (
// utils/processUserInput/processSlashCommand.tsx:874-912
const hooksAllowedForThisSkill = !isRestrictedToPluginOnly('hooks') || isSourceAdminTrusted(command.source);
if (command.hooks && hooksAllowedForThisSkill) {
const sessionId = getSessionId();
registerSkillHooks(context.setAppState, sessionId, command.hooks, command.name, command.type === 'prompt' ? command.skillRoot : undefined);
}
// ...
const skillPath = command.source ? `${command.source}:${command.name}` : command.name;
const skillContent = result.filter((b): b is TextBlockParam => b.type === 'text').map(b => b.text).join('\n\n');
addInvokedSkill(command.name, skillPath, skillContent, getAgentContext()?.agentId ?? null);
const metadata = formatCommandLoadingMetadata(command, args);
const additionalAllowedTools = parseToolListFromCLI(command.allowedTools ?? []);
// ...
const messages = [createUserMessage({
content: metadata,
uuid
}), createUserMessage({
content: mainMessageContent,
isMeta: true
}), ...attachmentMessages, createAttachmentMessage({
type: 'command_permissions',
allowedTools: additionalAllowedTools,
model: command.model
})];
Hidden Invariants & Failure Modes
- Skill hooks & compaction isolation:
hooksAllowedForThisSkillenforcesstrictPluginOnlyCustomization: ["hooks"], whileaddInvokedSkillkeysbootstrap/state.ts:1510-1524by"{agentId ?? ''}:{skillName}"so invoked skills never leak across subagents during compaction. - Metadata & 110 KB latency guard: Expanded-body attachment extraction passes
{ skipSkillDiscovery: true }(utils/processUserInput/processSlashCommand.tsx:897-901) to avoid stalls on 110 KBSKILL.mdfiles. Meanwhile,formatCommandLoadingMetadata(utils/processUserInput/processSlashCommand.tsx:803-816) emits<command-name>and<command-message>using qualifiedcommand.name(plus<skill-format>true</skill-format>for model-only skills). - Synthetic caveat shield (
utils/processUserInput/processSlashCommand.tsx:512-515): UnlessmessageShouldQuery, all messages aresystem/local_command, or the result is a compaction boundary,processSlashCommandprependscreateSyntheticUserCaveatMessage()(utils/messages.ts:566-571,isMeta: true<local-command-caveat>) so the model ignores local output next turn.
3. Multi-Modal Input Routing & UserPromptSubmit Hooks
The Problem
Input arrives from terminals (strings, pasted images, ! shell escapes, IDE selections), SDK/VS Code callers (ContentBlockParam[] arrays), and Remote Control bridges—while UserPromptSubmit hooks can augment, stop, or erase model-bound prompts.
The Real Implementation
ProcessUserInputContext = ToolUseContext & LocalJSXCommandContext and ProcessUserInputBaseResult (utils/processUserInput/processUserInput.ts:62-83) unify REPL (utils/handlePromptSubmit.ts:476-496) and SDK (QueryEngine.ts:416-428) callers behind processUserInput() (utils/processUserInput/processUserInput.ts:85-270). The outer shell echoes non-isMeta prompt strings (setUserInputOnProcessing), profiles with queryCheckpoint() (CLAUDE_CODE_PROFILE_QUERY, utils/queryProfiler.ts:36), and returns early when !result.shouldQuery so local turns bypass hooks.
Inside processUserInputBase() (utils/processUserInput/processUserInput.ts:281-589):
- String vs.
ContentBlockParam[](utils/processUserInput/processUserInput.ts:300-349): Resizes inlineimageblocks intonormalizedInput(fixing dropped resizes and iOSmediaTypevs.media_typepermobile-apps#5825), collectsimageMetadataTexts, splits a trailingtextblock intoinputString+precedingInputBlocks, and throws ifinputString === null && mode !== 'prompt'. - Terminal
pastedContents(utils/processUserInput/processUserInput.ts:351-420): Filters pastes viaisValidImagePaste(types/textInputTypes.ts:367-369, dropping 0-byte images that 400), persists viastoreImages()(utils/imageStore.ts:84-99), resizes in parallel (Promise.all), and appendsimageMetadataTextsas anisMeta: trueUserMessageviaaddImageMetadataMessage()(utils/processUserInput/processUserInput.ts:592-605) on every exit exceptbridgeOriginrejection. bridgeOrigingate (utils/processUserInput/processUserInput.ts:422-453): Mobile/web input arrives withbridgeOrigin: trueandskipSlashCommands: true. For/...,isBridgeSafeCommand(commands.ts:672-676) clearseffectiveSkipSlashfor safe commands, short-circuits known unsafe commands (shouldQuery: false), and lets unknown/shrugfall through as plain text.- Gated
ULTRAPLANrewrite (utils/processUserInput/processUserInput.ts:467-493): Behindfeature('ULTRAPLAN')plus 8 runtime guards (checkinghasUltraplanKeyword(preExpansionInput ?? inputString)so pasted text cannot trigger), rewrites to/ultraplan ${rewritten}before attachment I/O so React batches UI transitions without flicker. - Attachments & IDE selection (
utils/processUserInput/processUserInput.ts:495-514): Skipped for/slashcommands inpromptmode; otherwise drainsgetAttachmentMessages(utils/attachments.ts:2937-2970), which wrapsgetAttachments(utils/attachments.ts:743-1003). InsidegetAttachments, collection runs under a 1,000 ms timeout (utils/attachments.ts:767) and short-circuits to queued commands only whenCLAUDE_CODE_DISABLE_ATTACHMENTSorCLAUDE_CODE_SIMPLEis set (utils/attachments.ts:752-760). On the main thread,ideSelectionemitsselected_lines_in_ideoropened_file_in_idewhile honoringisFileReadDenied(). - Three dispatch exits:
mode === 'bash'(utils/processUserInput/processUserInput.ts:517-529): Dynamically importsutils/processUserInput/processBashCommand.tsx(140 lines: 139 code + line 140 sourcemap). It selects PowerShell only whenisPowerShellToolEnabled() && resolveDefaultShell() === 'powershell'(utils/processUserInput/processBashCommand.tsx:26), lazy-requiring ~300 KBPowerShellToolbehind a Windows-only gate (CLAUDE_CODE_USE_POWERSHELL_TOOL/process.env.USER_TYPE === 'ant',utils/shell/shellToolUtils.ts:17-22). Execution runs withdangerouslyDisableSandbox: true, formats stdout viaprocessToolResultBlock(shellTool, { ...data, stderr: '' }, randomUUID())(utils/processUserInput/processBashCommand.tsx:98-106), and returnsshouldQuery: false./slash(utils/processUserInput/processUserInput.ts:533-551): Dynamically importsutils/processUserInput/processSlashCommand.tsx.- Plain text (
utils/processUserInput/processUserInput.ts:553-588): Logstengu_subagent_at_mentionwhen an@agent-attachment is present and delegates assembly toprocessTextPrompt().
Plain-text assembly (utils/processUserInput/processTextPrompt.ts:19-100): Every non-slash, non-bash prompt funnels through processTextPrompt(). It mints a fresh promptId via setPromptId(promptId) (bootstrap/state.ts) and opens the telemetry interaction span using the first text block (.find(block => block.type === 'text')).
By contrast, OTel user_prompt logging (redacted unless OTEL_LOG_USER_PROMPTS is set, utils/telemetry/events.ts:14) reads .findLast(block => block.type === 'text') (utils/processUserInput/processTextPrompt.ts:47-57)—a production scar from anthropics/claude-code#33301, because VS Code submits ContentBlockParam[] arrays with <ide_selection> context prepended ahead of the user's actual message. After logging tengu_input_prompt keyword analytics, processTextPrompt() drops whitespace-only text when pasted images are attached (utils/processUserInput/processTextPrompt.ts:67-76) so the model sees clean image blocks, then returns [userMessage, ...attachmentMessages] with shouldQuery: true.
When result.shouldQuery is true, executeUserPromptSubmitHooks (utils/processUserInput/processUserInput.ts:182-263) distinguishes blocking from stopping and truncates additionalContexts / non-empty hook_success strings at MAX_HOOK_OUTPUT_LENGTH = 10000 (applyTruncation, utils/processUserInput/processUserInput.ts:272-279):
// utils/processUserInput/processUserInput.ts:193-224
// Return only a system-level error message, erasing the original user input
if (hookResult.blockingError) {
const blockingMessage = getUserPromptSubmitHookBlockingMessage(
hookResult.blockingError,
)
return {
messages: [
// TODO: Make this an attachment message
createSystemMessage(
`${blockingMessage}\n\nOriginal prompt: ${input}`,
'warning',
),
],
shouldQuery: false,
allowedTools: result.allowedTools,
}
}
// If preventContinuation is set, stop processing but keep the original
// prompt in context.
if (hookResult.preventContinuation) {
const message = hookResult.stopReason
? `Operation stopped by hook: ${hookResult.stopReason}`
: 'Operation stopped by hook'
result.messages.push(
createUserMessage({
content: message,
}),
)
result.shouldQuery = false
return result
}
Hidden Invariants & Failure Modes
Three Traps in
utils/processUserInput/:
blockingErrorerases vs.preventContinuationpreserves:blockingErrorwipesresult.messagesinto a UI-onlywarningSystemMessage(note${input}atutils/processUserInput/processUserInput.ts:202stringifies arrays as[object Object]);preventContinuationretains the prompt and appends a stop message.- Unescaped
<persisted-output>:utils/processUserInput/processBashCommand.tsx:102-106skipsescapeXml()onmapped.content(escaping onlystderrand raw fallbackdata.stdout) so disk-spilled<persisted-output>tags aren't broken into<persisted-output>.- Snapshot absences: Type-only imports
constants/querySource.js(utils/processUserInput/processUserInput.ts:8),types/message.js(utils/processUserInput/processUserInput.ts:20-27), andtypes/tools.js(utils/processUserInput/processBashCommand.tsx:8,ShellProgressobserved incomponents/BashModeProgress.tsx:34) are absent from this checkout.
4. Dual Message Views & The Two-Stage API Boundary
The Problem
One Message[] array must serve Ink (1-block rows, spinners, retry rows, compaction dividers) and the Anthropic API (alternating user/assistant turns, zero UI chrome), while ~10 internal callers—compaction (services/compact/compact.ts:1293), VCR (services/vcr.ts:96), analytics, and query.ts:855—need normalized messages without model, beta, or cache context.
Snapshot Absence of
types/message.ts&types/utils.ts: Bothtypes/message.ts(utils/messages.ts:41-73, 31 types) andtypes/utils.ts(utils/messages.ts:121,DeepImmutable) are absent from this snapshot. Observed viautils/messages.tsfactories: 5 core union variants (user,assistant,attachment,progress, andsystemwith 14 constructed subtypes atutils/messages.ts:4335-4603) plus 4 stream/SDK envelopes (tombstone,tool_use_summary,stream_event,stream_request_startatutils/messages.ts:2951-2962).
The Real Implementation
utils/messages.ts projects the transcript in opposite directions:
| Dimension | UI View (components/Messages.tsx) |
API View (normalizeMessagesForAPI → Stage 2) |
|---|---|---|
| Granularity | Splits 1 → N via normalizeMessages (utils/messages.ts:741-823) with deterministic deriveUUID (utils/messages.ts:725-728) so Ink never remounts rows. |
Merges N → 1 via mergeUserMessages (utils/messages.ts:2411-2449) and mergeAssistantMessages (utils/messages.ts:2389-2400). |
| Non-chat records | Keeps attachment, progress, system; reorderMessagesInUI (utils/messages.ts:855-1026) groups tools/hooks; shouldShowUserMessage (utils/messages.ts:4658-4677) hides isMeta except origin.kind === 'channel' (feature('KAIROS') || feature('KAIROS_CHANNELS')). |
Drops isVirtual, progress, isSyntheticApiErrorMessage, and 13 of 14 system subtypes; promotes system/local_command to UserMessage; bubbles and expands attachment into isMeta UserMessage[]. |
| Scale / Compaction | buildMessageLookups (utils/messages.ts:1170-1340) precomputes O(1) Maps; components/Messages.tsx:496-498 passes { includeSnipped: true } to getMessagesAfterCompactBoundary. |
query.ts:365 slices via getMessagesAfterCompactBoundary (utils/messages.ts:4643-4656, absent services/compact/snipProjection.js under feature('HISTORY_SNIP')). |
(Notably, filterUnresolvedToolUses at utils/messages.ts:2796-2800 skips normalizeMessages because derived UUIDs would defeat JSONL resume deduplication.)
The Two-Stage Normalization Boundary: Stage 1 (
normalizeMessagesForAPI,utils/messages.ts:1989-2370) is model-agnostic and returns internal(UserMessage | AssistantMessage)[]retaininguuid,isMeta,timestamp, andmessage.id—not wireMessageParam[]. Stage 2 lives inservices/api/claude.ts(Chapter 05): right after callingnormalizeMessagesForAPI(services/api/claude.ts:1266-1315), it runs model-aware!useToolSearchre-stripping, callsensureToolResultPairing(services/api/claude.ts:1301— outsidenormalizeMessagesForAPI), strips advisor blocks withoutADVISOR_BETA_HEADER, and caps media at100(API_MAX_MEDIA_PER_REQUEST). Finally,addCacheBreakpoints(services/api/claude.ts:3063-3211) emits wireMessageParam[]viauserMessageToMessageParamandassistantMessageToMessageParam(services/api/claude.ts:588-674).
Why UI-only system messages vanish here: First, reorderAttachmentsForAPI (utils/messages.ts:1481-1527) bubbles attachments upward in O(N) until hitting an assistant or tool_result user message, and isVirtual records are dropped (utils/messages.ts:1999-2001). Second, stripTargets (utils/messages.ts:2004-2054) scans isSyntheticApiErrorMessage (utils/messages.ts:321-329, model === '<synthetic>') for 5 PDF/image errors, tagging the preceding isMeta user message to strip poisoned blocks. Only after compact_boundary has been sliced (query.ts:365) and stripTargets has read synthetic errors—yet before role merging—does the filter run:
// utils/messages.ts:2056-2075
const result: (UserMessage | AssistantMessage)[] = []
reorderedMessages
.filter(
(
_,
): _ is
| UserMessage
| AssistantMessage
| AttachmentMessage
| SystemLocalCommandMessage => {
if (
_.type === 'progress' ||
(_.type === 'system' && !isSystemLocalCommandMessage(_)) ||
isSyntheticApiErrorMessage(_)
) {
return false
}
return true
},
)
Thirteen of fourteen system subtypes (compact_boundary, microcompact_boundary, api_error, turn_duration, stop_hook_summary, api_metrics, informational, etc.) are dropped; only subtype === 'local_command' (utils/messages.ts:1529-1533) promotes to a UserMessage (utils/messages.ts:2078-2093). Stripping earlier breaks compaction and error recovery; stripping later corrupts role merging.
Inside the main switch (utils/messages.ts:2076-2293) and post-passes (utils/messages.ts:2301-2367):
- User merging (
mergeUserMessages,utils/messages.ts:2411-2449): Stripstool_referenceblocks viaisToolSearchEnabledOptimistic(), appliesstripTargets, and merges consecutive user turns (required by Bedrock). It hoiststool_resultblocks first (hoistToolResults,utils/messages.ts:2470-2483), appends\nontoa's tail (joinTextAtSeam,utils/messages.ts:2505-2515) sob's leading<system-reminder>survives, and preserves the non-metauuid(a.isMeta ? b.uuid : a.uuid,utils/messages.ts:2443). Insideif (feature('HISTORY_SNIP'))whenisSnipRuntimeEnabled()is active (utils/messages.ts:2414-2438), an earlier branch preserves the same non-metauuid(utils/messages.ts:2429) while narrowingisMetatoa.isMeta && b.isMeta ? true : undefinedso mixed real-user turns still receive[id:]tags. - Assistant reassembly: Canonicalizes tool names, calls
normalizeToolInputForAPI(utils/api.ts:685-718, stripping injectedExitPlanModeV2and legacyFileEditToolfields), dropscallerwhen tool search is off, and walks backward across interleavedtool_results and different-idteammate assistants (utils/messages.ts:2250-2264) to merge split streaming chunks sharingmessage.id. - Attachment expansion (
normalizeAttachmentForAPI,utils/messages.ts:3453-4286): Expands runtime attachments intoisMetaUserMessage[]—with gated branches forfeature('EXPERIMENTAL_SKILL_SEARCH')(utils/messages.ts:3506),feature('HISTORY_SNIP')(context_efficiencyatutils/messages.ts:4149, requiring absentservices/compact/snipCompact.js), andprocess.env.CLAUDE_CODE_VERIFY_PLAN === 'true'(utils/messages.ts:4244)—then folds them into the preceding user turn viamergeUserMessagesAndToolResults(utils/messages.ts:2372-2387). - The
#21049smooshing scar: Leaving<system-reminder>text as siblings after atool_resulttaught models premature 3-tokenend_turnstops (#21049,utils/messages.ts:2605-2609). When Statsig/GrowthBook gatetengu_chair_sermonis off, a legacy ungated path still smooshes all-text siblings into string-contenttool_results (utils/messages.ts:2615-2628). Enablingtengu_chair_sermonturns on universal smooshing (images, documents, and search results),ensureSystemReminderWrap(utils/messages.ts:2273-2277), and post-passsmooshSystemReminderSiblings(utils/messages.ts:2334-2338). Separately,tengu_toolref_defer_j8m(utils/messages.ts:2160,utils/messages.ts:2301-2305) relocates siblings offtool_referencemessages (relocateToolReferenceSiblings) instead of injecting the fallback'Tool loaded.'turn boundary. - Final Stage 1 guards:
sanitizeErrorToolResultContent(utils/messages.ts:2343, unconditional) strips non-text blocks fromis_error: truetool results;feature('HISTORY_SNIP') && process.env.NODE_ENV !== 'test'appends[id:...]tags viaisSnipRuntimeEnabled()from absentservices/compact/snipCompact.js(utils/messages.ts:2351-2364); andvalidateImagesForAPI(utils/messages.ts:2367) enforces the 5 MB base64 cap.
Hidden Invariants & Failure Modes
- Cache & VCR protection: Inbound
normalizeContentFromAPI(utils/messages.ts:2721-2730) leaves whitespace text unmodified so prompt-cache hashes hold (logging unparseable JSON only whenprocess.env.USER_TYPE === 'ant'atutils/messages.ts:2687). Outbound Stage 2userMessageToMessageParam(services/api/claude.ts:622-630) clones arraycontentsocache_editssplicing (feature('CACHED_MICROCOMPACT')) never mutates caller arrays. Bothtengu_chair_sermonandHISTORY_SNIPguard against changing VCR fixture hashes.
5. Wire-Safety Invariants: Thinking Blocks & Tool Pairing
The Problem
Interrupted streams, compaction slices, /resume, /login, and fallback retries can strand thinking shards, leave trailing thinking atop whitespace text, invalidate signatures, duplicate tool_use IDs across turns, or orphan tool_results—all fatal 400 errors.
The Real Implementation
A. Thinking-Block Pass Order & Signature Stripping
Stage 1 runs four assistant-cleanup passes in a strict, non-commutable order:
// utils/messages.ts:2307-2325
// Filter orphaned thinking-only assistant messages (likely introduced by
// compaction slicing away intervening messages between a failed streaming
// response and its retry). Without this, consecutive assistant messages with
// mismatched thinking block signatures cause API 400 errors.
const withFilteredOrphans = filterOrphanedThinkingOnlyMessages(relocated)
// Order matters: strip trailing thinking first, THEN filter whitespace-only
// messages. The reverse order has a bug: a message like [text("\n\n"), thinking("...")]
// survives the whitespace filter (has a non-text block), then thinking stripping
// removes the thinking block, leaving [text("\n\n")] — which the API rejects.
//
// These multi-pass normalizations are inherently fragile — each pass can create
// conditions a prior pass was meant to handle. Consider unifying into a single
// pass that cleans content, then validates in one shot.
const withFilteredThinking =
filterTrailingThinkingFromLastAssistant(withFilteredOrphans)
const withFilteredWhitespace =
filterWhitespaceOnlyAssistantMessages(withFilteredThinking)
const withNonEmpty = ensureNonEmptyAssistantContent(withFilteredWhitespace)
filterOrphanedThinkingOnlyMessages(utils/messages.ts:4991-5058) drops thinking-only shards whosemessage.idhas no non-thinking sibling.filterTrailingThinkingFromLastAssistant(utils/messages.ts:4781-4828) strips trailing thinking blocks frommessages.at(-1)(substituting'[No message content]'if all blocks were thinking).filterWhitespaceOnlyAssistantMessages(utils/messages.ts:4869-4919) drops whitespace-only assistants and re-merges newly adjacentuserturns—catching[text("\n\n")]exposed by step 2.ensureNonEmptyAssistantContent(utils/messages.ts:4933-4977) patches non-finalcontent: []toNO_CONTENT_MESSAGE('(no content)'), leaving a final empty assistant legal for prefill.
Across credential or model changes, stripSignatureBlocks (utils/messages.ts:5066-5099) strips thinking, redacted_thinking, and—behind feature('CONNECTOR_TEXT')—isConnectorTextBlock (from absent types/connectorText.js), stripping thinking-only shards to content: [] so mergeAssistantMessages absorbs them. It runs unconditionally on /login (commands/login/login.tsx:24) and behind process.env.USER_TYPE === 'ant' on FallbackTriggeredError (query.ts:927-929). In Stage 2, assistantMessageToMessageParam (services/api/claude.ts:658-661) also refuses to attach cache_control to trailing thinking, redacted_thinking, or CONNECTOR_TEXT blocks.
B. Bidirectional ensureToolResultPairing (utils/messages.ts:5133-5460)
Because 1-to-1 (assistant, user) adjacency exists only after Stage 1 merges streaming chunks and consecutive user turns, ensureToolResultPairing runs in Stage 2 at services/api/claude.ts:1301:
// utils/messages.ts:5301-5443
// Find missing tool_result IDs (forward direction: tool_use without tool_result)
const toolUseIdSet = new Set(toolUseIds)
const missingIds = toolUseIds.filter(id => !existingToolResultIds.has(id))
// Find orphaned tool_result IDs (reverse direction: tool_result without tool_use)
const orphanedIds = [...existingToolResultIds].filter(
id => !toolUseIdSet.has(id),
)
// ...
// Build synthetic error tool_result blocks for missing IDs
const syntheticBlocks: ToolResultBlockParam[] = missingIds.map(id => ({
type: 'tool_result' as const,
tool_use_id: id,
content: SYNTHETIC_TOOL_RESULT_PLACEHOLDER,
is_error: true,
}))
// ...
if (getStrictToolResultPairing()) {
throw new Error(
`ensureToolResultPairing: tool_use/tool_result pairing mismatch detected (strict mode). ` +
`Refusing to repair — would inject synthetic placeholders into model context. ` +
`Message structure: ${messageTypes.join('; ')}. See inc-4977.`,
)
}
Six invariants are enforced in one forward pass:
- No leading
tool_result(utils/messages.ts:5152-5203): Stripstool_resultblocks from anyusermessage not preceded by anassistant, substituting'[Orphaned tool result removed due to conversation resume]'at index 0 so requests open withuser. - Cross-message
tool_usededup (utils/messages.ts:5147,utils/messages.ts:5225-5234): TracksallSeenToolUseIdsglobally across turns (CC-1212"tool_use ids must be unique"). - Same-message server/MCP pairing (
utils/messages.ts:5205-5211,utils/messages.ts:5235-5243): Stripsserver_tool_use/mcp_tool_uselacking a matching*_tool_resultin the same assistant message. - Non-empty assistant fallback (
utils/messages.ts:5248-5256): Inserts'[Tool use interrupted]'if stripping empties content. - Bidirectional repair + role alternation (
utils/messages.ts:5267-5399): Prependsis_error: trueSYNTHETIC_TOOL_RESULT_PLACEHOLDER('[Tool result missing due to internal error]',utils/messages.ts:246-247) for forwardmissingIds, strips reverseorphanedIdsand duplicatetool_results, and replaces an emptiednextMsgwithcreateUserMessage({ content: NO_CONTENT_MESSAGE, isMeta: true })(utils/messages.ts:5382-5387) to preserve role alternation. - HFI strict mode (
utils/messages.ts:5437-5456): Interactive turns repair and logtengu_tool_result_pairing_repaired; HFI enablesgetStrictToolResultPairing()(bootstrap/state.ts:1093-1095, defaultfalse) to throw citinginc-4977instead of tainting trajectories.
Hidden Invariants & Failure Modes
- Because
ensureToolResultPairingruns after Stage 1, prependingsyntheticBlockscan create a[tool_result, <system-reminder>]sibling Stage 1 never saw—soutils/messages.ts:5370-5374re-runssmooshSystemReminderSiblingsonpatchedNextwhentengu_chair_sermonis enabled.
Putting Layer 1 Together
Keyboard / SDK ContentBlockParam[] / Pasted Images / IDE / Bridge
│
▼
processUserInput() (utils/processUserInput/processUserInput.ts)
├─ resize inline & pasted images + storeImages() + imageMetadataTexts
├─ bridgeOrigin (isBridgeSafeCommand) + ULTRAPLAN rewrite (gated)
├─ getAttachmentMessages() (1s timeout; skipped for /slash in prompt)
├─ mode==='bash' -> processBashCommand.tsx (shouldQuery: false)
├─ '/slash' -> parseSlashCommand + processSlashCommand.tsx
│ [local/forked: false | local-jsx: opt-in | prompt: true]
├─ plain text -> processTextPrompt.ts (shouldQuery: true)
└─ if (shouldQuery) -> executeUserPromptSubmitHooks() (10k cap)
│
▼
Shared Message[] Transcript (user, assistant, attachment, progress, system)
├──> UI View : normalizeMessages (1->N, deriveUUID) + reorderMessagesInUI
└──> Model View:
[Pre] getMessagesAfterCompactBoundary + prependUserContext (Ch 04)
[Stg1] normalizeMessagesForAPI() (utils/messages.ts:1989-2370)
├─ reorderAttachmentsForAPI + drop isVirtual
├─ stripTargets + drop progress/UI system/synth errors
├─ promote local_command + merge user & split assistants
├─ filterOrphaned -> filterTrailing -> filterWhitespace
│ -> ensureNonEmpty + smoosh + sanitizeErrorToolResult
└─ returns internal (UserMessage | AssistantMessage)[]
[Stg2] services/api/claude.ts (Ch 05 seam)
├─ !useToolSearch strip + ensureToolResultPairing()
└─ stripAdvisor/media(100) + addCacheBreakpoints -> wire
How This Snaps Into the Next Layer
Layer 1 (Tool, Command, processUserInput, Stage 1 normalization) is complete and snaps directly into Layer 2, starting with Chapter 04:
- Skill inventory → dynamic prompt:
constants/prompts.ts:458awaitsgetSkillToolCommands(cwd)(commands.ts:563) insidegetSystemPrompt()to populate the post-boundarysession_guidancesection (constants/prompts.ts:491-494). - Split context snapshots →
isMetauser message & system prompt:context.tssplits cached snapshots acrossgetUserContext()(CLAUDE.md+currentDate,context.ts:155-189) andgetSystemContext()(git status,context.ts:116-150, appended to the system prompt viaappendSystemContextatutils/api.ts:437-447).prependUserContext(utils/api.ts:449-474, invoked atquery.ts:660, no-op whenprocess.env.NODE_ENV === 'test'atutils/api.ts:453) wrapsgetUserContext()viacreateUserMessage({ ..., isMeta: true })so Stage 1 merges it with the first user turn. - Reciprocal tool-input cleanup:
utils/messages.ts:2215calls back intoutils/api.ts:685-718(normalizeToolInputForAPI) to strip runtime-injected fields before API serialization. - Stage 2 forward seam (Chapter 05):
services/api/claude.ts:588-674,services/api/claude.ts:1266-1315,services/api/claude.ts:1701-1709, andservices/api/claude.ts:3063-3211runensureToolResultPairing,addCacheBreakpoints,userMessageToMessageParam, andassistantMessageToMessageParamto emit cached wireMessageParam[]payloads.
Reading Exercises & Code Checkpoints
-
Trace Remote Control bridge safety from registry to input router:
rg -n "isBridgeSafeCommand|BRIDGE_SAFE_COMMANDS|REMOTE_SAFE_COMMANDS" commands.ts utils/processUserInput/processUserInput.tsWhy does
isBridgeSafeCommand()automatically allow everypromptcommand while requiring an explicit allowlist forlocalcommands and blocking everylocal-jsxcommand? -
Follow the Stage 1 normalization and Stage 2 tool-pairing boundary:
rg -n "normalizeMessagesForAPI|reorderAttachmentsForAPI|ensureToolResultPairing|filterTrailingThinkingFromLastAssistant" utils/messages.ts services/api/claude.tsVerify that
ensureToolResultPairing()runs atservices/api/claude.ts:1301rather than insidenormalizeMessagesForAPI(). Why must same-idassistant merging and model-aware tool-search stripping finish before pairing runs? -
Audit input-pipeline gates, hook truncation, and
isMetaimage metadata:rg -n "MAX_HOOK_OUTPUT_LENGTH|executeUserPromptSubmitHooks|ULTRAPLAN|addImageMetadataMessage" utils/processUserInput/Which early-return branch inside
processUserInputBase()bypassesaddImageMetadataMessage(), and why doUserPromptSubmithooks never execute for!bash commands? -
Inspect the Chapter 03 → Chapter 04 and Stage 2 (Chapter 05) seams:
rg -n "getSkillToolCommands|prependUserContext|userMessageToMessageParam|assistantMessageToMessageParam" constants/prompts.ts utils/api.ts services/api/claude.tsNotice that
prependUserContext()(utils/api.ts:449-474) constructs aUserMessagewithisMeta: truewhileuserMessageToMessageParam()(services/api/claude.ts:588-631) strips internal metadata into an Anthropic SDKMessageParam. Where in the pipeline doesisMetastop mattering?
Manuscript chapters/03-command-and-message-contracts.md from AtlantisPleb/cc at 5aa286a, synced Nov 24, 2026.