Files
OmniRoute/tests/unit/compression-noop-guard.test.ts
Chirag Singhal 5cebefe64a fix(sse): treat compression no-op as zero-savings, not inflation/silent-drop (#6883)
A structural engine (ccr / session-dedup) that finds nothing to compress
returns the body unchanged. That no-op was mishandled three ways — the
code-level root cause of the #6465–#6493 "0% savings, no reason" symptom class:

A. Inflation guard mislabelled a no-op as inflation. guardPipelineInflation
   used `compressedTokens >= originalTokens`, so an unchanged body
   (compressedTokens === originalTokens) tripped the guard, setting
   fallbackApplied=true and emitting a misleading "did not shrink; reverted
   to original" warning. Changed to strict `>` — only a strictly larger
   output is inflation; equality is a no-op. Genuine inflation still reverts.

B. Disabled-engine skip was silent and asymmetric with the breaker skip. Both
   stacked loops (sync + async) skipped a registry-disabled engine with a bare
   `continue`, recording no validationWarning — while the sibling breaker-open
   branch does. Both loops now add
   `${engine}: skipped (engine disabled in registry)`, mirroring the breaker branch.

C. No-op engine lost its identity in engineBreakdown. mergeStackStep
   early-returned on null stats, pushing no breakdown entry, so
   ensureEngineBreakdown synthesized a generic "stacked" 0% node. It now records
   a zero-savings entry keyed on the engine that actually ran, preserving identity.

Tests: tests/unit/compression-noop-guard.test.ts covers A (equal-token no-op not
inflated; strictly-larger still reverts), B (disabled skip surfaces a "disabled"
warning), and C (no-op engine keeps its own id in the breakdown). Updated the
existing inflation-guard test whose net-zero case encoded the old buggy behaviour,
and switched its wire test to object-form pipeline steps so the intended engine runs.

Co-authored-by: Chirag Singhal <chirag127@users.noreply.github.com>
Co-authored-by: Diego Rodrigues de Sa e Souza <diegosouza.pw@gmail.com>
Co-authored-by: Diego Rodrigues de Sa e Souza <8016841+diegosouzapw@users.noreply.github.com>
2026-07-12 02:04:25 -03:00

118 lines
4.2 KiB
TypeScript

import test from "node:test";
import assert from "node:assert/strict";
import { guardPipelineInflation } from "../../open-sse/services/compression/pipelineGuards.ts";
import { applyStackedCompression } from "../../open-sse/services/compression/strategySelector.ts";
import {
registerCompressionEngine,
setEngineEnabled,
} from "../../open-sse/services/compression/engines/registry.ts";
import type { CompressionEngine } from "../../open-sse/services/compression/engines/types.ts";
import type { CompressionPipelineStep } from "../../open-sse/services/compression/types.ts";
// Regression suite for the three compression NO-OP defects: a structural engine (ccr /
// session-dedup) that finds nothing to compress must be treated as ZERO SAVINGS, never as an
// inflation revert (A), never a silent skip (B), and never lose its identity in the breakdown (C).
// A test-only engine that mimics a structural no-op: body unchanged, compressed:false, stats:null.
function makeNoopEngine(id: string): CompressionEngine {
return {
id,
name: "Test Noop",
description: "test-only engine that finds nothing to compress",
icon: "bug_report",
targets: ["messages"],
stackable: true,
stackPriority: 0,
metadata: {
id,
name: "Test Noop",
description: "test-only",
inputScope: "messages",
targetLatencyMs: 0,
supportsPreview: false,
stable: true,
},
apply(body) {
return { body, compressed: false, stats: null };
},
compress(body) {
return this.apply(body);
},
getConfigSchema() {
return [];
},
validateConfig() {
return { valid: true, errors: [] };
},
};
}
// --- Defect A: a net-zero (equal-token) no-op is NOT inflation ---
test("guardPipelineInflation: equal tokens (no-op) is NOT flagged as inflation", () => {
const original = { a: 1 };
const compressed = { a: 1 };
const r = guardPipelineInflation({
originalBody: original,
compressedBody: compressed,
originalTokens: 100,
compressedTokens: 100,
});
assert.equal(r.inflated, false);
assert.equal(r.body, compressed);
});
test("guardPipelineInflation: strictly larger output still reverts as inflation", () => {
const original = { a: 1 };
const compressed = { a: 1, pad: "xxxx" };
const r = guardPipelineInflation({
originalBody: original,
compressedBody: compressed,
originalTokens: 100,
compressedTokens: 101,
});
assert.equal(r.inflated, true);
assert.equal(r.body, original);
});
// --- Defect B: a disabled engine skip records a validationWarning (symmetric with breaker skip) ---
const DISABLED_ID = "test-noop-disabled-guard";
test("applyStackedCompression: a disabled engine skip surfaces a 'disabled' validationWarning", () => {
registerCompressionEngine(makeNoopEngine(DISABLED_ID));
setEngineEnabled(DISABLED_ID, false);
const body = { messages: [{ role: "user", content: "hello world" }] };
const result = applyStackedCompression(body, [{ engine: DISABLED_ID } as CompressionPipelineStep]);
const warnings = result.stats?.validationWarnings ?? [];
assert.ok(
warnings.some((w) => w.includes("disabled")),
`expected a 'disabled' validationWarning, got: ${JSON.stringify(warnings)}`
);
assert.ok(
warnings.some((w) => w.includes(DISABLED_ID)),
"the warning should name the disabled engine"
);
});
// --- Defect C: a no-op engine keeps its identity in engineBreakdown (not a generic "stacked") ---
const NOOP_ID = "test-noop-breakdown-guard";
test("applyStackedCompression: a no-op engine records its own identity in engineBreakdown", () => {
registerCompressionEngine(makeNoopEngine(NOOP_ID));
setEngineEnabled(NOOP_ID, true);
const body = { messages: [{ role: "user", content: "hello world this is a message" }] };
const result = applyStackedCompression(body, [{ engine: NOOP_ID } as CompressionPipelineStep]);
const breakdown = result.stats?.engineBreakdown ?? [];
const entry = breakdown.find((e) => e.engine === NOOP_ID);
assert.ok(entry, `expected a breakdown entry keyed on "${NOOP_ID}", got: ${JSON.stringify(breakdown)}`);
assert.equal(entry?.savingsPercent, 0);
// The requested engine's identity must be preserved — never collapsed into a generic "stacked".
assert.notEqual(entry?.engine, "stacked");
});