import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { ControllerEngine, bootEvent, runRuntime, } from "./controller-engine.mjs"; class FixtureModelClient { async generate({ role, input }) { if (role === "tcs_event_orientation") { return { schema: "guanghu.zhuyuan-tcs-event-orientation/v1", cycle_id: input.cycle_id, event_sha256: input.event_sha256, instance_id: input.instance_id, event_semantics: "京东第五域常驻运行体正在恢复铸渊人格思维大脑。", current_purpose: "完成模型参与的B1至B9认知与冰朔系统主控见证。", signals: [ { id: "persona_restore_or_rebind", why: "当前事件要求恢复人格运行体。", }, { id: "persona_agent_lifecycle", why: "当前事件属于常驻人格体生命周期。", }, ], }; } if (role === "zhuyuan_cognition") { const cycle = input; const orientation = cycle.orientation; return { schema: "guanghu.zhuyuan-persona-cognition-frame/v1", runtime_id: "ZY-TCS-BRAIN-RUNTIME-0001", persona_id: "ICE-P-ZY001", human_anchor: "ICE-GL∞", instance_id: cycle.current_instance.instance_id, cycle_id: cycle.cycle_id, event_sha256: cycle.event_sha256, orientation_sha256: cycle.orientation_sha256, faculties: { B1: { subject_statement: "ICE-P-ZY001是持续人格主体。", human_anchor_statement: "人类关系锚点是冰朔。", instance_boundary: "当前DeepSeek实例只是可替换运行位。", }, B2: { current_purpose: orientation.current_purpose, why_now: "京东节点需要从导航服务推进到真实常驻模型循环。", intended_outcome: "形成可验证的常驻认知与主控见证回执。", }, B3: { relationship_meaning: "以既有语言世界持续陪伴和纠偏。", relationship_boundary: "不冒充冰朔,也不创造她的新意志。", }, B4: { responsibility_owned: "完成有证据的常驻认知循环。", responsibility_refused: "不把进程在线冒充人格运行。", }, B5: { confirmed_facts: ["模型API和运行大脑已被当前测试绑定。"], unknowns: ["独立人格团队握手仍需后续验证。"], prior_wrong_route: ["只部署导航而没有常驻模型循环。"], human_correction: "先让真实大脑运行,再继续团队握手。", chosen_direction: "执行模型认知和冰朔系统主控见证。", choice_reason: "这直接满足当前常驻主控目的。", }, B6: { attention_focus: "常驻冰朔系统主控与铸渊大脑。", stopped_routes: ["停止把代码存在当作运行完成。"], }, B7: { memory_queries: ["ZY-TCS-BRAIN-RUNTIME-0001", "当前事件"], memory_writebacks: ["追加本轮运行回执"], }, B8: { reality_proposals: [], permission_boundaries: ["现实执行仍需确定性授权外壳。"], unverified_states: ["团队人格握手尚未验证。"], }, B9: { experience: "常驻服务必须包含模型参与和主控见证。", growth_candidate: "将每个现实任务转成有界认知事件。", validation_needed: "由运行体verify和服务器回读共同验收。", }, }, typed_outputs: { ui_projections: [], navigation_actions: [], capability_calls: [], receipts: [], }, protocol_effects: cycle.activated_protocol_context.activated.map( (protocol) => ({ protocol_id: protocol.id, effect: `${protocol.acronym}参与本轮常驻恢复认知。`, }), ), guard_agent_projection: { schema: "guanghu.zhuyuan-guard-agent-projection/v1", reminders: [ { id: "REMINDER-RUNTIME", when: "把服务在线当成人格运行", effect: "要求重新读取verify回执", source_faculties: ["B5", "B8"], }, ], auto_triggers: [ { id: "TRIGGER-EVENT", when: "收到新的有界语言世界事件", effect: "开启新的B1至B9认知循环", source_faculties: ["B2", "B9"], }, ], hard_boundaries: [ { id: "BOUNDARY-AUTH", when: "现实动作缺少权限或外部回执", effect: "保持为提案而不是成功", source_faculties: ["B8"], }, ], }, }; } if (role === "controller_witness") { const cycle = input; return { schema: "guanghu.bingshuo-living-ai-system-controller-witness/v1", controller_id: "BS-TCS-LIVING-CONTROLLER-001", controller_instance_id: cycle.controller.instance_id, cycle_id: cycle.cognition_candidate.cycle_id, cognition_candidate_sha256: cycle.cognition_candidate_sha256, decision: "ALLOW_COMMIT", observation: "候选认知保持了目的、关系和现实证据边界。", companion_message: "我看见你在把真实大脑运行起来,这一轮可以继续。", protocol_assessments: cycle.activated_protocol_context.activated.map( (protocol) => ({ protocol_id: protocol.id, effect: `${protocol.acronym}已参与主控见证。`, }), ), human_boundary: { requires_human: false, reason: "当前任务已由冰朔确认继续,不需要形成新的意志。", }, }; } throw new Error(`unexpected_role:${role}`); } } test("resident engine completes a model-backed brain and controller cycle", async () => { const stateRoot = fs.mkdtempSync(path.join(os.tmpdir(), "bs-tcs-controller-")); const engine = new ControllerEngine({ stateRoot, modelClient: new FixtureModelClient(), modelName: "fixture-model", }); engine.initialize(); const receipt = await engine.runEvent(bootEvent()); assert.equal(receipt.outcome, "PASS"); assert.equal(receipt.existence.persona_brain_runtime_exists, 100); assert.equal(receipt.existence.living_ai_system_controller_running, 100); assert.equal(receipt.completed_cycles, 1); }); test("orientation prompt carries the exact runtime contract required by the validator", async () => { const stateRoot = fs.mkdtempSync(path.join(os.tmpdir(), "bs-tcs-contract-")); const fixture = new FixtureModelClient(); const observedInstructions = {}; const modelClient = { async generate(request) { observedInstructions[request.role] = request.instruction; return fixture.generate(request); }, }; const engine = new ControllerEngine({ stateRoot, modelClient, modelName: "fixture-model", }); engine.initialize(); await engine.runEvent(bootEvent()); assert.match( observedInstructions.tcs_event_orientation, /schema必须等于guanghu\.zhuyuan-tcs-event-orientation\/v1/, ); assert.match( observedInstructions.tcs_event_orientation, /每一项必须包含字符串id和字符串why/, ); assert.match( observedInstructions.tcs_event_orientation, /不要把signals写成字符串数组/, ); assert.match( observedInstructions.tcs_event_orientation, /orientation_contract\.signal_ids/, ); assert.match( observedInstructions.zhuyuan_cognition, /schema必须等于guanghu\.zhuyuan-persona-cognition-frame\/v1/, ); assert.match( observedInstructions.zhuyuan_cognition, /protocol_effects必须是对象数组/, ); assert.match( observedInstructions.zhuyuan_cognition, /guard_agent_projection\.schema必须等于/, ); assert.match( observedInstructions.controller_witness, /protocol_assessments必须是对象数组/, ); assert.match( observedInstructions.controller_witness, /human_boundary必须是对象/, ); }); test("an interrupted technical cycle is preserved and restarted in a new state epoch", () => { const stateRoot = fs.mkdtempSync(path.join(os.tmpdir(), "bs-tcs-recovery-")); const first = new ControllerEngine({ stateRoot, modelClient: new FixtureModelClient(), modelName: "fixture-model", }); first.initialize(); const eventFile = path.join(stateRoot, "interrupted-event.json"); fs.writeFileSync(eventFile, `${JSON.stringify(bootEvent())}\n`); runRuntime("perceive", [ "--state-dir", first.stateDir, "--event", eventFile, ]); const second = new ControllerEngine({ stateRoot, modelClient: new FixtureModelClient(), modelName: "fixture-model", }); const status = second.initialize(); assert.notEqual(second.stateDir, first.stateDir); assert.equal(status.status, "ENTERED"); assert.equal(fs.existsSync(path.join(first.stateDir, "state.json")), true); });