cto-ai-cli 5.2.0 → 7.1.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 DELETED
@@ -1,902 +0,0 @@
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
- - [Security Audit](#security-audit---audit)
9
- - [Context Gateway](#context-gateway)
10
- - [Learning Mode](#learning-mode)
11
- - [Code Review](#code-review)
12
- - [MCP Server](#mcp-server)
13
- - [API Server](#api-server)
14
- - [Programmatic API](#programmatic-api)
15
- - [GitHub Action](#github-action)
16
- - [Configuration](#configuration)
17
- - [Architecture](#architecture)
18
-
19
- ---
20
-
21
- ## CLI Commands
22
-
23
- ### `cto2 init [path]`
24
-
25
- Initialize CTO for a project. Creates `.cto/config.yml` and `.cto/policy.yml`.
26
-
27
- ```bash
28
- cto2 init # current directory
29
- cto2 init ./my-project # specific directory
30
- ```
31
-
32
- ### `cto2 analyze [path]`
33
-
34
- Analyze project structure, dependencies, and risk profile.
35
-
36
- ```bash
37
- cto2 analyze # full analysis
38
- cto2 analyze --risk-only # show only risk distribution
39
- cto2 analyze --graph # show dependency graph (hubs, clusters, orphans)
40
- ```
41
-
42
- ### `cto2 interact <task>`
43
-
44
- Build optimized AI context for a specific task.
45
-
46
- ```bash
47
- cto2 interact "refactor the auth middleware"
48
- cto2 interact "fix the login bug" --explain # show selection decisions
49
- cto2 interact "review this PR" --pr # PR mode (diff vs main)
50
- cto2 interact "review this PR" --pr develop # PR mode (diff vs develop)
51
- cto2 interact "add tests" --output file # save to .cto/prompt-{id}.md
52
- cto2 interact "add tests" --output clipboard # copy to clipboard
53
- cto2 interact "add tests" --json # JSON output
54
- ```
55
-
56
- ### `cto2 snapshot`
57
-
58
- Reproducible context snapshots.
59
-
60
- ```bash
61
- cto2 snapshot create baseline # save current state
62
- cto2 snapshot verify baseline # check for drift
63
- cto2 snapshot compare a b # diff two snapshots
64
- cto2 snapshot list # list all snapshots
65
- ```
66
-
67
- ### `cto2 audit`
68
-
69
- Tamper-evident audit trail.
70
-
71
- ```bash
72
- cto2 audit list # show audit entries
73
- cto2 audit verify # verify integrity
74
- cto2 audit purge # remove old entries
75
- ```
76
-
77
- ### `cto2 policy`
78
-
79
- Context selection policies.
80
-
81
- ```bash
82
- cto2 policy show
83
- cto2 policy add no-tests exclude-always --pattern "**/*.test.*" --reason "Skip tests"
84
- cto2 policy remove no-tests
85
- cto2 policy toggle no-tests
86
- cto2 policy validate
87
- cto2 policy init
88
- ```
89
-
90
- ### `npx cto-ai-cli` (zero-install CLI)
91
-
92
- The default binary. All flags work with zero install via `npx`.
93
-
94
- ```bash
95
- npx cto-ai-cli # Score your project
96
- npx cto-ai-cli ./path # Score a specific project
97
- npx cto-ai-cli --fix # Auto-generate .cto/context.md, config.json, .cteignore
98
- npx cto-ai-cli --context "your task" # Task-specific context with file contents
99
- npx cto-ai-cli --audit # Security audit: detect secrets & PII
100
- npx cto-ai-cli --report # Shareable markdown report + shields.io badge
101
- npx cto-ai-cli --compare # Compare your score vs popular open source projects
102
- npx cto-ai-cli --benchmark # CTO vs naive vs random comparison
103
- npx cto-ai-cli --json # Machine-readable JSON output
104
- npx cto-ai-cli --help # Show all options
105
-
106
- # Phase 5 — CI/CD Quality Gate
107
- npx cto-ai-cli --ci # Run quality gate (exits 1 on failure)
108
- npx cto-ai-cli --ci --threshold 80 # Set minimum score (default: 70)
109
- npx cto-ai-cli --ci --json # JSON output for CI pipelines
110
-
111
- # Phase 7 — Learning Mode
112
- npx cto-ai-cli --learn # Show feedback model & cross-repo intelligence
113
- npx cto-ai-cli --feedback # Alias for --learn
114
- npx cto-ai-cli --predict # Predict relevant files for a task
115
- npx cto-ai-cli --learn --json # Export learning data as JSON
116
-
117
- # Phase 8 — Code Review
118
- npx cto-ai-cli --review # Smart PR review analysis
119
- npx cto-ai-cli --review --json # JSON output for CI
120
- ```
121
-
122
- Flags can be combined: `npx cto-ai-cli --fix --audit --report --compare`
123
-
124
- ---
125
-
126
- ## Security Audit (`--audit`)
127
-
128
- Full-project secret and PII detection. Scans all source files for hardcoded credentials, tokens, keys, passwords, connection strings, and personally identifiable information.
129
-
130
- ### Usage
131
-
132
- ```bash
133
- npx cto-ai-cli --audit # Full audit with terminal output
134
- npx cto-ai-cli --audit --fix # Audit + auto-generate context files
135
- CI=true npx cto-ai-cli --audit # CI mode: exit code 1 on critical/high findings
136
- ```
137
-
138
- ### Detection engine
139
-
140
- CTO uses a **dual-strategy** detection approach:
141
-
142
- **1. Pattern matching (30+ patterns)**
143
-
144
- | Type | Pattern examples | Severity |
145
- |------|-----------------|----------|
146
- | `api-key` | OpenAI `sk-*`, Anthropic `sk-ant-*`, Stripe `sk_live_*`, Google `AIza*`, SendGrid `SG.*.*` | Critical |
147
- | `aws-key` | `AKIA*` (Access Key ID), `aws_secret_access_key=*` | Critical |
148
- | `token` | GitHub `ghp_*`/`gho_*`, GitLab `glpat-*`, Slack `xoxb-*`/`xoxp-*`, npm `npm_*`, JWT `eyJ*.*.*` | Critical/High |
149
- | `private-key` | `-----BEGIN RSA PRIVATE KEY-----`, `-----BEGIN OPENSSH PRIVATE KEY-----` | Critical |
150
- | `connection-string` | `mongodb://user:pass@host`, `postgres://`, `DATABASE_URL=` | Critical |
151
- | `password` | `password=`, `DB_PASSWORD=`, `MYSQL_PASSWORD=` | High |
152
- | `env-variable` | `SECRET_KEY=`, `PRIVATE_TOKEN=`, `ENCRYPTION_PASS=` | High |
153
- | `pii` | Email addresses, possible SSNs | Medium |
154
-
155
- **2. Shannon entropy analysis**
156
-
157
- For strings that don't match a known pattern but look like secrets:
158
- - Scans all quoted strings ≥40 characters
159
- - Calculates Shannon entropy (randomness measure)
160
- - Flags strings with entropy ≥5.0 bits (very random = likely a secret)
161
- - Automatically skips: hex hashes, camelCase identifiers, base64 padding, integrity hashes, comments, test files
162
-
163
- ### Smart filtering (false positive reduction)
164
-
165
- The scanner skips:
166
- - **Placeholders**: `${API_KEY}`, `{{SECRET}}`, `YOUR_KEY_HERE`, `CHANGE_ME`, `example`, `TODO`, `dummy`, `sample`
167
- - **Test files**: `*.test.ts`, `*.spec.ts`, `__tests__/*` (entropy analysis only — patterns still apply)
168
- - **Declaration files**: `*.d.ts` (entropy analysis only)
169
- - **Comments**: Lines starting with `//`, `#`, `*`
170
-
171
- ### Output artifacts
172
-
173
- | File | Format | Purpose |
174
- |------|--------|---------|
175
- | `.cto/audit/YYYY-MM-DD.jsonl` | JSON Lines (append-only) | Audit log — one entry per run. Run daily to build history. |
176
- | `.cto/audit/report.md` | Markdown | Full report with findings table — share with team or compliance. |
177
- | `.cto/.env.example` | Text | Auto-generated `.env` template with all detected variable names. |
178
-
179
- ### Audit log format (`.jsonl`)
180
-
181
- Each line is a JSON object:
182
-
183
- ```json
184
- {
185
- "timestamp": "2026-02-24T23:38:00.000Z",
186
- "version": "3.2.0",
187
- "summary": {
188
- "filesScanned": 179,
189
- "filesWithSecrets": 12,
190
- "totalFindings": 51,
191
- "bySeverity": { "critical": 34, "high": 5, "medium": 12, "low": 0 },
192
- "byType": { "api-key": 21, "private-key": 5, "aws-key": 4, "token": 4, "connection-string": 3, "password": 2, "pii": 12 }
193
- },
194
- "findings": [
195
- { "type": "api-key", "file": "src/config/stripe.ts", "line": 8, "severity": "critical", "redacted": "sk_l********************yZ" }
196
- ]
197
- }
198
- ```
199
-
200
- ### Programmatic API
201
-
202
- ```typescript
203
- import { auditProject, scanContentForSecrets, scanContentForHighEntropy } from 'cto-ai-cli/govern';
204
-
205
- // Full project audit
206
- const result = await auditProject('/path/to/project', filePaths, {
207
- customPatterns: ['MY_INTERNAL_TOKEN=[a-z0-9]+'], // optional extra patterns
208
- entropyThreshold: 5.0, // default: 5.0
209
- includePII: true, // default: true
210
- });
211
-
212
- console.log(result.summary); // { totalFindings, bySeverity, byType, ... }
213
- console.log(result.findings); // SecretFinding[]
214
- console.log(result.recommendations); // string[]
215
-
216
- // Scan a single string
217
- const findings = scanContentForSecrets(code, 'file.ts');
218
-
219
- // Entropy-only scan
220
- const entropyFindings = scanContentForHighEntropy(code, 'file.ts', 5.0);
221
- ```
222
-
223
- ### CI/CD integration
224
-
225
- When `CI=true` is set, `--audit` exits with code 1 if any critical or high-severity findings are detected. Use this in:
226
-
227
- **GitHub Actions:**
228
- ```yaml
229
- - name: CTO Security Audit
230
- run: npx cto-ai-cli --audit
231
- env:
232
- CI: true
233
- ```
234
-
235
- **Pre-commit hook (`.husky/pre-commit`):**
236
- ```bash
237
- CI=true npx cto-ai-cli --audit
238
- ```
239
-
240
- ### Recommendations engine
241
-
242
- Based on findings, CTO generates actionable recommendations:
243
-
244
- | Finding type | Recommendation |
245
- |-------------|----------------|
246
- | Any critical | "Rotate all detected credentials immediately" |
247
- | `password` | "Move passwords to environment variables or a secrets manager" |
248
- | `api-key` / `aws-key` | "Use environment variables. Never commit to source control" |
249
- | `connection-string` | "Database connection strings should use environment variables" |
250
- | `private-key` | "Private keys should NEVER be in source code. Use a key management service" |
251
- | `pii` | "Review for GDPR/CCPA compliance. Consider data anonymization" |
252
- | `high-entropy` | "High-entropy strings detected. Review manually" |
253
- | Any finding | "Add .gitignore entry for .env files" + "Run --audit regularly or add to CI" |
254
-
255
- ---
256
-
257
- ## Context Gateway
258
-
259
- A transparent HTTP proxy that sits between your application and any LLM provider. It intercepts every request, scans for secrets, optimizes context, tracks costs, and enforces budgets — all with zero external dependencies.
260
-
261
- ### Quick start
262
-
263
- ```bash
264
- npx cto-gateway # Start on port 8787
265
- npx cto-gateway --port 9000 # Custom port
266
- npx cto-gateway --block-secrets # Hard block on critical secrets
267
- npx cto-gateway --budget-daily 10 # $10/day limit
268
- ```
269
-
270
- Then point your app:
271
- ```bash
272
- export OPENAI_BASE_URL=http://localhost:8787
273
- ```
274
-
275
- Every request must include the target provider URL as a header:
276
- ```
277
- x-cto-target: https://api.openai.com/v1/chat/completions
278
- ```
279
-
280
- ### Architecture
281
-
282
- ```
283
- Your App → Gateway (localhost:8787) → Provider (api.openai.com)
284
-
285
- ├── Secret Scanner → redacts/blocks secrets in messages
286
- ├── Context Optimizer → injects CTO-selected context
287
- ├── Cost Tracker → logs per-request cost to JSONL
288
- ├── Budget Guard → rejects requests over limit (429)
289
- └── Dashboard → live web UI at /__cto
290
- ```
291
-
292
- ### CLI flags
293
-
294
- | Flag | Default | Description |
295
- |------|---------|-------------|
296
- | `--port <n>` | `8787` | Port to listen on |
297
- | `--host <addr>` | `127.0.0.1` | Host to bind to |
298
- | `--project <path>` | `.` | Project to analyze for context optimization |
299
- | `--block-secrets` | off | Hard block requests with critical secrets (403) |
300
- | `--budget-daily <$>` | unlimited | Max cost per day — returns 429 when exceeded |
301
- | `--budget-monthly <$>` | unlimited | Max cost per month |
302
- | `--no-optimize` | on | Disable CTO context injection |
303
- | `--no-redact` | on | Disable secret redaction |
304
- | `--no-dashboard` | on | Disable web dashboard |
305
-
306
- ### Provider detection
307
-
308
- The Gateway auto-detects providers from the `x-cto-target` URL and request headers:
309
-
310
- | Provider | Detection | Auth header |
311
- |----------|-----------|-------------|
312
- | OpenAI | `api.openai.com` or `/v1/chat/completions` | `Authorization: Bearer sk-...` |
313
- | Anthropic | `api.anthropic.com` or `anthropic-version` header | `x-api-key` |
314
- | Google AI | `generativelanguage.googleapis.com` | `x-goog-api-key` |
315
- | Azure OpenAI | `*.openai.azure.com` or `api-key` header | `api-key` |
316
- | Custom | Fallback — assumes OpenAI-compatible | `Authorization` |
317
-
318
- ### Model pricing
319
-
320
- Built-in pricing for accurate cost tracking:
321
-
322
- | Model | Input ($/M tokens) | Output ($/M tokens) | Context window |
323
- |-------|--------------------|--------------------|----------------|
324
- | gpt-4o | $2.50 | $10.00 | 128K |
325
- | gpt-4o-mini | $0.15 | $0.60 | 128K |
326
- | o1 | $15.00 | $60.00 | 200K |
327
- | o3-mini | $1.10 | $4.40 | 200K |
328
- | claude-sonnet-4 | $3.00 | $15.00 | 200K |
329
- | claude-3.5-haiku | $0.80 | $4.00 | 200K |
330
- | gemini-2.5-pro | $1.25 | $10.00 | 1M |
331
- | gemini-2.0-flash | $0.10 | $0.40 | 1M |
332
-
333
- ### Request lifecycle
334
-
335
- 1. **Receive** — Client sends POST request to Gateway
336
- 2. **Budget check** — If daily/monthly budget exceeded → 429
337
- 3. **Parse** — Detect provider, extract messages from provider-specific format
338
- 4. **Scan secrets** — Run 30+ patterns against all message content
339
- 5. **Redact or block** — Replace secrets with `***REDACTED***` or return 403
340
- 6. **Optimize context** — If analysis ready, inject CTO-selected files into system prompt
341
- 7. **Forward** — Proxy to provider (streaming SSE or buffered)
342
- 8. **Track** — Log cost, tokens, savings, latency to JSONL
343
- 9. **Respond** — Forward provider response to client (zero-copy for streams)
344
-
345
- ### Streaming support
346
-
347
- The Gateway fully supports Server-Sent Events (SSE) streaming:
348
-
349
- - Detects streaming from `Content-Type: text/event-stream`
350
- - Zero-copy passthrough: chunks are forwarded to client as they arrive
351
- - Async token tracking: parses SSE events in background without blocking the stream
352
- - Usage data extracted from final SSE chunk (when provider includes it)
353
-
354
- ### Dashboard
355
-
356
- Available at `http://localhost:8787/__cto` (configurable via `--dashboardPath`).
357
-
358
- Shows:
359
- - **Today**: requests, cost, tokens saved, secrets redacted
360
- - **This month**: totals + budget progress
361
- - **Feature status**: optimization, redaction, tracking, audit log
362
- - **By model**: breakdown of requests, tokens, and cost per model
363
- - **By provider**: requests and cost per provider
364
- - Auto-refreshes every 30 seconds
365
-
366
- ### Usage storage
367
-
368
- | File | Format | Description |
369
- |------|--------|-------------|
370
- | `.cto/gateway/usage/YYYY-MM.jsonl` | JSON Lines | One line per request. Monthly files. |
371
-
372
- Each line:
373
- ```json
374
- {
375
- "id": "a1b2c3d4",
376
- "timestamp": "2026-02-24T23:52:00.000Z",
377
- "provider": "openai",
378
- "model": "gpt-4o",
379
- "inputTokens": 1200,
380
- "outputTokens": 350,
381
- "costUSD": 0.0065,
382
- "originalTokens": 6200,
383
- "optimizedTokens": 1200,
384
- "savedTokens": 5000,
385
- "savedUSD": 0.0130,
386
- "secretsRedacted": 2,
387
- "secretsBlocked": false,
388
- "latencyMs": 152,
389
- "stream": true
390
- }
391
- ```
392
-
393
- ### Budget enforcement
394
-
395
- When a budget is set, the Gateway checks cost totals before every request:
396
-
397
- | Condition | HTTP response |
398
- |-----------|---------------|
399
- | Daily budget exceeded | `429 Too Many Requests` + `{ "error": "Daily budget exceeded", "budget": 10, "current": 10.42 }` |
400
- | Monthly budget exceeded | `429 Too Many Requests` + `{ "error": "Monthly budget exceeded" }` |
401
- | Critical secrets + `--block-secrets` | `403 Forbidden` + `{ "error": "Request blocked: secrets detected" }` |
402
-
403
- Budget alerts are emitted at 80% of limit (configurable via `alertThreshold`).
404
-
405
- ### Programmatic API
406
-
407
- ```typescript
408
- import { ContextGateway, UsageTracker } from 'cto-ai-cli/gateway';
409
-
410
- const gateway = new ContextGateway({
411
- port: 8787,
412
- projectPath: '/path/to/project',
413
- redactSecrets: true,
414
- blockOnSecrets: false,
415
- budgetDaily: 20,
416
- budgetMonthly: 500,
417
- });
418
-
419
- // Listen to events
420
- gateway.onEvent((event) => {
421
- if (event.type === 'request') console.log(`${event.record.model}: $${event.record.costUSD}`);
422
- if (event.type === 'budget-alert') console.log(`Budget warning: ${event.period}`);
423
- });
424
-
425
- await gateway.start();
426
-
427
- // Get usage summary
428
- const tracker = gateway.getTracker();
429
- const summary = tracker.getSummary('month');
430
- console.log(`This month: ${summary.totalRequests} requests, $${summary.totalCostUSD}`);
431
-
432
- // Provider detection (standalone)
433
- import { detectProvider, estimateCost } from 'cto-ai-cli/gateway';
434
-
435
- const provider = detectProvider('https://api.openai.com/v1/chat/completions', {});
436
- const cost = estimateCost(provider, 'gpt-4o', 5000, 1000);
437
- ```
438
-
439
- ### Interceptor (standalone)
440
-
441
- ```typescript
442
- import { interceptRequest } from 'cto-ai-cli/gateway';
443
- import type { Message, GatewayConfig } from 'cto-ai-cli/gateway';
444
-
445
- const messages: Message[] = [
446
- { role: 'user', content: 'Deploy with key sk-live_abc123...' },
447
- ];
448
-
449
- const result = await interceptRequest(messages, config, analysis);
450
- // result.secretsRedacted → 1
451
- // result.messages[0].content → 'Deploy with key sk-l**********23...'
452
- // result.decisions → ['Redacted 1 secret(s) in user message: api-key']
453
- ```
454
-
455
- ---
456
-
457
- ## Learning Mode
458
-
459
- CTO learns from your usage patterns to improve context selection over time. Three engines work together:
460
-
461
- ### Feedback Engine (`--learn` / `--feedback`)
462
-
463
- Tracks which context selections lead to accepted AI output.
464
-
465
- ```bash
466
- npx cto-ai-cli --learn # Full learning dashboard
467
- npx cto-ai-cli --learn --json # Export for team sharing
468
- ```
469
-
470
- **v2.0 Features:**
471
-
472
- | Feature | Description |
473
- |---------|-------------|
474
- | **EWMA Temporal Decay** | Recent feedback weighs more (α=0.15). Old patterns fade naturally. |
475
- | **Bayesian Confidence (Wilson Score)** | Avoids over-trusting sparse data. 1/1 ≠ 100% confidence. |
476
- | **Session Tracking** | Groups related feedback for per-session analysis. |
477
- | **A/B Strategy Comparison** | Compare different context strategies with statistical rigor. |
478
- | **Team Export/Import** | Share learned models across teams with weighted merge (local 70%, team 30%). |
479
-
480
- ### Predictor Engine (`--predict`)
481
-
482
- Predicts which files are relevant for a task based on historical patterns.
483
-
484
- ```bash
485
- npx cto-ai-cli --predict --context "fix auth bug" # Predict files for a task
486
- npx cto-ai-cli --predict --json # JSON predictions
487
- ```
488
-
489
- The predictor uses:
490
- - **Task type frequency** — which files were selected for similar task types (3× weight)
491
- - **Keyword frequency** — which files correlate with task keywords (2× weight)
492
- - **General selection frequency** — files selected in >30% of all observations
493
- - **Co-selection patterns** — files that tend to be selected together
494
-
495
- ### Cross-Repo Intelligence
496
-
497
- Learns patterns across repositories. Stored in `~/.cto/global-intelligence.json`.
498
-
499
- - **Project fingerprinting** — stack, size class, structure pattern
500
- - **Archetype matching** — "TypeScript medium-size projects with tests"
501
- - **Universal patterns** — patterns that work across >50% of project types
502
-
503
- ### Programmatic API
504
-
505
- ```typescript
506
- import {
507
- recordFeedback,
508
- loadFeedbackModel,
509
- getFeedbackBoosts,
510
- exportFeedbackForTeam,
511
- importTeamFeedback,
512
- wilsonLowerBound,
513
- renderFeedbackReport,
514
- renderCrossRepoReport,
515
- } from 'cto-ai-cli/engine';
516
- import {
517
- recordSelection,
518
- predictRelevantFiles,
519
- getPredictorBoosts,
520
- loadModel,
521
- getModelStats,
522
- } from 'cto-ai-cli/engine';
523
- import {
524
- recordCrossRepoSelection,
525
- predictFromCrossRepo,
526
- loadGlobalModel,
527
- getCrossRepoStats,
528
- computeFingerprint,
529
- } from 'cto-ai-cli/engine';
530
-
531
- // Record feedback
532
- const model = await recordFeedback('/path/to/project', {
533
- task: 'fix auth bug',
534
- contextHash: 'abc123',
535
- filesIncluded: ['src/auth.ts', 'src/types.ts'],
536
- tokensUsed: 5000,
537
- budget: 50000,
538
- outcome: { accepted: true, compilable: true, timeToAcceptMs: 3000 },
539
- sessionId: 'sess-1', // optional: group feedback
540
- strategy: 'experimental', // optional: A/B testing
541
- });
542
-
543
- // Get boosts for context selection
544
- const boosts = await getFeedbackBoosts('/path/to/project', 'fix auth');
545
- // Map<string, number> — file path → boost value
546
-
547
- // Wilson score for statistical confidence
548
- const confidence = wilsonLowerBound(8, 10); // 8 successes out of 10
549
- // ~0.49 — 95% confident the true rate is ≥49%
550
-
551
- // Team sharing
552
- const exported = await exportFeedbackForTeam('/path/to/project', 'my-project');
553
- const merged = await importTeamFeedback('/other/project', exported);
554
-
555
- // Predict relevant files
556
- const predictions = await predictRelevantFiles('/path', 'fix auth', analysis);
557
- // { filePath, predictedScore, reasons }[]
558
- ```
559
-
560
- ### Storage
561
-
562
- | File | Scope | Description |
563
- |------|-------|-------------|
564
- | `.cto/feedback.json` | Per-project | Raw feedback entries (last 1000) |
565
- | `.cto/feedback-model.json` | Per-project | Rebuilt model with EWMA, Bayesian, sessions |
566
- | `.cto/predictor.json` | Per-project | ML predictor model |
567
- | `~/.cto/global-intelligence.json` | Cross-repo | Global archetype and pattern data |
568
-
569
- ---
570
-
571
- ## Code Review
572
-
573
- Context-aware PR review intelligence. Analyzes git diffs, detects breaking changes, finds missing files, and generates AI-ready review prompts.
574
-
575
- ### Usage
576
-
577
- ```bash
578
- npx cto-ai-cli --review # Full review analysis
579
- npx cto-ai-cli --review --json # JSON output for CI
580
- ```
581
-
582
- Generates:
583
- - **Terminal dashboard** — review quality score, breaking changes, missing files, impact radius
584
- - **`.cto/review-prompt.md`** — AI-ready review prompt with full file contents
585
-
586
- ### Features
587
-
588
- | Feature | Description |
589
- |---------|-------------|
590
- | **Diff Parsing** | Parses git diffs into structured DiffHunk[] with additions/deletions |
591
- | **Breaking Change Detection** | Removed exports, type changes, function signature changes, deleted files with dependents |
592
- | **Missing File Detection** | Sibling type files, test files, importers of changed exports, barrel index files |
593
- | **Impact Radius** | BFS on dependency graph: direct dependents, transitive (2-hop), affected tests |
594
- | **Review Quality Score** | 5-factor weighted score: PR size (25%), focus (20%), breaking changes (25%), completeness (15%), blast radius (15%) |
595
- | **Review Prompt Generation** | Context-rich prompt with breaking changes, missing files, and file contents for top-risk files |
596
-
597
- ### Review Quality Score
598
-
599
- | Grade | Score | Meaning |
600
- |-------|-------|---------|
601
- | A+/A/A- | 85-100 | Small, focused PR with no breaking changes |
602
- | B+/B/B- | 70-84 | Manageable PR, some issues to review |
603
- | C+/C/C- | 55-69 | Large or unfocused PR, needs careful review |
604
- | D+/D/D- | 40-54 | Risky PR with breaking changes or missing files |
605
- | F | <40 | Very large, unfocused, or highly risky PR |
606
-
607
- ### Breaking Change Severity
608
-
609
- | Severity | Trigger |
610
- |----------|--------|
611
- | **Critical** | Deleted file with dependents, removed export with >3 dependents |
612
- | **High** | Removed export, interface property removed, function signature changed |
613
- | **Medium** | Export changed with no direct dependents |
614
-
615
- ### Programmatic API
616
-
617
- ```typescript
618
- import { analyzeForReview, renderReviewSummary } from 'cto-ai-cli/engine';
619
- import type { ReviewResult, ReviewOptions } from 'cto-ai-cli/engine';
620
-
621
- const result: ReviewResult = await analyzeForReview(analysis, {
622
- baseBranch: 'main', // default: 'main'
623
- depth: 2, // dependency expansion depth
624
- maxPromptFiles: 20, // max files in review prompt
625
- });
626
-
627
- console.log(result.breakingChanges); // BreakingChange[]
628
- console.log(result.missingFiles); // MissingFile[]
629
- console.log(result.impactRadius); // { directlyAffected, transitivelyAffected, riskScore }
630
- console.log(result.reviewQuality); // { score, grade, factors }
631
- console.log(result.reviewPrompt); // Full AI-ready review prompt
632
- ```
633
-
634
- ---
635
-
636
- ## MCP Server
637
-
638
- CTO exposes 19 tools via the Model Context Protocol.
639
-
640
- ### Setup
641
-
642
- **Windsurf** (`~/.codeium/windsurf/mcp_config.json`):
643
- ```json
644
- {
645
- "mcpServers": {
646
- "cto": { "command": "cto2-mcp" }
647
- }
648
- }
649
- ```
650
-
651
- **Claude Desktop** (`~/Library/Application Support/Claude/claude_desktop_config.json`):
652
- ```json
653
- {
654
- "mcpServers": {
655
- "cto": {
656
- "command": "node",
657
- "args": ["/path/to/cto-ai-cli/dist/mcp/v2.js"]
658
- }
659
- }
660
- }
661
- ```
662
-
663
- ### Tools
664
-
665
- | Tool | Category | Description |
666
- |------|----------|-------------|
667
- | `cto_analyze` | Engine | Full project analysis |
668
- | `cto_risk_profile` | Engine | Risk factors per file |
669
- | `cto_select_context` | Engine | Deterministic context selection |
670
- | `cto_pr_context` | Engine | PR-focused context from git diff |
671
- | `cto_score` | Engine | Context Score (0-100) |
672
- | `cto_benchmark` | Engine | CTO vs naive vs random |
673
- | `cto_quality_benchmark` | Engine | Relevance, completeness, noise metrics |
674
- | `cto_plan_interaction` | Interact | Full pipeline: analyze → select → prompt → cost |
675
- | `cto_get_prompt` | Interact | Rendered prompt ready to use |
676
- | `cto_estimate_cost` | Interact | Cost estimation for a model |
677
- | `cto_init` | Govern | Initialize .cto/ directory |
678
- | `cto_scan_secrets` | Govern | Scan for hardcoded secrets |
679
- | `cto_validate_policy` | Govern | Validate against policy rules |
680
- | `cto_snapshot_create` | Govern | Create reproducible snapshot |
681
- | `cto_snapshot_verify` | Govern | Verify snapshot (detect drift) |
682
- | `cto_snapshot_compare` | Govern | Compare two snapshots |
683
- | `cto_audit_query` | Govern | Query audit trail |
684
- | `cto_cache_stats` | Infra | Cache statistics |
685
- | `cto_watch_status` | Infra | Active file watchers |
686
-
687
- ### Resources
688
-
689
- - `cto://analysis/{projectPath}` — Full analysis as JSON
690
-
691
- ### Prompts
692
-
693
- - `plan-task` — Plan an optimized AI interaction
694
- - `review-context` — Review what context would be selected
695
-
696
- ---
697
-
698
- ## API Server
699
-
700
- Start a REST API server for integration with any tool or language.
701
-
702
- ```bash
703
- cto2-api # start on port 3141
704
- PORT=8080 cto2-api # custom port
705
- CTO_API_KEY=your-secret cto2-api # with auth
706
- CTO_API_KEY=your-secret RATE_LIMIT=30 cto2-api # with rate limiting
707
- ```
708
-
709
- ### Endpoints
710
-
711
- | Method | Path | Body | Description |
712
- |--------|------|------|-------------|
713
- | `POST` | `/v1/analyze` | `{ "path": "..." }` | Full project analysis |
714
- | `POST` | `/v1/select` | `{ "path": "...", "task": "...", "budget": 50000 }` | Context selection |
715
- | `POST` | `/v1/score` | `{ "path": "..." }` | Context Score |
716
- | `POST` | `/v1/benchmark` | `{ "path": "...", "task": "..." }` | Benchmark comparison |
717
- | `POST` | `/v1/quality` | `{ "path": "...", "task": "..." }` | Quality benchmark |
718
- | `POST` | `/v1/pr-context` | `{ "path": "...", "task": "...", "base": "main" }` | PR context |
719
- | `POST` | `/v1/interact` | `{ "path": "...", "task": "...", "budget": 50000 }` | Full pipeline |
720
- | `GET` | `/v1/health` | — | Health check |
721
- | `GET` | `/v1/openapi` | — | OpenAPI 3.1 spec |
722
-
723
- ### Example
724
-
725
- ```bash
726
- # Score a project
727
- curl -X POST http://localhost:3141/v1/score \
728
- -H 'Content-Type: application/json' \
729
- -H 'Authorization: Bearer your-secret' \
730
- -d '{"path": "/path/to/project"}'
731
-
732
- # Select context for a task
733
- curl -X POST http://localhost:3141/v1/select \
734
- -H 'Content-Type: application/json' \
735
- -d '{"path": "/path/to/project", "task": "refactor auth", "budget": 50000}'
736
- ```
737
-
738
- ### Authentication
739
-
740
- Set `CTO_API_KEY` environment variable. All requests must include `Authorization: Bearer <key>`.
741
-
742
- If `CTO_API_KEY` is not set, the server runs without auth (development mode).
743
-
744
- ### Rate Limiting
745
-
746
- Default: 60 requests per minute per IP. Configurable via `RATE_LIMIT` env var.
747
-
748
- ---
749
-
750
- ## Programmatic API
751
-
752
- ```typescript
753
- import { analyzeProject, getCachedAnalysis } from 'cto-ai-cli/engine';
754
- import { planInteraction } from 'cto-ai-cli/interact';
755
- import { validateSelection, DEFAULT_POLICY } from 'cto-ai-cli/govern';
756
-
757
- // Analyze (cached — instant on repeat calls)
758
- const analysis = await getCachedAnalysis('/path/to/project');
759
-
760
- // Plan an interaction
761
- const plan = await planInteraction({
762
- task: 'refactor the auth middleware',
763
- analysis,
764
- budget: 50_000,
765
- });
766
-
767
- console.log(plan.context.files); // Selected files with prune levels
768
- console.log(plan.model); // Recommended model + alternatives
769
- console.log(plan.cost.savings); // Token & dollar savings
770
- console.log(plan.prompt.rendered); // Ready-to-use prompt
771
-
772
- // Context Score
773
- import { computeContextScore } from 'cto-ai-cli/engine';
774
- const score = await computeContextScore(analysis);
775
- console.log(score.overall, score.grade); // 87, "A-"
776
-
777
- // Benchmark
778
- import { runBenchmark } from 'cto-ai-cli/engine';
779
- const benchmark = await runBenchmark(analysis);
780
- console.log(benchmark.ctoAdvantage);
781
-
782
- // Validate against policies
783
- const validation = validateSelection(plan.context, DEFAULT_POLICY, analysis.files);
784
- console.log(validation.passed);
785
- ```
786
-
787
- ---
788
-
789
- ## GitHub Action
790
-
791
- Post Context Score on every PR.
792
-
793
- ```yaml
794
- # .github/workflows/cto-score.yml
795
- name: CTO Context Score
796
- on: [pull_request]
797
-
798
- jobs:
799
- score:
800
- runs-on: ubuntu-latest
801
- steps:
802
- - uses: actions/checkout@v4
803
- - uses: actions/setup-node@v4
804
- with:
805
- node-version: '20'
806
- - uses: cto-ai/cto-ai-cli@main
807
- with:
808
- comment-on-pr: 'true'
809
- fail-below: '40'
810
- ```
811
-
812
- ### Inputs
813
-
814
- | Input | Default | Description |
815
- |-------|---------|-------------|
816
- | `path` | `.` | Project path |
817
- | `budget` | `50000` | Token budget |
818
- | `task` | `general code review` | Representative task |
819
- | `comment-on-pr` | `true` | Post PR comment |
820
- | `fail-below` | `0` | Fail if score below threshold |
821
-
822
- ### Outputs
823
-
824
- | Output | Description |
825
- |--------|-------------|
826
- | `score` | Context Score (0-100) |
827
- | `grade` | Grade (A+ to F) |
828
- | `saved-percent` | Token savings % |
829
- | `monthly-savings` | USD saved per month |
830
- | `json` | Full results as JSON |
831
-
832
- ---
833
-
834
- ## Configuration
835
-
836
- CTO uses `.cto/config.yml` in your project root.
837
-
838
- ```yaml
839
- version: "2.0"
840
- tokenMethod: tiktoken # tiktoken (accurate) | chars4 (fast)
841
- defaultBudget: 50000 # default token budget
842
-
843
- ignoreDirs:
844
- - node_modules
845
- - .git
846
- - dist
847
- - build
848
- - coverage
849
-
850
- extensions:
851
- - ts
852
- - tsx
853
- - js
854
- - jsx
855
- - py
856
- - go
857
- - rs
858
- - java
859
- ```
860
-
861
- ---
862
-
863
- ## Architecture
864
-
865
- ```
866
- src/
867
- ├── engine/ # Context Intelligence Engine
868
- │ ├── analyzer.ts # Project analysis pipeline
869
- │ ├── graph.ts # Dependency graph
870
- │ ├── risk.ts # Risk scoring
871
- │ ├── selector.ts # Deterministic context selection
872
- │ ├── score.ts # Context Score (0-100)
873
- │ ├── benchmark.ts # Strategy comparison
874
- │ ├── compile-proof.ts # Real tsc compilation test
875
- │ ├── multi-model.ts # Per-model optimization
876
- │ ├── predictor.ts # ML-based prediction (learns from usage)
877
- │ ├── semantic.ts # Semantic domain analysis
878
- │ ├── feedback.ts # Output feedback loop (v2: EWMA, Bayesian, A/B, team)
879
- │ ├── cross-repo.ts # Cross-repo intelligence
880
- │ ├── code-review.ts # Context-aware PR review engine
881
- │ ├── monorepo.ts # Monorepo intelligence
882
- │ └── quality-gate.ts # CI/CD quality gate
883
- ├── interact/ # Interaction Optimization
884
- │ ├── orchestrator.ts # Full pipeline
885
- │ ├── router.ts # Model routing
886
- │ ├── prompt.ts # Prompt builder
887
- │ └── estimator.ts # Cost estimation
888
- ├── govern/ # Governance & Audit
889
- │ ├── audit.ts # Audit trail
890
- │ ├── secrets.ts # Secret detection
891
- │ ├── policy.ts # Policy engine
892
- │ ├── snapshot.ts # Snapshots
893
- │ └── integrity.ts # SHA-256 verification
894
- ├── cli/
895
- │ ├── score.ts # npx cto-score entry point
896
- │ └── v2/index.ts # Full CLI
897
- ├── mcp/v2.ts # MCP server (19 tools)
898
- ├── api/
899
- │ ├── server.ts # REST API server
900
- │ └── dashboard.ts # HTML dashboard generator
901
- └── action/index.ts # GitHub Action
902
- ```