# CLI Tools Execution Specification ## Table of Contents 1. [Configuration Reference](#configuration-reference) 2. [Tool Selection](#tool-selection) 3. [Prompt Template](#prompt-template) 4. [CLI Execution](#cli-execution) 5. [Execution Configuration](#execution-configuration) 6. [Best Practices](#best-practices) --- ## Configuration Reference ### Configuration File **Path**: `.claude/cli-tools.json` All tool availability, model selection, and routing are defined in this configuration file. ### Configuration Fields | Field | Description | |-------|-------------| | `enabled` | Tool availability status | | `primaryModel` | Default model for the tool | | `secondaryModel` | Fallback model | | `tags` | Capability tags for routing | ### Tool Types | Type | Usage | Capabilities | |------|-------|--------------| | `builtin` | `--tool gemini` | Full (analysis + write tools) | | `cli-wrapper` | `--tool doubao` | Full (analysis + write tools) | | `api-endpoint` | `--tool g25` | **Analysis only** (no file write tools) | > **Note**: `api-endpoint` tools only support analysis and code generation responses. They cannot create, modify, or delete files. --- ## Tool Selection ### Tag-Based Routing Tools are selected based on **tags** defined in the configuration. Use tags to match task requirements to tool capabilities. #### Common Tags | Tag | Use Case | |-----|----------| | `analysis` | Code review, architecture analysis, exploration | | `implementation` | Feature development, bug fixes | | `documentation` | Doc generation, comments | | `testing` | Test creation, coverage analysis | | `refactoring` | Code restructuring | | `security` | Security audits, vulnerability scanning | ### Selection Algorithm ``` 1. Parse task intent → extract required capabilities 2. Load cli-tools.json → get enabled tools with tags 3. Match tags → filter tools supporting required capabilities 4. Select tool → choose by priority (explicit > tag-match > default) 5. Select model → use primaryModel, fallback to secondaryModel ``` ### Selection Decision Tree ``` ┌─ Explicit --tool specified? │ └─→ YES: Use specified tool (validate enabled) │ └─ NO: Tag-based selection ├─ Task requires tags? │ └─→ Match tools with matching tags │ └─→ Multiple matches? Use first enabled │ └─ No tag match? └─→ Use default tool (first enabled in config) ``` ### Command Structure ```bash # Explicit tool selection ccw cli -p "" --tool --mode # Model override ccw cli -p "" --tool --model --mode # Code review (codex only) ccw cli -p "" --tool codex --mode review # Tag-based auto-selection (future) ccw cli -p "" --tags --mode ``` ### Tool Fallback Chain When primary tool fails or is unavailable: 1. Check `secondaryModel` for same tool 2. Try next enabled tool with matching tags 3. Fall back to default enabled tool --- ## Prompt Template ### Universal Prompt Template ```bash # Use --rule to auto-load protocol and template as $PROTO and $TMPL ccw cli -p " PURPOSE: [what] + [why] + [success criteria] + [constraints/scope] TASK: • [step 1: specific action] • [step 2: specific action] • [step 3: specific action] MODE: [analysis|write] CONTEXT: @[file patterns] | Memory: [session/tech/module context] EXPECTED: [deliverable format] + [quality criteria] + [structure requirements] RULES: $PROTO $TMPL | [domain constraints] " --tool --mode --rule ``` ### Intent Capture Checklist (Before CLI Execution) **⚠️ CRITICAL**: Before executing any CLI command, verify these intent dimensions: **Intent Validation Questions**: - [ ] Is the objective specific and measurable? - [ ] Are success criteria defined? - [ ] Is the scope clearly bounded? - [ ] Are constraints and limitations stated? - [ ] Is the expected output format clear? - [ ] Is the action level (read/write) explicit? ### Template Structure Every command MUST include these fields: - **PURPOSE** - Purpose: Goal + motivation + success - Components: What + Why + Success Criteria + Constraints - Bad Example: "Analyze code" - Good Example: "Identify security vulnerabilities in auth module to pass compliance audit; success = all OWASP Top 10 addressed; scope = src/auth/** only" - **TASK** - Purpose: Actionable steps - Components: Specific verbs + targets - Bad Example: "• Review code • Find issues" - Good Example: "• Scan for SQL injection in query builders • Check XSS in template rendering • Verify CSRF token validation" - **MODE** - Purpose: Permission level - Components: analysis / write / auto - Bad Example: (missing) - Good Example: "analysis" or "write" - **CONTEXT** - Purpose: File scope + history - Components: File patterns + Memory - Bad Example: "@**/*" - Good Example: "@src/auth/**/*.ts @shared/utils/security.ts \| Memory: Previous auth refactoring (WFS-001)" - **EXPECTED** - Purpose: Output specification - Components: Format + Quality + Structure - Bad Example: "Report" - Good Example: "Markdown report with: severity levels (Critical/High/Medium/Low), file:line references, remediation code snippets, priority ranking" - **RULES** - Purpose: Protocol + template + constraints - Components: $PROTO + $TMPL + domain rules (variables loaded beforehand) - Bad Example: (missing) - Good Example: "$PROTO $TMPL | Focus on authentication | Ignore test files" (where PROTO and TMPL are pre-loaded variables) ### CONTEXT Configuration **Format**: `CONTEXT: [file patterns] | Memory: [memory context]` #### File Patterns - **`@**/*`**: All files (default) - **`@src/**/*.ts`**: TypeScript in src - **`@../shared/**/*`**: Sibling directory (requires `--includeDirs`) - **`@CLAUDE.md`**: Specific file #### Memory Context Include when building on previous work: ```bash # Cross-task reference Memory: Building on auth refactoring (commit abc123), implementing refresh tokens # Cross-module integration Memory: Integration with auth module, using shared error patterns from @shared/utils/errors.ts ``` **Memory Sources**: - **Related Tasks**: Previous refactoring, extensions, conflict resolution - **Tech Stack Patterns**: Framework conventions, security guidelines - **Cross-Module References**: Integration points, shared utilities, type dependencies #### Pattern Discovery Workflow For complex requirements, discover files BEFORE CLI execution: ```bash # Step 1: Discover files rg "export.*Component" --files-with-matches --type ts # Step 2: Build CONTEXT CONTEXT: @components/Auth.tsx @types/auth.d.ts | Memory: Previous type refactoring # Step 3: Execute CLI ccw cli -p "..." --tool --mode analysis --cd src ``` ### RULES Configuration **使用 `--rule` 选项自动加载模板**: ```bash ccw cli -p "... RULES: \$PROTO \$TMPL | constraints" --tool gemini --mode analysis --rule analysis-review-architecture ``` **`--rule` 工作原理**: 1. 自动从 `~/.claude/workflows/cli-templates/prompts/` 发现模板 2. 根据 `--mode` 自动加载对应 protocol(analysis-protocol.md 或 write-protocol.md) 3. 设置环境变量 `$PROTO`(protocol)和 `$TMPL`(template)供子进程使用 4. 在提示词中用 `$PROTO` 和 `$TMPL` 引用 **模板选择**:从 Task-Template Matrix 选择或使用通用模板: - `universal-rigorous-style` - 精确型任务 - `universal-creative-style` - 探索型任务 ### Mode Protocol References **`--rule` 自动处理 Protocol**: - `--mode analysis` → `$PROTO` = analysis-protocol.md - `--mode write` → `$PROTO` = write-protocol.md **Protocol 映射**: - **`analysis`** 模式 - 权限:只读操作 - 约束:禁止文件创建/修改/删除 - **`write`** 模式 - 权限:创建/修改/删除文件 - 约束:完整工作流执行能力 ### Template System **Base Path**: `~/.claude/workflows/cli-templates/prompts/` **Naming Convention**: `category-function.txt` - 第一段为分类(analysis, development, planning 等) - 第二段为功能描述 **Universal Templates**: - `universal-rigorous-style` - 精确型任务 - `universal-creative-style` - 探索型任务 **Task-Template Matrix**: **Analysis**: - Execution Tracing: `analysis-trace-code-execution` - Bug Diagnosis: `analysis-diagnose-bug-root-cause` - Code Patterns: `analysis-analyze-code-patterns` - Document Analysis: `analysis-analyze-technical-document` - Architecture Review: `analysis-review-architecture` - Code Review: `analysis-review-code-quality` - Performance: `analysis-analyze-performance` - Security: `analysis-assess-security-risks` **Planning**: - Architecture: `planning-plan-architecture-design` - Task Breakdown: `planning-breakdown-task-steps` - Component Design: `planning-design-component-spec` - Migration: `planning-plan-migration-strategy` **Development**: - Feature: `development-implement-feature` - Refactoring: `development-refactor-codebase` - Tests: `development-generate-tests` - UI Component: `development-implement-component-ui` - Debugging: `development-debug-runtime-issues` --- ## CLI Execution ### MODE Options - **`analysis`** - Permission: Read-only - Use For: Code review, architecture analysis, pattern discovery, exploration - Specification: Safe for all tools - **`write`** - Permission: Create/Modify/Delete - Use For: Feature implementation, bug fixes, documentation, code creation, file modifications - Specification: Requires explicit `--mode write` - **`review`** - Permission: Read-only (code review output) - Use For: Git-aware code review of uncommitted changes, branch diffs, specific commits - Specification: **codex only** - uses `codex review` subcommand with `--uncommitted` by default - Tool Behavior: - `codex`: Executes `codex review --uncommitted [prompt]` for structured code review - Other tools (gemini/qwen/claude): Accept mode but no operation change (treated as analysis) ### Command Options - **`--tool `** - Description: Tool from config (e.g., gemini, qwen, codex) - Default: First enabled tool in config - **`--mode `** - Description: **REQUIRED**: analysis, write, review - Default: **NONE** (must specify) - Note: `review` mode triggers `codex review` subcommand for codex tool only - **`--model `** - Description: Model override - Default: Tool's primaryModel from config - **`--cd `** - Description: Working directory - Default: current - **`--includeDirs `** - Description: Additional directories (comma-separated) - Default: none - **`--resume [id]`** - Description: Resume previous session - Default: - - **`--rule