dsh-config-sync-plugin 0.3.93 → 0.3.95

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.en.md CHANGED
@@ -257,6 +257,23 @@ Two display fixes on top of 0.3.77:
257
257
 
258
258
  The "Plugin update" card now shows an **npm download URL** line with the actual fallback addresses (domestic mirror `registry.npmmirror.com` + official `registry.npmjs.org`). When the GitHub release download is unreachable (common in mainland China), the update package automatically falls back to npm. It is still verified against the authoritative sha256 in `version.json` — switching source never relaxes verification. The two sources back each other up: the mirror can briefly 404 during its first-sync window, and the official source takes over. The npm tarball is byte-identical to the GitHub release tgz (same sha256).
259
259
 
260
+ ### Fix post-exit.ps1 missing UTF-8 BOM, which crashed every queued sync (0.3.95)
261
+
262
+ The final root cause behind "auto pull/push enabled, clicking × still triggers nothing" was neither the queue logic nor the close hook — it was the **encoding of the executor script**. When 0.3.94 edited `post-exit.ps1`, the write tool stripped the leading **UTF-8 BOM (`EF BB BF`)**. Windows PowerShell 5.1 decodes a BOM-less Chinese UTF-8 script as GBK, turns full-width Chinese parentheses into quote characters, and fails with 55 syntax errors the moment the script starts. Result: the watcher was queued (`auto-sync-launch.log` logged "started"), but `auto-sync-wait.log` never got a new "waiting started" line — every queued sync (startup check, 2-minute recheck, ×-close trigger) crashed on launch, appearing as "nothing ever triggers".
263
+
264
+ 1. **Restore the BOM on the executor**: `post-exit.ps1` (plugin runtime, config-repo mirror, deployed directory) was rewritten with `UTF8Encoding($true)` to restore the BOM; `ParseFile` syntax errors dropped from 55 to 0, and an isolated probe confirmed the wait phase starts, writes its log, and stays alive.
265
+ 2. **Regression assertion**: `selftest.mjs` now checks that every `runtime/*.ps1` carries a UTF-8 BOM (first 3 bytes `EF BB BF`), with an extra check on `post-exit.ps1`. Editing a script and losing its BOM now turns the self-test red.
266
+ 3. **Packaging preserves BOM**: `fs.copyFileSync` copies bytes verbatim and never strips a BOM; this incident was the source file being edited incorrectly. The packed tarball is verified to contain a `post-exit.ps1` with BOM.
267
+
268
+ ### Clean up cross-restart stale watchers — the lock can no longer be held by an old instance (0.3.94)
269
+
270
+ A deeper cause was measured behind "switches enabled, clicking × still triggers nothing": after a plugin update restarts DSH, the watcher queued before the update is still waiting on the **old, already-dead DSH process** (WatchPid is gone), but the post-exit "a process with this name still exists" guard keeps it from starting. It hangs in the wait phase and holds `~/.dsh/auto-sync.lock` forever. When × queues a new watcher, it sees a live pid with the same mode in the lock, assumes "the same sync is already waiting" and reuses it — reusing a watcher that will never run, so nothing appears to happen. Two fixes:
271
+
272
+ 1. **Lock records watchPid and checks for staleness before reuse**: `auto-sync.lock` now stores `{pid, mode, watchPid: current DSH pid, at}`. Before reusing a lock, spawnWatcher checks whether `watchPid` is still alive: a missing watchPid (old lock) or an exited DSH means stale — kill the old watcher, clear the lock, and re-queue. Only when both DSH and watcher are alive and the mode matches is the watcher reused.
273
+ 2. **post-exit wait phase self-clears stale watchers**: when WatchPid has exited but processes with the DSH name remain, compare their start times — if they are all newer than this waiter, the DSH we waited for already exited and restarted, so this watcher is a leftover from the previous generation: log it, remove its own lock, and `exit 3` to yield, instead of waiting 30 minutes and hogging the lock.
274
+
275
+ `post-exit.ps1` is also mirrored into the config repo's runtime copies so the repo fallback script carries the same fix.
276
+
260
277
  ### Clicking × to close DSH now checks once for auto pull/push immediately (0.3.93)
261
278
 
