dsh-config-sync-plugin 0.3.98 → 0.3.99

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,16 @@ 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
+ ### Toggle auto-pull/push now triggers immediate sync check (0.3.99)
261
+
262
+ Previously toggling "Pull on startup" or "Push on exit" meant waiting for the next 2-minute cycle (or DSH exit) before any sync decision. The core logic is now factored into `checkAndQueueAutoSync`, reused in three places:
263
+
264
+ 1. **scheduleAutoStart** (8s after startup + every 2 min)
265
+ 2. **triggerAutoSyncOnClose** (× close)
266
+ 3. **handleSettings** (toggle save)
267
+
268
+ When a switch is saved and already on with a ready repo, it instantly checks drift and queues the watcher + closes DSH if criteria are met; manual-trigger mode (toggle change) skips the confirm dialog. No more "toggled switch, wait 2 min for anything to happen". 5 selftest assertions added. Keeps all 0.3.93–0.3.98 fixes.
269
+
260
270
  ### Default workspace conversations now sync; its files stay local (0.3.98)
261
271
 
262
272
  Previously the whole default workspace was skipped. Reason: its session directory name is encoded from the **local WorkspaceHome absolute path** (`--C-Users-...-default-workspace--`); the two machines have different paths, so copying A's directory onto B only leaves a dead directory DSH cannot recognize (shows up as an empty default-workspace history). This version splits "sync conversations, not files":
