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/paths.js
CHANGED
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
/** 存储布局(设计 §11):根目录可用 RONDS_AI_HOME 覆盖,便于测试隔离。 */
|
|
3
|
-
const os = require('os');
|
|
4
|
-
const path = require('path');
|
|
5
|
-
|
|
6
|
-
function home() {
|
|
7
|
-
return process.env.RONDS_AI_HOME
|
|
8
|
-
? path.resolve(process.env.RONDS_AI_HOME)
|
|
9
|
-
: path.join(os.homedir(), '.ronds_ai');
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
module.exports = {
|
|
13
|
-
home,
|
|
14
|
-
managerDir: () => path.join(home(), 'manager'),
|
|
15
|
-
componentDir: () => path.join(home(), 'component'),
|
|
16
|
-
componentVersionsDir: () => path.join(home(), 'component', 'versions'),
|
|
17
|
-
componentCurrent: () => path.join(home(), 'component', 'current.json'),
|
|
18
|
-
configDir: () => path.join(home(), 'config'),
|
|
19
|
-
organizationJson: () => path.join(home(), 'config', 'organization.json'),
|
|
20
|
-
consentJson: () => path.join(home(), 'config', 'consent.json'),
|
|
21
|
-
stateDir: () => path.join(home(), 'state'),
|
|
22
|
-
componentUpdateJson: () => path.join(home(), 'state', 'component-update.json'),
|
|
23
|
-
disabledJson: () => path.join(home(), 'state', 'disabled.json'),
|
|
24
|
-
};
|
|
1
|
+
'use strict';
|
|
2
|
+
/** 存储布局(设计 §11):根目录可用 RONDS_AI_HOME 覆盖,便于测试隔离。 */
|
|
3
|
+
const os = require('os');
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
function home() {
|
|
7
|
+
return process.env.RONDS_AI_HOME
|
|
8
|
+
? path.resolve(process.env.RONDS_AI_HOME)
|
|
9
|
+
: path.join(os.homedir(), '.ronds_ai');
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
module.exports = {
|
|
13
|
+
home,
|
|
14
|
+
managerDir: () => path.join(home(), 'manager'),
|
|
15
|
+
componentDir: () => path.join(home(), 'component'),
|
|
16
|
+
componentVersionsDir: () => path.join(home(), 'component', 'versions'),
|
|
17
|
+
componentCurrent: () => path.join(home(), 'component', 'current.json'),
|
|
18
|
+
configDir: () => path.join(home(), 'config'),
|
|
19
|
+
organizationJson: () => path.join(home(), 'config', 'organization.json'),
|
|
20
|
+
consentJson: () => path.join(home(), 'config', 'consent.json'),
|
|
21
|
+
stateDir: () => path.join(home(), 'state'),
|
|
22
|
+
componentUpdateJson: () => path.join(home(), 'state', 'component-update.json'),
|
|
23
|
+
disabledJson: () => path.join(home(), 'state', 'disabled.json'),
|
|
24
|
+
};
|
package/lib/uninstall.js
CHANGED
|
@@ -1,239 +1,239 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
/**
|
|
3
|
-
* 卸载编排(设计 §5 uninstall 行 / §6 / 规范 §2):
|
|
4
|
-
* 1 disable 写持久停用标记(取消隐式恢复)
|
|
5
|
-
* 2 schedule-uninstall 组件已装时转发组件 schedule uninstall(直接 spawn 组件入口,
|
|
6
|
-
* 不走 route 转发:disable 已写、route 会拦截;参照 route.forward)
|
|
7
|
-
* 3 component-data 组件业务数据不删:只报告位置与体积,留给用户决定(§11.2)
|
|
8
|
-
* 4 hooks-uninstall 组件已装时 spawn 组件入口 hooks uninstall(user 与 project
|
|
9
|
-
* 两个 scope 各一次;改写逻辑已迁组件,参照 schedule 步直接
|
|
10
|
-
* spawn);组件未装则跳过并记 leftover 残留
|
|
11
|
-
* 5 remove-component 删除 component/versions 与 component/current.json
|
|
12
|
-
* 6 remove-entrypoints 删除网关/引导器(manager/dispatch.cjs、manager/launch.cjs)
|
|
13
|
-
*
|
|
14
|
-
* 保留:config/(含 consent 撤销记录)、state/(审计与停用标记)、data/(业务数据)、
|
|
15
|
-
* runtime/launch.cjs 兼容适配器(规范 §2:留停用适配器对残留旧 Hook fail-open)、
|
|
16
|
-
* manager/versions 与 manager/current.json(npm 包层生命周期管理)。
|
|
17
|
-
* 任何步骤失败:后续步骤不执行(不半删),整体失败并给出恢复入口说明。
|
|
18
|
-
*/
|
|
19
|
-
const fs = require('fs');
|
|
20
|
-
const path = require('path');
|
|
21
|
-
const { spawnSync } = require('child_process');
|
|
22
|
-
const paths = require('./paths');
|
|
23
|
-
const consent = require('./consent');
|
|
24
|
-
const route = require('./route');
|
|
25
|
-
|
|
26
|
-
function statOrNull(file) {
|
|
27
|
-
try { return fs.statSync(file); } catch { return null; }
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// 卸载后保留项(拒执行时也一并提供,供调用方预检展示)
|
|
31
|
-
const UNINSTALL_KEPT = {
|
|
32
|
-
'config/': 'consent 撤销记录与组织配置(审计)',
|
|
33
|
-
'state/': '审计与停用标记(disabled.json 保留,阻断隐式恢复)',
|
|
34
|
-
'data/': '组件业务数据(未批准不删,见 component-data 步骤报告)',
|
|
35
|
-
'runtime/launch.cjs': '旧路径兼容适配器:残留旧 Hook 调用时停用 fail-open(规范 §2)',
|
|
36
|
-
'manager/versions 与 manager/current.json': '管理器自身安装归 npm 包层生命周期管理',
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
function walkFiles(dirAbs) {
|
|
40
|
-
const out = [];
|
|
41
|
-
const visit = (dir) => {
|
|
42
|
-
let entries;
|
|
43
|
-
try { entries = fs.readdirSync(dir, { withFileTypes: true }); }
|
|
44
|
-
catch { return; }
|
|
45
|
-
for (const entry of entries) {
|
|
46
|
-
const abs = path.join(dir, entry.name);
|
|
47
|
-
if (entry.isDirectory()) visit(abs);
|
|
48
|
-
else if (entry.isFile()) {
|
|
49
|
-
const st = statOrNull(abs);
|
|
50
|
-
if (st) out.push({ bytes: st.size });
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
visit(dirAbs);
|
|
55
|
-
return out;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** 直接 spawn 组件入口执行 schedule uninstall(不走 route:disable 标记已写入)。 */
|
|
59
|
-
function spawnScheduleUninstall(entry) {
|
|
60
|
-
const res = spawnSync(process.execPath, [entry, 'schedule', 'uninstall'], {
|
|
61
|
-
stdio: ['ignore', 'pipe', 'pipe'],
|
|
62
|
-
encoding: 'utf8',
|
|
63
|
-
windowsHide: true,
|
|
64
|
-
timeout: 60000,
|
|
65
|
-
});
|
|
66
|
-
const snippet = (text) => String(text || '').trim().slice(0, 300);
|
|
67
|
-
if (res.error) throw new Error(`组件 schedule uninstall 启动失败:${res.error.message}`);
|
|
68
|
-
if (res.status !== 0) {
|
|
69
|
-
throw new Error(`组件 schedule uninstall 退出码 ${res.status}:${snippet(res.stderr) || snippet(res.stdout)}`
|
|
70
|
-
+ '(如组件因缺少 schedule 授权拒绝清理,可先 ronds_ai consent grant schedule 再重试 uninstall --confirm)');
|
|
71
|
-
}
|
|
72
|
-
return { exitCode: res.status, output: snippet(res.stdout) || null };
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
/** 直接 spawn 组件入口执行 hooks uninstall(机制同 schedule 步:不走 route,disable 标记已写入)。 */
|
|
76
|
-
function spawnHooksUninstall(entry, scope) {
|
|
77
|
-
const res = spawnSync(process.execPath, [entry, 'hooks', 'uninstall', '--scope', scope], {
|
|
78
|
-
stdio: ['ignore', 'pipe', 'pipe'],
|
|
79
|
-
encoding: 'utf8',
|
|
80
|
-
windowsHide: true,
|
|
81
|
-
timeout: 60000,
|
|
82
|
-
});
|
|
83
|
-
const snippet = (text) => String(text || '').trim().slice(0, 300);
|
|
84
|
-
if (res.error) throw new Error(`组件 hooks uninstall(${scope})启动失败:${res.error.message}`);
|
|
85
|
-
if (res.status !== 0) {
|
|
86
|
-
throw new Error(`组件 hooks uninstall(${scope})退出码 ${res.status}:${snippet(res.stderr) || snippet(res.stdout)}`);
|
|
87
|
-
}
|
|
88
|
-
return { exitCode: res.status, output: snippet(res.stdout) || null };
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 步骤 4:受管 Hook 卸载(改写逻辑已迁组件,spawn 组件入口执行)。
|
|
93
|
-
* 显式清扫两个 scope:hooks-state 只记最后一次安装的 scope,
|
|
94
|
-
* user+project 混装时单 scope 清理会漏。组件未装则跳过并记 leftover 残留
|
|
95
|
-
* (顺序保证:本步骤先于 remove-component,组件入口仍可用)。
|
|
96
|
-
*/
|
|
97
|
-
function runHooksUninstall() {
|
|
98
|
-
const entry = route.resolveComponentEntry();
|
|
99
|
-
if (!entry) {
|
|
100
|
-
return { skipped: true, leftover: true, note: '组件未安装或入口无效,跳过受管 Hook 卸载(残留 Hook 记为 leftover;重装组件后可补跑 ronds_ai hooks uninstall)' };
|
|
101
|
-
}
|
|
102
|
-
const results = {};
|
|
103
|
-
for (const scope of ['user', 'project']) {
|
|
104
|
-
results[scope] = spawnHooksUninstall(entry, scope);
|
|
105
|
-
}
|
|
106
|
-
return { skipped: false, result: results };
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* 执行卸载步骤序(内部实现)。返回 {ok, steps, kept, recovery}。
|
|
111
|
-
* 步骤失败立即中断:失败步骤之后的步骤不执行,避免半删。
|
|
112
|
-
*/
|
|
113
|
-
function executeUninstallSteps() {
|
|
114
|
-
const steps = [];
|
|
115
|
-
|
|
116
|
-
// 步骤 1:先写停用状态(§5:先停用,再清理;此后任何隐式恢复被阻断)
|
|
117
|
-
let stepOk = true;
|
|
118
|
-
try {
|
|
119
|
-
consent.setDisabled('uninstall');
|
|
120
|
-
steps.push({ step: 'disable', ok: true, detail: { disabled: true, reason: 'uninstall', file: paths.disabledJson() } });
|
|
121
|
-
} catch (e) {
|
|
122
|
-
steps.push({ step: 'disable', ok: false, detail: e && e.message ? e.message : String(e) });
|
|
123
|
-
stepOk = false;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// 步骤 2:组件任务清理(schedule uninstall,组件内实现系统调度器操作)
|
|
127
|
-
if (stepOk) {
|
|
128
|
-
const entry = route.resolveComponentEntry();
|
|
129
|
-
if (!entry) {
|
|
130
|
-
steps.push({ step: 'schedule-uninstall', ok: true, detail: { skipped: true, note: '组件未安装或入口无效,跳过任务清理' } });
|
|
131
|
-
} else {
|
|
132
|
-
try {
|
|
133
|
-
steps.push({ step: 'schedule-uninstall', ok: true, detail: { entryVersionDir: path.dirname(path.dirname(path.dirname(entry))), ...spawnScheduleUninstall(entry) } });
|
|
134
|
-
} catch (e) {
|
|
135
|
-
steps.push({ step: 'schedule-uninstall', ok: false, detail: e && e.message ? e.message : String(e) });
|
|
136
|
-
stepOk = false;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// 步骤 3:业务数据只报告不删(§11.2:未批准不删)
|
|
142
|
-
if (stepOk) {
|
|
143
|
-
const dataDir = path.join(paths.home(), 'data');
|
|
144
|
-
const files = walkFiles(dataDir);
|
|
145
|
-
steps.push({
|
|
146
|
-
step: 'component-data',
|
|
147
|
-
ok: true,
|
|
148
|
-
detail: {
|
|
149
|
-
kept: true,
|
|
150
|
-
path: dataDir,
|
|
151
|
-
files: files.length,
|
|
152
|
-
bytes: files.reduce((n, f) => n + f.bytes, 0),
|
|
153
|
-
note: '组件业务数据不随 uninstall 删除,是否清除由用户决定(§11.2 未批准不删)',
|
|
154
|
-
},
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
// 步骤 4:受管 Hook 卸载(hooks 模块未就绪则跳过)
|
|
159
|
-
if (stepOk) {
|
|
160
|
-
try {
|
|
161
|
-
steps.push({ step: 'hooks-uninstall', ok: true, detail: runHooksUninstall() });
|
|
162
|
-
} catch (e) {
|
|
163
|
-
steps.push({ step: 'hooks-uninstall', ok: false, detail: e && e.message ? e.message : String(e) });
|
|
164
|
-
stepOk = false;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// 步骤 5:删除组件安装(versions 目录 + current 指针)
|
|
169
|
-
if (stepOk) {
|
|
170
|
-
try {
|
|
171
|
-
const removed = [];
|
|
172
|
-
const versionsDir = paths.componentVersionsDir();
|
|
173
|
-
if (statOrNull(versionsDir)) {
|
|
174
|
-
fs.rmSync(versionsDir, { recursive: true, force: true });
|
|
175
|
-
removed.push('component/versions');
|
|
176
|
-
}
|
|
177
|
-
if (statOrNull(paths.componentCurrent())) {
|
|
178
|
-
fs.rmSync(paths.componentCurrent(), { force: true });
|
|
179
|
-
removed.push('component/current.json');
|
|
180
|
-
}
|
|
181
|
-
try { fs.rmdirSync(paths.componentDir()); } catch { /* 非空或不存在:保留目录 */ }
|
|
182
|
-
steps.push({ step: 'remove-component', ok: true, detail: { removed: removed.length ? removed : ['(无残留可删)'] } });
|
|
183
|
-
} catch (e) {
|
|
184
|
-
steps.push({ step: 'remove-component', ok: false, detail: e && e.message ? e.message : String(e) });
|
|
185
|
-
stepOk = false;
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
// 步骤 6:删除网关与引导器(稳定入口不再被任务/Hook 引用后才删)
|
|
190
|
-
if (stepOk) {
|
|
191
|
-
try {
|
|
192
|
-
const removed = [];
|
|
193
|
-
for (const rel of ['dispatch.cjs', 'launch.cjs']) {
|
|
194
|
-
const file = path.join(paths.managerDir(), rel);
|
|
195
|
-
if (statOrNull(file)) {
|
|
196
|
-
fs.rmSync(file, { force: true });
|
|
197
|
-
removed.push(`manager/${rel}`);
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
steps.push({ step: 'remove-entrypoints', ok: true, detail: { removed: removed.length ? removed : ['(无残留可删)'] } });
|
|
201
|
-
} catch (e) {
|
|
202
|
-
steps.push({ step: 'remove-entrypoints', ok: false, detail: e && e.message ? e.message : String(e) });
|
|
203
|
-
stepOk = false;
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
const failed = steps.find((s) => !s.ok) || null;
|
|
208
|
-
return {
|
|
209
|
-
ok: !failed,
|
|
210
|
-
steps,
|
|
211
|
-
kept: UNINSTALL_KEPT,
|
|
212
|
-
recovery: failed
|
|
213
|
-
? `uninstall 在步骤 ${failed.step} 失败,其后步骤未执行(不半删)。处理失败原因后重新运行 ronds_ai uninstall --confirm;期间系统保持 disabled,config/、state/、data/ 与未执行的删除目标均未删除。`
|
|
214
|
-
: null,
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
module.exports = { runUninstall };
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* 卸载入口(供 bin 接线直接调用):runUninstall({confirm}) →
|
|
222
|
-
* {ok, exitCode, steps:[{step, ok, detail}], kept, recovery, message}。
|
|
223
|
-
* confirm !== true:拒绝执行(exitCode 2,不做任何改动);
|
|
224
|
-
* 步骤全部成功 exitCode 0;任一步骤失败 exitCode 1 且 recovery 给恢复入口说明。
|
|
225
|
-
*/
|
|
226
|
-
function runUninstall(options = {}) {
|
|
227
|
-
if (!options || options.confirm !== true) {
|
|
228
|
-
return {
|
|
229
|
-
ok: false,
|
|
230
|
-
exitCode: 2,
|
|
231
|
-
steps: [],
|
|
232
|
-
kept: UNINSTALL_KEPT,
|
|
233
|
-
recovery: null,
|
|
234
|
-
message: 'uninstall 拒绝执行:需要 --confirm(会停用、清理任务与受管 Hook、删除组件安装;config/state/data 保留)',
|
|
235
|
-
};
|
|
236
|
-
}
|
|
237
|
-
const result = executeUninstallSteps();
|
|
238
|
-
return { ...result, exitCode: result.ok ? 0 : 1 };
|
|
239
|
-
}
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* 卸载编排(设计 §5 uninstall 行 / §6 / 规范 §2):
|
|
4
|
+
* 1 disable 写持久停用标记(取消隐式恢复)
|
|
5
|
+
* 2 schedule-uninstall 组件已装时转发组件 schedule uninstall(直接 spawn 组件入口,
|
|
6
|
+
* 不走 route 转发:disable 已写、route 会拦截;参照 route.forward)
|
|
7
|
+
* 3 component-data 组件业务数据不删:只报告位置与体积,留给用户决定(§11.2)
|
|
8
|
+
* 4 hooks-uninstall 组件已装时 spawn 组件入口 hooks uninstall(user 与 project
|
|
9
|
+
* 两个 scope 各一次;改写逻辑已迁组件,参照 schedule 步直接
|
|
10
|
+
* spawn);组件未装则跳过并记 leftover 残留
|
|
11
|
+
* 5 remove-component 删除 component/versions 与 component/current.json
|
|
12
|
+
* 6 remove-entrypoints 删除网关/引导器(manager/dispatch.cjs、manager/launch.cjs)
|
|
13
|
+
*
|
|
14
|
+
* 保留:config/(含 consent 撤销记录)、state/(审计与停用标记)、data/(业务数据)、
|
|
15
|
+
* runtime/launch.cjs 兼容适配器(规范 §2:留停用适配器对残留旧 Hook fail-open)、
|
|
16
|
+
* manager/versions 与 manager/current.json(npm 包层生命周期管理)。
|
|
17
|
+
* 任何步骤失败:后续步骤不执行(不半删),整体失败并给出恢复入口说明。
|
|
18
|
+
*/
|
|
19
|
+
const fs = require('fs');
|
|
20
|
+
const path = require('path');
|
|
21
|
+
const { spawnSync } = require('child_process');
|
|
22
|
+
const paths = require('./paths');
|
|
23
|
+
const consent = require('./consent');
|
|
24
|
+
const route = require('./route');
|
|
25
|
+
|
|
26
|
+
function statOrNull(file) {
|
|
27
|
+
try { return fs.statSync(file); } catch { return null; }
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 卸载后保留项(拒执行时也一并提供,供调用方预检展示)
|
|
31
|
+
const UNINSTALL_KEPT = {
|
|
32
|
+
'config/': 'consent 撤销记录与组织配置(审计)',
|
|
33
|
+
'state/': '审计与停用标记(disabled.json 保留,阻断隐式恢复)',
|
|
34
|
+
'data/': '组件业务数据(未批准不删,见 component-data 步骤报告)',
|
|
35
|
+
'runtime/launch.cjs': '旧路径兼容适配器:残留旧 Hook 调用时停用 fail-open(规范 §2)',
|
|
36
|
+
'manager/versions 与 manager/current.json': '管理器自身安装归 npm 包层生命周期管理',
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function walkFiles(dirAbs) {
|
|
40
|
+
const out = [];
|
|
41
|
+
const visit = (dir) => {
|
|
42
|
+
let entries;
|
|
43
|
+
try { entries = fs.readdirSync(dir, { withFileTypes: true }); }
|
|
44
|
+
catch { return; }
|
|
45
|
+
for (const entry of entries) {
|
|
46
|
+
const abs = path.join(dir, entry.name);
|
|
47
|
+
if (entry.isDirectory()) visit(abs);
|
|
48
|
+
else if (entry.isFile()) {
|
|
49
|
+
const st = statOrNull(abs);
|
|
50
|
+
if (st) out.push({ bytes: st.size });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
visit(dirAbs);
|
|
55
|
+
return out;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 直接 spawn 组件入口执行 schedule uninstall(不走 route:disable 标记已写入)。 */
|
|
59
|
+
function spawnScheduleUninstall(entry) {
|
|
60
|
+
const res = spawnSync(process.execPath, [entry, 'schedule', 'uninstall'], {
|
|
61
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
62
|
+
encoding: 'utf8',
|
|
63
|
+
windowsHide: true,
|
|
64
|
+
timeout: 60000,
|
|
65
|
+
});
|
|
66
|
+
const snippet = (text) => String(text || '').trim().slice(0, 300);
|
|
67
|
+
if (res.error) throw new Error(`组件 schedule uninstall 启动失败:${res.error.message}`);
|
|
68
|
+
if (res.status !== 0) {
|
|
69
|
+
throw new Error(`组件 schedule uninstall 退出码 ${res.status}:${snippet(res.stderr) || snippet(res.stdout)}`
|
|
70
|
+
+ '(如组件因缺少 schedule 授权拒绝清理,可先 ronds_ai consent grant schedule 再重试 uninstall --confirm)');
|
|
71
|
+
}
|
|
72
|
+
return { exitCode: res.status, output: snippet(res.stdout) || null };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 直接 spawn 组件入口执行 hooks uninstall(机制同 schedule 步:不走 route,disable 标记已写入)。 */
|
|
76
|
+
function spawnHooksUninstall(entry, scope) {
|
|
77
|
+
const res = spawnSync(process.execPath, [entry, 'hooks', 'uninstall', '--scope', scope], {
|
|
78
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
79
|
+
encoding: 'utf8',
|
|
80
|
+
windowsHide: true,
|
|
81
|
+
timeout: 60000,
|
|
82
|
+
});
|
|
83
|
+
const snippet = (text) => String(text || '').trim().slice(0, 300);
|
|
84
|
+
if (res.error) throw new Error(`组件 hooks uninstall(${scope})启动失败:${res.error.message}`);
|
|
85
|
+
if (res.status !== 0) {
|
|
86
|
+
throw new Error(`组件 hooks uninstall(${scope})退出码 ${res.status}:${snippet(res.stderr) || snippet(res.stdout)}`);
|
|
87
|
+
}
|
|
88
|
+
return { exitCode: res.status, output: snippet(res.stdout) || null };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 步骤 4:受管 Hook 卸载(改写逻辑已迁组件,spawn 组件入口执行)。
|
|
93
|
+
* 显式清扫两个 scope:hooks-state 只记最后一次安装的 scope,
|
|
94
|
+
* user+project 混装时单 scope 清理会漏。组件未装则跳过并记 leftover 残留
|
|
95
|
+
* (顺序保证:本步骤先于 remove-component,组件入口仍可用)。
|
|
96
|
+
*/
|
|
97
|
+
function runHooksUninstall() {
|
|
98
|
+
const entry = route.resolveComponentEntry();
|
|
99
|
+
if (!entry) {
|
|
100
|
+
return { skipped: true, leftover: true, note: '组件未安装或入口无效,跳过受管 Hook 卸载(残留 Hook 记为 leftover;重装组件后可补跑 ronds_ai hooks uninstall)' };
|
|
101
|
+
}
|
|
102
|
+
const results = {};
|
|
103
|
+
for (const scope of ['user', 'project']) {
|
|
104
|
+
results[scope] = spawnHooksUninstall(entry, scope);
|
|
105
|
+
}
|
|
106
|
+
return { skipped: false, result: results };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 执行卸载步骤序(内部实现)。返回 {ok, steps, kept, recovery}。
|
|
111
|
+
* 步骤失败立即中断:失败步骤之后的步骤不执行,避免半删。
|
|
112
|
+
*/
|
|
113
|
+
function executeUninstallSteps() {
|
|
114
|
+
const steps = [];
|
|
115
|
+
|
|
116
|
+
// 步骤 1:先写停用状态(§5:先停用,再清理;此后任何隐式恢复被阻断)
|
|
117
|
+
let stepOk = true;
|
|
118
|
+
try {
|
|
119
|
+
consent.setDisabled('uninstall');
|
|
120
|
+
steps.push({ step: 'disable', ok: true, detail: { disabled: true, reason: 'uninstall', file: paths.disabledJson() } });
|
|
121
|
+
} catch (e) {
|
|
122
|
+
steps.push({ step: 'disable', ok: false, detail: e && e.message ? e.message : String(e) });
|
|
123
|
+
stepOk = false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// 步骤 2:组件任务清理(schedule uninstall,组件内实现系统调度器操作)
|
|
127
|
+
if (stepOk) {
|
|
128
|
+
const entry = route.resolveComponentEntry();
|
|
129
|
+
if (!entry) {
|
|
130
|
+
steps.push({ step: 'schedule-uninstall', ok: true, detail: { skipped: true, note: '组件未安装或入口无效,跳过任务清理' } });
|
|
131
|
+
} else {
|
|
132
|
+
try {
|
|
133
|
+
steps.push({ step: 'schedule-uninstall', ok: true, detail: { entryVersionDir: path.dirname(path.dirname(path.dirname(entry))), ...spawnScheduleUninstall(entry) } });
|
|
134
|
+
} catch (e) {
|
|
135
|
+
steps.push({ step: 'schedule-uninstall', ok: false, detail: e && e.message ? e.message : String(e) });
|
|
136
|
+
stepOk = false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// 步骤 3:业务数据只报告不删(§11.2:未批准不删)
|
|
142
|
+
if (stepOk) {
|
|
143
|
+
const dataDir = path.join(paths.home(), 'data');
|
|
144
|
+
const files = walkFiles(dataDir);
|
|
145
|
+
steps.push({
|
|
146
|
+
step: 'component-data',
|
|
147
|
+
ok: true,
|
|
148
|
+
detail: {
|
|
149
|
+
kept: true,
|
|
150
|
+
path: dataDir,
|
|
151
|
+
files: files.length,
|
|
152
|
+
bytes: files.reduce((n, f) => n + f.bytes, 0),
|
|
153
|
+
note: '组件业务数据不随 uninstall 删除,是否清除由用户决定(§11.2 未批准不删)',
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 步骤 4:受管 Hook 卸载(hooks 模块未就绪则跳过)
|
|
159
|
+
if (stepOk) {
|
|
160
|
+
try {
|
|
161
|
+
steps.push({ step: 'hooks-uninstall', ok: true, detail: runHooksUninstall() });
|
|
162
|
+
} catch (e) {
|
|
163
|
+
steps.push({ step: 'hooks-uninstall', ok: false, detail: e && e.message ? e.message : String(e) });
|
|
164
|
+
stepOk = false;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// 步骤 5:删除组件安装(versions 目录 + current 指针)
|
|
169
|
+
if (stepOk) {
|
|
170
|
+
try {
|
|
171
|
+
const removed = [];
|
|
172
|
+
const versionsDir = paths.componentVersionsDir();
|
|
173
|
+
if (statOrNull(versionsDir)) {
|
|
174
|
+
fs.rmSync(versionsDir, { recursive: true, force: true });
|
|
175
|
+
removed.push('component/versions');
|
|
176
|
+
}
|
|
177
|
+
if (statOrNull(paths.componentCurrent())) {
|
|
178
|
+
fs.rmSync(paths.componentCurrent(), { force: true });
|
|
179
|
+
removed.push('component/current.json');
|
|
180
|
+
}
|
|
181
|
+
try { fs.rmdirSync(paths.componentDir()); } catch { /* 非空或不存在:保留目录 */ }
|
|
182
|
+
steps.push({ step: 'remove-component', ok: true, detail: { removed: removed.length ? removed : ['(无残留可删)'] } });
|
|
183
|
+
} catch (e) {
|
|
184
|
+
steps.push({ step: 'remove-component', ok: false, detail: e && e.message ? e.message : String(e) });
|
|
185
|
+
stepOk = false;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// 步骤 6:删除网关与引导器(稳定入口不再被任务/Hook 引用后才删)
|
|
190
|
+
if (stepOk) {
|
|
191
|
+
try {
|
|
192
|
+
const removed = [];
|
|
193
|
+
for (const rel of ['dispatch.cjs', 'launch.cjs']) {
|
|
194
|
+
const file = path.join(paths.managerDir(), rel);
|
|
195
|
+
if (statOrNull(file)) {
|
|
196
|
+
fs.rmSync(file, { force: true });
|
|
197
|
+
removed.push(`manager/${rel}`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
steps.push({ step: 'remove-entrypoints', ok: true, detail: { removed: removed.length ? removed : ['(无残留可删)'] } });
|
|
201
|
+
} catch (e) {
|
|
202
|
+
steps.push({ step: 'remove-entrypoints', ok: false, detail: e && e.message ? e.message : String(e) });
|
|
203
|
+
stepOk = false;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const failed = steps.find((s) => !s.ok) || null;
|
|
208
|
+
return {
|
|
209
|
+
ok: !failed,
|
|
210
|
+
steps,
|
|
211
|
+
kept: UNINSTALL_KEPT,
|
|
212
|
+
recovery: failed
|
|
213
|
+
? `uninstall 在步骤 ${failed.step} 失败,其后步骤未执行(不半删)。处理失败原因后重新运行 ronds_ai uninstall --confirm;期间系统保持 disabled,config/、state/、data/ 与未执行的删除目标均未删除。`
|
|
214
|
+
: null,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
module.exports = { runUninstall };
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 卸载入口(供 bin 接线直接调用):runUninstall({confirm}) →
|
|
222
|
+
* {ok, exitCode, steps:[{step, ok, detail}], kept, recovery, message}。
|
|
223
|
+
* confirm !== true:拒绝执行(exitCode 2,不做任何改动);
|
|
224
|
+
* 步骤全部成功 exitCode 0;任一步骤失败 exitCode 1 且 recovery 给恢复入口说明。
|
|
225
|
+
*/
|
|
226
|
+
function runUninstall(options = {}) {
|
|
227
|
+
if (!options || options.confirm !== true) {
|
|
228
|
+
return {
|
|
229
|
+
ok: false,
|
|
230
|
+
exitCode: 2,
|
|
231
|
+
steps: [],
|
|
232
|
+
kept: UNINSTALL_KEPT,
|
|
233
|
+
recovery: null,
|
|
234
|
+
message: 'uninstall 拒绝执行:需要 --confirm(会停用、清理任务与受管 Hook、删除组件安装;config/state/data 保留)',
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
const result = executeUninstallSteps();
|
|
238
|
+
return { ...result, exitCode: result.ok ? 0 : 1 };
|
|
239
|
+
}
|