dsh-config-manager 0.4.3 → 0.5.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 (254) hide show
  1. package/README.md +10 -0
  2. package/README.zh-CN.md +10 -0
  3. package/lib/adapters/mcp.d.ts +9 -0
  4. package/lib/adapters/mcp.js +37 -0
  5. package/lib/adapters/plugins.js +10 -2
  6. package/lib/adapters/pnpm-workspace.js +1 -1
  7. package/lib/adapters/self.d.ts +2 -0
  8. package/lib/adapters/self.js +18 -2
  9. package/lib/adapters/workspaces.js +1 -1
  10. package/lib/cli/restore-resume.js +1 -1
  11. package/lib/cli/web/page.js +2 -2
  12. package/lib/client.d.ts +14 -41
  13. package/lib/client.js +794 -716
  14. package/lib/core/backup.d.ts +11 -0
  15. package/lib/core/backup.js +27 -0
  16. package/lib/core/env-lock-lifecycle.d.ts +17 -3
  17. package/lib/core/env-lock-lifecycle.js +38 -0
  18. package/lib/core/external-failure.d.ts +9 -0
  19. package/lib/core/external-failure.js +50 -0
  20. package/lib/core/local-plugin-host.d.ts +9 -7
  21. package/lib/core/local-plugin-host.js +109 -15
  22. package/lib/core/local-plugin-pack.d.ts +20 -0
  23. package/lib/core/local-plugin-pack.js +18 -3
  24. package/lib/core/mcp-connector-store.d.ts +13 -0
  25. package/lib/core/mcp-connector-store.js +16 -1
  26. package/lib/core/messages-core.d.ts +45 -0
  27. package/lib/core/messages-core.js +92 -0
  28. package/lib/core/messages.d.ts +1 -0
  29. package/lib/core/messages.js +6 -0
  30. package/lib/core/model-tool-plans.d.ts +66 -0
  31. package/lib/core/model-tool-plans.js +128 -0
  32. package/lib/core/model-tools.d.ts +96 -8
  33. package/lib/core/model-tools.js +568 -31
  34. package/lib/core/phase3-host.js +10 -2
  35. package/lib/core/plugin-data-registry.d.ts +1 -1
  36. package/lib/core/plugin-data-registry.js +1 -1
  37. package/lib/core/restore-plan-fingerprint.d.ts +2 -0
  38. package/lib/core/restore-plan-fingerprint.js +18 -0
  39. package/lib/core/restore-resume.d.ts +1 -1
  40. package/lib/core/restore-resume.js +1 -1
  41. package/lib/core/restore.d.ts +5 -0
  42. package/lib/core/restore.js +4 -1
  43. package/lib/core/types.d.ts +12 -0
  44. package/lib/foreign/claude-sessions.d.ts +1 -1
  45. package/lib/foreign/claude-sessions.js +1 -1
  46. package/lib/foreign/registry.js +1 -1
  47. package/lib/foreign/types.d.ts +1 -1
  48. package/lib/index.d.ts +15 -15
  49. package/lib/index.js +60 -67
  50. package/lib/market/git-file-writer.js +1 -1
  51. package/lib/market/github-repos.js +1 -1
  52. package/lib/market/market-config.js +1 -1
  53. package/lib/market/messages-market.js +1 -1
  54. package/lib/market/my-repo.js +1 -1
  55. package/lib/market/prepare.js +1 -1
  56. package/lib/market/reader.js +2 -2
  57. package/lib/market/repo-url.d.ts +1 -1
  58. package/lib/market/security.js +2 -2
  59. package/lib/market/star-cache.d.ts +1 -1
  60. package/lib/market/types.d.ts +1 -1
  61. package/lib/market/types.js +1 -1
  62. package/lib/market/url.js +1 -1
  63. package/lib/routes/backup.js +17 -9
  64. package/lib/routes/index.d.ts +1 -1
  65. package/lib/routes/kit.d.ts +2 -2
  66. package/lib/routes/market.d.ts +1 -1
  67. package/lib/routes/market.js +3 -60
  68. package/lib/routes/model-tools-deps.d.ts +41 -0
  69. package/lib/routes/model-tools-deps.js +18 -0
  70. package/lib/routes/recovery.js +5 -3
  71. package/lib/routes/route-support.d.ts +18 -26
  72. package/lib/routes/route-support.js +19 -32
  73. package/lib/routes/snapshots.d.ts +1 -1
  74. package/lib/routes/snapshots.js +4 -40
  75. package/lib/routes/sync.js +40 -28
  76. package/lib/schema/index.d.ts +2 -2
  77. package/lib/schema/index.js +2 -2
  78. package/lib/schema/section-registry.d.ts +5 -3
  79. package/lib/schema/section-registry.js +7 -3
  80. package/lib/security/secret-scanner.js +1 -1
  81. package/lib/sync/apply-bundle.d.ts +50 -0
  82. package/lib/sync/apply-bundle.js +60 -0
  83. package/lib/sync/autosync-scheduler.d.ts +9 -0
  84. package/lib/sync/autosync-scheduler.js +118 -89
  85. package/lib/sync/backup-scheduler.js +5 -4
  86. package/lib/sync/blob-store.d.ts +2 -2
  87. package/lib/sync/git/git-transport.d.ts +12 -0
  88. package/lib/sync/git/git-transport.js +50 -2
  89. package/lib/sync/merge.d.ts +2 -0
  90. package/lib/sync/merge.js +28 -0
  91. package/lib/sync/messages.d.ts +4 -0
  92. package/lib/sync/messages.js +9 -2
  93. package/lib/sync/s3/s3-transport.d.ts +1 -1
  94. package/lib/sync/s3/s3-transport.js +1 -1
  95. package/lib/sync/sync-engine.d.ts +4 -4
  96. package/lib/sync/sync-engine.js +25 -26
  97. package/lib/sync/sync-scope-boot.d.ts +34 -0
  98. package/lib/sync/sync-scope-boot.js +71 -0
  99. package/lib/sync/sync-scope.d.ts +103 -0
  100. package/lib/sync/sync-scope.js +229 -0
  101. package/lib/sync/sync-selection.d.ts +17 -4
  102. package/lib/sync/sync-selection.js +40 -14
  103. package/lib/sync/transport.d.ts +4 -1
  104. package/lib/sync/transport.js +6 -0
  105. package/lib/sync/webdav/webdav-transport.js +1 -1
  106. package/lib/sync/workspace-merge.d.ts +36 -0
  107. package/lib/sync/workspace-merge.js +152 -0
  108. package/lib/ui/backup-schedule.d.ts +13 -2
  109. package/lib/ui/backup-schedule.js +3 -0
  110. package/lib/ui/snapshots-view.d.ts +16 -0
  111. package/lib/ui/snapshots-view.js +21 -0
  112. package/lib/utils/env-lock.d.ts +96 -0
  113. package/lib/utils/env-lock.js +332 -111
  114. package/lib/utils/messages-utils.d.ts +6 -0
  115. package/lib/utils/messages-utils.js +17 -1
  116. package/lib/utils/patch-yaml.js +1 -1
  117. package/lib/utils/zip.js +1 -1
  118. package/package.json +3 -2
  119. package/src/adapters/mcp.test.ts +28 -0
  120. package/src/adapters/mcp.ts +33 -0
  121. package/src/adapters/plugins.test.ts +49 -0
  122. package/src/adapters/plugins.ts +9 -1
  123. package/src/adapters/pnpm-workspace.ts +1 -1
  124. package/src/adapters/self.test.ts +39 -1
  125. package/src/adapters/self.ts +20 -2
  126. package/src/adapters/workspaces.ts +1 -1
  127. package/src/cli/restore-resume.ts +1 -1
  128. package/src/cli/web/page.ts +2 -2
  129. package/src/client/about/AboutPanel.tsx +1 -1
  130. package/src/client/about/about-view.test.ts +1 -1
  131. package/src/client/about/about-view.ts +1 -1
  132. package/src/client/api.ts +1 -6
  133. package/src/client/chat/chat-entry-view.test.ts +1 -1
  134. package/src/client/chat/chat-slots.ts +1 -1
  135. package/src/client/chat/rollback-diff-tab.ts +1 -1
  136. package/src/client/client-types.ts +1 -1
  137. package/src/client/common/routes.ts +1 -5
  138. package/src/client/css-scale-guard.test.ts +1 -1
  139. package/src/client/home/HomePanel.tsx +3 -0
  140. package/src/client/library/ArtifactRow.tsx +14 -2
  141. package/src/client/library/LibraryPanel.tsx +44 -0
  142. package/src/client/locales/overview.ts +10 -2
  143. package/src/client/locales/sessions.ts +3 -0
  144. package/src/client/market/MyConfigsView.tsx +1 -1
  145. package/src/client/market/market-api.ts +2 -14
  146. package/src/client/market/market-locales.ts +1 -1
  147. package/src/client/market/market-view.ts +3 -3
  148. package/src/client/market/my-configs-api.ts +1 -1
  149. package/src/client/market/my-configs-view.ts +1 -1
  150. package/src/client/recovery/recovery-locales.test.ts +1 -1
  151. package/src/client/run-store.ts +1 -1
  152. package/src/client/sync/SyncSectionPickerDialog.tsx +0 -5
  153. package/src/client/sync/history-model.test.ts +28 -7
  154. package/src/client/sync/history-model.ts +6 -3
  155. package/src/client/sync/sync-locales.ts +10 -4
  156. package/src/client/sync/sync-view.ts +1 -1
  157. package/src/core/analyzer-cache.test.ts +1 -1
  158. package/src/core/backup.test.ts +45 -1
  159. package/src/core/backup.ts +25 -0
  160. package/src/core/disk-usage.test.ts +2 -0
  161. package/src/core/env-lock-lifecycle.ts +33 -3
  162. package/src/core/external-failure.test.ts +60 -0
  163. package/src/core/external-failure.ts +54 -0
  164. package/src/core/integrity-extra-entries.test.ts +1 -1
  165. package/src/core/local-plugin-host.test.ts +134 -0
  166. package/src/core/local-plugin-host.ts +122 -17
  167. package/src/core/local-plugin-pack.ts +39 -3
  168. package/src/core/mcp-connector-store.ts +26 -1
  169. package/src/core/messages-core.ts +94 -0
  170. package/src/core/messages.ts +6 -0
  171. package/src/core/model-tool-plans.test.ts +111 -0
  172. package/src/core/model-tool-plans.ts +170 -0
  173. package/src/core/model-tools.test.ts +212 -12
  174. package/src/core/model-tools.ts +710 -38
  175. package/src/core/phase3-host.ts +10 -2
  176. package/src/core/phase3-production-integration.test.ts +43 -0
  177. package/src/core/plugin-data-registry.ts +1 -1
  178. package/src/core/restore-plan-fingerprint.ts +19 -0
  179. package/src/core/restore-resume.ts +1 -1
  180. package/src/core/restore.ts +9 -1
  181. package/src/core/types.ts +12 -0
  182. package/src/foreign/claude-sessions.ts +3 -3
  183. package/src/foreign/registry.ts +1 -1
  184. package/src/foreign/types.ts +1 -1
  185. package/src/index.ts +91 -98
  186. package/src/market/git-file-writer.ts +1 -1
  187. package/src/market/github-repos.test.ts +1 -1
  188. package/src/market/github-repos.ts +1 -1
  189. package/src/market/market-config.ts +1 -1
  190. package/src/market/messages-market.ts +1 -1
  191. package/src/market/my-repo.test.ts +1 -1
  192. package/src/market/my-repo.ts +1 -1
  193. package/src/market/prepare.test.ts +1 -1
  194. package/src/market/prepare.ts +1 -1
  195. package/src/market/reader.ts +2 -2
  196. package/src/market/repo-url.ts +1 -1
  197. package/src/market/security.ts +2 -2
  198. package/src/market/star-cache.ts +1 -1
  199. package/src/market/types.ts +1 -1
  200. package/src/market/url.test.ts +1 -1
  201. package/src/market/url.ts +1 -1
  202. package/src/profiles/dsh-profile-copy-rollback.test.ts +1 -1
  203. package/src/routes/backup.ts +16 -9
  204. package/src/routes/index.ts +1 -1
  205. package/src/routes/kit.ts +2 -2
  206. package/src/routes/market.ts +2 -59
  207. package/src/routes/model-tools-deps.ts +56 -0
  208. package/src/routes/recovery.ts +5 -3
  209. package/src/routes/route-support.ts +49 -36
  210. package/src/routes/snapshots.ts +4 -40
  211. package/src/routes/sync.ts +39 -27
  212. package/src/schema/index.ts +2 -2
  213. package/src/schema/manifest-schema.test.ts +2 -2
  214. package/src/schema/registry.test.ts +1 -1
  215. package/src/schema/section-registry.ts +9 -4
  216. package/src/security/redaction.test.ts +1 -1
  217. package/src/security/secret-scanner.test.ts +2 -2
  218. package/src/security/secret-scanner.ts +1 -1
  219. package/src/sync/apply-bundle.ts +104 -0
  220. package/src/sync/autosync-scheduler.test.ts +180 -6
  221. package/src/sync/autosync-scheduler.ts +132 -92
  222. package/src/sync/backup-scheduler.test.ts +11 -4
  223. package/src/sync/backup-scheduler.ts +5 -4
  224. package/src/sync/blob-store.ts +2 -2
  225. package/src/sync/git/git-transport.test.ts +62 -0
  226. package/src/sync/git/git-transport.ts +54 -3
  227. package/src/sync/merge.ts +29 -0
  228. package/src/sync/messages.ts +9 -2
  229. package/src/sync/s3/s3-transport.ts +1 -1
  230. package/src/sync/sync-engine.test.ts +238 -2
  231. package/src/sync/sync-engine.ts +25 -26
  232. package/src/sync/sync-preview-push.test.ts +2 -1
  233. package/src/sync/sync-scope-boot.ts +87 -0
  234. package/src/sync/sync-scope.test.ts +154 -0
  235. package/src/sync/sync-scope.ts +256 -0
  236. package/src/sync/sync-selection.test.ts +76 -2
  237. package/src/sync/sync-selection.ts +40 -13
  238. package/src/sync/transport.test.ts +21 -1
  239. package/src/sync/transport.ts +12 -0
  240. package/src/sync/webdav/webdav-delete-index-unreadable.test.ts +2 -2
  241. package/src/sync/webdav/webdav-transport.ts +1 -1
  242. package/src/sync/workspace-merge.test.ts +110 -0
  243. package/src/sync/workspace-merge.ts +180 -0
  244. package/src/ui/backup-schedule.test.ts +5 -0
  245. package/src/ui/backup-schedule.ts +21 -2
  246. package/src/ui/session-issue-labels.ts +1 -1
  247. package/src/ui/snapshots-view.test.ts +32 -0
  248. package/src/ui/snapshots-view.ts +34 -0
  249. package/src/utils/env-lock.test.ts +242 -9
  250. package/src/utils/env-lock.ts +247 -13
  251. package/src/utils/messages-utils.ts +17 -1
  252. package/src/utils/patch-yaml.ts +1 -1
  253. package/src/utils/zip.ts +1 -1
  254. package/src/client/CLIENT_DEPENDENCIES.md +0 -94
