How muyinjiangxue/llm works
1. What this repository is
- This project is a CLI agent harness called Claw Code. The README says the canonical implementation is the Rust workspace under rust/, while the checked-in src/ tree is a Python companion/reference workspace used for audits, parity checks, and supporting tooling.
2. Top-level architecture
- The system is organized around a command-line agent loop: parse user intent, initialize runtime state, coordinate model/tool execution, persist session history, and present output.
- The main runtime boundary is the CLI binary, with supporting subsystems for sessions, tools, permissions, queries, remote access, and interactive UI modes.
3. Primary execution path
- User entry starts from the CLI entrypoints (src/main.py, src/entrypoints, src/cli, src/commands.py).
- From there, the app flows into bootstrap/system initialization, then into a coordinator/executor layer that decides how to run prompts, tools, and interactive modes.
- This is the central path an architecture diagram should show: input → initialization → orchestration → tool/model interaction → output/session update.
4. Agent orchestration layer
- The coordinator, assistant, task, tasks, command_graph, and execution_registry modules suggest a graph- or registry-based orchestration model for agent actions.
- This layer is where higher-level behavior lives: selecting commands, managing task state, sequencing tool calls, and controlling the agent loop rather than directly doing I/O.
5. Model and query integration
- query.py, query_engine.py, QueryEngine.py, models.py, tool_pool.py, tools.py, and Tool.py indicate a dedicated model/tool mediation boundary.
- The repo appears to centralize request shaping, tool selection, and response handling here, which is the part that would connect to external model APIs such as Anthropic/OpenAI-style API keys mentioned in the README.
6. Session, history, and local state
- session_store.py, history.py, transcript.py, state/, memdir/, projectOnboardingState.py, and context.py form the persistence and recall layer.
- This is the part responsible for keeping conversation/session context, transcript history, local working state, and onboarding/memory artifacts that the agent needs across runs.
7. Permissions, hooks, and policy boundaries
- permissions.py, hooks/, costHook.py, cost_tracker.py, and prefetch.py suggest a governance layer around execution.
- In diagram terms, this layer sits between orchestration and side effects, enforcing what can run, what can be fetched, and how usage/cost is tracked.
8. Tooling and external integrations
- The repo includes dedicated subsystems for remote execution and bridging: remote/, remote_runtime.py, bridge/, upstreamproxy/, server/, and services/.
- These modules likely separate local agent logic from remote/proxy/server-backed capabilities, letting the CLI interact with services or upstream systems without mixing transport concerns into core orchestration.
9. Interactive and alternative UI surfaces
- replLauncher.py, dialogLaunchers.py, interactiveHelpers.py, ink.py, screens/, outputStyles/, keybindings/, vim/, and voice/ point to multiple interaction front-ends beyond a plain prompt.
- These are presentation/adaptation layers around the same core agent runtime, not separate business logic.
10. Bootstrap, startup, and native/platform support
- bootstrap/, bootstrap_graph.py, system_init.py, deferred_init.py, runtime.py, direct_modes.py, and native_ts/ represent startup wiring and platform-specific setup.
- This is the boundary where the app decides how to initialize subsystems, resolve runtime mode, and prepare the environment before the agent loop begins.
11. Reference data and parity/audit support
- reference_data/, parity_audit.py, migrations/, and _archive_helper.py support snapshotting and compatibility checks.
- The many reference_data/subsystems/*.json files indicate the Python tree is also used as an inventory of subsystem shapes for parity with the Rust implementation and for architectural auditing.
12. Packaging, build, and delivery
- The repo includes Containerfile, install.sh, GitHub CI workflows, and docs such as docs/container.md.
- Operationally, this is a build-from-source project with container-first support, but the README makes clear the Rust workspace is the production surface and the Python tree is ancillary.
13. How to draw the architecture
- A useful diagram should show:
- CLI entrypoint
- bootstrap/system init
- orchestration/coordinator
- model/query + tools
- session/state/history
- policy/hooks/cost control
- remote/server/bridge integrations
- interactive UI adapters
- The key boundary is that core agent logic is separated from runtime adapters and persistence, with the Rust workspace acting as the primary implementation and src/ providing companion/reference structure.
Main entry points: coordinator Coordinator · bridge Bridge · entrypoints Entrypoints · cli CLI layer
Components 28
Entry
CLI startup and command dispatch
- CLI main · Top-level process start and handoff into command handling
src/main.py - Entrypoints · Entry surface for CLI invocations and modes
src/entrypoints - CLI layer · Command parsing and user-facing command wiring
src/cli - Commands · Command definitions and routing into the runtime
src/commands.py
Runtime
Initialization, orchestration, and agent execution
- System init · Bootstraps runtime state and environment setup
src/system_init.py - Bootstrap · Startup graph and initialization coordination
src/bootstrap - Runtime · Resolves execution mode and overall runtime behavior
src/runtime.py - Coordinator · Central agent loop orchestration and execution control
src/coordinator - Assistant · Primary agent behavior and task-facing logic
src/assistant - Tasks · Task lifecycle and sequencing support
src/tasks.py - Command graph · Represents command and action relationships for execution
src/command_graph.py - Query engine · Model-facing request shaping and response mediation
src/query_engine.py - Tools · Tool definitions and tool invocation surface
src/tools.py - Tool pool · Tool discovery and selection boundary
src/tool_pool.py
State
Session history, memory, and local persistence
- Session store · Session checkpointing and local run state
src/session_store.py - History · Conversation history and prior-turn recall
src/history.py - Context · Shared working context for the agent loop
src/context.py
Policy
Permissions, hooks, and cost controls
- Permissions · Execution allow/deny and user authorization boundary
src/permissions.py - Hooks · Extensibility and side-effect interception points
src/hooks - Cost tracker · Usage and cost accounting across runs
src/cost_tracker.py
Integrations
Remote, server, and proxy boundaries
- Remote runtime · Remote execution and runtime bridging layer
src/remote_runtime.py - Bridge · Adapter boundary between local agent logic and external services
src/bridge - Server · Local or remote server-side capabilities exposed to the CLI
src/server - Upstream proxy · Proxy layer for upstream or mediated external access
src/upstreamproxy
UI
Interactive front-ends and presentation
Connections 27
- CLI main → Entrypoints: start · Process entry routes into CLI entry surfaces
- Entrypoints → CLI layer: dispatch · Entrypoints dispatch into the command layer
- CLI layer → Commands: route · CLI commands map user intent into executable actions
- Commands → Bootstrap: initialize · Command execution begins by wiring startup state
- Bootstrap → System init: setup · Bootstrap prepares the system initialization path
- System init → Runtime: select mode · Initialization resolves the active runtime mode
- Runtime → Coordinator: run loop · Runtime hands control to the agent orchestrator
- Coordinator → Assistant: delegate · Coordinator delegates higher-level agent behavior
- Assistant → Tasks: sequence · Assistant manages task lifecycle and progression
- Tasks → Command graph: plan · Task execution is represented through a command graph
- Coordinator → Query engine: model calls · Orchestration sends work to the model/query boundary
- Query engine → Tools: invoke · Model responses can trigger tool execution
- Tools → Tool pool: resolve · Tool execution is mediated by tool selection
- Coordinator → Permissions: check · Execution is gated by permissions policy
- Coordinator → Hooks: intercept · Hooks observe and modify execution boundaries
- Hooks → Cost tracker: meter · Governance layer tracks usage and cost impacts
- Coordinator → Session store: persist · Agent progress is written to session storage
- Session store → History: record · Session state retains transcript history
- History → Context: load · Past turns inform current working context
- Coordinator → Remote runtime: bridge · Orchestration can hand off to remote execution
- Remote runtime → Bridge: adapt · Remote access is adapted through bridge modules
- Bridge → Server: serve · Bridge connects local logic to server capabilities
- Bridge → Upstream proxy: proxy · Bridge may route through upstream proxies
- Coordinator → REPL/UI: interactive · Interactive modes are surfaced through the REPL UI
- REPL/UI → Dialogs: launch · REPL can open dialog-based interaction flows
- Dialogs → Ink UI: render · Dialogs use terminal presentation helpers
- Ink UI → Screens: show · Presentation renders screen-based views