Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions config/quality/eslint-suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -4539,11 +4539,6 @@
"count": 1
}
},
"tests/unit/compression/result-memo.test.ts": {
"@typescript-eslint/no-unused-vars": {
"count": 1
}
},
"tests/unit/compression/rtk-grouping.test.ts": {
"@typescript-eslint/no-unused-vars": {
"count": 1
Expand Down
8 changes: 8 additions & 0 deletions open-sse/services/compression/resultMemo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { CompressionConfig, CompressionMode, CompressionResult } from "./ty
export const MEMO_CAP = 5_000;

const memoMap = new Map<string, CompressionResult>();
let lookupCountForTests = 0;

// Opt-IN whitelist (NOT opt-out): cache only engines proven pure + STATELESS across
// requests. Excluded on purpose: `ccr` and `session-dedup` write to the cross-request
Expand Down Expand Up @@ -94,6 +95,7 @@ function boundedSet(key: string, value: CompressionResult): void {
}

export function memoLookup(key: string): CompressionResult | null {
lookupCountForTests++;
const hit = memoMap.get(key);
if (!hit) return null;
// Return a clone so downstream mutation cannot corrupt the cached value.
Expand All @@ -110,4 +112,10 @@ export function memoStore(key: string, result: CompressionResult): void {
/** For tests only — clears the in-process memo store. */
export function clearMemoStore(): void {
memoMap.clear();
lookupCountForTests = 0;
}
export const resultMemoForTests = {
get lookupCount(): number {
return lookupCountForTests;
},
};
4 changes: 2 additions & 2 deletions open-sse/services/compression/strategySelector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ function runCompression(
config: { ...options.config, memoizeCompressionResults: false },
});
memoStore(key, result);
return memoLookup(key)!;
return result;
}
if (mode === "rtk") {
return applyRtkCompression(body, {
Expand Down Expand Up @@ -565,7 +565,7 @@ async function runCompressionAsync(
config: { ...options.config, memoizeCompressionResults: false },
});
memoStore(key, result);
return memoLookup(key)!;
return result;
}
// Single-mode omniglyph (async-only) — resolution lives in engines/omniglyphSingleMode.ts.
if (mode === "omniglyph") return applyOmniglyphSingleMode(body, options);
Expand Down
39 changes: 37 additions & 2 deletions tests/unit/compression/result-memo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import {
makeMemoKey,
isDeterministicMode,
clearMemoStore,
resultMemoForTests,
MEMO_CAP,
} from "../../../open-sse/services/compression/resultMemo.ts";
import type { CompressionResult } from "../../../open-sse/services/compression/types.ts";
import { DEFAULT_COMPRESSION_CONFIG } from "../../../open-sse/services/compression/types.ts";
import { applyCompression } from "../../../open-sse/services/compression/strategySelector.ts";
import {
applyCompression,
applyCompressionAsync,
} from "../../../open-sse/services/compression/strategySelector.ts";

const baseBody = {
messages: [{ role: "user", content: "hello world compress me please" }],
Expand Down Expand Up @@ -218,7 +222,6 @@ describe("applyCompression with memoization", () => {
});

it("flag OFF: two identical calls both compute (no caching path)", () => {
let callCount = 0;
// We can't easily spy on internal engine, so we verify via deterministic output
// equality between independent calls (proving cache isn't interfering).
// Use a body that will be lightly compressed.
Expand Down Expand Up @@ -257,6 +260,38 @@ describe("applyCompression with memoization", () => {
assert.notEqual(memoLookup(key), null);
});

it("memo misses and hits cannot mutate the cached result", () => {
const body = {
messages: [{ role: "user", content: "Mutation isolation test content. ".repeat(15) }],
model: "gpt-4",
};
const miss = applyCompression(body, "lite", { config: memoConfig, principalId: "u1" });
const expected = structuredClone(miss.body);
miss.body.messages[0]!.content = "mutated miss";

const hit = applyCompression(body, "lite", { config: memoConfig, principalId: "u1" });
assert.deepEqual(hit.body, expected);
hit.body.messages[0]!.content = "mutated hit";

assert.deepEqual(
applyCompression(body, "lite", { config: memoConfig, principalId: "u1" }).body,
expected
);
assert.equal(resultMemoForTests.lookupCount, 3);
});

it("async memo misses and hits perform one lookup per call", async () => {
const body = {
messages: [{ role: "user", content: "Async lookup count test content. ".repeat(15) }],
model: "gpt-4",
};

await applyCompressionAsync(body, "lite", { config: memoConfig, principalId: "u1" });
assert.equal(resultMemoForTests.lookupCount, 1);
await applyCompressionAsync(body, "lite", { config: memoConfig, principalId: "u1" });
assert.equal(resultMemoForTests.lookupCount, 2);
});

it("flag ON + deterministic mode: different principalId = MISS", () => {
const body = {
messages: [{ role: "user", content: "Cross-principal test content. ".repeat(10) }],
Expand Down
Loading