dsh-plugin-tool-management 0.21.7 → 0.21.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,8 +30,8 @@ import { isAsarPath, isFn, OWNER_CLASSES, realPathWithReason, referencePrototype
30
30
  // 未改(moved-verify 逐字对拍)。probe.js 的公开面照旧:搬走的那 12 件全部从这里再出口 ——
31
31
  // index.ts / ops/compat.ts / sessions/bridge.ts 都按 ./compat/probe.js 引它们,少一件 equiv
32
32
  // 对拍就报「导出面丢失」。
33
- export { asarHostPackageRoot, findingsById, hostRootCandidates, IDENTITY_PACKAGES, npxCacheHostRoots, OPERATION_ROUTES, recoveryFor, refusalsFor, routeFor, VERIFIED_HOST_VERSION, } from './host-roots.js';
34
- import { hostRootCandidates, IDENTITY_PACKAGES, routeFor, VERIFIED_HOST_VERSION } from './host-roots.js';
33
+ export { asarHostPackageRoot, findingsById, globalNpmHostPackageDirs, hostPackageDirsFromPath, hostRootCandidates, hostVersionOf, IDENTITY_PACKAGES, looksLikeHostPackageDir, npxCacheHostRoots, OPERATION_ROUTES, recoveryFor, refusalsFor, routeFor, VERIFIED_HOST_VERSION, } from './host-roots.js';
34
+ import { hostRootCandidates, IDENTITY_PACKAGES, looksLikeHostPackageDir, routeFor, VERIFIED_HOST_VERSION } from './host-roots.js';
35
35
  // 「宿主在哪」这份口径只有一份(host-roots.ts 的 hostRootCandidates)。静态能力表里有条目要
36
36
  // 按宿主侧解析结果决定**这一轮能不能查**(`staticWhen`,见 connection.admit 那条),而
37
37
  // capability-specs.ts 不能反向 import 本文件(probe → specs → probe 成环)。所以在这里把
@@ -54,7 +54,7 @@ export function isSourceInstall() {
54
54
  *
55
55
  * 官方全程走 prerelease 渠道(x.y.z-rc.N),而 semver 有一条硬规则:**带 prerelease 标签的
56
56
  * 版本,只有在"同 [major,minor,patch] 元组"的比较器也带 prerelease 标签时才能匹配**。所以
57
- * `>=0.2.1-rc.3` 匹配不了 `0.2.2-rc.1`,`^0.2.1-rc.3` 也匹配不了它 —— 范围只能**逐段写**,
57
+ * `>=0.2.1-rc.5` 匹配不了 `0.2.2-rc.1`,`^0.2.1-rc.5` 也匹配不了它 —— 范围只能**逐段写**,
58
58
  * 一代一段。
59
59
  *
60
60
  * 但「逐段写」不等于「把历史各代累积起来」。累积成 `0.1.7 || 0.2.0 || 0.2.1` 三代并集,
@@ -67,8 +67,8 @@ export function isSourceInstall() {
67
67
  * 对应版本即可(`dsh plugin add dsh-plugin-tool-management@<老版本号>`)。装上被拒 + 一版新适配,
68
68
  * 优于装上然后部分功能悄悄失效。
69
69
  *
70
- * 上界**必须写**(`<0.2.2`):`^` 不能用,它的隐含上界是 `<0.3.0`,而 `0.2.1-rc.3` 这个已适配
71
- * 版本自己都满足不了 `^0.2.1-rc.3`(prerelease 只在同元组比较器带 prerelease 时才匹配)。
70
+ * 上界**必须写**(`<0.2.2`):`^` 不能用,它的隐含上界是 `<0.3.0`,而 `0.2.1-rc.5` 这个已适配
71
+ * 版本自己都满足不了 `^0.2.1-rc.5`(prerelease 只在同元组比较器带 prerelease 时才匹配)。
72
72
  *
73
73
  * 两种写法(这里是 `^` 三段、那里是 `>=` 两段,且下界不同)。现在两处是**同一个字符串**,
74
74
  * 并由 `test/contracts.test.mjs` 的断言钉住 —— 漂移会让"界面/doctor 报的范围"与"安装期实际
@@ -80,14 +80,15 @@ export function isSourceInstall() {
80
80
  * "任意未来 prerelease"的 range。缓解方式:官方每发一代新 rc,同步改这一段(同时改
81
81
  * `VERIFIED_HOST_VERSION`),以及 `--legacy-peer-deps` 这条逃生门。
82
82
  *
83
- * 0.2.1-rc.3 的验证依据:rc.2 上 `assessHost` 全量通过(宿主版本读得出来),rc.3 与 rc.2
84
- * 的逐包比对里受监视的全部包只有版本号变化(改动集中在客户端 UI,cordis 未动)。
83
+ * 0.2.1-rc.5 的验证依据:桌面宿主上 `assessHost` 全量通过(23/23,身份比对全部同一份实现);
84
+ * 官方内置技能清单新增 `office` 路由与三个操作技能,LibreOffice Kit 的注入名单随之变化,
85
+ * 本插件已按官方源码对齐。
85
86
  */
86
- export const EXPECTED_PEER_RANGE = '>=0.2.1-rc.3 <0.2.2';
87
+ export const EXPECTED_PEER_RANGE = '>=0.2.1-rc.5 <0.2.2';
87
88
  /**
88
89
  * peer range 的下界。仍从 EXPECTED_PEER_RANGE 派生,避免两处手写漂移。
89
90
  *
90
- * 范围收窄到一代之后它**与 VERIFIED_HOST_VERSION 重合**(都是 0.2.1-rc.3)—— 这是正确的:
91
+ * 范围收窄到一代之后它**与 VERIFIED_HOST_VERSION 重合**(都是 0.2.1-rc.5)—— 这是正确的:
91
92
  * 适配的那一代就是唯一允许安装的那一代。保留这个导出是因为它进了 `compat-status` 的载荷,
92
93
  * doctor 与外部脚本可能仍在读;语义仍是"最早支持哪一代",只是现在只有一代。
93
94
  */
@@ -468,19 +469,16 @@ function hostContractStatus(hostRoot) {
468
469
  };
469
470
  }
470
471
  /**
471
- * 本插件所挂的那个 DSH 安装的 `@deepseek-ai` 目录,从某个共享包的物理位置推出来。
472
+ * 本插件所挂的那个 DSH 安装的 `@deepseek-ai` 目录。
472
473
  *
473
- * 锚在**已解析的入口**上而不是 `require.resolve('@deepseek-ai/dsh')` 上:插件从不 import
474
- * `dsh` 应用包,它本来就不必对插件可解析 —— 可解析的只有那些共享库。
475
- *
476
- * 注意 pnpm:这里在 `.pnpm/<name>@<ver>/node_modules/` 布局下会返回**该包的隔离目录**
477
- * (那也是一个 `node_modules/@deepseek-ai`)。它本身不是问题 —— node 的解析会继续向上走到
478
- * 顶层 `node_modules`,所以能解析出的包集合与顶层锚点相同(实测确认)。真正的风险在
479
- * 调用方:从锚点解析不到的包会被判成 `same = null`,见 assessHost 里的 `unverified`。
474
+ * 判据是「这个目录里确实装着宿主包」(`looksLikeHostPackageDir`),不是「有没有
475
+ * `@deepseek-ai/dsh` 同级包」:全局 npm 安装把依赖放在 `dsh/node_modules/@deepseek-ai`,
476
+ * 那一层没有 `dsh` 自己;按同级包判会把全局安装整体跳过,退到 npx 缓存里另一代宿主上
477
+ * 比出假的「两份不同拷贝」。
480
478
  */
481
479
  function hostPackageRoot() {
482
480
  for (const candidate of hostRootCandidates()) {
483
- if (existsSync(join(candidate, 'dsh', 'package.json')))
481
+ if (looksLikeHostPackageDir(candidate))
484
482
  return candidate;
485
483
  }
486
484
  return null;
@@ -0,0 +1,121 @@
1
+ // hub 级跨进程写锁。
2
+ //
3
+ // 为什么需要:DSH 的 web 与 desktop 是两个宿主进程,却共用同一个 `$DSH_HOME`。apply 里那条
4
+ // `writeChain` 只能串行**同一进程**的写入;两个进程同时读-改-写同一份侧车时,后写的会覆盖
5
+ // 先写的(停用表、作用域覆盖、工具表都是整份写)。这里用 hub 根下的 `.write.lock` 做第二层。
6
+ //
7
+ // 锁文件不常开:写入 pid / 时间 / 随机 token 后立即关闭;临界区结束时按 token 删除。持锁期间
8
+ // 每 5 秒 touch 一次 mtime,心跳停了超过 `staleMs` 才允许别的进程抢占。这样进程崩溃留下的锁
9
+ // 最多挡一分钟,不会永久锁死 hub。
10
+ import { mkdir, open, readFile, stat, unlink, utimes } from 'node:fs/promises';
11
+ import { join } from 'node:path';
12
+ import { randomUUID } from 'node:crypto';
13
+ /** 锁文件名(hub 根;点开头,迁移与备份都不认识它)。 */
14
+ export const HUB_LOCK_FILE = '.write.lock';
15
+ /** 等锁的上限:超过就报错,不无限挂住一次面板写入。 */
16
+ const DEFAULT_TIMEOUT_MS = 30_000;
17
+ /** 没有心跳多久算死锁。必须明显大于心跳间隔,给一次长写入留余量。 */
18
+ const DEFAULT_STALE_MS = 60_000;
19
+ /** 心跳间隔:touch 锁文件 mtime。 */
20
+ const HEARTBEAT_MS = 5_000;
21
+ /** 等锁的重试间隔(带少量随机抖动,避免两个进程同步醒来反复撞)。 */
22
+ const RETRY_MS = 25;
23
+ /** 读锁文件的内容与 mtime;不存在 / 读不动返回 null。 */
24
+ async function readLock(path) {
25
+ try {
26
+ const [raw, info] = await Promise.all([readFile(path, 'utf8'), stat(path)]);
27
+ let parsed = null;
28
+ try {
29
+ const obj = JSON.parse(raw);
30
+ if (typeof obj.pid === 'number' && typeof obj.at === 'number' && typeof obj.token === 'string') {
31
+ parsed = { pid: obj.pid, at: obj.at, token: obj.token };
32
+ }
33
+ }
34
+ catch { /* 刚创建还没写完 / 坏内容:按"有锁但读不懂"处理 */ }
35
+ return { info: parsed, mtime: info.mtimeMs };
36
+ }
37
+ catch {
38
+ return null;
39
+ }
40
+ }
41
+ /** 进程还在吗:`EPERM` 表示存在但没权限发信号,按活着算。 */
42
+ function processAlive(pid) {
43
+ if (!Number.isInteger(pid) || pid <= 0)
44
+ return false;
45
+ try {
46
+ process.kill(pid, 0);
47
+ return true;
48
+ }
49
+ catch (e) {
50
+ return e?.code === 'EPERM';
51
+ }
52
+ }
53
+ /**
54
+ * 建一把 hub 文件锁。
55
+ *
56
+ * @param dir hub 根目录(`$DSH_HOME/tool-management`);不存在时首次等锁会创建
57
+ * @param options 超时与死锁阈值(测试可缩短)
58
+ */
59
+ export function createHubFileLock(dir, options = {}) {
60
+ const path = join(dir, HUB_LOCK_FILE);
61
+ const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
62
+ const staleMs = options.staleMs ?? DEFAULT_STALE_MS;
63
+ /** 尝试拿锁;拿不到返回 false,拿到后由调用方持有。 */
64
+ async function tryAcquire(token) {
65
+ try {
66
+ const handle = await open(path, 'wx');
67
+ try {
68
+ await handle.writeFile(JSON.stringify({ pid: process.pid, at: Date.now(), token }), 'utf8');
69
+ }
70
+ finally {
71
+ await handle.close();
72
+ }
73
+ return true;
74
+ }
75
+ catch (e) {
76
+ if (e?.code !== 'EEXIST')
77
+ throw e;
78
+ const seen = await readLock(path);
79
+ if (seen === null)
80
+ return false; // 刚被删;下一轮重试即可
81
+ const age = Date.now() - seen.mtime;
82
+ if (seen.info !== null && processAlive(seen.info.pid) && age < staleMs)
83
+ return false;
84
+ // 进程已死 / 心跳过期 / 内容读不懂:只删我们看到的那一份,避免误删别人刚建的新锁。
85
+ const again = await readLock(path);
86
+ if (again !== null && (seen.info === null || again.info?.token === seen.info.token)) {
87
+ await unlink(path).catch(() => undefined);
88
+ }
89
+ return false;
90
+ }
91
+ }
92
+ return {
93
+ async run(fn) {
94
+ await mkdir(dir, { recursive: true });
95
+ const token = randomUUID();
96
+ const started = Date.now();
97
+ for (;;) {
98
+ if (await tryAcquire(token))
99
+ break;
100
+ if (Date.now() - started > timeoutMs) {
101
+ throw new Error('另一个 DSH 进程正在写同一个数据目录,等待超时;请稍后重试,或只保留一个宿主进程。');
102
+ }
103
+ await new Promise((resolve) => setTimeout(resolve, RETRY_MS + Math.floor(Math.random() * RETRY_MS)));
104
+ }
105
+ const beat = setInterval(() => {
106
+ // 心跳失败(文件被删 / 被抢占)不打断临界区:释放时还会按 token 再确认一次。
107
+ void utimes(path, new Date(), new Date()).catch(() => undefined);
108
+ }, HEARTBEAT_MS);
109
+ beat.unref();
110
+ try {
111
+ return await fn();
112
+ }
113
+ finally {
114
+ clearInterval(beat);
115
+ const seen = await readLock(path);
116
+ if (seen?.info?.token === token)
117
+ await unlink(path).catch(() => undefined);
118
+ }
119
+ },
120
+ };
121
+ }