262
279
  With "Pull on startup" or "Push on exit" enabled, the only queues used to be the 8-second startup check and the 2-minute re-check. If you made changes between two re-checks and then clicked the top-right × to close DSH, no watcher was waiting for the exit at that moment, so nothing ran after closing. A quit hook now covers it: when the last visible window closes (the × click), the plugin checks once and queues the watcher **before** the client exits; once the DSH processes are gone the watcher runs the pull/push immediately.
@@ -712,6 +729,8 @@ The log shows which path ran: `文件比较(快筛):比对 N 个,深哈
712
729
 
713
730
  | Date | Version | Summary |
714
731
  |---|---|---|
732
+ | 2026-11-24 | 0.3.95 | Fix post-exit.ps1 losing UTF-8 BOM (PS5.1 decoded as GBK, 55 syntax errors, crashed on launch): restored BOM on all three executors, selftest adds full ps1 BOM assertion to prevent regression |
733
+ | 2026-11-24 | 0.3.94 | Fix cross-restart stale watchers holding the lock: lock records watchPid, reuse checks staleness, stale locks are killed and re-queued; post-exit wait self-clears stale watchers; fixes "switches on, clicking × still triggers nothing" |
715
734
  | 2026-11-24 | 0.3.93 | Clicking × to close DSH now checks once for auto pull/push and queues the watcher immediately (no more waiting for the 2-minute window); autoRestart semantics unified to !== false |
716
735
  | 2026-11-24 | 0.3.92 | Remote "uncommitted" now includes local pending data (pending); Refresh button really triggers git fetch; fixes "remote figures always 0" |
717
736
  | 2026-11-24 | 0.3.91 | Settings summary row fetches before refreshing remote info (no longer stale, auto-refresh every 30s); four generate-script buttons replaced with Pull/Push now (closes DSH) sharing the automation queue |
package/README.md CHANGED
@@ -277,6 +277,23 @@ pwsh -File runtime\sync.ps1 pull `
277
277
 
278
278
  「插件更新」卡新增一行 **npm 下载路径**,直接显示实际兜底地址(国内镜像 `registry.npmmirror.com` + 官方 `registry.npmjs.org` 两个源)。GitHub release 整段不通时(国内常见),更新包会自动兜底从 npm 下载;仍按 `version.json` 里的权威 sha256 校验,换源不放宽校验。两个源互为备份:镜像首发同步窗口期会短暂 404,官方源随即兜上。实测 npm tarball 与 GitHub release tgz 是同一份产物、sha256 完全一致。
279
279
 
