@ryuenn3123/agentic-senior-core 2.0.2 → 2.0.4
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/.agent-context/state/token-optimization-benchmark.json +130 -0
- package/.cursorrules +1 -1
- package/.windsurfrules +1 -1
- package/README.md +28 -2
- package/lib/cli/commands/init.mjs +11 -7
- package/lib/cli/utils.mjs +3 -3
- package/package.json +2 -1
- package/scripts/token-optimization-benchmark.mjs +252 -0
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
{
|
|
2
|
+
"generatedAt": "2026-04-11T06:14:10.052Z",
|
|
3
|
+
"reportName": "token-optimization-benchmark",
|
|
4
|
+
"methodology": {
|
|
5
|
+
"tokenEstimate": "estimated_tokens = ceil(output_chars / 4)",
|
|
6
|
+
"scope": "command-output estimate only; model-specific tokenization differs by provider",
|
|
7
|
+
"repositoryRoot": "E:\\Project\\Agentic-Senior-Core"
|
|
8
|
+
},
|
|
9
|
+
"environment": {
|
|
10
|
+
"nodeVersion": "v22.22.0",
|
|
11
|
+
"platform": "win32",
|
|
12
|
+
"rtk": {
|
|
13
|
+
"isAvailable": true,
|
|
14
|
+
"version": "0.35.0",
|
|
15
|
+
"source": "local-tools",
|
|
16
|
+
"executablePath": "E:\\Project\\Agentic-Senior-Core\\.benchmarks\\tools\\rtk\\rtk.exe",
|
|
17
|
+
"reason": null
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"scenarios": [
|
|
21
|
+
{
|
|
22
|
+
"scenarioId": "git-show",
|
|
23
|
+
"scenarioName": "Latest commit detail review",
|
|
24
|
+
"baseline": {
|
|
25
|
+
"command": "git show HEAD --stat --patch --no-color",
|
|
26
|
+
"exitCode": 0,
|
|
27
|
+
"outputChars": 15191,
|
|
28
|
+
"outputLines": 368,
|
|
29
|
+
"estimatedTokens": 3798
|
|
30
|
+
},
|
|
31
|
+
"nativeOptimized": {
|
|
32
|
+
"command": "git show HEAD --stat --no-color",
|
|
33
|
+
"exitCode": 0,
|
|
34
|
+
"outputChars": 706,
|
|
35
|
+
"outputLines": 14,
|
|
36
|
+
"estimatedTokens": 177
|
|
37
|
+
},
|
|
38
|
+
"nativeSavings": {
|
|
39
|
+
"tokenDelta": 3621,
|
|
40
|
+
"savingsPercent": 95.34
|
|
41
|
+
},
|
|
42
|
+
"rtk": {
|
|
43
|
+
"command": "E:\\Project\\Agentic-Senior-Core\\.benchmarks\\tools\\rtk\\rtk.exe git show HEAD --stat --patch --no-color",
|
|
44
|
+
"exitCode": 0,
|
|
45
|
+
"outputChars": 15191,
|
|
46
|
+
"outputLines": 368,
|
|
47
|
+
"estimatedTokens": 3798
|
|
48
|
+
},
|
|
49
|
+
"rtkSavings": {
|
|
50
|
+
"tokenDelta": 0,
|
|
51
|
+
"savingsPercent": 0
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
"scenarioId": "git-log",
|
|
56
|
+
"scenarioName": "Commit history review",
|
|
57
|
+
"baseline": {
|
|
58
|
+
"command": "git log -n 50",
|
|
59
|
+
"exitCode": 0,
|
|
60
|
+
"outputChars": 14918,
|
|
61
|
+
"outputLines": 385,
|
|
62
|
+
"estimatedTokens": 3730
|
|
63
|
+
},
|
|
64
|
+
"nativeOptimized": {
|
|
65
|
+
"command": "git log --oneline -n 50",
|
|
66
|
+
"exitCode": 0,
|
|
67
|
+
"outputChars": 3555,
|
|
68
|
+
"outputLines": 50,
|
|
69
|
+
"estimatedTokens": 889
|
|
70
|
+
},
|
|
71
|
+
"nativeSavings": {
|
|
72
|
+
"tokenDelta": 2841,
|
|
73
|
+
"savingsPercent": 76.17
|
|
74
|
+
},
|
|
75
|
+
"rtk": {
|
|
76
|
+
"command": "E:\\Project\\Agentic-Senior-Core\\.benchmarks\\tools\\rtk\\rtk.exe git log -n 50",
|
|
77
|
+
"exitCode": 0,
|
|
78
|
+
"outputChars": 6437,
|
|
79
|
+
"outputLines": 74,
|
|
80
|
+
"estimatedTokens": 1610
|
|
81
|
+
},
|
|
82
|
+
"rtkSavings": {
|
|
83
|
+
"tokenDelta": 2120,
|
|
84
|
+
"savingsPercent": 56.84
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
"scenarioId": "search-token",
|
|
89
|
+
"scenarioName": "Search result scan",
|
|
90
|
+
"baseline": {
|
|
91
|
+
"command": "git grep -n token",
|
|
92
|
+
"exitCode": 0,
|
|
93
|
+
"outputChars": 21353,
|
|
94
|
+
"outputLines": 190,
|
|
95
|
+
"estimatedTokens": 5339
|
|
96
|
+
},
|
|
97
|
+
"nativeOptimized": {
|
|
98
|
+
"command": "git grep -n token README.md docs",
|
|
99
|
+
"exitCode": 0,
|
|
100
|
+
"outputChars": 4395,
|
|
101
|
+
"outputLines": 38,
|
|
102
|
+
"estimatedTokens": 1099
|
|
103
|
+
},
|
|
104
|
+
"nativeSavings": {
|
|
105
|
+
"tokenDelta": 4240,
|
|
106
|
+
"savingsPercent": 79.42
|
|
107
|
+
},
|
|
108
|
+
"rtk": {
|
|
109
|
+
"command": "E:\\Project\\Agentic-Senior-Core\\.benchmarks\\tools\\rtk\\rtk.exe git grep -n token",
|
|
110
|
+
"exitCode": 0,
|
|
111
|
+
"outputChars": 21353,
|
|
112
|
+
"outputLines": 190,
|
|
113
|
+
"estimatedTokens": 5339
|
|
114
|
+
},
|
|
115
|
+
"rtkSavings": {
|
|
116
|
+
"tokenDelta": 0,
|
|
117
|
+
"savingsPercent": 0
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
],
|
|
121
|
+
"summary": {
|
|
122
|
+
"scenarioCount": 3,
|
|
123
|
+
"averageNativeSavingsPercent": 83.64,
|
|
124
|
+
"averageRtkSavingsPercent": 18.95
|
|
125
|
+
},
|
|
126
|
+
"artifact": {
|
|
127
|
+
"path": "E:\\Project\\Agentic-Senior-Core\\.agent-context\\state\\token-optimization-benchmark.json",
|
|
128
|
+
"writeMode": "stdout-and-file"
|
|
129
|
+
}
|
|
130
|
+
}
|
package/.cursorrules
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# AGENTIC-SENIOR-CORE DYNAMIC GOVERNANCE RULESET
|
|
2
2
|
|
|
3
|
-
Generated by Agentic-Senior-Core CLI v2.0.
|
|
3
|
+
Generated by Agentic-Senior-Core CLI v2.0.4
|
|
4
4
|
Timestamp: 2026-04-08T14:58:53.570Z
|
|
5
5
|
Selected profile: beginner
|
|
6
6
|
Selected policy file: .agent-context/policies/llm-judge-threshold.json
|
package/.windsurfrules
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# AGENTIC-SENIOR-CORE DYNAMIC GOVERNANCE RULESET
|
|
2
2
|
|
|
3
|
-
Generated by Agentic-Senior-Core CLI v2.0.
|
|
3
|
+
Generated by Agentic-Senior-Core CLI v2.0.4
|
|
4
4
|
Timestamp: 2026-04-08T14:58:53.570Z
|
|
5
5
|
Selected profile: beginner
|
|
6
6
|
Selected policy file: .agent-context/policies/llm-judge-threshold.json
|
package/README.md
CHANGED
|
@@ -128,10 +128,11 @@ agentic-senior-core skill fullstack --json
|
|
|
128
128
|
|
|
129
129
|
When you run `init`, the CLI now auto-activates the matching skill packs for the chosen stack and blueprint, so the compiled governance context includes the relevant frontend, backend, fullstack, and CLI guidance by default.
|
|
130
130
|
|
|
131
|
-
### Token Optimization Mode (
|
|
131
|
+
### Token Optimization Mode (Enabled by Default on Init)
|
|
132
132
|
|
|
133
133
|
Use this mode when your AI session is shell-heavy and context usage is high.
|
|
134
|
-
|
|
134
|
+
By default, every `init` flow enables token optimization automatically (npx, npm exec, global CLI, preset, and interactive wizard).
|
|
135
|
+
The optimization engine works in two modes:
|
|
135
136
|
- Native fallback mode (no external dependency required)
|
|
136
137
|
- External proxy mode (auto-detected when available)
|
|
137
138
|
|
|
@@ -144,11 +145,36 @@ agentic-senior-core optimize . --disable
|
|
|
144
145
|
|
|
145
146
|
# Auto-enable during project initialization
|
|
146
147
|
npx @ryuenn3123/agentic-senior-core init --token-optimize --token-agent copilot
|
|
148
|
+
|
|
149
|
+
# Opt out when needed
|
|
150
|
+
npx @ryuenn3123/agentic-senior-core init --no-token-optimize
|
|
147
151
|
```
|
|
148
152
|
|
|
149
153
|
When enabled, the CLI writes `.agent-context/state/token-optimization.json`, regenerates compiled rules, and adds compact command guidance to `.cursorrules` and `.windsurfrules`.
|
|
150
154
|
If an external token proxy is available, the CLI prints setup hints. If not, native fallback guidance stays active, so outside users are not forced to install extra tooling.
|
|
151
155
|
|
|
156
|
+
### Token Efficiency Benchmark Snapshot
|
|
157
|
+
|
|
158
|
+
Latest local benchmark (2026-04-11) from `.agent-context/state/token-optimization-benchmark.json`:
|
|
159
|
+
|
|
160
|
+
| Scenario | Baseline Token Estimate | Agentic Native Token Estimate | Native Savings | RTK Token Estimate | RTK Status |
|
|
161
|
+
|----------|-------------------------|-------------------------------|----------------|--------------------|------------|
|
|
162
|
+
| Latest commit detail review | 3798 | 177 | 95.34% | 3798 | Detected (`rtk` v0.35.0) |
|
|
163
|
+
| Commit history review | 3730 | 889 | 76.17% | 1610 | Detected (`rtk` v0.35.0) |
|
|
164
|
+
| Search result scan | 5339 | 1099 | 79.42% | 5339 | Detected (`rtk` v0.35.0) |
|
|
165
|
+
| Average | - | - | 83.64% | 18.95% savings | Detected (`rtk` v0.35.0) |
|
|
166
|
+
|
|
167
|
+
Method note:
|
|
168
|
+
- Estimate formula is `ceil(output_chars / 4)`.
|
|
169
|
+
- This is a command-output estimate, not provider-specific tokenizer output.
|
|
170
|
+
- RTK results depend on host utilities. On Windows hosts without GNU `grep` or `ls`, RTK may fall back to `rtk git ...` paths for some scenarios.
|
|
171
|
+
|
|
172
|
+
Reproduce and refresh this table:
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
npm run benchmark:token
|
|
176
|
+
```
|
|
177
|
+
|
|
152
178
|
### Install and Setup Choices
|
|
153
179
|
|
|
154
180
|
The CLI now supports a smaller decision surface for first-time setup:
|
|
@@ -55,7 +55,7 @@ export function parseInitArguments(commandArguments) {
|
|
|
55
55
|
blueprint: undefined,
|
|
56
56
|
ci: undefined,
|
|
57
57
|
newbie: false,
|
|
58
|
-
tokenOptimize:
|
|
58
|
+
tokenOptimize: true,
|
|
59
59
|
tokenAgent: 'copilot',
|
|
60
60
|
};
|
|
61
61
|
|
|
@@ -146,14 +146,12 @@ export function parseInitArguments(commandArguments) {
|
|
|
146
146
|
|
|
147
147
|
if (currentArgument === '--token-agent') {
|
|
148
148
|
parsedInitOptions.tokenAgent = commandArguments[argumentIndex + 1] || 'copilot';
|
|
149
|
-
parsedInitOptions.tokenOptimize = true;
|
|
150
149
|
argumentIndex += 1;
|
|
151
150
|
continue;
|
|
152
151
|
}
|
|
153
152
|
|
|
154
153
|
if (currentArgument.startsWith('--token-agent=')) {
|
|
155
154
|
parsedInitOptions.tokenAgent = currentArgument.split('=')[1] || 'copilot';
|
|
156
|
-
parsedInitOptions.tokenOptimize = true;
|
|
157
155
|
continue;
|
|
158
156
|
}
|
|
159
157
|
|
|
@@ -176,6 +174,10 @@ export function parseInitArguments(commandArguments) {
|
|
|
176
174
|
|
|
177
175
|
export async function runInitCommand(targetDirectoryArgument, initOptions = {}) {
|
|
178
176
|
const resolvedTargetDirectoryPath = path.resolve(targetDirectoryArgument || '.');
|
|
177
|
+
const isTokenOptimizationEnabled = typeof initOptions.tokenOptimize === 'boolean'
|
|
178
|
+
? initOptions.tokenOptimize
|
|
179
|
+
: true;
|
|
180
|
+
const selectedTokenAgentName = normalizeAgentName(initOptions.tokenAgent || 'copilot');
|
|
179
181
|
|
|
180
182
|
if (resolvedTargetDirectoryPath.toLowerCase() === 'c:\\windows' || resolvedTargetDirectoryPath.toLowerCase() === 'c:\\windows\\system32') {
|
|
181
183
|
console.error('\n[FATAL] Target directory resolved to a Windows system folder (C:\\Windows).');
|
|
@@ -348,11 +350,11 @@ export async function runInitCommand(targetDirectoryArgument, initOptions = {})
|
|
|
348
350
|
|
|
349
351
|
await copyGovernanceAssetsToTarget(resolvedTargetDirectoryPath);
|
|
350
352
|
|
|
351
|
-
if (
|
|
353
|
+
if (isTokenOptimizationEnabled) {
|
|
352
354
|
const detectedExternalProxy = detectRtkBinary();
|
|
353
355
|
const tokenOptimizationState = createTokenOptimizationState({
|
|
354
356
|
isEnabled: true,
|
|
355
|
-
selectedAgentName:
|
|
357
|
+
selectedAgentName: selectedTokenAgentName,
|
|
356
358
|
rtkDetection: detectedExternalProxy,
|
|
357
359
|
});
|
|
358
360
|
|
|
@@ -417,8 +419,10 @@ export async function runInitCommand(targetDirectoryArgument, initOptions = {})
|
|
|
417
419
|
console.log(`- Blocking severities: ${formatBlockingSeverities(selectedProfile.blockingSeverities)}`);
|
|
418
420
|
console.log(`- Setup time: ${formatDuration(setupDurationMs)}`);
|
|
419
421
|
console.log('- Generated files: .cursorrules, .windsurfrules, and .agent-context/state/onboarding-report.json');
|
|
420
|
-
if (
|
|
421
|
-
console.log(`- Token optimization policy: enabled for ${
|
|
422
|
+
if (isTokenOptimizationEnabled) {
|
|
423
|
+
console.log(`- Token optimization policy: enabled for ${selectedTokenAgentName}`);
|
|
424
|
+
} else {
|
|
425
|
+
console.log('- Token optimization policy: disabled (--no-token-optimize)');
|
|
422
426
|
}
|
|
423
427
|
console.log('\nPlain-language summary:');
|
|
424
428
|
console.log(`I prepared a ${selectedProfile.displayName.toLowerCase()} governance pack for a ${toTitleCase(selectedResolvedStackFileName)} project using the ${toTitleCase(selectedResolvedBlueprintFileName)} blueprint.`);
|
package/lib/cli/utils.mjs
CHANGED
|
@@ -25,7 +25,7 @@ export function printUsage() {
|
|
|
25
25
|
console.log('');
|
|
26
26
|
console.log('Usage:');
|
|
27
27
|
console.log(' agentic-senior-core launch');
|
|
28
|
-
console.log(' agentic-senior-core init [target-directory] [--preset <name>] [--profile <beginner|balanced|strict>] [--profile-pack <name>] [--stack <name>] [--blueprint <name>] [--ci <true|false>] [--newbie] [--token-optimize] [--token-agent <name>]');
|
|
28
|
+
console.log(' agentic-senior-core init [target-directory] [--preset <name>] [--profile <beginner|balanced|strict>] [--profile-pack <name>] [--stack <name>] [--blueprint <name>] [--ci <true|false>] [--newbie] [--token-optimize] [--no-token-optimize] [--token-agent <name>]');
|
|
29
29
|
console.log(' agentic-senior-core upgrade [target-directory] [--dry-run] [--yes]');
|
|
30
30
|
console.log(' agentic-senior-core optimize [target-directory] [--agent <copilot|claude|cursor|windsurf|gemini|codex|cline>] [--enable|--disable] [--show]');
|
|
31
31
|
console.log(' agentic-senior-core rollback [target-directory]');
|
|
@@ -42,9 +42,9 @@ export function printUsage() {
|
|
|
42
42
|
console.log(' --stack Override stack selection');
|
|
43
43
|
console.log(' --blueprint Override blueprint selection');
|
|
44
44
|
console.log(' --ci Override CI/CD guardrails (true|false)');
|
|
45
|
-
console.log(' --token-optimize
|
|
45
|
+
console.log(' --token-optimize Explicitly enable token optimization policy during init (default behavior)');
|
|
46
46
|
console.log(' --token-agent Set token optimization agent target (copilot, claude, cursor, windsurf, gemini, codex, cline)');
|
|
47
|
-
console.log(' --no-token-optimize Disable token optimization
|
|
47
|
+
console.log(' --no-token-optimize Disable token optimization policy during init');
|
|
48
48
|
console.log(' --dry-run Preview upgrade without writing files');
|
|
49
49
|
console.log(' --yes Skip confirmation prompts for upgrade');
|
|
50
50
|
console.log(' --agent Target agent integration for token optimization mode');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ryuenn3123/agentic-senior-core",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Force your AI Agent to code like a Staff Engineer, not a Junior.",
|
|
6
6
|
"bin": {
|
|
@@ -47,6 +47,7 @@
|
|
|
47
47
|
"prepublishOnly": "npm run gate:release",
|
|
48
48
|
"sbom:generate": "node ./scripts/generate-sbom.mjs",
|
|
49
49
|
"benchmark:detection": "node ./scripts/detection-benchmark.mjs",
|
|
50
|
+
"benchmark:token": "node ./scripts/token-optimization-benchmark.mjs",
|
|
50
51
|
"benchmark:gate": "node ./scripts/benchmark-gate.mjs",
|
|
51
52
|
"benchmark:intelligence": "node ./scripts/benchmark-intelligence.mjs",
|
|
52
53
|
"validate": "node ./scripts/validate.mjs",
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* token-optimization-benchmark.mjs
|
|
5
|
+
*
|
|
6
|
+
* Measures token-estimate reduction between baseline commands and
|
|
7
|
+
* native optimized command variants. If RTK is installed, it also
|
|
8
|
+
* compares RTK command output in the same scenarios.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { existsSync } from 'node:fs';
|
|
12
|
+
import fs from 'node:fs/promises';
|
|
13
|
+
import { spawnSync } from 'node:child_process';
|
|
14
|
+
import { dirname, join, resolve } from 'node:path';
|
|
15
|
+
import { fileURLToPath } from 'node:url';
|
|
16
|
+
|
|
17
|
+
const SCRIPT_FILE_PATH = fileURLToPath(import.meta.url);
|
|
18
|
+
const SCRIPT_DIR = dirname(SCRIPT_FILE_PATH);
|
|
19
|
+
const REPOSITORY_ROOT = resolve(SCRIPT_DIR, '..');
|
|
20
|
+
const REPORT_PATH = join(REPOSITORY_ROOT, '.agent-context', 'state', 'token-optimization-benchmark.json');
|
|
21
|
+
const LOCAL_RTK_BINARY_PATH = join(REPOSITORY_ROOT, '.benchmarks', 'tools', 'rtk', 'rtk.exe');
|
|
22
|
+
const TOKEN_ESTIMATE_DIVISOR = 4;
|
|
23
|
+
const ARGUMENT_FLAGS = new Set(process.argv.slice(2));
|
|
24
|
+
const isStdoutOnlyMode = ARGUMENT_FLAGS.has('--stdout-only');
|
|
25
|
+
|
|
26
|
+
const BENCHMARK_SCENARIOS = [
|
|
27
|
+
{
|
|
28
|
+
scenarioId: 'git-show',
|
|
29
|
+
scenarioName: 'Latest commit detail review',
|
|
30
|
+
baselineCommand: { command: 'git', args: ['show', 'HEAD', '--stat', '--patch', '--no-color'] },
|
|
31
|
+
nativeOptimizedCommand: { command: 'git', args: ['show', 'HEAD', '--stat', '--no-color'] },
|
|
32
|
+
rtkCommand: { command: 'rtk', args: ['git', 'show', 'HEAD', '--stat', '--patch', '--no-color'] },
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
scenarioId: 'git-log',
|
|
36
|
+
scenarioName: 'Commit history review',
|
|
37
|
+
baselineCommand: { command: 'git', args: ['log', '-n', '50'] },
|
|
38
|
+
nativeOptimizedCommand: { command: 'git', args: ['log', '--oneline', '-n', '50'] },
|
|
39
|
+
rtkCommand: { command: 'rtk', args: ['git', 'log', '-n', '50'] },
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
scenarioId: 'search-token',
|
|
43
|
+
scenarioName: 'Search result scan',
|
|
44
|
+
baselineCommand: { command: 'git', args: ['grep', '-n', 'token'] },
|
|
45
|
+
nativeOptimizedCommand: { command: 'git', args: ['grep', '-n', 'token', 'README.md', 'docs'] },
|
|
46
|
+
rtkCommand: { command: 'rtk', args: ['git', 'grep', '-n', 'token'] },
|
|
47
|
+
},
|
|
48
|
+
];
|
|
49
|
+
|
|
50
|
+
function estimateTokenCount(outputText) {
|
|
51
|
+
if (!outputText || outputText.length === 0) {
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return Math.ceil(outputText.length / TOKEN_ESTIMATE_DIVISOR);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function formatCommandLine(commandDefinition) {
|
|
59
|
+
return [commandDefinition.command, ...commandDefinition.args].join(' ');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function executeCommand(commandDefinition) {
|
|
63
|
+
const commandResult = spawnSync(commandDefinition.command, commandDefinition.args, {
|
|
64
|
+
cwd: REPOSITORY_ROOT,
|
|
65
|
+
encoding: 'utf8',
|
|
66
|
+
maxBuffer: 1024 * 1024 * 20,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
const standardOutput = commandResult.stdout || '';
|
|
70
|
+
const standardError = commandResult.stderr || '';
|
|
71
|
+
const combinedOutput = `${standardOutput}${standardError}`;
|
|
72
|
+
const normalizedOutput = combinedOutput.trimEnd();
|
|
73
|
+
|
|
74
|
+
const outputLineCount = normalizedOutput.length === 0
|
|
75
|
+
? 0
|
|
76
|
+
: normalizedOutput.split(/\r?\n/u).length;
|
|
77
|
+
|
|
78
|
+
return {
|
|
79
|
+
command: formatCommandLine(commandDefinition),
|
|
80
|
+
exitCode: typeof commandResult.status === 'number' ? commandResult.status : 1,
|
|
81
|
+
outputChars: normalizedOutput.length,
|
|
82
|
+
outputLines: outputLineCount,
|
|
83
|
+
estimatedTokens: estimateTokenCount(normalizedOutput),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function resolveRtkBinaryPath() {
|
|
88
|
+
const configuredRtkBinaryPath = process.env.RTK_BINARY_PATH;
|
|
89
|
+
|
|
90
|
+
if (configuredRtkBinaryPath && existsSync(configuredRtkBinaryPath)) {
|
|
91
|
+
return {
|
|
92
|
+
executablePath: configuredRtkBinaryPath,
|
|
93
|
+
source: 'env',
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (existsSync(LOCAL_RTK_BINARY_PATH)) {
|
|
98
|
+
return {
|
|
99
|
+
executablePath: LOCAL_RTK_BINARY_PATH,
|
|
100
|
+
source: 'local-tools',
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return {
|
|
105
|
+
executablePath: 'rtk',
|
|
106
|
+
source: 'system-path',
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function detectRtkAvailability() {
|
|
111
|
+
const rtkBinary = resolveRtkBinaryPath();
|
|
112
|
+
|
|
113
|
+
const versionResult = spawnSync(rtkBinary.executablePath, ['--version'], {
|
|
114
|
+
cwd: REPOSITORY_ROOT,
|
|
115
|
+
encoding: 'utf8',
|
|
116
|
+
maxBuffer: 1024 * 1024,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
if (versionResult.error || versionResult.status !== 0) {
|
|
120
|
+
return {
|
|
121
|
+
isAvailable: false,
|
|
122
|
+
version: null,
|
|
123
|
+
source: rtkBinary.source,
|
|
124
|
+
executablePath: rtkBinary.executablePath,
|
|
125
|
+
reason: versionResult.error
|
|
126
|
+
? versionResult.error.message
|
|
127
|
+
: (versionResult.stderr || versionResult.stdout || 'RTK command unavailable').trim(),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const versionMatch = (versionResult.stdout || '').match(/\d+\.\d+\.\d+/u);
|
|
132
|
+
return {
|
|
133
|
+
isAvailable: true,
|
|
134
|
+
version: versionMatch ? versionMatch[0] : null,
|
|
135
|
+
source: rtkBinary.source,
|
|
136
|
+
executablePath: rtkBinary.executablePath,
|
|
137
|
+
reason: null,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function buildRtkCommand(commandDefinition, rtkAvailability) {
|
|
142
|
+
return {
|
|
143
|
+
command: rtkAvailability.executablePath || commandDefinition.command,
|
|
144
|
+
args: commandDefinition.args,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function computeSavingsSummary(baselineTokens, optimizedTokens) {
|
|
149
|
+
if (baselineTokens <= 0) {
|
|
150
|
+
return {
|
|
151
|
+
tokenDelta: 0,
|
|
152
|
+
savingsPercent: 0,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const tokenDelta = baselineTokens - optimizedTokens;
|
|
157
|
+
const savingsPercent = Number(((tokenDelta / baselineTokens) * 100).toFixed(2));
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
tokenDelta,
|
|
161
|
+
savingsPercent,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function calculateAverageSavings(benchmarkRows, savingsKeyName) {
|
|
166
|
+
const availableSavings = benchmarkRows
|
|
167
|
+
.map((benchmarkRow) => benchmarkRow[savingsKeyName]?.savingsPercent)
|
|
168
|
+
.filter((savingsPercent) => typeof savingsPercent === 'number');
|
|
169
|
+
|
|
170
|
+
if (availableSavings.length === 0) {
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const totalSavings = availableSavings.reduce((runningTotal, savingsPercent) => runningTotal + savingsPercent, 0);
|
|
175
|
+
return Number((totalSavings / availableSavings.length).toFixed(2));
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async function runTokenOptimizationBenchmark() {
|
|
179
|
+
const rtkAvailability = detectRtkAvailability();
|
|
180
|
+
const scenarioResults = [];
|
|
181
|
+
|
|
182
|
+
for (const benchmarkScenario of BENCHMARK_SCENARIOS) {
|
|
183
|
+
const baselineRun = executeCommand(benchmarkScenario.baselineCommand);
|
|
184
|
+
const nativeOptimizedRun = executeCommand(benchmarkScenario.nativeOptimizedCommand);
|
|
185
|
+
const nativeSavings = computeSavingsSummary(
|
|
186
|
+
baselineRun.estimatedTokens,
|
|
187
|
+
nativeOptimizedRun.estimatedTokens
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
let rtkRun = null;
|
|
191
|
+
let rtkSavings = null;
|
|
192
|
+
|
|
193
|
+
if (rtkAvailability.isAvailable) {
|
|
194
|
+
rtkRun = executeCommand(buildRtkCommand(benchmarkScenario.rtkCommand, rtkAvailability));
|
|
195
|
+
if (rtkRun.exitCode === 0) {
|
|
196
|
+
rtkSavings = computeSavingsSummary(baselineRun.estimatedTokens, rtkRun.estimatedTokens);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
scenarioResults.push({
|
|
201
|
+
scenarioId: benchmarkScenario.scenarioId,
|
|
202
|
+
scenarioName: benchmarkScenario.scenarioName,
|
|
203
|
+
baseline: baselineRun,
|
|
204
|
+
nativeOptimized: nativeOptimizedRun,
|
|
205
|
+
nativeSavings,
|
|
206
|
+
rtk: rtkRun,
|
|
207
|
+
rtkSavings,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const benchmarkReport = {
|
|
212
|
+
generatedAt: new Date().toISOString(),
|
|
213
|
+
reportName: 'token-optimization-benchmark',
|
|
214
|
+
methodology: {
|
|
215
|
+
tokenEstimate: `estimated_tokens = ceil(output_chars / ${TOKEN_ESTIMATE_DIVISOR})`,
|
|
216
|
+
scope: 'command-output estimate only; model-specific tokenization differs by provider',
|
|
217
|
+
repositoryRoot: REPOSITORY_ROOT,
|
|
218
|
+
},
|
|
219
|
+
environment: {
|
|
220
|
+
nodeVersion: process.version,
|
|
221
|
+
platform: process.platform,
|
|
222
|
+
rtk: rtkAvailability,
|
|
223
|
+
},
|
|
224
|
+
scenarios: scenarioResults,
|
|
225
|
+
summary: {
|
|
226
|
+
scenarioCount: scenarioResults.length,
|
|
227
|
+
averageNativeSavingsPercent: calculateAverageSavings(scenarioResults, 'nativeSavings'),
|
|
228
|
+
averageRtkSavingsPercent: calculateAverageSavings(scenarioResults, 'rtkSavings'),
|
|
229
|
+
},
|
|
230
|
+
artifact: {
|
|
231
|
+
path: REPORT_PATH,
|
|
232
|
+
writeMode: isStdoutOnlyMode ? 'stdout-only' : 'stdout-and-file',
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
if (!isStdoutOnlyMode) {
|
|
237
|
+
await fs.mkdir(dirname(REPORT_PATH), { recursive: true });
|
|
238
|
+
await fs.writeFile(REPORT_PATH, JSON.stringify(benchmarkReport, null, 2) + '\n', 'utf8');
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
return benchmarkReport;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
runTokenOptimizationBenchmark()
|
|
245
|
+
.then((benchmarkReport) => {
|
|
246
|
+
console.log(JSON.stringify(benchmarkReport, null, 2));
|
|
247
|
+
})
|
|
248
|
+
.catch((benchmarkError) => {
|
|
249
|
+
const errorMessage = benchmarkError instanceof Error ? benchmarkError.message : String(benchmarkError);
|
|
250
|
+
console.error(`Token benchmark failed: ${errorMessage}`);
|
|
251
|
+
process.exit(1);
|
|
252
|
+
});
|