token-pilot 0.35.0 → 0.39.1

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 (47) hide show
  1. package/.claude-plugin/marketplace.json +2 -2
  2. package/.claude-plugin/plugin.json +1 -1
  3. package/README.md +79 -0
  4. package/agents/tp-api-surface-tracker.md +1 -1
  5. package/agents/tp-audit-scanner.md +1 -1
  6. package/agents/tp-commit-writer.md +1 -1
  7. package/agents/tp-context-engineer.md +1 -1
  8. package/agents/tp-dead-code-finder.md +1 -1
  9. package/agents/tp-debugger.md +1 -1
  10. package/agents/tp-dep-health.md +1 -1
  11. package/agents/tp-doc-writer.md +1 -1
  12. package/agents/tp-history-explorer.md +1 -1
  13. package/agents/tp-impact-analyzer.md +1 -1
  14. package/agents/tp-incident-timeline.md +1 -1
  15. package/agents/tp-incremental-builder.md +1 -1
  16. package/agents/tp-migration-scout.md +1 -1
  17. package/agents/tp-onboard.md +1 -1
  18. package/agents/tp-performance-profiler.md +1 -1
  19. package/agents/tp-pr-reviewer.md +1 -1
  20. package/agents/tp-refactor-planner.md +1 -1
  21. package/agents/tp-review-impact.md +1 -1
  22. package/agents/tp-run.md +1 -1
  23. package/agents/tp-session-restorer.md +1 -1
  24. package/agents/tp-ship-coordinator.md +1 -1
  25. package/agents/tp-spec-writer.md +1 -1
  26. package/agents/tp-test-coverage-gapper.md +1 -1
  27. package/agents/tp-test-triage.md +1 -1
  28. package/agents/tp-test-writer.md +1 -1
  29. package/dist/cli/stats.d.ts +2 -0
  30. package/dist/cli/stats.js +32 -0
  31. package/dist/cli/typo-guard.d.ts +1 -1
  32. package/dist/cli/typo-guard.js +2 -0
  33. package/dist/core/event-log.d.ts +7 -0
  34. package/dist/core/event-log.js +10 -1
  35. package/dist/core/workflow.d.ts +117 -0
  36. package/dist/core/workflow.js +269 -0
  37. package/dist/hooks/post-task.d.ts +18 -3
  38. package/dist/hooks/post-task.js +44 -11
  39. package/dist/hooks/pre-task.d.ts +9 -4
  40. package/dist/hooks/pre-task.js +23 -8
  41. package/dist/hooks/session-start.js +61 -0
  42. package/dist/index.d.ts +14 -0
  43. package/dist/index.js +129 -1
  44. package/package.json +1 -1
  45. package/skills/guide/SKILL.md +11 -0
  46. package/skills/install/SKILL.md +10 -0
  47. package/skills/stats/SKILL.md +8 -0
@@ -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.35.0"
9
+ "version": "0.39.1"
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.35.0",
16
+ "version": "0.39.1",
17
17
  "author": {
18
18
  "name": "Digital-Threads"
19
19
  },
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "token-pilot",
3
- "version": "0.35.0",
3
+ "version": "0.39.1",
4
4
  "description": "Saves 60-90% tokens on AI code reading. AST-aware lazy reads, symbol navigation, find_usages, structural git diff/log, edit-safety guard, Task-routing matcher, cross-session telemetry (errors + diagnostics), 25 tp-* subagents tiered to haiku/sonnet/opus with budget watchdog.",
