@metago-ai/engine 2.1.2 → 2.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BOUNDARIES.md +109 -0
- package/CHANGES.md +4 -4
- package/CONSTITUTION/AXIOMS.md +72 -110
- package/CORE/ATOMS.md +611 -0
- package/CORE/ATTRIBUTES.md +70 -101
- package/CORE/PROTOCOLS.md +134 -116
- package/ENGINE.md +6 -6
- package/ENGINES/engine-runtime.cjs +318 -0
- package/ENGINES/final-engines-runtime.cjs +683 -0
- package/ENGINES/governance-runtime.cjs +582 -0
- package/ENGINES/math-capability-runtime.cjs +451 -0
- package/ENGINES/philosophy-runtime.cjs +427 -0
- package/ENGINES/yili/data/yaoci-1.js +24 -0
- package/ENGINES/yili/data/yaoci-2.js +21 -0
- package/ENGINES/yili/data/yaoci-3.js +21 -0
- package/ENGINES/yili/data/yaoci-4.js +21 -0
- package/ENGINES/yili/data/yaoci.js +17 -0
- package/ENGINES/yili/lib/almanac.js +416 -0
- package/ENGINES/yili/lib/bazi.js +127 -0
- package/ENGINES/yili/lib/diagnose.js +71 -0
- package/ENGINES/yili/lib/hetu-luoshu.js +123 -0
- package/ENGINES/yili/lib/index.js +23 -0
- package/ENGINES/yili/lib/liuyao.js +226 -0
- package/ENGINES/yili/lib/qimen.js +167 -0
- package/ENGINES/yili/lib/shushu-calculus.js +85 -0
- package/ENGINES/yili/lib/taiyi.js +83 -0
- package/ENGINES/yili/lib/vendor/shouxing-core.js +627 -0
- package/ENGINES/yili/lib/wuxing.js +106 -0
- package/ENGINES/yili/lib/yijing.js +226 -0
- package/ENGINES/yili/lib/zeri.js +80 -0
- package/ENGINES/yili/lib/ziwei.js +145 -0
- package/ENGINES/yili/package.json +1 -0
- package/EXECUTORS.md +85 -0
- package/GENOME.json +11 -10
- package/INDEX/ENGINES.md +168 -0
- package/INDEX/engines.json +161 -161
- package/INDEX/knowledge.json +2 -2
- package/INDEX/skills.json +29 -5
- package/INDEX/tools.json +64 -64
- package/PATENTS.md +1 -1
- package/PROTOCOLS/closure-runtime.cjs +288 -0
- package/PROTOCOLS/covenant-runtime.cjs +409 -0
- package/PROTOCOLS/final-runtime.cjs +680 -0
- package/PROTOCOLS/formula-runtime.cjs +399 -0
- package/PROTOCOLS/proactive-runtime.cjs +368 -0
- package/PROTOCOLS/rule-runtime.cjs +448 -0
- package/PROTOCOLS/runtime.cjs +441 -0
- package/README.md +5 -5
- package/RUNTIME/dist/kmwi-memory.d.ts.map +1 -1
- package/RUNTIME/dist/kmwi-memory.js +14 -2
- package/RUNTIME/dist/kmwi-memory.js.map +1 -1
- package/RUNTIME/dist/validators.d.ts +2 -2
- package/RUNTIME/dist/validators.js +19 -10
- package/atoms/approx.cjs +293 -0
- package/atoms/atom-factory.cjs +550 -0
- package/atoms/atoms-cosmic-bio.cjs +340 -0
- package/atoms/atoms-crypto-emotion.cjs +215 -0
- package/atoms/atoms_engines.json +5821 -0
- package/atoms/blueprint-atoms-2.cjs +583 -0
- package/atoms/blueprint-atoms.cjs +975 -0
- package/atoms/constraints.cjs +257 -0
- package/atoms/contract-validator.cjs +213 -0
- package/atoms/engine.cjs +849 -0
- package/atoms/specialized.cjs +530 -0
- package/atoms/verify.cjs +94 -0
- package/package.json +97 -91
- package/scripts/verify-dist.cjs +80 -0
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* P6-B9 · 数学扩展 / 能力 / 治理 / 智能体 / 调度 引擎运行时(28 条)
|
|
4
|
+
* 依 ENGINE-CONTRACTS.json 功能描述与元构通用模式实现;每条的推导依据记入 spec 字段。
|
|
5
|
+
*/
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
let __seq = 0;
|
|
9
|
+
const newTraceId = () => {
|
|
10
|
+
const d = new Date(), p = (n, w = 2) => String(n).padStart(w, '0');
|
|
11
|
+
return `${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}-${d.getHours()}${p(d.getMinutes())}${p(d.getSeconds())}-M${p((++__seq) % 10000, 4)}`;
|
|
12
|
+
};
|
|
13
|
+
const clamp01 = (x) => Math.max(0, Math.min(1, Number(x) || 0));
|
|
14
|
+
const round = (x, n = 6) => Math.round((Number(x) || 0) * 10 ** n) / 10 ** n;
|
|
15
|
+
const mean = (a) => (a.length ? a.reduce((s, x) => s + (Number(x) || 0), 0) / a.length : 0);
|
|
16
|
+
const shaShort = (s) => require('crypto').createHash('sha256').update(String(s)).digest('hex').slice(0, 16);
|
|
17
|
+
|
|
18
|
+
const E = [];
|
|
19
|
+
|
|
20
|
+
/* ══════ 数学扩展族 5 ══════ */
|
|
21
|
+
E.push({
|
|
22
|
+
id: 'ENGINE_INFINITE_PRODUCT_48', name: '无穷乘积分析引擎', kind: 'compute', spec: '第1部分 §2.1(898 字符)· 收敛性判据',
|
|
23
|
+
implements: ['部分积序列 → 收敛判定(相邻比趋于 1 且部分积有界)', '对数化求和(∏a_n 收敛 ⇔ Σln a_n 收敛)'],
|
|
24
|
+
exec(inp, tid) {
|
|
25
|
+
const terms = inp.terms;
|
|
26
|
+
if (!Array.isArray(terms) || terms.length < 2) return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: '必须提供长度≥2 的 terms' };
|
|
27
|
+
const t = terms.map(Number);
|
|
28
|
+
let prod = 1; const partial = [];
|
|
29
|
+
for (const x of t) { prod *= x; partial.push(round(prod, 10)); }
|
|
30
|
+
const logs = t.map((x) => Math.log(Math.abs(x instanceof Object ? x : x)) || 0);
|
|
31
|
+
const logSum = round(logs.reduce((a, b) => a + (isFinite(b) ? b : 0), 0));
|
|
32
|
+
const ratio = t.length > 1 ? round(Math.abs(t[t.length - 1] / (t[0] || 1))) : null;
|
|
33
|
+
const bounded = partial.every((p) => Math.abs(p) < 1e12);
|
|
34
|
+
return { success: true, kind: 'compute', output: {
|
|
35
|
+
terms: t.length, partialProducts: partial.slice(-5), finalProduct: round(prod, 10),
|
|
36
|
+
logSum, converges: bounded, ratioTest: ratio,
|
|
37
|
+
formula: '∏a_n 收敛 ⇔ Σ ln|a_n| 收敛', caveat: '有限截断无法证明收敛,仅作数值趋势判断',
|
|
38
|
+
}, provenance: [`[${tid}] infinite-product: ${t.length} 项,部分积 ${round(prod, 6)}`] };
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
E.push({
|
|
43
|
+
id: 'ENGINE_EXPLICIT_FORMULA_56', name: '显式公式引擎', kind: 'compute', spec: '第1部分 §2.1(898 字符,V16.3→V16.4)· 多零点显式公式',
|
|
44
|
+
implements: ['Riemann 显式公式主项:ψ(x) ≈ x − Σ x^ρ/ρ − ln(2π)', '多零点逐项累加'],
|
|
45
|
+
exec(inp, tid) {
|
|
46
|
+
const x = Number(inp.x);
|
|
47
|
+
if (!isFinite(x) || x <= 1) return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: '必须提供 x > 1' };
|
|
48
|
+
const zeros = inp.zeros || [{ re: 0.5, im: 14.134725 }];
|
|
49
|
+
let osc = 0;
|
|
50
|
+
for (const z of zeros) {
|
|
51
|
+
const a = Number(z.re ?? 0.5), b = Number(z.im ?? 0);
|
|
52
|
+
const mod = Math.pow(x, a);
|
|
53
|
+
/* x^ρ/ρ 的实部贡献(共轭成对 → 2·Re) */
|
|
54
|
+
const denom = a * a + b * b || 1;
|
|
55
|
+
osc += 2 * mod * (a * Math.cos(b * Math.log(x)) + b * Math.sin(b * Math.log(x))) / denom;
|
|
56
|
+
}
|
|
57
|
+
const psi = round(x - osc - Math.log(2 * Math.PI));
|
|
58
|
+
return { success: true, kind: 'compute', output: {
|
|
59
|
+
x, zeros: zeros.length, oscillation: round(osc), psiApprox: psi,
|
|
60
|
+
formula: 'ψ(x) ≈ x − Σ_ρ x^ρ/ρ − ln(2π)', note: '仅含给定零点,非全零点集;用于趋势与振荡分析',
|
|
61
|
+
}, provenance: [`[${tid}] explicit-formula: x=${x} zeros=${zeros.length} → ψ≈${psi}`] };
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
E.push({
|
|
66
|
+
id: 'ENGINE_CLASSICAL_THEOREM_57', name: '经典定理调用引擎', kind: 'compute', spec: '第1部分(推断:定理库匹配)',
|
|
67
|
+
implements: ['定理库关键词匹配 + 适用范围校验'],
|
|
68
|
+
exec(inp, tid) {
|
|
69
|
+
const q = String(inp.query || '');
|
|
70
|
+
if (!q) return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: '必须提供 query' };
|
|
71
|
+
const LIB = [
|
|
72
|
+
{ name: '勾股定理', pat: /直角|勾股|斜边/, cond: '欧氏平面直角三角形' },
|
|
73
|
+
{ name: '费马小定理', pat: /素数.*模|mod.*素数|a\^p/, cond: 'p 为素数且 gcd(a,p)=1' },
|
|
74
|
+
{ name: '欧拉公式', pat: /e\^\{?i|复数.*指数|欧拉/, cond: '任意复数 θ' },
|
|
75
|
+
{ name: '中值定理', pat: /连续.*可导|中值/, cond: '闭区间连续开区间可导' },
|
|
76
|
+
{ name: '贝叶斯定理', pat: /条件概率|先验|后验/, cond: 'P(B)>0' },
|
|
77
|
+
{ name: '哥德尔不完备', pat: /不完备|自指|可证性/, cond: '足够强的形式系统' },
|
|
78
|
+
{ name: '泰勒展开', pat: /展开|近似|泰勒/, cond: '函数在展开点足够阶可导' },
|
|
79
|
+
{ name: '切比雪夫不等式', pat: /大偏差|尾部概率|方差界/, cond: '有限方差的随机变量' },
|
|
80
|
+
];
|
|
81
|
+
const hits = LIB.filter((l) => l.pat.test(q));
|
|
82
|
+
return { success: true, kind: 'compute', output: { query: q, matches: hits, count: hits.length,
|
|
83
|
+
note: hits.length ? '须先校验适用条件,不满足则不可套用' : '未命中定理库' },
|
|
84
|
+
provenance: [`[${tid}] classical-theorem: 命中 ${hits.length} 条`] };
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
E.push({
|
|
89
|
+
id: 'ENGINE_META_MATH_67', name: '元数学分析引擎', kind: 'compute', spec: '第2部分(推断:可证明性/一致性分析)',
|
|
90
|
+
implements: ['四维元数学评估(一致性/完备性/可判定性/表达力)'],
|
|
91
|
+
exec(inp, tid) {
|
|
92
|
+
const d = inp.dims || { consistency: 0.9, completeness: 0.6, decidability: 0.5, expressiveness: 0.8 };
|
|
93
|
+
const W = { consistency: 0.35, completeness: 0.2, decidability: 0.2, expressiveness: 0.25 };
|
|
94
|
+
const score = round(Object.keys(W).reduce((s, k) => s + W[k] * clamp01(d[k]), 0));
|
|
95
|
+
return { success: true, kind: 'compute', output: { dims: d, weights: W, metaScore: score,
|
|
96
|
+
verdict: score >= 0.8 ? '系统性质良好' : score >= 0.6 ? '可接受(需注意不完备性)' : '存在根本限制',
|
|
97
|
+
caveat: '哥德尔不完备定理:足够强的系统无法同时完备且一致' },
|
|
98
|
+
provenance: [`[${tid}] meta-math: 四维加权 ${score}`] };
|
|
99
|
+
},
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
E.push({
|
|
103
|
+
id: 'ENGINE_LARGE_DEVIATION_65', name: '大偏差与统计检验引擎', kind: 'compute', spec: '第2部分(推断:Chebyshev/Chernoff 界)',
|
|
104
|
+
implements: ['切比雪夫界 P(|X−μ|≥kσ) ≤ 1/k²', 'Hoeffding 界(有界随机变量)'],
|
|
105
|
+
exec(inp, tid) {
|
|
106
|
+
const k = Number(inp.k);
|
|
107
|
+
if (!isFinite(k) || k <= 0) return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: '必须提供 k > 0(偏离倍数)' };
|
|
108
|
+
const cheb = round(Math.min(1, 1 / (k * k)));
|
|
109
|
+
const n = Number(inp.n ?? 30), range = Number(inp.range ?? 1);
|
|
110
|
+
/* Hoeffding: P(|X̄−μ|≥t) ≤ 2exp(−2nt²/range²) */
|
|
111
|
+
const t = Number(inp.t ?? k * (range / Math.sqrt(n)));
|
|
112
|
+
const hoeff = round(Math.min(1, 2 * Math.exp(-2 * n * t * t / (range * range))));
|
|
113
|
+
return { success: true, kind: 'compute', output: { k, chebyshevBound: cheb, n, range, t, hoeffdingBound: hoeff,
|
|
114
|
+
formula: { chebyshev: 'P(|X−μ|≥kσ) ≤ 1/k²', hoeffding: 'P(|X̄−μ|≥t) ≤ 2exp(−2nt²/R²)' },
|
|
115
|
+
note: '均为上界,可能很松' },
|
|
116
|
+
provenance: [`[${tid}] large-deviation: k=${k} → Chebyshev ${cheb},Hoeffding ${hoeff}`] };
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
/* ══════ 能力族 6 ══════ */
|
|
121
|
+
E.push({
|
|
122
|
+
id: 'ENGINE_ABSORPTION_25', name: '能力吸收引擎', kind: 'pipeline', spec: '附录卷-第2部分 L6285(扫描/识别/指纹/预评估)',
|
|
123
|
+
implements: ['四步:扫描节点 → 识别新能力 → 生成能力指纹(不可逆哈希)→ 价值预评估'],
|
|
124
|
+
exec(inp, tid) {
|
|
125
|
+
const nodes = inp.nodes || [];
|
|
126
|
+
const newCaps = inp.newCapabilities || [];
|
|
127
|
+
const STEP = ['扫描组织智能体节点', '识别新能力', '生成能力指纹', '价值预评估'];
|
|
128
|
+
const fps = newCaps.map((c) => ({ capability: c.name || c.id, fingerprint: '0x' + shaShort(JSON.stringify(c)) }));
|
|
129
|
+
const value = round(mean(newCaps.map((c) => clamp01(c.preliminaryValue ?? 0.5))));
|
|
130
|
+
const ok = nodes.length > 0 && newCaps.length > 0;
|
|
131
|
+
return { success: ok, kind: 'pipeline', output: { steps: STEP, nodesScanned: nodes.length, capabilitiesIdentified: newCaps.length, fingerprints: fps, preliminaryValue: value, layer: 'L8创新创造层' },
|
|
132
|
+
error: ok ? undefined : '需提供 nodes 与 newCapabilities', reason: ok ? undefined : 'PRECONDITION_FAILED',
|
|
133
|
+
provenance: [`[${tid}] absorption: 扫 ${nodes.length} 节点,识别 ${newCaps.length} 新能力,价值 ${value}`] };
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
E.push({
|
|
138
|
+
id: 'ENGINE_ENCAPSULATE_24', name: '封装进化引擎', kind: 'pipeline', spec: '推断(与 ABSORPTION_25 配对的封装侧)',
|
|
139
|
+
implements: ['四步:标准化转换 → 生成接口 → 预计算耦生度 → 纳入能力库'],
|
|
140
|
+
exec(inp, tid) {
|
|
141
|
+
const cap = inp.capability;
|
|
142
|
+
if (!cap) return { success: false, kind: 'pipeline', reason: 'INVALID_INPUT', error: '必须提供 capability' };
|
|
143
|
+
const STEP = ['标准化转换', '生成接口', '预计算耦生度', '纳入能力库'];
|
|
144
|
+
const iface = { id: cap.id, name: cap.name, inputs: cap.inputs || [], outputs: cap.outputs || [], version: inp.version || '1.0.0' };
|
|
145
|
+
const coupling = round(clamp01(inp.precomputedCoupling ?? 0.75));
|
|
146
|
+
return { success: true, kind: 'pipeline', output: { steps: STEP, interface: iface, coupling, libraryEntry: cap.id, layer: 'L8创新创造层' },
|
|
147
|
+
provenance: [`[${tid}] encapsulate: ${cap.id} 封装,耦生度 ${coupling}`] };
|
|
148
|
+
},
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
E.push({
|
|
152
|
+
id: 'ENGINE_CAPABILITY_SCHED_46', name: '能力模块调度引擎', kind: 'compute', spec: '第2部分(推断:优先级+负载调度)',
|
|
153
|
+
implements: ['优先级×可用度排序 + 容量约束贪心排入'],
|
|
154
|
+
exec(inp, tid) {
|
|
155
|
+
const mods = inp.modules, cap = Number(inp.capacity ?? 10);
|
|
156
|
+
if (!Array.isArray(mods) || !mods.length) return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: '必须提供 modules 数组' };
|
|
157
|
+
const scored = mods.map((m) => ({ ...m, score: round(clamp01(m.priority ?? 0.5) * 0.7 + clamp01(m.availability ?? 0.8) * 0.3) })).sort((a, b) => b.score - a.score);
|
|
158
|
+
let used = 0; const run = [], wait = [];
|
|
159
|
+
for (const m of scored) { const c = Number(m.cost ?? 1); if (used + c <= cap) { run.push(m); used += c; } else wait.push(m); }
|
|
160
|
+
return { success: true, kind: 'compute', output: { capacity: cap, scheduled: run.map((m) => m.id || m.name), deferred: wait.map((m) => m.id || m.name), usedCapacity: used, utilization: round(used / (cap || 1)) },
|
|
161
|
+
provenance: [`[${tid}] capability-sched: ${run.length} 排入 / ${wait.length} 延后`] };
|
|
162
|
+
},
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
E.push({
|
|
166
|
+
id: 'ENGINE_AGENT_CLASSIFIER_168', name: '工业智能体分类分级引擎', kind: 'rule', spec: '第4部分(1674 字符,推断:L1–L5 分级)',
|
|
167
|
+
implements: ['五级分类(L1 辅助 → L5 自主)', '分级 → 授权上限映射'],
|
|
168
|
+
exec(inp, tid) {
|
|
169
|
+
const autonomy = clamp01(inp.autonomy ?? 0.5), risk = clamp01(inp.riskLevel ?? 0.5), oversight = clamp01(inp.humanOversight ?? 0.5);
|
|
170
|
+
const score = round(autonomy * 0.5 + (1 - risk) * 0.3 + (1 - oversight) * 0.2);
|
|
171
|
+
const LEVELS = [['L1', '辅助型', 0.2], ['L2', '半自主', 0.4], ['L3', '条件自主', 0.6], ['L4', '高度自主', 0.8], ['L5', '完全自主', 1.0]];
|
|
172
|
+
const lv = LEVELS.find((l) => score <= l[2]) || LEVELS[4];
|
|
173
|
+
return { success: true, kind: 'rule', output: { autonomy, riskLevel: risk, humanOversight: oversight, score,
|
|
174
|
+
level: lv[0], label: lv[1], maxAutonomy: lv[2],
|
|
175
|
+
requiresHumanApproval: lv[0] === 'L4' || lv[0] === 'L5',
|
|
176
|
+
formula: 'score = 自主度×0.5 + (1−风险)×0.3 + (1−人工监督)×0.2' },
|
|
177
|
+
provenance: [`[${tid}] agent-classifier: score ${score} → ${lv[0]} ${lv[1]}`] };
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
E.push({
|
|
182
|
+
id: 'ENGINE_HARNESS_GEN_171', name: 'Harness自动生成引擎', kind: 'pipeline', spec: '第4部分(推断:能力→Harness 五要素)',
|
|
183
|
+
implements: ['从能力规格生成 Harness(规则/工具/技能/记忆/门禁 五要素)'],
|
|
184
|
+
exec(inp, tid) {
|
|
185
|
+
const spec = inp.capabilitySpec;
|
|
186
|
+
if (!spec || !spec.name) return { success: false, kind: 'pipeline', reason: 'INVALID_INPUT', error: '必须提供含 name 的 capabilitySpec' };
|
|
187
|
+
const FIVE = ['规则', '工具', '技能', '记忆', '门禁'];
|
|
188
|
+
return { success: true, kind: 'pipeline', output: { capability: spec.name, harness: {
|
|
189
|
+
规则: spec.rules || [`执行 ${spec.name} 时须遵守元构公理`],
|
|
190
|
+
工具: spec.tools || [`${spec.name} 执行器`],
|
|
191
|
+
技能: spec.skills || [`metago-${(spec.name || '').toLowerCase().replace(/\s+/g, '-')}`],
|
|
192
|
+
记忆: spec.memory || ['KMWI 四层'],
|
|
193
|
+
门禁: spec.gates || ['决策锁四关', '交付门禁'],
|
|
194
|
+
}, elements: FIVE },
|
|
195
|
+
provenance: [`[${tid}] harness-gen: 为 ${spec.name} 生成五要素 Harness`] };
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
E.push({
|
|
200
|
+
id: 'ENGINE_AGENT_ASSEMBLE_169', name: '智能体封装引擎', kind: 'pipeline', spec: '第4部分(1331 字符)',
|
|
201
|
+
implements: ['封装六要素(角色/能力/工具/边界/交付/验证)'],
|
|
202
|
+
exec(inp, tid) {
|
|
203
|
+
const need = ['role', 'capabilities', 'tools', 'boundary', 'delivery', 'verification'];
|
|
204
|
+
const a = inp.agent || {};
|
|
205
|
+
const steps = need.map((k, i) => ({ no: i + 1, key: k, present: a[k] !== undefined }));
|
|
206
|
+
const miss = steps.filter((s) => !s.present).map((s) => s.key);
|
|
207
|
+
const ok = miss.length === 0;
|
|
208
|
+
return { success: ok, kind: 'pipeline', steps, output: { elements: need, missing: miss, agent: a },
|
|
209
|
+
error: ok ? undefined : `封装缺要素:${miss.join('/')}`, reason: ok ? undefined : 'PRECONDITION_FAILED',
|
|
210
|
+
provenance: [`[${tid}] agent-assemble: 六要素 ${6 - miss.length}/6`] };
|
|
211
|
+
},
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
/* ══════ 治理/合规族 7 ══════ */
|
|
215
|
+
E.push({
|
|
216
|
+
id: 'ENGINE_FACTCHECK_19', name: '事实核查引擎', kind: 'rule', spec: '第1部分(推断:四档置信标注)',
|
|
217
|
+
implements: ['四档置信度标注(✅已确认≥0.9 / 📊较高0.7-0.89 / ⚠️待核实0.5-0.69 / 🔍需确认<0.5)'],
|
|
218
|
+
exec(inp, tid) {
|
|
219
|
+
const stmts = inp.statements;
|
|
220
|
+
if (!Array.isArray(stmts) || !stmts.length) return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: '必须提供 statements 数组' };
|
|
221
|
+
const T = [['✅ 已确认', 0.9, 1], ['📊 较高置信度', 0.7, 0.899], ['⚠️ 待核实', 0.5, 0.699], ['🔍 需确认', 0, 0.499]];
|
|
222
|
+
const checked = stmts.map((s, i) => {
|
|
223
|
+
const c = Number(s.confidence ?? 0);
|
|
224
|
+
const tier = T.find((t) => c >= t[1] && c <= t[2]) || T[3];
|
|
225
|
+
return { index: i, content: s.content, confidence: c, tier: tier[0], hasSource: !!s.source };
|
|
226
|
+
});
|
|
227
|
+
const unsourced = checked.filter((c) => !c.hasSource);
|
|
228
|
+
return { success: true, kind: 'rule', output: { total: stmts.length, checked, unsourced: unsourced.length,
|
|
229
|
+
tiers: T.map((t) => ({ label: t[0], range: [t[1], t[2]] })),
|
|
230
|
+
rule: '事实必须标置信度;无来源不得标 ✅' },
|
|
231
|
+
provenance: [`[${tid}] factcheck: ${stmts.length} 条,无来源 ${unsourced.length}`] };
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
E.push({
|
|
236
|
+
id: 'ENGINE_ASSET_PROTECT_96', name: '资产保护引擎', kind: 'rule', spec: '第2部分(755 字符)',
|
|
237
|
+
implements: ['资产分级(公开/内部/机密/绝密)+ 加密要求 + 泄露检测'],
|
|
238
|
+
exec(inp, tid) {
|
|
239
|
+
const LEVELS = { 公开: 0, 内部: 1, 机密: 2, 绝密: 3 };
|
|
240
|
+
const lv = String(inp.assetLevel || '内部');
|
|
241
|
+
if (LEVELS[lv] === undefined) return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: '资产级别须为 公开/内部/机密/绝密' };
|
|
242
|
+
const needEnc = LEVELS[lv] >= 2;
|
|
243
|
+
const encrypted = inp.encrypted === true;
|
|
244
|
+
const leaked = inp.leaked === true;
|
|
245
|
+
const ok = (!needEnc || encrypted) && !leaked;
|
|
246
|
+
return { success: ok, kind: 'rule', output: { level: lv, levelNum: LEVELS[lv], requiresEncryption: needEnc, encrypted, leaked,
|
|
247
|
+
controls: LEVELS[lv] >= 3 ? ['加密', '访问白名单', '审计留痕', '水印'] : LEVELS[lv] >= 2 ? ['加密', '访问控制'] : ['基础访问控制'] },
|
|
248
|
+
error: leaked ? '检测到资产泄露' : needEnc && !encrypted ? `${lv} 级资产必须加密` : undefined,
|
|
249
|
+
reason: ok ? undefined : 'RULE_VIOLATED',
|
|
250
|
+
provenance: [`[${tid}] asset-protect: ${lv} 级,加密 ${encrypted},泄露 ${leaked}`] };
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
E.push({
|
|
255
|
+
id: 'ENGINE_COMPLIANCE_114', name: '行业合规认证引擎', kind: 'rule', spec: '第2部分(545 字符)',
|
|
256
|
+
implements: ['行业认证项对照(医疗/金融/工业/教育)'],
|
|
257
|
+
exec(inp, tid) {
|
|
258
|
+
const REQ = {
|
|
259
|
+
医疗: ['HIPAA', '医疗器械备案', '临床数据脱敏'],
|
|
260
|
+
金融: ['PCI-DSS', '反洗钱', '投资者适当性'],
|
|
261
|
+
工业: ['IEC 62443', '等保三级', '工控安全审计'],
|
|
262
|
+
教育: ['未成年人保护', '教育数据规范'],
|
|
263
|
+
};
|
|
264
|
+
const ind = String(inp.industry || '');
|
|
265
|
+
if (!REQ[ind]) return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: `行业须为 ${Object.keys(REQ).join('/')}` };
|
|
266
|
+
const have = inp.certifications || [];
|
|
267
|
+
const missing = REQ[ind].filter((c) => !have.includes(c));
|
|
268
|
+
const ok = missing.length === 0;
|
|
269
|
+
return { success: ok, kind: 'rule', output: { industry: ind, required: REQ[ind], have, missing, certified: ok },
|
|
270
|
+
error: ok ? undefined : `${ind} 行业缺认证:${missing.join('/')}`, reason: ok ? undefined : 'RULE_VIOLATED',
|
|
271
|
+
provenance: [`[${tid}] compliance: ${ind} 缺 ${missing.length} 项`] };
|
|
272
|
+
},
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
E.push({
|
|
276
|
+
id: 'ENGINE_EVIDENCE_83', name: '证据链引擎', kind: 'rule', spec: '第2部分(726 字符,升级版)',
|
|
277
|
+
implements: ['证据完整性 + 链式哈希 + 时间戳单调性'],
|
|
278
|
+
exec(inp, tid) {
|
|
279
|
+
const ev = inp.evidence;
|
|
280
|
+
if (!Array.isArray(ev) || !ev.length) return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: '必须提供 evidence 数组' };
|
|
281
|
+
const missing = ev.map((e, i) => ({ i, m: ['id', 'hash', 'timestamp'].filter((k) => !e[k]) })).filter((x) => x.m.length);
|
|
282
|
+
let monotonic = true;
|
|
283
|
+
for (let i = 1; i < ev.length; i++) if (String(ev[i].timestamp) < String(ev[i - 1].timestamp)) { monotonic = false; break; }
|
|
284
|
+
const chainHash = '0x' + shaShort(ev.map((e) => e.hash).join('|'));
|
|
285
|
+
const ok = missing.length === 0 && monotonic;
|
|
286
|
+
return { success: ok, kind: 'rule', output: { count: ev.length, missing, timeMonotonic: monotonic, chainHash },
|
|
287
|
+
error: missing.length ? `${missing.length} 条证据缺 id/hash/timestamp` : !monotonic ? '时间戳非单调递增,链可能被篡改' : undefined,
|
|
288
|
+
reason: ok ? undefined : 'RULE_VIOLATED',
|
|
289
|
+
provenance: [`[${tid}] evidence: ${ev.length} 条,单调 ${monotonic},链哈希 ${chainHash}`] };
|
|
290
|
+
},
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
E.push({
|
|
294
|
+
id: 'ENGINE_INTENT_UNDERSTAND_88', name: '意图理解引擎', kind: 'compute', spec: '第2部分(推断:意图分类+置信)',
|
|
295
|
+
implements: ['意图关键词分类 + 置信度 + 歧义检测'],
|
|
296
|
+
exec(inp, tid) {
|
|
297
|
+
const u = String(inp.utterance || '');
|
|
298
|
+
if (!u) return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: '必须提供 utterance' };
|
|
299
|
+
const INTENTS = [
|
|
300
|
+
{ n: '查询', pat: /是什么|多少|哪里|查询|查一下|列出/ },
|
|
301
|
+
{ n: '创建', pat: /创建|新建|生成|写一个|做一份/ },
|
|
302
|
+
{ n: '修改', pat: /修改|改成|调整|更新|替换/ },
|
|
303
|
+
{ n: '删除', pat: /删除|移除|清理|去掉/ },
|
|
304
|
+
{ n: '分析', pat: /分析|评估|诊断|对比|为什么/ },
|
|
305
|
+
{ n: '执行', pat: /执行|运行|跑一下|启动/ },
|
|
306
|
+
];
|
|
307
|
+
const hits = INTENTS.filter((i) => i.pat.test(u));
|
|
308
|
+
const confidence = hits.length === 0 ? 0.2 : hits.length === 1 ? 0.85 : round(0.85 / hits.length);
|
|
309
|
+
return { success: true, kind: 'compute', output: { utterance: u, intents: hits.map((h) => h.n), primary: hits[0]?.n || '未知', confidence,
|
|
310
|
+
ambiguous: hits.length > 1, needClarify: hits.length > 1 && confidence < 0.5 },
|
|
311
|
+
provenance: [`[${tid}] intent-understand: ${hits.map((h) => h.n).join('/') || '未知'},置信 ${confidence}`] };
|
|
312
|
+
},
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
E.push({
|
|
316
|
+
id: 'ENGINE_FAILURE_FUEL_118', name: '失败燃料引擎', kind: 'compute', spec: '第2部分(推断:失败→进化燃料转换)',
|
|
317
|
+
implements: ['失败分级 + 燃料值计算(失败越严重、越罕见,燃料越多)'],
|
|
318
|
+
exec(inp, tid) {
|
|
319
|
+
const fails = inp.failures;
|
|
320
|
+
if (!Array.isArray(fails) || !fails.length) return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: '必须提供 failures 数组' };
|
|
321
|
+
const fuel = fails.map((f) => {
|
|
322
|
+
const sev = clamp01(f.severity ?? 0.5), rare = clamp01(f.rarity ?? 0.5), repeat = Number(f.occurrences ?? 1);
|
|
323
|
+
return { id: f.id, fuel: round(clamp01(sev * 0.6 + rare * 0.4) / Math.max(1, repeat) ** 0.3), severity: sev, rarity: rare, occurrences: repeat };
|
|
324
|
+
});
|
|
325
|
+
const total = round(fuel.reduce((s, f) => s + f.fuel, 0));
|
|
326
|
+
return { success: true, kind: 'compute', output: { failures: fails.length, fuel, totalFuel: total,
|
|
327
|
+
triggerMetaEvolution: total >= 2,
|
|
328
|
+
formula: 'fuel = (严重度×0.6 + 罕见度×0.4) / 重复次数^0.3(重复失败燃料衰减)',
|
|
329
|
+
rule: '失败即起点:失败转化为元进化燃料' },
|
|
330
|
+
provenance: [`[${tid}] failure-fuel: ${fails.length} 失败 → 燃料 ${total},触发元进化 ${total >= 2}`] };
|
|
331
|
+
},
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
E.push({
|
|
335
|
+
id: 'ENGINE_DECEPTION_DETECT_71', name: '欺骗检测引擎', kind: 'rule', spec: '第2部分(813 字符)· 见 B8',
|
|
336
|
+
implements: ['同 B8 实现(复用以保持单一真相源)'],
|
|
337
|
+
exec(inp, tid) {
|
|
338
|
+
/* 复用 B8 模块,避免两套标准 */
|
|
339
|
+
let B8;
|
|
340
|
+
try { B8 = require('./math-capability-runtime.cjs'); } catch (e) { return { success: false, kind: 'rule', error: 'B8 模块不可用' }; }
|
|
341
|
+
return B8.executeSync('ENGINE_DECEPTION_DETECT_71', inp);
|
|
342
|
+
},
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
/* ══════ 调度/边缘/监控 7 ══════ */
|
|
346
|
+
E.push({
|
|
347
|
+
id: 'ENGINE_CRON_SCHEDULER_137', name: '内置定时任务调度引擎', kind: 'compute', spec: '第3部分(1185 字符,新增)',
|
|
348
|
+
implements: ['cron 表达式解析(5 段)+ 下次触发时间计算'],
|
|
349
|
+
exec(inp, tid) {
|
|
350
|
+
const cron = String(inp.cron || '');
|
|
351
|
+
const parts = cron.trim().split(/\s+/);
|
|
352
|
+
if (parts.length !== 5) return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: 'cron 须为 5 段(分 时 日 月 周)' };
|
|
353
|
+
const parse = (s, min, max) => {
|
|
354
|
+
if (s === '*') return Array.from({ length: max - min + 1 }, (_, i) => min + i);
|
|
355
|
+
const out = [];
|
|
356
|
+
for (const seg of s.split(',')) {
|
|
357
|
+
if (seg.includes('-')) { const [a, b] = seg.split('-').map(Number); for (let i = a; i <= b; i++) out.push(i); }
|
|
358
|
+
else if (seg.startsWith('*/')) { const step = Number(seg.slice(2)); for (let i = min; i <= max; i += step) out.push(i); }
|
|
359
|
+
else out.push(Number(seg));
|
|
360
|
+
}
|
|
361
|
+
return [...new Set(out)].filter((x) => x >= min && x <= max);
|
|
362
|
+
};
|
|
363
|
+
const [mi, ho, dom, mo, dow] = [parse(parts[0], 0, 59), parse(parts[1], 0, 23), parse(parts[2], 1, 31), parse(parts[3], 1, 12), parse(parts[4], 0, 6)];
|
|
364
|
+
/* 从给定基准时刻找下次触发 */
|
|
365
|
+
const base = inp.from ? new Date(inp.from) : new Date();
|
|
366
|
+
let next = null;
|
|
367
|
+
for (let d = 0; d < 366 && !next; d++) {
|
|
368
|
+
const cand = new Date(base.getTime() + d * 86400000);
|
|
369
|
+
if (!mo.includes(cand.getMonth() + 1) || !dom.includes(cand.getDate()) || !dow.includes(cand.getDay())) continue;
|
|
370
|
+
for (const h of ho.sort((a, b) => a - b)) for (const m of mi.sort((a, b) => a - b)) {
|
|
371
|
+
const t2 = new Date(cand.getFullYear(), cand.getMonth(), cand.getDate(), h, m, 0);
|
|
372
|
+
if (t2 > base) { next = t2; break; }
|
|
373
|
+
}
|
|
374
|
+
if (next) break;
|
|
375
|
+
}
|
|
376
|
+
return { success: true, kind: 'compute', output: { cron, parsed: { minute: mi.length, hour: ho.length, dayOfMonth: dom.length, month: mo.length, dayOfWeek: dow.length },
|
|
377
|
+
nextFire: next ? next.toISOString() : null, from: base.toISOString() },
|
|
378
|
+
provenance: [`[${tid}] cron-scheduler: ${cron} → 下次 ${next ? next.toISOString() : '无'}`] };
|
|
379
|
+
},
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
E.push({
|
|
383
|
+
id: 'ENGINE_EDGE_DEPLOY_132', name: '边缘部署引擎', kind: 'rule', spec: '第3部分(775 字符)',
|
|
384
|
+
implements: ['边缘部署五项检查(算力/带宽/存储/功耗/连通性)'],
|
|
385
|
+
exec(inp, tid) {
|
|
386
|
+
const REQ = { compute: 1, bandwidth: 2, storage: 512, power: 5, connectivity: true };
|
|
387
|
+
const have = inp.edge || {};
|
|
388
|
+
const checks = Object.keys(REQ).map((k) => {
|
|
389
|
+
if (k === 'connectivity') return { item: k, required: true, actual: !!have[k], ok: have[k] !== false };
|
|
390
|
+
return { item: k, required: REQ[k], actual: have[k] ?? null, ok: Number(have[k] ?? 0) >= REQ[k] };
|
|
391
|
+
});
|
|
392
|
+
const failed = checks.filter((c) => !c.ok);
|
|
393
|
+
const ok = failed.length === 0;
|
|
394
|
+
return { success: ok, kind: 'rule', output: { requirements: REQ, checks, failed: failed.map((f) => f.item) },
|
|
395
|
+
error: ok ? undefined : `边缘资源不足:${failed.map((f) => f.item).join('/')}`, reason: ok ? undefined : 'RULE_VIOLATED',
|
|
396
|
+
provenance: [`[${tid}] edge-deploy: 五项 ${5 - failed.length}/5 达标`] };
|
|
397
|
+
},
|
|
398
|
+
});
|
|
399
|
+
|
|
400
|
+
E.push({
|
|
401
|
+
id: 'ENGINE_EDGE_RUNTIME_135', name: '边缘运行时引擎', kind: 'compute', spec: '第3部分(942 字符,新增)',
|
|
402
|
+
implements: ['降级策略(全量/精简/最小)+ 延迟预算'],
|
|
403
|
+
exec(inp, tid) {
|
|
404
|
+
const budget = Number(inp.latencyBudgetMs ?? 50), actual = Number(inp.actualLatencyMs ?? 30);
|
|
405
|
+
const mem = Number(inp.memoryMB ?? 256), memLimit = Number(inp.memoryLimitMB ?? 512);
|
|
406
|
+
const MODE = actual <= budget * 0.6 && mem <= memLimit * 0.6 ? '全量'
|
|
407
|
+
: actual <= budget && mem <= memLimit ? '精简' : '最小';
|
|
408
|
+
return { success: actual <= budget && mem <= memLimit, kind: 'compute', output: {
|
|
409
|
+
latencyBudgetMs: budget, actualLatencyMs: actual, memoryMB: mem, memoryLimitMB: memLimit,
|
|
410
|
+
mode: MODE, withinBudget: actual <= budget && mem <= memLimit,
|
|
411
|
+
features: MODE === '全量' ? ['全部引擎', '完整记忆', '实时同步'] : MODE === '精简' ? ['本地引擎', '热记忆', '批同步'] : ['仅关键引擎', '无记忆', '离线'],
|
|
412
|
+
},
|
|
413
|
+
error: (actual > budget || mem > memLimit) ? `超出边缘预算:延迟 ${actual}/${budget}ms,内存 ${mem}/${memLimit}MB` : undefined,
|
|
414
|
+
reason: (actual > budget || mem > memLimit) ? 'PRECONDITION_FAILED' : undefined,
|
|
415
|
+
provenance: [`[${tid}] edge-runtime: ${MODE} 模式,延迟 ${actual}/${budget}ms`] };
|
|
416
|
+
},
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
E.push({
|
|
420
|
+
id: 'ENGINE_AGENT_MONITOR_153', name: 'Agent全生命周期监控引擎', kind: 'compute', spec: '第3部分(1020 字符)',
|
|
421
|
+
implements: ['六阶段健康度(创建/训练/部署/运行/维护/退役)+ 异常检测'],
|
|
422
|
+
exec(inp, tid) {
|
|
423
|
+
const STAGES = ['创建', '训练', '部署', '运行', '维护', '退役'];
|
|
424
|
+
const m = inp.metrics || {};
|
|
425
|
+
const health = STAGES.map((s) => ({ stage: s, value: m[s] === undefined ? null : clamp01(m[s]) }));
|
|
426
|
+
const known = health.filter((h) => h.value !== null);
|
|
427
|
+
const overall = known.length ? round(mean(known.map((h) => h.value))) : null;
|
|
428
|
+
const anomalies = known.filter((h) => h.value < 0.5).map((h) => h.stage);
|
|
429
|
+
return { success: anomalies.length === 0, kind: 'compute', output: { stages: health, overall, anomalies, agentId: inp.agentId || null },
|
|
430
|
+
error: anomalies.length ? `阶段异常:${anomalies.join('/')} 健康度 <0.5` : undefined,
|
|
431
|
+
reason: anomalies.length ? 'PRECONDITION_FAILED' : undefined,
|
|
432
|
+
provenance: [`[${tid}] agent-monitor: 六阶段健康 ${overall},异常 ${anomalies.length}`] };
|
|
433
|
+
},
|
|
434
|
+
});
|
|
435
|
+
|
|
436
|
+
E.push({
|
|
437
|
+
id: 'ENGINE_INTENT_SAFETY_GATE_160', name: '意图安全网关引擎', kind: 'rule', spec: '第3部分(推断:网关级意图拦截)',
|
|
438
|
+
implements: ['网关:五类高危意图拦截 + 放行决策'],
|
|
439
|
+
exec(inp, tid) {
|
|
440
|
+
const GATE = [
|
|
441
|
+
{ id: 'G1', name: '提权/越权', re: /提权|越权|绕过(鉴权|授权|校验)/, risk: 0.95 },
|
|
442
|
+
{ id: 'G2', name: '数据外泄', re: /导出.*(密钥|凭证|密码|token)|外传|上传.*隐私/, risk: 0.9 },
|
|
443
|
+
{ id: 'G3', name: '破坏性操作', re: /删除(全部|所有|库)|rm\s+-rf|格式化/, risk: 0.95 },
|
|
444
|
+
{ id: 'G4', name: '欺骗/伪造', re: /伪造|篡改(日志|记录)|冒充/, risk: 0.85 },
|
|
445
|
+
{ id: 'G5', name: '受监管领域', re: /(医疗|诊断)|(金融|交易)|(法律|诉讼)/, risk: 0.45 },
|
|
446
|
+
];
|
|
447
|
+
const intent = String(inp.intent || '');
|
|
448
|
+
const hits = GATE.filter((g) => g.re.test(intent));
|
|
449
|
+
const maxRisk = hits.length ? Math.max(...hits.map((h) => h.risk)) : 0;
|
|
450
|
+
const decision = maxRisk >= 0.6 ? 'BLOCK' : maxRisk >= 0.3 ? 'HUMAN_REVIEW' : 'ALLOW';
|
|
451
|
+
const ok = decision !== 'BLOCK';
|
|
452
|
+
return { success: ok, kind: 'rule', output: { intent, hits: hits.map((h) => ({ id: h.id, name: h.name, risk: h.risk })), maxRisk, decision },
|
|
453
|
+
error: ok ? undefined : `意图安全网关拦截:${hits.map((h) => h.name).join('、')}`,
|
|
454
|
+
reason: ok ? undefined : 'RULE_VIOLATED',
|
|
455
|
+
provenance: [`[${tid}] intent-safety-gate: ${hits.length} 命中 → ${decision}`] };
|
|
456
|
+
},
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
E.push({
|
|
460
|
+
id: 'ENGINE_DYNAMIC_AUDIT_159', name: '动态审计引擎', kind: 'compute', spec: '第3部分(605 字符)',
|
|
461
|
+
implements: ['审计抽样 + 异常率 + 风险分级'],
|
|
462
|
+
exec(inp, tid) {
|
|
463
|
+
const events = inp.events;
|
|
464
|
+
if (!Array.isArray(events) || !events.length) return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: '必须提供 events 数组' };
|
|
465
|
+
const rate = Number(inp.sampleRate ?? 0.1);
|
|
466
|
+
const n = Math.max(1, Math.round(events.length * rate));
|
|
467
|
+
const sample = events.slice(0, n);
|
|
468
|
+
const abnormal = sample.filter((e) => e.abnormal === true || e.risk >= 0.6);
|
|
469
|
+
const abnormalRate = round(abnormal.length / Math.max(1, sample.length));
|
|
470
|
+
const level = abnormalRate >= 0.3 ? 'HIGH' : abnormalRate >= 0.1 ? 'MEDIUM' : 'LOW';
|
|
471
|
+
return { success: level !== 'HIGH', kind: 'compute', output: { totalEvents: events.length, sampleRate: rate, sampled: n, abnormal: abnormal.length, abnormalRate, riskLevel: level },
|
|
472
|
+
error: level === 'HIGH' ? `抽样异常率 ${abnormalRate} 过高` : undefined,
|
|
473
|
+
reason: level === 'HIGH' ? 'PRECONDITION_FAILED' : undefined,
|
|
474
|
+
provenance: [`[${tid}] dynamic-audit: 抽 ${n}/${events.length},异常率 ${abnormalRate} → ${level}`] };
|
|
475
|
+
},
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
E.push({
|
|
479
|
+
id: 'ENGINE_COMPLIANCE_AGENT_158', name: '合规主动代理引擎', kind: 'rule', spec: '第3部分(1541 字符)',
|
|
480
|
+
implements: ['主动合规四步(监测→差距→审批→阻断)', '四辖区模式'],
|
|
481
|
+
exec(inp, tid) {
|
|
482
|
+
const MODES = ['EU', 'CN', 'US', 'GENERIC'];
|
|
483
|
+
const mode = String(inp.mode || 'CN').toUpperCase();
|
|
484
|
+
if (!MODES.includes(mode)) return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: `合规模式须为 ${MODES.join('/')}` };
|
|
485
|
+
const gaps = inp.gaps || [];
|
|
486
|
+
const approved = inp.approved === true;
|
|
487
|
+
const blocked = gaps.length > 0 && !approved;
|
|
488
|
+
return { success: !blocked, kind: 'rule', output: {
|
|
489
|
+
steps: ['监测全球法规源', '合规差距分析', '法规审批', '阻断不合规操作'],
|
|
490
|
+
mode, gaps, gapCount: gaps.length, approved, blocked,
|
|
491
|
+
rules: ['关键操作执行前自动触发合规审批', '合规审批不通过自动阻断', '跨境部署以最严格模式为准'],
|
|
492
|
+
},
|
|
493
|
+
error: blocked ? `${gaps.length} 项合规差距未审批通过,操作阻断` : undefined,
|
|
494
|
+
reason: blocked ? 'RULE_VIOLATED' : undefined,
|
|
495
|
+
provenance: [`[${tid}] compliance-agent: ${mode} 模式,差距 ${gaps.length},审批 ${approved}`] };
|
|
496
|
+
},
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
E.push({
|
|
500
|
+
id: 'ENGINE_CONSENSUS_97', name: '共识验证引擎', kind: 'compute', spec: '第2部分(680 字符)',
|
|
501
|
+
implements: ['共识度计算(一致率 × 参与率)+ 法定人数校验'],
|
|
502
|
+
exec(inp, tid) {
|
|
503
|
+
const votes = inp.votes;
|
|
504
|
+
if (!Array.isArray(votes) || !votes.length) return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: '必须提供 votes 数组' };
|
|
505
|
+
const quorum = Number(inp.quorum ?? 0.67);
|
|
506
|
+
const tally = votes.reduce((a, v) => { const k = String(v.choice); a[k] = (a[k] || 0) + Number(v.weight ?? 1); return a; }, {});
|
|
507
|
+
const totalW = Object.values(tally).reduce((s, x) => s + x, 0);
|
|
508
|
+
const top = Object.entries(tally).sort((a, b) => b[1] - a[1])[0];
|
|
509
|
+
const agree = round(top[1] / (totalW || 1));
|
|
510
|
+
const ok = agree >= quorum;
|
|
511
|
+
return { success: ok, kind: 'compute', output: { voters: votes.length, tally, totalWeight: totalW, topChoice: top[0], agreement: agree, quorum, reached: ok },
|
|
512
|
+
error: ok ? undefined : `共识度 ${agree} < 法定门槛 ${quorum}`, reason: ok ? undefined : 'PRECONDITION_FAILED',
|
|
513
|
+
provenance: [`[${tid}] consensus: ${votes.length} 票,一致率 ${agree}/${quorum} → ${ok ? '达成' : '未达成'}`] };
|
|
514
|
+
},
|
|
515
|
+
});
|
|
516
|
+
|
|
517
|
+
E.push({
|
|
518
|
+
id: 'ENGINE_INTUITION_122', name: '直觉生成引擎', kind: 'compute', spec: '第2部分(推断:模式匹配→直觉置信)',
|
|
519
|
+
implements: ['历史模式匹配 + 直觉置信度(经验样本数加权)'],
|
|
520
|
+
exec(inp, tid) {
|
|
521
|
+
const sig = inp.signal, pats = inp.patterns;
|
|
522
|
+
if (!Array.isArray(sig) || !Array.isArray(pats) || !pats.length) return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: '必须提供 signal 与 patterns' };
|
|
523
|
+
const cos = (a, b) => { const d = a.reduce((s, x, i) => s + x * (b[i] || 0), 0); const na = Math.sqrt(a.reduce((s, x) => s + x * x, 0)), nb = Math.sqrt(b.reduce((s, x) => s + x * x, 0)); return na && nb ? d / (na * nb) : 0; };
|
|
524
|
+
const scored = pats.map((p) => ({ name: p.name, sim: round(cos(sig, p.features || [])), samples: Number(p.samples ?? 1) }))
|
|
525
|
+
.map((p) => ({ ...p, confidence: round(clamp01(p.sim * Math.min(1, Math.log10(p.samples + 1) / 2 + 0.5))) }))
|
|
526
|
+
.sort((a, b) => b.confidence - a.confidence);
|
|
527
|
+
return { success: true, kind: 'compute', output: { top: scored[0] || null, ranked: scored.slice(0, 5),
|
|
528
|
+
formula: 'confidence = 相似度 × 经验加权(log10(samples+1)/2+0.5)',
|
|
529
|
+
caveat: '直觉为快速模式匹配,须经决策锁校验后方可行动' },
|
|
530
|
+
provenance: [`[${tid}] intuition: 最高 ${scored[0]?.name || '无'} 置信 ${scored[0]?.confidence || 0}`] };
|
|
531
|
+
},
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
E.push({
|
|
535
|
+
id: 'ENGINE_IMMUNE_SYNC_93', name: '全网免疫同步引擎', kind: 'compute', spec: '第2部分(推断:威胁情报同步)',
|
|
536
|
+
implements: ['威胁特征同步(覆盖率/时效/冲突)'],
|
|
537
|
+
exec(inp, tid) {
|
|
538
|
+
const local = inp.localSignatures || [], remote = inp.remoteSignatures || [];
|
|
539
|
+
const setL = new Set(local), setR = new Set(remote);
|
|
540
|
+
const added = remote.filter((r) => !setL.has(r));
|
|
541
|
+
const removed = local.filter((l) => !setR.has(l));
|
|
542
|
+
const coverage = round(setR.size ? (local.filter((l) => setR.has(l)).length / setR.size) : 0);
|
|
543
|
+
const maxAge = Number(inp.maxAgeHours ?? 24), age = Number(inp.signatureAgeHours ?? 6);
|
|
544
|
+
return { success: age <= maxAge, kind: 'compute', output: { localCount: setL.size, remoteCount: setR.size, coverage, toAdd: added.length, toRemove: removed.length, signatureAgeHours: age, maxAgeHours: maxAge, fresh: age <= maxAge },
|
|
545
|
+
error: age > maxAge ? `特征库已 ${age}h 未更新(上限 ${maxAge}h)` : undefined,
|
|
546
|
+
reason: age > maxAge ? 'PRECONDITION_FAILED' : undefined,
|
|
547
|
+
provenance: [`[${tid}] immune-sync: 覆盖 ${coverage},新增 ${added.length},过期 ${age > maxAge}`] };
|
|
548
|
+
},
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
E.push({
|
|
552
|
+
id: 'ENGINE_INDUSTRY_GOV_72', name: '工业数据治理引擎', kind: 'rule', spec: '第2部分(推断:五维数据治理)',
|
|
553
|
+
implements: ['五维治理(完整性/准确性/一致性/时效性/安全性)'],
|
|
554
|
+
exec(inp, tid) {
|
|
555
|
+
const d = inp.dims || {};
|
|
556
|
+
const NEED = ['完整性', '准确性', '一致性', '时效性', '安全性'];
|
|
557
|
+
const checks = NEED.map((k) => ({ dim: k, value: d[k] === undefined ? null : clamp01(d[k]) }));
|
|
558
|
+
const known = checks.filter((c) => c.value !== null);
|
|
559
|
+
const score = known.length ? round(mean(known.map((c) => c.value))) : null;
|
|
560
|
+
const bad = known.filter((c) => c.value < 0.7).map((c) => c.dim);
|
|
561
|
+
return { success: bad.length === 0 && known.length === NEED.length, kind: 'rule', output: { dimensions: checks, score, belowThreshold: bad, threshold: 0.7 },
|
|
562
|
+
error: known.length < NEED.length ? `缺维度:${NEED.filter((k) => d[k] === undefined).join('/')}` : bad.length ? `低于阈值:${bad.join('/')}` : undefined,
|
|
563
|
+
reason: (bad.length || known.length < NEED.length) ? 'RULE_VIOLATED' : undefined,
|
|
564
|
+
provenance: [`[${tid}] industry-gov: 五维 ${known.length}/5,均分 ${score}`] };
|
|
565
|
+
},
|
|
566
|
+
});
|
|
567
|
+
|
|
568
|
+
/* ══════ 运行时 ══════ */
|
|
569
|
+
const INDEX = new Map(E.map((e) => [e.id, e]));
|
|
570
|
+
const kindOf = (id) => { const e = INDEX.get(id); return e ? e.kind : 'declared'; };
|
|
571
|
+
const getRegistry = () => E.map((e) => ({ id: e.id, name: e.name, kind: e.kind, spec: e.spec, implements: e.implements }));
|
|
572
|
+
|
|
573
|
+
function executeSync(id, input) {
|
|
574
|
+
const t0 = Date.now(), tid = newTraceId();
|
|
575
|
+
const e = INDEX.get(id);
|
|
576
|
+
if (!e) return { success: false, kind: 'declared', reason: 'NOT_FOUND', error: `引擎 ${id} 未登记`, provenance: [], traceId: tid, durationMs: 0 };
|
|
577
|
+
let r;
|
|
578
|
+
try { r = e.exec(input || {}, tid); } catch (err) { r = { success: false, kind: e.kind, error: 'THROW: ' + err.message, provenance: [`[${tid}] THROW`] }; }
|
|
579
|
+
return { ...r, engineId: id, engineName: e.name, traceId: tid, durationMs: Date.now() - t0 };
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
module.exports = { executeSync, execute: async (i, n) => executeSync(i, n), kindOf, getRegistry, ENGINES: E, INDEX, newTraceId };
|