File size: 6,122 Bytes
cd8bd0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/**
 * 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);
  });
});