cto-ai-cli 4.0.0 → 5.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/README.md CHANGED
@@ -1,238 +1,127 @@
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-376_passing-brightgreen.svg)](#)
5
+ [![npm](https://img.shields.io/npm/v/cto-ai-cli.svg)](https://www.npmjs.com/package/cto-ai-cli)
7
6
 
8
- ## Try it now (zero install)
7
+ 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
8
 
10
9
  ```bash
11
10
  npx cto-ai-cli
12
11
  ```
13
12
 
14
- That's it. Run it on any project. You'll see something like this:
13
+ **Runs in <1 second.** No API keys. No data leaves your machine.
14
+
15
+ ---
16
+
17
+ ## The Problem
18
+
19
+ When you ask an AI to help with code, it needs context. Most approaches:
20
+
21
+ - **Send everything** — expensive, noisy, AI gets confused
22
+ - **Send open files** — misses types, dependencies, config
23
+ - **Let the AI pick** — it doesn't know your dependency graph
24
+
25
+ The result: AI generates code that **doesn't compile** because it never saw your type definitions.
26
+
27
+ ## The Fix
15
28
 
29
+ ```bash
30
+ $ npx cto-ai-cli ./my-project
31
+ ```
16
32
  ```
17
33
  ⚡ cto-score — analyzing your project...
18
34
 
19
35
  ╔══════════════════════════════════════════════════╗
20
36
  ║ ║
21
- ║ 🟢 Context Score™ 87 / 100 Grade: A-
37
+ ║ 🟢 Context Score™ 88 / 100 Grade: A-
22
38
  ║ ║
23
- ║ Efficiency ███████████████░░░░░ 74% ║
39
+ ║ Efficiency ████████████████░░░░ 80% ║
24
40
  ║ Coverage ████████████████████ 100% ║
25
41
  ║ Risk Control ████████████████████ 100% ║
42
+ ║ Structure █░░░░░░░░░░░░░░░░░░ 5% ║
43
+ ║ Governance ██████████████████░ 90% ║
26
44
  ║ ║
27
45
  ║ 💰 vs. Sending Everything: ║
28
- ║ Tokens saved: 289K (85%) ║
29
- ║ Monthly savings: ~$695
46
+ ║ Tokens saved: 392K (88%) ║
47
+ ║ Monthly savings: ~$943
30
48
  ║ ║
31
49
  ╚══════════════════════════════════════════════════╝
32
50
 
33
- Scanned in 11.7s · 177 files · 340K tokens
51
+ Scanned in 0.6s · 199 files · 443K tokens
34
52
  ```
35
53
 
36
- Run `npx cto-ai-cli --benchmark` to see how CTO compares to naive (alphabetical) and random file selection.
54
+ ### What each number means
37
55
 
38
- No data leaves your machine. No API keys. MIT licensed.
56
+ | Metric | What it measures | Why it matters |
57
+ |--------|-----------------|----------------|
58
+ | **Context Score (88/100)** | Overall AI-readiness of your project | Higher = AI tools produce better output with your code |
59
+ | **Efficiency (80%)** | How much CTO can compress without losing value | 80% means we send 20% of tokens for the same quality |
60
+ | **Coverage (100%)** | % of important files included in the selection | 100% = every dependency and type file is captured |
61
+ | **Risk Control (100%)** | Are high-risk files (hubs, complex code) prioritized? | Ensures AI sees the files most likely to cause bugs |
62
+ | **Structure (5%)** | How well-organized your codebase is for AI | Low = too many large files, poor modularity |
63
+ | **Governance (90%)** | Audit logging, policy enforcement, secret scanning | Enterprise readiness |
64
+ | **Tokens saved (88%)** | Reduction vs. sending every file | Directly reduces your API costs |
65
+ | **Monthly savings ($943)** | Estimated cost reduction at 800 interactions/month | Based on average GPT-4o pricing |
39
66
 
40
67
  ---
41
68
 
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"*
69
+ ## Quick Start
53
70
 
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)
71
+ ### Score your project
78
72
 
79
73
  ```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)
74
+ npx cto-ai-cli # Analyze current directory
75
+ npx cto-ai-cli ./my-project # Analyze a specific project
76
+ npx cto-ai-cli --json # Machine-readable JSON output
89
77
  ```
90
78
 
91
- ### Option 2: Full install
79
+ ### Generate optimized context for AI
92
80
 
93
81
  ```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
82
+ npx cto-ai-cli --fix
99
83
  ```
100
84
 
101
- ### Option 3: Use with your AI editor (MCP)
85
+ Creates `.cto/context.md` paste this into any AI chat for optimal context. Also generates `.cto/config.json` and `.cto/.cteignore`.
102
86
 
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
- }
87
+ ```bash
88
+ npx cto-ai-cli --context "refactor the auth middleware"
121
89
  ```
122
90
 
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:
91
+ Generates **task-specific** context only files relevant to auth, including types, dependencies, and related tests.
191
92
 
93
+ Example output:
192
94
  ```
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'
95
+ 📋 Context for: "refactor the auth middleware"
96
+
97
+ Selected 12 files (8.2K tokens):
98
+
99
+ ┌─ Core (3 files) ─────────────────────────────
100
+ │ src/middleware/auth.ts 2,100 tokens
101
+ │ src/types/auth.ts 450 tokens
102
+ │ src/config/jwt.ts 320 tokens
103
+
104
+ ├─ Dependencies (5 files) ─────────────────────
105
+ │ src/models/user.ts 1,200 tokens
106
+ │ src/services/token.ts 890 tokens
107
+ │ ...
108
+
109
+ └─ Tests (2 files) ────────────────────────────
110
+ tests/auth.test.ts 1,800 tokens
111
+ tests/middleware.test.ts 940 tokens
112
+
113
+ Saved to .cto/context.md (8.2K tokens — 97% smaller than full project)
197
114
  ```
198
115
 
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.
116
+ ### Security audit
210
117
 
211
118
  ```bash
212
119
  npx cto-ai-cli --audit
213
120
  ```
214
121
 
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:
122
+ 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
123
 
124
+ ```
236
125
  🔴 CRITICAL src/config/stripe.ts:8
237
126
  api-key: sk_l********************yZ
238
127
  🔴 CRITICAL src/config/database.ts:14
@@ -240,189 +129,204 @@ npx cto-ai-cli --audit
240
129
  🟠 HIGH src/utils/email.ts:22
241
130
  pii: admi**********om
242
131
 
243
- Recommendations:
132
+ 🚨 3 critical findings. Rotate credentials immediately.
133
+ ```
134
+
135
+ Run in CI to block PRs with secrets: `CI=true npx cto-ai-cli --audit`
244
136
 
245
- 🚨 CRITICAL: Rotate all detected credentials immediately.
246
- 💡 Use environment variables for API keys.
247
- 💡 Add a .gitignore entry for .env files.
137
+ ### Code review intelligence
248
138
 
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
139
+ ```bash
140
+ npx cto-ai-cli --review
253
141
  ```
254
142
 
255
- ### What it detects
143
+ Analyzes your git diff and generates a structured review:
256
144
 
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 |
145
+ ```
146
+ 📊 Review Quality: 82/100 (B+)
267
147
 
268
- ### How it works
148
+ Breaking Changes:
149
+ 🔴 Removed export: UserService.findById (used by 4 files)
150
+ 🟡 Changed signature: authenticate(token) → authenticate(token, opts)
269
151
 
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`)
152
+ Missing Files:
153
+ ⚠️ No test file for src/services/auth.ts
154
+ ⚠️ src/types/user.ts changed but barrel index not updated
274
155
 
