Skip to content

Commit 34e7c23

Browse files
author
ContextForge
committed
fix(report): use source-token baseline for reduction metrics
- compute reduction against estimated input/source tokens instead of raw signature tokens - keep JSON compatibility via rawTokens while adding inputTokens field - update human-readable report label to 'input tokens'
1 parent 6858776 commit 34e7c23

1 file changed

Lines changed: 12 additions & 9 deletions

File tree

gen-context.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3311,14 +3311,15 @@ function writeClaude(content, cwd) {
33113311
// ---------------------------------------------------------------------------
33123312
// Report
33133313
// ---------------------------------------------------------------------------
3314-
function printReport(rawTokens, finalTokens, fileCount, droppedCount, asJson, budgetLimit) {
3315-
const reduction = rawTokens > 0 ? (100 - (finalTokens / rawTokens) * 100).toFixed(1) : 0;
3314+
function printReport(inputTokens, finalTokens, fileCount, droppedCount, asJson, budgetLimit) {
3315+
const reduction = inputTokens > 0 ? (100 - (finalTokens / inputTokens) * 100).toFixed(1) : 0;
33163316
const overBudget = finalTokens > (budgetLimit || 6000);
33173317
if (asJson) {
33183318
process.stdout.write(JSON.stringify({
33193319
version: VERSION,
33203320
timestamp: new Date().toISOString(),
3321-
rawTokens,
3321+
rawTokens: inputTokens,
3322+
inputTokens,
33223323
finalTokens,
33233324
fileCount,
33243325
droppedCount,
@@ -3333,7 +3334,7 @@ function printReport(rawTokens, finalTokens, fileCount, droppedCount, asJson, bu
33333334
console.log(` version : ${VERSION}`);
33343335
console.log(` files processed : ${fileCount}`);
33353336
console.log(` files dropped : ${droppedCount}`);
3336-
console.log(` raw tokens : ~${rawTokens}`);
3337+
console.log(` input tokens : ~${inputTokens}`);
33373338
console.log(` output tokens : ~${finalTokens}`);
33383339
console.log(` budget limit : ${budgetLimit || 6000}`);
33393340
console.log(` reduction : ${reduction}%`);
@@ -3432,7 +3433,7 @@ function runGenerate(cwd, config, reportMode, reportJson = false) {
34323433
// Gather mtime and git-committed info
34333434
const recentFiles = config.diffPriority ? getRecentlyCommittedFiles(cwd) : new Set();
34343435

3435-
let rawTokenTotal = 0;
3436+
let inputTokenTotal = 0;
34363437
let fileEntries = [];
34373438

34383439
for (const filePath of allFiles) {
@@ -3446,6 +3447,9 @@ function runGenerate(cwd, config, reportMode, reportJson = false) {
34463447
let sigs = detectAndExtract(filePath, content, config.maxSigsPerFile);
34473448
if (sigs.length === 0) continue;
34483449

3450+
// Baseline = estimated tokens of original source content for intuitive reduction stats.
3451+
inputTokenTotal += estimateTokens(content);
3452+
34493453
if (config.secretScan) {
34503454
const { scan } = __require('./src/security/scanner');
34513455
const result = scan(sigs, filePath);
@@ -3455,7 +3459,6 @@ function runGenerate(cwd, config, reportMode, reportJson = false) {
34553459
sigs = result.safe;
34563460
}
34573461

3458-
rawTokenTotal += estimateTokens(sigs.join('\n'));
34593462
let mtime = 0;
34603463
try {
34613464
mtime = fs.statSync(filePath).mtimeMs;
@@ -3495,7 +3498,7 @@ function runGenerate(cwd, config, reportMode, reportJson = false) {
34953498
}
34963499

34973500
if (reportMode || process.argv.includes('--report')) {
3498-
printReport(rawTokenTotal, finalTokens, beforeCount, droppedCount, reportJson, config.maxTokens);
3501+
printReport(inputTokenTotal, finalTokens, beforeCount, droppedCount, reportJson, config.maxTokens);
34993502
}
35003503

35013504
// Usage tracking (v0.9) — optional append-only NDJSON log
@@ -3507,7 +3510,7 @@ function runGenerate(cwd, config, reportMode, reportJson = false) {
35073510
version: VERSION,
35083511
fileCount: beforeCount,
35093512
droppedCount,
3510-
rawTokens: rawTokenTotal,
3513+
rawTokens: inputTokenTotal,
35113514
finalTokens,
35123515
overBudget: finalTokens > config.maxTokens,
35133516
budgetLimit: config.maxTokens,
@@ -3517,7 +3520,7 @@ function runGenerate(cwd, config, reportMode, reportJson = false) {
35173520
}
35183521
}
35193522

3520-
return { rawTokenTotal, finalTokens, fileCount: beforeCount, droppedCount };
3523+
return { inputTokenTotal, finalTokens, fileCount: beforeCount, droppedCount };
35213524
}
35223525

35233526
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)