dsh-config-manager 0.8.2 → 0.9.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 (345) hide show
  1. package/lib/adapters/mcp.js +42 -13
  2. package/lib/adapters/messages.d.ts +1 -0
  3. package/lib/adapters/messages.js +2 -0
  4. package/lib/adapters/patch-key-guard.d.ts +23 -0
  5. package/lib/adapters/patch-key-guard.js +13 -0
  6. package/lib/adapters/plugin-data.d.ts +19 -5
  7. package/lib/adapters/plugin-data.js +76 -22
  8. package/lib/adapters/plugins.d.ts +14 -1
  9. package/lib/adapters/plugins.js +147 -4
  10. package/lib/cli/actions.d.ts +7 -105
  11. package/lib/cli/actions.js +16 -172
  12. package/lib/cli/control-state.d.ts +54 -0
  13. package/lib/cli/control-state.js +107 -0
  14. package/lib/cli/index.js +14 -0
  15. package/lib/cli/write-gates.d.ts +61 -0
  16. package/lib/cli/write-gates.js +110 -0
  17. package/lib/client.d.ts +104 -49
  18. package/lib/client.js +12536 -12006
  19. package/lib/core/backup.d.ts +0 -4
  20. package/lib/core/backup.js +21 -4
  21. package/lib/core/boot-rescue.d.ts +1 -1
  22. package/lib/core/boot-rescue.js +12 -1
  23. package/lib/core/boot-safety.js +37 -2
  24. package/lib/core/conflict-decisions.js +2 -1
  25. package/lib/core/exporter.js +22 -1
  26. package/lib/core/journal.d.ts +54 -4
  27. package/lib/core/journal.js +158 -35
  28. package/lib/core/local-plugin-host.d.ts +29 -0
  29. package/lib/core/local-plugin-host.js +55 -48
  30. package/lib/core/messages.d.ts +7 -0
  31. package/lib/core/messages.js +14 -0
  32. package/lib/core/migration-history.js +16 -9
  33. package/lib/core/model-tools.d.ts +2 -2
  34. package/lib/core/model-tools.js +14 -2
  35. package/lib/core/patch-secret-guard.d.ts +49 -0
  36. package/lib/core/patch-secret-guard.js +164 -0
  37. package/lib/core/reconcile.d.ts +8 -0
  38. package/lib/core/reconcile.js +18 -3
  39. package/lib/core/reinstall.d.ts +2 -0
  40. package/lib/core/reinstall.js +6 -2
  41. package/lib/core/restore.js +31 -6
  42. package/lib/core/section-sanitize.d.ts +32 -0
  43. package/lib/core/section-sanitize.js +13 -0
  44. package/lib/core/snapshot-diff.d.ts +30 -0
  45. package/lib/core/snapshot-diff.js +56 -20
  46. package/lib/core/startup-barrier.js +8 -2
  47. package/lib/core/types.d.ts +22 -0
  48. package/lib/core/update-check.js +51 -2
  49. package/lib/foreign/kernel.js +9 -2
  50. package/lib/foreign/read-claude-plugins.js +29 -11
  51. package/lib/foreign/read-dsh.js +31 -2
  52. package/lib/foreign/session-ir.js +50 -9
  53. package/lib/foreign/session-read.d.ts +8 -0
  54. package/lib/foreign/session-read.js +26 -0
  55. package/lib/index.d.ts +1 -1
  56. package/lib/index.js +1 -1
  57. package/lib/profiles/dsh-profile-io.d.ts +20 -0
  58. package/lib/profiles/dsh-profile-io.js +27 -0
  59. package/lib/profiles/dsh-profile-manager.d.ts +27 -0
  60. package/lib/profiles/dsh-profile-manager.js +45 -4
  61. package/lib/profiles/dsh-profile-runtime.d.ts +5 -1
  62. package/lib/profiles/dsh-profile-runtime.js +16 -3
  63. package/lib/profiles/process-control.d.ts +18 -1
  64. package/lib/profiles/process-control.js +25 -1
  65. package/lib/routes/profiles.js +2 -1
  66. package/lib/routes/sync.js +13 -1
  67. package/lib/schema/types.d.ts +11 -0
  68. package/lib/security/redaction.js +22 -6
  69. package/lib/sync/autosync-repeat.d.ts +27 -0
  70. package/lib/sync/autosync-repeat.js +23 -0
  71. package/lib/sync/autosync-scheduler.d.ts +34 -1
  72. package/lib/sync/autosync-scheduler.js +151 -43
  73. package/lib/sync/baseline-heal.js +7 -0
  74. package/lib/sync/baseline-record.d.ts +67 -0
  75. package/lib/sync/baseline-record.js +74 -0
  76. package/lib/sync/blob-store.js +13 -0
  77. package/lib/sync/content-diff.d.ts +39 -0
  78. package/lib/sync/content-diff.js +46 -0
  79. package/lib/sync/git/git-transport.d.ts +32 -1
  80. package/lib/sync/git/git-transport.js +146 -26
  81. package/lib/sync/github-auth.d.ts +17 -1
  82. package/lib/sync/github-auth.js +37 -1
  83. package/lib/sync/merge-apply-plan.d.ts +38 -0
  84. package/lib/sync/merge-apply-plan.js +76 -0
  85. package/lib/sync/messages.d.ts +1 -0
  86. package/lib/sync/messages.js +2 -0
  87. package/lib/sync/pending-merge.d.ts +34 -0
  88. package/lib/sync/pending-merge.js +53 -0
  89. package/lib/sync/s3/s3-transport.d.ts +14 -2
  90. package/lib/sync/s3/s3-transport.js +72 -48
  91. package/lib/sync/sync-engine.d.ts +23 -40
  92. package/lib/sync/sync-engine.js +115 -132
  93. package/lib/sync/sync-history.d.ts +7 -0
  94. package/lib/sync/sync-state.d.ts +28 -0
  95. package/lib/sync/sync-state.js +20 -0
  96. package/lib/sync/transport.d.ts +12 -3
  97. package/lib/sync/transport.js +13 -4
  98. package/lib/sync/webdav/webdav-transport.d.ts +19 -2
  99. package/lib/sync/webdav/webdav-transport.js +81 -40
  100. package/lib/ui/commands.js +5 -11
  101. package/lib/ui/dsh-profiles-view.d.ts +8 -3
  102. package/lib/ui/dsh-profiles-view.js +9 -5
  103. package/lib/ui/i18n.d.ts +2 -1
  104. package/lib/ui/i18n.js +4 -2
  105. package/lib/ui/menu-placement.js +1 -1
  106. package/lib/ui/migration-consult-view.d.ts +11 -0
  107. package/lib/ui/migration-consult-view.js +15 -0
  108. package/lib/ui/overview-view.d.ts +7 -0
  109. package/lib/ui/overview-view.js +3 -0
  110. package/lib/ui/report.d.ts +9 -0
  111. package/lib/ui/report.js +17 -4
  112. package/lib/ui/selection-model.d.ts +8 -0
  113. package/lib/ui/selection-model.js +16 -0
  114. package/lib/utils/patch-yaml.d.ts +39 -1
  115. package/lib/utils/patch-yaml.js +82 -2
  116. package/lib/utils/paths.d.ts +13 -0
  117. package/lib/utils/paths.js +35 -0
  118. package/lib/utils/workdir-lock.js +18 -3
  119. package/package.json +2 -1
  120. package/src/adapters/mcp.test.ts +29 -0
  121. package/src/adapters/mcp.ts +43 -12
  122. package/src/adapters/messages.ts +2 -0
  123. package/src/adapters/patch-key-guard.ts +29 -0
  124. package/src/adapters/plugin-data.test.ts +126 -0
  125. package/src/adapters/plugin-data.ts +97 -26
  126. package/src/adapters/plugins.test.ts +288 -0
  127. package/src/adapters/plugins.ts +141 -4
  128. package/src/cli/actions.test.ts +74 -1
  129. package/src/cli/actions.ts +27 -246
  130. package/src/cli/control-state.ts +143 -0
  131. package/src/cli/index.ts +18 -0
  132. package/src/cli/web/web.test.ts +27 -0
  133. package/src/cli/write-gates.ts +149 -0
  134. package/src/client/about/AboutPanel.tsx +7 -6
  135. package/src/client/about/release-notes-view.test.ts +17 -0
  136. package/src/client/about/release-notes-view.ts +49 -24
  137. package/src/client/chat/ChatEntryView.tsx +2 -2
  138. package/src/client/chat/rollback-entry-view.ts +6 -1
  139. package/src/client/common/CommandPalette.tsx +6 -3
  140. package/src/client/common/ConfirmDialog.tsx +29 -12
  141. package/src/client/common/ContentPicker.tsx +13 -12
  142. package/src/client/common/Modal.tsx +12 -5
  143. package/src/client/common/MoreMenu.tsx +4 -1
  144. package/src/client/common/RunsCenter.tsx +103 -32
  145. package/src/client/common/Select.tsx +2 -2
  146. package/src/client/common/floating-bounds.ts +1 -1
  147. package/src/client/common/floating-units.test.ts +1 -2
  148. package/src/client/common/plan-text-redaction.test.ts +188 -3
  149. package/src/client/common/runs-center-guard.test.ts +60 -0
  150. package/src/client/common/runs-center-view.test.ts +128 -0
  151. package/src/client/common/runs-center-view.ts +96 -0
  152. package/src/client/common/section-title-action.test.ts +66 -0
  153. package/src/client/common/ui.tsx +93 -30
  154. package/src/client/config-manager.module.css +1507 -272
  155. package/src/client/consult/ConsultCard.tsx +7 -6
  156. package/src/client/environment/EnvironmentPanel.tsx +20 -21
  157. package/src/client/environment/maintenance/DiskUsageCard.tsx +65 -91
  158. package/src/client/environment/profiles-incomplete-ui.test.ts +8 -8
  159. package/src/client/export/ExportView.tsx +7 -16
  160. package/src/client/history/HistoryPanel.tsx +88 -28
  161. package/src/client/history/history-locales.ts +3 -3
  162. package/src/client/home/BackupScheduleCard.tsx +35 -30
  163. package/src/client/home/HomePanel.tsx +45 -10
  164. package/src/client/import/ConflictList.tsx +13 -13
  165. package/src/client/import/ForeignImportView.tsx +7 -6
  166. package/src/client/import/ImportWizardView.tsx +2 -3
  167. package/src/client/import/PathMappingForm.tsx +4 -4
  168. package/src/client/import/PathPickerDialog.tsx +2 -1
  169. package/src/client/import/import-wizard-steps.tsx +51 -39
  170. package/src/client/import/use-import-wizard-controller.test.ts +129 -0
  171. package/src/client/import/use-import-wizard-controller.ts +116 -22
  172. package/src/client/library/ArtifactRow.tsx +2 -2
  173. package/src/client/library/BackupInspectView.tsx +19 -15
  174. package/src/client/library/LibraryActions.tsx +56 -16
  175. package/src/client/library/LibraryPanel.tsx +7 -7
  176. package/src/client/library/RestorePlanView.tsx +13 -8
  177. package/src/client/locales/about.ts +0 -2
  178. package/src/client/locales/backup.ts +10 -12
  179. package/src/client/locales/common.ts +2 -0
  180. package/src/client/locales/export.ts +5 -12
  181. package/src/client/locales/foreign.ts +2 -2
  182. package/src/client/locales/import.ts +3 -2
  183. package/src/client/locales/overview.ts +4 -0
  184. package/src/client/locales/profiles.ts +32 -4
  185. package/src/client/locales/restore.ts +0 -2
  186. package/src/client/locales/sessions.ts +0 -2
  187. package/src/client/locales/shell.ts +2 -2
  188. package/src/client/market/MarketPanel.tsx +38 -20
  189. package/src/client/market/MyConfigsList.tsx +34 -32
  190. package/src/client/market/MyConfigsLoginCard.tsx +12 -15
  191. package/src/client/market/MyConfigsView.tsx +5 -5
  192. package/src/client/market/MyConfigsWizard.tsx +7 -12
  193. package/src/client/market/market-locales.ts +0 -6
  194. package/src/client/market/my-configs-split.test.ts +2 -1
  195. package/src/client/nav-menu-clip-guard.test.ts +52 -0
  196. package/src/client/recovery/IncompleteCopiesSection.tsx +1 -3
  197. package/src/client/recovery/RecoveryPanel.tsx +31 -16
  198. package/src/client/recovery/RollbackCard.tsx +58 -16
  199. package/src/client/recovery/RollbackFileChanges.tsx +40 -27
  200. package/src/client/recovery/SessionHealthCard.tsx +2 -2
  201. package/src/client/recovery/SessionHealthDialog.tsx +5 -7
  202. package/src/client/recovery/SessionLayoutSection.tsx +3 -5
  203. package/src/client/recovery/recovery-locales.ts +12 -8
  204. package/src/client/run-store.test.ts +30 -0
  205. package/src/client/run-store.ts +21 -1
  206. package/src/client/sync/AddChannelCard.tsx +65 -0
  207. package/src/client/sync/AutosyncCard.tsx +182 -108
  208. package/src/client/sync/ChannelClearConfirmDialog.tsx +5 -1
  209. package/src/client/sync/ChannelConfigDialog.tsx +27 -48
  210. package/src/client/sync/MergePreviewView.tsx +3 -5
  211. package/src/client/sync/PushReportSections.tsx +35 -0
  212. package/src/client/sync/SyncChannelEntryCard.tsx +33 -24
  213. package/src/client/sync/SyncConfirmView.tsx +3 -3
  214. package/src/client/sync/SyncLogList.tsx +16 -5
  215. package/src/client/sync/SyncPanel.tsx +153 -159
  216. package/src/client/sync/SyncProbeRow.tsx +5 -7
  217. package/src/client/sync/SyncRepositoryPicker.tsx +17 -4
  218. package/src/client/sync/SyncSecuritySection.tsx +157 -0
  219. package/src/client/sync/SyncSettingsDialog.tsx +304 -0
  220. package/src/client/sync/SyncSettingsView.test.ts +26 -8
  221. package/src/client/sync/WorkDirCard.tsx +2 -4
  222. package/src/client/sync/config-clear-guard.test.ts +94 -0
  223. package/src/client/sync/config-clear-guard.ts +98 -0
  224. package/src/client/sync/config-dirty.test.ts +91 -0
  225. package/src/client/sync/config-dirty.ts +88 -0
  226. package/src/client/sync/history-model.test.ts +2 -0
  227. package/src/client/sync/history-model.ts +3 -0
  228. package/src/client/sync/merge-request.ts +20 -0
  229. package/src/client/sync/sync-api.ts +12 -0
  230. package/src/client/sync/sync-locales.ts +87 -47
  231. package/src/client/sync/sync-panel-clear.test.ts +44 -0
  232. package/src/client/sync/sync-view.test.ts +20 -0
  233. package/src/client/sync/sync-view.ts +42 -4
  234. package/src/core/backup.test.ts +61 -1
  235. package/src/core/backup.ts +21 -4
  236. package/src/core/boot-rescue.test.ts +35 -0
  237. package/src/core/boot-rescue.ts +13 -2
  238. package/src/core/boot-safety.test.ts +25 -0
  239. package/src/core/boot-safety.ts +37 -2
  240. package/src/core/conflict-decisions.ts +2 -1
  241. package/src/core/exporter.ts +21 -1
  242. package/src/core/journal.test.ts +183 -0
  243. package/src/core/journal.ts +163 -25
  244. package/src/core/local-plugin-host.test.ts +68 -2
  245. package/src/core/local-plugin-host.ts +31 -4
  246. package/src/core/messages.ts +14 -0
  247. package/src/core/migration-history.test.ts +27 -0
  248. package/src/core/migration-history.ts +12 -4
  249. package/src/core/model-tools.test.ts +67 -0
  250. package/src/core/model-tools.ts +16 -4
  251. package/src/core/patch-secret-guard.test.ts +110 -0
  252. package/src/core/patch-secret-guard.ts +176 -0
  253. package/src/core/reconcile.ts +32 -4
  254. package/src/core/reinstall.ts +8 -2
  255. package/src/core/restore.ts +32 -7
  256. package/src/core/section-sanitize.ts +37 -0
  257. package/src/core/snapshot-diff.test.ts +73 -1
  258. package/src/core/snapshot-diff.ts +66 -21
  259. package/src/core/startup-barrier.test.ts +64 -0
  260. package/src/core/startup-barrier.ts +8 -2
  261. package/src/core/types.ts +27 -1
  262. package/src/core/update-check.test.ts +40 -0
  263. package/src/core/update-check.ts +53 -2
  264. package/src/foreign/claude-code.test.ts +5 -0
  265. package/src/foreign/claude-plugins.test.ts +57 -0
  266. package/src/foreign/claude-sessions-bytes.test.ts +7 -1
  267. package/src/foreign/dsh.test.ts +23 -0
  268. package/src/foreign/kernel.ts +9 -2
  269. package/src/foreign/read-claude-plugins.ts +32 -11
  270. package/src/foreign/read-dsh.ts +33 -2
  271. package/src/foreign/session-ir.test.ts +67 -0
  272. package/src/foreign/session-ir.ts +54 -14
  273. package/src/foreign/session-read.ts +23 -0
  274. package/src/index.ts +1 -1
  275. package/src/profiles/dsh-profile-copy-rollback.test.ts +58 -4
  276. package/src/profiles/dsh-profile-io.ts +29 -0
  277. package/src/profiles/dsh-profile-manager.test.ts +30 -0
  278. package/src/profiles/dsh-profile-manager.ts +53 -5
  279. package/src/profiles/dsh-profile-runtime.test.ts +29 -7
  280. package/src/profiles/dsh-profile-runtime.ts +17 -3
  281. package/src/profiles/process-control.test.ts +97 -0
  282. package/src/profiles/process-control.ts +28 -1
  283. package/src/routes/profiles.ts +2 -1
  284. package/src/routes/sync.ts +13 -1
  285. package/src/schema/types.ts +16 -1
  286. package/src/security/redaction.test.ts +31 -0
  287. package/src/security/redaction.ts +23 -6
  288. package/src/sync/autosync-repeat.test.ts +58 -0
  289. package/src/sync/autosync-repeat.ts +55 -0
  290. package/src/sync/autosync-scheduler.test.ts +417 -0
  291. package/src/sync/autosync-scheduler.ts +154 -41
  292. package/src/sync/baseline-heal.ts +6 -0
  293. package/src/sync/baseline-record.ts +135 -0
  294. package/src/sync/blob-store.test.ts +16 -0
  295. package/src/sync/blob-store.ts +13 -0
  296. package/src/sync/content-diff.test.ts +95 -0
  297. package/src/sync/content-diff.ts +79 -0
  298. package/src/sync/git/git-transport.test.ts +151 -4
  299. package/src/sync/git/git-transport.ts +143 -27
  300. package/src/sync/github-auth.test.ts +26 -1
  301. package/src/sync/github-auth.ts +37 -1
  302. package/src/sync/merge-apply-plan.ts +117 -0
  303. package/src/sync/messages.ts +2 -0
  304. package/src/sync/pending-merge.ts +77 -0
  305. package/src/sync/s3/s3-delete-and-blob-index.test.ts +249 -0
  306. package/src/sync/s3/s3-transport.ts +76 -42
  307. package/src/sync/sync-config.test.ts +39 -12
  308. package/src/sync/sync-device-identity.test.ts +3 -0
  309. package/src/sync/sync-engine.test.ts +465 -12
  310. package/src/sync/sync-engine.ts +130 -135
  311. package/src/sync/sync-history.ts +7 -0
  312. package/src/sync/sync-state.ts +48 -0
  313. package/src/sync/transport.test.ts +13 -1
  314. package/src/sync/transport.ts +12 -4
  315. package/src/sync/webdav/webdav-delete-and-blob-index.test.ts +249 -0
  316. package/src/sync/webdav/webdav-transport.ts +86 -34
  317. package/src/ui/commands.test.ts +11 -0
  318. package/src/ui/commands.ts +5 -14
  319. package/src/ui/dsh-profiles-view.test.ts +16 -0
  320. package/src/ui/dsh-profiles-view.ts +11 -5
  321. package/src/ui/i18n.ts +4 -2
  322. package/src/ui/menu-placement.ts +1 -1
  323. package/src/ui/migration-consult-view.test.ts +20 -1
  324. package/src/ui/migration-consult-view.ts +17 -0
  325. package/src/ui/overview-view.ts +10 -0
  326. package/src/ui/report.test.ts +25 -1
  327. package/src/ui/report.ts +15 -4
  328. package/src/ui/selection-model.test.ts +15 -1
  329. package/src/ui/selection-model.ts +18 -0
  330. package/src/utils/js-yaml4.d.ts +35 -0
  331. package/src/utils/patch-yaml.test.ts +207 -1
  332. package/src/utils/patch-yaml.ts +98 -2
  333. package/src/utils/paths.test.ts +14 -1
  334. package/src/utils/paths.ts +35 -0
  335. package/src/utils/workdir-lock.test.ts +69 -0
  336. package/src/utils/workdir-lock.ts +17 -3
  337. package/lib/ui/path-mapping.d.ts +0 -38
  338. package/lib/ui/path-mapping.js +0 -75
  339. package/src/client/common/InfoHint.tsx +0 -289
  340. package/src/client/common/info-hint-guard.test.ts +0 -1119
  341. package/src/client/sync/DecryptPasswordCard.tsx +0 -48
  342. package/src/client/sync/SecurityOptionsCard.tsx +0 -97
  343. package/src/client/sync/SyncSectionPickerDialog.tsx +0 -213
  344. package/src/ui/path-mapping.test.ts +0 -67
  345. package/src/ui/path-mapping.ts +0 -103
