Context: Studying Claude Code source code (backup branch)

How Hook additionalContext Reaches the Model

Data flow

  1. Hook outputs JSON with hookSpecificOutput.additionalContext (string). Parsed in hooks.ts per event type (~line 622 for PreToolUse, 644 for PostToolUse, 625 for UserPromptSubmit, etc.)
  2. Aggregated into additionalContexts[] on AggregatedHookResult (hooks.ts:2787)
  3. Wrapped as attachment message type: 'hook_additional_context' in toolHooks.ts (PreToolUse: 566-578, PostToolUse: 133-142, PostToolUseFailure: 270-279)
  4. Pushed into resultingMessages in toolExecution.ts (PreToolUse at line 846 before tool runs, PostToolUse via hookResults at line 1585 after tool result)
  5. Converted to API message in messages.ts:4117-4128: becomes a createUserMessage with isMeta: true, content wrapped in <system-reminder> tags

What the model sees

<system-reminder>
PreToolUse:Bash hook additional context: [hook's string output]
</system-reminder>

User-role message, hidden from user UI (isMeta: true), fully visible to the API. Same structural privilege as skills.

Message ordering

[assistant message with tool_use block]
  [PreToolUse hook additional context]     <-- before tool runs
  [tool_result block]                      <-- actual tool output
  [PostToolUse hook additional context]    <-- after tool result
  [tool's newMessages (e.g. skill content)]
  [hook_stopped_continuation, if any]

Both pre and post contexts are recent user-role messages wrapped in <system-reminder>, so the model treats them as authoritative injected instructions.