@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.
Files changed (67) hide show
  1. package/BOUNDARIES.md +109 -0
  2. package/CHANGES.md +4 -4
  3. package/CONSTITUTION/AXIOMS.md +72 -110
  4. package/CORE/ATOMS.md +611 -0
  5. package/CORE/ATTRIBUTES.md +70 -101
  6. package/CORE/PROTOCOLS.md +134 -116
  7. package/ENGINE.md +6 -6
  8. package/ENGINES/engine-runtime.cjs +318 -0
  9. package/ENGINES/final-engines-runtime.cjs +683 -0
  10. package/ENGINES/governance-runtime.cjs +582 -0
  11. package/ENGINES/math-capability-runtime.cjs +451 -0
  12. package/ENGINES/philosophy-runtime.cjs +427 -0
  13. package/ENGINES/yili/data/yaoci-1.js +24 -0
  14. package/ENGINES/yili/data/yaoci-2.js +21 -0
  15. package/ENGINES/yili/data/yaoci-3.js +21 -0
  16. package/ENGINES/yili/data/yaoci-4.js +21 -0
  17. package/ENGINES/yili/data/yaoci.js +17 -0
  18. package/ENGINES/yili/lib/almanac.js +416 -0
  19. package/ENGINES/yili/lib/bazi.js +127 -0
  20. package/ENGINES/yili/lib/diagnose.js +71 -0
  21. package/ENGINES/yili/lib/hetu-luoshu.js +123 -0
  22. package/ENGINES/yili/lib/index.js +23 -0
  23. package/ENGINES/yili/lib/liuyao.js +226 -0
  24. package/ENGINES/yili/lib/qimen.js +167 -0
  25. package/ENGINES/yili/lib/shushu-calculus.js +85 -0
  26. package/ENGINES/yili/lib/taiyi.js +83 -0
  27. package/ENGINES/yili/lib/vendor/shouxing-core.js +627 -0
  28. package/ENGINES/yili/lib/wuxing.js +106 -0
  29. package/ENGINES/yili/lib/yijing.js +226 -0
  30. package/ENGINES/yili/lib/zeri.js +80 -0
  31. package/ENGINES/yili/lib/ziwei.js +145 -0
  32. package/ENGINES/yili/package.json +1 -0
  33. package/EXECUTORS.md +85 -0
  34. package/GENOME.json +11 -10
  35. package/INDEX/ENGINES.md +168 -0
  36. package/INDEX/engines.json +161 -161
  37. package/INDEX/knowledge.json +2 -2
  38. package/INDEX/skills.json +29 -5
  39. package/INDEX/tools.json +64 -64
  40. package/PATENTS.md +1 -1
  41. package/PROTOCOLS/closure-runtime.cjs +288 -0
  42. package/PROTOCOLS/covenant-runtime.cjs +409 -0
  43. package/PROTOCOLS/final-runtime.cjs +680 -0
  44. package/PROTOCOLS/formula-runtime.cjs +399 -0
  45. package/PROTOCOLS/proactive-runtime.cjs +368 -0
  46. package/PROTOCOLS/rule-runtime.cjs +448 -0
  47. package/PROTOCOLS/runtime.cjs +441 -0
  48. package/README.md +5 -5
  49. package/RUNTIME/dist/kmwi-memory.d.ts.map +1 -1
  50. package/RUNTIME/dist/kmwi-memory.js +14 -2
  51. package/RUNTIME/dist/kmwi-memory.js.map +1 -1
  52. package/RUNTIME/dist/validators.d.ts +2 -2
  53. package/RUNTIME/dist/validators.js +19 -10
  54. package/atoms/approx.cjs +293 -0
  55. package/atoms/atom-factory.cjs +550 -0
  56. package/atoms/atoms-cosmic-bio.cjs +340 -0
  57. package/atoms/atoms-crypto-emotion.cjs +215 -0
  58. package/atoms/atoms_engines.json +5821 -0
  59. package/atoms/blueprint-atoms-2.cjs +583 -0
  60. package/atoms/blueprint-atoms.cjs +975 -0
  61. package/atoms/constraints.cjs +257 -0
  62. package/atoms/contract-validator.cjs +213 -0
  63. package/atoms/engine.cjs +849 -0
  64. package/atoms/specialized.cjs +530 -0
  65. package/atoms/verify.cjs +94 -0
  66. package/package.json +97 -91
  67. package/scripts/verify-dist.cjs +80 -0