@@ -19,7 +19,7 @@
19
19
  import { existsSync, mkdirSync, readdirSync, rmSync } from 'node:fs'
20
20
  import { join } from 'node:path'
21
21
  import { atomicWriteFileSync } from '../utils/atomic-write.ts'
22
- import { readTextSafe, sanitizeFilePart } from './dsh-profile-io.ts'
22
+ import { legacyRuntimeFileName, readTextSafe, runtimeFileName } from './dsh-profile-io.ts'
23
23
  import { DshProfileError } from './dsh-profile-manager.ts'
24
24
  import { profilesMsg } from './messages.ts'
25
25
  import { resolveProcessControl, stopPid, type ProcessControlDeps } from './process-control.ts'
@@ -132,9 +132,21 @@ export class DshProfileRuntimeRegistry {
132
132
  return join(this.dataDir, RUNTIME_DIR_NAME)
133
133
  }
134
134
 
135
- /** 某个 profile 的心跳文件绝对路径 */
135
+ /** 某个 profile 的心跳文件绝对路径(名字哈希保证唯一,见 runtimeFileName) */
136
136
  fileFor(name: string): string {
137
- return join(this.dir(), `${sanitizeFilePart(name)}.json`)
137
+ return join(this.dir(), runtimeFileName(name))
138
+ }
139
+
140
+ /** 旧版心跳文件(升级清理用:名字清洗多对一,旧文件可能已被别的 profile 覆盖过) */
141
+ private legacyFileFor(name: string): string {
142
+ return join(this.dir(), legacyRuntimeFileName(name))
143
+ }
144
+
145
+ /** 删掉自己那份旧版心跳(升级后不留双份;不碰别人的文件) */
146
+ private dropLegacyHeartbeat(name: string): void {
147
+ const legacy = this.legacyFileFor(name)
148
+ if (legacy === this.fileFor(name)) return
149
+ rmSync(legacy, { force: true })
138
150
  }