5
5
  "author": {
6
6
  "name": "Digital-Threads",
package/README.md CHANGED
@@ -232,6 +232,85 @@ lines instead of bouncing the call. The structural summary still
232
232
  rides along in `additionalContext`. Default OFF because the field
233
233
  is undocumented and may change.
234
234
 
235
+ ## What's new for Claude Code 2.1.151+
236
+
237
+ These notes are about behaviour you'll see automatically once you
238
+ update both Claude Code and `token-pilot@latest`. No extra
239
+ configuration required.
240
+
241
+ ### Session title badge (`[TP] Nk saved`)
242
+
243
+ The SessionStart hook now sets the window/tab title to the cumulative
244
+ token savings for the current project, using Claude Code 2.1.152's
245
+ `hookSpecificOutput.sessionTitle` field. You'll see a badge like
246
+ `[TP] 1.2M saved` in the title bar so you can confirm at a glance
247
+ that the plugin is doing its job.
248
+
249
+ ### Hardened skills (`disallowed-tools`)
250
+
251
+ The three bundled skills (`guide`, `install`, `stats`) declare
252
+ `disallowed-tools` (Claude Code 2.1.152+) so a runaway model can't
253
+ issue `Write` / `Edit` / `Task` while the skill is on display. The
254
+ install skill keeps `Bash` because it has to run
255
+ `npx token-pilot install-ast-index`; the other two have Bash
256
+ disallowed too.
257
+
258
+ ### Auto mode on third-party providers
259
+
260
+ Claude Code 2.1.158 opened auto mode to Bedrock / Vertex / Foundry
261
+ on Opus 4.7 + 4.8. If you're on one of those, opt in with
262
+ `CLAUDE_CODE_ENABLE_AUTO_MODE=1`. token-pilot's deny-Read /
263
+ deny-Bash gates still run on top — auto mode never bypasses them.
264
+
265
+ ### Opus 4.8 as fast-mode default
266
+
267
+ Claude Code 2.1.154 made Opus 4.8 the default for high effort. The
268
+ tp-* agents that already declared `model: haiku` keep their cheaper
269
+ tier (90 %+ of the agent roster); the few sonnet/opus-tier ones
270
+ ride the upgrade automatically.
271
+
272
+ ## Fleet workflows (v0.38.0)
273
+
274
+ When you fan a task across many subagents — via Claude Code's
275
+ `/workflow`, the Agent tool, or your own orchestration — token-pilot
276
+ can treat the whole run as one budgeted, telemetry-tagged unit.
277
+
278
+ token-pilot **owns** the workflow boundary, so this works regardless
279
+ of whether Claude Code propagates a workflow id. You wrap the batch:
280
+
281
+ ```bash
282
+ # Start a workflow — prints an export line you eval into your shell
283
+ eval "$(token-pilot workflow start "review every PR from last sprint" --budget=2000000)"
284
+
285
+ # ...now run your fan-out work. Every hook event is tagged with the
286
+ # workflow id automatically (TOKEN_PILOT_WORKFLOW_ID is set).
287
+
288
+ token-pilot workflow status # live budget + task counts
289
+ token-pilot workflow list # all recorded workflows
290
+ token-pilot workflow end # stamp it finished + print summary
291
+ ```
292
+
293
+ While a workflow is active:
294
+
295
+ - Every `event:"task"` / `denied` / `diagnostic` row in
296
+ `hook-events.jsonl` carries `workflow_id`, so you can slice one
297
+ fan-out run out of the global log.
298
+ - The PreToolUse:Task hook watches the token ceiling. At ≥90 % it
299
+ appends a wind-down note to its routing advice ("finish in-flight
300
+ work rather than starting new branches") and logs a
301
+ `workflow_near_budget` diagnostic — visible in `workflow status`.
302
+ Dispatch is never hard-blocked on budget (a half-finished fan-out
303
+ is worse than a small overrun).
304
+ - The window title switches to `[TP] wf · N tasks · X%` so a long run
305
+ shows live progress.
306
+
307
+ Claude Code's own `/workflow` (2.1.154+) does **not** expose a
308
+ per-workflow id env var to subagents (verified against the 2.1.161
309
+ bundle — it has only a `CLAUDE_CODE_WORKFLOWS` feature flag). So
310
+ token-pilot's workflows are independent: they rely on our own
311
+ `TOKEN_PILOT_WORKFLOW_ID`. If CC adds a per-workflow env var later,
312
+ `activeWorkflowId()` already probes for it — no config change needed.
313
+
235
314
  ## Troubleshooting
236
315
 
237
316
  ```bash
@@ -9,7 +9,7 @@ tools:
9
9
  - mcp__token-pilot__read_symbol
10
10
  - Bash
11
11
  model: haiku
12
- token_pilot_version: "0.35.0"
12
+ token_pilot_version: "0.39.1"
13
13
  token_pilot_body_hash: dd184501203fa7f3c73f419c4ffbe33c4be75400cb64a7a51733a3fe23f6e085
14
14
  requiredMcpServers:
15
15
  - "token-pilot"
@@ -11,7 +11,7 @@ tools:
11
11
  - Grep
12
12
  - Read
13
13
  model: sonnet
14
- token_pilot_version: "0.35.0"
14
+ token_pilot_version: "0.39.1"
15
15
  token_pilot_body_hash: d172f600bf32277ea6eb4cbbee4542ddd698a986dcd96997d33930561964569b
16
16
  requiredMcpServers:
17
17
  - "token-pilot"
@@ -8,7 +8,7 @@ tools:
8
8
  - mcp__token-pilot__test_summary
9
9
  - mcp__token-pilot__outline
10
10
  - Bash
11
- token_pilot_version: "0.35.0"
11
+ token_pilot_version: "0.39.1"
12
12
  token_pilot_body_hash: de64a406b5176de19f7422619c7de7949b1f28865f225402c9cea9255f377428
13
13
  requiredMcpServers:
14
14
  - "token-pilot"
@@ -13,7 +13,7 @@ tools:
13
13
  - Edit
14
14
  - Glob
15
15
  model: sonnet
16
- token_pilot_version: "0.35.0"
16
+ token_pilot_version: "0.39.1"
17
17
  token_pilot_body_hash: 68b32af2dacd82ebe52c4eec93edb903d452688274c3065218270627c564d8b0
18
18
  requiredMcpServers:
19
19
  - "token-pilot"
@@ -11,7 +11,7 @@ tools:
11
11
  - Grep
12
12
  - Read
13
13
  model: sonnet
14
- token_pilot_version: "0.35.0"
14
+ token_pilot_version: "0.39.1"
15
15
  token_pilot_body_hash: d9b7f5b7ae6f4ae21305c775361bcab097cc774370a6d976c093571d46d55021
16
16
  requiredMcpServers:
17
17
  - "token-pilot"
@@ -12,7 +12,7 @@ tools:
12
12
  - Read
13
13
  - Bash
14
14
  model: sonnet
15
- token_pilot_version: "0.35.0"
15
+ token_pilot_version: "0.39.1"
16
16
  token_pilot_body_hash: 052413de8d92377edcde6ae5c823f5378db304baccfa29e8866467f42553a500
17
17
  requiredMcpServers:
18
18
  - "token-pilot"
@@ -9,7 +9,7 @@ tools:
9
9
  - Bash
10
10
  - Read
11
11
  model: haiku
12
- token_pilot_version: "0.35.0"
12
+ token_pilot_version: "0.39.1"
13
13
  token_pilot_body_hash: e14dc57493d816f8c2e017963e2ef5f66bea50fd0b805a80e8a0d97c968427e7
14
14
  requiredMcpServers:
15
15
  - "token-pilot"
@@ -13,7 +13,7 @@ tools:
13
13
  - Edit
14
14
  - Glob
15
15
  model: haiku
16
- token_pilot_version: "0.35.0"
16
+ token_pilot_version: "0.39.1"
17
17
  token_pilot_body_hash: 57d741794ab40e31a7ac49c68ea39a9088f5827cdef866ce81bfca1b7c9180cf
18
18
  requiredMcpServers:
19
19
  - "token-pilot"
@@ -10,7 +10,7 @@ tools:
10
10
  - Bash
11
11
  - Read
12
12
  model: haiku
13
- token_pilot_version: "0.35.0"
13
+ token_pilot_version: "0.39.1"
14
14
  token_pilot_body_hash: 7b70fa76a60e3c58a1de4f56c32c0f166424137e203a0cf1c8654e7c9235d904
15
15
  requiredMcpServers:
16
16
  - "token-pilot"
@@ -12,7 +12,7 @@ tools:
12
12
  - mcp__token-pilot__read_symbols
13
13
  - Read
14
14
  model: sonnet
15
- token_pilot_version: "0.35.0"
15
+ token_pilot_version: "0.39.1"
16
16
  token_pilot_body_hash: 351a987e11eba63852f5431a16d8eb53104f4f689f82fdcc5a2bf4db948ba92f
17
17
  requiredMcpServers:
18
18
  - "token-pilot"
@@ -8,7 +8,7 @@ tools:
8
8
  - mcp__token-pilot__read_symbol
9
9
  - Bash
10
10
  model: inherit
11
- token_pilot_version: "0.35.0"
11
+ token_pilot_version: "0.39.1"
12
12
  token_pilot_body_hash: de5722bfea374eaab096c1ae635c37879e7a91370ee3cd0532f4240be03c91eb
13
13
  requiredMcpServers:
14
14
  - "token-pilot"
@@ -13,7 +13,7 @@ tools:
13
13
  - Edit
14
14
  - Bash
15
15
  model: sonnet
16
- token_pilot_version: "0.35.0"
16
+ token_pilot_version: "0.39.1"
17
17
  token_pilot_body_hash: 375a824d0d847bb5453ec594c7a62ad566ee7e4d92717b0473f771f1a0477c60
18
18
  requiredMcpServers:
19
19
  - "token-pilot"
@@ -11,7 +11,7 @@ tools:
11
11
  - Grep
12
12
  - Glob
13
13
  model: sonnet
14
- token_pilot_version: "0.35.0"
14
+ token_pilot_version: "0.39.1"
15
15
  token_pilot_body_hash: 0334de1bf99b431b65359637d125cda7c44c6f780eb92c57cc538715b1939536
16
16
  requiredMcpServers:
17
17
  - "token-pilot"
@@ -10,7 +10,7 @@ tools:
10
10
  - mcp__token-pilot__smart_read
11
11
  - mcp__token-pilot__smart_read_many
12
12
  - mcp__token-pilot__read_section
13
- token_pilot_version: "0.35.0"
13
+ token_pilot_version: "0.39.1"
14
14
  token_pilot_body_hash: 832e95633fbc8e9b0c10f3e540a327d4be062fb4b3f17a6cce6be13f414e2927
15
15
  requiredMcpServers:
16
16
  - "token-pilot"
@@ -11,7 +11,7 @@ tools:
11
11
  - Bash
12
12
  - Read
13
13
  model: sonnet
14
- token_pilot_version: "0.35.0"
14
+ token_pilot_version: "0.39.1"
15
15
  token_pilot_body_hash: b61f06380d80798fa2e49d37bcba0653495bee04dd6bdbc1feff9a75607b0508
16
16
  requiredMcpServers:
17
17
  - "token-pilot"
@@ -11,7 +11,7 @@ tools:
11
11
  - mcp__token-pilot__read_for_edit
12
12
  - Read
13
13
  model: sonnet
14
- token_pilot_version: "0.35.0"
14
+ token_pilot_version: "0.39.1"
15
15
  token_pilot_body_hash: f83f50d05b4f70285ae7afed2b1a406fc436df56e61a0aedbfb31edc7f2b6e66
16
16
  requiredMcpServers:
17
17
  - "token-pilot"
@@ -8,7 +8,7 @@ tools:
8
8
  - mcp__token-pilot__outline
9
9
  - mcp__token-pilot__read_symbol
10
10
  model: sonnet
11
- token_pilot_version: "0.35.0"
11
+ token_pilot_version: "0.39.1"
12
12
  token_pilot_body_hash: c5f6fc122c89e16e5cf774045f92169ee3468555320b898171ba13eca5323550
13
13
  requiredMcpServers:
14
14
  - "token-pilot"
@@ -9,7 +9,7 @@ tools:
9
9
  - mcp__token-pilot__module_info
10
10
  - Bash
11
11
  model: sonnet
12
- token_pilot_version: "0.35.0"
12
+ token_pilot_version: "0.39.1"
13
13
  token_pilot_body_hash: 8ef3c3341cbfed4eb8dd130126a9683edc57e378c92ff0ca764d584fd941c55c
14
14
  requiredMcpServers:
15
15
  - "token-pilot"
package/agents/tp-run.md CHANGED
@@ -16,7 +16,7 @@ tools:
16
16
  - Glob
17
17
  - Bash
18
18
  model: haiku
19
- token_pilot_version: "0.35.0"
19
+ token_pilot_version: "0.39.1"
20
20
  token_pilot_body_hash: 2b08618d34a61f00aafccbda9fed6d83243296dedb83440edbd2d5c28bb6dbc4
21
21
  requiredMcpServers:
22
22
  - "token-pilot"
@@ -9,7 +9,7 @@ tools:
9
9
  - mcp__token-pilot__session_budget
10
10
  - Bash
11
11
  - Read
12
- token_pilot_version: "0.35.0"
12
+ token_pilot_version: "0.39.1"
13
13
  token_pilot_body_hash: 529374ed728f5eed5b758b3be3da65624783c0bf0c1a253d7d661a843eb5f767
14
14
  requiredMcpServers:
15
15
  - "token-pilot"
@@ -11,7 +11,7 @@ tools:
11
11
  - Read
12
12
  - Grep
13
13
  model: sonnet
14
- token_pilot_version: "0.35.0"
14
+ token_pilot_version: "0.39.1"
15
15
  token_pilot_body_hash: a60f6ae110eb3138064bce074e8ba26fa0ce5f4659df1624a9d9d3646803391b
16
16
  requiredMcpServers:
17
17
  - "token-pilot"
@@ -9,7 +9,7 @@ tools:
9
9
  - Read
10
10
  - Write
11
11
  model: sonnet
12
- token_pilot_version: "0.35.0"
12
+ token_pilot_version: "0.39.1"
13
13
  token_pilot_body_hash: c7a4e8b39228fd5158528f389c924c5ff2d98c4b9b05ee0106d54a26c5dc1350
14
14
  requiredMcpServers:
15
15
  - "token-pilot"
@@ -10,7 +10,7 @@ tools:
10
10
  - mcp__token-pilot__test_summary
11
11
  - Glob
12
12
  - Grep
13
- token_pilot_version: "0.35.0"
13
+ token_pilot_version: "0.39.1"
14
14
  token_pilot_body_hash: be81eed53a3720d146cf89e4a14a7a56577633f7c84c234c412ab70d64c05b11
15
15
  requiredMcpServers:
16
16
  - "token-pilot"
@@ -8,7 +8,7 @@ tools:
8
8
  - mcp__token-pilot__find_usages
9
9
  - mcp__token-pilot__read_symbol
10
10
  model: sonnet
11
- token_pilot_version: "0.35.0"
11
+ token_pilot_version: "0.39.1"
12
12
  token_pilot_body_hash: 362ecf4cb03b059421ea26933473700900073dc38b3a7fe271208dfb1ae14f90
13
13
  requiredMcpServers:
14
14
  - "token-pilot"
@@ -13,7 +13,7 @@ tools:
13
13
  - Edit
14
14
  - Bash
15
15
  model: sonnet
16
- token_pilot_version: "0.35.0"
16
+ token_pilot_version: "0.39.1"
17
17
  token_pilot_body_hash: 269f2fe22ff4517c277d3f56ca67d8a5527b93290ab21079a83ba7af22c1b5a9
18
18
  requiredMcpServers:
19
19
  - "token-pilot"
@@ -22,6 +22,8 @@ export interface StatsOptions {
22
22
  byAgent?: boolean;
23
23
  /** v0.31.0 — Task-routing view: subagent_type usage + miss-rate. */
24
24
  tasks?: boolean;
25
+ /** v0.39.0 — aggregate of `event:"workflow"` completion rows. */
26
+ workflows?: boolean;
25
27
  }
26
28
  /**
27
29
  * Pure formatter. Takes the full event list and options; returns the
package/dist/cli/stats.js CHANGED
@@ -67,6 +67,36 @@ export function formatStats(events, opts) {
67
67
  const total = sumSaved(scope);
68
68
  const sessionSuffix = sessionLabel ? ` (session ${sessionLabel})` : "";
69
69
  lines.push(`token-pilot stats${sessionSuffix} — ${scope.length} event${scope.length === 1 ? "" : "s"}, ~${total} tokens saved`);
70
+ if (opts.workflows) {
71
+ // v0.39.0 — workflow completion view. Each `event:"workflow"` row is
72
+ // a frozen summary written by `workflow end`.
73
+ const wf = scope.filter((e) => e.event === "workflow");
74
+ if (wf.length === 0) {
75
+ return lines[0] + "\n\nNo completed workflows yet.";
76
+ }
77
+ let totalUsed = 0;
78
+ let totalTasks = 0;
79
+ let totalOver = 0;
80
+ const rows = [];
81
+ for (const e of wf) {
82
+ const d = (e.detail ?? {});
83
+ const used = d.used_tokens ?? e.estTokens ?? 0;
84
+ const tasks = d.task_count ?? 0;
85
+ const over = d.over_budget_workers ?? 0;
86
+ totalUsed += used;
87
+ totalTasks += tasks;
88
+ totalOver += over;
89
+ const pct = d.pct != null ? ` (${d.pct}%)` : "";
90
+ rows.push(` ${e.workflow_id ?? "?"} ${tasks} tasks · ~${used} tok${pct}` +
91
+ (over > 0 ? ` · ${over} over-budget` : "") +
92
+ (d.goal ? ` — ${d.goal}` : ""));
93
+ }
94
+ lines.push("");
95
+ lines.push(`Completed workflows: ${wf.length} · ${totalTasks} tasks · ~${totalUsed} tokens · ${totalOver} over-budget`);
96
+ lines.push("");
97
+ lines.push(...rows);
98
+ return lines.join("\n");
99
+ }
70
100
  if (opts.tasks) {
71
101
  // v0.31.0 — Task-routing view. Scope to event:"task" records only.
72
102
  const taskEvents = scope.filter((e) => e.event === "task");
@@ -172,10 +202,12 @@ export async function handleStats(argv, opts) {
172
202
  const session = parseFlag(argv, "session");
173
203
  const byAgent = parseFlag(argv, "by-agent");
174
204
  const tasks = parseFlag(argv, "tasks");
205
+ const workflows = parseFlag(argv, "workflows");
175
206
  const rendered = formatStats(events, {
176
207
  session: session === undefined ? undefined : session,
177
208
  byAgent: byAgent === true,
178
209
  tasks: tasks === true,
210
+ workflows: workflows === true,
179
211
  });
180
212
  process.stdout.write(rendered + "\n");
181
213
  return 0;
@@ -17,7 +17,7 @@
17
17
  * Everything else passes through untouched — a real project root like
18
18
  * `/home/user/my-project` or `./subdir` goes to startServer as before.
19
19
  */
20
- export declare const KNOWN_COMMANDS: readonly ["hook-read", "hook-edit", "hook-pre-bash", "hook-pre-grep", "hook-pre-task", "hook-post-bash", "hook-post-task", "hook-session-start", "hook-bootstrap", "install-hook", "uninstall-hook", "install-ast-index", "doctor", "bless-agents", "unbless-agents", "install-agents", "uninstall-agents", "stats", "tool-audit", "save-doc", "list-docs", "init", "migrate-hooks", "errors", "--version", "-v", "--help", "-h"];
20
+ export declare const KNOWN_COMMANDS: readonly ["hook-read", "hook-edit", "hook-pre-bash", "hook-pre-grep", "hook-pre-task", "hook-post-bash", "hook-post-task", "hook-session-start", "hook-bootstrap", "install-hook", "uninstall-hook", "install-ast-index", "doctor", "bless-agents", "unbless-agents", "install-agents", "uninstall-agents", "stats", "tool-audit", "save-doc", "list-docs", "init", "migrate-hooks", "errors", "workflow", "--version", "-v", "--help", "-h"];
21
21
  export interface TypoGuardResult {
22
22
  kind: "pass-through" | "typo";
23
23
  suggestion?: string;
@@ -51,6 +51,8 @@ export const KNOWN_COMMANDS = [
51
51
  // v0.33.0
52
52
  "migrate-hooks",
53
53
  "errors",
54
+ // v0.38.0 — fleet workflow lifecycle
55
+ "workflow",
54
56
  "--version",
55
57
  "-v",
56
58
  "--help",
@@ -38,6 +38,13 @@ export interface HookEvent {
38
38
  * never populate it; events stay shape-compatible.
39
39
  */
40
40
  parent_agent_id?: string | null;
41
+ /**
42
+ * v0.38.0 — id of the token-pilot workflow this event belongs to,
43
+ * when one is active (TOKEN_PILOT_WORKFLOW_ID set). Lets fleet-level
44
+ * budget + telemetry slice a fan-out run out of the global log.
45
+ * Optional — absent when no workflow is active.
46
+ */
47
+ workflow_id?: string;
41
48
  event: "denied" | "allowed" | "bypass" | "pass-through" | "task" | "diagnostic" | string;
42
49
  file: string;
43
50
  lines: number;
@@ -108,9 +108,18 @@ async function rotateIfNeeded(projectRoot, thresholdBytes = ROTATION_THRESHOLD_B
108
108
  */
109
109
  export async function appendEvent(projectRoot, event) {
110
110
  try {
111
+ // v0.38.0 — auto-tag with the active workflow id so every event
112
+ // emitted inside a `token-pilot workflow` boundary is sliceable.
113
+ // Read the env var directly here to keep call sites unchanged; an
114
+ // explicit workflow_id on the event always wins.
115
+ const wf = event.workflow_id ??
116
+ process.env.TOKEN_PILOT_WORKFLOW_ID ??
117
+ process.env.CLAUDE_CODE_WORKFLOW_ID ??
118
+ undefined;
119
+ const tagged = wf ? { ...event, workflow_id: wf } : event;
111
120
  await ensureLogDir(projectRoot);
112
121
  await rotateIfNeeded(projectRoot);
113
- const line = JSON.stringify(event) + "\n";
122
+ const line = JSON.stringify(tagged) + "\n";
114
123
  await fs.appendFile(currentLogPath(projectRoot), line);
115
124
  }
116
125
  catch {
@@ -0,0 +1,117 @@
1
+ /**
2
+ * v0.38.0 — fleet workflow lifecycle.
3
+ *
4
+ * The fleet design note (docs/design/2026-06-tp-fleet-dynamic-workflows.md)
5
+ * flagged one blocker: it assumed Claude Code's `/workflow` would set a
6
+ * propagated workflow-id env var on dispatched subagents. The 2.1.131
7
+ * bundle has no such variable, so building tagging/budget against it
8
+ * would be the v0.34.0-args mistake again (shipping against an
9
+ * interface that may not exist).
10
+ *
11
+ * Resolution: token-pilot OWNS the workflow boundary. A user wraps a
12
+ * batch of fan-out work with `token-pilot workflow start` / `end`,
13
+ * which writes an envelope file and exports `TOKEN_PILOT_WORKFLOW_ID`.
14
+ * Every hook reads that env var and tags its events. No dependency on
15
+ * Claude Code's `/workflow` — and if CC ever propagates its own
16
+ * workflow id env var, we read that too (see activeWorkflowId).
17
+ *
18
+ * State lives in `<projectRoot>/.token-pilot/workflows/`:
19
+ * <id>.json — the envelope (goal, budget, started/ended)
20
+ * Events stay in the normal hook-events.jsonl, tagged with workflow_id.
21
+ *
22
+ * Pure-ish: all filesystem helpers swallow errors (telemetry must
23
+ * never break a hook). The `now`/`id` injection points keep the unit
24
+ * tests deterministic.
25
+ */
26
+ import { type HookEvent } from "./event-log.js";
27
+ export declare const WORKFLOW_SUBDIR = ".token-pilot/workflows";
28
+ export interface WorkflowEnvelope {
29
+ workflow_id: string;
30
+ started_at: number;
31
+ ended_at: number | null;
32
+ goal: string;
33
+ budget_tokens: number | null;
34
+ max_parallel: number | null;
35
+ }
36
+ export interface WorkflowBudgetStatus {
37
+ workflow_id: string;
38
+ goal: string;
39
+ budget_tokens: number | null;
40
+ used_tokens: number;
41
+ pct: number | null;
42
+ event_count: number;
43
+ task_count: number;
44
+ over_budget_workers: number;
45
+ ended: boolean;
46
+ }
47
+ /**
48
+ * Resolve the active workflow id. token-pilot's own env var takes
49
+ * precedence.
50
+ *
51
+ * Note (verified against CC 2.1.161): Claude Code's `/workflow` does
52
+ * NOT export a per-workflow id env var to dispatched subagents — the
53
+ * bundle only has the `CLAUDE_CODE_WORKFLOWS` feature flag and an
54
+ * internal `WorkflowId`. The extra names below are a harmless
55
+ * forward-compat probe (they return null today); token-pilot's fleet
56
+ * workflows are independent and rely solely on our own
57
+ * `TOKEN_PILOT_WORKFLOW_ID`. Returns null when none is set.
58
+ */
59
+ export declare function activeWorkflowId(env?: NodeJS.ProcessEnv): string | null;
60
+ /**
61
+ * Build a workflow id. Format `wf-<base36 ts>-<suffix>`. `now` and
62
+ * `suffix` are injectable for deterministic tests; production passes a
63
+ * timestamp + a short random token.
64
+ */
65
+ export declare function makeWorkflowId(now: number, suffix: string): string;
66
+ export interface StartWorkflowInput {
67
+ projectRoot: string;
68
+ goal: string;
69
+ budgetTokens?: number | null;
70
+ maxParallel?: number | null;
71
+ /** Injectable for tests. */
72
+ now?: number;
73
+ /** Injectable for tests. */
74
+ idSuffix?: string;
75
+ }
76
+ /**
77
+ * Create a workflow envelope and return it. The caller is responsible
78
+ * for exporting `TOKEN_PILOT_WORKFLOW_ID=<id>` into the environment of
79
+ * the work that follows (the CLI prints an `export` line for this).
80
+ */
81
+ export declare function startWorkflow(input: StartWorkflowInput): Promise<WorkflowEnvelope>;
82
+ export declare function loadWorkflow(projectRoot: string, id: string): Promise<WorkflowEnvelope | null>;
83
+ /**
84
+ * Mark a workflow ended (stamps ended_at) and emit a frozen
85
+ * `event:"workflow"` completion record into hook-events.jsonl.
86
+ *
87
+ * v0.39.0 — the envelope stores only the static plan (goal, budget,
88
+ * timestamps); the aggregates (tokens used, task count) are computed
89
+ * live from tagged events. Freezing them in a single summary row at
90
+ * end time makes historical analysis cheap and survives event-log
91
+ * rotation (which could otherwise drop the underlying per-task rows).
92
+ * Returns the updated envelope, or null when the id is unknown.
93
+ */
94
+ export declare function endWorkflow(projectRoot: string, id: string, now?: number): Promise<WorkflowEnvelope | null>;
95
+ /** List all workflow envelopes, newest first. */
96
+ export declare function listWorkflows(projectRoot: string): Promise<WorkflowEnvelope[]>;
97
+ /**
98
+ * Pure aggregation of a workflow's status from its envelope + the set
99
+ * of events tagged with its id. Separated from I/O so tests drive it
100
+ * directly.
101
+ */
102
+ export declare function computeWorkflowStatus(envelope: WorkflowEnvelope, events: HookEvent[]): WorkflowBudgetStatus;
103
+ /**
104
+ * Load a workflow's live status from disk (envelope + tagged events
105
+ * across the repo tree). Returns null when the id is unknown.
106
+ */
107
+ export declare function workflowStatus(projectRoot: string, id: string): Promise<WorkflowBudgetStatus | null>;
108
+ /**
109
+ * Returns true when the workflow's used tokens are within `nearPct`
110
+ * percent of its ceiling (or over). Used by the pre-task hook to warn
111
+ * the fleet to wind down before the ceiling is breached. False when no
112
+ * budget is set.
113
+ */
114
+ export declare function isWorkflowNearBudget(status: WorkflowBudgetStatus, nearPct?: number): boolean;
115
+ export declare function formatWorkflowStatus(status: WorkflowBudgetStatus): string;
116
+ export declare function formatWorkflowList(workflows: WorkflowEnvelope[]): string;
117
+ //# sourceMappingURL=workflow.d.ts.map