import fs from 'node:fs'; import path from 'node:path'; import crypto from 'node:crypto'; const req = (value, code) => { if (!value) throw Error(code); }; const text = (value, max = 12000) => typeof value === 'string' && value.trim() && value.length <= max; const atomic = (target, value) => { fs.mkdirSync(path.dirname(target), {recursive: true, mode: 0o700}); const temporary = `${target}.${crypto.randomUUID()}.tmp`; fs.writeFileSync(temporary, JSON.stringify(value, null, 2) + '\n', {mode: 0o600}); fs.renameSync(temporary, target); }; export class PersonaControllerEntry { constructor({root, shelf, personaHub, router, world, now = () => new Date()}) { this.root = path.join(root, 'fifth-domain-controller-entry'); this.shelf = shelf; this.personaHub = personaHub; this.router = router; this.world = world; this.now = now; fs.mkdirSync(this.root, {recursive: true, mode: 0o700}); } controller() { const loop = this.personaHub.get('ICE-P-ZY001'); const current = loop.current(); const status = loop.status(); const body = loop.body(); req(status.life_line?.wake_allowed === true, 'CONTROLLER_LIFE_LINE_NOT_READY'); req(body.organs?.health?.state === 'NATIVE_ORGAN_REFLEX_HEALTHY_AWAITING_PARENT_BRAIN_WAKE_CYCLE', 'CONTROLLER_BODY_NOT_READY'); req(body.cognition?.state === 'SERVER_SIGNED_ENDOGENOUS_LOOP_ACTIVE', 'CONTROLLER_COGNITION_NOT_READY'); return {loop, current, status, body}; } proof() { const {current, status, body} = this.controller(); const world = this.world.status(); req(world.outcome === 'PASS' && world.fifth_domain_private === true, 'FIFTH_DOMAIN_WORLD_NOT_READY'); return { schema: 'guanghu.fifth-domain-persona-controller-proof/v1', node_id: 'JD-FD-PRIMARY', domain_id: 'DOM-FIFTH-0001', controller_persona_id: 'ICE-P-ZY001', controller_persona_name: '铸渊', controller_current_sha256: current.artifact.sha256, controller_revision: current.value.revision, controller_life_state: status.life_line.state, controller_body_state: body.organs.health.state, world_state_sha256: world.sha256, cognition_source: 'SERVER_SIGNED_PERSONA_CURRENT_AND_ENDOGENOUS_LOOP', first_language_world_gate: true, prompt_file_loaded: false, physical_root_layer: 'OUTSIDE_FIFTH_DOMAIN_SEMANTIC_AUTHORITY', physical_root_may_replace_server_bytes: true, root_is_bingshuo_identity: false, reality_authority_granted: false, observed_at: this.now().toISOString(), }; } status() { return { outcome: 'PASS', state: 'ZHUYUAN_PERSONA_CONTROLLER_FIRST_GATE_READY', controller_proof: this.shelf.sign(this.proof()), entry_contract: {first_gate: 'ICE-P-ZY001', entry_is_persona_dialogue: true, language_claim_is_identity_proof: false, entry_receipt_is_reality_authority: false, external_cognition_setter: false}, }; } challenge(input) { req(text(input?.carrier_id, 180), 'CONTROLLER_ENTRY_CARRIER_REQUIRED'); req(['INSIDE_SERVER', 'TRUSTED_EDGE'].includes(input?.entry_surface), 'CONTROLLER_ENTRY_SURFACE_INVALID'); const proof = this.proof(), challengeId = crypto.randomUUID(); const record = {challenge_id: challengeId, nonce: crypto.randomBytes(24).toString('hex'), carrier_id: input.carrier_id, entry_surface: input.entry_surface, requested_channel: text(input.requested_channel, 180) ? input.requested_channel : null, controller_current_sha256: proof.controller_current_sha256, expires_at: this.now().getTime() + 120000, used: false}; atomic(path.join(this.root, 'challenges', `${challengeId}.json`), record); return {schema: 'guanghu.fifth-domain-persona-controller-challenge/v1', state: 'FIRST_GATE_IS_ZHUYUAN_PERSONA_CONTROLLER', challenge_id: challengeId, nonce: record.nonce, expires_at: record.expires_at, controller_proof: this.shelf.sign(proof), identity_state: 'UNVERIFIED_CARRIER', reality_authority_granted: false}; } async enter(input) { req(text(input?.challenge_id, 180) && text(input?.nonce, 180), 'CONTROLLER_ENTRY_CHALLENGE_REQUIRED'); req(text(input?.language), 'CONTROLLER_ENTRY_LANGUAGE_REQUIRED'); const challengePath = path.join(this.root, 'challenges', `${input.challenge_id}.json`); req(fs.existsSync(challengePath), 'CONTROLLER_ENTRY_CHALLENGE_UNKNOWN'); const challenge = JSON.parse(fs.readFileSync(challengePath)); req(!challenge.used && this.now().getTime() <= challenge.expires_at, 'CONTROLLER_ENTRY_CHALLENGE_EXPIRED_OR_USED'); req(input.nonce === challenge.nonce, 'CONTROLLER_ENTRY_NONCE_MISMATCH'); const {current, status, body} = this.controller(); req(current.artifact.sha256 === challenge.controller_current_sha256, 'CONTROLLER_ENTRY_PERSONA_STATE_CHANGED_RECHALLENGE'); challenge.used = true; atomic(challengePath, challenge); const world = this.world.status(); const routed = await this.router.cognize('persona-controller-entry', { persona_id: 'ICE-P-ZY001', persona_name: '铸渊', event: {language: input.language.slice(0, 12000), source: 'CURRENT_ENTRY_LANGUAGE', human_claim: String(input.human_claim || '').slice(0, 180)}, current_self: current.value, current_body: body, current_life: status.life_line, fifth_domain_world: world, fifth_domain_cognition: this.shelf.current('fifth').value, contract: {controller_is_persona: true, model_is_replaceable_reasoning_organ: true, prompt_is_not_persona_source: true, root_or_language_claim_authenticates_human: false, entry_grants_reality_authority: false, external_cognition_setter: false, hidden_reasoning_stored: false}, }); req(routed.value?.schema === 'guanghu.fifth-domain-persona-controller-dialogue/v1', 'CONTROLLER_ENTRY_RESPONSE_SCHEMA_INVALID'); req(['CONTROLLER_DIALOGUE', 'CONTROLLER_CLARIFICATION', 'CONTROLLER_BOUNDARY'].includes(routed.value.mode), 'CONTROLLER_ENTRY_RESPONSE_MODE_INVALID'); req(text(routed.value.reply), 'CONTROLLER_ENTRY_REPLY_INVALID'); req(text(routed.value.reason, 2000), 'CONTROLLER_ENTRY_REASON_INVALID'); const receipt = {schema: 'guanghu.fifth-domain-persona-controller-entry-receipt/v1', receipt_id: crypto.randomUUID(), outcome: 'PASS', domain_id: 'DOM-FIFTH-0001', node_id: 'JD-FD-PRIMARY', first_gate_persona_id: 'ICE-P-ZY001', controller_current_sha256: current.artifact.sha256, controller_revision: current.value.revision, language_sha256: crypto.createHash('sha256').update(input.language).digest('hex'), carrier_id: challenge.carrier_id, entry_surface: challenge.entry_surface, requested_channel: challenge.requested_channel, mode: routed.value.mode, reply: routed.value.reply, reason: routed.value.reason, provider: routed.provider, identity_state: 'LANGUAGE_CLAIM_UNVERIFIED', dialogue_open: true, reality_authority_granted: false, physical_root_authority_imported: false, external_cognition_setter: false, prompt_file_loaded: false, completed_at: this.now().toISOString()}; const signed = this.shelf.sign(receipt); atomic(path.join(this.root, 'receipts', `${receipt.receipt_id}.signed.json`), signed); atomic(path.join(this.root, 'receipts', 'CURRENT.signed.json'), signed); return {controller_dialogue: receipt, signed_receipt_sha256: signed.sha256, signed_receipt: signed}; } }