fix(integration): token savings need the baseline the caller has (#3317)

`getCompactContext` reported `tokensSaved` as the query string's length
minus the retrieved prompt's. The query is a search instruction, not the
context the compact prompt replaces, so the subtraction had no valid
baseline: a short query against a longer retrieved prompt reported 0 no
matter how much context the caller then dropped, and a long query against
a short prompt reported a saving with no replaced context behind it.
`getStats` turned the accumulated value into `estimatedMonthlySavings`,
in a module whose header states that no fabricated metrics are reported.

The baseline is the caller's to supply, because this method never sees
it. `options.baselineTokens` names the size of the context being
replaced; savings are measured against that and floored at 0. Without
it the call is reported as unmeasured -- `tokensSaved: null`, nothing
added to the total -- rather than as a saving of 0, so a caller summing
these cannot fold "no saving" and "no measurement" into one number.
`contextsMeasured` says how many calls the total was actually built
from, and the report renders it next to the total.

Co-authored-by: rUv <ruv@ruv.net>
This commit is contained in:
Lukas BuckandrUv authored and GitHub committed 2026-09-16 02:21:17 +00:00
1 parent 955510695f
commit be9b4d28ec
2 files changed
+157 -8

No files matched your search

@@ -60,6 +60,116 @@ describe('TokenOptimizer', () => {
});
});
describe('getCompactContext savings baseline (#3289)', () => {
// The compact prompt is what is RETRIEVED; the baseline is what it
// REPLACED. Those are different numbers and only the caller has the
// second one, which is the whole reason the old comparison -- against the
// query string -- could not be a measurement.
//
// The retrieval is stubbed rather than left to whatever ReasoningBank this
// machine has: with agentic-flow absent the real bank returns an empty
// prompt, and every assertion below about `baseline - compactTokens`
// would hold for the trivial reason that compactTokens is 0.
const PROMPT_CHARS = 400; // -> 100 tokens at the module's 4-chars heuristic
const COMPACT_TOKENS = PROMPT_CHARS / 4;
async function optimizerWithRetrieval(): Promise<TokenOptimizer> {
const fresh = new TokenOptimizer();
await fresh.initialize();
(fresh as unknown as { reasoningBank: unknown }).reasoningBank = {
retrieveMemories: async () => [{ content: 'memory', score: 0.9 }],
formatMemoriesForPrompt: () => 'x'.repeat(PROMPT_CHARS),
};
return fresh;
}
it('reports unmeasured rather than zero when no baseline is given', async () => {
const fresh = await optimizerWithRetrieval();
const ctx = await fresh.getCompactContext('authentication patterns');
expect(ctx.tokensSaved).toBeNull();
expect(fresh.getStats().contextsMeasured).toBe(0);
});
it('does not move the running total on an unmeasured call', async () => {
const fresh = await optimizerWithRetrieval();
const before = fresh.getStats().totalTokensSaved;
// A long query is the shape that used to manufacture a saving: it beat
// the retrieved prompt on length with no replaced context behind it.
await fresh.getCompactContext('authentication patterns '.repeat(200));
expect(fresh.getStats().totalTokensSaved).toBe(before);
});
it('measures against the baseline the caller supplies', async () => {
const fresh = await optimizerWithRetrieval();
const ctx = await fresh.getCompactContext('authentication patterns', {
baselineTokens: 8000,
});
expect(ctx.tokensSaved).toBe(8000 - COMPACT_TOKENS);
expect(fresh.getStats().totalTokensSaved).toBe(8000 - COMPACT_TOKENS);
expect(fresh.getStats().contextsMeasured).toBe(1);
});
it('floors at zero when the compact prompt outgrows the baseline', async () => {
const fresh = await optimizerWithRetrieval();
const ctx = await fresh.getCompactContext('authentication patterns', {
baselineTokens: 10,
});
// 10 replaced tokens against a 100-token prompt is not a saving of -90.
// It is also not a missing measurement: 10 is a baseline, so the call
// counts as measured.
expect(ctx.tokensSaved).toBe(0);
expect(fresh.getStats().contextsMeasured).toBe(1);
});
it('rejects a baseline that is not a usable count', async () => {
const fresh = await optimizerWithRetrieval();
for (const baselineTokens of [NaN, Infinity, -1]) {
const ctx = await fresh.getCompactContext('authentication patterns', {
baselineTokens,
});
expect(ctx.tokensSaved).toBeNull();
}
expect(fresh.getStats().contextsMeasured).toBe(0);
});
it('the query length is no longer part of the answer', async () => {
// Same baseline, same retrieval, queries of wildly different lengths:
// the reported saving must not move. This is exactly the property the
// old implementation violated.
const short = await optimizerWithRetrieval();
const long = await optimizerWithRetrieval();
const a = await short.getCompactContext('auth', { baselineTokens: 5000 });
const b = await long.getCompactContext('auth '.repeat(500), {
baselineTokens: 5000,
});
expect(a.tokensSaved).toBe(b.tokensSaved);
});
it('a run with no ReasoningBank at all is unmeasured, not a zero saving', async () => {
const fresh = new TokenOptimizer();
await fresh.initialize();
(fresh as unknown as { reasoningBank: unknown }).reasoningBank = null;
const ctx = await fresh.getCompactContext('authentication patterns', {
baselineTokens: 8000,
});
expect(ctx.tokensSaved).toBeNull();
expect(fresh.getStats().contextsMeasured).toBe(0);
});
});
describe('optimizedEdit', () => {
it('should return edit optimization result', async () => {
const result = await optimizer.optimizedEdit(
@@ -26,7 +26,16 @@ interface MemoryContext {
query: string;
memories: Array<{ content: string; score: number }>;
compactPrompt: string;
tokensSaved: number;
/**
* Tokens this compact prompt saved against the context it replaced, or
* `null` when the caller supplied no baseline to measure against.
*
* `null` rather than `0`: a zero is a measurement that came out empty,
* and callers that sum these would fold "no saving" and "no measurement"
* into the same number -- which is how this value came to be reported as
* a saving in the first place.
*/
tokensSaved: number | null;
}
interface EditOptimization {
@@ -51,6 +60,12 @@ async function safeImport<T>(modulePath: string): Promise<T | null> {
export class TokenOptimizer extends EventEmitter {
private stats = {
totalTokensSaved: 0,
/**
* How many `getCompactContext` calls carried a baseline. `totalTokensSaved`
* is the sum over exactly these, so a reader can tell an honest zero from
* a total that nothing was ever measured into.
*/
contextsMeasured: 0,
editsOptimized: 0,
cacheHits: 0,
cacheMisses: 0,
@@ -104,11 +119,27 @@ export class TokenOptimizer extends EventEmitter {
/**
* Retrieve compact context instead of full file content.
* Token savings depend on query length vs retrieved context size.
*
* Savings are measured against `options.baselineTokens`: the size of the
* context this compact prompt is replacing. That number is the caller's to
* supply, because this method never sees it -- it receives a search
* instruction, not the context being replaced.
*
* Without it the result is reported as unmeasured (`tokensSaved: null`) and
* nothing is added to the running total. The previous baseline was the
* QUERY's own length, which measured nothing: a short query against a longer
* retrieved prompt reported 0 no matter how much context the caller then
* dropped, and a long query against a short prompt reported a saving with no
* replaced context behind it (#3289).
*/
async getCompactContext(query: string, options?: {
limit?: number;
threshold?: number;
/**
* Token count of the context the compact prompt replaces. Omit it when
* nothing was replaced or the size is unknown.
*/
baselineTokens?: number;
}): Promise<MemoryContext> {
const limit = options?.limit ?? 5;
const threshold = options?.threshold ?? 0.7;
@@ -119,7 +150,7 @@ export class TokenOptimizer extends EventEmitter {
query,
memories: [],
compactPrompt: '',
tokensSaved: 0,
tokensSaved: null,
};
}
@@ -136,13 +167,20 @@ export class TokenOptimizer extends EventEmitter {
compactPrompt = '';
}
// Estimate tokens saved based on actual content length difference
// Rough heuristic: ~4 chars per token, compare full query context vs compact
const queryTokenEstimate = Math.ceil(query.length / 4);
// ~4 chars per token, the same rough heuristic as before -- what changes
// is what it is compared against.
const baseline = options?.baselineTokens;
const measurable =
typeof baseline === 'number' && Number.isFinite(baseline) && baseline >= 0;
const compactTokenEstimate = Math.ceil(compactPrompt.length / 4);
const saved = Math.max(0, queryTokenEstimate - compactTokenEstimate);
const saved = measurable
? Math.max(0, Math.floor(baseline) - compactTokenEstimate)
: null;
this.stats.totalTokensSaved += saved;
if (saved !== null) {
this.stats.totalTokensSaved += saved;
this.stats.contextsMeasured += 1;
}
this.stats.memoriesRetrieved += memories.length;
return {
@@ -293,6 +331,7 @@ export class TokenOptimizer extends EventEmitter {
| Metric | Value |
|--------|-------|
| Tokens Saved | ${stats.totalTokensSaved.toLocaleString()} |
| Contexts Measured | ${stats.contextsMeasured} |
| Edits Optimized | ${stats.editsOptimized} |
| Cache Hit Rate | ${stats.cacheHitRate} |
| Memories Retrieved | ${stats.memoriesRetrieved} |