dsh-cloud-backup 0.0.0-stage → 0.1.139
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/CHANGELOG.md +28 -0
- package/LICENSE +674 -0
- package/README.md +134 -2
- package/THIRD_PARTY.md +11 -0
- package/client.js +3060 -0
- package/cordis.patch.yml +3 -0
- package/lib/auth/adrive-local.js +17 -0
- package/lib/auth/aliyun.js +600 -0
- package/lib/auth/drive-login-state.js +240 -0
- package/lib/auth/qr-page.js +140 -0
- package/lib/engine/backups.js +341 -0
- package/lib/engine/blob.js +80 -0
- package/lib/engine/collect.js +148 -0
- package/lib/engine/host-info.js +48 -0
- package/lib/engine/host-version.js +139 -0
- package/lib/engine/lock.js +46 -0
- package/lib/engine/manifest.js +77 -0
- package/lib/engine/plugin-install.js +442 -0
- package/lib/engine/plugin-packs.js +328 -0
- package/lib/engine/prune.js +102 -0
- package/lib/engine/restart.js +172 -0
- package/lib/engine/restore.js +361 -0
- package/lib/engine/rotation.js +50 -0
- package/lib/engine/run.js +170 -0
- package/lib/index.js +1017 -0
- package/lib/providers/aliyun.js +1222 -0
- package/lib/qr.js +29 -0
- package/lib/store/config.js +63 -0
- package/lib/store/credentials.js +69 -0
- package/lib/store/paths.js +45 -0
- package/lib/store/secret.js +94 -0
- package/lib/store/state.js +17 -0
- package/lib/util/errors.js +5 -0
- package/lib/util/log.js +107 -0
- package/lib/util/proc.js +104 -0
- package/lib/util/validate.js +42 -0
- package/lib/vendor/qrcode-generator.cjs +2297 -0
- package/package.json +55 -4
|
@@ -0,0 +1,442 @@
|
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import os from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { runCmd } from "../util/proc.js";
|
|
5
|
+
import { blobPath } from "./blob.js";
|
|
6
|
+
import { logInfo, logWarn } from "../util/log.js";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
|
|
9
|
+
/** 本插件自身:还原时不需要(也不应该)用 CLI 重装,只要保证 profile 里指向本体的链接可用。 */
|
|
10
|
+
const SELF_DIR = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
|
|
11
|
+
const SELF_NAME = (() => {
|
|
12
|
+
try { return JSON.parse(fs.readFileSync(path.join(SELF_DIR, "package.json"), "utf8")).name || ""; } catch { return ""; }
|
|
13
|
+
})();
|
|
14
|
+
|
|
15
|
+
// 还原插件本体:把备份里的 tarball 或来源说明交回 dsh CLI 安装。
|
|
16
|
+
// 默认不执行包内的 postinstall(--ignore-scripts);安装失败要能退回原清单,避免把 profile 弄坏。
|
|
17
|
+
const DEFAULT_TIMEOUT_MS = 15 * 60 * 1000;
|
|
18
|
+
// pnpm 11 的供应链策略(minimumReleaseAge)会在锁文件校验阶段拦下“刚发布”的版本。
|
|
19
|
+
// 快照清单里的版本是用户自己备份时的既成事实,按精确版本放行一次,并把放行记录回写进安装结果。
|
|
20
|
+
const AGE_MARKER = "ERR_PNPM_MINIMUM_RELEASE_AGE_VIOLATION";
|
|
21
|
+
|
|
22
|
+
/** 从报错里挑出被拦下的 精确 name@version(含本次要装的条目本身)。 */
|
|
23
|
+
export function ageBlockedPins(entry, output) {
|
|
24
|
+
const out = [];
|
|
25
|
+
const name = String((entry && entry.name) || "");
|
|
26
|
+
const ver = String((entry && entry.version) || "");
|
|
27
|
+
if (name && ver && ver !== "unknown") out.push(name + "@" + ver);
|
|
28
|
+
const src = String(output || "");
|
|
29
|
+
const at = src.indexOf(AGE_MARKER);
|
|
30
|
+
const block = at >= 0 ? src.slice(at, at + 4000) : src;
|
|
31
|
+
const re = /(?:^|[\s"'\[(])((?:@[a-z0-9][\w.-]*\/)?[a-z0-9][\w.-]*)@(\d+\.\d+\.\d+(?:-[\w.-]+)?)/gi;
|
|
32
|
+
let m = null;
|
|
33
|
+
while ((m = re.exec(block))) {
|
|
34
|
+
const pin = m[1] + "@" + m[2];
|
|
35
|
+
if (out.indexOf(pin) < 0) out.push(pin);
|
|
36
|
+
if (out.length >= 12) break;
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 把精确版本写进 profile 的 pnpm-workspace.yaml → minimumReleaseAgeExclude(幂等,保留原有条目)。 */
|
|
42
|
+
export function addReleaseAgeExclude(profileDir, pins) {
|
|
43
|
+
const list = (Array.isArray(pins) ? pins : []).map((x) => String(x)).filter(Boolean);
|
|
44
|
+
const file = path.join(profileDir, "pnpm-workspace.yaml");
|
|
45
|
+
const res = { file, added: [], existing: [] };
|
|
46
|
+
if (!list.length) return res;
|
|
47
|
+
let text = "";
|
|
48
|
+
try { text = fs.readFileSync(file, "utf8"); } catch { text = ""; }
|
|
49
|
+
const eol = text.indexOf("\r\n") >= 0 ? "\r\n" : "\n";
|
|
50
|
+
const lines = text.length ? text.split(/\r?\n/) : [];
|
|
51
|
+
const key = lines.findIndex((y) => /^minimumReleaseAgeExclude\s*:/.test(y));
|
|
52
|
+
const kept = [];
|
|
53
|
+
if (key >= 0) {
|
|
54
|
+
for (let i = key + 1; i < lines.length; i++) {
|
|
55
|
+
const y = lines[i];
|
|
56
|
+
if (!y.trim()) continue;
|
|
57
|
+
if (/^\s*-/.test(y)) { kept.push(y.replace(/^\s*-\s*/, "").replace(/^['"]|['"]$/g, "").trim()); continue; }
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
res.existing = kept.slice();
|
|
62
|
+
for (const pin of list) if (kept.indexOf(pin) < 0) { kept.push(pin); res.added.push(pin); }
|
|
63
|
+
if (!res.added.length) return res;
|
|
64
|
+
const block = ["minimumReleaseAgeExclude:"].concat(kept.map((x) => " - '" + x + "'"));
|
|
65
|
+
let out;
|
|
66
|
+
if (key >= 0) {
|
|
67
|
+
let end = key + 1;
|
|
68
|
+
for (let i = key + 1; i < lines.length; i++) {
|
|
69
|
+
const y = lines[i];
|
|
70
|
+
if (!y.trim()) { end = i + 1; continue; }
|
|
71
|
+
if (/^\s*-/.test(y)) { end = i + 1; continue; }
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
out = lines.slice(0, key).concat(block, lines.slice(end));
|
|
75
|
+
} else {
|
|
76
|
+
out = lines.slice();
|
|
77
|
+
while (out.length && !out[out.length - 1].trim()) out.pop();
|
|
78
|
+
out = out.concat(block);
|
|
79
|
+
}
|
|
80
|
+
while (out.length && !out[out.length - 1].trim()) out.pop();
|
|
81
|
+
try { fs.writeFileSync(file, out.join(eol) + eol); } catch (e) { res.added = []; res.error = String((e && e.message) || e); }
|
|
82
|
+
return res;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
// dsh CLI 不在 PATH 上是常态(桌面版自带一份),且用户目录可能不在系统盘,
|
|
87
|
+
// 因此按「显式路径 → 环境变量 → 运行时相对路径 → 已知安装位置(含跨盘探测)」依次找。
|
|
88
|
+
function cliCandidates(explicit) {
|
|
89
|
+
const rel = ["Programs", "DeepSeek Harness", "resources", "runtime", "cli", "bin", "dsh.cmd"];
|
|
90
|
+
const roots = [];
|
|
91
|
+
const local = process.env.LOCALAPPDATA;
|
|
92
|
+
const home = os.homedir();
|
|
93
|
+
if (local) roots.push(local);
|
|
94
|
+
if (home) roots.push(path.join(home, "AppData", "Local"));
|
|
95
|
+
const user = path.basename(home || "");
|
|
96
|
+
if (user && process.platform === "win32") {
|
|
97
|
+
for (const d of "CDEFGHIJKLMNOPQRSTUVWXYZ") roots.push(d + ":/Users/" + user + "/AppData/Local");
|
|
98
|
+
}
|
|
99
|
+
const out = [explicit, process.env.DSH_CLOUD_BACKUP_DSH_CMD, process.env.DSH_BIN];
|
|
100
|
+
const exe = process.execPath || "";
|
|
101
|
+
const marker = exe.indexOf("DeepSeek Harness");
|
|
102
|
+
if (marker > 0) out.push(path.join(exe.slice(0, marker), "DeepSeek Harness", ...rel));
|
|
103
|
+
for (const r of roots) out.push(path.join(r, ...rel));
|
|
104
|
+
return out.filter(Boolean);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function localPluginsDir(dshHome) {
|
|
108
|
+
return path.join(dshHome, "cloud-backup", "local-plugins");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function resolveDshCmd(explicit = null) {
|
|
112
|
+
for (const c of cliCandidates(explicit)) {
|
|
113
|
+
try {
|
|
114
|
+
if (fs.existsSync(c)) return { ok: true, cmd: c, via: "abs" };
|
|
115
|
+
} catch { /* 忽略不可读候选 */ }
|
|
116
|
+
}
|
|
117
|
+
return { ok: true, cmd: "dsh", via: "path" };
|
|
118
|
+
}
|
|
119
|
+
export function addTargetFor(entry, tgzPath = null) {
|
|
120
|
+
const name = String(entry.name || "");
|
|
121
|
+
if (tgzPath) return { via: "tarball", target: tgzPath };
|
|
122
|
+
const spec = String(entry.spec || "");
|
|
123
|
+
const version = String(entry.version || "");
|
|
124
|
+
if (entry.source_kind === "registry") {
|
|
125
|
+
if (version && version !== "unknown") return { via: "spec", target: name + "@" + version };
|
|
126
|
+
return { via: "spec", target: name };
|
|
127
|
+
}
|
|
128
|
+
// github:/https:/file: 之类的来源原样交给 pnpm,避免改写后指向不存在的版本
|
|
129
|
+
return { via: "spec", target: spec || name };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// 只对 tarball 生效:把内容寻址的块复制成 CLI 能吃的文件。
|
|
133
|
+
// 必须落在稳定目录:profile 的 package.json 会记住这个路径,临时目录被清掉会让后续 pnpm 安装失败。
|
|
134
|
+
function materialize(storeDir, blob, name, version, keepDir, tmpDir) {
|
|
135
|
+
const src = blobPath(storeDir, blob);
|
|
136
|
+
if (!fs.existsSync(src)) return { ok: false, error: "blob 不在本地(先下载该快照)" };
|
|
137
|
+
const safe = String(name).replace(/[^a-zA-Z0-9._@-]+/g, "-");
|
|
138
|
+
// 文件名带内容前缀:块内容变了就是新文件,pnpm 不会拿旧 lockfile 的校验和来比对新内容
|
|
139
|
+
const fileName = safe + "-" + String(version || "0") + "-" + String(blob).slice(0, 8) + ".tgz";
|
|
140
|
+
try {
|
|
141
|
+
fs.mkdirSync(keepDir, { recursive: true });
|
|
142
|
+
const kept = path.join(keepDir, fileName);
|
|
143
|
+
if (!fs.existsSync(kept)) fs.copyFileSync(src, kept);
|
|
144
|
+
return { ok: true, path: kept, kept: true };
|
|
145
|
+
} catch {
|
|
146
|
+
const out = path.join(tmpDir, fileName);
|
|
147
|
+
fs.copyFileSync(src, out);
|
|
148
|
+
return { ok: true, path: out, kept: false };
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// profile 的 bundles 里若留着「已经不在 node_modules」的插件,宿主会拒绝再装任何插件
|
|
153
|
+
// 装之前先摘掉失效条目(否则报 cannot resolve profile bundle),运行时的 @deepseek-ai/* 保留。
|
|
154
|
+
function sanitizeBundles(dshHome, profiles, log) {
|
|
155
|
+
const dropped = [];
|
|
156
|
+
for (const p of profiles || []) {
|
|
157
|
+
const pkgPath = path.join(dshHome, "profiles", String(p), "package.json");
|
|
158
|
+
let pkg = null;
|
|
159
|
+
try { pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8")); } catch { continue; }
|
|
160
|
+
const list = pkg && pkg.dsh && pkg.dsh.profile && pkg.dsh.profile.bundles;
|
|
161
|
+
if (!Array.isArray(list) || !list.length) continue;
|
|
162
|
+
const nm = path.join(dshHome, "profiles", String(p), "node_modules");
|
|
163
|
+
const keep = list.filter(function (name) {
|
|
164
|
+
const n = String(name);
|
|
165
|
+
if (n.indexOf("@deepseek-ai/") === 0) return true;
|
|
166
|
+
return fs.existsSync(path.join(nm, n, "package.json"));
|
|
167
|
+
});
|
|
168
|
+
if (keep.length === list.length) continue;
|
|
169
|
+
pkg.dsh.profile.bundles = keep;
|
|
170
|
+
try { fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + String.fromCharCode(10)); } catch { continue; }
|
|
171
|
+
for (const name of list) if (keep.indexOf(name) < 0) dropped.push(String(p) + "/" + String(name));
|
|
172
|
+
}
|
|
173
|
+
if (dropped.length && typeof log === "function") log({ phase: "cleanup", dropped });
|
|
174
|
+
return dropped;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// 装回成功后把插件名补回 profile 的 dsh.profile.bundles,保证宿主重启时能加载它们
|
|
178
|
+
function restoreBundles(dshHome, installed, log) {
|
|
179
|
+
const added = [];
|
|
180
|
+
for (const rec of installed || []) {
|
|
181
|
+
const name = String(rec.name);
|
|
182
|
+
const p = String(rec.profile || "");
|
|
183
|
+
if (!p) continue;
|
|
184
|
+
const pkgPath = path.join(dshHome, "profiles", p, "package.json");
|
|
185
|
+
let pkg = null;
|
|
186
|
+
try { pkg = JSON.parse(fs.readFileSync(pkgPath, "utf8")); } catch { continue; }
|
|
187
|
+
if (!pkg.dsh || !pkg.dsh.profile) continue;
|
|
188
|
+
const list = Array.isArray(pkg.dsh.profile.bundles) ? pkg.dsh.profile.bundles : [];
|
|
189
|
+
if (list.indexOf(name) >= 0) continue;
|
|
190
|
+
if (!fs.existsSync(path.join(dshHome, "profiles", p, "node_modules", name, "package.json"))) continue;
|
|
191
|
+
pkg.dsh.profile.bundles = list.concat([name]);
|
|
192
|
+
try { fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + String.fromCharCode(10)); added.push(p + "/" + name); } catch { /* 写不回就保持原样 */ }
|
|
193
|
+
}
|
|
194
|
+
if (added.length && typeof log === "function") log({ phase: "bundles-restored", added });
|
|
195
|
+
return added;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function snapshotManifests(dshHome, profiles) {
|
|
199
|
+
const saved = {};
|
|
200
|
+
for (const p of profiles) {
|
|
201
|
+
const dir = path.join(dshHome, "profiles", p);
|
|
202
|
+
for (const f of ["package.json", "pnpm-lock.yaml"]) {
|
|
203
|
+
const abs = path.join(dir, f);
|
|
204
|
+
try {
|
|
205
|
+
saved[p + "/" + f] = fs.existsSync(abs) ? fs.readFileSync(abs) : null;
|
|
206
|
+
} catch { saved[p + "/" + f] = null; }
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return saved;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function rollbackManifests(dshHome, saved) {
|
|
213
|
+
const undone = [];
|
|
214
|
+
for (const key of Object.keys(saved)) {
|
|
215
|
+
const abs = path.join(dshHome, "profiles", key);
|
|
216
|
+
const buf = saved[key];
|
|
217
|
+
try {
|
|
218
|
+
if (buf === null) { if (fs.existsSync(abs)) fs.unlinkSync(abs); }
|
|
219
|
+
else fs.writeFileSync(abs, buf);
|
|
220
|
+
undone.push(key);
|
|
221
|
+
} catch (e) { logWarn("plugin.install.rollback-failed", { file: key, error: String((e && e.message) || e) }); }
|
|
222
|
+
}
|
|
223
|
+
return undone;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export async function installPluginPacks({
|
|
227
|
+
dshHome,
|
|
228
|
+
storeDir,
|
|
229
|
+
plugins = [],
|
|
230
|
+
profiles = null,
|
|
231
|
+
allowScripts = false,
|
|
232
|
+
timeoutMs = DEFAULT_TIMEOUT_MS,
|
|
233
|
+
cliPath = null,
|
|
234
|
+
dryRun = false,
|
|
235
|
+
onLog = null,
|
|
236
|
+
} = {}) {
|
|
237
|
+
const list = Array.isArray(plugins) ? plugins.filter((x) => x && x.name) : [];
|
|
238
|
+
const wanted = Array.isArray(profiles) && profiles.length ? new Set(profiles.map(String)) : null;
|
|
239
|
+
const cli = resolveDshCmd(cliPath);
|
|
240
|
+
const log = typeof onLog === "function" ? onLog : () => {};
|
|
241
|
+
const out = { ok: true, cli, via: {}, installed: [], skipped: [], failed: [], rollback: null, depsOk: true };
|
|
242
|
+
|
|
243
|
+
if (!list.length) { out.ok = false; out.error = "清单里没有 plugins 记录"; return out; }
|
|
244
|
+
if (!dryRun && !cli.ok) { out.ok = false; out.error = cli.error; return out; }
|
|
245
|
+
|
|
246
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "dshcb-plugin-"));
|
|
247
|
+
const targetProfiles = [];
|
|
248
|
+
for (const e of list) for (const p of (e.profiles || [])) if (!wanted || wanted.has(String(p))) if (targetProfiles.indexOf(p) < 0) targetProfiles.push(p);
|
|
249
|
+
const saved = dryRun ? {} : snapshotManifests(dshHome, targetProfiles);
|
|
250
|
+
|
|
251
|
+
try {
|
|
252
|
+
const droppedBundles = sanitizeBundles(dshHome, Array.from(wanted || []), log);
|
|
253
|
+
for (const entry of list) {
|
|
254
|
+
const name = String(entry.name);
|
|
255
|
+
const profs = (entry.profiles || []).filter((p) => !wanted || wanted.has(String(p)));
|
|
256
|
+
if (!profs.length) { out.skipped.push({ name, reason: "profile-not-in-scope" }); continue; }
|
|
257
|
+
|
|
258
|
+
// 自身插件:装自己会把它变成指向 .pnpm 的断链(原本是 link: 到工作区),故只重建链接、不走 CLI
|
|
259
|
+
if (SELF_NAME && name === SELF_NAME) {
|
|
260
|
+
const rec = { name, profile: profs[0], via: "self", target: SELF_DIR, ok: true, seconds: 0, error: null, output: null };
|
|
261
|
+
try {
|
|
262
|
+
for (const p2 of profs) {
|
|
263
|
+
const nm = path.join(dshHome, "profiles", p2, "node_modules");
|
|
264
|
+
fs.mkdirSync(nm, { recursive: true });
|
|
265
|
+
const dst = path.join(nm, name);
|
|
266
|
+
let good = false;
|
|
267
|
+
try { good = !!fs.realpathSync(dst) && fs.existsSync(path.join(dst, "package.json")); } catch { good = false; }
|
|
268
|
+
if (!good) {
|
|
269
|
+
try { fs.rmSync(dst, { recursive: true, force: true }); } catch { /* 断链删不掉就继续建 */ }
|
|
270
|
+
fs.symlinkSync(SELF_DIR, dst, "junction");
|
|
271
|
+
}
|
|
272
|
+
if (!fs.existsSync(path.join(dst, "package.json"))) rec.ok = false;
|
|
273
|
+
}
|
|
274
|
+
} catch (e) { rec.ok = false; rec.error = "无法恢复自身链接:" + String((e && e.message) || e); }
|
|
275
|
+
if (rec.ok) { out.installed.push(rec); log({ phase: "self", name, profile: rec.profile }); }
|
|
276
|
+
else { out.failed.push(rec); out.depsOk = false; log({ phase: "failed", name, profile: rec.profile, error: rec.error }); }
|
|
277
|
+
logInfo("plugin.install." + (rec.ok ? "done" : "failed"), { name, profile: rec.profile, via: "self", error: rec.error || "" });
|
|
278
|
+
continue;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
let prep = { ok: true, path: null };
|
|
282
|
+
if (entry.state === "packed" && entry.blob) prep = materialize(storeDir, entry.blob, name, entry.version, path.join(dshHome, "cloud-backup", "local-plugins"), tmpDir);
|
|
283
|
+
if (!prep.ok && /^file:/i.test(String(entry.spec || ""))) {
|
|
284
|
+
out.skipped.push({ name, reason: prep.error, fallback: null });
|
|
285
|
+
continue; // 条目来源就是本机 tarball,包没了重试也没意义
|
|
286
|
+
}
|
|
287
|
+
const { via, target } = prep.ok && prep.path ? addTargetFor(entry, prep.path) : addTargetFor(entry, null);
|
|
288
|
+
if (!prep.ok) out.skipped.push({ name, reason: prep.error, fallback: "spec" });
|
|
289
|
+
// 来源是本机 tarball(例如上次安装写进 package.json 的 file: 路径)但文件已经不在了:跳过,别硬装
|
|
290
|
+
if (via === "spec" && /^file:/i.test(String(target || ""))) {
|
|
291
|
+
const p = String(target).replace(/^file:/i, "");
|
|
292
|
+
if (!fs.existsSync(p)) { out.skipped.push({ name, reason: "本机没有这个 tarball(来源已失效)", fallback: null }); continue; }
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
for (const p of profs) {
|
|
296
|
+
// 参数数组直传:profile 名与包名/spec 里的引号、&、&& 都只是普通参数,不会被 cmd.exe 解释
|
|
297
|
+
const args = ["plugin", "--profile", p, "add"];
|
|
298
|
+
if (!allowScripts) args.push("--ignore-scripts");
|
|
299
|
+
args.push(target);
|
|
300
|
+
if (dryRun) { out.installed.push({ name, profile: p, via, target, dryRun: true }); continue; }
|
|
301
|
+
log({ phase: "start", name, profile: p, via });
|
|
302
|
+
const cmdEnv = { ...process.env, DSH_HOME: dshHome, CI: "true", ...(allowScripts ? {} : { npm_config_ignore_scripts: "true" }) };
|
|
303
|
+
const installedAt = path.join(dshHome, "profiles", p, "node_modules", name, "package.json");
|
|
304
|
+
const attempt = function (r) {
|
|
305
|
+
const present = fs.existsSync(installedAt);
|
|
306
|
+
const rec = {
|
|
307
|
+
name, profile: p, via, target,
|
|
308
|
+
ok: r.ok && present,
|
|
309
|
+
seconds: null,
|
|
310
|
+
error: r.ok ? (present ? null : "安装后未在 node_modules 找到该插件") : (r.timedOut ? "命令超时(" + Math.round(timeoutMs / 1000) + "s)" : r.error),
|
|
311
|
+
output: (r.stdout + "\n" + r.stderr).trim().split("\n").slice(-3).join(" | ").slice(0, 300) || null,
|
|
312
|
+
};
|
|
313
|
+
if (!rec.ok) rec.detail = pnpmDiagnostics((r.stdout || "") + "\n" + (r.stderr || ""));
|
|
314
|
+
return rec;
|
|
315
|
+
};
|
|
316
|
+
let r = await runCmd(cli.cmd, args, { cwd: dshHome, env: cmdEnv, timeoutMs });
|
|
317
|
+
let rec = attempt(r);
|
|
318
|
+
// 被供应链年龄策略拦下:按精确版本放行一次再重试(版本来自用户自己的快照清单)
|
|
319
|
+
if (!rec.ok && ((r.stdout || "") + "\n" + (r.stderr || "")).indexOf(AGE_MARKER) >= 0) {
|
|
320
|
+
const raw = (r.stdout || "") + "\n" + (r.stderr || "");
|
|
321
|
+
const applied = addReleaseAgeExclude(path.join(dshHome, "profiles", p), ageBlockedPins(entry, raw));
|
|
322
|
+
if (applied.added.length) {
|
|
323
|
+
logWarn("plugin.install.ageExclude", { name, profile: p, pins: applied.added });
|
|
324
|
+
log({ phase: "retry", name, profile: p, via, reason: "minimumReleaseAge" });
|
|
325
|
+
r = await runCmd(cli.cmd, args, { cwd: dshHome, env: cmdEnv, timeoutMs });
|
|
326
|
+
rec = attempt(r);
|
|
327
|
+
rec.ageExcluded = applied.added;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (rec.ok) { out.installed.push(rec); log({ phase: "done", name, profile: p, via }); }
|
|
331
|
+
else { out.failed.push(rec); out.depsOk = false; log({ phase: "failed", name, profile: p, via, error: rec.error }); }
|
|
332
|
+
logInfo("plugin.install." + (rec.ok ? "done" : "failed"), { name, profile: p, via, error: rec.error || "", detail: rec.detail || "" });
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
} finally {
|
|
336
|
+
try { fs.rmSync(tmpDir, { recursive: true, force: true }); } catch { /* 临时目录清理失败不影响结论 */ }
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
// 装成功后把本次装上的插件补回 profile 的 bundles(装前为过 bundle 校验临时摘掉过)
|
|
340
|
+
if (!dryRun && out.installed.length) restoreBundles(dshHome, out.installed, log);
|
|
341
|
+
|
|
342
|
+
if (!dryRun && out.failed.length) {
|
|
343
|
+
out.rollback = rollbackManifests(dshHome, saved);
|
|
344
|
+
logWarn("plugin.install.rollback", { files: out.rollback.length, failed: out.failed.length });
|
|
345
|
+
}
|
|
346
|
+
out.ok = out.failed.length === 0;
|
|
347
|
+
return out;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** 插件 CLI 失败时把「真正的原因」抠出来:先看它自己的输出,再看它指出的诊断日志;都没有就给日志路径。 */
|
|
351
|
+
export function pnpmDiagnostics(text) {
|
|
352
|
+
const src = String(text || "");
|
|
353
|
+
const rows = (x) => x.split(/\r?\n/).map((y) => y.trim()).filter(Boolean);
|
|
354
|
+
const strong = (x) => x.filter((y) => /ERR_PNPM|ERR_|EACCES|ENOENT|not match|integrity|404/i.test(y));
|
|
355
|
+
const weak = (x) => x.filter((y) => !/^Command failed with exit code/i.test(y) && /error|failed/i.test(y));
|
|
356
|
+
const fmt = (x) => x.slice(-2).join(" | ").slice(0, 300);
|
|
357
|
+
|
|
358
|
+
const named = (x) => x.filter((y) => /ERR_PNPM/i.test(y));
|
|
359
|
+
// 年龄策略会把「哪些条目被拦」列在后面几行,一并留下,否则事后无从追查
|
|
360
|
+
const ageBlock = function (list) {
|
|
361
|
+
const i = list.findIndex(function (y) { return y.indexOf(AGE_MARKER) >= 0; });
|
|
362
|
+
return i < 0 ? null : list.slice(i, i + 5).join(" | ").slice(0, 300);
|
|
363
|
+
};
|
|
364
|
+
const fromOut = named(rows(src));
|
|
365
|
+
if (fromOut.length) return ageBlock(rows(src)) || fromOut[0].slice(0, 300);
|
|
366
|
+
const fromStrong = strong(rows(src));
|
|
367
|
+
if (fromStrong.length) return fmt(fromStrong);
|
|
368
|
+
|
|
369
|
+
const m = /([A-Za-z]:[^\s"']*?\.log)/.exec(src);
|
|
370
|
+
if (m) {
|
|
371
|
+
try {
|
|
372
|
+
const lines = rows(fs.readFileSync(m[1], "utf8"));
|
|
373
|
+
const st = named(lines);
|
|
374
|
+
if (st.length) return st[0].slice(0, 300);
|
|
375
|
+
const st2 = strong(lines);
|
|
376
|
+
if (st2.length) return fmt(st2);
|
|
377
|
+
const wk = weak(lines);
|
|
378
|
+
if (wk.length) return fmt(wk);
|
|
379
|
+
return ("pnpm 退出码非 0;诊断日志:" + m[1]).slice(0, 300);
|
|
380
|
+
} catch { return ("pnpm 退出码非 0;诊断日志:" + m[1]).slice(0, 300); }
|
|
381
|
+
}
|
|
382
|
+
const w = weak(rows(src));
|
|
383
|
+
return w.length ? fmt(w) : null;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/** 生成「插件还原提示词」:给没装本插件的机器上的 agent 用;纯文本、不含凭据。 */
|
|
387
|
+
export function buildRestorePrompt({ stamp, plugins = [], profiles = [], tarballDir = "", dshHome = "", fromProfile = null, toProfile = null } = {}) {
|
|
388
|
+
const list = (plugins || []).filter((p) => p && p.name);
|
|
389
|
+
const packed = list.filter((p) => p.state === "packed");
|
|
390
|
+
const others = list.filter((p) => p.state !== "packed");
|
|
391
|
+
const profs = (profiles && profiles.length
|
|
392
|
+
? profiles
|
|
393
|
+
: [...new Set(list.flatMap((p) => p.profiles || []))]).join(", ") || "(未知)";
|
|
394
|
+
let HOME = String(dshHome || "").split("\\").join("/");
|
|
395
|
+
while (HOME.length > 1 && HOME.slice(-1) === "/") HOME = HOME.slice(0, -1);
|
|
396
|
+
const port = function (p) {
|
|
397
|
+
let s = String(p || "").split("\\").join("/");
|
|
398
|
+
while (s.length > 1 && s.slice(-1) === "/") s = s.slice(0, -1);
|
|
399
|
+
if (!s) return "<DSH_HOME>";
|
|
400
|
+
if (HOME && s.toLowerCase().indexOf(HOME.toLowerCase()) === 0) return "<DSH_HOME>" + s.slice(HOME.length);
|
|
401
|
+
return s;
|
|
402
|
+
};
|
|
403
|
+
if (!list.length) {
|
|
404
|
+
return "该快照(" + String(stamp || "") + ")里没有记录插件包,无需按插件步骤还原;"
|
|
405
|
+
+ "它只包含配置与数据,直接按快照还原即可。";
|
|
406
|
+
}
|
|
407
|
+
const L = [];
|
|
408
|
+
L.push("请把 DSH 快照 " + String(stamp || "") + " 里的插件装回这台机器(务必不要执行包内的 install/postinstall 脚本)。");
|
|
409
|
+
L.push("");
|
|
410
|
+
L.push("背景:这是 DSH(DeepSeek Harness)的配置快照,插件本体已随快照上传并按内容寻址存放;涉及 profile:" + profs + "。");
|
|
411
|
+
if (fromProfile && toProfile && fromProfile !== toProfile) {
|
|
412
|
+
L.push("注意:该快照来自 profile \"" + fromProfile + "\",当前环境是 \"" + toProfile + "\";下面的 --profile 请填你要装回的目标 profile。");
|
|
413
|
+
}
|
|
414
|
+
L.push("");
|
|
415
|
+
let step = 0;
|
|
416
|
+
if (packed.length) {
|
|
417
|
+
step++;
|
|
418
|
+
L.push(step + ") 取回插件包:快照目录下的 blobs/<hash> 就是该插件包(gzip tar)。");
|
|
419
|
+
for (const p of packed) {
|
|
420
|
+
const h = String(p.blob || "?");
|
|
421
|
+
L.push(" - " + p.name + "@" + (p.version || "?") + " blob=" + h.slice(0, 16) + " → 另存为 " + p.name + "-" + (p.version || "0") + ".tgz");
|
|
422
|
+
}
|
|
423
|
+
if (tarballDir) L.push(" (若本机装了 cloud-backup 插件,包已在:" + port(tarballDir) + ")");
|
|
424
|
+
}
|
|
425
|
+
if (others.length) {
|
|
426
|
+
step++;
|
|
427
|
+
L.push(step + ") 以下条目未打包,直接用来源重装:");
|
|
428
|
+
for (const p of others) L.push(" - " + p.name + "@" + (p.version || "?") + " 来源:" + (p.spec || p.via || "(未知)"));
|
|
429
|
+
}
|
|
430
|
+
step++;
|
|
431
|
+
L.push(step + ") 安装命令(逐条执行,tarball 一律带 --ignore-scripts):");
|
|
432
|
+
const tgt = toProfile || "<profile>";
|
|
433
|
+
for (const p of packed) L.push(' dsh plugin --profile ' + tgt + ' add --ignore-scripts "<绝对路径>/' + p.name + "-" + (p.version || "0") + '.tgz"');
|
|
434
|
+
for (const p of others) L.push(' dsh plugin --profile ' + tgt + ' add "' + (p.spec || p.name) + '"');
|
|
435
|
+
step++;
|
|
436
|
+
L.push(step + ") 校验:<DSH_HOME>/profiles/<profile>/node_modules/<name>/package.json 必须存在,版本与上面一致。");
|
|
437
|
+
step++;
|
|
438
|
+
L.push(step + ") 任一条失败就停下并保留报错原文,不要静默跳过。");
|
|
439
|
+
L.push("");
|
|
440
|
+
L.push("注意:<DSH_HOME> 指本机 DSH 主目录(Windows 默认 C:/Users/<你的用户名>/.dsh);路径一律用绝对路径;不要改动快照里的 storages/ 与 sessions/ 数据。");
|
|
441
|
+
return L.join(String.fromCharCode(10));
|
|
442
|
+
}
|