cto-ai-cli 1.3.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/LICENSE +21 -0
- package/README.md +326 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +6331 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/core/index.d.ts +717 -0
- package/dist/core/index.js +4446 -0
- package/dist/core/index.js.map +1 -0
- package/dist/mcp/index.d.ts +2 -0
- package/dist/mcp/index.js +15336 -0
- package/dist/mcp/index.js.map +1 -0
- package/package.json +85 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CTO Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
# π§ CTO β Claude Token Optimizer
|
|
2
|
+
|
|
3
|
+
> Optimize your token usage with Claude Code without modifying your projects.
|
|
4
|
+
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
[](https://nodejs.org)
|
|
7
|
+
[](https://www.typescriptlang.org)
|
|
8
|
+
|
|
9
|
+
CTO analyzes your codebase, classifies files into **hot/warm/cold tiers** based on recency and dependency structure, and generates optimized `CLAUDE.md` and `.claudeignore` files β so Claude Code reads only what matters. Includes an **MCP Server** for native Claude Code integration and **enterprise-grade security** with secret detection, audit logging, and data integrity verification.
|
|
10
|
+
|
|
11
|
+
## β¨ Features
|
|
12
|
+
|
|
13
|
+
- **π Token Analysis** β Scan your project and estimate token usage per file (chars/4 or tiktoken)
|
|
14
|
+
- **π File Tiering** β Classify files as π₯ hot / π‘οΈ warm / βοΈ cold based on recency + AST analysis
|
|
15
|
+
- **π§ AST Analysis** β Dependency graph, hub file detection, cyclomatic complexity via ts-morph
|
|
16
|
+
- **π― Smart Model Routing** β Suggests Opus/Sonnet/Haiku based on file complexity
|
|
17
|
+
- **π CLAUDE.md Generation** β Auto-generate context files optimized for Claude Code
|
|
18
|
+
- **π« .claudeignore Generation** β Keep cold files out of Claude's context
|
|
19
|
+
- **ποΈ Watch Mode** β Auto-recalculate tiers and regenerate artifacts when files change
|
|
20
|
+
- **π Session Tracking** β Track Claude Code sessions, log file reads, detect waste patterns
|
|
21
|
+
- **π Dashboard** β Terminal dashboard with metrics, savings ratio, and suggestions
|
|
22
|
+
- **π Reports** β Weekly reports, project comparison, CSV/JSON export
|
|
23
|
+
- **π MCP Server** β Native Claude Code integration via Model Context Protocol
|
|
24
|
+
- **π Prompt Templates** β Pre-built prompts optimized by task type (debug, review, refactor, test)
|
|
25
|
+
- **π Secret Detection** β Scans for API keys, tokens, passwords, connection strings before generating artifacts
|
|
26
|
+
- **π Audit Logging** β Immutable, integrity-verified audit trail of every CTO action
|
|
27
|
+
- **π‘οΈ Data Integrity** β SHA-256 verification of artifacts, backups, and sessions
|
|
28
|
+
- **πͺ§ CI/CD Validation** β `cto validate` for automated security checks in pipelines
|
|
29
|
+
- **βοΈ Smart Context Pruning** β Signatures-only for warm files, skeleton for cold β 60-80% token savings
|
|
30
|
+
- **π Git-Aware Tiering** β Uses `git diff`/blame for precise tier classification, auto-promotes changed files
|
|
31
|
+
- **π° Cost Estimation** β Real dollar savings per session, weekly/monthly/yearly projections
|
|
32
|
+
- **π― Token Budget Optimizer** β Knapsack algorithm selects optimal files within a token budget
|
|
33
|
+
- **π€ Multi-AI Generator** β Generate context for Claude, Cursor, Copilot, and Gemini
|
|
34
|
+
- **π PR Context** β Focused context for code reviews with dependency-aware file selection
|
|
35
|
+
- **π‘ Explain** β Transparent tier reasoning showing all factors per file
|
|
36
|
+
- **π― Prompt Engineering** β Enhanced prompts with role priming, chain-of-thought, constraints, and anti-hallucination
|
|
37
|
+
- **π§ Smart Model Routing** β Auto-select Haiku/Sonnet/Opus based on task complexity
|
|
38
|
+
- **π Specification-Driven Development** β Extract specs, generate contract documents, validate implementations
|
|
39
|
+
- **π Project-Local Config** β `cto init --local` creates `.cto/` in project root, shareable via git
|
|
40
|
+
- **π Reversible** β Every apply creates a backup; revert anytime
|
|
41
|
+
- **π¦ Autocontained** β All CTO data in `~/.config/cto/` or `.cto/` in project root
|
|
42
|
+
- **βοΈ Configurable** β YAML config with global, per-project, and local overrides
|
|
43
|
+
|
|
44
|
+
## π¦ Installation
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# From source
|
|
48
|
+
git clone <repo-url>
|
|
49
|
+
cd cto
|
|
50
|
+
npm install
|
|
51
|
+
npm run build
|
|
52
|
+
npm link
|
|
53
|
+
|
|
54
|
+
# Coming soon: npm i -g @cto/cli
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## π Quick Start
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
# 1. Initialize CTO (interactive wizard)
|
|
61
|
+
cto init
|
|
62
|
+
|
|
63
|
+
# 2. Analyze your project
|
|
64
|
+
cto analyze /path/to/project
|
|
65
|
+
|
|
66
|
+
# 3. View file tiers
|
|
67
|
+
cto tiers /path/to/project
|
|
68
|
+
|
|
69
|
+
# 4. Generate optimized artifacts
|
|
70
|
+
cto generate /path/to/project
|
|
71
|
+
|
|
72
|
+
# 5. Preview what would change
|
|
73
|
+
cto diff claude-md /path/to/project
|
|
74
|
+
|
|
75
|
+
# 6. Apply to your project (with confirmation)
|
|
76
|
+
cto apply claude-md /path/to/project
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## π Commands
|
|
80
|
+
|
|
81
|
+
| Command | Description |
|
|
82
|
+
|---------|-------------|
|
|
83
|
+
| `cto init` | Interactive setup wizard |
|
|
84
|
+
| `cto analyze [path]` | Analyze token usage (read-only) |
|
|
85
|
+
| `cto tiers [path]` | Show hot/warm/cold file tiers |
|
|
86
|
+
| `cto generate [path]` | Generate CLAUDE.md & .claudeignore |
|
|
87
|
+
| `cto show <artifact>` | Preview a generated artifact |
|
|
88
|
+
| `cto diff <artifact>` | Show what would change before applying |
|
|
89
|
+
| `cto apply <artifact>` | Apply artifact to project (with backup) |
|
|
90
|
+
| `cto revert [artifact]` | Undo last apply using backup |
|
|
91
|
+
| `cto clean [path]` | Remove all CTO data for a project |
|
|
92
|
+
| `cto prompts [path]` | Show/generate optimized prompt templates |
|
|
93
|
+
| `cto config [path]` | Show current configuration |
|
|
94
|
+
| `cto deps [path]` | Show dependency graph, hub files & complexity |
|
|
95
|
+
| `cto watch [path]` | Watch for changes, auto-recalculate tiers |
|
|
96
|
+
| `cto session start\|end\|current\|list\|log` | Track Claude Code sessions |
|
|
97
|
+
| `cto dashboard [path]` | Terminal dashboard with metrics |
|
|
98
|
+
| `cto report weekly\|project\|export` | Usage reports and data export |
|
|
99
|
+
| `cto doctor [path]` | Health check & security audit |
|
|
100
|
+
| `cto audit log\|verify\|purge` | View and manage audit trail |
|
|
101
|
+
| `cto validate [path]` | CI/CD validation & secret scan |
|
|
102
|
+
| `cto prune preview\|file` | Smart context pruning preview |
|
|
103
|
+
| `cto costs estimate\|history\|pricing` | Token cost estimation & savings |
|
|
104
|
+
| `cto context budget\|pr` | Budget optimizer & PR-focused context |
|
|
105
|
+
| `cto multi-gen generate\|list` | Generate for Claude/Cursor/Copilot/Gemini |
|
|
106
|
+
| `cto explain <file>` | Explain why a file is in a specific tier |
|
|
107
|
+
| `cto route recommend\|tasks` | Smart model routing per task type |
|
|
108
|
+
| `cto multi-gen generate --enhanced` | Prompt-engineered context generation |
|
|
109
|
+
| `cto sdd extract\|spec\|validate` | Specification-Driven Development |
|
|
110
|
+
| `cto init --local` | Create .cto/ in project root (team-shareable) |
|
|
111
|
+
|
|
112
|
+
**Artifact types:** `claude-md`, `claudeignore`, `all`
|
|
113
|
+
|
|
114
|
+
## βοΈ Configuration
|
|
115
|
+
|
|
116
|
+
CTO uses YAML config files:
|
|
117
|
+
|
|
118
|
+
- **Global:** `~/.config/cto/config.yaml`
|
|
119
|
+
- **Per-project:** `~/.config/cto/projects/<hash>/config.yaml`
|
|
120
|
+
|
|
121
|
+
```yaml
|
|
122
|
+
version: "1.3.0"
|
|
123
|
+
model: sonnet # sonnet | opus | haiku
|
|
124
|
+
tokenEstimation: tiktoken # tiktoken (accurate) | chars4 (fast)
|
|
125
|
+
|
|
126
|
+
tiering:
|
|
127
|
+
hotDays: 3 # Files modified within N days = hot
|
|
128
|
+
warmDays: 14 # Files modified within N days = warm
|
|
129
|
+
hotTokenLimit: 50000
|
|
130
|
+
warmTokenLimit: 200000
|
|
131
|
+
|
|
132
|
+
ignoreDirs:
|
|
133
|
+
- node_modules
|
|
134
|
+
- .git
|
|
135
|
+
- dist
|
|
136
|
+
- build
|
|
137
|
+
|
|
138
|
+
extensions:
|
|
139
|
+
code:
|
|
140
|
+
- ts
|
|
141
|
+
- tsx
|
|
142
|
+
- js
|
|
143
|
+
- py
|
|
144
|
+
config:
|
|
145
|
+
- json
|
|
146
|
+
- yaml
|
|
147
|
+
docs:
|
|
148
|
+
- md
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## π How Tiering Works
|
|
152
|
+
|
|
153
|
+
| Tier | Criteria | Action |
|
|
154
|
+
|------|----------|--------|
|
|
155
|
+
| π₯ **Hot** | Modified within 3 days | Read first, always include |
|
|
156
|
+
| π‘οΈ **Warm** | Modified within 14 days | Read if needed |
|
|
157
|
+
| βοΈ **Cold** | Not modified in 14+ days | Skip unless explicitly needed |
|
|
158
|
+
|
|
159
|
+
CTO supports two token estimation methods:
|
|
160
|
+
- **`chars4`** β Fast estimate at ~4 characters per token (default)
|
|
161
|
+
- **`tiktoken`** β Accurate estimation using Claude's real tokenizer
|
|
162
|
+
|
|
163
|
+
### AST-Enhanced Tiering (v0.5.1+)
|
|
164
|
+
|
|
165
|
+
Beyond recency, CTO uses AST analysis to boost tier priority:
|
|
166
|
+
- **Hub files** (imported by 3+ files) get promoted one tier (coldβwarm, warmβhot)
|
|
167
|
+
- **High-complexity files** (cyclomatic complexity >30) get promoted from warmβhot
|
|
168
|
+
- **Model suggestions**: Opus for complex files, Sonnet for moderate, Haiku for simple
|
|
169
|
+
|
|
170
|
+
The tier thresholds are fully configurable.
|
|
171
|
+
|
|
172
|
+
## ποΈ Architecture
|
|
173
|
+
|
|
174
|
+
```
|
|
175
|
+
~/.config/cto/
|
|
176
|
+
βββ config.yaml # Global config
|
|
177
|
+
βββ audit/ # Immutable audit logs
|
|
178
|
+
β βββ audit_YYYYMMDD.json # Daily audit entries (0600 perms)
|
|
179
|
+
βββ projects/
|
|
180
|
+
βββ <project-hash>/
|
|
181
|
+
βββ config.yaml # Project-specific overrides
|
|
182
|
+
βββ analysis.json # Last analysis results
|
|
183
|
+
βββ integrity.json # SHA-256 integrity manifest (0600)
|
|
184
|
+
βββ artifacts/
|
|
185
|
+
β βββ CLAUDE.md # Generated CLAUDE.md (sanitized)
|
|
186
|
+
β βββ .claudeignore # Generated .claudeignore
|
|
187
|
+
βββ backups/
|
|
188
|
+
β βββ manifest.json # Backup tracking
|
|
189
|
+
β βββ <backup-files> # Original files before apply
|
|
190
|
+
βββ sessions/
|
|
191
|
+
βββ current.json # Active session
|
|
192
|
+
βββ session_*.json # Archived sessions
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
## π Security
|
|
196
|
+
|
|
197
|
+
CTO is built with enterprise security requirements in mind.
|
|
198
|
+
|
|
199
|
+
### Secret Detection
|
|
200
|
+
|
|
201
|
+
Before generating CLAUDE.md, CTO scans for and redacts:
|
|
202
|
+
- **API keys** β OpenAI, Anthropic, generic API keys
|
|
203
|
+
- **Cloud credentials** β AWS access keys, secret keys
|
|
204
|
+
- **Private keys** β RSA, EC, DSA, OpenSSH
|
|
205
|
+
- **Passwords** β Hardcoded passwords, database passwords
|
|
206
|
+
- **Tokens** β GitHub, GitLab, npm, OAuth, bearer tokens
|
|
207
|
+
- **Connection strings** β PostgreSQL, MongoDB, MySQL, Redis, AMQP
|
|
208
|
+
- **Custom patterns** β Define your own regex patterns in config
|
|
209
|
+
|
|
210
|
+
### Audit Logging
|
|
211
|
+
|
|
212
|
+
Every CTO action is logged to an immutable audit trail:
|
|
213
|
+
- Each entry includes: timestamp, user, action, project, and details
|
|
214
|
+
- Entries are **integrity-protected** with SHA-256 hashes
|
|
215
|
+
- Tampering is detected with `cto audit verify`
|
|
216
|
+
- Auto-purge after configurable retention period (default: 90 days)
|
|
217
|
+
|
|
218
|
+
### Data Integrity
|
|
219
|
+
|
|
220
|
+
- **SHA-256 hashes** for all artifacts, backups, and sessions
|
|
221
|
+
- `cto validate` verifies integrity on demand (CI/CD ready)
|
|
222
|
+
- Exits with code 1 on failure β use `--strict` to fail on warnings
|
|
223
|
+
|
|
224
|
+
### Secure File Permissions
|
|
225
|
+
|
|
226
|
+
- All CTO data files: `0600` (owner read/write only)
|
|
227
|
+
- All CTO directories: `0700` (owner access only)
|
|
228
|
+
- `cto doctor --fix` enforces permissions automatically
|
|
229
|
+
|
|
230
|
+
### CI/CD Integration
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
# Add to your CI pipeline
|
|
234
|
+
cto validate /path/to/project --strict --json
|
|
235
|
+
|
|
236
|
+
# Fails on: leaked secrets, corrupted data, stale artifacts
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
## π Principles
|
|
240
|
+
|
|
241
|
+
| Principle | Guarantee |
|
|
242
|
+
|-----------|-----------|
|
|
243
|
+
| π **Transparent** | Zero files created inside your project unless you explicitly `apply` |
|
|
244
|
+
| π **Read-only by default** | Analysis never modifies anything |
|
|
245
|
+
| π **Reversible** | Every `apply` creates a backup, every change can be reverted |
|
|
246
|
+
| π¦ **Autocontained** | `~/.config/cto/` is the only location CTO writes to |
|
|
247
|
+
| π« **No secrets leak** | All generated artifacts are sanitized for secrets |
|
|
248
|
+
| π **Auditable** | Every action is logged with tamper-proof integrity hashes |
|
|
249
|
+
| β‘ **Minimal deps** | No databases, no Docker, no external services |
|
|
250
|
+
| π― **Quality first** | Optimizing tokens β losing context |
|
|
251
|
+
|
|
252
|
+
## π§ͺ Development
|
|
253
|
+
|
|
254
|
+
```bash
|
|
255
|
+
# Install dependencies
|
|
256
|
+
npm install
|
|
257
|
+
|
|
258
|
+
# Build
|
|
259
|
+
npm run build
|
|
260
|
+
|
|
261
|
+
# Run tests
|
|
262
|
+
npm test
|
|
263
|
+
|
|
264
|
+
# Watch mode
|
|
265
|
+
npm run test:watch
|
|
266
|
+
|
|
267
|
+
# Type check
|
|
268
|
+
npm run typecheck
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## π MCP Server
|
|
272
|
+
|
|
273
|
+
CTO includes a Model Context Protocol server for native Claude Code integration.
|
|
274
|
+
|
|
275
|
+
### Setup
|
|
276
|
+
|
|
277
|
+
Add to your Claude Code MCP config (`~/.claude.json` or project `.mcp.json`):
|
|
278
|
+
|
|
279
|
+
```json
|
|
280
|
+
{
|
|
281
|
+
"mcpServers": {
|
|
282
|
+
"cto": {
|
|
283
|
+
"command": "cto-mcp",
|
|
284
|
+
"args": []
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
### Available Tools
|
|
291
|
+
|
|
292
|
+
| Tool | Description |
|
|
293
|
+
|------|-------------|
|
|
294
|
+
| `cto_analyze_project` | Analyze project token usage and tier breakdown |
|
|
295
|
+
| `cto_get_hot_files` | Get hot-tier files (read these first) |
|
|
296
|
+
| `cto_should_read_file` | Check if a file is hot/warm/cold with recommendation |
|
|
297
|
+
| `cto_get_context` | Get the optimized CLAUDE.md context |
|
|
298
|
+
| `cto_session_log` | Start/end sessions and log file reads |
|
|
299
|
+
| `cto_get_prompt` | Get an optimized prompt template |
|
|
300
|
+
| `cto_dashboard` | Get dashboard metrics (sessions, tokens, savings) |
|
|
301
|
+
|
|
302
|
+
### MCP Resources
|
|
303
|
+
|
|
304
|
+
- `cto://context/{projectPath}` β Project context as markdown
|
|
305
|
+
|
|
306
|
+
### MCP Prompts
|
|
307
|
+
|
|
308
|
+
- `optimize-task` β Optimized prompt for a task with minimal token usage
|
|
309
|
+
- `review-code` β Optimized prompt for code review
|
|
310
|
+
|
|
311
|
+
## πΊοΈ Roadmap
|
|
312
|
+
|
|
313
|
+
- [x] **v0.5.0** β TypeScript CLI with full analysis, tiering, generation, apply/revert
|
|
314
|
+
- [x] **v0.5.1** β AST-based analysis (ts-morph), tiktoken integration, dependency graph
|
|
315
|
+
- [x] **v0.5.2** β Watch mode with chokidar, auto-regeneration, tier change notifications
|
|
316
|
+
- [x] **v0.7.x** β Session tracking, terminal dashboard, weekly reports, metrics export
|
|
317
|
+
- [x] **v0.9.x** β MCP Server with 7 tools, resources, and prompt templates
|
|
318
|
+
- [x] **v1.0.0** β Enterprise security: secret detection, audit logging, integrity verification, CI/CD validation
|
|
319
|
+
- [x] **v1.1.0** β Smart context pruning, git-aware tiering, cost estimation in real dollars
|
|
320
|
+
- [x] **v1.2.0** β Token budget optimizer, multi-AI generator, PR context, explain command
|
|
321
|
+
- [x] **v1.3.0** β Smart model routing, prompt engineering, SDD, project-local config
|
|
322
|
+
- [ ] **v1.x+** β Plugin system, GitHub Action, VS Code extension
|
|
323
|
+
|
|
324
|
+
## π License
|
|
325
|
+
|
|
326
|
+
[MIT](LICENSE)
|