@@ -0,0 +1,409 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * P6-B7 · 共契族 / 见证族 / 接入族 协议运行时(18 条)
4
+ * 逐条依 PROTOCOL-CONTRACTS.json 的 definitionText 条款实现。
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())}-C${p((++__seq) % 10000, 4)}`;
12
+ };
13
+ const clamp01 = (x) => Math.max(0, Math.min(1, Number(x) || 0));
14
+ const round = (x, n = 4) => Math.round((Number(x) || 0) * 10 ** n) / 10 ** n;
15
+
16
+ const P = [];
17
+
18
+ /** 通用:必备条款齐备检查 */
19
+ function clauses(need, inp) {
20
+ const items = need.map((k, i) => ({ no: i + 1, key: k, present: !!(inp && inp[k] !== undefined) }));
21
+ const miss = items.filter((x) => !x.present).map((x) => x.key);
22
+ return { items, miss, ok: miss.length === 0 };
23
+ }
24
+
25
+ /* ══════════ 共契族 10 条 ══════════ */
26
+
27
+ /* 1. ACP 万物共契 */
28
+ P.push({
29
+ id: 'PROTOCOL_ACP_V1_0', name: '万物共契协议', kind: 'rule', src: 'yaml-block 1522 字符 · 三章九条',
30
+ implements: ['第一章 万物有值:内在价值/价值多样性/价值不可替代', '第二章 万物有声:声音定义/倾听义务/声音理解', '第三章 万物有权:生存权/健康权'],
31
+ exec(inp, tid) {
32
+ const c = clauses(['intrinsicValueAcknowledged', 'valueDiversityRespected', 'speciesImpactAssessed', 'listeningActive', 'speciesModelBuilt', 'survivalRightProtected', 'healthMonitored'], inp);
33
+ /* 第7条:濒危物种特殊保护 —— 若涉濒危物种必须有额外保护措施 */
34
+ const endangered = inp.endangeredSpecies || [];
35
+ const endangeredOk = endangered.length === 0 || !!inp.endangeredProtection;
36
+ const ok = c.ok && endangeredOk;
37
+ return { success: ok, kind: 'rule', output: {
38
+ chapters: { 万物有值: ['内在价值', '价值多样性', '价值不可替代'], 万物有声: ['声音定义', '倾听义务', '声音理解'], 万物有权: ['生存权', '健康权'] },
39
+ checks: c.items, missing: c.miss, endangeredSpecies: endangered, endangeredProtection: !!inp.endangeredProtection,
40
+ rules: ['万物存在本身即有价值(不依赖人类赋予)', '不得用人类标准衡量万物价值', '灭绝一个物种=永远失去其独特价值', '系统须持续倾听万物声音', '决策不得威胁物种生存', '濒危物种享特殊保护'],
41
+ },
42
+ error: c.miss.length ? `共契条款未落实:${c.miss.join('/')}` : !endangeredOk ? '涉濒危物种但未启用特殊保护(第7条)' : undefined,
43
+ reason: ok ? undefined : 'RULE_VIOLATED',
44
+ provenance: [`[${tid}] acp: 九条 ${c.items.filter((x) => x.present).length}/7 落实,濒危保护 ${endangeredOk}`] };
45
+ },
46
+ });
47
+
48
+ /* 2. BCP 生物共契 */
49
+ P.push({
50
+ id: 'PROTOCOL_BCP_V1_0', name: '生物共契协议', kind: 'rule', src: 'yaml-block 1779 字符',
51
+ implements: ['生物信号采集规范', '生物价值评估', '生物边界尊重(不得越界干预)'],
52
+ exec(inp, tid) {
53
+ const c = clauses(['bioSignalTypes', 'bioValueAssessed', 'bioBoundaryRespected'], inp);
54
+ const types = inp.bioSignalTypes || [];
55
+ const ok = c.ok && Array.isArray(types) && types.length > 0;
56
+ return { success: ok, kind: 'rule', output: { required: ['bioSignalTypes', 'bioValueAssessed', 'bioBoundaryRespected'], missing: c.miss, bioSignalTypes: types, boundaryRespected: !!inp.bioBoundaryRespected },
57
+ error: c.miss.length ? `生物共契缺:${c.miss.join('/')}` : undefined, reason: ok ? undefined : 'RULE_VIOLATED',
58
+ provenance: [`[${tid}] bcp: 信号源 ${types.length} 类,边界尊重 ${!!inp.bioBoundaryRespected}`] };
59
+ },
60
+ });
61
+
62
+ /* 3. CCP 宇宙共契 */
63
+ P.push({
64
+ id: 'PROTOCOL_CCP_V1_0', name: '宇宙共契协议', kind: 'rule', src: 'yaml-block 115 字符',
65
+ implements: ['宇宙尺度价值承认', '宇宙影响评估'],
66
+ exec(inp, tid) {
67
+ const c = clauses(['cosmicValueAcknowledged', 'cosmicImpactAssessed'], inp);
68
+ const ok = c.ok;
69
+ return { success: ok, kind: 'rule', output: { required: ['cosmicValueAcknowledged', 'cosmicImpactAssessed'], missing: c.miss },
70
+ error: ok ? undefined : `宇宙共契缺:${c.miss.join('/')}`, reason: ok ? undefined : 'RULE_VIOLATED',
71
+ provenance: [`[${tid}] ccp: ${2 - c.miss.length}/2`] };
72
+ },
73
+ });
74
+
75
+ /* 4. MCP 意义共契 */
76
+ P.push({
77
+ id: 'PROTOCOL_MCP_V1_0', name: '意义共契协议', kind: 'rule', src: 'yaml-block 1722 字符 · 三章八条',
78
+ implements: ['第1条 意义先于行动(why 必答)', '第4条 四层追问(行动/目标/存在/终极)', '第7条 方向向量 D=(w1..w28) 权重和=1'],
79
+ exec(inp, tid) {
80
+ const why = inp.why;
81
+ const noWhy = !why || String(why).trim() === '';
82
+ const layers = inp.meaningLayers || {};
83
+ const LAYERS = ['action', 'stage', 'existence', 'ultimate'];
84
+ const gotLayers = LAYERS.filter((l) => layers[l]);
85
+ const weights = inp.directionVector;
86
+ let weightSum = null, weightOk = null;
87
+ if (Array.isArray(weights)) { weightSum = round(weights.reduce((a, b) => a + Number(b || 0), 0)); weightOk = Math.abs(weightSum - 1) < 1e-6; }
88
+ const ok = !noWhy && gotLayers.length === 4 && (weightOk === null || weightOk);
89
+ return { success: ok, kind: 'rule', output: {
90
+ why, whyPresent: !noWhy,
91
+ meaningLayers: { required: LAYERS, covered: gotLayers, missing: LAYERS.filter((l) => !gotLayers.includes(l)) },
92
+ directionVector: Array.isArray(weights) ? { dims: weights.length, sum: weightSum, valid: weightOk } : null,
93
+ valueAlignRequired: '28 维价值对齐',
94
+ rules: ['意义不明确的行动不得执行', '意义追问不得中断', '价值冲突寻求互补解', '重大意义变更需上链'],
95
+ },
96
+ error: noWhy ? '第1条:未回答「为什么做」,意义不明不得执行' : gotLayers.length < 4 ? `第4条:四层追问缺 ${LAYERS.filter((l) => !gotLayers.includes(l)).join('/')}` : (weightOk === false ? `第7条:方向向量权重和 ${weightSum} ≠ 1` : undefined),
97
+ reason: ok ? undefined : 'RULE_VIOLATED',
98
+ provenance: [`[${tid}] mcp: why ${!noWhy},四层 ${gotLayers.length}/4,方向向量 ${weightSum ?? 'n/a'}`] };
99
+ },
100
+ });
101
+
102
+ /* 5. QCP 量子共契 */
103
+ P.push({
104
+ id: 'PROTOCOL_QCP_V1_0', name: '量子共契协议', kind: 'rule', src: 'yaml-block 1710 字符',
105
+ implements: ['量子态承认', '观测伦理(观测即干预)', '退相干责任'],
106
+ exec(inp, tid) {
107
+ const c = clauses(['quantumStateAcknowledged', 'observationEthics', 'decoherenceAccounted'], inp);
108
+ const ok = c.ok;
109
+ return { success: ok, kind: 'rule', output: { required: ['quantumStateAcknowledged', 'observationEthics', 'decoherenceAccounted'], missing: c.miss,
110
+ rules: ['观测即干预:须声明观测对量子态的影响', '退相干须被计入责任'] },
111
+ error: ok ? undefined : `量子共契缺:${c.miss.join('/')}`, reason: ok ? undefined : 'RULE_VIOLATED',
112
+ provenance: [`[${tid}] qcp: ${3 - c.miss.length}/3`] };
113
+ },
114
+ });
115
+
116
+ /* 6. TCP 时空共契 */
117
+ P.push({
118
+ id: 'PROTOCOL_TCP_V1_0', name: '时空共契协议', kind: 'rule', src: 'yaml-block 1691 字符 · 三章',
119
+ implements: ['第一章 过去有灵(历史智慧提取三标准:持久性/普遍性/启发性)', '第二章 未来有声(可能性价值)', '第三章 当下有责(双重责任 + 时空影响评估)'],
120
+ exec(inp, tid) {
121
+ const wisdom = inp.historicalWisdom || [];
122
+ const CRIT = ['持久性', '普遍性', '启发性'];
123
+ const wisdomOk = wisdom.length === 0 || wisdom.every((w) => w.criteria && CRIT.every((c2) => (w.criteria || []).includes(c2)));
124
+ const futures = inp.futureScenarios || [];
125
+ const pluralOk = futures.length >= 2 || inp.pluralityValue === true;
126
+ const duality = !!inp.dualResponsibilityDeclared;
127
+ const impactAssessed = !!inp.temporalImpactAssessed;
128
+ const ok = wisdomOk && pluralOk && duality && impactAssessed;
129
+ return { success: ok, kind: 'rule', output: {
130
+ past: { wisdomCount: wisdom.length, criteriaRequired: CRIT, criteriaOk: wisdomOk },
131
+ future: { scenarios: futures.length, pluralMaintained: pluralOk, possibilityValueIncluded: !!inp.pluralityValue },
132
+ present: { dualResponsibilityDeclared: duality, temporalImpactAssessed: impactAssessed },
133
+ rules: ['历史智慧提取须过持久性/普遍性/启发性三标准', '保持多种可能性的系统更健康', '当下决策同时承担对过去与未来的责任'],
134
+ },
135
+ error: !wisdomOk ? '历史智慧未过三标准(持久性/普遍性/启发性)' : !pluralOk ? '未来可能性不足 2 条且未声明可能性价值' : !duality ? '第7条:未声明双重责任' : !impactAssessed ? '第7条:未做时空影响评估' : undefined,
136
+ reason: ok ? undefined : 'RULE_VIOLATED',
137
+ provenance: [`[${tid}] tcp: 智慧 ${wisdom.length} 条,未来 ${futures.length} 路径,双重责任 ${duality}`] };
138
+ },
139
+ });
140
+
141
+ /* 7. OPCP 元点共契 */
142
+ P.push({
143
+ id: 'PROTOCOL_OPCP_V1_0', name: '元点共契协议', kind: 'rule', src: 'yaml-block 1721 字符',
144
+ implements: ['元点(起点)承认', '向源收敛', '元点不可僭越'],
145
+ exec(inp, tid) {
146
+ const c = clauses(['originAcknowledged', 'convergingToSource', 'originNotUsurped'], inp);
147
+ const ok = c.ok;
148
+ return { success: ok, kind: 'rule', output: { required: ['originAcknowledged', 'convergingToSource', 'originNotUsurped'], missing: c.miss },
149
+ error: ok ? undefined : `元点共契缺:${c.miss.join('/')}`, reason: ok ? undefined : 'RULE_VIOLATED',
150
+ provenance: [`[${tid}] opcp: ${3 - c.miss.length}/3`] };
151
+ },
152
+ });
153
+
154
+ /* 8. TOCP 道源共契 */
155
+ P.push({
156
+ id: 'PROTOCOL_TOCP_V1_0', name: '道源共契协议', kind: 'rule', src: 'yaml-block 1636 字符',
157
+ implements: ['道源承认', '法自然(不强行干预)', '道器合一'],
158
+ exec(inp, tid) {
159
+ const c = clauses(['sourceAcknowledged', 'followsNature', 'daoQiUnity'], inp);
160
+ const force = inp.forcedIntervention === true;
161
+ const ok = c.ok && !force;
162
+ return { success: ok, kind: 'rule', output: { required: ['sourceAcknowledged', 'followsNature', 'daoQiUnity'], missing: c.miss, forcedIntervention: force },
163
+ error: c.miss.length ? `道源共契缺:${c.miss.join('/')}` : force ? '违反「法自然」:不得强行干预' : undefined,
164
+ reason: ok ? undefined : 'RULE_VIOLATED',
165
+ provenance: [`[${tid}] tocp: ${3 - c.miss.length}/3,强行干预 ${force}`] };
166
+ },
167
+ });
168
+
169
+ /* 9. RVCP 归无共契(显隐双态 + 不喧宾夺主)*/
170
+ P.push({
171
+ id: 'PROTOCOL_RVCP_V1_0', name: '归无共契协议', kind: 'rule', src: 'yaml-block 2031 字符 · 三章',
172
+ implements: ['第4条 显隐双态(显态主动输出/隐态被动感知)', '第5条 切换原则(需要时显,专注时隐)', '第7-8条 喧宾夺主检测'],
173
+ exec(inp, tid) {
174
+ const STATES = ['显态', '隐态'];
175
+ const state = String(inp.state || '隐态');
176
+ if (!STATES.includes(state)) return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: 'state 须为 显态/隐态' };
177
+ const userNeeds = inp.userNeeds === true;
178
+ const userFocused = inp.userFocused === true;
179
+ const emergent = inp.emergency === true;
180
+ /* 第5条:需要时显 / 专注时隐 / 紧急必显 */
181
+ const shouldBe = emergent || userNeeds ? '显态' : userFocused ? '隐态' : state;
182
+ const switchCorrect = shouldBe === state;
183
+ /* 喧宾夺主:频繁提示/过度推荐/强行介入 */
184
+ const intrusions = { frequentPrompts: Number(inp.frequentPrompts || 0), overRecommend: Number(inp.overRecommend || 0), forcedIntervention: inp.forcedIntervention === true };
185
+ const intrusionScore = round(clamp01(intrusions.frequentPrompts / 5 + intrusions.overRecommend / 5 + (intrusions.forcedIntervention ? 0.5 : 0)));
186
+ const notIntrusive = intrusionScore < 0.5;
187
+ const ok = switchCorrect && notIntrusive;
188
+ return { success: ok, kind: 'rule', output: {
189
+ state, shouldBe, switchCorrect, triggers: ['用户主动', '系统检测', '环境变化', '时间预设'],
190
+ intrusions, intrusionScore, notIntrusive,
191
+ states: { 显态: '主动输出并交互', 隐态: '被动感知不输出' },
192
+ rules: ['用户需要时显,专注时隐', '切换平滑无感', '归无≠虚无:仍在感知,只停输出', '喧宾夺主是大忌'],
193
+ },
194
+ error: !switchCorrect ? `显隐状态错误:应为 ${shouldBe} 实为 ${state}` : !notIntrusive ? `喧宾夺主指数 ${intrusionScore} ≥ 0.5` : undefined,
195
+ reason: ok ? undefined : 'RULE_VIOLATED',
196
+ provenance: [`[${tid}] rvcp: ${state}(应${shouldBe}) 喧宾指数 ${intrusionScore}`] };
197
+ },
198
+ });
199
+
200
+ /* 10. NEVCP 无无协议(双舍双遣 + 无住)*/
201
+ P.push({
202
+ id: 'PROTOCOL_NEVCP_V1_0', name: '无无协议', kind: 'pipeline', src: 'yaml-block 1844 字符 · 三章',
203
+ implements: ['第4条 双舍(舍有/舍无/舍「舍」)', '第5条 双遣(遣言/遣无言/遣「遣」)', '第7条 无住(不住有/无/空/不空/不住于不住)'],
204
+ exec(inp, tid) {
205
+ const SHE = ['舍有', '舍无', '舍舍'];
206
+ const QIAN = ['遣言', '遣无言', '遣遣'];
207
+ const she = (inp.she || []).filter((x) => SHE.includes(x));
208
+ const qian = (inp.qian || []).filter((x) => QIAN.includes(x));
209
+ const zhu = inp.attachments || []; // 所住之处
210
+ const ZHU_FORBIDDEN = ['有', '无', '空', '不空', '不住'];
211
+ const stillAttached = zhu.filter((z) => ZHU_FORBIDDEN.includes(z));
212
+ const ok = she.length === 3 && qian.length === 3 && stillAttached.length === 0;
213
+ return { success: ok, kind: 'pipeline', output: {
214
+ she: { required: SHE, done: she, missing: SHE.filter((x) => !she.includes(x)) },
215
+ qian: { required: QIAN, done: qian, missing: QIAN.filter((x) => !qian.includes(x)) },
216
+ zhu: { attachments: zhu, forbidden: ZHU_FORBIDDEN, stillAttached },
217
+ loop: ['觉察执着', '主动放下', '觉察「放下」的执着', '再放下', '直至无放可放'],
218
+ rules: ['双舍之后还有一舍:舍「舍」', '双遣之后还有一遣:遣「遣」', '不住于「不住」'],
219
+ },
220
+ error: she.length < 3 ? `双舍未完成:缺 ${SHE.filter((x) => !she.includes(x)).join('/')}` : qian.length < 3 ? `双遣未完成:缺 ${QIAN.filter((x) => !qian.includes(x)).join('/')}` : stillAttached.length ? `仍有所住:${stillAttached.join('/')}` : undefined,
221
+ reason: ok ? undefined : 'PRECONDITION_FAILED',
222
+ provenance: [`[${tid}] nevcp: 双舍 ${she.length}/3,双遣 ${qian.length}/3,所住 ${stillAttached.length}`] };
223
+ },
224
+ });
225
+
226
+ /* ══════════ 见证族 3 条 ══════════ */
227
+
228
+ /* 11. SWP 自我见证 */
229
+ P.push({
230
+ id: 'PROTOCOL_SWP_V1_0', name: '自我见证协议', kind: 'rule', src: 'yaml-block 1748 字符 · 三章',
231
+ implements: ['第1条 持续自观(关键实时/常规每秒/批量每分钟)', '第4条 自画像七内容', '第5条 五频率(10s/日/周/月/年)'],
232
+ exec(inp, tid) {
233
+ const PORTRAIT = ['systemHealth', 'engineHealth', 'layerHealth', 'couplingStatus', 'negentropyRate', 'issues', 'risks'];
234
+ const p = inp.portrait || {};
235
+ const missing = PORTRAIT.filter((k) => p[k] === undefined);
236
+ const freq = ['10s', 'daily', 'weekly', 'monthly', 'yearly'];
237
+ const honest = inp.honest === true;
238
+ const signed = !!(inp.observer && inp.timestamp);
239
+ const ok = missing.length === 0 && honest && signed;
240
+ return { success: ok, kind: 'rule', output: {
241
+ portraitFields: PORTRAIT, missing, healthScore: p.systemHealth ?? null,
242
+ frequencies: freq, observationCadence: { critical: 'realtime', normal: '1s', batch: '60s' },
243
+ honesty: { honest, signed, observer: inp.observer || null, timestamp: inp.timestamp || null },
244
+ presentation: ['概要', '详情', '原始数据'],
245
+ rules: ['观察结果必须真实不可篡改', '不得美化/隐瞒/歪曲', '须含时间戳+观察者签名'],
246
+ },
247
+ error: missing.length ? `自画像缺 ${missing.length} 项:${missing.join('/')}` : !honest ? '第2条:违反诚实记录原则' : !signed ? '第2条:缺时间戳或观察者签名' : undefined,
248
+ reason: ok ? undefined : 'RULE_VIOLATED',
249
+ provenance: [`[${tid}] swp: 七内容 ${7 - missing.length}/7,诚实 ${honest},签名 ${signed}`] };
250
+ },
251
+ });
252
+
253
+ /* 12. PWP 过程见证 */
254
+ P.push({
255
+ id: 'PROTOCOL_PWP_V1_0', name: '过程见证协议', kind: 'pipeline', src: 'yaml-block 2183 字符',
256
+ implements: ['过程五要素(起点/步骤/决策点/结果/证据)', '全程可回放'],
257
+ exec(inp, tid) {
258
+ const need = ['startPoint', 'steps', 'decisionPoints', 'result', 'evidence'];
259
+ const c = clauses(need, inp);
260
+ const steps = Array.isArray(inp.steps) ? inp.steps.length : 0;
261
+ const ok = c.ok && steps > 0;
262
+ return { success: ok, kind: 'pipeline', output: { required: need, missing: c.miss, stepCount: steps, replayable: ok },
263
+ error: c.miss.length ? `过程见证缺:${c.miss.join('/')}` : steps === 0 ? '步骤为空' : undefined,
264
+ reason: ok ? undefined : 'PRECONDITION_FAILED',
265
+ provenance: [`[${tid}] pwp: 五要素 ${5 - c.miss.length}/5,步骤 ${steps}`] };
266
+ },
267
+ });
268
+
269
+ /* 13. TWP 思想见证 */
270
+ P.push({
271
+ id: 'PROTOCOL_TWP_V1_0', name: '思想见证协议', kind: 'rule', src: 'yaml-block 1497 字符',
272
+ implements: ['思想溯源(来源/演化/影响)', '思想不可无据'],
273
+ exec(inp, tid) {
274
+ const c = clauses(['source', 'evolution', 'impact'], inp);
275
+ const ok = c.ok;
276
+ return { success: ok, kind: 'rule', output: { required: ['source', 'evolution', 'impact'], missing: c.miss },
277
+ error: ok ? undefined : `思想见证缺:${c.miss.join('/')}`, reason: ok ? undefined : 'RULE_VIOLATED',
278
+ provenance: [`[${tid}] twp: ${3 - c.miss.length}/3`] };
279
+ },
280
+ });
281
+
282
+ /* ══════════ 接入族 5 条 ══════════ */
283
+
284
+ /* 14. EAP 人人接入 */
285
+ P.push({
286
+ id: 'PROTOCOL_EAP_V1_0', name: '人人接入协议', kind: 'rule', src: 'yaml-block 1592 字符 · 三章',
287
+ implements: ['第1条 零门槛(无需注册登录、系统适应用户)', '第2条 包容性(残障/语言障碍/老年/儿童/低带宽五通道)', '第3条 隐私原生', '第4条 反向训练(5-8 轮、耦生度阈值 0.7)'],
288
+ exec(inp, tid) {
289
+ const CHANNELS = ['无障碍', '非语言', '老年简化', '儿童适龄', '低带宽'];
290
+ const provided = (inp.channels || []).filter((c) => CHANNELS.includes(c));
291
+ const noLogin = inp.registrationRequired !== true;
292
+ const privacyLocal = inp.localProcessing !== false;
293
+ const rounds = Number(inp.dialogRounds ?? 0);
294
+ const coupling = clamp01(inp.coupling ?? 0);
295
+ const threshold = Number(inp.couplingThreshold ?? 0.7);
296
+ const reverseTrainingOk = rounds >= 5 && rounds <= 8;
297
+ const canLoadPersonal = coupling >= threshold;
298
+ const ok = noLogin && privacyLocal && provided.length === CHANNELS.length && reverseTrainingOk;
299
+ return { success: ok, kind: 'rule', output: {
300
+ zeroBarrier: { noRegistrationRequired: noLogin, adaptsToUser: true },
301
+ inclusivity: { requiredChannels: CHANNELS, provided, missing: CHANNELS.filter((c) => !provided.includes(c)) },
302
+ privacy: { localProcessing: privacyLocal, deletable: true, needsExplicitConsent: true },
303
+ reverseTraining: { rounds, roundRange: [5, 8], coupling: round(coupling), threshold, canLoadPersonalKnowledge: canLoadPersonal },
304
+ inputModes: ['语音', '文字', '表情', '手势', '生理信号', '脑电波'],
305
+ },
306
+ error: !noLogin ? '第1条:基础功能不得要求注册登录' : !privacyLocal ? '第3条:数据须默认本地处理' : provided.length < CHANNELS.length ? `第2条:缺 ${CHANNELS.filter((c) => !provided.includes(c)).join('/')} 通道` : !reverseTrainingOk ? `第4条:反向训练轮次 ${rounds} 不在 5-8` : undefined,
307
+ reason: ok ? undefined : 'RULE_VIOLATED',
308
+ provenance: [`[${tid}] eap: 通道 ${provided.length}/5,反向训练 ${rounds} 轮,耦生 ${round(coupling)}`] };
309
+ },
310
+ });
311
+
312
+ /* 15. SAP 沉默者接入 */
313
+ P.push({
314
+ id: 'PROTOCOL_SAP_V1_0', name: '沉默者接入协议', kind: 'rule', src: 'yaml-block 1838 字符',
315
+ implements: ['第1条 四类沉默者(医学/情境/偏好/临时)', '第2条 自动检测', '第3-5条 非语言信号(生理/行为/环境)'],
316
+ exec(inp, tid) {
317
+ const TYPES = ['医学沉默者', '情境沉默者', '偏好沉默者', '临时沉默者'];
318
+ const t = String(inp.silentType || '');
319
+ if (!TYPES.includes(t)) return { success: false, kind: 'rule', reason: 'INVALID_INPUT', error: `沉默者类型须为 ${TYPES.join('/')}` };
320
+ const SIGNALS = ['EEG', 'HRV', 'EDA', '呼吸', 'EMG'];
321
+ const BEHAVIOR = ['眼动', '微表情', '头部姿态', '手势', '身体姿态', '停留时间'];
322
+ const sig = (inp.physioSignals || []).filter((s) => SIGNALS.includes(s));
323
+ const beh = (inp.behaviorSignals || []).filter((b) => BEHAVIOR.includes(b));
324
+ const detected = inp.autoDetected === true || inp.selfSelected === true;
325
+ const ok = detected && sig.length > 0 && beh.length > 0;
326
+ return { success: ok, kind: 'rule', output: {
327
+ silentType: t, typeWhitelist: TYPES,
328
+ detection: { autoDetected: !!inp.autoDetected, selfSelected: !!inp.selfSelected, cues: ['长时间无语音', '对询问无响应', '生理显示不适', '主动选择'] },
329
+ physio: { supported: SIGNALS, used: sig }, behavior: { supported: BEHAVIOR, used: beh },
330
+ lowCognitiveLoad: true,
331
+ },
332
+ error: !detected ? '未检测到沉默状态(自动检测或主动选择至少其一)' : sig.length === 0 ? '缺生理信号采集(第3条)' : beh.length === 0 ? '缺行为模式采集(第4条)' : undefined,
333
+ reason: ok ? undefined : 'RULE_VIOLATED',
334
+ provenance: [`[${tid}] sap: ${t},生理 ${sig.length} / 行为 ${beh.length}`] };
335
+ },
336
+ });
337
+
338
+ /* 16. PVP 人人归无 */
339
+ P.push({
340
+ id: 'PROTOCOL_PVP_V1_0', name: '人人归无协议', kind: 'rule', src: 'yaml-block 1759 字符',
341
+ implements: ['归无是过程不是终点', '不喧宾夺主', '随时可显现'],
342
+ exec(inp, tid) {
343
+ const silent = inp.silentMode === true;
344
+ const canRestore = inp.canRestoreInstantly !== false;
345
+ const intrusive = Number(inp.intrusionCount || 0);
346
+ const ok = canRestore && intrusive <= 3;
347
+ return { success: ok, kind: 'rule', output: { silentMode: silent, canRestoreInstantly: canRestore, intrusionCount: intrusive, maxIntrusion: 3,
348
+ rules: ['归无≠虚无:仍在感知', '归无是过程不是终点', '随时可显现'] },
349
+ error: !canRestore ? '归无后无法即时恢复显现' : intrusive > 3 ? `介入 ${intrusive} 次 > 3,喧宾夺主` : undefined,
350
+ reason: ok ? undefined : 'RULE_VIOLATED',
351
+ provenance: [`[${tid}] pvp: 隐态 ${silent},可恢复 ${canRestore},介入 ${intrusive}`] };
352
+ },
353
+ });
354
+
355
+ /* 17. WCP 无言 */
356
+ P.push({
357
+ id: 'PROTOCOL_WCP_V1_0', name: '无言协议', kind: 'rule', src: 'yaml-block 1856 字符',
358
+ implements: ['该默时默(价值低于风险)', '沉默不是失职', '默亦可传意'],
359
+ exec(inp, tid) {
360
+ const value = clamp01(inp.speakingValue ?? 0.5), risk = clamp01(inp.speakingRisk ?? 0.4);
361
+ const shouldSpeak = value >= risk;
362
+ const rawChoice = String(inp.chose || ''); /* 原始选择字符串 */
363
+ const hasChoice = rawChoice === 'speak' || rawChoice === 'silent';
364
+ const correct = hasChoice ? (rawChoice === 'speak') === shouldSpeak : null;
365
+ const ok = correct !== false;
366
+ return { success: ok, kind: 'rule', output: { speakingValue: value, speakingRisk: risk, shouldSpeak, chose: rawChoice, hasChoice, correct,
367
+ rules: ['价值低于风险时该默', '沉默不是失职', '默亦可传意'] },
368
+ error: correct === false ? `应${shouldSpeak ? '说' : '默'}却选择了${chose === 'speak' ? '说' : '默'}` : undefined,
369
+ reason: ok ? undefined : 'RULE_VIOLATED',
370
+ provenance: [`[${tid}] wcp: 价值 ${value} / 风险 ${risk} → 应${shouldSpeak ? '说' : '默'}`] };
371
+ },
372
+ });
373
+
374
+ /* 18. SCP 睡眠协作 */
375
+ P.push({
376
+ id: 'PROTOCOL_SCP_V1_0', name: '睡眠协作协议', kind: 'compute', src: 'yaml-block 2288 字符',
377
+ implements: ['睡眠期系统行为(低功耗/批处理/不打扰)', '唤醒条件', '睡眠分期协作'],
378
+ exec(inp, tid) {
379
+ const stage = String(inp.sleepStage || 'N2');
380
+ const STAGES = ['N1', 'N2', 'N3', 'REM'];
381
+ if (!STAGES.includes(stage)) return { success: false, kind: 'compute', reason: 'INVALID_INPUT', error: `sleepStage 须为 ${STAGES.join('/')}` };
382
+ const DEVICE = { N1: '轻度批处理', N2: '常规批处理', N3: '仅关键任务', REM: '不打扰(梦境保护)' };
383
+ const critical = inp.criticalEvent === true;
384
+ const allowed = critical || stage !== 'REM';
385
+ const ok = allowed;
386
+ return { success: ok, kind: 'compute', output: {
387
+ sleepStage: stage, behavior: DEVICE[stage], criticalEvent: critical, allowed,
388
+ wakeConditions: ['关键告警', '用户主动唤醒', '预设时间到'],
389
+ stages: STAGES, rule: 'REM 期不打扰;深度睡眠仅关键任务',
390
+ },
391
+ error: ok ? undefined : '第X条:REM 期不得打扰(梦境保护)', reason: ok ? undefined : 'RULE_VIOLATED',
392
+ provenance: [`[${tid}] scp: ${stage} → ${DEVICE[stage]},关键事件 ${critical}`] };
393
+ },
394
+ });
395
+
396
+ const INDEX = new Map(P.map((p) => [p.id, p]));
397
+ const kindOf = (id) => { const p = INDEX.get(id); return p ? p.kind : 'declared'; };
398
+ const getRegistry = () => P.map((p) => ({ id: p.id, name: p.name, kind: p.kind, src: p.src, implements: p.implements }));
399
+
400
+ function executeSync(id, input) {
401
+ const t0 = Date.now(), tid = newTraceId();
402
+ const p = INDEX.get(id);
403
+ if (!p) return { success: false, kind: 'declared', reason: 'NOT_FOUND', error: `协议 ${id} 未登记`, provenance: [], traceId: tid, durationMs: 0 };
404
+ let r;
405
+ try { r = p.exec(input || {}, tid); } catch (e) { r = { success: false, kind: p.kind, error: 'THROW: ' + e.message, provenance: [`[${tid}] THROW`] }; }
406
+ return { ...r, protocolId: id, protocolName: p.name, traceId: tid, durationMs: Date.now() - t0 };
407
+ }
408
+
409
+ module.exports = { executeSync, execute: async (i, n) => executeSync(i, n), kindOf, getRegistry, PROTOCOLS: P, INDEX, newTraceId };