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.
This commit is contained in:
Diego Rodrigues de Sa e Souza
2026-06-26 22:28:35 -03:00
committed by Diego Rodrigues de Sa e Souza
parent fc41228a8f
commit 2b00e4e4fd
3 changed files with 44 additions and 39 deletions

View File

@@ -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 <cap) and quotaShareStrategy.ts (per-model bucket gating via isBucketSaturated + DRR proportional to weight + P2C over in-flight, ~240 LOC <cap). Only the dispatch wiring is irreducible at the existing combo strategy chokepoint (mirrors the headroom/reset-aware/reset-window/context-optimized branches); not extractable without hiding the call site. ZERO existing strategy cases were modified — only this branch was added, and the qtSd/ combos switched from fill-first to quota-share in src/lib/quota/quotaCombos.ts. Covered by tests/unit/quota-share-strategy.test.ts (gating, DRR fairness, P2C in-flight, fail-open, activation). Structural shrink of combo.ts tracked in #3501.",
"_rebaseline_2026_06_24_task_aware_routing": "Task-aware routing strategy (port PR #2045, OmniRoute #4945): combo.ts 3190->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 <cap). Only the dispatch wiring is irreducible at the existing combo strategy chokepoint (mirrors quota-share/headroom/reset-aware branches). ZERO existing strategy cases modified. Covered by tests/unit/combo-task-aware.test.ts (35 tests). Structural shrink of combo.ts tracked in #3501.",
"open-sse/services/combo.ts": 3368,
"open-sse/services/compression/strategySelector.ts": 848,
"_rebaseline_2026_06_26_fidelity_gate_extraction": "Milestone-B fidelity-gate wiring residual: bodyToText+gateAdvance extracted to fidelityGateStep.ts (889->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,

View File

@@ -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, unknown>): 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<string, unknown>,
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;
}

View File

@@ -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<string>;
rules: Set<string>;
breakdown: NonNullable<CompressionStats["engineBreakdown"]>;
@@ -650,41 +650,6 @@ function decideStep(result: CompressionResult, bailout: BailoutConfig): { advanc
return { advance: true };
}
function bodyToText(body: Record<string, unknown>): 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<string, unknown>,
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;