Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **fix(compression):** the legacy default-combo fallback now respects the `x-omniroute-compression` header and the lossy request policy: a header-chosen plan (named combo, `engine:<id>`, `default`, `off`) always wins, and without an opt-in the default combo runs only its safe steps, so an operator-edited default combo can no longer run rtk or other lossy engines on requests that never asked for them
17 changes: 11 additions & 6 deletions open-sse/handlers/chatCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ import {
import type { EnforceDecision } from "@/lib/quota/types";
import { isCompressionExcluded } from "../services/compression/exclusions.ts";
import {
defaultComboForRequest,
isBuiltinStackedPipeline,
isStackedCompressionCombo,
type RuntimeCompressionCombo,
} from "./chatCore/compressionComboPredicates.ts";
import { emitOutputStyleTelemetry } from "./chatCore/outputStyleTelemetry.ts";
Expand Down Expand Up @@ -1672,15 +1672,20 @@ async function handleChatCoreInner({
try {
const { getDefaultCompressionCombo } =
await import("../../src/lib/db/compressionCombos.ts");
const defaultCompressionCombo = getDefaultCompressionCombo();
if (
isStackedCompressionCombo(defaultCompressionCombo as RuntimeCompressionCombo | null) &&
applyCompressionComboConfig(defaultCompressionCombo as RuntimeCompressionCombo | null)
) {
const defaultCompressionCombo = defaultComboForRequest(
getDefaultCompressionCombo() as RuntimeCompressionCombo | null,
{ config, header: compressionHeader, combos: namedCombos }
);
if (applyCompressionComboConfig(defaultCompressionCombo)) {
log?.debug?.(
"COMPRESSION",
`Default compression combo applied: ${defaultCompressionCombo?.id}`
);
} else if (compressionHeader) {
log?.debug?.(
"COMPRESSION",
`Default compression combo not applied (header: ${compressionHeader})`
);
}
} catch (err) {
log?.debug?.(
Expand Down
40 changes: 35 additions & 5 deletions open-sse/handlers/chatCore/compressionComboPredicates.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
/**
* chatCore compression-combo predicates (Quality Gate v2 / Fase 9 — chatCore god-file
* chatCore compression-combo helpers (Quality Gate v2 / Fase 9 — chatCore god-file
* decomposition, #3501).
*
* Pure predicates extracted from handleChatCore's compression setup: detect the built-in
* RTK→caveman stacked pipeline and whether a runtime combo has at least one pipeline layer.
* No handler state is captured; behaviour is byte-identical to the previous inline closures.
* Extracted from handleChatCore's compression setup: the two predicates detect the built-in
* RTK→caveman stacked pipeline and whether a runtime combo has at least one pipeline layer
* (byte-identical to the original inline closures); defaultComboForRequest resolves the legacy
* default combo for one request — yielding to a plan the request header chose, otherwise
* applying the lossy request policy to the combo's pipeline. No handler state is captured.
*/

import type { CompressionConfig } from "../../services/compression/types.ts";
import type {
CompressionConfig,
CompressionPipelineStep,
} from "../../services/compression/types.ts";
import { applyLossyRequestPolicy } from "../../services/compression/lossyRequestPolicy.ts";
import { planFromHeader } from "../../services/compression/planResolution.ts";

export type RuntimeCompressionCombo = {
id: string;
Expand Down Expand Up @@ -39,3 +46,26 @@ export function isStackedCompressionCombo(
// page) must still apply. applyCompressionComboConfig already guards length === 0.
return Boolean(compressionCombo && compressionCombo.pipeline.length >= 1);
}

/**
* The legacy default combo as this request may run it, or null when it must not apply. A plan
* the request header chose (a named combo, engine:<id>, default) wins over every operator layer.
* The default combo is operator configuration, not a request opt-in, so its lossy steps get the
* same policy resolveBasePlan applies to every other plan.
*/
export function defaultComboForRequest(
compressionCombo: RuntimeCompressionCombo | null,
request: {
config: CompressionConfig;
header: string | null;
combos: Record<string, CompressionPipelineStep[]>;
}
): RuntimeCompressionCombo | null {
if (!isStackedCompressionCombo(compressionCombo)) return null;
if (request.header && planFromHeader(request.config, request.header, request.combos)) return null;
const { stackedPipeline } = applyLossyRequestPolicy(
{ mode: "stacked", stackedPipeline: compressionCombo.pipeline },
request.header
);
return { ...compressionCombo, pipeline: stackedPipeline as RuntimeCompressionCombo["pipeline"] };
}
7 changes: 6 additions & 1 deletion open-sse/services/compression/planResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ export function planFromHeader(
}

const combo = combos[lower] ?? combos[h];
return combo ? withSource({ mode: "stacked", stackedPipeline: combo }, "request-header") : null;
// Only a real, non-empty pipeline is a chosen plan: an empty-array combo would otherwise win
// the precedence race and let the builtin fallback pipeline run lossy unrequested, and the
// plain-object lookup resolves prototype keys (constructor, toString) to truthy garbage.
return Array.isArray(combo) && combo.length > 0
? withSource({ mode: "stacked", stackedPipeline: combo }, "request-header")
: null;
}

/** Renders the X-OmniRoute-Compression response header value. */
Expand Down
93 changes: 0 additions & 93 deletions tests/integration/chatcore-compression-integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -811,99 +811,6 @@ test("chatCore integration: default stacked compression combo applies for unassi
}
});

test.skip("chatCore integration: seeded default combo runs RTK before Caveman", async () => {
const provider = "openai";
const model = "gpt-4";

await compressionDb.updateCompressionSettings({
enabled: true,
defaultMode: "stacked",
autoTriggerTokens: 0,
cavemanOutputMode: {
enabled: false,
intensity: "full",
autoClarity: true,
},
languageConfig: {
enabled: false,
defaultLanguage: "en",
autoDetect: true,
enabledPacks: ["en"],
},
});

const connection = await providersDb.createProviderConnection({
provider,
apiKey: "test-key",
isActive: true,
});

let capturedBody: { messages?: Array<{ role?: string; content?: string }> } | null = null;
globalThis.fetch = async (_url: string | URL | Request, init?: RequestInit) => {
if (init?.body) {
capturedBody = JSON.parse(init.body as string) as typeof capturedBody;
}
return new Response(
JSON.stringify({
choices: [{ message: { role: "assistant", content: "ok" } }],
usage: { prompt_tokens: 10, completion_tokens: 5, total_tokens: 15 },
}),
{
status: 200,
headers: { "content-type": "application/json" },
}
);
};

try {
const result = await handleChatCore({
body: {
model,
stream: false,
messages: [
{
role: "tool",
content: Array.from({ length: 8 }, () => "same noisy line").join("\n"),
},
],
},
modelInfo: { provider, model },
credentials: { apiKey: "test-key" },
log: { debug: () => {}, info: () => {}, warn: () => {}, error: () => {} },
clientRawRequest: { endpoint: "/v1/chat/completions", headers: new Map() },
connectionId: connection.id,
onCredentialsRefreshed: () => {},
onRequestSuccess: () => {},
onStreamFailure: () => {},
onDisconnect: () => {},
userAgent: "test-agent",
comboName: null,
});

assert.ok(result.success, "Request should succeed");
assert.ok(capturedBody, "Fetch should receive the request body");
const toolContent = capturedBody.messages?.[0]?.content ?? "";
assert.match(toolContent, /rtk:dropped 7 repeated lines/);

let summary = compressionAnalyticsDb.getCompressionAnalyticsSummary();
for (
let attempt = 0;
attempt < 100 &&
(summary.byCompressionCombo["default-caveman"]?.count !== 1 ||
summary.realUsage.requestsWithReceipts === 0);
attempt += 1
) {
await new Promise((resolve) => setTimeout(resolve, 20));
summary = compressionAnalyticsDb.getCompressionAnalyticsSummary();
}

assert.equal(summary.totalRequests, 1);
assert.equal(summary.byCompressionCombo["default-caveman"].count, 1);
} finally {
globalThis.fetch = originalFetch;
}
});

test("chatCore integration: modular compression records analytics row best-effort", async () => {
const provider = "openai";
const model = "gpt-4";
Expand Down
129 changes: 117 additions & 12 deletions tests/unit/chatcore-compression-combo-predicates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import { test } from "node:test";
import assert from "node:assert/strict";

const { isBuiltinStackedPipeline, isStackedCompressionCombo } = await import(
"../../open-sse/handlers/chatCore/compressionComboPredicates.ts"
);
const { defaultComboForRequest, isBuiltinStackedPipeline, isStackedCompressionCombo } =
await import("../../open-sse/handlers/chatCore/compressionComboPredicates.ts");
const { DEFAULT_COMPRESSION_CONFIG } = await import("../../open-sse/services/compression/types.ts");

test("isBuiltinStackedPipeline true only for the rtk(standard)→caveman(full) shape", () => {
assert.equal(
isBuiltinStackedPipeline([{ engine: "rtk" }, { engine: "caveman" }] as never),
true
);
assert.equal(isBuiltinStackedPipeline([{ engine: "rtk" }, { engine: "caveman" }] as never), true);
assert.equal(
isBuiltinStackedPipeline([
{ engine: "rtk", intensity: "standard" },
Expand All @@ -37,18 +34,126 @@ test("isBuiltinStackedPipeline false for wrong length / engines / intensities /
false
);
assert.equal(
isBuiltinStackedPipeline([
{ engine: "rtk", config: { x: 1 } },
{ engine: "caveman" },
] as never),
isBuiltinStackedPipeline([{ engine: "rtk", config: { x: 1 } }, { engine: "caveman" }] as never),
false
);
});

const editedDefaultCombo = {
id: "default-caveman",
pipeline: [{ engine: "session-dedup" }, { engine: "lite" }, { engine: "rtk" }],
languagePacks: ["en"],
outputMode: false,
outputModeIntensity: "full",
} as never;
const legacyConfig = {
...DEFAULT_COMPRESSION_CONFIG,
enabled: true,
defaultMode: "stacked" as const,
};

test("defaultComboForRequest keeps only the safe steps when the request does not opt in", () => {
for (const header of [null, "safe", "engine:rtk"]) {
const combo = defaultComboForRequest(editedDefaultCombo, {
config: legacyConfig,
header,
combos: {},
});
assert.deepEqual(
combo?.pipeline,
[{ engine: "session-dedup" }, { engine: "lite" }],
String(header)
);
}
});

test("defaultComboForRequest keeps the lossy steps under allow-lossy", () => {
const combo = defaultComboForRequest(editedDefaultCombo, {
config: legacyConfig,
header: "allow-lossy",
combos: {},
});
assert.deepEqual(combo?.pipeline, [
{ engine: "session-dedup" },
{ engine: "lite" },
{ engine: "rtk" },
]);
});

test("defaultComboForRequest yields to a plan the header chose", () => {
const combos = { "rtk only": [{ engine: "rtk" }] } as never;
for (const header of ["RTK only", "default", "off"]) {
assert.equal(
defaultComboForRequest(editedDefaultCombo, { config: legacyConfig, header, combos }),
null,
header
);
}
assert.equal(
defaultComboForRequest(null, { config: legacyConfig, header: null, combos: {} }),
null
);
});

test("defaultComboForRequest returns null for a non-null combo with an empty pipeline", () => {
const emptyPipelineCombo = {
id: "default-caveman",
pipeline: [],
languagePacks: ["en"],
outputMode: false,
outputModeIntensity: "full",
} as never;
assert.equal(
defaultComboForRequest(emptyPipelineCombo, { config: legacyConfig, header: null, combos: {} }),
null
);
});

test("defaultComboForRequest swaps a single lossy engine for the safe default pair", () => {
const singleLossy = { ...editedDefaultCombo, pipeline: [{ engine: "rtk" }] } as never;
const combo = defaultComboForRequest(singleLossy, {
config: legacyConfig,
header: null,
combos: {},
});
assert.deepEqual(combo?.pipeline, [{ engine: "session-dedup" }, { engine: "lite" }]);
});

test("defaultComboForRequest keeps a single safe engine as-is", () => {
const singleSafe = { ...editedDefaultCombo, pipeline: [{ engine: "lite" }] } as never;
const combo = defaultComboForRequest(singleSafe, {
config: legacyConfig,
header: null,
combos: {},
});
assert.deepEqual(combo?.pipeline, [{ engine: "lite" }]);
});

test("defaultComboForRequest yields when an engine:<id> header resolves a plan", () => {
const engineEnabledConfig = {
...legacyConfig,
engines: { ...legacyConfig.engines, rtk: { enabled: true } },
} as never;
assert.equal(
defaultComboForRequest(editedDefaultCombo, {
config: engineEnabledConfig,
header: "engine:rtk",
combos: {},
}),
null
);
});

test("isStackedCompressionCombo true when the combo has >= 1 pipeline layer", () => {
assert.equal(isStackedCompressionCombo(null), false);
assert.equal(
isStackedCompressionCombo({ id: "c", pipeline: [], languagePacks: [], outputMode: false, outputModeIntensity: "full" } as never),
isStackedCompressionCombo({
id: "c",
pipeline: [],
languagePacks: [],
outputMode: false,
outputModeIntensity: "full",
} as never),
false
);
assert.equal(
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/compression/compression-header-dispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ describe("planFromHeader (Phase 3)", () => {
assert.deepEqual(planFromHeader(cfg(), "c1", combos)?.stackedPipeline, combos.c1);
});

it("an empty-pipeline combo is not a chosen plan (fall-through)", () => {
assert.equal(planFromHeader(cfg(), "empty", { empty: [] }), null);
});

it("prototype-key header values do not resolve to a plan", () => {
for (const header of ["__proto__", "constructor", "toString"]) {
assert.equal(planFromHeader(cfg(), header, combos), null, header);
}
});

it("unknown value => null (fall-through)", () => {
assert.equal(planFromHeader(cfg(), "nonsense", combos), null);
});
Expand Down
Loading
Loading