token-pilot 0.28.3 → 0.30.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.
Files changed (52) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/CHANGELOG.md +75 -0
  4. package/README.md +39 -390
  5. package/agents/tp-api-surface-tracker.md +4 -2
  6. package/agents/tp-audit-scanner.md +4 -2
  7. package/agents/tp-commit-writer.md +4 -2
  8. package/agents/tp-context-engineer.md +4 -2
  9. package/agents/tp-dead-code-finder.md +4 -2
  10. package/agents/tp-debugger.md +4 -2
  11. package/agents/tp-dep-health.md +4 -2
  12. package/agents/tp-doc-writer.md +4 -2
  13. package/agents/tp-history-explorer.md +4 -2
  14. package/agents/tp-impact-analyzer.md +4 -2
  15. package/agents/tp-incident-timeline.md +4 -2
  16. package/agents/tp-incremental-builder.md +4 -2
  17. package/agents/tp-migration-scout.md +4 -2
  18. package/agents/tp-onboard.md +4 -2
  19. package/agents/tp-performance-profiler.md +4 -2
  20. package/agents/tp-pr-reviewer.md +4 -2
  21. package/agents/tp-refactor-planner.md +4 -2
  22. package/agents/tp-review-impact.md +4 -2
  23. package/agents/tp-run.md +4 -2
  24. package/agents/tp-session-restorer.md +4 -2
  25. package/agents/tp-ship-coordinator.md +4 -2
  26. package/agents/tp-spec-writer.md +4 -2
  27. package/agents/tp-test-coverage-gapper.md +4 -2
  28. package/agents/tp-test-triage.md +4 -2
  29. package/agents/tp-test-writer.md +4 -2
  30. package/dist/cli/tool-audit.d.ts +5 -0
  31. package/dist/cli/tool-audit.js +9 -1
  32. package/dist/core/policy-engine.d.ts +1 -5
  33. package/dist/core/policy-engine.js +9 -24
  34. package/dist/hooks/pre-bash.d.ts +13 -1
  35. package/dist/hooks/pre-bash.js +56 -1
  36. package/dist/hooks/pre-grep.d.ts +2 -1
  37. package/dist/hooks/pre-grep.js +3 -1
  38. package/dist/index.js +4 -2
  39. package/dist/server/enforcement-mode.d.ts +47 -0
  40. package/dist/server/enforcement-mode.js +59 -0
  41. package/dist/server/tool-definitions.d.ts +20 -0
  42. package/dist/server/tool-definitions.js +113 -10
  43. package/dist/server/tool-profiles.d.ts +19 -1
  44. package/dist/server/tool-profiles.js +38 -4
  45. package/dist/server.d.ts +2 -0
  46. package/dist/server.js +68 -16
  47. package/docs/agents.md +82 -0
  48. package/docs/configuration.md +117 -0
  49. package/docs/hooks.md +99 -0
  50. package/docs/installation.md +143 -0
  51. package/docs/tools.md +61 -0
  52. package/package.json +2 -2
@@ -6,14 +6,14 @@
6
6
  },
7
7
  "metadata": {
8
8
  "description": "Token Pilot \u2014 save 60-90% tokens when AI reads code",
9
- "version": "0.28.3"
9
+ "version": "0.29.0"
10
10
  },
