Files
OmniRoute/tests/unit/compression-pipeline-inflation-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

134 lines
4.4 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";
// --- Pure guard logic ---
test("guardPipelineInflation reverts to the original when the stacked output did not shrink", () => {
const original = { a: 1 };
const compressed = { a: 1, pad: "xxxxxxxx" };
const r = guardPipelineInflation({
originalBody: original,
compressedBody: compressed,
originalTokens: 100,
compressedTokens: 120,
});
assert.equal(r.inflated, true);
assert.equal(r.body, original);
});
test("guardPipelineInflation treats a net-zero (equal-token) no-op as NOT inflated", () => {
// A structural engine (ccr / session-dedup) that finds nothing returns the body unchanged, so
// compressedTokens === originalTokens. That is a no-op (zero savings), not inflation — the guard
// must keep the compressed body and never emit the "did not shrink; reverted" warning.
const original = { a: 1 };
const compressed = { b: 2 };
const r = guardPipelineInflation({
originalBody: original,
compressedBody: compressed,
originalTokens: 50,
compressedTokens: 50,
});
assert.equal(r.inflated, false);
assert.equal(r.body, compressed);
});
test("guardPipelineInflation keeps the compressed body when it actually shrank", () => {
const original = { a: 1, pad: "xxxxxxxx" };
const compressed = { a: 1 };
const r = guardPipelineInflation({
originalBody: original,
compressedBody: compressed,
originalTokens: 100,
compressedTokens: 40,
});
assert.equal(r.inflated, false);
assert.equal(r.body, compressed);
});
test("guardPipelineInflation treats empty input as not inflated", () => {
const r = guardPipelineInflation({
originalBody: {},
compressedBody: {},
originalTokens: 0,
compressedTokens: 0,
});
assert.equal(r.inflated, false);
});
// --- Wire: an inflating engine in the stacked pipeline is reverted ---
const INFLATE_ID = "test-inflate-guard";
const inflatingEngine: CompressionEngine = {
id: INFLATE_ID,
name: "Test Inflate",
description: "test-only engine that inflates the body",
icon: "bug_report",
targets: ["messages"],
stackable: true,
stackPriority: 0,
metadata: {
id: INFLATE_ID,
name: "Test Inflate",
description: "test-only",
inputScope: "messages",
targetLatencyMs: 0,
supportsPreview: false,
stable: true,
},
apply(body) {
const inflated = { ...body, __pad: "x".repeat(4000) };
return {
body: inflated,
compressed: true,
stats: {
originalTokens: 10,
compressedTokens: 1010,
savingsPercent: -10000,
techniquesUsed: [INFLATE_ID],
mode: "stacked",
timestamp: 0,
},
};
},
compress(body) {
return this.apply(body);
},
getConfigSchema() {
return [];
},
validateConfig() {
return { valid: true, errors: [] };
},
};
test("applyStackedCompression reverts to the original body when the pipeline inflates", () => {
registerCompressionEngine(inflatingEngine);
setEngineEnabled(INFLATE_ID, true);
const body = {
messages: [{ role: "user", content: "hello world this is a short message" }],
};
// Pass a step object, not a bare string: `normalizePipelineStep()` only recognizes a fixed
// set of built-in bare-string aliases ("standard"/"rtk"/"lite"/"aggressive"/"ultra") and
// silently falls back to `{ engine: "caveman" }` for any other string — a bare custom-engine
// id here would silently run caveman instead of the registered `inflatingEngine`.
const result = applyStackedCompression(body, [{ engine: INFLATE_ID } as CompressionPipelineStep]);
// The inflating engine produced a bigger body, so the aggregate guard discarded it.
assert.equal(result.compressed, false);
assert.deepEqual(result.body, body);
assert.ok(
(result.stats.validationWarnings ?? []).some((w) => w.includes("pipeline-inflation-guard")),
"expected the inflation-guard warning in stats.validationWarnings"
);
});