139
151
 
140
152
  /** 自报/刷新心跳(幂等;apply 时写一次,之后由 startHeartbeat 定期刷)。 */
@@ -149,12 +161,14 @@ export class DshProfileRuntimeRegistry {
149
161
  }
150
162
  mkdirSync(this.dir(), { recursive: true })
151
163
  atomicWriteFileSync(this.fileFor(this.name), `${JSON.stringify(record, null, 2)}\n`, { mode: 0o644 })
164
+ this.dropLegacyHeartbeat(this.name)
152
165
  return record
153
166
  }
154
167
 
155
168
  /** 撤回自己的心跳(dispose 时;正常退出的实例不留垃圾文件)。 */
156
169
  withdraw(): void {
157
170
  rmSync(this.fileFor(this.name), { force: true })
171
+ this.dropLegacyHeartbeat(this.name)
158
172
  }
159
173
 
160
174
  /**
@@ -0,0 +1,97 @@
1
+ /**
2
+ * 进程控制内核单测:终止结果的三态(graceful / killed / failed)与**强杀后的有限等待**。
3
+ *
4
+ * 钉住审计 P11-3:Windows 上 `taskkill /F` 返回 ≠ 进程已消失(TerminateProcess 只投递请求),
5
+ * 旧实现在强杀后**立刻**回查一次,把「正在消失」误判成 failed —— 调用方(launcher / runtime)
6
+ * 据此抛 stopFailed 并保留心跳,UI 说「停不掉」,而进程其实已经没了。
7
+ */
8
+ import test from 'node:test'
9
+ import assert from 'node:assert/strict'
10
+
11
+ import {
12
+ PROFILE_FORCE_EXIT_WAIT_MS, resolveProcessControl, stopPid,
13
+ } from './process-control.ts'
14
+
15
+ /** 虚拟时钟 + 可编排存活的进程控制器(sleep 只推进时钟,不真等待) */
16
+ function fixture(opts: {
17
+ graceMs?: number
18
+ forceWaitMs?: number
19
+ /** 收到 force 后经过多少毫秒才消失(null = 永远不死) */
20
+ diesAfterForceMs?: number | null
21
+ /** 优雅期是否自己退出 */
22
+ diesOnGraceful?: boolean
23
+ }) {
24
+ let clock = 1_000_000
25
+ let forceAt: number | null = null
26
+ let gracefulKilled = false
27
+ const killed: string[] = []
28
+ const deps = resolveProcessControl({
29
+ graceMs: opts.graceMs ?? 20,
30
+ ...(opts.forceWaitMs === undefined ? {} : { forceWaitMs: opts.forceWaitMs }),
31
+ sleep: (ms) => { clock += ms; return Promise.resolve() },
32
+ now: () => clock,
33
+ isAlivePid: () => {
34
+ if (opts.diesOnGraceful === true && gracefulKilled) return false
35
+ if (forceAt === null) return true
36
+ const diesAfter = opts.diesAfterForceMs ?? null
37
+ if (diesAfter === null) return true
38
+ return clock - forceAt < diesAfter
39
+ },
40
+ killPid: (_pid, mode) => {
41
+ killed.push(mode)
42
+ if (mode === 'graceful') gracefulKilled = true
43
+ if (mode === 'force') forceAt = clock
44
+ },
45
+ })
46
+ return { deps, killed, clock: () => clock }
47
+ }
48
+
49
+ test('P11-3 回归:强杀后 40ms 才消失 → killed(不再误判 failed)', async () => {
50
+ // 旧行为:killPid('force') 后立刻 isAlivePid → true → 'failed'(而进程 40ms 后确实没了)
51
+ const fx = fixture({ diesAfterForceMs: 40 })
52
+ const outcome = await stopPid(4242, fx.deps)
53
+ assert.equal(outcome, 'killed')
54
+ assert.deepEqual(fx.killed, ['graceful', 'force'], '优雅 → 强杀,各一次')
55
+ // 强杀后至少轮询了一次(时钟被 sleep 推着走,说明等待是「查询 + 短睡」而非一次死判)
56
+ assert.ok(fx.clock() > 1_000_200, '强杀后必须实际等待/轮询过,而不是立刻回查一次')
57
+ })
58
+
59
+ test('P11-3:等待窗口是有限的 —— 怎么都杀不掉 → failed(不死循环)', async () => {
60
+ const fx = fixture({ graceMs: 20, forceWaitMs: 120, diesAfterForceMs: null })
61
+ const outcome = await stopPid(4242, fx.deps)
62
+ assert.equal(outcome, 'failed')
63
+ const waited = fx.clock() - 1_000_000
64
+ assert.ok(waited >= 20 + 120, '至少等满 优雅期 + 强杀等待窗口,实际 ' + String(waited) + 'ms')
65
+ assert.ok(waited < 20 + 120 + 1_000, '不得无限等:' + String(waited) + 'ms')
66
+ })
67
+
68
+ test('P11-3:forceWaitMs=0 时退回「杀完立刻判定」的旧口径(显式关闭等待)', async () => {
69
+ const alive = fixture({ forceWaitMs: 0, diesAfterForceMs: null })
70
+ assert.equal(await stopPid(1, alive.deps), 'failed')
71
+ const dying = fixture({ forceWaitMs: 0, diesAfterForceMs: 0 })
72
+ assert.equal(await stopPid(1, dying.deps), 'killed')
73
+ })
74
+
75
+ test('优雅期生效:自己退出 → graceful,绝不强杀', async () => {
76
+ const fx = fixture({ diesOnGraceful: true })
77
+ assert.equal(await stopPid(7, fx.deps), 'graceful')
78
+ assert.deepEqual(fx.killed, ['graceful'])
79
+ })
80
+
81
+ test('已经不在 → already-stopped,不发任何终止', async () => {
82
+ const killed: string[] = []
83
+ const deps = resolveProcessControl({
84
+ isAlivePid: () => false,
85
+ killPid: (_pid, mode) => { killed.push(mode) },
86
+ sleep: () => Promise.resolve(),
87
+ now: () => 0,
88
+ })
89
+ assert.equal(await stopPid(9, deps), 'already-stopped')
90
+ assert.deepEqual(killed, [])
91
+ })
92
+
93
+ test('缺省强杀等待窗口是有限正数(POSIX/Windows 共用)', () => {
94
+ assert.equal(Number.isFinite(PROFILE_FORCE_EXIT_WAIT_MS), true)
95
+ assert.ok(PROFILE_FORCE_EXIT_WAIT_MS > 0)
96
+ assert.equal(resolveProcessControl().forceWaitMs, PROFILE_FORCE_EXIT_WAIT_MS)
97
+ })
@@ -18,6 +18,18 @@ export const PROFILE_STOP_GRACE_MS = 6_000
18
18
  /** Windows 的优雅期(更短,原因见文件头)。 */