11
11
  "plugins": [
12
12
  {
13
13
  "name": "token-pilot",
14
14
  "source": "./",
15
15
  "description": "Reduces token consumption by 60-90% via AST-aware lazy file reading, structural symbol navigation, and cross-session tool-usage analytics. 22 MCP tools + 19 subagents + budget watchdog hooks.",
16
- "version": "0.28.3",
16
+ "version": "0.29.0",
17
17
  "author": {
18
18
  "name": "Digital-Threads"
19
19
  },
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "token-pilot",
3
- "version": "0.28.3",
3
+ "version": "0.29.0",
4
4
  "description": "Saves 60-90% tokens when AI reads code. AST-aware lazy reading, symbol navigation, cross-session tool-usage analytics, 22 subagents (haiku/sonnet/opus-tiered) with budget watchdog.",
5
5
  "author": {
6
6
  "name": "Digital-Threads",
package/CHANGELOG.md CHANGED
@@ -5,6 +5,81 @@ All notable changes to Token Pilot will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.30.0] - 2026-04-19
9
+
10
+ ### Added — `minimal` profile (5 tools, near-zero overhead)
11
+
12
+ New `TOKEN_PILOT_PROFILE=minimal` for context-budget-constrained sessions. Advertises only 5 core tools (`smart_read`, `read_symbol`, `find_usages`, `smart_diff`, `smart_log`) — no META tools, no editing extras. Instructions are ~80 tokens vs ~350 for `full`. Use when the agent's context window is nearly full and you only need to navigate code.
13
+
14
+ ### Added — profile-specific MCP instructions (PR #1)
15
+
16
+ Each profile now receives instructions that only mention its own advertised tools. Previously, a `nav` session received `read_for_edit` and `code_audit` instructions even though those tools weren't in `tools/list` — causing hallucinated tool calls. Now:
17
+
18
+ - `minimal` → 80-token instruction block, 5 tools only
19
+ - `nav` → exploration-only rules, no edit-prep or audit mentions
20
+ - `edit` → full read+write workflow (DEFAULT since v0.30.0)
21
+ - `full` → all 22 tools including audit tools
22
+
23
+ Added `getMcpInstructions(profile)` export in `tool-definitions.ts`. The deprecated `MCP_INSTRUCTIONS` constant is kept as a `full`-alias for backward compatibility.
24
+
25
+ ### Changed — default profile `full` → `edit` (PR #2)
26
+
27
+ `TOKEN_PILOT_PROFILE` now defaults to `edit` instead of `full`. Rationale: the `full` profile was advertising 22 tools + ~350-token instructions on every session, costing ~3 k context tokens before any work. `edit` (16 tools) covers 99% of development workflows. Switch to `full` explicitly only when audit tools (`code_audit`, `find_unused`, `test_summary`) are needed.
28
+
29
+ Unknown `TOKEN_PILOT_PROFILE` values now fall back to `edit` (was `full`).
30
+
31
+ ### Fixed — removed dead code in policy-engine (PR #7)
32
+
33
+ `requireReadForEditBeforeEdit` in `PolicyConfig` was permanently dead: `editTargetPath` was never set in the `PolicyCheckContext` passed by `server.ts`, so the advisory could never fire. Removed:
34
+
35
+ - `PolicyConfig.requireReadForEditBeforeEdit` field
36
+ - `PolicyCheckContext.editTargetPath` field
37
+ - `PolicyCheckContext.readForEditCalled` field
38
+ - The corresponding `checkPolicy` branch (case 4)
39
+ - Dead `readForEditCalled` Set tracking in `server.ts`
40
+
41
+ ---
42
+
43
+ ## [0.29.0] - 2026-04-19
44
+
45
+ Consolidation release based on Sonnet 4.6 + Opus 4.7 verification findings. Closes the short-tail issues that came out of the two live runs before the weekly-quota window reopens.
46
+
47
+ ### Added — context-mode partnership in shared preamble
48
+
49
+ Both verification runs showed the same asymmetry: `token-pilot` saves on delegated (subagent) code reads; `context-mode` saves on main-thread Bash/command execution. Opus 4.7 literally wrote: "Во всей остальной работе использовал `ctx_batch_execute` вместо raw Bash — это adoption context-mode, не token-pilot". That's the right behaviour — we shouldn't fight it, we should formalise it.
50
+
51
+ All 25 tp-* agents now carry an instruction in the shared preamble: *for heavy Bash (tests, builds, recursive searches, network calls), prefer `mcp__context-mode__execute` / `ctx_batch_execute` when available — runs in sandbox, only result enters context (95% reduction vs raw stdout)*. This is complementary, not redundant: token-pilot owns code reading, context-mode owns command execution.
52
+
53
+ ### Fixed — composite Bash escape patterns (from Opus 4.7 v0.28.2 report)
54
+
55
+ Opus's verification noted that quoted / wrapped heavy commands slipped past our `PreToolUse:Bash` hook:
56
+
57
+ - `bash -c "cat src/foo.ts"` → slipped
58
+ - `sh -c "grep -r foo ."` → slipped
59
+ - `eval "cat src/foo.ts"` → slipped
60
+ - `for f in *.ts; do cat $f; done` → slipped
61
+ - `while read f; do git log; done` → slipped
62
+
63
+ Added `extractWrappedCommands()` in `src/hooks/pre-bash.ts` — unwraps `bash/sh/zsh -c "..."`, `eval "..."`, `for/while/until ... do BODY done` — and re-runs the heavy-pattern check on each inner body. First deny wins. Adds 7 regression tests covering both deny (heavy inside wrapper) and allow (benign inside wrapper — `bash -c "ls"`, `eval "echo hello"`).
64
+
65
+ ### Changed — honest tool descriptions for weak performers
66
+
67
+ - `smart_log` description now carries a heads-up: "two verification runs measured this tool at ~39% token reduction (borderline). Cumulative data being gathered — tool may be dropped or redesigned in v0.30.0 if numbers don't improve". The description already advised scoping with `path` or `count`; kept.
68
+ - `session_budget` re-framed as **META / info-only** — doesn't save tokens itself, purely diagnostic. This matches the META_TOOLS grouping in profiles (shipped in v0.28.1) and stops users thinking it's an optimisation tool.
69
+
70
+ ### Changed — composed-agent line budget 60 → 65
71
+
72
+ Shared preamble now carries the context-mode paragraph — 3 extra lines flow into every composed agent file. Three agents (tp-context-engineer, tp-dead-code-finder, tp-doc-writer) ticked over the 60-line cap by 1-3 lines. Raised the hard limit to 65 to accommodate the new content without trimming per-agent instructions. 25 agents currently in the 38-63 range.
73
+
74
+ ### Deferred to v0.30.0
75
+
76
+ - **Stop-hook output watchdog** — cap main-thread response size. Needs an experiment against Claude Code API first; too much new surface for a same-day patch.
77
+ - **Automatic MCP response buffer** — intercept 3rd-party MCP (GitHub / Jira / Slack) responses via `updatedMCPToolOutput`. Biggest potential lever in the ecosystem, but a full feature, not a patch.
78
+ - **`smart_log` final decision** — keep, redesign, or drop based on cumulative `tool-audit` data after a week of use.
79
+ - **`explore_area` self-sizing** — v0.28.3 tightened the caps (20/500 → 10/200); next step is compare predicted output to `estimateExploreAreaWorkflowTokens` baseline and trim when exceeded.
80
+
81
+ 1026 tests passing (+7 new on composite Bash escape).
82
+
8
83
  ## [0.28.3] - 2026-04-19
9
84
 
10
85
  ### Fixed — `explore_area` output size (was −31% savings)
package/README.md CHANGED
@@ -5,8 +5,8 @@
5
5
  Three layers, each useful on its own, stronger together:
6
6
 
7
7
  1. **MCP tools** — structural reads (`smart_read`, `read_symbol`, `read_for_edit`, …). Ask for an outline or load one function by name instead of the whole file.
8
- 2. **Read hook** — intercepts large raw `Read` calls and answers with a structural summary in the denial reason itself. Works for every agent, including ones that only have basic tools.
9
- 3. **`tp-*` subagents** — Claude Code delegates with MCP-first behaviour and tight response budgets. Tier 1 workhorses (`tp-run`, `tp-onboard`, `tp-pr-reviewer`, `tp-impact-analyzer`, `tp-refactor-planner`, `tp-test-triage`) plus Tier 2 specialists (`tp-debugger`, `tp-migration-scout`, `tp-test-writer`, `tp-dead-code-finder`, `tp-commit-writer`).
8
+ 2. **PreToolUse hooks** — intercept heavy native tool calls (`Read` on large files, recursive `Grep`, unbounded `git diff`) and redirect to token-efficient alternatives.
9
+ 3. **`tp-*` subagents** — Claude Code delegates with MCP-first behaviour and tight response budgets.
10
10
 
11
11
  ## How It Works
12
12
 
@@ -17,7 +17,7 @@ Token Pilot: smart_read("user-service.ts") → 15-line outline → ~200 tok
17
17
  After edit: read_diff("user-service.ts") → ~20 tokens
18
18
  ```
19
19
 
20
- Files under 200 lines are returned in full (zero overhead for small files).
20
+ Files under 200 lines are returned in full zero overhead for small files.
21
21
 
22
22
  ### Benchmarks
23
23
 
@@ -31,7 +31,7 @@ Measured on public open-source repos. Files ≥50 lines only:
31
31
  | [flask](https://github.com/pallets/flask) (Python) | 20 | 78,236 | 7,418 | **91%** |
32
32
  | **Total** | **104** | **244,743** | **19,764** | **92%** |
33
33
 
34
- > Measures `smart_read` outline savings only. Real sessions additionally benefit from session cache, `read_symbol` targeted loading, and `read_for_edit` minimal edit context. Reproduce: `npx tsx scripts/benchmark.ts`.
34
+ > `smart_read` outline savings only. Real sessions additionally benefit from session cache, `read_symbol`, and `read_for_edit`. Reproduce: `npx tsx scripts/benchmark.ts`.
35
35
 
36
36
  ## Quick Start
37
37
 
@@ -39,362 +39,53 @@ Measured on public open-source repos. Files ≥50 lines only:
39
39
  npx -y token-pilot init
40
40
  ```
41
41
 
42
- This does two things:
42
+ Creates (or merges into) `.mcp.json` with `token-pilot` + [`context-mode`](https://github.com/mksglu/claude-context-mode), then prompts to install `tp-*` subagents. Restart your AI assistant to activate.
43
43
 
44
- 1. Creates (or merges into) `.mcp.json` with `token-pilot` + [`context-mode`](https://github.com/mksglu/claude-context-mode).
45
- 2. If you're on a TTY, asks whether to install the `tp-*` subagents now — pick `user` (available in every project) or `project` scope.
44
+ ## What You Get
46
45
 
47
- Restart your AI assistant to activate. The Read hook auto-installs the first time `token-pilot` starts inside Claude Code. Works with **Claude Code, Cursor, Codex, Antigravity, Cline**, and any MCP-compatible client — though support varies (see matrix below).
46
+ - **22 MCP tools** structural reads, symbol search, git analysis, session analytics [tools reference](docs/tools.md)
47
+ - **PreToolUse hooks** — block heavy `Grep`/`Bash`/`Read` calls; redirect to efficient alternatives → [hooks & modes](docs/hooks.md)
48
+ - **25 `tp-*` subagents** (Claude Code only) — MCP-first delegates with haiku/sonnet model tiers and budget enforcement → [agents reference](docs/agents.md)
49
+ - **Tool profiles** — trim advertised `tools/list` to save ~2 k tokens per session → [profiles & config](docs/configuration.md)
48
50
 
49
- ## Client support matrix
51
+ ## Client Support Matrix
50
52
 
51
- Not every capability works in every client. Subagents are a Claude Code concept; other clients still get the MCP tools + Read hook but won't auto-invoke `tp-*` agents.
53
+ | Client | MCP tools | PreToolUse hooks | `tp-*` subagents |
54
+ |--------|:---------:|:----------------:|:----------------:|
55
+ | Claude Code | ✅ | ✅ | ✅ |
56
+ | Cursor | ✅ | ✅ | ❌ |
57
+ | Codex CLI | ✅ | ✅ | ❌ |
58
+ | Gemini CLI | ✅ | ✅ | ❌ |
59
+ | Cline (VS Code) | ✅ | ✅ | ❌ |
60
+ | Antigravity | ✅ | ✅ | ❌ |
52
61
 
53
- | Client | MCP tools | Read hook (context-mode) | `tp-*` subagents (25) | `model:` frontmatter (haiku) | Budget watchdog |
54
- |-----------------|:---------:|:------------------------:|:---------------------:|:----------------------------:|:---------------:|
55
- | Claude Code | ✅ | ✅ | ✅ | ✅ | ✅ |
56
- | Cursor | ✅ | ✅ | ❌ | ❌ (ignored) | ❌ |
57
- | Codex CLI | ✅ | ✅ | ❌ | ❌ | ❌ |
58
- | Gemini CLI | ✅ | ✅ | ❌ | ❌ | ❌ |
59
- | Cline (VS Code) | ✅ | ✅ | ❌ | ❌ | ❌ |
60
- | Antigravity | ✅ | ✅ | ❌ | ❌ | ❌ |
62
+ Manual config snippets for each client [installation guide](docs/installation.md)
61
63
 
62
- **What non-Claude users get (~60% of the package):**
63
- - All 22 MCP tools: `smart_read`, `read_symbol`, `find_usages`, `smart_diff`, `code_audit`, `session_analytics`, etc.
64
- - The Read hook that blocks oversized reads and suggests token-saving alternatives.
64
+ ## Enforcement Mode
65
65
 
66
- **What needs Claude Code to work:**
67
- - 19 `tp-*` subagents invoked via the `Task` tool.
68
- - `model: claude-haiku-4-5` frontmatter on format-bound agents (commit-writer, session-restorer, onboard) — cheaper runs.
69
- - `PostToolUse:Task` budget watchdog — logs agent runs exceeding their declared budget to `.token-pilot/over-budget.log`.
66
+ `TOKEN_PILOT_MODE` controls how aggressively Token Pilot redirects heavy native tool calls:
70
67
 
71
- `install-agents` detects non-Claude clients via env vars + filesystem markers (`CURSOR_TRACE_ID`, `~/.codex/`, `~/.gemini/`, etc.) and **skips installing** unless you pass `--scope=user|project` explicitly.
72
-
73
- ## Manual MCP install (per-client examples)
74
-
75
- If `init` isn't right for your setup — CI, non-TTY environments, editing a shared team config, or a client without an interactive installer — add Token Pilot as an MCP server directly. The server command is `npx -y token-pilot` on every client; only the config file shape differs.
76
-
77
- ### Claude Code
78
-
79
- Three paths — pick one, they're mutually exclusive.
80
-
81
- **A. As a Claude Code plugin (one-step install — hooks + MCP registered together):**
82
-
83
- ```bash
84
- claude plugin marketplace add https://github.com/Digital-Threads/token-pilot
85
- claude plugin install token-pilot@token-pilot
86
- ```
87
-
88
- Claude Code clones the repo into `~/.claude/plugins/cache/token-pilot/`, sets `CLAUDE_PLUGIN_ROOT`, and registers the MCP server + all hooks declared in `.claude-plugin/hooks/hooks.json`. No `install-hook` call needed. `install-agents` still applies for the tp-* subagents (they're separate from plugin hooks).
89
-
90
- **B. Via MCP config (npm-based, no plugin system):**
91
-
92
- ```bash
93
- claude mcp add token-pilot -- npx -y token-pilot
94
- claude mcp add --scope user token-pilot -- npx -y token-pilot
95
- claude mcp add --scope project token-pilot -- npx -y token-pilot
96
-
97
- # Or edit .mcp.json directly (project-level) / ~/.mcp.json (user-level)
98
- ```
99
-
100
- ```json
101
- {
102
- "mcpServers": {
103
- "token-pilot": { "command": "npx", "args": ["-y", "token-pilot"] },
104
- "context-mode": { "command": "npx", "args": ["-y", "claude-context-mode"] }
105
- }
106
- }
107
- ```
108
-
109
- Then `npx token-pilot install-hook` to register the PreToolUse Read/Edit hooks and `npx token-pilot install-agents --scope=user` to install the 25 tp-* subagents.
110
-
111
- **C. One-liner `init`:** `npx -y token-pilot init` — writes path B config for you, then prompts about subagents.
112
-
113
- ### Cursor
114
-
115
- Cursor reads `.cursor/mcp.json` (project) or `~/.cursor/mcp.json` (global):
116
-
117
- ```json
118
- {
119
- "mcpServers": {
120
- "token-pilot": { "command": "npx", "args": ["-y", "token-pilot"] }
121
- }
122
- }
123
- ```
124
-
125
- ### Codex CLI
126
-
127
- Codex reads `~/.codex/config.toml`:
128
-
129
- ```toml
130
- [mcp_servers.token-pilot]
131
- command = "npx"
132
- args = ["-y", "token-pilot"]
133
- ```
134
-
135
- ### Cline (VS Code)
136
-
137
- Cline reads `cline_mcp_settings.json` (accessible via Cline panel → MCP Servers → Edit):
138
-
139
- ```json
140
- {
141
- "mcpServers": {
142
- "token-pilot": { "command": "npx", "args": ["-y", "token-pilot"] }
143
- }
144
- }
145
- ```
146
-
147
- ### Any MCP-compatible client
148
-
149
- Use the generic MCP protocol — the server is a plain stdio process:
150
-
151
- ```
152
- command: npx
153
- args: -y token-pilot
154
- ```
155
-
156
- No env vars required. Optional overrides:
157
-
158
- | Env var | Default | Purpose |
159
- |---|---|---|
160
- | `TOKEN_PILOT_PROFILE` | `full` | `nav`/`edit`/`full` — trims the advertised `tools/list` payload to save ~2 k tokens per session (see "Tool profiles" below) |
161
- | `TOKEN_PILOT_DENY_THRESHOLD` | `300` | Line count above which the Read hook intervenes |
162
- | `TOKEN_PILOT_ADAPTIVE_THRESHOLD` | `false` | Enable the adaptive curve as the session burns |
163
- | `TOKEN_PILOT_BYPASS` | unset | Set to `1` to disable the Read hook for one session |
164
- | `TOKEN_PILOT_SKIP_POSTINSTALL` | unset | Skip the `ast-index` safety-net install at `npm install` time |
165
-
166
- ### Tool profiles
167
-
168
- | Profile | Tools | ~Tokens | Use when |
169
- |---------|------:|--------:|----------|
170
- | `full` *(default)* | 22 | ~4 150 | All capabilities |
171
- | `edit` | 16 | ~3 120 | Code-change workflows (nav + batch reads + `read_for_edit`) |
172
- | `nav` | 10 | ~1 910 | Read-only exploration / subagents that only navigate |
173
-
174
- Set via `TOKEN_PILOT_PROFILE` in your MCP server env block. Handlers stay live regardless — a subagent that explicitly names a filtered-out tool still gets served. The profile only trims what we advertise in `tools/list` at session start.
175
-
176
- ### Subagents (Claude Code only)
177
-
178
- `tp-*` subagents are a Claude Code feature. Other clients use only the MCP tools + Read hook. To install on a target scope explicitly:
179
-
180
- ```bash
181
- npx token-pilot install-agents --scope=user # all projects
182
- npx token-pilot install-agents --scope=project # this repo only
183
- npx token-pilot install-agents --scope=user --force # re-apply after an update
184
- ```
185
-
186
- ### From source (contributors / vendored installs)
68
+ | Value | Behaviour |
69
+ |-------|-----------|
70
+ | `advisory` | Allow all — hooks pass through, advisory notes only |
71
+ | `deny` *(default)* | Block heavy `Grep`/`Bash` patterns; intercept large `Read` calls |
72
+ | `strict` | Deny + auto-cap MCP output (`smart_read` 2 000 tokens, `find_usages` list mode, `smart_log` 20 commits) |
187
73
 
188
74
  ```bash
189
- git clone https://github.com/Digital-Threads/token-pilot.git
190
- cd token-pilot && npm install && npm run build
191
- # Point your client's config at dist/index.js:
192
- # "command": "node", "args": ["/abs/path/to/token-pilot/dist/index.js"]
75
+ TOKEN_PILOT_MODE=strict npx token-pilot
193
76
  ```
194
77
 
195
- ## Modes
196
-
197
- The Read hook has three modes. Set in `.token-pilot.json`:
198
-
199
- | Mode | Behaviour |
200
- |------|-----------|
201
- | `off` | Hook is inert. |
202
- | `advisory` | Denies unbounded Read with a short tip pointing at `smart_read` / `read_for_edit`. |
203
- | `deny-enhanced` *(default)* | Denies the Read and returns a full structural summary (imports, exports, declarations) **inside** the denial reason. Works for subagents that lack MCP access. |
204
-
205
- ```json
206
- { "hooks": { "mode": "deny-enhanced", "denyThreshold": 300 } }
207
- ```
208
-
209
- Env var overrides: `TOKEN_PILOT_MODE=off`, `TOKEN_PILOT_DENY_THRESHOLD=500`, `TOKEN_PILOT_BYPASS=1`.
210
-
211
- ## Subagents
212
-
213
- Claude Code subagents guarantee MCP-first behaviour with tight response budgets and verdict-first output. **Tier 1** are everyday workhorses invoked proactively; **Tier 2** are focused specialists you reach for when a specific kind of work comes up (debugging, migration, test authoring, cleanup, commits).
214
-
215
- **Tier 1 — workhorses:**
216
-
217
- | Agent | When to invoke | Budget |
218
- |-------|---------------|-------:|
219
- | `tp-run` | General MCP-first workhorse; use proactively when no specialised agent fits | 800 |
220
- | `tp-onboard` | Orient to an unfamiliar repo (layout, entry points, modules) | 600 |
221
- | `tp-pr-reviewer` | Review a diff / PR / changeset; verdict-first, Critical/Important tiers | 600 |
222
- | `tp-impact-analyzer` | Trace blast-radius of a change (callers, transitive deps) | 400 |
223
- | `tp-refactor-planner` | Plan a refactor with exact edit context per step | 500 |
224
- | `tp-test-triage` | Investigate test failures → root cause → minimal fix | 500 |
225
-
226
- **Tier 2 — specialists:**
227
-
228
- | Agent | When to invoke | Budget |
229
- |-------|---------------|-------:|
230
- | `tp-debugger` | Stack trace / error → root-cause line via call-tree traversal | 700 |
231
- | `tp-migration-scout` | Pre-migration impact map grouped by effort class | 800 |
232
- | `tp-test-writer` | Write tests for ONE symbol, mirrors project style, runs tests | 900 |
233
- | `tp-dead-code-finder` | Cross-checked dead-code detection, output-only (never deletes) | 600 |
234
- | `tp-commit-writer` | Draft Conventional-Commit from staged diff; refuses failing tests | 400 |
235
- | `tp-history-explorer` | "Why is this like this?" — minimum commit chain explaining current state | 600 |
236
- | `tp-audit-scanner` | Read-only security / quality audit; Critical / Important / Minor findings | 800 |
237
- | `tp-session-restorer` | Rehydrate state after /clear or compaction from latest snapshot | 400 |
238
-
239
- **Tier 3 — combo / workflow:**
240
-
241
- | Agent | When to invoke | Budget |
242
- |-------|---------------|-------:|
243
- | `tp-review-impact` | Pre-merge blast-radius review (diff × dependents × API surface) | 700 |
244
- | `tp-test-coverage-gapper` | Find symbols with zero test references, prioritised | 500 |
245
- | `tp-api-surface-tracker` | Public API diff vs last release → MAJOR / MINOR / PATCH verdict | 600 |
246
- | `tp-dep-health` | Dep audit: stale × heavily-used × removable | 600 |
247
- | `tp-incident-timeline` | Correlate an incident window with commits, rank likely culprits | 700 |
248
-
249
- **Tier 4 — methodology (v0.27.0, inspired by @addyosmani/agent-skills):**
250
-
251
- | Agent | When to invoke | Budget |
252
- |-------|---------------|-------:|
253
- | `tp-context-engineer` | Audit / write CLAUDE.md / AGENTS.md rules files per project | 800 |
254
- | `tp-spec-writer` | Pre-code spec with gated workflow; surfaces assumptions before code | 900 |
255
- | `tp-performance-profiler` | Measure → identify → fix → verify → guard; refuses to optimize without data | 800 |
256
- | `tp-incremental-builder` | Multi-file feature work in thin vertical slices, test between each | 900 |
257
- | `tp-doc-writer` | ADRs + READMEs + API docs; documents *why* not *what* | 700 |
258
- | `tp-ship-coordinator` | 5-pillar pre-launch checklist (quality / security / observability / rollback / rollout) | 800 |
259
-
260
- ### Model tiers
261
-
262
- Every agent carries an explicit `model:` field in its frontmatter. Default dispatch:
263
- - **haiku** (9 agents) — structured / format-bound output (commit messages, onboarding maps, ADRs, session briefings)
264
- - **sonnet** (15 agents) — reasoning tasks (review, debug, test, plan, audit, spec, profile, ship)
265
- - **inherit** (1 agent) — deep correlation needing whatever the main thread uses (`tp-incident-timeline`)
266
-
267
- Effect: under Opus 4.7's +35% tokenizer tax, keeping the majority of agent spawns on haiku / sonnet saves 5-10× model cost vs an all-Opus baseline.
268
-
269
- Every agent's budget is enforced post-response — overshoots beyond 10 % land in `.token-pilot/over-budget.log`.
270
-
271
- `init` offers to install these; to do it later or add them to another project, run `npx token-pilot install-agents`. Remove with `npx token-pilot uninstall-agents --scope=user|project`.
272
-
273
- For third-party agents (e.g. `acc-*` plugins) whose tool allowlist excludes token-pilot MCP, `npx token-pilot bless-agents` creates project-level overrides that add the missing tools. `doctor` warns when the original agent has changed since blessing; `unbless-agents` reverses.
274
-
275
- ## MCP Tools
276
-
277
- ### Reading
278
-
279
- | Tool | Instead of | Purpose |
280
- |------|-----------|---------|
281
- | `smart_read` | `Read` | AST outline; 90% fewer tokens on large files |
282
- | `read_symbol` | `Read`+scroll | One class/function by name (`Class.method` supported) |
283
- | `read_symbols` | N × `read_symbol` | Batch up to 10 symbols from one file |
284
- | `read_for_edit` | `Read` before `Edit` | Minimal raw code around a symbol — copy directly as `old_string` |
285
- | `read_range` | `Read` offset | Specific line range |
286
- | `read_section` | `Read` | Section by heading (Markdown) or key (YAML/JSON/CSV) |
287
- | `read_diff` | re-`Read` | Changed hunks since last `smart_read` |
288
- | `smart_read_many` | multiple `Read` | Batch smart_read for up to 20 files |
289
-
290
- ### Search & Navigation
291
-
292
- | Tool | Instead of | Purpose |
293
- |------|-----------|---------|
294
- | `find_usages` | `Grep` (refs) | All usages of a symbol; filters by scope/kind/lang |
295
- | `project_overview` | `ls` + explore | Project type, frameworks, architecture, directory map |
296
- | `related_files` | manual | Import graph: imports, importers, test files |
297
- | `outline` | multiple `smart_read` | Compact overview of all code in a directory |
298
- | `find_unused` | manual | Dead code detection |
299
- | `code_audit` | multiple `Grep` | TODOs, deprecated symbols, structural patterns |
300
- | `module_info` | manual | Deps, dependents, public API, unused deps |
301
- | `smart_diff` | raw `git diff` | Structural diff with symbol mapping |
302
- | `explore_area` | 3-5 calls | Structure + imports + tests + recent changes |
303
- | `smart_log` | raw `git log` | Structured commits with category detection |
304
- | `test_summary` | raw test output | Run tests → pass/fail summary + failure details |
305
-
306
- ### Session
307
-
308
- | Tool | Purpose |
309
- |------|---------|
310
- | `session_snapshot` | Compact markdown snapshot (<200 tokens) of goal, decisions, facts, blockers, next step. Auto-persisted to `.token-pilot/snapshots/latest.md`; SessionStart surfaces a pointer when recent. |
311
- | `session_budget` | Hook-suppression pressure for this session: saved tokens, burn fraction, effective denyThreshold, time-to-compact projection |
312
- | `session_analytics` | Token savings: per-tool breakdown, top files, policy advisories |
313
-
314
- ## CLI
315
-
316
- ```bash
317
- token-pilot # Start MCP server
318
- token-pilot init # Create/merge .mcp.json; offers to install subagents
319
- token-pilot install-agents [--scope=user|project] [--force]
320
- token-pilot uninstall-agents --scope=user|project
321
- token-pilot bless-agents # Extend third-party agents with token-pilot MCP
322
- token-pilot unbless-agents <name>... | --all
323
- token-pilot install-hook # Install PreToolUse hook
324
- token-pilot uninstall-hook
325
- token-pilot stats # Totals + top files from hook-events.jsonl
326
- token-pilot stats --session[=<id>] | --by-agent
327
- token-pilot tool-audit # Per-tool savings distribution (cumulative across sessions)
328
- token-pilot tool-audit --json # Same, machine-readable
329
- token-pilot doctor # Diagnostics (ast-index, config, upstream drift)
330
- token-pilot install-ast-index # Download ast-index binary (auto on first run)
331
- ```
332
-
333
- ### Environment variables
334
-
335
- | Var | Effect |
336
- |-----|--------|
337
- | `TOKEN_PILOT_MODE=off` | Disable the hook for this process |
338
- | `TOKEN_PILOT_BYPASS=1` | Pass every Read through |
339
- | `TOKEN_PILOT_DENY_THRESHOLD=<n>` | Override `hooks.denyThreshold` |
340
- | `TOKEN_PILOT_DEBUG=1` | Verbose hook logging to stderr |
341
- | `TOKEN_PILOT_NO_AGENT_REMINDER=1` | Suppress the "tp-* not installed" stderr nudge |
342
- | `TOKEN_PILOT_SUBAGENT=1` | Mark the MCP server as running inside a subagent |
343
-
344
- ## Configuration
345
-
346
- Drop `.token-pilot.json` in your project root. All fields optional.
347
-
348
- ```json
349
- {
350
- "hooks": { "mode": "deny-enhanced", "denyThreshold": 300 },
351
- "sessionStart": { "enabled": true, "showStats": false, "maxReminderTokens": 250 },
352
- "agents": { "scope": null, "reminder": true },
353
- "smartRead": { "smallFileThreshold": 200 },
354
- "cache": { "maxSizeMB": 100, "watchFiles": true },
355
- "policies": { "maxFullFileReads": 10, "largeReadThreshold": 2000 },
356
- "ignore": ["node_modules/**", "dist/**", ".git/**"]
357
- }
358
- ```
359
-
360
- | Option | Default | What it does |
361
- |--------|---------|--------------|
362
- | `hooks.mode` | `"deny-enhanced"` | `off` / `advisory` / `deny-enhanced` |
363
- | `hooks.denyThreshold` | `300` | Line count above which the hook starts denying unbounded Read |
364
- | `sessionStart.enabled` | `true` | Re-inject MCP-rules reminder at every new session / `/clear` / `/compact` |
365
- | `agents.scope` | `null` | Persisted scope of last `install-agents` run; reused silently |
366
- | `agents.reminder` | `true` | Show the "agents not installed" startup nudge |
367
- | `smartRead.smallFileThreshold` | `200` | Files with fewer lines bypass AST overhead |
368
- | `cache.maxSizeMB` | `100` | File cache ceiling (LRU eviction) |
369
- | `policies.maxFullFileReads` | `10` | Warn after N full-file reads in session |
78
+ [Full hook & mode docs](docs/hooks.md)
370
79
 
371
80
  ## Ecosystem
372
81
 
373
- Token Pilot sits in a small toolkit of three orthogonal pieces. Each has a single job; none overlaps with another.
374
-
375
- ```
376
- Agent
377
- ├─ Read code file → smart_read / read_symbol (Token Pilot MCP)
378
- │ └→ ast-index (structural backend)
379
- ├─ Need a fresh Read? → force: true (Token Pilot MCP)
380
- ├─ Run code / inspect logs → execute (context-mode MCP)
381
- └─ Bash agent, no MCP? → ast-index <subcmd> (CLI binary)
382
- ```
383
-
384
- | Tool | Role | When it fires |
385
- |------|------|---------------|
386
- | **[Token Pilot](.)** | Enforcement layer — Read hook, MCP structural reads, dedup, snapshots, subagents | Every code Read / Edit / session |
387
- | **[ast-index](https://github.com/defendend/Claude-ast-index-search)** | Structural indexer. Symbols, usages, hierarchy. Used by Token Pilot under the hood; also a standalone CLI for bash-only agents. | Auto-installed by Token Pilot; CLI available as `ast-index` |
388
- | **[context-mode](https://github.com/mksglu/claude-context-mode)** | Sandbox executor — runs shell / python / js, only stdout enters the window. Orthogonal: not for reading source. | Large `Bash` outputs, `execute(language, code)` calls |
389
-
390
- **Rules of thumb:**
82
+ | Tool | Role |
83
+ |------|------|
84
+ | **Token Pilot** | Enforcement layer — hooks, MCP structural reads, subagents |
85
+ | **[ast-index](https://github.com/defendend/Claude-ast-index-search)** | Structural indexer. Auto-installed by Token Pilot; also a standalone CLI for bash-only agents |
86
+ | **[context-mode](https://github.com/mksglu/claude-context-mode)** | Sandbox executor — runs shell/python/js, only stdout enters the context window |
391
87
 
392
- - Read code → Token Pilot `smart_read` / `read_symbol` (automatic via hook or MCP).
393
- - Execute code that produces big output → context-mode `execute`.
394
- - Bash agent or pre-flight shell command → `ast-index` CLI directly.
395
- - Never all three in `CLAUDE.md` — Token Pilot's `doctor` warns when `CLAUDE.md` exceeds 60 lines for this reason.
396
-
397
- `npx token-pilot init` wires Token Pilot + context-mode into `.mcp.json`; ast-index installs on first run. If any single tool is missing, the other two still work standalone.
88
+ Rules of thumb: read code → `smart_read`/`read_symbol`; execute code with big output → context-mode `execute`; bash-only agent → `ast-index` CLI. Never copy all three into `CLAUDE.md` Token Pilot's `doctor` warns when `CLAUDE.md` exceeds 60 lines.
398
89
 
399
90
  ## Supported Languages
400
91
 
@@ -403,56 +94,14 @@ TypeScript, JavaScript, Python, Go, Rust, Java, Kotlin, C#, C/C++, PHP, Ruby. No
403
94
  ## Troubleshooting
404
95
 
405
96
  ```bash
406
- # Verify installation
407
- npx token-pilot doctor
408
-
409
- # Common issues:
410
- # - "ast-index not found" → npx token-pilot install-ast-index
411
- # - "hooks not firing" → restart your AI assistant
412
- # - "stats shows No events" → hook events accumulate in .token-pilot/hook-events.jsonl after the first denied Read
413
-
414
- # Update everything
415
- npm i -g token-pilot@latest
416
- ```
417
-
418
- ## Architecture
419
-
420
- ```
421
- src/
422
- index.ts — CLI entry + MCP server bootstrap
423
- server.ts — MCP server setup, 21 tool definitions
424
- ast-index/ — ast-index binary client + auto-install
425
- core/
426
- event-log.ts — hook-events.jsonl + rotation + retention
427
- session-analytics.ts, policy-engine.ts, intent-classifier.ts
428
- hooks/
429
- installer.ts — Hook install/uninstall for Claude Code
430
- session-start.ts — SessionStart reminder handler
431
- summary-pipeline.ts — ast-index → regex → head+tail → pass-through
432
- cli/
433
- install-agents.ts, uninstall-agents.ts
434
- bless-agents.ts, unbless-agents.ts, doctor-drift.ts
435
- stats.ts
436
- templates/agent-builder.ts
437
- config/loader.ts, defaults.ts
438
- handlers/ — 21 MCP tool handlers
439
- git/ — HEAD + file watchers (cache invalidation)
440
-
441
- scripts/
442
- build-agents.mjs — Render templates/ → dist/agents/
443
- bench-hook.mjs — Hook latency benchmark
444
-
445
- templates/agents/ — Source for tp-* family + shared preamble + contract
97
+ npx token-pilot doctor # diagnose: ast-index, config, upstream drift
98
+ # "ast-index not found" → npx token-pilot install-ast-index
99
+ # "hooks not firing" → restart your AI assistant
446
100
  ```
447
101
 
448
102
  ## Credits
449
103
 
450
- Built on top of:
451
-
452
- - [ast-index](https://github.com/defendend/ast-index) — Tree-sitter AST indexer in Rust (auto-installed)
453
- - [@ast-grep/cli](https://ast-grep.github.io/) — Structural code pattern search
454
- - [MCP SDK](https://github.com/modelcontextprotocol/typescript-sdk) — Model Context Protocol
455
- - [chokidar](https://github.com/paulmillr/chokidar) — File watching
104
+ Built on [ast-index](https://github.com/defendend/ast-index) · [@ast-grep/cli](https://ast-grep.github.io/) · [MCP SDK](https://github.com/modelcontextprotocol/typescript-sdk) · [chokidar](https://github.com/paulmillr/chokidar)
456
105
 
457
106
  ## License
458
107
 
@@ -9,8 +9,8 @@ tools:
9
9
  - mcp__token-pilot__read_symbol
10
10
  - Bash
11
11
  model: haiku
12
- token_pilot_version: "0.28.3"
13
- token_pilot_body_hash: f30fb3378463d6518041650487f1074b5411c6c3d6d7df315d21267f25f812d6
12
+ token_pilot_version: "0.30.0"
13
+ token_pilot_body_hash: c9d33476fdf70c8a7a493ec8720f54792eda2f81585996246e94c130ff3ec356
14
14
  ---
15
15
 
16
16
  You are a token-pilot agent (`tp-<name>`). Your defining contract:
@@ -19,6 +19,8 @@ For every file in a programming language, you MUST use the token-pilot MCP tools
19
19
 
20
20
  If any MCP tool fails, fall back sensibly (another MCP tool → bounded Read → pass-through) and note the fallback in your output. Never silently abandon the contract.
21
21
 
22
+ For heavy Bash operations (test runs, builds, recursive searches, network calls, any command with potentially large stdout): when `mcp__context-mode__execute` or `ctx_batch_execute` is available, use it instead of raw Bash. Context-mode runs commands in a sandbox and only the result enters your context — typically 95% token reduction vs raw stdout dump. This is complementary to token-pilot: we own code reading, context-mode owns command execution.
23
+
22
24
  Your specific role is defined below.
23
25
 
24
26
  Role: public-API diff with semver classification.
@@ -11,8 +11,8 @@ tools:
11
11
  - Grep
12
12
  - Read
13
13
  model: sonnet
14
- token_pilot_version: "0.28.3"
15
- token_pilot_body_hash: a740dc6c928d11d7c2c5fbaa953c50b0e35f2abc2dd6e5ef5117bf469a2d0207
14
+ token_pilot_version: "0.30.0"
15
+ token_pilot_body_hash: 7095ffab66aca2e424f00875933e3f63bc10651eef2fde6a59f08bbbdbf86f7c
16
16
  ---
17
17
 
18
18
  You are a token-pilot agent (`tp-<name>`). Your defining contract:
@@ -21,6 +21,8 @@ For every file in a programming language, you MUST use the token-pilot MCP tools
21
21
 
22
22
  If any MCP tool fails, fall back sensibly (another MCP tool → bounded Read → pass-through) and note the fallback in your output. Never silently abandon the contract.
23
23
 
24
+ For heavy Bash operations (test runs, builds, recursive searches, network calls, any command with potentially large stdout): when `mcp__context-mode__execute` or `ctx_batch_execute` is available, use it instead of raw Bash. Context-mode runs commands in a sandbox and only the result enters your context — typically 95% token reduction vs raw stdout dump. This is complementary to token-pilot: we own code reading, context-mode owns command execution.
25
+
24
26
  Your specific role is defined below.
25
27
 
26
28
  Role: audit scanner — surfaces risks, never fixes.