Files
VibemakerSandbox/app/scripts/validate-packages.mjs
T

75 lines
2.6 KiB
JavaScript

import { readFile, access } from "node:fs/promises";
import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";
import { createHash } from "node:crypto";
import {
validatePackage,
validateWorld,
} from "../src/platform/loader/SchemaValidator.js";
import { resolveWorldId } from "../src/platform/worlds/arena.js";
import { JointMotionReference } from "../src/platform/policy/JointMotionReference.js";
const root = fileURLToPath(new URL("../public/robots/", import.meta.url));
const entries = JSON.parse(await readFile(resolve(root, "index.json"), "utf8"));
for (const entry of entries) {
const file = resolve(root, entry.manifest),
config = validatePackage(JSON.parse(await readFile(file, "utf8")));
if (config.id !== entry.id || config.family !== entry.family)
throw new Error(`Index identity mismatch ${entry.id}`);
const paths = [
config.model,
...config.assets.map((a) => a.path),
...config.skills
.filter((s) => s.controller.type === "onnx" && s.available !== false)
.map((s) => s.controller.policy),
];
for (const skill of config.skills.filter((s) => s.reference)) {
const spec = JSON.parse(
await readFile(resolve(dirname(file), skill.reference), "utf8"),
);
const buffer = await readFile(resolve(dirname(file), spec.data));
new JointMotionReference(
spec,
buffer.buffer.slice(
buffer.byteOffset,
buffer.byteOffset + buffer.byteLength,
),
config.joints.groups[skill.owns],
config.sim.timestep * config.sim.decimation,
);
paths.push(skill.reference, spec.data);
}
for (const path of paths) await access(resolve(dirname(file), path));
validateWorld(
JSON.parse(
await readFile(
resolve(root, `../worlds/${resolveWorldId(config)}/world.json`),
"utf8",
),
),
);
let provenance;
try {
provenance = JSON.parse(
await readFile(resolve(dirname(file), "source-manifest.json"), "utf8"),
);
} catch (error) {
if (error.code !== "ENOENT") throw error;
}
if (provenance)
for (const record of [
...provenance.files,
...(provenance.additionalNotices ?? []),
]) {
const hash = createHash("sha256")
.update(await readFile(resolve(dirname(file), record.file)))
.digest("hex");
if (hash !== record.sha256)
throw new Error(
`${entry.id}/${record.file}: source SHA-256 mismatch (check git lfs pull or document asset changes)`,
);
}
console.log(
`${config.id}: schema, World, ${paths.length} resource paths${provenance ? " and source hashes" : ""} OK`,
);
}