ronds_ai 0.13.0 → 0.14.0
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/LICENSE +21 -21
- package/PRIVACY.md +56 -56
- package/README.md +59 -59
- package/bin/ronds_ai.js +10 -1
- package/contracts/component-protocol.json +85 -74
- package/lib/component-store.js +245 -245
- package/lib/consent.js +238 -221
- package/lib/default-org.js +27 -27
- package/lib/gateway.js +195 -185
- package/lib/legacy.js +760 -760
- package/lib/manager-store.js +703 -703
- package/lib/paths.js +24 -24
- package/lib/uninstall.js +239 -239
- package/lib/update-manager.js +247 -247
- package/package.json +1 -1
package/lib/component-store.js
CHANGED
|
@@ -1,245 +1,245 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
/**
|
|
3
|
-
* 组件本地安装与激活(设计 §8/§11):暂存 → 摘要核验 → 安全解包 → 隔离自检
|
|
4
|
-
* (capabilities/health + N05 能力字段全量验收与必需 dispatch 导出/require 无副作用
|
|
5
|
-
* 探针)→ 原子激活 current.json。version-id = 版本 + 内容标识。
|
|
6
|
-
* 激活即写资格字段(R06):status=confirmed(安装链隔离自检通过才激活)与组件回报的
|
|
7
|
-
* protocolVersion/managerRange;网关加载前按 status/protocolVersion/隔离清单查禁。
|
|
8
|
-
* F05 本机兼容预检:checkLocalCompatibility 在锁内 accepted 水位之后(N04)与激活前
|
|
9
|
-
* (installComponent 内把关,覆盖全部安装入口)对清单声明的 managerRange/nodeRange/
|
|
10
|
-
* protocolVersion/stateSchemaVersion 校验本机管理器版本、Node 版本、稳定入口协议与
|
|
11
|
-
* 状态 schema;不兼容或范围语法非法一律拒绝,保留旧组合不激活。
|
|
12
|
-
* N05 归档能力验收:签名清单兼容而归档打包回报不兼容(caps 范围/schema/statusFormat)
|
|
13
|
-
* 或缺必需 dispatch 导出、require 有副作用 → 同样拒绝确认,current 不动(元数据/打包
|
|
14
|
-
* 不一致的可靠性缺口在安装期拦截,不等到网关首调才发现业务停用)。
|
|
15
|
-
*/
|
|
16
|
-
const fs = require('fs');
|
|
17
|
-
const path = require('path');
|
|
18
|
-
const crypto = require('crypto');
|
|
19
|
-
const { execFileSync } = require('child_process');
|
|
20
|
-
const paths = require('./paths');
|
|
21
|
-
const { extractTgz } = require('./archive');
|
|
22
|
-
const { readJson, atomicWrite } = require('./consent');
|
|
23
|
-
const updateManager = require('./update-manager');
|
|
24
|
-
|
|
25
|
-
const ENTRY = path.join('package', 'bin', 'component.js');
|
|
26
|
-
const MANAGER_VERSION = require('../package.json').version;
|
|
27
|
-
// 稳定入口协议常量(与 bin/ronds_ai.js、dispatch.cjs 网关保持一致)
|
|
28
|
-
const PROTOCOL_VERSION = 1;
|
|
29
|
-
// 本机管理器支持的组件状态 schema 上限(当前仅 1)
|
|
30
|
-
const SUPPORTED_STATE_SCHEMA_VERSION = 1;
|
|
31
|
-
|
|
32
|
-
// ---- F05:本机兼容预检 ----
|
|
33
|
-
|
|
34
|
-
/** 范围比较器版本归一化:1-3 段纯数字(缺段补 0)或 '*'(任意);非法返回 null。 */
|
|
35
|
-
function normalizeRangeVersion(ver) {
|
|
36
|
-
if (ver === '*') return { any: true };
|
|
37
|
-
if (typeof ver !== 'string' || !/^\d+(\.\d+){0,2}$/.test(ver)) return null;
|
|
38
|
-
const parts = ver.split('.');
|
|
39
|
-
return { any: false, ver: `${parts[0]}.${parts[1] || '0'}.${parts[2] || '0'}` };
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* 简单 SemVer 范围解析:空格分隔 AND 的比较器(>=、<=、>、<、= 或裸版本;
|
|
44
|
-
* 版本支持 1-3 段数字与 '*'),不支持 || / 连字符区间。复用 update-manager
|
|
45
|
-
* 的 SemVer 比较语义(compareVersions)。
|
|
46
|
-
* @returns {Array<{op:string, ver:string|null}>|null} 非法语法返回 null。
|
|
47
|
-
*/
|
|
48
|
-
function parseRange(range) {
|
|
49
|
-
if (typeof range !== 'string' || !range.trim()) return null;
|
|
50
|
-
const comparators = [];
|
|
51
|
-
for (const token of range.trim().split(/\s+/)) {
|
|
52
|
-
const m = /^(>=|<=|>|<|=)?(.+)$/.exec(token);
|
|
53
|
-
if (!m) return null;
|
|
54
|
-
const normalized = normalizeRangeVersion(m[2]);
|
|
55
|
-
if (!normalized) return null;
|
|
56
|
-
comparators.push({ op: m[1] || '=', ver: normalized.any ? null : normalized.ver });
|
|
57
|
-
}
|
|
58
|
-
return comparators;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function satisfiesRange(version, comparators) {
|
|
62
|
-
for (const { op, ver } of comparators) {
|
|
63
|
-
if (ver === null) continue; // '*' 恒真
|
|
64
|
-
const cmp = updateManager.compareVersions(version, ver);
|
|
65
|
-
if (cmp === null) return false;
|
|
66
|
-
if (op === '>=' && cmp < 0) return false;
|
|
67
|
-
if (op === '<=' && cmp > 0) return false;
|
|
68
|
-
if (op === '>' && cmp <= 0) return false;
|
|
69
|
-
if (op === '<' && cmp >= 0) return false;
|
|
70
|
-
if (op === '=' && cmp !== 0) return false;
|
|
71
|
-
}
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
function requireCompatibleRange(range, actual, field, subject, prefix = '清单') {
|
|
76
|
-
const comparators = parseRange(range);
|
|
77
|
-
if (!comparators) throw new Error(`${prefix} ${field} 范围语法非法:${JSON.stringify(range)}`);
|
|
78
|
-
if (!satisfiesRange(actual, comparators)) {
|
|
79
|
-
throw new Error(`${prefix} ${field} 不兼容本机${subject}:要求 ${range},本机 ${actual}`);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* F05 本机兼容预检(下载前与激活前共用):清单声明的协议版本、状态 schema、
|
|
85
|
-
* 管理器/Node 兼容范围必须对本机成立;缺失字段或非法范围语法同样拒绝(fail-closed)。
|
|
86
|
-
* @throws 不兼容/非法时不改任何状态,调用方保留旧组合。
|
|
87
|
-
*/
|
|
88
|
-
function checkLocalCompatibility(payload) {
|
|
89
|
-
if (!Number.isInteger(payload.protocolVersion) || payload.protocolVersion !== PROTOCOL_VERSION) {
|
|
90
|
-
throw new Error(`清单协议版本不兼容:${JSON.stringify(payload.protocolVersion)} ≠ 稳定入口 ${PROTOCOL_VERSION},拒绝安装`);
|
|
91
|
-
}
|
|
92
|
-
if (!Number.isInteger(payload.stateSchemaVersion)
|
|
93
|
-
|| payload.stateSchemaVersion < 1 || payload.stateSchemaVersion > SUPPORTED_STATE_SCHEMA_VERSION) {
|
|
94
|
-
throw new Error(`清单 stateSchemaVersion 不受本机支持:${JSON.stringify(payload.stateSchemaVersion)}(支持 1..${SUPPORTED_STATE_SCHEMA_VERSION}),拒绝安装`);
|
|
95
|
-
}
|
|
96
|
-
requireCompatibleRange(payload.managerRange, MANAGER_VERSION, 'managerRange', `管理器版本 ${MANAGER_VERSION}`);
|
|
97
|
-
requireCompatibleRange(payload.nodeRange, process.versions.node, 'nodeRange', `Node 版本 ${process.version}`);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* N05:归档 capabilities 能力字段全量验收(激活前隔离自检的一部分)。
|
|
102
|
-
* 归档实际回报的字段逐一按本机判定——签名清单兼容而归档打包回报不兼容(元数据/
|
|
103
|
-
* 打包不一致的可靠性缺口)在此拒绝确认:
|
|
104
|
-
* - statusFormat:必报且为本机已知状态格式(v1);
|
|
105
|
-
* - managerRange:必报;checkLocalCompatibility 同款范围解析 + 本机管理器版本判定;
|
|
106
|
-
* - nodeRange / stateSchemaVersion:归档回报即核验(同款范围解析/受支持上限);
|
|
107
|
-
* 未回报时以签名清单声明为准(checkLocalCompatibility 已把关,fail-closed)。
|
|
108
|
-
* @throws 任一字段缺失(必报项)/非法/不兼容即抛错,调用方不确认、current 不动。
|
|
109
|
-
*/
|
|
110
|
-
function checkCapsCompatibility(caps) {
|
|
111
|
-
if (caps.statusFormat !== 'v1') {
|
|
112
|
-
throw new Error(`归档 capabilities statusFormat 不受支持:${JSON.stringify(caps.statusFormat)}(支持 v1),拒绝确认`);
|
|
113
|
-
}
|
|
114
|
-
requireCompatibleRange(caps.managerRange, MANAGER_VERSION, 'capabilities managerRange', `管理器版本 ${MANAGER_VERSION}`, '归档');
|
|
115
|
-
if (caps.nodeRange !== undefined) {
|
|
116
|
-
requireCompatibleRange(caps.nodeRange, process.versions.node, 'capabilities nodeRange', `Node 版本 ${process.version}`, '归档');
|
|
117
|
-
}
|
|
118
|
-
if (caps.stateSchemaVersion !== undefined
|
|
119
|
-
&& (!Number.isInteger(caps.stateSchemaVersion) || caps.stateSchemaVersion < 1 || caps.stateSchemaVersion > SUPPORTED_STATE_SCHEMA_VERSION)) {
|
|
120
|
-
throw new Error(`归档 capabilities stateSchemaVersion 不受本机支持:${JSON.stringify(caps.stateSchemaVersion)}(支持 1..${SUPPORTED_STATE_SCHEMA_VERSION}),拒绝确认`);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* N05:归档入口模块必需 dispatch 导出验收(§5.1 固定 inProcessExport 合同)。
|
|
126
|
-
* 在子进程中 require 归档入口(cwd=隔离目录、RONDS_AI_HOME 指向隔离探针目录):
|
|
127
|
-
* - dispatch 必须存在且为函数(网关 require 后即调,安装时验收而非首调时才发现);
|
|
128
|
-
* - require 无副作用承诺:require 前后对隔离目录做递归快照(相对路径+内容摘要),
|
|
129
|
-
* 不一致即拒——安装期入口模块不得擅自写文件(网络副作用同以探针目录隔离暴露)。
|
|
130
|
-
* 子进程执行:入口 require 崩溃/超时不波及管理器进程。
|
|
131
|
-
* @throws 缺导出/类型错误/require 崩溃/超时/文件副作用。
|
|
132
|
-
*/
|
|
133
|
-
function verifyDispatchExport(stageDir, entry) {
|
|
134
|
-
const before = snapshotTree(stageDir);
|
|
135
|
-
const script = [
|
|
136
|
-
`const mod = require(${JSON.stringify(entry)});`,
|
|
137
|
-
"const t = mod && typeof mod.dispatch;",
|
|
138
|
-
"if (t !== 'function') { process.stderr.write('dispatch 导出类型 ' + t); process.exit(3); }",
|
|
139
|
-
"process.stdout.write('ok');",
|
|
140
|
-
].join('\n');
|
|
141
|
-
let stdout;
|
|
142
|
-
try {
|
|
143
|
-
stdout = execFileSync(process.execPath, ['-e', script], {
|
|
144
|
-
encoding: 'utf8', windowsHide: true, timeout: 15000,
|
|
145
|
-
cwd: stageDir,
|
|
146
|
-
env: { ...process.env, RONDS_AI_HOME: path.join(stageDir, '.dispatch-probe-home') },
|
|
147
|
-
});
|
|
148
|
-
} catch (e) {
|
|
149
|
-
if (e.status === 3) throw new Error(`归档入口 dispatch 导出缺失或非函数:${String(e.stderr || '').trim()}`);
|
|
150
|
-
if (e.killed || e.signal) throw new Error('归档入口 require 验证超时(15s),拒绝确认');
|
|
151
|
-
throw new Error(`归档入口 require 失败(退出码 ${e.status}):${String(e.stderr || e.message).trim().slice(0, 300)}`);
|
|
152
|
-
}
|
|
153
|
-
if (stdout.trim() !== 'ok') throw new Error('归档入口 require 探针输出异常,拒绝确认');
|
|
154
|
-
const after = snapshotTree(stageDir);
|
|
155
|
-
if (JSON.stringify(before) !== JSON.stringify(after)) {
|
|
156
|
-
const diff = after.filter((x) => !before.some((y) => y[0] === x[0] && y[1] === x[1])).map((x) => x[0]);
|
|
157
|
-
throw new Error(`归档入口 require 产生文件副作用(隔离目录快照不一致:${diff.slice(0, 5).join(', ')}),拒绝确认`);
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/** 隔离目录递归快照:[相对路径, 内容 sha256],按路径排序(快照对比用)。 */
|
|
162
|
-
function snapshotTree(root) {
|
|
163
|
-
const entries = [];
|
|
164
|
-
const walk = (dir, rel) => {
|
|
165
|
-
for (const name of fs.readdirSync(dir).sort()) {
|
|
166
|
-
const abs = path.join(dir, name);
|
|
167
|
-
const r = rel ? `${rel}/${name}` : name;
|
|
168
|
-
if (fs.statSync(abs).isDirectory()) walk(abs, r);
|
|
169
|
-
else entries.push([r, crypto.createHash('sha256').update(fs.readFileSync(abs)).digest('hex')]);
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
walk(root, '');
|
|
173
|
-
return entries;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function isolatedCheck(entry, payload) {
|
|
177
|
-
const caps = JSON.parse(execFileSync(process.execPath, [entry, 'capabilities'], { encoding: 'utf8', windowsHide: true, timeout: 15000 }));
|
|
178
|
-
if (caps.componentId !== payload.componentId) throw new Error(`componentId 不符:${caps.componentId}`);
|
|
179
|
-
if (caps.version !== payload.version) throw new Error(`版本不符:${caps.version} != ${payload.version}`);
|
|
180
|
-
if (caps.protocolVersion !== payload.protocolVersion) throw new Error(`protocolVersion 不符:${caps.protocolVersion}`);
|
|
181
|
-
checkCapsCompatibility(caps); // N05:能力字段全量验收(statusFormat/managerRange/nodeRange/stateSchemaVersion)
|
|
182
|
-
const health = JSON.parse(execFileSync(process.execPath, [entry, 'health'], { encoding: 'utf8', windowsHide: true, timeout: 15000 }));
|
|
183
|
-
if (health.status !== 'ok') throw new Error(`health=${health.status}`);
|
|
184
|
-
return caps;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function loadCurrent() {
|
|
188
|
-
const cur = readJson(paths.componentCurrent());
|
|
189
|
-
if (!cur || cur.schemaVersion !== 1 || typeof cur.versionId !== 'string') return null;
|
|
190
|
-
return cur;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
function componentStatus() {
|
|
194
|
-
const cur = loadCurrent();
|
|
195
|
-
if (!cur) return { installed: false, version: null, versionId: null };
|
|
196
|
-
return { installed: true, version: cur.version, versionId: cur.versionId };
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* 安装并激活。任何一步失败都不改 current 指针、不留半成品为当前版本。
|
|
201
|
-
* @returns {{versionId:string, archiveSha512:string}}
|
|
202
|
-
*/
|
|
203
|
-
function installComponent(tgz, payload) {
|
|
204
|
-
checkLocalCompatibility(payload); // F05:激活前本机兼容把关(全部安装入口共用)
|
|
205
|
-
const sha512b64 = crypto.createHash('sha512').update(tgz).digest('base64');
|
|
206
|
-
const versionId = `${payload.version}-${crypto.createHash('sha256').update(tgz).digest('hex').slice(0, 12)}`;
|
|
207
|
-
const versionsDir = paths.componentVersionsDir();
|
|
208
|
-
fs.mkdirSync(versionsDir, { recursive: true });
|
|
209
|
-
const finalDir = path.join(versionsDir, versionId);
|
|
210
|
-
if (loadCurrent() && loadCurrent().versionId === versionId) {
|
|
211
|
-
return { versionId, archiveSha512: sha512b64, alreadyActive: true };
|
|
212
|
-
}
|
|
213
|
-
const stage = fs.mkdtempSync(path.join(versionsDir, '.staging-'));
|
|
214
|
-
let caps;
|
|
215
|
-
try {
|
|
216
|
-
extractTgz(tgz, stage);
|
|
217
|
-
const entry = path.join(stage, ENTRY);
|
|
218
|
-
if (!fs.existsSync(entry)) throw new Error(`归档缺少组件入口:${ENTRY}`);
|
|
219
|
-
caps = isolatedCheck(entry, payload);
|
|
220
|
-
verifyDispatchExport(stage, entry); // N05:必需 dispatch 导出 + require 无副作用(隔离目录快照对比)
|
|
221
|
-
fs.mkdirSync(versionsDir, { recursive: true });
|
|
222
|
-
const keepFinal = fs.existsSync(finalDir);
|
|
223
|
-
if (keepFinal) fs.rmSync(finalDir, { recursive: true, force: true }); // 同 version-id 重装:内容标识一致才可能发生
|
|
224
|
-
fs.renameSync(stage, finalDir);
|
|
225
|
-
} finally {
|
|
226
|
-
try { fs.rmSync(stage, { recursive: true, force: true }); } catch { /* 已改名 */ }
|
|
227
|
-
}
|
|
228
|
-
atomicWrite(paths.componentCurrent(), {
|
|
229
|
-
schemaVersion: 1,
|
|
230
|
-
version: payload.version,
|
|
231
|
-
versionId,
|
|
232
|
-
archiveSha512: sha512b64,
|
|
233
|
-
entry: ENTRY,
|
|
234
|
-
status: 'confirmed', // R06:隔离自检通过才激活,激活即已确认;网关据此查禁 pending/无状态指针
|
|
235
|
-
protocolVersion: caps.protocolVersion, // R06:网关与模板 PROTOCOL_VERSION 一致才加载
|
|
236
|
-
managerRange: caps.managerRange, // R06:组件回报的兼容范围,供管理器侧升级判断
|
|
237
|
-
installedAt: new Date().toISOString(),
|
|
238
|
-
});
|
|
239
|
-
return { versionId, archiveSha512: sha512b64 };
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
module.exports = {
|
|
243
|
-
PROTOCOL_VERSION, checkLocalCompatibility, checkCapsCompatibility, parseRange,
|
|
244
|
-
installComponent, loadCurrent, componentStatus,
|
|
245
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 组件本地安装与激活(设计 §8/§11):暂存 → 摘要核验 → 安全解包 → 隔离自检
|
|
4
|
+
* (capabilities/health + N05 能力字段全量验收与必需 dispatch 导出/require 无副作用
|
|
5
|
+
* 探针)→ 原子激活 current.json。version-id = 版本 + 内容标识。
|
|
6
|
+
* 激活即写资格字段(R06):status=confirmed(安装链隔离自检通过才激活)与组件回报的
|
|
7
|
+
* protocolVersion/managerRange;网关加载前按 status/protocolVersion/隔离清单查禁。
|
|
8
|
+
* F05 本机兼容预检:checkLocalCompatibility 在锁内 accepted 水位之后(N04)与激活前
|
|
9
|
+
* (installComponent 内把关,覆盖全部安装入口)对清单声明的 managerRange/nodeRange/
|
|
10
|
+
* protocolVersion/stateSchemaVersion 校验本机管理器版本、Node 版本、稳定入口协议与
|
|
11
|
+
* 状态 schema;不兼容或范围语法非法一律拒绝,保留旧组合不激活。
|
|
12
|
+
* N05 归档能力验收:签名清单兼容而归档打包回报不兼容(caps 范围/schema/statusFormat)
|
|
13
|
+
* 或缺必需 dispatch 导出、require 有副作用 → 同样拒绝确认,current 不动(元数据/打包
|
|
14
|
+
* 不一致的可靠性缺口在安装期拦截,不等到网关首调才发现业务停用)。
|
|
15
|
+
*/
|
|
16
|
+
const fs = require('fs');
|
|
17
|
+
const path = require('path');
|
|
18
|
+
const crypto = require('crypto');
|
|
19
|
+
const { execFileSync } = require('child_process');
|
|
20
|
+
const paths = require('./paths');
|
|
21
|
+
const { extractTgz } = require('./archive');
|
|
22
|
+
const { readJson, atomicWrite } = require('./consent');
|
|
23
|
+
const updateManager = require('./update-manager');
|
|
24
|
+
|
|
25
|
+
const ENTRY = path.join('package', 'bin', 'component.js');
|
|
26
|
+
const MANAGER_VERSION = require('../package.json').version;
|
|
27
|
+
// 稳定入口协议常量(与 bin/ronds_ai.js、dispatch.cjs 网关保持一致)
|
|
28
|
+
const PROTOCOL_VERSION = 1;
|
|
29
|
+
// 本机管理器支持的组件状态 schema 上限(当前仅 1)
|
|
30
|
+
const SUPPORTED_STATE_SCHEMA_VERSION = 1;
|
|
31
|
+
|
|
32
|
+
// ---- F05:本机兼容预检 ----
|
|
33
|
+
|
|
34
|
+
/** 范围比较器版本归一化:1-3 段纯数字(缺段补 0)或 '*'(任意);非法返回 null。 */
|
|
35
|
+
function normalizeRangeVersion(ver) {
|
|
36
|
+
if (ver === '*') return { any: true };
|
|
37
|
+
if (typeof ver !== 'string' || !/^\d+(\.\d+){0,2}$/.test(ver)) return null;
|
|
38
|
+
const parts = ver.split('.');
|
|
39
|
+
return { any: false, ver: `${parts[0]}.${parts[1] || '0'}.${parts[2] || '0'}` };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 简单 SemVer 范围解析:空格分隔 AND 的比较器(>=、<=、>、<、= 或裸版本;
|
|
44
|
+
* 版本支持 1-3 段数字与 '*'),不支持 || / 连字符区间。复用 update-manager
|
|
45
|
+
* 的 SemVer 比较语义(compareVersions)。
|
|
46
|
+
* @returns {Array<{op:string, ver:string|null}>|null} 非法语法返回 null。
|
|
47
|
+
*/
|
|
48
|
+
function parseRange(range) {
|
|
49
|
+
if (typeof range !== 'string' || !range.trim()) return null;
|
|
50
|
+
const comparators = [];
|
|
51
|
+
for (const token of range.trim().split(/\s+/)) {
|
|
52
|
+
const m = /^(>=|<=|>|<|=)?(.+)$/.exec(token);
|
|
53
|
+
if (!m) return null;
|
|
54
|
+
const normalized = normalizeRangeVersion(m[2]);
|
|
55
|
+
if (!normalized) return null;
|
|
56
|
+
comparators.push({ op: m[1] || '=', ver: normalized.any ? null : normalized.ver });
|
|
57
|
+
}
|
|
58
|
+
return comparators;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function satisfiesRange(version, comparators) {
|
|
62
|
+
for (const { op, ver } of comparators) {
|
|
63
|
+
if (ver === null) continue; // '*' 恒真
|
|
64
|
+
const cmp = updateManager.compareVersions(version, ver);
|
|
65
|
+
if (cmp === null) return false;
|
|
66
|
+
if (op === '>=' && cmp < 0) return false;
|
|
67
|
+
if (op === '<=' && cmp > 0) return false;
|
|
68
|
+
if (op === '>' && cmp <= 0) return false;
|
|
69
|
+
if (op === '<' && cmp >= 0) return false;
|
|
70
|
+
if (op === '=' && cmp !== 0) return false;
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function requireCompatibleRange(range, actual, field, subject, prefix = '清单') {
|
|
76
|
+
const comparators = parseRange(range);
|
|
77
|
+
if (!comparators) throw new Error(`${prefix} ${field} 范围语法非法:${JSON.stringify(range)}`);
|
|
78
|
+
if (!satisfiesRange(actual, comparators)) {
|
|
79
|
+
throw new Error(`${prefix} ${field} 不兼容本机${subject}:要求 ${range},本机 ${actual}`);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* F05 本机兼容预检(下载前与激活前共用):清单声明的协议版本、状态 schema、
|
|
85
|
+
* 管理器/Node 兼容范围必须对本机成立;缺失字段或非法范围语法同样拒绝(fail-closed)。
|
|
86
|
+
* @throws 不兼容/非法时不改任何状态,调用方保留旧组合。
|
|
87
|
+
*/
|
|
88
|
+
function checkLocalCompatibility(payload) {
|
|
89
|
+
if (!Number.isInteger(payload.protocolVersion) || payload.protocolVersion !== PROTOCOL_VERSION) {
|
|
90
|
+
throw new Error(`清单协议版本不兼容:${JSON.stringify(payload.protocolVersion)} ≠ 稳定入口 ${PROTOCOL_VERSION},拒绝安装`);
|
|
91
|
+
}
|
|
92
|
+
if (!Number.isInteger(payload.stateSchemaVersion)
|
|
93
|
+
|| payload.stateSchemaVersion < 1 || payload.stateSchemaVersion > SUPPORTED_STATE_SCHEMA_VERSION) {
|
|
94
|
+
throw new Error(`清单 stateSchemaVersion 不受本机支持:${JSON.stringify(payload.stateSchemaVersion)}(支持 1..${SUPPORTED_STATE_SCHEMA_VERSION}),拒绝安装`);
|
|
95
|
+
}
|
|
96
|
+
requireCompatibleRange(payload.managerRange, MANAGER_VERSION, 'managerRange', `管理器版本 ${MANAGER_VERSION}`);
|
|
97
|
+
requireCompatibleRange(payload.nodeRange, process.versions.node, 'nodeRange', `Node 版本 ${process.version}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* N05:归档 capabilities 能力字段全量验收(激活前隔离自检的一部分)。
|
|
102
|
+
* 归档实际回报的字段逐一按本机判定——签名清单兼容而归档打包回报不兼容(元数据/
|
|
103
|
+
* 打包不一致的可靠性缺口)在此拒绝确认:
|
|
104
|
+
* - statusFormat:必报且为本机已知状态格式(v1);
|
|
105
|
+
* - managerRange:必报;checkLocalCompatibility 同款范围解析 + 本机管理器版本判定;
|
|
106
|
+
* - nodeRange / stateSchemaVersion:归档回报即核验(同款范围解析/受支持上限);
|
|
107
|
+
* 未回报时以签名清单声明为准(checkLocalCompatibility 已把关,fail-closed)。
|
|
108
|
+
* @throws 任一字段缺失(必报项)/非法/不兼容即抛错,调用方不确认、current 不动。
|
|
109
|
+
*/
|
|
110
|
+
function checkCapsCompatibility(caps) {
|
|
111
|
+
if (caps.statusFormat !== 'v1') {
|
|
112
|
+
throw new Error(`归档 capabilities statusFormat 不受支持:${JSON.stringify(caps.statusFormat)}(支持 v1),拒绝确认`);
|
|
113
|
+
}
|
|
114
|
+
requireCompatibleRange(caps.managerRange, MANAGER_VERSION, 'capabilities managerRange', `管理器版本 ${MANAGER_VERSION}`, '归档');
|
|
115
|
+
if (caps.nodeRange !== undefined) {
|
|
116
|
+
requireCompatibleRange(caps.nodeRange, process.versions.node, 'capabilities nodeRange', `Node 版本 ${process.version}`, '归档');
|
|
117
|
+
}
|
|
118
|
+
if (caps.stateSchemaVersion !== undefined
|
|
119
|
+
&& (!Number.isInteger(caps.stateSchemaVersion) || caps.stateSchemaVersion < 1 || caps.stateSchemaVersion > SUPPORTED_STATE_SCHEMA_VERSION)) {
|
|
120
|
+
throw new Error(`归档 capabilities stateSchemaVersion 不受本机支持:${JSON.stringify(caps.stateSchemaVersion)}(支持 1..${SUPPORTED_STATE_SCHEMA_VERSION}),拒绝确认`);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* N05:归档入口模块必需 dispatch 导出验收(§5.1 固定 inProcessExport 合同)。
|
|
126
|
+
* 在子进程中 require 归档入口(cwd=隔离目录、RONDS_AI_HOME 指向隔离探针目录):
|
|
127
|
+
* - dispatch 必须存在且为函数(网关 require 后即调,安装时验收而非首调时才发现);
|
|
128
|
+
* - require 无副作用承诺:require 前后对隔离目录做递归快照(相对路径+内容摘要),
|
|
129
|
+
* 不一致即拒——安装期入口模块不得擅自写文件(网络副作用同以探针目录隔离暴露)。
|
|
130
|
+
* 子进程执行:入口 require 崩溃/超时不波及管理器进程。
|
|
131
|
+
* @throws 缺导出/类型错误/require 崩溃/超时/文件副作用。
|
|
132
|
+
*/
|
|
133
|
+
function verifyDispatchExport(stageDir, entry) {
|
|
134
|
+
const before = snapshotTree(stageDir);
|
|
135
|
+
const script = [
|
|
136
|
+
`const mod = require(${JSON.stringify(entry)});`,
|
|
137
|
+
"const t = mod && typeof mod.dispatch;",
|
|
138
|
+
"if (t !== 'function') { process.stderr.write('dispatch 导出类型 ' + t); process.exit(3); }",
|
|
139
|
+
"process.stdout.write('ok');",
|
|
140
|
+
].join('\n');
|
|
141
|
+
let stdout;
|
|
142
|
+
try {
|
|
143
|
+
stdout = execFileSync(process.execPath, ['-e', script], {
|
|
144
|
+
encoding: 'utf8', windowsHide: true, timeout: 15000,
|
|
145
|
+
cwd: stageDir,
|
|
146
|
+
env: { ...process.env, RONDS_AI_HOME: path.join(stageDir, '.dispatch-probe-home') },
|
|
147
|
+
});
|
|
148
|
+
} catch (e) {
|
|
149
|
+
if (e.status === 3) throw new Error(`归档入口 dispatch 导出缺失或非函数:${String(e.stderr || '').trim()}`);
|
|
150
|
+
if (e.killed || e.signal) throw new Error('归档入口 require 验证超时(15s),拒绝确认');
|
|
151
|
+
throw new Error(`归档入口 require 失败(退出码 ${e.status}):${String(e.stderr || e.message).trim().slice(0, 300)}`);
|
|
152
|
+
}
|
|
153
|
+
if (stdout.trim() !== 'ok') throw new Error('归档入口 require 探针输出异常,拒绝确认');
|
|
154
|
+
const after = snapshotTree(stageDir);
|
|
155
|
+
if (JSON.stringify(before) !== JSON.stringify(after)) {
|
|
156
|
+
const diff = after.filter((x) => !before.some((y) => y[0] === x[0] && y[1] === x[1])).map((x) => x[0]);
|
|
157
|
+
throw new Error(`归档入口 require 产生文件副作用(隔离目录快照不一致:${diff.slice(0, 5).join(', ')}),拒绝确认`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** 隔离目录递归快照:[相对路径, 内容 sha256],按路径排序(快照对比用)。 */
|
|
162
|
+
function snapshotTree(root) {
|
|
163
|
+
const entries = [];
|
|
164
|
+
const walk = (dir, rel) => {
|
|
165
|
+
for (const name of fs.readdirSync(dir).sort()) {
|
|
166
|
+
const abs = path.join(dir, name);
|
|
167
|
+
const r = rel ? `${rel}/${name}` : name;
|
|
168
|
+
if (fs.statSync(abs).isDirectory()) walk(abs, r);
|
|
169
|
+
else entries.push([r, crypto.createHash('sha256').update(fs.readFileSync(abs)).digest('hex')]);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
walk(root, '');
|
|
173
|
+
return entries;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
function isolatedCheck(entry, payload) {
|
|
177
|
+
const caps = JSON.parse(execFileSync(process.execPath, [entry, 'capabilities'], { encoding: 'utf8', windowsHide: true, timeout: 15000 }));
|
|
178
|
+
if (caps.componentId !== payload.componentId) throw new Error(`componentId 不符:${caps.componentId}`);
|
|
179
|
+
if (caps.version !== payload.version) throw new Error(`版本不符:${caps.version} != ${payload.version}`);
|
|
180
|
+
if (caps.protocolVersion !== payload.protocolVersion) throw new Error(`protocolVersion 不符:${caps.protocolVersion}`);
|
|
181
|
+
checkCapsCompatibility(caps); // N05:能力字段全量验收(statusFormat/managerRange/nodeRange/stateSchemaVersion)
|
|
182
|
+
const health = JSON.parse(execFileSync(process.execPath, [entry, 'health'], { encoding: 'utf8', windowsHide: true, timeout: 15000 }));
|
|
183
|
+
if (health.status !== 'ok') throw new Error(`health=${health.status}`);
|
|
184
|
+
return caps;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function loadCurrent() {
|
|
188
|
+
const cur = readJson(paths.componentCurrent());
|
|
189
|
+
if (!cur || cur.schemaVersion !== 1 || typeof cur.versionId !== 'string') return null;
|
|
190
|
+
return cur;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function componentStatus() {
|
|
194
|
+
const cur = loadCurrent();
|
|
195
|
+
if (!cur) return { installed: false, version: null, versionId: null };
|
|
196
|
+
return { installed: true, version: cur.version, versionId: cur.versionId };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* 安装并激活。任何一步失败都不改 current 指针、不留半成品为当前版本。
|
|
201
|
+
* @returns {{versionId:string, archiveSha512:string}}
|
|
202
|
+
*/
|
|
203
|
+
function installComponent(tgz, payload) {
|
|
204
|
+
checkLocalCompatibility(payload); // F05:激活前本机兼容把关(全部安装入口共用)
|
|
205
|
+
const sha512b64 = crypto.createHash('sha512').update(tgz).digest('base64');
|
|
206
|
+
const versionId = `${payload.version}-${crypto.createHash('sha256').update(tgz).digest('hex').slice(0, 12)}`;
|
|
207
|
+
const versionsDir = paths.componentVersionsDir();
|
|
208
|
+
fs.mkdirSync(versionsDir, { recursive: true });
|
|
209
|
+
const finalDir = path.join(versionsDir, versionId);
|
|
210
|
+
if (loadCurrent() && loadCurrent().versionId === versionId) {
|
|
211
|
+
return { versionId, archiveSha512: sha512b64, alreadyActive: true };
|
|
212
|
+
}
|
|
213
|
+
const stage = fs.mkdtempSync(path.join(versionsDir, '.staging-'));
|
|
214
|
+
let caps;
|
|
215
|
+
try {
|
|
216
|
+
extractTgz(tgz, stage);
|
|
217
|
+
const entry = path.join(stage, ENTRY);
|
|
218
|
+
if (!fs.existsSync(entry)) throw new Error(`归档缺少组件入口:${ENTRY}`);
|
|
219
|
+
caps = isolatedCheck(entry, payload);
|
|
220
|
+
verifyDispatchExport(stage, entry); // N05:必需 dispatch 导出 + require 无副作用(隔离目录快照对比)
|
|
221
|
+
fs.mkdirSync(versionsDir, { recursive: true });
|
|
222
|
+
const keepFinal = fs.existsSync(finalDir);
|
|
223
|
+
if (keepFinal) fs.rmSync(finalDir, { recursive: true, force: true }); // 同 version-id 重装:内容标识一致才可能发生
|
|
224
|
+
fs.renameSync(stage, finalDir);
|
|
225
|
+
} finally {
|
|
226
|
+
try { fs.rmSync(stage, { recursive: true, force: true }); } catch { /* 已改名 */ }
|
|
227
|
+
}
|
|
228
|
+
atomicWrite(paths.componentCurrent(), {
|
|
229
|
+
schemaVersion: 1,
|
|
230
|
+
version: payload.version,
|
|
231
|
+
versionId,
|
|
232
|
+
archiveSha512: sha512b64,
|
|
233
|
+
entry: ENTRY,
|
|
234
|
+
status: 'confirmed', // R06:隔离自检通过才激活,激活即已确认;网关据此查禁 pending/无状态指针
|
|
235
|
+
protocolVersion: caps.protocolVersion, // R06:网关与模板 PROTOCOL_VERSION 一致才加载
|
|
236
|
+
managerRange: caps.managerRange, // R06:组件回报的兼容范围,供管理器侧升级判断
|
|
237
|
+
installedAt: new Date().toISOString(),
|
|
238
|
+
});
|
|
239
|
+
return { versionId, archiveSha512: sha512b64 };
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
module.exports = {
|
|
243
|
+
PROTOCOL_VERSION, checkLocalCompatibility, checkCapsCompatibility, parseRange,
|
|
244
|
+
installComponent, loadCurrent, componentStatus,
|
|
245
|
+
};
|