Skip to content

Commit 5e8a534

Browse files
authored
fix: retry live Gateway readiness proof (#104374)
* fix: retry live Gateway readiness proof * style: format live updater readiness helper
1 parent cbc8476 commit 5e8a534

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

‎.agents/skills/openclaw-live-updater/scripts/update-main.mjs‎

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import {
3535
const DEFAULT_CHECKOUT = "/Users/steipete/openclaw";
3636
const DEFAULT_EXPECTED_ORIGIN = "openclaw/openclaw";
3737
const FULL_SHA_RE = /^[0-9a-f]{40}$/u;
38+
const GATEWAY_READINESS_ATTEMPTS = 3;
39+
const GATEWAY_READINESS_RETRY_DELAY_MS = 5_000;
3840
const DEPENDENCY_INPUT_RE =
3941
/^(?:\.npmrc$|package\.json$|pnpm-lock\.yaml$|pnpm-workspace\.yaml$|patches\/)|(?:^|\/)package\.json$/u;
4042

@@ -578,6 +580,26 @@ function verifyGateway(runCommand, checkout, expectedSha) {
578580
runCommand("pnpm", ["openclaw", "health", "--verbose", "--json"], checkout);
579581
}
580582

583+
function defaultSleep(ms) {
584+
execFileSync("sleep", [String(ms / 1_000)]);
585+
}
586+
587+
export function verifyGatewayReadiness(runCommand, checkout, expectedSha, sleep = defaultSleep) {
588+
let lastError;
589+
for (let attempt = 1; attempt <= GATEWAY_READINESS_ATTEMPTS; attempt += 1) {
590+
try {
591+
verifyGateway(runCommand, checkout, expectedSha);
592+
return;
593+
} catch (error) {
594+
lastError = error;
595+
if (attempt < GATEWAY_READINESS_ATTEMPTS) {
596+
sleep(GATEWAY_READINESS_RETRY_DELAY_MS);
597+
}
598+
}
599+
}
600+
throw lastError;
601+
}
602+
581603
function summarizeGatewayLogEntry(entry) {
582604
return {
583605
time: entry.time,
@@ -690,10 +712,17 @@ function defaultAuditGatewayLogs(checkout, sinceMs) {
690712
return audit;
691713
}
692714

693-
function verifyAndAuditGateway({ runCommand, auditGatewayLogs, checkout, expectedSha, sinceMs }) {
715+
function verifyAndAuditGateway({
716+
runCommand,
717+
auditGatewayLogs,
718+
checkout,
719+
expectedSha,
720+
sinceMs,
721+
sleep,
722+
}) {
694723
let verificationError;
695724
try {
696-
verifyGateway(runCommand, checkout, expectedSha);
725+
verifyGatewayReadiness(runCommand, checkout, expectedSha, sleep);
697726
} catch (error) {
698727
verificationError = error;
699728
}
@@ -769,6 +798,7 @@ export function maintainMain(options, dependencies = {}) {
769798
const runCommand = dependencies.runCommand ?? defaultRunCommand;
770799
const verifyMacTarget = dependencies.verifyMacTarget ?? defaultVerifyMacTarget;
771800
const auditGatewayLogs = dependencies.auditGatewayLogs ?? defaultAuditGatewayLogs;
801+
const sleep = dependencies.sleep ?? defaultSleep;
772802
let gatewayLogAudit = null;
773803
let queuedMacState = null;
774804
if (actions.macAppRebuild) {
@@ -798,6 +828,7 @@ export function maintainMain(options, dependencies = {}) {
798828
checkout: update.checkout,
799829
expectedSha: update.afterSha,
800830
sinceMs: restartStartedAt,
831+
sleep,
801832
});
802833
} else {
803834
try {
@@ -812,6 +843,7 @@ export function maintainMain(options, dependencies = {}) {
812843
checkout: update.checkout,
813844
expectedSha: update.afterSha,
814845
sinceMs: restartStartedAt,
846+
sleep,
815847
});
816848
}
817849
}

‎test/scripts/openclaw-live-updater.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
maintainMain,
2525
originMatches,
2626
parseGatewayLogAudit,
27+
verifyGatewayReadiness,
2728
} from "../../.agents/skills/openclaw-live-updater/scripts/update-main.mjs";
2829
import {
2930
BUILD_STAMP_FILE,
@@ -204,6 +205,35 @@ describe("openclaw live updater", () => {
204205
});
205206
});
206207

208+
test("retries bounded Gateway readiness after restart", () => {
209+
const { mirror } = makeFixture();
210+
writeBuild(mirror);
211+
const calls: string[] = [];
212+
const delays: number[] = [];
213+
let statusAttempts = 0;
214+
215+
verifyGatewayReadiness(
216+
(command: string, args: string[]) => {
217+
const call = [command, ...args].join(" ");
218+
calls.push(call);
219+
if (call.includes("gateway status") && ++statusAttempts < 3) {
220+
throw new Error("RPC warming up");
221+
}
222+
},
223+
mirror,
224+
git(mirror, "rev-parse", "HEAD"),
225+
(ms: number) => delays.push(ms),
226+
);
227+
228+
expect(delays).toEqual([5_000, 5_000]);
229+
expect(calls).toEqual([
230+
"pnpm openclaw gateway status --deep --require-rpc --json",
231+
"pnpm openclaw gateway status --deep --require-rpc --json",
232+
"pnpm openclaw gateway status --deep --require-rpc --json",
233+
"pnpm openclaw health --verbose --json",
234+
]);
235+
});
236+
207237
test("accepts supported OpenClaw GitHub origins", () => {
208238
expect(originMatches("https://github.com/openclaw/openclaw.git")).toBe(true);
209239
expect(originMatches("[email protected]:openclaw/openclaw.git")).toBe(true);
@@ -628,10 +658,11 @@ describe("openclaw live updater", () => {
628658
auditCalls += 1;
629659
return { entries: 1, errorCount: 0, warningCount: 0, errors: [], warnings: [] };
630660
},
661+
sleep() {},
631662
},
632663
),
633664
).toThrow("RPC unavailable");
634-
expect(statusCalls).toBe(2);
665+
expect(statusCalls).toBe(4);
635666
expect(auditCalls).toBe(1);
636667
});
637668

0 commit comments

Comments
 (0)