Files
OmniRoute/open-sse/services/compression/stackedStepCore.ts
Diego Rodrigues de Sa e Souza c089ca9d1a fix(compression): surface silently-dropped stacked-pipeline steps and fix inflation-guard no-op misfire (#6479, #6480, #6491) (#6901)
Two related root causes in the stacked compression pipeline:

- #6479/#6491: a dispatched step whose engine legitimately finds nothing
  eligible (session-dedup with no repeated blocks, ccr below its min-chars
  threshold) returns `{ stats: null }`. `mergeStackStep()` silently dropped
  that step from `engineBreakdown` with zero trace — no warning, no error.
  Now records a `"<engine>: skipped (no eligible content)"` validation
  warning for any null-stats step, covering every engine that follows this
  convention (session-dedup, ccr, headroom, relevance, llm, llmlingua,
  ionizer, readLifecycle), not just the two reported.

- #6480: `finalizeStackedResult` ran the aggregate `guardPipelineInflation`
  check unconditionally, even when the loop-level `compressed` flag stayed
  false (no step ever advanced `currentBody`). Since tokens are trivially
  equal when nothing ran, the guard mislabeled a genuine no-op as
  `fallbackApplied: true` with a misleading "reverted to original" warning.
  Extracted the guard into `applyStackedInflationGuard()` in
  `pipelineGuards.ts` (keeps `strategySelector.ts` under its frozen line
  budget) and gated it on `compressed === true`.

Also fixes `compression-pipeline-inflation-guard.test.ts`'s wire test,
which passed a bare engine-id string to the pipeline; `normalizePipelineStep()`
only recognizes a fixed set of built-in string aliases and silently
downgrades any other string to `{ engine: "caveman" }`, so the test's
custom inflating engine was never actually exercised. Passing a step object
restores the test's original intent.

New regression tests: tests/unit/compression/repro-6479-6491-null-stats-silent-drop.test.ts,
tests/unit/compression/repro-6480-noop-guard-misfire.test.ts.
2026-07-11 11:21:52 -03:00

102 lines
4.3 KiB
TypeScript

/**
* Core telemetry + step-decision machinery for the stacked compression pipeline, extracted
* from `strategySelector.ts` so that god-file stays bounded. This module is a leaf: it depends
* only on the shared compression types, so it never participates in a cycle.
*
* It holds the per-run accumulator (`StackAccumulator` + `createStackAccumulator`), the TV1
* bail-out config + advance decision (`BailoutConfig` + `decideStep`), and the per-step
* telemetry fold (`mergeStackStep`). The sync/async stacked loops in `strategySelector.ts`
* consume these.
*/
import type { CompressionResult, CompressionStats } from "./types.ts";
/**
* TV1 — Opt-in bail-out configuration for the stacked pipeline.
* When enabled: a step that throws is silently skipped (verbatim kept); a step whose gain is
* below `minGainPercent` is also skipped. DEFAULT = disabled (byte-identical to pre-TV1).
*/
export interface BailoutConfig {
enabled: boolean;
/** Minimum savings percent required to advance currentBody. Default: 10. */
minGainPercent?: number;
}
/** Accumulates per-step telemetry across a stacked run (shared sync/async). */
export interface StackAccumulator {
techniques: Set<string>;
rules: Set<string>;
breakdown: NonNullable<CompressionStats["engineBreakdown"]>;
rtkRawOutputPointers: NonNullable<CompressionStats["rtkRawOutputPointers"]>;
validationWarnings: Set<string>;
validationErrors: Set<string>;
fallbackApplied: boolean;
}
export function createStackAccumulator(): StackAccumulator {
return {
techniques: new Set<string>(),
rules: new Set<string>(),
breakdown: [],
rtkRawOutputPointers: [],
validationWarnings: new Set<string>(),
validationErrors: new Set<string>(),
fallbackApplied: false,
};
}
/**
* TV1 — Pure helper that decides whether a completed step should advance `currentBody`. Called
* only when bail-out is ENABLED; the loops bypass it on the default-off path (zero cost). Returns
* `{ advance: true }` to accept the step, or `{ advance: false }` to skip it (verbatim kept).
*/
export function decideStep(
result: CompressionResult,
bailout: BailoutConfig
): { advance: boolean } {
if (!result.compressed) return { advance: false };
// Clamp: a negative minGainPercent would mean "always advance" (invalid state).
const minGain = Math.max(0, bailout.minGainPercent ?? 10);
const gain = result.stats?.savingsPercent ?? 0;
if (gain < minGain) return { advance: false };
return { advance: true };
}
/**
* A dispatched step whose engine found nothing eligible (e.g. session-dedup with no repeated
* blocks, ccr below its min-chars threshold) returns `stats: null` instead of throwing or
* advancing. Left unrecorded, that step vanishes from the pipeline's telemetry with zero trace —
* no `engineBreakdown` entry, no warning, no error (#6479, #6491). Surface it as a validation
* warning so operators can tell "engine ran but had nothing to do" apart from "engine never ran".
*/
function recordNullStatsStep(acc: StackAccumulator, engineId: string): void {
acc.validationWarnings.add(`${engineId}: skipped (no eligible content)`);
}
/** Folds one engine result into the accumulator (telemetry + breakdown entry). */
export function mergeStackStep(
acc: StackAccumulator,
engineId: string,
result: CompressionResult
): void {
if (!result.stats) {
recordNullStatsStep(acc, engineId);
return;
}
result.stats.techniquesUsed.forEach((technique) => acc.techniques.add(technique));
result.stats.rulesApplied?.forEach((rule) => acc.rules.add(rule));
result.stats.rtkRawOutputPointers?.forEach((pointer) => acc.rtkRawOutputPointers.push(pointer));
result.stats.validationWarnings?.forEach((warning) => acc.validationWarnings.add(warning));
result.stats.validationErrors?.forEach((error) => acc.validationErrors.add(error));
acc.fallbackApplied = acc.fallbackApplied || result.stats.fallbackApplied === true;
acc.breakdown.push({
engine: engineId,
originalTokens: result.stats.originalTokens,
compressedTokens: result.stats.compressedTokens,
savingsPercent: result.stats.savingsPercent,
techniquesUsed: result.stats.techniquesUsed,
...(result.stats.rulesApplied ? { rulesApplied: result.stats.rulesApplied } : {}),
...(result.stats.durationMs !== undefined ? { durationMs: result.stats.durationMs } : {}),
});
}