Files
mtusi2026lab7/scripts/package-checkpoint.ts

201 lines
6.5 KiB
TypeScript

import { createHash } from "node:crypto";
import {
readFile,
readdir,
stat,
writeFile,
mkdir,
open,
} from "node:fs/promises";
import { resolve, relative } from "node:path";
import { root, run, requireThat, safeFailure } from "./runtime";
import { verifyBackendHandover } from "./preflight-build";
import { assertSameSnapshot } from "./checkpoint-guards";
const hash = (bytes: Uint8Array) =>
createHash("sha256").update(bytes).digest("hex");
async function sourceFiles() {
const files: string[] = [];
async function walk(path: string) {
for (const entry of await readdir(path, { withFileTypes: true })) {
if (
[
".git",
"node_modules",
"__pycache__",
".pytest_cache",
".mypy_cache",
".ruff_cache",
".venv",
"secrets",
"volumes",
"data",
"backups",
"dist",
"build",
"target",
"bin",
"obj",
"coverage",
"submission",
].includes(entry.name) ||
entry.name.startsWith(".local") ||
(entry.name.startsWith(".env") && entry.name !== ".env.example") ||
/\.(zip|key|pem|p12|pfx|jks|log|pyc|pyo)$/.test(entry.name)
)
continue;
const target = resolve(path, entry.name);
if (entry.isDirectory()) await walk(target);
else if (entry.isFile()) {
const name = relative(root, target);
requireThat(!/[\r\n]/.test(name), "UNSAFE_ARCHIVE_ENTRY");
files.push(name);
} else throw new Error("NONREGULAR_SOURCE_ENTRY");
}
}
await walk(root);
return files.sort();
}
try {
const handover = await verifyBackendHandover();
const baseline = JSON.parse(
await readFile(resolve(root, "PUBLIC-BASELINE.json"), "utf8"),
);
requireThat(
/^[a-f0-9]{40}$/.test(baseline.original_head),
"INVALID_PUBLIC_BASELINE",
);
let preserved = 0;
for (const [path, expected] of Object.entries(baseline.files)) {
requireThat(
!path.startsWith("/") && !path.split("/").includes(".."),
"INVALID_PUBLIC_BASELINE_PATH",
);
requireThat(
hash(await readFile(resolve(root, path))) === expected,
"PUBLIC_BASELINE_DRIFT",
);
preserved++;
}
const files = await sourceFiles(),
source: Record<string, string> = {};
for (const path of files) {
if (path === "SOURCE-MANIFEST.json") continue;
const bytes = await readFile(resolve(root, path));
requireThat(bytes.length <= 20 * 1024 * 1024, "SOURCE_ARTIFACT_TOO_LARGE");
source[path] = hash(bytes);
}
const manifest = {
state: "SOURCE_CHECKPOINT_NATIVE_GATES_PENDING",
grade_awarded: false,
backend_checked_at: handover.receipt.checkedAt,
preserved_public_files: preserved,
files: source,
};
for (const [path, expected] of Object.entries(baseline.files))
requireThat(source[path] === expected, "PUBLIC_BASELINE_DRIFT");
const manifestBytes = Buffer.from(JSON.stringify(manifest, null, 2) + "\n");
await writeFile(resolve(root, "SOURCE-MANIFEST.json"), manifestBytes);
const entries = await sourceFiles(),
expected = new Map<string, string>(Object.entries(source));
expected.set("SOURCE-MANIFEST.json", hash(manifestBytes));
const second: Record<string, string> = {};
for (const path of entries)
if (path !== "SOURCE-MANIFEST.json")
second[path] = hash(await readFile(resolve(root, path)));
assertSameSnapshot(source, second);
requireThat(
hash(await readFile(resolve(root, "SOURCE-MANIFEST.json"))) ===
expected.get("SOURCE-MANIFEST.json"),
"SOURCE_SNAPSHOT_CHANGED",
);
// Verify every relative bind path's delivered config/entrypoint. Runtime .local paths are generated by prepare.
const topology = JSON.parse(
await readFile(resolve(root, "compose.yaml"), "utf8"),
);
let binds = 0;
for (const service of Object.values(topology.services) as any[])
for (const mount of service.volumes ?? []) {
if (mount.type !== "bind" || mount.source.startsWith("./.local/"))
continue;
const prefix = mount.source.replace(/^\.\//, "");
requireThat(
expected.has(prefix) ||
entries.some((path) => path.startsWith(prefix + "/")),
"ARCHIVE_CONFIG_BIND_MISSING",
);
binds++;
}
const directory = resolve(root, "submission");
await mkdir(directory, { recursive: true });
const stamp = new Date().toISOString().replace(/[^0-9]/g, "");
const archive = resolve(directory, "vp7-source-checkpoint-" + stamp + ".zip"),
receiptPath = archive + ".json";
const claim = await open(receiptPath, "wx", 0o600);
await claim.writeFile('{"state":"RUNNING"}\n');
await claim.close();
await run(["zip", "-q", "-X", archive, "-@"], {
input: entries.join("\n") + "\n",
});
await run(["unzip", "-tqq", archive]);
const actual = (await run(["unzip", "-Z", "-1", archive])).out
.trim()
.split("\n")
.sort();
requireThat(
JSON.stringify(actual) === JSON.stringify(entries),
"ARCHIVE_ENTRY_SET_MISMATCH",
);
for (const path of entries) {
const child = Bun.spawn(["unzip", "-p", archive, path], {
stdout: "pipe",
stderr: "pipe",
});
const [bytes, stderr, exit] = await Promise.all([
new Response(child.stdout).arrayBuffer(),
new Response(child.stderr).text(),
child.exited,
]);
requireThat(
exit === 0 && hash(new Uint8Array(bytes)) === expected.get(path),
"ARCHIVE_FILE_HASH_MISMATCH",
);
requireThat(
hash(await readFile(resolve(root, path))) === expected.get(path),
"SOURCE_CHANGED_DURING_ARCHIVE",
);
}
requireThat(
JSON.stringify(await sourceFiles()) === JSON.stringify(entries),
"SOURCE_SNAPSHOT_CHANGED",
);
await handover.verify();
const size = (await stat(archive)).size;
await writeFile(
receiptPath,
JSON.stringify(
{
state: "PASS_SOURCE_CHECKPOINT_ONLY",
archive: relative(root, archive),
bytes: size,
sha256: hash(await readFile(archive)),
files: entries.length,
preserved_public_files: preserved,
delivered_bind_sources: binds,
every_file_byte_equal: true,
zip_crc: true,
backend_checked_at: handover.receipt.checkedAt,
native_security_complete: false,
native_telemetry_complete: false,
grade_awarded: false,
},
null,
2,
) + "\n",
);
console.log(
"PASS_CHECKPOINT_ZIP_CRC_ALL_BYTES_PUBLIC_BASELINE_AND_CONFIG_BINDS",
);
} catch (error) {
safeFailure(error);
}