|
| 1 | +#!/usr/bin/env node |
| 2 | +import { execFileSync, spawnSync } from "node:child_process"; |
| 3 | +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { dirname, join, resolve } from "node:path"; |
| 6 | + |
| 7 | +const FORWARDED_OPTIONS = new Set([ |
| 8 | + "--artifact", |
| 9 | + "--producer-run-id", |
| 10 | + "--producer-run-attempt", |
| 11 | + "--producer-workflow-full-ref", |
| 12 | + "--producer-workflow-sha", |
| 13 | +]); |
| 14 | + |
| 15 | +function main() { |
| 16 | + const [rootFlag, root, validatorFlag, validator, policyFlag, policyFile, ...forwarded] = |
| 17 | + process.argv.slice(2); |
| 18 | + if ( |
| 19 | + rootFlag !== "--repository-root" || |
| 20 | + !root || |
| 21 | + validatorFlag !== "--validator" || |
| 22 | + !validator || |
| 23 | + policyFlag !== "--policy" || |
| 24 | + !policyFile |
| 25 | + ) { |
| 26 | + throw new Error("expected --repository-root <path> --validator <path> --policy <path>"); |
| 27 | + } |
| 28 | + const seen = new Set(); |
| 29 | + for (let index = 0; index < forwarded.length; index += 2) { |
| 30 | + const option = forwarded[index]; |
| 31 | + const value = forwarded[index + 1]; |
| 32 | + if (!FORWARDED_OPTIONS.has(option) || seen.has(option) || !value || value.startsWith("--")) { |
| 33 | + throw new Error(`invalid focused evidence verifier option: ${option ?? "<missing>"}`); |
| 34 | + } |
| 35 | + seen.add(option); |
| 36 | + } |
| 37 | + |
| 38 | + const repositoryRoot = resolve(root); |
| 39 | + const validatorPath = resolve(validator); |
| 40 | + const policyPath = resolve(policyFile); |
| 41 | + if (policyPath !== join(dirname(validatorPath), "authorized-beta-focused-policy.json")) { |
| 42 | + throw new Error("focused evidence policy must be the trusted validator's adjacent policy"); |
| 43 | + } |
| 44 | + const policy = JSON.parse(readFileSync(policyPath, "utf8")); |
| 45 | + if ( |
| 46 | + policy === null || |
| 47 | + typeof policy !== "object" || |
| 48 | + Array.isArray(policy) || |
| 49 | + policy.schema !== "openclaw.authorized-beta-focused-policy.v1" || |
| 50 | + policy.mode !== "authorized-beta-focused-v1" || |
| 51 | + typeof policy.candidateSha !== "string" || |
| 52 | + !/^[a-f0-9]{40}$/u.test(policy.candidateSha) |
| 53 | + ) { |
| 54 | + throw new Error("invalid trusted focused evidence policy or candidate SHA"); |
| 55 | + } |
| 56 | + |
| 57 | + const candidateSha = policy.candidateSha; |
| 58 | + const git = (args, options = {}) => |
| 59 | + execFileSync("git", ["-C", repositoryRoot, ...args], { stdio: "inherit", ...options }); |
| 60 | + const hasCandidate = () => |
| 61 | + spawnSync("git", ["-C", repositoryRoot, "cat-file", "-e", `${candidateSha}^{commit}`], { |
| 62 | + stdio: "ignore", |
| 63 | + }).status === 0; |
| 64 | + if (!hasCandidate()) { |
| 65 | + git(["fetch", "--no-tags", "origin", candidateSha], { timeout: 120_000 }); |
| 66 | + if (!hasCandidate()) { |
| 67 | + throw new Error(`trusted focused evidence candidate is unavailable: ${candidateSha}`); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + const directory = mkdtempSync(join(tmpdir(), "authorized-beta-focused-candidate-")); |
| 72 | + const candidateRoot = join(directory, "candidate"); |
| 73 | + let worktreeAdded = false; |
| 74 | + try { |
| 75 | + // Verification reads Git objects only; no checkout also prevents candidate checkout hooks. |
| 76 | + git(["worktree", "add", "--detach", "--no-checkout", candidateRoot, candidateSha]); |
| 77 | + worktreeAdded = true; |
| 78 | + execFileSync( |
| 79 | + process.execPath, |
| 80 | + [validatorPath, "verify", "--candidate-root", candidateRoot, ...forwarded], |
| 81 | + { cwd: repositoryRoot, stdio: "inherit" }, |
| 82 | + ); |
| 83 | + } finally { |
| 84 | + try { |
| 85 | + if (worktreeAdded) { |
| 86 | + git(["worktree", "remove", "--force", candidateRoot]); |
| 87 | + } |
| 88 | + } finally { |
| 89 | + try { |
| 90 | + rmSync(directory, { force: true, recursive: true }); |
| 91 | + } finally { |
| 92 | + git(["worktree", "prune", "--expire", "now"]); |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +try { |
| 99 | + main(); |
| 100 | +} catch (error) { |
| 101 | + console.error(error instanceof Error ? error.message : "unknown error"); |
| 102 | + console.error("[authorized-beta-focused-candidate] FAILED (exit 1)"); |
| 103 | + process.exitCode = 1; |
| 104 | +} |
0 commit comments