@@ -753,6 +763,7 @@ The log shows which path ran: `文件比较(快筛):比对 N 个,深哈
753
763
 
754
764
  | Date | Version | Summary |
755
765
  |---|---|---|
766
+ | 2026-11-25 | 0.3.99 | Toggle auto-pull/push now triggers immediate sync check (core logic checkAndQueueAutoSync reused by scheduleAutoStart/triggerAutoSyncOnClose/handleSettings; toggle change queues immediately, manual-trigger mode skips confirm) |
756
767
  | 2026-11-25 | 0.3.98 | Default workspace conversations now sync (stable name --default-workspace--; apply re-encodes directory/cwd/projcache from local defaultWorkspaceId and merges sessionIds), files stay local |
757
768
  | 2026-11-25 | 0.3.97 | Clicking × with pending pull/push actively force-kills the DSH process family from a detached process (detached taskkill /F); the watcher runs immediately instead of waiting for orphaned processes |
758
769
  | 2026-11-24 | 0.3.96 | Page-unload beacon (pagehide→sendBeacon /auto-exit) replaces the dead Electron close hook; fast check (local tracking ref, no fetch) queues the watcher in milliseconds, finally fixing "× does not trigger" |
package/README.md CHANGED
@@ -277,6 +277,16 @@ 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
+ ### 勾选自动同步开关即时触发判断(0.3.99)
281
+
282
+ 以前勾选「启动时自动拉取」或「退出时自动推送」后,必须等到下一个 2 分钟周期(或退出 DSH)才会判断是否需要同步。这次把核心判断逻辑抽离为 `checkAndQueueAutoSync`,在三处复用:
283
+
284
+ 1. **scheduleAutoStart**(启动 8 秒后 + 每 2 分钟复查)
285
+ 2. **triggerAutoSyncOnClose**(点 × 关闭时)
286
+ 3. **handleSettings**(开关保存后)
287
+
288
+ 开关保存后,若已开启且仓库就位,立即判 drift→满足条件即排队 watcher 并关闭 DSH;手动触发模式(开关变更)跳过确认框,直接排队。不再有「勾了开关、等 2 分钟才动」的延迟。selftest 加 5 条断言防回归。保留 0.3.93-0.3.98 全部修复。
289
+
280
290
  ### 默认工作区会话纳入同步、资料仍各机独立(0.3.98)
281
291
 
282
292
  以前默认工作区整个跳过同步。原因:它的会话目录名由**本机 WorkspaceHome 绝对路径**编码(`--C-Users-...-default-workspace--`),两台机器路径不同,直接把 A 机的目录铺到 B 机只会留下 DSH 认不出的死目录(表现为默认工作区历史为空)。这次按「只同步会话、不同步资料」拆开处理:
@@ -735,6 +745,7 @@ pwsh -File sync.ps1 push -ScanHistory
735
745
 
736
746
  | 日期 | 版本 | 要点 |
737
747
  |---|---|---|
748
+ | 2026-11-25 | 0.3.99 | 勾选自动同步开关即时触发判断(核心逻辑抽离为 checkAndQueueAutoSync,scheduleAutoStart/triggerAutoSyncOnClose/handleSettings 三路复用,开关变更即时排队,手动模式跳过确认框) |
738
749
  | 2026-11-25 | 0.3.98 | 默认工作区会话纳入同步(稳定名 --default-workspace--,apply 按本机 defaultWorkspaceId 重编码目录/cwd/projcache 并合并 sessionIds),资料仍各机独立 |
739
750
  | 2026-11-25 | 0.3.97 | 点 × 检测到待拉取/待推送后,从独立进程主动强制退出 DSH 进程族(detached taskkill /F),watcher 立刻执行,不再干等残留进程 |
740
751
  | 2026-11-24 | 0.3.96 | 页面卸载信标(pagehide→sendBeacon /auto-exit)取代失效的 Electron 关窗钩子,fast 判据(本地 tracking ref、不 fetch)毫秒级排队 watcher,真正修好「点 × 不触发」 |
package/index.js CHANGED
@@ -2163,7 +2163,7 @@ async function handleSettings(req, res, config) {
2163
2163
  }
2164
2164
 
2165
2165
  const sshKey2 = sshKey || defaultSshKey()
2166
- sendJson(res, 200, {
2166
+ const response = {
2167
2167
  ok: true,
2168
2168
  changed,
2169
2169
  gitRemoteSet: Boolean(gitSet),
@@ -2187,7 +2187,17 @@ async function handleSettings(req, res, config) {
2187
2187
  defaultWorkspaceAuto: !defaultWorkspace,
2188
2188
  settingsPath: file,
2189
2189
  },
2190
- })
2190
+ }
2191
+ // autoPull/autoPush 开关变更时,若已开启则立即判断并排队(不等 2 分钟周期)
2192
+ if (changed.autoPull || changed.autoPush) {
2193
+ const repoPathResolved = repo || resolveRepo(config?.repoPath, dshHome).repo || ''
2194
+ const appRoot = resolveAppRoot(config?.appRoot) || ''
2195
+ const newSettings = readSettings(dshHome)
2196
+ if (repoPathResolved && isDir(join(repoPathResolved, '.git'))) {
2197
+ await checkAndQueueAutoSync(dshHome, config, newSettings, repoPathResolved, appRoot, { isManualTrigger: true })
2198
+ }
2199
+ }
2200
+ sendJson(res, 200, response)
2191
2201
  } catch (error) {
2192
2202
  sendJson(res, 400, { ok: false, error: String((error && error.message) || error) })
2193
2203
  }
@@ -3488,6 +3498,75 @@ async function handleAuto(req, res, config) {
3488
3498
  }
3489
3499
  }
3490
3500
 
3501
+ /** 核心判断逻辑:给定配置与仓库,判断是否需要自动拉取/推送,需要则排队 watcher 并关闭 DSH。
3502
+ * 供 scheduleAutoStart(定时)、triggerAutoSyncOnClose(关闭钩子)、handleSettings(开关即时触发)复用。 */
3503
+ async function checkAndQueueAutoSync(dshHome, config, settings, repo, appRoot, options = {}) {
3504
+ const { skipConfirm = false, isManualTrigger = false } = options
3505
+ // 已经排队等退出了就别重复拉起 watcher
3506
+ if (autoState.pending) return { queued: false, reason: 'already_pending' }
3507
+ if (!repo || !isDir(join(repo, '.git'))) {
3508
+ return { queued: false, reason: 'no_repo' }
3509
+ }
3510
+ const drift = await checkDrift(repo)
3511
+ if (!drift) {
3512
+ return { queued: false, reason: 'drift_failed' }
3513
+ }
3514
+ autoState.drift = drift
3515
+
3516
+ // autoPull:云端领先,且开启了自动拉取
3517
+ if (settings.autoPull && drift.behind > 0) {
3518
+ if (!skipConfirm && !isManualTrigger) {
3519
+ const yes = await askConfirm(
3520
+ t('云端有更新,要同步到本机吗?', 'The cloud has new commits — sync them to this machine?'),
3521
+ t(
3522
+ `云端比本机多 ${drift.behind} 个提交。\n`
3523
+ + '同步需要先完全退出 DSH:确认后客户端会自动关闭,拉取完成后按设置自动重新打开。\n'
3524
+ + `(本机领先 ${drift.ahead} 个提交,拉取不会动它们)`,
3525
+ `The cloud is ${drift.behind} commit(s) ahead of this machine.\n`
3526
+ + 'Syncing requires DSH to fully exit first: after you confirm, the client closes itself and reopens according to your settings once the pull finishes.\n'
3527
+ + `(This machine is ${drift.ahead} commit(s) ahead — pulling leaves those alone.)`,
3528
+ ),
3529
+ )
3530
+ if (!yes) return { queued: false, reason: 'user_cancelled_pull' }
3531
+ }
3532
+ const started = await spawnWatcher({ mode: 'pull', repo, appRoot, dshHome, restart: settings.autoRestart })
3533
+ if (started.ok) {
3534
+ autoState.pending = { mode: 'pull', at: Date.now(), restart: settings.autoRestart, watcherPid: started.pid, closeRequested: true }
3535
+ closeDsh()
3536
+ return { queued: true, mode: 'pull', reason: `behind ${drift.behind}` }
3537
+ } else {
3538
+ return { queued: false, reason: 'spawn_failed', error: started.error }
3539
+ }
3540
+ }
3541
+
3542
+ // autoPush:三个来源判据(领先提交 / 未提交改动 / 本机资料比上次同步新)
3543
+ if (settings.autoPush) {
3544
+ let dirty = 0
3545
+ const raw = await git(repo, ['status', '--porcelain'])
3546
+ if (raw) dirty = raw.split('\n').filter((l) => l.trim()).length
3547
+ const syncState = readSyncState(dshHome)
3548
+ const lp = localPending(dshHome, syncState.lastSyncAt, config?.profileName, { repo, appRoot })
3549
+ const reasonZh = drift.ahead > 0 ? `领先 ${drift.ahead} 个提交`
3550
+ : dirty > 0 ? `未提交 ${dirty} 项`
3551
+ : (lp.known && lp.pending > 0) ? `本机 ${lp.pending} 个文件比上次同步新` : ''
3552
+ const reasonEn = drift.ahead > 0 ? `${drift.ahead} commit(s) ahead`
3553
+ : dirty > 0 ? `${dirty} uncommitted change(s)`
3554
+ : (lp.known && lp.pending > 0) ? `${lp.pending} file(s) newer than the last sync` : ''
3555
+ if (reasonZh) {
3556
+ const restart = settings.autoRestart !== false
3557
+ const started = await spawnWatcher({ mode: 'push', repo, appRoot, dshHome, restart })
3558
+ if (started.ok) {
3559
+ autoState.pending = { mode: 'push', at: Date.now(), reason: reasonZh, reasonZh, reasonEn, restart, watcherPid: started.pid, closeRequested: false }
3560
+ closeDsh()
3561
+ return { queued: true, mode: 'push', reason: reasonZh }
3562
+ } else {
3563
+ return { queued: false, reason: 'spawn_failed', error: started.error }
3564
+ }
3565
+ }
3566
+ }
3567
+ return { queued: false, reason: 'no_action_needed' }
3568
+ }
3569
+
3491
3570
  /** 启动后的自动检查:延迟跑,避免和客户端自身的启动流程抢 IO;失败只记进状态,不打扰用户。 */
3492
3571
  function scheduleAutoStart(config) {
3493
3572
  const dshHome = config?.dshHome || process.env.DSH_HOME || join(homedir(), '.dsh')
@@ -3496,80 +3575,10 @@ function scheduleAutoStart(config) {
3496
3575
  if (autoState.timer) return
3497
3576
  const run = async () => {
3498
3577
  try {
3499
- // 已经排队等退出了就别重复拉起 watcher:第二遍 fetch/stat 纯属浪费
3500
3578
  if (autoState.pending) return
3501
3579
  const repo = resolveRepo(config?.repoPath, dshHome).repo || ''
3502
- if (!repo || !isDir(join(repo, '.git'))) {
3503
- // 空目录是「等首次拉取」的正常状态:不记错误,免得面板的云端徽标把它当成 fetch 失败
3504
- autoState.lastCheckError = (repo && repoPhase(repo) === 'empty')
3505
- ? ''
3506
- : t('仓库还没就位,跳过本次自动检查', 'The repo is not in place yet — skipping this auto check')
3507
- autoState.lastCheckAt = Date.now()
3508
- return
3509
- }
3510
- autoState.checking = true
3511
- const drift = await checkDrift(repo)
3512
- autoState.checking = false
3513
- autoState.lastCheckAt = Date.now()
3514
- if (!drift) {
3515
- autoState.lastCheckError = t('读不到远端(fetch 失败或没有 origin)', 'Cannot read the remote (fetch failed or there is no origin)')
3516
- return
3517
- }
3518
- autoState.drift = drift
3519
3580
  const appRoot = resolveAppRoot(config?.appRoot) || ''
3520
-
3521
- if (settings.autoPull && drift.behind > 0) {
3522
- const yes = await askConfirm(
3523
- t('云端有更新,要同步到本机吗?', 'The cloud has new commits — sync them to this machine?'),
3524
- t(
3525
- `云端比本机多 ${drift.behind} 个提交。\n`
3526
- + '同步需要先完全退出 DSH:确认后客户端会自动关闭,拉取完成后按设置自动重新打开。\n'
3527
- + `(本机领先 ${drift.ahead} 个提交,拉取不会动它们)`,
3528
- `The cloud is ${drift.behind} commit(s) ahead of this machine.\n`
3529
- + 'Syncing requires DSH to fully exit first: after you confirm, the client closes itself and reopens according to your settings once the pull finishes.\n'
3530
- + `(This machine is ${drift.ahead} commit(s) ahead — pulling leaves those alone.)`,
3531
- ),
3532
- )
3533
- if (yes) {
3534
- const started = await spawnWatcher({ mode: 'pull', repo, appRoot, dshHome, restart: settings.autoRestart })
3535
- if (started.ok) {
3536
- autoState.pending = { mode: 'pull', at: Date.now(), restart: settings.autoRestart, watcherPid: started.pid, closeRequested: true }
3537
- closeDsh()
3538
- } else {
3539
- autoState.lastCheckError = started.error
3540
- }
3541
- } else {
3542
- autoState.pending = null
3543
- }
3544
- return
3545
- }
3546
-
3547
- if (settings.autoPush) {
3548
- // 该不该推,三个来源都算:本地领先的提交、未提交的改动、以及"本机资料比上次同步新"
3549
- // (新会话还没 capture 时仓库是干净的,只看 ahead 会把最该推的那种改动漏掉 —— 真实踩过)
3550
- let dirty = 0
3551
- const raw = await git(repo, ['status', '--porcelain'])
3552
- if (raw) dirty = raw.split('\n').filter((l) => l.trim()).length
3553
- const syncState = readSyncState(dshHome)
3554
- const lp = localPending(dshHome, syncState.lastSyncAt, config?.profileName, { repo, appRoot })
3555
- // 后台调度里拿不到请求语言,所以中英各存一份,由客户端按自己的语言挑
3556
- const reasonZh = drift.ahead > 0 ? `领先 ${drift.ahead} 个提交`
3557
- : dirty > 0 ? `未提交 ${dirty} 项`
3558
- : (lp.known && lp.pending > 0) ? `本机 ${lp.pending} 个文件比上次同步新` : ''
3559
- const reasonEn = drift.ahead > 0 ? `${drift.ahead} commit(s) ahead`
3560
- : dirty > 0 ? `${dirty} uncommitted change(s)`
3561
- : (lp.known && lp.pending > 0) ? `${lp.pending} file(s) newer than the last sync` : ''
3562
- if (reasonZh) {
3563
- // 自动推送不需要确认(用户已开开关),排队等退出即可;推完把客户端拉回来
3564
- const restart = settings.autoRestart !== false
3565
- const started = await spawnWatcher({ mode: 'push', repo, appRoot, dshHome, restart })
3566
- if (started.ok) {
3567
- autoState.pending = { mode: 'push', at: Date.now(), reason: reasonZh, reasonZh, reasonEn, restart, watcherPid: started.pid, closeRequested: false }
3568
- } else {
3569
- autoState.lastCheckError = started.error
3570
- }
3571
- }
3572
- }
3581
+ await checkAndQueueAutoSync(dshHome, config, settings, repo, appRoot)
3573
3582
  } catch (error) {
3574
3583
  autoState.checking = false
3575
3584
  autoState.lastCheckAt = Date.now()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-config-sync-plugin",
3
- "version": "0.3.98",
3
+ "version": "0.3.99",
4
4
  "type": "module",
5
5
  "description": "在 DSH 设置里查看 dsh-config 仓库与本机的同步状态,并生成退出后执行的 push/pull 脚本。同步动作本身不在应用内执行。面板与文案跟随客户端语言(中文/English)。",
6
6
  "main": "./index.js",
@@ -1905,6 +1905,23 @@ console.log('== 0.3.97:检测到待拉取/待推送就主动强制退出 DSH
1905
1905
  /result && result\.queued[\s\S]{0,60}?forceCloseDshDetached/.test(idxSrc), 'index.js queued 守卫')
1906
1906
  }
1907
1907
  console.log('')
1908
+ console.log('== 0.3.99:开关变更即时触发自动同步判断(不等 2 分钟周期)==')
1909
+ {
1910
+ const idxSrc = readFileSync(join(pluginRootSelf, 'index.js'), 'utf8')
1911
+ check('新增 checkAndQueueAutoSync 核心判断函数',
1912
+ /async function checkAndQueueAutoSync\(/.test(idxSrc), 'index.js checkAndQueueAutoSync')
1913
+ check('scheduleAutoStart 复用 checkAndQueueAutoSync',
1914
+ /await checkAndQueueAutoSync\(/.test(idxSrc), 'index.js scheduleAutoStart 复用')
1915
+ check('handleSettings 保存 autoPull/autoPush 后立即调用 checkAndQueueAutoSync',
1916
+ /changed\.autoPull \|\| changed\.autoPush/.test(idxSrc)
1917
+ && /await checkAndQueueAutoSync\(/.test(idxSrc), 'index.js 即时触发')
1918
+ check('checkAndQueueAutoSync 支持 isManualTrigger 跳过确认框',
1919
+ /isManualTrigger/.test(idxSrc) && /skipConfirm/.test(idxSrc), 'index.js 手动触发模式')
1920
+ check('checkAndQueueAutoSync 返回 queued/mode/reason 供上层判断',
1921
+ /return \{ queued: true, mode:/.test(idxSrc)
1922
+ && /return \{ queued: false, reason:/.test(idxSrc), 'index.js 返回结构')
1923
+ }
1924
+ console.log('')
1908
1925
  console.log('== 0.3.98:默认工作区会话同步(稳定名 --default-workspace--,资料仍不同步)==')
1909
1926
  {
1910
1927
  const capPath = join(pluginRuntime, 'capture.ps1')