dsh-plugin-snapshot 0.3.6 → 0.3.9
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/README.en.md +3 -0
- package/README.md +3 -0
- package/lib/allow-builds.js +163 -0
- package/lib/auto-update.js +410 -146
- package/lib/doctor.js +98 -4
- package/lib/dsh-version.js +127 -0
- package/lib/exec.js +39 -0
- package/lib/health-gate.js +192 -0
- package/lib/index.js +49 -7
- package/lib/policy.js +245 -0
- package/lib/routes.js +25 -4
- package/lib/updates.js +97 -19
- package/package.json +6 -11
package/lib/policy.js
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 更新策略:安全等级、逐包风险分级、更新计划、预检。
|
|
3
|
+
*
|
|
4
|
+
* 移植自桌面版 v3.3.5 的 engine/policy.mjs,语义保持一致:
|
|
5
|
+
* 「引擎会装什么」只有 buildPlan() 一处定义,界面、工具、自动更新三条路径
|
|
6
|
+
* 都调用它,避免出现「面板说 0 个、实际装了 5 个」。
|
|
7
|
+
*
|
|
8
|
+
* 三档安全等级(默认 conservative):
|
|
9
|
+
* conservative 同主版本、非预发布、非原生;主版本只提示
|
|
10
|
+
* standard 同主版本全装;主版本进 deferred 待确认
|
|
11
|
+
* aggressive 主版本也装;仍需预检与健康校验
|
|
12
|
+
* 任何一档都 blocked 的:目标明确不兼容、核心 bundle、包名非法、patch id 冲突、查询失败。
|
|
13
|
+
*/
|
|
14
|
+
import { isValidPkgName } from './util.js';
|
|
15
|
+
|
|
16
|
+
export const SAFETY_LEVELS = ['conservative', 'standard', 'aggressive'];
|
|
17
|
+
|
|
18
|
+
export const LEVEL_PRESETS = {
|
|
19
|
+
conservative: {
|
|
20
|
+
label: '保守',
|
|
21
|
+
desc: '只装同主版本、非预发布、无原生模块的插件;主版本只提示不执行。异常立即回滚。',
|
|
22
|
+
allowMajor: false,
|
|
23
|
+
allowPrerelease: false,
|
|
24
|
+
allowNative: false,
|
|
25
|
+
requireHealthGate: true,
|
|
26
|
+
autoRollback: true,
|
|
27
|
+
stopOnFirstBatchFailure: true,
|
|
28
|
+
minFreeBytes: 1024 * 1024 * 1024
|
|
29
|
+
},
|
|
30
|
+
standard: {
|
|
31
|
+
label: '标准',
|
|
32
|
+
desc: '同主版本全部安装,主版本进「待确认」由用户勾选;仍做快照、健康校验与自动回滚。',
|
|
33
|
+
allowMajor: false,
|
|
34
|
+
allowPrerelease: false,
|
|
35
|
+
allowNative: true,
|
|
36
|
+
requireHealthGate: true,
|
|
37
|
+
autoRollback: true,
|
|
38
|
+
stopOnFirstBatchFailure: false,
|
|
39
|
+
minFreeBytes: 800 * 1024 * 1024
|
|
40
|
+
},
|
|
41
|
+
aggressive: {
|
|
42
|
+
label: '激进',
|
|
43
|
+
desc: '主版本也自动装。成功率优先,仍保留快照与健康校验;仅在明确不兼容或冲突时拦截。',
|
|
44
|
+
allowMajor: true,
|
|
45
|
+
allowPrerelease: false,
|
|
46
|
+
allowNative: true,
|
|
47
|
+
requireHealthGate: true,
|
|
48
|
+
autoRollback: true,
|
|
49
|
+
stopOnFirstBatchFailure: false,
|
|
50
|
+
minFreeBytes: 500 * 1024 * 1024
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/** 核心 bundle:停用或升级失败都会让 DSH 起不来,永远不进自动计划 */
|
|
55
|
+
const CORE_BUNDLE = /^@deepseek-ai\/dsh($|[-/])/;
|
|
56
|
+
export function isCoreBundle(name) { return CORE_BUNDLE.test(String(name || '')); }
|
|
57
|
+
|
|
58
|
+
/** 解析生效策略:显式 safetyLevel 优先,其次按旧布尔开关推断,最后回落保守 */
|
|
59
|
+
export function resolvePolicy(cfg) {
|
|
60
|
+
const c = cfg || {};
|
|
61
|
+
let level = SAFETY_LEVELS.indexOf(String(c.safetyLevel || '')) >= 0 ? String(c.safetyLevel) : '';
|
|
62
|
+
if (!level) level = c.allowMajorUpdates === true ? 'standard' : 'conservative';
|
|
63
|
+
const p = Object.assign({}, LEVEL_PRESETS[level], { level });
|
|
64
|
+
if (typeof c.allowMajorUpdates === 'boolean') p.allowMajor = c.allowMajorUpdates;
|
|
65
|
+
if (typeof c.healthGate === 'boolean') p.requireHealthGate = c.healthGate;
|
|
66
|
+
if (typeof c.autoRollbackOnUnhealthy === 'boolean') p.autoRollback = c.autoRollbackOnUnhealthy;
|
|
67
|
+
if (Number.isFinite(Number(c.minFreeBytes))) p.minFreeBytes = Number(c.minFreeBytes);
|
|
68
|
+
return p;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 逐包风险分级。
|
|
73
|
+
* @returns {{ risk: 'low'|'medium'|'high'|'blocked', reasons: string[] }}
|
|
74
|
+
*/
|
|
75
|
+
export function classifyItem(item, ctx) {
|
|
76
|
+
const c = ctx || {};
|
|
77
|
+
const policy = c.policy || LEVEL_PRESETS.conservative;
|
|
78
|
+
const reasons = [];
|
|
79
|
+
let risk = 'low';
|
|
80
|
+
const rank = { low: 0, medium: 1, high: 2, blocked: 3 };
|
|
81
|
+
const bump = (lvl) => { if (rank[lvl] > rank[risk]) risk = lvl; };
|
|
82
|
+
|
|
83
|
+
const name = String(item.name || '');
|
|
84
|
+
const state = String(item.state || '');
|
|
85
|
+
if (!isValidPkgName(name)) { reasons.push('包名不合法'); bump('blocked'); return { risk, reasons }; }
|
|
86
|
+
if (isCoreBundle(name)) { reasons.push('DSH 核心 bundle,不参与自动升级'); bump('blocked'); }
|
|
87
|
+
|
|
88
|
+
if (state === 'prerelease' && !policy.allowPrerelease) { reasons.push('目标版本是预发布版'); bump('blocked'); }
|
|
89
|
+
if (state === 'major') {
|
|
90
|
+
/* 主版本不是 blocked:「不能装」和「要你点头」是两件事,由 buildPlan 决定进 deferred */
|
|
91
|
+
reasons.push('跨主版本');
|
|
92
|
+
bump('high');
|
|
93
|
+
}
|
|
94
|
+
if (item.hasNative) {
|
|
95
|
+
reasons.push('含原生模块(ABI 不匹配会直接崩溃)');
|
|
96
|
+
bump(policy.allowNative ? 'high' : 'blocked');
|
|
97
|
+
}
|
|
98
|
+
const compat = (item.compatibility || {}).state;
|
|
99
|
+
if (compat === 'incompatible') { reasons.push('目标版本明确不兼容当前 DSH'); bump('blocked'); }
|
|
100
|
+
else if (compat === 'untested') { reasons.push('目标版本未声明支持当前 DSH'); bump('medium'); }
|
|
101
|
+
|
|
102
|
+
const conflictIds = c.patchIds || {};
|
|
103
|
+
for (const id of (item.patchIds || [])) {
|
|
104
|
+
const owners = (conflictIds[id] || []).filter((x) => x !== name);
|
|
105
|
+
if (owners.length) { reasons.push('patch id ' + id + ' 与 ' + owners.slice(0, 2).join('、') + ' 冲突'); bump('blocked'); }
|
|
106
|
+
}
|
|
107
|
+
if (c.crashy && c.crashy.indexOf(name) >= 0) { reasons.push('启动日志里有该插件的失败记录'); bump('high'); }
|
|
108
|
+
if (item.kind === 'github') { reasons.push('GitHub 直连源,无法校验发布完整性'); bump('medium'); }
|
|
109
|
+
if (state === 'failed') { reasons.push('版本查询失败'); bump('blocked'); }
|
|
110
|
+
if (state === 'downgrade') { reasons.push('候选版本低于当前安装版本'); bump('blocked'); }
|
|
111
|
+
|
|
112
|
+
return { risk, reasons };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 生成更新计划。
|
|
117
|
+
* @returns {{ targets, blocked, deferred, groups, counts, policy, level }}
|
|
118
|
+
*/
|
|
119
|
+
export function buildPlan(items, cfg, ctx) {
|
|
120
|
+
const o = ctx || {};
|
|
121
|
+
const policy = resolvePolicy(cfg);
|
|
122
|
+
const frozen = new Set((cfg && cfg.frozenPackages) || []);
|
|
123
|
+
const want = o.names && o.names.length ? new Set(o.names) : null;
|
|
124
|
+
const forced = new Set(o.forced || []);
|
|
125
|
+
|
|
126
|
+
const targets = [];
|
|
127
|
+
const blocked = [];
|
|
128
|
+
const deferred = [];
|
|
129
|
+
|
|
130
|
+
for (const item of items || []) {
|
|
131
|
+
const st = String(item.state || '');
|
|
132
|
+
if (st !== 'updatable' && st !== 'major') continue;
|
|
133
|
+
if (frozen.has(item.name)) continue;
|
|
134
|
+
|
|
135
|
+
const cls = classifyItem(item, Object.assign({}, o, { policy }));
|
|
136
|
+
const row = Object.assign({}, item, { risk: cls.risk, riskReasons: cls.reasons });
|
|
137
|
+
|
|
138
|
+
if (cls.risk === 'blocked') {
|
|
139
|
+
const hard = cls.reasons.some((r) => /核心 bundle|明确不兼容|冲突|不合法|查询失败|低于当前/.test(r));
|
|
140
|
+
if (want && want.has(item.name) && forced.has(item.name) && !hard) targets.push(row);
|
|
141
|
+
else blocked.push(row);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (st === 'major' && !policy.allowMajor) {
|
|
145
|
+
if (want && want.has(item.name)) targets.push(Object.assign({}, row, { confirmed: true }));
|
|
146
|
+
else deferred.push(row);
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
if (want && !want.has(item.name) && !forced.has(item.name)) continue;
|
|
150
|
+
targets.push(row);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const rk = { low: 0, medium: 1, high: 2 };
|
|
154
|
+
targets.sort((a, b) => (rk[a.risk] == null ? 1 : rk[a.risk]) - (rk[b.risk] == null ? 1 : rk[b.risk]));
|
|
155
|
+
|
|
156
|
+
/* 分组:低/中风险一次批量装(一次解析一次写盘),高风险逐个装(失败面最小) */
|
|
157
|
+
const groups = [];
|
|
158
|
+
const lowMid = targets.filter((t) => t.risk !== 'high');
|
|
159
|
+
const high = targets.filter((t) => t.risk === 'high');
|
|
160
|
+
if (lowMid.length) groups.push({ risk: 'batch', items: lowMid, batch: true });
|
|
161
|
+
for (const t of high) groups.push({ risk: 'high', items: [t], batch: false });
|
|
162
|
+
|
|
163
|
+
const counts = {
|
|
164
|
+
total: targets.length,
|
|
165
|
+
low: targets.filter((t) => t.risk === 'low').length,
|
|
166
|
+
medium: targets.filter((t) => t.risk === 'medium').length,
|
|
167
|
+
high: high.length,
|
|
168
|
+
blocked: blocked.length,
|
|
169
|
+
deferred: deferred.length
|
|
170
|
+
};
|
|
171
|
+
return { targets, blocked, deferred, groups, counts, policy, level: policy.level };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function chk(key, label, status, detail) { return { key, label, status, detail: detail || '' }; }
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 更新前预检:把「必然失败」和「会把 DSH 弄坏」的情况挡在写盘之前。
|
|
178
|
+
* @param deps { profilePkg, runtime, probeLocked, isRunning }
|
|
179
|
+
*/
|
|
180
|
+
export async function preflight(cfg, deps) {
|
|
181
|
+
const d = deps || {};
|
|
182
|
+
const policy = resolvePolicy(cfg);
|
|
183
|
+
const checks = [];
|
|
184
|
+
let pkg = null;
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
pkg = await d.profilePkg();
|
|
188
|
+
const depsObj = pkg && pkg.dependencies;
|
|
189
|
+
const okShape = pkg && typeof pkg === 'object' && depsObj && typeof depsObj === 'object' && !Array.isArray(depsObj);
|
|
190
|
+
checks.push(chk('profile', 'profile 清单', okShape ? 'ok' : 'blocker',
|
|
191
|
+
okShape ? Object.keys(depsObj).length + ' 个依赖' : 'package.json 结构异常(dependencies 必须是对象)'));
|
|
192
|
+
const bundles = pkg && pkg.dsh && pkg.dsh.profile && pkg.dsh.profile.bundles;
|
|
193
|
+
if (bundles !== undefined && !Array.isArray(bundles)) {
|
|
194
|
+
checks.push(chk('bundles', '启用清单', 'blocker', 'dsh.profile.bundles 不是数组,DSH 很可能已经起不来'));
|
|
195
|
+
}
|
|
196
|
+
} catch (e) {
|
|
197
|
+
checks.push(chk('profile', 'profile 清单', 'blocker', String((e && e.message) || e)));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const runtime = d.runtime || null;
|
|
201
|
+
const hasCustom = String((cfg && cfg.pnpmCommand) || '').trim();
|
|
202
|
+
const pnpmOk = !!(runtime || (hasCustom && Array.isArray(cfg.pnpmArgs) && cfg.pnpmArgs.length));
|
|
203
|
+
checks.push(chk('pnpm', 'pnpm 运行时', pnpmOk ? 'ok' : 'blocker',
|
|
204
|
+
pnpmOk ? (runtime ? String(runtime.root || runtime) : '使用自定义 pnpmCommand') : '未发现运行时,且未配置 pnpmCommand/pnpmArgs'));
|
|
205
|
+
|
|
206
|
+
try {
|
|
207
|
+
const { statfs } = await import('node:fs/promises');
|
|
208
|
+
const st = await statfs(String((cfg && (cfg.profileDir || cfg.dataDir)) || '.'));
|
|
209
|
+
const free = Number(st.bavail) * Number(st.bsize);
|
|
210
|
+
const gb = free / 1024 / 1024 / 1024;
|
|
211
|
+
if (free < policy.minFreeBytes) {
|
|
212
|
+
checks.push(chk('disk', '磁盘空间', 'blocker', '可用 ' + gb.toFixed(2) + ' GB,低于本策略下限 ' + (policy.minFreeBytes / 1073741824).toFixed(1) + ' GB'));
|
|
213
|
+
} else if (free < 2 * 1024 * 1024 * 1024) {
|
|
214
|
+
checks.push(chk('disk', '磁盘空间', 'warning', '可用 ' + gb.toFixed(2) + ' GB,偏紧'));
|
|
215
|
+
} else {
|
|
216
|
+
checks.push(chk('disk', '磁盘空间', 'ok', gb.toFixed(1) + ' GB'));
|
|
217
|
+
}
|
|
218
|
+
} catch (e) {
|
|
219
|
+
checks.push(chk('disk', '磁盘空间', 'warning', '无法读取(' + String((e && e.message) || e) + ')'));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
try {
|
|
223
|
+
const { open, unlink } = await import('node:fs/promises');
|
|
224
|
+
const probe = String(cfg && cfg.profileDir) + '/.dsh-snapshot-write-probe';
|
|
225
|
+
const fh = await open(probe, 'w');
|
|
226
|
+
await fh.close();
|
|
227
|
+
await unlink(probe);
|
|
228
|
+
checks.push(chk('writable', 'profile 可写', 'ok', ''));
|
|
229
|
+
} catch (e) {
|
|
230
|
+
checks.push(chk('writable', 'profile 可写', 'blocker', '无法写入,可能是权限或只读挂载:' + String((e && e.message) || e)));
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
const names = Object.keys((pkg && pkg.dependencies) || {});
|
|
235
|
+
const core = names.filter(isCoreBundle);
|
|
236
|
+
const isDesktop = !!(process.versions && process.versions.electron);
|
|
237
|
+
if (core.length) checks.push(chk('core', '核心 bundle', 'ok', core.length + ' 个'));
|
|
238
|
+
else if (isDesktop) checks.push(chk('core', '核心 bundle', 'ok', '桌面版:核心内置在 app.asar,profile 无需声明'));
|
|
239
|
+
else checks.push(chk('core', '核心 bundle', 'warning', '依赖里没有任何 @deepseek-ai/dsh* 包,profile 可能选错了'));
|
|
240
|
+
} catch (e) { /* 忽略 */ }
|
|
241
|
+
|
|
242
|
+
const blockers = checks.filter((c) => c.status === 'blocker');
|
|
243
|
+
const warnings = checks.filter((c) => c.status === 'warning');
|
|
244
|
+
return { ok: blockers.length === 0, checks, blockers, warnings, policy, level: policy.level, at: Date.now() };
|
|
245
|
+
}
|
package/lib/routes.js
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
* - 所有异常都转成 { ok:false, error } 返回,不让路由 500 把前端打崩。
|
|
9
9
|
*/
|
|
10
10
|
import { execFile } from 'node:child_process';
|
|
11
|
-
import { existsSync, readdirSync } from 'node:fs';
|
|
11
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
|
12
12
|
import { join } from 'node:path';
|
|
13
13
|
import { homedir } from 'node:os';
|
|
14
14
|
import { checkUpdates } from './updates.js';
|
|
@@ -18,6 +18,8 @@ import { registerApplyAll } from './apply-all.js';
|
|
|
18
18
|
import { readAutoState, verifyInstalled, classifyResult } from './auto-update.js';
|
|
19
19
|
import { validateInstallTargets } from './install-guard.js';
|
|
20
20
|
import { findPnpm, runPnpm } from './exec.js';
|
|
21
|
+
import { readAllowBuilds, installArgs } from './allow-builds.js';
|
|
22
|
+
import { requestCancel, isRunning } from './auto-update.js';
|
|
21
23
|
|
|
22
24
|
function send(response, code, payload) {
|
|
23
25
|
/* 请求体超限时调用方会 destroy socket,此时不能再写响应 */
|
|
@@ -215,9 +217,18 @@ export function mountRoutes(hostCtx, cfg, needProfile) {
|
|
|
215
217
|
const pnpmCjs = findPnpm();
|
|
216
218
|
if (!pnpmCjs) return send(response, 200, { ok: false, error: '找不到 pnpm,无法执行更新' });
|
|
217
219
|
const snapshot = createSnapshot(dir, cfg);
|
|
218
|
-
/*
|
|
219
|
-
|
|
220
|
-
|
|
220
|
+
/* 构建授权:只有 allowBuilds 明确放行的包才跑生命周期脚本,
|
|
221
|
+
其余仍加 --ignore-scripts 并在结果里标注 scriptsSkipped,
|
|
222
|
+
避免「版本装上了、postinstall 没跑」被伪装成成功。 */
|
|
223
|
+
const table = readAllowBuilds(dir);
|
|
224
|
+
const decision = installArgs(['add', target.name + '@' + target.version, '--save-exact'], table, target.name, target.version);
|
|
225
|
+
const r = await runPnpm(pnpmCjs, decision.args, dir, 600000);
|
|
226
|
+
let declaredNow;
|
|
227
|
+
try {
|
|
228
|
+
const pkgNow = JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8'));
|
|
229
|
+
declaredNow = (pkgNow.dependencies || {})[target.name];
|
|
230
|
+
} catch (e) { declaredNow = undefined; }
|
|
231
|
+
const installed = verifyInstalled(dir, target.name, target.version, declaredNow);
|
|
221
232
|
const v = classifyResult(r.code, installed, r.output);
|
|
222
233
|
send(response, 200, {
|
|
223
234
|
ok: v.ok,
|
|
@@ -225,6 +236,8 @@ export function mountRoutes(hostCtx, cfg, needProfile) {
|
|
|
225
236
|
code: r.code,
|
|
226
237
|
snapshot: snapshot.name,
|
|
227
238
|
output: r.output.slice(-4000),
|
|
239
|
+
scriptsOk: decision.scriptsOk,
|
|
240
|
+
scriptsReason: decision.scriptsReason,
|
|
228
241
|
needRestart: v.ok
|
|
229
242
|
});
|
|
230
243
|
} catch (e) { send(response, 200, { ok: false, error: String((e && e.message) || e) }); }
|
|
@@ -237,6 +250,14 @@ export function mountRoutes(hostCtx, cfg, needProfile) {
|
|
|
237
250
|
} catch (e) { send(response, 200, { ok: false, error: String((e && e.message) || e) }); }
|
|
238
251
|
});
|
|
239
252
|
|
|
253
|
+
/* 写:取消正在跑的自动更新(只停止后续包,不打断已在跑的 pnpm) */
|
|
254
|
+
reg('/dsh-plugin-snapshot/api/auto/cancel', async (request, response) => {
|
|
255
|
+
try {
|
|
256
|
+
requestCancel();
|
|
257
|
+
send(response, 200, { ok: true, running: isRunning(), message: '已请求取消,当前包完成后停止' });
|
|
258
|
+
} catch (e) { send(response, 200, { ok: false, error: String((e && e.message) || e) }); }
|
|
259
|
+
}, { write: true });
|
|
260
|
+
|
|
240
261
|
registerApplyAll({ reg, cfg, needProfile, send, readBody, createSnapshot, findPnpm, runPnpm });
|
|
241
262
|
|
|
242
263
|
return () => { for (const off of offs) { try { off(); } catch (e) { /* 忽略 */ } } };
|
package/lib/updates.js
CHANGED
|
@@ -1,22 +1,38 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* 版本检测:以 lockfile 的解析版本为准,去 registry 查候选版本。
|
|
3
3
|
* 只读操作,不写 profile、不安装任何东西。
|
|
4
|
+
*
|
|
5
|
+
* 与桌面版对齐的两处关键改动:
|
|
6
|
+
* 1) registry 同源:检测源必须等于 pnpm 实际使用的源,否则会出现
|
|
7
|
+
* 「检测到新版、安装报 ERR_PNPM_NO_MATCHING_VERSION」的假失败;
|
|
8
|
+
* 2) DSH 版本可解析:dsh-version.js 提供 asar 兜底,兼容性判定不再恒为 unknown。
|
|
4
9
|
*/
|
|
5
10
|
import { readProfile, readLockVersions, specKind } from './profile.js';
|
|
6
11
|
import { parseSpec, parseSemver, cmpSemver, isValidPkgName } from './util.js';
|
|
7
12
|
import { judgeCompatibility } from './doctor.js';
|
|
13
|
+
import { findPnpm, resolveRegistry } from './exec.js';
|
|
14
|
+
import { resolveDshVersion } from './dsh-version.js';
|
|
8
15
|
|
|
9
16
|
const DEFAULT_REGISTRY = 'https://registry.npmjs.org';
|
|
10
17
|
|
|
11
|
-
function
|
|
12
|
-
const
|
|
13
|
-
return
|
|
18
|
+
function normalizeRegistry(v) {
|
|
19
|
+
const s = String(v || '').trim().replace(/[/]+$/, '');
|
|
20
|
+
return /^https?:[/][/]/.test(s) ? s : '';
|
|
14
21
|
}
|
|
15
22
|
|
|
16
|
-
|
|
23
|
+
function registryOf(cfg, resolved) {
|
|
24
|
+
const explicit = normalizeRegistry(cfg && cfg.registry);
|
|
25
|
+
if (explicit) return explicit;
|
|
26
|
+
const fromPnpm = normalizeRegistry(resolved);
|
|
27
|
+
if (fromPnpm) return fromPnpm;
|
|
28
|
+
const env = normalizeRegistry(process.env.DSH_REGISTRY);
|
|
29
|
+
if (env) return env;
|
|
30
|
+
return DEFAULT_REGISTRY;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async function fetchLatest(name, base, cfg) {
|
|
17
34
|
if (!isValidPkgName(name)) return { ok: false, reason: '非法包名' };
|
|
18
|
-
|
|
19
|
-
if (!/^https?:[/][/]/.test(base)) return { ok: false, reason: '非法的 registry 地址' };
|
|
35
|
+
if (!base) return { ok: false, reason: '非法的 registry 地址' };
|
|
20
36
|
try {
|
|
21
37
|
const res = await fetch(base + '/' + name + '/latest', {
|
|
22
38
|
signal: AbortSignal.timeout(Math.max(5000, Number((cfg && cfg.timeoutMs) || 20000))),
|
|
@@ -47,25 +63,49 @@ async function mapLimit(items, limit, worker) {
|
|
|
47
63
|
}
|
|
48
64
|
|
|
49
65
|
export async function checkUpdates(profileDir, cfg) {
|
|
50
|
-
/*
|
|
51
|
-
|
|
66
|
+
/* registry 同源:pnpm 实际用的源才是安装时能取到版本的源。
|
|
67
|
+
取不到就回落显式配置/默认源,不猜。 */
|
|
68
|
+
let resolvedRegistry = null;
|
|
69
|
+
if (!normalizeRegistry(cfg && cfg.registry)) {
|
|
70
|
+
try {
|
|
71
|
+
const pnpmCjs = findPnpm();
|
|
72
|
+
resolvedRegistry = await resolveRegistry(pnpmCjs, profileDir);
|
|
73
|
+
} catch (e) { resolvedRegistry = null; }
|
|
74
|
+
}
|
|
75
|
+
const registry = registryOf(cfg, resolvedRegistry);
|
|
76
|
+
|
|
77
|
+
/* DSH 版本:cfg → env → app.asar → profile,四路兜底 */
|
|
78
|
+
const dsh = resolveDshVersion(profileDir, cfg);
|
|
79
|
+
const dshVersion = dsh.version;
|
|
80
|
+
|
|
52
81
|
const prof = readProfile(profileDir);
|
|
53
82
|
const lock = readLockVersions(profileDir);
|
|
83
|
+
const frozenList = Array.isArray(cfg && cfg.frozenPackages) ? cfg.frozenPackages : [];
|
|
54
84
|
const names = Object.keys(prof.deps);
|
|
55
85
|
const targets = [];
|
|
56
86
|
const skipped = [];
|
|
57
87
|
for (const name of names) {
|
|
58
88
|
if (!isValidPkgName(name)) { skipped.push({ name, reason: '非法包名' }); continue; }
|
|
59
89
|
const kind = specKind(prof.deps[name]);
|
|
60
|
-
if (kind === 'local') {
|
|
90
|
+
if (kind === 'local') {
|
|
91
|
+
/* 与桌面版对齐:本地依赖也要出现在结果里(state = local),
|
|
92
|
+
而不是被静默跳过——否则两边「总数」永远差一个,用户无法核对。 */
|
|
93
|
+
targets.push({ name, kind, declared: String(prof.deps[name]), resolved: lock[name] || '', localOnly: true });
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
61
96
|
targets.push({ name, kind, declared: String(prof.deps[name]), resolved: lock[name] || '' });
|
|
62
97
|
}
|
|
63
98
|
|
|
64
99
|
const results = await mapLimit(targets, Math.max(1, Math.min(Number((cfg && cfg.concurrency) || 8), 16)), async (t) => {
|
|
100
|
+
if (t.localOnly) return { ...t, state: 'local', reason: '本地依赖(file:/link:),不参与升级' };
|
|
101
|
+
/* 冻结名单也要出现在结果里:静默过滤会让「为什么它不更新」无从排查 */
|
|
102
|
+
if (frozenList.indexOf(t.name) >= 0) {
|
|
103
|
+
return { ...t, state: 'frozen', reason: '在冻结名单(frozenPackages)中,不参与检测与升级' };
|
|
104
|
+
}
|
|
65
105
|
if (t.kind === 'github') {
|
|
66
106
|
return { ...t, state: 'manual', reason: 'GitHub 源需人工确认,本工具不自动判定' };
|
|
67
107
|
}
|
|
68
|
-
const r = await fetchLatest(t.name, cfg);
|
|
108
|
+
const r = await fetchLatest(t.name, registry, cfg);
|
|
69
109
|
if (!r.ok) return { ...t, state: 'failed', reason: r.reason };
|
|
70
110
|
const cur = parseSpec(t.resolved || t.declared);
|
|
71
111
|
const lat = parseSemver(r.latest);
|
|
@@ -83,22 +123,60 @@ export async function checkUpdates(profileDir, cfg) {
|
|
|
83
123
|
return { ...t, state: 'incompatible', reason: compat.label, candidate: r.latest, compatibility: compat };
|
|
84
124
|
}
|
|
85
125
|
|
|
126
|
+
/* 与桌面版 3.3.5 registry.mjs 完全对齐:只有 major 变化才算 major。
|
|
127
|
+
0.x 的次版本(如 0.2.7 -> 0.3.0)按 semver 属 minor,交给 safetyLevel 决断:
|
|
128
|
+
conservative 档仍会因风险分级把它留在待确认,standard/aggressive 档可自动装。
|
|
129
|
+
把这条硬编码在检测层会让三个安全等级在这一项上毫无区别。 */
|
|
86
130
|
if (cur.major !== lat.major) return { ...t, state: 'major', reason: '主版本变更,需人工确认', candidate: r.latest, compatibility: compat };
|
|
87
|
-
/* 0.x 的次版本变更按 semver 是破坏性的,等同主版本处理 */
|
|
88
|
-
if (cur.major === 0 && cur.minor !== lat.minor) return { ...t, state: 'major', reason: '0.x 次版本变更(破坏性),需人工确认', candidate: r.latest, compatibility: compat };
|
|
89
131
|
return { ...t, state: 'updatable', candidate: r.latest, compatibility: compat };
|
|
90
132
|
});
|
|
91
133
|
|
|
134
|
+
const count = (s) => results.filter((x) => x.state === s).length;
|
|
92
135
|
const summary = {
|
|
93
136
|
total: results.length,
|
|
94
|
-
updatable:
|
|
95
|
-
major:
|
|
96
|
-
uptodate:
|
|
97
|
-
failed:
|
|
137
|
+
updatable: count('updatable'),
|
|
138
|
+
major: count('major'),
|
|
139
|
+
uptodate: count('uptodate'),
|
|
140
|
+
failed: count('failed'),
|
|
98
141
|
skipped: skipped.length,
|
|
99
|
-
downgrade:
|
|
100
|
-
incompatible:
|
|
142
|
+
downgrade: count('downgrade'),
|
|
143
|
+
incompatible: count('incompatible'),
|
|
144
|
+
frozen: count('frozen'),
|
|
145
|
+
prerelease: count('prerelease'),
|
|
146
|
+
manual: count('manual'),
|
|
147
|
+
unknown: count('unknown')
|
|
148
|
+
};
|
|
149
|
+
return {
|
|
150
|
+
items: results,
|
|
151
|
+
skipped,
|
|
152
|
+
summary,
|
|
153
|
+
profile: prof.name,
|
|
154
|
+
registry,
|
|
155
|
+
registrySource: normalizeRegistry(cfg && cfg.registry) ? 'config' : (resolvedRegistry ? 'pnpm' : 'default'),
|
|
156
|
+
dshVersion,
|
|
157
|
+
dshVersionSource: dsh.source
|
|
101
158
|
};
|
|
102
|
-
return { items: results, skipped, summary, profile: prof.name, registry: registryOf(cfg), dshVersion };
|
|
103
159
|
}
|
|
104
160
|
|
|
161
|
+
/**
|
|
162
|
+
* 取某个具体版本的 manifest。
|
|
163
|
+
* 更新前必须用「目标版本自己的声明」判兼容,而不是用当前版本——
|
|
164
|
+
* 桌面版 v3.2.9/v3.3.2 的核心修复之一:拿 latest 的兼容声明去判候选版本,
|
|
165
|
+
* 会把「新版本要求更高的 DSH」放过,装完再回滚。
|
|
166
|
+
*/
|
|
167
|
+
export async function fetchVersionManifest(name, version, base, cfg) {
|
|
168
|
+
if (!isValidPkgName(String(name))) return { ok: false, reason: '非法包名' };
|
|
169
|
+
const ver = String(version || '').trim().replace(/^[\^~>=<\s]+/, '');
|
|
170
|
+
if (!ver) return { ok: false, reason: '缺少目标版本' };
|
|
171
|
+
if (!base) return { ok: false, reason: '非法的 registry 地址' };
|
|
172
|
+
try {
|
|
173
|
+
const res = await fetch(base + '/' + name + '/' + encodeURIComponent(ver), {
|
|
174
|
+
signal: AbortSignal.timeout(Math.max(5000, Number((cfg && cfg.timeoutMs) || 20000))),
|
|
175
|
+
headers: { accept: 'application/json' }
|
|
176
|
+
});
|
|
177
|
+
if (!res.ok) return { ok: false, reason: 'HTTP ' + res.status };
|
|
178
|
+
return { ok: true, manifest: await res.json() };
|
|
179
|
+
} catch (e) {
|
|
180
|
+
return { ok: false, reason: String((e && e.message) || e) };
|
|
181
|
+
}
|
|
182
|
+
}
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-plugin-snapshot",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.9",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "
|
|
5
|
+
"description": "Aligned with dsh-plugin-updater 3.3.5: safety levels, risk-graded update plan, preflight, target-version compatibility (engines + peerDependencies), health gate with auto rollback, allowBuilds-aware installs, snapshot/rollback and static diagnostics.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"deepseek-harness",
|
|
8
8
|
"dsh",
|
|
@@ -49,20 +49,15 @@
|
|
|
49
49
|
},
|
|
50
50
|
"compatibility": {
|
|
51
51
|
"dshReleases": {
|
|
52
|
+
"0.2.2-rc.5": "compatible",
|
|
53
|
+
"0.2.2-rc.3": "compatible",
|
|
52
54
|
"0.2.2-rc.1": "compatible",
|
|
53
|
-
"0.2.1": "compatible",
|
|
54
55
|
"0.2.0+": "compatible"
|
|
55
56
|
}
|
|
56
57
|
}
|
|
57
58
|
},
|
|
58
|
-
"peerDependencies": {
|
|
59
|
-
|
|
60
|
-
},
|
|
61
|
-
"peerDependenciesMeta": {
|
|
62
|
-
"@deepseek-ai/dsh-tools": {
|
|
63
|
-
"optional": true
|
|
64
|
-
}
|
|
65
|
-
},
|
|
59
|
+
"peerDependencies": {},
|
|
60
|
+
"peerDependenciesMeta": {},
|
|
66
61
|
"engines": {
|
|
67
62
|
"node": ">=20"
|
|
68
63
|
},
|