dsh-session-sync 0.2.27 → 0.2.29
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/CHANGELOG.md +19 -0
- package/README.md +1 -1
- package/lib/encrypted.mjs +16 -7
- package/lib/engine.mjs +28 -9
- package/lib/git.mjs +1 -1
- package/lib/mirror.mjs +153 -11
- package/package.json +14 -14
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.2.29] - 2026-10-19
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
|
|
7
|
+
- **Mirror writes now reject parent-directory symlink escapes (issue #13).** `writeIfDifferent` resolves the parent chain and refuses any target outside `repoDir` (or `sessionRoot` for restore), in addition to the existing final-component check.
|
|
8
|
+
- **`git fetch` no longer force-updates the tracking ref (issue #13).** The refspec dropped its leading `+`; a remote that rewrote history now fails the fetch instead of silently force-updating and merging the rewritten tree.
|
|
9
|
+
- The token-as-username redaction half had already landed in 0.2.27.
|
|
10
|
+
|
|
11
|
+
## [0.2.28] - 2026-10-18
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
|
|
15
|
+
- **Delete/modify conflicts now keep both sides instead of aborting a pull.** When one side deletes and the other modifies, the modified bytes are preserved (remote side as a fork file when present), the deletion intent is honored, and the merge continues; the earlier code called `checkout --ours/--theirs` on a missing stage and aborted the whole pull.
|
|
16
|
+
- **Pulled-only sessions are never deleted by the next local sync.** `mirrorSessionRoot` records the source snapshot under `.git/` and only deletes files it previously mirrored from the local `sessionRoot`; a file that arrived via pull is left untouched, and an unknown snapshot deletes nothing.
|
|
17
|
+
- **Remote deletions reach the non-owner device without being resurrected.** `restoreSessionRoot` records hashes of copies it created; a later pull prunes a pulled copy only while its bytes still match the recorded hash. A user-edited copy is kept and pushes normally.
|
|
18
|
+
- The three-way tree merge also tolerates a missing side instead of putting `undefined` into the merged tree.
|
|
19
|
+
|
|
20
|
+
Closes the conflict-resolution half of PR #14. The redaction half landed in 0.2.27.
|
|
21
|
+
|
|
3
22
|
## [0.2.27] - 2026-10-18
|
|
4
23
|
|
|
5
24
|
### Fixed
|
package/README.md
CHANGED
|
@@ -182,7 +182,7 @@ Example override in your profile patch:
|
|
|
182
182
|
|
|
183
183
|
## Security boundaries
|
|
184
184
|
|
|
185
|
-
- **Never silently overwrite.** The append-only three-way merge keeps both sides on any divergence; fork files are never deleted, and git never force-pushes, resets, rebases, or switches branches.
|
|
185
|
+
- **Never silently overwrite.** The append-only three-way merge keeps both sides on any divergence, including delete/modify (modified bytes kept, deletion intent honored); fork files are never deleted, and git never force-pushes, resets, rebases, or switches branches.
|
|
186
186
|
- **Path containment.** Files are mirrored as opaque bytes with symlinks refused and every joined path containment-checked (`PATH_UNSAFE` fails loud).
|
|
187
187
|
- **Sanitized output.** Remote-URL credentials, tokens, and `key=value` secrets are redacted before reaching the model or the log; path display refuses anything outside its root.
|
|
188
188
|
- **No credential storage.** The plugin stores no credentials; git credentials live in your normal git credential helper. age keys are read from paths you configure (`ageIdentity`); keys never enter the sync repository.
|
package/lib/encrypted.mjs
CHANGED
|
@@ -155,16 +155,25 @@ export function mergeTrees(base, ours, theirs, stamp, deviceShort) {
|
|
|
155
155
|
let diverged = 0
|
|
156
156
|
const keys = new Set([...base.keys(), ...ours.keys(), ...theirs.keys()])
|
|
157
157
|
for (const rel of keys) {
|
|
158
|
-
const
|
|
158
|
+
const remote = theirs.get(rel)
|
|
159
|
+
const verdict = classifyPath(base.get(rel), ours.get(rel), remote)
|
|
160
|
+
// 远端删除(theirs 无字节)是这棵树里唯一「没有远端内容可留存」的分歧:
|
|
161
|
+
// 采纳删除=从合并树移除,其余=保留 ours 并计分歧。绝不把 undefined 塞进
|
|
162
|
+
// merged —— writeTree 对每个条目 fs.writeFile,undefined 会直接抛错。
|
|
159
163
|
if (verdict.kind === MERGE_KINDS.THEIRS_ONLY) {
|
|
160
|
-
|
|
164
|
+
if (remote === undefined) merged.delete(rel)
|
|
165
|
+
else merged.set(rel, remote)
|
|
161
166
|
adopted += 1
|
|
162
167
|
} else if (verdict.forkTheirs) {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
+
if (remote === undefined) {
|
|
169
|
+
diverged += 1
|
|
170
|
+
} else {
|
|
171
|
+
const forkRel = path.posix.join(path.posix.dirname(rel), forkFileName(path.posix.basename(rel), stamp, deviceShort))
|
|
172
|
+
merged.set(forkRel, remote)
|
|
173
|
+
forks.push(forkRel)
|
|
174
|
+
if (verdict.kind === MERGE_KINDS.APPEND_BOTH) appended += 1
|
|
175
|
+
else diverged += 1
|
|
176
|
+
}
|
|
168
177
|
}
|
|
169
178
|
// identical / ours-only:保留 ours(已在 merged 中)。
|
|
170
179
|
}
|
package/lib/engine.mjs
CHANGED
|
@@ -12,7 +12,7 @@ import { promises as fs } from 'node:fs'
|
|
|
12
12
|
import path from 'node:path'
|
|
13
13
|
import { MERGE_KINDS, MIRROR_DIR, DEVICE_FILE } from './constants.mjs'
|
|
14
14
|
import { planMerge } from './merge.mjs'
|
|
15
|
-
import { mirrorSessionRoot, restoreSessionRoot, ensureDeviceFile } from './mirror.mjs'
|
|
15
|
+
import { mirrorSessionRoot, restoreSessionRoot, pruneRestoredSessions, ensureDeviceFile } from './mirror.mjs'
|
|
16
16
|
import { collectStatus } from './status.mjs'
|
|
17
17
|
import { forkFileName } from './paths.mjs'
|
|
18
18
|
import { messageOf, SyncError } from './errors.mjs'
|
|
@@ -218,12 +218,15 @@ export class SyncEngine {
|
|
|
218
218
|
await this.#deps.git.addAll()
|
|
219
219
|
}
|
|
220
220
|
const newHead = await this.#deps.git.commitMerge()
|
|
221
|
+
const mirrorRoot = path.join(this.#deps.repoDir, MIRROR_DIR)
|
|
222
|
+
const pruned = await pruneRestoredSessions({ repoDir: this.#deps.repoDir, mirrorRoot, sessionRoot: this.#deps.sessionRoot })
|
|
221
223
|
const restored = await restoreSessionRoot({
|
|
222
|
-
|
|
224
|
+
repoDir: this.#deps.repoDir,
|
|
225
|
+
mirrorRoot,
|
|
223
226
|
sessionRoot: this.#deps.sessionRoot,
|
|
224
227
|
})
|
|
225
228
|
this.#deps.reportMeta({ lastPullAt: Date.now() })
|
|
226
|
-
this.#deps.logger.info(`sync pull ok: merged remote head ${remoteHead} → ${newHead ?? ''} (${result.appended} appended-both, ${result.diverged} diverged, ${result.forks.length} forks, ${restored.restored} restored)`)
|
|
229
|
+
this.#deps.logger.info(`sync pull ok: merged remote head ${remoteHead} → ${newHead ?? ''} (${result.appended} appended-both, ${result.diverged} diverged, ${result.forks.length} forks, ${restored.restored} restored, ${pruned.pruned.length} pulled-deletions)`)
|
|
227
230
|
if (result.forks.length > 0) this.#deps.onForks(result.forks)
|
|
228
231
|
return {
|
|
229
232
|
ok: true,
|
|
@@ -241,6 +244,10 @@ export class SyncEngine {
|
|
|
241
244
|
/**
|
|
242
245
|
* 冲突解决:逐路径分类 → ours/theirs 检出 → 远端侧留存为 fork 文件 →
|
|
243
246
|
* 全量暂存(提交由调用方落合并提交)。失败时 abortMerge 恢复。
|
|
247
|
+
*
|
|
248
|
+
* 删除/修改冲突(一侧删除、另一侧修改)同样走 keep-both:git 只为存在的一侧
|
|
249
|
+
* 登记 stage,缺失的一侧按「该侧删除了该文件」解释——删除意图保留,另一侧
|
|
250
|
+
* 按既有规则存续(远端内容仍以 fork 文件留存)。绝不因缺 stage 而中止整次 pull。
|
|
244
251
|
* @returns {Promise<{adopted: number, appended: number, diverged: number, forks: string[]}>}
|
|
245
252
|
*/
|
|
246
253
|
async #resolveConflicts() {
|
|
@@ -256,6 +263,8 @@ export class SyncEngine {
|
|
|
256
263
|
entries.push({ path: conflict.path, base, ours, theirs })
|
|
257
264
|
}
|
|
258
265
|
const { resolutions, summary } = planMerge(entries)
|
|
266
|
+
/** 路径 → ours/theirs 字节;某侧缺失即该侧删除了该文件(git 不登记其 stage)。 */
|
|
267
|
+
const sides = new Map(entries.map(entry => [entry.path, entry]))
|
|
259
268
|
const forks = []
|
|
260
269
|
const now = new Date()
|
|
261
270
|
// 严格 14 位 UTC 戳(yyyyMMddHHmmss):与 lib/encrypted.mjs 同口径剔除 ISO
|
|
@@ -263,23 +272,33 @@ export class SyncEngine {
|
|
|
263
272
|
// forkFileName 的 /^\d{14}$/ 失配并静默回退到纪元戳。
|
|
264
273
|
const stamp = now.toISOString().replace(/[-:T]/gu, '').slice(0, 14)
|
|
265
274
|
for (const resolution of resolutions) {
|
|
275
|
+
const side = sides.get(resolution.path)
|
|
276
|
+
const hasOurs = side?.ours !== undefined
|
|
277
|
+
const hasTheirs = side?.theirs !== undefined
|
|
278
|
+
const absolute = path.join(this.#deps.repoDir, ...resolution.path.split('/'))
|
|
279
|
+
// `checkout --ours/--theirs` 对缺失的 stage 会失败(`does not have
|
|
280
|
+
// our/their version`),因此每一侧都先看它在冲突里是否存在。
|
|
266
281
|
if (resolution.adoptTheirs) {
|
|
267
|
-
await this.#deps.git.checkoutStage('theirs', resolution.path)
|
|
282
|
+
if (hasTheirs) await this.#deps.git.checkoutStage('theirs', resolution.path)
|
|
283
|
+
else await fs.rm(absolute, { force: true })
|
|
268
284
|
continue
|
|
269
285
|
}
|
|
270
|
-
if (resolution.forkTheirs) {
|
|
271
|
-
// 先取 theirs 工作树字节 → 写 fork 文件 →
|
|
286
|
+
if (resolution.forkTheirs && hasTheirs) {
|
|
287
|
+
// 先取 theirs 工作树字节 → 写 fork 文件 → 再恢复 ours 侧。
|
|
272
288
|
await this.#deps.git.checkoutStage('theirs', resolution.path)
|
|
273
|
-
const absolute = path.join(this.#deps.repoDir, ...resolution.path.split('/'))
|
|
274
289
|
const content = await fs.readFile(absolute)
|
|
275
290
|
const forkName = forkFileName(path.posix.basename(resolution.path), stamp, this.deviceShort)
|
|
276
291
|
await fs.writeFile(path.join(path.dirname(absolute), forkName), content)
|
|
277
|
-
|
|
292
|
+
// ours 缺失=本地已删除:保留删除,远端内容只以 fork 文件存续。
|
|
293
|
+
if (hasOurs) await this.#deps.git.checkoutStage('ours', resolution.path)
|
|
294
|
+
else await fs.rm(absolute, { force: true })
|
|
278
295
|
const forkRel = path.posix.join(path.posix.dirname(resolution.path), forkName)
|
|
279
296
|
forks.push(forkRel)
|
|
280
297
|
continue
|
|
281
298
|
}
|
|
282
|
-
|
|
299
|
+
// 远端已删除(theirs 缺失):保留本地版本,分歧照常计入 summary.diverged。
|
|
300
|
+
if (hasOurs) await this.#deps.git.checkoutStage('ours', resolution.path)
|
|
301
|
+
else await fs.rm(absolute, { force: true })
|
|
283
302
|
}
|
|
284
303
|
await this.#deps.git.addAll()
|
|
285
304
|
return { adopted: summary.adopted, appended: summary.appended, diverged: summary.diverged, forks }
|
package/lib/git.mjs
CHANGED
|
@@ -262,7 +262,7 @@ export class GitBackend {
|
|
|
262
262
|
* @returns {Promise<boolean>} 是否拿到远端分支。
|
|
263
263
|
*/
|
|
264
264
|
async fetch(opts = {}) {
|
|
265
|
-
const result = await this.#git(['fetch', 'origin',
|
|
265
|
+
const result = await this.#git(['fetch', 'origin', `${this.#branch}:refs/remotes/origin/${this.#branch}`], { signal: opts.signal })
|
|
266
266
|
if (result.code === 0) return true
|
|
267
267
|
const stderr = String(result.stderr ?? '')
|
|
268
268
|
if (/couldn't find remote ref/i.test(stderr)) return false
|
package/lib/mirror.mjs
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
import { promises as fs } from 'node:fs'
|
|
12
12
|
import path from 'node:path'
|
|
13
|
+
import { createHash } from 'node:crypto'
|
|
13
14
|
import { FORK_NAME_RE, HOST_ARTIFACT_NAME_RE } from './constants.mjs'
|
|
14
15
|
|
|
15
16
|
/** 递归枚举 root 下全部常规文件(POSIX 相对路径;符号链接跳过并回报)。 */
|
|
@@ -37,8 +38,22 @@ async function listFiles(root) {
|
|
|
37
38
|
return { files, skipped }
|
|
38
39
|
}
|
|
39
40
|
|
|
40
|
-
/**
|
|
41
|
-
async function
|
|
41
|
+
/** 拒绝任何解析后落在 root 之外的写入(含父目录 symlink 逃逸)。 */
|
|
42
|
+
async function assertWriteInside(root, target) {
|
|
43
|
+
let rootReal
|
|
44
|
+
try { rootReal = await fs.realpath(root) } catch {
|
|
45
|
+
await fs.mkdir(root, { recursive: true })
|
|
46
|
+
rootReal = await fs.realpath(root)
|
|
47
|
+
}
|
|
48
|
+
await fs.mkdir(path.dirname(target), { recursive: true })
|
|
49
|
+
const parentReal = await fs.realpath(path.dirname(target))
|
|
50
|
+
const rel = path.relative(rootReal, parentReal)
|
|
51
|
+
if (rel.startsWith('..') || path.isAbsolute(rel)) throw new Error(`refusing to write outside ${rootReal}`)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** 内容一致则跳过写(返回 false),否则覆写(返回 true)。root 可选,提供时做真实路径 containment。 */
|
|
55
|
+
async function writeIfDifferent(target, content, root) {
|
|
56
|
+
if (root !== undefined) await assertWriteInside(root, target)
|
|
42
57
|
try {
|
|
43
58
|
const st = await fs.lstat(target)
|
|
44
59
|
if (st.isSymbolicLink()) throw new Error('refusing to write through a symbolic link')
|
|
@@ -56,6 +71,34 @@ async function writeIfDifferent(target, content) {
|
|
|
56
71
|
return true
|
|
57
72
|
}
|
|
58
73
|
|
|
74
|
+
/** 源快照状态文件:本地、不进提交、不随仓库同步。 */
|
|
75
|
+
export const SOURCE_SNAPSHOT_FILE = path.join('.git', 'dsh-session-sync-mirror.json')
|
|
76
|
+
|
|
77
|
+
/** @param {string} repoDir */
|
|
78
|
+
async function readSourceSnapshot(repoDir) {
|
|
79
|
+
try {
|
|
80
|
+
const raw = await fs.readFile(path.join(repoDir, SOURCE_SNAPSHOT_FILE), 'utf8')
|
|
81
|
+
const parsed = JSON.parse(raw)
|
|
82
|
+
if (parsed === null || typeof parsed !== 'object' || !Array.isArray(parsed.sourcePaths)) return undefined
|
|
83
|
+
return new Set(parsed.sourcePaths.filter(rel => typeof rel === 'string'))
|
|
84
|
+
} catch {
|
|
85
|
+
return undefined
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** @param {string} repoDir @param {Set<string>} sourcePaths */
|
|
90
|
+
async function writeSourceSnapshot(repoDir, sourcePaths) {
|
|
91
|
+
const target = path.join(repoDir, SOURCE_SNAPSHOT_FILE)
|
|
92
|
+
const payload = Buffer.from(`${JSON.stringify({ version: 1, sourcePaths: [...sourcePaths].sort() })}\n`)
|
|
93
|
+
try {
|
|
94
|
+
const st = await fs.stat(path.dirname(target))
|
|
95
|
+
if (!st.isDirectory()) return
|
|
96
|
+
} catch {
|
|
97
|
+
return
|
|
98
|
+
}
|
|
99
|
+
await writeIfDifferent(target, payload)
|
|
100
|
+
}
|
|
101
|
+
|
|
59
102
|
/**
|
|
60
103
|
* 把 sessionRoot 镜像进 <repoDir>/<mirrorDir>/…。
|
|
61
104
|
* 宿主私有会话产物(租约/迁移暂存,HOST_ARTIFACT_NAME_RE)既不复制、也不删除:
|
|
@@ -68,6 +111,7 @@ export async function mirrorSessionRoot(deps) {
|
|
|
68
111
|
const source = await listFiles(deps.sessionRoot)
|
|
69
112
|
const mirrorRoot = path.join(deps.repoDir, deps.mirrorDir)
|
|
70
113
|
const targets = new Set()
|
|
114
|
+
const previous = await readSourceSnapshot(deps.repoDir)
|
|
71
115
|
|
|
72
116
|
let mirrored = 0
|
|
73
117
|
let unchanged = 0
|
|
@@ -79,13 +123,14 @@ export async function mirrorSessionRoot(deps) {
|
|
|
79
123
|
}
|
|
80
124
|
const target = path.join(mirrorRoot, ...file.rel.split('/'))
|
|
81
125
|
targets.add(file.rel)
|
|
82
|
-
const wrote = await writeIfDifferent(target, await fs.readFile(file.absolute))
|
|
126
|
+
const wrote = await writeIfDifferent(target, await fs.readFile(file.absolute), deps.repoDir)
|
|
83
127
|
if (wrote) mirrored += 1
|
|
84
128
|
else unchanged += 1
|
|
85
129
|
}
|
|
86
130
|
|
|
87
131
|
// 删除:目标树中源已不存在、且非 fork 命名、非宿主私有产物的文件。
|
|
88
132
|
const deleted = []
|
|
133
|
+
const remotePreserved = []
|
|
89
134
|
const forkedPreserved = []
|
|
90
135
|
const hostArtifactsPreserved = []
|
|
91
136
|
const targetFiles = []
|
|
@@ -117,13 +162,102 @@ export async function mirrorSessionRoot(deps) {
|
|
|
117
162
|
hostArtifactsPreserved.push(rel)
|
|
118
163
|
continue
|
|
119
164
|
}
|
|
120
|
-
if (
|
|
121
|
-
|
|
122
|
-
|
|
165
|
+
if (targets.has(rel)) continue
|
|
166
|
+
if (previous === undefined || !previous.has(rel)) {
|
|
167
|
+
// Never delete a file this device did not previously mirror from its own
|
|
168
|
+
// sessionRoot: it arrived via pull, or provenance is unknown.
|
|
169
|
+
remotePreserved.push(rel)
|
|
170
|
+
continue
|
|
123
171
|
}
|
|
172
|
+
await fs.unlink(absolute)
|
|
173
|
+
deleted.push(rel)
|
|
174
|
+
}
|
|
175
|
+
await writeSourceSnapshot(deps.repoDir, targets)
|
|
176
|
+
|
|
177
|
+
return { mirrored, unchanged, skippedLinks: source.skipped, deleted, remotePreserved, snapshotUnknown: previous === undefined, forkedPreserved, hostArtifactsSkipped, hostArtifactsPreserved }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** 恢复状态文件:本地、不进提交、不随仓库同步。 */
|
|
181
|
+
export const RESTORE_STATE_FILE = path.join('.git', 'dsh-session-sync-restored.json')
|
|
182
|
+
|
|
183
|
+
/** @param {Buffer|string} content */
|
|
184
|
+
function hashContent(content) {
|
|
185
|
+
return createHash('sha256').update(content).digest('hex')
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** @param {string} repoDir */
|
|
189
|
+
async function readRestoreState(repoDir) {
|
|
190
|
+
try {
|
|
191
|
+
const raw = await fs.readFile(path.join(repoDir, RESTORE_STATE_FILE), 'utf8')
|
|
192
|
+
const parsed = JSON.parse(raw)
|
|
193
|
+
if (parsed === null || typeof parsed !== 'object' || parsed.version !== 1 || parsed.files === null || typeof parsed.files !== 'object') return new Map()
|
|
194
|
+
return new Map(Object.entries(parsed.files).filter(([rel, hash]) => typeof rel === 'string' && typeof hash === 'string'))
|
|
195
|
+
} catch {
|
|
196
|
+
return new Map()
|
|
124
197
|
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** @param {string} repoDir @param {Map<string,string>} files */
|
|
201
|
+
async function writeRestoreState(repoDir, files) {
|
|
202
|
+
const target = path.join(repoDir, RESTORE_STATE_FILE)
|
|
203
|
+
const payload = Buffer.from(`${JSON.stringify({ version: 1, files: Object.fromEntries([...files].sort(([a], [b]) => a.localeCompare(b))) })}
|
|
204
|
+
`)
|
|
205
|
+
try {
|
|
206
|
+
const st = await fs.stat(path.dirname(target))
|
|
207
|
+
if (!st.isDirectory()) return
|
|
208
|
+
} catch {
|
|
209
|
+
return
|
|
210
|
+
}
|
|
211
|
+
await writeIfDifferent(target, payload)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** @param {string} root */
|
|
215
|
+
async function mirrorRelSet(root) {
|
|
216
|
+
const set = new Set()
|
|
217
|
+
try {
|
|
218
|
+
const entries = await fs.readdir(root, { recursive: true, withFileTypes: true })
|
|
219
|
+
for (const entry of entries) {
|
|
220
|
+
if (!entry.isFile()) continue
|
|
221
|
+
const parent = entry.parentPath ?? entry.path ?? root
|
|
222
|
+
set.add(path.relative(root, path.join(parent, entry.name)).split(path.sep).join('/'))
|
|
223
|
+
}
|
|
224
|
+
} catch {}
|
|
225
|
+
return set
|
|
226
|
+
}
|
|
125
227
|
|
|
126
|
-
|
|
228
|
+
/**
|
|
229
|
+
* Remove live-store copies that this plugin restored from a previous pull and
|
|
230
|
+
* that the remote has now deleted, but only while the live bytes still match
|
|
231
|
+
* the restored hash. A user-edited copy is kept (and will push normally).
|
|
232
|
+
* @param {{repoDir: string, mirrorRoot: string, sessionRoot: string}} deps
|
|
233
|
+
* @returns {Promise<{pruned: string[], keptEdited: string[]}>}
|
|
234
|
+
*/
|
|
235
|
+
export async function pruneRestoredSessions(deps) {
|
|
236
|
+
const state = await readRestoreState(deps.repoDir)
|
|
237
|
+
if (state.size === 0) return { pruned: [], keptEdited: [] }
|
|
238
|
+
const mirror = await mirrorRelSet(deps.mirrorRoot)
|
|
239
|
+
const pruned = []
|
|
240
|
+
const keptEdited = []
|
|
241
|
+
for (const [rel, expected] of [...state]) {
|
|
242
|
+
if (mirror.has(rel)) continue
|
|
243
|
+
const target = path.join(deps.sessionRoot, ...rel.split('/'))
|
|
244
|
+
try {
|
|
245
|
+
const st = await fs.lstat(target)
|
|
246
|
+
if (st.isSymbolicLink()) { state.delete(rel); continue }
|
|
247
|
+
const content = await fs.readFile(target)
|
|
248
|
+
if (hashContent(content) === expected) {
|
|
249
|
+
await fs.unlink(target)
|
|
250
|
+
pruned.push(rel)
|
|
251
|
+
} else {
|
|
252
|
+
keptEdited.push(rel)
|
|
253
|
+
}
|
|
254
|
+
} catch {
|
|
255
|
+
// already gone
|
|
256
|
+
}
|
|
257
|
+
state.delete(rel)
|
|
258
|
+
}
|
|
259
|
+
await writeRestoreState(deps.repoDir, state)
|
|
260
|
+
return { pruned, keptEdited }
|
|
127
261
|
}
|
|
128
262
|
|
|
129
263
|
/**
|
|
@@ -131,8 +265,8 @@ export async function mirrorSessionRoot(deps) {
|
|
|
131
265
|
* into the mirror; the next push then saw those files as "missing locally" and
|
|
132
266
|
* unlinked them. This writes new/changed regular files back, never deletes, never
|
|
133
267
|
* follows symlinks, and never writes fork files or host-private artifacts.
|
|
134
|
-
* @param {object} deps - {mirrorRoot, sessionRoot, forkNameRe?}.
|
|
135
|
-
* @returns {Promise<{restored: number, unchanged: number, skipped: number}>}
|
|
268
|
+
* @param {object} deps - {repoDir, mirrorRoot, sessionRoot, forkNameRe?}.
|
|
269
|
+
* @returns {Promise<{restored: number, unchanged: number, skipped: number, tracked: number}>}
|
|
136
270
|
*/
|
|
137
271
|
export async function restoreSessionRoot(deps) {
|
|
138
272
|
const forkRe = deps.forkNameRe ?? FORK_NAME_RE
|
|
@@ -145,6 +279,7 @@ export async function restoreSessionRoot(deps) {
|
|
|
145
279
|
let restored = 0
|
|
146
280
|
let unchanged = 0
|
|
147
281
|
let skipped = 0
|
|
282
|
+
const state = deps.repoDir === undefined ? new Map() : await readRestoreState(deps.repoDir)
|
|
148
283
|
for (const entry of entries) {
|
|
149
284
|
if (!entry.isFile()) { skipped += 1; continue }
|
|
150
285
|
const parent = entry.parentPath ?? entry.path ?? deps.mirrorRoot
|
|
@@ -153,15 +288,22 @@ export async function restoreSessionRoot(deps) {
|
|
|
153
288
|
const basename = path.posix.basename(rel)
|
|
154
289
|
if (forkRe.test(basename) || HOST_ARTIFACT_NAME_RE.test(basename)) { skipped += 1; continue }
|
|
155
290
|
const target = path.join(deps.sessionRoot, ...rel.split('/'))
|
|
291
|
+
let existed = false
|
|
156
292
|
try {
|
|
157
293
|
const st = await fs.lstat(target)
|
|
158
294
|
if (st.isSymbolicLink()) { skipped += 1; continue }
|
|
295
|
+
existed = true
|
|
159
296
|
} catch { /* target absent */ }
|
|
160
|
-
const
|
|
297
|
+
const content = await fs.readFile(absolute)
|
|
298
|
+
const wrote = await writeIfDifferent(target, content, deps.sessionRoot)
|
|
161
299
|
if (wrote) restored += 1
|
|
162
300
|
else unchanged += 1
|
|
301
|
+
// Track only copies this plugin created (target absent before) or already
|
|
302
|
+
// tracked. A pre-existing user file is never claimed by the sync plugin.
|
|
303
|
+
if (!existed || state.has(rel)) state.set(rel, hashContent(content))
|
|
163
304
|
}
|
|
164
|
-
|
|
305
|
+
if (deps.repoDir !== undefined) await writeRestoreState(deps.repoDir, state)
|
|
306
|
+
return { restored, unchanged, skipped, tracked: state.size }
|
|
165
307
|
}
|
|
166
308
|
|
|
167
309
|
/**
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-session-sync",
|
|
3
3
|
"description": "Cross-device sync for DeepSeek Harness sessions: a dedicated git mirror of the session store, append-only three-way merge with keep-both + fork conflict resolution, /sync command, sync_pull/sync_push/sync_status tools, and configurable auto push/pull",
|
|
4
|
-
"version": "0.2.
|
|
4
|
+
"version": "0.2.29",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/PerryLink/dsh-session-sync.git"
|
|
@@ -114,22 +114,22 @@
|
|
|
114
114
|
"zod": "^4.4.3"
|
|
115
115
|
},
|
|
116
116
|
"devDependencies": {
|
|
117
|
-
"@deepseek-ai/dsh-attachment": "0.2.1-
|
|
117
|
+
"@deepseek-ai/dsh-attachment": "0.2.1-rc.2",
|
|
118
118
|
"@deepseek-ai/cordis": "^4.0.5-alpha.1",
|
|
119
119
|
"@deepseek-ai/cordis-plugin-include": "^1.0.10-alpha.1",
|
|
120
120
|
"@deepseek-ai/cordis-plugin-loader": "^1.0.6-alpha.1",
|
|
121
|
-
"@deepseek-ai/dsh-commands": "0.2.1-
|
|
122
|
-
"@deepseek-ai/dsh-llm": "0.2.1-
|
|
123
|
-
"@deepseek-ai/dsh-session": "0.2.1-
|
|
124
|
-
"@deepseek-ai/dsh-session-format-v3-to-v4": "0.2.1-
|
|
125
|
-
"@deepseek-ai/dsh-storage": "0.2.1-
|
|
126
|
-
"@deepseek-ai/dsh-storage-domain": "0.2.1-
|
|
127
|
-
"@deepseek-ai/dsh-storage-json": "0.2.1-
|
|
128
|
-
"@deepseek-ai/dsh-subprocess": "0.2.1-
|
|
129
|
-
"@deepseek-ai/dsh-system-prompt": "0.2.1-
|
|
130
|
-
"@deepseek-ai/dsh-tools": "0.2.1-
|
|
131
|
-
"@deepseek-ai/dsh-user-approval": "0.2.1-
|
|
132
|
-
"@deepseek-ai/dsh-user-questions": "0.2.1-
|
|
121
|
+
"@deepseek-ai/dsh-commands": "0.2.1-rc.2",
|
|
122
|
+
"@deepseek-ai/dsh-llm": "0.2.1-rc.2",
|
|
123
|
+
"@deepseek-ai/dsh-session": "0.2.1-rc.2",
|
|
124
|
+
"@deepseek-ai/dsh-session-format-v3-to-v4": "0.2.1-rc.2",
|
|
125
|
+
"@deepseek-ai/dsh-storage": "0.2.1-rc.2",
|
|
126
|
+
"@deepseek-ai/dsh-storage-domain": "0.2.1-rc.2",
|
|
127
|
+
"@deepseek-ai/dsh-storage-json": "0.2.1-rc.2",
|
|
128
|
+
"@deepseek-ai/dsh-subprocess": "0.2.1-rc.2",
|
|
129
|
+
"@deepseek-ai/dsh-system-prompt": "0.2.1-rc.2",
|
|
130
|
+
"@deepseek-ai/dsh-tools": "0.2.1-rc.2",
|
|
131
|
+
"@deepseek-ai/dsh-user-approval": "0.2.1-rc.2",
|
|
132
|
+
"@deepseek-ai/dsh-user-questions": "0.2.1-rc.2",
|
|
133
133
|
"@deepseek-ai/schemastery": "^3.18.5-alpha.1",
|
|
134
134
|
"@types/node": "^22.19.0",
|
|
135
135
|
"oxlint": "0.18.1",
|