Files
OmniRoute/tests/unit/compression/body-adapter.test.ts
Jan Leon 32f8340719 Fix custom tool output pairing during context compression (#8933)
* Fix custom tool output pairing during compression (#8932)

* Bypass proxy compaction for native Codex context

* fix(sse): extract Codex tool-call output repair to leaf module for file-size gate

repairMissingCodexToolCallOutputs (added by #8932 for custom_tool_call
pairing) pushed codex.ts past the frozen file-size baseline. Extract it
to open-sse/executors/codex/toolCallRepair.ts, leaving only the wiring
call in codex.ts. Rebaseline the test file's genuine +41 line growth
from #8932's new custom_tool_call_output coverage.

Co-authored-by: JxnLexn <JxnLexn@users.noreply.github.com>

---------

Co-authored-by: diegosouzapw <diegosouzapw@users.noreply.github.com>
Co-authored-by: JxnLexn <JxnLexn@users.noreply.github.com>
2026-08-11 04:25:55 -03:00

322 lines
9.9 KiB
TypeScript

import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { applyCompression } from "../../../open-sse/services/compression/strategySelector.ts";
import { adaptBodyForCompression } from "../../../open-sse/services/compression/bodyAdapter.ts";
import { applyRtkCompression } from "../../../open-sse/services/compression/engines/rtk/index.ts";
describe("compression body adapter", () => {
it("drops a custom tool call when compaction removes its mapped output (#8932)", () => {
const body = {
input: [
{
type: "custom_tool_call",
call_id: "call_patch_1",
name: "apply_patch",
input: "*** Begin Patch",
},
{
type: "custom_tool_call_output",
call_id: "call_patch_1",
output: "Done!",
},
{
type: "message",
role: "user",
content: [{ type: "input_text", text: "Continue." }],
},
],
};
const adapter = adaptBodyForCompression(body);
const compressedMessages = (adapter.body.messages as Array<Record<string, unknown>>).filter(
(message) => message.role !== "tool"
);
const restored = adapter.restore(
{ ...adapter.body, messages: compressedMessages },
{ dropMissingMappedItems: true }
);
const input = restored.input as Array<Record<string, unknown>>;
assert.equal(
input.some((item) => item.type === "custom_tool_call"),
false
);
assert.equal(
input.some((item) => item.type === "custom_tool_call_output"),
false
);
assert.equal(
input.some((item) => item.type === "message"),
true
);
});
it("applies Caveman compression to OpenAI Responses input messages", () => {
const body = {
model: "gpt-5.5-codex",
input: [
{
type: "message",
role: "user",
content: [
{
type: "input_text",
text: "Please could you provide a detailed explanation of this implementation? Thank you so much for your help!",
},
],
},
{
type: "function_call",
call_id: "call_1",
name: "read_file",
arguments: "{}",
},
],
};
const result = applyCompression(body, "standard", {
config: {
enabled: true,
defaultMode: "standard",
autoTriggerTokens: 0,
cacheMinutes: 5,
preserveSystemPrompt: true,
comboOverrides: {},
cavemanConfig: {
enabled: true,
compressRoles: ["user"],
skipRules: [],
minMessageLength: 10,
preservePatterns: [],
intensity: "full",
},
},
});
assert.equal(result.compressed, true);
assert.ok(!("messages" in result.body), "Responses body must not leak synthetic messages");
const input = result.body.input as typeof body.input;
assert.equal(input[1], body.input[1], "non-message Responses items should be preserved");
const text = input[0].content[0].text;
assert.ok(!text.includes("Please could you"));
assert.ok(!text.includes("Thank you so much"));
assert.ok(text.includes("explain"));
});
it("applies RTK compression to Responses function_call_output items", () => {
const repeatedOutput = Array.from({ length: 20 }, () => "same noisy line").join("\n");
const body = {
input: [
{
type: "function_call_output",
call_id: "call_1",
output: repeatedOutput,
},
],
};
const result = applyRtkCompression(body);
assert.equal(result.compressed, true);
assert.ok(!("messages" in result.body), "Responses body must not leak synthetic messages");
const input = result.body.input as typeof body.input;
assert.match(input[0].output, /\[rtk:dropped/);
assert.equal(input[0].call_id, "call_1");
});
it("applies RTK compression to Codex custom_tool_call_output items", () => {
const repeatedOutput = Array.from({ length: 20 }, () => "same noisy line").join("\n");
const body = {
input: [
{
type: "custom_tool_call_output",
call_id: "call_patch_1",
output: repeatedOutput,
},
],
};
const result = applyRtkCompression(body);
assert.equal(result.compressed, true);
assert.ok(!("messages" in result.body), "Responses body must not leak synthetic messages");
const input = result.body.input as typeof body.input;
assert.equal(input[0].type, "custom_tool_call_output");
assert.equal(input[0].call_id, "call_patch_1");
assert.match(input[0].output, /\[rtk:dropped/);
});
it("preserves wrapped custom tool output metadata while compressing its output", () => {
const repeatedOutput = Array.from({ length: 20 }, () => "same noisy line").join("\n");
const body = {
input: [
{
type: "custom_tool_call_output",
call_id: "call_exec_1",
output: JSON.stringify({ output: repeatedOutput, metadata: { exitCode: 0 } }),
},
],
};
const result = applyRtkCompression(body);
const input = result.body.input as typeof body.input;
const restoredOutput = JSON.parse(input[0].output) as {
output: string;
metadata: { exitCode: number };
};
assert.equal(result.compressed, true);
assert.match(restoredOutput.output, /\[rtk:dropped/);
assert.deepEqual(restoredOutput.metadata, { exitCode: 0 });
});
it("restores custom tool output to content when that was the source field", () => {
const repeatedOutput = Array.from({ length: 20 }, () => "same noisy line").join("\n");
const body = {
input: [
{
type: "custom_tool_call_output",
call_id: "call_exec_2",
content: repeatedOutput,
},
],
};
const result = applyRtkCompression(body);
const input = result.body.input as Array<Record<string, unknown>>;
assert.equal(result.compressed, true);
assert.match(input[0].content as string, /\[rtk:dropped/);
assert.ok(!("output" in input[0]), "restore must not add a conflicting output field");
});
it("restores function call output to content when that was the source field", () => {
const repeatedOutput = Array.from({ length: 20 }, () => "same noisy line").join("\n");
const body = {
input: [
{
type: "function_call_output",
call_id: "call_2",
content: repeatedOutput,
},
],
};
const result = applyRtkCompression(body);
const input = result.body.input as Array<Record<string, unknown>>;
assert.equal(result.compressed, true);
assert.match(input[0].content as string, /\[rtk:dropped/);
assert.ok(!("output" in input[0]), "restore must not add a conflicting output field");
});
it("restores compressed array output on Responses function_call_output items", () => {
const repeatedOutput = Array.from({ length: 20 }, () => "same noisy line").join("\n");
const body = {
input: [
{
type: "function_call_output",
call_id: "call_1",
output: [{ type: "input_text", text: repeatedOutput }],
},
],
};
const result = applyRtkCompression(body);
const input = result.body.input as typeof body.input;
assert.equal(result.compressed, true);
assert.match(input[0].output[0].text, /\[rtk:dropped/);
assert.ok(!("content" in input[0]), "function_call_output should keep canonical output field");
});
it("restores adapted Responses bodies even when no compression is applied", () => {
const body = {
input: [
{
type: "message",
role: "user",
content: [{ type: "input_text", text: "short" }],
},
],
};
const result = applyCompression(body, "standard", {
config: {
enabled: true,
defaultMode: "standard",
autoTriggerTokens: 0,
cacheMinutes: 5,
preserveSystemPrompt: true,
comboOverrides: {},
cavemanConfig: {
enabled: true,
compressRoles: ["user"],
skipRules: [],
minMessageLength: 50,
preservePatterns: [],
intensity: "full",
},
},
});
assert.equal(result.compressed, false);
assert.ok(!("messages" in result.body));
assert.deepEqual(result.body.input, body.input);
});
it("does not misalign Responses input items if an engine removes a synthetic message", () => {
const body = {
input: [
{ type: "message", role: "user", content: "duplicate" },
{ type: "message", role: "user", content: "duplicate" },
{ type: "message", role: "user", content: "unique" },
],
};
const result = applyCompression(body, "lite", {
config: {
enabled: true,
defaultMode: "lite",
autoTriggerTokens: 0,
cacheMinutes: 5,
preserveSystemPrompt: true,
comboOverrides: {},
},
});
assert.equal(result.compressed, true);
assert.deepEqual(result.body.input, body.input);
});
it("compresses string Responses input without converting the request shape", () => {
const body = {
input:
"Please could you provide a detailed explanation of this implementation? Thank you so much for your help!",
};
const result = applyCompression(body, "standard", {
config: {
enabled: true,
defaultMode: "standard",
autoTriggerTokens: 0,
cacheMinutes: 5,
preserveSystemPrompt: true,
comboOverrides: {},
cavemanConfig: {
enabled: true,
compressRoles: ["user"],
skipRules: [],
minMessageLength: 10,
preservePatterns: [],
intensity: "full",
},
},
});
assert.equal(result.compressed, true);
assert.equal(typeof result.body.input, "string");
assert.ok(!(result.body.input as string).includes("Please could you"));
});
});