返回 CodeWhale
command-dispatch.md
根目录 / docs / architecture / command-dispatch.md
1 # Command Dispatch Architecture
2
3 **Target branch:** `main`
4 **Related EPIC:** [#2870](https://github.com/codewhale-hq/CodeWhale/issues/2870)
5 **Related issue:** [#2791](https://github.com/codewhale-hq/CodeWhale/issues/2791)
6 **EPIC-002 (Command Single Responsibility Extraction):** Layer 4.x (FEAT-006 through FEAT-008)
7
8 This document records the command-dispatch ownership model after the
9 command-boundary replay landed on `main`, updated through EPIC-002 (command
10 single responsibility extraction). It reflects the final layered ownership:
11 top-level group registration, group-owned command registration, and
12 command-level ownership of metadata and behavior. It is the public reference for the
13 module boundaries, dispatch precedence, and permanent exceptions that remain
14 after the command-boundary refactor.
15
16 ## Dispatch Flow
17
18 `commands::execute()` owns the slash-command dispatch gate. The order is
19 intentional:
20
21 | Step | Source | Behavior |
22 |------|--------|----------|
23 | 0 | `$skill` compatibility | `$name` is resolved as `/skill name` before slash parsing. |
24 | 1 | User commands | `user_registry::try_dispatch()` checks workspace and global markdown commands first, so user commands can shadow built-ins. Workspace commands (`.codewhale`, `.deepseek`, `.claude`, `.cursor`) load only in a trusted workspace and never under the name or alias of a protected built-in — the commands that grant or revoke authority, hold credentials, or undo work, such as `/trust`, `/undo`, `/permissions`, `/mode`, `/config` (`PROTECTED_BUILTINS` in `user_registry.rs`); such a definition is skipped with a load error. |
25 | 2 | Permanent mode compatibility alias | `/jihua` routes through config mode dispatch so it selects Plan mode directly. It remains a registered alias for discovery, bypassing normal `/mode` execution. |
26 | 3 | Built-in registry | `CommandRegistry` resolves group-owned built-in commands by canonical name or alias. |
27 | 4 | Legacy migration hints | Retired commands such as `/set` and `/deepseek` return targeted replacement guidance. |
28 | 5 | Skills fallback | If no command matches, a skill with the same name may run before unknown-command suggestions are shown. |
29
30 ## Module Boundaries
31
32 | Module | Responsibility |
33 |--------|----------------|
34 | `crates/tui/src/commands/mod.rs` | Central dispatch gate, registry initialization, public command lookup helpers, and unknown-command suggestions. |
35 | `crates/tui/src/commands/traits.rs` | Built-in command metadata, trait-backed command objects, command groups, and registry lookup. |
36 | `crates/tui/src/commands/groups/` | Built-in group registration and group-owned command handlers. |
37 | `crates/command-contract/` | Capability declarations, dispatch envelopes and typed host interfaces. |
38 | `crates/commands/` | Extracted portable handlers, including core policy, router, workflow, fleet and lane commands. |
39 | `crates/tui/src/commands/contract/` | TUI adapters that supply each command’s declared capabilities. |
40 | `crates/tui/src/commands/user_registry.rs` | User-command registry boundary: markdown metadata, aliases, hidden entries, validation errors, dispatch state resets, and shadowing behavior. |
41 | `crates/tui/src/commands/discovery.rs` | Shared discovery-shadowing contract consumed by both the command palette and slash completion: canonical-token shadowing, alias-token shadowing, and unshadowed-alias projection. Both discovery surfaces interpret user-command ownership identically through this module. |
42 | `crates/tui/src/commands/user_commands.rs` | Lower-level file scanning, frontmatter parsing, allowed-tools parsing, and template substitution used by the registry. |
43 | `crates/tui/src/tui/command_palette.rs` | Palette entries for built-ins and visible user commands, with user commands shadowing built-ins. |
44 | `crates/tui/src/tui/widgets/mod.rs` | Slash completion, user-command metadata display, and alias-shadowing behavior. |
45
46 ## Built-In Command Groups
47
48 | Group | Scope |
49 |-------|-------|
50 | `core` | Help, model/provider selection, queue, hooks, subagents, links, feedback, voice, and core navigation. |
51 | `config` | Config, settings, status surfaces, mode, theme, trust, logout, and related settings commands. |
52 | `debug` | Token/cost introspection, cache, system/context, diff/edit, undo, and retry. |
53 | `memory` | Persistent memory and notes. |
54 | `plugins` | Read-only bundle discovery/validation plus explicit trust, enable, disable, revoke, and reload lifecycle commands; legacy executable tools remain separate. |
55 | `project` | Project initialization, sharing, LSP, and goal/hunt commands. |
56 | `session` | Rename, save, fork/new/load sessions, compaction, purge, relay, and export. |
57 | `skills` | Skills Manager (`/skills`), text inspect/remote/sync paths, activation (`/skill`), and managed install/update/uninstall/trust. |
58 | `utility` | Attachments, tasks/jobs, MCP, and network. |
59
60 ## User Commands
61
62 User commands are markdown files loaded from these locations in precedence
63 order:
64
65 1. `<workspace>/.codewhale/commands/`
66 2. `<workspace>/.deepseek/commands/`
67 3. `<workspace>/.claude/commands/`
68 4. `<workspace>/.cursor/commands/`
69 5. `~/.codewhale/commands/`
70 6. `~/.deepseek/commands/`
71
72 Supported frontmatter fields:
73
74 | Field | Meaning |
75 |-------|---------|
76 | `name` | Canonical slash-command name. It is normalized without a leading slash and replaces the filename-derived default. |
77 | `description` | Work objective and UI description. |
78 | `usage` | Preferred user-facing invocation syntax shown in the palette and slash completion. |
79 | `arguments` | Argument synopsis and a signal that selection should leave the composer open for input. It does not impose runtime validation. |
80 | `argument-hint` | Backward-compatible palette/completion hint for expected arguments. It remains the display fallback when `usage` is absent. |
81 | `allowed-tools` | Restricts command execution tools. An explicit empty value blocks all tools. |
82 | `pausable` | Marks the command as pause/resume capable. |
83 | `alias` / `aliases` | Additional user-command names. A workspace command's alias cannot claim a protected built-in. |
84 | `hidden` | Hides the command from palette/completion while allowing direct dispatch. |
85
86 The canonical name defaults to the normalized markdown filename. A valid
87 frontmatter `name` replaces that default; the filename is not retained as an
88 implicit alias, so a renamed command must list the old filename under `alias`
89 or `aliases` if both spellings should dispatch. A configured name may include
90 one leading slash for readability, but after normalization it must be one
91 non-empty slash-command token with no whitespace or embedded `/`. An invalid
92 configured name is a recoverable error attached to the filename-derived
93 command, preventing silent fallthrough to a built-in.
94
95 Source precedence is resolved before frontmatter naming: a higher-precedence
96 directory wins when the same filename exists in more than one location. Files
97 inside each directory are ordered by normalized filename. If distinct files
98 then resolve to the same frontmatter `name`, the first file in that stable
99 directory-and-filename order wins and the losing file records a recoverable
100 load error. Aliases cannot replace any canonical user-command name; duplicate
101 aliases also use first-wins order. Errors on a losing duplicate never poison a
102 valid winning definition.
103
104 Presentation metadata has an explicit fallback order: non-empty `usage`, then
105 non-empty legacy `argument-hint`, then non-empty `arguments`. `arguments` and
106 `argument-hint` also cause palette/menu selection to append a space; `usage`
107 does so when it describes more than the bare command name. These fields do not
108 parse, require, or reject invocation arguments. Runtime expansion remains
109 backwards-compatible: `$ARGUMENTS` receives the complete argument tail and
110 `$1`, `$2`, and so on receive whitespace-separated positional values.
111
112 Malformed files remain registered under their resolved name with a
113 dispatch-time error, so they cannot silently fall through to a built-in. Their
114 errors are isolated per file: valid siblings still load, appear, and dispatch.
115 Hidden commands participate in shadowing and remain directly dispatchable, but
116 are removed from palette and slash-completion discovery.
117
118 Explicit `/help <name>` topics and unknown-command typo suggestions resolve
119 through the same user-command precedence as execution. When a user command
120 owns a built-in name or alias, help shows the user command's metadata and typo
121 suggestions point to its canonical name rather than the shadowed built-in.
122
123 Dispatch through `user_registry` resets stale command state before sending the
124 new command body: hunt objective fields, token/time counters, continuation
125 count, allowed tools, pause state, todos, and plan state.
126
127 ## Permanent Exceptions
128
129 | Exception | Rationale |
130 |-----------|-----------|
131 | `/jihua` | Backward-compatible mode alias that predates the group-owned registry. It routes through config mode dispatch to preserve its fixed Plan mode selection. |
132 | `/set` and `/deepseek` migration hints | Retired commands kept only as direct typed guidance. They are excluded from registry and autocomplete. |
133 | `#[allow(clippy::module_inception)]` in matching group modules | Group directories intentionally contain same-named child modules such as `core/core.rs`. |
134 | `user_commands.rs` lower layer | The registry owns runtime behavior, while this module remains the shared filesystem and parser layer. |
135 | `#[cfg(test)]` helpers in `user_commands.rs` | Deferred test migration compatibility while registry-specific tests are added. |
136
137 ## EPIC-002 Completion Status (closed)
138
139 EPIC-002 (Command Single Responsibility Extraction) extracted commands for
140 all 9 command groups through Layer 4.x sublayers. Layer 4.2 (FEAT-008) landed as
141 PR #3706 (merged) and issue #2870 closed on 2026-08-01.
142
143 | Layer | FEAT | Title | Status |
144 |---|---|---|---|
145 | 4 | FEAT-006 | Core, Config, Session, and Debug Command Extraction | Complete |
146 | 4.1 | FEAT-007 | Project, Memory, Skills, Utility, and Plugins Extraction | Complete |
147 | 4.2 | FEAT-008 | Registry Cleanup, Documentation, and Full Validation | Complete |
148
149 ## Replay Status (EPIC-001)
150
151 FEAT-001's group-owned built-in command direction is represented on `main` by
152 the newer trait-backed registry and nested group tree. FEAT-002 is replayed as
153 the dedicated user-command registry boundary. FEAT-003 is replayed as public
154 architecture and PR/issue evidence documentation, updated for the current
155 `main` target instead of the old `release/v0.8.60` branch.
156
156 lines MARKDOWN