dsh-config-sync-plugin 0.3.97 → 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 +20 -0
- package/README.md +20 -0
- package/index.js +82 -73
- package/package.json +1 -1
- package/runtime/apply.ps1 +64 -2
- package/runtime/capture.ps1 +29 -4
- package/tools/selftest.mjs +47 -0
package/README.en.md
CHANGED
|
@@ -257,6 +257,24 @@ 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
|
+
|
|
270
|
+
### Default workspace conversations now sync; its files stay local (0.3.98)
|
|
271
|
+
|
|
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":
|
|
273
|
+
|
|
274
|
+
1. **Conversations use a cross-machine stable name**: capture writes the default-workspace session directory under the stable name `--default-workspace--` (alongside the existing `--ws-<id>--` and `--im--`), and normalizes the session header cwd to `{{WORKSPACE_HOME}}/default-workspace`; apply resolves the local default workspace path from **this machine's** `workspace.json` global.defaultWorkspaceId, re-encodes the directory name, fixes the header cwd, aligns projcache, and merges the session ids into the local default workspace registry entry — so DSH shows these conversations under the default workspace. No dependence on A's workspace ID.
|
|
275
|
+
2. **Files stay local, registration kept**: capture skips default-workspace file copying by `defaultWorkspaceId` (files are per-machine), but keeps its `workspace-registry.json` entry — session ownership and sessionIds merging need it. apply's file restore likewise won't lay the default-workspace files slot back down.
|
|
276
|
+
3. **Regression guards**: 10 selftest assertions (stable-name mapping, cwd normalization, local path resolution, re-encoding, sessionIds merge, files skip). Normal workspaces, im, all other mechanics and scope filters unchanged. On a fresh machine, let DSH start once to create its default-workspace registration before the first pull.
|
|
277
|
+
|
|
260
278
|
### Clicking × with pending changes now actively exits DSH and syncs immediately (0.3.97)
|
|
261
279
|
|
|
262
280
|
In 0.3.96 the beacon could "check once on × and queue the watcher", but the watcher then sat in `wait`, **passively waiting for all DSH processes to disappear** before working. In practice clicking × leaves 7 orphaned processes (the plugin itself runs in one of them, the host child), so the normal close path often fails to clean up and the watcher just idles until its 30-minute timeout — appearing as "queued but nothing happens". This version closes the last gap:
|
|
@@ -745,6 +763,8 @@ The log shows which path ran: `文件比较(快筛):比对 N 个,深哈
|
|
|
745
763
|
|
|
746
764
|
| Date | Version | Summary |
|
|
747
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) |
|
|
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 |
|
|
748
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 |
|
|
749
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" |
|
|
750
770
|
| 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 |
|
package/README.md
CHANGED
|
@@ -277,6 +277,24 @@ 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
|
+
|
|
290
|
+
### 默认工作区会话纳入同步、资料仍各机独立(0.3.98)
|
|
291
|
+
|
|
292
|
+
以前默认工作区整个跳过同步。原因:它的会话目录名由**本机 WorkspaceHome 绝对路径**编码(`--C-Users-...-default-workspace--`),两台机器路径不同,直接把 A 机的目录铺到 B 机只会留下 DSH 认不出的死目录(表现为默认工作区历史为空)。这次按「只同步会话、不同步资料」拆开处理:
|
|
293
|
+
|
|
294
|
+
1. **会话走跨机稳定名**:capture 把默认工作区会话目录写成稳定名 `--default-workspace--`(和已有工作区的 `--ws-<id>--`、`--im--` 并列),会话头 cwd 归一到 `{{WORKSPACE_HOME}}/default-workspace`;apply 按**本机** `workspace.json` 的 `global.defaultWorkspaceId` 反查本机默认工作区路径,重编码目录名、改会话头 cwd、对齐 projcache,并把会话 id 并入本机默认工作区登记条目——DSH 才能把这些会话显示在默认工作区下。不依赖 A 机的工作区 ID。
|
|
295
|
+
2. **资料不同步、登记保留**:capture 按 `defaultWorkspaceId` 跳过默认工作区的资料文件拷贝(资料各机独立),但 `workspace-registry.json` 里它的登记条目保留——会话归属与 sessionIds 合并不需要它。apply 侧资料还原也不会把默认工作区资料槽铺回来。
|
|
296
|
+
3. **防回归**:selftest 加 10 条断言(稳定名映射、cwd 归一、本机路径反查、重编码、sessionIds 合并、资料跳过)。普通工作区、im、其余机制与同步内容勾选过滤全部不变。首次拉取前 B 机需先让 DSH 启动一次生成默认工作区登记。
|
|
297
|
+
|
|
280
298
|
### 点 × 检测到待同步就主动退出 DSH 并立即执行(0.3.97)
|
|
281
299
|
|
|
282
300
|
0.3.96 的信标已经能「点 × 判一次并排好 watcher」,但 watcher 进入 `wait` 后是**被动等 DSH 进程全部消失**才开工。实测点 × 后 DSH 常残留 7 个进程(插件就跑在其中的 host 子进程里),单靠正常关闭流程经常关不干净,watcher 只能干等到 30 分钟超时——表现为「排了队但迟迟不动」。这次补上最后一环:
|
|
@@ -727,6 +745,8 @@ pwsh -File sync.ps1 push -ScanHistory
|
|
|
727
745
|
|
|
728
746
|
| 日期 | 版本 | 要点 |
|
|
729
747
|
|---|---|---|
|
|
748
|
+
| 2026-11-25 | 0.3.99 | 勾选自动同步开关即时触发判断(核心逻辑抽离为 checkAndQueueAutoSync,scheduleAutoStart/triggerAutoSyncOnClose/handleSettings 三路复用,开关变更即时排队,手动模式跳过确认框) |
|
|
749
|
+
| 2026-11-25 | 0.3.98 | 默认工作区会话纳入同步(稳定名 --default-workspace--,apply 按本机 defaultWorkspaceId 重编码目录/cwd/projcache 并合并 sessionIds),资料仍各机独立 |
|
|
730
750
|
| 2026-11-25 | 0.3.97 | 点 × 检测到待拉取/待推送后,从独立进程主动强制退出 DSH 进程族(detached taskkill /F),watcher 立刻执行,不再干等残留进程 |
|
|
731
751
|
| 2026-11-24 | 0.3.96 | 页面卸载信标(pagehide→sendBeacon /auto-exit)取代失效的 Electron 关窗钩子,fast 判据(本地 tracking ref、不 fetch)毫秒级排队 watcher,真正修好「点 × 不触发」 |
|
|
732
752
|
| 2026-11-24 | 0.3.95 | 修复 post-exit.ps1 丢 UTF-8 BOM 致 PS5.1 按 GBK 解码、55 处语法错启动即崩:三处执行体补回 BOM,selftest 加全量 ps1 BOM 断言防回归 |
|
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
|
-
|
|
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
package/runtime/apply.ps1
CHANGED
|
@@ -123,9 +123,16 @@ function Sync-WorkspaceApply {
|
|
|
123
123
|
# DSH 归属权威就是这里的 sessionIds 成员:没登记的会话即便 cwd 改对也不会挂到工作区。
|
|
124
124
|
# 按 conversations\sessions\--ws-<workspaceId>--\<sessionId>\ 反推补齐,与会话还原段同源。
|
|
125
125
|
$sidByWsDir = @{}
|
|
126
|
+
$defaultSids = @() # 默认工作区会话(仓库稳定名 --default-workspace--)的会话 id
|
|
126
127
|
$convSessionsDir = Join-Path $RepoRoot 'conversations\sessions'
|
|
127
128
|
if (Test-Path -LiteralPath $convSessionsDir) {
|
|
128
129
|
foreach ($wd in @(Get-ChildItem -LiteralPath $convSessionsDir -Directory -Force)) {
|
|
130
|
+
if ($wd.Name -eq '--default-workspace--') {
|
|
131
|
+
foreach ($sd in @(Get-ChildItem -LiteralPath $wd.FullName -Directory -Force)) {
|
|
132
|
+
$defaultSids += [string]$sd.Name
|
|
133
|
+
}
|
|
134
|
+
continue
|
|
135
|
+
}
|
|
129
136
|
if ($wd.Name -notmatch '^--ws-(?<id>.+)--$') { continue }
|
|
130
137
|
$wid = $Matches['id']
|
|
131
138
|
if (-not $sidByWsDir.ContainsKey($wid)) { $sidByWsDir[$wid] = @() }
|
|
@@ -355,6 +362,13 @@ function Sync-WorkspaceApply {
|
|
|
355
362
|
else { $localGlobal['defaultWorkspaceId'] = '' }
|
|
356
363
|
}
|
|
357
364
|
elseif (-not $localGlobal.Contains('defaultWorkspaceId')) { $localGlobal['defaultWorkspaceId'] = $localDefault }
|
|
365
|
+
# 默认工作区的会话 id 并入「本机」默认工作区条目:普通工作区靠上面 sidByWsDir 反推补进 sessionIds,
|
|
366
|
+
# 默认工作区是稳定名 --default-workspace-- 不那条路,这里用仓库会话目录反推出来的 $defaultSids 补。
|
|
367
|
+
# DSH 归属权威就是 sessionIds,不补的话 cwd 和目录名对了、会话也不会挂到默认工作区下。
|
|
368
|
+
$dwId = [string]$localGlobal['defaultWorkspaceId']
|
|
369
|
+
if ($defaultSids.Count -gt 0 -and $dwId -and $localTable.Contains($dwId)) {
|
|
370
|
+
$localTable[$dwId]['sessionIds'] = @(@($localTable[$dwId]['sessionIds']) + $defaultSids | Where-Object { $_ -ne '' } | Sort-Object -Unique)
|
|
371
|
+
}
|
|
358
372
|
$localGlobal['initialized'] = $true
|
|
359
373
|
$localGlobal['workspaceIds'] = $ids
|
|
360
374
|
$localGlobal['archivedSessionIds'] = @($gArchAll | Sort-Object -Unique)
|
|
@@ -426,6 +440,27 @@ function Sync-ConversationApply {
|
|
|
426
440
|
}
|
|
427
441
|
}
|
|
428
442
|
|
|
443
|
+
# 1b) 本机默认工作区路径:从「本机」workspace.json 的 global.defaultWorkspaceId 反查。
|
|
444
|
+
# 绝不用仓库 registry 里那台机器的 default ID——各机默认工作区 ID 不同;但「本机这一个」
|
|
445
|
+
# 由本机登记表唯一确定。默认工作区会话在仓库里是稳定名 --default-workspace--,
|
|
446
|
+
# 还原时按这里的本机路径重编码目录名 + 改写会话头 cwd,B 机才能认出。
|
|
447
|
+
$localDefaultWsPath = ''
|
|
448
|
+
$localWsReg = Join-Path $DshHome 'storages\workspace.json'
|
|
449
|
+
if (Test-Path -LiteralPath $localWsReg) {
|
|
450
|
+
try {
|
|
451
|
+
$lreg = Get-Content -LiteralPath $localWsReg -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
452
|
+
$ldefId = if ($lreg.global) { [string]$lreg.global.defaultWorkspaceId } else { '' }
|
|
453
|
+
if ($ldefId -and $lreg.tables -and $lreg.tables.workspaces) {
|
|
454
|
+
$lp = $lreg.tables.workspaces.PSObject.Properties[$ldefId]
|
|
455
|
+
if ($lp) { $localDefaultWsPath = [string]$lp.Value.path }
|
|
456
|
+
}
|
|
457
|
+
} catch { }
|
|
458
|
+
}
|
|
459
|
+
if (-not $localDefaultWsPath -and $WorkspaceHome) {
|
|
460
|
+
# 兜底:默认工作区路径固定就是 <WorkspaceHome>\default-workspace
|
|
461
|
+
$localDefaultWsPath = Join-Path $WorkspaceHome 'default-workspace'
|
|
462
|
+
}
|
|
463
|
+
|
|
429
464
|
# 2) 目录名重映射:A 机目录名 → B 机目录名;同时记下 B 机这条目录应对应的 cwd
|
|
430
465
|
# (会话文件 header 里的 cwd 必须与目录名编码一致,否则 DSH 判 corrupt)
|
|
431
466
|
$dirMap = @{}
|
|
@@ -450,6 +485,16 @@ function Sync-ConversationApply {
|
|
|
450
485
|
$newDir = Get-SessionDirName $imLocal
|
|
451
486
|
$dirMap[$oldDir] = $newDir
|
|
452
487
|
$cwdMap[$newDir] = $imLocal
|
|
488
|
+
} elseif ($e.special -eq 'default') {
|
|
489
|
+
# 默认工作区:按本机路径重编码目录名,会话头 cwd 也指本机默认工作区
|
|
490
|
+
if ($localDefaultWsPath) {
|
|
491
|
+
$newDir = Get-SessionDirName $localDefaultWsPath
|
|
492
|
+
$dirMap[$oldDir] = $newDir
|
|
493
|
+
$cwdMap[$newDir] = $localDefaultWsPath
|
|
494
|
+
} else {
|
|
495
|
+
$dirMap[$oldDir] = $oldDir
|
|
496
|
+
Warn ('默认工作区会话 {0} 找不到本机落点,请先在 DSH 里打开一次默认工作区再拉取' -f $oldDir)
|
|
497
|
+
}
|
|
453
498
|
} else {
|
|
454
499
|
$dirMap[$oldDir] = $oldDir # passthrough / 对应不到的工作区,原名保留
|
|
455
500
|
Warn ('会话目录 {0} 对应不到本机工作区,目录名与文件头 cwd 都无法重算,DSH 可能把它判为损坏:请在本机重新打开该工作区后再同步' -f $oldDir)
|
|
@@ -461,6 +506,15 @@ function Sync-ConversationApply {
|
|
|
461
506
|
if (Test-Path -LiteralPath $repoSessions) {
|
|
462
507
|
foreach ($d in @(Get-ChildItem -LiteralPath $repoSessions -Directory -Force)) {
|
|
463
508
|
if ($dirMap.ContainsKey($d.Name)) { continue }
|
|
509
|
+
# 默认工作区稳定名(session-index 缺失时兜底)
|
|
510
|
+
if ($d.Name -eq '--default-workspace--') {
|
|
511
|
+
if ($localDefaultWsPath) {
|
|
512
|
+
$newDir = Get-SessionDirName $localDefaultWsPath
|
|
513
|
+
$dirMap[$d.Name] = $newDir
|
|
514
|
+
$cwdMap[$newDir] = $localDefaultWsPath
|
|
515
|
+
}
|
|
516
|
+
continue
|
|
517
|
+
}
|
|
464
518
|
if ($d.Name -match '^--ws-(?<id>.+)--$') {
|
|
465
519
|
$id = $Matches['id']
|
|
466
520
|
$hit = $null
|
|
@@ -530,6 +584,12 @@ function Sync-ConversationApply {
|
|
|
530
584
|
$projMap[[string]$sd.Name] = $imLocal
|
|
531
585
|
}
|
|
532
586
|
}
|
|
587
|
+
# 默认工作区会话(--default-workspace-- 顶层)的 projcache 指到本机默认工作区
|
|
588
|
+
if ($wd.Name -eq '--default-workspace--' -and $localDefaultWsPath) {
|
|
589
|
+
foreach ($sd in @(Get-ChildItem -LiteralPath $wd.FullName -Directory -Force)) {
|
|
590
|
+
$projMap[[string]$sd.Name] = $localDefaultWsPath
|
|
591
|
+
}
|
|
592
|
+
}
|
|
533
593
|
}
|
|
534
594
|
}
|
|
535
595
|
if ($cwdMap.Count -gt 0 -or $projMap.Count -gt 0) {
|
|
@@ -568,8 +628,10 @@ function Sync-ConversationApply {
|
|
|
568
628
|
if ($b.Remap) {
|
|
569
629
|
# 相对路径第一段是 A 机的编码目录名,换成 B 机重编码后的名字
|
|
570
630
|
$parts = @($rel -split '\\')
|
|
571
|
-
#
|
|
572
|
-
|
|
631
|
+
# 默认工作区会话现在要同步:稳定的 --default-workspace-- 已在 dirMap 里映射到本机编码名,
|
|
632
|
+
# 下面会随 Remap 改写。老仓库里可能还留着「路径式」的默认工作区目录名(本机编码、对不上
|
|
633
|
+
# 任何映射),那种仍跳过不铺——它跨机无法重定位,铺了反而是死目录。
|
|
634
|
+
if ((Test-DefaultWorkspaceDir $parts[0]) -and -not $dirMap.ContainsKey($parts[0])) { continue }
|
|
573
635
|
# 该工作区的会话被取消勾选:不铺回本机(否则等于把未勾选的内容写回来了)
|
|
574
636
|
if (-not $dirMap.ContainsKey($parts[0]) -and $skipConvDirs.ContainsKey($parts[0])) { continue }
|
|
575
637
|
if ($dirMap.ContainsKey($parts[0])) { $parts[0] = $dirMap[$parts[0]] }
|
package/runtime/capture.ps1
CHANGED
|
@@ -233,6 +233,10 @@ function Sync-WorkspaceCapture {
|
|
|
233
233
|
foreach ($s in $skipWs) { $slotSet[$s.Slot] = $true }
|
|
234
234
|
$nAdd = 0; $nDel = 0
|
|
235
235
|
foreach ($e in $entries) {
|
|
236
|
+
# 默认工作区只同步「会话」、不同步「资料」(资料各机独立):登记条目已在上面的
|
|
237
|
+
# workspace-registry.json 里保留(会话归属/sessionIds 合并需要它),这里跳过资料文件
|
|
238
|
+
# 拷贝,也不给它建槽;镜像删除会把仓库里历史遗留的默认工作区资料槽清掉。
|
|
239
|
+
if ($e.Id -eq $gDefault) { continue }
|
|
236
240
|
$slotSet[$e.Slot] = $true
|
|
237
241
|
$srcRoot = $e.LocalPath
|
|
238
242
|
$dstRoot = Join-Path $wsDir $e.Slot
|
|
@@ -333,6 +337,10 @@ function Get-RepoSessionDirName([string]$kind, [string]$id, [string]$localDir) {
|
|
|
333
337
|
if ($safe) { return ('--ws-{0}--' -f $safe) }
|
|
334
338
|
}
|
|
335
339
|
if ($kind -eq 'im') { return '--im--' }
|
|
340
|
+
# 默认工作区用跨机稳定名:它的 ID 与路径编码各机都不同,无法像普通工作区那样按 ID/路径对齐。
|
|
341
|
+
# 每台机器有且只有一个默认工作区(title=default-workspace、路径=<WorkspaceHome>\default-workspace),
|
|
342
|
+
# apply 侧按「本机」workspace.json 的 defaultWorkspaceId 找到本机路径再重编码即可。
|
|
343
|
+
if ($kind -eq 'default') { return '--default-workspace--' }
|
|
336
344
|
return $localDir
|
|
337
345
|
}
|
|
338
346
|
|
|
@@ -344,6 +352,8 @@ function Sync-ConversationCapture {
|
|
|
344
352
|
$wsEncToId = @{}
|
|
345
353
|
$wsPathById = @{}
|
|
346
354
|
$regSrc = Join-Path $DshHome 'storages/workspace.json'
|
|
355
|
+
$defaultWsEnc = '' # 本机默认工作区的会话目录编码名(路径由 defaultWorkspaceId 反查)
|
|
356
|
+
$defaultWsPath = ''
|
|
347
357
|
if (Test-Path -LiteralPath $regSrc) {
|
|
348
358
|
$reg = Get-Content -LiteralPath $regSrc -Raw -Encoding UTF8 | ConvertFrom-Json
|
|
349
359
|
if ($reg.tables.workspaces) {
|
|
@@ -352,6 +362,12 @@ function Sync-ConversationCapture {
|
|
|
352
362
|
$wsPathById[[string]$prop.Name] = [string]$prop.Value.path
|
|
353
363
|
}
|
|
354
364
|
}
|
|
365
|
+
# 默认工作区:ID 从 global 读,路径从登记表反查(各机的 ID 不同,但「本机这一个」是确定的)
|
|
366
|
+
$defId = if ($reg.global) { [string]$reg.global.defaultWorkspaceId } else { '' }
|
|
367
|
+
if ($defId -and $wsPathById.ContainsKey($defId)) {
|
|
368
|
+
$defaultWsPath = $wsPathById[$defId]
|
|
369
|
+
$defaultWsEnc = Get-SessionDirName $defaultWsPath
|
|
370
|
+
}
|
|
355
371
|
}
|
|
356
372
|
$imEnc = Get-SessionDirName (Join-Path $DshHome 'im')
|
|
357
373
|
$sessSrc = Join-Path $DshHome 'sessions'
|
|
@@ -372,8 +388,12 @@ function Sync-ConversationCapture {
|
|
|
372
388
|
if (Test-Path -LiteralPath $sessSrc) {
|
|
373
389
|
foreach ($d in @(Get-ChildItem -LiteralPath $sessSrc -Directory -Force | Sort-Object Name)) {
|
|
374
390
|
if (Test-DefaultWorkspaceDir $d.Name) {
|
|
375
|
-
#
|
|
376
|
-
|
|
391
|
+
# 默认工作区的会话要同步(资料不同步)。ID/路径编码各机不同,用跨机稳定名 --default-workspace--
|
|
392
|
+
# 入库;apply 侧按本机 workspace.json 的 defaultWorkspaceId 找到本机路径再重编码。
|
|
393
|
+
$repoDir = Get-RepoSessionDirName 'default' '' $d.Name
|
|
394
|
+
$dirRemap[$d.Name] = $repoDir
|
|
395
|
+
$idxEntries += [ordered]@{ dir = $repoDir; special = 'default' }
|
|
396
|
+
Ok ('默认工作区会话纳入同步(稳定名 {0}):{1}' -f $repoDir, $d.Name)
|
|
377
397
|
continue
|
|
378
398
|
}
|
|
379
399
|
if ($wsEncToId.ContainsKey($d.Name) -and -not (Test-SyncItemEnabled $Scope 'conversations' ([string]$wsEncToId[$d.Name]))) {
|
|
@@ -407,6 +427,9 @@ function Sync-ConversationCapture {
|
|
|
407
427
|
# 不归一的话,仓库里这条 cwd 永远是「最后推上来的那台机器」的路径,两台机器互推时每次都改这批文件。
|
|
408
428
|
$imRepoDirName = Get-RepoSessionDirName 'im' '' ''
|
|
409
429
|
$cwdToken[$imRepoDirName] = ($DshHomeToken + '/im')
|
|
430
|
+
# 默认工作区的 cwd 归一到 {{WORKSPACE_HOME}}/default-workspace(路径各机不同,必须占位符)
|
|
431
|
+
$defaultRepoDirName = Get-RepoSessionDirName 'default' '' ''
|
|
432
|
+
if ($defaultWsPath) { $cwdToken[$defaultRepoDirName] = (ConvertTo-WorkspaceToken $defaultWsPath) }
|
|
410
433
|
foreach ($e in @($idxEntries)) {
|
|
411
434
|
if (-not $e.workspaceId) { continue }
|
|
412
435
|
$localCwd = $null
|
|
@@ -448,8 +471,10 @@ function Sync-ConversationCapture {
|
|
|
448
471
|
$rel = Get-RelPath $b.Src $f.FullName
|
|
449
472
|
if (-not $rel) { continue }
|
|
450
473
|
$parts = @($rel -split '\\')
|
|
451
|
-
#
|
|
452
|
-
|
|
474
|
+
# 默认工作区的「会话」现在也要入库(资料仍不同步):它在 dirRemap 里已映射成稳定名
|
|
475
|
+
# --default-workspace--,下面会随 Remap 一起改写。只有非 Remap 块仍按原名跳过默认工作区
|
|
476
|
+
# (那些块按 sessionId/hash 平铺,通常也不会出现这种目录名,纯属兜底)。
|
|
477
|
+
if ((-not $b.Remap) -and (Test-DefaultWorkspaceDir $parts[0])) { continue }
|
|
453
478
|
# 该工作区的会话被取消勾选:整块不抓(目录名保持本机编码,绝不能原样入库)
|
|
454
479
|
if ($b.Remap -and $skipSessDirs.ContainsKey($parts[0])) { continue }
|
|
455
480
|
if ($b.Remap -and $dirRemap.ContainsKey($parts[0])) { $parts[0] = $dirRemap[$parts[0]] }
|
package/tools/selftest.mjs
CHANGED
|
@@ -1904,5 +1904,52 @@ console.log('== 0.3.97:检测到待拉取/待推送就主动强制退出 DSH
|
|
|
1904
1904
|
check('只在 queued 时才强制退出(无待同步不误杀 DSH)',
|
|
1905
1905
|
/result && result\.queued[\s\S]{0,60}?forceCloseDshDetached/.test(idxSrc), 'index.js queued 守卫')
|
|
1906
1906
|
}
|
|
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('')
|
|
1925
|
+
console.log('== 0.3.98:默认工作区会话同步(稳定名 --default-workspace--,资料仍不同步)==')
|
|
1926
|
+
{
|
|
1927
|
+
const capPath = join(pluginRuntime, 'capture.ps1')
|
|
1928
|
+
const appPath = join(pluginRuntime, 'apply.ps1')
|
|
1929
|
+
const cap = existsSync(capPath) ? readFileSync(capPath, 'utf8') : ''
|
|
1930
|
+
const app = existsSync(appPath) ? readFileSync(appPath, 'utf8') : ''
|
|
1931
|
+
// capture:默认工作区会话用稳定名入库(不再跳过)
|
|
1932
|
+
check('capture Get-RepoSessionDirName 支持 default → --default-workspace--',
|
|
1933
|
+
/if \(\$kind -eq 'default'\) \{ return '--default-workspace--' \}/.test(cap), 'capture.ps1 default 稳定名')
|
|
1934
|
+
check('capture 默认工作区会话纳入映射(special=default,不再跳过)',
|
|
1935
|
+
/special = 'default'/.test(cap) && /默认工作区会话纳入同步/.test(cap), 'capture.ps1 顶层映射')
|
|
1936
|
+
check('capture 从本机 defaultWorkspaceId 反查默认工作区路径',
|
|
1937
|
+
/defaultWorkspaceId/.test(cap) && /\$defaultWsPath = \$wsPathById\[\$defId\]/.test(cap), 'capture.ps1 默认路径反查')
|
|
1938
|
+
check('capture 默认工作区 cwd 归一到 {{WORKSPACE_HOME}}/default-workspace',
|
|
1939
|
+
/\$cwdToken\[\$defaultRepoDirName\] = \(ConvertTo-WorkspaceToken \$defaultWsPath\)/.test(cap), 'capture.ps1 cwd 归一')
|
|
1940
|
+
check('capture 资料按 defaultWorkspaceId 跳过默认工作区(资料不同步、登记保留)',
|
|
1941
|
+
/if \(\$e\.Id -eq \$gDefault\) \{ continue \}/.test(cap), 'capture.ps1 资料跳过')
|
|
1942
|
+
check('capture 会话镜像不再无条件跳过默认工作区目录',
|
|
1943
|
+
/\(Test-DefaultWorkspaceDir \$parts\[0\]\)/.test(cap) && /-not \$b\.Remap/.test(cap), 'capture.ps1 镜像')
|
|
1944
|
+
// apply:按本机默认工作区路径重编码 + 注册会话到默认工作区
|
|
1945
|
+
check('apply 从本机 workspace.json 读 defaultWorkspaceId 反查默认工作区路径',
|
|
1946
|
+
/\$localDefaultWsPath/.test(app) && /\$lreg\.global\.defaultWorkspaceId/.test(app), 'apply.ps1 本机默认路径')
|
|
1947
|
+
check('apply special=default 分支按本机路径重编码目录与 cwd',
|
|
1948
|
+
/\$e\.special -eq 'default'/.test(app) && /\$newDir = Get-SessionDirName \$localDefaultWsPath/.test(app), 'apply.ps1 重编码')
|
|
1949
|
+
check('apply 2b 兜底与 projcache 都处理 --default-workspace--',
|
|
1950
|
+
/\$d\.Name -eq '--default-workspace--'/.test(app) && /\$wd\.Name -eq '--default-workspace--'/.test(app), 'apply.ps1 兜底+projcache')
|
|
1951
|
+
check('apply 默认工作区会话 id 并入本机默认工作区登记(sessionIds)',
|
|
1952
|
+
/\$localTable\[\$dwId\]\['sessionIds'\]/.test(app) && /\$defaultSids/.test(app), 'apply.ps1 sessionIds 合并')
|
|
1953
|
+
}
|
|
1907
1954
|
process.exit(failures === 0 ? 0 : 1)
|
|
1908
1955
|
|