Skip to content

Commit 375378e

Browse files
committed
fix(qa): enforce Matrix preparation deadline
Punchcard-Session: cobalt-valley-meadow-mg
1 parent 1d55b76 commit 375378e

7 files changed

Lines changed: 523 additions & 89 deletions

File tree

‎extensions/qa-lab/src/gateway-child.test.ts‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import os from "node:os";
66
import path from "node:path";
77
import { Writable } from "node:stream";
88
import { pathToFileURL } from "node:url";
9+
import { toErrorObject } from "openclaw/plugin-sdk/error-runtime";
910
import { afterEach, describe, expect, it, vi } from "vitest";
1011
import {
1112
testing,
@@ -272,6 +273,34 @@ describe("waitForGatewayReady", () => {
272273
}
273274
},
274275
);
276+
277+
it("bounds a stalled readiness probe by the remaining deadline", async () => {
278+
let probeSignal: AbortSignal | undefined;
279+
fetchWithSsrFGuardMock.mockImplementation(
280+
async ({ init }: { init?: RequestInit }) =>
281+
await new Promise((_, reject) => {
282+
probeSignal = init?.signal ?? undefined;
283+
probeSignal?.addEventListener(
284+
"abort",
285+
() => reject(toErrorObject(probeSignal?.reason, "QA readiness probe aborted")),
286+
{ once: true },
287+
);
288+
}),
289+
);
290+
const startedAt = Date.now();
291+
292+
await expect(
293+
testing.waitForGatewayReady({
294+
baseUrl: "http://127.0.0.1:43124",
295+
logs: () => "near-expiry logs",
296+
child: { exitCode: null, signalCode: null },
297+
timeoutMs: 25,
298+
}),
299+
).rejects.toThrow("gateway failed to become healthy");
300+
301+
expect(probeSignal?.aborted).toBe(true);
302+
expect(Date.now() - startedAt).toBeLessThan(1_000);
303+
});
275304
});
276305