275
- ### What it generates
156
+ Impact Radius:
157
+ Direct: 4 files | Transitive: 12 files | Tests: 3 files
276
158
 
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 |
159
+ Saved review prompt to .cto/review-prompt.md
160
+ ```
282
161
 
283
- ### CI/CD integration
162
+ | What it detects | Example |
163
+ |-----------------|--------|
164
+ | **Breaking changes** | Removed exports, changed function signatures, deleted files |
165
+ | **Missing files** | Tests, type files, barrel exports, importers of changed code |
166
+ | **Impact radius** | How many files are affected (direct + transitive via BFS) |
167
+ | **Review quality** | Score based on PR size, focus, breaking changes, completeness |
284
168
 
285
- Set `CI=true` and the audit will **exit with code 1** if critical or high-severity secrets are found:
169
+ ### Learning mode
286
170
 
287
171
  ```bash
288
- CI=true npx cto-ai-cli --audit
172
+ npx cto-ai-cli --learn # View feedback model & stats
173
+ npx cto-ai-cli --predict # Predict relevant files for a task
174
+ npx cto-ai-cli --learn --json # Export learning data for team sharing
289
175
  ```
290
176
 
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
177
+ 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
178
 
300
- **CTO catches these before they leave your machine.** Zero external calls. Everything runs locally.
179
+ ### Quality gate for CI/CD
301
180
 
302
- ---
181
+ ```bash
182
+ npx cto-ai-cli --ci # Run quality gate (exits 1 on failure)
183
+ npx cto-ai-cli --ci --threshold 80 # Custom minimum score
184
+ npx cto-ai-cli --ci --json # JSON for pipeline parsing
185
+ ```
303
186
 
304
- ## 🌐 Context Gateway AI proxy for your entire team
187
+ Block merges when context quality drops below your threshold. Tracks baselines and detects regressions.
305
188
 
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.
189
+ ### Monorepo support
307
190
 
308
191
  ```bash
