dsh-session-sync 0.2.33 → 0.3.2

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/lib/render.mjs CHANGED
@@ -30,6 +30,16 @@ export function renderStatus(status) {
30
30
  if (status.forks.length > 0) {
31
31
  lines.push(` fork files (both-sides-kept conflicts): ${status.forks.slice(0, 10).join(', ')}${status.forks.length > 10 ? ' …' : ''}`)
32
32
  }
33
+ if ((status.cwdMapped ?? 0) > 0 || (status.cwdUnmapped ?? 0) > 0) {
34
+ lines.push(` cwd projection: ${status.cwdMapped ?? 0} mapped to local path(s), ${status.cwdUnmapped ?? 0} unmapped (read-only under ${status.cwdPlaceholder ?? 'sessionRoot/--unmapped--'})`)
35
+ }
36
+ if ((status.followedSymlinks ?? 0) > 0) {
37
+ lines.push(` followed symbolic links inside sessionRoot: ${status.followedSymlinks}`)
38
+ }
39
+ if ((status.skippedSymlinks ?? 0) > 0) {
40
+ const sample = (status.skippedSymlinkPaths ?? []).slice(0, 5).join(', ')
41
+ lines.push(` skipped symbolic links (not mirrored; followSessionSymlinks=${status.followSessionSymlinks === true}): ${status.skippedSymlinks}${sample.length > 0 ? ` — ${sample}${status.skippedSymlinks > 5 ? ' …' : ''}` : ''}`)
42
+ }
33
43
  if (status.diffStat !== '') {
34
44
  lines.push(` diff HEAD..remote:\n${status.diffStat.split('\n').map(line => ` ${line}`).join('\n')}`)
35
45
  }
@@ -62,7 +72,7 @@ export function renderDiff(status) {
62
72
 
63
73
  /**
64
74
  * pull 结果文本。
65
- * @param {{ok: boolean, pulled?: boolean, merged?: boolean, adopted?: number, appended?: number, diverged?: number, forks?: string[], head?: string, error?: string, warnings?: string[]}} result - pull 产物。
75
+ * @param {{ok: boolean, pulled?: boolean, merged?: boolean, adopted?: number, appended?: number, diverged?: number, forks?: string[], head?: string, restored?: number, cwdMapped?: number, cwdUnmapped?: number, error?: string, warnings?: string[]}} result - pull 产物。
66
76
  * @returns {string}
67
77
  */
68
78
  export function renderPull(result) {
@@ -76,6 +86,8 @@ export function renderPull(result) {
76
86
  if ((result.adopted ?? 0) > 0) lines.push(` adopted ${result.adopted} remote-only append(s) (local side was unchanged)`)
77
87
  if ((result.appended ?? 0) > 0) lines.push(` both sides appended in ${result.appended} file(s): local version kept, remote version preserved as fork file(s)`)
78
88
  if ((result.diverged ?? 0) > 0) lines.push(` diverged in ${result.diverged} file(s) (non-append rewrite): local version kept, remote version preserved as fork file(s) — inspect before continuing`)
89
+ if ((result.cwdMapped ?? 0) > 0) lines.push(` cwd projection: ${result.cwdMapped} restored session(s) mapped to a local path (header on disk is local; the mirror keeps the origin device's bytes)`)
90
+ if ((result.cwdUnmapped ?? 0) > 0) lines.push(` cwd projection: ${result.cwdUnmapped} restored session(s) have no cwdMap entry — shown read-only under the --unmapped-- placeholder`)
79
91
  for (const fork of (result.forks ?? []).slice(0, 20)) lines.push(` fork ${fork}`)
80
92
  if ((result.forks ?? []).length > 20) lines.push(` … ${result.forks.length - 20} more`)
81
93
  for (const warning of (result.warnings ?? [])) lines.push(` warning: ${warning}`)
@@ -84,7 +96,7 @@ export function renderPull(result) {
84
96
 
85
97
  /**
86
98
  * push 结果文本。
87
- * @param {{ok: boolean, pushed?: boolean, head?: string, mirrored?: number, deleted?: number, remote?: string, error?: string, warnings?: string[]}} result - push 产物。
99
+ * @param {{ok: boolean, pushed?: boolean, head?: string, mirrored?: number, deleted?: number, remote?: string, skippedSymlinks?: number, skippedSymlinkPaths?: string[], error?: string, warnings?: string[]}} result - push 产物。
88
100
  * @returns {string}
89
101
  */
90
102
  export function renderPush(result) {
@@ -95,6 +107,9 @@ export function renderPush(result) {
95
107
  ` mirrored ${result.mirrored ?? 0} file(s)${(result.deleted ?? 0) > 0 ? `, deleted ${result.deleted} (removed locally)` : ''}`,
96
108
  ` pushed to ${result.remote ?? 'remote'} → head ${result.head ?? ''}`,
97
109
  ]
110
+ if ((result.skippedSymlinks ?? 0) > 0) {
111
+ lines.push(` skipped ${result.skippedSymlinks} symbolic link(s) under sessionRoot (not mirrored; set followSessionSymlinks: true to include links inside the root)`)
112
+ }
98
113
  for (const warning of (result.warnings ?? [])) lines.push(` warning: ${warning}`)
99
114
  return lines.join('\n')
100
115
  }
package/lib/sanitize.mjs CHANGED
File without changes
@@ -0,0 +1,142 @@
1
+ // lib/sessionlog.mjs — 会话日志首帧(header)读取/改写(零 DSH 依赖,仅 node:zlib)。
2
+ //
3
+ // 宿主 session-persistence-jsonl 把每个会话写成一串**独立的 zstd 帧**:第一帧
4
+ // 只含 header 行,之后每个追加批次各一帧(`compression: none` 时是纯 JSONL,
5
+ // header 就是第一行)。追加从不重写已提交帧。因此投影只需要替换第一帧/第一行,
6
+ // 其余字节原样保留——这也让反向投影可以"把镜像里的原首帧接回本地尾部",追加
7
+ // 事件仍逐字节相同(append-only 合并语义不受影响)。
8
+ //
9
+ // 本模块只做结构性扫描与解压/压缩;任何不可解析的输入都返回 undefined,由调用方
10
+ // 保持文件原样(绝不猜测、绝不改写看不懂的字节)。
11
+
12
+ import { constants, zstdCompressSync, zstdDecompressSync } from 'node:zlib'
13
+
14
+ const ZSTD_MAGIC = 0xFD2FB528
15
+ // 与宿主 compressZstdFrame 同参数(带 checksum 的 zstd 帧)。
16
+ const ZSTD_CHECKSUM_OPTIONS = { params: { [constants.ZSTD_c_checksumFlag]: 1 } }
17
+
18
+ /** 宿主会话日志文件名:`session.v<N>.jsonl` 或 `session.v<N>.jsonl.zstd`。 */
19
+ export const SESSION_LOG_NAME_RE = /^session\.v\d+\.jsonl(?:\.zstd)?$/u
20
+
21
+ /**
22
+ * 扩展名是否表示 zstd 编码。
23
+ * @param {string} name - 文件名。
24
+ * @returns {boolean} `.zstd` 结尾为 true。
25
+ */
26
+ export function isCompressedName(name) {
27
+ return typeof name === 'string' && name.endsWith('.zstd')
28
+ }
29
+
30
+ /**
31
+ * 扫描首个完整 zstd 帧的结束偏移(只走结构,不解压)。
32
+ * 参考 zstd 帧格式:magic + frame header descriptor + 可选字段 + blocks + 可选
33
+ * checksum。任何结构损坏/尾部不完整都返回 undefined。
34
+ * @param {Buffer} buffer - 文件字节。
35
+ * @returns {number|undefined} 首帧的排他结束偏移。
36
+ */
37
+ function firstFrameEnd(buffer) {
38
+ let offset = 0
39
+ if (buffer.length < 4) return undefined
40
+ if (buffer.readUInt32LE(offset) !== ZSTD_MAGIC) return undefined
41
+ offset += 4
42
+ if (offset === buffer.length) return undefined
43
+ const descriptor = buffer.readUInt8(offset)
44
+ offset += 1
45
+ if ((descriptor & 0x18) !== 0) return undefined
46
+ const contentSizeFlag = descriptor >>> 6
47
+ const singleSegment = (descriptor & 0x20) !== 0
48
+ const checksum = (descriptor & 0x04) !== 0
49
+ const dictionaryFlag = descriptor & 0x03
50
+ const dictionaryBytes = dictionaryFlag === 3 ? 4 : dictionaryFlag
51
+ const contentSizeBytes = contentSizeFlag === 0
52
+ ? (singleSegment ? 1 : 0)
53
+ : 1 << contentSizeFlag
54
+ const remainingHeaderBytes = (singleSegment ? 0 : 1) + dictionaryBytes + contentSizeBytes
55
+ if (buffer.length - offset < remainingHeaderBytes) return undefined
56
+ offset += remainingHeaderBytes
57
+ for (;;) {
58
+ if (buffer.length - offset < 3) return undefined
59
+ const blockHeader = buffer.readUIntLE(offset, 3)
60
+ offset += 3
61
+ const lastBlock = (blockHeader & 1) !== 0
62
+ const blockType = (blockHeader >>> 1) & 0x03
63
+ const blockSize = blockHeader >>> 3
64
+ if (blockType === 0x03) return undefined
65
+ const payloadBytes = blockType === 0x01 ? 1 : blockSize
66
+ if (buffer.length - offset < payloadBytes) return undefined
67
+ offset += payloadBytes
68
+ if (lastBlock) break
69
+ }
70
+ if (checksum) {
71
+ if (buffer.length - offset < 4) return undefined
72
+ offset += 4
73
+ }
74
+ return offset
75
+ }
76
+
77
+ /**
78
+ * 读取会话日志的 header 行(首帧/首行)与其余字节。
79
+ * @param {Buffer} buffer - 文件字节。
80
+ * @param {boolean} compressed - 是否 zstd 编码。
81
+ * @returns {{header: {type: string, id: string, cwd?: string, [key: string]: unknown}, text: string, frame: Buffer, rest: Buffer}|undefined} 解析结果。
82
+ */
83
+ export function readSessionHeader(buffer, compressed) {
84
+ if (!Buffer.isBuffer(buffer)) return undefined
85
+ let frame
86
+ let text
87
+ let rest
88
+ if (compressed) {
89
+ const end = firstFrameEnd(buffer)
90
+ if (end === undefined) return undefined
91
+ frame = buffer.subarray(0, end)
92
+ rest = buffer.subarray(end)
93
+ try {
94
+ text = zstdDecompressSync(frame).toString('utf8')
95
+ } catch {
96
+ return undefined
97
+ }
98
+ } else {
99
+ const newline = buffer.indexOf(0x0a)
100
+ if (newline < 0) return undefined
101
+ frame = buffer.subarray(0, newline + 1)
102
+ text = frame.toString('utf8')
103
+ rest = buffer.subarray(newline + 1)
104
+ }
105
+ if (!text.endsWith('\n')) return undefined
106
+ const line = text.slice(0, -1)
107
+ if (line.length === 0 || line.includes('\n')) return undefined
108
+ let header
109
+ try {
110
+ header = JSON.parse(line)
111
+ } catch {
112
+ return undefined
113
+ }
114
+ if (header === null || typeof header !== 'object' || Array.isArray(header)) return undefined
115
+ if (header.type !== 'session' || typeof header.id !== 'string' || header.id.length === 0) return undefined
116
+ if (header.cwd !== undefined && typeof header.cwd !== 'string') return undefined
117
+ return { header: /** @type {{type: string, id: string, cwd?: string, [key: string]: unknown}} */ (header), text, frame, rest }
118
+ }
119
+
120
+ /**
121
+ * 编码一个 header 行为独立帧(zstd 时与宿主同参数,含 checksum)。
122
+ * @param {{type: string, id: string, [key: string]: unknown}} header - header 对象。
123
+ * @param {boolean} compressed - 是否 zstd 编码。
124
+ * @returns {Buffer} 首帧/首行字节。
125
+ */
126
+ export function encodeSessionHeader(header, compressed) {
127
+ const line = Buffer.from(`${JSON.stringify(header)}\n`, 'utf8')
128
+ return compressed ? zstdCompressSync(line, ZSTD_CHECKSUM_OPTIONS) : line
129
+ }
130
+
131
+ /**
132
+ * 用新 header 替换首帧/首行,其余字节原样保留。
133
+ * @param {Buffer} buffer - 原文件字节。
134
+ * @param {boolean} compressed - 是否 zstd 编码。
135
+ * @param {{type: string, id: string, cwd?: string, [key: string]: unknown}} header - 新 header。
136
+ * @returns {Buffer|undefined} 改写后的字节;输入不可解析时 undefined。
137
+ */
138
+ export function rewriteSessionHeader(buffer, compressed, header) {
139
+ const parsed = readSessionHeader(buffer, compressed)
140
+ if (parsed === undefined) return undefined
141
+ return Buffer.concat([encodeSessionHeader(header, compressed), parsed.rest])
142
+ }
package/lib/status.mjs CHANGED
File without changes
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.33",
4
+ "version": "0.3.2",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/PerryLink/dsh-session-sync.git"
@@ -28,10 +28,10 @@
28
28
  "lib",
29
29
  "cordis.patch.yml",
30
30
  "README.md",
31
- "README-zh.md",
32
- "README-es.md",
33
- "README-pt.md",
34
- "README-hi.md",
31
+ "README.en.md",
32
+ "README.es.md",
33
+ "README.pt.md",
34
+ "README.hi.md",
35
35
  "ARCHITECTURE.md",
36
36
  "CHANGELOG.md",
37
37
  "SECURITY.md",
@@ -47,9 +47,26 @@
47
47
  },
48
48
  "capabilities": [
49
49
  "network:egress",
50
- "subprocess"
50
+ "subprocess",
51
+ "sessions:read",
52
+ "sessions:write"
51
53
  ],
52
- "labels": []
54
+ "labels": [],
55
+ "standards": {
56
+ "schema": "dsh-levels/1",
57
+ "entry": {
58
+ "status": "not-run",
59
+ "evidence": null
60
+ },
61
+ "flow": {
62
+ "status": "not-run",
63
+ "evidence": null
64
+ },
65
+ "boost": {
66
+ "status": "not-measured",
67
+ "evidence": null
68
+ }
69
+ }
53
70
  },
54
71
  "dshWorkshop": {
55
72
  "schema": "omdsh-workshop-package/v1",
@@ -118,31 +135,33 @@
118
135
  }
119
136
  },
120
137
  "dependencies": {
121
- "zod": "4.4.3"
138
+ "zod": "4.6.6"
122
139
  },
123
140
  "devDependencies": {
124
- "@deepseek-ai/dsh-attachment": "0.2.2-rc.1",
141
+ "@deepseek-ai/dsh-attachment": "0.2.2-rc.3",
125
142
  "@deepseek-ai/cordis": "^4.1.0",
126
143
  "@deepseek-ai/cordis-plugin-include": "^1.1.0",
127
144
  "@deepseek-ai/cordis-plugin-loader": "^1.1.0",
128
- "@deepseek-ai/dsh-commands": "0.2.2-rc.1",
129
- "@deepseek-ai/dsh-llm": "0.2.2-rc.1",
130
- "@deepseek-ai/dsh-session": "0.2.2-rc.1",
131
- "@deepseek-ai/dsh-session-format-v3-to-v4": "0.2.2-rc.1",
132
- "@deepseek-ai/dsh-storage": "0.2.2-rc.1",
133
- "@deepseek-ai/dsh-storage-domain": "0.2.2-rc.1",
134
- "@deepseek-ai/dsh-storage-json": "0.2.2-rc.1",
135
- "@deepseek-ai/dsh-subprocess": "0.2.2-rc.1",
136
- "@deepseek-ai/dsh-system-prompt": "0.2.2-rc.1",
137
- "@deepseek-ai/dsh-tools": "0.2.2-rc.1",
138
- "@deepseek-ai/dsh-user-approval": "0.2.2-rc.1",
139
- "@deepseek-ai/dsh-user-questions": "0.2.2-rc.1",
145
+ "@deepseek-ai/dsh-commands": "0.2.2-rc.3",
146
+ "@deepseek-ai/dsh-llm": "0.2.2-rc.3",
147
+ "@deepseek-ai/dsh-session": "0.2.2-rc.3",
148
+ "@deepseek-ai/dsh-session-format-v3-to-v4": "0.2.2-rc.3",
149
+ "@deepseek-ai/dsh-session-projection": "0.2.2-rc.3",
150
+ "@deepseek-ai/dsh-session-persistence-jsonl": "0.2.2-rc.3",
151
+ "@deepseek-ai/dsh-storage": "0.2.2-rc.3",
152
+ "@deepseek-ai/dsh-storage-domain": "0.2.2-rc.3",
153
+ "@deepseek-ai/dsh-storage-json": "0.2.2-rc.3",
154
+ "@deepseek-ai/dsh-subprocess": "0.2.2-rc.3",
155
+ "@deepseek-ai/dsh-system-prompt": "0.2.2-rc.3",
156
+ "@deepseek-ai/dsh-tools": "0.2.2-rc.3",
157
+ "@deepseek-ai/dsh-user-approval": "0.2.2-rc.3",
158
+ "@deepseek-ai/dsh-user-questions": "0.2.2-rc.3",
140
159
  "@deepseek-ai/schemastery": "~3.19.0",
141
160
  "@types/node": "^22.19.0",
142
161
  "oxlint": "0.18.1",
143
162
  "typescript": "^5.9.0",
144
- "@deepseek-ai/dsh-working-directory": "0.2.2-rc.1",
145
- "@deepseek-ai/dsh-package-manifest": "0.2.2-rc.1"
163
+ "@deepseek-ai/dsh-working-directory": "0.2.2-rc.3",
164
+ "@deepseek-ai/dsh-package-manifest": "0.2.2-rc.3"
146
165
  },
147
166
  "scripts": {
148
167
  "typecheck": "tsc -p tsconfig.check.json",
package/types.d.ts CHANGED
@@ -94,10 +94,29 @@ export interface Config {
94
94
  autoPullOnStart?: boolean
95
95
  /** 每个 turn/end 后自动推送(配置即授权,不再确认)。 */
96
96
  autoPushOnTurnEnd?: boolean
97
+ /**
98
+ * 自动推送前等待该会话落盘的上限(ms;默认 2000,0 = 不等待)。
99
+ * 宿主的 JSONL 持久化对 live 事件有 200ms 批量写窗;等待调用公开的
100
+ * `ctx.sessions.flush(session)` 落盘屏障,超时只记日志并照常推送。
101
+ */
102
+ durabilityTimeoutMs?: number
97
103
  /** 周期自动拉取间隔分钟(0 = 关闭;上限 10080)。 */
98
104
  pullIntervalMinutes?: number
99
105
  /** 确认通道:auto(userQuestions 优先,其次 approval)· userQuestions · approval。 */
100
106
  confirmVia?: 'auto' | 'userQuestions' | 'approval'
107
+ /**
108
+ * 恢复期 cwd 投影表(issue #28):`[{ from, to }]`,最长前缀优先,前缀按
109
+ * 路径段边界匹配。命中时恢复出的会话以 `to` 下的本机路径可见;未命中的
110
+ * 跨平台 cwd 归一到 `<sessionRoot>/--unmapped--` 并以只读视图呈现。
111
+ * 同步仓库里的字节始终保留原设备的 cwd。
112
+ */
113
+ cwdMap?: ReadonlyArray<{ from: string, to: string }>
114
+ /**
115
+ * 镜像时是否跟随符号链接(issue #30;默认 false)。只有 realpath 仍落在
116
+ * sessionRoot 内的链接才会被读取/递归;越界链接一律跳过并在 sync_status 里
117
+ * 报告。默认值保持严格的"绝不跟随"。
118
+ */
119
+ followSessionSymlinks?: boolean
101
120
  /** git 子进程终止宽限(ms;默认 10000)。 */
102
121
  graceMs?: number
103
122
  /** 单条 git 命令超时(ms;默认 120000)。 */
@@ -132,6 +151,22 @@ export interface StatusValue {
132
151
  lastPushAt?: number
133
152
  lastError?: string
134
153
  error?: string
154
+ /** 本次镜像跳过的符号链接数(未镜像;default 严格模式)。 */
155
+ skippedSymlinks?: number
156
+ /** 跳过的符号链接路径样本(最多 20 条)。 */
157
+ skippedSymlinkPaths?: string[]
158
+ /** 本次镜像跟随的根内符号链接数(followSessionSymlinks: true 时)。 */
159
+ followedSymlinks?: number
160
+ /** 恢复期投影到本机 cwd 的会话数。 */
161
+ cwdMapped?: number
162
+ /** 无 cwdMap 命中的跨平台会话数(占位只读视图)。 */
163
+ cwdUnmapped?: number
164
+ /** 无法投影的会话文件数(header 不可解析/旧布局)。 */
165
+ cwdProjectionSkipped?: number
166
+ /** 占位只读视图的目录(`<sessionRoot>/--unmapped--`)。 */
167
+ cwdPlaceholder?: string
168
+ /** 是否开启符号链接跟随(诊断用)。 */
169
+ followSessionSymlinks?: boolean
135
170
  warnings?: string[]
136
171
  }
137
172
 
@@ -145,6 +180,9 @@ export interface PullValue {
145
180
  diverged?: number
146
181
  forks?: string[]
147
182
  head?: string
183
+ restored?: number
184
+ cwdMapped?: number
185
+ cwdUnmapped?: number
148
186
  error?: string
149
187
  warnings?: string[]
150
188
  }
@@ -157,6 +195,8 @@ export interface PushValue {
157
195
  mirrored?: number
158
196
  deleted?: number
159
197
  remote?: string
198
+ skippedSymlinks?: number
199
+ skippedSymlinkPaths?: string[]
160
200
  error?: string
161
201
  warnings?: string[]
162
202
  }