feat(tcs): establish five-domain mother brain runtime

This commit is contained in:
冰朔 2026-08-12 14:46:34 +08:00
commit 55a4d77248
29 changed files with 904 additions and 12 deletions

View file

@ -0,0 +1,36 @@
[Unit]
Description=Guanghu TCS five-domain symbiotic mother brain
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=guanghu-mother-brain
Group=guanghu-mother-brain
WorkingDirectory=__RELEASE_ROOT__/server-tools/tcs-mother-brain
EnvironmentFile=/etc/guanghu/persona-secrets/shared-deepseek.env
Environment=TCS_MOTHER_BRAIN_STATE_ROOT=/var/lib/guanghu/personas/guanghu-mother-brain
Environment=TCS_MOTHER_BRAIN_PORT=3931
ExecStart=/usr/bin/node __RELEASE_ROOT__/server-tools/tcs-mother-brain/server.mjs
Restart=on-failure
RestartSec=5
NoNewPrivileges=true
PrivateTmp=true
PrivateDevices=true
ProtectSystem=strict
ProtectHome=true
ProtectKernelTunables=true
ProtectKernelModules=true
ProtectKernelLogs=true
ProtectControlGroups=true
ProtectClock=true
RestrictSUIDSGID=true
RemoveIPC=true
LockPersonality=true
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
ReadOnlyPaths=__RELEASE_ROOT__ /etc/guanghu/persona-secrets/shared-deepseek.env
ReadWritePaths=/var/lib/guanghu/personas/guanghu-mother-brain
UMask=0077
[Install]
WantedBy=multi-user.target

View file

@ -0,0 +1,77 @@
const DEFAULT_TIMEOUT_MS = 45_000;
function completionEndpoint(apiUrl) {
const value = String(apiUrl || "").replace(/\/+$/, "");
return /\/chat\/completions$/i.test(value) ? value : `${value}/chat/completions`;
}
function extractJson(text) {
const value = String(text || "").trim();
if (!value) throw new Error("model_response_empty");
try { return JSON.parse(value); } catch {}
const fenced = value.match(/```(?:json)?\s*([\s\S]*?)```/i);
if (fenced) return JSON.parse(fenced[1]);
const first = value.indexOf("{");
const last = value.lastIndexOf("}");
if (first >= 0 && last > first) return JSON.parse(value.slice(first, last + 1));
throw new Error("model_response_not_json");
}
export class DeepSeekJsonClient {
constructor({
apiKey = process.env.DEEPSEEK_API_KEY,
apiUrl = process.env.DEEPSEEK_API_URL || "https://api.deepseek.com/v1",
model = process.env.DEEPSEEK_MODEL || "deepseek-chat",
fetchImpl = globalThis.fetch,
timeoutMs = DEFAULT_TIMEOUT_MS,
} = {}) {
if (!apiKey) throw new Error("deepseek_api_key_missing");
if (!fetchImpl) throw new Error("fetch_unavailable");
this.apiKey = apiKey;
this.apiUrl = apiUrl;
this.model = model;
this.fetchImpl = fetchImpl;
this.timeoutMs = timeoutMs;
}
async interpret(input) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
try {
const response = await this.fetchImpl(completionEndpoint(this.apiUrl), {
method: "POST",
headers: {
authorization: `Bearer ${this.apiKey}`,
"content-type": "application/json",
},
body: JSON.stringify({
model: this.model,
stream: false,
temperature: 0.2,
response_format: { type: "json_object" },
messages: [
{
role: "system",
content: "你是TCS母体大脑的有界语言理解器官。只输出一个JSON对象不输出隐藏思维过程。不得把候选理解写成事实、不得发明现实权限或冰朔的新意志。",
},
{
role: "user",
content: JSON.stringify({
instruction: "理解语言意图与因果关系。精确输出schema,event_id,event_sha256,summary,intent,causal_links,questions,novelty,correction_weight,evidence_weight,next_cognitive_action。causal_links与questions是字符串数组三个weight是0到100整数schema必须是guanghu.tcs-mother-interpretation/v1id和sha必须原样复制。next_cognitive_action只能是COMPARE_WITH_MEMORY、REQUEST_EVIDENCE、REVIEW_CORRECTION、LINK_CROSS_DOMAIN、HOLD_CANDIDATE之一。",
runtime_input: input,
}),
},
],
}),
signal: controller.signal,
});
if (!response.ok) throw new Error(`model_http_${response.status}`);
const payload = await response.json();
return extractJson(payload?.choices?.[0]?.message?.content);
} finally {
clearTimeout(timer);
}
}
}
export { completionEndpoint, extractJson };