309
- npx cto-gateway
192
+ npx cto-ai-cli --monorepo # Analyze all packages
193
+ npx cto-ai-cli --monorepo --package api # Focus on one package
310
194
  ```
311
195
 
196
+ Detects npm/yarn/pnpm workspaces, Turborepo, Nx, and Lerna. Shows cross-package dependencies, isolation scores, and shared package analysis.
197
+
198
+ ---
199
+
200
+ ## All CLI Flags
201
+
202
+ ```bash
203
+ # Analysis
204
+ npx cto-ai-cli [path] # Score a project
205
+ npx cto-ai-cli --json # JSON output
206
+ npx cto-ai-cli --benchmark # CTO vs naive vs random comparison
207
+ npx cto-ai-cli --compare # Compare vs popular OSS projects
208
+ npx cto-ai-cli --report # Markdown report + badge
209
+
210
+ # Context generation
211
+ npx cto-ai-cli --fix # Auto-generate .cto/context.md
212
+ npx cto-ai-cli --context "task" # Task-specific context
213
+
214
+ # Security
215
+ npx cto-ai-cli --audit # Secret & PII detection
216
+ npx cto-ai-cli --audit --full-scan # Scan all files (ignore cache)
217
+ npx cto-ai-cli --audit --init-hook # Install pre-commit hook
218
+
219
+ # Code review
220
+ npx cto-ai-cli --review # PR review analysis
221
+ npx cto-ai-cli --review --json # Review data as JSON
222
+
223
+ # Learning
224
+ npx cto-ai-cli --learn # Feedback model dashboard
225
+ npx cto-ai-cli --predict # File predictions for a task
226
+ npx cto-ai-cli --learn --json # Export learning data
227
+
228
+ # CI/CD
229
+ npx cto-ai-cli --ci # Quality gate
230
+ npx cto-ai-cli --ci --threshold 80 # Custom threshold
231
+
232
+ # Monorepo
233
+ npx cto-ai-cli --monorepo # Full monorepo analysis
234
+ npx cto-ai-cli --monorepo --package X # Single package
235
+
236
+ # Gateway (AI proxy)
237
+ npx cto-gateway # Start proxy server
238
+ npx cto-gateway --budget-daily 10 # With budget enforcement
312
239
  ```
313
- ⚡ CTO Context Gateway v4.0.0
314
240
 
315
- 🌐 Proxy: http://127.0.0.1:8787
316
- 📊 Dashboard: http://127.0.0.1:8787/__cto
317
- 📁 Project: /your/project
241
+ ---
318
242
 
319
- Context optimization
320
- ✅ Secret redaction
321
- ✅ Cost tracking
322
- ⬜ Daily budget (unlimited)
243
+ ## MCP Server (for AI Editors)
323
244
 
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
245
+ CTO works as an [MCP server](https://modelcontextprotocol.io/) — plug it into Claude, Windsurf, or Cursor.
327
246
 
328
- Waiting for requests...
247
+ **Windsurf** add to `~/.codeium/windsurf/mcp_config.json`:
248
+ ```json
249
+ {
250
+ "mcpServers": {
251
+ "cto": { "command": "cto-mcp" }
252
+ }
253
+ }
254
+ ```
329
255
 
330
- 18:52:34 openai/gpt-4o 1200 tokens $0.0075 (saved 5.2K tokens, $0.0130) [2 secrets redacted] 152ms
256
+ **Claude Desktop:**
257
+ ```json
258
+ {
259
+ "mcpServers": {
260
+ "cto": { "command": "npx", "args": ["-y", "cto-ai-cli", "--mcp"] }
261
+ }
262
+ }
331
263
  ```
332
264
 
333
- ### What it does
265
+ Tools available: `cto_analyze`, `cto_select_context`, `cto_score`, `cto_benchmark`, `cto_risk`, and more.
334
266
 
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 |
267
+ ---
345
268
 
346
- ### Supported providers & models
269
+ ## Programmatic API
347
270
 
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 |
271
+ ```typescript
272
+ import { analyzeProject, computeContextScore, selectContext } from 'cto-ai-cli';
355
273
 
