smart-context-mcp 1.1.0 → 1.2.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 +110 -20
- package/package.json +10 -7
- package/scripts/init-clients.js +56 -27
- package/scripts/report-metrics.js +5 -0
- package/scripts/report-workflow-metrics.js +255 -0
- package/src/analytics/adoption.js +197 -0
- package/src/storage/sqlite.js +30 -1
- package/src/tools/smart-metrics.js +7 -0
- package/src/tools/smart-read-batch.js +9 -0
- package/src/tools/smart-read.js +21 -1
- package/src/tools/smart-shell.js +33 -9
- package/src/tools/smart-turn.js +1 -0
- package/src/workflow-tracker-stub.js +53 -0
- package/src/workflow-tracker.js +410 -0
package/README.md
CHANGED
|
@@ -7,16 +7,69 @@ MCP server that reduces AI agent token usage by 90% with intelligent context com
|
|
|
7
7
|
|
|
8
8
|
## Installation
|
|
9
9
|
|
|
10
|
+
### Cursor
|
|
10
11
|
```bash
|
|
11
|
-
npm install smart-context-mcp
|
|
12
|
+
npm install -g smart-context-mcp
|
|
13
|
+
npx smart-context-init --target . --clients cursor
|
|
14
|
+
```
|
|
15
|
+
Restart Cursor. Done.
|
|
16
|
+
|
|
17
|
+
### Codex CLI
|
|
18
|
+
```bash
|
|
19
|
+
npm install -g smart-context-mcp
|
|
20
|
+
npx smart-context-init --target . --clients codex
|
|
21
|
+
```
|
|
22
|
+
Restart Codex. Done.
|
|
23
|
+
|
|
24
|
+
### Claude Desktop
|
|
25
|
+
```bash
|
|
26
|
+
npm install -g smart-context-mcp
|
|
27
|
+
npx smart-context-init --target . --clients claude
|
|
28
|
+
```
|
|
29
|
+
Restart Claude Desktop. Done.
|
|
30
|
+
|
|
31
|
+
### Qwen Code
|
|
32
|
+
```bash
|
|
33
|
+
npm install -g smart-context-mcp
|
|
34
|
+
npx smart-context-init --target . --clients qwen
|
|
35
|
+
```
|
|
36
|
+
Restart Qwen Code. Done.
|
|
37
|
+
|
|
38
|
+
### All Clients
|
|
39
|
+
```bash
|
|
40
|
+
npm install -g smart-context-mcp
|
|
12
41
|
npx smart-context-init --target .
|
|
13
42
|
```
|
|
43
|
+
Restart your AI client. Done.
|
|
44
|
+
|
|
45
|
+
## How it Works in Practice
|
|
46
|
+
|
|
47
|
+
**The reality:** This MCP does not intercept prompts automatically. Here's the actual flow:
|
|
48
|
+
|
|
49
|
+
1. **You:** "Fix the login bug"
|
|
50
|
+
2. **Agent reads rules:** Sees debugging workflow
|
|
51
|
+
3. **Agent decides:** Uses `smart_search(intent=debug)`
|
|
52
|
+
4. **MCP returns:** Ranked results (errors prioritized)
|
|
53
|
+
5. **Agent continues:** Calls `smart_read(symbol)` for function
|
|
54
|
+
6. **Agent fixes:** Makes changes
|
|
55
|
+
7. **Agent verifies:** Calls `smart_shell('npm test')`
|
|
56
|
+
8. **Agent checkpoints:** Calls `smart_turn(end)`
|
|
57
|
+
|
|
58
|
+
**Key points:**
|
|
59
|
+
- ✅ Agent **chooses** to use devctx tools (not forced)
|
|
60
|
+
- ✅ Rules **guide** the agent (not enforce)
|
|
61
|
+
- ✅ Agent can use built-in tools when appropriate
|
|
62
|
+
- ✅ Token savings: 85-90% on complex tasks
|
|
14
63
|
|
|
15
|
-
|
|
64
|
+
Check actual usage:
|
|
65
|
+
- `npm run report:metrics` - Tool-level savings
|
|
66
|
+
- `npm run report:workflows` - Workflow-level savings (requires `DEVCTX_WORKFLOW_TRACKING=true`)
|
|
16
67
|
|
|
17
68
|
## What it does
|
|
18
69
|
|
|
19
|
-
|
|
70
|
+
Provides **two key components**:
|
|
71
|
+
|
|
72
|
+
### 1. Specialized Tools (12 tools)
|
|
20
73
|
|
|
21
74
|
| Tool | Purpose | Savings |
|
|
22
75
|
|------|---------|---------|
|
|
@@ -24,8 +77,8 @@ Replaces inefficient file reading and searching with 12 specialized tools:
|
|
|
24
77
|
| `smart_read_batch` | Read multiple files in one call | 90% |
|
|
25
78
|
| `smart_search` | Intent-aware code search with ranking | 95% |
|
|
26
79
|
| `smart_context` | One-call context builder | 85% |
|
|
27
|
-
| `smart_summary` |
|
|
28
|
-
| `smart_turn` |
|
|
80
|
+
| `smart_summary` | Task checkpoint management | 98% |
|
|
81
|
+
| `smart_turn` | Task recovery orchestration | - |
|
|
29
82
|
| `smart_metrics` | Token usage inspection | - |
|
|
30
83
|
| `smart_shell` | Safe command execution | 94% |
|
|
31
84
|
| `build_index` | Symbol index builder | - |
|
|
@@ -33,6 +86,18 @@ Replaces inefficient file reading and searching with 12 specialized tools:
|
|
|
33
86
|
| `git_blame` | Function-level code attribution | - |
|
|
34
87
|
| `cross_project` | Multi-project context | - |
|
|
35
88
|
|
|
89
|
+
### 2. Agent Rules (Task-Specific Guidance)
|
|
90
|
+
|
|
91
|
+
Installation generates rules that teach agents optimal workflows:
|
|
92
|
+
|
|
93
|
+
**Debugging:** `smart_search(intent=debug)` → `smart_read(symbol)` → fix (90% savings)
|
|
94
|
+
**Code Review:** `smart_context(diff=true)` → `smart_read(signatures)` → review (87% savings)
|
|
95
|
+
**Refactoring:** `smart_context(entryFile)` → `smart_read(signatures)` → refactor (89% savings)
|
|
96
|
+
**Testing:** `smart_search(intent=tests)` → `smart_read(symbol)` → write test (90% savings)
|
|
97
|
+
**Architecture:** `smart_context(detail=minimal)` → `smart_read(signatures)` → analyze (90% savings)
|
|
98
|
+
|
|
99
|
+
**Key insight:** The value isn't just in the tools—it's in teaching agents **when** and **how** to use them.
|
|
100
|
+
|
|
36
101
|
## Real Metrics
|
|
37
102
|
|
|
38
103
|
Production usage: **14.5M tokens → 1.6M tokens** (89.87% reduction)
|
|
@@ -80,16 +145,18 @@ Returns: relevant files + compressed content + symbol details + graph relationsh
|
|
|
80
145
|
|
|
81
146
|
### smart_summary
|
|
82
147
|
|
|
83
|
-
Maintain
|
|
148
|
+
Maintain task checkpoint:
|
|
84
149
|
|
|
85
150
|
```javascript
|
|
86
|
-
//
|
|
87
|
-
{ action: 'update', update: { goal: 'Implement OAuth', status: 'in_progress' }}
|
|
151
|
+
// Save checkpoint
|
|
152
|
+
{ action: 'update', update: { goal: 'Implement OAuth', status: 'in_progress', nextStep: '...' }}
|
|
88
153
|
|
|
89
|
-
// Resume
|
|
154
|
+
// Resume task
|
|
90
155
|
{ action: 'get' }
|
|
91
156
|
```
|
|
92
157
|
|
|
158
|
+
Stores compressed task state (~100 tokens: goal, status, decisions, blockers), not full conversation.
|
|
159
|
+
|
|
93
160
|
## New Features
|
|
94
161
|
|
|
95
162
|
### Diff-Aware Context
|
|
@@ -189,8 +256,8 @@ npm run verify
|
|
|
189
256
|
|
|
190
257
|
Data stored in `.devctx/`:
|
|
191
258
|
- `index.json` - Symbol index
|
|
192
|
-
- `state.sqlite` -
|
|
193
|
-
- `metrics.jsonl` - Legacy fallback
|
|
259
|
+
- `state.sqlite` - Task checkpoints, metrics, patterns (Node 22+)
|
|
260
|
+
- `metrics.jsonl` - Legacy fallback (Node 18-20)
|
|
194
261
|
|
|
195
262
|
Add to `.gitignore`:
|
|
196
263
|
```
|
|
@@ -202,22 +269,45 @@ Add to `.gitignore`:
|
|
|
202
269
|
- Node.js 18+ (22+ for SQLite features)
|
|
203
270
|
- Git (for diff and blame features)
|
|
204
271
|
|
|
272
|
+
## Security
|
|
273
|
+
|
|
274
|
+
This MCP is **secure by default**:
|
|
275
|
+
|
|
276
|
+
- ✅ **Allowlist-only commands** - Only safe diagnostic commands (`ls`, `git status`, `npm test`, etc.)
|
|
277
|
+
- ✅ **No shell operators** - Blocks `|`, `&`, `;`, `>`, `<`, `` ` ``, `$()`
|
|
278
|
+
- ✅ **Path validation** - Cannot escape project root
|
|
279
|
+
- ✅ **No write access** - Cannot modify your code
|
|
280
|
+
- ✅ **Repository safety** - Prevents accidental commit of local state
|
|
281
|
+
- ✅ **Resource limits** - 15s timeout, 10MB buffer
|
|
282
|
+
|
|
283
|
+
**Configuration:**
|
|
284
|
+
|
|
285
|
+
```bash
|
|
286
|
+
# Disable shell execution entirely
|
|
287
|
+
export DEVCTX_SHELL_DISABLED=true
|
|
288
|
+
|
|
289
|
+
# Disable cache warming
|
|
290
|
+
export DEVCTX_CACHE_WARMING=false
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
See [SECURITY.md](https://github.com/Arrayo/smart-context-mcp/blob/main/SECURITY.md) for complete security documentation.
|
|
294
|
+
|
|
205
295
|
## Documentation
|
|
206
296
|
|
|
207
|
-
Full documentation in [GitHub repository](https://github.com/Arrayo/
|
|
297
|
+
Full documentation in [GitHub repository](https://github.com/Arrayo/smart-context-mcp):
|
|
208
298
|
|
|
209
|
-
- [
|
|
210
|
-
- [
|
|
211
|
-
- [
|
|
212
|
-
- [
|
|
213
|
-
- [
|
|
214
|
-
- [
|
|
299
|
+
- [Streaming Progress](https://github.com/Arrayo/smart-context-mcp/blob/main/docs/features/streaming.md) - Progress notifications
|
|
300
|
+
- [Context Prediction](https://github.com/Arrayo/smart-context-mcp/blob/main/docs/features/context-prediction.md) - File prediction
|
|
301
|
+
- [Diff-Aware Context](https://github.com/Arrayo/smart-context-mcp/blob/main/docs/features/diff-aware.md) - Change analysis
|
|
302
|
+
- [Cache Warming](https://github.com/Arrayo/smart-context-mcp/blob/main/docs/features/cache-warming.md) - Cold-start optimization
|
|
303
|
+
- [Git Blame](https://github.com/Arrayo/smart-context-mcp/blob/main/docs/features/git-blame.md) - Code attribution
|
|
304
|
+
- [Cross-Project Context](https://github.com/Arrayo/smart-context-mcp/blob/main/docs/features/cross-project.md) - Multi-project support
|
|
215
305
|
|
|
216
306
|
## Links
|
|
217
307
|
|
|
218
|
-
- [GitHub](https://github.com/Arrayo/
|
|
308
|
+
- [GitHub](https://github.com/Arrayo/smart-context-mcp)
|
|
219
309
|
- [npm](https://www.npmjs.com/package/smart-context-mcp)
|
|
220
|
-
- [Issues](https://github.com/Arrayo/
|
|
310
|
+
- [Issues](https://github.com/Arrayo/smart-context-mcp/issues)
|
|
221
311
|
|
|
222
312
|
## Author
|
|
223
313
|
|
package/package.json
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "smart-context-mcp",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "MCP server that reduces agent token usage
|
|
3
|
+
"version": "1.2.0",
|
|
4
|
+
"description": "MCP server that reduces agent token usage by 90% with intelligent context compression, task checkpoint persistence, and workflow-aware agent guidance.",
|
|
5
5
|
"author": "Francisco Caballero Portero <fcp1978@hotmail.com>",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
|
-
"url": "git+https://github.com/Arrayo/
|
|
9
|
+
"url": "git+https://github.com/Arrayo/smart-context-mcp.git"
|
|
10
10
|
},
|
|
11
|
-
"homepage": "https://github.com/Arrayo/
|
|
11
|
+
"homepage": "https://github.com/Arrayo/smart-context-mcp#readme",
|
|
12
12
|
"bugs": {
|
|
13
|
-
"url": "https://github.com/Arrayo/
|
|
13
|
+
"url": "https://github.com/Arrayo/smart-context-mcp/issues"
|
|
14
14
|
},
|
|
15
15
|
"bin": {
|
|
16
16
|
"smart-context-headless": "scripts/headless-wrapper.js",
|
|
@@ -30,7 +30,8 @@
|
|
|
30
30
|
"scripts/devctx-server.js",
|
|
31
31
|
"scripts/headless-wrapper.js",
|
|
32
32
|
"scripts/init-clients.js",
|
|
33
|
-
"scripts/report-metrics.js"
|
|
33
|
+
"scripts/report-metrics.js",
|
|
34
|
+
"scripts/report-workflow-metrics.js"
|
|
34
35
|
],
|
|
35
36
|
"engines": {
|
|
36
37
|
"node": ">=18"
|
|
@@ -52,12 +53,14 @@
|
|
|
52
53
|
"smoke:formats": "node ./scripts/format-smoke.js",
|
|
53
54
|
"test": "node --test --test-concurrency=1 ./tests/*.test.js",
|
|
54
55
|
"verify": "node ./scripts/verify-features-direct.js",
|
|
56
|
+
"benchmark": "node ./scripts/run-benchmark.js",
|
|
55
57
|
"eval": "node ./evals/harness.js",
|
|
56
58
|
"eval:context": "node ./evals/harness.js --tool=context",
|
|
57
59
|
"eval:both": "node ./evals/harness.js --tool=both",
|
|
58
60
|
"eval:self": "node ./evals/harness.js --root=../.. --corpus=./evals/corpus/self-tasks.json",
|
|
59
61
|
"eval:report": "node ./evals/report.js",
|
|
60
|
-
"report:metrics": "node ./scripts/report-metrics.js"
|
|
62
|
+
"report:metrics": "node ./scripts/report-metrics.js",
|
|
63
|
+
"report:workflows": "node ./scripts/report-workflow-metrics.js"
|
|
61
64
|
},
|
|
62
65
|
"dependencies": {
|
|
63
66
|
"@modelcontextprotocol/sdk": "^1.13.0",
|
package/scripts/init-clients.js
CHANGED
|
@@ -375,42 +375,71 @@ const updatePreCommitHook = (targetDir, dryRun) => {
|
|
|
375
375
|
// Agent rules — instruct agents to prefer devctx tools over built-in ones
|
|
376
376
|
// ---------------------------------------------------------------------------
|
|
377
377
|
|
|
378
|
-
const agentRuleBody =
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
-
|
|
382
|
-
-
|
|
383
|
-
-
|
|
384
|
-
-
|
|
385
|
-
|
|
386
|
-
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
- Review: smart_turn(start) → smart_search with intent=implementation → read outline/signatures, focus on changed symbols, minimal changes → smart_turn(end event=milestone).
|
|
398
|
-
- Refactor: smart_turn(start) → smart_search with intent=implementation → signatures for public API, preserve behavior, small edits, verify with tests → smart_turn(end event=milestone).
|
|
399
|
-
- Tests: smart_turn(start) → smart_search with intent=tests → find existing tests, read symbol of function under test → smart_turn(end event=milestone).
|
|
400
|
-
- Config: smart_search with intent=config → find settings, env vars, infrastructure files.
|
|
401
|
-
- Architecture: smart_turn(start) → smart_search with intent=explore → directory structure, outlines of key modules and API boundaries → smart_turn(end event=task_switch or milestone).`;
|
|
378
|
+
const agentRuleBody = `**First time in project?** Run build_index to enable search/context quality.
|
|
379
|
+
|
|
380
|
+
Prefer devctx MCP for non-trivial tasks:
|
|
381
|
+
- smart_read(outline|signatures|symbol) instead of Read → 90% savings
|
|
382
|
+
- smart_search(intent=...) instead of Grep → ranked results
|
|
383
|
+
- smart_context instead of multiple reads → one-call builder
|
|
384
|
+
- smart_shell instead of Shell → safe diagnostics
|
|
385
|
+
|
|
386
|
+
For non-trivial tasks: smart_turn(start) → [work with devctx tools] → smart_turn(end)
|
|
387
|
+
|
|
388
|
+
Reading cascade: outline → signatures → symbol → full (last resort)
|
|
389
|
+
|
|
390
|
+
Detailed workflows: .cursor/rules/profiles-compact/ (debugging, code-review, refactoring, testing, architecture)
|
|
391
|
+
|
|
392
|
+
---
|
|
393
|
+
|
|
394
|
+
If you didn't use devctx tools in a non-trivial programming task, add at the end:
|
|
395
|
+
|
|
396
|
+
**Note:** devctx not used because: [task too simple | MCP unavailable | index not built | already had sufficient context | native tool more direct for this case]. To use devctx next time: "Use smart-context-mcp: smart_turn(start) → smart_context/smart_search → smart_read → smart_turn(end)"`;
|
|
402
397
|
|
|
403
398
|
const cursorRuleContent = `---
|
|
404
|
-
description: Prefer devctx MCP tools for
|
|
399
|
+
description: Prefer devctx MCP tools for non-trivial tasks
|
|
405
400
|
alwaysApply: true
|
|
406
401
|
---
|
|
407
402
|
|
|
408
403
|
${agentRuleBody}
|
|
409
404
|
`;
|
|
410
405
|
|
|
406
|
+
const cursorProfilesNote = `
|
|
407
|
+
# Task-Specific Profiles
|
|
408
|
+
|
|
409
|
+
For detailed workflows, see rules in this directory:
|
|
410
|
+
- debugging.mdc - Error-first, symbol-focused (90% savings)
|
|
411
|
+
- code-review.mdc - Diff-aware, API-focused (87% savings)
|
|
412
|
+
- refactoring.mdc - Graph-aware, test-verified (89% savings)
|
|
413
|
+
- testing.mdc - Coverage-aware, TDD-friendly (90% savings)
|
|
414
|
+
- architecture.mdc - Index-first, minimal-detail (90% savings)
|
|
415
|
+
|
|
416
|
+
These profiles are **conditionally applied** based on file globs and task context.
|
|
417
|
+
The base rule (devctx.mdc) is **always active** but kept minimal to reduce fixed context cost.
|
|
418
|
+
`;
|
|
419
|
+
|
|
411
420
|
const updateCursorRule = (targetDir, dryRun) => {
|
|
412
|
-
const
|
|
413
|
-
|
|
421
|
+
const rulesDir = path.join(targetDir, '.cursor', 'rules');
|
|
422
|
+
const profilesDir = path.join(rulesDir, 'profiles-compact');
|
|
423
|
+
|
|
424
|
+
// Write base rule (always active)
|
|
425
|
+
const baseFilePath = path.join(rulesDir, 'devctx.mdc');
|
|
426
|
+
writeFile(baseFilePath, cursorRuleContent, dryRun);
|
|
427
|
+
|
|
428
|
+
// Write profiles README
|
|
429
|
+
const profilesReadmePath = path.join(profilesDir, 'README.md');
|
|
430
|
+
writeFile(profilesReadmePath, cursorProfilesNote, dryRun);
|
|
431
|
+
|
|
432
|
+
// Copy compact profiles from package
|
|
433
|
+
const sourceProfilesDir = path.join(devctxDir, 'agent-rules', 'profiles-compact');
|
|
434
|
+
if (fs.existsSync(sourceProfilesDir)) {
|
|
435
|
+
const profiles = fs.readdirSync(sourceProfilesDir).filter(f => f.endsWith('.mdc'));
|
|
436
|
+
profiles.forEach(profile => {
|
|
437
|
+
const sourcePath = path.join(sourceProfilesDir, profile);
|
|
438
|
+
const targetPath = path.join(profilesDir, profile);
|
|
439
|
+
const content = fs.readFileSync(sourcePath, 'utf8');
|
|
440
|
+
writeFile(targetPath, content, dryRun);
|
|
441
|
+
});
|
|
442
|
+
}
|
|
414
443
|
};
|
|
415
444
|
|
|
416
445
|
const SECTION_START = '<!-- devctx:start -->';
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
4
|
import { smartMetrics } from '../src/tools/smart-metrics.js';
|
|
5
|
+
import { formatAdoptionReport } from '../src/analytics/adoption.js';
|
|
5
6
|
|
|
6
7
|
const requireValue = (argv, index, flag) => {
|
|
7
8
|
const value = argv[index + 1];
|
|
@@ -88,6 +89,10 @@ const printHuman = (report) => {
|
|
|
88
89
|
` ${tool.tool.padEnd(14)} count=${formatNumber(tool.count)} raw=${formatNumber(tool.rawTokens)} final=${formatNumber(tool.compressedTokens)} saved=${formatNumber(tool.savedTokens)} (${tool.savingsPct}%)`
|
|
89
90
|
);
|
|
90
91
|
}
|
|
92
|
+
|
|
93
|
+
if (report.adoption) {
|
|
94
|
+
console.log(formatAdoptionReport(report.adoption));
|
|
95
|
+
}
|
|
91
96
|
};
|
|
92
97
|
|
|
93
98
|
export const main = async () => {
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
4
|
+
|
|
5
|
+
// Dynamic import to handle SQLite availability
|
|
6
|
+
let workflowTracker;
|
|
7
|
+
try {
|
|
8
|
+
workflowTracker = await import('../src/workflow-tracker.js');
|
|
9
|
+
} catch {
|
|
10
|
+
workflowTracker = await import('../src/workflow-tracker-stub.js');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const { getWorkflowMetrics, getWorkflowSummaryByType, WORKFLOW_DEFINITIONS } = workflowTracker;
|
|
14
|
+
|
|
15
|
+
const parseArgs = (argv) => {
|
|
16
|
+
const options = {
|
|
17
|
+
type: null,
|
|
18
|
+
sessionId: null,
|
|
19
|
+
limit: 10,
|
|
20
|
+
json: false,
|
|
21
|
+
summary: false,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
for (let index = 0; index < argv.length; index += 1) {
|
|
25
|
+
const token = argv[index];
|
|
26
|
+
|
|
27
|
+
if (token === '--type') {
|
|
28
|
+
options.type = argv[index + 1];
|
|
29
|
+
index += 1;
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (token === '--session') {
|
|
34
|
+
options.sessionId = argv[index + 1];
|
|
35
|
+
index += 1;
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (token === '--limit') {
|
|
40
|
+
options.limit = parseInt(argv[index + 1], 10);
|
|
41
|
+
index += 1;
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (token === '--json') {
|
|
46
|
+
options.json = true;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (token === '--summary') {
|
|
51
|
+
options.summary = true;
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (token === '--help' || token === '-h') {
|
|
56
|
+
console.log(`
|
|
57
|
+
Usage: report-workflow-metrics [options]
|
|
58
|
+
|
|
59
|
+
Options:
|
|
60
|
+
--type <type> Filter by workflow type (debugging, code-review, refactoring, testing, architecture)
|
|
61
|
+
--session <id> Filter by session ID
|
|
62
|
+
--limit <n> Limit number of workflows (default: 10)
|
|
63
|
+
--summary Show summary by workflow type
|
|
64
|
+
--json Output as JSON
|
|
65
|
+
--help, -h Show this help
|
|
66
|
+
|
|
67
|
+
Examples:
|
|
68
|
+
# Show summary by workflow type
|
|
69
|
+
npm run report:workflows -- --summary
|
|
70
|
+
|
|
71
|
+
# Show last 10 debugging workflows
|
|
72
|
+
npm run report:workflows -- --type debugging
|
|
73
|
+
|
|
74
|
+
# Show workflows for specific session
|
|
75
|
+
npm run report:workflows -- --session abc123
|
|
76
|
+
|
|
77
|
+
# Output as JSON
|
|
78
|
+
npm run report:workflows -- --json
|
|
79
|
+
`);
|
|
80
|
+
process.exit(0);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
throw new Error(`Unknown argument: ${token}`);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return options;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
const formatNumber = (value) => new Intl.NumberFormat('en-US').format(value);
|
|
90
|
+
|
|
91
|
+
const formatDuration = (ms) => {
|
|
92
|
+
if (ms < 1000) return `${ms}ms`;
|
|
93
|
+
if (ms < 60000) return `${(ms / 1000).toFixed(1)}s`;
|
|
94
|
+
if (ms < 3600000) return `${(ms / 60000).toFixed(1)}m`;
|
|
95
|
+
return `${(ms / 3600000).toFixed(1)}h`;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const printSummary = (summary) => {
|
|
99
|
+
console.log('');
|
|
100
|
+
console.log('Workflow Metrics Summary');
|
|
101
|
+
console.log('═'.repeat(120));
|
|
102
|
+
console.log('');
|
|
103
|
+
|
|
104
|
+
if (!Array.isArray(summary) || summary.length === 0) {
|
|
105
|
+
console.log('No completed workflows found.');
|
|
106
|
+
console.log('');
|
|
107
|
+
console.log('Workflows are tracked when:');
|
|
108
|
+
console.log(' 1. Agent calls smart_turn(start) with a task description');
|
|
109
|
+
console.log(' 2. Task matches a workflow pattern (debugging, review, refactor, testing, architecture)');
|
|
110
|
+
console.log(' 3. Agent calls smart_turn(end) to complete the workflow');
|
|
111
|
+
console.log('');
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const totalWorkflows = summary.reduce((sum, s) => sum + s.count, 0);
|
|
116
|
+
const totalRaw = summary.reduce((sum, s) => sum + s.total_raw_tokens, 0);
|
|
117
|
+
const totalCompressed = summary.reduce((sum, s) => sum + s.total_compressed_tokens, 0);
|
|
118
|
+
const totalSaved = summary.reduce((sum, s) => sum + s.total_saved_tokens, 0);
|
|
119
|
+
const totalBaseline = summary.reduce((sum, s) => sum + s.total_baseline_tokens, 0);
|
|
120
|
+
|
|
121
|
+
console.log(`Total Workflows: ${formatNumber(totalWorkflows)}`);
|
|
122
|
+
console.log(`Total Raw Tokens: ${formatNumber(totalRaw)}`);
|
|
123
|
+
console.log(`Total Compressed Tokens: ${formatNumber(totalCompressed)}`);
|
|
124
|
+
console.log(`Total Saved Tokens: ${formatNumber(totalSaved)} (${((totalSaved / totalRaw) * 100).toFixed(2)}%)`);
|
|
125
|
+
console.log(`Total Baseline Tokens: ${formatNumber(totalBaseline)}`);
|
|
126
|
+
console.log(`Savings vs Baseline: ${formatNumber(totalBaseline - totalCompressed)} (${(((totalBaseline - totalCompressed) / totalBaseline) * 100).toFixed(2)}%)`);
|
|
127
|
+
console.log('');
|
|
128
|
+
console.log('By Workflow Type:');
|
|
129
|
+
console.log('─'.repeat(120));
|
|
130
|
+
console.log(
|
|
131
|
+
'Type'.padEnd(20) +
|
|
132
|
+
'Count'.padStart(8) +
|
|
133
|
+
'Avg Steps'.padStart(12) +
|
|
134
|
+
'Avg Duration'.padStart(15) +
|
|
135
|
+
'Avg Savings'.padStart(15) +
|
|
136
|
+
'vs Baseline'.padStart(15),
|
|
137
|
+
);
|
|
138
|
+
console.log('─'.repeat(120));
|
|
139
|
+
|
|
140
|
+
for (const s of summary) {
|
|
141
|
+
const def = WORKFLOW_DEFINITIONS[s.workflow_type];
|
|
142
|
+
const name = def ? def.name : s.workflow_type;
|
|
143
|
+
|
|
144
|
+
console.log(
|
|
145
|
+
name.padEnd(20) +
|
|
146
|
+
formatNumber(s.count).padStart(8) +
|
|
147
|
+
s.avgStepsCount.toString().padStart(12) +
|
|
148
|
+
formatDuration(s.avgDurationMs).padStart(15) +
|
|
149
|
+
`${s.avgSavingsPct}%`.padStart(15) +
|
|
150
|
+
`${s.avgVsBaselinePct}%`.padStart(15),
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
console.log('─'.repeat(120));
|
|
155
|
+
console.log('');
|
|
156
|
+
|
|
157
|
+
// Show detailed breakdown for each workflow type
|
|
158
|
+
console.log('Detailed Breakdown:');
|
|
159
|
+
console.log('');
|
|
160
|
+
|
|
161
|
+
for (const s of summary) {
|
|
162
|
+
const def = WORKFLOW_DEFINITIONS[s.workflow_type];
|
|
163
|
+
const name = def ? def.name : s.workflow_type;
|
|
164
|
+
|
|
165
|
+
console.log(`${name}:`);
|
|
166
|
+
console.log(` Workflows: ${formatNumber(s.count)}`);
|
|
167
|
+
console.log(` Avg Steps: ${s.avgStepsCount}`);
|
|
168
|
+
console.log(` Avg Duration: ${formatDuration(s.avgDurationMs)}`);
|
|
169
|
+
console.log(` Total Raw Tokens: ${formatNumber(s.total_raw_tokens)}`);
|
|
170
|
+
console.log(` Total Compressed Tokens: ${formatNumber(s.total_compressed_tokens)}`);
|
|
171
|
+
console.log(` Total Saved Tokens: ${formatNumber(s.total_saved_tokens)} (${s.avgSavingsPct}%)`);
|
|
172
|
+
console.log(` Baseline Tokens: ${formatNumber(s.total_baseline_tokens)}`);
|
|
173
|
+
console.log(` Savings vs Baseline: ${formatNumber(s.total_baseline_tokens - s.total_compressed_tokens)} (${s.avgVsBaselinePct}%)`);
|
|
174
|
+
console.log('');
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const printWorkflows = (workflows) => {
|
|
179
|
+
console.log('');
|
|
180
|
+
console.log('Recent Workflows');
|
|
181
|
+
console.log('═'.repeat(120));
|
|
182
|
+
console.log('');
|
|
183
|
+
|
|
184
|
+
if (workflows.length === 0) {
|
|
185
|
+
console.log('No workflows found.');
|
|
186
|
+
console.log('');
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
for (const w of workflows) {
|
|
191
|
+
const def = WORKFLOW_DEFINITIONS[w.workflow_type];
|
|
192
|
+
const name = def ? def.name : w.workflow_type;
|
|
193
|
+
const status = w.end_time ? '✓ Completed' : '⏳ In Progress';
|
|
194
|
+
|
|
195
|
+
console.log(`${name} (${status})`);
|
|
196
|
+
console.log(` Workflow ID: ${w.workflow_id}`);
|
|
197
|
+
console.log(` Session ID: ${w.session_id || 'N/A'}`);
|
|
198
|
+
console.log(` Started: ${new Date(w.start_time).toLocaleString()}`);
|
|
199
|
+
|
|
200
|
+
if (w.end_time) {
|
|
201
|
+
console.log(` Ended: ${new Date(w.end_time).toLocaleString()}`);
|
|
202
|
+
console.log(` Duration: ${formatDuration(w.duration_ms)}`);
|
|
203
|
+
console.log(` Steps: ${w.steps_count}`);
|
|
204
|
+
console.log(` Tools Used: ${w.toolsUsed.join(', ')}`);
|
|
205
|
+
console.log(` Raw Tokens: ${formatNumber(w.raw_tokens)}`);
|
|
206
|
+
console.log(` Compressed Tokens: ${formatNumber(w.compressed_tokens)}`);
|
|
207
|
+
console.log(` Saved Tokens: ${formatNumber(w.saved_tokens)} (${w.savings_pct}%)`);
|
|
208
|
+
console.log(` Baseline Tokens: ${formatNumber(w.baseline_tokens)}`);
|
|
209
|
+
console.log(` Savings vs Baseline: ${formatNumber(w.baseline_tokens - w.compressed_tokens)} (${w.vs_baseline_pct}%)`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
console.log('');
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const main = async () => {
|
|
217
|
+
try {
|
|
218
|
+
const options = parseArgs(process.argv.slice(2));
|
|
219
|
+
|
|
220
|
+
if (options.summary) {
|
|
221
|
+
const summary = getWorkflowSummaryByType();
|
|
222
|
+
|
|
223
|
+
if (options.json) {
|
|
224
|
+
console.log(JSON.stringify(summary, null, 2));
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
printSummary(summary);
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const workflows = getWorkflowMetrics({
|
|
233
|
+
workflowType: options.type,
|
|
234
|
+
sessionId: options.sessionId,
|
|
235
|
+
limit: options.limit,
|
|
236
|
+
completed: true,
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
if (options.json) {
|
|
240
|
+
console.log(JSON.stringify(workflows, null, 2));
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
printWorkflows(workflows);
|
|
245
|
+
} catch (error) {
|
|
246
|
+
console.error('Error:', error.message);
|
|
247
|
+
process.exit(1);
|
|
248
|
+
}
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
const isDirectExecution = process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url);
|
|
252
|
+
|
|
253
|
+
if (isDirectExecution) {
|
|
254
|
+
main();
|
|
255
|
+
}
|