19
19
  export const PROFILE_STOP_GRACE_MS_WINDOWS = 1_500
20
20
 
21
+ /**
22
+ * 强杀后的**有限等待**(ms):`taskkill /F` 返回 ≠ 进程已消失。
23
+ *
24
+ * Windows 上 TerminateProcess 只是投递终止请求,进程表项与句柄回收是异步的(真机上有几十毫秒窗口);
25
+ * 强杀后立刻回查会把「刚被杀、正在消失」判成 failed,调用方(launcher / runtime)据此抛 stopFailed
26
+ * 并保留心跳 —— 而进程其实已经没了(审计 P11-3)。POSIX 的 SIGKILL 也有同样的(更短的)窗口。
27
+ * 所以强杀后与优雅阶段同构地轮询:窗口内消失即 killed,窗口耗尽才 failed。
28
+ */
29
+ export const PROFILE_FORCE_EXIT_WAIT_MS = 1_000
30
+ /** 强杀后的回查间隔(ms):强杀预期立即生效,轮询比优雅期(200ms)更密。 */
31
+ export const PROFILE_FORCE_EXIT_POLL_MS = 50
32
+
21
33
  /** 一次停止的终态;failed = 强杀后仍活着(调用方必须如实报错,不许说成已停止)。 */
22
34
  export type StopPidOutcome = 'graceful' | 'killed' | 'already-stopped' | 'failed'
