import { createHash } from 'node:crypto' import { mkdirSync, readFileSync, writeFileSync } from 'node:fs' import { dirname, resolve } from 'node:path' import { fileURLToPath } from 'node:url' const root = resolve(dirname(fileURLToPath(import.meta.url)), '..') const paths = { identity: resolve(root, 'contracts/zero-core-numbering-kernel.json'), webview: resolve(root, 'contracts/numbered-ipc-registry.json'), broker: resolve(root, 'contracts/direct-local-broker-numbered-registry.json'), output: resolve(root, 'generated/unified-number-coordinate-tree.json'), } function readSource(path) { const bytes = readFileSync(path) return { value: JSON.parse(bytes.toString('utf8')), sha256: createHash('sha256').update(bytes).digest('hex'), } } export function compileUnifiedNumberTree() { const identity = readSource(paths.identity) const webview = readSource(paths.webview) const broker = readSource(paths.broker) const routes = [ ...webview.value.operations.map((route) => ({ transport: 'TAURI_WEBVIEW_NUMBERED_IPC', protocolVersion: webview.value.runtime.protocol_version, callerNumber: webview.value.runtime.caller_number, channelNumber: route.channel_number, moduleNumber: route.module_number, operationNumber: route.operation_number, targetNumber: route.target_number, alias: route.alias, admission: route.admission, effect: route.effect, evidence: 'HASH_CHAINED_NUMBERED_IPC_RECEIPT', path: `HLP-NUMBER-WORLD-ROOT-001/TAURI/${route.channel_number}/${route.module_number}/${route.operation_number}/${route.target_number}`, })), ...broker.value.operations.map((route) => ({ transport: 'DIRECT_LOCAL_NUMBERED_BROKER', protocolVersion: broker.value.runtime.protocol_version, callerNumber: broker.value.runtime.caller_number, channelNumber: route.channel_number, moduleNumber: route.module_number, operationNumber: route.operation_number, targetNumber: route.target_number, alias: route.alias, admission: ['DISCOVER_NEARBY', 'PING', 'GET_BEIJING_TIME'].includes(route.alias) ? 'PREAUTH_LOCAL_SYSTEM_ROUTE' : ['OPEN_VISITOR_SESSION', 'RECEIVE_LANGUAGE'].includes(route.alias) ? 'BOUNDED_VISITOR_ROUTE' : 'AUTHENTICATED_LOCAL_SESSION_WITH_PERSONA_LICENSE_IF_PERSONA_MODE', effect: ['DISCOVER_NEARBY', 'PING', 'GET_BEIJING_TIME', 'GET_PERSONA_CARRIER_LICENSE_STATUS', 'GET_WORK_ENVIRONMENT', 'RESOLVE_CAPABILITY_ROUTE', 'INSPECT_MOUNTED_PNCC_REPOSITORY', 'READ_MOUNTED_PNCC_REMOTE_OBJECT', 'QUERY_PNCC_RECEIPT_PROJECTION', 'INSPECT_DEVELOPMENT_WRITE_LANE'].includes(route.alias) ? 'READ_OR_STATUS' : 'STATE_CHANGE', evidence: 'NUMBERED_RESPONSE_PLUS_SESSION_OR_DOMAIN_RECEIPT', path: `HLP-NUMBER-WORLD-ROOT-001/BROKER/${route.channel_number}/${route.module_number}/${route.operation_number}/${route.target_number}`, })), ].sort((left, right) => left.path.localeCompare(right.path, 'en')) const uniquePaths = new Set(routes.map((route) => route.path)) const uniqueOperations = new Set(routes.map((route) => `${route.transport}:${route.operationNumber}`)) if (routes.length !== uniquePaths.size || routes.length !== uniqueOperations.size) { throw new Error('HOLOLAKE_UNIFIED_NUMBER_TREE_DUPLICATE_COORDINATE') } return { schema: 'hololake.unified-number-coordinate-tree/v1', recordId: 'HLP-UNIFIED-NUMBER-TREE-001', state: 'MACHINE_COMPILED_STARTUP_ENFORCED', rootNumber: 'HLP-NUMBER-WORLD-ROOT-001', identityAuthority: { mapId: identity.value.authority.map_id, mapVersion: identity.value.authority.map_version, namespaces: identity.value.namespaces.map((namespace) => ({ namespaceId: namespace.id, roots: namespace.roots, prefixes: namespace.prefixes, subjectKind: namespace.subject_kind, domainScope: namespace.domain_scope, })), }, sources: [ { recordId: identity.value.record_id, sha256: identity.sha256 }, { recordId: webview.value.record_id, sha256: webview.sha256 }, { recordId: broker.value.record_id, sha256: broker.sha256 }, ], invariants: { numberIsStableCoordinateNotAuthority: true, pathIsUniqueNavigation: true, admissionIsSeparateFromIdentity: true, everyPhysicalCallHasNumberedRoute: true, everyAcceptedCallHasEvidenceClass: true, mismatchedCoordinate: 'FAIL_CLOSED', }, routeCount: routes.length, routes, } } const compiled = compileUnifiedNumberTree() const output = `${JSON.stringify(compiled, null, 2)}\n` if (process.argv.includes('--check')) { if (readFileSync(paths.output, 'utf8') !== output) { throw new Error('HOLOLAKE_UNIFIED_NUMBER_TREE_STALE') } } else { mkdirSync(dirname(paths.output), { recursive: true }) writeFileSync(paths.output, output) }