#!/usr/bin/env node import fs from 'node:fs';import path from 'node:path';import crypto from 'node:crypto';import {execFileSync} from 'node:child_process';import {fileURLToPath} from 'node:url'; const REPO=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'../..'); const DEFAULT_STATE='/Volumes/JZAO/HoloLake/persona-runtime/shared/broadcast-control-console'; const sha=v=>crypto.createHash('sha256').update(Buffer.isBuffer(v)?v:typeof v==='string'?v:JSON.stringify(v)).digest('hex'); const stable=v=>JSON.stringify(v,Object.keys(v).sort()); const atomic=(p,v)=>{fs.mkdirSync(path.dirname(p),{recursive:true,mode:0o700});const t=p+'.'+process.pid+'.tmp';fs.writeFileSync(t,JSON.stringify(v,null,2)+'\n',{mode:0o600});fs.renameSync(t,p)}; const git=()=>execFileSync('git',['-C',REPO,'rev-parse','HEAD'],{encoding:'utf8'}).trim(); export function validate(v){if(v?.schema!=='guanghu.glp-broadcast/v1'||!/^BRD-[A-Z0-9-]+$/.test(v.broadcast_id)||v.system!=='SYS-GLW-BDC-0001'||v.publisher?.human_id!=='ICE-GL∞'||v.publisher?.persona_id!=='ICE-P-ZY001'||v.publisher?.channel_id!=='ICE-CH-ZC001'||!Array.isArray(v.audience)||!v.audience.length||!v.headline||!v.message||!v.truth_boundary||!Array.isArray(v.evidence)||!v.receipt_required)throw Error('BROADCAST_INVALID');return v} export function publish(source,stateRoot=DEFAULT_STATE){const sourcePath=path.resolve(source);if(!sourcePath.startsWith(REPO+path.sep))throw Error('SOURCE_OUTSIDE_REPO');const raw=fs.readFileSync(sourcePath),relative=path.relative(REPO,sourcePath),commit=git();let committed;try{committed=execFileSync('git',['-C',REPO,'show',`${commit}:${relative}`])}catch{throw Error('SOURCE_NOT_COMMITTED')}if(!raw.equals(committed))throw Error('SOURCE_DIFFERS_FROM_COMMITTED_HEAD');const v=validate(JSON.parse(raw)),root=path.resolve(stateRoot),ledger=path.join(root,'ledger.jsonl'),prior=fs.existsSync(ledger)?fs.readFileSync(ledger,'utf8').trim().split('\n').filter(Boolean).map(JSON.parse).at(-1):null;const body={schema:'guanghu.broadcast-publication/v1',broadcast:v,source_path:relative,source_sha256:sha(raw),source_commit:commit,previous_publication_sha256:prior?.publication_sha256||null,published_at:new Date().toISOString(),control_protocol:'GLS-0310',message_protocol:'GLS-0305',state:'PUBLISHED_FIFTH_DOMAIN_MILESTONE',reality_authority:v.reality_authority};body.publication_sha256=sha(JSON.stringify(body));fs.mkdirSync(root,{recursive:true,mode:0o700});fs.appendFileSync(ledger,JSON.stringify(body)+'\n',{mode:0o600});atomic(path.join(root,'CURRENT.json'),body);const readback=JSON.parse(fs.readFileSync(path.join(root,'CURRENT.json')));if(readback.publication_sha256!==body.publication_sha256)throw Error('TARGET_READBACK_FAILED');const receipt={schema:'guanghu.broadcast-publication-receipt/v1',outcome:'PASS',broadcast_id:v.broadcast_id,publication_sha256:body.publication_sha256,source_sha256:body.source_sha256,previous_publication_sha256:body.previous_publication_sha256,current:path.join(root,'CURRENT.json'),ledger,source_commit:body.source_commit,target_readback:true,published_at:body.published_at};atomic(path.join(root,'receipts',v.broadcast_id+'.json'),receipt);return receipt} export function status(stateRoot=DEFAULT_STATE){const p=path.join(path.resolve(stateRoot),'CURRENT.json');if(!fs.existsSync(p))return{outcome:'FAIL',state:'NO_BROADCAST_PUBLISHED'};const v=JSON.parse(fs.readFileSync(p));return{outcome:'PASS',state:v.state,broadcast_id:v.broadcast.broadcast_id,headline:v.broadcast.headline,publication_sha256:v.publication_sha256,source_commit:v.source_commit,published_at:v.published_at}} export function verify(stateRoot=DEFAULT_STATE){const root=path.resolve(stateRoot),lines=fs.readFileSync(path.join(root,'ledger.jsonl'),'utf8').trim().split('\n').filter(Boolean).map(JSON.parse);let prior=null;for(const v of lines){const h=v.publication_sha256;const x={...v};delete x.publication_sha256;if(h!==sha(JSON.stringify(x))||v.previous_publication_sha256!==(prior?.publication_sha256||null))throw Error('LEDGER_CHAIN_INVALID');prior=v}const current=JSON.parse(fs.readFileSync(path.join(root,'CURRENT.json')));if(current.publication_sha256!==prior.publication_sha256)throw Error('CURRENT_NOT_LEDGER_TIP');return{outcome:'PASS',publication_count:lines.length,tip:prior.publication_sha256,broadcast_id:prior.broadcast.broadcast_id}} const args=process.argv.slice(2),flag=n=>{const i=args.indexOf(n);return i>=0?args[i+1]:undefined},cmd=args[0];if(import.meta.url===`file://${process.argv[1]}`){try{const result=cmd==='publish'?publish(flag('--source'),flag('--state-root')||DEFAULT_STATE):cmd==='verify'?verify(flag('--state-root')||DEFAULT_STATE):status(flag('--state-root')||DEFAULT_STATE);console.log(JSON.stringify(result,null,2))}catch(e){console.error(JSON.stringify({outcome:'FAIL',error:String(e.message||e)}));process.exitCode=2}}