23
35
 
@@ -31,6 +43,8 @@ export interface ProcessControlDeps {
31
43
  now?: () => number
32
44
  /** 优雅期(缺省按平台:POSIX 6s / Windows 1.5s) */
33
45
  graceMs?: number
46
+ /** 强杀后等待进程消失的上限(缺省 PROFILE_FORCE_EXIT_WAIT_MS) */
47
+ forceWaitMs?: number
34
48
  }
35
49
 
36
50
  /** 解析后的进程控制依赖(构造器注入一次,后续只读)。 */
@@ -40,6 +54,7 @@ export interface ResolvedProcessControl {
40
54
  sleep: (ms: number) => Promise<void>
41
55
  now: () => number
42
56
  graceMs: number
57
+ forceWaitMs: number
43
58
  }
44
59
 
45
60
  /** 缺省优雅期(按平台)。 */
@@ -54,12 +69,16 @@ export function resolveProcessControl(deps: ProcessControlDeps = {}): ResolvedPr
54
69
  sleep: deps.sleep ?? ((ms) => delay(ms)),
55
70
  now: deps.now ?? (() => Date.now()),
56
71
  graceMs: deps.graceMs ?? defaultStopGraceMs(),
72
+ forceWaitMs: deps.forceWaitMs ?? PROFILE_FORCE_EXIT_WAIT_MS,
57
73
  }
58
74
  }
59
75
 
60
76
  /**
61
- * 停一个 pid:先优雅 → 等 graceMs → 仍活着则强杀 → 仍活着返回 'failed'。
77
+ * 停一个 pid:先优雅 → 等 graceMs → 仍活着则强杀 → 强杀后再等 forceWaitMs → 仍活着才返回 'failed'。
62
78
  * 只做进程动作,不碰任何台账/心跳文件(那是调用方的事)。
79
+ *
80
+ * 强杀阶段为什么不能「杀完立刻查一次」:见 PROFILE_FORCE_EXIT_WAIT_MS(审计 P11-3:
81
+ * 立刻回查会把「正在消失」误判成 failed,调用方保留心跳、UI 报「停不掉」,而进程其实已死)。
63
82
  */
64
83
  export async function stopPid(pid: number, deps: ResolvedProcessControl): Promise<StopPidOutcome> {
65
84
  if (!deps.isAlivePid(pid)) return 'already-stopped'
@@ -71,6 +90,14 @@ export async function stopPid(pid: number, deps: ResolvedProcessControl): Promis
71
90
  }
72
91
  if (!deps.isAlivePid(pid)) return 'graceful'
73
92
  deps.killPid(pid, 'force')
