exportasyncfunctionloadEpiphanyOperatorTrustAnchor(path,{decode}={}){construntime=decode?{decode}:loadCultNetRuntime().msgpack,value=runtime.decode(awaitreadFile(path));if(!Array.isArray(value)||value.length!==6)thrownewError("Epiphany operator trust anchor must be the raw compact 6-field MessagePack crossing artifact.");returnparseTrustAnchor({schemaVersion:value[0],identityId:value[1],publicKey:value[2],assurance:value[3],identityCreatedAt:value[4],sourceIdentityRecordSha256:value[5]});}
if(receipt.result.schemaVersion==="epiphany.operator_command.status_result.v2"&&admission.packet.command.kind!=="status")thrownewError("Epiphany Status-v2 result is not bound to a Status command.");
if(receipt.providerIdentityId!==anchor.identityId)thrownewError("Epiphany sealed result used an untrusted host identity.");
if(receipt.commandId!==admission.packet.commandId||receipt.packetSha256!==admission.packetSha256||receipt.targetRuntimeId!==admission.packet.targetRuntimeId)thrownewError("Epiphany sealed result receipt is not bound to the admitted command.");
if(receipt.result.commandId!==receipt.commandId||receipt.result.packetSha256!==receipt.packetSha256||receipt.result.targetRuntimeId!==receipt.targetRuntimeId||receipt.result.completedAt!==receipt.completedAt)thrownewError("Epiphany sealed result receipt substituted its embedded application result.");
// Expiry gates first admission, not exact crash recovery. Epiphany may seal
// an already-admitted command after its lease while recovering the one
// idempotent consequence; Bifrost's durable ledger owns whether such a
if(!verifyPurpose(anchor.publicKey,purpose,encode(resultReceiptTuple(receipt)),receipt.executorSignature))thrownewError("Epiphany sealed result signature is invalid.");
functionwaitForReceipt(connection,msgpack,timeoutMs,admission){returnnewPromise((resolveReceipt,reject)=>{constfinish=(fn,value)=>{clearTimeout(timer);connection.off?.("frame",onFrame);connection.off?.("error",onError);fn(value);};constonError=error=>finish(reject,error);constonFrame=frame=>{try{if(frame.channelId!=="schema")thrownewError("Epiphany operator response arrived on a foreign CultNet channel.");constmessage=msgpack.decode(frame.payload);if(!message||message.schemaVersion!=="cultnet.document_put_raw.v0")thrownewError("Epiphany operator response is not a CultNet raw typed document.");constd=message.document;if(!d||d.schemaId!==operatorResultReceiptType||d.recordKey!==admission.packet.commandId||d.payloadEncoding!=="messagepack"||d.sourceRole!=="epiphany-operator-command-executor"||d.sourceRuntimeId!==admission.packet.targetRuntimeId)thrownewError("Epiphany operator response envelope substituted result authority.");constreceipt=msgpack.decode(d.payload);if(d.sourceAgentId!==receipt?.providerIdentityId)thrownewError("Epiphany operator response envelope substituted executor identity.");finish(resolveReceipt,receipt);}catch(error){finish(reject,error);}};consttimer=setTimeout(()=>finish(reject,newError("Timed out waiting for the correlated Epiphany application result.")),timeoutMs);connection.on("frame",onFrame);connection.on("error",onError);});}
asyncfunctionwaitForConnected(connection,timeoutMs){conststarted=Date.now();while(!connection.connected){if(Date.now()-started>=timeoutMs)thrownewError("Timed out establishing the Epiphany CultNet RUDP session.");awaitnewPromise(done=>setTimeout(done,5));}}
functionparseEndpoint(value){required(value,"Epiphany operator RUDP endpoint");consturl=newURL(value);if(url.protocol!=="rudp:"||!url.hostname||!url.port||(url.pathname&&url.pathname!=="/"))thrownewError("Epiphany operator endpoint must be rudp://host:port.");constport=Number(url.port);if(!Number.isInteger(port)||port<1||port>65535)thrownewError("Epiphany operator endpoint port is invalid.");return{host:url.hostname,port};}
functionparseTrustAnchor(value){if(!value||typeofvalue!=="object"||Array.isArray(value))thrownewError("Trusted Epiphany host identity is required.");constkeys=Object.keys(value).sort(),expected=["assurance","identityCreatedAt","identityId","publicKey","schemaVersion","sourceIdentityRecordSha256"].sort();if(keys.join("\0")!==expected.join("\0")||value.schemaVersion!=="epiphany.host_identity_trust_anchor.v0"||typeofvalue.identityId!=="string"||!value.assurance?.trim()||!/^sha256-[0-9a-f]{64}$/.test(value.sourceIdentityRecordSha256)||!Number.isFinite(Date.parse(value.identityCreatedAt)))thrownewError("Trusted Epiphany host identity violates its exact schema.");value.publicKey=value.publicKeyinstanceofUint8Array?value.publicKey:Array.isArray(value.publicKey)&&value.publicKey.every(x=>Number.isInteger(x)&&x>=0&&x<=255)?Uint8Array.from(value.publicKey):null;if(!value.publicKey||value.publicKey.length!==32)thrownewError("Trusted Epiphany host identity public key must be 32 bytes.");constderived=createHash("sha256").update(Buffer.from("epiphany.host-incarnation.identity.v0\0")).update(value.publicKey).digest("hex");if(derived!==value.identityId)thrownewError("Trusted Epiphany host identity id does not match its public key.");returnvalue;}
functionloadCultNetRuntime(){constroot=resolve(import.meta.dirname,".."),projects=resolve(root,".."),cult=resolve(process.env.VOIDBOT_CULTLIB_ROOT||resolve(projects,"CultLib")),pkg=resolve(cult,"packages","cultnet-ts","package.json");if(!existsSync(pkg))thrownewError(`CultNet TypeScript runtime is unavailable at ${pkg}.`);constrequireCult=createRequire(pkg);return{cultnet:requireCult("cultnet-ts"),msgpack:requireCult("@msgpack/msgpack")};}
functionrequired(value,label){if(typeofvalue!=="string"||!value.trim())thrownewError(`${label} is required.`);returnvalue;}