Skip to content

Commit 4b60f5e

Browse files
committed
test(responses): cover passthrough replay state regressions
1 parent 2f9eb0f commit 4b60f5e

3 files changed

Lines changed: 417 additions & 1 deletion

File tree

‎tests/multi-agent-compat.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,23 @@ describe("multiAgentGuidanceText", () => {
562562
});
563563

564564
describe("injectDeveloperMessage", () => {
565+
const guidance = "guidance text";
566+
const generatedItem = (text = guidance) => ({
567+
type: "message",
568+
role: "developer",
569+
content: [{ type: "input_text", text }],
570+
});
571+
const countExact = (input: unknown[], text = guidance): number => input.filter(item => {
572+
if (!item || typeof item !== "object" || Array.isArray(item)) return false;
573+
const record = item as Record<string, unknown>;
574+
if (record.type !== "message" || record.role !== "developer" || !Array.isArray(record.content)) return false;
575+
if (record.content.length !== 1) return false;
576+
const part = record.content[0];
577+
return !!part && typeof part === "object" && !Array.isArray(part)
578+
&& (part as Record<string, unknown>).type === "input_text"
579+
&& (part as Record<string, unknown>).text === text;
580+
}).length;
581+
565582
test("appends to both the parsed messages and the raw passthrough input", () => {
566583
const parsed = parsedFixture({ reasoning: "max" });
567584
injectDeveloperMessage(parsed, "hello there");
@@ -597,6 +614,51 @@ describe("injectDeveloperMessage", () => {
597614
expect((input[1] as { role: string }).role).toBe("developer");
598615
expect((input[2] as { type: string }).type).toBe("compaction_trigger");
599616
});
617+
618+
test("exact-guidance predicate rejects every near-match replay-prefix shape (#326)", () => {
619+
const nearMatches: Array<[string, unknown]> = [
620+
["non-record item", null],
621+
["wrong type", { ...generatedItem(), type: "other" }],
622+
["wrong role", { ...generatedItem(), role: "user" }],
623+
["non-array content", { type: "message", role: "developer", content: "guidance text" }],
624+
["wrong content length", { type: "message", role: "developer", content: [] }],
625+
["non-record part", { type: "message", role: "developer", content: [null] }],
626+
["wrong part type", { type: "message", role: "developer", content: [{ type: "output_text", text: guidance }] }],
627+
["different text", generatedItem("different guidance")],
628+
["extra content part", {
629+
type: "message",
630+
role: "developer",
631+
content: [{ type: "input_text", text: guidance }, { type: "input_text", text: "extra" }],
632+
}],
633+
];
634+
635+
for (const [label, nearMatch] of nearMatches) {
636+
const parsed = parsedFixture({ reasoning: "max", rawInput: [nearMatch] });
637+
parsed._replayPrefixLen = 1;
638+
injectDeveloperMessage(parsed, guidance);
639+
const rawInput = (parsed._rawBody as { input: unknown[] }).input;
640+
expect(countExact(rawInput), label).toBe(1);
641+
expect(parsed.context.messages.filter(message => message.role === "developer" && message.content === guidance), label)
642+
.toHaveLength(1);
643+
}
644+
});
645+
646+
test("matching guidance in the current suffix does not suppress replay-prefix injection (#326)", () => {
647+
const parsed = parsedFixture({
648+
reasoning: "max",
649+
rawInput: [
650+
{ type: "message", role: "user", content: "replayed" },
651+
generatedItem(),
652+
],
653+
});
654+
parsed._replayPrefixLen = 1;
655+
injectDeveloperMessage(parsed, guidance);
656+
657+
const rawInput = (parsed._rawBody as { input: unknown[] }).input;
658+
expect(countExact(rawInput)).toBe(2);
659+
expect(parsed.context.messages.filter(message => message.role === "developer" && message.content === guidance))
660+
.toHaveLength(1);
661+
});
600662
});
601663

602664
describe("sanitizeEncryptedContentInPlace", () => {

‎tests/relay-eager.test.ts‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,35 @@ async function readAll(stream: ReadableStream<Uint8Array>): Promise<string> {
9393
return text;
9494
}
9595

96+
async function readAllBytes(stream: ReadableStream<Uint8Array>): Promise<Uint8Array> {
97+
const reader = stream.getReader();
98+
const chunks: Uint8Array[] = [];
99+
let length = 0;
100+
for (;;) {
101+
const { done, value } = await reader.read();
102+
if (done) break;
103+
chunks.push(value);
104+
length += value.byteLength;
105+
}
106+
const joined = new Uint8Array(length);
107+
let offset = 0;
108+
for (const chunk of chunks) {
109+
joined.set(chunk, offset);
110+
offset += chunk.byteLength;
111+
}
112+
return joined;
113+
}
114+
115+
function joinBytes(chunks: Uint8Array[]): Uint8Array {
116+
const joined = new Uint8Array(chunks.reduce((total, chunk) => total + chunk.byteLength, 0));
117+
let offset = 0;
118+
for (const chunk of chunks) {
119+
joined.set(chunk, offset);
120+
offset += chunk.byteLength;
121+
}
122+
return joined;
123+
}
124+
96125
describe("relaySseEagerBounded — side-effect parity", () => {
97126
test("(a) relays bytes verbatim; terminal recorded once; completed captured; onDone once", async () => {
98127
const { hooks, rec } = makeHooks();
@@ -123,6 +152,63 @@ describe("relaySseEagerBounded — side-effect parity", () => {
123152
expect(rec.synthetics).toEqual(["incomplete"]);
124153
expect(rec.dones).toBe(1);
125154
});
155+
156+
test("eager relay backfills missing completed output before passthrough persistence (#334)", async () => {
157+
const { hooks, rec } = makeHooks();
158+
const up = controlledUpstream();
159+
const relayed = relaySseEagerBounded(up.stream, new AbortController(), hooks);
160+
const frames = [
161+
sse(JSON.stringify({
162+
type: "response.output_item.done",
163+
output_index: 0,
164+
item: { type: "message", role: "assistant", content: [{ type: "output_text", text: "hello" }] },
165+
})),
166+
sse(JSON.stringify({
167+
type: "response.output_item.done",
168+
output_index: 1,
169+
item: { type: "function_call", call_id: "call_eager", name: "lookup", arguments: "{}" },
170+
})),
171+
sse(JSON.stringify({
172+
type: "response.completed",
173+
response: { id: "resp_334_eager", status: "completed" },
174+
})),
175+
];
176+
for (const frame of frames) up.push(frame);
177+
up.close();
178+
179+
const clientBytes = await readAllBytes(relayed);
180+
await settle();
181+
expect(clientBytes).toEqual(joinBytes(frames));
182+
const wireText = new TextDecoder().decode(clientBytes);
183+
expect(wireText).not.toContain('"output":');
184+
expect(rec.completed).toHaveLength(1);
185+
expect((rec.completed[0] as { output: Array<{ type: string }> }).output.map(item => item.type))
186+
.toEqual(["message", "function_call"]);
187+
expect(rec.dones).toBe(1);
188+
});
189+
190+
test("malformed SSE payload is skipped before a valid completed response (#334) — eager relay subcase", async () => {
191+
const { hooks, rec } = makeHooks();
192+
const up = controlledUpstream();
193+
const relayed = relaySseEagerBounded(up.stream, new AbortController(), hooks);
194+
up.push(enc.encode("data: {not-json}\n\n"));
195+
up.push(sse(JSON.stringify({
196+
type: "response.output_item.done",
197+
output_index: 0,
198+
item: { type: "message", role: "assistant", content: [{ type: "output_text", text: "survived" }] },
199+
})));
200+
up.push(sse(JSON.stringify({
201+
type: "response.completed",
202+
response: { id: "resp_334_eager_malformed", status: "completed", output: [] },
203+
})));
204+
up.close();
205+
206+
await readAll(relayed);
207+
await settle();
208+
expect(rec.completed).toHaveLength(1);
209+
expect((rec.completed[0] as { output: Array<{ type: string }> }).output.map(item => item.type))
210+
.toEqual(["message"]);
211+
});
126212
});
127213

128214
describe("relaySseEagerBounded — bounded queue", () => {

0 commit comments

Comments
 (0)