Skip to content

Commit 507b9df

Browse files
committed
fix(qa): retry transport cleanup and always stop owned labs
1 parent c8730c7 commit 507b9df

4 files changed

Lines changed: 199 additions & 5 deletions

File tree

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { createQaBusState } from "./bus-state.js";
3+
import type { QaLabServerHandle } from "./lab-server.types.js";
4+
import type { QaTransportAdapterFactory } from "./qa-transport-registry.js";
5+
import { runQaFlowSuiteIsolated } from "./suite-run-isolated.js";
6+
import { makeQaSuiteTestScenario } from "./suite-test-helpers.js";
7+
import type { QaSuiteResolvedRunContext, QaSuiteRunner } from "./suite-types.js";
8+
9+
function createCleanupTestLab(): QaLabServerHandle {
10+
return {
11+
baseUrl: "http://127.0.0.1:43123",
12+
listenUrl: "http://127.0.0.1:43123",
13+
state: createQaBusState(),
14+
setControlUi: vi.fn(),
15+
setScenarioRun: vi.fn(),
16+
setLatestReport: vi.fn(),
17+
runSelfCheck: vi.fn(),
18+
stop: vi.fn(async () => {}),
19+
};
20+
}
21+
22+
function createCleanupTestContext(): QaSuiteResolvedRunContext {
23+
return {
24+
startedAt: new Date("2026-08-04T00:00:00.000Z"),
25+
repoRoot: "/qa-repo",
26+
outputDir: "/qa-output",
27+
transportId: "qa-channel",
28+
selectedScenarios: [makeQaSuiteTestScenario("leased-channel-scenario")],
29+
providerMode: "mock-openai",
30+
primaryModel: "mock-openai/test-model",
31+
alternateModel: "mock-openai/test-model-alt",
32+
fastMode: true,
33+
channelDriver: "live",
34+
enabledPluginIds: [],
35+
gatewayConfigPatch: undefined,
36+
gatewayRuntimeOptions: undefined,
37+
concurrency: 1,
38+
progressEnabled: false,
39+
gatewayHeapCheckpointsEnabled: false,
40+
};
41+
}
42+
43+
describe("isolated QA suite transport cleanup", () => {
44+
it.each(["cleanup", "cleanupAfterGatewayStop"] as const)(
45+
"retries a failed parent %s phase before disposing its owned lab",
46+
async (cleanupPhase) => {
47+
const lab = createCleanupTestLab();
48+
const releaseError = new Error("credential release failed");
49+
const release = vi
50+
.fn<() => Promise<void>>()
51+
.mockRejectedValueOnce(releaseError)
52+
.mockResolvedValueOnce(undefined);
53+
const factory: QaTransportAdapterFactory = {
54+
id: "leased",
55+
matches: ({ channelId, driver }) => channelId === "leased" && driver === "live",
56+
async create() {
57+
return {
58+
id: "leased",
59+
label: "Leased channel",
60+
accountId: "sut",
61+
requiredPluginIds: [],
62+
supportedActions: [],
63+
sendInbound: async (input) => lab.state.addInboundMessage(input),
64+
createGatewayConfig: () => ({}),
65+
async waitReady() {},
66+
buildAgentDelivery: ({ target }) => ({
67+
channel: "leased",
68+
to: target,
69+
replyChannel: "leased",
70+
replyTo: target,
71+
}),
72+
async handleAction() {},
73+
createReportNotes: () => [],
74+
[cleanupPhase]: release,
75+
};
76+
},
77+
};
78+
const runChild = vi.fn<QaSuiteRunner>();
79+
80+
await expect(
81+
runQaFlowSuiteIsolated(
82+
{
83+
adapterFactories: [factory],
84+
channelDriver: "live",
85+
channelId: "leased",
86+
startLab: async () => lab,
87+
},
88+
createCleanupTestContext(),
89+
runChild,
90+
),
91+
).rejects.toBe(releaseError);
92+
93+
expect(release).toHaveBeenCalledTimes(2);
94+
expect(runChild).not.toHaveBeenCalled();
95+
expect(lab.stop).toHaveBeenCalledOnce();
96+
},
97+
);
98+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ export async function runQaFlowSuiteIsolated(
139139
if (params?.channelDriver === "live") {
140140
// The parent only renders aggregate artifacts. Release its live credentials
141141
// before child workers acquire the same exclusive transport lease.
142-
parentTransportCleaned = true;
143142
await transportFactoryResult.cleanupWithoutGateway();
143+
parentTransportCleaned = true;
144144
}
145145
updateScenarioRun();
146146
const workerStartStaggerMs =
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { describe, expect, it, vi } from "vitest";
2+
import { createQaBusState } from "./bus-state.js";
3+
import type { QaLabServerHandle } from "./lab-server.types.js";
4+
import type { QaTransportAdapterFactory } from "./qa-transport-registry.js";
5+
import { runQaRuntimeParitySuite } from "./suite-runtime-parity-runner.js";
6+
import { makeQaSuiteTestScenario } from "./suite-test-helpers.js";
7+
import type { QaSuiteRunner } from "./suite-types.js";
8+
9+
function createCleanupTestLab(): QaLabServerHandle {
10+
return {
11+
baseUrl: "http://127.0.0.1:43123",
12+
listenUrl: "http://127.0.0.1:43123",
13+
state: createQaBusState(),
14+
setControlUi: vi.fn(),
15+
setScenarioRun: vi.fn(),
16+
setLatestReport: vi.fn(),
17+
runSelfCheck: vi.fn(),
18+
stop: vi.fn(async () => {}),
19+
};
20+
}
21+
22+
describe("runtime parity suite transport cleanup", () => {
23+
it("stops its owned lab and preserves the scenario error when transport cleanup fails", async () => {
24+
const lab = createCleanupTestLab();
25+
const scenarioError = new Error("runtime scenario failed");
26+
const cleanupError = new Error("credential release failed");
27+
const cleanup = vi.fn(async () => {
28+
throw cleanupError;
29+
});
30+
const factory: QaTransportAdapterFactory = {
31+
id: "leased",
32+
matches: ({ channelId, driver }) => channelId === "leased" && driver === "live",
33+
async create() {
34+
return {
35+
id: "leased",
36+
label: "Leased channel",
37+
accountId: "sut",
38+
requiredPluginIds: [],
39+
supportedActions: [],
40+
sendInbound: async (input) => lab.state.addInboundMessage(input),
41+
createGatewayConfig: () => ({}),
42+
async waitReady() {},
43+
buildAgentDelivery: ({ target }) => ({
44+
channel: "leased",
45+
to: target,
46+
replyChannel: "leased",
47+
replyTo: target,
48+
}),
49+
async handleAction() {},
50+
createReportNotes: () => [],
51+
cleanup,
52+
};
53+
},
54+
};
55+
const runChild = vi.fn<QaSuiteRunner>().mockRejectedValueOnce(scenarioError);
56+
57+
await expect(
58+
runQaRuntimeParitySuite({
59+
runQaFlowSuite: runChild,
60+
adapterFactories: [factory],
61+
channelDriver: "live",
62+
channelId: "leased",
63+
repoRoot: "/qa-repo",
64+
outputDir: "/qa-output",
65+
startedAt: new Date("2026-08-04T00:00:00.000Z"),
66+
providerMode: "mock-openai",
67+
transportId: "qa-channel",
68+
primaryModel: "mock-openai/test-model",
69+
alternateModel: "mock-openai/test-model-alt",
70+
fastMode: true,
71+
concurrency: 1,
72+
selectedScenarios: [makeQaSuiteTestScenario("runtime-cleanup")],
73+
startLab: async () => lab,
74+
progressEnabled: false,
75+
runtimePair: ["openclaw", "codex"],
76+
}),
77+
).rejects.toMatchObject({
78+
message: "QA suite and cleanup failed",
79+
cause: scenarioError,
80+
errors: [scenarioError, cleanupError],
81+
});
82+
83+
expect(runChild).toHaveBeenCalledOnce();
84+
expect(cleanup).toHaveBeenCalledOnce();
85+
expect(lab.stop).toHaveBeenCalledOnce();
86+
});
87+
});

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import type {
3636
import {
3737
createQaSuiteTransportAdapter,
3838
requireQaSuiteStartLab,
39+
runQaSuiteCleanupSteps,
40+
throwQaSuiteCleanupErrors,
3941
writeQaSuiteProgress,
4042
} from "./suite.js";
4143

@@ -102,6 +104,8 @@ export async function runQaRuntimeParitySuite(params: {
102104
scenarios: [...liveScenarioOutcomes],
103105
});
104106

107+
let runFailed = false;
108+
let runError: unknown;
105109
try {
106110
const scenarios = await mapQaSuiteWithConcurrency(
107111
params.selectedScenarios,
@@ -283,10 +287,15 @@ export async function runQaRuntimeParitySuite(params: {
283287
scenarios,
284288
watchUrl: lab.baseUrl,
285289
} satisfies QaSuiteResult;
290+
} catch (error) {
291+
runFailed = true;
292+
runError = error;
293+
throw error;
286294
} finally {
287-
await transportFactoryResult.cleanupWithoutGateway();
288-
if (ownsLab) {
289-
await lab.stop();
290-
}
295+
const cleanupErrors = await runQaSuiteCleanupSteps([
296+
() => transportFactoryResult.cleanupWithoutGateway(),
297+
...(ownsLab ? [() => lab.stop()] : []),
298+
]);
299+
throwQaSuiteCleanupErrors({ cleanupErrors, runFailed, runError });
291300
}
292301
}

0 commit comments

Comments
 (0)