Files
OmniRoute/tests/unit/compression/strategySelector-cache-aware.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

61 lines
2.4 KiB
TypeScript

/**
* Tests for #3890: the cache-aware `skipSystemPrompt` flag was computed by
* getCacheAwareStrategy() but dropped by selectCompressionStrategy() (which can only
* return a mode string). resolveCacheAwareConfig() applies it: in a caching context the
* system prompt must stay uncompressed (it is part of the cacheable prefix) even when the
* operator disabled preserveSystemPrompt.
*/
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { resolveCacheAwareConfig } from "../../../open-sse/services/compression/strategySelector.ts";
import type { CompressionConfig } from "../../../open-sse/services/compression/types.ts";
function cfg(overrides: Partial<CompressionConfig> = {}): CompressionConfig {
return {
enabled: true,
defaultMode: "standard",
autoTriggerTokens: 0,
cacheMinutes: 5,
preserveSystemPrompt: true,
comboOverrides: {},
...overrides,
} as CompressionConfig;
}
describe("resolveCacheAwareConfig (#3890)", () => {
it("forces preserveSystemPrompt on for a caching request that disabled it", () => {
const out = resolveCacheAwareConfig(
cfg({ preserveSystemPrompt: false }),
{ messages: [{ role: "system", content: "x", cache_control: { type: "ephemeral" } }] },
{ provider: "anthropic", targetFormat: "claude" }
);
assert.equal(out.preserveSystemPrompt, true);
});
it("leaves a non-caching request untouched (preserveSystemPrompt stays false)", () => {
// google has no prompt caching, so the prefix-protection guard does not apply.
// (openai/codex now count as automatic-cache providers per #3955.)
const out = resolveCacheAwareConfig(
cfg({ preserveSystemPrompt: false }),
{ messages: [{ role: "system", content: "x" }] },
{ provider: "google" }
);
assert.equal(out.preserveSystemPrompt, false);
});
it("returns the same config object when there is no body", () => {
const base = cfg({ preserveSystemPrompt: false });
assert.equal(resolveCacheAwareConfig(base), base);
});
it("does not change a config that already preserves the system prompt", () => {
const out = resolveCacheAwareConfig(
cfg({ preserveSystemPrompt: true }),
{ messages: [{ role: "system", content: "x", cache_control: { type: "ephemeral" } }] },
{ provider: "anthropic", targetFormat: "claude" }
);
assert.equal(out.preserveSystemPrompt, true);
});
});