mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-11-20 08:20:12 +00:00
fix(sse): keep tool-name maps across post-translation compression (#19029)
Evidence: combined board on release/v3.8.54 8aaffff52c: 76 node:test cases passed across the lot's test files; eslint with project suppressions, typecheck:core and open-sse typecheck clean; docs-counts, file-size and changelog integrity OK.
Closes #18979
Closes #18979.
This commit is contained in:
@@ -0,0 +1 @@
|
||||
- **fix(sse):** OmniGlyph request compression no longer drops the internal tool-name maps, so namespaced MCP tools (`mcp__server__tool`) and sanitized tool names keep round-tripping correctly on compressed requests. ([#19029](https://github.com/cheaper-inference/OmniRoute/pull/19029))
|
||||
@@ -51,7 +51,16 @@ export async function applyPostTranslationCompression<TBody>(
|
||||
}
|
||||
): Promise<void> {
|
||||
const transientFields = new Map<string, unknown>();
|
||||
const postInput = { ...(state.translatedBody as Record<string, unknown>) };
|
||||
const source = state.translatedBody as Record<string, unknown>;
|
||||
// Tool-name ledgers (_namespaceToolIdentityMap #9780, _toolNameMap) are attached
|
||||
// NON-enumerable so they never reach the JSON wire body — the spread below copies
|
||||
// enumerable keys only, so both the compressor input and the restore loop miss
|
||||
// them. Collect them here by name and re-attach them non-enumerably after the
|
||||
// transform, or a successful compression silently drops tool-name restore.
|
||||
const sideChannels = Object.getOwnPropertyNames(source).filter(
|
||||
(key) => key.startsWith("_") && source[key] instanceof Map
|
||||
);
|
||||
const postInput = { ...source };
|
||||
for (const [key, value] of Object.entries(postInput)) {
|
||||
// Translators keep response-side aliases in Maps under private keys. They
|
||||
// are not JSON request fields and would otherwise be stringified to `{}`
|
||||
@@ -72,6 +81,14 @@ export async function applyPostTranslationCompression<TBody>(
|
||||
...(postResult.body as TBody),
|
||||
...Object.fromEntries(transientFields),
|
||||
};
|
||||
for (const key of sideChannels) {
|
||||
Object.defineProperty(state.translatedBody, key, {
|
||||
value: source[key],
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
}
|
||||
// Same arithmetic as the inline `tokensCompressed += …`: a null binding coerces to 0.
|
||||
state.tokensCompressed =
|
||||
(state.tokensCompressed as number) +
|
||||
|
||||
111
tests/unit/compression-keeps-tool-name-maps.test.ts
Normal file
111
tests/unit/compression-keeps-tool-name-maps.test.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
// The translators attach response-side tool-name ledgers (`_namespaceToolIdentityMap`,
|
||||
// `_toolNameMap`) as NON-enumerable properties (translator/index.ts #9780, executors/base.ts).
|
||||
// The post-translation compression stage rebuilds the body with a spread, which only
|
||||
// copies enumerable keys, and its `_`-key restore loop iterates Object.entries — so a
|
||||
// successful compression silently dropped both maps. Namespaced tool calls then came
|
||||
// back flattened (`mcp__srv__tool`) and sanitized tool names were never restored.
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
const { applyPostTranslationCompression } =
|
||||
await import("../../open-sse/handlers/chatCore/postTranslationCompression.ts");
|
||||
|
||||
const identity = () => new Map([["mcp__srv__run", { namespace: "mcp__srv", name: "run" }]]);
|
||||
|
||||
function bodyWithSideChannels() {
|
||||
const body: Record<string, unknown> = {
|
||||
model: "m",
|
||||
messages: [{ role: "user", content: "hi" }],
|
||||
};
|
||||
// Exactly how openai-responses.ts and translator/index.ts attach them.
|
||||
for (const key of ["_namespaceToolIdentityMap", "_toolNameMap"]) {
|
||||
Object.defineProperty(body, key, {
|
||||
value: identity(),
|
||||
enumerable: false,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
});
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
function options(override: Partial<Parameters<typeof applyPostTranslationCompression>[1]> = {}) {
|
||||
return {
|
||||
runPostTranslationCompression: async (input: Record<string, unknown>) =>
|
||||
({
|
||||
compressed: true,
|
||||
body: { ...input, messages: [{ role: "user", content: "h" }] },
|
||||
stats: null,
|
||||
}) as never,
|
||||
provider: "openai-compatible-x",
|
||||
effectiveModel: "m",
|
||||
effectiveServiceTier: undefined,
|
||||
comboName: null,
|
||||
skillRequestId: "r",
|
||||
sourceFormat: "openai-responses",
|
||||
targetFormat: "openai",
|
||||
log: null,
|
||||
...override,
|
||||
} as Parameters<typeof applyPostTranslationCompression>[1];
|
||||
}
|
||||
|
||||
test("a successful compression keeps the non-enumerable tool-name side channels", async () => {
|
||||
const state = {
|
||||
translatedBody: bodyWithSideChannels(),
|
||||
tokensCompressed: 0,
|
||||
compressionResponseMeta: null,
|
||||
compressionAnalyticsWritePromise: null,
|
||||
};
|
||||
await applyPostTranslationCompression(state, options());
|
||||
const out = state.translatedBody as Record<string, unknown>;
|
||||
assert.ok(out._namespaceToolIdentityMap instanceof Map, "namespace identity map lost");
|
||||
assert.ok(out._toolNameMap instanceof Map, "tool name map lost");
|
||||
// Non-enumerable on the way in, non-enumerable on the way out: they must never
|
||||
// reach the JSON wire body (the OmniGlyph wrapper would stringify them to {}).
|
||||
for (const key of ["_namespaceToolIdentityMap", "_toolNameMap"]) {
|
||||
assert.equal(
|
||||
Object.getOwnPropertyDescriptor(out, key)?.enumerable,
|
||||
false,
|
||||
`${key} restored as enumerable`
|
||||
);
|
||||
assert.equal(JSON.stringify(out).includes(key), false, `${key} leaked into JSON body`);
|
||||
}
|
||||
});
|
||||
|
||||
test("the compressor never receives the side channels", async () => {
|
||||
const seen: string[] = [];
|
||||
const state = {
|
||||
translatedBody: bodyWithSideChannels(),
|
||||
tokensCompressed: 0,
|
||||
compressionResponseMeta: null,
|
||||
compressionAnalyticsWritePromise: null,
|
||||
};
|
||||
await applyPostTranslationCompression(
|
||||
state,
|
||||
options({
|
||||
runPostTranslationCompression: async (input: Record<string, unknown>) => {
|
||||
seen.push(...Object.keys(input));
|
||||
seen.push(...Object.getOwnPropertyNames(input));
|
||||
return { compressed: false } as never;
|
||||
},
|
||||
})
|
||||
);
|
||||
assert.equal(seen.includes("_namespaceToolIdentityMap"), false);
|
||||
assert.equal(seen.includes("_toolNameMap"), false);
|
||||
});
|
||||
|
||||
test("an uncompressed result leaves the body object untouched", async () => {
|
||||
const body = bodyWithSideChannels();
|
||||
const state = {
|
||||
translatedBody: body,
|
||||
tokensCompressed: 0,
|
||||
compressionResponseMeta: null,
|
||||
compressionAnalyticsWritePromise: null,
|
||||
};
|
||||
await applyPostTranslationCompression(
|
||||
state,
|
||||
options({ runPostTranslationCompression: async () => ({ compressed: false }) as never })
|
||||
);
|
||||
assert.equal(state.translatedBody, body);
|
||||
assert.ok((body as Record<string, unknown>)._toolNameMap instanceof Map);
|
||||
});
|
||||
Reference in New Issue
Block a user