From 2b00e4e4fd549d09dbaae8417fc328b53b8f96b3 Mon Sep 17 00:00:00 2001 From: Diego Rodrigues de Sa e Souza Date: Fri, 26 Jun 2026 22:28:35 -0300 Subject: [PATCH] refactor(compression): extract fidelity-gate step helpers to shrink strategySelector (file-size gate) bodyToText and gateAdvance moved to fidelityGateStep.ts; StackAccumulator exported. strategySelector: 889->854 (-35). Residual +6 vs pre-Milestone-B frozen 848 is the irreducible StackOptions.fidelityGate field + two stacked-loop dispatch reads + import. Baseline updated to 854 with justification. No cycle introduced (import type only). 940 compression tests pass; typecheck clean. --- config/quality/file-size-baseline.json | 3 +- .../services/compression/fidelityGateStep.ts | 39 ++++++++++++++++++ .../services/compression/strategySelector.ts | 41 ++----------------- 3 files changed, 44 insertions(+), 39 deletions(-) create mode 100644 open-sse/services/compression/fidelityGateStep.ts diff --git a/config/quality/file-size-baseline.json b/config/quality/file-size-baseline.json index f2c9818185..e99bbd3d44 100644 --- a/config/quality/file-size-baseline.json +++ b/config/quality/file-size-baseline.json @@ -158,7 +158,8 @@ "_rebaseline_2026_06_24_quota_share_strategy": "Dedicated quota-share strategy (Phase 3 #9): combo.ts 3180->3190 (+10 = one new `else if (strategy === \"quota-share\")` dispatch branch in handleComboChat that delegates 100% to selectQuotaShareTarget + its log line, plus the import). All the new logic lives OUT of the god-file in two new leaves under open-sse/services/combo/: quotaShareInflight.ts (in-flight counter with TTL/lease, ~150 LOC 3225 (+35) = one new `else if (strategy === \"task-aware\")` dispatch branch delegating 100% to selectTaskAwareTarget + its imports/log lines. All scoring/classification logic lives OUT of the god-file in the new leaf open-sse/services/taskAwareRouting.ts (553 LOC 854, -35), but the StackOptions.fidelityGate field, the `const fidelityGate` reads at the two stacked-loop dispatch chokepoints, and the import of FidelityGateConfig are irreducible wiring that cannot leave strategySelector without an architectural refactor of the pre-existing stacked pipeline. Net: 889->854 (+6 vs the pre-Milestone-B frozen 848). Covered by tests/unit/compression/*.test.ts (940 pass).", + "open-sse/services/compression/strategySelector.ts": 854, "open-sse/services/rateLimitManager.ts": 1035, "open-sse/services/tokenRefresh.ts": 2103, "open-sse/services/usage.ts": 3454, diff --git a/open-sse/services/compression/fidelityGateStep.ts b/open-sse/services/compression/fidelityGateStep.ts new file mode 100644 index 0000000000..ce0a1e6cbc --- /dev/null +++ b/open-sse/services/compression/fidelityGateStep.ts @@ -0,0 +1,39 @@ +import { extractTextContent } from "./messageContent.ts"; +import { checkFidelity, type FidelityGateConfig } from "./fidelityGate.ts"; +import type { CompressionResult } from "./types.ts"; +import type { StackAccumulator } from "./strategySelector.ts"; + +function bodyToText(body: Record): string { + const messages = body.messages; + if (!Array.isArray(messages)) return ""; + return messages.map((m) => extractTextContent((m as { content?: unknown }).content as never)).join("\n"); +} + +/** + * Fidelity gate (opt-in, independent of TV1). Called at each advance point AFTER mergeStackStep + * pushed the step's breakdown entry. Off → zero-cost `return true` (byte-identical legacy). On a + * fidelity failure it marks the just-pushed breakdown entry rejected (no advance) and returns false. + */ +export function gateAdvance( + result: CompressionResult, + inputBody: Record, + fidelityGate: FidelityGateConfig | undefined, + acc: StackAccumulator +): boolean { + if (!fidelityGate?.enabled) return true; + const verdict = checkFidelity(bodyToText(inputBody), bodyToText(result.body), fidelityGate); + if (verdict.passed) return true; + // mergeStackStep only pushed a breakdown entry when result.stats exists; only then is + // acc.breakdown's last entry THIS step's (else it would be the prior engine's — don't touch it). + if (result.stats) { + const last = acc.breakdown[acc.breakdown.length - 1]; + if (last) { + last.rejected = true; + last.rejectReason = verdict.detail ?? verdict.failedInvariant; + last.compressedTokens = last.originalTokens; + last.savingsPercent = 0; + } + } + acc.fallbackApplied = true; + return false; +} diff --git a/open-sse/services/compression/strategySelector.ts b/open-sse/services/compression/strategySelector.ts index bc1c521692..6f8ea2779b 100644 --- a/open-sse/services/compression/strategySelector.ts +++ b/open-sse/services/compression/strategySelector.ts @@ -5,8 +5,8 @@ import type { CompressionResult, CompressionStats, } from "./types.ts"; -import { extractTextContent } from "./messageContent.ts"; -import { checkFidelity, type FidelityGateConfig } from "./fidelityGate.ts"; +import { type FidelityGateConfig } from "./fidelityGate.ts"; +import { gateAdvance } from "./fidelityGateStep.ts"; import type { CompressionEngineApplyOptions } from "./engines/types.ts"; import { applyLiteCompression } from "./lite.ts"; import { cavemanCompress } from "./caveman.ts"; @@ -585,7 +585,7 @@ function reportEngineStep( } /** Accumulates per-step telemetry across a stacked run (shared sync/async). */ -interface StackAccumulator { +export interface StackAccumulator { techniques: Set; rules: Set; breakdown: NonNullable; @@ -650,41 +650,6 @@ function decideStep(result: CompressionResult, bailout: BailoutConfig): { advanc return { advance: true }; } -function bodyToText(body: Record): string { - const messages = body.messages; - if (!Array.isArray(messages)) return ""; - return messages.map((m) => extractTextContent((m as { content?: unknown }).content as never)).join("\n"); -} - -/** - * Fidelity gate (opt-in, independent of TV1). Called at each advance point AFTER mergeStackStep - * pushed the step's breakdown entry. Off → zero-cost `return true` (byte-identical legacy). On a - * fidelity failure it marks the just-pushed breakdown entry rejected (no advance) and returns false. - */ -function gateAdvance( - result: CompressionResult, - inputBody: Record, - fidelityGate: FidelityGateConfig | undefined, - acc: StackAccumulator -): boolean { - if (!fidelityGate?.enabled) return true; - const verdict = checkFidelity(bodyToText(inputBody), bodyToText(result.body), fidelityGate); - if (verdict.passed) return true; - // mergeStackStep only pushed a breakdown entry when result.stats exists; only then is - // acc.breakdown's last entry THIS step's (else it would be the prior engine's — don't touch it). - if (result.stats) { - const last = acc.breakdown[acc.breakdown.length - 1]; - if (last) { - last.rejected = true; - last.rejectReason = verdict.detail ?? verdict.failedInvariant; - last.compressedTokens = last.originalTokens; - last.savingsPercent = 0; - } - } - acc.fallbackApplied = true; - return false; -} - /** Folds one engine result into the accumulator (telemetry + breakdown entry). */ function mergeStackStep(acc: StackAccumulator, engineId: string, result: CompressionResult): void { if (!result.stats) return;