Skip to content

Commit b7a42b7

Browse files
committed
Merge remote-tracking branch 'origin/main' into pr-107879-conflict-resolution
2 parents d041a46 + c1eba30 commit b7a42b7

12 files changed

Lines changed: 258 additions & 4 deletions

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ import path from "node:path";
44
import type { OpenClawCrablineChannelDriverSelection } from "@openclaw/crabline";
55
import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime";
66
import { withTempDir } from "openclaw/plugin-sdk/test-env";
7-
import { describe, expect, it } from "vitest";
7+
import { afterEach, describe, expect, it, vi } from "vitest";
88
import { createQaBusState } from "./bus-state.js";
99
import { createQaCrablineTransportAdapter } from "./crabline-transport.js";
1010

11+
afterEach(() => {
12+
vi.unstubAllGlobals();
13+
});
14+
1115
function createSelection(channel: OpenClawCrablineChannelDriverSelection["channel"] = "telegram") {
1216
return {
1317
capabilityMatrixPath: "crabline-fake-provider-capabilities.json",
@@ -25,6 +29,42 @@ function requireString(value: unknown, label: string): string {
2529
}
2630

2731
describe("crabline transport", () => {
32+
it("cancels a failed inbound response before surfacing the provider error", async () => {
33+
await withTempDir("qa-crabline-transport-", async (outputDir) => {
34+
const transport = await createQaCrablineTransportAdapter({
35+
outputDir,
36+
selection: createSelection(),
37+
state: createQaBusState(),
38+
});
39+
const cancel = vi.fn(() => {
40+
throw new Error("cancel failed");
41+
});
42+
vi.stubGlobal(
43+
"fetch",
44+
vi.fn(
45+
async () =>
46+
new Response(new ReadableStream<Uint8Array>({ cancel }), {
47+
status: 503,
48+
}),
49+
),
50+
);
51+
52+
try {
53+
await expect(
54+
transport.sendInbound({
55+
conversation: { id: "-1001234567890", kind: "group" },
56+
senderId: "100001",
57+
senderName: "Alice",
58+
text: "Telegram failure marker.",
59+
}),
60+
).rejects.toThrow("Crabline telegram inbound injection failed with HTTP 503");
61+
expect(cancel).toHaveBeenCalledOnce();
62+
} finally {
63+
await transport.cleanup?.();
64+
}
65+
});
66+
});
67+
2868
it("configures OpenClaw's Telegram plugin against a Crabline local provider server", async () => {
2969
await withTempDir("qa-crabline-transport-", async (outputDir) => {
3070
const transport = await createQaCrablineTransportAdapter({

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
resolveTelegramQaSenderId,
2525
} from "./crabline-provider-targets.js";
2626
import { QaSuiteInfraError } from "./errors.js";
27+
import { discardIgnoredResponseBody } from "./ignored-response-body.js";
2728
import {
2829
QaStateBackedTransportAdapter,
2930
waitForQaTransportOutboundSequence,
@@ -209,6 +210,7 @@ async function postCrablineInbound(params: {
209210
});
210211
try {
211212
if (!response.ok) {
213+
await discardIgnoredResponseBody(response);
212214
throw new Error(
213215
`Crabline ${params.adapter.channel} inbound injection failed with HTTP ${response.status}.`,
214216
);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { discardIgnoredResponseBody } from "./ignored-response-body.js";
3+
4+
describe("discardIgnoredResponseBody", () => {
5+
it("swallows cancellation failures for an unread body", async () => {
6+
const cancel = vi.fn(() => {
7+
throw new Error("cancel failed");
8+
});
9+
const response = new Response(new ReadableStream<Uint8Array>({ cancel }));
10+
11+
await expect(discardIgnoredResponseBody(response)).resolves.toBeUndefined();
12+
expect(cancel).toHaveBeenCalledOnce();
13+
});
14+
15+
it("does not cancel a body a caller already consumed", async () => {
16+
const cancel = vi.fn();
17+
const response = new Response(
18+
new ReadableStream<Uint8Array>({
19+
start(controller) {
20+
controller.enqueue(new TextEncoder().encode("done"));
21+
controller.close();
22+
},
23+
cancel,
24+
}),
25+
);
26+
await response.text();
27+
28+
await discardIgnoredResponseBody(response);
29+
expect(cancel).not.toHaveBeenCalled();
30+
});
31+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Qa Lab HTTP callers discard response bodies they do not inspect before
2+
// releasing guarded fetch resources. Otherwise the dispatcher must destroy
3+
// the still-streaming connection during release.
4+
export async function discardIgnoredResponseBody(response: Response): Promise<void> {
5+
if (response.bodyUsed) {
6+
return;
7+
}
8+
await response.body?.cancel().catch(() => undefined);
9+
}

‎extensions/qa-lab/src/runtime-parity.test.ts‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
upsertSessionEntry,
99
} from "openclaw/plugin-sdk/session-store-runtime";
1010
import { appendSessionTranscriptMessageByIdentity } from "openclaw/plugin-sdk/session-transcript-runtime";
11-
import { afterEach, describe, expect, it } from "vitest";
11+
import { afterEach, describe, expect, it, vi } from "vitest";
1212
import {
1313
captureRuntimeParityCell,
1414
isRuntimeParityResultPass,
@@ -23,6 +23,7 @@ import { createTempDirHarness } from "./temp-dir.test-helper.js";
2323
const tempDirs = createTempDirHarness();
2424

2525
afterEach(async () => {
26+
vi.unstubAllGlobals();
2627
await tempDirs.cleanup();
2728
});
2829

@@ -127,6 +128,38 @@ function makeRuntimeParityCell(
127128
}
128129

129130
describe("runtime parity", () => {
131+
it("cancels a failed mock-request response before falling back to transcript calls", async () => {
132+
const parentPrompt = "Delegate one bounded QA task to a subagent.";
133+
const tempRoot = await seedRuntimeParityTranscript({
134+
sessionId: "mock-runtime-parity-failure",
135+
sessionKey: "agent:qa:mock-runtime-parity-failure",
136+
messages: [{ role: "user", content: parentPrompt }],
137+
});
138+
const cancel = vi.fn(() => {
139+
throw new Error("cancel failed");
140+
});
141+
vi.stubGlobal(
142+
"fetch",
143+
vi.fn(
144+
async () =>
145+
new Response(new ReadableStream<Uint8Array>({ cancel }), {
146+
status: 503,
147+
}),
148+
),
149+
);
150+
151+
const cell = await captureRuntimeParityCell({
152+
runtime: "openclaw",
153+
gateway: { tempRoot },
154+
mockBaseUrl: "http://127.0.0.1:43123",
155+
scenarioResult: { status: "pass" },
156+
wallClockMs: 10,
157+
});
158+
159+
expect(cancel).toHaveBeenCalledOnce();
160+
expect(cell.toolCalls).toEqual([]);
161+
});
162+
130163
it("captures tool results from the canonical SQLite session transcript", async () => {
131164
const tempRoot = await seedRuntimeParityTranscript({
132165
sessionId: "capability-flip",

‎extensions/qa-lab/src/runtime-parity.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
scanGatewayLogSentinels,
1515
type GatewayLogSentinelFinding,
1616
} from "./gateway-log-sentinel.js";
17+
import { discardIgnoredResponseBody } from "./ignored-response-body.js";
1718
import * as parity from "./parity-shared.js";
1819

1920
export type RuntimeId = "openclaw" | "codex";
@@ -1052,6 +1053,7 @@ async function loadRuntimeParityMockToolCalls(
10521053
let payload: unknown;
10531054
try {
10541055
if (!response.ok) {
1056+
await discardIgnoredResponseBody(response);
10551057
return null;
10561058
}
10571059
payload = await response.json();

‎extensions/qa-lab/src/suite-runtime-gateway.test.ts‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,82 @@ describe("qa suite gateway helpers", () => {
158158
expect(release).toHaveBeenCalledTimes(1);
159159
});
160160

161+
it("cancels failed suite gateway JSON response bodies before releasing their guard", async () => {
162+
const events: string[] = [];
163+
const cancelBody = vi.fn(() => {
164+
events.push("cancel");
165+
throw new Error("cancel failed");
166+
});
167+
const release = vi.fn(async () => {
168+
events.push("release");
169+
});
170+
fetchWithSsrFGuardMock.mockResolvedValue({
171+
response: new Response(
172+
new ReadableStream<Uint8Array>({
173+
cancel: cancelBody,
174+
}),
175+
{ status: 503 },
176+
),
177+
release,
178+
});
179+
180+
await expect(fetchJson("http://127.0.0.1:43123/config")).rejects.toThrow(
181+
"request failed 503: http://127.0.0.1:43123/config",
182+
);
183+
expect(cancelBody).toHaveBeenCalledTimes(1);
184+
expect(release).toHaveBeenCalledTimes(1);
185+
expect(events).toEqual(["cancel", "release"]);
186+
});
187+
188+
it("cancels every ignored gateway health body before releasing its guard", async () => {
189+
const events: string[] = [];
190+
const failedCancel = vi.fn(() => {
191+
events.push("failed:cancel");
192+
});
193+
const successCancel = vi.fn(() => {
194+
events.push("success:cancel");
195+
});
196+
const failedRelease = vi.fn(async () => {
197+
events.push("failed:release");
198+
});
199+
const successRelease = vi.fn(async () => {
200+
events.push("success:release");
201+
});
202+
fetchWithSsrFGuardMock
203+
.mockResolvedValueOnce({
204+
response: new Response(
205+
new ReadableStream<Uint8Array>({
206+
cancel: failedCancel,
207+
}),
208+
{ status: 503 },
209+
),
210+
release: failedRelease,
211+
})
212+
.mockResolvedValueOnce({
213+
response: new Response(
214+
new ReadableStream<Uint8Array>({
215+
cancel: successCancel,
216+
}),
217+
{ status: 200 },
218+
),
219+
release: successRelease,
220+
});
221+
222+
await expect(
223+
waitForGatewayHealthy({ gateway: { baseUrl: "http://127.0.0.1:43123" } } as never, 1_000),
224+
).resolves.toBeUndefined();
225+
expect(failedCancel).toHaveBeenCalledTimes(1);
226+
expect(successCancel).toHaveBeenCalledTimes(1);
227+
expect(failedRelease).toHaveBeenCalledTimes(1);
228+
expect(successRelease).toHaveBeenCalledTimes(1);
229+
expect(events).toEqual([
230+
"failed:cancel",
231+
"failed:release",
232+
"success:cancel",
233+
"success:release",
234+
]);
235+
});
236+
161237
it("bounds a hung gateway health request by the remaining readiness deadline", async () => {
162238
vi.useFakeTimers();
163239
fetchWithSsrFGuardMock.mockImplementation(

‎extensions/qa-lab/src/suite-runtime-gateway.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { readProviderJsonResponse } from "openclaw/plugin-sdk/provider-http";
66
import { fetchWithSsrFGuard } from "openclaw/plugin-sdk/ssrf-runtime";
77
import { isRecord as isPlainObject } from "openclaw/plugin-sdk/string-coerce-runtime";
88
import { QaSuiteInfraError, toQaErrorObject } from "./errors.js";
9+
import { discardIgnoredResponseBody } from "./ignored-response-body.js";
910
import { applyQaMergePatch } from "./suite-merge-patch.js";
1011
import { liveTurnTimeoutMs } from "./suite-runtime-agent-common.js";
1112
import type { QaConfigSnapshot, QaSuiteRuntimeEnv } from "./suite-runtime-types.js";
@@ -27,6 +28,7 @@ async function fetchJson<T>(url: string, timeoutMs = QA_SUITE_FETCH_JSON_TIMEOUT
2728
});
2829
try {
2930
if (!response.ok) {
31+
await discardIgnoredResponseBody(response);
3032
throw new Error(`request failed ${response.status}: ${url}`);
3133
}
3234
return await readProviderJsonResponse<T>(response, "qa-lab-suite-fetch-json");
@@ -46,7 +48,9 @@ async function waitForGatewayHealthy(env: Pick<QaSuiteRuntimeEnv, "gateway">, ti
4648
auditContext: "qa-lab-suite-wait-for-gateway-healthy",
4749
});
4850
try {
49-
if (response.ok) {
51+
const ready = response.ok;
52+
await discardIgnoredResponseBody(response);
53+
if (ready) {
5054
return;
5155
}
5256
} finally {

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,37 @@ describe("qa suite", () => {
246246
expect(stop).toHaveBeenCalledTimes(1);
247247
});
248248

249+
it("cancels a successful lab readiness body before releasing its guard", async () => {
250+
const events: string[] = [];
251+
const stop = vi.fn(async () => {});
252+
fetchWithSsrFGuardMock.mockResolvedValue({
253+
response: new Response(
254+
new ReadableStream<Uint8Array>({
255+
cancel() {
256+
events.push("cancel");
257+
},
258+
}),
259+
{ status: 200 },
260+
),
261+
release: async () => {
262+
events.push("release");
263+
},
264+
});
265+
266+
await expect(
267+
qaSuiteProgressTesting.waitForQaLabReadyOrStopOwned({
268+
lab: {
269+
listenUrl: "http://127.0.0.1:43123",
270+
stop,
271+
},
272+
ownsLab: false,
273+
}),
274+
).resolves.toBeUndefined();
275+
276+
expect(events).toEqual(["cancel", "release"]);
277+
expect(stop).not.toHaveBeenCalled();
278+
});
279+
249280
it("bounds a hung lab readiness request by the remaining startup deadline", async () => {
250281
vi.useFakeTimers();
251282
const stop = vi.fn(async () => {});

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
type QaCliBackendAuthMode,
2626
type QaGatewayChildCommand,
2727
} from "./gateway-child.js";
28+
import { discardIgnoredResponseBody } from "./ignored-response-body.js";
2829
import type {
2930
QaLabLatestReport,
3031
QaLabScenarioOutcome,
@@ -274,7 +275,9 @@ async function waitForQaLabReady(baseUrl: string, timeoutMs = 10_000) {
274275
auditContext: "qa-lab-suite-wait-for-lab-ready",
275276
});
276277
try {
277-
if (response.ok) {
278+
const ready = response.ok;
279+
await discardIgnoredResponseBody(response);
280+
if (ready) {
278281
return;
279282
}
280283
} finally {

0 commit comments

Comments
 (0)