280
+ ### 修复 post-exit.ps1 丢 UTF-8 BOM 致排队同步全崩(0.3.95)
281
+
282
+ 前面几版「勾了自动拉取/推送、点 × 还是不触发」的最终根因,既不在排队逻辑也不在关闭钩子,而在**执行体脚本的编码**:0.3.94 编辑 `post-exit.ps1` 时,写入工具把文件开头的 **UTF-8 BOM(`EF BB BF`)弄丢了**。Windows PowerShell 5.1 遇到无 BOM 的中文 UTF-8 脚本会按 GBK 解码,中文全角括号被解成引号,脚本一启动就报 55 处语法错直接退出。结果:watcher 明明排上了(`auto-sync-launch.log` 有「已启动」),但 `auto-sync-wait.log` 没有新的「等待开始」——所有排队的同步(启动首检、2 分钟复查、点 × 触发)全部启动即崩,表现成「完全没触发」。
283
+
284
+ 1. **执行体补回 BOM**:`post-exit.ps1`(插件 runtime、配置仓库镜像、实际部署目录三处)用 `UTF8Encoding($true)` 重写补回 BOM,`ParseFile` 语法错误从 55 降到 0,隔离探针实测 wait 段能正常启动、写日志、持续存活。
285
+ 2. **防回归断言**:`selftest.mjs` 新增检查——所有 `runtime/*.ps1` 必须带 UTF-8 BOM(前 3 字节 `EF BB BF`),`post-exit.ps1` 执行体单独再验一次。以后再有人编辑脚本丢了 BOM,自测直接红。
286
+ 3. **打包保持 BOM**:`fs.copyFileSync` 原样复制不会丢 BOM;本次是源文件被编辑工具写坏的,打包产物核验包内 `post-exit.ps1` 带 BOM。
287
+
288
+ ### 清理跨重启僵死 watcher,锁不再被旧实例占死(0.3.94)
289
+
290
+ 实测「勾了开关、点 × 关闭还是不触发」还有一个更深的原因:插件更新流程重启 DSH 后,更新前排的那个 watcher 还在等**已经退出的旧 DSH 进程**(WatchPid 已死),却被 post-exit 的「进程名还有新实例」判断挡着不敢开工,于是僵死在 wait 段、一直占着 `~/.dsh/auto-sync.lock`。此时点 × 新排的 watcher 看到锁里 pid 存活且同为 push,就误判「同一个同步正在等」直接复用——复用的是一个永远不会执行的僵死 watcher,所以看着像彻底没触发。这次两处修复:
291
+
292
+ 1. **锁记录 watchPid、复用前查僵死**:`auto-sync.lock` 现在写入 `{pid, mode, watchPid: 当前 DSH pid, at}`。spawnWatcher 复用锁前先看 `watchPid` 是否还活着:watchPid 缺失(旧版锁)或对应 DSH 已退出 → 判定僵死,杀掉旧 watcher、清锁、重新排;只有 DSH 与 watcher 都还活着,且 mode 相同才复用。
293
+ 2. **post-exit wait 段自清过期 watcher**:当 WatchPid 已退出、而进程名还有实例时,比较这批 DSH 进程的启动时间——若它们全部晚于本等待进程,说明我们等的那次 DSH 早已退出重启,本 watcher 属于上一代残留:写日志、清掉自己那把锁、`exit 3` 让位,不再死等 30 分钟霸占锁。
294
+
295
+ 配套把 `post-exit.ps1` 同步进配置仓库的执行体镜像,保证走仓库兜底脚本时也是同一份修复。
296
+
280
297
  ### 点 × 关闭 DSH 时立即判一次自动拉取/推送(0.3.93)
281
298
 
282
299
  勾了「启动时自动拉取」或「退出时自动推送」后,原来只有启动后的 8 秒首检 + 每 2 分钟复查会排队。如果你在两次复查之间做了改动就点右上角 × 关闭,那一刻没有 watcher 在等退出,关闭后什么都不会执行。现在补上退出钩子:点 ×(最后一个可见窗口关闭)时插件先判一次并排好 watcher,再退出客户端;DSH 进程归零后 watcher 立刻执行拉取/推送。
@@ -694,6 +711,8 @@ pwsh -File sync.ps1 push -ScanHistory
694
711
 
695
712
  | 日期 | 版本 | 要点 |
696
713
  |---|---|---|
714
+ | 2026-11-24 | 0.3.95 | 修复 post-exit.ps1 丢 UTF-8 BOM 致 PS5.1 按 GBK 解码、55 处语法错启动即崩:三处执行体补回 BOM,selftest 加全量 ps1 BOM 断言防回归 |
715
+ | 2026-11-24 | 0.3.94 | 修复跨重启僵死 watcher 占锁:锁记 watchPid、复用前查僵死、旧锁杀掉重排;post-exit wait 段过期 watcher 自清让位;解决「勾了开关点 × 仍不触发」 |
697
716
  | 2026-11-24 | 0.3.93 | 点 × 关闭 DSH 时立即判一次自动拉取/推送并排队 watcher,不再等 2 分钟复查窗口;autoRestart 语义统一为 !== false |
698
717
  | 2026-11-24 | 0.3.92 | 远端「未提交」纳入本机待同步数据(pending),刷新按钮真正触发 git fetch;解决「远端数字一直是 0」 |
699
718
  | 2026-11-24 | 0.3.91 | 设置-通用-配置同步摘要行展开时先 fetch 再刷新远端信息(不再陈旧、每 30 秒自动刷新);四个生成脚本按钮换成「立即拉取/推送(会关闭 DSH)」两个,走自动同步同一队列 |
