Skip to content

fix(compression): preserve cacheable prefix for automatic-cache providers (#3955) - #4334

Merged
diegosouzapw merged 3 commits into
release/v3.8.30from
fix/3955-compression-cache-guard
Jun 20, 2026
Merged

diegosouzapw merged 3 commits into
release/v3.8.30from
fix/3955-compression-cache-guard

Conversation

@diegosouzapw

Copy link
Copy Markdown
Collaborator

Refs #3955 (the deterministic, reproducible sub-bug)

Problem

Higher prompt-cache miss rate through OmniRoute vs direct for OpenAI/Codex. The cache-aware compression guard (getCacheAwareStrategy) only protected the cacheable prefix when isCachingProvider && hasCacheControl. But OpenAI / Codex / Azure-OpenAI use automatic prefix caching — the upstream caches the longest matching prefix without any cache_control markers. Those providers weren't even in CACHING_PROVIDERS, so getCacheAwareStrategy was a no-op for them, and with compression active + preserveSystemPrompt:false (or aggressive/ultra) the system prompt/earliest messages got rewritten → guaranteed cache miss.

Fix (twofold — both required)

  1. open-sse/utils/cacheControlPolicy.ts — add openai, codex, azure to CACHING_PROVIDERS (they have automatic prefix caching). This also activates the already-present-but-dormant prompt_cache_key cache-routing hint for OpenAI in chatCore.ts (gated on providerSupportsCaching; injects a prefix-stable key from generatePromptCacheKey → analyzePrefix().prefixHash, only when the client didn't set one; codex/nvidia/xai stay excluded from that injection).
  2. open-sse/services/compression/cachingAware.ts — getCacheAwareStrategy protects the prefix on isCachingProvider alone (the explicit-cache_control path is now a subset): skipSystemPrompt: true + downgrade aggressive/ultra → standard.

Validation (Hard Rule #18 — TDD)

tests/unit/compression-cache-guard-3955.test.ts (7 cases): caching provider with a long system prompt and no cache_control + compression on + preserveSystemPrompt:false → prefix preserved (RED 2/7 → GREEN 7/7); a genuinely non-caching provider (gemini/google) is unaffected. Four existing tests that encoded the old buggy "openai is non-caching" behavior were aligned to the corrected behavior (negative cases switched to a real non-caching provider — not weakened). Regression: full tests/unit/compression/ (87 files) 737/737 + cache-control/codex-cache-key suites; 82/82 on my own re-run. Lint 0-err, typecheck:core clean, no file-size bump.

Caveat: compression is OFF by default, so the compression half only bites operators who enabled it with prefix preservation off. Confirming compression is the dominant cause of the reporter's symptom still needs their cache_read_input_tokens (direct vs via OmniRoute) — this PR fixes the one deterministic sub-bug + activates the OpenAI cache-routing hint.

…ntrol (#3955)

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
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@diegosouzapw
diegosouzapw merged commit 62e0920 into release/v3.8.30 Jun 20, 2026
diegosouzapw added a commit that referenced this pull request Jun 20, 2026
… lote drift) (#4336)

#4327 (per-key USD usage quotas), #4334 (cache-aware compression guard) and #4326
(phaseComboSetup) added new conditional branches landing after #4330 measured 1890;
the release fast-path doesn't run check:complexity. Measured 1896 on the merged tip.
@diegosouzapw
diegosouzapw deleted the fix/3955-compression-cache-guard branch June 20, 2026 04:21
tkgo11 pushed a commit to tkgo11/OmniRoute that referenced this pull request Sep 23, 2026
…ders (cheaper-inference#3955) (cheaper-inference#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 cheaper-inference#3955
tkgo11 pushed a commit to tkgo11/OmniRoute that referenced this pull request Sep 23, 2026
… lote drift) (cheaper-inference#4336)

cheaper-inference#4327 (per-key USD usage quotas), cheaper-inference#4334 (cache-aware compression guard) and cheaper-inference#4326
(phaseComboSetup) added new conditional branches landing after cheaper-inference#4330 measured 1890;
the release fast-path doesn't run check:complexity. Measured 1896 on the merged tip.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant