Skip to content

Commit f105b97

Browse files
author
Peter Steinberger
committed
fix: preserve fresh builds through changed-gate fallback
1 parent a9a84ae commit f105b97

2 files changed

Lines changed: 105 additions & 1 deletion

File tree

‎src/repair/target-validation.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,17 +1360,31 @@ export function runAllowedValidationCommandsWithBinding(
13601360
cwd,
13611361
ignoredValidationInputs,
13621362
validationIdentityProofDeadlineAt(deadlineAt),
1363+
preservedRuntimeRoots,
13631364
);
13641365
} catch (error) {
13651366
fallbackError ??= error as Error;
13661367
}
1368+
if (!fallbackError && pendingRuntimeBuild) {
1369+
try {
1370+
assertRuntimeArtifactBuildOutputBinding(
1371+
cwd,
1372+
pendingRuntimeBuild,
1373+
validationIdentityProofDeadlineAt(deadlineAt),
1374+
fallbackRendered,
1375+
);
1376+
} catch (error) {
1377+
fallbackError = error as Error;
1378+
}
1379+
}
13671380
assertValidationCheckoutIdentityWithinCommand(
13681381
cwd,
13691382
baseRef,
1370-
checkoutIdentity,
1383+
activeRuntimeBuild?.protectedIdentity ?? checkoutIdentity,
13711384
validationIdentityProofDeadlineAt(deadlineAt),
13721385
fallbackRendered,
13731386
fallbackError ?? executionError,
1387+
preservedRuntimeRoots,
13741388
);
13751389
if (fallbackError) throw fallbackError;
13761390
executed.push(fallbackRendered);

‎test/repair/target-validation.test.ts‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3900,6 +3900,96 @@ test("intermediate validation cannot tamper with fresh runtime outputs before ar
39003900
);
39013901
});
39023902

3903+
test("changed-gate fallback preserves and protects pending fresh runtime output", () => {
3904+
for (const tamperWithFreshRuntime of [false, true]) {
3905+
const cwd = gitPackageFixture({
3906+
"build:ci-artifacts": "node scripts/build-runtime.mjs",
3907+
"test:serial": "node --test",
3908+
});
3909+
fs.appendFileSync(path.join(cwd, ".gitignore"), "dist/\n");
3910+
const scripts = path.join(cwd, "scripts");
3911+
fs.mkdirSync(scripts, { recursive: true });
3912+
fs.writeFileSync(path.join(scripts, "build-runtime.mjs"), "// trusted fixture shim\n");
3913+
fs.writeFileSync(
3914+
path.join(scripts, "dist-runtime-build-artifact.mjs"),
3915+
[
3916+
'import fs from "node:fs";',
3917+
'if (fs.readFileSync("dist/runtime.js", "utf8") !== "fresh runtime\\n") process.exit(71);',
3918+
'const archive = process.argv[process.argv.indexOf("--archive") + 1];',
3919+
'fs.writeFileSync(archive, "runtime archive proof\\n");',
3920+
"",
3921+
].join("\n"),
3922+
);
3923+
fs.mkdirSync(path.join(cwd, "test"), { recursive: true });
3924+
fs.writeFileSync(path.join(cwd, "test", "example.test.ts"), "export const value = 1;\n");
3925+
git(cwd, "add", ".");
3926+
git(cwd, "commit", "-m", "initial");
3927+
attachOrigin(cwd);
3928+
fs.writeFileSync(path.join(cwd, "test", "example.test.ts"), "export const value = 2;\n");
3929+
3930+
const binDir = fs.mkdtempSync(path.join(os.tmpdir(), "clawsweeper-runtime-fallback-"));
3931+
writeNodeCommandShim(
3932+
binDir,
3933+
"pnpm",
3934+
[
3935+
'const fs = require("node:fs");',
3936+
"const args = process.argv.slice(2);",
3937+
'if (args.includes("build:ci-artifacts")) {',
3938+
' fs.mkdirSync("dist", { recursive: true });',
3939+
' fs.writeFileSync("dist/runtime.js", "fresh runtime\\n");',
3940+
"}",
3941+
'if (args.includes("check:changed")) {',
3942+
' console.error("terminating stalled Vitest process");',
3943+
" process.exit(1);",
3944+
"}",
3945+
...(tamperWithFreshRuntime
3946+
? [
3947+
'if (args.includes("test:serial")) {',
3948+
' fs.writeFileSync("dist/runtime.js", "tampered runtime\\n");',
3949+
"}",
3950+
]
3951+
: []),
3952+
].join("\n"),
3953+
);
3954+
3955+
const smoke =
3956+
"node scripts/dist-runtime-build-artifact.mjs pack-and-smoke --archive dist-runtime-build.tar.zst";
3957+
const execute = () =>
3958+
withPathOnlyPrefix(binDir, () =>
3959+
runAllowedValidationCommands(
3960+
["pnpm build:ci-artifacts", "pnpm check:changed", smoke],
3961+
cwd,
3962+
validationOptions("openclaw/openclaw", {
3963+
allowExpensiveValidation: true,
3964+
pinnedBaseRef: "origin/main",
3965+
toolchain: {
3966+
packageManager: "pnpm",
3967+
baseValidationCommands: [],
3968+
changedGate: { command: "pnpm check:changed", requiredScript: "check:changed" },
3969+
},
3970+
}),
3971+
),
3972+
);
3973+
3974+
if (tamperWithFreshRuntime) {
3975+
assert.throws(
3976+
execute,
3977+
/unsafe validation command mutated fresh runtime build output \(pnpm test:serial test\/example\.test\.ts\)/,
3978+
);
3979+
continue;
3980+
}
3981+
3982+
assert.deepEqual(execute(), [
3983+
"pnpm build:ci-artifacts",
3984+
"git diff --check origin/main...HEAD",
3985+
"pnpm test:serial test/example.test.ts",
3986+
smoke,
3987+
]);
3988+
assert.equal(fs.readFileSync(path.join(cwd, "dist", "runtime.js"), "utf8"), "fresh runtime\n");
3989+
assert.equal(fs.existsSync(path.join(cwd, "dist-runtime-build.tar.zst")), false);
3990+
}
3991+
});
3992+
39033993
test("OpenClaw archive smoke cannot pass by reusing a pre-existing stale build", () => {
39043994
const cwd = gitPackageFixture({ "build:ci-artifacts": "node scripts/build-runtime.mjs" });
39053995
fs.appendFileSync(path.join(cwd, ".gitignore"), "dist/\n");

0 commit comments

Comments
 (0)