InkdownInkdown
Start writing

Claude-Code

62 filesยท4 subfolders

Shared Workspace

Claude-Code
codex

16-stop-hooks

Shared from "Claude-Code" on Inkdown

Stop Hooks & Post-Turn Pipeline

What happens after the AI finishes a response.


Overview

query/stopHooks.ts runs after every AI turn completes. It's the post-turn pipeline that triggers background tasks, checks for hook conditions, and decides whether to continue.


Hook Types

Stop Hooks

Run when the model stops (no more tool_use blocks):

TypeScript
0000_start_here_index_and_recommended_reading_order.md
0100_project_overview_tech_stack_runtime_modes_and_folder_map.md
0200_startup_flow_entry_points_and_cold_start_sequence.md
0300_codebase_modules_layers_state_models_and_schemas.md
0400_system_architecture_and_design_rationale.md
0500_interactive_repl_request_flow_end_to_end.md
0600_headless_sdk_and_print_mode_request_flow_end_to_end.md
0700_mcp_integration_connection_and_tool_call_flow.md
0800_external_services_sdks_storage_and_local_dependencies.md
0900_environment_variables_settings_feature_flags_and_failure_modes.md
1000_non_obvious_patterns_gotchas_and_debugging_traps.md
1100_full_codebase_file_inventory_grouped_by_directory.md
kimi
00-overview.md
01-entrypoints.md
02-state-management.md
03-query-system.md
04-tools-system.md
05-tasks-system.md
06-ui-components.md
07-bridge-remote.md
08-services.md
09-skills-plugins.md
10-commands.md
11-testing-architecture.md
12-permission-system.md
13-build-system.md
14-ink-internals.md
15-git-internals.md
16-context-compaction.md
17-vim-mode.md
18-mailbox-notifications.md
19-session-persistence.md
20-hooks-system.md
21-error-recovery.md
README.md
qwen
00-overview.md
01-entry-points.md
02-query-engine.md
03-tools-and-tasks.md
04-commands-and-skills.md
05-state-management.md
06-ink-rendering.md
07-bridge-remote.md
08-mcp-services.md
09-services-overview.md
10-multi-agent.md
11-system-prompt-constants.md
12-tool-interface.md
13-memory-system.md
14-buddy-companion.md
15-keybindings.md
16-stop-hooks.md
17-vim-mode.md
18-upstreamproxy.md
19-cost-tracking-history.md
20-contexts-styles-onboarding.md
21-hooks.md
22-screens.md
tweets-explain
claude-code-memory-analysis.md
compact
memory-system
agentic-architecture
// Check if a stop hook should fire const hookResult = await executeStopHook(messages, systemPrompt, toolUseContext) // If hook returns "retry", the query loop continues if (hookResult.retry) { state.transition = { reason: 'stop_hook_retry' } continue }

Stop hooks are used for:

  • "Should I dream?" โ€” suggesting background processing
  • "PR ready for review" โ€” detecting completed work
  • Other conditional post-turn actions
TaskCompleted Hooks

Fire when a task completes:

TypeScript
// Notify about completed task
onTaskCompleted(task)
TeammateIdle Hooks

Fire when a teammate agent has been idle:

TypeScript
// Check if teammate has been idle too long
checkTeammateIdle(teammate)

Background Tasks

After hooks, several background tasks are triggered (non-blocking):

Prompt Suggestions
TypeScript
// Suggest next prompts based on conversation state
triggerPromptSuggestion(messages, toolUseContext)
Memory Extraction
TypeScript
// Extract memories from the conversation (EXTRACT_MEMORIES feature)
triggerMemoryExtraction(messages, toolUseContext)
Auto-Dream
TypeScript
// Auto-dream: process conversation in background
triggerAutoDream(messages, toolUseContext)
Job Classification
TypeScript
// Classify the job type (TEMPLATES feature)
triggerJobClassification(messages, toolUseContext)
Computer Use Cleanup
TypeScript
// Clean up computer use state (CHICAGO_MCP feature)
if (feature('CHICAGO_MCP') && !toolUseContext.agentId) {
  await cleanupComputerUseAfterTurn(toolUseContext)
}

Hook Execution Flow

Plain text
Model finishes response (no more tool_use)
    โ”‚
    โ–ผ
executeStopHooks()
    โ”‚
    โ”œโ”€ Check Stop hook conditions
    โ”‚   โ””โ”€ If matched โ†’ execute hook โ†’ maybe retry
    โ”‚
    โ”œโ”€ Fire TaskCompleted hooks
    โ”‚
    โ”œโ”€ Fire TeammateIdle hooks
    โ”‚
    โ–ผ
Background tasks (non-blocking, fire-and-forget)
    โ”œโ”€ Prompt suggestions
    โ”œโ”€ Memory extraction
    โ”œโ”€ Auto-dream
    โ”œโ”€ Job classification
    โ””โ”€ Computer use cleanup
    โ”‚
    โ–ผ
Return to query loop
    โ”œโ”€ If stop hook requested retry โ†’ continue
    โ””โ”€ Otherwise โ†’ return { reason: 'done' }

Stop Hook Retry

When a stop hook requests a retry:

TypeScript
// In query.ts
if (stopHookRetried) {
  state.transition = { reason: 'stop_hook_retry' }
  continue  // Back to the top of the query loop
}

This allows the hook to inject additional messages or context and re-query the model.


Hook Configuration

Hooks are configured via Zod schemas (schemas/hooks.ts):

BashCommandHook
JSON
{
  "event": "Stop",
  "type": "bash",
  "command": "npm test",
  "matcher": { "pattern": "tests?\\s*passed" }
}
PromptHook
JSON
{
  "event": "Stop",
  "type": "prompt",
  "prompt": "Summarize what was accomplished in this session",
  "matcher": { "pattern": "done|complete" }
}
HttpHook
JSON
{
  "event": "Stop",
  "type": "http",
  "url": "https://hooks.example.com/notify",
  "method": "POST",
  "headers": { "Authorization": "Bearer ${API_KEY}" },
  "body": { "message": "${tool_result}" }
}
AgentHook
JSON
{
  "event": "Stop",
  "type": "agent",
  "prompt": "Review the changes made in this session",
  "tools": ["Bash", "FileRead", "Grep"]
}
Hook Matcher

Patterns for triggering hooks:

JSON
{
  "matcher": {
    "pattern": "regex pattern",
    "caseSensitive": false,
    "source": "assistant" | "user" | "tool_result"
  }
}

Hook Schema (schemas/hooks.ts)

Extracted Zod schemas to break import cycles:

TypeScript
const BashCommandHookSchema = z.object({ ... })
const PromptHookSchema = z.object({ ... })
const HttpHookSchema = z.object({ ... })
const AgentHookSchema = z.object({ ... })
const HookMatcherSchema = z.object({ ... })

const HooksSchema = z.record(HookEventSchema, z.array(
  z.discriminatedUnion('type', [
    BashCommandHookSchema,
    PromptHookSchema,
    HttpHookSchema,
    AgentHookSchema,
  ])
))

Files

FilePurpose
src/query/stopHooks.tsPost-turn hook execution
src/query/config.tsQuery config snapshot
src/query/deps.tsDependency injection
src/query/tokenBudget.tsToken budget tracking
src/schemas/hooks.tsZod hook schemas