InkdownInkdown
Start writing

Claude-Code

62 filesยท4 subfolders

Shared Workspace

Claude-Code
codex

0500_interactive_repl_request_flow_end_to_end

Shared from "Claude-Code" on Inkdown

Critical Flow: Interactive REPL Prompt To Model To Tools To UI

This is the primary request flow for understanding the codebase because it exercises almost every subsystem.

Flow Overview

  1. Process boot and REPL render.
  2. User types a prompt into PromptInput.
  3. Prompt is normalized into messages, attachments, and optional command actions.
  4. Query loop sends the request to the model.
  5. Streamed assistant output is rendered live.
  6. Tool calls are permission-checked and executed.
  7. Tool results feed back into the same turn.
  8. Final messages, costs, file history, and session metadata are persisted.

Every Hop

1. Startup reaches the REPL
  • entrypoints/cli.tsx dispatches into .
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
main.tsx
  • main.tsx runs init() and setup(), loads commands, agents, tools, and config, and mounts Ink.
  • interactiveHelpers.tsx and dialogLaunchers.tsx handle trust and onboarding setup before the REPL becomes the long-lived screen.
  • screens/REPL.tsx is mounted with app state, command list, tool pool, agent definitions, MCP state, and session metadata.
  • 2. Prompt input is captured
    • components/PromptInput/PromptInput.tsx collects the input.
    • utils/handlePromptSubmit.ts coordinates submission from the UI side.
    • The REPL stores transient UI state such as queued commands, paste references, IDE selection, prompt mode, and permission dialog state.
    3. Input is converted into messages and side effects
    • utils/processUserInput/processUserInput.ts is the first important transformation layer.
    • It delegates plain-text prompt work to processTextPrompt.ts.
    • It can parse slash commands, attach pasted content, include IDE selections, run user prompt hooks, and decide whether the input should call the model at all.
    • Why this split exists: not every submission becomes an LLM query. Some become local command execution, local rendering, or hook-blocked actions.
    4. Query execution begins
    • screens/REPL.tsx calls query() from query.ts.
    • query.ts normalizes messages, appends system and user context, inserts attachment messages, manages compact and retry state, and tracks turn budget.
    5. The API request is built and streamed
    • services/api/claude.ts converts tools into API schemas, chooses provider, model, betas, and thinking options, applies prompt-caching logic, and opens the provider stream.
    • Stream events are normalized into internal message objects by helpers such as utils/messages.ts.
    • screens/REPL.tsx incrementally renders assistant text, thinking state, progress, and tool boundaries as they arrive.
    6. Tool calls are detected and scheduled
    • When the assistant emits tool-use blocks, query.ts routes them into either StreamingToolExecutor or the batch path in services/tools/toolOrchestration.ts.
    • StreamingToolExecutor preserves ordering while tools are still arriving in the stream.
    • toolOrchestration.ts partitions tool calls into concurrency-safe read-only batches versus serial exclusive batches.
    7. Each tool call is permission-checked
    • services/tools/toolExecution.ts drives each individual tool use.
    • It calls the canUseTool function produced by hooks/useCanUseTool.tsx.
    • useCanUseTool first asks utils/permissions/permissions.ts and hasPermissionsToUseTool() for the baseline decision.
    • Depending on the result, the system can auto-allow, auto-deny, wait for coordinator or worker permissions, use classifier results, or open an interactive permission dialog.
    8. Tool implementation runs
    • tools.ts provided the concrete tool registry earlier in startup.
    • runToolUse() in toolExecution.ts resolves the tool by name, validates its schema, emits progress, runs hooks, executes the tool, and converts the result into user-visible and model-visible message blocks.
    • Tool implementations live in tools/*. Common families include shell and file tools, agent and task tools, MCP tool proxies, web fetch and search, plan tools, worktree tools, brief tools, and todo tools.
    9. Tool results re-enter the same turn
    • Tool result messages are appended to the running conversation.
    • query.ts continues the turn if the model still has work to do.
    • Compacting, stop hooks, and token-budget continuation logic can add more transitions before the turn ends.
    10. Turn state is persisted and surfaced
    • utils/sessionStorage.ts records transcript updates and session metadata.
    • utils/fileHistory.ts and attribution helpers update file history and commit-attribution snapshots.
    • cost-tracker.ts and bootstrap/state.ts capture usage, costs, durations, and telemetry.
    • screens/REPL.tsx updates tasks, notifications, spinner state, footer state, and remote or MCP indicators.

    Where Interactive-Only Bugs Usually Hide

    • UI-only issues: screens/REPL.tsx, components/*, and hooks/*.
    • Permission dialog and approval behavior: hooks/useCanUseTool.tsx, components/permissions/*, and utils/permissions/*.
    • Prompt parsing and slash command oddities: utils/processUserInput/*, commands.ts, and commands/*.
    • Tool scheduling and ordering problems: query.ts, StreamingToolExecutor.ts, toolOrchestration.ts, and toolExecution.ts.