import { createHash } from "node:crypto"; import { mkdirSync, readFileSync, writeFileSync, lstatSync } from "node:fs"; import { dirname, join } from "node:path"; import { parse, stringify } from "yaml"; export const serviceRoles = Object.freeze({ core: "core", frontend: "frontend", "catalog-db": "catalog", "catalog-migrate": "core", "workspace-maintenance": "core", qdrant: "qdrant", embedding: "embedding", "embedding-model-init": "embedding" }); export const sha256 = (data) => createHash("sha256").update(data).digest("hex"); /** Only distribution assets enter the bundle: never a checkout, environment file or workspace. */ export function prepareBundle({ source, destination, platform, version, revision, images, distribution = "prebuilt" }) { if (!["prebuilt", "source"].includes(distribution)) throw new Error("Unknown installation distribution."); const compose = parse(readFileSync(join(source, "compose.yaml"), "utf8")); if (Object.keys(compose.services).sort().join() !== Object.keys(serviceRoles).sort().join()) throw new Error("Release service contract changed; review packaging before publishing."); for (const [name, role] of Object.entries(serviceRoles)) { const local = distribution === "source" && ["core", "frontend"].includes(role); const pattern = local ? /^sha256:[a-f0-9]{64}$/ : /^docker\.io\/[a-z0-9][a-z0-9._/-]*@sha256:[a-f0-9]{64}$/; if (!pattern.test(images[role] ?? "")) throw new Error("Release requires immutable image references for its distribution."); delete compose.services[name].build; delete compose.services[name].pull_policy; compose.services[name].image = images[role]; compose.services[name].platform = platform; } const files = {}; const write = (name, data) => { mkdirSync(dirname(join(destination, name)), { recursive: true }); writeFileSync(join(destination, name), data); files[name] = sha256(data); }; write("compose.yaml", stringify(compose)); for (const name of ["deploy/compose.local.yaml", "deploy/compose.git-https.yaml", "deploy/compose.git-ssh.yaml", "docker/catalog-db-init.sql", "docker/embedding-model-init.sh"]) { const path = join(source, name); if (!lstatSync(path).isFile()) throw new Error("Release asset must be a regular tracked file."); write(name, readFileSync(path)); } // Any future source bind mount must be deliberately added to the asset allowlist. for (const service of Object.values(compose.services)) { for (const volume of service.volumes ?? []) { const sourcePath = typeof volume === "string" ? volume.split(":")[0] : volume.type === "bind" ? volume.source : undefined; if (sourcePath?.startsWith(".") && !files[sourcePath.replace(/^\.\//, "")]) throw new Error("Source bind mount is missing from the release bundle."); } } const manifest = { schema_version: 1, version, revision, validator_protocol: 1, ...(distribution === "source" ? { distribution } : {}), requirements: { cpus: 2, memory_bytes: 4 * 2 ** 30, disk_bytes: 10 * 2 ** 30 }, components: ["pi", "catalog-migrations", "workspace-maintenance"], images: Object.fromEntries(Object.entries(images).map(([role, reference]) => [role, { [platform]: reference }])), files, compose: ["compose.yaml", "deploy/compose.local.yaml"] }; writeFileSync(join(destination, "release-manifest.json"), JSON.stringify(manifest, null, 2) + "\n"); writeFileSync(join(destination, "README.md"), `# ThothII ${version} — ${platform} Source / Sorgente: ${revision} Distribution / Distribuzione: ${distribution}. ${distribution === 'source' ? 'Application images are local to the producer Docker daemon; this bundle is not a public image release. Le immagini applicative sono locali al daemon Docker del produttore.' : 'Application images are downloaded from Docker Hub. Le immagini applicative vengono scaricate da Docker Hub.'} This prerelease provides images, document/preflight tools, non-interactive platform apply, prepared binding import and workspace/runtime readiness checks. A pending destructive schema review stops apply for a human decision. Windows/Omarchy/macOS and human-reviewed question acceptance remain separate; this is not a certified complete installation. Questa prerelease fornisce immagini, preparazione/preflight, avvio non interattivo, import dei binding e controlli di readiness workspace/runtime. Una revisione distruttiva dello schema ferma apply per una decisione umana. Collaudi Windows/Omarchy/macOS e domanda con revisione umana restano separati: non è ancora un'installazione completa certificata. Use bin/tht and its sibling bin/tht-workspace-documents together; no Node, Python, Bun or application checkout is required on the consumer host. Windows: use the Linux amd64 bundle inside Ubuntu WSL2, not a native Windows shell. Run as the same non-root operator that prepared the documents, with access to local Docker. 1. bin/tht workspace prepare --directory NEW_WORKSPACE --id practice --name Practice 2. bin/tht workspace validate --directory WORKSPACE 3. bin/tht installation prepare --directory NEW_PRIVATE_INSTALLATION 4. bin/tht installation preflight --directory INSTALLATION --release ABSOLUTE_RELEASE_DIR/release-manifest.json 5. Complete the commented documents, generate technical credentials explicitly, then run installation validate and installation plan with --installation ABSOLUTE_INSTALLATION_FILE. 6. bin/tht --installation ABSOLUTE_INSTALLATION_FILE installation apply --plan ABSOLUTE_PLAN --json Images are pinned by digest; runtime credentials and user workspaces are never bundled. Consult the accompanying IT/EN guides for prepared documents, retry behavior and mandatory runtime checks. `); for (const [name, target] of [["standalone-manual-it.md", "GUIDE-IT.md"], ["standalone-manual-en.md", "GUIDE-EN.md"], ["installation-preflight.md", "PREFLIGHT.md"]]) writeFileSync(join(destination, target), readFileSync(join(source, "docs/install", name))); return manifest; }