dsh-session-sync 0.1.0
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/ARCHITECTURE.md +86 -0
- package/CHANGELOG.md +18 -0
- package/LICENSE +201 -0
- package/README.es.md +181 -0
- package/README.hi.md +181 -0
- package/README.md +181 -0
- package/README.pt.md +181 -0
- package/README.zh.md +181 -0
- package/SECURITY.md +39 -0
- package/THIRD_PARTY_NOTICES.md +25 -0
- package/cordis.patch.yml +41 -0
- package/index.mjs +775 -0
- package/lib/constants.mjs +109 -0
- package/lib/domain.mjs +29 -0
- package/lib/engine.mjs +289 -0
- package/lib/errors.mjs +99 -0
- package/lib/gate.mjs +152 -0
- package/lib/git.mjs +377 -0
- package/lib/merge.mjs +96 -0
- package/lib/mirror.mjs +120 -0
- package/lib/paths.mjs +107 -0
- package/lib/render.mjs +107 -0
- package/lib/sanitize.mjs +105 -0
- package/lib/status.mjs +69 -0
- package/package.json +144 -0
- package/types.d.ts +132 -0
package/lib/paths.mjs
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// lib/paths.mjs — 路径解析与越界校验(零依赖)。
|
|
2
|
+
//
|
|
3
|
+
// 全部纯函数:解析同步根/工作树根、校验嵌套关系、把会话文件相对路径
|
|
4
|
+
// 安全地映射进工作树。任何把外部输入拼进路径的地方必须先经 assertRelPath
|
|
5
|
+
// 或 containment 检查(路径越界 = PATH_UNSAFE 响亮失败,绝不静默)。
|
|
6
|
+
|
|
7
|
+
import path from 'node:path'
|
|
8
|
+
import { badConfig, pathUnsafe } from './errors.mjs'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 解析会话存储根:配置为空时取 $DSH_HOME/sessions;两者皆缺 → 响亮失败。
|
|
12
|
+
* 目录是否真实存在由消费方(镜像/状态)处理;这里只做路径层面的解析。
|
|
13
|
+
* @param {string} configured - Config.sessionRoot('' = 默认)。
|
|
14
|
+
* @param {string|undefined} [dshHome] - 环境 $DSH_HOME(测试注入)。
|
|
15
|
+
* @returns {string} 绝对路径。
|
|
16
|
+
*/
|
|
17
|
+
export function resolveSessionRoot(configured, dshHome = process.env.DSH_HOME) {
|
|
18
|
+
if (typeof configured === 'string' && configured.length > 0) {
|
|
19
|
+
return path.resolve(configured)
|
|
20
|
+
}
|
|
21
|
+
if (typeof dshHome === 'string' && dshHome.length > 0) {
|
|
22
|
+
return path.join(dshHome, 'sessions')
|
|
23
|
+
}
|
|
24
|
+
throw badConfig('sessionRoot is empty and $DSH_HOME is not set; run under dsh or set sessionRoot explicitly')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 解析同步仓库工作树根:配置为空时取 $DSH_HOME/dsh-session-sync/repo。
|
|
29
|
+
* @param {string} configured - Config.repoDir('' = 默认)。
|
|
30
|
+
* @param {string|undefined} [dshHome] - 环境 $DSH_HOME(测试注入)。
|
|
31
|
+
* @returns {string} 绝对路径。
|
|
32
|
+
*/
|
|
33
|
+
export function resolveRepoDir(configured, dshHome = process.env.DSH_HOME) {
|
|
34
|
+
if (typeof configured === 'string' && configured.length > 0) {
|
|
35
|
+
return path.resolve(configured)
|
|
36
|
+
}
|
|
37
|
+
if (typeof dshHome === 'string' && dshHome.length > 0) {
|
|
38
|
+
return path.join(dshHome, 'dsh-session-sync', 'repo')
|
|
39
|
+
}
|
|
40
|
+
throw badConfig('repoDir is empty and $DSH_HOME is not set; run under dsh or set repoDir explicitly')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 两目录不得互相包含:镜像会把 sessionRoot 复制进 repoDir(或反之时递归)。
|
|
45
|
+
* @param {string} sessionRoot - 已解析的会话存储根。
|
|
46
|
+
* @param {string} repoDir - 已解析的同步仓库根。
|
|
47
|
+
* @returns {void} 违反时抛 BAD_CONFIG。
|
|
48
|
+
*/
|
|
49
|
+
export function assertNestingSafe(sessionRoot, repoDir) {
|
|
50
|
+
const inside = (a, b) => {
|
|
51
|
+
const rel = path.relative(a, b)
|
|
52
|
+
return rel === '' || (!rel.startsWith('..') && !path.isAbsolute(rel))
|
|
53
|
+
}
|
|
54
|
+
if (inside(sessionRoot, repoDir)) {
|
|
55
|
+
throw badConfig(`repoDir ${JSON.stringify(repoDir)} must not be inside sessionRoot ${JSON.stringify(sessionRoot)} (the mirror would copy itself)`)
|
|
56
|
+
}
|
|
57
|
+
if (inside(repoDir, sessionRoot)) {
|
|
58
|
+
throw badConfig(`sessionRoot ${JSON.stringify(sessionRoot)} must not be inside repoDir ${JSON.stringify(repoDir)} (the mirror would copy itself)`)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 校验会话文件的相对路径:拒绝绝对路径、空段、`.`/`..`、反斜杠与越界。
|
|
64
|
+
* 镜像与 fork 路径拼接的唯一入口。
|
|
65
|
+
* @param {string} rel - 相对路径(POSIX 分隔)。
|
|
66
|
+
* @returns {string} 规范化后的同一相对路径。
|
|
67
|
+
*/
|
|
68
|
+
export function assertRelPath(rel) {
|
|
69
|
+
if (typeof rel !== 'string' || rel.length === 0) {
|
|
70
|
+
throw pathUnsafe('relative path must be a non-empty string')
|
|
71
|
+
}
|
|
72
|
+
if (rel.includes('\\') || rel.includes('\0')) {
|
|
73
|
+
throw pathUnsafe(`relative path contains reserved characters: ${JSON.stringify(rel)}`)
|
|
74
|
+
}
|
|
75
|
+
const segments = rel.split('/')
|
|
76
|
+
for (const segment of segments) {
|
|
77
|
+
if (segment.length === 0 || segment === '.' || segment === '..') {
|
|
78
|
+
throw pathUnsafe(`relative path contains traversal segment: ${JSON.stringify(rel)}`)
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return rel
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 相对路径 → 工作树镜像路径(<repoDir>/sessions/<rel>)。
|
|
86
|
+
* @param {string} repoDir - 同步仓库根。
|
|
87
|
+
* @param {string} mirrorDir - 镜像目录名(MIRROR_DIR)。
|
|
88
|
+
* @param {string} rel - 已校验的相对路径。
|
|
89
|
+
* @returns {string} 工作树内绝对路径。
|
|
90
|
+
*/
|
|
91
|
+
export function mirrorPathOf(repoDir, mirrorDir, rel) {
|
|
92
|
+
return path.join(repoDir, mirrorDir, ...assertRelPath(rel).split('/'))
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* fork 文件名:<basename>.remote-fork-<UTC 14 位时间戳>-<设备短 id>。
|
|
97
|
+
* 同一目录内同一时刻的冲突靠设备 id 区分;绝不覆盖既有 fork 文件。
|
|
98
|
+
* @param {string} basename - 原文件名。
|
|
99
|
+
* @param {string} timestampUtc - UTC 时间戳(yyyyMMddHHmmss)。
|
|
100
|
+
* @param {string} deviceShort - 设备短 id(≤ 8 位小写十六进制)。
|
|
101
|
+
* @returns {string} fork 文件名。
|
|
102
|
+
*/
|
|
103
|
+
export function forkFileName(basename, timestampUtc, deviceShort) {
|
|
104
|
+
const stamp = /^\d{14}$/u.test(timestampUtc) ? timestampUtc : '19700101000000'
|
|
105
|
+
const device = /^[0-9a-z]{1,8}$/iu.test(deviceShort) ? deviceShort.toLowerCase() : 'unknown'
|
|
106
|
+
return `${basename}.remote-fork-${stamp}-${device}`
|
|
107
|
+
}
|
package/lib/render.mjs
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// lib/render.mjs — 结果文本渲染(纯函数,零依赖;输入全部 JSON 安全)。
|
|
2
|
+
|
|
3
|
+
import { MERGE_KINDS } from './constants.mjs'
|
|
4
|
+
|
|
5
|
+
/** 时间戳 → 本地可读时间(缺省 '-')。 */
|
|
6
|
+
function timeOf(value) {
|
|
7
|
+
if (typeof value !== 'number' || value <= 0) return '-'
|
|
8
|
+
return new Date(value).toISOString()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* /sync 状态文本(sync_status 工具共用同一渲染)。
|
|
13
|
+
* @param {object} status - collectStatus 产物。
|
|
14
|
+
* @returns {string}
|
|
15
|
+
*/
|
|
16
|
+
export function renderStatus(status) {
|
|
17
|
+
if (!status.ok) {
|
|
18
|
+
return `sync: status unavailable: ${status.error ?? 'unknown error'}`
|
|
19
|
+
}
|
|
20
|
+
const lines = [
|
|
21
|
+
`sync: repo branch ${status.branch} · remote ${status.remote}`,
|
|
22
|
+
` head ${status.head ?? '(no commits yet)'} · remote ${status.remoteHead ?? '(never fetched)'}`,
|
|
23
|
+
` ahead ${status.ahead} · behind ${status.behind}${status.mergeInProgress ? ' · merge in progress (resolve or re-pull)' : ''}`,
|
|
24
|
+
]
|
|
25
|
+
if (status.dirty.length > 0) {
|
|
26
|
+
lines.push(` uncommitted (${status.dirty.length}): ${status.dirty.slice(0, 10).join(', ')}${status.dirty.length > 10 ? ' …' : ''}`)
|
|
27
|
+
} else {
|
|
28
|
+
lines.push(' mirror clean (nothing uncommitted)')
|
|
29
|
+
}
|
|
30
|
+
if (status.forks.length > 0) {
|
|
31
|
+
lines.push(` fork files (both-sides-kept conflicts): ${status.forks.slice(0, 10).join(', ')}${status.forks.length > 10 ? ' …' : ''}`)
|
|
32
|
+
}
|
|
33
|
+
if (status.diffStat !== '') {
|
|
34
|
+
lines.push(` diff HEAD..remote:\n${status.diffStat.split('\n').map(line => ` ${line}`).join('\n')}`)
|
|
35
|
+
}
|
|
36
|
+
if (status.lastCommits.length > 0) {
|
|
37
|
+
lines.push(` last commits:\n${status.lastCommits.map(line => ` ${line}`).join('\n')}`)
|
|
38
|
+
}
|
|
39
|
+
if (status.error !== undefined) lines.push(` last error: ${status.error}`)
|
|
40
|
+
return lines.join('\n')
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* /sync diff 文本:本地未提交变更 + HEAD..远端统计(只读)。
|
|
45
|
+
* @param {object} status - collectStatus 产物。
|
|
46
|
+
* @returns {string}
|
|
47
|
+
*/
|
|
48
|
+
export function renderDiff(status) {
|
|
49
|
+
if (!status.ok) return `sync: diff unavailable: ${status.error ?? 'unknown error'}`
|
|
50
|
+
const lines = ['sync: diff (local worktree vs last-fetched remote; read-only)']
|
|
51
|
+
if (status.dirty.length === 0) lines.push(' uncommitted changes: none')
|
|
52
|
+
else {
|
|
53
|
+
lines.push(` uncommitted changes (${status.dirty.length}):`)
|
|
54
|
+
for (const path of status.dirty.slice(0, 50)) lines.push(` M ${path}`)
|
|
55
|
+
if (status.dirty.length > 50) lines.push(` … ${status.dirty.length - 50} more`)
|
|
56
|
+
}
|
|
57
|
+
if (status.diffStat === '') lines.push(' HEAD..remote: no difference (or remote never fetched)')
|
|
58
|
+
else lines.push(` HEAD..remote:\n${status.diffStat.split('\n').map(line => ` ${line}`).join('\n')}`)
|
|
59
|
+
return lines.join('\n')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* pull 结果文本。
|
|
64
|
+
* @param {{ok: boolean, pulled?: boolean, merged?: boolean, adopted?: number, appended?: number, diverged?: number, forks?: string[], head?: string, error?: string}} result - pull 产物。
|
|
65
|
+
* @returns {string}
|
|
66
|
+
*/
|
|
67
|
+
export function renderPull(result) {
|
|
68
|
+
if (!result.ok) return `sync: pull failed: ${result.error ?? 'unknown error'}`
|
|
69
|
+
const lines = ['sync: pull ok']
|
|
70
|
+
if (!result.pulled) {
|
|
71
|
+
lines.push(' already up to date (no remote changes or remote never fetched)')
|
|
72
|
+
return lines.join('\n')
|
|
73
|
+
}
|
|
74
|
+
lines.push(` remote changes merged${result.merged === true ? ' (merge commit)' : ''} → head ${result.head ?? ''}`)
|
|
75
|
+
if ((result.adopted ?? 0) > 0) lines.push(` adopted ${result.adopted} remote-only append(s) (local side was unchanged)`)
|
|
76
|
+
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)`)
|
|
77
|
+
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`)
|
|
78
|
+
for (const fork of (result.forks ?? []).slice(0, 20)) lines.push(` fork ${fork}`)
|
|
79
|
+
if ((result.forks ?? []).length > 20) lines.push(` … ${result.forks.length - 20} more`)
|
|
80
|
+
return lines.join('\n')
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* push 结果文本。
|
|
85
|
+
* @param {{ok: boolean, pushed?: boolean, head?: string, mirrored?: number, deleted?: number, remote?: string, error?: string}} result - push 产物。
|
|
86
|
+
* @returns {string}
|
|
87
|
+
*/
|
|
88
|
+
export function renderPush(result) {
|
|
89
|
+
if (!result.ok) return `sync: push failed: ${result.error ?? 'unknown error'}`
|
|
90
|
+
if (!result.pushed) return 'sync: push ok — nothing new to push (mirror already committed and remote up to date)'
|
|
91
|
+
return [
|
|
92
|
+
'sync: push ok',
|
|
93
|
+
` mirrored ${result.mirrored ?? 0} file(s)${(result.deleted ?? 0) > 0 ? `, deleted ${result.deleted} (removed locally)` : ''}`,
|
|
94
|
+
` pushed to ${result.remote ?? 'remote'} → head ${result.head ?? ''}`,
|
|
95
|
+
].join('\n')
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 工具规范结果里的错误块(pull/push/status 共用)。
|
|
100
|
+
* @param {string} message - 已脱敏的错误消息。
|
|
101
|
+
* @returns {{ok: boolean, error: string}}
|
|
102
|
+
*/
|
|
103
|
+
export function errorValue(message) {
|
|
104
|
+
return { ok: false, error: message }
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export { MERGE_KINDS, timeOf }
|
package/lib/sanitize.mjs
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// lib/sanitize.mjs — 展示/日志脱敏纯函数(零依赖)。
|
|
2
|
+
//
|
|
3
|
+
// 规则:任何可能携带凭据的文本(remote URL、git 输出、错误消息)在进入
|
|
4
|
+
// 日志、命令结果或工具结果前都必须过这里的纯函数。函数绝不抛错、绝不
|
|
5
|
+
// 访问 I/O;输入不合法时返回保守的脱敏文本。
|
|
6
|
+
|
|
7
|
+
/** URL 中视为凭据的查询键(值一律整体打码)。 */
|
|
8
|
+
const CREDENTIAL_QUERY_KEYS = /^(?:access_?token|token|key|secret|password|passwd|auth|credential|code|signature|x-amz-|sig)$/iu
|
|
9
|
+
|
|
10
|
+
/** 常见令牌形态(前缀 + 足够长的 secret)。 */
|
|
11
|
+
const TOKEN_PATTERN = /\b(?:sk-[A-Za-z0-9_-]{8,}|ghp_[A-Za-z0-9]{16,}|gho_[A-Za-z0-9]{16,}|xox[baprs]-[A-Za-z0-9-]{8,}|AKIA[0-9A-Z]{12,}|Bearer\s+[A-Za-z0-9._~+/=-]{8,}|authorization:\s*[A-Za-z0-9._~+/=-]{8,})/giu
|
|
12
|
+
|
|
13
|
+
/** key=value 形态的凭据(值整体打码)。 */
|
|
14
|
+
const CREDENTIAL_ASSIGNMENT = /\b((?:api[_-]?key|access[_-]?token|auth[_-]?token|refresh[_-]?token|password|passwd|client[_-]?secret|private[_-]?key)\s*=\s*)([^\s&;,]+)/giu
|
|
15
|
+
|
|
16
|
+
/** URL userinfo(user:password@)——git stderr 可能回显远端地址。 */
|
|
17
|
+
const URL_USERINFO = /\/\/([^/\s:@]+):([^/\s@]+)@/gu
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 脱敏 remote URL:用户信息中的密码打码,凭据查询键的值打码。
|
|
21
|
+
* scp 语法(user@host:path)不含密码,原样保留(用户名不是秘密)。
|
|
22
|
+
* @param {string} remote - git remote 地址。
|
|
23
|
+
* @returns {string} 脱敏后的地址。
|
|
24
|
+
*/
|
|
25
|
+
export function sanitizeRemote(remote) {
|
|
26
|
+
if (typeof remote !== 'string' || remote.length === 0) return '<unset>'
|
|
27
|
+
const trimmed = remote.trim()
|
|
28
|
+
try {
|
|
29
|
+
const url = new URL(trimmed)
|
|
30
|
+
if (url.username !== '' && url.password !== '') {
|
|
31
|
+
url.password = '***'
|
|
32
|
+
return url.toString()
|
|
33
|
+
}
|
|
34
|
+
for (const key of [...url.searchParams.keys()]) {
|
|
35
|
+
if (CREDENTIAL_QUERY_KEYS.test(key)) url.searchParams.set(key, '***')
|
|
36
|
+
}
|
|
37
|
+
return url.toString()
|
|
38
|
+
} catch {
|
|
39
|
+
return remote
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 文本脱敏:令牌、Bearer/authorization 头与 key=value 凭据打码。
|
|
45
|
+
* 非字符串输入先 String() 强转(绝不抛错——测试锁定此契约)。
|
|
46
|
+
* @param {unknown} text - 任意输出文本(git stderr、错误消息等)。
|
|
47
|
+
* @returns {string} 脱敏文本。
|
|
48
|
+
*/
|
|
49
|
+
export function redactText(text) {
|
|
50
|
+
if (typeof text !== 'string') return String(text)
|
|
51
|
+
return text
|
|
52
|
+
.replace(TOKEN_PATTERN, '***')
|
|
53
|
+
.replace(CREDENTIAL_ASSIGNMENT, '$1***')
|
|
54
|
+
.replace(URL_USERINFO, '//***@')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 展示路径:绝对路径落在 root 内时转为相对路径(仓库内视角),否则给出
|
|
59
|
+
* 中性占位——绝不把根外的路径原样呈现给模型/用户(路径越界防御)。
|
|
60
|
+
* @param {string} absolute - 绝对路径。
|
|
61
|
+
* @param {string} root - 归属根目录(绝对、规范化后)。
|
|
62
|
+
* @returns {string} 相对路径或 '<outside>'。
|
|
63
|
+
*/
|
|
64
|
+
export function displayPath(absolute, root) {
|
|
65
|
+
if (typeof absolute !== 'string' || absolute.length === 0) return '<missing>'
|
|
66
|
+
if (typeof root !== 'string' || root.length === 0) return '<outside>'
|
|
67
|
+
const rel = relativeWithin(absolute, root)
|
|
68
|
+
return rel === undefined ? '<outside>' : rel
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 计算 absolute 相对 root 的路径;不在 root 内时返回 undefined。
|
|
73
|
+
* 纯路径运算(不访问文件系统),拒绝 `..` 越界。
|
|
74
|
+
* @param {string} absolute - 绝对路径(可为相对形式,按原样计算)。
|
|
75
|
+
* @param {string} root - 根目录。
|
|
76
|
+
* @returns {string|undefined} 相对路径或 undefined。
|
|
77
|
+
*/
|
|
78
|
+
export function relativeWithin(absolute, root) {
|
|
79
|
+
const normalized = normalizeSegments(absolute)
|
|
80
|
+
const rootSegments = normalizeSegments(root)
|
|
81
|
+
if (normalized.length < rootSegments.length) return undefined
|
|
82
|
+
for (let index = 0; index < rootSegments.length; index += 1) {
|
|
83
|
+
if (normalized[index] !== rootSegments[index]) return undefined
|
|
84
|
+
}
|
|
85
|
+
// 相对段按栈折叠 `.` 与 `..`:`..` 在空栈上即越出 root → undefined(绝不
|
|
86
|
+
// 把能经 `..` 逃出 root 的路径呈现出来)。
|
|
87
|
+
const stack = []
|
|
88
|
+
for (const segment of normalized.slice(rootSegments.length)) {
|
|
89
|
+
if (segment === '..') {
|
|
90
|
+
if (stack.length === 0) return undefined
|
|
91
|
+
stack.pop()
|
|
92
|
+
} else {
|
|
93
|
+
stack.push(segment)
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
return stack.length === 0 ? '.' : stack.join('/')
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/** 把路径切成规范段(跳过空段与 `.`,不做 I/O)。 */
|
|
100
|
+
function normalizeSegments(path) {
|
|
101
|
+
return String(path)
|
|
102
|
+
.replaceAll('\\', '/')
|
|
103
|
+
.split('/')
|
|
104
|
+
.filter(segment => segment.length > 0 && segment !== '.')
|
|
105
|
+
}
|
package/lib/status.mjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// lib/status.mjs — 只读状态收集(零依赖;组合 GitBackend 原语)。
|
|
2
|
+
//
|
|
3
|
+
// 全部命令为本地只读(status/log/diff/ls-files/rev-parse),不 fetch、
|
|
4
|
+
// 不写任何文件;远端对比基于最近一次 fetch 的远端跟踪引用(报告注明)。
|
|
5
|
+
|
|
6
|
+
import { FORK_NAME_RE } from './constants.mjs'
|
|
7
|
+
import { sanitizeRemote } from './sanitize.mjs'
|
|
8
|
+
|
|
9
|
+
/** porcelain 状态行 → 路径(rename 取新路径)。 */
|
|
10
|
+
function pathOfPorcelainLine(line) {
|
|
11
|
+
const rest = line.slice(3)
|
|
12
|
+
const arrow = rest.indexOf(' -> ')
|
|
13
|
+
return arrow >= 0 ? rest.slice(arrow + 4) : rest
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 收集同步状态快照(JSON 安全、可直接进工具结果)。
|
|
18
|
+
* @param {import('./git.mjs').GitBackend} git - git 后端。
|
|
19
|
+
* @param {object} deps - {branch, remote}。
|
|
20
|
+
* @returns {Promise<{ok: boolean, branch: string, remote: string, head?: string, remoteHead?: string, ahead: number, behind: number, dirty: string[], forks: string[], diffStat: string, mergeInProgress: boolean, lastCommits: string[], error?: string}>}
|
|
21
|
+
*/
|
|
22
|
+
export async function collectStatus(git, deps) {
|
|
23
|
+
const base = {
|
|
24
|
+
ok: false,
|
|
25
|
+
branch: deps.branch,
|
|
26
|
+
remote: sanitizeRemote(deps.remote),
|
|
27
|
+
head: undefined,
|
|
28
|
+
remoteHead: undefined,
|
|
29
|
+
ahead: 0,
|
|
30
|
+
behind: 0,
|
|
31
|
+
dirty: [],
|
|
32
|
+
forks: [],
|
|
33
|
+
diffStat: '',
|
|
34
|
+
mergeInProgress: false,
|
|
35
|
+
lastCommits: [],
|
|
36
|
+
error: undefined,
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
const [head, remoteHead, dirtyLines, inProgress, lastCommits] = await Promise.all([
|
|
40
|
+
git.headSha(),
|
|
41
|
+
git.remoteHeadSha(),
|
|
42
|
+
git.dirtyLines(),
|
|
43
|
+
git.mergeInProgress(),
|
|
44
|
+
git.recentCommits(5),
|
|
45
|
+
])
|
|
46
|
+
base.head = head
|
|
47
|
+
base.remoteHead = remoteHead
|
|
48
|
+
base.dirty = dirtyLines.map(pathOfPorcelainLine)
|
|
49
|
+
base.mergeInProgress = inProgress
|
|
50
|
+
base.lastCommits = lastCommits
|
|
51
|
+
if (head !== undefined && remoteHead !== undefined && head !== remoteHead) {
|
|
52
|
+
const [ahead, behind, diffStat] = await Promise.all([
|
|
53
|
+
git.countCommitsSafe(remoteHead, head),
|
|
54
|
+
git.countCommitsSafe(head, remoteHead),
|
|
55
|
+
git.diffStat(head, remoteHead),
|
|
56
|
+
])
|
|
57
|
+
base.ahead = ahead
|
|
58
|
+
base.behind = behind
|
|
59
|
+
base.diffStat = diffStat
|
|
60
|
+
}
|
|
61
|
+
const ls = await git.lsFiles()
|
|
62
|
+
base.forks = ls.filter(file => FORK_NAME_RE.test(file.split('/').at(-1) ?? '')).sort()
|
|
63
|
+
base.ok = true
|
|
64
|
+
return base
|
|
65
|
+
} catch (error) {
|
|
66
|
+
base.error = error instanceof Error ? error.message : String(error)
|
|
67
|
+
return base
|
|
68
|
+
}
|
|
69
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-session-sync",
|
|
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.1.0",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/PerryLink/dsh-session-sync.git"
|
|
8
|
+
},
|
|
9
|
+
"homepage": "https://github.com/PerryLink/dsh-session-sync#readme",
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/PerryLink/dsh-session-sync/issues"
|
|
12
|
+
},
|
|
13
|
+
"author": "PerryLink",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./index.mjs",
|
|
16
|
+
"types": "./types.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./types.d.ts",
|
|
20
|
+
"default": "./index.mjs"
|
|
21
|
+
},
|
|
22
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"index.mjs",
|
|
27
|
+
"types.d.ts",
|
|
28
|
+
"lib",
|
|
29
|
+
"cordis.patch.yml",
|
|
30
|
+
"README.md",
|
|
31
|
+
"README.zh.md",
|
|
32
|
+
"README.es.md",
|
|
33
|
+
"README.pt.md",
|
|
34
|
+
"README.hi.md",
|
|
35
|
+
"ARCHITECTURE.md",
|
|
36
|
+
"CHANGELOG.md",
|
|
37
|
+
"SECURITY.md",
|
|
38
|
+
"LICENSE",
|
|
39
|
+
"THIRD_PARTY_NOTICES.md"
|
|
40
|
+
],
|
|
41
|
+
"dsh": {
|
|
42
|
+
"bundle": {
|
|
43
|
+
"patch": "./cordis.patch.yml"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"dshWorkshop": {
|
|
47
|
+
"schema": "omdsh-workshop-package/v1",
|
|
48
|
+
"type": "plugin",
|
|
49
|
+
"integration": {
|
|
50
|
+
"protocol": "harness-profile",
|
|
51
|
+
"artifact": "cordis.patch.yml"
|
|
52
|
+
},
|
|
53
|
+
"install": {
|
|
54
|
+
"mode": "transactional",
|
|
55
|
+
"adapter": "profile-bundle",
|
|
56
|
+
"failurePolicy": "generation-rollback",
|
|
57
|
+
"touchesCurrentBeforeActivation": false
|
|
58
|
+
},
|
|
59
|
+
"lifecycle": {
|
|
60
|
+
"activation": "restart-profile",
|
|
61
|
+
"dispose": "supported"
|
|
62
|
+
},
|
|
63
|
+
"permissions": [
|
|
64
|
+
"filesystem:read",
|
|
65
|
+
"filesystem:write",
|
|
66
|
+
"session-log:read",
|
|
67
|
+
"network:git-remote",
|
|
68
|
+
"subprocess:git",
|
|
69
|
+
"credentials:none"
|
|
70
|
+
],
|
|
71
|
+
"compatibility": {
|
|
72
|
+
"dshVersions": [
|
|
73
|
+
"0.1.0-rc.6"
|
|
74
|
+
]
|
|
75
|
+
},
|
|
76
|
+
"capability": {
|
|
77
|
+
"id": "session-sync",
|
|
78
|
+
"kind": "command",
|
|
79
|
+
"invocation": "/sync status",
|
|
80
|
+
"expected": "the /sync command is registered on the commands service: status lists the git mirror state, pull/push move session bytes with append-only keep-both conflict resolution, diff shows local-vs-remote changes read-only"
|
|
81
|
+
},
|
|
82
|
+
"evidence": {
|
|
83
|
+
"install": null,
|
|
84
|
+
"failureIsolation": null,
|
|
85
|
+
"hotReload": null,
|
|
86
|
+
"remove": null
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
"peerDependencies": {
|
|
90
|
+
"@deepseek-ai/cordis": "^4.0.1",
|
|
91
|
+
"@deepseek-ai/dsh-session": "0.1.0-rc.6",
|
|
92
|
+
"@deepseek-ai/dsh-storage-domain": "0.1.0-rc.6",
|
|
93
|
+
"@deepseek-ai/dsh-tools": "0.1.0-rc.6",
|
|
94
|
+
"@deepseek-ai/schemastery": "^3.18.0"
|
|
95
|
+
},
|
|
96
|
+
"dependencies": {
|
|
97
|
+
"typescript": "^5.9.0",
|
|
98
|
+
"zod": "^4.4.3"
|
|
99
|
+
},
|
|
100
|
+
"devDependencies": {
|
|
101
|
+
"@deepseek-ai/cordis": "^4.0.1",
|
|
102
|
+
"@deepseek-ai/dsh-commands": "0.1.0-rc.6",
|
|
103
|
+
"@deepseek-ai/dsh-llm": "0.1.0-rc.6",
|
|
104
|
+
"@deepseek-ai/dsh-session": "0.1.0-rc.6",
|
|
105
|
+
"@deepseek-ai/dsh-storage": "0.1.0-rc.6",
|
|
106
|
+
"@deepseek-ai/dsh-storage-domain": "0.1.0-rc.6",
|
|
107
|
+
"@deepseek-ai/dsh-storage-json": "0.1.0-rc.6",
|
|
108
|
+
"@deepseek-ai/dsh-subprocess": "0.1.0-rc.6",
|
|
109
|
+
"@deepseek-ai/dsh-tools": "0.1.0-rc.6",
|
|
110
|
+
"@deepseek-ai/dsh-user-approval": "0.1.0-rc.6",
|
|
111
|
+
"@deepseek-ai/dsh-user-questions": "0.1.0-rc.6",
|
|
112
|
+
"@deepseek-ai/schemastery": "^3.18.0",
|
|
113
|
+
"@types/node": "^22.19.0"
|
|
114
|
+
},
|
|
115
|
+
"scripts": {
|
|
116
|
+
"typecheck": "tsc -p tsconfig.check.json",
|
|
117
|
+
"typecheck:ci": "tsc -p tsconfig.check.json",
|
|
118
|
+
"test": "node --test \"test/*.test.mjs\"",
|
|
119
|
+
"coverage": "node --test --experimental-test-coverage \"test/*.test.mjs\"",
|
|
120
|
+
"verify:self-contained": "node scripts/verify-self-contained.mjs",
|
|
121
|
+
"verify:artifacts": "node scripts/verify-artifacts.mjs",
|
|
122
|
+
"check:readmes": "node scripts/verify-readmes.mjs"
|
|
123
|
+
},
|
|
124
|
+
"keywords": [
|
|
125
|
+
"dsh",
|
|
126
|
+
"dsh-plugin",
|
|
127
|
+
"deepseek-harness",
|
|
128
|
+
"deepseek",
|
|
129
|
+
"cordis",
|
|
130
|
+
"session-sync",
|
|
131
|
+
"session",
|
|
132
|
+
"git",
|
|
133
|
+
"sync",
|
|
134
|
+
"cross-device"
|
|
135
|
+
],
|
|
136
|
+
"engines": {
|
|
137
|
+
"node": "^22.19.0 || >=24.0.0"
|
|
138
|
+
},
|
|
139
|
+
"packageManager": "pnpm@11.7.0",
|
|
140
|
+
"publishConfig": {
|
|
141
|
+
"access": "public"
|
|
142
|
+
},
|
|
143
|
+
"license": "Apache-2.0"
|
|
144
|
+
}
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// types.d.ts — dsh-session-sync 类型契约(会话事件声明合并 + 配置类型)。
|
|
2
|
+
|
|
3
|
+
declare module '@deepseek-ai/dsh-session' {
|
|
4
|
+
interface SessionEventMap {
|
|
5
|
+
/**
|
|
6
|
+
* 一次推送结果(log-only)。auto 模式(turn/end)与 /sync push、
|
|
7
|
+
* sync_push 工具共用;ok=false 携带结构化错误。
|
|
8
|
+
* 注意:当前宿主构建(KNOWN_SESSION_EVENT_TYPES)尚未收录 sync/*,
|
|
9
|
+
* 运行时经自适应门跳过 append;宿主收录后自动开启(见 README「会话事件」)。
|
|
10
|
+
*/
|
|
11
|
+
'sync/push': {
|
|
12
|
+
sessionId: string
|
|
13
|
+
ok: boolean
|
|
14
|
+
error?: string
|
|
15
|
+
pushed?: boolean
|
|
16
|
+
mirrored?: number
|
|
17
|
+
deleted?: number
|
|
18
|
+
head?: string
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* 一次拉取结果(log-only)。conflict 语义(append-only、keep-both +
|
|
22
|
+
* fork)的关键字段:adopted(仅远端追加,本地未变)、appended(双边
|
|
23
|
+
* 追加)、diverged(非追加分歧);forks 为留存远端版本的 fork 文件。
|
|
24
|
+
*/
|
|
25
|
+
'sync/pull': {
|
|
26
|
+
sessionId: string
|
|
27
|
+
ok: boolean
|
|
28
|
+
error?: string
|
|
29
|
+
pulled?: boolean
|
|
30
|
+
merged?: boolean
|
|
31
|
+
adopted?: number
|
|
32
|
+
appended?: number
|
|
33
|
+
diverged?: number
|
|
34
|
+
forks?: string[]
|
|
35
|
+
head?: string
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 冲突裁决(log-only):fork 文件路径 + 可选会话级 fork 子会话 id。
|
|
39
|
+
* 会话级 fork 仅在目标会话 live 且无开放轮次时发生(轮次中跳过,
|
|
40
|
+
* 文件级两边保留恒成立)。
|
|
41
|
+
*/
|
|
42
|
+
'sync/conflict': {
|
|
43
|
+
sessionId: string
|
|
44
|
+
forkPaths: string[]
|
|
45
|
+
childSessionId?: string
|
|
46
|
+
diverged?: number
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface Config {
|
|
52
|
+
/** 总开关;false 时命令、工具、监听器与自动模式全部卸载。 */
|
|
53
|
+
enabled?: boolean
|
|
54
|
+
/** 同步后端词汇;只有 'git' 实现,加密后端预留(配置即响亮失败)。 */
|
|
55
|
+
backend?: 'git'
|
|
56
|
+
/** 会话存储根;空 = $DSH_HOME/sessions(两者皆缺加载期响亮失败)。 */
|
|
57
|
+
sessionRoot?: string
|
|
58
|
+
/** 同步 git 工作树根;空 = $DSH_HOME/dsh-session-sync/repo。 */
|
|
59
|
+
repoDir?: string
|
|
60
|
+
/** 远端地址(自定义 remote);pull/push 前必须非空,status/diff 不需要。 */
|
|
61
|
+
remote?: string
|
|
62
|
+
/** 远端分支名(默认 main)。 */
|
|
63
|
+
branch?: string
|
|
64
|
+
/** git 可执行路径(默认 'git')。 */
|
|
65
|
+
gitBin?: string
|
|
66
|
+
/** 挂载时自动拉取一次(配置即授权,不再确认)。 */
|
|
67
|
+
autoPullOnStart?: boolean
|
|
68
|
+
/** 每个 turn/end 后自动推送(配置即授权,不再确认)。 */
|
|
69
|
+
autoPushOnTurnEnd?: boolean
|
|
70
|
+
/** 周期自动拉取间隔分钟(0 = 关闭;上限 10080)。 */
|
|
71
|
+
pullIntervalMinutes?: number
|
|
72
|
+
/** 确认通道:auto(userQuestions 优先,其次 approval)· userQuestions · approval。 */
|
|
73
|
+
confirmVia?: 'auto' | 'userQuestions' | 'approval'
|
|
74
|
+
/** git 子进程终止宽限(ms;默认 10000)。 */
|
|
75
|
+
graceMs?: number
|
|
76
|
+
/** 单条 git 命令超时(ms;默认 120000)。 */
|
|
77
|
+
commandTimeoutMs?: number
|
|
78
|
+
/** git 输出单流收集上限(字节;默认 262144)。 */
|
|
79
|
+
maxOutputBytes?: number
|
|
80
|
+
/** 插件提交者名。 */
|
|
81
|
+
commitName?: string
|
|
82
|
+
/** 插件提交者邮箱。 */
|
|
83
|
+
commitEmail?: string
|
|
84
|
+
/** 注册 /sync 命令(默认 true)。 */
|
|
85
|
+
registerCommand?: boolean
|
|
86
|
+
/** tools 服务存在时注册 sync_* 工具(默认 true)。 */
|
|
87
|
+
registerTools?: boolean
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** sync_status 工具规范结果。 */
|
|
91
|
+
export interface StatusValue {
|
|
92
|
+
ok: boolean
|
|
93
|
+
branch: string
|
|
94
|
+
remote: string
|
|
95
|
+
head?: string
|
|
96
|
+
remoteHead?: string
|
|
97
|
+
ahead: number
|
|
98
|
+
behind: number
|
|
99
|
+
dirty: string[]
|
|
100
|
+
forks: string[]
|
|
101
|
+
diffStat: string
|
|
102
|
+
mergeInProgress: boolean
|
|
103
|
+
lastCommits: string[]
|
|
104
|
+
lastPullAt?: number
|
|
105
|
+
lastPushAt?: number
|
|
106
|
+
lastError?: string
|
|
107
|
+
error?: string
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** sync_pull 工具规范结果。 */
|
|
111
|
+
export interface PullValue {
|
|
112
|
+
ok: boolean
|
|
113
|
+
pulled?: boolean
|
|
114
|
+
merged?: boolean
|
|
115
|
+
adopted?: number
|
|
116
|
+
appended?: number
|
|
117
|
+
diverged?: number
|
|
118
|
+
forks?: string[]
|
|
119
|
+
head?: string
|
|
120
|
+
error?: string
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** sync_push 工具规范结果。 */
|
|
124
|
+
export interface PushValue {
|
|
125
|
+
ok: boolean
|
|
126
|
+
pushed?: boolean
|
|
127
|
+
head?: string
|
|
128
|
+
mirrored?: number
|
|
129
|
+
deleted?: number
|
|
130
|
+
remote?: string
|
|
131
|
+
error?: string
|
|
132
|
+
}
|