93
+ // 与优雅阶段同构:强杀生效是异步的,窗口内消失即 killed,窗口耗尽才 failed。
94
+ // 缺省兜底:直接调 stopPid 且只给了部分 deps 的调用方(脚本/复现)也不该退化成「杀完立刻查一次」。
95
+ const forceWaitMs = deps.forceWaitMs ?? PROFILE_FORCE_EXIT_WAIT_MS
96
+ const forceDeadline = deps.now() + forceWaitMs
97
+ while (deps.now() < forceDeadline) {
98
+ if (!deps.isAlivePid(pid)) return 'killed'
99
+ await deps.sleep(PROFILE_FORCE_EXIT_POLL_MS)
100
+ }
74
101
  return deps.isAlivePid(pid) ? 'failed' : 'killed'
75
102
  }
76
103
 
@@ -212,7 +212,8 @@ export function profileRoutes(env: RoutesEnv): WebRoute[] {
212
212
  const allowCurrent = body['allowCurrent'] === true
213
213
  try {
214
214
  if (runningView().some((r) => r.name === name)) throw new DshProfileError('instanceRunning')
215
- profiles.remove(name, { allowCurrent })
215
+ // 档案目录是整仓最大的树:异步删除,别让事件循环停摆十几秒(审计 P11-9)。
216
+ await profiles.removeAsync(name, { allowCurrent })
216
217
  const historyError = await tryAppendHistory({
217
218
  kind: 'profile-delete',
218
219
  result: 'success',
@@ -282,6 +282,14 @@ export function syncRoutes(env: RoutesEnv): WebRoute[] {
282
282
  autosync: await buildAutosyncStatus(syncDir, transport),
283
283
  // 全部通道的自动同步状态(git/webdav 各自独立;UI 按当前 tab 取对应通道)
284
284
  autosyncByChannel: await buildAutosyncStatusByChannel(syncDir),
285
+ // 待裁决分区(P2,2026-11-14 真机事故):远端有比基线新的快照、且自动同步撞上冲突时,
286
+ // sync-state.pendingMerge 会记录「哪些分区等人裁决」与「从什么时候开始挂着的」。
287
+ // 首页「待裁决」徽章据此从纯颜色变成可点击入口(跳同步页打开裁决视图)。
288
+ // **绝不回 remoteSnapshotId**:与 hasBaseline 同一纪律 —— 界面只需要知不知道该裁决,
289
+ // 不需要(也不该拿到)远端快照坐标。
290
+ ...(state.pendingMerge === undefined
291
+ ? {}
292
+ : { pendingMerge: { sections: [...state.pendingMerge.sections], since: state.pendingMerge.since } }),
285
293
  // 工作副本事实(仅 git 且配置了地址时返回;'facts' = 自检成功,'error' = 自检读不出来)
286
294
  ...(workDir === null ? {} : { workDir }),
287
295
  }
@@ -475,6 +483,9 @@ export function syncRoutes(env: RoutesEnv): WebRoute[] {
475
483
  const sessionsOpt = sections !== undefined && sections.includes('sessions') && sessions !== undefined
476
484
  ? { sessions }
477
485
  : {}
486
+ // issue #140:强制推送(GUI 等价入口,agent 侧早已有 force)。缺省 false = 内容与远端
487
+ // 最新快照一致时不新建快照(引擎返回 skipped:'unchanged');true 时即使一致也新建。
488
+ const force = body['force'] === true
478
489
  // P0-②:push 前只读预览(body.preview === true → 不写远端,只返回「将推送什么」)
479
490
  const preview = body['preview'] === true
480
491
  // 分支调用以保证 withTimeout 的泛型结果类型正确(SyncPushReport | SyncPushPreview)
@@ -494,6 +505,7 @@ export function syncRoutes(env: RoutesEnv): WebRoute[] {
494
505
  ...(sections === undefined ? {} : { sections }),
495
506
  ...sessionsOpt,
496
507
  ...(encrypt || includeSecrets ? { encrypt: true, includeSecrets, password: effectiveEncryptPassword ?? '' } : {}),
508
+ ...(force ? { force: true } : {}),
497
509
  }),
498
510
  ROUTE_TIMEOUT_MS,
499
511
  msg('host.syncPushTimeout'),
@@ -1018,7 +1030,7 @@ export function syncRoutes(env: RoutesEnv): WebRoute[] {
1018
1030
  ...item,
1019
1031
  kind: 'Update' as const,
1020
1032
  severity: 'info' as const,
1021
- conflict: { itemId: c?.itemId ?? item.id, resolution: 'useImported' as const },
1033
+ conflict: { itemId: c?.itemId ?? item.id, resolution: 'useImported' as const, decidedBy: 'item' as const },
1022
1034
  } as PlanItem]
1023
1035
  }
1024
1036
  return [item]
@@ -176,7 +176,22 @@ export interface PluginEntry {
176
176
  fiberPhase?: string;
177
177
  }
178
178
 
179
- export interface PatchLine { file: string; lineId: string; raw: unknown; }
179
+ export interface PatchLine {
180
+ file: string;
181
+ lineId: string;
182
+ raw: unknown;
183
+ /**
184
+ * 本行 `raw` 里**被脱敏剥空**的叶子路径(行内相对,如 `raw.mcp.args.token`),issue #136 C。
185
+ *
186
+ * 语义:这些路径上的值**不是**用户在源机写空,而是导出侧 SecretScanner 剥掉的(占位符 = 空串),
187
+ * 目标机不得据此覆盖本机同叶子的非空值。写回侧据此精确保护(见
188
+ * `src/core/patch-secret-guard.ts`)。
189
+ *
190
+ * 兼容:字段只增不改。**缺省 = 未知**(老快照没有这个信息)→ 写回侧走保守分支
191
+ * (导入空 + 本机非空即保留本机),绝不假定「空值一定来自用户主动清空」。
192
+ */
193
+ redactedPaths?: string[];
194
+ }
180
195
 
181
196
  /**
182
197
  * 本地源插件的打包产物(T1)。
@@ -163,3 +163,34 @@ test('redact:scheme://user:password@host 的密码段被掩码(用户名保
163
163
  // createRedactor 与 redact 同源(同一份规则)
164
164
  assert.equal(createRedactor()('https://u:p4ssword@h/x').includes('p4ssword'), false);
165
165
  });
166
+
167
+ /* ---------------- 结构化 colon 形态的边界(审计 P12-2 / P12-3,2026-11-13 实测) ---------------- */
168
+
169
+ test('redact:colon 值不跨行 —— 后续行的敏感字段不会被前一行吞掉', () => {
170
+ // 原值域 [^,;}] 含 \n:'note: ok' 一路吞到行尾,整段只匹配一次、字段名是 note ⇒ password 明文透传。
171
+ const input = 'note: hello\npassword: s3cr3t-value';
172
+ const out = redact(input);
173
+ assert.equal(out.includes('s3cr3t-value'), false, '第二行的 password 值必须被掩');
174
+ assert.ok(out.startsWith('note: hello\n'), '不敏感行原样保留(含换行)');
175
+ assert.equal(redact(out), out, '幂等');
176
+ });
177
+
178
+ test('redact:JSON 里出现 colon 值形态时,收尾引号不得被吞(输出仍是合法 JSON)', () => {
179
+ // 原值域含 ":'{"error":"Authorization: Bearer abc"}' 把 JSON 收尾引号一起替换 ⇒ JSON.parse 抛错。
180
+ const input = JSON.stringify({ error: 'Authorization: Bearer abc-123', code: 1 });
181
+ const out = redact(input);
182
+ assert.doesNotThrow(() => JSON.parse(out), '脱敏后必须仍是合法 JSON');
183
+ assert.equal(out.includes('abc-123'), false, 'Bearer 载荷被掩');
184
+ assert.equal(redact(out), out, '幂等');
185
+ });
186
+
187
+ test('redact:同一行多个 field: value 逐个判定(不因首个字段不敏感而整行跳过)', () => {
188
+ const out = redact('note: hello password: hunter2');
189
+ assert.equal(out.includes('hunter2'), false, '同一行第二个敏感字段必须被掩');
190
+ assert.ok(out.startsWith('note: hello'), '前一个不敏感字段保留');
191
+ // 带引号值保留引号形态(只替换引号内的值,引号本身不动)
192
+ assert.equal(redact('password: "x9"'), 'password: "***REDACTED***"');
193
+ // URL 值的冒号不得导致误截断(值里带冒号仍整体保留)
194
+ assert.equal(redact('endpoint: https://h/a:b'), 'endpoint: https://h/a:b');
195
+ });
196
+
@@ -20,8 +20,22 @@ export const REDACTED = '***REDACTED***';
20
20
  const JSON_FIELD_RE = /"([A-Za-z0-9_.\-]+)"\s*:\s*"([^"]*)"/g;
