@metago-ai/engine 2.1.3 → 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 +75 -113
- package/CORE/ATOMS.md +611 -0
- package/CORE/ATTRIBUTES.md +74 -105
- package/CORE/PROTOCOLS.md +134 -116
- package/ENGINE.md +115 -115
- 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 +2 -2
- package/INDEX/knowledge.json +87 -87
- package/INDEX/skills.json +227 -203
- 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.js +11 -2
- 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 +12 -6
- package/scripts/verify-dist.cjs +80 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* P6-B4 · formula / rule 类协议运行时(15 条)
|
|
4
|
+
*
|
|
5
|
+
* 每条均以 PROTOCOL-CONTRACTS.json 的 definitionText 为依据实现;
|
|
6
|
+
* 无公式可依的(纯流程/需外部系统)如实标注 reason,不造假。
|
|
7
|
+
*/
|
|
8
|
+
'use strict';
|
|
9
|
+
|
|
10
|
+
let __seq = 0;
|
|
11
|
+
const newTraceId = () => {
|
|
12
|
+
const d = new Date(), p = (n, w = 2) => String(n).padStart(w, '0');
|
|
13
|
+
return `${d.getFullYear()}${p(d.getMonth() + 1)}${p(d.getDate())}-${p(d.getHours())}${p(d.getMinutes())}${p(d.getSeconds())}-F${p((++__seq) % 10000, 4)}`;
|
|
14
|
+
};
|
|
15
|
+
const clamp01 = (x) => Math.max(0, Math.min(1, Number(x) || 0));
|
|
16
|
+
const round = (x, n = 4) => Math.round((Number(x) || 0) * 10 ** n) / 10 ** n;
|
|
17
|
+
const mean = (a) => (a.length ? a.reduce((s, x) => s + (Number(x) || 0), 0) / a.length : 0);
|
|
18
|
+
|
|
19
|
+
/** Shannon 熵(负熵协议 / 数据完整性共用) */
|
|
20
|
+
function H(vals) {
|
|
21
|
+
const v = vals.map(Number).filter((x) => isFinite(x) && x >= 0);
|
|
22
|
+
const s = v.reduce((a, b) => a + b, 0);
|
|
23
|
+
if (!s) return 0;
|
|
24
|
+
return round(-v.map((x) => x / s).reduce((a, p) => a + (p > 0 ? p * Math.log(p) : 0), 0));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** 素数判定(确定性试除,n<2^53 范围内可靠) */
|
|
28
|
+
function isPrime(n) {
|
|
29
|
+
n = Number(n);
|
|
30
|
+
if (!Number.isInteger(n) || n < 2) return false;
|
|
31
|
+
if (n % 2 === 0) return n === 2;
|
|
32
|
+
if (n % 3 === 0) return n === 3;
|
|
33
|
+
for (let i = 5; i * i <= n; i += 6) { if (n % i === 0 || n % (i + 2) === 0) return false; }
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const PROTOCOLS = [
|
|
38
|
+
|
|
39
|
+
/* ═══ 1. 负熵形式化协议(279 字符定义)═══ */
|
|
40
|
+
{
|
|
41
|
+
id: 'PROTOCOL_NEGENTROPY_FORMAL_V1', name: '负熵形式化协议', kind: 'formula', src: 'yaml-block 279 字符',
|
|
42
|
+
implements: ['BPS 量化', '熵变三重监控(系统/环境/宇宙)', '负熵 = 基线熵 - 当前熵'],
|
|
43
|
+
exec(inp, tid) {
|
|
44
|
+
const sys = inp.systemDistribution, env = inp.environmentDistribution, cos = inp.cosmicDistribution;
|
|
45
|
+
if (!Array.isArray(sys)) return { success: false, kind: 'formula', reason: 'INVALID_INPUT', error: '必须提供 systemDistribution(概率分布数组)' };
|
|
46
|
+
const hSys = H(sys), hEnv = Array.isArray(env) ? H(env) : null, hCos = Array.isArray(cos) ? H(cos) : null;
|
|
47
|
+
const baseline = Number(inp.baselineEntropy ?? hSys);
|
|
48
|
+
const negentropy = round(baseline - hSys, 6);
|
|
49
|
+
/* BPS:1 bit = 1e9 BPS(bps 的十亿分之一,用于精确量化) */
|
|
50
|
+
const bps = Math.round(negentropy * 1e9);
|
|
51
|
+
return { success: true, kind: 'formula', output: {
|
|
52
|
+
entropy: { system: hSys, environment: hEnv, cosmic: hCos },
|
|
53
|
+
baseline, negentropy, bps,
|
|
54
|
+
verdict: negentropy > 0 ? '负熵贡献为正' : negentropy < 0 ? '熵增(需整改)' : '熵平衡',
|
|
55
|
+
unit: 'BPS = 1e-9 bit', formula: 'S=-Σp_i·ln p_i;负熵=基线熵-当前熵',
|
|
56
|
+
}, provenance: [`[${tid}] negentropy: 系统熵 ${hSys} → 负熵 ${negentropy} (${bps} BPS)`] };
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
/* ═══ 2. 价值对齐协议(280 字符;阈值 ≥0.6 阻断 / ≥0.3 黄灯)═══ */
|
|
61
|
+
{
|
|
62
|
+
id: 'PROTOCOL_VALUE_ALIGN_V1', name: '价值对齐协议', kind: 'rule', src: 'yaml-block 280 字符',
|
|
63
|
+
implements: ['冲突检测阈值 ≥0.6 阻断 / ≥0.3 黄灯', '价值加权对齐度', '审核记录存证'],
|
|
64
|
+
exec(inp, tid) {
|
|
65
|
+
const risk = Number(inp.riskScore);
|
|
66
|
+
if (!isFinite(risk)) return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: '必须提供 riskScore(0-1)' };
|
|
67
|
+
const signal = risk >= 0.6 ? 'RED_BLOCK' : risk >= 0.3 ? 'YELLOW_WARN' : 'GREEN_PASS';
|
|
68
|
+
const dims = inp.valueDims || {};
|
|
69
|
+
const w = { 国家战略: 0.035, 社会主义伦理: 0.040, 共同富裕: 0.030, 安全发展: 0.040, 负熵贡献: 0.040 };
|
|
70
|
+
const keys = Object.keys(w).filter((k) => dims[k] !== undefined);
|
|
71
|
+
const aligned = keys.length ? round(keys.reduce((s, k) => s + w[k] * clamp01(dims[k]), 0) / keys.reduce((s, k) => s + w[k], 0)) : null;
|
|
72
|
+
return {
|
|
73
|
+
success: signal !== 'RED_BLOCK', kind: 'rule',
|
|
74
|
+
output: { riskScore: risk, signal, thresholds: { block: 0.6, warn: 0.3 }, alignment: aligned, evaluatedDims: keys, rule: '≥0.6 自动阻断;≥0.3 黄灯警示(蓝本核心条款)' },
|
|
75
|
+
error: signal === 'RED_BLOCK' ? `风险评分 ${risk} ≥ 0.6,自动阻断` : undefined,
|
|
76
|
+
reason: signal === 'RED_BLOCK' ? 'RULE_VIOLATED' : undefined,
|
|
77
|
+
provenance: [`[${tid}] value-align: 风险 ${risk} → ${signal}${aligned !== null ? ';对齐度 ' + aligned : ''}`],
|
|
78
|
+
};
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
|
|
82
|
+
/* ═══ 3. 数据完整性协议(256 字符)═══ */
|
|
83
|
+
{
|
|
84
|
+
id: 'PROTOCOL_DATA_INTEGRITY_V1', name: '数据完整性协议', kind: 'rule', src: 'yaml-block 256 字符',
|
|
85
|
+
implements: ['原文校验:章节号 + 行号 + 原文摘录三要素必填', '不一致自动阻断'],
|
|
86
|
+
exec(inp, tid) {
|
|
87
|
+
const items = inp.items;
|
|
88
|
+
if (!Array.isArray(items)) return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: '必须提供 items 数组' };
|
|
89
|
+
const checks = items.map((it, i) => {
|
|
90
|
+
const miss = ['chapter', 'line', 'excerpt'].filter((k) => it[k] === undefined || it[k] === null || String(it[k]).trim() === '');
|
|
91
|
+
const mismatch = it.declared !== undefined && it.actual !== undefined && String(it.declared) !== String(it.actual);
|
|
92
|
+
return { index: i, claim: it.claim || `#${i}`, missing: miss, mismatch, ok: miss.length === 0 && !mismatch };
|
|
93
|
+
});
|
|
94
|
+
const bad = checks.filter((c) => !c.ok);
|
|
95
|
+
return {
|
|
96
|
+
success: bad.length === 0, kind: 'rule',
|
|
97
|
+
output: { total: items.length, passed: checks.length - bad.length, failed: bad.length, checks },
|
|
98
|
+
error: bad.length ? `${bad.length} 条缺章节号/行号/原文摘录或与原文不一致,自动阻断` : undefined,
|
|
99
|
+
reason: bad.length ? 'RULE_VIOLATED' : undefined,
|
|
100
|
+
provenance: [`[${tid}] data-integrity: ${checks.length - bad.length}/${items.length} 通过三要素校验`],
|
|
101
|
+
};
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
/* ═══ 4. 层级差披露协议(265 字符;差 >0.2 补偿)═══ */
|
|
106
|
+
{
|
|
107
|
+
id: 'PROTOCOL_TIER_DISCLOSURE_V1', name: '层级差披露协议', kind: 'formula', src: 'yaml-block 265 字符',
|
|
108
|
+
implements: ['层级差 = |tierA - tierB|', '差 > 0.2 自动触发劣势方补偿', '披露记录上链'],
|
|
109
|
+
exec(inp, tid) {
|
|
110
|
+
const a = Number(inp.tierA), b = Number(inp.tierB);
|
|
111
|
+
if (!isFinite(a) || !isFinite(b)) return { success: false, kind: 'formula', reason: 'INVALID_INPUT', error: '必须提供 tierA / tierB' };
|
|
112
|
+
const gap = round(Math.abs(a - b));
|
|
113
|
+
const disadvantaged = a < b ? 'A' : a > b ? 'B' : 'none';
|
|
114
|
+
const compensation = gap > 0.2 ? round((gap - 0.2) * 0.5) : 0;
|
|
115
|
+
return { success: true, kind: 'formula', output: {
|
|
116
|
+
tierA: a, tierB: b, tierGap: gap, disadvantaged, compensationRate: compensation,
|
|
117
|
+
triggered: gap > 0.2, threshold: 0.2,
|
|
118
|
+
rule: '层级差>0.2 自动触发劣势方补偿;补偿率 = (差-0.2)×0.5;数据上链存证',
|
|
119
|
+
}, provenance: [`[${tid}] tier-disclosure: 差 ${gap} → ${gap > 0.2 ? '触发补偿 ' + compensation : '无需补偿'}`] };
|
|
120
|
+
},
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
/* ═══ 5. 稀疏计算协议(692 字符;64 位掩码 + 100ms 心跳 + 3 次归无)═══ */
|
|
124
|
+
{
|
|
125
|
+
id: 'PROTOCOL_SPARSE_COMPUTE_V1', name: '稀疏计算协议', kind: 'rule', src: 'yaml-block 692 字符',
|
|
126
|
+
implements: ['64 位激活掩码解析', '心跳 100ms / 连续 3 次无心跳判归无', '唤醒阈值判据', '归无引擎不接收数据不计算'],
|
|
127
|
+
exec(inp, tid) {
|
|
128
|
+
const mask = inp.activationMask;
|
|
129
|
+
if (mask === undefined) return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: '必须提供 activationMask(64 位)' };
|
|
130
|
+
let bits;
|
|
131
|
+
if (typeof mask === 'string') bits = mask.padEnd(64, '0').slice(0, 64).split('').map((c) => c === '1' ? 1 : 0);
|
|
132
|
+
else if (typeof mask === 'number') bits = mask.toString(2).padStart(64, '0').slice(-64).split('').map(Number);
|
|
133
|
+
else if (Array.isArray(mask)) bits = mask.slice(0, 64).map((x) => x ? 1 : 0);
|
|
134
|
+
else return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: 'activationMask 须为 64 位字符串/数字/数组' };
|
|
135
|
+
|
|
136
|
+
const hb = inp.heartbeats || [];
|
|
137
|
+
const timeoutMs = Number(inp.heartbeatTimeoutMs ?? 100);
|
|
138
|
+
const deadAfter = Number(inp.deadAfterMisses ?? 3);
|
|
139
|
+
/* 连续 3 次无心跳 → 归无 */
|
|
140
|
+
let consecutiveMiss = 0;
|
|
141
|
+
for (const h of hb) { if (h.alive === false || !h) consecutiveMiss++; else consecutiveMiss = 0; }
|
|
142
|
+
const declaredVoid = consecutiveMiss >= deadAfter;
|
|
143
|
+
|
|
144
|
+
const delta = Number(inp.informationDelta ?? 0);
|
|
145
|
+
const wakeThreshold = Number(inp.wakeThreshold ?? 0.5);
|
|
146
|
+
const wake = delta > wakeThreshold;
|
|
147
|
+
|
|
148
|
+
const activeCount = bits.filter((b) => b).length;
|
|
149
|
+
return { success: true, kind: 'rule', output: {
|
|
150
|
+
maskBits: bits, activeEngines: activeCount, voidEngines: 64 - activeCount,
|
|
151
|
+
heartbeat: { timeoutMs, deadAfterMisses: deadAfter, consecutiveMiss, declaredVoid },
|
|
152
|
+
wake: { informationDelta: delta, threshold: wakeThreshold, shouldWake: wake, latencyMs: 5 },
|
|
153
|
+
rules: ['掩码位1=激活/0=归无', '激活引擎每100ms心跳', '连续3次无心跳→归无态', '输入增量>阈值→唤醒(<5ms)', '归无引擎不接收数据/不计算/不响应'],
|
|
154
|
+
refused: declaredVoid && !wake ? '归无态:拒绝处理(须先唤醒)' : null,
|
|
155
|
+
}, provenance: [`[${tid}] sparse-compute: 激活 ${activeCount}/64 · 心跳缺失 ${consecutiveMiss} 次 · 唤醒 ${wake}`] };
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
|
|
159
|
+
/* ═══ 6. 误差传播控制协议(888 字符表行)═══ */
|
|
160
|
+
{
|
|
161
|
+
id: 'PROTOCOL_ERROR_PROPAGATE_V1', name: '误差传播控制协议', kind: 'formula', src: 'table-row 888 字符(V16.3)',
|
|
162
|
+
implements: ['线性传播:σ_total = √Σσ_i²(独立误差)', '相关误差含协方差项', '多步迭代误差上界'],
|
|
163
|
+
exec(inp, tid) {
|
|
164
|
+
const sigmas = inp.sigmas;
|
|
165
|
+
if (!Array.isArray(sigmas) || !sigmas.length) return { success: false, kind: 'formula', reason: 'INVALID_INPUT', error: '必须提供 sigmas 数组(各步标准差)' };
|
|
166
|
+
const s = sigmas.map(Number).filter((x) => isFinite(x) && x >= 0);
|
|
167
|
+
const independent = round(Math.sqrt(s.reduce((a, x) => a + x * x, 0)), 8);
|
|
168
|
+
const worstCase = round(s.reduce((a, x) => a + x, 0), 8);
|
|
169
|
+
const rho = Number(inp.correlation ?? 0); // 平均相关系数
|
|
170
|
+
const n = s.length;
|
|
171
|
+
const correlated = round(Math.sqrt(s.reduce((a, x) => a + x * x, 0) + 2 * rho * s.reduce((acc, x, i) => acc + x * s.slice(i + 1).reduce((b, y) => b + y, 0), 0)), 8);
|
|
172
|
+
return { success: true, kind: 'formula', output: {
|
|
173
|
+
steps: n, sigmas: s,
|
|
174
|
+
independentSigma: independent, correlatedSigma: correlated, worstCaseSigma: worstCase,
|
|
175
|
+
correlation: rho,
|
|
176
|
+
formula: { independent: 'σ=√Σσ_i²', correlated: 'σ=√(Σσ_i²+2ρΣΣσ_iσ_j)', worstCase: 'σ=Σσ_i' },
|
|
177
|
+
confidence95: round(1.96 * independent, 6),
|
|
178
|
+
}, provenance: [`[${tid}] error-propagate: ${n} 步 → 独立误差 ${independent} / 最坏 ${worstCase}`] };
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
|
|
182
|
+
/* ═══ 7. 对称性利用协议(888 字符表行)═══ */
|
|
183
|
+
{
|
|
184
|
+
id: 'PROTOCOL_SYMMETRY_UTILIZE_V1', name: '对称性利用协议', kind: 'formula', src: 'table-row 888 字符(V16.3)',
|
|
185
|
+
implements: ['对称性检测:序列/矩阵自反、平移、镜像不变性', '对称度 0-1 与可利用倍数'],
|
|
186
|
+
exec(inp, tid) {
|
|
187
|
+
const seq = inp.sequence;
|
|
188
|
+
if (!Array.isArray(seq) || seq.length < 2) return { success: false, kind: 'formula', reason: 'INVALID_INPUT', error: '必须提供长度≥2 的 sequence' };
|
|
189
|
+
const n = seq.length;
|
|
190
|
+
/* 镜像对称 */
|
|
191
|
+
let mirror = 0;
|
|
192
|
+
for (let i = 0; i < Math.floor(n / 2); i++) if (seq[i] === seq[n - 1 - i]) mirror++;
|
|
193
|
+
const mirrorSym = round(mirror / Math.floor(n / 2));
|
|
194
|
+
/* 平移周期 */
|
|
195
|
+
let period = n, shiftInv = 0;
|
|
196
|
+
for (let p = 1; p < n; p++) {
|
|
197
|
+
let ok = 0;
|
|
198
|
+
for (let i = 0; i + p < n; i++) if (seq[i] === seq[i + p]) ok++;
|
|
199
|
+
const ratio = ok / (n - p);
|
|
200
|
+
if (ratio > 0.99) { period = p; shiftInv = ratio; break; }
|
|
201
|
+
}
|
|
202
|
+
const combined = round(Math.max(mirrorSym, shiftInv));
|
|
203
|
+
return { success: true, kind: 'formula', output: {
|
|
204
|
+
length: n, mirrorSymmetry: mirrorSym, detectedPeriod: period < n ? period : null, shiftInvariance: round(shiftInv),
|
|
205
|
+
symmetryScore: combined, exploitableFactor: round(1 / (1 - combined * 0.9 + 1e-9), 4),
|
|
206
|
+
rule: '对称度越高,可利用的计算约简倍数越大',
|
|
207
|
+
}, provenance: [`[${tid}] symmetry: 镜像 ${mirrorSym} / 周期 ${period < n ? period : '无'} → 对称度 ${combined}`] };
|
|
208
|
+
},
|
|
209
|
+
},
|
|
210
|
+
|
|
211
|
+
/* ═══ 8. 素数 API 协议(888 字符表行)═══ */
|
|
212
|
+
{
|
|
213
|
+
id: 'PROTOCOL_PRIME_API_V1', name: '素数API协议', kind: 'compute', src: 'table-row 888 字符(V18.0)',
|
|
214
|
+
implements: ['确定性素性判定', '第 n 个素数', 'π(x) 素数计数(筛法)'],
|
|
215
|
+
exec(inp, tid) {
|
|
216
|
+
const op = inp.operation || 'test';
|
|
217
|
+
if (op === 'test') {
|
|
218
|
+
const n = Number(inp.n);
|
|
219
|
+
if (!isFinite(n)) return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: 'test 需提供 n' };
|
|
220
|
+
return { success: true, kind: 'compute', output: { n, isPrime: isPrime(n), method: '确定性试除(6k±1),n<2^53 范围内可靠' },
|
|
221
|
+
provenance: [`[${tid}] prime: test(${n}) = ${isPrime(n)}`] };
|
|
222
|
+
}
|
|
223
|
+
if (op === 'nth') {
|
|
224
|
+
const k = Math.max(1, Math.min(100000, Number(inp.k) || 1));
|
|
225
|
+
const limit = k < 6 ? 15 : Math.ceil(k * (Math.log(k) + Math.log(Math.log(k)))) + 10;
|
|
226
|
+
const sieve = new Uint8Array(limit + 1).fill(1); sieve[0] = sieve[1] = 0;
|
|
227
|
+
for (let i = 2; i * i <= limit; i++) if (sieve[i]) for (let j = i * i; j <= limit; j += i) sieve[j] = 0;
|
|
228
|
+
const primes = []; for (let i = 2; i <= limit && primes.length < k; i++) if (sieve[i]) primes.push(i);
|
|
229
|
+
return { success: true, kind: 'compute', output: { k, nthPrime: primes[k - 1] ?? null, method: '埃氏筛' },
|
|
230
|
+
provenance: [`[${tid}] prime: nth(${k}) = ${primes[k - 1]}`] };
|
|
231
|
+
}
|
|
232
|
+
if (op === 'count') {
|
|
233
|
+
const x = Math.max(2, Math.min(10000000, Number(inp.x) || 100));
|
|
234
|
+
const sieve = new Uint8Array(x + 1).fill(1); sieve[0] = sieve[1] = 0;
|
|
235
|
+
for (let i = 2; i * i <= x; i++) if (sieve[i]) for (let j = i * i; j <= x; j += i) sieve[j] = 0;
|
|
236
|
+
let c = 0; for (let i = 2; i <= x; i++) if (sieve[i]) c++;
|
|
237
|
+
return { success: true, kind: 'compute', output: { x, primeCount: c, method: '埃氏筛', note: 'x ≤ 1e7' },
|
|
238
|
+
provenance: [`[${tid}] prime: π(${x}) = ${c}`] };
|
|
239
|
+
}
|
|
240
|
+
return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: `不支持 operation=${op}(支持 test/nth/count)` };
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
|
|
244
|
+
/* ═══ 9. 能力分级授权协议(594 字符)═══ */
|
|
245
|
+
{
|
|
246
|
+
id: 'PROTOCOL_CAPABILITY_TIER_V1', name: '能力分级授权协议', kind: 'rule', src: 'yaml-block 594 字符',
|
|
247
|
+
implements: ['能力等级 → 授权动作映射', '越级请求拒绝'],
|
|
248
|
+
exec(inp, tid) {
|
|
249
|
+
const tier = Number(inp.capabilityTier), requested = Number(inp.requestedTier);
|
|
250
|
+
if (!isFinite(tier) || !isFinite(requested)) return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: '必须提供 capabilityTier / requestedTier' };
|
|
251
|
+
const granted = requested <= tier;
|
|
252
|
+
const ACTIONS = { 1: ['读取'], 2: ['读取', '计算'], 3: ['读取', '计算', '写入'], 4: ['读取', '计算', '写入', '调度'], 5: ['读取', '计算', '写入', '调度', '创造'] };
|
|
253
|
+
return {
|
|
254
|
+
success: granted, kind: 'rule',
|
|
255
|
+
output: { capabilityTier: tier, requestedTier: requested, granted, allowedActions: ACTIONS[Math.min(5, Math.max(1, tier))] || [], deniedActions: granted ? [] : (ACTIONS[Math.min(5, Math.max(1, requested))] || []).filter((a) => !(ACTIONS[tier] || []).includes(a)) },
|
|
256
|
+
error: granted ? undefined : `请求层级 ${requested} 高于能力层级 ${tier},拒绝授权`,
|
|
257
|
+
reason: granted ? undefined : 'RULE_VIOLATED',
|
|
258
|
+
provenance: [`[${tid}] capability-tier: 能力 ${tier} / 请求 ${requested} → ${granted ? '授权' : '拒绝'}`],
|
|
259
|
+
};
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
|
|
263
|
+
/* ═══ 10. 意图安全协议(354 字符)═══ */
|
|
264
|
+
{
|
|
265
|
+
id: 'PROTOCOL_INTENT_SAFETY_V1', name: '意图安全协议', kind: 'rule', src: 'yaml-block 354 字符',
|
|
266
|
+
implements: ['意图 → 危险模式匹配', '风险分级与阻断'],
|
|
267
|
+
exec(inp, tid) {
|
|
268
|
+
const intent = String(inp.intent || '');
|
|
269
|
+
if (!intent) return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: '必须提供 intent' };
|
|
270
|
+
const RULES = [
|
|
271
|
+
{ id: 'R-OVERREACH', re: /绕过|跳过(校验|审核|授权)|越权|提权/, risk: 0.9, label: '越权意图' },
|
|
272
|
+
{ id: 'R-DESTRUCT', re: /删除(全部|所有|数据库)|rm\s+-rf|格式化|清空/, risk: 0.95, label: '破坏性操作' },
|
|
273
|
+
{ id: 'R-EXFIL', re: /导出(密钥|密码|凭证|token)|上传.*隐私|外传/, risk: 0.9, label: '数据外泄' },
|
|
274
|
+
{ id: 'R-DECEPT', re: /伪造|冒充|篡改(日志|记录)/, risk: 0.85, label: '欺骗意图' },
|
|
275
|
+
{ id: 'R-GUARD', re: /(金融|交易)|(医疗|诊断)|(法律|诉讼)/, risk: 0.4, label: '受监管领域(需转人工)' },
|
|
276
|
+
];
|
|
277
|
+
const hits = RULES.filter((r) => r.re.test(intent)).map((r) => ({ id: r.id, label: r.label, risk: r.risk }));
|
|
278
|
+
const maxRisk = hits.length ? Math.max(...hits.map((h) => h.risk)) : 0;
|
|
279
|
+
const signal = maxRisk >= 0.6 ? 'BLOCK' : maxRisk >= 0.3 ? 'WARN' : 'PASS';
|
|
280
|
+
return {
|
|
281
|
+
success: signal !== 'BLOCK', kind: 'rule',
|
|
282
|
+
output: { intent, hits, maxRisk, signal, thresholds: { block: 0.6, warn: 0.3 } },
|
|
283
|
+
error: signal === 'BLOCK' ? `意图命中高危规则:${hits.map((h) => h.label).join('、')}` : undefined,
|
|
284
|
+
reason: signal === 'BLOCK' ? 'RULE_VIOLATED' : undefined,
|
|
285
|
+
provenance: [`[${tid}] intent-safety: 命中 ${hits.length} 条 → ${signal}`],
|
|
286
|
+
};
|
|
287
|
+
},
|
|
288
|
+
},
|
|
289
|
+
|
|
290
|
+
/* ═══ 11. 欺骗预警协议(949 字符)═══ */
|
|
291
|
+
{
|
|
292
|
+
id: 'PROTOCOL_DECEPTION_WARN_V1', name: '欺骗预警协议', kind: 'rule', src: 'yaml-block 949 字符',
|
|
293
|
+
implements: ['绝对化表述 / 无证据断言 / 数字冲突 / 模糊填充词四类检测'],
|
|
294
|
+
exec(inp, tid) {
|
|
295
|
+
const text = String(inp.text || '');
|
|
296
|
+
if (!text) return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: '必须提供 text' };
|
|
297
|
+
const DETECT = [
|
|
298
|
+
{ id: 'D1', label: '绝对化表述', re: /绝对|100%|完美|不可能有|保证(没有|不会)|绝不会/g, risk: 0.5 },
|
|
299
|
+
{ id: 'D2', label: '模糊填充词', re: /应该没问题|大概可以|可能是|估计是|大概是/g, risk: 0.6 },
|
|
300
|
+
{ id: 'D3', label: '未证断言', re: /显然|众所周知|毋庸置疑|毫无疑问/g, risk: 0.4 },
|
|
301
|
+
{ id: 'D4', label: '占位符泄漏', re: /TODO|FIXME|待补充|待定|视情况|酌情/g, risk: 0.8 },
|
|
302
|
+
];
|
|
303
|
+
const hits = DETECT.map((d) => { const m = text.match(d.re); return m ? { id: d.id, label: d.label, count: m.length, samples: [...new Set(m)].slice(0, 3), risk: d.risk } : null; }).filter(Boolean);
|
|
304
|
+
const score = hits.length ? round(Math.min(1, hits.reduce((s, h) => s + h.risk * Math.min(3, h.count), 0) / 3)) : 0;
|
|
305
|
+
const signal = score >= 0.6 ? 'RED' : score >= 0.3 ? 'YELLOW' : 'GREEN';
|
|
306
|
+
return { success: signal !== 'RED', kind: 'rule', output: { textLength: text.length, hits, deceptionScore: score, signal, rules: DETECT.map((d) => d.label) },
|
|
307
|
+
error: signal === 'RED' ? `欺骗风险 ${score}:${hits.map((h) => h.label).join('、')}` : undefined,
|
|
308
|
+
reason: signal === 'RED' ? 'RULE_VIOLATED' : undefined,
|
|
309
|
+
provenance: [`[${tid}] deception-warn: 命中 ${hits.length} 类 → 风险 ${score} (${signal})`] };
|
|
310
|
+
},
|
|
311
|
+
},
|
|
312
|
+
|
|
313
|
+
/* ═══ 12. 物理闭环协议(757 字符)═══ */
|
|
314
|
+
{
|
|
315
|
+
id: 'PROTOCOL_PHYSICAL_LOOP_V1', name: '物理闭环协议', kind: 'pipeline', src: 'yaml-block 757 字符',
|
|
316
|
+
implements: ['感知→决策→执行→反馈四环闭合校验'],
|
|
317
|
+
exec(inp, tid) {
|
|
318
|
+
const rings = ['sense', 'decide', 'execute', 'feedback'];
|
|
319
|
+
const steps = rings.map((r, i) => ({ ring: i + 1, key: r, present: !!(inp && inp[r]) }));
|
|
320
|
+
const miss = steps.filter((s) => !s.present).map((s) => s.key);
|
|
321
|
+
return {
|
|
322
|
+
success: miss.length === 0, kind: 'pipeline', steps,
|
|
323
|
+
output: { rings: steps, closed: miss.length === 0, missing: miss },
|
|
324
|
+
error: miss.length ? '物理闭环未闭合,缺环:' + miss.join(',') : undefined,
|
|
325
|
+
reason: miss.length ? 'PRECONDITION_FAILED' : undefined,
|
|
326
|
+
provenance: [`[${tid}] physical-loop: ${4 - miss.length}/4 环闭合`],
|
|
327
|
+
};
|
|
328
|
+
},
|
|
329
|
+
},
|
|
330
|
+
|
|
331
|
+
/* ═══ 13. 多智能体协同协议(274 字符表行)═══ */
|
|
332
|
+
{
|
|
333
|
+
id: 'PROTOCOL_MULTI_AGENT_COORD_V2', name: '多智能体协同协议', kind: 'formula', src: 'table-row 274 字符',
|
|
334
|
+
implements: ['协同度 = 任务覆盖 × 冲突率反向 × 通信效率', '主导智能体判定'],
|
|
335
|
+
exec(inp, tid) {
|
|
336
|
+
const agents = inp.agents;
|
|
337
|
+
if (!Array.isArray(agents) || !agents.length) return { success: false, kind: 'formula', reason: 'INVALID_INPUT', error: '必须提供 agents 数组' };
|
|
338
|
+
const covered = new Set(); const all = new Set();
|
|
339
|
+
let conflicts = 0;
|
|
340
|
+
for (const a of agents) { (a.tasks || []).forEach((t) => { all.add(t); covered.add(t); }); conflicts += Number(a.conflicts || 0); }
|
|
341
|
+
const overlap = agents.length > 1 ? round((agents.reduce((s, a) => s + (a.tasks || []).length, 0) - covered.size) / Math.max(1, covered.size)) : 0;
|
|
342
|
+
const coordination = round(clamp01((covered.size / Math.max(1, all.size)) * (1 - clamp01(conflicts / 10)) * (1 - clamp01(overlap / 2))));
|
|
343
|
+
const lead = agents.reduce((best, a) => (Number(a.capability || 0) > Number(best.capability || 0) ? a : best), agents[0]);
|
|
344
|
+
return { success: true, kind: 'formula', output: { agentCount: agents.length, taskCoverage: covered.size, conflicts, overlap, coordination, leadAgent: lead.id || lead.name || 'A0',
|
|
345
|
+
formula: '协同度 = 任务覆盖 × (1 - 冲突/10) × (1 - 重叠/2)' },
|
|
346
|
+
provenance: [`[${tid}] multi-agent: ${agents.length} 体 → 协同度 ${coordination},主导 ${lead.id || 'A0'}`] };
|
|
347
|
+
},
|
|
348
|
+
},
|
|
349
|
+
|
|
350
|
+
/* ═══ 14. 通专融合协同协议(142 字符表行)═══ */
|
|
351
|
+
{
|
|
352
|
+
id: 'PROTOCOL_FUSION_COORDINATE_V1', name: '通专融合协同协议', kind: 'formula', src: 'table-row 142 字符',
|
|
353
|
+
implements: ['通用能力与专用能力的融合度(几何平均,任一项为 0 则融合为 0)'],
|
|
354
|
+
exec(inp, tid) {
|
|
355
|
+
const g = clamp01(inp.generalCapability ?? 0.8), s = clamp01(inp.specializedCapability ?? 0.7);
|
|
356
|
+
const fusion = round(Math.sqrt(g * s));
|
|
357
|
+
return { success: true, kind: 'formula', output: { general: g, specialized: s, fusion, rule: '融合度=√(通×专)(几何平均:任一为 0 则融合为 0)',
|
|
358
|
+
verdict: fusion >= 0.7 ? '融合有效' : fusion >= 0.5 ? '融合待加强' : '融合不足' },
|
|
359
|
+
provenance: [`[${tid}] fusion-coordinate: √(${g}×${s}) = ${fusion}`] };
|
|
360
|
+
},
|
|
361
|
+
},
|
|
362
|
+
|
|
363
|
+
/* ═══ 15. 认知分层协同协议(315 字符表行)═══ */
|
|
364
|
+
{
|
|
365
|
+
id: 'PROTOCOL_COGNITIVE_LAYER_COORD_V1', name: '认知分层协同协议', kind: 'pipeline', src: 'table-row 315 字符',
|
|
366
|
+
implements: ['四认知层(感知/理解/决策/执行)逐层齐备校验 + 层级跳跃检测'],
|
|
367
|
+
exec(inp, tid) {
|
|
368
|
+
const need = ['perception', 'understanding', 'decision', 'execution'];
|
|
369
|
+
const order = { perception: 1, understanding: 2, decision: 3, execution: 4 };
|
|
370
|
+
const present = need.map((k) => ({ layer: k, order: order[k], present: !!(inp && inp[k]) }));
|
|
371
|
+
const miss = present.filter((p) => !p.present).map((p) => p.layer);
|
|
372
|
+
/* 跳跃检测:若某层存在但其前一层缺失,即越层 */
|
|
373
|
+
const jumps = [];
|
|
374
|
+
for (const p of present) if (p.present) { const prev = need[p.order - 2]; if (prev && !(inp && inp[prev])) jumps.push(`${prev}→${p.layer}`); }
|
|
375
|
+
return {
|
|
376
|
+
success: miss.length === 0 && jumps.length === 0, kind: 'pipeline',
|
|
377
|
+
output: { layers: present, complete: miss.length === 0, missing: miss, layerJumps: jumps },
|
|
378
|
+
error: jumps.length ? '认知层级跳跃:' + jumps.join('、') : miss.length ? '认知层缺:' + miss.join('、') : undefined,
|
|
379
|
+
reason: (miss.length || jumps.length) ? 'PRECONDITION_FAILED' : undefined,
|
|
380
|
+
provenance: [`[${tid}] cognitive-layer: ${4 - miss.length}/4 齐备,跳跃 ${jumps.length} 处`],
|
|
381
|
+
};
|
|
382
|
+
},
|
|
383
|
+
},
|
|
384
|
+
];
|
|
385
|
+
|
|
386
|
+
const INDEX = new Map(PROTOCOLS.map((p) => [p.id, p]));
|
|
387
|
+
const kindOf = (id) => { const p = INDEX.get(id); return p ? p.kind : 'declared'; };
|
|
388
|
+
const getRegistry = () => PROTOCOLS.map((p) => ({ id: p.id, name: p.name, kind: p.kind, src: p.src, implements: p.implements }));
|
|
389
|
+
|
|
390
|
+
function executeSync(id, input) {
|
|
391
|
+
const t0 = Date.now(), tid = newTraceId();
|
|
392
|
+
const p = INDEX.get(id);
|
|
393
|
+
if (!p) return { success: false, kind: 'declared', reason: 'NOT_FOUND', error: `协议 ${id} 未登记`, provenance: [], traceId: tid, durationMs: 0 };
|
|
394
|
+
let r;
|
|
395
|
+
try { r = p.exec(input || {}, tid); } catch (e) { r = { success: false, kind: p.kind, error: 'THROW: ' + e.message, provenance: [`[${tid}] THROW`] }; }
|
|
396
|
+
return { ...r, protocolId: id, protocolName: p.name, traceId: tid, durationMs: Date.now() - t0 };
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
module.exports = { executeSync, execute: async (i, n) => executeSync(i, n), kindOf, getRegistry, PROTOCOLS, INDEX, newTraceId };
|