hololake-system-architecture/product-source/hololake-native-desktop/scripts/compile-unified-number-tree.mjs

162 lines
7.4 KiB
JavaScript

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'),
gls: resolve(root, 'contracts/gls-runtime-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 gls = readSource(paths.gls)
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')
}
const identityNodes = identity.value.namespaces.map((namespace) => ({
nodeKind: 'IDENTITY_NAMESPACE',
nodeNumber: `HLP-IDENTITY-NS-${namespace.id}`,
namespaceId: namespace.id,
subjectKind: namespace.subject_kind,
domainScope: namespace.domain_scope,
admission: namespace.human_entry ? 'REGISTERED_HUMAN_NAMESPACE' : 'NON_HUMAN_NAMESPACE',
executionState: 'AUTHORITY_RESOLUTION_ONLY',
evidence: identity.value.authority.map_id,
path: `HLP-NUMBER-WORLD-ROOT-001/IDENTITY/${namespace.id}`,
})).sort((left, right) => left.path.localeCompare(right.path, 'en'))
const protocolNodes = [
...gls.value.protocols.map((protocol) => ({
nodeKind: 'GLS_PROTOCOL_SOURCE',
nodeNumber: protocol.id,
protocolId: protocol.id,
title: protocol.title,
sourceState: protocol.source_format,
executionState: protocol.projection_state,
evidence: protocol.source_sha256,
path: `HLP-NUMBER-WORLD-ROOT-001/GLS/SOURCE/${protocol.id}`,
})),
...gls.value.numbered_reference_nodes.map((reference) => ({
nodeKind: 'GLS_REFERENCE_ONLY',
nodeNumber: reference.node_number,
protocolId: reference.protocol_id,
title: reference.title,
sourceState: reference.source_state,
executionState: reference.execution_state,
evidence: reference.evidence_paths[0],
path: `HLP-NUMBER-WORLD-ROOT-001/GLS/REFERENCE/${reference.node_number}`,
})),
].sort((left, right) => left.path.localeCompare(right.path, 'en'))
const allPaths = [...routes.map((route) => route.path), ...identityNodes.map((node) => node.path), ...protocolNodes.map((node) => node.path)]
if (new Set(allPaths).size !== allPaths.length
|| gls.value.reconciliation.unresolved_number_reference_count !== 0
|| gls.value.reconciliation.every_dependency_has_number_coordinate !== true
|| protocolNodes.some((node) => node.nodeKind === 'GLS_REFERENCE_ONLY' && node.executionState !== 'REFERENCE_ONLY_NOT_EXECUTABLE')) {
throw new Error('HOLOLAKE_UNIFIED_NUMBER_TREE_INCOMPLETE_COVERAGE')
}
return {
schema: 'hololake.unified-number-coordinate-tree/v2',
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 },
{ recordId: gls.value.record_id, sha256: gls.sha256 },
],
invariants: {
numberIsStableCoordinateNotAuthority: true,
pathIsUniqueNavigation: true,
admissionIsSeparateFromIdentity: true,
everyPhysicalCallHasNumberedRoute: true,
everyAcceptedCallHasEvidenceClass: true,
everyProtocolReferenceHasNumberCoordinate: true,
referenceOnlyNodesNeverExecutable: true,
unresolvedNumberReferenceCount: 0,
mismatchedCoordinate: 'FAIL_CLOSED',
},
coordinateCount: routes.length + identityNodes.length + protocolNodes.length,
routeCount: routes.length,
identityNodeCount: identityNodes.length,
protocolNodeCount: protocolNodes.length,
referenceOnlyNodeCount: protocolNodes.filter((node) => node.nodeKind === 'GLS_REFERENCE_ONLY').length,
identityNodes,
protocolNodes,
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)
}