cto-ai-cli 1.3.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/DOCS.md ADDED
@@ -0,0 +1,351 @@
1
+ # CTO — Full Documentation
2
+
3
+ > Complete reference for CLI, MCP server, API server, programmatic API, GitHub Action, and configuration.
4
+
5
+ ## Table of Contents
6
+
7
+ - [CLI Commands](#cli-commands)
8
+ - [MCP Server](#mcp-server)
9
+ - [API Server](#api-server)
10
+ - [Programmatic API](#programmatic-api)
11
+ - [GitHub Action](#github-action)
12
+ - [Configuration](#configuration)
13
+ - [Architecture](#architecture)
14
+
15
+ ---
16
+
17
+ ## CLI Commands
18
+
19
+ ### `cto2 init [path]`
20
+
21
+ Initialize CTO for a project. Creates `.cto/config.yml` and `.cto/policy.yml`.
22
+
23
+ ```bash
24
+ cto2 init # current directory
25
+ cto2 init ./my-project # specific directory
26
+ ```
27
+
28
+ ### `cto2 analyze [path]`
29
+
30
+ Analyze project structure, dependencies, and risk profile.
31
+
32
+ ```bash
33
+ cto2 analyze # full analysis
34
+ cto2 analyze --risk-only # show only risk distribution
35
+ cto2 analyze --graph # show dependency graph (hubs, clusters, orphans)
36
+ ```
37
+
38
+ ### `cto2 interact <task>`
39
+
40
+ Build optimized AI context for a specific task.
41
+
42
+ ```bash
43
+ cto2 interact "refactor the auth middleware"
44
+ cto2 interact "fix the login bug" --explain # show selection decisions
45
+ cto2 interact "review this PR" --pr # PR mode (diff vs main)
46
+ cto2 interact "review this PR" --pr develop # PR mode (diff vs develop)
47
+ cto2 interact "add tests" --output file # save to .cto/prompt-{id}.md
48
+ cto2 interact "add tests" --output clipboard # copy to clipboard
49
+ cto2 interact "add tests" --json # JSON output
50
+ ```
51
+
52
+ ### `cto2 snapshot`
53
+
54
+ Reproducible context snapshots.
55
+
56
+ ```bash
57
+ cto2 snapshot create baseline # save current state
58
+ cto2 snapshot verify baseline # check for drift
59
+ cto2 snapshot compare a b # diff two snapshots
60
+ cto2 snapshot list # list all snapshots
61
+ ```
62
+
63
+ ### `cto2 audit`
64
+
65
+ Tamper-evident audit trail.
66
+
67
+ ```bash
68
+ cto2 audit list # show audit entries
69
+ cto2 audit verify # verify integrity
70
+ cto2 audit purge # remove old entries
71
+ ```
72
+
73
+ ### `cto2 policy`
74
+
75
+ Context selection policies.
76
+
77
+ ```bash
78
+ cto2 policy show
79
+ cto2 policy add no-tests exclude-always --pattern "**/*.test.*" --reason "Skip tests"
80
+ cto2 policy remove no-tests
81
+ cto2 policy toggle no-tests
82
+ cto2 policy validate
83
+ cto2 policy init
84
+ ```
85
+
86
+ ---
87
+
88
+ ## MCP Server
89
+
90
+ CTO exposes 19 tools via the Model Context Protocol.
91
+
92
+ ### Setup
93
+
94
+ **Windsurf** (`~/.codeium/windsurf/mcp_config.json`):
95
+ ```json
96
+ {
97
+ "mcpServers": {
98
+ "cto": { "command": "cto2-mcp" }
99
+ }
100
+ }
101
+ ```
102
+
103
+ **Claude Desktop** (`~/Library/Application Support/Claude/claude_desktop_config.json`):
104
+ ```json
105
+ {
106
+ "mcpServers": {
107
+ "cto": {
108
+ "command": "node",
109
+ "args": ["/path/to/cto-ai-cli/dist/mcp/v2.js"]
110
+ }
111
+ }
112
+ }
113
+ ```
114
+
115
+ ### Tools
116
+
117
+ | Tool | Category | Description |
118
+ |------|----------|-------------|
119
+ | `cto_analyze` | Engine | Full project analysis |
120
+ | `cto_risk_profile` | Engine | Risk factors per file |
121
+ | `cto_select_context` | Engine | Deterministic context selection |
122
+ | `cto_pr_context` | Engine | PR-focused context from git diff |
123
+ | `cto_score` | Engine | Context Score (0-100) |
124
+ | `cto_benchmark` | Engine | CTO vs naive vs random |
125
+ | `cto_quality_benchmark` | Engine | Relevance, completeness, noise metrics |
126
+ | `cto_plan_interaction` | Interact | Full pipeline: analyze → select → prompt → cost |
127
+ | `cto_get_prompt` | Interact | Rendered prompt ready to use |
128
+ | `cto_estimate_cost` | Interact | Cost estimation for a model |
129
+ | `cto_init` | Govern | Initialize .cto/ directory |
130
+ | `cto_scan_secrets` | Govern | Scan for hardcoded secrets |
131
+ | `cto_validate_policy` | Govern | Validate against policy rules |
132
+ | `cto_snapshot_create` | Govern | Create reproducible snapshot |
133
+ | `cto_snapshot_verify` | Govern | Verify snapshot (detect drift) |
134
+ | `cto_snapshot_compare` | Govern | Compare two snapshots |
135
+ | `cto_audit_query` | Govern | Query audit trail |
136
+ | `cto_cache_stats` | Infra | Cache statistics |
137
+ | `cto_watch_status` | Infra | Active file watchers |
138
+
139
+ ### Resources
140
+
141
+ - `cto://analysis/{projectPath}` — Full analysis as JSON
142
+
143
+ ### Prompts
144
+
145
+ - `plan-task` — Plan an optimized AI interaction
146
+ - `review-context` — Review what context would be selected
147
+
148
+ ---
149
+
150
+ ## API Server
151
+
152
+ Start a REST API server for integration with any tool or language.
153
+
154
+ ```bash
155
+ cto2-api # start on port 3141
156
+ PORT=8080 cto2-api # custom port
157
+ CTO_API_KEY=your-secret cto2-api # with auth
158
+ CTO_API_KEY=your-secret RATE_LIMIT=30 cto2-api # with rate limiting
159
+ ```
160
+
161
+ ### Endpoints
162
+
163
+ | Method | Path | Body | Description |
164
+ |--------|------|------|-------------|
165
+ | `POST` | `/v1/analyze` | `{ "path": "..." }` | Full project analysis |
166
+ | `POST` | `/v1/select` | `{ "path": "...", "task": "...", "budget": 50000 }` | Context selection |
167
+ | `POST` | `/v1/score` | `{ "path": "..." }` | Context Score |
168
+ | `POST` | `/v1/benchmark` | `{ "path": "...", "task": "..." }` | Benchmark comparison |
169
+ | `POST` | `/v1/quality` | `{ "path": "...", "task": "..." }` | Quality benchmark |
170
+ | `POST` | `/v1/pr-context` | `{ "path": "...", "task": "...", "base": "main" }` | PR context |
171
+ | `POST` | `/v1/interact` | `{ "path": "...", "task": "...", "budget": 50000 }` | Full pipeline |
172
+ | `GET` | `/v1/health` | — | Health check |
173
+ | `GET` | `/v1/openapi` | — | OpenAPI 3.1 spec |
174
+
175
+ ### Example
176
+
177
+ ```bash
178
+ # Score a project
179
+ curl -X POST http://localhost:3141/v1/score \
180
+ -H 'Content-Type: application/json' \
181
+ -H 'Authorization: Bearer your-secret' \
182
+ -d '{"path": "/path/to/project"}'
183
+
184
+ # Select context for a task
185
+ curl -X POST http://localhost:3141/v1/select \
186
+ -H 'Content-Type: application/json' \
187
+ -d '{"path": "/path/to/project", "task": "refactor auth", "budget": 50000}'
188
+ ```
189
+
190
+ ### Authentication
191
+
192
+ Set `CTO_API_KEY` environment variable. All requests must include `Authorization: Bearer <key>`.
193
+
194
+ If `CTO_API_KEY` is not set, the server runs without auth (development mode).
195
+
196
+ ### Rate Limiting
197
+
198
+ Default: 60 requests per minute per IP. Configurable via `RATE_LIMIT` env var.
199
+
200
+ ---
201
+
202
+ ## Programmatic API
203
+
204
+ ```typescript
205
+ import { analyzeProject, getCachedAnalysis } from 'cto-ai-cli/engine';
206
+ import { planInteraction } from 'cto-ai-cli/interact';
207
+ import { validateSelection, DEFAULT_POLICY } from 'cto-ai-cli/govern';
208
+
209
+ // Analyze (cached — instant on repeat calls)
210
+ const analysis = await getCachedAnalysis('/path/to/project');
211
+
212
+ // Plan an interaction
213
+ const plan = await planInteraction({
214
+ task: 'refactor the auth middleware',
215
+ analysis,
216
+ budget: 50_000,
217
+ });
218
+
219
+ console.log(plan.context.files); // Selected files with prune levels
220
+ console.log(plan.model); // Recommended model + alternatives
221
+ console.log(plan.cost.savings); // Token & dollar savings
222
+ console.log(plan.prompt.rendered); // Ready-to-use prompt
223
+
224
+ // Context Score
225
+ import { computeContextScore } from 'cto-ai-cli/engine';
226
+ const score = await computeContextScore(analysis);
227
+ console.log(score.overall, score.grade); // 87, "A-"
228
+
229
+ // Benchmark
230
+ import { runBenchmark } from 'cto-ai-cli/engine';
231
+ const benchmark = await runBenchmark(analysis);
232
+ console.log(benchmark.ctoAdvantage);
233
+
234
+ // Validate against policies
235
+ const validation = validateSelection(plan.context, DEFAULT_POLICY, analysis.files);
236
+ console.log(validation.passed);
237
+ ```
238
+
239
+ ---
240
+
241
+ ## GitHub Action
242
+
243
+ Post Context Score on every PR.
244
+
245
+ ```yaml
246
+ # .github/workflows/cto-score.yml
247
+ name: CTO Context Score
248
+ on: [pull_request]
249
+
250
+ jobs:
251
+ score:
252
+ runs-on: ubuntu-latest
253
+ steps:
254
+ - uses: actions/checkout@v4
255
+ - uses: actions/setup-node@v4
256
+ with:
257
+ node-version: '20'
258
+ - uses: cto-ai/cto-ai-cli@main
259
+ with:
260
+ comment-on-pr: 'true'
261
+ fail-below: '40'
262
+ ```
263
+
264
+ ### Inputs
265
+
266
+ | Input | Default | Description |
267
+ |-------|---------|-------------|
268
+ | `path` | `.` | Project path |
269
+ | `budget` | `50000` | Token budget |
270
+ | `task` | `general code review` | Representative task |
271
+ | `comment-on-pr` | `true` | Post PR comment |
272
+ | `fail-below` | `0` | Fail if score below threshold |
273
+
274
+ ### Outputs
275
+
276
+ | Output | Description |
277
+ |--------|-------------|
278
+ | `score` | Context Score (0-100) |
279
+ | `grade` | Grade (A+ to F) |
280
+ | `saved-percent` | Token savings % |
281
+ | `monthly-savings` | USD saved per month |
282
+ | `json` | Full results as JSON |
283
+
284
+ ---
285
+
286
+ ## Configuration
287
+
288
+ CTO uses `.cto/config.yml` in your project root.
289
+
290
+ ```yaml
291
+ version: "2.0"
292
+ tokenMethod: tiktoken # tiktoken (accurate) | chars4 (fast)
293
+ defaultBudget: 50000 # default token budget
294
+
295
+ ignoreDirs:
296
+ - node_modules
297
+ - .git
298
+ - dist
299
+ - build
300
+ - coverage
301
+
302
+ extensions:
303
+ - ts
304
+ - tsx
305
+ - js
306
+ - jsx
307
+ - py
308
+ - go
309
+ - rs
310
+ - java
311
+ ```
312
+
313
+ ---
314
+
315
+ ## Architecture
316
+
317
+ ```
318
+ src/
319
+ ├── engine/ # Context Intelligence Engine
320
+ │ ├── analyzer.ts # Project analysis pipeline
321
+ │ ├── graph.ts # Dependency graph
322
+ │ ├── risk.ts # Risk scoring
323
+ │ ├── selector.ts # Deterministic context selection
324
+ │ ├── score.ts # Context Score (0-100)
325
+ │ ├── benchmark.ts # Strategy comparison
326
+ │ ├── compile-proof.ts # Real tsc compilation test
327
+ │ ├── multi-model.ts # Per-model optimization
328
+ │ ├── predictor.ts # ML-based prediction (learns from usage)
329
+ │ ├── semantic.ts # Semantic domain analysis
330
+ │ ├── feedback.ts # Output feedback loop
331
+ │ └── cross-repo.ts # Cross-repo intelligence
332
+ ├── interact/ # Interaction Optimization
333
+ │ ├── orchestrator.ts # Full pipeline
334
+ │ ├── router.ts # Model routing
335
+ │ ├── prompt.ts # Prompt builder
336
+ │ └── estimator.ts # Cost estimation
337
+ ├── govern/ # Governance & Audit
338
+ │ ├── audit.ts # Audit trail
339
+ │ ├── secrets.ts # Secret detection
340
+ │ ├── policy.ts # Policy engine
341
+ │ ├── snapshot.ts # Snapshots
342
+ │ └── integrity.ts # SHA-256 verification
343
+ ├── cli/
344
+ │ ├── score.ts # npx cto-score entry point
345
+ │ └── v2/index.ts # Full CLI
346
+ ├── mcp/v2.ts # MCP server (19 tools)
347
+ ├── api/
348
+ │ ├── server.ts # REST API server
349
+ │ └── dashboard.ts # HTML dashboard generator
350
+ └── action/index.ts # GitHub Action
351
+ ```