Skip to content

Commit b0d8dd3

Browse files
authored
fix(qa): reject deleted replies and failed streaming outcomes (#119660)
1 parent eba4487 commit b0d8dd3

4 files changed

Lines changed: 149 additions & 13 deletions

File tree

‎extensions/qa-lab/src/bus-state.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,37 @@ describe("qa-bus state", () => {
482482
).rejects.toThrow("qa-bus wait timeout");
483483
});
484484

485+
it("ignores deleted message matches until a visible replacement arrives", async () => {
486+
const state = createQaBusState();
487+
const deleted = state.addOutboundMessage({
488+
to: "dm:alice",
489+
text: "QA-VISIBLE-REPLACEMENT",
490+
});
491+
state.deleteMessage({ messageId: deleted.id });
492+
493+
await expect(
494+
state.waitFor({
495+
direction: "outbound",
496+
kind: "message-text",
497+
textIncludes: "QA-VISIBLE-REPLACEMENT",
498+
timeoutMs: 10,
499+
}),
500+
).rejects.toThrow("qa-bus wait timeout after 10ms");
501+
502+
const pending = state.waitFor({
503+
direction: "outbound",
504+
kind: "message-text",
505+
textIncludes: "QA-VISIBLE-REPLACEMENT",
506+
timeoutMs: 100,
507+
});
508+
const replacement = state.addOutboundMessage({
509+
to: "dm:alice",
510+
text: "QA-VISIBLE-REPLACEMENT",
511+
});
512+
513+
await expect(pending).resolves.toMatchObject({ id: replacement.id, direction: "outbound" });
514+
});
515+
485516
it("caps oversized wait timers", async () => {
486517
vi.useFakeTimers();
487518
const timeoutSpy = vi.spyOn(globalThis, "setTimeout");

‎extensions/qa-lab/src/bus-waiters.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function createQaBusMatcher(
4040
return (
4141
snapshot.messages.find(
4242
(message) =>
43+
!message.deleted &&
4344
(!input.direction || message.direction === input.direction) &&
4445
message.text.includes(input.textIncludes),
4546
) ?? null

‎extensions/qa-lab/src/qa-transport.test.ts‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,105 @@ describe("waitForQaTransportOutboundSequence", () => {
136136
});
137137
});
138138

139+
it.each([
140+
{ description: "before the final marker", failureBeforeFinal: true },
141+
{ description: "after the final marker", failureBeforeFinal: false },
142+
])("rejects an owned-account failure reply $description", async ({ failureBeforeFinal }) => {
143+
const state = createQaBusState();
144+
const preview = state.addOutboundMessage({
145+
accountId: "default",
146+
to: "dm:alice",
147+
text: "owned preview",
148+
});
149+
const addFailure = () =>
150+
state.addOutboundMessage({
151+
accountId: "default",
152+
to: "dm:alice",
153+
text: "⚠️ agent failed before reply: provider rejected this request",
154+
});
155+
156+
if (failureBeforeFinal) {
157+
addFailure();
158+
}
159+
state.editMessage({
160+
accountId: "default",
161+
messageId: preview.id,
162+
text: "final marker",
163+
});
164+
if (!failureBeforeFinal) {
165+
addFailure();
166+
}
167+
168+
await expect(
169+
waitForQaTransportOutboundSequence({
170+
accountId: "default",
171+
input: {
172+
conversationId: "alice",
173+
finalSettleMs: 0,
174+
finalTextIncludes: "final marker",
175+
minimumPreviewEvents: 1,
176+
timeoutMs: 25,
177+
},
178+
readEvents: () => state.getSnapshot().events,
179+
}),
180+
).rejects.toThrow("provider rejected this request");
181+
});
182+
183+
it("ignores stale, foreign-account, and inbound failure replies", async () => {
184+
const state = createQaBusState();
185+
state.addOutboundMessage({
186+
accountId: "default",
187+
to: "dm:alice",
188+
text: "⚠️ agent failed before reply: stale failure",
189+
});
190+
const sinceCursor = state.getSnapshot().cursor;
191+
192+
state.addOutboundMessage({
193+
accountId: "other",
194+
to: "dm:alice",
195+
text: "⚠️ agent failed before reply: foreign account failure",
196+
});
197+
const inbound = state.addInboundMessage({
198+
accountId: "default",
199+
conversation: { id: "alice", kind: "direct" },
200+
senderId: "alice",
201+
text: "⚠️ agent failed before reply: inbound failure",
202+
});
203+
state.editMessage({
204+
accountId: "default",
205+
messageId: inbound.id,
206+
text: "⚠️ agent failed before reply: edited inbound failure",
207+
});
208+
const preview = state.addOutboundMessage({
209+
accountId: "default",
210+
to: "dm:alice",
211+
text: "owned preview",
212+
});
213+
state.editMessage({
214+
accountId: "default",
215+
messageId: preview.id,
216+
text: "final marker",
217+
});
218+
219+
await expect(
220+
waitForQaTransportOutboundSequence({
221+
accountId: "default",
222+
input: {
223+
conversationId: "alice",
224+
finalSettleMs: 0,
225+
finalTextIncludes: "final marker",
226+
minimumPreviewEvents: 1,
227+
sinceCursor,
228+
timeoutMs: 25,
229+
},
230+
readEvents: () => state.getSnapshot().events,
231+
}),
232+
).resolves.toMatchObject({
233+
events: [{ kind: "sent" }, { kind: "edited" }],
234+
final: { accountId: "default", direction: "outbound", id: preview.id },
235+
});
236+
});
237+
139238
it("does not accept a matching preview that is deleted during final settling", async () => {
140239
const state = createQaBusState();
141240
const preview = state.addOutboundMessage({

‎extensions/qa-lab/src/qa-transport.ts‎

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -494,24 +494,29 @@ export async function waitForQaTransportOutboundSequence(params: {
494494
let stableCursor: number | null = null;
495495
let stableSince = 0;
496496
return await waitForQaTransportCondition(async () => {
497-
const events = (await params.readEvents())
497+
const ownedEvents = (await params.readEvents())
498498
.filter((event) => event.cursor > (params.input.sinceCursor ?? 0))
499499
.map((event) =>
500500
isQaTransportOutboundEvent(event) ? event : normalizeQaBusOutboundEvent(event),
501501
)
502502
.filter((event): event is QaTransportOutboundEvent => event !== null)
503-
.filter(({ message }) => {
504-
if (message.accountId !== params.accountId || message.direction !== "outbound") {
505-
return false;
506-
}
507-
if (
508-
params.input.conversationId &&
509-
message.conversation.id !== params.input.conversationId
510-
) {
511-
return false;
512-
}
513-
return !params.input.threadId || message.threadId === params.input.threadId;
514-
});
503+
.filter(
504+
({ message }) => message.accountId === params.accountId && message.direction === "outbound",
505+
);
506+
// Failures belong to the account, even when a different conversation has a matching final.
507+
for (const { kind, message } of ownedEvents) {
508+
const failureReply =
509+
kind === "deleted" || message.deleted ? undefined : extractQaFailureReplyText(message.text);
510+
if (failureReply) {
511+
throw new Error(failureReply);
512+
}
513+
}
514+
const events = ownedEvents.filter(({ message }) => {
515+
if (params.input.conversationId && message.conversation.id !== params.input.conversationId) {
516+
return false;
517+
}
518+
return !params.input.threadId || message.threadId === params.input.threadId;
519+
});
515520
const finalIndex = events.findLastIndex(
516521
({ kind, message }) =>
517522
kind !== "deleted" &&

0 commit comments

Comments
 (0)