277306
describe("Gateway child fixture helpers", () => {
@@ -852,6 +881,56 @@ describe("buildQaRuntimeEnv", () => {
852881
expect(testing.isRetryableGatewayCallError("permission denied")).toBe(false);
853882
});
854883

884+
it("preserves relative gateway retry timeouts without an absolute deadline", async () => {
885+
const request = vi
886+
.fn()
887+
.mockRejectedValueOnce(new Error("gateway closed (1012 service restart)"))
888+
.mockResolvedValueOnce({ ok: true });
889+
const waitForReady = vi.fn(async () => {});
890+
891+
await expect(
892+
testing.callQaGatewayWithRetry({
893+
logs: () => "qa logs",
894+
request,
895+
throwChildFailure: vi.fn(),
896+
timeoutMs: 2_000,
897+
waitForReady,
898+
}),
899+
).resolves.toEqual({ ok: true });
900+
901+
expect(request).toHaveBeenNthCalledWith(1, { timeoutMs: 2_000 });
902+
expect(request).toHaveBeenNthCalledWith(2, { timeoutMs: 2_000 });
903+
expect(waitForReady).toHaveBeenCalledWith(10_000);
904+
});
905+
906+
it("bounds near-expiry restart recovery by the absolute deadline", async () => {
907+
vi.useFakeTimers();
908+
vi.setSystemTime(0);
909+
const request = vi.fn(async () => {
910+
vi.setSystemTime(9_995);
911+
throw new Error("gateway closed (1012 service restart)");
912+
});
913+
const waitForReady = vi.fn(async (timeoutMs: number) => {
914+
vi.setSystemTime(Date.now() + timeoutMs);
915+
});
916+
917+
await expect(
918+
testing.callQaGatewayWithRetry({
919+
deadlineMs: 10_000,
920+
logs: () => "qa logs",
921+
request,
922+
throwChildFailure: vi.fn(),
923+
timeoutMs: 20_000,
924+
waitForReady,
925+
}),
926+
).rejects.toThrow("gateway call deadline exceeded");
927+
928+
expect(request).toHaveBeenCalledTimes(1);
929+
expect(request).toHaveBeenCalledWith({ deadlineMs: 10_000, timeoutMs: 10_000 });
930+
expect(waitForReady).toHaveBeenCalledWith(5);
931+
expect(Date.now()).toBe(10_000);
932+
});
933+
855934
it("waits for a fresh in-process restart boundary after the current log offset", async () => {
856935
let logs = "old restart mode: in-process restart\n";
857936
const offset = logs.length;

‎extensions/qa-lab/src/gateway-child.ts‎

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,56 @@ function isRetryableGatewayCallError(details: string): boolean {
569569
);
570570
}
571571

572+
async function callQaGatewayWithRetry<T>(params: {
573+
deadlineMs?: number;
574+
logs: () => string;
575+
request: (options: { deadlineMs?: number; timeoutMs: number }) => Promise<T>;
576+
throwChildFailure: () => void;
577+
timeoutMs: number;
578+
waitForReady: (timeoutMs: number) => Promise<void>;
579+
}) {
580+
const remainingMs = () =>
581+
params.deadlineMs === undefined ? undefined : params.deadlineMs - Date.now();
582+
const deadlineError = () =>
583+
new Error(`gateway call deadline exceeded${formatQaGatewayLogsForError(params.logs())}`);
584+
let lastDetails = "";
585+
for (let attempt = 1; attempt <= 3; attempt += 1) {
586+
params.throwChildFailure();
587+
const requestRemainingMs = remainingMs();
588+
if (requestRemainingMs !== undefined && requestRemainingMs <= 0) {
589+
throw deadlineError();
590+
}
591+
try {
592+
return await params.request({
593+
...(params.deadlineMs === undefined ? {} : { deadlineMs: params.deadlineMs }),
594+
timeoutMs:
595+
requestRemainingMs === undefined
596+
? params.timeoutMs
597+
: Math.min(params.timeoutMs, requestRemainingMs),
598+
});
599+
} catch (error) {
600+
params.throwChildFailure();
601+
const details = formatErrorMessage(error);
602+
lastDetails = details;
603+
if (attempt >= 3 || !isRetryableGatewayCallError(details)) {
604+
throw new Error(`${details}${formatQaGatewayLogsForError(params.logs())}`, {
605+
cause: error,
606+
});
607+
}
608+
const readinessRemainingMs = remainingMs();
609+
if (readinessRemainingMs !== undefined && readinessRemainingMs <= 0) {
610+
throw deadlineError();
611+
}
612+
await params.waitForReady(
613+
readinessRemainingMs === undefined
614+
? Math.max(10_000, params.timeoutMs)
615+
: Math.min(Math.max(10_000, params.timeoutMs), readinessRemainingMs),
616+
);
617+
}
618+
}
619+
throw new Error(`${lastDetails}${formatQaGatewayLogsForError(params.logs())}`);
620+
}
621+
572622
function createQaGatewayChildLogCollector() {
573623
const chunks: Buffer[] = [];
574624
return {
@@ -633,6 +683,7 @@ function formatQaGatewayProcessBoundaryStartupFailure(error: unknown, logs: stri
633683
async function fetchLocalGatewayHealth(params: {
634684
baseUrl: string;
635685
healthPath: "/readyz" | "/healthz";
686+
timeoutMs?: number;
636687
}): Promise<boolean> {
637688
const { response, release } = await fetchWithSsrFGuard({
638689
url: `${params.baseUrl}${params.healthPath}`,
@@ -641,7 +692,7 @@ async function fetchLocalGatewayHealth(params: {
641692
headers: {
642693
connection: "close",
643694
},
644-
signal: AbortSignal.timeout(2_000),
695+
signal: AbortSignal.timeout(params.timeoutMs ?? 2_000),
645696
},
646697
policy: { allowPrivateNetwork: true },
647698
auditContext: "qa-lab-gateway-child-health",
@@ -698,6 +749,7 @@ export const testing = {
698749
buildQaRuntimeEnv,
699750
cleanupQaGatewayTempRoots,
700751
fetchLocalGatewayHealth,
752+
callQaGatewayWithRetry,
701753
isRetryableGatewayCallError,
702754
isRetryableRpcStartupError,
703755
classifyQaGatewayStartupRetry,
@@ -1093,8 +1145,9 @@ async function waitForGatewayReady(params: {
10931145
getChildFailure?: () => QaChildFailure | null;
10941146
timeoutMs?: number;
10951147
}) {
1096-
const startedAt = Date.now();
1097-
while (Date.now() - startedAt < (params.timeoutMs ?? 60_000)) {
1148+
const deadline = Date.now() + (params.timeoutMs ?? 60_000);
1149+
let remainingMs: number;
1150+
while ((remainingMs = deadline - Date.now()) > 0) {
10981151
throwQaGatewayChildFailure(params.getChildFailure, params.logs);
10991152
if (params.child.exitCode !== null || params.child.signalCode !== null) {
11001153
throw new QaSuiteInfraError(
@@ -1104,13 +1157,19 @@ async function waitForGatewayReady(params: {
11041157
}
11051158
// Listener liveness can turn green before the Gateway can admit startup or restart work.
11061159
try {
1107-
if (await fetchLocalGatewayHealth({ baseUrl: params.baseUrl, healthPath: "/readyz" })) {
1160+
if (
1161+
await fetchLocalGatewayHealth({
1162+
baseUrl: params.baseUrl,
1163+
healthPath: "/readyz",
1164+
timeoutMs: Math.min(2_000, remainingMs),
1165+
})
1166+
) {
11081167
return;
11091168
}
11101169
} catch {
11111170
// retry until timeout
11121171
}
1113-
await sleep(250);
1172+
await sleep(Math.min(250, Math.max(0, deadline - Date.now())));
11141173
}
11151174
throw new QaSuiteInfraError(
11161175
"gateway_startup_unhealthy",
@@ -1853,34 +1912,28 @@ export async function startQaGatewayChild(params: {
18531912
async call(
18541913
method: string,
18551914
rpcParams?: unknown,
1856-
opts?: { expectFinal?: boolean; timeoutMs?: number },
1915+
opts?: { deadlineMs?: number; expectFinal?: boolean; timeoutMs?: number },
18571916
) {
18581917
const timeoutMs = opts?.timeoutMs ?? 20_000;
1859-
let lastDetails = "";
1860-
for (let attempt = 1; attempt <= 3; attempt += 1) {
1861-
throwActiveChildFailure();
1862-
try {
1863-
return await activeRpcClient.request(method, rpcParams, {
1918+
return await callQaGatewayWithRetry({
1919+
deadlineMs: opts?.deadlineMs,
1920+
logs,
1921+
request: async (requestOptions) =>
1922+
await activeRpcClient.request(method, rpcParams, {
18641923
...opts,
1865-
timeoutMs,
1866-
});
1867-
} catch (error) {
1868-
throwActiveChildFailure();
1869-
const details = formatErrorMessage(error);
1870-
lastDetails = details;
1871-
if (attempt >= 3 || !isRetryableGatewayCallError(details)) {
1872-
throw new Error(`${details}${formatQaGatewayLogsForError(logs())}`, { cause: error });
1873-
}
1924+
...requestOptions,
1925+
}),
1926+
throwChildFailure: throwActiveChildFailure,
1927+
timeoutMs,
1928+
waitForReady: async (readinessTimeoutMs) =>
18741929
await waitForGatewayReady({
18751930
baseUrl,
18761931
logs,
18771932
child: activeChild,
18781933
getChildFailure: activeGetChildFailure,
1879-
timeoutMs: Math.max(10_000, timeoutMs),
1880-
});
1881-
}
1882-
}
1883-
throw new Error(`${lastDetails}${formatQaGatewayLogsForError(logs())}`);
1934+
timeoutMs: readinessTimeoutMs,
1935+
}),
1936+
});
18841937
},
18851938
async stop(opts?: { keepTemp?: boolean; preserveToDir?: string }) {
18861939
await activeRpcClient.stop().catch(() => {});

0 commit comments

Comments
 (0)