dsh-xlock 0.2.8 → 0.2.10
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.md +4 -2
- package/client.js +307 -307
- package/cordis.patch.yml +10 -10
- package/core/audit.mjs +226 -226
- package/core/origin.mjs +267 -267
- package/package.json +1 -1
package/core/origin.mjs
CHANGED
|
@@ -1,267 +1,267 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* dsh-xlock — 会话创建归属判定。
|
|
3
|
-
*
|
|
4
|
-
* 为什么不能复用 `.dsh-xlock.json`(粘锁标记):
|
|
5
|
-
* 标记记的是【当前/最后持有者】—— acquireMarker 在 released 态会把
|
|
6
|
-
* machineId/host/pid 整组改写成自己(core/marker.mjs)。一台机器打开
|
|
7
|
-
* 别人创建的会话,标记就变成它,归属从此不可考。
|
|
8
|
-
*
|
|
9
|
-
* 三层证据(强 → 弱):
|
|
10
|
-
* L1 创建记录(权威):`<session-dir>/.dsh-xlock-origin.json`,只在
|
|
11
|
-
* sessionPersistence.create() 那一刻由创建方写入,wx 独占、只写一次,
|
|
12
|
-
* 此后任何机器都不改写。文件名不以 `.jsonl`/`.jsonl.zstd` 结尾,
|
|
13
|
-
* 不会被 dsh-session-persistence-jsonl 的 legacy-layout 检测误判。
|
|
14
|
-
* L2 目录桶推断(历史回填):DSH 的会话桶名 = 创建时 cwd 的 projectKey()。
|
|
15
|
-
* 桶名不可逆,但可正向算:拿 registry 里登记过的 cwd 逐个算 projectKey()
|
|
16
|
-
* 比对桶名,命中即得机器。这样 feature 上线前的历史会话也能判。
|
|
17
|
-
* L3 未知:都没有就报 unknown,绝不猜。
|
|
18
|
-
*
|
|
19
|
-
* 同步面注意:origin 记录在会话目录内(跨机同步),registry 在包目录内
|
|
20
|
-
* (同样跨机共享),两边都可能出现 Syncthing 冲突副本 —— 都要处理。
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
import { closeSync, existsSync, fsyncSync, mkdirSync, openSync, readdirSync, writeSync } from 'node:fs'
|
|
24
|
-
import { basename, dirname, join } from 'node:path'
|
|
25
|
-
import { readJsonSafe } from './util.mjs'
|
|
26
|
-
|
|
27
|
-
/** 创建记录的固定文件名(只写一次,永不改写)。 */
|
|
28
|
-
export const ORIGIN_NAME = '.dsh-xlock-origin.json'
|
|
29
|
-
|
|
30
|
-
/** 创建记录可能出现的 Syncthing 冲突副本。 */
|
|
31
|
-
const ORIGIN_CONFLICT_RE = /^\.dsh-xlock-origin\.json\.sync-conflict-\d{8}-\d{6}-[A-Za-z0-9]+\.\w+$/
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* 复刻 dsh-session-persistence-jsonl 的 projectKey():
|
|
35
|
-
* `/`、`\`、`:` 折叠成一个 `-`,保留 `[A-Za-z0-9._-]`,其余转 `~XXXX`,
|
|
36
|
-
* 结果 `--${readable.slice(0,251)}--`。桶名由它生成,所以正向比对必须逐字一致。
|
|
37
|
-
* @param {string} cwd
|
|
38
|
-
* @returns {string}
|
|
39
|
-
*/
|
|
40
|
-
export function projectKey(cwd) {
|
|
41
|
-
const text = String(cwd)
|
|
42
|
-
if (text.length === 0) throw new Error('cannot encode an empty project path')
|
|
43
|
-
let readable = ''
|
|
44
|
-
let separatorRun = false
|
|
45
|
-
for (let i = 0; i < text.length; i++) {
|
|
46
|
-
const code = text.charCodeAt(i)
|
|
47
|
-
const ch = String.fromCharCode(code)
|
|
48
|
-
if (ch === '/' || ch === '\\' || ch === ':') {
|
|
49
|
-
if (!separatorRun) readable += '-'
|
|
50
|
-
separatorRun = true
|
|
51
|
-
} else if (ch !== '~' && /^[A-Za-z0-9._-]$/.test(ch)) {
|
|
52
|
-
readable += ch
|
|
53
|
-
separatorRun = false
|
|
54
|
-
} else {
|
|
55
|
-
readable += `~${code.toString(16).toUpperCase().padStart(4, '0')}`
|
|
56
|
-
separatorRun = false
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
return `--${(readable.replace(/^-+/, '') || 'root').slice(0, 251)}--`
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* 创建记录文件路径。
|
|
64
|
-
* @param {string} sessionDir
|
|
65
|
-
* @returns {string}
|
|
66
|
-
*/
|
|
67
|
-
export function originPath(sessionDir) {
|
|
68
|
-
return join(sessionDir, ORIGIN_NAME)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* 读取创建记录。
|
|
73
|
-
* @param {string} sessionDir
|
|
74
|
-
* @returns {{state:'missing'}|{state:'corrupt'}|{state:'ok',data:any}}
|
|
75
|
-
*/
|
|
76
|
-
export function readOrigin(sessionDir) {
|
|
77
|
-
const path = originPath(sessionDir)
|
|
78
|
-
if (!existsSync(path)) return { state: 'missing' }
|
|
79
|
-
const data = readJsonSafe(path, undefined)
|
|
80
|
-
if (!data || typeof data !== 'object' || typeof data.machineId !== 'string' || data.machineId.length < 8) {
|
|
81
|
-
return { state: 'corrupt' }
|
|
82
|
-
}
|
|
83
|
-
return { state: 'ok', data }
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* 列出创建记录的冲突副本(有副本说明两台机器都写过,仍然只是"痕迹"不算争抢)。
|
|
88
|
-
* @param {string} sessionDir
|
|
89
|
-
* @returns {string[]}
|
|
90
|
-
*/
|
|
91
|
-
export function listOriginConflicts(sessionDir) {
|
|
92
|
-
let names
|
|
93
|
-
try {
|
|
94
|
-
names = readdirSync(sessionDir)
|
|
95
|
-
} catch {
|
|
96
|
-
return []
|
|
97
|
-
}
|
|
98
|
-
return names.filter((name) => ORIGIN_CONFLICT_RE.test(name))
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* 写创建记录(wx 独占,只写一次)。已存在 / 目录未物化 → 返回 false。
|
|
103
|
-
* @param {string} sessionDir
|
|
104
|
-
* @param {{uuid:string,host:string}} identity
|
|
105
|
-
* @param {string} sessionId
|
|
106
|
-
* @param {string|undefined} cwd
|
|
107
|
-
* @returns {boolean} 是否由本次调用写入
|
|
108
|
-
*/
|
|
109
|
-
export function writeOriginOnce(sessionDir, identity, sessionId, cwd) {
|
|
110
|
-
const path = originPath(sessionDir)
|
|
111
|
-
const payload = JSON.stringify(
|
|
112
|
-
{
|
|
113
|
-
v: 1,
|
|
114
|
-
machineId: identity.uuid,
|
|
115
|
-
host: identity.host,
|
|
116
|
-
sessionId,
|
|
117
|
-
cwd: typeof cwd === 'string' ? cwd : null,
|
|
118
|
-
createdAt: new Date().toISOString(),
|
|
119
|
-
},
|
|
120
|
-
null,
|
|
121
|
-
2,
|
|
122
|
-
)
|
|
123
|
-
let fd
|
|
124
|
-
try {
|
|
125
|
-
fd = openSync(path, 'wx')
|
|
126
|
-
} catch (error) {
|
|
127
|
-
if (error?.code === 'EEXIST') return false
|
|
128
|
-
if (error?.code === 'ENOENT') {
|
|
129
|
-
try {
|
|
130
|
-
mkdirSync(sessionDir, { recursive: true })
|
|
131
|
-
fd = openSync(path, 'wx')
|
|
132
|
-
} catch {
|
|
133
|
-
return false
|
|
134
|
-
}
|
|
135
|
-
} else {
|
|
136
|
-
return false
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
try {
|
|
140
|
-
writeSync(fd, `${payload}\n`)
|
|
141
|
-
fsyncSync(fd)
|
|
142
|
-
return true
|
|
143
|
-
} catch {
|
|
144
|
-
return false
|
|
145
|
-
} finally {
|
|
146
|
-
try {
|
|
147
|
-
closeSync(fd)
|
|
148
|
-
} catch {
|
|
149
|
-
/* 已关闭 */
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* 由桶名反查机器:拿 registry 里每个已知机器的 cwds 逐个算 projectKey 比对。
|
|
156
|
-
* @param {string} bucket 例如 "--D-
|
|
157
|
-
* @param {object} registry loadMachines() 结果
|
|
158
|
-
* @returns {{machineId:string, host:string}|undefined}
|
|
159
|
-
*/
|
|
160
|
-
export function machineByBucket(bucket, registry) {
|
|
161
|
-
const machines = registry?.machines || {}
|
|
162
|
-
for (const [machineId, entry] of Object.entries(machines)) {
|
|
163
|
-
const cwds = Array.isArray(entry?.cwds) ? entry.cwds : []
|
|
164
|
-
for (const cwd of cwds) {
|
|
165
|
-
if (typeof cwd !== 'string' || cwd === '') continue
|
|
166
|
-
let key
|
|
167
|
-
try {
|
|
168
|
-
key = projectKey(cwd)
|
|
169
|
-
} catch {
|
|
170
|
-
continue
|
|
171
|
-
}
|
|
172
|
-
if (key === bucket) return { machineId, host: typeof entry.host === 'string' ? entry.host : machineId }
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
return undefined
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* registry 里查机器显示名。
|
|
180
|
-
* @param {string|undefined} machineId
|
|
181
|
-
* @param {object} registry
|
|
182
|
-
* @returns {{host:string, known:boolean}}
|
|
183
|
-
*/
|
|
184
|
-
export function machineLabel(machineId, registry) {
|
|
185
|
-
if (!machineId) return { host: '(unknown)', known: false }
|
|
186
|
-
const entry = registry?.machines?.[machineId]
|
|
187
|
-
if (entry && typeof entry.host === 'string' && entry.host !== '') return { host: entry.host, known: true }
|
|
188
|
-
return { host: machineId, known: false }
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* 判一个会话目录的创建归属(三层证据)。
|
|
193
|
-
* @param {string} sessionDir 会话目录
|
|
194
|
-
* @param {{identity:{uuid:string,host:string}, registry?:object, bucket?:string}} env
|
|
195
|
-
* bucket = 会话桶目录名;不给则从 sessionDir 的父目录名取
|
|
196
|
-
* @returns {{
|
|
197
|
-
* machineId: string|undefined,
|
|
198
|
-
* host: string,
|
|
199
|
-
* known: boolean,
|
|
200
|
-
* source: 'record'|'bucket'|'unknown',
|
|
201
|
-
* local: boolean,
|
|
202
|
-
* conflicts: number,
|
|
203
|
-
* cwd: string|undefined,
|
|
204
|
-
* }}
|
|
205
|
-
*/
|
|
206
|
-
export function inferOrigin(sessionDir, env = {}) {
|
|
207
|
-
const registry = env.registry || {}
|
|
208
|
-
const identity = env.identity
|
|
209
|
-
const conflicts = listOriginConflicts(sessionDir).length
|
|
210
|
-
|
|
211
|
-
const record = readOrigin(sessionDir)
|
|
212
|
-
if (record.state === 'ok') {
|
|
213
|
-
const machineId = record.data.machineId
|
|
214
|
-
const label = machineLabel(machineId, registry)
|
|
215
|
-
// 记录自带 host 作为兜底(另一台机器还没把 registry 同步过来时也能显示名字)
|
|
216
|
-
const host = label.known ? label.host : typeof record.data.host === 'string' ? record.data.host : machineId
|
|
217
|
-
return {
|
|
218
|
-
machineId,
|
|
219
|
-
host,
|
|
220
|
-
known: label.known || typeof record.data.host === 'string',
|
|
221
|
-
source: 'record',
|
|
222
|
-
local: Boolean(identity && identity.uuid === machineId),
|
|
223
|
-
conflicts,
|
|
224
|
-
cwd: typeof record.data.cwd === 'string' ? record.data.cwd : undefined,
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
const bucket =
|
|
229
|
-
typeof env.bucket === 'string' && env.bucket !== ''
|
|
230
|
-
? env.bucket
|
|
231
|
-
: basename(dirname(sessionDir)) // 会话目录的父目录就是桶名
|
|
232
|
-
if (bucket && bucket !== '.' && bucket !== '') {
|
|
233
|
-
const hit = machineByBucket(bucket, registry)
|
|
234
|
-
if (hit) {
|
|
235
|
-
const label = machineLabel(hit.machineId, registry)
|
|
236
|
-
return {
|
|
237
|
-
machineId: hit.machineId,
|
|
238
|
-
host: label.host,
|
|
239
|
-
known: label.known,
|
|
240
|
-
source: 'bucket',
|
|
241
|
-
local: Boolean(identity && identity.uuid === hit.machineId),
|
|
242
|
-
conflicts,
|
|
243
|
-
cwd: undefined,
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return {
|
|
249
|
-
machineId: undefined,
|
|
250
|
-
host: '(unknown)',
|
|
251
|
-
known: false,
|
|
252
|
-
source: 'unknown',
|
|
253
|
-
local: false,
|
|
254
|
-
conflicts,
|
|
255
|
-
cwd: undefined,
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* 归属来源的中文标签(报告用)。
|
|
261
|
-
* @param {'record'|'bucket'|'unknown'} source
|
|
262
|
-
*/
|
|
263
|
-
export function originSourceLabel(source) {
|
|
264
|
-
if (source === 'record') return '创建记录'
|
|
265
|
-
if (source === 'bucket') return '目录桶推断'
|
|
266
|
-
return '未知'
|
|
267
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* dsh-xlock — 会话创建归属判定。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不能复用 `.dsh-xlock.json`(粘锁标记):
|
|
5
|
+
* 标记记的是【当前/最后持有者】—— acquireMarker 在 released 态会把
|
|
6
|
+
* machineId/host/pid 整组改写成自己(core/marker.mjs)。一台机器打开
|
|
7
|
+
* 别人创建的会话,标记就变成它,归属从此不可考。
|
|
8
|
+
*
|
|
9
|
+
* 三层证据(强 → 弱):
|
|
10
|
+
* L1 创建记录(权威):`<session-dir>/.dsh-xlock-origin.json`,只在
|
|
11
|
+
* sessionPersistence.create() 那一刻由创建方写入,wx 独占、只写一次,
|
|
12
|
+
* 此后任何机器都不改写。文件名不以 `.jsonl`/`.jsonl.zstd` 结尾,
|
|
13
|
+
* 不会被 dsh-session-persistence-jsonl 的 legacy-layout 检测误判。
|
|
14
|
+
* L2 目录桶推断(历史回填):DSH 的会话桶名 = 创建时 cwd 的 projectKey()。
|
|
15
|
+
* 桶名不可逆,但可正向算:拿 registry 里登记过的 cwd 逐个算 projectKey()
|
|
16
|
+
* 比对桶名,命中即得机器。这样 feature 上线前的历史会话也能判。
|
|
17
|
+
* L3 未知:都没有就报 unknown,绝不猜。
|
|
18
|
+
*
|
|
19
|
+
* 同步面注意:origin 记录在会话目录内(跨机同步),registry 在包目录内
|
|
20
|
+
* (同样跨机共享),两边都可能出现 Syncthing 冲突副本 —— 都要处理。
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { closeSync, existsSync, fsyncSync, mkdirSync, openSync, readdirSync, writeSync } from 'node:fs'
|
|
24
|
+
import { basename, dirname, join } from 'node:path'
|
|
25
|
+
import { readJsonSafe } from './util.mjs'
|
|
26
|
+
|
|
27
|
+
/** 创建记录的固定文件名(只写一次,永不改写)。 */
|
|
28
|
+
export const ORIGIN_NAME = '.dsh-xlock-origin.json'
|
|
29
|
+
|
|
30
|
+
/** 创建记录可能出现的 Syncthing 冲突副本。 */
|
|
31
|
+
const ORIGIN_CONFLICT_RE = /^\.dsh-xlock-origin\.json\.sync-conflict-\d{8}-\d{6}-[A-Za-z0-9]+\.\w+$/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 复刻 dsh-session-persistence-jsonl 的 projectKey():
|
|
35
|
+
* `/`、`\`、`:` 折叠成一个 `-`,保留 `[A-Za-z0-9._-]`,其余转 `~XXXX`,
|
|
36
|
+
* 结果 `--${readable.slice(0,251)}--`。桶名由它生成,所以正向比对必须逐字一致。
|
|
37
|
+
* @param {string} cwd
|
|
38
|
+
* @returns {string}
|
|
39
|
+
*/
|
|
40
|
+
export function projectKey(cwd) {
|
|
41
|
+
const text = String(cwd)
|
|
42
|
+
if (text.length === 0) throw new Error('cannot encode an empty project path')
|
|
43
|
+
let readable = ''
|
|
44
|
+
let separatorRun = false
|
|
45
|
+
for (let i = 0; i < text.length; i++) {
|
|
46
|
+
const code = text.charCodeAt(i)
|
|
47
|
+
const ch = String.fromCharCode(code)
|
|
48
|
+
if (ch === '/' || ch === '\\' || ch === ':') {
|
|
49
|
+
if (!separatorRun) readable += '-'
|
|
50
|
+
separatorRun = true
|
|
51
|
+
} else if (ch !== '~' && /^[A-Za-z0-9._-]$/.test(ch)) {
|
|
52
|
+
readable += ch
|
|
53
|
+
separatorRun = false
|
|
54
|
+
} else {
|
|
55
|
+
readable += `~${code.toString(16).toUpperCase().padStart(4, '0')}`
|
|
56
|
+
separatorRun = false
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return `--${(readable.replace(/^-+/, '') || 'root').slice(0, 251)}--`
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* 创建记录文件路径。
|
|
64
|
+
* @param {string} sessionDir
|
|
65
|
+
* @returns {string}
|
|
66
|
+
*/
|
|
67
|
+
export function originPath(sessionDir) {
|
|
68
|
+
return join(sessionDir, ORIGIN_NAME)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 读取创建记录。
|
|
73
|
+
* @param {string} sessionDir
|
|
74
|
+
* @returns {{state:'missing'}|{state:'corrupt'}|{state:'ok',data:any}}
|
|
75
|
+
*/
|
|
76
|
+
export function readOrigin(sessionDir) {
|
|
77
|
+
const path = originPath(sessionDir)
|
|
78
|
+
if (!existsSync(path)) return { state: 'missing' }
|
|
79
|
+
const data = readJsonSafe(path, undefined)
|
|
80
|
+
if (!data || typeof data !== 'object' || typeof data.machineId !== 'string' || data.machineId.length < 8) {
|
|
81
|
+
return { state: 'corrupt' }
|
|
82
|
+
}
|
|
83
|
+
return { state: 'ok', data }
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 列出创建记录的冲突副本(有副本说明两台机器都写过,仍然只是"痕迹"不算争抢)。
|
|
88
|
+
* @param {string} sessionDir
|
|
89
|
+
* @returns {string[]}
|
|
90
|
+
*/
|
|
91
|
+
export function listOriginConflicts(sessionDir) {
|
|
92
|
+
let names
|
|
93
|
+
try {
|
|
94
|
+
names = readdirSync(sessionDir)
|
|
95
|
+
} catch {
|
|
96
|
+
return []
|
|
97
|
+
}
|
|
98
|
+
return names.filter((name) => ORIGIN_CONFLICT_RE.test(name))
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 写创建记录(wx 独占,只写一次)。已存在 / 目录未物化 → 返回 false。
|
|
103
|
+
* @param {string} sessionDir
|
|
104
|
+
* @param {{uuid:string,host:string}} identity
|
|
105
|
+
* @param {string} sessionId
|
|
106
|
+
* @param {string|undefined} cwd
|
|
107
|
+
* @returns {boolean} 是否由本次调用写入
|
|
108
|
+
*/
|
|
109
|
+
export function writeOriginOnce(sessionDir, identity, sessionId, cwd) {
|
|
110
|
+
const path = originPath(sessionDir)
|
|
111
|
+
const payload = JSON.stringify(
|
|
112
|
+
{
|
|
113
|
+
v: 1,
|
|
114
|
+
machineId: identity.uuid,
|
|
115
|
+
host: identity.host,
|
|
116
|
+
sessionId,
|
|
117
|
+
cwd: typeof cwd === 'string' ? cwd : null,
|
|
118
|
+
createdAt: new Date().toISOString(),
|
|
119
|
+
},
|
|
120
|
+
null,
|
|
121
|
+
2,
|
|
122
|
+
)
|
|
123
|
+
let fd
|
|
124
|
+
try {
|
|
125
|
+
fd = openSync(path, 'wx')
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (error?.code === 'EEXIST') return false
|
|
128
|
+
if (error?.code === 'ENOENT') {
|
|
129
|
+
try {
|
|
130
|
+
mkdirSync(sessionDir, { recursive: true })
|
|
131
|
+
fd = openSync(path, 'wx')
|
|
132
|
+
} catch {
|
|
133
|
+
return false
|
|
134
|
+
}
|
|
135
|
+
} else {
|
|
136
|
+
return false
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
try {
|
|
140
|
+
writeSync(fd, `${payload}\n`)
|
|
141
|
+
fsyncSync(fd)
|
|
142
|
+
return true
|
|
143
|
+
} catch {
|
|
144
|
+
return false
|
|
145
|
+
} finally {
|
|
146
|
+
try {
|
|
147
|
+
closeSync(fd)
|
|
148
|
+
} catch {
|
|
149
|
+
/* 已关闭 */
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 由桶名反查机器:拿 registry 里每个已知机器的 cwds 逐个算 projectKey 比对。
|
|
156
|
+
* @param {string} bucket 例如 "--D-work-demo--"
|
|
157
|
+
* @param {object} registry loadMachines() 结果
|
|
158
|
+
* @returns {{machineId:string, host:string}|undefined}
|
|
159
|
+
*/
|
|
160
|
+
export function machineByBucket(bucket, registry) {
|
|
161
|
+
const machines = registry?.machines || {}
|
|
162
|
+
for (const [machineId, entry] of Object.entries(machines)) {
|
|
163
|
+
const cwds = Array.isArray(entry?.cwds) ? entry.cwds : []
|
|
164
|
+
for (const cwd of cwds) {
|
|
165
|
+
if (typeof cwd !== 'string' || cwd === '') continue
|
|
166
|
+
let key
|
|
167
|
+
try {
|
|
168
|
+
key = projectKey(cwd)
|
|
169
|
+
} catch {
|
|
170
|
+
continue
|
|
171
|
+
}
|
|
172
|
+
if (key === bucket) return { machineId, host: typeof entry.host === 'string' ? entry.host : machineId }
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return undefined
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* registry 里查机器显示名。
|
|
180
|
+
* @param {string|undefined} machineId
|
|
181
|
+
* @param {object} registry
|
|
182
|
+
* @returns {{host:string, known:boolean}}
|
|
183
|
+
*/
|
|
184
|
+
export function machineLabel(machineId, registry) {
|
|
185
|
+
if (!machineId) return { host: '(unknown)', known: false }
|
|
186
|
+
const entry = registry?.machines?.[machineId]
|
|
187
|
+
if (entry && typeof entry.host === 'string' && entry.host !== '') return { host: entry.host, known: true }
|
|
188
|
+
return { host: machineId, known: false }
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 判一个会话目录的创建归属(三层证据)。
|
|
193
|
+
* @param {string} sessionDir 会话目录
|
|
194
|
+
* @param {{identity:{uuid:string,host:string}, registry?:object, bucket?:string}} env
|
|
195
|
+
* bucket = 会话桶目录名;不给则从 sessionDir 的父目录名取
|
|
196
|
+
* @returns {{
|
|
197
|
+
* machineId: string|undefined,
|
|
198
|
+
* host: string,
|
|
199
|
+
* known: boolean,
|
|
200
|
+
* source: 'record'|'bucket'|'unknown',
|
|
201
|
+
* local: boolean,
|
|
202
|
+
* conflicts: number,
|
|
203
|
+
* cwd: string|undefined,
|
|
204
|
+
* }}
|
|
205
|
+
*/
|
|
206
|
+
export function inferOrigin(sessionDir, env = {}) {
|
|
207
|
+
const registry = env.registry || {}
|
|
208
|
+
const identity = env.identity
|
|
209
|
+
const conflicts = listOriginConflicts(sessionDir).length
|
|
210
|
+
|
|
211
|
+
const record = readOrigin(sessionDir)
|
|
212
|
+
if (record.state === 'ok') {
|
|
213
|
+
const machineId = record.data.machineId
|
|
214
|
+
const label = machineLabel(machineId, registry)
|
|
215
|
+
// 记录自带 host 作为兜底(另一台机器还没把 registry 同步过来时也能显示名字)
|
|
216
|
+
const host = label.known ? label.host : typeof record.data.host === 'string' ? record.data.host : machineId
|
|
217
|
+
return {
|
|
218
|
+
machineId,
|
|
219
|
+
host,
|
|
220
|
+
known: label.known || typeof record.data.host === 'string',
|
|
221
|
+
source: 'record',
|
|
222
|
+
local: Boolean(identity && identity.uuid === machineId),
|
|
223
|
+
conflicts,
|
|
224
|
+
cwd: typeof record.data.cwd === 'string' ? record.data.cwd : undefined,
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
const bucket =
|
|
229
|
+
typeof env.bucket === 'string' && env.bucket !== ''
|
|
230
|
+
? env.bucket
|
|
231
|
+
: basename(dirname(sessionDir)) // 会话目录的父目录就是桶名
|
|
232
|
+
if (bucket && bucket !== '.' && bucket !== '') {
|
|
233
|
+
const hit = machineByBucket(bucket, registry)
|
|
234
|
+
if (hit) {
|
|
235
|
+
const label = machineLabel(hit.machineId, registry)
|
|
236
|
+
return {
|
|
237
|
+
machineId: hit.machineId,
|
|
238
|
+
host: label.host,
|
|
239
|
+
known: label.known,
|
|
240
|
+
source: 'bucket',
|
|
241
|
+
local: Boolean(identity && identity.uuid === hit.machineId),
|
|
242
|
+
conflicts,
|
|
243
|
+
cwd: undefined,
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
machineId: undefined,
|
|
250
|
+
host: '(unknown)',
|
|
251
|
+
known: false,
|
|
252
|
+
source: 'unknown',
|
|
253
|
+
local: false,
|
|
254
|
+
conflicts,
|
|
255
|
+
cwd: undefined,
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* 归属来源的中文标签(报告用)。
|
|
261
|
+
* @param {'record'|'bucket'|'unknown'} source
|
|
262
|
+
*/
|
|
263
|
+
export function originSourceLabel(source) {
|
|
264
|
+
if (source === 'record') return '创建记录'
|
|
265
|
+
if (source === 'bucket') return '目录桶推断'
|
|
266
|
+
return '未知'
|
|
267
|
+
}
|
package/package.json
CHANGED