21
21
  /** kv 值到空白/逗号/分号/&(& 截断:避免吞掉 URL query 的后续参数) */
22
22
  const KV_FIELD_RE = /([A-Za-z0-9_.\-]+)\s*=\s*([^\s,;&]+)/g;
23
- /** colon 值允许空格(如 `Authorization: Bearer xyz`),到逗号/分号/右大括号截断 */
24
- const COLON_FIELD_RE = /([A-Za-z0-9_.\-]+)\s*:\s*([^,;}]+)/g;
23
+ /**
24
+ * colon 形态的**带引号值**(`"field": "value"` 之外的非 JSON 文本,如 `password: "x"`)。
25
+ * 单独一条是为了「保留引号」:替换只动引号内的值,输出仍是合法 JSON / 合法带引号文本。
26
+ */
27
+ const QUOTED_COLON_FIELD_RE = /([A-Za-z0-9_.\-]+)\s*:\s*"([^"\n\r]*)"/g;
28
+ /**
29
+ * colon 值允许空格(如 `Authorization: Bearer xyz`),到逗号/分号/右大括号/**换行/引号**截断。
30
+ *
31
+ * 为什么值域必须排除 \n 与 "(审计 P12-2/P12-3,2026-11-13 实测):
32
+ * - 原值域 `[^,;}]` 含换行 ⇒ `note: ok\npassword: s3cr3t` 里首个不敏感字段一路吞到行尾,
33
+ * 整段只匹配一次、字段名是 note,于是 password 明文透传;
34
+ * - 还含 `"` ⇒ `{"error":"Authorization: Bearer abc"}` 会把 JSON 的收尾引号一起替换掉,
35
+ * 脱敏后 `JSON.parse` 直接抛 Unterminated string。
36
+ */
37
+ const COLON_FIELD_RE =
38
+ /([A-Za-z0-9_.\-]+)[ \t]*:[ \t]*([^\s,;}\n\r"][^,;}\n\r"]{0,199}?)(?=\s+[A-Za-z0-9_.\-]+[ \t]*:|[,;}\n\r"]|$)/g;
25
39
 
26
40
  /* ---------------- 值形状模式(包含式,字段名无关) ---------------- */
27
41
 
@@ -123,10 +137,12 @@ function replaceSensitiveField(text: string, re: RegExp, blacklist: readonly str
123
137
  * 现在只由调用方决定正则实例的生命周期(模块级常量 / 每次预编译),流水线本身不再有副本。
124
138
  */
125
139
  function redactPipeline(
126
- text: string, blacklist: readonly string[], json: RegExp, kv: RegExp, colon: RegExp,
140
+ text: string, blacklist: readonly string[], json: RegExp, quotedColon: RegExp, kv: RegExp, colon: RegExp,
127
141
  ): string {
128
- // 1. 结构化字段形态(顺序:JSON → kv → colon;JSON 键不会被后续形态二次命中)
142
+ // 1. 结构化字段形态(顺序:JSON → 带引号 colon → kv → 裸 colon);
143
+ // JSON 键不会被后续形态二次命中;带引号 colon 先跑,免得裸 colon 吃掉收尾引号。
129
144
  let out = replaceSensitiveField(text, json, blacklist);
145
+ out = replaceSensitiveField(out, quotedColon, blacklist);
130
146
  out = replaceSensitiveField(out, kv, blacklist);
131
147
  out = replaceSensitiveField(out, colon, blacklist);
132
148
  // 2. 值形状模式(字段名无关,包含式)
@@ -143,7 +159,7 @@ function redactPipeline(
143
159
  * 输出仍保留结构化形态(JSON 行合法)。
144
160
  */
145
161
  export function redact(text: string, blacklist: readonly string[] = []): string {
146
- return redactPipeline(text, blacklist, JSON_FIELD_RE, KV_FIELD_RE, COLON_FIELD_RE);
162
+ return redactPipeline(text, blacklist, JSON_FIELD_RE, QUOTED_COLON_FIELD_RE, KV_FIELD_RE, COLON_FIELD_RE);
147
163
  }
148
164
 
149
165
  /** 对象级掩码:字段名命中敏感名单 → 整值替换(返回新对象,不改原对象;结构与类型保留) */
@@ -165,9 +181,10 @@ export function redactValue(value: unknown, blacklist: readonly string[] = []):
165
181
  */
166
182
  export function createRedactor(blacklist: readonly string[] = []): (text: string) => string {
167
183
  const json = new RegExp(JSON_FIELD_RE.source, 'g');
184
+ const quotedColon = new RegExp(QUOTED_COLON_FIELD_RE.source, 'g');
168
185
  const kv = new RegExp(KV_FIELD_RE.source, 'g');
169
186
  const colon = new RegExp(COLON_FIELD_RE.source, 'g');
170
- return (text: string): string => redactPipeline(text, blacklist, json, kv, colon);
187
+ return (text: string): string => redactPipeline(text, blacklist, json, quotedColon, kv, colon);
171
188
  }
172
189
 
173
190
  /** 默认名单(文档用;redact 的默认附加名单为空,内置判定已含 DEFAULT_SECRET_FIELD_NAMES) */
@@ -0,0 +1,58 @@
1
+ /**
2
+ * autosync-repeat:自动同步历史的「同一结论」签名(2026-11-14 真机事故 P1)。
3
+ *
4
+ * 现场:远端工作副本被别的设备推进后,每 60s 的 remote-poll 轮都会走一遍「合并 → 推送被挡 →
5
+ * 记一条 partial|conflict」,sync-history 的 200 条上限里 196 条是同一句话、间隔精确 60s。
6
+ * 签名 = 「这一轮说的还是不是同一件事」;返回 null 表示「必须记」(真推进 / 成败都要留痕)。
7
+ */
8
+ import test from 'node:test';
9
+ import assert from 'node:assert/strict';
10
+
11
+ import { repeatHistorySignature } from './autosync-repeat.ts';
12
+ import type { RepeatSignatureEntry } from './autosync-repeat.ts';
13
+
14
+ function entry(patch: Partial<RepeatSignatureEntry> = {}): RepeatSignatureEntry {
15
+ return {
16
+ status: 'partial',
17
+ direction: 'push',
18
+ skipReason: 'conflict',
19
+ conflictedSections: ['plugins'],
20
+ appliedSections: [],
21
+ error: '远端最新快照 sync-remote 与本地基线 sync-base 不一致:本次推送未执行',
22
+ ...patch,
23
+ } as RepeatSignatureEntry;
24
+ }
25
+
26
+ test('同一结论(同一远端快照 / 同一冲突分区)→ 同一个签名', () => {
27
+ const a = repeatHistorySignature('git', entry());
28
+ const b = repeatHistorySignature('git', entry());
29
+ assert.notEqual(a, null);
30
+ assert.equal(a, b);
31
+ });
32
+
33
+ test('分区顺序与空值形态不影响签名(同一结论就是同一结论)', () => {
34
+ const a = repeatHistorySignature('git', entry({ conflictedSections: ['plugins', 'settings'] }));
35
+ const b = repeatHistorySignature('git', entry({ conflictedSections: ['settings', 'plugins'] }));
36
+ assert.equal(a, b);
37
+
38
+ const c = repeatHistorySignature('git', entry({ conflictedSections: undefined, skipReason: undefined }));
39
+ const d = repeatHistorySignature('git', entry({ conflictedSections: [] as never, skipReason: undefined }));
40
+ assert.equal(c, d, '缺席与空数组同义(都是「没有分区」)');
41
+ });
42
+
43
+ test('结论变化 → 签名必须变(新事实要留新记录)', () => {
44
+ const base = repeatHistorySignature('git', entry());
45
+ assert.notEqual(base, repeatHistorySignature('git', entry({ error: '远端最新快照 sync-newer 与本地基线 sync-base 不一致' })));
46
+ assert.notEqual(base, repeatHistorySignature('git', entry({ conflictedSections: ['settings'] })));
47
+ assert.notEqual(base, repeatHistorySignature('git', entry({ skipReason: 'conflict-kept-local' })));
48
+ assert.notEqual(base, repeatHistorySignature('git', entry({ appliedSections: ['settings'] })));
49
+ assert.notEqual(base, repeatHistorySignature('git', entry({ status: 'skipped' })));
50
+ assert.notEqual(base, repeatHistorySignature('webdav', entry()), '通道不同不是同一件事');
51
+ });
52
+
53
+ test('成功 / 失败 / 真推进的轮次恒返回 null(这些必须逐条可见)', () => {
54
+ assert.equal(repeatHistorySignature('git', entry({ status: 'success' })), null);
55
+ assert.equal(repeatHistorySignature('git', entry({ status: 'failed' })), null);
56
+ assert.equal(repeatHistorySignature('git', entry({ pushedSnapshotId: 'sync-pushed' })), null);
57
+ assert.equal(repeatHistorySignature('git', entry({ pulledSnapshotId: 'sync-pulled' })), null);
58
+ });
@@ -0,0 +1,55 @@
1
+ /**
2
+ * 自动同步「同一状态连续轮询」的历史去重签名(P1,2026-11-14 真机事故)。
3
+ *
4
+ * 事故现场:远端出现一份比本地基线新的快照后,60s 远端轮询每轮都得出同一结论
5
+ * (skipReason='conflict' + 同样的 conflictedSections + 同样的「远端已推进」原文),
6
+ * 于是 sync-history 被同一条结论刷满(200 条上限里 196 条完全一样,间隔精确 60s),
7
+ * 首页「最近活动」同步变成每 1 分钟一条的噪音,而真正的冲突分区没有任何新信息。
8
+ *
9
+ * 去重口径(只压「同一件事的重复播报」,不压状态变化):
10
+ * - status 'success' / 'failed' 一律**不去重**:upToDate 逐轮记一条是文档化的既有行为
11
+ * (见 core/migration-history.ts 的 isAutosyncIdleRound 注释),失败必须逐条可见;
12
+ * - 带 pushedSnapshotId / pulledSnapshotId 的轮次**不去重**:真的推进了远端或本地 = 进度,
13
+ * 每一轮都是不同的事件;
14
+ * - 其余(skipped / partial)按 channel + status + direction + skipReason + 排序后的
15
+ * conflictedSections/appliedSections + error 取签名:签名相同 = 同一状态的第 N 轮。
16
+ *
17
+ * 被压掉的轮次仍然会刷新 autosync-config 的 lastRunAt / lastRunStatus / 跳过计数
18
+ * (见 autosync-scheduler.writeFinalConfig),所以界面「上次运行」不会停在旧时间。
19
+ */
20
+ import type { AutosyncHistoryEntry } from './sync-history.ts';
21
+
22
+ /** 参与签名的字段子集(AutosyncHistoryEntry 的只读投影)。 */
23
+ export type RepeatSignatureEntry = Pick<
24
+ AutosyncHistoryEntry,
25
+ | 'status'
26
+ | 'direction'
27
+ | 'skipReason'
28
+ | 'conflictedSections'
29
+ | 'appliedSections'
30
+ | 'pushedSnapshotId'
31
+ | 'pulledSnapshotId'
32
+ | 'error'
33
+ >;
34
+
35
+ function sortedList(ids: readonly string[] | undefined): string {
36
+ return ids === undefined ? '' : [...ids].sort().join(',');
37
+ }
38
+
39
+ /**
40
+ * 返回本轮历史签名;null = 本轮必须照常记录(见文件头的不去重清单)。
41
+ * 签名只用于进程内比较,永不落盘,故用 \u0000 分隔以避免字段拼接歧义。
42
+ */
43
+ export function repeatHistorySignature(channel: string, entry: RepeatSignatureEntry): string | null {
44
+ if (entry.status === 'success' || entry.status === 'failed') return null;
45
+ if (entry.pushedSnapshotId !== undefined || entry.pulledSnapshotId !== undefined) return null;
46
+ return [
47
+ channel,
48
+ entry.status,
49
+ entry.direction,
50
+ entry.skipReason ?? '',
51
+ sortedList(entry.conflictedSections),
52
+ sortedList(entry.appliedSections),
53
+ entry.error ?? '',
54
+ ].join('\u0000');
55
+ }