cto-ai-cli 4.0.0 → 5.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/README.md CHANGED
@@ -1,238 +1,128 @@
1
- # CTO — Your AI is reading too much code. We fix that.
2
-
3
- > **Early access** — This is a test version. We'd love your feedback.
1
+ # CTO — Stop sending your entire codebase to AI
4
2
 
5
3
  [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
6
- [![Tests](https://img.shields.io/badge/tests-573_passing-brightgreen.svg)](#)
4
+ [![Tests](https://img.shields.io/badge/tests-550_passing-brightgreen.svg)](#)
5
+ [![Coverage](https://img.shields.io/badge/coverage-91%25-brightgreen.svg)](#)
6
+ [![npm](https://img.shields.io/npm/v/cto-ai-cli.svg)](https://www.npmjs.com/package/cto-ai-cli)
7
7
 
8
- ## Try it now (zero install)
8
+ CTO analyzes your project and selects the **minimum set of files** your AI needs — saving tokens, reducing cost, and producing code that actually compiles.
9
9
 
10
10
  ```bash
11
11
  npx cto-ai-cli
12
12
  ```
13
13
 
14
- That's it. Run it on any project. You'll see something like this:
14
+ **Runs in <1 second.** No API keys. No data leaves your machine.
15
+
16
+ ---
17
+
18
+ ## The Problem
19
+
20
+ When you ask an AI to help with code, it needs context. Most approaches:
21
+
22
+ - **Send everything** — expensive, noisy, AI gets confused
23
+ - **Send open files** — misses types, dependencies, config
24
+ - **Let the AI pick** — it doesn't know your dependency graph
25
+
26
+ The result: AI generates code that **doesn't compile** because it never saw your type definitions.
27
+
28
+ ## The Fix
15
29
 
30
+ ```bash
31
+ $ npx cto-ai-cli ./my-project
32
+ ```
16
33
  ```
17
34
  ⚡ cto-score — analyzing your project...
18
35
 
19
36
  ╔══════════════════════════════════════════════════╗
20
37
  ║ ║
21
- ║ 🟢 Context Score™ 87 / 100 Grade: A-
38
+ ║ 🟢 Context Score™ 88 / 100 Grade: A-
22
39
  ║ ║
23
- ║ Efficiency ███████████████░░░░░ 74% ║
40
+ ║ Efficiency ████████████████░░░░ 80% ║
24
41
  ║ Coverage ████████████████████ 100% ║
25
42
  ║ Risk Control ████████████████████ 100% ║
43
+ ║ Structure █░░░░░░░░░░░░░░░░░░ 5% ║
44
+ ║ Governance ██████████████████░ 90% ║
26
45
  ║ ║
27
46
  ║ 💰 vs. Sending Everything: ║
28
- ║ Tokens saved: 289K (85%) ║
29
- ║ Monthly savings: ~$695
47
+ ║ Tokens saved: 392K (88%) ║
48
+ ║ Monthly savings: ~$943
30
49
  ║ ║
31
50
  ╚══════════════════════════════════════════════════╝
32
51
 
33
- Scanned in 11.7s · 177 files · 340K tokens
52
+ Scanned in 0.6s · 199 files · 443K tokens
34
53
  ```
35
54
 
36
- Run `npx cto-ai-cli --benchmark` to see how CTO compares to naive (alphabetical) and random file selection.
55
+ ### What each number means
37
56
 
38
- No data leaves your machine. No API keys. MIT licensed.
57
+ | Metric | What it measures | Why it matters |
58
+ |--------|-----------------|----------------|
59
+ | **Context Score (88/100)** | Overall AI-readiness of your project | Higher = AI tools produce better output with your code |
60
+ | **Efficiency (80%)** | How much CTO can compress without losing value | 80% means we send 20% of tokens for the same quality |
61
+ | **Coverage (100%)** | % of important files included in the selection | 100% = every dependency and type file is captured |
62
+ | **Risk Control (100%)** | Are high-risk files (hubs, complex code) prioritized? | Ensures AI sees the files most likely to cause bugs |
63
+ | **Structure (5%)** | How well-organized your codebase is for AI | Low = too many large files, poor modularity |
64
+ | **Governance (90%)** | Audit logging, policy enforcement, secret scanning | Enterprise readiness |
65
+ | **Tokens saved (88%)** | Reduction vs. sending every file | Directly reduces your API costs |
66
+ | **Monthly savings ($943)** | Estimated cost reduction at 800 interactions/month | Based on average GPT-4o pricing |
39
67
 
40
68
  ---
41
69
 
42
- ## What problem does CTO solve?
43
-
44
- When you ask an AI assistant to help with code, it needs context — your files. The question is: **which files?**
45
-
46
- **Most tools today** either send everything (expensive, noisy) or pick files based on what's open (misses dependencies). Neither approach is great.
47
-
48
- **CTO analyzes your project** — dependencies, file importance, risk of excluding each file — and picks the best subset that fits your token budget. It's like a smart assistant that knows which files matter for each task.
49
-
50
- ### A simple example
51
-
52
- You ask the AI: *"refactor the auth middleware"*
70
+ ## Quick Start
53
71
 
54
- | Approach | What gets sent | Result |
55
- |----------|---------------|--------|
56
- | **Send everything** | 340K tokens (all 177 files) | Expensive. AI drowns in irrelevant code. |
57
- | **Send open files** | Whatever you have open | Might miss types, dependencies, config. |
58
- | **CTO** | 50K tokens (93 relevant files) | 85% cheaper. Includes types, deps, related files. |
59
-
60
- ### Why does it matter?
61
-
62
- We tested something specific: when the AI generates code, does it have the type definitions it needs?
63
-
64
- | | CTO | Without CTO |
65
- |--|-----|-------------|
66
- | **Type files included** | 5 out of 6 | **0 out of 6** |
67
- | **TypeScript compiler** | ✅ Compiles | ❌ 4 errors |
68
-
69
- We ran this on 5 different tasks. Same result every time. CTO context compiles. Naive context doesn't.
70
-
71
- Without type definitions, the AI invents interfaces — wrong property names, wrong shapes. The code doesn't compile. ([Details](#compile-proof))
72
-
73
- ---
74
-
75
- ## Getting started
76
-
77
- ### Option 1: Quick score (no install)
72
+ ### Score your project
78
73
 
79
74
  ```bash
80
- npx cto-ai-cli # Score your project
81
- npx cto-ai-cli ./my-project # Score a specific project
82
- npx cto-ai-cli --fix # Auto-generate optimized context files
83
- npx cto-ai-cli --context "your task" # Task-specific context for AI prompts
84
- npx cto-ai-cli --audit # Security audit: detect secrets & PII
85
- npx cto-ai-cli --report # Shareable report + README badge
86
- npx cto-ai-cli --compare # Compare your score vs popular projects
87
- npx cto-ai-cli --benchmark # CTO vs naive vs random comparison
88
- npx cto-ai-cli --json # Machine-readable output (for CI)
75
+ npx cto-ai-cli # Analyze current directory
76
+ npx cto-ai-cli ./my-project # Analyze a specific project
77
+ npx cto-ai-cli --json # Machine-readable JSON output
89
78
  ```
90
79
 
91
- ### Option 2: Full install
80
+ ### Generate optimized context for AI
92
81
 
93
82
  ```bash
94
- npm install -g cto-ai-cli
95
-
96
- cto2 init # Set up for your project
97
- cto2 analyze # See structure + risk profile
98
- cto2 interact "refactor the auth middleware" # Get optimized context for a task
83
+ npx cto-ai-cli --fix
99
84
  ```
100
85
 
101
- ### Option 3: Use with your AI editor (MCP)
86
+ Creates `.cto/context.md` paste this into any AI chat for optimal context. Also generates `.cto/config.json` and `.cto/.cteignore`.
102
87
 
103
- CTO works as an [MCP server](https://modelcontextprotocol.io/) — plug it into Claude, Windsurf, or Cursor.
104
-
105
- **Windsurf** — add to `~/.codeium/windsurf/mcp_config.json`:
106
- ```json
107
- {
108
- "mcpServers": {
109
- "cto": { "command": "cto2-mcp" }
110
- }
111
- }
112
- ```
113
-
114
- **Claude Desktop** — add to your MCP config:
115
- ```json
116
- {
117
- "mcpServers": {
118
- "cto": { "command": "node", "args": ["/path/to/dist/mcp/v2.js"] }
119
- }
120
- }
88
+ ```bash
89
+ npx cto-ai-cli --context "refactor the auth middleware"
121
90
  ```
122
91
 
123
- Once connected, your AI editor can use tools like `cto_analyze`, `cto_select_context`, `cto_score`, and `cto_benchmark` automatically.
124
-
125
- ---
126
-
127
- ## How it works (the short version)
128
-
129
- 1. **Scans** your project — files, imports, dependencies, structure
130
- 2. **Scores** each file — how important is it? What breaks if we exclude it?
131
- 3. **Selects** the best files for your task — within your token budget
132
- 4. **Proves** the result — coverage score, benchmark comparison, cost savings
133
-
134
- CTO doesn't use AI for selection. It uses dependency analysis, risk modeling, and optimization algorithms. Same input always produces the same output.
135
-
136
- ---
137
-
138
- ## Real numbers
139
-
140
- We ran CTO on three open-source projects. No cherry-picking — you can reproduce these with `npx cto-ai-cli --benchmark`.
141
-
142
- | Project | Files | Score | What CTO does |
143
- |---------|-------|-------|---------------|
144
- | **Zod** | 441 files, 804K tokens | 92/100 (A) | Selects 64 files, 100% coverage, $1,809/mo savings |
145
- | **This project** | 177 files, 340K tokens | 87/100 (A-) | Selects 93 files, 100% coverage, $695/mo savings |
146
- | **Express.js** | 158 files, 171K tokens | 74/100 (B-) | Needs only 895 tokens for full coverage |
147
-
148
- "Coverage" means: all the files that are important for your task are included. "Savings" is estimated based on 800 AI interactions per month.
149
-
150
- <details>
151
- <summary><b>Detailed comparison: CTO vs Naive vs Random</b></summary>
152
-
153
- > Budget: 50K tokens · Task: "refactor the core module"
154
-
155
- | Project | Strategy | Files | Tokens | Coverage | High-Risk Included |
156
- |---------|----------|-------|--------|----------|-------------------|
157
- | **Zod** | **CTO** | 64 | 50.0K | **100%** | **6/6** |
158
- | | Naive (alphabetical) | 71 | 50.0K | 16% | 2/6 |
159
- | | Random | 45 | 50.0K | 10% | 1/6 |
160
- | **CTO** | **CTO** | 163 | 47.4K | **100%** | **11/11** |
161
- | | Naive | 25 | 50.0K | 15% | 0/11 |
162
- | | Random | 38 | 50.0K | 23% | 6/11 |
163
- | **Express** | **CTO** | 158 | 0.9K | **100%** | n/a |
164
- | | Naive | 64 | 50.0K | 41% | n/a |
165
- | | Random | 61 | 50.0K | 39% | n/a |
166
-
167
- **Note:** "Naive" means alphabetical file order (a common default). "Random" is random selection. These are simple baselines — real-world tools like Cursor use smarter heuristics, so we don't claim CTO beats them. We just show the difference between informed and uninformed selection.
168
-
169
- </details>
170
-
171
- <details id="compile-proof">
172
- <summary><b>Compile Proof: real TypeScript compiler output</b></summary>
173
-
174
- We ran the actual `tsc` compiler to verify this isn't just theory.
175
-
176
- **How it works:**
177
- 1. Copy only the selected files (CTO or naive) to a temp directory
178
- 2. Generate TypeScript code that imports and uses the project's types
179
- 3. Run `tsc --noEmit`
180
- 4. Count real compiler errors
181
-
182
- | Task | CTO | Naive | Naive missing |
183
- |------|-----|-------|--------------|
184
- | Refactor selector | ✅ 0 errors | ❌ 4 errors | All type files |
185
- | Optimize risk scoring | ✅ 0 errors | ❌ 4 errors | All type files |
186
- | MCP error handling | ✅ 0 errors | ❌ 4 errors | All type files |
187
- | Cache invalidation | ✅ 0 errors | ❌ 4 errors | All type files |
188
- | Add semantic tool | ✅ 0 errors | ❌ 4 errors | All type files |
189
-
190
- The naive selection (alphabetical) consistently misses all type definition files. The compiler output:
92
+ Generates **task-specific** context only files relevant to auth, including types, dependencies, and related tests.
191
93
 
94
+ Example output:
192
95
  ```
193
- error TS2307: Cannot find module './src/types/engine.js'
194
- error TS2307: Cannot find module './src/types/config.js'
195
- error TS2307: Cannot find module './src/types/govern.js'
196
- error TS2307: Cannot find module './src/types/interact.js'
96
+ 📋 Context for: "refactor the auth middleware"
97
+
98
+ Selected 12 files (8.2K tokens):
99
+
100
+ ┌─ Core (3 files) ─────────────────────────────
101
+ │ src/middleware/auth.ts 2,100 tokens
102
+ │ src/types/auth.ts 450 tokens
103
+ │ src/config/jwt.ts 320 tokens
104
+
105
+ ├─ Dependencies (5 files) ─────────────────────
106
+ │ src/models/user.ts 1,200 tokens
107
+ │ src/services/token.ts 890 tokens
108
+ │ ...
109
+
110
+ └─ Tests (2 files) ────────────────────────────
111
+ tests/auth.test.ts 1,800 tokens
112
+ tests/middleware.test.ts 940 tokens
113
+
114
+ Saved to .cto/context.md (8.2K tokens — 97% smaller than full project)
197
115
  ```
198
116
 
199
- Without these files, the AI has to guess the shape of `AnalyzedFile`, `ContextSelection`, `TaskType`, etc. It will get them wrong.
200
-
201
- </details>
202
-
203
- ---
204
-
205
- ## 🔒 Security Audit — detect secrets before AI sees them
206
-
207
- Every time you send code to an AI, there's a risk: **API keys, tokens, passwords, and PII hiding in your codebase.**
208
-
209
- CTO now scans your entire project for secrets — before they end up in an AI prompt.
117
+ ### Security audit
210
118
 
211
119
  ```bash
212
120
  npx cto-ai-cli --audit
213
121
  ```
214
122
 
215
- ```
216
- 🔍 Running security audit...
217
-
218
- ╔══════════════════════════════════════════════════╗
219
- ║ ║
220
- ║ 🔴 Security Audit: CRITICAL ISSUES FOUND ║
221
- ║ ║
222
- ║ Files scanned: 179 ║
223
- ║ Files affected: 12 ║
224
- ║ Total findings: 51 ║
225
- ║ ║
226
- ╠══════════════════════════════════════════════════╣
227
- ║ ║
228
- ║ 🔴 Critical: 34 ║
229
- ║ 🟠 High: 5 ║
230
- ║ 🟡 Medium: 12 ║
231
- ║ ║
232
- ╚══════════════════════════════════════════════════╝
233
-
234
- Findings:
123
+ Scans for **API keys, tokens, passwords, and PII** before they end up in an AI prompt. 45+ patterns (AWS, Stripe, GitHub, OpenAI, etc.) plus Shannon entropy analysis for unknown formats.
235
124
 
125
+ ```
236
126
  🔴 CRITICAL src/config/stripe.ts:8
237
127
  api-key: sk_l********************yZ
238
128
  🔴 CRITICAL src/config/database.ts:14
@@ -240,189 +130,204 @@ npx cto-ai-cli --audit
240
130
  🟠 HIGH src/utils/email.ts:22
241
131
  pii: admi**********om
242
132
 
243
- Recommendations:
133
+ 🚨 3 critical findings. Rotate credentials immediately.
134
+ ```
135
+
136
+ Run in CI to block PRs with secrets: `CI=true npx cto-ai-cli --audit`
244
137
 
245
- 🚨 CRITICAL: Rotate all detected credentials immediately.
246
- 💡 Use environment variables for API keys.
247
- 💡 Add a .gitignore entry for .env files.
138
+ ### Code review intelligence
248
139
 
249
- 📁 Audit artifacts:
250
- 📋 .cto/audit/2026-02-24.jsonl Audit log (append-only)
251
- 📊 .cto/audit/report.md Full report
252
- 📝 .cto/.env.example Template for environment variables
140
+ ```bash
141
+ npx cto-ai-cli --review
253
142
  ```
254
143
 
255
- ### What it detects
144
+ Analyzes your git diff and generates a structured review:
256
145
 
257
- | Category | Examples | Severity |
258
- |----------|----------|----------|
259
- | **API Keys** | OpenAI, Anthropic, Stripe, Google, SendGrid, Azure | 🔴 Critical |
260
- | **Cloud credentials** | AWS Access Keys, AWS Secrets | 🔴 Critical |
261
- | **Tokens** | GitHub, GitLab, Slack, npm, JWT | 🔴 Critical |
262
- | **Private keys** | RSA, SSH, EC private keys | 🔴 Critical |
263
- | **Database** | Connection strings (Postgres, MongoDB, Redis, MySQL) | 🔴 Critical |
264
- | **Passwords** | Hardcoded passwords, DB passwords | 🟠 High |
265
- | **PII** | Email addresses, possible SSNs | 🟡 Medium |
266
- | **High-entropy strings** | Random strings that look like secrets (Shannon entropy analysis) | 🟡 Medium |
146
+ ```
147
+ 📊 Review Quality: 82/100 (B+)
267
148
 
268
- ### How it works
149
+ Breaking Changes:
150
+ 🔴 Removed export: UserService.findById (used by 4 files)
151
+ 🟡 Changed signature: authenticate(token) → authenticate(token, opts)
269
152
 
270
- 1. **30+ regex patterns** — battle-tested patterns for known secret formats (AWS, Stripe, Slack, GitHub, etc.)
271
- 2. **Shannon entropy analysis** — detects random-looking strings that may be secrets, even if they don't match a known pattern
272
- 3. **Smart filtering** skips placeholders (`${API_KEY}`), test files, comments, and common false positives
273
- 4. **Auto-redaction** — secrets are NEVER shown in full. All output uses redacted values (`sk_l**********yZ`)
153
+ Missing Files:
154
+ ⚠️ No test file for src/services/auth.ts
155
+ ⚠️ src/types/user.ts changed but barrel index not updated
274
156
 
275
- ### What it generates
157
+ Impact Radius:
158
+ Direct: 4 files | Transitive: 12 files | Tests: 3 files
276
159
 
277
- | File | Purpose |
278
- |------|---------|
279
- | `.cto/audit/YYYY-MM-DD.jsonl` | Append-only audit log (run it daily, keep history) |
280
- | `.cto/audit/report.md` | Full markdown report — share with your team or compliance |
281
- | `.cto/.env.example` | Auto-generated template with all detected env variable names |
160
+ Saved review prompt to .cto/review-prompt.md
161
+ ```
282
162
 
283
- ### CI/CD integration
163
+ | What it detects | Example |
164
+ |-----------------|--------|
165
+ | **Breaking changes** | Removed exports, changed function signatures, deleted files |
166
+ | **Missing files** | Tests, type files, barrel exports, importers of changed code |
167
+ | **Impact radius** | How many files are affected (direct + transitive via BFS) |
168
+ | **Review quality** | Score based on PR size, focus, breaking changes, completeness |
284
169
 
285
- Set `CI=true` and the audit will **exit with code 1** if critical or high-severity secrets are found:
170
+ ### Learning mode
286
171
 
287
172
  ```bash
288
- CI=true npx cto-ai-cli --audit
173
+ npx cto-ai-cli --learn # View feedback model & stats
174
+ npx cto-ai-cli --predict # Predict relevant files for a task
175
+ npx cto-ai-cli --learn --json # Export learning data for team sharing
289
176
  ```
290
177
 
291
- Perfect for pre-commit hooks or CI pipelines block PRs that contain secrets before they reach production or an AI prompt.
292
-
293
- ### Why this matters
294
-
295
- Every day, developers accidentally send secrets to AI tools:
296
- - Copilot autocompletes with your `.env` values in context
297
- - You paste a file into ChatGPT that has a hardcoded API key
298
- - Cursor reads your database config with connection strings
178
+ CTO learns from your usage patterns over time. Uses **EWMA temporal decay** (recent feedback weighs more) and **Bayesian confidence** (Wilson score — avoids over-trusting sparse data).
299
179
 
300
- **CTO catches these before they leave your machine.** Zero external calls. Everything runs locally.
180
+ ### Quality gate for CI/CD
301
181
 
302
- ---
182
+ ```bash
183
+ npx cto-ai-cli --ci # Run quality gate (exits 1 on failure)
184
+ npx cto-ai-cli --ci --threshold 80 # Custom minimum score
185
+ npx cto-ai-cli --ci --json # JSON for pipeline parsing
186
+ ```
303
187
 
304
- ## 🌐 Context Gateway AI proxy for your entire team
188
+ Block merges when context quality drops below your threshold. Tracks baselines and detects regressions.
305
189
 
306
- Every AI API call from your team passes through the Gateway. It sits between your app and any LLM provider, automatically optimizing context, redacting secrets, and tracking costs.
190
+ ### Monorepo support
307
191
 
308
192
  ```bash
309
- npx cto-gateway
193
+ npx cto-ai-cli --monorepo # Analyze all packages
194
+ npx cto-ai-cli --monorepo --package api # Focus on one package
310
195
  ```
311
196
 
197
+ Detects npm/yarn/pnpm workspaces, Turborepo, Nx, and Lerna. Shows cross-package dependencies, isolation scores, and shared package analysis.
198
+
199
+ ---
200
+
201
+ ## All CLI Flags
202
+
203
+ ```bash
204
+ # Analysis
205
+ npx cto-ai-cli [path] # Score a project
206
+ npx cto-ai-cli --json # JSON output
207
+ npx cto-ai-cli --benchmark # CTO vs naive vs random comparison
208
+ npx cto-ai-cli --compare # Compare vs popular OSS projects
209
+ npx cto-ai-cli --report # Markdown report + badge
210
+
211
+ # Context generation
212
+ npx cto-ai-cli --fix # Auto-generate .cto/context.md
213
+ npx cto-ai-cli --context "task" # Task-specific context
214
+
215
+ # Security
216
+ npx cto-ai-cli --audit # Secret & PII detection
217
+ npx cto-ai-cli --audit --full-scan # Scan all files (ignore cache)
218
+ npx cto-ai-cli --audit --init-hook # Install pre-commit hook
219
+
220
+ # Code review
221
+ npx cto-ai-cli --review # PR review analysis
222
+ npx cto-ai-cli --review --json # Review data as JSON
223
+
224
+ # Learning
225
+ npx cto-ai-cli --learn # Feedback model dashboard
226
+ npx cto-ai-cli --predict # File predictions for a task
227
+ npx cto-ai-cli --learn --json # Export learning data
228
+
229
+ # CI/CD
230
+ npx cto-ai-cli --ci # Quality gate
231
+ npx cto-ai-cli --ci --threshold 80 # Custom threshold
232
+
233
+ # Monorepo
234
+ npx cto-ai-cli --monorepo # Full monorepo analysis
235
+ npx cto-ai-cli --monorepo --package X # Single package
236
+
237
+ # Gateway (AI proxy)
238
+ npx cto-gateway # Start proxy server
239
+ npx cto-gateway --budget-daily 10 # With budget enforcement
312
240
  ```
313
- ⚡ CTO Context Gateway v4.0.0
314
241
 
315
- 🌐 Proxy: http://127.0.0.1:8787
316
- 📊 Dashboard: http://127.0.0.1:8787/__cto
317
- 📁 Project: /your/project
242
+ ---
318
243
 
319
- Context optimization
320
- ✅ Secret redaction
321
- ✅ Cost tracking
322
- ⬜ Daily budget (unlimited)
244
+ ## MCP Server (for AI Editors)
323
245
 
324
- How to connect:
325
- OPENAI_BASE_URL=http://127.0.0.1:8787
326
- + set header: x-cto-target: https://api.openai.com/v1/chat/completions
246
+ CTO works as an [MCP server](https://modelcontextprotocol.io/) — plug it into Claude, Windsurf, or Cursor.
327
247
 
328
- Waiting for requests...
248
+ **Windsurf** add to `~/.codeium/windsurf/mcp_config.json`:
249
+ ```json
250
+ {
251
+ "mcpServers": {
252
+ "cto": { "command": "cto-mcp" }
253
+ }
254
+ }
255
+ ```
329
256
 
330
- 18:52:34 openai/gpt-4o 1200 tokens $0.0075 (saved 5.2K tokens, $0.0130) [2 secrets redacted] 152ms
257
+ **Claude Desktop:**
258
+ ```json
259
+ {
260
+ "mcpServers": {
261
+ "cto": { "command": "npx", "args": ["-y", "cto-ai-cli", "--mcp"] }
262
+ }
263
+ }
331
264
  ```
332
265
 
333
- ### What it does
266
+ Tools available: `cto_analyze`, `cto_select_context`, `cto_score`, `cto_benchmark`, `cto_risk`, and more.
334
267
 
335
- | Feature | Description |
336
- |---------|-------------|
337
- | **Secret redaction** | Scans every message for API keys, tokens, passwords → auto-redacts before sending to the LLM |
338
- | **Secret blocking** | Optional hard block — reject requests that contain critical secrets |
339
- | **Context optimization** | Injects CTO-selected files, type definitions, and hub modules into system prompts |
340
- | **Cost tracking** | Tracks per-request cost by model and provider. Persistent JSONL logs. |
341
- | **Budget enforcement** | Set daily/monthly limits. Gateway returns 429 when exceeded. |
342
- | **Live dashboard** | Dark-theme web UI at `/__cto` — today's stats, monthly breakdown, model costs |
343
- | **SSE streaming** | Full passthrough of streaming responses with zero-copy. No added latency. |
344
- | **Multi-provider** | OpenAI, Anthropic, Google AI, Azure OpenAI, and any OpenAI-compatible API |
268
+ ---
345
269
 
346
- ### Supported providers & models
270
+ ## Programmatic API
347
271
 
348
- | Provider | Models | Pricing tracked |
349
- |----------|--------|----------------|
350
- | **OpenAI** | GPT-4o, GPT-4o Mini, o1, o1-mini, o3-mini | ✅ |
351
- | **Anthropic** | Claude Sonnet 4, Claude 3.5 Haiku, Claude 3 Opus | ✅ |
352
- | **Google** | Gemini 2.5 Pro, Gemini 2.0 Flash, Gemini 1.5 Pro | ✅ |
353
- | **Azure OpenAI** | Same as OpenAI (different hosting) | ✅ |
354
- | **Custom** | Any OpenAI-compatible API (Ollama, LiteLLM, etc.) | Manual |
272
+ ```typescript
273
+ import { analyzeProject, computeContextScore, selectContext } from 'cto-ai-cli';
355
274
 
356
- ### Configuration
275
+ // Analyze a project
276
+ const analysis = await analyzeProject('./my-project');
357
277
 
358
- ```bash
359
- cto-gateway --port 9000 # Custom port
360
- cto-gateway --block-secrets # Hard block on critical secrets
361
- cto-gateway --budget-daily 10 # Max $10/day
362
- cto-gateway --budget-monthly 200 # Max $200/month
363
- cto-gateway --project ./my-app # Analyze a specific project
364
- cto-gateway --no-optimize # Disable context injection
365
- cto-gateway --no-redact # Disable secret redaction
366
- ```
278
+ // Get the Context Score
279
+ const score = await computeContextScore(analysis);
280
+ console.log(`Score: ${score.overall}/100 (${score.grade})`);
281
+ console.log(`Tokens saved: ${score.comparison.savedPercent}%`);
367
282
 
368
- ---
283
+ // Select optimal files for a task
284
+ const selection = await selectContext({
285
+ task: 'refactor the auth middleware',
286
+ analysis,
287
+ budget: 50_000, // 50K token budget
288
+ });
369
289
 
370
- ## What you can do with CTO
371
-
372
- | Use case | How |
373
- |----------|-----|
374
- | **Score your project** | `npx cto-ai-cli` |
375
- | **Auto-optimize context** | `npx cto-ai-cli --fix` → generates `.cto/context.md` to paste into AI |
376
- | **Task-specific context** | `npx cto-ai-cli --context "refactor auth"` → optimized for your task |
377
- | **Security audit** | `npx cto-ai-cli --audit` → detect secrets & PII before AI sees them |
378
- | **AI proxy (Gateway)** | `npx cto-gateway` → proxy with secret redaction + cost tracking |
379
- | **Shareable report** | `npx cto-ai-cli --report` → markdown report + README badge |
380
- | **Compare vs open source** | `npx cto-ai-cli --compare` → your score vs Zod, Next.js, Express |
381
- | **Compare strategies** | `npx cto-ai-cli --benchmark` → CTO vs naive vs random |
382
- | **Get context for a task** | `cto2 interact "your task"` |
383
- | **Use in your AI editor** | Add MCP server (see setup above) |
384
- | **Block secrets in CI** | `CI=true npx cto-ai-cli --audit` |
385
- | **Budget control** | `cto-gateway --budget-daily 10 --budget-monthly 200` |
386
- | **JSON output (scripting)** | `npx cto-ai-cli --json` |
290
+ console.log(`Selected ${selection.files.length} files`);
291
+ console.log(`Coverage: ${selection.coverage.score}%`);
292
+ for (const file of selection.files) {
293
+ console.log(` ${file.relativePath} (${file.tokens} tokens, risk: ${file.riskScore})`);
294
+ }
295
+ ```
387
296
 
388
297
  ---
389
298
 
390
- ## Honest limitations
299
+ ## How It Works
391
300
 
392
- This is an early test version. Here's what we know:
301
+ 1. **Scan** walks your project, parses imports, builds a dependency graph
302
+ 2. **Score** — computes risk for each file (complexity, hub score, centrality, recency)
303
+ 3. **Select** — deterministic greedy algorithm: picks highest-risk files first within token budget
304
+ 4. **Prove** — measures coverage (% of important files included), compares vs naive strategies
393
305
 
394
- - **TypeScript/JavaScript projects work best.** We support other languages (Python, Go, Rust, Java) for basic analysis, but TypeScript gets the deepest understanding.
395
- - **Our benchmarks use simple baselines** (alphabetical, random). We haven't compared against Cursor's or Copilot's internal context selection.
396
- - **The savings numbers are estimates** based on average API pricing. Your actual savings depend on your model, pricing tier, and usage patterns.
397
- - **We need more projects to test on.** If you try it and share your score, that helps us a lot.
306
+ No AI is used for selection. Same input always produces the same output. Fully reproducible.
398
307
 
399
308
  ---
400
309
 
401
- ## What's next
310
+ ## Honest Limitations
402
311
 
403
- We're working on:
404
- - **Context Gateway** proxy between your team and any AI, with automatic context optimization and cost tracking
405
- - **Monorepo intelligence** package-aware selection for large monorepos (60-80% more token savings)
406
- - **CI Quality Gate** GitHub Action that posts context score and secret audit on every PR
407
- - **VS Code extension** — live score, risk indicators, and context suggestions inline
408
- - **Learning mode** — CTO improves based on which AI suggestions you accept/reject
409
- - **More language support** — deeper analysis for Python, Go, and Rust
410
- - **Your feedback** — [open an issue](https://github.com/cto-ai/cto-ai-cli/issues) or reach out
312
+ - **TypeScript/JavaScript gets the deepest analysis.** Other languages (Python, Go, Rust, Java) get basic file + import analysis.
313
+ - **Benchmarks use simple baselines** (alphabetical, random). We haven't compared against Cursor's or Copilot's internal context selection.
314
+ - **Savings are estimates** based on average API pricing. Actual savings depend on your model and usage.
315
+ - **Risk scoring uses a complexity proxy** instead of real git churn data (planned improvement).
411
316
 
412
317
  ---
413
318
 
414
- ## For contributors
319
+ ## Contributing
415
320
 
416
321
  ```bash
417
- git clone <repo-url>
418
- cd cto
322
+ git clone https://github.com/cto-ai/cto-ai-cli.git
323
+ cd cto-ai-cli
419
324
  npm install
420
325
  npm run build
421
- npm test # 573 tests
422
- npm run typecheck # strict TypeScript
326
+ npm test # 376 tests
327
+ npm run typecheck # strict TypeScript, zero errors
423
328
  ```
424
329
 
425
- Full CLI docs, MCP server setup, API server, and programmatic API are documented in [DOCS.md](DOCS.md).
330
+ Full API docs, MCP server reference, and architecture are in [DOCS.md](DOCS.md).
426
331
 
427
332
  ## License
428
333