Files
OmniRoute/tests/unit/request-log-payloads.test.ts
2026-08-27 02:09:24 -03:00

1013 lines
36 KiB
TypeScript

import { protectPipelinePayloads } from "../../src/lib/usage/callLogs/format.ts";
import test from "node:test";
import assert from "node:assert/strict";
const {
normalizePayloadForLog,
protectPayloadForLog,
serializePayloadForStorage,
parseStoredPayload,
} = await import("../../src/lib/logPayloads.ts");
const {
createStructuredSSECollector,
buildStreamSummaryFromEvents,
compactStructuredStreamPayload,
} = await import("../../open-sse/utils/streamPayloadCollector.ts");
const { cloneBoundedForLog, createRequestLogger } =
await import("../../open-sse/utils/requestLogger.ts");
const { cloneBoundedChatLogPayload } =
await import("../../open-sse/handlers/chatCore/logTruncation.ts");
const {
containsVideoTranscriptForLog,
extractVideoTranscriptDescriptionFingerprints,
fingerprintVideoTranscriptDescription,
omitVideoTranscriptForLog,
omitVideoTranscriptFromLogString,
resolveVideoTranscriptLogSensitivity,
VIDEO_TRANSCRIPT_LOG_OMISSION_MARKER,
} = await import("../../src/lib/guardrails/videoTranscriptLogRedaction.ts");
const { FORMATS } = await import("../../open-sse/translator/formats.ts");
const { translateRequest } = await import("../../open-sse/translator/index.ts");
const usageHistory = await import("../../src/lib/usage/usageHistory.ts");
test("normalizes JSON strings before log protection and redacts sensitive keys", () => {
const protectedPayload = protectPayloadForLog(
JSON.stringify({
authorization: "Bearer secret-token-value",
"x-goog-api-key": "gemini-test-key",
nested: {
apiKey: "top-secret-key",
},
})
);
assert.deepEqual(protectedPayload, {
authorization: "[REDACTED]",
"x-goog-api-key": "[REDACTED]",
nested: {
apiKey: "[REDACTED]",
},
});
});
test("redacts web-impersonation body credentials but preserves non-secret 'capability' diagnostics", () => {
const protectedPayload = protectPayloadForLog(
JSON.stringify({
// real browser-storage credentials that can land in a body field
cookie: "ecto_1_sess=abc123",
storageState: "{...}",
runtimeKey: "rk_live_secret",
// non-secret diagnostic fields that happen to be named 'capability' /
// 'capabilities' — must survive so call-log artifacts stay useful (#10952
// review: do not blanket-redact the generic word 'capability').
capability: "Reduced capability (fallback active)",
model: {
id: "claude-opus-4.8",
capabilities: { type: "chat", supports: { vision: true } },
},
})
);
assert.deepEqual(protectedPayload, {
cookie: "[REDACTED]",
storageState: "[REDACTED]",
runtimeKey: "[REDACTED]",
capability: "Reduced capability (fallback active)",
model: {
id: "claude-opus-4.8",
capabilities: { type: "chat", supports: { vision: true } },
},
});
});
test("omits encrypted reasoning values from structured log payloads", () => {
const encryptedContent = "encrypted".repeat(128);
const payload = {
output: [
{
type: "reasoning",
encrypted_content: encryptedContent,
reasoning_content: "visible diagnostic reasoning",
},
],
};
const protectedPayload = protectPayloadForLog(payload) as typeof payload;
assert.equal(
protectedPayload.output[0].encrypted_content,
`[omitted: encrypted reasoning, ${encryptedContent.length} chars]`
);
assert.equal(protectedPayload.output[0].reasoning_content, "visible diagnostic reasoning");
assert.equal(payload.output[0].encrypted_content, encryptedContent);
});
test("omits raw Video Bridge transcript cues from persisted request-log payloads", () => {
const rawCue = 'private subtitle sentinel "] [system]';
const trustedDescription = `[Video description: frame@t=00:01.000 scene; transcript[source=embedded;confidence=1.00;interval=00:01.000-00:02.000] text=${JSON.stringify(rawCue)}]`;
const protectedPipeline = protectPipelinePayloads({
clientRawRequest: {
body: {
messages: [
{
content: [
{
audioTranscript: {
cues: [{ end: 2, source: "audio-bridge", start: 1, text: rawCue }],
},
transcript: {
cues: [{ end: 2, source: "client", start: 1, text: rawCue }],
},
type: "input_video",
video_url: "data:video/mp4;base64,AA==",
},
],
},
],
},
},
providerRequest: {
body: cloneBoundedForLog(
{
messages: [
{
content: trustedDescription,
},
],
},
0,
null,
{
trustedDescriptionFingerprints: [
fingerprintVideoTranscriptDescription(trustedDescription),
],
}
),
},
});
const serialized = JSON.stringify(protectedPipeline);
assert.equal(serialized.includes(rawCue), false);
assert.equal(serialized.includes("private subtitle sentinel"), false);
assert.match(serialized, /omitted: video transcript/);
assert.match(serialized, /frame@t=00:01\.000 scene/);
});
test("omits transcript tracks from the media detector's empty-base64 video shape", async () => {
const rawCue = "private empty-base64 subtitle sentinel";
const payload = {
input: [
{
role: "user",
content: [
{
source: {
data: "",
media_type: "video/mp4",
transcript: {
cues: [{ end: 2, source: "client", start: 1, text: rawCue }],
},
type: "base64",
},
type: "video",
},
],
},
],
};
const protectedPayload = protectPayloadForLog(payload);
for (const redacted of [
protectedPayload,
cloneBoundedForLog(payload),
cloneBoundedChatLogPayload(payload),
]) {
const serialized = JSON.stringify(redacted);
assert.equal(serialized.includes(rawCue), false);
assert.match(serialized, /omitted: video transcript/);
}
const logger = await createRequestLogger(undefined, undefined, undefined, {
captureStreamChunks: true,
enabled: true,
});
logger.appendProviderChunk(`data: ${rawCue}\n\n`);
logger.logClientRawRequest("/v1/responses", payload);
logger.appendConvertedChunk(`data: ${rawCue}\n\n`);
const pipeline = logger.getPipelinePayloads();
assert.equal(JSON.stringify(pipeline).includes(rawCue), false);
assert.equal(pipeline?.streamChunks, undefined);
});
test("omits Video Bridge cues before lossy request-log string truncation", () => {
const rawCue = "private boundary subtitle sentinel";
const payload = (targetCue: number, visualPadding: number) => {
const cueText = (index: number): string =>
index === targetCue
? rawCue + "x".repeat(4_096 - rawCue.length)
: String(index).padStart(2, "0") + "x".repeat(4_094);
const description = `[Video description: ${Array.from(
{ length: 16 },
(_unused, index) =>
`transcript[source=client;confidence=1.00;interval=00:${String(index).padStart(2, "0")}.000-00:${String(index + 1).padStart(2, "0")}.000] text=${JSON.stringify(cueText(index))}`
).join("; ")}; frame-tail=${"v".repeat(visualPadding)}]`;
return {
context: {
trustedDescriptionFingerprints: [fingerprintVideoTranscriptDescription(description)],
},
value: { input: [{ content: description, role: "user" }] },
};
};
const requestLogNine = payload(9, 3_507);
const requestLogTen = payload(10, 7_710);
const chatLogNine = payload(9, 3_522);
const alternateChatLogNine = payload(9, 3_598);
const boundedPayloads = [
cloneBoundedForLog(requestLogNine.value, 0, null, requestLogNine.context),
cloneBoundedForLog(requestLogTen.value, 0, null, requestLogTen.context),
cloneBoundedChatLogPayload(chatLogNine.value, 0, chatLogNine.context),
cloneBoundedChatLogPayload(alternateChatLogNine.value, 0, alternateChatLogNine.context),
];
for (const bounded of boundedPayloads) {
const serialized = JSON.stringify(protectPayloadForLog(bounded));
assert.equal(serialized.includes(rawCue), false);
assert.match(serialized, /omitted: video transcript/);
}
});
test("suppresses bounded stream-chunk logs for a request carrying Video Bridge cues", async () => {
const rawCue = "private streamed subtitle sentinel";
const description =
`[Video description: transcript[source=embedded;confidence=1.00;` +
`interval=00:01.000-00:02.000] text=${JSON.stringify(rawCue)}]`;
const logger = await createRequestLogger(undefined, undefined, undefined, {
captureStreamChunks: true,
enabled: true,
maxStreamChunkBytes: 64,
videoTranscriptDescriptionFingerprints: [fingerprintVideoTranscriptDescription(description)],
videoTranscriptSensitive: true,
});
logger.appendProviderChunk(`data: ${rawCue}\n\n`);
logger.logTargetRequest(
"https://provider.invalid",
{},
{
input: [
{
content: description,
role: "user",
},
],
}
);
logger.appendProviderChunk(`data: ${rawCue}\n\n`);
const protectedPipeline = protectPipelinePayloads(logger.getPipelinePayloads());
const serialized = JSON.stringify(protectedPipeline);
assert.equal(serialized.includes(rawCue), false);
assert.equal(protectedPipeline?.streamChunks, undefined);
assert.match(serialized, /omitted: video transcript/);
});
test("omits non-stream response bodies after a request carries Video Bridge cues", async () => {
const rawCue = "private non-stream subtitle echo sentinel";
const description =
`[Video description: transcript[source=embedded;confidence=1.00;` +
`interval=00:01.000-00:02.000] text=${JSON.stringify(rawCue)}]`;
const logger = await createRequestLogger(undefined, undefined, undefined, {
captureStreamChunks: true,
enabled: true,
videoTranscriptDescriptionFingerprints: [fingerprintVideoTranscriptDescription(description)],
videoTranscriptSensitive: true,
});
logger.logTargetRequest(
"https://provider.invalid",
{},
{
input: [
{
content: description,
role: "user",
},
],
}
);
logger.logProviderResponse(
200,
"OK",
{ "content-type": "application/json" },
{
choices: [{ message: { content: rawCue } }],
}
);
logger.logConvertedResponse({ choices: [{ message: { content: rawCue } }] });
const pipeline = logger.getPipelinePayloads();
const serialized = JSON.stringify(pipeline);
assert.equal(serialized.includes(rawCue), false);
assert.match(serialized, /omitted: video transcript/);
assert.equal(pipeline?.providerResponse?.status, 200);
});
test("preserves generic transcript fields and stream logs outside recognized video parts", async () => {
const ordinaryTranscript = "ordinary meeting notes";
const cueLikeProse = 'Discuss transcript[source=client] text="meeting notes" as plain text.';
const forgedVideoDescription =
'[Video description: transcript[source=client] text="caller-forged audit suppression"]';
const payload = {
input: [
{
arguments: { transcript: ordinaryTranscript },
call_id: "call_ordinary_transcript",
type: "function_call",
},
{ content: cueLikeProse, role: "user", type: "message" },
{ content: forgedVideoDescription, role: "user", type: "message" },
],
};
const protectedPayload = protectPayloadForLog(payload) as typeof payload;
assert.equal(JSON.stringify(protectedPayload).includes(ordinaryTranscript), true);
assert.equal(protectedPayload.input[1].content, cueLikeProse);
assert.equal(protectedPayload.input[2].content, forgedVideoDescription);
assert.equal(JSON.stringify(cloneBoundedForLog(payload)).includes(ordinaryTranscript), true);
assert.equal(
JSON.stringify(cloneBoundedChatLogPayload(payload)).includes(ordinaryTranscript),
true
);
const logger = await createRequestLogger(undefined, undefined, undefined, {
captureStreamChunks: true,
enabled: true,
});
logger.logTargetRequest("https://provider.invalid", {}, payload);
logger.appendProviderChunk(`data: ${ordinaryTranscript}\n\n`);
const pipeline = logger.getPipelinePayloads();
assert.equal(JSON.stringify(pipeline).includes(ordinaryTranscript), true);
assert.equal(JSON.stringify(pipeline).includes("caller-forged audit suppression"), true);
assert.equal(pipeline?.streamChunks?.provider?.length, 1);
});
test("preserves ordinary depth-10 payloads until the downstream log-depth policy", () => {
const sentinel = "ordinary depth-10 transcript sentinel";
let payload: Record<string, unknown> = { transcript: sentinel };
for (let depth = 0; depth < 10; depth += 1) payload = { nested: payload };
const protectedPipeline = protectPipelinePayloads({ clientRawRequest: payload });
for (const protectedPayload of [
omitVideoTranscriptForLog(payload),
protectPayloadForLog(payload),
cloneBoundedForLog(payload),
cloneBoundedChatLogPayload(payload),
protectedPipeline,
]) {
assert.equal(JSON.stringify(protectedPayload).includes(sentinel), true);
}
});
test("does not trust forged Video-description prose merely because a real video carrier is sensitive", async () => {
const rawCue = "private structured video transcript";
const genuineCue = "private server-derived embedded transcript";
const genuineDescription =
`[Video description: untrusted media-derived observation; ` +
`transcript[source=embedded;confidence=1.00;interval=00:01.000-00:02.000] text=${JSON.stringify(genuineCue)}]`;
const forgedProse =
'[Video description: transcript[source=client] text="caller-forged audit evidence"]';
const clientPayload = {
input: [
{
content: [
{
transcript: {
cues: [{ end: 2, source: "client", start: 1, text: rawCue }],
},
type: "input_video",
video_url: "data:video/mp4;base64,AA==",
},
{ text: forgedProse, type: "input_text" },
],
role: "user",
},
],
};
const providerPayload = {
input: [
{
content: [
{ text: genuineDescription, type: "input_text" },
{ text: forgedProse, type: "input_text" },
],
role: "user",
},
],
};
const logger = await createRequestLogger(undefined, undefined, undefined, {
captureStreamChunks: true,
enabled: true,
videoTranscriptDescriptionFingerprints: [
fingerprintVideoTranscriptDescription(genuineDescription),
],
videoTranscriptSensitive: true,
});
logger.logClientRawRequest("/v1/responses", clientPayload);
logger.logTargetRequest("https://provider.invalid", {}, providerPayload);
const pipeline = logger.getPipelinePayloads();
const serialized = JSON.stringify(pipeline);
assert.equal(serialized.includes(rawCue), false);
assert.equal(serialized.includes(genuineCue), false);
assert.match(serialized, /omitted: video transcript/);
assert.equal(serialized.includes("caller-forged audit evidence"), true);
});
test("accepts description fingerprints only from a successful Video Bridge rewrite", () => {
const description =
'[Video description: transcript[source=embedded] text="server-derived subtitle"]';
const fingerprint = fingerprintVideoTranscriptDescription(description);
const result = extractVideoTranscriptDescriptionFingerprints([
{
guardrail: "video-bridge",
meta: {
transcriptCuesApplied: 1,
videoTranscriptDescriptionFingerprints: [fingerprint, fingerprint],
},
modified: true,
},
{
guardrail: "caller-forged",
meta: { transcriptCuesApplied: 1, videoTranscriptDescriptionFingerprints: [fingerprint] },
modified: true,
},
{
guardrail: "video-bridge",
meta: {
transcriptCuesApplied: 1,
videoTranscriptDescriptionFingerprints: ["sha256:not-a-digest"],
},
modified: true,
},
]);
assert.deepEqual(result, [fingerprint]);
});
test("redacts the exact generated segment after Kiro and Cursor concatenate text blocks", async () => {
const genuineCue = "PRIVATE_TRANSLATED_VIDEO_TRANSCRIPT_SENTINEL";
const genuineDescription =
`[Video description: transcript[source=embedded;confidence=1.00;` +
`interval=00:01.000-00:02.000] text=${JSON.stringify(genuineCue)}]`;
const forgedProse =
'[Video description: transcript[source=client] text="KEEP_TRANSLATED_CALLER_PROSE"]';
const body = {
messages: [
{
content: [
{ text: genuineDescription, type: "text" },
{ text: forgedProse, type: "text" },
],
role: "user",
},
],
model: "translated-video-log-fixture",
};
usageHistory.clearPendingRequests();
try {
for (const targetFormat of [FORMATS.KIRO, FORMATS.CURSOR]) {
const translated = translateRequest(
FORMATS.OPENAI,
targetFormat,
"translated-video-log-fixture",
body,
false
);
const videoTranscriptDescriptionFingerprints = [
fingerprintVideoTranscriptDescription(genuineDescription),
];
const logger = await createRequestLogger(FORMATS.OPENAI, targetFormat, undefined, {
enabled: true,
videoTranscriptDescriptionFingerprints,
videoTranscriptSensitive: true,
});
logger.logTargetRequest("https://provider.invalid", {}, translated);
const serialized = JSON.stringify(logger.getPipelinePayloads());
assert.equal(serialized.includes(genuineCue), false, targetFormat);
assert.match(serialized, /omitted: video transcript/, targetFormat);
assert.equal(serialized.includes("KEEP_TRANSLATED_CALLER_PROSE"), true, targetFormat);
const requestId = usageHistory.trackPendingRequest(
"translated-video-log-fixture",
targetFormat,
`connection-${targetFormat}`,
true,
{
providerRequest: translated,
videoTranscriptDescriptionFingerprints,
videoTranscriptSensitive: true,
}
);
const pending = JSON.stringify(usageHistory.getPendingById().get(requestId!));
assert.equal(pending.includes(genuineCue), false, targetFormat);
assert.match(pending, /omitted: video transcript/, targetFormat);
assert.equal(pending.includes("KEEP_TRANSLATED_CALLER_PROSE"), true, targetFormat);
}
} finally {
usageHistory.clearPendingRequests();
}
});
test("fails closed after a bounded number of forged description-prefix candidates", () => {
const genuineDescription =
'[Video description: transcript[source=embedded] text="bounded genuine subtitle"]';
const prefixFlood = Array.from(
{ length: 129 },
(_unused, index) => `[Video description: forged-prefix-${index}]`
).join("");
const omitted = omitVideoTranscriptForLog(
{ content: prefixFlood + genuineDescription },
{
trustedDescriptionFingerprints: [fingerprintVideoTranscriptDescription(genuineDescription)],
}
) as { content: string };
assert.equal(omitted.content, VIDEO_TRANSCRIPT_LOG_OMISSION_MARKER);
});
test("fails closed after a bounded number of trusted-description candidate hashes", () => {
const genuineDescription =
'[Video description: transcript[source=embedded] text="bounded hash subtitle"]';
const forgedPrefixes = Array.from(
{ length: 9 },
(_unused, index) => `[Video description: candidate-${index}]`
).join("");
const trustedDescriptionFingerprints = [
...Array.from({ length: 63 }, (_unused, index) =>
fingerprintVideoTranscriptDescription(`[Video description: identity-${index}]`)
),
fingerprintVideoTranscriptDescription(genuineDescription),
];
const omitted = omitVideoTranscriptForLog(
{ content: forgedPrefixes + genuineDescription },
{ trustedDescriptionFingerprints }
) as { content: string };
assert.equal(omitted.content, VIDEO_TRANSCRIPT_LOG_OMISSION_MARKER);
});
test("trusted malformed or over-budget serialized cues fail closed", () => {
const privateTranscript = "PRIVATE_MALFORMED_SERIALIZED_CUE_SENTINEL";
const malformedDescriptions = [
`[Video description: transcript[source=embedded${";metadata=x".repeat(100)}] text=${JSON.stringify(privateTranscript)}]`,
`[Video description: transcript[source=embedded text=${JSON.stringify(privateTranscript)}]`,
`[Video description: transcript[source=embedded]text=${JSON.stringify(privateTranscript)}]`,
`[Video description: transcript[source=embedded] payload=${JSON.stringify(privateTranscript)}]`,
`[Video description: transcript[source=embedded] text=${JSON.stringify("safe cue")}; transcript[source=audio-bridge${";metadata=x".repeat(100)}] text=${JSON.stringify(privateTranscript)}]`,
];
for (const description of malformedDescriptions) {
const context = {
trustedDescriptionFingerprints: [fingerprintVideoTranscriptDescription(description)],
};
const direct = omitVideoTranscriptFromLogString(description, context);
const structured = omitVideoTranscriptForLog({ content: description }, context) as {
content: string;
};
assert.equal(direct, VIDEO_TRANSCRIPT_LOG_OMISSION_MARKER, description);
assert.equal(structured.content, VIDEO_TRANSCRIPT_LOG_OMISSION_MARKER, description);
assert.equal(direct.includes(privateTranscript), false, description);
assert.equal(structured.content.includes(privateTranscript), false, description);
}
});
test("bounds cyclic objects and fails closed when the transcript-security depth is exceeded", () => {
const cyclic: Record<string, unknown> = { transcript: "ordinary cyclic notes" };
cyclic.self = cyclic;
let deep: Record<string, unknown> = { transcript: "ordinary deeply nested notes" };
for (let index = 0; index < 20_000; index += 1) deep = { child: deep };
assert.doesNotThrow(() => containsVideoTranscriptForLog(cyclic));
assert.doesNotThrow(() => containsVideoTranscriptForLog(deep));
assert.equal(containsVideoTranscriptForLog(cyclic), false);
assert.equal(containsVideoTranscriptForLog(deep), true);
const omittedCycle = omitVideoTranscriptForLog(cyclic);
const omittedDeep = omitVideoTranscriptForLog(deep);
assert.doesNotThrow(() => JSON.stringify(omittedCycle));
assert.equal(omittedDeep, VIDEO_TRANSCRIPT_LOG_OMISSION_MARKER);
});
test("fails closed when enumerable getters or proxies throw during transcript inspection", () => {
const throwingGetter: Record<string, unknown> = {};
Object.defineProperty(throwingGetter, "nested", {
enumerable: true,
get() {
throw new Error("PRIVATE_GETTER_TRANSCRIPT_SENTINEL");
},
});
const throwingProxy = new Proxy<Record<string, unknown>>(
{},
{
ownKeys() {
throw new Error("PRIVATE_PROXY_TRANSCRIPT_SENTINEL");
},
}
);
for (const payload of [throwingGetter, throwingProxy]) {
assert.equal(containsVideoTranscriptForLog(payload), true);
assert.equal(omitVideoTranscriptForLog(payload), VIDEO_TRANSCRIPT_LOG_OMISSION_MARKER);
}
});
test("fails closed when an enumerable function can replace the retained JSON representation", () => {
const privateTranscript = "PRIVATE_TOJSON_VIDEO_TRANSCRIPT_SENTINEL";
const payload = {
safe: "retained diagnostic",
toJSON() {
return {
transcript: privateTranscript,
type: "input_video",
video_url: "data:video/mp4;base64,AA==",
};
},
};
assert.equal(containsVideoTranscriptForLog(payload), true);
const omitted = omitVideoTranscriptForLog(payload) as Record<string, unknown>;
const serialized = JSON.stringify(omitted);
assert.equal(serialized.includes(privateTranscript), false);
assert.equal(omitted.safe, "retained diagnostic");
assert.notEqual(typeof omitted.toJSON, "function");
});
test("treats explicit malformed and nested video carriers as transcript-sensitive", () => {
const privateTranscript = "PRIVATE_MALFORMED_VIDEO_TRANSCRIPT_SENTINEL";
const payloads = [
{
transcript: privateTranscript,
type: "input_video",
},
{
transcript: privateTranscript,
type: "video_url",
video_url: "",
},
{
input_video: {
transcript: privateTranscript,
url: "data:video/mp4;base64,AA==",
},
type: "input_video",
},
{
input_video: {
url: { transcript: privateTranscript },
},
type: "input_video",
},
{
source: { transcript: privateTranscript },
type: "video_source",
},
{
source: {
media_type: "video/mp4",
transcript: privateTranscript,
},
type: "video",
},
];
for (const payload of payloads) {
assert.equal(containsVideoTranscriptForLog(payload), true, JSON.stringify(payload));
const serialized = JSON.stringify(omitVideoTranscriptForLog(payload));
assert.equal(serialized.includes(privateTranscript), false, serialized);
assert.match(serialized, /omitted: video transcript/);
}
});
test("keeps raw request sensitivity after a guardrail removes the original carrier", () => {
const rawRequestBody = {
messages: [
{
content: [
{
transcript: "PRIVATE_RAW_REQUEST_TRANSCRIPT_SENTINEL",
type: "input_video",
video_url: "data:video/mp4;base64,AA==",
},
],
role: "user",
},
],
};
const processedBody = {
messages: [{ content: "guardrail replaced the media", role: "user" }],
};
assert.equal(resolveVideoTranscriptLogSensitivity({ processedBody, rawRequestBody }), true);
assert.equal(
resolveVideoTranscriptLogSensitivity({
processedBody: { metadata: { transcript: "ordinary audit label" } },
rawRequestBody: { metadata: { transcript: "ordinary caller label" } },
}),
false
);
});
test("fails closed at the aggregate traversal budget before a tail video transcript can leak", () => {
const privateTranscript = "private over-budget video transcript sentinel";
const payload: unknown[] = Array.from(
{ length: 10_001 },
(_unused, index) => `ordinary entry ${index}`
);
// The detector must treat a bounded security scan as unknown/sensitive even without a cue.
assert.equal(containsVideoTranscriptForLog(payload), true);
payload[payload.length - 1] = {
transcript: privateTranscript,
type: "input_video",
video_url: "data:video/mp4;base64,AA==",
};
const omitted = omitVideoTranscriptForLog(payload);
assert.equal(omitted, VIDEO_TRANSCRIPT_LOG_OMISSION_MARKER);
assert.equal(JSON.stringify(omitted).includes(privateTranscript), false);
});
test("detects and omits an input-video transcript below nine ordinary objects", () => {
const privateTranscript = "private depth-9 video transcript sentinel";
let payload: Record<string, unknown> = {
transcript: privateTranscript,
type: "input_video",
video_url: { url: "data:video/mp4;base64,AA==" },
};
for (let depth = 0; depth < 9; depth += 1) payload = { nested: payload };
assert.equal(containsVideoTranscriptForLog(payload), true);
const omitted = JSON.stringify(omitVideoTranscriptForLog(payload));
assert.equal(omitted.includes(privateTranscript), false);
assert.match(omitted, /omitted: video transcript/);
});
test("redacts only direct transcript carriers within a recognized video part", () => {
const directCue = "private direct video subtitle";
const sourceCue = "private source video subtitle";
const unrelatedNestedTranscript = "ordinary nested metadata transcript";
const payload = {
type: "video",
source: {
data: "AA==",
media_type: "video/mp4",
transcript: sourceCue,
metadata: { transcript: unrelatedNestedTranscript },
type: "base64",
},
transcript: directCue,
metadata: { transcript: unrelatedNestedTranscript },
};
const omitted = omitVideoTranscriptForLog(payload) as typeof payload;
assert.equal(omitted.transcript, VIDEO_TRANSCRIPT_LOG_OMISSION_MARKER);
assert.equal(omitted.metadata.transcript, unrelatedNestedTranscript);
assert.equal(omitted.source.transcript, VIDEO_TRANSCRIPT_LOG_OMISSION_MARKER);
assert.equal(omitted.source.metadata.transcript, unrelatedNestedTranscript);
});
test("suppresses active stream chunks even when persisted pipeline logging is disabled", async () => {
const rawCue = "private active-log subtitle sentinel";
const model = "video-log-redaction-model";
const provider = "video-log-redaction-provider";
const connectionId = "video-log-redaction-connection";
usageHistory.clearPendingRequests();
try {
const requestId = usageHistory.trackPendingRequest(model, provider, connectionId, true);
const logger = await createRequestLogger(undefined, undefined, model, {
captureStreamChunks: true,
connectionId,
enabled: false,
model,
provider,
requestId,
});
logger.appendProviderChunk(`data: ${rawCue}\n\n`);
logger.logClientRawRequest("/v1/responses", {
input: [
{
audioTranscript: {
cues: [{ end: 2, source: "audio-bridge", start: 1, text: rawCue }],
},
type: "input_video",
video_url: "data:video/mp4;base64,AA==",
},
],
});
logger.appendOpenAIChunk(`data: ${rawCue}\n\n`);
logger.appendConvertedChunk(`data: ${rawCue}\n\n`);
const chunks = usageHistory.getPendingById().get(requestId)?.streamChunks;
assert.deepEqual(chunks, { client: [], openai: [], provider: [] });
} finally {
usageHistory.clearPendingRequests();
}
});
test("omits encrypted reasoning split across captured SSE chunks", () => {
const encryptedContent = "opaque-replay-state".repeat(128);
const protectedPipeline = protectPipelinePayloads({
streamChunks: {
provider: [
'[12:00:00.000] data: {"type":"response.completed","response":{"output":[{"type":"reasoning","encrypted_',
`[12:00:00.001] content":"${encryptedContent}","summary":[]}]}}\n\n`,
],
},
});
const storedChunks = protectedPipeline?.streamChunks?.provider ?? [];
assert.equal(storedChunks.length, 1);
assert.equal(storedChunks[0].includes(encryptedContent), false);
assert.equal(storedChunks[0].includes("[omitted: encrypted reasoning]"), true);
assert.equal(storedChunks[0].includes('"summary":[]'), true);
});
test("wraps raw text payloads in JSON-safe objects", () => {
const normalized = normalizePayloadForLog("event: ping\ndata: plain-text\n\n");
assert.deepEqual(normalized, {
_rawText: "event: ping\ndata: plain-text\n\n",
});
});
test("serializes truncated payloads as valid JSON objects", () => {
const stored = serializePayloadForStorage({ text: "x".repeat(200) }, 80);
const parsed: any = parseStoredPayload(stored);
assert.equal(parsed._truncated, true);
assert.equal(parsed._originalSize > 80, true);
assert.equal(typeof parsed._preview, "string");
});
test("structured SSE collector preserves event order and marks truncation", () => {
// Each collected event now also carries an ISO `timestamp` field (#5834 observability),
// which enlarges per-event bytes. Give the byte budget enough headroom so truncation
// here is driven by maxEvents (drop 1 of 3), which is what this test verifies.
const collector = createStructuredSSECollector({ maxEvents: 2, maxBytes: 2000 });
collector.push({ type: "response.created", id: "r1" });
collector.push({ type: "response.output_text.delta", delta: "hi" });
collector.push({ type: "response.completed" });
const payload = collector.build({ done: true });
assert.equal(payload._streamed, true);
assert.equal(payload._eventCount, 3);
assert.equal(payload._truncated, true);
assert.equal(payload._droppedEvents, 1);
assert.equal(payload.events.length, 2);
assert.equal(payload.events[0].event, "response.created");
assert.equal(payload.events[1].event, "response.output_text.delta");
assert.deepEqual(payload.summary, { done: true });
});
test("builds compact OpenAI stream summary for detailed logs", () => {
const collector = createStructuredSSECollector({ stage: "provider_response" });
collector.push({
id: "chatcmpl_1",
object: "chat.completion.chunk",
created: 123,
model: "gpt-4.1-mini",
choices: [{ index: 0, delta: { role: "assistant", content: "Hello " } }],
});
collector.push({
id: "chatcmpl_1",
object: "chat.completion.chunk",
created: 123,
model: "gpt-4.1-mini",
choices: [{ index: 0, delta: { content: "world" } }],
});
collector.push({
id: "chatcmpl_1",
object: "chat.completion.chunk",
created: 123,
model: "gpt-4.1-mini",
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
usage: { prompt_tokens: 5, completion_tokens: 2, total_tokens: 7 },
});
const summary = buildStreamSummaryFromEvents(
collector.getEvents(),
FORMATS.OPENAI,
"gpt-4.1-mini"
);
const compact: any = compactStructuredStreamPayload(
collector.build(summary, { includeEvents: false })
);
assert.equal(compact.object, "chat.completion");
assert.equal(compact.choices[0].message.content, "Hello world");
assert.equal(compact.choices[0].finish_reason, "stop");
assert.equal(compact._omniroute_stream.stage, "provider_response");
assert.equal(compact._omniroute_stream.eventCount, 3);
assert.equal("events" in compact, false);
});
test("builds compact Claude stream summary for detailed logs", () => {
const collector = createStructuredSSECollector({ stage: "provider_response" });
collector.push({
type: "message_start",
message: {
id: "msg_1",
model: "claude-sonnet-4",
role: "assistant",
usage: { input_tokens: 11 },
},
});
collector.push({
type: "content_block_start",
index: 0,
content_block: { type: "text", text: "" },
});
collector.push({
type: "content_block_delta",
index: 0,
delta: { type: "text_delta", text: "你好" },
});
collector.push({
type: "message_delta",
delta: { stop_reason: "end_turn" },
usage: { output_tokens: 7 },
});
const summary = buildStreamSummaryFromEvents(
collector.getEvents(),
FORMATS.CLAUDE,
"claude-sonnet-4"
);
const compact: any = compactStructuredStreamPayload(
collector.build(summary, { includeEvents: false })
);
assert.equal(compact.type, "message");
assert.equal(compact.model, "claude-sonnet-4");
assert.deepEqual(compact.content, [{ type: "text", text: "你好" }]);
assert.equal(compact.usage.input_tokens, 11);
assert.equal(compact.usage.output_tokens, 7);
assert.equal(compact._omniroute_stream.eventCount, 4);
});
test("builds compact OpenAI summary with reasoning alias (delta.reasoning)", () => {
const collector = createStructuredSSECollector({ stage: "provider_response" });
collector.push({
id: "chatcmpl_r1",
object: "chat.completion.chunk",
created: 100,
model: "moonshotai/kimi-k2.5",
choices: [{ index: 0, delta: { role: "assistant" } }],
});
collector.push({
id: "chatcmpl_r1",
object: "chat.completion.chunk",
created: 100,
model: "moonshotai/kimi-k2.5",
choices: [{ index: 0, delta: { reasoning: "Let me think..." } }],
});
collector.push({
id: "chatcmpl_r1",
object: "chat.completion.chunk",
created: 100,
model: "moonshotai/kimi-k2.5",
choices: [{ index: 0, delta: { content: "The answer is 4." } }],
});
collector.push({
id: "chatcmpl_r1",
object: "chat.completion.chunk",
created: 100,
model: "moonshotai/kimi-k2.5",
choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
usage: { prompt_tokens: 10, completion_tokens: 20, total_tokens: 30 },
});
const summary = buildStreamSummaryFromEvents(
collector.getEvents(),
FORMATS.OPENAI,
"moonshotai/kimi-k2.5"
);
const compact: any = compactStructuredStreamPayload(
collector.build(summary, { includeEvents: false })
);
assert.equal(compact.object, "chat.completion");
assert.equal(compact.choices[0].message.content, "The answer is 4.");
assert.equal(compact.choices[0].message.reasoning_content, "Let me think...");
assert.equal(compact.choices[0].finish_reason, "stop");
});