dsh-cloud-backup 0.0.0-stage → 0.1.138

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.
@@ -0,0 +1,341 @@
1
+ /**
2
+ */
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+ import { storeDir as storeDirOf } from "../store/paths.js";
6
+ import { loadState } from "../store/state.js";
7
+ import { listSnapshots, readManifest, manifestPath, snapshotDir, rebuildIndex } from "./manifest.js";
8
+ import { gcBlobs, listBlobs } from "./blob.js";
9
+ import { acquireLock, releaseLock, busyResult } from "./lock.js";
10
+ import { logWarn } from "../util/log.js";
11
+ import {
12
+ cachedRemoteSnapshots,
13
+ listRemoteSnapshots,
14
+ pruneRemoteSnapshots,
15
+ getBackupIndex,
16
+ upsertBackupIndex,
17
+ syncSnapshot,
18
+ getRemoteManifest,
19
+ classifyCloudError,
20
+ } from "../providers/aliyun.js";
21
+
22
+ /** 某个本地缓存目录占多少字节(缓存清理的展示用,容忍失败)。 */
23
+ export function localCacheBytes(storeDir, stamp) {
24
+ storeDir = storeDir || storeDirOf();
25
+ let total = 0;
26
+ const walk = (dir) => {
27
+ let ents = [];
28
+ try { ents = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; }
29
+ for (const e of ents) {
30
+ const p = path.join(dir, e.name);
31
+ if (e.isDirectory()) walk(p);
32
+ else { try { total += fs.statSync(p).size; } catch { /* 忽略 */ } }
33
+ }
34
+ };
35
+ walk(snapshotDir(storeDir, stamp));
36
+ return total;
37
+ }
38
+
39
+ export async function listBackups({ storeDir, fresh = false } = {}) {
40
+ storeDir = storeDir || storeDirOf();
41
+ const localSnaps = listSnapshots(storeDir);
42
+ const localSet = new Set(localSnaps.map((s) => s.stamp));
43
+ const localMeta = new Map();
44
+ const cacheBytes = new Map();
45
+ for (const s of localSnaps) {
46
+ const m = readManifest(manifestPath(storeDir, s.stamp));
47
+ localMeta.set(s.stamp, m ? (function () {
48
+ const list = m.plugins || [];
49
+ const per = {};
50
+ let packed = 0;
51
+ for (const p of list) {
52
+ if (p && p.state === "packed") packed += 1;
53
+ for (const pf of ((p && p.profiles) || [])) per[pf] = (per[pf] || 0) + 1;
54
+ }
55
+ return { files: (m.files || []).length, bytes: m.stats?.bytes ?? 0, createdAt: m.created_at || null,
56
+ plugins: list.length, pluginPacked: packed, pluginProfiles: per,
57
+ hostProfile: (m.runtime && m.runtime.profile) || null };
58
+ })() : null);
59
+ cacheBytes.set(s.stamp, localCacheBytes(storeDir, s.stamp));
60
+ }
61
+
62
+ const [cloud, idx] = await Promise.all([
63
+ listRemoteSnapshots({ storeDir, fresh }).catch((e) => {
64
+ const msg = (e && e.message) || String(e);
65
+ const kind = classifyCloudError(e);
66
+ // 面板只说人话,原始报错留在这里备查(「更多 → 打开日志」)
67
+ logWarn("cloud.list.failed", { kind, error: msg });
68
+ return { ok: false, snapshots: [], error: msg, errorKind: kind };
69
+ }),
70
+ getBackupIndex({ fresh }).catch((e) => ({ ok: false, entries: {}, error: (e && e.message) || String(e) })),
71
+ ]);
72
+ const cloudOk = !!cloud.ok;
73
+ const cloudKind = cloudOk ? null : (cloud.errorKind || "network");
74
+ // 云端读不到时:先退回「最近一次成功列举到的集合」判断各行的真实状态;
75
+ // 连这份缓存都没有,才把行标成 unknown(并且绝不改写成"未同步"——那是在替云端下结论)
76
+ const knownCloud = cloudOk ? null : cachedRemoteSnapshots(storeDir);
77
+ const stampSet = (list) => new Set((list || []).filter((s) => s.hasManifest).map((s) => s.stamp));
78
+ const cloudStamps = stampSet(cloud.snapshots);
79
+ const knownStamps = stampSet(((knownCloud && knownCloud.snapshots) || []));
80
+ const effStamps = cloudOk ? cloudStamps : knownStamps;
81
+ const cloudListUsable = cloudOk || knownStamps.size > 0;
82
+ const entries = (idx && idx.entries) || {};
83
+ const lastBackup = (loadState().last_backup || null);
84
+
85
+ const all = new Set([...localSet, ...cloudStamps]);
86
+ const items = [];
87
+ for (const stamp of all) {
88
+ const inCloud = effStamps.has(stamp);
89
+ const hasCache = localSet.has(stamp);
90
+ const meta = entries[stamp] || null;
91
+ const lm = localMeta.get(stamp) || null;
92
+ const upErr = lastBackup && lastBackup.snapshot === stamp && lastBackup.upload && lastBackup.upload.ok === false
93
+ ? String(lastBackup.upload.error || "upload failed")
94
+ : null;
95
+ items.push({
96
+ stamp,
97
+ state: upErr ? "failed" : (inCloud ? "uploaded" : (cloudListUsable ? "pending" : "unknown")),
98
+ cloudKnown: cloudListUsable,
99
+ inCloud,
100
+ hasCache,
101
+ uploadError: upErr,
102
+ files: meta && Number.isFinite(meta.files) ? meta.files : (lm ? lm.files : null),
103
+ bytes: meta && Number.isFinite(meta.bytes) ? meta.bytes : (lm ? lm.bytes : null),
104
+ createdAt: (meta && meta.created_at) || (lm && lm.createdAt) || null,
105
+ uploadedAt: (meta && meta.uploaded_at) || null,
106
+ cacheBytes: cacheBytes.get(stamp) || 0,
107
+ plugins: meta && Number.isFinite(meta.plugins) ? meta.plugins : (lm && lm.plugins != null ? lm.plugins : null),
108
+ hostProfile: (lm && lm.hostProfile) || null,
109
+ pluginPacked: lm && Number.isFinite(lm.pluginPacked) ? lm.pluginPacked : null,
110
+ pluginProfiles: (lm && lm.pluginProfiles) || null,
111
+ });
112
+ }
113
+ items.sort((a, b) => (a.stamp < b.stamp ? 1 : -1));
114
+
115
+ const needMeta = items.filter((x) => x.inCloud && (x.files == null || x.bytes == null)).slice(0, 3);
116
+ if (needMeta.length) {
117
+ const patch = {};
118
+ for (const x of needMeta) {
119
+ try {
120
+ const got = await getRemoteManifest(x.stamp, { storeDir });
121
+ const man = got && got.manifest;
122
+ const st = (man && man.stats) || {};
123
+ const mFiles = Number.isFinite(st.files) ? st.files
124
+ : (man && Array.isArray(man.files) ? man.files.length : null);
125
+ const mBytes = Number.isFinite(st.bytes) ? st.bytes
126
+ : (man && Array.isArray(man.files)
127
+ ? man.files.reduce((n, f) => n + (Number.isFinite(f && f.size) ? f.size : 0), 0)
128
+ : null);
129
+ if (man && Number.isFinite(mFiles) && Number.isFinite(mBytes)) {
130
+ x.files = mFiles;
131
+ x.bytes = mBytes;
132
+ if (!x.createdAt && man.created_at) x.createdAt = man.created_at;
133
+ if (!x.uploadedAt && man.uploaded_at) x.uploadedAt = man.uploaded_at;
134
+ patch[x.stamp] = {
135
+ files: mFiles, bytes: mBytes,
136
+ created_at: man.created_at || x.createdAt || null,
137
+ uploaded_at: man.uploaded_at || x.uploadedAt || null,
138
+ };
139
+ }
140
+ } catch (e) { /* 读不到就算了 */ }
141
+ }
142
+ if (Object.keys(patch).length) { try { await upsertBackupIndex(patch); } catch (e) { /* 索引写失败不影响本次展示 */ } }
143
+ }
144
+
145
+ const totals = {
146
+ backups: items.filter((x) => x.state === "uploaded").length,
147
+ pending: items.filter((x) => x.state === "pending").length,
148
+ failed: items.filter((x) => x.state === "failed").length,
149
+ nosync: items.filter((x) => x.state === "unknown").length,
150
+ cloudBytes: items.reduce((n, x) => n + (x.state === "uploaded" && Number.isFinite(x.bytes) ? x.bytes : 0), 0),
151
+ cacheCopies: items.filter((x) => x.hasCache).length,
152
+ cacheBytes: items.reduce((n, x) => n + (x.cacheBytes || 0), 0),
153
+ };
154
+ return { ok: true, items, totals, cloudOk, cloudUnknown: !cloudOk,
155
+ cloud: { ok: cloudOk, error: cloud.error || null, errorKind: cloudKind,
156
+ cached: !cloudOk && knownStamps.size > 0, cachedAt: (knownCloud && knownCloud.cachedAt) || null },
157
+ index: { ok: !!idx.ok, error: idx.error || null } };
158
+ }
159
+
160
+ /**
161
+ */
162
+ export async function deleteBackup({ storeDir, stamp, dryRun = true, allowLast = false, logger = () => {} } = {}) {
163
+ storeDir = storeDir || storeDirOf();
164
+ if (!stamp) return { ok: false, error: "缺少 stamp" };
165
+ const hasCache = listSnapshots(storeDir).some((s) => s.stamp === stamp);
166
+ const cacheBytes = hasCache ? localCacheBytes(storeDir, stamp) : 0;
167
+
168
+ // 是不是“云端最后一份”:必须在真正删除之前算出来,否则保护形同虚设。
169
+ let lastCloudCopy = false;
170
+ try {
171
+ const cl = await listRemoteSnapshots({ fresh: true });
172
+ if (!cl || cl.ok === false) throw new Error((cl && cl.error) || "无法列举云端快照");
173
+ const stamps = new Set(((cl.snapshots) || []).filter((x) => x.hasManifest).map((x) => x.stamp));
174
+ lastCloudCopy = stamps.size <= 1 && stamps.has(stamp);
175
+ } catch (e) {
176
+ // 无法确认云端还剩几份时保守当作“最后一份”,避免误删唯一副本
177
+ lastCloudCopy = true;
178
+ logger("last-cloud-copy check failed: " + ((e && e.message) || e));
179
+ }
180
+ if (lastCloudCopy && !allowLast) {
181
+ return {
182
+ ok: false,
183
+ code: "lastCloudCopy",
184
+ error: "这是云端最后一份备份" + (dryRun ? ";确认要删请带 allowLast:true(会清空云端备份)" : ",已拒绝删除(需 allowLast:true)"),
185
+ lastCloudCopy: true,
186
+ dryRun,
187
+ hasCache,
188
+ cacheBytes,
189
+ };
190
+ }
191
+
192
+ const remote = await pruneRemoteSnapshots({
193
+ storeDir,
194
+ stamps: [stamp],
195
+ dryRun,
196
+ allowLocalCopy: true, // 新语义:本地就是缓存,和云一起删
197
+ allowLastCopy: allowLast, // 最后一份仍要显式放行
198
+ logger,
199
+ }).catch((e) => ({ ok: false, error: (e && e.message) || String(e) }));
200
+
201
+ const cloudRow = !!remote.ok ? (remote.plan || []).find((p) => p.stamp === stamp) || null : null;
202
+ // 快照体积:本地清单优先,其次用云端扫描结果(只存云端、本机无缓存时也要能报出真实大小)
203
+ const lman = hasCache ? (readManifest(manifestPath(storeDir, stamp)) || null) : null;
204
+ const sizeBytes = (lman && lman.stats && Number.isFinite(lman.stats.bytes)) ? lman.stats.bytes
205
+ : (cloudRow && Number.isFinite(cloudRow.bytes) ? cloudRow.bytes : null);
206
+ const plan = {
207
+ stamp,
208
+ cloud: cloudRow,
209
+ hasCache,
210
+ cacheBytes,
211
+ lastCloudCopy,
212
+ bytes: sizeBytes,
213
+ files: (lman && Array.isArray(lman.files)) ? lman.files.length
214
+ : (cloudRow && Number.isFinite(cloudRow.files) ? cloudRow.files : null),
215
+ };
216
+
217
+ // 本机与云端都不存在:直接报「不存在」,不要返回成功
218
+ const remoteRow = plan.cloud;
219
+ const remoteMissing = !!remoteRow && remoteRow.action === "skip" && /云端没有这份快照/.test(String(remoteRow.reason || ""));
220
+ if (!hasCache && !remoteRow) return { ok: false, code: "notFound", error: "快照不存在(本机与云端都没有):" + stamp, plan };
221
+ if (!hasCache && remoteMissing) return { ok: false, code: "notFound", error: "快照不存在(本机与云端都没有):" + stamp, plan };
222
+
223
+ const lastRefusal = !remote.ok && /最后一份/.test(String(remote.error || ""));
224
+ if (!remote.ok && !(dryRun && lastRefusal)) return { ok: false, error: remote.error || "删除云端失败", plan };
225
+ if (dryRun) {
226
+ return { ok: true, dryRun: true, plan: Object.assign({}, plan, { lastCloudCopy: plan.lastCloudCopy || lastRefusal, lastError: lastRefusal ? remote.error : null }), remote };
227
+ }
228
+
229
+ // 云端删成功(或本来就不在云端)→ 再清本地缓存。
230
+ // 本地删除与数据块回收必须与备份写入互斥(见 lib/engine/lock.js),
231
+ // 否则可能清掉备份刚写入、清单还没落盘的块。
232
+ const lock = acquireLock(storeDir);
233
+ if (!lock.ok) return busyResult(lock);
234
+ try {
235
+ let cacheDeleted = false;
236
+ if (hasCache) {
237
+ try { fs.rmSync(snapshotDir(storeDir, stamp), { recursive: true, force: true }); cacheDeleted = true; logger("removed local cache " + stamp); }
238
+ catch (e) { logger("remove cache failed: " + ((e && e.message) || e)); }
239
+ }
240
+
241
+ // 元数据索引同步抹掉这一项(失败不影响数据)
242
+ const idxDrop = await upsertBackupIndex({ [stamp]: null }).catch((e) => ({ ok: false, error: (e && e.message) || String(e) }));
243
+
244
+ // 缓存目录变了 → 重建索引 + 回收无主数据块
245
+ const { refs, broken } = rebuildIndex(storeDir, { provider: "aliyun" });
246
+ let gc = { deleted: 0, freed: 0, skipped: false };
247
+ if (broken && broken.length) gc.skipped = "broken-manifests";
248
+ else if (refs.size === 0) gc.skipped = "empty-refset";
249
+ else gc = gcBlobs(storeDir, refs);
250
+
251
+ return {
252
+ ok: true,
253
+ dryRun: false,
254
+ plan,
255
+ cacheDeleted,
256
+ gc,
257
+ index: { ok: !!idxDrop.ok, error: idxDrop.error || null },
258
+ remote,
259
+ };
260
+ } finally { releaseLock(lock); }
261
+ }
262
+
263
+ /**
264
+ */
265
+ export function pruneCache({ storeDir, keep = 5, cloudStamps = null } = {}) {
266
+ storeDir = storeDir || storeDirOf();
267
+ const n = Number.isFinite(Number(keep)) ? Math.max(0, Math.floor(Number(keep))) : 5;
268
+ if (!(cloudStamps instanceof Set)) {
269
+ return { ok: false, reason: "没有云端快照清单,跳过缓存回收(避免误删唯一副本)", deleted: [], freed: 0, kept: [], skipped: [] };
270
+ }
271
+ // 删本地缓存 + 回收数据块同样要和备份写入互斥(见 lib/engine/lock.js)
272
+ const lock = acquireLock(storeDir);
273
+ if (!lock.ok) return busyResult(lock);
274
+ try {
275
+ const snaps = listSnapshots(storeDir).filter((s) => fs.existsSync(manifestPath(storeDir, s.stamp)));
276
+ const deleted = [];
277
+ const kept = [];
278
+ const skipped = [];
279
+ let freed = 0;
280
+ snaps.forEach((s, i) => {
281
+ if (!cloudStamps.has(s.stamp)) { skipped.push(s.stamp); return; }
282
+ if (i < n) { kept.push(s.stamp); return; }
283
+ const size = localCacheBytes(storeDir, s.stamp);
284
+ try {
285
+ fs.rmSync(snapshotDir(storeDir, s.stamp), { recursive: true, force: true });
286
+ deleted.push(s.stamp);
287
+ freed += size;
288
+ } catch { kept.push(s.stamp); }
289
+ });
290
+ if (deleted.length) {
291
+ const { refs, broken } = rebuildIndex(storeDir, { provider: "aliyun" });
292
+ if (!broken.length && refs.size) {
293
+ const g = gcBlobs(storeDir, refs);
294
+ freed += g.freed || 0;
295
+ }
296
+ }
297
+ return { ok: true, deleted, freed, kept, skipped };
298
+ } finally { releaseLock(lock); }
299
+ }
300
+
301
+ /**
302
+ */
303
+ export async function uploadBackup({ storeDir, stamp, logger = () => {} } = {}) {
304
+ storeDir = storeDir || storeDirOf();
305
+ if (!stamp) return { ok: false, error: "缺少 stamp" };
306
+ const mf = manifestPath(storeDir, stamp);
307
+ if (!fs.existsSync(mf)) return { ok: false, error: "本机没有这份快照(可能已被清理)" };
308
+ const manifest = readManifest(mf);
309
+ if (!manifest) return { ok: false, error: "清单损坏,无法上传" };
310
+ const blobs = [...new Set([].concat(
311
+ (manifest.files || []).map((f) => f.hash),
312
+ (manifest.plugins || []).filter((p) => p && p.blob).map((p) => p.blob),
313
+ ))];
314
+ if (!blobs.length) return { ok: false, error: "这份快照没有文件" };
315
+ const r = await syncSnapshot({ manifest, blobs, storeDir, logger }).catch((e) => ({ ok: false, error: (e && e.message) || String(e) }));
316
+ return { ok: !!r.ok, uploaded: r.uploaded || 0, skipped: r.skipped || 0, error: r.error || null };
317
+ }
318
+
319
+ /** 只读:本地缓存总量 + 数据块数量(维护区展示用)。 */
320
+ export function cacheStats(storeDir) {
321
+ storeDir = storeDir || storeDirOf();
322
+ const snaps = listSnapshots(storeDir);
323
+ let bytes = 0;
324
+ for (const s of snaps) bytes += localCacheBytes(storeDir, s.stamp);
325
+ let blobs = 0;
326
+ try { blobs = listBlobs(storeDir).length; } catch { /* 忽略 */ }
327
+ return { copies: snaps.length, bytes, blobs };
328
+ }
329
+
330
+ /** 最近若干份「本地有、云端没有」的快照,失败优先;供备份后顺带重传。 */
331
+ export async function listRetryStamps({ storeDir, limit = 3, exclude = null } = {}) {
332
+ const n = Number.isFinite(limit) ? Math.max(0, Math.floor(limit)) : 0;
333
+ if (n === 0) return [];
334
+ const r = await listBackups({ storeDir });
335
+ if (!r.cloudOk) return [];
336
+ return r.items
337
+ .filter((x) => x.hasCache && x.stamp !== exclude && (x.state === "failed" || x.state === "pending"))
338
+ .sort((a, b) => (a.state === b.state ? (a.stamp < b.stamp ? 1 : -1) : (a.state === "failed" ? -1 : 1)))
339
+ .slice(0, n)
340
+ .map((x) => x.stamp);
341
+ }
@@ -0,0 +1,80 @@
1
+ // lib/engine/blob.js —— 内容寻址存储(sha256)
2
+
3
+ import crypto from "node:crypto";
4
+ import fs from "node:fs";
5
+ import path from "node:path";
6
+ import { assertHash } from "../util/validate.js";
7
+
8
+ export function blobPath(storeDir, hash) {
9
+ assertHash(hash, "blob hash"); // hash 未校验即拼路径 → 恶意清单可越界读写
10
+ return path.join(storeDir, "objects", "blobs", hash.slice(0, 2), hash.slice(2, 4), hash.slice(4));
11
+ }
12
+
13
+ export function sha256File(file) {
14
+ const h = crypto.createHash("sha256");
15
+ const fd = fs.openSync(file, "r");
16
+ try {
17
+ const buf = Buffer.allocUnsafe(1 << 20);
18
+ for (;;) {
19
+ const n = fs.readSync(fd, buf, 0, buf.length, null);
20
+ if (!n) break;
21
+ h.update(buf.subarray(0, n));
22
+ }
23
+ } finally { fs.closeSync(fd); }
24
+ return h.digest("hex");
25
+ }
26
+
27
+ /** 把一个文件纳入 blob 存储;已存在则只返回 created:false(去重命中)。 */
28
+ export function putBlob(storeDir, abs, { hash = null } = {}) {
29
+ const h = hash || sha256File(abs);
30
+ const dest = blobPath(storeDir, h);
31
+ if (fs.existsSync(dest)) return { hash: h, path: dest, created: false, size: fs.statSync(dest).size };
32
+ fs.mkdirSync(path.dirname(dest), { recursive: true });
33
+ const tmp = dest + ".tmp-" + process.pid + "-" + Math.random().toString(16).slice(2, 8);
34
+ fs.copyFileSync(abs, tmp);
35
+ const h2 = sha256File(tmp);
36
+ if (h2 !== h) { try { fs.unlinkSync(tmp); } catch {} throw new Error("blob hash mismatch after copy: " + abs); }
37
+ fs.renameSync(tmp, dest);
38
+ return { hash: h, path: dest, created: true, size: fs.statSync(dest).size };
39
+ }
40
+
41
+ export function listBlobs(storeDir) {
42
+ const root = path.join(storeDir, "objects", "blobs");
43
+ const out = [];
44
+ if (!fs.existsSync(root)) return out;
45
+ for (const d1 of fs.readdirSync(root, { withFileTypes: true })) {
46
+ if (!d1.isDirectory()) continue;
47
+ for (const d2 of fs.readdirSync(path.join(root, d1.name), { withFileTypes: true })) {
48
+ if (!d2.isDirectory()) continue;
49
+ for (const f of fs.readdirSync(path.join(root, d1.name, d2.name), { withFileTypes: true })) {
50
+ if (!f.isFile() || f.name.startsWith(".")) continue;
51
+ const full = path.join(root, d1.name, d2.name, f.name);
52
+ out.push({ hash: d1.name + d2.name + f.name, path: full, size: fs.statSync(full).size });
53
+ }
54
+ }
55
+ }
56
+ return out;
57
+ }
58
+
59
+ /** 删除未被任何清单引用的 blob。refSet 必须是 Set<string> 且非空(空集合 = 没有任何引用,视为误调用,拒绝执行)。 */
60
+ export function gcBlobs(storeDir, refSet, { dryRun = false } = {}) {
61
+ if (!(refSet instanceof Set)) throw new Error("gcBlobs: refSet must be a Set");
62
+ // 空集合必须拒绝:清单全部读不懂时若照常清理,会把所有 blob 当孤儿删掉
63
+ if (refSet.size === 0) throw new Error("gcBlobs: 空引用集合,拒绝回收(疑似清单读取失败)");
64
+ const all = listBlobs(storeDir);
65
+ const del = [];
66
+ let freed = 0;
67
+ for (const b of all) {
68
+ if (refSet.has(b.hash)) continue;
69
+ del.push(b);
70
+ freed += b.size;
71
+ if (!dryRun) { try { fs.unlinkSync(b.path); } catch {} }
72
+ }
73
+ for (const b of del) {
74
+ const d = path.dirname(b.path);
75
+ for (const dir of [d, path.dirname(d)]) {
76
+ try { if (fs.readdirSync(dir).length === 0) fs.rmdirSync(dir); } catch {}
77
+ }
78
+ }
79
+ return { deleted: del.length, freed, total: all.length, dryRun };
80
+ }
@@ -0,0 +1,148 @@
1
+ // lib/engine/collect.js —— 决定"备份什么",只读、不改源文件
2
+
3
+ import fs from "node:fs";
4
+ import path from "node:path";
5
+
6
+ export const PROFILE_FILES = ["package.json", "cordis.patch.yml", "cordis.yml", "pnpm-lock.yaml"];
7
+ export const ROOT_FILES = ["AGENTS.md", "prompt-inject.md", "composer-predict.json"];
8
+ export const PLUGIN_STATE_DIRS = [
9
+ ["our-free-model", /\.json$/i],
10
+ ["dsh-market", /\.json$/i],
11
+ ["dsh-purge-community", /\.json$/i],
12
+ ["dsh-ops", /\.json$/i],
13
+ ];
14
+
15
+ /**
16
+ */
17
+ export const PLUGIN_STATE_DENY = new Set([
18
+ "profiles", "sessions", "storages", "cache", "attachments", "logs", "skills", "skills-disabled",
19
+ "cloud-backup", "dsh-runtimes", "rea-mcp", "ghidra", "dist", "data", "node_modules",
20
+ ".keys", ".git", ".cache", ".dsh", ".tmp",
21
+ ]);
22
+
23
+ /** 名字像凭据的 json 不收(别把 token/账号类文件送到云盘);按「词段」匹配,避免误伤 printed-build-keys.json。 */
24
+ export const PLUGIN_STATE_SKIP = /(^|[._-])(tokens?|credentials?|secrets?|passwords?|passwd|cookies?|sessions?|licen[cs]es?|accounts?|auth)([._-]|\d|$)/i;
25
+
26
+ /** 自动发现目录的上限:文件数 / 累计字节,超了就跳过(宁缺勿滥)。 */
27
+ export const PLUGIN_STATE_LIMIT = { files: 40, bytes: 4 * 1024 * 1024 };
28
+
29
+ const HARD_EXCLUDE = [
30
+ /(^|[\\/])node_modules([\\/]|$)/i,
31
+ /(^|[\\/])\.keys([\\/]|$)/i,
32
+ /(^|[\\/])\.run-lock$/,
33
+ /\.tmp-\d+/,
34
+ /\.bak[-.]/i,
35
+ /\.log$/i,
36
+ /(^|[\\/])\.DS_Store$/,
37
+ ];
38
+
39
+ function excluded(rel) { return HARD_EXCLUDE.some((re) => re.test(rel)); }
40
+
41
+ /** 相对 DSH_HOME 的 posix 路径(skipped 记录用)。 */
42
+ function relOf(root, abs) { return path.relative(root, abs).split(path.sep).join("/"); }
43
+
44
+ function statSafe(p) { try { return fs.statSync(p); } catch { return null; } }
45
+ /** 只看链接本身、不跟随:用来识别符号链接与目录联接(junction)。 */
46
+ function lstatSafe(p) { try { return fs.lstatSync(p); } catch { return null; } }
47
+
48
+ export function collectFiles({ dshHome, config = {} } = {}) {
49
+ const maxFileBytes = config.maxFileBytes ?? 64 * 1024 * 1024;
50
+ const maxTotalBytes = config.maxTotalBytes ?? 3 * 1024 * 1024 * 1024;
51
+ const wsFilter = config.workspace && config.workspace !== "all" ? config.workspace : null;
52
+ const skipPlugins = !!config.skipPlugins;
53
+
54
+ const files = [];
55
+ const skipped = [];
56
+ let bytes = 0;
57
+
58
+ const add = (abs) => {
59
+ const rel = path.relative(dshHome, abs).split(path.sep).join("/");
60
+ if (excluded(rel)) { skipped.push({ rel, reason: "excluded" }); return; }
61
+ const lp = lstatSafe(abs);
62
+ if (lp && lp.isSymbolicLink()) { skipped.push({ rel, reason: "link" }); return; }
63
+ const st = statSafe(abs);
64
+ if (!st || !st.isFile()) return;
65
+ if (st.size > maxFileBytes) { skipped.push({ rel, reason: "too-large:" + st.size }); return; }
66
+ if (bytes + st.size > maxTotalBytes) { skipped.push({ rel, reason: "quota" }); return; }
67
+ files.push({ rel, abs, size: st.size, mtime: st.mtimeMs });
68
+ bytes += st.size;
69
+ };
70
+
71
+ const walk = (dir, depth = 0) => {
72
+ if (depth > 8) return;
73
+ let es = [];
74
+ try { es = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; }
75
+ for (const e of es) {
76
+ const p = path.join(dir, e.name);
77
+ // 符号链接与目录联接(junction)一律不跟随、不采集,记为 skipped,
78
+ // 避免把链接目标(可能在 DSH_HOME 之外)的内容静默当成普通文件收进备份。
79
+ if (e.isSymbolicLink()) { skipped.push({ rel: relOf(dshHome, p), reason: "link" }); continue; }
80
+ if (e.isDirectory()) { if (!excluded(path.relative(dshHome, p))) walk(p, depth + 1); }
81
+ else add(p);
82
+ }
83
+ };
84
+
85
+ const sroot = path.join(dshHome, "sessions");
86
+ for (const ws of readdirDirsLinked(sroot, skipped, dshHome)) {
87
+ if (wsFilter && ws !== wsFilter) continue;
88
+ walk(path.join(sroot, ws), 0);
89
+ }
90
+
91
+ // 2) storages(工作区列表、成本、会话投影缓存)
92
+ walk(path.join(dshHome, "storages"), 0);
93
+
94
+ for (const prof of readdirDirsLinked(path.join(dshHome, "profiles"), skipped, dshHome)) {
95
+ for (const f of PROFILE_FILES) add(path.join(dshHome, "profiles", prof, f));
96
+ }
97
+
98
+ for (const f of ROOT_FILES) add(path.join(dshHome, f));
99
+
100
+ // 5) 插件状态(只挑 json,别把它们的 downloads/backups 拖进来)
101
+ if (!skipPlugins) {
102
+ const dirs = new Map(PLUGIN_STATE_DIRS);
103
+ // 白名单之外,再自动发现一级目录(顶层有 json 且不在 DENY 里的,视为插件状态目录)
104
+ for (const name of readdirDirsLinked(dshHome, skipped, dshHome)) {
105
+ if (dirs.has(name) || PLUGIN_STATE_DENY.has(name)) continue;
106
+ const base = path.join(dshHome, name);
107
+ if (readdirFiles(base).some((f) => /\.json$/i.test(f))) dirs.set(name, /\.json$/i);
108
+ }
109
+ for (const [dir, re] of dirs) {
110
+ const base = path.join(dshHome, dir);
111
+ const picked = readdirFiles(base).filter((f) => re.test(f)).sort();
112
+ let pickedBytes = 0;
113
+ const staged = [];
114
+ for (const f of picked) {
115
+ const abs = path.join(base, f);
116
+ if (PLUGIN_STATE_SKIP.test(f)) { skipped.push({ rel: relOf(dshHome, abs), reason: "sensitive-name" }); continue; }
117
+ const st = statSafe(abs);
118
+ if (!st || !st.isFile()) continue;
119
+ if (staged.length >= PLUGIN_STATE_LIMIT.files || pickedBytes + st.size > PLUGIN_STATE_LIMIT.bytes) {
120
+ skipped.push({ rel: relOf(dshHome, abs), reason: "plugin-dir-limit" });
121
+ continue;
122
+ }
123
+ staged.push(abs);
124
+ pickedBytes += st.size;
125
+ }
126
+ for (const abs of staged) add(abs);
127
+ }
128
+ }
129
+
130
+ files.sort((a, b) => (a.rel < b.rel ? -1 : a.rel > b.rel ? 1 : 0));
131
+ return { files, skipped, stats: { count: files.length, bytes, skipped: skipped.length } };
132
+ }
133
+
134
+ /** 列子目录名;遇到符号链接/目录联接时记为 skipped(link),不跟随、不采集。 */
135
+ function readdirDirsLinked(dir, skipped, root) {
136
+ try {
137
+ const es = fs.readdirSync(dir, { withFileTypes: true });
138
+ const dirs = [];
139
+ for (const e of es) {
140
+ if (e.isSymbolicLink()) { skipped.push({ rel: relOf(root, path.join(dir, e.name)), reason: "link" }); continue; }
141
+ if (e.isDirectory()) dirs.push(e.name);
142
+ }
143
+ return dirs;
144
+ } catch { return []; }
145
+ }
146
+ function readdirFiles(dir) {
147
+ try { return fs.readdirSync(dir, { withFileTypes: true }).filter((e) => e.isFile()).map((e) => e.name); } catch { return []; }
148
+ }
@@ -0,0 +1,48 @@
1
+ /** 宿主识别:只问「哪个 profile、有没有 Electron 能力」;识别失败退化为 generic,只影响显示。 */
2
+ import fs from "node:fs";
3
+ import path from "node:path";
4
+
5
+ export function shapeOfProfile(dshHome, profile) {
6
+ if (!profile) return "generic";
7
+ if (profile === "desktop") return "desktop";
8
+ try {
9
+ const file = path.join(dshHome, "profiles", profile, "package.json");
10
+ const j = JSON.parse(fs.readFileSync(file, "utf8"));
11
+ const bundles = (j && j.dsh && j.dsh.profile && j.dsh.profile.bundles) || [];
12
+ return bundles.indexOf("@deepseek-ai/dsh-web-app") >= 0 ? "web" : "generic";
13
+ } catch {
14
+ return "generic";
15
+ }
16
+ }
17
+
18
+ export function profileFromArgv(argv, dshHome, env = {}) {
19
+ const list = Array.isArray(argv) ? argv : [];
20
+ for (let i = 0; i < list.length; i += 1) {
21
+ const a = String(list[i]);
22
+ if (a === "--profile" && list[i + 1]) return String(list[i + 1]);
23
+ if (a.indexOf("--profile=") === 0) return a.slice("--profile=".length);
24
+ }
25
+ if (env && env.DSH_PROFILE) return String(env.DSH_PROFILE);
26
+ const home = String(dshHome || "").replace(/\\/g, "/").replace(/\/+$/, "");
27
+ for (const raw of list) {
28
+ const s = String(raw).replace(/\\/g, "/");
29
+ if (home && s.indexOf(home + "/profiles/") === 0) {
30
+ const name = s.slice((home + "/profiles/").length).split("/")[0];
31
+ if (name) return name;
32
+ }
33
+ const m = /\/profiles\/([^/]+)\/?$/.exec(s);
34
+ if (m) return m[1];
35
+ }
36
+ return null;
37
+ }
38
+
39
+ export function detectHost({ dshHome = "", argv = [], env = {}, versions = {} } = {}) {
40
+ const profile = profileFromArgv(argv, dshHome, env) || null;
41
+ const electron = !!versions.electron || !!env.ELECTRON_RUN_AS_NODE;
42
+ return { profile: profile || null, shape: shapeOfProfile(dshHome, profile), electron: electron, node: versions.node || "" };
43
+ }
44
+
45
+ export function sameHost(a, b) {
46
+ if (!a || !b) return true;
47
+ return String(a.profile || "") === String(b.profile || "") && String(a.shape || "") === String(b.shape || "");
48
+ }