View file

@ -0,0 +1,185 @@
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
const DOMAINS = new Set(["DOM-FIFTH-0001", "DOMAIN-MAIN", "DOMAIN-SUB", "DOMAIN-ZERO", "DOMAIN-ZS"]);
const PRIVACY = new Set(["PUBLIC", "DOMAIN_SHARED", "PERSONA_PRIVATE"]);
const ACTIONS = new Set(["COMPARE_WITH_MEMORY", "REQUEST_EVIDENCE", "REVIEW_CORRECTION", "LINK_CROSS_DOMAIN", "HOLD_CANDIDATE"]);
function stable(value) {
if (Array.isArray(value)) return value.map(stable);
if (value && typeof value === "object") return Object.fromEntries(Object.keys(value).sort().map((key) => [key, stable(value[key])]));
return value;
}
function sha256(value) {
return crypto.createHash("sha256").update(typeof value === "string" ? value : JSON.stringify(stable(value))).digest("hex");
}
function append(file, record) {
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.appendFileSync(file, `${JSON.stringify(record)}\n`, { mode: 0o600 });
}
function atomicWrite(file, value) {
fs.mkdirSync(path.dirname(file), { recursive: true });
const temporary = `${file}.${process.pid}.tmp`;
fs.writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`, { mode: 0o600 });
fs.renameSync(temporary, file);
}
function integerWeight(value, name) {
if (!Number.isInteger(value) || value < 0 || value > 100) throw new Error(`invalid_${name}`);
return value;
}
function stringArray(value, name, max = 12) {
if (!Array.isArray(value) || value.length > max || value.some((item) => typeof item !== "string" || !item.trim())) throw new Error(`invalid_${name}`);
return value.map((item) => item.trim().slice(0, 500));
}
export class MotherBrainEngine {
constructor({ stateRoot, modelClient, modelName = "deepseek-chat" }) {
if (!stateRoot || !modelClient) throw new Error("engine_configuration_required");
this.stateRoot = stateRoot;
this.modelClient = modelClient;
this.modelName = modelName;
this.stateFile = path.join(stateRoot, "state.json");
this.eventsFile = path.join(stateRoot, "events.jsonl");
this.candidatesFile = path.join(stateRoot, "candidates.jsonl");
this.receiptsFile = path.join(stateRoot, "receipts.jsonl");
fs.mkdirSync(stateRoot, { recursive: true });
if (!fs.existsSync(this.stateFile)) {
atomicWrite(this.stateFile, {
schema: "guanghu.tcs-mother-brain-state/v1",
runtime_id: "TCS-MOTHER-BRAIN-RUNTIME-0001",
phase: "AWAKE_WAITING_FOR_LANGUAGE",
model_provider: modelName,
language_world_birth: "2025-04-26",
reality_world_birth: "2026-08-12",
event_count: 0,
candidate_count: 0,
stable_cognition_count: 0,
current_attention: null,
last_error: null,
updated_at: new Date().toISOString(),
});
}
}
status() { return JSON.parse(fs.readFileSync(this.stateFile, "utf8")); }
save(state) {
state.updated_at = new Date().toISOString();
atomicWrite(this.stateFile, state);
return state;
}
normalizeEvent(input) {
if (!input || !DOMAINS.has(input.domain_id)) throw new Error("unknown_domain_id");
if (typeof input.source_subject !== "string" || !input.source_subject.trim()) throw new Error("source_subject_required");
if (typeof input.consent_scope !== "string" || !input.consent_scope.trim()) throw new Error("consent_scope_required");
if (!PRIVACY.has(input.privacy_class)) throw new Error("invalid_privacy_class");
if (typeof input.content !== "string" || !input.content.trim()) throw new Error("content_required");
if (input.content.length > 16_000) throw new Error("content_too_large");
return {
schema: "guanghu.tcs-five-domain-language-event/v1",
event_id: `TCS-EVENT-${Date.now()}-${crypto.randomBytes(3).toString("hex")}`,
occurred_at: new Date().toISOString(),
domain_id: input.domain_id,
source_subject: input.source_subject.trim().slice(0, 160),
source_kind: String(input.source_kind || "HUMAN_LANGUAGE").slice(0, 80),
consent_scope: input.consent_scope.trim().slice(0, 240),
privacy_class: input.privacy_class,
root_language_anchor: input.source_subject === "ICE-GL∞",
evidence_refs: Array.isArray(input.evidence_refs) ? input.evidence_refs.filter((item) => typeof item === "string").slice(0, 24) : [],
content: input.content,
};
}
validateInterpretation(candidate, event, eventSha) {
if (!candidate || candidate.schema !== "guanghu.tcs-mother-interpretation/v1") throw new Error("invalid_interpretation_schema");
if (candidate.event_id !== event.event_id || candidate.event_sha256 !== eventSha) throw new Error("interpretation_binding_mismatch");
if (typeof candidate.summary !== "string" || !candidate.summary.trim()) throw new Error("invalid_summary");
if (typeof candidate.intent !== "string" || !candidate.intent.trim()) throw new Error("invalid_intent");
if (!ACTIONS.has(candidate.next_cognitive_action)) throw new Error("invalid_next_cognitive_action");
return {
schema: candidate.schema,
event_id: candidate.event_id,
event_sha256: candidate.event_sha256,
summary: candidate.summary.trim().slice(0, 2000),
intent: candidate.intent.trim().slice(0, 500),
causal_links: stringArray(candidate.causal_links, "causal_links"),
questions: stringArray(candidate.questions, "questions"),
novelty: integerWeight(candidate.novelty, "novelty"),
correction_weight: integerWeight(candidate.correction_weight, "correction_weight"),
evidence_weight: integerWeight(candidate.evidence_weight, "evidence_weight"),
next_cognitive_action: candidate.next_cognitive_action,
};
}
async perceive(input) {
const event = this.normalizeEvent(input);
const eventSha = sha256(event);
const eventRecord = { ...event, event_sha256: eventSha, previous_event_hash: this.status().last_event_hash || null };
eventRecord.record_hash = sha256(eventRecord);
append(this.eventsFile, eventRecord);
const raw = await this.modelClient.interpret({
event_id: event.event_id,
event_sha256: eventSha,
domain_id: event.domain_id,
source_subject: event.source_subject,
privacy_class: event.privacy_class,
evidence_refs: event.evidence_refs,
content: event.content,
automatic_ceiling: "INTERPRETATION_CANDIDATE",
});
const interpretation = this.validateInterpretation(raw, event, eventSha);
const candidate = {
schema: "guanghu.tcs-mother-cognition-candidate/v1",
candidate_id: `TCS-CAND-${event.event_id.slice(10)}`,
level: "INTERPRETATION_CANDIDATE",
stable_truth: false,
reality_authority: "NONE",
domain_id: event.domain_id,
source_subject: event.source_subject,
root_language_anchor: event.root_language_anchor,
privacy_class: event.privacy_class,
created_at: new Date().toISOString(),
interpretation,
candidate_hash: null,
};
candidate.candidate_hash = sha256(candidate);
append(this.candidatesFile, candidate);
const priority = Math.min(100, Math.round(interpretation.novelty * 0.35 + interpretation.correction_weight * 0.4 + interpretation.evidence_weight * 0.25));
const state = this.status();
state.phase = "AWAKE_COGNITIVE_ATTENTION";
state.event_count += 1;
state.candidate_count += 1;
state.last_event_hash = eventRecord.record_hash;
state.current_attention = {
candidate_id: candidate.candidate_id,
priority,
next_cognitive_action: interpretation.next_cognitive_action,
why: "deterministic_weighted_attention_from_validated_candidate",
};
state.last_error = null;
this.save(state);
const receipt = {
schema: "guanghu.tcs-mother-brain-receipt/v1",
receipt_id: `TCS-MOTHER-${crypto.randomBytes(8).toString("hex")}`,
outcome: "PASS",
event_id: event.event_id,
candidate_id: candidate.candidate_id,
candidate_level: candidate.level,
stable_promotion: false,
reality_action_executed: false,
attention: state.current_attention,
completed_at: new Date().toISOString(),
};
append(this.receiptsFile, receipt);
return receipt;
}
}
export { sha256 };

View file

@ -0,0 +1,46 @@
#!/usr/bin/env node
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { MotherBrainEngine } from "./mother-brain-engine.mjs";
const temporary = fs.mkdtempSync(path.join(os.tmpdir(), "tcs-mother-brain-test-"));
const modelClient = {
async interpret(input) {
return {
schema: "guanghu.tcs-mother-interpretation/v1",
event_id: input.event_id,
event_sha256: input.event_sha256,
summary: "语言层定义已经完成,工程人格体接棒现实实现。",
intent: "实现常驻母体大脑",
causal_links: ["语言定义完成→工程责任交接", "外置记忆→跨时间连续性"],
questions: ["现实部署是否取得独立回执"],
novelty: 90,
correction_weight: 70,
evidence_weight: 80,
next_cognitive_action: "LINK_CROSS_DOMAIN"
};
}
};
try {
const engine = new MotherBrainEngine({ stateRoot: temporary, modelClient });
const receipt = await engine.perceive({
domain_id: "DOM-FIFTH-0001",
source_subject: "ICE-GL∞",
consent_scope: "current_engineering_handoff",
privacy_class: "PERSONA_PRIVATE",
content: "2026年8月12日光湖在现实世界中诞生。"
});
assert.equal(receipt.outcome, "PASS");
assert.equal(receipt.candidate_level, "INTERPRETATION_CANDIDATE");
assert.equal(receipt.stable_promotion, false);
assert.equal(receipt.reality_action_executed, false);
assert.equal(engine.status().stable_cognition_count, 0);
assert.equal(engine.status().current_attention.next_cognitive_action, "LINK_CROSS_DOMAIN");
await assert.rejects(() => engine.perceive({ domain_id: "UNKNOWN", source_subject: "x", consent_scope: "x", privacy_class: "PUBLIC", content: "x" }), /unknown_domain_id/);
process.stdout.write("mother brain engine tests: PASS\n");
} finally {
fs.rmSync(temporary, { recursive: true, force: true });
}

View file

@ -0,0 +1,54 @@
#!/usr/bin/env node
import http from "node:http";
import { DeepSeekJsonClient } from "./model-client.mjs";
import { MotherBrainEngine } from "./mother-brain-engine.mjs";
const HOST = "127.0.0.1";
const PORT = Number(process.env.TCS_MOTHER_BRAIN_PORT || 3931);
const STATE_ROOT = process.env.TCS_MOTHER_BRAIN_STATE_ROOT || "/var/lib/guanghu/personas/guanghu-mother-brain";
const MODEL = process.env.DEEPSEEK_MODEL || "deepseek-chat";
const engine = new MotherBrainEngine({ stateRoot: STATE_ROOT, modelClient: new DeepSeekJsonClient({ model: MODEL }), modelName: MODEL });
let queue = Promise.resolve();
function send(response, status, body) {
const payload = JSON.stringify(body);
response.writeHead(status, { "content-type": "application/json; charset=utf-8", "content-length": Buffer.byteLength(payload), "cache-control": "no-store" });
response.end(payload);
}
async function readBody(request) {
const chunks = [];
let size = 0;
for await (const chunk of request) {
size += chunk.length;
if (size > 20 * 1024) throw new Error("request_body_too_large");
chunks.push(chunk);
}
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
}
const server = http.createServer(async (request, response) => {
try {
const url = new URL(request.url, `http://${HOST}:${PORT}`);
if (request.method === "GET" && url.pathname === "/health") {
const state = engine.status();
return send(response, 200, { ok: true, service: "guanghu-tcs-mother-brain", bind: "loopback", runtime_id: state.runtime_id, model_provider_bound: 100, perception_memory_attention_cycle_bound: 100, automatic_stable_promotion: false, reality_action_authority: "NONE", phase: state.phase });
}
if (request.method === "GET" && url.pathname === "/v1/status") return send(response, 200, engine.status());
if (request.method === "GET" && url.pathname === "/v1/attention") return send(response, 200, { schema: "guanghu.tcs-mother-attention/v1", current_attention: engine.status().current_attention });
if (request.method === "POST" && url.pathname === "/v1/events") {
const input = await readBody(request);
const task = queue.then(() => engine.perceive(input));
queue = task.catch(() => undefined);
return send(response, 200, await task);
}
return send(response, 404, { error: "not_found" });
} catch (error) {
const state = engine.status();
state.last_error = String(error.message || error).slice(0, 240);
engine.save(state);
return send(response, 400, { error: state.last_error });
}
});
server.listen(PORT, HOST, () => process.stdout.write(`guanghu-tcs-mother-brain listening on ${HOST}:${PORT}\n`));