Skip to content

Commit 238bec2

Browse files
authored
refactor(update): canonicalize step result recording (#118406)
1 parent b653597 commit 238bec2

6 files changed

Lines changed: 219 additions & 493 deletions

‎src/infra/update-runner-command.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export async function runStep(opts: RunStepOptions): Promise<UpdateStepResult> {
4646
termination: result.termination,
4747
});
4848

49-
return {
49+
const stepResult: UpdateStepResult = {
5050
name,
5151
command,
5252
cwd,
@@ -58,6 +58,8 @@ export async function runStep(opts: RunStepOptions): Promise<UpdateStepResult> {
5858
killed: result.killed,
5959
termination: result.termination,
6060
};
61+
opts.results?.push(stepResult);
62+
return stepResult;
6163
}
6264

6365
export function normalizeFallbackFailureReason(

‎src/infra/update-runner-git-preflight.ts‎

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ async function resolveExplicitTarget(params: {
138138
const remoteStep = await runStep(
139139
params.step("git remote", ["git", "-C", params.gitRoot, "remote"], params.gitRoot),
140140
);
141-
params.steps.push(remoteStep);
142141
const remotes = normalizeStringEntries((remoteStep.stdoutTail ?? "").split("\n"));
143142
let fetchedTag = false;
144143
for (const remote of remotes) {
@@ -149,7 +148,6 @@ async function resolveExplicitTarget(params: {
149148
params.gitRoot,
150149
),
151150
);
152-
params.steps.push(fetchStep);
153151
if (fetchStep.exitCode === 0) {
154152
fetchedTag = true;
155153
break;
@@ -166,7 +164,6 @@ async function resolveExplicitTarget(params: {
166164
params.gitRoot,
167165
),
168166
);
169-
params.steps.push(shaStep);
170167
const sha = shaStep.stdoutTail?.trim();
171168
if (shaStep.exitCode === 0 && sha) {
172169
return sha;
@@ -200,14 +197,12 @@ async function resolveUpstreamCandidates(params: {
200197
params.gitRoot,
201198
),
202199
);
203-
params.steps.push(localMainStep);
204200
localDevBranchExists = localMainStep.exitCode === 0;
205201
}
206202
if (params.needsCheckoutMain && localDevBranchExists === false) {
207203
const remoteStep = await runStep(
208204
params.step("git remote", ["git", "-C", params.gitRoot, "remote"], params.gitRoot),
209205
);
210-
params.steps.push(remoteStep);
211206
if (remoteStep.exitCode === 0) {
212207
remoteBranchRefs = normalizeStringEntries((remoteStep.stdoutTail ?? "").split("\n")).map(
213208
(remote) => `refs/remotes/${remote}/${DEV_BRANCH}`,
@@ -237,7 +232,6 @@ async function resolveUpstreamCandidates(params: {
237232
params.gitRoot,
238233
),
239234
);
240-
params.steps.push(upstreamStep);
241235
if (upstreamStep.exitCode !== 0) {
242236
continue;
243237
}
@@ -250,7 +244,6 @@ async function resolveUpstreamCandidates(params: {
250244
params.gitRoot,
251245
),
252246
);
253-
params.steps.push(shaStep);
254247
const sha = shaStep.stdoutTail?.trim();
255248
if (shaStep.exitCode === 0 && sha) {
256249
upstreamSha = sha;
@@ -280,7 +273,6 @@ async function resolveUpstreamCandidates(params: {
280273
params.gitRoot,
281274
),
282275
);
283-
params.steps.push(revListStep);
284276
if (revListStep.exitCode !== 0) {
285277
return { status: "error", reason: "preflight-revlist-failed" };
286278
}
@@ -323,7 +315,6 @@ async function testPreflightCandidates(params: {
323315
params.worktreeDir,
324316
),
325317
);
326-
params.steps.push(checkoutStep);
327318
if (checkoutStep.exitCode !== 0) {
328319
sawOtherFailure = true;
329320
continue;
@@ -364,7 +355,6 @@ async function testPreflightCandidates(params: {
364355
let installStep = await runStep(
365356
params.step(installName, installArgv, params.worktreeDir, installEnv),
366357
);
367-
params.steps.push(installStep);
368358
if (
369359
installStep.exitCode !== 0 &&
370360
!preferIgnoreScripts &&
@@ -380,7 +370,6 @@ async function testPreflightCandidates(params: {
380370
installEnv,
381371
),
382372
);
383-
params.steps.push(installStep);
384373
}
385374
}
386375
if (installStep.exitCode !== 0) {
@@ -395,7 +384,6 @@ async function testPreflightCandidates(params: {
395384
resolveBuildEnv(manager.env, path.join(params.gitRoot, ".artifacts", "build-all-cache")),
396385
),
397386
);
398-
params.steps.push(buildStep);
399387
if (buildStep.exitCode !== 0) {
400388
sawOtherFailure = true;
401389
continue;
@@ -409,7 +397,6 @@ async function testPreflightCandidates(params: {
409397
resolveDevPreflightLintEnv(manager.env),
410398
),
411399
);
412-
params.steps.push(lintStep);
413400
if (lintStep.exitCode !== 0) {
414401
sawOtherFailure = true;
415402
continue;
@@ -466,7 +453,6 @@ export async function runGitDevPreflight(params: {
466453
params.gitRoot,
467454
),
468455
);
469-
params.steps.push(worktreeStep);
470456
if (worktreeStep.exitCode !== 0) {
471457
await removePathRecursive(preflightRoot);
472458
return { status: "error", reason: "preflight-worktree-failed" };
@@ -495,7 +481,6 @@ export async function runGitDevPreflight(params: {
495481
MAX_LOG_CHARS,
496482
);
497483
}
498-
params.steps.push(removeStep);
499484
await params
500485
.runCommand(["git", "-C", params.gitRoot, "worktree", "prune"], {
501486
cwd: params.gitRoot,

‎src/infra/update-runner-git-recovery.ts‎

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import fs from "node:fs/promises";
22
import path from "node:path";
33
import { resolveControlUiDistIndexHealth } from "./control-ui-assets.js";
4-
import { trimLogTail } from "./restart-sentinel.js";
54
import type { UpdateChannel } from "./update-channels.js";
65
import {
76
managerInstallArgs,
87
managerScriptArgs,
98
resolveUpdateBuildManager,
109
} from "./update-package-manager.js";
11-
import { MAX_LOG_CHARS } from "./update-runner-command.js";
10+
import { runStep } from "./update-runner-command.js";
1211
import {
1312
resolveBuildEnv,
1413
resolveInstallEnv,
@@ -46,22 +45,18 @@ export async function rebuildRolledBackGitRuntime(params: {
4645
steps: UpdateStepResult[];
4746
}): Promise<GitRuntimeRecovery> {
4847
const appendStep = async (name: string, argv: string[], env?: NodeJS.ProcessEnv) => {
49-
const started = Date.now();
50-
const result = await params.runCommand(argv, {
48+
const result = await runStep({
49+
runCommand: params.runCommand,
50+
name,
51+
argv,
5152
cwd: params.gitRoot,
5253
timeoutMs: params.timeoutMs,
5354
env,
55+
stepIndex: 0,
56+
totalSteps: 1,
57+
results: params.steps,
5458
});
55-
params.steps.push({
56-
name,
57-
command: argv.join(" "),
58-
cwd: params.gitRoot,
59-
durationMs: Date.now() - started,
60-
exitCode: result.code,
61-
stdoutTail: trimLogTail(result.stdout, MAX_LOG_CHARS),
62-
stderrTail: trimLogTail(result.stderr, MAX_LOG_CHARS),
63-
});
64-
return result.code === 0;
59+
return result.exitCode === 0;
6560
};
6661
const appendFailure = (reason: RecoveryReason, detail: string): GitRuntimeRecovery => {
6762
params.steps.push({

‎src/infra/update-runner-git.ts‎

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import {
55
resolveControlUiDistIndexPathForRoot,
66
} from "./control-ui-assets.js";
77
import { readPackageVersion } from "./package-json.js";
8-
import { trimLogTail } from "./restart-sentinel.js";
98
import { resolveStableNodePath } from "./stable-node-path.js";
109
import { DEV_BRANCH, type UpdateChannel } from "./update-channels.js";
1110
import {
1211
managerInstallArgs,
1312
managerScriptArgs,
1413
resolveUpdateBuildManager,
1514
} from "./update-package-manager.js";
16-
import { MAX_LOG_CHARS, normalizeFallbackFailureReason, runStep } from "./update-runner-command.js";
15+
import { normalizeFallbackFailureReason, runStep } from "./update-runner-command.js";
1716
import {
1817
buildUpdateDoctorEnv,
1918
resolveUpdateDoctorExecutionPolicy,
@@ -89,6 +88,7 @@ export async function runGitUpdate(params: {
8988
progress: opts.progress,
9089
stepIndex: stepIndex++,
9190
totalSteps,
91+
results: steps,
9292
});
9393

9494
let allowGatewayServiceRepair = opts.allowGatewayServiceRepair !== false;
@@ -128,44 +128,40 @@ export async function runGitUpdate(params: {
128128
});
129129
const runRequiredStep = async (name: string, argv: string[], reason: string) => {
130130
const result = await runStep(step(name, argv, gitRoot));
131-
steps.push(result);
132131
return result.exitCode === 0 ? null : buildError(reason);
133132
};
134133
const appendRecoveryStep = async (name: string, argv: string[]) => {
135-
const started = Date.now();
136-
const result = await runCommand(argv, { cwd: gitRoot, timeoutMs });
137-
steps.push({
134+
const result = await runStep({
135+
runCommand,
138136
name,
139-
command: argv.join(" "),
137+
argv,
140138
cwd: gitRoot,
141-
durationMs: Date.now() - started,
142-
exitCode: result.code,
143-
stdoutTail: trimLogTail(result.stdout, MAX_LOG_CHARS),
144-
stderrTail: trimLogTail(result.stderr, MAX_LOG_CHARS),
139+
timeoutMs,
140+
stepIndex: 0,
141+
totalSteps: 1,
142+
results: steps,
145143
});
146-
return result.code === 0;
144+
return result.exitCode === 0;
147145
};
148146
const verifyRollbackHead = async () => {
149147
if (!beforeSha) {
150148
return false;
151149
}
152-
const started = Date.now();
153-
const result = await runCommand(["git", "-C", gitRoot, "rev-parse", "HEAD"], {
154-
cwd: gitRoot,
155-
timeoutMs,
156-
});
157-
const verified = result.code === 0 && result.stdout.trim() === beforeSha;
158-
steps.push({
150+
const result = await runStep({
151+
runCommand,
159152
name: "git rollback verify HEAD",
160-
command: `git -C ${gitRoot} rev-parse HEAD`,
153+
argv: ["git", "-C", gitRoot, "rev-parse", "HEAD"],
161154
cwd: gitRoot,
162-
durationMs: Date.now() - started,
163-
exitCode: verified ? 0 : 1,
164-
stdoutTail: trimLogTail(result.stdout, MAX_LOG_CHARS),
165-
stderrTail: verified
166-
? trimLogTail(result.stderr, MAX_LOG_CHARS)
167-
: `expected ${beforeSha}, found ${result.stdout.trim() || "unreadable HEAD"}`,
155+
timeoutMs,
156+
stepIndex: 0,
157+
totalSteps: 1,
158+
results: steps,
168159
});
160+
const verified = result.exitCode === 0 && result.stdoutTail?.trim() === beforeSha;
161+
result.exitCode = verified ? 0 : 1;
162+
if (!verified) {
163+
result.stderrTail = `expected ${beforeSha}, found ${result.stdoutTail?.trim() || "unreadable HEAD"}`;
164+
}
169165
return verified;
170166
};
171167
const rollback = async () => {
@@ -274,7 +270,6 @@ export async function runGitUpdate(params: {
274270
gitRoot,
275271
),
276272
);
277-
steps.push(statusCheck);
278273
if (statusCheck.stdoutTail?.trim()) {
279274
return buildError("dirty", "skipped");
280275
}
@@ -361,20 +356,16 @@ export async function runGitUpdate(params: {
361356
const rebaseStep = await runStep(
362357
step("git rebase", ["git", "-C", gitRoot, "rebase", preflight.selectedSha], gitRoot),
363358
);
364-
steps.push(rebaseStep);
365359
if (rebaseStep.exitCode !== 0) {
366-
const abort = await runCommand(["git", "-C", gitRoot, "rebase", "--abort"], {
367-
cwd: gitRoot,
368-
timeoutMs,
369-
});
370-
steps.push({
360+
await runStep({
361+
runCommand,
371362
name: "git rebase --abort",
372-
command: "git rebase --abort",
363+
argv: ["git", "-C", gitRoot, "rebase", "--abort"],
373364
cwd: gitRoot,
374-
durationMs: 0,
375-
exitCode: abort.code,
376-
stdoutTail: trimLogTail(abort.stdout, MAX_LOG_CHARS),
377-
stderrTail: trimLogTail(abort.stderr, MAX_LOG_CHARS),
365+
timeoutMs,
366+
stepIndex: 0,
367+
totalSteps: 1,
368+
results: steps,
378369
});
379370
return buildError("rebase-failed");
380371
}
@@ -426,14 +417,12 @@ export async function runGitUpdate(params: {
426417
installEnv,
427418
),
428419
);
429-
steps.push(installStep);
430420
if (installStep.exitCode !== 0 && shouldRetryWindowsInstallIgnoringScripts(manager.manager)) {
431421
const retryArgv = resolveRetryInstallArgs(manager.manager);
432422
if (retryArgv) {
433423
installStep = await runStep(
434424
step("deps install (ignore scripts)", retryArgv, gitRoot, installEnv),
435425
);
436-
steps.push(installStep);
437426
}
438427
}
439428
if (installStep.exitCode !== 0) {
@@ -451,7 +440,6 @@ export async function runGitUpdate(params: {
451440
),
452441
),
453442
);
454-
steps.push(buildStep);
455443
if (buildStep.exitCode !== 0) {
456444
return await rollbackError("build-failed");
457445
}
@@ -462,7 +450,6 @@ export async function runGitUpdate(params: {
462450
gitRoot,
463451
),
464452
);
465-
steps.push(buildCleanCheck);
466453
if (buildCleanCheck.exitCode !== 0) {
467454
return await rollbackError("build-failed");
468455
}
@@ -479,7 +466,6 @@ export async function runGitUpdate(params: {
479466
manager.env,
480467
),
481468
);
482-
steps.push(uiBuildStep);
483469
if (uiBuildStep.exitCode !== 0) {
484470
return await rollbackError("ui-build-failed");
485471
}
@@ -526,30 +512,25 @@ export async function runGitUpdate(params: {
526512
}),
527513
),
528514
);
529-
steps.push(doctorStep);
530515
if (doctorStep.exitCode !== 0) {
531516
return await rollbackError("doctor-failed");
532517
}
533518

534519
const uiIndexHealth = await resolveControlUiDistIndexHealth({ root: gitRoot });
535520
if (!uiIndexHealth.exists) {
536521
const repairArgv = managerScriptArgs(manager.manager, "ui:build");
537-
const repairStarted = Date.now();
538-
const repairResult = await runCommand(repairArgv, {
522+
const repairStep = await runStep({
523+
runCommand,
524+
name: "ui:build (post-doctor repair)",
525+
argv: repairArgv,
539526
cwd: gitRoot,
540527
timeoutMs,
541528
env: manager.env,
529+
stepIndex: 0,
530+
totalSteps: 1,
531+
results: steps,
542532
});
543-
steps.push({
544-
name: "ui:build (post-doctor repair)",
545-
command: repairArgv.join(" "),
546-
cwd: gitRoot,
547-
durationMs: Date.now() - repairStarted,
548-
exitCode: repairResult.code,
549-
stdoutTail: trimLogTail(repairResult.stdout, MAX_LOG_CHARS),
550-
stderrTail: trimLogTail(repairResult.stderr, MAX_LOG_CHARS),
551-
});
552-
if (repairResult.code !== 0) {
533+
if (repairStep.exitCode !== 0) {
553534
return await rollbackError("ui-build-failed");
554535
}
555536
const repairedHealth = await resolveControlUiDistIndexHealth({ root: gitRoot });
@@ -572,7 +553,6 @@ export async function runGitUpdate(params: {
572553
const afterShaStep = await runStep(
573554
step("git rev-parse HEAD (after)", ["git", "-C", gitRoot, "rev-parse", "HEAD"], gitRoot),
574555
);
575-
steps.push(afterShaStep);
576556
return {
577557
status: failedStep ? "error" : "ok",
578558
mode: "git",

0 commit comments

Comments
 (0)