Skip to content

Commit f7f38bc

Browse files
authored
fix(update): keep persisted and printed completion durations consistent (#133809)
1 parent a36fedb commit f7f38bc

2 files changed

Lines changed: 75 additions & 80 deletions

File tree

‎src/cli/update-cli/update-command-post-update.test.ts‎

Lines changed: 57 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type FinishUpdateParams = Parameters<typeof finishUpdate>[0];
104104
const stdinIsTTYDescriptor = Object.getOwnPropertyDescriptor(process.stdin, "isTTY");
105105

106106
afterEach(() => {
107+
vi.restoreAllMocks();
107108
vi.unstubAllEnvs();
108109
if (stdinIsTTYDescriptor) {
109110
Object.defineProperty(process.stdin, "isTTY", stdinIsTTYDescriptor);
@@ -164,18 +165,24 @@ function createManagedServiceIdentityFixture() {
164165
};
165166
}
166167

167-
async function finishSuccessfulPackageSwitch(params: {
168-
previousRoot: string;
169-
packageRoot: string;
170-
restartEnvironment?: NodeJS.ProcessEnv;
171-
json?: boolean;
172-
sealed?: boolean;
173-
updateMode?: UpdateRunResult["mode"];
174-
stoppedForUpdate?: boolean;
175-
windowsTaskAutoStartRecovery?: NonNullable<
176-
FinishUpdateParams["preManagedServiceStop"]
177-
>["windowsTaskAutoStartRecovery"];
178-
}): Promise<void> {
168+
async function finishSuccessfulPackageSwitch(
169+
params: {
170+
previousRoot: string;
171+
packageRoot: string;
172+
restartEnvironment?: NodeJS.ProcessEnv;
173+
json?: boolean;
174+
sealed?: boolean;
175+
updateMode?: UpdateRunResult["mode"];
176+
stoppedForUpdate?: boolean;
177+
windowsTaskAutoStartRecovery?: NonNullable<
178+
FinishUpdateParams["preManagedServiceStop"]
179+
>["windowsTaskAutoStartRecovery"];
180+
} = {
181+
previousRoot: "/tmp/openclaw-update",
182+
packageRoot: "/tmp/openclaw-update",
183+
restartEnvironment: process.env,
184+
},
185+
): Promise<void> {
179186
await finishUpdate({
180187
result: {
181188
status: "ok",
@@ -258,11 +265,7 @@ describe("successful update finalization ordering", () => {
258265

259266
let failure: unknown;
260267
try {
261-
await finishSuccessfulPackageSwitch({
262-
previousRoot: "/tmp/openclaw-update",
263-
packageRoot: "/tmp/openclaw-update",
264-
restartEnvironment: process.env,
265-
});
268+
await finishSuccessfulPackageSwitch();
266269
} catch (err) {
267270
failure = err;
268271
}
@@ -312,11 +315,7 @@ describe("successful update finalization ordering", () => {
312315
});
313316
mocks.ensureCompletionCache.mockResolvedValueOnce(false);
314317

315-
await finishSuccessfulPackageSwitch({
316-
previousRoot: "/tmp/openclaw-update",
317-
packageRoot: "/tmp/openclaw-update",
318-
restartEnvironment: process.env,
319-
});
318+
await finishSuccessfulPackageSwitch();
320319

321320
const output = vi.mocked(defaultRuntime.log).mock.calls.flat().map(String).join("\n");
322321
expect(output).toContain("completion cache generation failed");
@@ -348,11 +347,7 @@ describe("successful update finalization ordering", () => {
348347
it("skips interactive completion in non-TTY mode", async () => {
349348
Object.defineProperty(process.stdin, "isTTY", { configurable: true, value: false });
350349

351-
await finishSuccessfulPackageSwitch({
352-
previousRoot: "/tmp/openclaw-update",
353-
packageRoot: "/tmp/openclaw-update",
354-
restartEnvironment: process.env,
355-
});
350+
await finishSuccessfulPackageSwitch();
356351

357352
expect(mocks.checkCompletionStatus).not.toHaveBeenCalled();
358353
expect(mocks.ensureCompletionCache).not.toHaveBeenCalled();
@@ -363,11 +358,7 @@ describe("successful update finalization ordering", () => {
363358
Object.defineProperty(process.stdin, "isTTY", { configurable: true, value: true });
364359
mocks.restartService.mockResolvedValueOnce(false);
365360

366-
await finishSuccessfulPackageSwitch({
367-
previousRoot: "/tmp/openclaw-update",
368-
packageRoot: "/tmp/openclaw-update",
369-
restartEnvironment: process.env,
370-
});
361+
await finishSuccessfulPackageSwitch();
371362

372363
expect(mocks.printResult).toHaveBeenCalledOnce();
373364
expect(mocks.printResult).toHaveBeenCalledWith(
@@ -383,7 +374,7 @@ describe("successful update finalization ordering", () => {
383374

384375
it("reports elapsed time through restart and shell completion refresh", async () => {
385376
let now = 1_000;
386-
const clock = vi.spyOn(Date, "now").mockImplementation(() => now);
377+
vi.spyOn(Date, "now").mockImplementation(() => now);
387378
Object.defineProperty(process.stdin, "isTTY", { configurable: true, value: true });
388379
mocks.restartService.mockImplementationOnce(async () => {
389380
now += 200;
@@ -393,21 +384,18 @@ describe("successful update finalization ordering", () => {
393384
now += 300;
394385
return { shell: "zsh", profileInstalled: true, cacheExists: true, usesSlowPattern: false };
395386
});
396-
try {
397-
await finishSuccessfulPackageSwitch({
398-
previousRoot: "/tmp/openclaw-update",
399-
packageRoot: "/tmp/openclaw-update",
400-
restartEnvironment: process.env,
387+
mocks.writeSentinel
388+
.mockImplementationOnce(async () => undefined)
389+
.mockImplementationOnce(async () => {
390+
now += 100;
401391
});
392+
await finishSuccessfulPackageSwitch();
402393

403-
expect(mocks.printResult).toHaveBeenCalledOnce();
404-
expect(mocks.printResult.mock.lastCall?.[0]).toMatchObject({ status: "ok", durationMs: 500 });
405-
expect(mocks.writeSentinel.mock.lastCall?.[0].result).toEqual(
406-
mocks.printResult.mock.lastCall?.[0],
407-
);
408-
} finally {
409-
clock.mockRestore();
410-
}
394+
expect(mocks.printResult).toHaveBeenCalledOnce();
395+
expect(mocks.printResult.mock.lastCall?.[0]).toMatchObject({ status: "ok", durationMs: 500 });
396+
expect(mocks.writeSentinel.mock.lastCall?.[0].result).toEqual(
397+
mocks.printResult.mock.lastCall?.[0],
398+
);
411399
});
412400

413401
it("reports Windows autostart recovery failure before exiting", async () => {
@@ -702,11 +690,7 @@ describe("successful update finalization ordering", () => {
702690
},
703691
});
704692

705-
await finishSuccessfulPackageSwitch({
706-
previousRoot: "/tmp/openclaw-update",
707-
packageRoot: "/tmp/openclaw-update",
708-
restartEnvironment: process.env,
709-
});
693+
await finishSuccessfulPackageSwitch();
710694

711695
expect(mocks.restartService).toHaveBeenCalledWith(
712696
expect.objectContaining({
@@ -780,6 +764,11 @@ describe("successful update finalization ordering", () => {
780764
it.each(["inspection", "revalidation"] as const)(
781765
"does not restart a stopped sealed service when fresh %s fails",
782766
async (failure) => {
767+
let now = 1_000;
768+
vi.spyOn(Date, "now").mockImplementation(() => now);
769+
mocks.writeSentinel.mockImplementationOnce(async () => {
770+
now += 100;
771+
});
783772
const error = new Error("inspection-secret-canary");
784773
mocks.readServiceState.mockResolvedValue({
785774
installed: true,
@@ -965,19 +954,17 @@ describe("skipped update exit status", () => {
965954
vi.spyOn(defaultRuntime, "log").mockImplementation(() => undefined);
966955
});
967956

968-
it("exits nonzero when local changes block a Git update", async () => {
969-
await finishSkippedUpdate("dirty");
970-
971-
expect(defaultRuntime.error).toHaveBeenCalledWith(
972-
expect.stringContaining("Update blocked: local files are edited"),
973-
);
974-
expect(defaultRuntime.exit).toHaveBeenCalledWith(1);
975-
});
976-
977-
it("keeps a non-Git install skip successful", async () => {
978-
await finishSkippedUpdate("not-git-install");
979-
980-
expect(defaultRuntime.exit).toHaveBeenCalledWith(0);
957+
it.each([
958+
["dirty", 1],
959+
["not-git-install", 0],
960+
] as const)("handles %s with exit %i", async (reason, exitCode) => {
961+
await finishSkippedUpdate(reason);
962+
if (reason === "dirty") {
963+
expect(defaultRuntime.error).toHaveBeenCalledWith(
964+
expect.stringContaining("Update blocked: local files are edited"),
965+
);
966+
}
967+
expect(defaultRuntime.exit).toHaveBeenCalledWith(exitCode);
981968
});
982969
});
983970

@@ -990,16 +977,22 @@ describe("failed Git update recovery restart", () => {
990977
it.each(["error", "skipped"] as const)(
991978
"records the %s outcome before recovery starts the Gateway",
992979
async (status) => {
980+
let now = 1_000;
981+
vi.spyOn(Date, "now").mockImplementation(() => now);
993982
mocks.restart.mockImplementationOnce(async () => {
994983
expect(mocks.writeSentinel.mock.lastCall?.[0].result).toMatchObject({ status });
984+
expect(mocks.writeSentinel.mock.lastCall?.[0].result.durationMs).toBe(0);
995985
expect(mocks.printResult).not.toHaveBeenCalled();
986+
now += 200;
996987
});
997988

998989
await finishFailedUpdate({ ...failedResult({ serviceRestartSafe: true }), status });
999990

1000991
expect(mocks.restart).toHaveBeenCalledWith(expect.objectContaining({ root: "/repo" }));
1001992
expect(mocks.writeSentinel).toHaveBeenCalledOnce();
993+
expect(mocks.writeSentinel.mock.lastCall?.[0].result.durationMs).toBe(0);
1002994
expect(mocks.printResult).toHaveBeenCalledOnce();
995+
expect(mocks.printResult.mock.lastCall?.[0]).toMatchObject({ status, durationMs: 200 });
1003996
},
1004997
);
1005998

‎src/cli/update-cli/update-command-post-update.ts‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ export async function finishUpdate(params: {
9191
invocationCwd?: string;
9292
}): Promise<void> {
9393
// Finalization owns the complete outcome, including recovery, restart, and completion work.
94+
const completedResult = (result: UpdateRunResult): UpdateRunResult => ({
95+
...result,
96+
durationMs: Math.max(0, Date.now() - params.startedAt),
97+
});
9498
const printFinalResult = (result: UpdateRunResult) =>
95-
printResult(
96-
{ ...result, durationMs: Math.max(0, Date.now() - params.startedAt) },
97-
{ ...params.opts, hideSteps: params.showProgress },
98-
);
99+
printResult(result, { ...params.opts, hideSteps: params.showProgress });
99100
const reportResult = async (result: UpdateRunResult, recoverService = false) => {
100-
const finalResult = { ...result, durationMs: Math.max(0, Date.now() - params.startedAt) };
101+
const finalResult = completedResult(result);
101102
await writeControlPlaneUpdateRestartSentinelBestEffort({
102103
meta: params.controlPlaneUpdateSentinelMeta,
103104
result: finalResult,
@@ -112,7 +113,8 @@ export async function finishUpdate(params: {
112113
jsonMode: Boolean(params.opts.json),
113114
});
114115
}
115-
printFinalResult(finalResult);
116+
// Only recovery advances the outcome after persistence; ordinary reports share one snapshot.
117+
printFinalResult(recoverService ? completedResult(result) : finalResult);
116118
};
117119
const restoreWindowsAutoStart = async (result: UpdateRunResult) => {
118120
try {
@@ -516,11 +518,9 @@ export async function finishUpdate(params: {
516518
reason: "restart-unhealthy",
517519
jsonMode: Boolean(params.opts.json),
518520
});
519-
printFinalResult({
520-
...resultWithPostUpdate,
521-
status: "error",
522-
reason: "restart-unhealthy",
523-
});
521+
printFinalResult(
522+
completedResult({ ...resultWithPostUpdate, status: "error", reason: "restart-unhealthy" }),
523+
);
524524
defaultRuntime.exit(1);
525525
return;
526526
}
@@ -558,11 +558,13 @@ export async function finishUpdate(params: {
558558
reason: "wrapper-retirement-failed",
559559
jsonMode: Boolean(params.opts.json),
560560
});
561-
printFinalResult({
562-
...resultWithPostUpdate,
563-
status: "error",
564-
reason: "wrapper-retirement-failed",
565-
});
561+
printFinalResult(
562+
completedResult({
563+
...resultWithPostUpdate,
564+
status: "error",
565+
reason: "wrapper-retirement-failed",
566+
}),
567+
);
566568
defaultRuntime.exit(1);
567569
return;
568570
}

0 commit comments

Comments
 (0)