Files
OmniRoute/tests/unit/compression/cachingAware.test.ts
Diego Rodrigues de Sa e Souza 62e0920e5e fix(compression): preserve cacheable prefix for automatic-cache providers (#3955) (#4334)
OpenAI / Codex / Azure-OpenAI use automatic prefix caching: the upstream
caches the longest matching prefix of a request (system prompt + earliest
messages) WITHOUT any explicit cache_control markers. The cache-aware
compression guard only protected that prefix when the body carried explicit
cache_control, so for automatic-cache providers the guard was skipped — and
with compression active + preserveSystemPrompt:false (or a prefix-compressing
mode) it rewrote the prefix, guaranteeing a cache miss and higher token spend
through OmniRoute than going direct.

getCacheAwareStrategy now treats isCachingProvider alone as sufficient to skip
the system prompt and downgrade aggressive/ultra (the explicit cache_control
path is a subset). openai/codex/azure are added to CACHING_PROVIDERS so they
are recognized as automatic-cache providers (this also activates the intended
prompt_cache_key cache-routing hint for OpenAI in chatCore).

Compression remains off by default — this only affects operators who enabled
it with prefix preservation turned off.

TDD: tests/unit/compression-cache-guard-3955.test.ts (RED 5/7 fail → GREEN
7/7). Aligned the existing cachingAware / strategySelector-cache-aware /
cache-control-policy / cache-control-claude-providers tests that encoded the
old (buggy) "openai is non-caching" behavior.

Refs #3955
2026-06-19 22:31:42 -03:00

156 lines
6.0 KiB
TypeScript

/**
* Unit tests for open-sse/services/compression/cachingAware.ts
*/
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import {
detectCachingContext,
getCacheAwareStrategy,
} from "../../../open-sse/services/compression/cachingAware.ts";
describe("detectCachingContext", () => {
it("returns hasCacheControl=true when body has cache_control", () => {
const ctx = detectCachingContext({ cache_control: { type: "ephemeral" } });
assert.equal(ctx.hasCacheControl, true);
});
it("returns hasCacheControl=false when body has no cache_control", () => {
const ctx = detectCachingContext({ model: "anthropic/claude-3" });
assert.equal(ctx.hasCacheControl, false);
});
it("extracts anthropic provider from model string", () => {
const ctx = detectCachingContext({ model: "anthropic/claude-3-sonnet" });
assert.equal(ctx.provider, "anthropic");
assert.equal(ctx.isCachingProvider, true);
});
it("extracts openai provider from model string (automatic prefix caching, #3955)", () => {
const ctx = detectCachingContext({ model: "openai/gpt-4o" });
assert.equal(ctx.provider, "openai");
// #3955 — OpenAI uses automatic prefix caching; it counts as a caching provider.
assert.equal(ctx.isCachingProvider, true);
});
it("extracts google provider from model string", () => {
const ctx = detectCachingContext({ model: "google/gemini-pro" });
assert.equal(ctx.provider, "google");
assert.equal(ctx.isCachingProvider, false);
});
it("keeps provider prefix and applies the shared caching policy", () => {
const ctx = detectCachingContext({ model: "deepseek/deepseek-chat" });
assert.equal(ctx.provider, "deepseek");
assert.equal(ctx.isCachingProvider, true);
});
it("handles null/undefined body gracefully", () => {
const ctx = detectCachingContext(null);
assert.equal(ctx.hasCacheControl, false);
assert.equal(ctx.provider, null);
assert.equal(ctx.isCachingProvider, false);
});
it("handles empty object body", () => {
const ctx = detectCachingContext({});
assert.equal(ctx.hasCacheControl, false);
assert.equal(ctx.provider, null);
assert.equal(ctx.isCachingProvider, false);
});
it("detects cache_control in Claude message content blocks", () => {
const ctx = detectCachingContext(
{
messages: [
{
role: "user",
content: [{ type: "text", text: "cached", cache_control: { type: "ephemeral" } }],
},
],
},
{ provider: "anthropic", targetFormat: "claude" }
);
assert.equal(ctx.hasCacheControl, true);
assert.equal(ctx.provider, "anthropic");
assert.equal(ctx.targetFormat, "claude");
assert.equal(ctx.isCachingProvider, true);
});
it("detects cache_control in Claude tools", () => {
const ctx = detectCachingContext(
{
tools: [{ name: "lookup", cache_control: { type: "ephemeral" } }],
},
{ provider: "qwen", targetFormat: "claude" }
);
assert.equal(ctx.hasCacheControl, true);
assert.equal(ctx.isCachingProvider, true);
});
it("prefers explicit provider context over the body model prefix", () => {
const ctx = detectCachingContext(
{ model: "openai/gpt-4o", cache_control: { type: "ephemeral" } },
{ provider: "anthropic", targetFormat: "claude", model: "claude-3-5-sonnet" }
);
assert.equal(ctx.provider, "anthropic");
assert.equal(ctx.isCachingProvider, true);
});
});
describe("getCacheAwareStrategy", () => {
it("downgrades aggressive to standard for caching provider with cache_control", () => {
const ctx = { hasCacheControl: true, provider: "anthropic", isCachingProvider: true };
const result = getCacheAwareStrategy("aggressive", ctx);
assert.equal(result.strategy, "standard");
assert.equal(result.skipSystemPrompt, true);
assert.equal(result.deterministicOnly, true);
});
it("downgrades ultra to standard for caching provider with cache_control", () => {
const ctx = { hasCacheControl: true, provider: "openai", isCachingProvider: true };
const result = getCacheAwareStrategy("ultra", ctx);
assert.equal(result.strategy, "standard");
assert.equal(result.skipSystemPrompt, true);
assert.equal(result.deterministicOnly, true);
});
it("keeps standard strategy unchanged for caching provider with cache_control", () => {
const ctx = { hasCacheControl: true, provider: "anthropic", isCachingProvider: true };
const result = getCacheAwareStrategy("standard", ctx);
assert.equal(result.strategy, "standard");
assert.equal(result.skipSystemPrompt, true);
assert.equal(result.deterministicOnly, true);
});
it("keeps strategy unchanged for non-caching provider", () => {
const ctx = { hasCacheControl: true, provider: "deepseek", isCachingProvider: false };
const result = getCacheAwareStrategy("aggressive", ctx);
assert.equal(result.strategy, "aggressive");
assert.equal(result.skipSystemPrompt, false);
assert.equal(result.deterministicOnly, false);
});
it("protects the prefix for a caching provider even WITHOUT cache_control (#3955)", () => {
// #3955 — automatic prefix caching (OpenAI/Codex/Anthropic) sets no cache_control
// markers, but the cacheable prefix must still be preserved. isCachingProvider alone
// is sufficient to skip the system prompt and downgrade prefix-compressing modes.
const ctx = { hasCacheControl: false, provider: "anthropic", isCachingProvider: true };
const result = getCacheAwareStrategy("aggressive", ctx);
assert.equal(result.strategy, "standard");
assert.equal(result.skipSystemPrompt, true);
assert.equal(result.deterministicOnly, true);
});
it("returns none strategy unchanged", () => {
const ctx = { hasCacheControl: false, provider: null, isCachingProvider: false };
const result = getCacheAwareStrategy("none", ctx);
assert.equal(result.strategy, "none");
assert.equal(result.skipSystemPrompt, false);
assert.equal(result.deterministicOnly, false);
});
});