@@ -36,6 +36,8 @@ import {
36
36
  OWNERSHIP_FILE,
37
37
  HEARTBEAT_PREFIX,
38
38
  RECOVERING_PREFIX,
39
+ LOCK_BLOCK_MESSAGE,
40
+ lockSkipToken,
39
41
  type LockOwnershipRecord,
40
42
  type ProcessIdentityProbe,
41
43
  type EnvLockManagerOptions,
@@ -113,12 +115,15 @@ function makeIo(): {
113
115
  failOpenWhen(p: (p: string) => boolean, code?: string): void;
114
116
  failUnlinkWhen(p: (p: string) => boolean): void;
115
117
  failReadWhen(p: (p: string) => boolean): void;
118
+ /** 注入 rename 故障(quarantine / recover 的捕获 rename 都在这里) */
119
+ failRenameWhen(p: (p: string) => boolean): void;
116
120
  hookRename(fn: (a: string, b: string) => void | Promise<void>): void;
117
121
  clear(): void;
118
122
  } {
119
123
  const openFaults: Array<{ p: (p: string) => boolean; code?: string }> = [];
120
124
  const unlinkFaults: Array<(p: string) => boolean> = [];
121
125
  const readFaults: Array<(p: string) => boolean> = [];
126
+ const renameFaults: Array<(p: string) => boolean> = [];
122
127
  const renameHooks: Array<(a: string, b: string) => void | Promise<void>> = [];
123
128
  const io: import('./env-lock.ts').EnvLockIo = {
124
129
  async mkdir(d, o) { await fs.mkdir(d, o); },
@@ -130,7 +135,15 @@ function makeIo(): {
130
135
  }
131
136
  return fs.open(p, flag as never, mode) as never;
132
137
  },
133
- async rename(a, b) { await fs.rename(a, b); for (const h of renameHooks) await h(a, b); },
138
+ async rename(a, b) {
139
+ for (const f of renameFaults) if (f(a)) {
140
+ const e = new Error('injected rename fault') as NodeJS.ErrnoException;
141
+ e.code = 'EPERM';
142
+ throw e;
143
+ }
144
+ await fs.rename(a, b);
145
+ for (const h of renameHooks) await h(a, b);
146
+ },
134
147
  async unlink(p) {
135
148
  for (const f of unlinkFaults) if (f(p)) {
136
149
  const e = new Error('injected unlink fault') as NodeJS.ErrnoException;
@@ -152,8 +165,9 @@ function makeIo(): {
152
165
  failOpenWhen(p, code) { openFaults.push({ p, code }); },
153
166
  failUnlinkWhen(p) { unlinkFaults.push(p); },
154
167
  failReadWhen(p) { readFaults.push(p); },
168
+ failRenameWhen(p) { renameFaults.push(p); },
155
169
  hookRename(fn) { renameHooks.push(fn); },
156
- clear() { openFaults.length = 0; unlinkFaults.length = 0; readFaults.length = 0; },
170
+ clear() { openFaults.length = 0; unlinkFaults.length = 0; readFaults.length = 0; renameFaults.length = 0; },
157
171
  };
158
172
  }
159
173
 
@@ -335,24 +349,32 @@ process.exit(1); // 期望被拒:非 0 指示「未获得锁」;父进程根
335
349
  await parent.release(pres.token!);
336
350
  });
337
351
 
338
- test('§11.1-c4 release:close→unlink;unlink 失败抛 EnvironmentLockIOError 且保留 activeToken(可重试)', async (t) => {
352
+ test('§11.1-c4 release:unlink 失败先 quarantine 腾空正式路径(不抛);rename 也失败才抛错并保留 activeToken', async (t) => {
339
353
  const locksDir = tmp(t);
340
354
  const ctl = makeIo();
341
- const mgr = lockManager(t, { locksDir, io: ctl.io });
355
+ const mgr = lockManager(t, { locksDir, io: ctl.io, heartbeatIntervalMs: 40 });
342
356
  const res = await mgr.acquire({ op: 'u' });
343
357
  assert.equal(res.state, 'ACQUIRED');
344
- const token = res.token!;
345
- // 注入 unlink(environment.lock) 失败
358
+ // ① 只有 unlink 失败(rename 可用)→ 把记录挪走:正式路径腾空,release 不抛。
359
+ // 真机事故正是「unlink 失败 + 调用方吞错」把锁永久留在盘上,所以这条路径不允许再把锁留在原地。
346
360
  ctl.failUnlinkWhen((p) => p === mgr.ownershipPath);
361
+ await mgr.release(res.token!);
362
+ assert.equal(mgr.isHolding, false, 'quarantine 成功即视为已释放');
363
+ assert.equal(fssync.existsSync(mgr.ownershipPath), false, '正式路径必须腾空(否则下一个 acquire 只是继承僵尸锁)');
364
+
365
+ // ② unlink + rename 双失败 → 抛错并保留 activeToken(仍持有该 inode,可重试)
366
+ const res2 = await mgr.acquire({ op: 'u' });
367
+ assert.equal(res2.state, 'ACQUIRED');
368
+ ctl.failRenameWhen((p) => p === mgr.ownershipPath);
347
369
  await assert.rejects(
348
- () => mgr.release(token),
370
+ () => mgr.release(res2.token!),
349
371
  (e) => e instanceof EnvironmentLockIOError,
350
372
  );
351
- assert.equal(mgr.isHolding, true, 'unlink 失败后必须保留 activeToken(可重试,不卡死磁盘锁)');
373
+ assert.equal(mgr.isHolding, true, '双失败后必须保留 activeToken(可重试,不卡死磁盘锁)');
352
374
  assert.ok(fssync.existsSync(mgr.ownershipPath), '锁文件仍在磁盘(未被误删)');
353
375
  // 恢复后重试 release 成功
354
376
  ctl.clear();
355
- await mgr.release(token);
377
+ await mgr.release(res2.token!);
356
378
  assert.equal(mgr.isHolding, false);
357
379
  assert.equal(fssync.existsSync(mgr.ownershipPath), false, 'release 后 ownership 被删除');
358
380
  });
@@ -1415,3 +1437,214 @@ test('§11.7-g 孤儿心跳清理:只删「非持有者且心跳过期」的 s
1415
1437
  });
1416
1438
 
1417
1439
 
1440
+ /* ------------------------------------------------- §11.8 release 失败留痕与自愈(真机 423 卡死态) */
1441
+
1442
+ /*
1443
+ * 真机现场(v0.4.3):autosync 取锁后 release 的 unlink 失败(Windows 句柄冲突),调用方
1444
+ * `release().catch(() => {})` 把错误吞掉 → ownership 留在盘上、心跳停在 seq=3,而进程仍活
1445
+ * ⇒ inspectLockState 判 LOCKED(owner alive degraded) ⇒ 永不 STALE、显式回收也拒
1446
+ * ⇒ 档案删除等 destructive 操作**永久** 423「另一个任务正在运行」。
1447
+ * 这一组锁定修法的三条边:① 自己删不掉时把记录挪走(quarantine)而不是留原地;
1448
+ * ② 盘上是自己的残留时 acquire 能结构可证地自愈;③ 进程存活但心跳长过期要能被显式回收。
1449
+ */
1450
+
1451
+ test('§11.8-a release 的 unlink 反复失败 → quarantine 腾空正式路径(不抛),随后 acquire 仍可用', async (t) => {
1452
+ const locksDir = tmp(t);
1453
+ const io = makeIo();
1454
+ const m = lockManager(t, { locksDir, io: io.io, heartbeatIntervalMs: 40 });
1455
+ const acq = await m.acquire({ op: 'import' });
1456
+ assert.equal(acq.state, 'ACQUIRED');
1457
+ await waitHeartbeat(m);
1458
+
1459
+ // 模拟「unlink 怎么都删不掉」:只有正式 ownership 路径失败,recovering 副本可删
1460
+ io.failUnlinkWhen((p) => path.basename(p) === OWNERSHIP_FILE);
1461
+ await m.release(acq.token!);
1462
+ assert.equal(m.isHolding, false, 'quarantine 成功即视为已释放');
1463
+ assert.equal(
1464
+ fssync.existsSync(path.join(locksDir, OWNERSHIP_FILE)),
1465
+ false,
1466
+ '正式路径必须腾空 —— 否则下一次 acquire 只会继承同一个僵尸锁',
1467
+ );
1468
+
1469
+ const again = await m.acquire({ op: 'import' });
1470
+ assert.equal(again.state, 'ACQUIRED', '释放失败不得让这个 manager 永久失去写能力');
1471
+ await m.release(again.token!);
1472
+ });
1473
+
1474
+ test('§11.8-b unlink 与 rename 双失败 → release 抛错并保留 token,故障消失后 acquire 自愈回收自己的残留', async (t) => {
1475
+ const locksDir = tmp(t);
1476
+ const io = makeIo();
1477
+ const reports: Array<{ quarantined: boolean }> = [];
1478
+ const m = lockManager(t, {
1479
+ locksDir,
1480
+ io: io.io,
1481
+ heartbeatIntervalMs: 40,
1482
+ onReleaseFailure: (b) => { reports.push({ quarantined: b.quarantined }); },
1483
+ });
1484
+ const acq = await m.acquire({ op: 'import' });
1485
+ assert.equal(acq.state, 'ACQUIRED');
1486
+ await waitHeartbeat(m);
1487
+
1488
+ io.failUnlinkWhen((p) => path.basename(p) === OWNERSHIP_FILE);
1489
+ io.failRenameWhen((p) => path.basename(p) === OWNERSHIP_FILE); // quarantine 的 source 就是它
1490
+ await assert.rejects(() => m.release(acq.token!), (e: unknown) => e instanceof EnvironmentLockIOError);
1491
+ assert.equal(m.isHolding, true, 'unlink/quarantine 全失败 → 保留 token(调用方可重试 release)');
1492
+ assert.deepEqual(reports, [{ quarantined: false }], '失败必须留痕,不许静默');
1493
+
1494
+ // 故障消失:同一个 manager 的下一次 acquire 必须结构可证地回收自己的残留(盘上 instanceId === 自己)
1495
+ io.clear();
1496
+ const again = await m.acquire({ op: 'import' });
1497
+ assert.equal(again.state, 'ACQUIRED', '自己的残留必须自愈,否则每轮 destructive 都是 423');
1498
+ await m.release(again.token!);
1499
+ assert.equal(fssync.existsSync(path.join(locksDir, OWNERSHIP_FILE)), false);
1500
+ assert.equal(m.isHolding, false);
1501
+ });
1502
+
1503
+ test('§11.8-c 进程存活且身份一致但心跳长过期 → 判 stale(heartbeat-stopped),显式回收可用', async (t) => {
1504
+ const locksDir = tmp(t);
1505
+ const clk = makeClock();
1506
+ const p = makeProbe();
1507
+ p.respond(() => ({ alive: true, osProcessStartIdentity: 'identity-A' }));
1508
+ await seedOwnership(locksDir, { instanceId: 'zomb1', pid: 999_010, osIdentity: 'identity-A', op: 'autosync' });
1509
+ await seedHeartbeat(locksDir, 'zomb1', clk.clock() - 120_000);
1510
+ const m = lockManager(t, { locksDir, now: clk.clock, probe: p.probe, staleAfterMs: 1000, longExpiredAfterMs: 5_000 });
1511
+
1512
+ const insp = await m.inspectLockState();
1513
+ assert.equal(insp.state, 'STALE_LOCK_DETECTED', '心跳长过期 = 写方已停摆,不能永远说「另一个任务在运行」');
1514
+ assert.equal(insp.staleKind, 'heartbeat-stopped');
1515
+
1516
+ const blocked = await withMutationLock(m, { op: 'profile-delete' });
1517
+ assert.equal(blocked.context, null);
1518
+ assert.equal(blocked.reason, 'stale-degraded', '文案必须区别于「持有进程已不存在」');
1519
+ assert.notEqual(LOCK_BLOCK_MESSAGE['stale-degraded'], LOCK_BLOCK_MESSAGE.stale);
1520
+ assert.match(LOCK_BLOCK_MESSAGE['stale-degraded'], /回收/);
1521
+
1522
+ const rec = await m.recoverStaleLock();
1523
+ assert.equal(rec.ok, true, '二次验证必须与 inspect 同判据,否则用户点「回收残留锁」只会拿到「失败」');
1524
+ assert.deepEqual(fssync.readdirSync(locksDir), []);
1525
+ });
1526
+
1527
+ test('§11.8-d 心跳未达长过期 + 进程存活 → 仍 LOCKED,acquire 与 recover 都不放行(不放开误删)', async (t) => {
1528
+ const locksDir = tmp(t);
1529
+ const clk = makeClock();
1530
+ const p = makeProbe();
1531
+ p.respond(() => ({ alive: true, osProcessStartIdentity: 'identity-A' }));
1532
+ await seedOwnership(locksDir, { instanceId: 'live1', pid: 999_011, osIdentity: 'identity-A' });
1533
+ await seedHeartbeat(locksDir, 'live1', clk.clock() - 2_000);
1534
+ const m = lockManager(t, { locksDir, now: clk.clock, probe: p.probe, staleAfterMs: 1000, longExpiredAfterMs: 60_000 });
1535
+
1536
+ const insp = await m.inspectLockState();
1537
+ assert.equal(insp.state, 'LOCKED', '活锁就是活锁:心跳过期但没长过期,不能判残留');
1538
+ assert.equal(insp.staleKind, undefined);
1539
+ assert.equal((await withMutationLock(m, { op: 'profile-delete' })).reason, 'locked');
1540
+ assert.equal((await m.recoverStaleLock()).ok, false);
1541
+ assert.ok(fssync.existsSync(path.join(locksDir, OWNERSHIP_FILE)), '拒绝后现场必须原样保留');
1542
+ });
1543
+
1544
+ /* ------------------------------------------- §11.9 长持有看门狗(issue:锁与心跳都不释放) */
1545
+
1546
+ /**
1547
+ * 手动 tick 的假定时器(只实现 LockTimerApi 的两个方法)。
1548
+ * 与真实定时器不同:到点由测试显式 fire(ms),不依赖墙钟与事件循环负载。
1549
+ */
1550
+ function manualTimers(): {
1551
+ api: { setInterval(fn: () => void | Promise<void>, ms: number): unknown; clearInterval(handle: unknown): void };
1552
+ /** 触发所有 ms 匹配且未被清除的定时器一次;返回触发个数 */
1553
+ fire(ms: number): number;
1554
+ /** 仍存活的定时器个数(含 heartbeat) */
1555
+ live(): number;
1556
+ } {
1557
+ const handles: Array<{ id: object; fn: () => void | Promise<void>; ms: number; cleared: boolean }> = [];
1558
+ return {
1559
+ api: {
1560
+ setInterval(fn, ms) { const h = { id: {}, fn, ms, cleared: false }; handles.push(h); return h.id; },
1561
+ clearInterval(handle) { const h = handles.find((x) => x.id === handle); if (h !== undefined) h.cleared = true; },
1562
+ },
1563
+ fire(ms) {
1564
+ let n = 0;
1565
+ for (const h of handles.slice()) {
1566
+ if (h.cleared || h.ms !== ms) continue;
1567
+ h.cleared = true;
1568
+ n += 1;
1569
+ void h.fn();
1570
+ }
1571
+ return n;
1572
+ },
1573
+ live() { return handles.filter((h) => !h.cleared).length; },
1574
+ };
1575
+ }
1576
+
1577
+ test('§11.9-a 长持有看门狗:超过阈值只告警一次(点名 op 与持有时长),绝不强拆活持有者', async (t) => {
1578
+ const locksDir = tmp(t);
1579
+ const timers = manualTimers();
1580
+ let clock = 1_000_000;
1581
+ const seen: Array<{ op: string; heldMs: number; tokenId: string }> = [];
1582
+ const mgr = lockManager(t, {
1583
+ locksDir,
1584
+ timers: timers.api,
1585
+ now: () => clock,
1586
+ heartbeatIntervalMs: 1000,
1587
+ holdWarnAfterMs: 5000,
1588
+ onHoldExceeded: (info) => { seen.push(info); },
1589
+ });
1590
+ const res = await mgr.acquire({ op: 'autosync' });
1591
+ assert.equal(res.state, 'ACQUIRED');
1592
+ assert.equal(seen.length, 0, '刚 acquire 不得告警');
1593
+ clock += 5000;
1594
+ assert.equal(timers.fire(5000), 1, '看门狗必须在阈值到点触发');
1595
+ assert.equal(seen.length, 1);
1596
+ assert.equal(seen[0]!.op, 'autosync', '告警必须点名是哪个 op 卡住了');
1597
+ assert.ok(seen[0]!.heldMs >= 5000, '告警必须给出已持有时长: ' + seen[0]!.heldMs);
1598
+ // 只观察、不解锁:活持有者可能真在跑长任务,强拆会造出双持有(比卡死更难查)
1599
+ assert.equal(mgr.isHolding, true, '看门狗绝不释放锁');
1600
+ assert.notEqual(await readOwnership(locksDir), null, 'ownership 仍在盘上');
1601
+ assert.deepEqual(mgr.holdInfo, { op: 'autosync', heldMs: 5000 }, 'holdInfo 给出「是哪个 op 持有」');
1602
+ // 触发后自行停掉:不得每 tick 重复告警
1603
+ assert.equal(timers.fire(5000), 0, '看门狗命中一次即停');
1604
+ assert.equal(seen.length, 1);
1605
+ await mgr.release(res.token!);
1606
+ });
1607
+
1608
+ test('§11.9-b release 之后看门狗随之失效(停表 + holdInfo 为空)', async (t) => {
1609
+ const locksDir = tmp(t);
1610
+ const timers = manualTimers();
1611
+ let clock = 2_000_000;
1612
+ const seen: unknown[] = [];
1613
+ const mgr = lockManager(t, {
1614
+ locksDir, timers: timers.api, now: () => clock,
1615
+ heartbeatIntervalMs: 1000, holdWarnAfterMs: 5000,
1616
+ onHoldExceeded: (i) => { seen.push(i); },
1617
+ });
1618
+ const res = await mgr.acquire({ op: 'backup' });
1619
+ assert.equal(timers.live(), 2, 'acquire 后应有 heartbeat + 看门狗两只定时器');
1620
+ await mgr.release(res.token!);
1621
+ assert.equal(timers.live(), 0, 'release 必须停掉全部定时器(含看门狗)');
1622
+ assert.equal(mgr.holdInfo, null);
1623
+ clock += 60_000;
1624
+ assert.equal(timers.fire(5000), 0);
1625
+ assert.equal(seen.length, 0, '已释放的持有不得再告警');
1626
+ });
1627
+
1628
+ test('§11.9-c holdWarnAfterMs=0 关闭看门狗(只留 heartbeat,供不需要告警的宿主)', async (t) => {
1629
+ const locksDir = tmp(t);
1630
+ const timers = manualTimers();
1631
+ const mgr = lockManager(t, { locksDir, timers: timers.api, heartbeatIntervalMs: 1000, holdWarnAfterMs: 0 });
1632
+ const res = await mgr.acquire({ op: 'export' });
1633
+ assert.equal(timers.live(), 1, '关闭时只剩 heartbeat');
1634
+ assert.equal(timers.fire(5000), 0);
1635
+ await mgr.release(res.token!);
1636
+ });
1637
+
1638
+ test('§11.9-d lockSkipToken:锁分类 → 稳定机器 token(只有真被活任务占用才叫 mutation-locked)', () => {
1639
+ // issue:此前所有被挡路径都发 'mutation-locked',客户端只能写「被占用或残留」,
1640
+ // 真机 locks 目录为空时这句文案把排查方向带偏。分档后一类一个 token。
1641
+ assert.equal(lockSkipToken('blocked'), 'mutation-safe-mode');
1642
+ assert.equal(lockSkipToken('locked'), 'mutation-locked');
1643
+ assert.equal(lockSkipToken('stale'), 'mutation-lock-stale');
1644
+ assert.equal(lockSkipToken('stale-degraded'), 'mutation-lock-stale');
1645
+ assert.equal(lockSkipToken('unavailable'), 'mutation-lock-unavailable');
1646
+ // acquire 直接抛错(未拿到分类)→ 不可用,而不是谎称被占用
1647
+ assert.equal(lockSkipToken(undefined), 'mutation-lock-unavailable');
1648
+ });
1649
+
1650
+