package/index.js CHANGED
@@ -1630,17 +1630,27 @@ async function spawnWatcher({ mode, repo, appRoot, dshHome, restart, extraArgs =
1630
1630
  if (isFile(lockFile)) {
1631
1631
  const lockText = readFileSync(lockFile, 'utf8')
1632
1632
  const lockPid = Number((lockText.match(/"pid"\s*:\s*(\d+)/) || [])[1])
1633
+ const lockWatchPid = Number((lockText.match(/"watchPid"\s*:\s*(\d+)/) || [])[1])
1633
1634
  if (lockPid > 0) {
1634
1635
  let alive = false
1635
1636
  try { process.kill(lockPid, 0); alive = true } catch { alive = false }
1636
- if (alive) {
1637
- const lockMode = (lockText.match(/"mode"\s*:\s*"([a-z]+)"/) || [])[1]
1638
- // 同一种同步已经在等,直接复用,别再加一个并行的
1637
+ const lockMode = (lockText.match(/"mode"\s*:\s*"([a-z]+)"/) || [])[1]
1638
+ // 僵死判定:锁文件没记 watchPid(旧版本锁),或记的 DSH pid 已经不在了。
1639
+ // 场景:插件更新重启后,旧 watcher 还在等已经死掉的旧 DSH 进程,却被「进程名还有新实例」
1640
+ // 挡着不敢开工,一直占着锁。此时它永远不会执行,必须杀掉重排,绝不能复用。
1641
+ let watcherOwnerAlive = false
1642
+ if (lockWatchPid > 0) { try { process.kill(lockWatchPid, 0); watcherOwnerAlive = true } catch { watcherOwnerAlive = false } }
1643
+ const stale = !lockWatchPid || !watcherOwnerAlive
1644
+ if (alive && !stale) {
1645
+ // 锁里的 DSH 还活着、watcher 也在 → 才是真正「同一个同步正在等」,可以直接复用
1639
1646
  if (lockMode === mode) return { ok: true, pid: lockPid, reused: true }
1640
1647
  // 不同的同步正在等(例如首检排了推送、用户在确认框点了拉取):用户的手动动作优先,
1641
1648
  // 干掉那个还没开工的等待进程,改排这一次。它此时只可能在等 DSH 退出,没有副作用。
1642
1649
  try { process.kill(lockPid) } catch { }
1643
1650
  logLaunch(dshHome, `取消上一个等待中的 ${lockMode || '同步'}(pid ${lockPid}),改排 ${mode}`)
1651
+ } else if (alive && stale) {
1652
+ try { process.kill(lockPid) } catch { }
1653
+ logLaunch(dshHome, `清理僵死 watcher(pid ${lockPid},其 DSH pid ${lockWatchPid || '未记录'} 已退出),改排 ${mode}`)
1644
1654
  }
1645
1655
  }
1646
1656
  try { unlinkSync(lockFile) } catch { }
@@ -1689,7 +1699,7 @@ async function spawnWatcher({ mode, repo, appRoot, dshHome, restart, extraArgs =
1689
1699
  const pid = Number(String(stdout).trim().split(/\s+/).pop()) || 0
1690
1700
  autoState.watcherStartedAt = Date.now()
1691
1701
  try {
1692
- writeFileSync(lockFile, JSON.stringify({ pid, mode, at: Date.now() }), 'utf8')
1702
+ writeFileSync(lockFile, JSON.stringify({ pid, mode, watchPid: process.pid, at: Date.now() }), 'utf8')
1693
1703
  } catch { }
1694
1704
  logLaunch(dshHome, `已启动 ${mode} pid=${pid} exe=${exe} script=${script}`)
1695
1705
  return { ok: true, pid }
@@ -3542,7 +3552,9 @@ async function triggerAutoSyncOnClose(config) {
3542
3552
  try {
3543
3553
  const settings = readSettings(dshHome)
3544
3554
  if (!settings.autoPull && !settings.autoPush) return { queued: false }
3545
- // 已经排过队(2 分钟复查 / 手动按钮)就别重复拉起 watcher:spawnWatcher 的锁也会挡,这里先省一次开销
3555
+ // 已经排过队(2 分钟复查 / 手动按钮)就别重复拉起 watcher:spawnWatcher 的锁也会挡,这里先省一次开销。
3556
+ // 注意:这里只信 autoState.pending(本进程内、一定带当前 DSH pid 的新 watcher)。跨重启的旧锁
3557
+ // 不带 autoState.pending,仍会往下走;由 spawnWatcher 里的僵死判定负责清理,绝不在这一步复用。
3546
3558
  if (autoState.pending && isProcessAlive(autoState.pending.watcherPid)) {
3547
3559
  return { queued: false, reused: true, mode: autoState.pending.mode }
3548
3560
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-config-sync-plugin",
3
- "version": "0.3.93",
3
+ "version": "0.3.95",
4
4
  "type": "module",
5
5
  "description": "在 DSH 设置里查看 dsh-config 仓库与本机的同步状态,并生成退出后执行的 push/pull 脚本。同步动作本身不在应用内执行。面板与文案跟随客户端语言(中文/English)。",
6
6
  "main": "./index.js",
@@ -65,6 +65,29 @@ if ($Phase -eq 'wait') {
65
65
  $byPid = if ($WatchPid -gt 0) { Get-Process -Id $WatchPid -ErrorAction SilentlyContinue } else { $null }
66
66
  if ($byName.Count -eq 0 -and -not $byPid) { WaitLog 'DSH 已完全退出'; break }
67
67
 
68
+ # 僵死 watcher 自清:WatchPid(排队那次的 DSH)已经退出,但进程名还有实例 ——
69
+ # 若这些实例全部是本 watcher 启动之后才起来的,说明我们等的那次 DSH 早就退出并重启过了,
70
+ # 本 watcher 属于上一代残留。继续等只会被「进程名还有新实例」挡住、死等 30 分钟还霸占锁,
71
+ # 让新排的 watcher(带当前 DSH pid)无法复用/接管。这里主动退出并清锁,把位置让出来。
72
+ # 正常场景不会误伤:排队时 DSH 已在运行,其进程启动时间一定早于本 watcher。
73
+ if ($WatchPid -gt 0 -and -not $byPid -and $byName.Count -gt 0) {
74
+ $myStart = (Get-Process -Id $PID -ErrorAction SilentlyContinue).StartTime
75
+ if ($myStart) {
76
+ $newerAll = @($byName | Where-Object { $_.StartTime -and $_.StartTime -ge $myStart })
77
+ if ($newerAll.Count -eq $byName.Count) {
78
+ WaitLog ('WatchPid {0} 已退出,现存 DSH 均为本等待启动后的新实例,判定为过期 watcher,退出让位' -f $WatchPid)
79
+ try {
80
+ $lockFile = Join-Path $DshHome 'auto-sync.lock'
81
+ if (Test-Path -LiteralPath $lockFile) {
82
+ $lock = Get-Content -LiteralPath $lockFile -Raw -Encoding utf8 | ConvertFrom-Json
83
+ if ($lock -and $lock.pid -eq $PID) { Remove-Item -LiteralPath $lockFile -Force -ErrorAction SilentlyContinue }
84
+ }
85
+ } catch { }
86
+ exit 3
87
+ }
88
+ }
89
+ }
90
+
68
91
  # 用户点了窗口右上角的关闭、前台窗口全没了,但进程赖着不走(实测 7 个进程全部 hwnd=0 还活着)。
69
92
  # 不能只看「曾经出现过窗口」——等待进程可能是在窗口关掉之后才起来的(实测就这么漏判了),
70
93
  # 改用批次年龄:这批客户端进程已经活了 60 秒以上、却一个可见窗口都没有 → 认定用户已关闭。
@@ -1806,5 +1806,53 @@ console.log('== 0.3.93:点 × 关闭时判一次并排队自动拉取/推送 =
1806
1806
  check('排队成功写日志:退出后立即执行',
1807
1807
  /退出前已排队 \$\{r\.mode\}(自动同步)/.test(idxSrc), 'index.js 排队日志')
1808
1808
  }
1809
+ console.log('')
1810
+ console.log('== 0.3.94:清理跨重启僵死 watcher,锁不再被旧实例占死 ==')
1811
+ {
1812
+ const idxSrc = existsSync(join(pluginRootSelf, 'index.js')) ? readFileSync(join(pluginRootSelf, 'index.js'), 'utf8') : ''
1813
+ const postSrc = existsSync(join(pluginRuntime, 'post-exit.ps1')) ? readFileSync(join(pluginRuntime, 'post-exit.ps1'), 'utf8') : ''
1814
+ check('锁文件写入 watchPid=当前 DSH pid',
1815
+ /JSON\.stringify\(\{ pid, mode, watchPid: process\.pid, at: Date\.now\(\) \}\)/.test(idxSrc), 'index.js 写锁')
1816
+ check('复用前先判定锁是否僵死:读取 lockWatchPid',
1817
+ /"watchPid"\\s\*:\\s\*\(\\d\+\)/.test(idxSrc)
1818
+ || idxSrc.includes('const lockWatchPid = Number('), 'index.js 读 watchPid')
1819
+ check('僵死判定:watchPid 缺失或其进程已退出',
1820
+ idxSrc.includes('const stale = !lockWatchPid || !watcherOwnerAlive')
1821
+ && /process\.kill\(lockWatchPid, 0\)/.test(idxSrc), 'index.js stale 判定')
1822
+ check('只在锁未僵死时才复用同 mode 的 watcher',
1823
+ /if \(alive && !stale\)[\s\S]{0,300}?if \(lockMode === mode\) return/.test(idxSrc), 'index.js 复用守卫')
1824
+ check('僵死锁会被杀掉并记日志,不再复用',
1825
+ /else if \(alive && stale\)/.test(idxSrc)
1826
+ && idxSrc.includes('清理僵死 watcher'), 'index.js 清僵死')
1827
+ check('post-exit wait:WatchPid 已死但进程名有新实例时判定过期 watcher',
1828
+ postSrc.includes('-not $byPid -and $byName.Count -gt 0')
1829
+ && postSrc.includes('判定为过期 watcher'), 'post-exit.ps1 过期判定')
1830
+ check('post-exit wait:过期 watcher 清自己的锁并 exit 3 让位',
1831
+ /\$lock\.pid -eq \$PID/.test(postSrc) && /\bexit 3\b/.test(postSrc), 'post-exit.ps1 让位')
1832
+ }
1833
+ console.log('')
1834
+ console.log('== 0.3.95:所有 runtime/*.ps1 必须带 UTF-8 BOM(PS5.1 中文脚本防乱码崩溃)==')
1835
+ {
1836
+ // 决定性教训:0.3.94 编辑 post-exit.ps1 时写入工具丢了 BOM,PS5.1 按 GBK 解码,
1837
+ // 中文全角括号被解成引号 → 55 处语法错 → 脚本启动即崩 → 所有排队同步(含点 ×)全部失效。
1838
+ // 所有含中文的 .ps1 必须带 UTF-8 BOM,否则在 Windows PowerShell 5.1 下必崩。
1839
+ const ps1Names = ['apply.ps1', 'capture.ps1', 'dsh-sync-common.ps1', 'install-plugin.ps1',
1840
+ 'post-exit.ps1', 'restore.ps1', 'sync.ps1']
1841
+ for (const name of ps1Names) {
1842
+ const p = join(pluginRuntime, name)
1843
+ if (!existsSync(p)) continue
1844
+ const bytes = readFileSync(p)
1845
+ const hasBom = bytes.length >= 3 && bytes[0] === 0xEF && bytes[1] === 0xBB && bytes[2] === 0xBF
1846
+ const hex = [...bytes.slice(0, 3)].map((b) => b.toString(16).padStart(2, '0')).join(' ')
1847
+ check(`${name} 带 UTF-8 BOM(PS5.1 不会按 GBK 解成乱码)`, hasBom, `前3字节 ${hex}`)
1848
+ }
1849
+ // post-exit.ps1 是排队同步(含点 × 触发)的唯一执行体,崩了就全盘不动,单独强调
1850
+ const postPath = join(pluginRuntime, 'post-exit.ps1')
1851
+ if (existsSync(postPath)) {
1852
+ const b = readFileSync(postPath)
1853
+ check('post-exit.ps1 执行体非零字节且带 BOM',
1854
+ b.length > 1000 && b[0] === 0xEF && b[1] === 0xBB && b[2] === 0xBF, `bytes=${b.length}`)
1855
+ }
1856
+ }
1809
1857
  process.exit(failures === 0 ? 0 : 1)
1810
1858