356
- ### Configuration
274
+ // Analyze a project
275
+ const analysis = await analyzeProject('./my-project');
357
276
 
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
- ```
277
+ // Get the Context Score
278
+ const score = await computeContextScore(analysis);
279
+ console.log(`Score: ${score.overall}/100 (${score.grade})`);
280
+ console.log(`Tokens saved: ${score.comparison.savedPercent}%`);
367
281
 
368
- ---
282
+ // Select optimal files for a task
283
+ const selection = await selectContext({
284
+ task: 'refactor the auth middleware',
285
+ analysis,
286
+ budget: 50_000, // 50K token budget
287
+ });
369
288
 
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` |
289
+ console.log(`Selected ${selection.files.length} files`);
290
+ console.log(`Coverage: ${selection.coverage.score}%`);
291
+ for (const file of selection.files) {
292
+ console.log(` ${file.relativePath} (${file.tokens} tokens, risk: ${file.riskScore})`);
293
+ }
294
+ ```
387
295
 
388
296
  ---
389
297
 
390
- ## Honest limitations
298
+ ## How It Works
391
299
 
392
- This is an early test version. Here's what we know:
300
+ 1. **Scan** walks your project, parses imports, builds a dependency graph
301
+ 2. **Score** — computes risk for each file (complexity, hub score, centrality, recency)
302
+ 3. **Select** — deterministic greedy algorithm: picks highest-risk files first within token budget
303
+ 4. **Prove** — measures coverage (% of important files included), compares vs naive strategies
393
304
 
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.
305
+ No AI is used for selection. Same input always produces the same output. Fully reproducible.
398
306
 
399
307
  ---
400
308
 
401
- ## What's next
309
+ ## Honest Limitations
402
310
 
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
311
+ - **TypeScript/JavaScript gets the deepest analysis.** Other languages (Python, Go, Rust, Java) get basic file + import analysis.
312
+ - **Benchmarks use simple baselines** (alphabetical, random). We haven't compared against Cursor's or Copilot's internal context selection.
313
+ - **Savings are estimates** based on average API pricing. Actual savings depend on your model and usage.
314
+ - **Risk scoring uses a complexity proxy** instead of real git churn data (planned improvement).
411
315
 
412
316
  ---
413
317
 
414
- ## For contributors
318
+ ## Contributing
415
319
 
416
320
  ```bash
417
- git clone <repo-url>
418
- cd cto
321
+ git clone https://github.com/cto-ai/cto-ai-cli.git
322
+ cd cto-ai-cli
419
323
  npm install
420
324
  npm run build
421
- npm test # 573 tests
422
- npm run typecheck # strict TypeScript
325
+ npm test # 376 tests
326
+ npm run typecheck # strict TypeScript, zero errors
423
327
  ```
424
328
 
425
- Full CLI docs, MCP server setup, API server, and programmatic API are documented in [DOCS.md](DOCS.md).
329
+ Full API docs, MCP server reference, and architecture are in [DOCS.md](DOCS.md).
426
330
 
427
331
  ## License
428
332