File size: 8,339 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import {
  cavemanCompress,
  applyRulesToText,
} from "../../../open-sse/services/compression/caveman.ts";
import { CAVEMAN_RULES } from "../../../open-sse/services/compression/cavemanRules.ts";
import type { CavemanRule } from "../../../open-sse/services/compression/types.ts";

describe("caveman engine", () => {
  it("should compress a verbose user prompt", () => {
    const body = {
      messages: [
        {
          role: "user",
          content:
            "Please could you help me analyze this code? I would like you to provide a detailed explanation of what the function does. Thank you so much for your help!",
        },
      ],
    };
    const result = cavemanCompress(body, {
      enabled: true,
      compressRoles: ["user"],
      skipRules: [],
      minMessageLength: 50,
      preservePatterns: [],
    });
    assert.equal(result.compressed, true);
    assert.ok(
      result.stats.savingsPercent > 0,
      `Expected savings > 0, got ${result.stats.savingsPercent}`
    );
    assert.ok(
      result.stats.rulesApplied && result.stats.rulesApplied.length > 0,
      "Expected rules applied"
    );
  });

  it("should skip messages below minMessageLength", () => {
    const body = { messages: [{ role: "user", content: "Hi" }] };
    const result = cavemanCompress(body, {
      enabled: true,
      compressRoles: ["user"],
      skipRules: [],
      minMessageLength: 50,
      preservePatterns: [],
    });
    assert.equal(result.compressed, false);
  });

  it("should return unchanged body when disabled", () => {
    const body = { messages: [{ role: "user", content: "Please help me with this code" }] };
    const result = cavemanCompress(body, {
      enabled: false,
      compressRoles: ["user"],
      skipRules: [],
      minMessageLength: 50,
      preservePatterns: [],
    });
    assert.equal(result.compressed, false);
  });

  it("should preserve code blocks", () => {
    const codeContent = "const x = 42;\nconsole.log(x);";
    const body = {
      messages: [
        {
          role: "user",
          content: `Please analyze this code:\n\`\`\`typescript\n${codeContent}\n\`\`\`\nThank you so much!`,
        },
      ],
    };
    const result = cavemanCompress(body, {
      enabled: true,
      compressRoles: ["user"],
      skipRules: [],
      minMessageLength: 50,
      preservePatterns: [],
    });
    const text = result.body.messages[0].content as string;
    assert.ok(text.includes(codeContent), `Code block should be preserved exactly`);
  });

  it("should preserve URLs", () => {
    const url = "https://example.com/api/v1/users";
    const body = {
      messages: [
        {
          role: "user",
          content: `Please check ${url} for the API docs. Thank you so much!`,
        },
      ],
    };
    const result = cavemanCompress(body, {
      enabled: true,
      compressRoles: ["user"],
      skipRules: [],
      minMessageLength: 50,
      preservePatterns: [],
    });
    const text = result.body.messages[0].content as string;
    assert.equal(
      text.split(/\s+/).some((token) => token === url),
      true,
      `URL should be preserved`
    );
  });

  it("should handle empty messages array", () => {
    const body = { messages: [] };
    const result = cavemanCompress(body, {
      enabled: true,
      compressRoles: ["user"],
      skipRules: [],
      minMessageLength: 50,
      preservePatterns: [],
    });
    assert.equal(result.compressed, false);
  });

  it("should handle messages without content", () => {
    const body = { messages: [{ role: "user" }] };
    const result = cavemanCompress(body, {
      enabled: true,
      compressRoles: ["user"],
      skipRules: [],
      minMessageLength: 50,
      preservePatterns: [],
    });
    assert.equal(result.compressed, false);
  });

  it("should skip rules in skipRules list", () => {
    const body = {
      messages: [{ role: "user", content: "Please help me with this code. Thank you so much!" }],
    };
    const resultWithSkip = cavemanCompress(body, {
      enabled: true,
      compressRoles: ["user"],
      skipRules: ["polite_framing"],
      minMessageLength: 50,
      preservePatterns: [],
    });
    assert.ok(
      !resultWithSkip.stats.rulesApplied?.includes("polite_framing"),
      "polite_framing should be skipped"
    );
  });

  it("should respect compressRoles setting", () => {
    const body = {
      messages: [
        {
          role: "system",
          content:
            "You are a very helpful assistant. Please be extremely detailed in all your responses.",
        },
        {
          role: "user",
          content:
            "Please could you help me with this code problem? I would like you to provide a detailed explanation. Thank you so much!",
        },
      ],
    };
    const result = cavemanCompress(body, {
      enabled: true,
      compressRoles: ["user"],
      skipRules: [],
      minMessageLength: 50,
      preservePatterns: [],
    });
    const systemMsg = result.body.messages[0].content as string;
    const userMsg = result.body.messages[1].content as string;
    assert.ok(
      systemMsg.includes("very"),
      "System message should not be compressed (not in compressRoles)"
    );
    assert.ok(
      !userMsg.includes("Please could you"),
      "User message should have 'Please could you' removed"
    );
    assert.ok(
      !userMsg.includes("Thank you so much"),
      "User message should have 'Thank you so much' removed"
    );
  });

  it("should compute stats accurately", () => {
    const body = {
      messages: [
        { role: "user", content: "Please could you help me with this problem? Thank you so much!" },
      ],
    };
    const result = cavemanCompress(body, {
      enabled: true,
      compressRoles: ["user"],
      skipRules: [],
      minMessageLength: 50,
      preservePatterns: [],
    });
    assert.ok(result.stats.originalTokens > 0);
    assert.ok(result.stats.compressedTokens >= 0);
    assert.ok(result.stats.savingsPercent >= 0);
    assert.ok(result.stats.durationMs >= 0);
    assert.ok(
      // Loose catastrophic budget (see the 10K-token test below for rationale).
      result.stats.durationMs < 500,
      `Duration ${result.stats.durationMs}ms should stay under the 500ms catastrophic budget`
    );
  });

  it("applyRulesToText should track applied rules", () => {
    const { text, appliedRules } = applyRulesToText(
      "Please help me",
      CAVEMAN_RULES.filter((r) => r.context === "all" || r.context === "user")
    );
    assert.ok(appliedRules.length > 0, "Should track applied rules");
    assert.ok(appliedRules.includes("polite_framing"), "polite_framing should be in applied rules");
  });

  it("should complete in under 5ms for 10K token messages", () => {
    const longContent = "Please help me analyze this code. ".repeat(1000);
    const body = { messages: [{ role: "user", content: longContent }] };
    const result = cavemanCompress(body, {
      enabled: true,
      compressRoles: ["user"],
      skipRules: [],
      minMessageLength: 50,
      preservePatterns: [],
    });
    // Catastrophic-regression budget, not a benchmark: under a saturated full
    // suite (concurrency 20) this measured 175ms on a healthy engine — absolute
    // wall-clock asserts flake under load (re-wired by 6A.1c, 2026-06-09).
    // Real perf tracking belongs in tests/benchmarks/.
    assert.ok(result.stats.durationMs < 500, `Expected <500ms, got ${result.stats.durationMs}ms`);
  });

  it("cleans whitespace and punctuation artifacts without regex backtracking", () => {
    const body = {
      messages: [
        {
          role: "user",
          content: "\n\nPlease\t make sure to keep this   stable   !!!   \n\n\n\nThank you.",
        },
      ],
    };
    const result = cavemanCompress(body, {
      enabled: true,
      compressRoles: ["user"],
      skipRules: [],
      minMessageLength: 0,
      preservePatterns: [],
    });
    const text = result.body.messages[0].content as string;

    assert.doesNotMatch(text, /^\n/);
    assert.doesNotMatch(text, /\n$/);
    assert.doesNotMatch(text, /\n\n\n/);
    assert.doesNotMatch(text, /[ \t]+[,.!?;:]/);
    assert.doesNotMatch(text, /[ \t]{2,}/);
  });
});