@marktrue/dsh-mobile-remote 3.2.4-alpha.2 → 3.2.4-alpha.20
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 +527 -0
- package/README.md +2 -0
- package/docs/03-api.md +328 -26
- package/docs/04-security.md +14 -0
- package/docs/09-compatibility.md +25 -4
- package/docs/10-release.md +33 -60
- package/docs/11-notification-reliability.md +45 -0
- package/docs/12-implementation-status.md +41 -0
- package/docs/13-device-acceptance-2026-11-03.md +37 -0
- package/docs/16-next-window-handoff-2026-11-04.md +44 -0
- package/docs/17-remaining-work-review-2026-11-04.md +53 -0
- package/docs/18-device-reliability-acceptance-2026-11-04.md +63 -0
- package/docs/19-issue-followup-2026-11-04.md +39 -0
- package/docs/20-dsh-022-compatibility-2026-11-05.md +32 -0
- package/lib/client.js +23 -1
- package/lib/endpoint-registry.js +32 -12
- package/lib/git-read-bundled-provider.js +27 -13
- package/lib/git-read-service.js +309 -38
- package/lib/host-manager-update.js +178 -0
- package/lib/host-update-capabilities.js +209 -0
- package/lib/index.js +3019 -145
- package/lib/npm-update-source.js +16 -16
- package/lib/plugin-self-update.js +127 -12
- package/lib/plugin-update-check.js +268 -0
- package/lib/plugin-update-controller.js +129 -7
- package/lib/plugin-update-transaction.js +38 -2
- package/lib/update-lock-arbiter.js +46 -6
- package/lib/update-supervisor.js +38 -5
- package/package.json +9 -6
package/lib/git-read-service.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
-
import { basename, isAbsolute, relative, resolve, sep } from "node:path";
|
|
2
|
+
import { basename, dirname, isAbsolute, relative, resolve, sep } from "node:path";
|
|
3
3
|
import { lstat, realpath } from "node:fs/promises";
|
|
4
4
|
import { createBundledGitReadProvider } from "./git-read-bundled-provider.js";
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
// task-28(issue #86 方案 B):GET 仍 ≤32;POST 支持 ≤128(URL 16KB 限制解除)。
|
|
7
|
+
const MAX_GRAPH_TIPS_GET = 32;
|
|
8
|
+
const MAX_GRAPH_TIPS_POST = 128;
|
|
9
|
+
const GRAPH_DEADLINE_MS = 25_000;
|
|
10
|
+
// issue #93(全部分支模式):refs/扫描/页大小。阈值必须高于正常仓库(211 分支)。
|
|
11
|
+
const GRAPH_ALL_PAGE_SIZE = 16;
|
|
12
|
+
const MAX_GRAPH_ALL_REFS = 4096;
|
|
13
|
+
const MAX_GRAPH_ALL_SCAN = 5000;
|
|
7
14
|
const MAX_TRACKED_FILES = 200;
|
|
8
15
|
const FILE_HISTORY_DEFAULT_LIMIT = 20;
|
|
9
16
|
const FILE_HISTORY_MAX_LIMIT = 100;
|
|
@@ -151,7 +158,7 @@ const historyStatus = (value) => {
|
|
|
151
158
|
};
|
|
152
159
|
|
|
153
160
|
/** Session-authorized read facade over an optional external or bundled provider. */
|
|
154
|
-
export function createGitReadService(ctx, { bundledProvider, now = () => Date.now(), snapshotTtlMs = 5 * 60_000, pollIntervalMs = 30_000, onChanged } = {}) {
|
|
161
|
+
export function createGitReadService(ctx, { bundledProvider, now = () => Date.now(), snapshotTtlMs = 5 * 60_000, pollIntervalMs = 30_000, onChanged, graphDeadlineMs = GRAPH_DEADLINE_MS, onSnapshotDisposed } = {}) {
|
|
155
162
|
const get = (name) => { try { return ctx.get?.(name); } catch { return undefined; } };
|
|
156
163
|
const external = get("gitReadProvider");
|
|
157
164
|
let externalAvailable = false;
|
|
@@ -209,6 +216,7 @@ export function createGitReadService(ctx, { bundledProvider, now = () => Date.no
|
|
|
209
216
|
for (const [id, item] of historyCursors) if (item.expiresAt <= now()) historyCursors.delete(id);
|
|
210
217
|
};
|
|
211
218
|
let pollTimer = null;
|
|
219
|
+
let pollInFlight = null;
|
|
212
220
|
let unsubscribeProvider = null;
|
|
213
221
|
const validOid = (value) => typeof value === "string" && /^(?:[0-9a-f]{40}|[0-9a-f]{64})$/i.test(value);
|
|
214
222
|
const safeFilePath = (value) => typeof value === "string" && value !== "" && !isAbsolute(value) && !/^(?:[A-Za-z]:[\\/]|\\\\|[a-z][a-z\d+.-]*:\/\/)/i.test(value) && !value.split(/[\\/]/).includes("..") && !value.includes("\0");
|
|
@@ -238,12 +246,140 @@ export function createGitReadService(ctx, { bundledProvider, now = () => Date.no
|
|
|
238
246
|
return Array.isArray(records) ? records : null;
|
|
239
247
|
} catch { return null; }
|
|
240
248
|
};
|
|
249
|
+
const graphTimeout = () => failure("graph-timeout", 504);
|
|
250
|
+
/** 单次 provider 调用的 25s 硬截止(provider 自身超时/挂起兜底)。 */
|
|
251
|
+
const graphRace = async (factory, deadline) => {
|
|
252
|
+
const remaining = Math.max(1, deadline - Date.now());
|
|
253
|
+
const call = Promise.resolve().then(factory);
|
|
254
|
+
let timer = null;
|
|
255
|
+
const timeout = new Promise((_, reject) => {
|
|
256
|
+
timer = setTimeout(() => reject(graphTimeout()), remaining);
|
|
257
|
+
});
|
|
258
|
+
try {
|
|
259
|
+
return await Promise.race([call, timeout]);
|
|
260
|
+
} finally {
|
|
261
|
+
if (timer) clearTimeout(timer);
|
|
262
|
+
// 超时后底层 promise 仍可能 settle:吞掉,避免 unhandledRejection。
|
|
263
|
+
void call.catch(() => {});
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
/** provider.refs 一次映射(name → commit oid);无 refs 能力/调用失败返回 null → 逐 tip 回退。 */
|
|
267
|
+
const graphRefsByName = async (root, deadline) => {
|
|
268
|
+
if (typeof provider.refs !== "function") return null;
|
|
269
|
+
try {
|
|
270
|
+
const records = await graphRace(
|
|
271
|
+
() => provider.refs(root, { timeoutMs: Math.max(1, deadline - Date.now()) }),
|
|
272
|
+
deadline,
|
|
273
|
+
);
|
|
274
|
+
if (!Array.isArray(records)) return null;
|
|
275
|
+
const byName = new Map();
|
|
276
|
+
for (const record of records) {
|
|
277
|
+
if (!record || typeof record.name !== "string") continue;
|
|
278
|
+
byName.set(record.name, validOid(record.commitOid) ? String(record.commitOid) : null);
|
|
279
|
+
}
|
|
280
|
+
return byName;
|
|
281
|
+
} catch (error) {
|
|
282
|
+
if (error?.code === "graph-timeout") throw error;
|
|
283
|
+
return null; // 外部 provider 无 refs/调用失败 → 逐 tip(兼容旧契约)
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
/** 批量解析新快照 tips:一次 refs 映射优先,缺项才逐 tip resolveTip。 */
|
|
287
|
+
const resolveGraphTips = async (root, selected, deadline) => {
|
|
288
|
+
const byName = await graphRefsByName(root, deadline);
|
|
289
|
+
const resolved = [];
|
|
290
|
+
for (const tip of selected) {
|
|
291
|
+
let oid;
|
|
292
|
+
if (byName && byName.has(tip.name)) {
|
|
293
|
+
oid = byName.get(tip.name);
|
|
294
|
+
if (!validOid(oid)) throw failure("graph-tip-invalid", 409);
|
|
295
|
+
} else {
|
|
296
|
+
try {
|
|
297
|
+
oid = await graphRace(() => provider.resolveTip(root, tip.name), deadline);
|
|
298
|
+
} catch (error) {
|
|
299
|
+
if (error?.code === "graph-timeout") throw error;
|
|
300
|
+
if (tip.name === "HEAD" && error?.code === "git-command-failed") continue;
|
|
301
|
+
throw failure("graph-tip-invalid", 409);
|
|
302
|
+
}
|
|
303
|
+
if (!validOid(oid)) throw failure("graph-tip-invalid", 409);
|
|
304
|
+
}
|
|
305
|
+
if (tip.tipOid != null && tip.tipOid.toLowerCase() !== oid.toLowerCase()) throw failure("graph-tip-invalid", 409);
|
|
306
|
+
resolved.push({ name: tip.name, tipOid: oid });
|
|
307
|
+
if (Date.now() > deadline) throw graphTimeout();
|
|
308
|
+
}
|
|
309
|
+
return resolved;
|
|
310
|
+
};
|
|
311
|
+
/** 续页/前后校验:一次 refs 映射比对 tips 未移动;失败抛 graph-stale。 */
|
|
312
|
+
const verifyGraphTips = async (root, tips, deadline) => {
|
|
313
|
+
const byName = await graphRefsByName(root, deadline);
|
|
314
|
+
for (const tip of tips) {
|
|
315
|
+
let current;
|
|
316
|
+
if (byName && byName.has(tip.name)) current = byName.get(tip.name);
|
|
317
|
+
else {
|
|
318
|
+
try {
|
|
319
|
+
current = await graphRace(() => provider.resolveTip(root, tip.name), deadline);
|
|
320
|
+
} catch (error) {
|
|
321
|
+
if (error?.code === "graph-timeout") throw error;
|
|
322
|
+
throw stale();
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (!validOid(current) || current.toLowerCase() !== tip.tipOid.toLowerCase()) throw stale();
|
|
326
|
+
if (Date.now() > deadline) throw graphTimeout();
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
/**
|
|
330
|
+
* issue #93:解析**全部分支**(refs/heads/* + refs/remotes/*,排除 /HEAD)。
|
|
331
|
+
* tags 不参与 tips;返回按 name 排序的 `{name, tipOid}`,供 provider.graph 使用。
|
|
332
|
+
* 超内部保护上限抛明确 graph-too-many-refs(绝不静默截断)。
|
|
333
|
+
*/
|
|
334
|
+
const resolveAllGraphRefs = async (root, deadline) => {
|
|
335
|
+
const byName = await graphRefsByName(root, deadline);
|
|
336
|
+
if (byName === null) throw failure("git-provider-unavailable", 503);
|
|
337
|
+
const refs = [];
|
|
338
|
+
for (const [name, oid] of byName) {
|
|
339
|
+
if (!name.startsWith("refs/heads/") && !name.startsWith("refs/remotes/")) continue;
|
|
340
|
+
if (name.endsWith("/HEAD")) continue;
|
|
341
|
+
if (!validOid(oid)) continue; // 非 commit 目标的 branch 不进图(与 provider 语义一致)
|
|
342
|
+
refs.push({ name, tipOid: String(oid) });
|
|
343
|
+
if (Date.now() > deadline) throw graphTimeout();
|
|
344
|
+
}
|
|
345
|
+
refs.sort((a, b) => compareText(a.name, b.name));
|
|
346
|
+
if (refs.length > MAX_GRAPH_ALL_REFS) {
|
|
347
|
+
throw failure("graph-too-many-refs", 400, `all branch refs exceed ${MAX_GRAPH_ALL_REFS}`);
|
|
348
|
+
}
|
|
349
|
+
return refs;
|
|
350
|
+
};
|
|
351
|
+
/** 全部分支快照指纹:name+oid 排序后 sha256;任一分支新增/移动/删除都会变。 */
|
|
352
|
+
const allRefsFingerprint = (refs) => createHash("sha256")
|
|
353
|
+
.update(refs.map((ref) => `${ref.name}:${ref.tipOid}`).join("\n"))
|
|
354
|
+
.digest("hex");
|
|
355
|
+
const verifyAllGraphRefs = async (snapshot, root, deadline) => {
|
|
356
|
+
const current = await resolveAllGraphRefs(root, deadline);
|
|
357
|
+
if (allRefsFingerprint(current) !== snapshot.refFingerprint) throw stale();
|
|
358
|
+
};
|
|
359
|
+
/** graph 专用:readRefRecords 纳入同一 absolute deadline(挂起→504,不返回半页)。 */
|
|
360
|
+
const readRefRecordsWithin = async (root, deadline) => {
|
|
361
|
+
if (typeof provider.refs !== "function") return null;
|
|
362
|
+
try {
|
|
363
|
+
const records = await graphRace(
|
|
364
|
+
() => provider.refs(root, { timeoutMs: Math.max(1, deadline - Date.now()) }),
|
|
365
|
+
deadline,
|
|
366
|
+
);
|
|
367
|
+
return Array.isArray(records) ? records : null;
|
|
368
|
+
} catch (error) {
|
|
369
|
+
if (error?.code === "graph-timeout") throw error;
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
};
|
|
241
373
|
const capabilities = () => {
|
|
242
374
|
const base = provider.capabilities?.() ?? { available: true, reason: null };
|
|
243
375
|
return {
|
|
244
376
|
contractVersion: "1.1.0", available: base.available !== false,
|
|
245
377
|
reason: ["git-provider-unavailable", "workspace-registry-unavailable"].includes(base.reason) ? base.reason : null,
|
|
246
378
|
readOnly: true, provider: provider === external ? "external" : "bundled",
|
|
379
|
+
// task-28:POST graph 的 tips 上限(旧宿主缺该字段,App 回退 5/5)。
|
|
380
|
+
graphTips: { max: MAX_GRAPH_TIPS_POST, getMax: MAX_GRAPH_TIPS_GET },
|
|
381
|
+
// issue #93:全部分支模式(all=true)与内部保护阈值;旧宿主缺该字段 → App 回退选择档。
|
|
382
|
+
graphAll: { pageSize: GRAPH_ALL_PAGE_SIZE, maxRefs: MAX_GRAPH_ALL_REFS, maxScan: MAX_GRAPH_ALL_SCAN },
|
|
247
383
|
features: {
|
|
248
384
|
repository: true, branches: true, graph: true, commit: true, worktree: true, preview: true,
|
|
249
385
|
// Both flags are independent and additive: refsDetail can be
|
|
@@ -263,7 +399,20 @@ export function createGitReadService(ctx, { bundledProvider, now = () => Date.no
|
|
|
263
399
|
// Windows-style absolute paths cannot be mapped on a POSIX host; report
|
|
264
400
|
// them as invalid instead of treating them as a relative filename.
|
|
265
401
|
if (/^(?:[A-Za-z]:[\\/]|\\\\)/.test(input) && !isAbsolute(input)) return null;
|
|
266
|
-
|
|
402
|
+
let candidate = isAbsolute(input) ? resolve(input) : resolve(root, input);
|
|
403
|
+
// Windows clients can send an 8.3 alias while the authorized repository
|
|
404
|
+
// uses its canonical long name. Canonicalize the nearest existing parent,
|
|
405
|
+
// preserving the final filename (a tracked symlink is itself a file).
|
|
406
|
+
let parent = dirname(candidate);
|
|
407
|
+
const tail = [basename(candidate)];
|
|
408
|
+
for (;;) {
|
|
409
|
+
try { candidate = resolve(await realpath(parent), ...tail); break; }
|
|
410
|
+
catch {
|
|
411
|
+
const next = dirname(parent);
|
|
412
|
+
if (next === parent) break;
|
|
413
|
+
tail.unshift(basename(parent)); parent = next;
|
|
414
|
+
}
|
|
415
|
+
}
|
|
267
416
|
const rel = relative(root, candidate);
|
|
268
417
|
if (rel === "") throw failure("path-invalid", 400);
|
|
269
418
|
if (rel === ".." || rel.startsWith(`..${sep}`) || isAbsolute(rel)) return null;
|
|
@@ -413,60 +562,162 @@ export function createGitReadService(ctx, { bundledProvider, now = () => Date.no
|
|
|
413
562
|
stats, files: { items, total: allFiles.length, nextCursor },
|
|
414
563
|
};
|
|
415
564
|
},
|
|
416
|
-
|
|
565
|
+
/**
|
|
566
|
+
* 图分页。task-28:`maxTips` 由 HTTP 层按传输方式传入(GET=32 / POST=128),
|
|
567
|
+
* 续页可省略 tips;snapshotId/cursor 与传输方式无关(GET/POST 互通)。
|
|
568
|
+
*/
|
|
569
|
+
async graph(sessionId, repositoryId, { tips, snapshotId, cursor, limit = 100, maxTips = MAX_GRAPH_TIPS_GET, all = false, focus } = {}) {
|
|
417
570
|
const authorized = await binding(sessionId, repositoryId);
|
|
418
571
|
prune();
|
|
419
|
-
const
|
|
572
|
+
const deadline = Date.now() + graphDeadlineMs;
|
|
573
|
+
const tipCap = maxTips === MAX_GRAPH_TIPS_POST ? MAX_GRAPH_TIPS_POST : MAX_GRAPH_TIPS_GET;
|
|
574
|
+
const allMode = all === true;
|
|
575
|
+
// issue #93:全部分支模式固定每页 16;5/10 选择集保持原 limit 语义。
|
|
576
|
+
const pageSize = allMode
|
|
577
|
+
? GRAPH_ALL_PAGE_SIZE
|
|
578
|
+
: Math.max(1, Math.min(200, Number(limit) || 100));
|
|
420
579
|
let snapshot, offset = 0;
|
|
580
|
+
let snapshotCreatedNow = false;
|
|
421
581
|
if (snapshotId !== undefined || cursor !== undefined) {
|
|
422
582
|
if (!snapshotId || !cursor) throw stale();
|
|
423
583
|
snapshot = snapshots.get(snapshotId);
|
|
424
584
|
if (!snapshot || snapshot.sessionId !== sessionId || snapshot.repositoryId !== repositoryId || snapshot.root !== authorized.root || snapshot.expiresAt <= now()) throw stale();
|
|
585
|
+
if (allMode !== (snapshot.all === true)) throw stale();
|
|
425
586
|
offset = snapshot.cursors.get(cursor);
|
|
426
587
|
if (!Number.isInteger(offset)) throw stale();
|
|
588
|
+
} else if (allMode) {
|
|
589
|
+
// 全部分支:忽略 tips 选择集,自行解析 refs/heads/* + refs/remotes/*。
|
|
590
|
+
const refs = await resolveAllGraphRefs(authorized.root, deadline);
|
|
591
|
+
if (refs.length === 0) throw failure("graph-tip-invalid", 400, "no branch refs");
|
|
592
|
+
const refFingerprint = allRefsFingerprint(refs);
|
|
593
|
+
let focusOid = null;
|
|
594
|
+
let focusIndex = null;
|
|
595
|
+
if (typeof focus === "string" && focus !== "") {
|
|
596
|
+
if (!validRef(focus) && !validOid(focus)) throw failure("graph-focus-invalid", 400);
|
|
597
|
+
if (validOid(focus)) {
|
|
598
|
+
focusOid = String(focus).toLowerCase();
|
|
599
|
+
} else {
|
|
600
|
+
const hit = refs.find((ref) => ref.name === focus);
|
|
601
|
+
if (!hit) throw failure("graph-focus-invalid", 400, "focus is not a branch ref");
|
|
602
|
+
focusOid = String(hit.tipOid).toLowerCase();
|
|
603
|
+
}
|
|
604
|
+
// 与图相同的 topo/date 顺序定位 focus;扫描上限明确报错,不静默截断。
|
|
605
|
+
const scan = await graphRace(
|
|
606
|
+
() => provider.graph(authorized.root, {
|
|
607
|
+
refs: refs.map((ref) => ref.tipOid),
|
|
608
|
+
offset: 0,
|
|
609
|
+
limit: MAX_GRAPH_ALL_SCAN,
|
|
610
|
+
timeoutMs: Math.max(1, deadline - Date.now()),
|
|
611
|
+
}),
|
|
612
|
+
deadline,
|
|
613
|
+
);
|
|
614
|
+
if (scan && scan.timedOut === true) throw graphTimeout();
|
|
615
|
+
const rows = Array.isArray(scan) ? scan : [];
|
|
616
|
+
focusIndex = rows.findIndex((row) => String(row?.oid ?? "").toLowerCase() === focusOid);
|
|
617
|
+
if (focusIndex < 0) throw failure("graph-focus-not-found", 409, "focus commit not found within scan limit");
|
|
618
|
+
}
|
|
619
|
+
const pageStart = focusIndex === null ? 0 : Math.floor(focusIndex / pageSize) * pageSize;
|
|
620
|
+
snapshot = {
|
|
621
|
+
id: randomUUID(), sessionId, repositoryId, root: authorized.root,
|
|
622
|
+
all: true, branchRefs: refs, refFingerprint,
|
|
623
|
+
tips: refs.map((ref) => ({ name: ref.name, tipOid: ref.tipOid })),
|
|
624
|
+
focusOid, focusIndex, pageStart,
|
|
625
|
+
expiresAt: now() + snapshotTtlMs, cursors: new Map(),
|
|
626
|
+
};
|
|
627
|
+
snapshotCreatedNow = true;
|
|
628
|
+
snapshots.set(snapshot.id, snapshot);
|
|
629
|
+
keepBounded(snapshots, 128);
|
|
630
|
+
offset = pageStart;
|
|
427
631
|
} else {
|
|
428
632
|
const selected = tips === undefined ? [{ name: "HEAD", tipOid: null }] : tips;
|
|
429
|
-
if (!Array.isArray(selected) || selected.length >
|
|
633
|
+
if (!Array.isArray(selected) || selected.length > tipCap) throw failure("graph-too-many-tips", 400, `tips limit is ${tipCap}`);
|
|
430
634
|
if (selected.length === 0) throw failure("graph-tip-invalid", 400);
|
|
431
635
|
const names = selected.map((tip) => tip?.name);
|
|
432
636
|
if (new Set(names).size !== names.length || names.some((name) => !validRef(name)) || selected.some((tip) => tip?.tipOid != null && !validOid(tip.tipOid))) throw failure("graph-tip-invalid", 400);
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
catch (error) {
|
|
438
|
-
if (tip.name === "HEAD" && error?.code === "git-command-failed") continue;
|
|
439
|
-
throw failure("graph-tip-invalid", 409);
|
|
440
|
-
}
|
|
441
|
-
if (!validOid(oid)) throw failure("graph-tip-invalid", 409);
|
|
442
|
-
if (tip.tipOid != null && tip.tipOid.toLowerCase() !== oid.toLowerCase()) throw failure("graph-tip-invalid", 409);
|
|
443
|
-
resolved.push({ name: tip.name, tipOid: oid });
|
|
444
|
-
}
|
|
445
|
-
snapshot = { id: randomUUID(), sessionId, repositoryId, root: authorized.root, tips: resolved, expiresAt: now() + snapshotTtlMs, cursors: new Map() };
|
|
637
|
+
// P0 批量解析:128 tips 只做一次 provider.refs(外部 provider 无 refs 才逐 tip)。
|
|
638
|
+
const resolved = await resolveGraphTips(authorized.root, selected, deadline);
|
|
639
|
+
snapshot = { id: randomUUID(), sessionId, repositoryId, root: authorized.root, all: false, tips: resolved, expiresAt: now() + snapshotTtlMs, cursors: new Map(), pageStart: 0 };
|
|
640
|
+
snapshotCreatedNow = true;
|
|
446
641
|
snapshots.set(snapshot.id, snapshot);
|
|
447
642
|
keepBounded(snapshots, 128);
|
|
448
643
|
}
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
644
|
+
// 统一 snapshot 清理策略(re-review P1):
|
|
645
|
+
// - 纯 graph-timeout:本次请求**新建且尚未交付**的 initial 快照可删;
|
|
646
|
+
// 已交付/续页快照保留至 TTL,同 cursor 可重试;
|
|
647
|
+
// - graph-stale / ref 变化 / 鉴权失败等确定性失效:立即删除。
|
|
648
|
+
// 该策略覆盖 pre/post verify、provider.graph、装饰 readRefRecords 所有阶段。
|
|
649
|
+
const disposeSnapshot = () => {
|
|
650
|
+
if (snapshots.delete(snapshot.id)) onSnapshotDisposed?.(snapshot.id);
|
|
651
|
+
};
|
|
652
|
+
const disposeOnGraphFailure = (error) => {
|
|
653
|
+
if (error?.code === "graph-timeout") {
|
|
654
|
+
if (snapshotCreatedNow) disposeSnapshot();
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
disposeSnapshot();
|
|
658
|
+
};
|
|
659
|
+
try {
|
|
660
|
+
// 全部分支:指纹覆盖全部分支 refs(新增/移动/删除都 stale);5/10:只校验选择集。
|
|
661
|
+
if (snapshot.all === true) await verifyAllGraphRefs(snapshot, authorized.root, deadline);
|
|
662
|
+
else await verifyGraphTips(authorized.root, snapshot.tips, deadline);
|
|
663
|
+
} catch (error) {
|
|
664
|
+
disposeOnGraphFailure(error);
|
|
665
|
+
throw error;
|
|
453
666
|
}
|
|
667
|
+
const graphRefs = snapshot.all === true
|
|
668
|
+
? snapshot.branchRefs.map((ref) => ref.tipOid)
|
|
669
|
+
: snapshot.tips.map((tip) => tip.tipOid);
|
|
454
670
|
let rawCommits;
|
|
455
|
-
try {
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
671
|
+
try {
|
|
672
|
+
rawCommits = await graphRace(
|
|
673
|
+
() => provider.graph(authorized.root, {
|
|
674
|
+
refs: graphRefs,
|
|
675
|
+
offset,
|
|
676
|
+
limit: pageSize,
|
|
677
|
+
timeoutMs: Math.max(1, deadline - Date.now()),
|
|
678
|
+
}),
|
|
679
|
+
deadline,
|
|
680
|
+
);
|
|
681
|
+
// provider 自报 timedOut(不得把半页当完整页返回)。
|
|
682
|
+
if (rawCommits && rawCommits.timedOut === true) throw graphTimeout();
|
|
683
|
+
} catch (error) {
|
|
684
|
+
// 超时是明确失败(504 graph-timeout),不做 stale 转换(否则面板看不到)。
|
|
685
|
+
// 已交付续页快照保留;非确定性 provider 错误保留快照供重试(verify 失败才 stale)。
|
|
686
|
+
if (error?.code === "graph-timeout") {
|
|
687
|
+
disposeOnGraphFailure(error);
|
|
688
|
+
throw error;
|
|
689
|
+
}
|
|
690
|
+
try {
|
|
691
|
+
if (snapshot.all === true) await verifyAllGraphRefs(snapshot, authorized.root, deadline);
|
|
692
|
+
else await verifyGraphTips(authorized.root, snapshot.tips, deadline);
|
|
693
|
+
} catch (verifyError) {
|
|
694
|
+
// 混合错误:provider.graph 先抛临时 503,随后为判 stale 的二次
|
|
695
|
+
// verify 因 deadline 挂起 —— 必须原样透传 504,并按统一策略
|
|
696
|
+
// (已交付保留 / 未交付新建清理),不能误判成 ref 真变化的 stale。
|
|
697
|
+
if (verifyError?.code === "graph-timeout") {
|
|
698
|
+
disposeOnGraphFailure(verifyError);
|
|
699
|
+
throw verifyError;
|
|
700
|
+
}
|
|
701
|
+
disposeSnapshot();
|
|
702
|
+
throw stale();
|
|
461
703
|
}
|
|
462
704
|
throw error;
|
|
463
705
|
}
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
706
|
+
try {
|
|
707
|
+
if (snapshot.all === true) await verifyAllGraphRefs(snapshot, authorized.root, deadline);
|
|
708
|
+
else await verifyGraphTips(authorized.root, snapshot.tips, deadline);
|
|
709
|
+
} catch (error) {
|
|
710
|
+
disposeOnGraphFailure(error);
|
|
711
|
+
throw error;
|
|
712
|
+
}
|
|
713
|
+
let records;
|
|
714
|
+
try {
|
|
715
|
+
records = await readRefRecordsWithin(authorized.root, deadline);
|
|
716
|
+
} catch (error) {
|
|
717
|
+
// 装饰(refs 记录)阶段超时同样按统一策略处理:已交付快照可重试。
|
|
718
|
+
disposeOnGraphFailure(error);
|
|
719
|
+
throw error;
|
|
468
720
|
}
|
|
469
|
-
const records = await readRefRecords(authorized.root);
|
|
470
721
|
const commits = (Array.isArray(rawCommits) ? rawCommits : []).map((row) => {
|
|
471
722
|
const oid = String(row.oid ?? "");
|
|
472
723
|
const legacy = Array.isArray(row.refs) ? row.refs.map(String) : [];
|
|
@@ -482,13 +733,28 @@ export function createGitReadService(ctx, { bundledProvider, now = () => Date.no
|
|
|
482
733
|
refs: legacy, refsDetail: refDetails.refsDetail, tagsDetail: refDetails.tagsDetail,
|
|
483
734
|
};
|
|
484
735
|
});
|
|
736
|
+
// issue #93:双向分页。nextCursor 更早(offset+本页),prevCursor 更新(offset-页)。
|
|
485
737
|
let nextCursor = null;
|
|
486
738
|
if (commits.length === pageSize) {
|
|
487
739
|
nextCursor = randomUUID();
|
|
488
740
|
snapshot.cursors.set(nextCursor, offset + commits.length);
|
|
489
|
-
keepBounded(snapshot.cursors, 128);
|
|
490
741
|
}
|
|
491
|
-
|
|
742
|
+
let prevCursor = null;
|
|
743
|
+
if (offset > 0) {
|
|
744
|
+
prevCursor = randomUUID();
|
|
745
|
+
snapshot.cursors.set(prevCursor, Math.max(0, offset - pageSize));
|
|
746
|
+
}
|
|
747
|
+
keepBounded(snapshot.cursors, 256);
|
|
748
|
+
return {
|
|
749
|
+
repositoryId, snapshotId: snapshot.id, tips: snapshot.tips, commits,
|
|
750
|
+
nextCursor, prevCursor,
|
|
751
|
+
all: snapshot.all === true,
|
|
752
|
+
pageSize,
|
|
753
|
+
pageStart: offset,
|
|
754
|
+
focusOid: snapshot.all === true ? snapshot.focusOid : null,
|
|
755
|
+
hasNewer: offset > 0,
|
|
756
|
+
hasOlder: nextCursor !== null,
|
|
757
|
+
};
|
|
492
758
|
},
|
|
493
759
|
async files(sessionId, repositoryId, { q, limit } = {}) {
|
|
494
760
|
const authorized = await binding(sessionId, repositoryId);
|
|
@@ -552,7 +818,9 @@ export function createGitReadService(ctx, { bundledProvider, now = () => Date.no
|
|
|
552
818
|
}
|
|
553
819
|
return { repositoryId, path: normalized, entries: items, nextCursor, truncated: snapshot.truncated };
|
|
554
820
|
},
|
|
555
|
-
|
|
821
|
+
_pollOnce() {
|
|
822
|
+
if (pollInFlight) return pollInFlight;
|
|
823
|
+
pollInFlight = (async () => {
|
|
556
824
|
for (const [repositoryId, authorized] of bindings) {
|
|
557
825
|
try {
|
|
558
826
|
await binding(authorized.sessionId, repositoryId);
|
|
@@ -563,6 +831,8 @@ export function createGitReadService(ctx, { bundledProvider, now = () => Date.no
|
|
|
563
831
|
}
|
|
564
832
|
} catch { bindings.delete(repositoryId); refSignatures.delete(repositoryId); }
|
|
565
833
|
}
|
|
834
|
+
})().finally(() => { pollInFlight = null; });
|
|
835
|
+
return pollInFlight;
|
|
566
836
|
},
|
|
567
837
|
start() {
|
|
568
838
|
if (unsubscribeProvider || pollTimer) return;
|
|
@@ -595,6 +865,7 @@ export function createGitReadService(ctx, { bundledProvider, now = () => Date.no
|
|
|
595
865
|
pollTimer = null;
|
|
596
866
|
try { unsubscribeProvider?.(); } catch { /* optional provider teardown must not break host shutdown */ }
|
|
597
867
|
unsubscribeProvider = null;
|
|
868
|
+
return pollInFlight ?? Promise.resolve();
|
|
598
869
|
},
|
|
599
870
|
};
|
|
600
871
|
return service;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// 通过宿主原生插件管理器执行更新(DSH 0.2.2+):不重启宿主。
|
|
2
|
+
//
|
|
3
|
+
// 与 `plugin-self-update.js` 那条「改依赖 → pnpm install → 重启 → 健康确认」的传统
|
|
4
|
+
// 通道并列;本模块只在宿主提供 `pluginManager.updateBundle()` 时使用。
|
|
5
|
+
//
|
|
6
|
+
// 为什么优先用它:
|
|
7
|
+
// 1. **不重启**:宿主在 HMR 可用时移除旧版本、装新版本并在进程内重新组合 profile,
|
|
8
|
+
// 插件被重新加载而宿主进程不变——手机与桌面都不掉连接。
|
|
9
|
+
// 2. **判定权交给宿主**:registry 选择、pnpm 配置、凭据、兼容性豁免、构建脚本批准
|
|
10
|
+
// 全部由宿主那一套既有实现决定,我们不重复实现、也不绕过它的安全检查。
|
|
11
|
+
// 3. **失败即回滚**:宿主在装新版本失败/取消时会把旧版本装回去,并把结果如实报出来。
|
|
12
|
+
//
|
|
13
|
+
// 本模块不做的事:不自己跑 pnpm、不改 profile 文件、不做健康判定(宿主的
|
|
14
|
+
// `application` 字段就是结论)。
|
|
15
|
+
import { compareVersion, parseVersion } from "./plugin-update.js";
|
|
16
|
+
import { HOST_MANAGER_REASON, UPDATE_CHANNEL } from "./host-update-capabilities.js";
|
|
17
|
+
|
|
18
|
+
/** 宿主更新失败的稳定原因码(对外只暴露这一类,不含宿主异常原文)。 */
|
|
19
|
+
export const HOST_MANAGER_ERROR = {
|
|
20
|
+
/** 探测阶段就不可用。 */
|
|
21
|
+
UNAVAILABLE: "host-manager-unavailable",
|
|
22
|
+
/** 目标版本不是合法 semver。 */
|
|
23
|
+
VERSION_INVALID: "host-manager-version-invalid",
|
|
24
|
+
/** 目标版本不高于已安装版本(宿主会拒绝,这里提前挡)。 */
|
|
25
|
+
VERSION_NOT_NEWER: "host-manager-version-not-newer",
|
|
26
|
+
/** 宿主拒绝(`ChangeResult.application === 'failed'`)。 */
|
|
27
|
+
REJECTED: "host-manager-rejected",
|
|
28
|
+
/** 用户/调用方取消。 */
|
|
29
|
+
CANCELLED: "host-manager-cancelled",
|
|
30
|
+
/** 装上了但需要重启才生效。 */
|
|
31
|
+
RESTART_REQUIRED: "host-manager-restart-required",
|
|
32
|
+
/** 调用 itself 抛错。 */
|
|
33
|
+
CALL_FAILED: "host-manager-call-failed",
|
|
34
|
+
/** 被替代/覆盖(同一插件条目上有更高优先级的覆盖)。 */
|
|
35
|
+
OVERRIDDEN: "host-manager-overridden",
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* 把宿主的 `ChangeResult`(以及 `updateBundle` 外层结果)压成我们自己的稳定结论。
|
|
40
|
+
*
|
|
41
|
+
* 宿主字段语义(见 dsh-plugin-manager 的 `ChangeResult` / `PluginUpdateResult`):
|
|
42
|
+
* - `application: 'applied'` → 已生效,无需重启;
|
|
43
|
+
* - `application: 'restart-required'` → 装了但下次启动才生效;
|
|
44
|
+
* - `application: 'failed' | 'cancelled'` → 未生效(`restore` 存在时表示旧版本已装回);
|
|
45
|
+
* - `application: 'overridden'` → 被其它条目覆盖。
|
|
46
|
+
*
|
|
47
|
+
* @param {object|undefined} outcome - `updateBundle()` 的返回值。
|
|
48
|
+
* @returns {{ok: boolean, reason: string, restored: boolean, from: string, to: string, step: string, detail: string}}
|
|
49
|
+
*/
|
|
50
|
+
export function interpretHostManagerUpdate(outcome) {
|
|
51
|
+
const from = typeof outcome?.from === "string" ? outcome.from : "";
|
|
52
|
+
const to = typeof outcome?.to === "string" ? outcome.to : "";
|
|
53
|
+
const step = typeof outcome?.step === "string" ? outcome.step : "";
|
|
54
|
+
const base = { from, to, step, restored: Boolean(outcome?.restore) };
|
|
55
|
+
// `updateBundle` 在「检查阶段就拒绝」时也可能没有 result。
|
|
56
|
+
const result = outcome?.result;
|
|
57
|
+
if (!result || typeof result !== "object") {
|
|
58
|
+
const reason = typeof result === "string" ? result : "";
|
|
59
|
+
return { ...base, ok: false, reason: HOST_MANAGER_ERROR.REJECTED, detail: reason };
|
|
60
|
+
}
|
|
61
|
+
switch (result.application) {
|
|
62
|
+
case "applied":
|
|
63
|
+
return { ...base, ok: true, reason: "", detail: "" };
|
|
64
|
+
case "restart-required":
|
|
65
|
+
return { ...base, ok: false, reason: HOST_MANAGER_ERROR.RESTART_REQUIRED, detail: String(result.restartReason ?? "") };
|
|
66
|
+
case "cancelled":
|
|
67
|
+
return { ...base, ok: false, reason: HOST_MANAGER_ERROR.CANCELLED, detail: "" };
|
|
68
|
+
case "overridden":
|
|
69
|
+
return { ...base, ok: false, reason: HOST_MANAGER_ERROR.OVERRIDDEN, detail: "" };
|
|
70
|
+
case "failed":
|
|
71
|
+
default: {
|
|
72
|
+
// 宿主的 error 是结构化对象(code/diagnostic);只取 code,绝不透传 diagnostic 原文
|
|
73
|
+
// (可能含 registry URL、凭据或缺路径)。
|
|
74
|
+
const code = typeof result.error?.code === "string" ? result.error.code : "";
|
|
75
|
+
return { ...base, ok: false, reason: HOST_MANAGER_ERROR.REJECTED, detail: code };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* 校验目标版本可以交给宿主更新。
|
|
82
|
+
*
|
|
83
|
+
* 宿主自己也会校验(`semver.gt`),这里提前挡是为了给出**我们自己的稳定原因码**,
|
|
84
|
+
* 而不是把宿主的英文 diagnostic 抛给手机。
|
|
85
|
+
*
|
|
86
|
+
* @param {object} input
|
|
87
|
+
* @param {string} input.targetVersion - 目标版本。
|
|
88
|
+
* @param {string} input.installedVersion - 当前已安装版本。
|
|
89
|
+
* @returns {{ok: boolean, reason: string}}
|
|
90
|
+
*/
|
|
91
|
+
export function validateHostManagerTarget({ targetVersion, installedVersion } = {}) {
|
|
92
|
+
if (!parseVersion(targetVersion)) return { ok: false, reason: HOST_MANAGER_ERROR.VERSION_INVALID };
|
|
93
|
+
if (installedVersion) {
|
|
94
|
+
const comparison = compareVersion(targetVersion, installedVersion);
|
|
95
|
+
// 比较不出来时放行给宿主判定:我们的比较器只认 `X.Y.Z[-pre][+N]`,
|
|
96
|
+
// 而宿主用的是完整 semver——不因为我们的解析面更窄就误拒。
|
|
97
|
+
if (comparison !== null && comparison <= 0) {
|
|
98
|
+
return { ok: false, reason: HOST_MANAGER_ERROR.VERSION_NOT_NEWER };
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return { ok: true, reason: "" };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* 通过宿主插件管理器更新本插件。
|
|
106
|
+
*
|
|
107
|
+
* 执行顺序刻意固定:
|
|
108
|
+
* 1. 探测结论必须可用(`probe.channel === host-manager`),否则直接拒绝;
|
|
109
|
+
* 2. 校验目标版本;
|
|
110
|
+
* 3. 调 `updateBundle(name, version, { registry?, requestId? })`;
|
|
111
|
+
* 4. 把结果压成稳定结论。
|
|
112
|
+
*
|
|
113
|
+
* 不在这里做重试:宿主的 `updateBundle` 内部已经处理了 registry 回退与失败回滚,
|
|
114
|
+
* 我们外加一层重试只会让「已经回滚过的事」再发生一次。
|
|
115
|
+
*
|
|
116
|
+
* @param {object} input
|
|
117
|
+
* @param {object} input.service - `pluginManager` 服务实例。
|
|
118
|
+
* @param {string} input.packageName - 本插件包名。
|
|
119
|
+
* @param {string} input.targetVersion - 目标版本。
|
|
120
|
+
* @param {string} [input.installedVersion] - 当前版本(用于提前挡降级)。
|
|
121
|
+
* @param {string} [input.registry] - 指定 registry;缺省由宿主按配置决定。
|
|
122
|
+
* @param {string} [input.requestId] - 宿主用它关联进度与日志。
|
|
123
|
+
* @param {(progress: object) => void} [input.onProgress] - 宿主 `update-state` 事件的转发。
|
|
124
|
+
* @param {object} [input.log] - 结构化日志(注入以便测试)。
|
|
125
|
+
* @returns {Promise<object>} 稳定结论。
|
|
126
|
+
*/
|
|
127
|
+
export async function runHostManagerUpdate({
|
|
128
|
+
service,
|
|
129
|
+
packageName,
|
|
130
|
+
targetVersion,
|
|
131
|
+
installedVersion = "",
|
|
132
|
+
registry,
|
|
133
|
+
requestId,
|
|
134
|
+
onProgress,
|
|
135
|
+
log = {},
|
|
136
|
+
} = {}) {
|
|
137
|
+
if (!service || typeof service.updateBundle !== "function") {
|
|
138
|
+
return { ok: false, channel: UPDATE_CHANNEL.HOST_MANAGER, reason: HOST_MANAGER_REASON.SERVICE_MISSING, from: "", to: String(targetVersion ?? ""), step: "", restored: false };
|
|
139
|
+
}
|
|
140
|
+
if (typeof packageName !== "string" || packageName === "") {
|
|
141
|
+
return { ok: false, channel: UPDATE_CHANNEL.HOST_MANAGER, reason: HOST_MANAGER_REASON.NOT_UPDATABLE, from: "", to: String(targetVersion ?? ""), step: "", restored: false };
|
|
142
|
+
}
|
|
143
|
+
const validated = validateHostManagerTarget({ targetVersion, installedVersion });
|
|
144
|
+
if (!validated.ok) {
|
|
145
|
+
return { ok: false, channel: UPDATE_CHANNEL.HOST_MANAGER, reason: validated.reason, from: String(installedVersion ?? ""), to: String(targetVersion ?? ""), step: "check", restored: false };
|
|
146
|
+
}
|
|
147
|
+
const options = {};
|
|
148
|
+
// 只在调用方明确给了 registry 时才传:缺省要比「传 undefined」更明确地走宿主配置。
|
|
149
|
+
if (typeof registry === "string" && registry !== "") options.registry = registry;
|
|
150
|
+
if (typeof requestId === "string" && requestId !== "") options.requestId = requestId;
|
|
151
|
+
let outcome;
|
|
152
|
+
try {
|
|
153
|
+
log.stage?.("host-manager", `Updating ${packageName} to ${targetVersion} through the host plugin manager`);
|
|
154
|
+
outcome = await service.updateBundle(packageName, targetVersion, Object.keys(options).length > 0 ? options : undefined);
|
|
155
|
+
} catch (error) {
|
|
156
|
+
// 宿主在锁超时/服务销毁时是 reject 而不是返回失败结果;这仍然是一次失败结论。
|
|
157
|
+
log.fail?.("host-manager", "Host plugin manager update call rejected");
|
|
158
|
+
return {
|
|
159
|
+
ok: false,
|
|
160
|
+
channel: UPDATE_CHANNEL.HOST_MANAGER,
|
|
161
|
+
reason: HOST_MANAGER_ERROR.CALL_FAILED,
|
|
162
|
+
from: String(installedVersion ?? ""),
|
|
163
|
+
to: String(targetVersion ?? ""),
|
|
164
|
+
step: "",
|
|
165
|
+
restored: false,
|
|
166
|
+
detail: typeof error?.code === "string" ? error.code : "",
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
const interpreted = interpretHostManagerUpdate(outcome);
|
|
170
|
+
if (typeof onProgress === "function") {
|
|
171
|
+
try {
|
|
172
|
+
onProgress({ step: interpreted.step, from: interpreted.from, to: interpreted.to });
|
|
173
|
+
} catch {
|
|
174
|
+
// 进度回调不应影响更新结论
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return { ...interpreted, channel: UPDATE_CHANNEL.HOST_MANAGER };
|
|
178
|
+
}
|