exportfunctionparseOperatorRequest(value){constv=exactRecord(value,["schemaName","schemaVersion","requestId","commandId","nonce","sourceEventId","sourceActorDiscordId","discordGuildId","discordChannelId","discordMessageId","targetRuntimeId","issuedAt","expiresAt","producerId","producerRuntimeId","authorityClass","status","command"],"operator request");exact(v.schemaName,operatorRequestType,"schemaName");exact(v.schemaVersion,operatorRequestSchema,"schemaVersion");for(constkof["requestId","commandId","nonce","sourceEventId","sourceActorDiscordId","discordGuildId","discordChannelId","discordMessageId","targetRuntimeId","producerRuntimeId"])text(v[k],k);exact(v.producerId,"voidbot","producerId");exact(v.authorityClass,"operator_request_only","authorityClass");exact(v.status,"pending","status");constissued=date(v.issuedAt,"issuedAt"),expires=date(v.expiresAt,"expiresAt");if(expires<=issued||expires-issued>60000)thrownewError("operator request TTL must be positive and at most 60 seconds.");parseCommand(v.command);returnv;}
exportfunctionparseOperatorAdmission(v,{allowLegacy=false}={}){v=exactRecord(v,["schemaName","schemaVersion","admissionId","packet","packetSha256","sourceObserverId","sourceObserverRuntimeId","provider","bifrostAdmissionReceiptId","authority","providerIdentityId","providerSignature"],"operator admission");exact(v.schemaName,operatorAdmissionType,"schemaName");if(v.schemaVersion!==operatorAdmissionSchema&&!(allowLegacy&&v.schemaVersion===legacyOperatorAdmissionSchema))thrownewError("schemaVersion must be the current admission schema.");for(constkof["admissionId","packetSha256","sourceObserverRuntimeId","bifrostAdmissionReceiptId","providerIdentityId"])text(v[k],k);exact(v.sourceObserverId,"voidbot","sourceObserverId");exact(v.provider,"bifrost","provider");exact(v.authority,"exact_operator_command_only","authority");v.providerSignature=bytes(v.providerSignature,"providerSignature",64);parsePacket(v.packet);if(v.schemaVersion===legacyOperatorAdmissionSchema&&["reviews","review"].includes(v.packet.command.kind))thrownewError("legacy v0 admission cannot carry Mind review commands.");returnv;}
exportfunctionparseOperatorResult(v,{allowLegacy=false}={}){constlegacy=v?.schemaVersion===legacyOperatorResultSchema;if(legacy&&!allowLegacy)thrownewError("legacy result is replay-only.");v=exactRecord(v,legacy?["schemaVersion","resultId","commandId","packetSha256","targetRuntimeId","disposition","consequenceKind","consequenceRef","completedAt","privateStateExposed","operatorStatus","stateStatus","coordinatorAction","brakeStatus","detail"]:["schemaVersion","resultId","commandId","packetSha256","targetRuntimeId","disposition","consequenceKind","consequenceRef","completedAt","privateStateExposed","operatorStatus","stateStatus","coordinatorAction","brakeStatus","detail","reviews","reviewCandidateId","reviewDecision"],"operator result");if(!legacy)exact(v.schemaVersion,operatorResultSchema,"schemaVersion");for(constkof["resultId","commandId","packetSha256","targetRuntimeId","disposition","consequenceKind","completedAt"])text(v[k],k);if(!["observed","applied","refused"].includes(v.disposition))thrownewError("result disposition is invalid.");if(v.privateStateExposed!==false)thrownewError("operator result must not expose private state.");for(constkof["consequenceRef","operatorStatus","stateStatus","coordinatorAction","brakeStatus","detail"]){if(typeofv[k]!=="string")thrownewError(`${k} must be a string.`);if(v[k].length>512)thrownewError(`${k} exceeds 512 characters.`);}if(!legacy){for(constkof["reviewCandidateId","reviewDecision"]){if(typeofv[k]!=="string")thrownewError(`${k} must be a string.`);if(v[k].length>256)thrownewError(`${k} exceeds 256 characters.`);}if(!Array.isArray(v.reviews)||v.reviews.length>10)thrownewError("operator reviews must be a bounded array.");v.reviews.forEach(parseReviewSummary);if(v.reviewDecision&&!['adopt','refuse','hold'].includes(v.reviewDecision))thrownewError("reviewDecision is invalid.");}date(v.completedAt,"completedAt");returnv;}
exportfunctionparseOperatorResultReceipt(v,{allowLegacy=false}={}){v=exactRecord(v,["schemaVersion","result","resultPayloadSha256","commandId","packetSha256","targetRuntimeId","completedAt","providerIdentityId","privateStateExposed","executorSignature"],"operator sealed result receipt");constlegacy=v.schemaVersion===legacyOperatorResultReceiptSchema;if(legacy&&!allowLegacy)thrownewError("legacy sealed result is replay-only.");if(!legacy)exact(v.schemaVersion,operatorResultReceiptSchema,"schemaVersion");parseOperatorResult(v.result,{allowLegacy});if(legacy!==(v.result.schemaVersion===legacyOperatorResultSchema))thrownewError("sealed result and embedded result versions differ.");for(constkof["resultPayloadSha256","commandId","packetSha256","targetRuntimeId","completedAt","providerIdentityId"])text(v[k],k);date(v.completedAt,"completedAt");if(v.privateStateExposed!==false)thrownewError("operator sealed result receipt must not expose private state.");v.executorSignature=bytes(v.executorSignature,"executorSignature",64);returnv;}
exportfunctionparseOperatorTerminal(v){v=exactRecord(v,["schemaVersion","requestId","commandId","status","failureCode","recordedAt","retryable","privateStateExposed"],"operator terminal disposition");exact(v.schemaVersion,operatorTerminalSchema,"schemaVersion");for(constkof["requestId","commandId","failureCode","recordedAt"])text(v[k],k);exact(v.status,"refused","status");date(v.recordedAt,"recordedAt");if(v.retryable!==false||v.privateStateExposed!==false)thrownewError("operator terminal disposition must be non-retryable and private-state-free.");returnv;}
exportfunctionparseOperatorDiscordDelivery(v){v=exactRecord(v,["schemaVersion","deliveryId","requestId","requestPayloadSha256","commandId","discordGuildId","discordChannelId","discordInteractionId","targetRuntimeId","status","disposition","failureCode","detail","operatorStatus","stateStatus","coordinatorAction","brakeStatus","reviews","reviewCandidateId","reviewDecision","sealedResultPayloadSha256","executorSignatureSha256","resultProviderIdentityId","recordedAt","privateStateExposed"],"operator Discord delivery");exact(v.schemaVersion,operatorDiscordDeliverySchema,"schemaVersion");for(constkof["deliveryId","requestId","requestPayloadSha256","commandId","discordGuildId","discordChannelId","discordInteractionId","targetRuntimeId","status","recordedAt"])text(v[k],k);if(!/^sha256-[0-9a-f]{64}$/.test(v.requestPayloadSha256))thrownewError("operator Discord delivery request digest is invalid.");if(!["completed","refused"].includes(v.status))thrownewError("operator Discord delivery status is invalid.");for(constkof["disposition","failureCode","detail","operatorStatus","stateStatus","coordinatorAction","brakeStatus","reviewCandidateId","reviewDecision","sealedResultPayloadSha256","executorSignatureSha256","resultProviderIdentityId"])if(typeofv[k]!=="string")thrownewError(`${k} must be a string.`);if(!Array.isArray(v.reviews)||v.reviews.length>10)thrownewError("operator Discord delivery reviews must be bounded.");v.reviews.forEach(parseReviewSummary);if(v.reviewDecision&&!['adopt','refuse','hold'].includes(v.reviewDecision))thrownewError("operator Discord delivery reviewDecision is invalid.");for(constkof["detail","operatorStatus","stateStatus","coordinatorAction","brakeStatus"])if(v[k].length>512)thrownewError(`operator Discord delivery ${k} exceeds 512 characters.`);if(v.status==="completed"&&(!v.disposition||!/^sha256-[0-9a-f]{64}$/.test(v.sealedResultPayloadSha256)||!/^sha256-[0-9a-f]{64}$/.test(v.executorSignatureSha256)||!v.resultProviderIdentityId))thrownewError("completed operator Discord delivery lacks sealed-result binding.");if(v.status==="refused"&&!v.failureCode)thrownewError("refused operator Discord delivery lacks failure code.");date(v.recordedAt,"recordedAt");if(v.privateStateExposed!==false)thrownewError("operator Discord delivery must not expose private state.");returnv;}
functionparseCommand(v){if(!v||typeofv!=="object"||Array.isArray(v))thrownewError("command must be an object.");if(["status","wake","reviews"].includes(v.kind))returnexactRecord(v,["kind"],"command");if(v.kind==="sleep"){exactRecord(v,["kind","reason"],"command");text(v.reason,"reason");if(v.reason.length>512)thrownewError("sleep reason exceeds 512 characters.");returnv;}if(v.kind==="directive"){exactRecord(v,["kind","objective"],"command");text(v.objective,"objective");if(v.objective.length>4096)thrownewError("directive objective exceeds 4096 characters.");returnv;}if(v.kind==="review"){exactRecord(v,["kind","mindRequestId","candidateId","candidateSha256","expectedModelRevision","expectedModelHash","decision"],"command");for(constkof["mindRequestId","candidateId"]){text(v[k],k);if(v[k].length>256)thrownewError(`${k} exceeds 256 characters.`);}hex64(v.candidateSha256,"candidateSha256");hex64(v.expectedModelHash,"expectedModelHash");if(!Number.isSafeInteger(v.expectedModelRevision)||v.expectedModelRevision<0)thrownewError("expectedModelRevision must be an unsigned safe integer.");if(!["adopt","refuse","hold"].includes(v.decision))thrownewError("review decision is invalid.");returnv;}thrownewError("command kind is invalid.");}
functionexactRecord(v,keys,label){if(!v||typeofv!=="object"||Array.isArray(v))thrownewError(`${label} must be an object.`);constactual=Object.keys(v).sort(),expected=[...keys].sort();if(actual.join("\0")!==expected.join("\0"))thrownewError(`${label} has unknown or missing fields.`);returnv;}
functiontext(v,label){if(typeofv!=="string"||!v.trim())thrownewError(`${label} is required.`);returnv;}
functionexact(v,e,label){if(v!==e)thrownewError(`${label} must be ${e}.`);}
functiondate(v,label){text(v,label);if(!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})$/.test(v))thrownewError(`${label} must be RFC3339.`);constn=Date.parse(v);if(!Number.isFinite(n))thrownewError(`${label} must be RFC3339.`);returnn;}
functionbytes(v,label,length){constnormalized=vinstanceofUint8Array?v:Array.isArray(v)&&v.every(x=>Number.isInteger(x)&&x>=0&&x<=255)?Uint8Array.from(v):null;if(!normalized||normalized.length!==length)thrownewError(`${label} must be ${length} bytes.`);returnnormalized;}