import crypto from "node:crypto"; import fs from "node:fs"; import path from "node:path"; import { createRequire } from "node:module"; import { pathToFileURL } from "node:url"; const root = "/home/mes123456/FreeCADWasmDev/working/FreeCAD"; const build = path.resolve(process.argv[2] || path.join(root, "freecad-source/build-web/configure/bin")); const hash = (value) => crypto.createHash("sha256").update(Buffer.isBuffer(value) ? value : typeof value === "string" ? value : JSON.stringify(value)).digest("hex"); globalThis.__dirname = `${build}${path.sep}`; globalThis.require = createRequire(import.meta.url); const loader = await import(pathToFileURL(path.join(build, "FreeCADWebInspectionRuntimeProbe.js")).href); const kernel = await loader.default({ wasmBinary: fs.readFileSync(path.join(build, "FreeCADWebInspectionRuntimeProbe.wasm")) }); const manifest = JSON.parse(fs.readFileSync(path.join(root, "docs/p6/python-api-manifest.json"), "utf8")); const records = manifest.records.filter((row) => row.module === "Inspection" && row.tier === "tier-1-readonly").map((row) => ({ apiId: row.id, name: row.name, family: row.family, sourcePath: row.evidence?.path, disposition: row.disposition })); if (records.length !== 1 || records[0].apiId !== "py:Inspection:class:InspectionWorkbench") throw new Error(`unexpected Inspection records: ${JSON.stringify(records)}`); const request = { cmd: "inspection.pythonFamilyBatch", args: { tier: "tier-1-readonly", records } }; const invoke = (value) => JSON.parse(String(kernel.ccall("fc_web_dispatch_json", "string", ["string"], [JSON.stringify(value)]))); const success = invoke(request); const repeated = invoke(request); if (success.ok !== true || success.schema !== "freecad-web-inspection/python-family-batch/v1" || success.module !== "Inspection" || success.source !== "freecad-source" || success.count !== records.length || success.resourcesReleased !== true || success.records.length !== records.length) throw new Error("Inspection family dispatch did not return a complete result"); const counts = Object.fromEntries(["exposed", "restricted", "nonportable", "not_implemented"].map((status) => [status, success.records.filter((record) => record.status === status).length])); if (counts.restricted !== 1 || counts.not_implemented !== 0 || counts.exposed !== 0 || counts.nonportable !== 0) throw new Error(`unexpected Inspection disposition counts: ${JSON.stringify(counts)}`); if (success.records.some((record) => record.native !== true || !record.sourceEntry || !record.sourceRevision || !record.code || !record.message)) throw new Error("Inspection native response is missing source/error metadata"); if (JSON.stringify(repeated) !== JSON.stringify(success)) throw new Error("Inspection family replay changed the native result"); const mismatch = invoke({ cmd: "inspection.pythonFamilyBatch", args: { tier: "tier-1-readonly", records: [{ ...records[0], apiId: "py:BIM:class:InspectionWorkbench" }] } }); if (mismatch.ok !== true || mismatch.records?.[0]?.status !== "not_implemented" || mismatch.records?.[0]?.code !== "unmatched_source_record") throw new Error("Inspection module-scoped mismatch was not rejected"); const malformed = invoke({ cmd: "inspection.pythonFamilyBatch", args: { tier: "tier-2-pure", records: [records[0]] } }); if (malformed.ok !== false || malformed.code !== "invalid_request") throw new Error("Inspection invalid tier was not rejected"); const unsupported = invoke({ cmd: "inspection.unknown", args: {} }); if (unsupported.ok !== false || unsupported.code !== "unsupported_command") throw new Error("Inspection unsupported command was not rejected"); const batch = JSON.parse(fs.readFileSync(path.join(root, "docs/current/module-batch-manifest.json"), "utf8")); if (batch.batchId !== "76ff9dd4ab3e500a7cfc5c26" || batch.selected?.module !== "Inspection" || batch.selected?.family !== "generic-object") throw new Error("Inspection focused gate does not match the active module batch"); const evidencePath = `docs/p6/inspection-tier1-readonly-module-batch-${batch.batchId}-runtime.json`; const nativeById = new Map(success.records.map((record) => [record.apiId, record])); for (const row of manifest.records) { const native = nativeById.get(row.id); if (!native) continue; row.disposition = native.status; row.dispositionEvidence = [evidencePath]; row.dispositionReason = native.message; } const report = { schema: "freecad-web-p6-inspection-tier1-readonly-family-runtime/v1", taskId: "FW-P6-FPA-Y", batchId: batch.batchId, module: "Inspection", tier: "tier-1-readonly", family: "generic-object", status: "pass_with_restricted", sourceRevision: "4f4e9244e613ea2df06e70027074a562695048cb", route: { worker: "web/src/inspection.worker.ts", command: "inspection.pythonFamilyBatch", facade: "web/src/inspection-python-family-facade.ts", native: "freecad-source/src/WebAssembly/FreeCADCoreProbe/InspectionRuntimeProbe.cpp:478" }, input: { count: records.length, requestSha256: hash(request) }, result: { count: success.count, resourcesReleased: success.resourcesReleased, dispositionCounts: counts, resultSha256: hash(success) }, replay: { equal: true, resultSha256: hash(repeated) }, mismatchRequest: { result: mismatch, resultSha256: hash(mismatch) }, malformedRequest: { result: malformed, resultSha256: hash(malformed) }, unsupportedRequest: { result: unsupported, resultSha256: hash(unsupported) }, records: success.records.map((record) => ({ apiId: record.apiId, name: record.name, family: record.family, status: record.status, code: record.code, message: record.message, sourceEntry: record.sourceEntry, sourceRevision: record.sourceRevision, evidenceSha256: hash(record) })), artifacts: { module: "Inspection", worker: "inspection.worker.ts", revision: "4f4e9244e613ea2df06e70027074a562695048cb", runtimeUrl: "/kernel/FreeCADWebInspectionRuntimeProbe.js", wasmUrl: "/kernel/FreeCADWebInspectionRuntimeProbe.wasm", dataUrl: null, assetBaseUrl: "/kernel/", buildProfile: "fast-dev", js: { path: path.relative(root, path.join(build, "FreeCADWebInspectionRuntimeProbe.js")), sha256: hash(fs.readFileSync(path.join(build, "FreeCADWebInspectionRuntimeProbe.js"))) }, wasm: { path: path.relative(root, path.join(build, "FreeCADWebInspectionRuntimeProbe.wasm")), sha256: hash(fs.readFileSync(path.join(build, "FreeCADWebInspectionRuntimeProbe.wasm"))) }, }, boundaries: ["InspectionWorkbench is restricted because its official InitGui.py surface requires FreeCADGui unavailable in the headless Web kernel", "the single generic-object record is not promoted to exposed without method-level readonly evidence", "all statuses, source metadata, errors and resource release are returned by compiled FreeCAD through fc_web_dispatch_json", "module-scoped API IDs are required; cross-module names never match"], }; fs.writeFileSync(path.join(root, "docs/p6/python-api-manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`); fs.writeFileSync(path.join(root, "docs/p6/inspection-tier1-readonly-family-runtime.json"), `${JSON.stringify(report, null, 2)}\n`); fs.writeFileSync(path.join(root, evidencePath), `${JSON.stringify(report, null, 2)}\n`); fs.writeFileSync(path.join(root, "docs/freecad-p6-inspection-f1-readonly.log"), `${JSON.stringify({ status: report.status, count: records.length, dispositionCounts: counts, resultSha256: report.result.resultSha256, wasmSha256: report.artifacts.wasm.sha256 })}\n`); console.log(JSON.stringify({ status: report.status, count: records.length, dispositionCounts: counts, resultSha256: report.result.resultSha256, wasmSha256: report.artifacts.wasm.sha256 }));