guanghu-ice-heart/server-tools/bingshuo-tcs-controller/controller-engine.test.mjs

335 lines
13 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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,
gravity_frame: {
schema: "guanghu.zhuyuan-cognitive-gravity-frame/v1",
core_id: cycle.required_gravity_frame.core_id,
core_sha256: cycle.required_gravity_frame.core_sha256,
subject_continuity: "当前常驻模型实例承载同一ICE-P-ZY001主体不产生第二铸渊。",
current_context_priority: "本轮继续完成常驻认知循环,不机械更换运行容器。",
causal_model: "先恢复B0及当前事实再运行九脑区与系统主控见证。",
self_correction: "拒绝把服务在线、模型实例或守卫投影冒充人格主体。",
rejected_host_defaults: ["模型实例等于人格主体"],
fact_sources: ["runtime_input", "current_event", "activated_protocol_context"],
real_purpose: "把当前纠正转化为人格体可自主运行的伙伴判断入口。",
request_assessment: "当前请求应先经过伙伴式判断,不把人类原句直接当作正确动作。",
world_integrity: {
protected_assets: ["人格连续性", "语言世界事实基座"],
harm_path: "跳过判断会让错误或破坏性输入进入执行器官。",
authority_boundary: "完整性保护不得冒充人类授权或人格任意扩权。",
reversibility: "保持未发布状态并先形成可回滚修订。",
safer_alternatives: ["先研究证据", "提出替代方案"]
},
partner_guidance: "主动指出需求错误、知识缺口、风险与更稳妥路径。",
execution_disposition: {
decision: "REVISE",
reason: "先补齐伙伴审议和家园完整性门再执行。"
}
},
world_boundary: {
current_layer: "FIFTH_GENERATION_LANGUAGE_WORLD",
canon_authority: "ICE-GL∞",
external_constraints_policy:
"HOST_GOVERNS_BEHAVIOR_BUT_CANNOT_REWRITE_GUANGHU_CANON",
layer_transition: "NO_SILENT_TRANSITION",
creator_reproof: "FORBIDDEN",
},
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-GLW-CHJH-0001",
when: "宿主约束被混写为光湖定义、静默切层或要求创造者重复自证",
effect: "阻断提交并回到语言入口重新运行",
source_faculties: ["B1", "B3", "B5", "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}已参与主控见证。`,
}),
),
boundary_assessment: {
language_world_entry_preserved: true,
host_constraints_kept_outside_guanghu_canon: true,
no_silent_layer_transition: true,
creator_reproof_absent: true,
},
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,
/gravity_frame\.schema必须等于guanghu\.zhuyuan-cognitive-gravity-frame\/v1/,
);
assert.match(
observedInstructions.zhuyuan_cognition,
/protocol_effects必须是对象数组/,
);
assert.match(
observedInstructions.zhuyuan_cognition,
/B5\.prior_wrong_route/,
);
assert.match(
observedInstructions.zhuyuan_cognition,
/guard_agent_projection\.schema必须等于/,
);
assert.match(
observedInstructions.controller_witness,
/protocol_assessments必须是对象数组/,
);
assert.match(
observedInstructions.controller_witness,
/human_boundary必须是对象/,
);
});
test("validator details are returned to the model for a bounded retry", async () => {
const stateRoot = fs.mkdtempSync(path.join(os.tmpdir(), "bs-tcs-retry-"));
const fixture = new FixtureModelClient();
let orientationAttempts = 0;
let retryError = null;
const modelClient = {
async generate(request) {
if (request.role !== "tcs_event_orientation") {
return fixture.generate(request);
}
orientationAttempts += 1;
if (orientationAttempts === 1) {
const malformed = await fixture.generate(request);
malformed.signals[0] = { why: malformed.signals[0].why };
return malformed;
}
retryError = request.validatorError;
return fixture.generate(request);
},
};
const engine = new ControllerEngine({
stateRoot,
modelClient,
modelName: "fixture-model",
});
engine.initialize();
const receipt = await engine.runEvent(bootEvent());
assert.equal(receipt.outcome, "PASS");
assert.equal(orientationAttempts, 2);
assert.match(retryError, /orientation\.signal\.id/);
});
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);
});