@refeain/dsh-xdpool 3.1.1 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/lib/bin.js +48 -0
  2. package/lib/client.js +1106 -259
  3. package/lib/index.js +592 -76
  4. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -5915,11 +5915,41 @@ var MarvisHostAdapter = class extends LlmAdapter {
5915
5915
  * stable per machine, so a re-login does not produce a new device, and it needs no binary — the
5916
5916
  * reference implementation once shipped four DLLs to sign this and no longer does.
5917
5917
  *
5918
+ * WHEN THERE IS NO USER (F7)
5919
+ *
5920
+ * `os.userInfo()` THROWS rather than returning blanks on a platform where the running user has
5921
+ * no entry — a service context, a container, a stripped-down image. 3.2.0 called it unguarded
5922
+ * inside the derivation, so `deviceGuid()` threw from three call sites, and the failure read as
5923
+ * whatever the caller did with an uncaught exception. Worse, the obvious "fix" of catching it
5924
+ * and substituting `Date.now()` or `Math.random()` would mint a NEW device on every call, which
5925
+ * the upstream treats as a fleet of new machines.
5926
+ *
5927
+ * So the fallback is a SECOND STABLE INPUT, not a random one: the same machine+user environment
5928
+ * variables the platform sets. Their stable boundary is written down at {@link environmentUser}
5929
+ * — they are per-machine and per-login, not per-process, which is exactly the granularity a
5930
+ * device id needs. When even those are absent, this module says so in a diagnosable way instead
5931
+ * of inventing an identity: {@link deviceIdentity} returns a problem, and each call site decides
5932
+ * how to report it.
5933
+ *
5918
5934
  * @module dsh-xdpool/marvis-device
5919
5935
  */
5920
5936
  /** The registry value the client writes. */
5921
5937
  const SVID_KEY = "HKCU\\SOFTWARE\\Tencent\\Marvis\\Env";
5922
- /** Cached: the lookup runs a synchronous `reg query`, and this is called per request. */
5938
+ /** Thrown by {@link deviceGuid} when no stable identity can be derived. */
5939
+ var DeviceIdUnavailableError = class extends Error {
5940
+ problem;
5941
+ constructor(problem) {
5942
+ super(problem);
5943
+ this.problem = problem;
5944
+ this.name = "DeviceIdUnavailableError";
5945
+ }
5946
+ };
5947
+ /**
5948
+ * Cached: the lookup runs a synchronous `reg query`, and this is called per request.
5949
+ *
5950
+ * Failures are cached too. A failing derivation would otherwise pay the 3s `reg.exe` timeout on
5951
+ * every single request, and the inputs cannot change inside one process lifetime anyway.
5952
+ */
5923
5953
  let cached$1;
5924
5954
  /** The registry value, or undefined when the client is not installed. */
5925
5955
  function fromRegistry() {
@@ -5940,22 +5970,121 @@ function fromRegistry() {
5940
5970
  return;
5941
5971
  }
5942
5972
  }
5973
+ /** `hostname()` without the throw: it is a syscall and can fail in a locked-down runtime. */
5974
+ function safeHostname() {
5975
+ try {
5976
+ const value = hostname();
5977
+ return value === "" ? void 0 : value;
5978
+ } catch {
5979
+ return;
5980
+ }
5981
+ }
5982
+ /** The username from `os.userInfo()`, or why it could not be read. */
5983
+ function systemUser() {
5984
+ try {
5985
+ const username = userInfo().username;
5986
+ return username === "" ? { problem: "userInfo() 返回了空用户名" } : { username };
5987
+ } catch (cause) {
5988
+ return { problem: `userInfo() 读取失败:${cause instanceof Error ? cause.message : String(cause)}` };
5989
+ }
5990
+ }
5991
+ /**
5992
+ * The fallback user input, from the environment.
5993
+ *
5994
+ * STABLE BOUNDARY — what makes this safe to substitute for `os.userInfo()`:
5995
+ *
5996
+ * - All of these are set once per LOGIN by the OS/terminal, not per process, so repeated calls
5997
+ * inside one run and across restarts on the same machine+user agree.
5998
+ * - They are read in a fixed order, so an environment that sets several always derives the
5999
+ * same one. Adding a name to the front of this list WOULD change the derived id for machines
6000
+ * whose userInfo also fails — that is a last-resort path, but worth knowing.
6001
+ * - They can legitimately be absent (a Windows service with a scrubbed environment, a
6002
+ * container running as an unnamed uid). That is the case this module reports instead of
6003
+ * guessing.
6004
+ *
6005
+ * What is deliberately NOT here: `Math.random()`, `Date.now()`, `process.pid`, or a freshly
6006
+ * generated uuid. Any of those would present a second device to the upstream on every call.
6007
+ */
6008
+ function environmentUser() {
6009
+ for (const name of [
6010
+ "USERNAME",
6011
+ "USER",
6012
+ "LOGNAME"
6013
+ ]) {
6014
+ const value = process.env[name];
6015
+ if (value !== void 0 && value.trim() !== "") return value.trim();
6016
+ }
6017
+ }
6018
+ /** The fallback machine input, from the environment, when `hostname()` itself failed. */
6019
+ function environmentMachine() {
6020
+ for (const name of ["COMPUTERNAME", "HOSTNAME"]) {
6021
+ const value = process.env[name];
6022
+ if (value !== void 0 && value.trim() !== "") return value.trim();
6023
+ }
6024
+ }
6025
+ function digest(seed) {
6026
+ return createHash("md5").update(seed).digest("hex");
6027
+ }
6028
+ /** Resolve the identity from scratch. Called once per process (see {@link cached}). */
6029
+ function resolve$1() {
6030
+ const registry = fromRegistry();
6031
+ if (registry !== void 0) return {
6032
+ ok: true,
6033
+ guid: registry,
6034
+ source: {
6035
+ kind: "registry",
6036
+ detail: `注册表 ${SVID_KEY}\\svid`
6037
+ }
6038
+ };
6039
+ const machine = safeHostname();
6040
+ const user = systemUser();
6041
+ if (machine !== void 0 && "username" in user) return {
6042
+ ok: true,
6043
+ guid: digest(`${machine}|${user.username}|marvis`),
6044
+ source: {
6045
+ kind: "machine",
6046
+ detail: `主机名 + 系统用户名(${machine}|${user.username})`
6047
+ }
6048
+ };
6049
+ const envUser = environmentUser();
6050
+ const envMachine = machine ?? environmentMachine();
6051
+ if (envUser !== void 0 && envMachine !== void 0) return {
6052
+ ok: true,
6053
+ guid: digest(`${envMachine}|${envUser}|marvis-env`),
6054
+ source: {
6055
+ kind: "environment",
6056
+ detail: `环境变量用户名 + 主机名(${envMachine}|${envUser})—— ${"problem" in user ? user.problem : "主机名不可用"}`
6057
+ }
6058
+ };
6059
+ const missing = [...envUser === void 0 ? ["环境变量里没有 USERNAME / USER / LOGNAME"] : [], ...envMachine === void 0 ? ["主机名与环境变量里都没有 COMPUTERNAME / HOSTNAME"] : []];
6060
+ return {
6061
+ ok: false,
6062
+ problem: [
6063
+ "无法确定这台机器的稳定设备标识",
6064
+ "problem" in user ? user.problem : "主机名不可用",
6065
+ ...missing,
6066
+ "请安装 Marvis 客户端(提供注册表 svid),或在带有用户名与主机名的环境下启动。"
6067
+ ].join(";")
6068
+ };
6069
+ }
6070
+ /** The identity, resolved once and cached. */
6071
+ function deviceIdentity() {
6072
+ cached$1 ??= resolve$1();
6073
+ return cached$1;
6074
+ }
5943
6075
  /**
5944
6076
  * The device id.
5945
6077
  *
5946
6078
  * Always 32 lowercase hex: the service rejects any other shape, and a per-call value would make
5947
6079
  * every request look like a new device.
6080
+ *
6081
+ * @throws {DeviceIdUnavailableError} when no stable input exists — see {@link deviceIdentity}
6082
+ * for the branch that lets a call site report it in its own words instead.
5948
6083
  */
5949
6084
  function deviceGuid() {
5950
- if (cached$1 !== void 0) return cached$1;
5951
- const registry = fromRegistry();
5952
- if (registry !== void 0) {
5953
- cached$1 = registry;
5954
- return cached$1;
5955
- }
5956
- const seed = `${hostname()}|${userInfo().username}|marvis`;
5957
- cached$1 = createHash("md5").update(seed).digest("hex");
5958
- return cached$1;
6085
+ const identity = deviceIdentity();
6086
+ if (!identity.ok) throw new DeviceIdUnavailableError(identity.problem);
6087
+ return identity.guid;
5959
6088
  }
5960
6089
  //#endregion
5961
6090
  //#region src/marvis-login.ts
@@ -6095,7 +6224,16 @@ async function browserLogin(options) {
6095
6224
  ok: false,
6096
6225
  reason: "没找到可用的 Chromium 内核浏览器(Chrome / Edge / 360 / QQ 浏览器都行)"
6097
6226
  };
6098
- const guid = deviceGuid();
6227
+ let guid;
6228
+ try {
6229
+ guid = deviceGuid();
6230
+ } catch (cause) {
6231
+ if (cause instanceof DeviceIdUnavailableError) return {
6232
+ ok: false,
6233
+ reason: cause.problem
6234
+ };
6235
+ throw cause;
6236
+ }
6099
6237
  const url = authorizeUrl(options.loginType, guid);
6100
6238
  const timeoutMs = options.timeoutMs ?? 3e5;
6101
6239
  const profile = mkdtempSync(join(tmpdir(), "xdpool-marvis-login-"));
@@ -6560,6 +6698,15 @@ function messageOf$6(error) {
6560
6698
  function registerMarvisRoutes(ctx, deps) {
6561
6699
  ctx.effect(() => {
6562
6700
  const trust = (req) => originTrusted(req, deps.trustedHosts?.() ?? []);
6701
+ /**
6702
+ * The device id a sign-in needs, as a throw that both routes already answer with a reason.
6703
+ *
6704
+ * Both sign-in handlers end in `json(res, 200, { ok: false, reason: … })`, so letting this
6705
+ * throw is the shortest path from "no device" to a sentence the user can act on. Calling
6706
+ * `deviceGuid()` directly would have done the same thing here; the named helper exists so
6707
+ * the two call sites cannot drift into one throwing and one swallowing.
6708
+ */
6709
+ const requireGuid = () => deviceGuid();
6563
6710
  /** The session: what is stored, and how to forget it. */
6564
6711
  const disposeSession = ctx.webServer.register({
6565
6712
  kind: "exact",
@@ -6580,6 +6727,7 @@ function registerMarvisRoutes(ctx, deps) {
6580
6727
  }
6581
6728
  if (req.method !== "GET") return json$7(res, 405, { error: "method not allowed" });
6582
6729
  const accounts = await deps.accounts();
6730
+ const device = deviceIdentity();
6583
6731
  return json$7(res, 200, {
6584
6732
  ok: true,
6585
6733
  signedIn: accounts.length > 0,
@@ -6589,7 +6737,12 @@ function registerMarvisRoutes(ctx, deps) {
6589
6737
  name: model.name
6590
6738
  })),
6591
6739
  /** The device this machine presents; shown so a support thread can compare. */
6592
- guid: deviceGuid()
6740
+ ...device.ok ? {
6741
+ guid: device.guid,
6742
+ deviceSource: device.source
6743
+ } : {},
6744
+ /** Why there is no id, when there is none — the diagnosable half of F7. */
6745
+ ...device.ok ? {} : { deviceProblem: device.problem }
6593
6746
  });
6594
6747
  } catch (error) {
6595
6748
  return json$7(res, 500, { error: messageOf$6(error) });
@@ -6613,7 +6766,7 @@ function registerMarvisRoutes(ctx, deps) {
6613
6766
  const account = await exchangeCode({
6614
6767
  code: result.code,
6615
6768
  loginType,
6616
- guid: deviceGuid(),
6769
+ guid: requireGuid(),
6617
6770
  ...result.state === void 0 ? {} : { state: result.state }
6618
6771
  });
6619
6772
  await saveMarvisAccount(account);
@@ -6658,7 +6811,7 @@ function registerMarvisRoutes(ctx, deps) {
6658
6811
  const account = await exchangeCode({
6659
6812
  code,
6660
6813
  loginType,
6661
- guid: deviceGuid(),
6814
+ guid: requireGuid(),
6662
6815
  ...state === void 0 ? {} : { state }
6663
6816
  });
6664
6817
  await saveMarvisAccount(account);
@@ -29380,47 +29533,21 @@ function parseBody(body) {
29380
29533
  *
29381
29534
  * @module dsh-xdpool/version
29382
29535
  */
29383
- const POOL_VERSION = "3.1.1";
29384
- //#endregion
29385
- //#region src/update-check.ts
29386
- /**
29387
- * Whether a newer build of this plugin exists.
29388
- *
29389
- * WHY IT LIVES IN THE PLUGIN
29390
- *
29391
- * Updating meant watching the repository by hand or asking an agent to check — neither is
29392
- * something a user should have to do to stay current. A version read from the registry on demand
29393
- * answers the only question worth asking ("is there something newer than what I run?") without a
29394
- * background task that phones home on its own schedule.
29395
- *
29396
- * WHAT IT ASKS
29397
- *
29398
- * The npm registry's abbreviated document for this package: `dist-tags.latest` and the publish
29399
- * time of that version. Nothing else is read, nothing is sent beyond the package name, and the
29400
- * answer is cached for a few minutes so opening the panel repeatedly is one request.
29401
- *
29402
- * A FAILURE IS AN ORDINARY ANSWER
29403
- *
29404
- * A machine behind a proxy, offline, or with no registry access still has a working plugin. The
29405
- * result carries `error` and the card says "could not check" rather than hiding the control or
29406
- * claiming there is nothing new — telling a user they are current when the check did not run is
29407
- * the one wrong answer here.
29408
- *
29409
- * @module dsh-xdpool/update-check
29410
- */
29411
- /** The package name this plugin publishes under. */
29412
- const PACKAGE$2 = "dsh-xdpool";
29413
- /** The registries tried, in order. npm first: it is the canonical one. */
29414
- const REGISTRIES = [`https://registry.npmjs.org/${PACKAGE$2}/latest`, `https://registry.npmjs.org/${PACKAGE$2}`];
29415
- /** How long an answer is reused. */
29416
- const TTL_MS = 6e5;
29417
- let cached;
29536
+ const POOL_VERSION = "3.3.0";
29418
29537
  /**
29419
29538
  * Compare two dotted versions.
29420
29539
  *
29421
29540
  * Numeric segments compare as numbers and everything else as text, which is the ordering npm
29422
29541
  * itself uses for the plain `x.y.z` releases this package publishes. A prerelease tag is older
29423
29542
  * than its release (`2.7.3-rc.1 < 2.7.3`), which is what the `-` check below states.
29543
+ *
29544
+ * WHY IT LIVES HERE AND NOT IN update-check
29545
+ *
29546
+ * The card compares versions in the BROWSER (`shell.tsx`, `UpdatePanel.tsx`). While this lived
29547
+ * in `update-check.ts`, importing it dragged that whole module into `lib/client.js` — and once
29548
+ * the update chain started reading its own package name from disk, that meant `node:fs` in a
29549
+ * browser bundle (`tests/client-bundle-purity.test.ts` caught it). Version arithmetic is pure;
29550
+ * keeping it next to the version constant is what makes the bundle stay pure.
29424
29551
  */
29425
29552
  function compareVersions(left, right) {
29426
29553
  const parse = (value) => {
@@ -29445,6 +29572,134 @@ function compareVersions(left, right) {
29445
29572
  if (a.pre !== "" && b.pre === "") return -1;
29446
29573
  return a.pre === b.pre ? 0 : a.pre < b.pre ? -1 : 1;
29447
29574
  }
29575
+ //#endregion
29576
+ //#region src/update-package.ts
29577
+ /**
29578
+ * 本插件的发布身份:一个地方说清楚「我们叫什么」。
29579
+ *
29580
+ * WHY THIS EXISTS
29581
+ *
29582
+ * 更新链路以前在三处各写了一个裸名 `dsh-xdpool`:查询 registry、拼 tarball 下载地址、
29583
+ * 写备份清单与回退键。包实际发布在 `@refeain/dsh-xdpool` 下,于是这三处一起去问一个
29584
+ * 不存在的裸名包 —— 查询永远读不到更新,备份把错的键写进 restore.json,回退也改不回来。
29585
+ *
29586
+ * 裸名不是纯粹的错:`dsh-xdpool` 是上游原名,早期由写死裸名的 fork 建出的 format=1 备份
29587
+ * 里记的就是它。所以这里同时导出两个身份,并明确它们各自的用途:
29588
+ *
29589
+ * - `PACKAGE_NAME` —— 现在的发布身份。查询、下载、写新备份、回退新备份都用它。
29590
+ * - `LEGACY_PACKAGE_NAME` —— 旧备份里可能出现的裸名。只用于「读得懂旧备份」,绝不用于
29591
+ * 拼新 URL 或写新键。
29592
+ * - `KNOWN_PACKAGE_NAMES` —— 识别一份备份属于本插件时的候选集合。
29593
+ *
29594
+ * 身份从 `package.json` 读,而不是再写一遍常量:源码测试里它就在旁边;构建后的宿主入口
29595
+ * 由 tsdown 打成单文件,读不到时退回这里的默认值。两条路都指向同一个名字,
29596
+ * `tests/update-identity.test.ts` 盯着这个一致。
29597
+ *
29598
+ * @module dsh-xdpool/update-package
29599
+ */
29600
+ /** 上游原名,也是旧 format=1 备份里可能出现的名字。 */
29601
+ const LEGACY_PACKAGE_NAME = "dsh-xdpool";
29602
+ /**
29603
+ * 兜底:与仓库 `package.json` 的 `name` 一致,构建后读不到清单时用它。
29604
+ *
29605
+ * 导出是为了让测试能断言「兜底值就是真实包名」—— 这条如果退化成裸名,构建产物里的更新
29606
+ * 检查就又会去问另一个包,而那种错误在运行时不报任何异常。
29607
+ */
29608
+ const FALLBACK_PACKAGE_NAME = "@refeain/dsh-xdpool";
29609
+ /**
29610
+ * 从相邻的 `package.json` 读包名。
29611
+ *
29612
+ * 只在启动时读一次。读不到(构建产物里没有清单)、读坏了、或者读出来的不是本插件可选的名字,
29613
+ * 都退回兜底值 —— 更新功能不能用「读不到名字」当理由整个失效。
29614
+ */
29615
+ function readPackageName() {
29616
+ try {
29617
+ const name = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).name;
29618
+ if (typeof name === "string" && name !== "") return name;
29619
+ } catch {}
29620
+ return FALLBACK_PACKAGE_NAME;
29621
+ }
29622
+ /** 现在的发布身份,例如 `@refeain/dsh-xdpool`。查询、下载、写新备份都用它。 */
29623
+ const PACKAGE_NAME = readPackageName();
29624
+ /** 识别一份备份是否属于本插件时可接受的名字;新的在前。 */
29625
+ const KNOWN_PACKAGE_NAMES = PACKAGE_NAME === "dsh-xdpool" ? [PACKAGE_NAME] : [PACKAGE_NAME, LEGACY_PACKAGE_NAME];
29626
+ /**
29627
+ * 把一个包名安全地用作文件名 / 目录名。
29628
+ *
29629
+ * scoped 包名里的斜杠会把 `join(tmpdir(), name)` 变成一层不存在的子目录,所以 `/` 换成 `+`
29630
+ * 之后再做一次白名单收紧。`@refeain/dsh-xdpool` -> `@refeain+dsh-xdpool`。
29631
+ */
29632
+ function packageFileStem(name = PACKAGE_NAME) {
29633
+ return name.replaceAll("/", "+").replace(/[^A-Za-z0-9@._+-]/gu, "_");
29634
+ }
29635
+ /**
29636
+ * registry 上某个版本的 tarball 保底地址。
29637
+ *
29638
+ * npm 的约定是 scoped 包的 tarball 文件名不带 scope:`@refeain/dsh-xdpool` 的 3.3.0 落在
29639
+ * `.../@refeain/dsh-xdpool/-/dsh-xdpool-3.3.0.tgz`。这只是保底 —— 正常路径是读版本元数据里的
29640
+ * `dist.tarball`,因为镜像或私有 registry 会给出别的形状。
29641
+ */
29642
+ function fallbackTarballUrl(version, name = PACKAGE_NAME) {
29643
+ return `https://registry.npmjs.org/${name}/-/${name.startsWith("@") ? name.split("/")[1] ?? name : name}-${version}.tgz`;
29644
+ }
29645
+ /**
29646
+ * 从 registry 的版本文档里读 `dist.tarball`。
29647
+ *
29648
+ * `metadata` 是 `/@refeain%2Fdsh-xdpool/3.3.0` 的响应体。读不到就返回 undefined,由调用方
29649
+ * 决定退到保底地址还是直接报错 —— 这里不编一个地址出来。
29650
+ */
29651
+ function parseVersionMetadata(metadata) {
29652
+ if (metadata === null || typeof metadata !== "object") return void 0;
29653
+ const record = metadata;
29654
+ const dist = record["dist"];
29655
+ if (dist === null || typeof dist !== "object") return void 0;
29656
+ const tarball = dist["tarball"];
29657
+ if (typeof tarball !== "string" || tarball === "") return void 0;
29658
+ const time = record["time"];
29659
+ const publishedAt = typeof time === "string" && time !== "" ? time : void 0;
29660
+ return {
29661
+ tarball,
29662
+ ...publishedAt === void 0 ? {} : { publishedAt }
29663
+ };
29664
+ }
29665
+ //#endregion
29666
+ //#region src/update-check.ts
29667
+ /**
29668
+ * Whether a newer build of this plugin exists.
29669
+ *
29670
+ * WHY IT LIVES IN THE PLUGIN
29671
+ *
29672
+ * Updating meant watching the repository by hand or asking an agent to check — neither is
29673
+ * something a user should have to do to stay current. A version read from the registry on demand
29674
+ * answers the only question worth asking ("is there something newer than what I run?") without a
29675
+ * background task that phones home on its own schedule.
29676
+ *
29677
+ * WHAT IT ASKS
29678
+ *
29679
+ * The npm registry's abbreviated document for this package: `dist-tags.latest` and the publish
29680
+ * time of that version. Nothing else is read, nothing is sent beyond the package name, and the
29681
+ * answer is cached for a few minutes so opening the panel repeatedly is one request.
29682
+ *
29683
+ * A FAILURE IS AN ORDINARY ANSWER
29684
+ *
29685
+ * A machine behind a proxy, offline, or with no registry access still has a working plugin. The
29686
+ * result carries `error` and the card says "could not check" rather than hiding the control or
29687
+ * claiming there is nothing new — telling a user they are current when the check did not run is
29688
+ * the one wrong answer here.
29689
+ *
29690
+ * @module dsh-xdpool/update-check
29691
+ */
29692
+ /**
29693
+ * The registries tried, in order. npm first: it is the canonical one.
29694
+ *
29695
+ * `@refeain/dsh-xdpool` 必须编码成 `@refeain%2Fdsh-xdpool` —— 未编码的斜杠会被 registry
29696
+ * 当成两个路径段,`/@refeain/dsh-xdpool/latest` 是 404,而 `%2F` 那个才是包。这条以前写成
29697
+ * 裸名 `dsh-xdpool`,于是查的根本不是本包。
29698
+ */
29699
+ const REGISTRIES = [`https://registry.npmjs.org/${encodeURIComponent(PACKAGE_NAME)}/latest`, `https://registry.npmjs.org/${encodeURIComponent(PACKAGE_NAME)}`];
29700
+ /** How long an answer is reused. */
29701
+ const TTL_MS = 6e5;
29702
+ let cached;
29448
29703
  /** Read `dist-tags.latest`, its publish time and the README from a registry document. */
29449
29704
  function parseRegistry(body) {
29450
29705
  if (body === null || typeof body !== "object") return {};
@@ -31507,8 +31762,6 @@ const PROVIDER_NOTES_ZH = Object.freeze({
31507
31762
  *
31508
31763
  * @module dsh-xdpool/pre-update-backup
31509
31764
  */
31510
- /** 本插件发布用的包名。 */
31511
- const PACKAGE$1 = "dsh-xdpool";
31512
31765
  /** 备份根目录名,位于插件数据目录下。 */
31513
31766
  const PRE_UPDATE_DIR_NAME = "pre-update";
31514
31767
  /** 机器读的回退信息。 */
@@ -31523,9 +31776,18 @@ const DEPENDENCY_BLOCKS = [
31523
31776
  ];
31524
31777
  /** 插件数据目录顶层不进备份的条目。 */
31525
31778
  const SKIP_TOP_LEVEL = /* @__PURE__ */ new Set([PRE_UPDATE_DIR_NAME, "login-profiles"]);
31526
- /** profile 目录里要备份的文件。 */
31779
+ /**
31780
+ * profile 目录里要备份的文件。
31781
+ *
31782
+ * 锁文件必须在内:更新会写 package.json 并让 pnpm 重写 pnpm-lock.yaml,恢复时不能只回退
31783
+ * 清单 —— 一份新清单配一份旧锁文件正是「更新了却没有生效」的那个状态。回退要能把两者
31784
+ * 一起还原,首先得有一份保存下来的锁文件。
31785
+ */
31527
31786
  const PROFILE_FILES = [
31528
31787
  "package.json",
31788
+ "pnpm-lock.yaml",
31789
+ "package-lock.json",
31790
+ "yarn.lock",
31529
31791
  "settings.yaml",
31530
31792
  "settings.yml",
31531
31793
  "settings.json",
@@ -31645,6 +31907,7 @@ function localTarballOf(spec, profileDir) {
31645
31907
  }
31646
31908
  }
31647
31909
  function rollbackNote(manifest, dir) {
31910
+ const key = manifestKey(manifest);
31648
31911
  const lines = [
31649
31912
  `XD Pool 更新前备份`,
31650
31913
  ``,
@@ -31652,11 +31915,11 @@ function rollbackNote(manifest, dir) {
31652
31915
  `更新前版本:${manifest.previousVersion}`,
31653
31916
  `准备更新到:${manifest.targetVersion}`,
31654
31917
  `profile 的 package.json:${manifest.packageJsonPath}`,
31655
- `更新前的 dsh-xdpool spec:${manifest.previousSpec ?? "(未记录)"}`,
31918
+ `更新前的 ${key} spec:${manifest.previousSpec ?? "(未记录)"}`,
31656
31919
  ``,
31657
31920
  `—— 回退到更新前的版本 ——`,
31658
31921
  `1. 完全退出 DSH。`,
31659
- `2. 打开 ${manifest.packageJsonPath},把 "${PACKAGE$1}" 那一行的值改回:`,
31922
+ `2. 打开 ${manifest.packageJsonPath},把 "${key}" 那一行的值改回:`,
31660
31923
  ` ${manifest.rollbackSpec ?? manifest.previousSpec ?? "(见 restore.json)"}`,
31661
31924
  ` (或者直接用本目录 profile\\package.json 覆盖它。)`,
31662
31925
  `3. 重新打开 DSH。`,
@@ -31769,7 +32032,7 @@ function backupBeforeUpdate(options) {
31769
32032
  }
31770
32033
  }
31771
32034
  const manifest = {
31772
- package: PACKAGE$1,
32035
+ package: PACKAGE_NAME,
31773
32036
  format: 1,
31774
32037
  createdAt: now.toISOString(),
31775
32038
  previousVersion: options.previousVersion,
@@ -31778,6 +32041,7 @@ function backupBeforeUpdate(options) {
31778
32041
  packageJsonPath: join(options.profileDir, "package.json"),
31779
32042
  ...previousSpec === void 0 ? {} : { previousSpec },
31780
32043
  previousSpecs: specs,
32044
+ ...options.previousKeys === void 0 ? {} : { previousKeys: { ...options.previousKeys } },
31781
32045
  ...options.newSpec === void 0 ? {} : { newSpec: options.newSpec },
31782
32046
  ...previousTarball !== void 0 ? {
31783
32047
  rollbackSpec: `file:${previousTarball.replaceAll("\\", "/")}`,
@@ -31802,6 +32066,10 @@ function backupBeforeUpdate(options) {
31802
32066
  manifest
31803
32067
  };
31804
32068
  }
32069
+ /** 备份清单里记的本插件名字;缺失时按现在的发布身份。 */
32070
+ function manifestKey(manifest) {
32071
+ return typeof manifest.package === "string" && manifest.package !== "" ? manifest.package : PACKAGE_NAME;
32072
+ }
31805
32073
  /** 列出备份根目录下的备份(只认带 restore.json 的目录),新的在前。永不抛错。 */
31806
32074
  function listPreUpdateBackups(root) {
31807
32075
  let names;
@@ -31816,7 +32084,7 @@ function listPreUpdateBackups(root) {
31816
32084
  try {
31817
32085
  if (!statSync(path).isDirectory()) continue;
31818
32086
  const manifest = JSON.parse(readFileSync(join(path, RESTORE_FILE_NAME), "utf8"));
31819
- if (manifest.package !== PACKAGE$1) continue;
32087
+ if (!KNOWN_PACKAGE_NAMES.includes(manifestKey(manifest))) continue;
31820
32088
  const createdAt = typeof manifest.createdAt === "string" ? manifest.createdAt : "";
31821
32089
  const parsed = Date.parse(createdAt);
31822
32090
  out.push({
@@ -31914,8 +32182,6 @@ function pruneOldBackups(root, keep = 5, keepPath) {
31914
32182
  *
31915
32183
  * @module dsh-xdpool/update-install
31916
32184
  */
31917
- /** The package this plugin publishes under. */
31918
- const PACKAGE = "dsh-xdpool";
31919
32185
  /** The profile directory whose `package.json` names this plugin. */
31920
32186
  function profileDir() {
31921
32187
  const candidates = [
@@ -32014,6 +32280,26 @@ function readProfilePackage(pkgPath) {
32014
32280
  }
32015
32281
  }
32016
32282
  /**
32283
+ * 某个版本 tarball 的真实地址。
32284
+ *
32285
+ * 读 `/@refeain%2Fdsh-xdpool/<version>` 的 `dist.tarball`;读不到(网络失败、代理返回 HTML、
32286
+ * 版本元数据异常)时退回保底地址。**不**在这里把一个读不到当失败 —— 下载那一步会给出
32287
+ * 明确的 HTTP 状态,比「读元数据失败」更好诊断。
32288
+ */
32289
+ async function resolveTarballUrl(version) {
32290
+ try {
32291
+ const response = await fetch(`https://registry.npmjs.org/${encodeURIComponent(PACKAGE_NAME)}/${encodeURIComponent(version)}`, {
32292
+ headers: { accept: "application/json" },
32293
+ signal: AbortSignal.timeout(8e3)
32294
+ });
32295
+ if (response.ok) {
32296
+ const meta = parseVersionMetadata(await response.json());
32297
+ if (meta !== void 0) return meta.tarball;
32298
+ }
32299
+ } catch {}
32300
+ return fallbackTarballUrl(version);
32301
+ }
32302
+ /**
32017
32303
  * Download a version and point the profile at it.
32018
32304
  *
32019
32305
  * The order matters, and it is the order the panel draws:
@@ -32028,6 +32314,47 @@ function readProfilePackage(pkgPath) {
32028
32314
  * A failure at any step leaves the working install and its data exactly as they were.
32029
32315
  */
32030
32316
  async function installVersion(version) {
32317
+ const profile = profileDir();
32318
+ if (profile === void 0) return {
32319
+ ok: false,
32320
+ restartRequired: false,
32321
+ previousVersion: POOL_VERSION,
32322
+ error: "找不到插件所在的 profile 目录,无法自动更新;请在 DSH 的插件设置里更新"
32323
+ };
32324
+ /**
32325
+ * 同一个 profile 的更新合并成一次。
32326
+ *
32327
+ * `POST /plugins/dsh-xdpool/update` 是并发的 HTTP 入口:界面上那个 busy 只存在于某一个
32328
+ * 浏览器标签里,拦不住第二个标签、另一台机器、或者脚本连发两次。两个 installVersion
32329
+ * 同时跑同一个 profile 会互相踩 —— 双方都读到旧清单、各建一份备份、后成的那次把
32330
+ * node_modules 与 lockfile 交错写。这里让后来者直接等前一次的结果:
32331
+ * 一次更新只产生一份备份,写入与同步也不会交错。
32332
+ *
32333
+ * 队列里只留「正在进行的那一次」;结束即删,进程闲置时不留状态。
32334
+ */
32335
+ return await coalesceByProfile(profile, () => installIntoProfile(profile, version));
32336
+ }
32337
+ /** profile 目录(已归一化的绝对路径)→ 正在进行的更新。 */
32338
+ const inFlightInstalls = /* @__PURE__ */ new Map();
32339
+ async function coalesceByProfile(profile, run) {
32340
+ /**
32341
+ * 键要归一化。Windows 的文件系统不区分大小写,同一个 profile 在两个请求里可能一个写
32342
+ * `C:\Users\…\Desktop`、另一个写 `c:\users\…\desktop` —— 不折叠就把它们当成两个 profile,
32343
+ * 于是互斥形同虚设。
32344
+ */
32345
+ const absolute = resolve(profile);
32346
+ const key = process.platform === "win32" ? absolute.toLowerCase() : absolute;
32347
+ const running = inFlightInstalls.get(key);
32348
+ if (running !== void 0) return await running;
32349
+ let started;
32350
+ started = run().finally(() => {
32351
+ if (inFlightInstalls.get(key) === started) inFlightInstalls.delete(key);
32352
+ });
32353
+ inFlightInstalls.set(key, started);
32354
+ return await started;
32355
+ }
32356
+ /** 真正做一次更新;只在 `coalesceByProfile` 的独占窗口里被调用。 */
32357
+ async function installIntoProfile(profile, version) {
32031
32358
  const previousVersion = POOL_VERSION;
32032
32359
  const steps = [];
32033
32360
  const failed = (error, extra = {}) => ({
@@ -32038,12 +32365,25 @@ async function installVersion(version) {
32038
32365
  error,
32039
32366
  ...extra
32040
32367
  });
32041
- const profile = profileDir();
32042
- if (profile === void 0) return failed("找不到插件所在的 profile 目录,无法自动更新;请在 DSH 的插件设置里更新");
32043
- const target = join(tmpdir(), `${PACKAGE}-${version}.tgz`);
32368
+ /**
32369
+ * 临时文件必须独占:两个更新同时开始(或上一次的残留)会互相覆盖同一个 `.part`,
32370
+ * 结果是一个谁也没完整写过的 tar 包被当成新版本装上。mkdtemp 给出唯一目录,
32371
+ * 文件名只保留安全字符(scoped 包名里的 `/` 会造出不存在的子目录)。
32372
+ */
32373
+ const staging = mkdtempSync(join(tmpdir(), "xdpool-update-"));
32374
+ const target = join(staging, `${packageFileStem()}-${version}.tgz`);
32044
32375
  const partial = `${target}.part`;
32045
32376
  try {
32046
- const response = await fetch(`https://registry.npmjs.org/${PACKAGE}/-/${PACKAGE}-${version}.tgz`, { signal: AbortSignal.timeout(12e4) });
32377
+ /**
32378
+ * 先读这个版本的元数据,用其中的 `dist.tarball`。
32379
+ *
32380
+ * 拼地址的时代留下了两个坑:裸名 `dsh-xdpool` 指向的是另一个包;scoped 包的 tarball
32381
+ * 文件名不带 scope,凭名字猜很容易拼错。registry 自己给出的地址是唯一可信的那份,
32382
+ * 镜像 / 私有 registry 也会给出不同形状。读不到元数据时才退回约定的保底地址 —— 那是
32383
+ * 一个仍然正确、只是可能被镜像改写过的地址。
32384
+ */
32385
+ const tarballUrl = await resolveTarballUrl(version);
32386
+ const response = await fetch(tarballUrl, { signal: AbortSignal.timeout(12e4) });
32047
32387
  if (!response.ok || response.body === null) {
32048
32388
  steps.push({
32049
32389
  id: "download",
@@ -32125,28 +32465,65 @@ async function installVersion(version) {
32125
32465
  return failed(read.error, { path: target });
32126
32466
  }
32127
32467
  const pkg = read.pkg;
32468
+ /**
32469
+ * 在 profile 里找到本插件是哪一行的键。
32470
+ *
32471
+ * 现在的发布身份优先;找不到时退到旧裸名 —— 早期由写死裸名的 fork 安装过的 profile,那一行
32472
+ * 就叫 `dsh-xdpool`,不认它就没法从那个状态升上来。**同一依赖块里两个键同时存在**
32473
+ * 是历史误装的痕迹,此时不猜:两个都改会让回退说不清恢复成哪一行,直接报出需要人工处理。
32474
+ */
32475
+ const keysByBlock = resolveDependencyKeys(pkg);
32476
+ if ("conflict" in keysByBlock) {
32477
+ steps.push({
32478
+ id: "write",
32479
+ ok: false,
32480
+ detail: keysByBlock.conflict
32481
+ });
32482
+ return failed(keysByBlock.conflict, { path: target });
32483
+ }
32128
32484
  const previousSpecs = {};
32129
32485
  for (const block of DEPENDENCY_BLOCKS) {
32486
+ const key = keysByBlock.keys[block];
32487
+ if (key === void 0) continue;
32130
32488
  const record = pkg[block];
32131
32489
  if (record === null || typeof record !== "object" || Array.isArray(record)) continue;
32132
- const deps = record;
32133
- if (!Object.hasOwn(deps, PACKAGE)) continue;
32134
- const spec = deps[PACKAGE];
32490
+ const spec = record[key];
32135
32491
  previousSpecs[block] = typeof spec === "string" ? spec : JSON.stringify(spec);
32136
32492
  }
32137
32493
  if (Object.keys(previousSpecs).length === 0) {
32138
32494
  steps.push({
32139
32495
  id: "write",
32140
32496
  ok: false,
32141
- detail: `package.json 里没有 ${PACKAGE}`
32497
+ detail: `package.json 里没有 ${PACKAGE_NAME}`
32498
+ });
32499
+ return failed(`profile 的 package.json 里没有 ${PACKAGE_NAME},无法自动更新`, { path: target });
32500
+ }
32501
+ /**
32502
+ * 锁文件的快照必须在**任何东西被创建之前**取,并且读不出来就立刻停下。
32503
+ *
32504
+ * 两个理由:
32505
+ * 1. 快照是恢复阶段唯一的依据 —— 要决定「写回旧内容」还是「删掉它」。既然判断不出来,
32506
+ * 两种做法都可能破坏用户的安装状态(把存在的锁文件当不存在删掉,下次启动就换一套
32507
+ * 解析结果),所以宁可不更新。
32508
+ * 2. 放在建备份之前,失败时连一份新备份都不会留下,备份环也不会被 prune 挤掉一份旧备份。
32509
+ */
32510
+ const lockPath = join(profile, "pnpm-lock.yaml");
32511
+ const lockBefore = snapshotFile(lockPath);
32512
+ if (lockBefore.kind === "unreadable") {
32513
+ const reason = chineseFailure("读不到 profile 的 pnpm-lock.yaml,已取消更新(文件没有被改动)", new Error(lockBefore.detail));
32514
+ steps.push({
32515
+ id: "write",
32516
+ ok: false,
32517
+ detail: reason
32142
32518
  });
32143
- return failed(`profile 的 package.json 里没有 ${PACKAGE},无法自动更新`, { path: target });
32519
+ return failed(reason, { path: target });
32144
32520
  }
32145
32521
  const backup = backupBeforeUpdate({
32146
32522
  profileDir: profile,
32147
32523
  previousVersion,
32148
32524
  targetVersion: version,
32149
32525
  previousSpecs,
32526
+ previousKeys: Object.fromEntries(Object.entries(keysByBlock.keys).filter((entry) => entry[1] !== void 0)),
32150
32527
  newSpec
32151
32528
  });
32152
32529
  if (!backup.ok || backup.path === void 0) {
@@ -32166,9 +32543,18 @@ async function installVersion(version) {
32166
32543
  });
32167
32544
  pruneOldBackups(preUpdateRoot(), void 0, backupPath);
32168
32545
  try {
32546
+ /**
32547
+ * 写入规范包名,并把旧裸名那一行删掉。
32548
+ *
32549
+ * 只加不改是不够的:一个由旧 fork 装过的 profile 里留着 `dsh-xdpool` 那一行,再加一行
32550
+ * `@refeain/dsh-xdpool`,pnpm 会当成两个包各装一份 —— 一个是被 deprecate 的错包。
32551
+ * 旧键的原值已经记在 previousSpecs 里,回退时按它写回,所以这里删得掉也回得去。
32552
+ */
32169
32553
  for (const block of Object.keys(previousSpecs)) {
32170
32554
  const deps = pkg[block];
32171
- deps[PACKAGE] = newSpec;
32555
+ const legacy = keysByBlock.keys[block];
32556
+ if (legacy !== void 0 && legacy !== PACKAGE_NAME) delete deps[legacy];
32557
+ deps[PACKAGE_NAME] = newSpec;
32172
32558
  }
32173
32559
  writeFileSync(`${pkgPath}.tmp`, `${JSON.stringify(pkg, null, 2)}\n`, "utf8");
32174
32560
  renameSync(`${pkgPath}.tmp`, pkgPath);
@@ -32181,16 +32567,36 @@ async function installVersion(version) {
32181
32567
  * 以前只到上一行就返回成功,DSH 下次启动按旧 lockfile 装回旧版,版本号从不变化。
32182
32568
  * 同步失败就回滚,宁可这次没更新,也不留下会触发宿主 repair 的半更新 profile。
32183
32569
  */
32184
- const synced = await syncImplementation(profile);
32570
+ let synced;
32571
+ let syncThrew;
32572
+ try {
32573
+ synced = await syncImplementation(profile);
32574
+ } catch (error) {
32575
+ syncThrew = error;
32576
+ synced = {
32577
+ ok: false,
32578
+ detail: error instanceof Error ? error.message : String(error)
32579
+ };
32580
+ }
32185
32581
  if (!synced.ok) {
32186
- writeFileSync(`${pkgPath}.tmp`, originalManifest, "utf8");
32187
- renameSync(`${pkgPath}.tmp`, pkgPath);
32582
+ const recovery = await recoverProfile({
32583
+ pkgPath,
32584
+ originalManifest,
32585
+ lockPath,
32586
+ lockBefore,
32587
+ profile
32588
+ });
32589
+ const detail = syncThrew === void 0 ? synced.detail : `同步钩子抛出异常:${synced.detail}`;
32188
32590
  steps.push({
32189
32591
  id: "write",
32190
32592
  ok: false,
32191
- detail: `pnpm 同步失败,已回滚:${synced.detail}`
32593
+ detail: recovery.summary
32594
+ });
32595
+ if (!recovery.filesRestored) return failed(`安装依赖失败(${detail}),且文件回滚未完成。${recovery.summary}。profile 的备份在 ${backupPath}(restore.json 记着原 spec),可据此手动修复;修好后重启 DSH 让宿主重新安装依赖`, {
32596
+ path: target,
32597
+ backupPath
32192
32598
  });
32193
- return failed(`安装依赖失败,已回滚,插件和数据都没有改动:${synced.detail}`, {
32599
+ return failed(`安装依赖失败(${detail})。package.json 与 pnpm-lock.yaml 已恢复成更新前的样子,但 node_modules 在同步过程中可能已被部分改动 —— 这一点无法从插件侧确认。再点一次「更新」可以重试;也可以直接重启 DSH,宿主会按写回的清单与锁文件把依赖装回去。`, {
32194
32600
  path: target,
32195
32601
  backupPath
32196
32602
  });
@@ -32210,17 +32616,121 @@ async function installVersion(version) {
32210
32616
  steps
32211
32617
  };
32212
32618
  } catch (error) {
32619
+ const recovery = await recoverProfile({
32620
+ pkgPath,
32621
+ originalManifest,
32622
+ lockPath,
32623
+ lockBefore,
32624
+ profile
32625
+ });
32626
+ const reason = chineseFailure("写入 profile 的 package.json 失败", error);
32213
32627
  steps.push({
32214
32628
  id: "write",
32215
32629
  ok: false,
32216
- detail: error instanceof Error ? error.message : String(error)
32630
+ detail: `${reason};${recovery.summary}`
32631
+ });
32632
+ if (!recovery.filesRestored) return failed(`${reason},且文件回滚未完成。${recovery.summary}。备份在 ${backupPath}(restore.json 记着原 spec),可据此手动修复;修好后重启 DSH 让宿主重新安装依赖`, {
32633
+ path: target,
32634
+ backupPath
32217
32635
  });
32218
- return failed(chineseFailure("写入 profile 的 package.json 失败", error), {
32636
+ return failed(`${reason}。${recovery.summary};node_modules 未做同步,应当仍是更新前的样子,但插件侧无法逐字节确认。重启 DSH 可让宿主按现有清单与锁文件重建依赖。`, {
32219
32637
  path: target,
32220
32638
  backupPath
32221
32639
  });
32222
32640
  }
32223
32641
  }
32642
+ /** 读一个文件的内容与存在性;只有「确实不存在」才算不存在,其余读取错误原样报出来。 */
32643
+ function snapshotFile(path) {
32644
+ try {
32645
+ return {
32646
+ kind: "present",
32647
+ content: readFileSync(path, "utf8")
32648
+ };
32649
+ } catch (error) {
32650
+ const code = error?.code;
32651
+ if (code === "ENOENT" || code === "ENOTDIR") return { kind: "missing" };
32652
+ return {
32653
+ kind: "unreadable",
32654
+ detail: error instanceof Error ? error.message : String(error)
32655
+ };
32656
+ }
32657
+ }
32658
+ /** 把文件恢复成快照里的样子:原来有就写回原内容,原来没有就删掉。 */
32659
+ function restoreFile(path, snapshot) {
32660
+ if (snapshot.kind === "unreadable") return {
32661
+ ok: false,
32662
+ detail: "更新前没能读出这个文件,无法判断该恢复成什么"
32663
+ };
32664
+ try {
32665
+ if (snapshot.kind === "present") {
32666
+ writeFileSync(`${path}.tmp`, snapshot.content, "utf8");
32667
+ renameSync(`${path}.tmp`, path);
32668
+ } else rmSync(path, { force: true });
32669
+ return { ok: true };
32670
+ } catch (error) {
32671
+ return {
32672
+ ok: false,
32673
+ detail: error instanceof Error ? error.message : String(error)
32674
+ };
32675
+ }
32676
+ }
32677
+ /**
32678
+ * 把 profile 恢复成更新前的样子,并如实报告结果。
32679
+ *
32680
+ * 恢复的是**可证明的那部分状态**:package.json 与锁文件的字节。顺序有理由 —— 先写回清单
32681
+ * (它是「安装了什么」的唯一声明),再写回锁文件(它决定下次启动装哪一版)。
32682
+ *
32683
+ * **不在这里重跑 pnpm 同步**,这一条是踩出来的:
32684
+ * - 同步刚刚失败过,立刻重跑多半还是同一个原因(权限、占用、磁盘),只是让失败慢一倍;
32685
+ * - 更糟的是它会**重新造出刚被删掉的文件** —— 更新前没有锁文件的 profile,恢复时删掉
32686
+ * 半成品锁文件,重跑同步又写回来,于是「回滚」之后仍留着一份来路不明的锁文件;
32687
+ * - node_modules 的字节本来就不在可证明范围里,凭重跑来断言「依赖也回去了」是空头承诺。
32688
+ *
32689
+ * 下次 DSH 启动会按写回的锁文件重建依赖,那时状态自然一致。所以这里只承诺文件级恢复,
32690
+ * 并把 node_modules 的实际处境讲清楚,不假装它也是干净的。
32691
+ */
32692
+ async function recoverProfile(options) {
32693
+ const notes = [];
32694
+ let filesRestored = true;
32695
+ const manifest = restoreFile(options.pkgPath, {
32696
+ kind: "present",
32697
+ content: options.originalManifest
32698
+ });
32699
+ if (manifest.ok) notes.push("package.json 已写回");
32700
+ else {
32701
+ filesRestored = false;
32702
+ notes.push(`package.json 写回失败(${manifest.detail ?? "未知原因"})`);
32703
+ }
32704
+ const lock = restoreFile(options.lockPath, options.lockBefore);
32705
+ if (lock.ok) notes.push(options.lockBefore.kind === "present" ? "pnpm-lock.yaml 已写回" : "pnpm-lock.yaml 已删除(更新前没有)");
32706
+ else {
32707
+ filesRestored = false;
32708
+ notes.push(`pnpm-lock.yaml 恢复失败(${lock.detail ?? "未知原因"})`);
32709
+ }
32710
+ return {
32711
+ filesRestored,
32712
+ summary: notes.join(";")
32713
+ };
32714
+ }
32715
+ /**
32716
+ * 在 profile 里找出本插件出现在哪些依赖块、用什么键。
32717
+ *
32718
+ * 返回的 `keys` 是「依赖块 -> 键名」。旧的裸名 profile 会给出 `dsh-xdpool`;同一块里同时
32719
+ * 出现两个键时返回 `conflict` —— 那是历史误装,两个都改会让回退说不清,必须人工选。
32720
+ */
32721
+ function resolveDependencyKeys(pkg) {
32722
+ const keys = {};
32723
+ for (const block of DEPENDENCY_BLOCKS) {
32724
+ const record = pkg[block];
32725
+ if (record === null || typeof record !== "object" || Array.isArray(record)) continue;
32726
+ const deps = record;
32727
+ const present = KNOWN_PACKAGE_NAMES.filter((name) => Object.hasOwn(deps, name));
32728
+ if (present.length > 1) return { conflict: `profile 的 ${block} 里同时存在 ${KNOWN_PACKAGE_NAMES.join(" 和 ")},无法判断该更新哪一行;请先手动保留一个再更新` };
32729
+ const only = present[0];
32730
+ if (only !== void 0) keys[block] = only;
32731
+ }
32732
+ return { keys };
32733
+ }
32224
32734
  /**
32225
32735
  * Check, then install if there is something newer.
32226
32736
  *
@@ -33478,10 +33988,16 @@ function registerPoolStatusRoute(ctx, deps) {
33478
33988
  try {
33479
33989
  return json(res, 200, await updateToLatest());
33480
33990
  } catch (error) {
33991
+ /**
33992
+ * updateToLatest 自己的失败都已经是中文,到这里说明是它没预料到的异常。
33993
+ *
33994
+ * 不能说「插件和数据都没有改动」:写入 package.json 之后还有同步依赖这一步,
33995
+ * 异常可能发生在它之后,那时清单与锁文件确实已经被改过。只报事实 + 出路。
33996
+ */
33481
33997
  return json(res, 200, {
33482
33998
  ok: false,
33483
33999
  restartRequired: false,
33484
- error: `更新失败(${safeMessage(error)}),插件和数据都没有改动`
34000
+ error: `更新失败(${safeMessage(error)})。无法确认 profile 当前状态,请重启 DSH 让宿主按现有清单重建依赖,再回到这里看版本号是否已变。`
33485
34001
  });
33486
34002
  }
33487
34003
  }