dsh-handoff-baton 0.4.0 → 0.5.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/CHANGELOG.md +28 -1
- package/README.md +165 -151
- package/bin/baton.mjs +335 -125
- package/cordis.patch.yml +2 -1
- package/docs/AGENTS-snippet.md +16 -0
- package/docs/DETAIL.en.md +8 -4
- package/docs/DETAIL.md +8 -4
- package/docs/README.zh-CN.md +165 -151
- package/index.js +624 -508
- package/lib/catalog.js +292 -0
- package/lib/command.js +14 -8
- package/lib/paths.js +61 -0
- package/lib/protocol.js +208 -58
- package/package.json +4 -4
- package/skills/baton/SKILL.md +22 -12
- package/skills/baton/references/PROTOCOL.md +18 -7
package/bin/baton.mjs
CHANGED
|
@@ -2,23 +2,30 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* 交接棒 CLI · 零依赖
|
|
4
4
|
*
|
|
5
|
+
* 全机只有一份索引(catalog)。查找顺序:--catalog > BATON_ROOT > location.json > 默认 $DSH_HOME/baton。
|
|
6
|
+
* 文档可以放在任何地方,索引行负责记住它在哪。
|
|
7
|
+
*
|
|
5
8
|
* 用法:
|
|
6
|
-
* baton
|
|
7
|
-
* baton
|
|
8
|
-
* baton
|
|
9
|
-
* baton
|
|
10
|
-
* baton
|
|
11
|
-
* baton
|
|
12
|
-
* baton
|
|
13
|
-
* baton
|
|
9
|
+
* baton init [--dir <位置>]
|
|
10
|
+
* baton doctor [--json]
|
|
11
|
+
* baton snippet
|
|
12
|
+
* baton write --json payload.json [--dir <文档目录>] [--catalog <索引目录>] [--dry-run]
|
|
13
|
+
* baton new --title "标题"
|
|
14
|
+
* baton adopt <文件...>
|
|
15
|
+
* baton scan [--roots a;b] [--adopt]
|
|
16
|
+
* baton resolve [--json]
|
|
17
|
+
* baton list [--open]
|
|
18
|
+
* baton status [--json]
|
|
19
|
+
* baton touch <文件|路径> [--status 进行中|已接手|已关闭]
|
|
20
|
+
* baton drift <文件> [--workspace <目录>]
|
|
21
|
+
* baton verify <文件> / update <文件> / code <文件> / archive <文件> / sync / prune
|
|
14
22
|
*/
|
|
15
|
-
import {
|
|
23
|
+
import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs'
|
|
16
24
|
import { mkdir, writeFile } from 'node:fs/promises'
|
|
17
|
-
import {
|
|
25
|
+
import { basename, dirname, join, resolve } from 'node:path'
|
|
18
26
|
import { execFileSync } from 'node:child_process'
|
|
19
27
|
|
|
20
28
|
import {
|
|
21
|
-
DEFAULT_DIR,
|
|
22
29
|
DEFAULT_WINDOW,
|
|
23
30
|
HANDOFF_RATIO,
|
|
24
31
|
STATUS_CLOSED,
|
|
@@ -27,6 +34,7 @@ import {
|
|
|
27
34
|
STATUSES,
|
|
28
35
|
WARN_RATIO,
|
|
29
36
|
appendIndex,
|
|
37
|
+
entryLabel,
|
|
30
38
|
localIso,
|
|
31
39
|
makeCodes,
|
|
32
40
|
makeId,
|
|
@@ -43,6 +51,17 @@ import {
|
|
|
43
51
|
} from '../lib/protocol.js'
|
|
44
52
|
import { inspectIndex } from '../lib/maintenance.js'
|
|
45
53
|
import { DEFAULT_STALE_HOURS, ageHours, parseKeyFiles, summarizeDrift } from '../lib/drift.js'
|
|
54
|
+
import { resolveRowPath, toIndexPath } from '../lib/paths.js'
|
|
55
|
+
import {
|
|
56
|
+
agentsCandidates,
|
|
57
|
+
agentsSnippet,
|
|
58
|
+
bootstrapDir,
|
|
59
|
+
diagnoseCatalog,
|
|
60
|
+
initCatalog,
|
|
61
|
+
registeredPaths,
|
|
62
|
+
resolveCatalog,
|
|
63
|
+
scanHandoffs,
|
|
64
|
+
} from '../lib/catalog.js'
|
|
46
65
|
|
|
47
66
|
/** --json 时失败也吐结构化结果,方便脚本消费。 */
|
|
48
67
|
let jsonMode = false
|
|
@@ -93,6 +112,27 @@ function readPayload(args) {
|
|
|
93
112
|
}
|
|
94
113
|
}
|
|
95
114
|
|
|
115
|
+
/** 解析目录;未初始化时按情况给提示,绝不回退到当前目录。 */
|
|
116
|
+
function catalog(args, options = {}) {
|
|
117
|
+
const info = resolveCatalog({ catalog: typeof args.catalog === 'string' ? args.catalog : undefined })
|
|
118
|
+
if (!info.initialized && options.allowUninit !== true) {
|
|
119
|
+
fail(
|
|
120
|
+
`还没有交接目录(找不到 INDEX.md)。先运行 baton init 选择位置;默认位置是 ${join(bootstrapDir(), 'INDEX.md')}。`,
|
|
121
|
+
1,
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
return info
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function readEntries(info) {
|
|
128
|
+
if (!info.initialized) return []
|
|
129
|
+
return parseIndex(readFileSync(info.indexPath, 'utf8'))
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function docTitle(text, fallback) {
|
|
133
|
+
return (text.match(/^# 交接棒 · (.+)$/mu) ?? [])[1]?.trim() ?? fallback
|
|
134
|
+
}
|
|
135
|
+
|
|
96
136
|
async function cmdCheck(args) {
|
|
97
137
|
const tokens = Number(args.tokens)
|
|
98
138
|
if (!Number.isFinite(tokens) || tokens <= 0) fail('check 需提供 --tokens <正数>')
|
|
@@ -126,20 +166,73 @@ async function cmdCheck(args) {
|
|
|
126
166
|
}
|
|
127
167
|
|
|
128
168
|
async function cmdInit(args) {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
169
|
+
let result
|
|
170
|
+
try {
|
|
171
|
+
result = await initCatalog({
|
|
172
|
+
dir: typeof args.dir === 'string' ? args.dir : undefined,
|
|
173
|
+
force: args.force === true,
|
|
174
|
+
})
|
|
175
|
+
} catch (error) {
|
|
176
|
+
fail(error instanceof Error ? error.message : String(error), 1)
|
|
177
|
+
return
|
|
178
|
+
}
|
|
179
|
+
const out = {
|
|
180
|
+
root: result.root,
|
|
181
|
+
indexPath: result.indexPath,
|
|
182
|
+
locationPath: result.locationPath,
|
|
183
|
+
createdIndex: result.createdIndex,
|
|
184
|
+
snippet: agentsSnippet(result.indexPath),
|
|
185
|
+
}
|
|
186
|
+
if (args.json) {
|
|
187
|
+
process.stdout.write(`${JSON.stringify(out, null, 2)}\n`)
|
|
188
|
+
return
|
|
189
|
+
}
|
|
190
|
+
process.stdout.write(`交接目录:${result.root}\n`)
|
|
191
|
+
process.stdout.write(`索引:${result.indexPath}${result.createdIndex ? '(新建)' : '(已存在,未改动)'}\n`)
|
|
192
|
+
if (result.locationPath === null) {
|
|
193
|
+
process.stdout.write('位置记录:未写(BATON_ROOT 生效中,只建目录;要把它固定成全机索引就加 --force)\n')
|
|
194
|
+
} else {
|
|
195
|
+
process.stdout.write(`位置记录:${result.locationPath}\n`)
|
|
196
|
+
}
|
|
197
|
+
process.stdout.write('\n下一步:把下面这段贴进 AGENTS.md(用户级),别的会话才找得到这里。\n\n')
|
|
198
|
+
process.stdout.write(`${out.snippet}\n`)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
async function cmdSnippet(args) {
|
|
202
|
+
const info = resolveCatalog({ catalog: typeof args.catalog === 'string' ? args.catalog : undefined })
|
|
203
|
+
const indexPath = info.indexPath ?? join(bootstrapDir(), 'INDEX.md')
|
|
204
|
+
const text = agentsSnippet(indexPath)
|
|
205
|
+
if (args.json) process.stdout.write(`${JSON.stringify({ indexPath, initialized: info.initialized, snippet: text }, null, 2)}\n`)
|
|
206
|
+
else process.stdout.write(`${text}\n`)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async function cmdDoctor(args) {
|
|
210
|
+
const info = resolveCatalog({ catalog: typeof args.catalog === 'string' ? args.catalog : undefined })
|
|
211
|
+
const extraAgents = typeof args.agents === 'string' ? args.agents.split(';').filter(Boolean) : []
|
|
212
|
+
const report = diagnoseCatalog(info, {
|
|
213
|
+
staleDays: Number(args['stale-days']) > 0 ? Number(args['stale-days']) : undefined,
|
|
214
|
+
agents: extraAgents,
|
|
215
|
+
})
|
|
216
|
+
if (args.json) {
|
|
217
|
+
process.stdout.write(`${JSON.stringify({ ...report, snippet: report.ok ? undefined : agentsSnippet(info.indexPath ?? join(bootstrapDir(), 'INDEX.md')) }, null, 2)}\n`)
|
|
218
|
+
process.exitCode = report.ok ? 0 : 1
|
|
219
|
+
return
|
|
220
|
+
}
|
|
221
|
+
for (const check of report.checks) {
|
|
222
|
+
const mark = check.level === 'ok' ? '✓' : check.level === 'warn' ? '!' : '✗'
|
|
223
|
+
process.stdout.write(`${mark} ${check.message}\n`)
|
|
224
|
+
}
|
|
225
|
+
if (!report.ok) {
|
|
226
|
+
process.stdout.write(`\n按上面 ✗ 处理;AGENTS.md 片段可用 baton snippet 取。\n`)
|
|
227
|
+
process.exitCode = 1
|
|
135
228
|
}
|
|
136
|
-
process.stdout.write(`交接目录就绪:${dir}\n`)
|
|
137
229
|
}
|
|
138
230
|
|
|
139
231
|
async function cmdWrite(args) {
|
|
140
232
|
const payload = readPayload(args)
|
|
141
|
-
const
|
|
233
|
+
const info = catalog(args)
|
|
142
234
|
const workspace = resolve(typeof args.workspace === 'string' ? args.workspace : process.cwd())
|
|
235
|
+
const docDir = resolve(typeof args.dir === 'string' ? args.dir : info.root)
|
|
143
236
|
const document = {
|
|
144
237
|
...payload,
|
|
145
238
|
workspace: payload.workspace ?? workspace,
|
|
@@ -160,42 +253,125 @@ async function cmdWrite(args) {
|
|
|
160
253
|
process.exitCode = check.ok ? 0 : 1
|
|
161
254
|
return
|
|
162
255
|
}
|
|
163
|
-
const result = await writeHandoff(document, {
|
|
256
|
+
const result = await writeHandoff(document, {
|
|
257
|
+
dir: docDir,
|
|
258
|
+
catalog: info.root,
|
|
259
|
+
archive: args['no-archive'] !== true,
|
|
260
|
+
workspace,
|
|
261
|
+
cwd: workspace,
|
|
262
|
+
})
|
|
164
263
|
if (args.quiet === true) {
|
|
165
264
|
process.stdout.write(`${result.short}\n`)
|
|
166
265
|
} else {
|
|
167
266
|
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`)
|
|
168
267
|
}
|
|
169
|
-
|
|
170
|
-
const indexPath = join(dir, 'INDEX.md')
|
|
171
|
-
if (existsSync(indexPath)) {
|
|
172
|
-
const report = inspectIndex(parseIndex(readFileSync(indexPath, 'utf8')))
|
|
173
|
-
for (const hint of report.hints) process.stderr.write(`注意:${hint}\n`)
|
|
174
|
-
}
|
|
175
|
-
} catch {
|
|
176
|
-
// 体检失败不影响已经写好的交接
|
|
177
|
-
}
|
|
268
|
+
for (const hint of inspectIndex(readEntries(info), {}).hints) process.stderr.write(`注意:${hint}\n`)
|
|
178
269
|
}
|
|
179
270
|
|
|
180
271
|
async function cmdNew(args) {
|
|
272
|
+
const info = catalog(args)
|
|
273
|
+
const dir = resolve(typeof args.dir === 'string' ? args.dir : info.root)
|
|
274
|
+
await mkdir(dir, { recursive: true })
|
|
181
275
|
const payload = {
|
|
182
276
|
title: typeof args.title === 'string' ? args.title : '未命名交接',
|
|
277
|
+
name: typeof args.name === 'string' ? args.name : '',
|
|
183
278
|
goal: '(待补)',
|
|
184
279
|
status: '(待补)',
|
|
185
280
|
files: '(待补)',
|
|
186
|
-
blockers: '(无)',
|
|
187
281
|
next: '(待补)',
|
|
282
|
+
blockers: '(无)',
|
|
188
283
|
constraints: '(无)',
|
|
189
284
|
evidence: '(无)',
|
|
190
285
|
}
|
|
191
|
-
const dir = resolve(typeof args.dir === 'string' ? args.dir : DEFAULT_DIR)
|
|
192
|
-
await mkdir(dir, { recursive: true })
|
|
193
286
|
const stamp = new Date()
|
|
194
287
|
const file = join(dir, `${makeId(stamp, payload.title)}.md`)
|
|
195
288
|
const text = renderDocument({ ...payload, workspace: process.cwd(), git: gitHead(process.cwd()) })
|
|
196
|
-
const { writeFile } = await import('node:fs/promises')
|
|
197
289
|
await writeFile(file, text, 'utf8')
|
|
198
|
-
|
|
290
|
+
const out = { file, indexPath: null, note: '骨架不登记索引;补齐七节后运行 baton adopt <文件> 登记' }
|
|
291
|
+
process.stdout.write(`${JSON.stringify(out, null, 2)}\n`)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
async function cmdAdopt(args) {
|
|
295
|
+
const info = catalog(args)
|
|
296
|
+
const files = args._.map((file) => resolve(file))
|
|
297
|
+
if (files.length === 0) fail('adopt 需要至少一个文件路径')
|
|
298
|
+
const status = typeof args.status === 'string' ? args.status : STATUS_OPEN
|
|
299
|
+
if (!STATUSES.includes(status)) fail(`--status 只能是 ${STATUSES.join(' / ')}`)
|
|
300
|
+
const added = []
|
|
301
|
+
const rejected = []
|
|
302
|
+
for (const file of files) {
|
|
303
|
+
if (!existsSync(file)) {
|
|
304
|
+
rejected.push({ file, reason: '文件不存在' })
|
|
305
|
+
continue
|
|
306
|
+
}
|
|
307
|
+
const text = readFileSync(file, 'utf8').replace(/^\uFEFF/u, '')
|
|
308
|
+
if (parseHeader(text) === null) {
|
|
309
|
+
rejected.push({ file, reason: '没有 baton:v1 头,不是交接文档' })
|
|
310
|
+
continue
|
|
311
|
+
}
|
|
312
|
+
const header = parseHeader(text) ?? {}
|
|
313
|
+
await appendIndex(info.root, file, {
|
|
314
|
+
title: docTitle(text, basename(file, '.md')),
|
|
315
|
+
status,
|
|
316
|
+
workspace: typeof args.workspace === 'string' && args.workspace.trim() !== '' ? args.workspace.trim() : header.workspace,
|
|
317
|
+
name: typeof args.name === 'string' ? args.name : header.name,
|
|
318
|
+
})
|
|
319
|
+
added.push({ file, path: toIndexPath(info.root, file), status })
|
|
320
|
+
}
|
|
321
|
+
if (args.json) process.stdout.write(`${JSON.stringify({ indexPath: info.indexPath, added, rejected }, null, 2)}\n`)
|
|
322
|
+
else {
|
|
323
|
+
for (const item of added) process.stdout.write(`已登记:${item.file}(${item.status})\n`)
|
|
324
|
+
for (const item of rejected) process.stdout.write(`跳过:${item.file} — ${item.reason}\n`)
|
|
325
|
+
if (added.length > 0) process.stdout.write(`索引:${info.indexPath}\n`)
|
|
326
|
+
}
|
|
327
|
+
process.exitCode = rejected.length > 0 && added.length === 0 ? 1 : 0
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
async function cmdScan(args) {
|
|
331
|
+
const info = resolveCatalog({ catalog: typeof args.catalog === 'string' ? args.catalog : undefined })
|
|
332
|
+
const extraRoots = typeof args.roots === 'string' ? args.roots.split(';').filter((item) => item.trim() !== '') : []
|
|
333
|
+
const roots = [process.cwd(), ...extraRoots]
|
|
334
|
+
if (info.initialized) {
|
|
335
|
+
for (const entry of readEntries(info)) {
|
|
336
|
+
const abs = resolveRowPath(info.root, entry.path)
|
|
337
|
+
if (abs !== null && existsSync(abs)) roots.push(dirname(abs))
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
const found = scanHandoffs(roots, { maxDepth: args.depth, limit: args.limit })
|
|
341
|
+
const registered = info.initialized ? registeredPaths(info) : new Set()
|
|
342
|
+
const unregistered = found.filter((doc) => !registered.has(doc.path.toLowerCase().replace(/\\/gu, '/')))
|
|
343
|
+
if (args.adopt === true) {
|
|
344
|
+
if (!info.initialized) fail('还没有交接目录,先 baton init')
|
|
345
|
+
for (const doc of unregistered) {
|
|
346
|
+
await appendIndex(info.root, doc.path, {
|
|
347
|
+
title: doc.title,
|
|
348
|
+
status: STATUS_OPEN,
|
|
349
|
+
workspace: doc.workspace,
|
|
350
|
+
})
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
const payload = {
|
|
354
|
+
root: info.root,
|
|
355
|
+
scanned: roots,
|
|
356
|
+
found: found.length,
|
|
357
|
+
unregistered: unregistered.map((doc) => ({ path: doc.path, title: doc.title, created: doc.created, workspace: doc.workspace })),
|
|
358
|
+
adopted: args.adopt === true ? unregistered.length : 0,
|
|
359
|
+
}
|
|
360
|
+
if (args.json) {
|
|
361
|
+
process.stdout.write(`${JSON.stringify(payload, null, 2)}\n`)
|
|
362
|
+
return
|
|
363
|
+
}
|
|
364
|
+
process.stdout.write(`扫描 ${roots.length} 个根目录,找到 ${found.length} 份交接文档。\n`)
|
|
365
|
+
if (unregistered.length === 0) {
|
|
366
|
+
process.stdout.write('都已在索引里,没有漏登记的。\n')
|
|
367
|
+
return
|
|
368
|
+
}
|
|
369
|
+
process.stdout.write(`未登记 ${unregistered.length} 份:\n`)
|
|
370
|
+
for (const doc of unregistered) {
|
|
371
|
+
const name = doc.name === '' ? '' : `#${doc.name} — `
|
|
372
|
+
process.stdout.write(` ${doc.path} — ${name}${doc.title}\n`)
|
|
373
|
+
}
|
|
374
|
+
process.stdout.write(args.adopt === true ? '已全部登记。\n' : '确认后加 --adopt 登记,或用 baton adopt <文件> 逐个登记。\n')
|
|
199
375
|
}
|
|
200
376
|
|
|
201
377
|
async function cmdVerify(args) {
|
|
@@ -225,12 +401,13 @@ async function cmdUpdate(args) {
|
|
|
225
401
|
if (!file) fail('update 需提供文件路径')
|
|
226
402
|
const target = resolve(file)
|
|
227
403
|
if (!existsSync(target)) fail(`找不到文件:${target}`, 1)
|
|
404
|
+
const info = catalog(args)
|
|
228
405
|
const payload = readPayload(args)
|
|
229
406
|
const text = readFileSync(target, 'utf8').replace(/^\uFEFF/u, '')
|
|
230
407
|
const header = parseHeader(text) ?? {}
|
|
231
408
|
const title = typeof payload.title === 'string' && payload.title.trim() !== ''
|
|
232
409
|
? payload.title
|
|
233
|
-
: (text
|
|
410
|
+
: docTitle(text, basename(target))
|
|
234
411
|
const next = renderDocument({
|
|
235
412
|
...payload,
|
|
236
413
|
title,
|
|
@@ -245,24 +422,27 @@ async function cmdUpdate(args) {
|
|
|
245
422
|
const check = verifyDocument(next)
|
|
246
423
|
if (!check.ok) fail(`更新后未通过校验:${check.problems.join('; ')}`, 1)
|
|
247
424
|
await writeFile(target, next, 'utf8')
|
|
248
|
-
const
|
|
249
|
-
const
|
|
250
|
-
|
|
425
|
+
const touched = await touchIndexEntry(info.root, target, { title }).catch(() => ({ updated: false }))
|
|
426
|
+
const out = {
|
|
427
|
+
file: target,
|
|
428
|
+
title,
|
|
429
|
+
indexPath: touched.updated ? info.indexPath : undefined,
|
|
430
|
+
registered: touched.updated === true,
|
|
431
|
+
warnings: check.warnings,
|
|
432
|
+
}
|
|
251
433
|
process.stdout.write(`${JSON.stringify(out, null, 2)}\n`)
|
|
252
434
|
}
|
|
253
435
|
|
|
254
436
|
async function cmdSync(args) {
|
|
255
|
-
const
|
|
256
|
-
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
const title = ((text.match(/^# 交接棒 · (.+)$/mu) ?? [])[1] ?? basename(name, '.md')).trim()
|
|
261
|
-
return { path: name, title }
|
|
437
|
+
const info = catalog(args)
|
|
438
|
+
const names = readdirSync(info.root).filter((name) => /\.md$/iu.test(name) && name !== 'INDEX.md')
|
|
439
|
+
const docs = names.map((name) => {
|
|
440
|
+
const text = readFileSync(join(info.root, name), 'utf8').replace(/^\uFEFF/u, '')
|
|
441
|
+
return { path: name, title: docTitle(text, basename(name, '.md')) }
|
|
262
442
|
})
|
|
263
|
-
const report = await syncIndex(
|
|
443
|
+
const report = await syncIndex(info.root, docs, { apply: args.fix === true })
|
|
264
444
|
if (args.json) {
|
|
265
|
-
process.stdout.write(`${JSON.stringify({
|
|
445
|
+
process.stdout.write(`${JSON.stringify({ root: info.root, ...report }, null, 2)}\n`)
|
|
266
446
|
process.exitCode = report.changed === 0 ? 0 : report.applied ? 0 : 2
|
|
267
447
|
return
|
|
268
448
|
}
|
|
@@ -284,7 +464,7 @@ async function cmdSync(args) {
|
|
|
284
464
|
|
|
285
465
|
async function cmdCode(args) {
|
|
286
466
|
const file = args._[0]
|
|
287
|
-
if (!file) fail('code
|
|
467
|
+
if (!file) fail('code 需要文件路径')
|
|
288
468
|
const target = resolve(file)
|
|
289
469
|
if (!existsSync(target)) fail(`找不到文件:${target}`, 1)
|
|
290
470
|
const workspace = typeof args.workspace === 'string' ? resolve(args.workspace) : process.cwd()
|
|
@@ -294,7 +474,7 @@ async function cmdCode(args) {
|
|
|
294
474
|
else if (args.long) process.stdout.write(`长码(跨模型,粘贴至新对话首条消息):\n${codes.long}\n`)
|
|
295
475
|
else {
|
|
296
476
|
process.stdout.write(`短码(同一模型,粘贴至新会话首条消息):\n${codes.short}\n`)
|
|
297
|
-
process.stdout.write(`\n仅发送标记 ${codes.bare}
|
|
477
|
+
process.stdout.write(`\n仅发送标记 ${codes.bare} 亦可:表示接手全局索引中处于进行中的那一份(多份时将列出候选)。\n`)
|
|
298
478
|
process.stdout.write('跨模型,或对方未安装本技能时,添加 --long 输出长码。\n')
|
|
299
479
|
}
|
|
300
480
|
}
|
|
@@ -317,13 +497,17 @@ async function cmdArchive(args) {
|
|
|
317
497
|
if (!file) fail('archive 需提供文件路径')
|
|
318
498
|
const target = resolve(file)
|
|
319
499
|
if (!existsSync(target)) fail(`找不到文件:${target}`, 1)
|
|
500
|
+
const info = catalog(args)
|
|
320
501
|
const text = readFileSync(target, 'utf8').replace(/^\uFEFF/u, '')
|
|
321
502
|
const result = verifyDocument(text)
|
|
322
503
|
if (!result.ok) fail(`文档未通过校验,已拒绝归档:${result.problems.join('; ')}`, 1)
|
|
323
|
-
const dir = resolve(typeof args.dir === 'string' ? args.dir : DEFAULT_DIR)
|
|
324
504
|
const header = parseHeader(text) ?? {}
|
|
325
|
-
const
|
|
326
|
-
const indexPath = await appendIndex(
|
|
505
|
+
const status = typeof args.status === 'string' ? args.status : STATUS_OPEN
|
|
506
|
+
const indexPath = await appendIndex(info.root, target, {
|
|
507
|
+
title: docTitle(text, basename(target, '.md')),
|
|
508
|
+
status,
|
|
509
|
+
workspace: header.workspace,
|
|
510
|
+
})
|
|
327
511
|
process.stdout.write(`已写入索引:${indexPath}\n`)
|
|
328
512
|
}
|
|
329
513
|
|
|
@@ -334,12 +518,20 @@ async function cmdDrift(args) {
|
|
|
334
518
|
if (!existsSync(target)) fail(`未找到文件:${target}`, 1)
|
|
335
519
|
const text = readFileSync(target, 'utf8').replace(/^\uFEFF/u, '')
|
|
336
520
|
const header = parseHeader(text) ?? {}
|
|
337
|
-
const
|
|
521
|
+
const keyFiles = parseKeyFiles(text)
|
|
522
|
+
const candidates = []
|
|
523
|
+
if (typeof args.workspace === 'string') candidates.push({ dir: resolve(args.workspace), source: '--workspace' })
|
|
524
|
+
if (typeof header.workspace_root === 'string') candidates.push({ dir: resolve(header.workspace_root), source: 'header' })
|
|
525
|
+
candidates.push({ dir: resolve(dirname(target), '..'), source: '文档上级目录' })
|
|
526
|
+
candidates.push({ dir: resolve(process.cwd()), source: '当前目录' })
|
|
527
|
+
candidates.push({ dir: dirname(target), source: '文档所在目录' })
|
|
528
|
+
const picked = candidates.find((cand) => keyFiles.some((rel) => existsSync(resolve(cand.dir, rel)))) ?? candidates[candidates.length - 1]
|
|
529
|
+
const workspace = picked.dir
|
|
338
530
|
const staleHours = Number(args['stale-hours']) > 0 ? Number(args['stale-hours']) : DEFAULT_STALE_HOURS
|
|
339
531
|
const age = ageHours(header.created)
|
|
340
532
|
const created = Date.parse(String(header.created ?? ''))
|
|
341
533
|
|
|
342
|
-
const files =
|
|
534
|
+
const files = keyFiles.map((relPath) => {
|
|
343
535
|
const abs = resolve(workspace, relPath)
|
|
344
536
|
if (!existsSync(abs)) return { path: relPath, exists: false, changed: false }
|
|
345
537
|
const mtime = statSync(abs).mtimeMs
|
|
@@ -363,19 +555,13 @@ async function cmdDrift(args) {
|
|
|
363
555
|
}
|
|
364
556
|
}
|
|
365
557
|
|
|
366
|
-
const report = summarizeDrift({
|
|
367
|
-
doc: basename(target),
|
|
368
|
-
ageHours: age,
|
|
369
|
-
files,
|
|
370
|
-
git,
|
|
371
|
-
staleHours,
|
|
372
|
-
force: args.force === true,
|
|
373
|
-
})
|
|
558
|
+
const report = summarizeDrift({ doc: basename(target), ageHours: age, files, git, staleHours, force: args.force === true })
|
|
374
559
|
|
|
375
560
|
if (args.json) {
|
|
376
|
-
process.stdout.write(`${JSON.stringify({ file: target, ...report }, null, 2)}\n`)
|
|
561
|
+
process.stdout.write(`${JSON.stringify({ file: target, workspace, workspaceSource: picked.source, ...report }, null, 2)}\n`)
|
|
377
562
|
} else {
|
|
378
563
|
process.stdout.write(`检查:${basename(target)}\n`)
|
|
564
|
+
process.stdout.write(`工作区:${workspace}(来自${picked.source})\n`)
|
|
379
565
|
process.stdout.write(`${report.summary}\n`)
|
|
380
566
|
for (const detail of report.details) process.stdout.write(`· ${detail}\n`)
|
|
381
567
|
if (report.advice !== '') process.stdout.write(`建议:${report.advice}\n`)
|
|
@@ -384,23 +570,28 @@ async function cmdDrift(args) {
|
|
|
384
570
|
}
|
|
385
571
|
|
|
386
572
|
async function cmdStatus(args) {
|
|
387
|
-
const
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
process.stdout.write('暂无索引。可执行 baton init 创建,或直接使用 baton write 编写第一份。\n')
|
|
573
|
+
const info = resolveCatalog({ catalog: typeof args.catalog === 'string' ? args.catalog : undefined })
|
|
574
|
+
if (!info.initialized) {
|
|
575
|
+
process.stdout.write('还没有交接目录。运行 baton init 选择位置(默认 $DSH_HOME/baton)。\n')
|
|
391
576
|
return
|
|
392
577
|
}
|
|
393
|
-
const entries =
|
|
578
|
+
const entries = readEntries(info)
|
|
394
579
|
const report = inspectIndex(entries, {
|
|
395
580
|
staleDays: args['stale-days'],
|
|
396
581
|
closedDays: args['closed-days'],
|
|
397
582
|
maxClosed: args['max-closed'],
|
|
398
583
|
})
|
|
584
|
+
const missing = entries.filter((entry) => {
|
|
585
|
+
const abs = resolveRowPath(info.root, entry.path)
|
|
586
|
+
return abs === null || !existsSync(abs)
|
|
587
|
+
})
|
|
399
588
|
if (args.json) {
|
|
400
|
-
process.stdout.write(`${JSON.stringify({
|
|
589
|
+
process.stdout.write(`${JSON.stringify({ root: info.root, ...report, missing: missing.map((e) => e.path) }, null, 2)}\n`)
|
|
401
590
|
return
|
|
402
591
|
}
|
|
592
|
+
process.stdout.write(`交接目录:${info.root}\n`)
|
|
403
593
|
process.stdout.write(`交接索引:${report.summary}\n`)
|
|
594
|
+
if (missing.length > 0) process.stdout.write(`· ${missing.length} 条索引指向的文件不存在:${missing.slice(0, 5).map((e) => e.path).join('、')}(报告给用户,不要自己找替代)\n`)
|
|
404
595
|
if (report.hints.length === 0) {
|
|
405
596
|
process.stdout.write('没有滞留项,也没有需要清理的条目。\n')
|
|
406
597
|
return
|
|
@@ -409,60 +600,68 @@ async function cmdStatus(args) {
|
|
|
409
600
|
}
|
|
410
601
|
|
|
411
602
|
async function cmdList(args) {
|
|
412
|
-
const
|
|
413
|
-
|
|
414
|
-
const files = readdirSync(dir).filter((name) => /\.md$/iu.test(name) && name !== 'INDEX.md').sort().reverse()
|
|
415
|
-
if (files.length === 0) {
|
|
416
|
-
process.stdout.write('暂无交接文档\n')
|
|
417
|
-
return
|
|
418
|
-
}
|
|
419
|
-
const indexPath = join(dir, 'INDEX.md')
|
|
420
|
-
const entries = existsSync(indexPath) ? parseIndex(readFileSync(indexPath, 'utf8')) : []
|
|
421
|
-
const byPath = new Map(entries.map((entry) => [entry.path, entry]))
|
|
603
|
+
const info = catalog(args)
|
|
604
|
+
const entries = readEntries(info)
|
|
422
605
|
const openOnly = args.open === true
|
|
423
|
-
const rows =
|
|
424
|
-
.filter((
|
|
425
|
-
.map((
|
|
426
|
-
const
|
|
427
|
-
|
|
606
|
+
const rows = entries
|
|
607
|
+
.filter((entry) => !openOnly || entry.status !== STATUS_CLOSED)
|
|
608
|
+
.map((entry) => {
|
|
609
|
+
const abs = resolveRowPath(info.root, entry.path)
|
|
610
|
+
const mark = abs !== null && existsSync(abs) ? '' : ' — 文件缺失'
|
|
611
|
+
const workspace = entry.workspace === '' ? '' : ` · ${entry.workspace}`
|
|
612
|
+
return `${entryLabel(entry)} — ${entry.status} · ${entry.updated}${workspace} — ${entry.path}${mark}`
|
|
428
613
|
})
|
|
614
|
+
if (args.json) {
|
|
615
|
+
process.stdout.write(`${JSON.stringify({ root: info.root, entries }, null, 2)}\n`)
|
|
616
|
+
return
|
|
617
|
+
}
|
|
429
618
|
if (rows.length === 0) {
|
|
430
|
-
process.stdout.write('当前没有进行中的交接\n')
|
|
619
|
+
process.stdout.write(openOnly ? '当前没有进行中的交接\n' : '索引还是空的\n')
|
|
431
620
|
return
|
|
432
621
|
}
|
|
433
622
|
for (const row of rows) process.stdout.write(`${row}\n`)
|
|
434
623
|
}
|
|
435
624
|
|
|
436
625
|
async function cmdResolve(args) {
|
|
437
|
-
const
|
|
438
|
-
const
|
|
439
|
-
if (!existsSync(indexPath)) fail(`还没有索引:${indexPath}`, 1)
|
|
440
|
-
const entries = parseIndex(readFileSync(indexPath, 'utf8'))
|
|
626
|
+
const info = catalog(args)
|
|
627
|
+
const entries = readEntries(info)
|
|
441
628
|
const result = resolveIndex(entries)
|
|
629
|
+
const decorate = (entry) => {
|
|
630
|
+
const abs = resolveRowPath(info.root, entry.path)
|
|
631
|
+
return { ...entry, resolvedPath: abs, exists: abs !== null && existsSync(abs) }
|
|
632
|
+
}
|
|
633
|
+
const payload = { root: info.root, ...result, candidates: (result.candidates ?? []).map(decorate) }
|
|
634
|
+
if (result.kind === 'unique') payload.target = decorate(result.target)
|
|
442
635
|
if (args.json) {
|
|
443
|
-
process.stdout.write(`${JSON.stringify(
|
|
444
|
-
process.exitCode = result.kind === 'unique' ? 0 : result.kind === 'ambiguous' ? 2 : 1
|
|
636
|
+
process.stdout.write(`${JSON.stringify(payload, null, 2)}\n`)
|
|
637
|
+
process.exitCode = result.kind === 'unique' && payload.target.exists ? 0 : result.kind === 'ambiguous' ? 2 : 1
|
|
445
638
|
return
|
|
446
639
|
}
|
|
447
640
|
if (result.kind === 'unique') {
|
|
448
|
-
const
|
|
641
|
+
const target = payload.target
|
|
449
642
|
const staleDays = Number(args['stale-days']) > 0 ? Number(args['stale-days']) : undefined
|
|
450
643
|
const stale = inspectIndex(entries, { staleDays }).stale.find((item) => item.entry.path === target.path)
|
|
644
|
+
if (!target.exists) {
|
|
645
|
+
process.stdout.write(`唯一在用的交接指向的文件不存在:${target.path}\n`)
|
|
646
|
+
process.stdout.write('按规矩这要报告用户,不要自己找替代文件;可用 baton doctor 看全貌。\n')
|
|
647
|
+
process.exitCode = 1
|
|
648
|
+
return
|
|
649
|
+
}
|
|
451
650
|
const note = stale === undefined ? '' : `(${stale.days} 天没更新,可能已经过时)`
|
|
452
|
-
process.stdout.write(`唯一目标:${target.
|
|
651
|
+
process.stdout.write(`唯一目标:${target.resolvedPath}(${entryLabel(target)} · ${target.status} · ${target.updated})${note}\n`)
|
|
453
652
|
return
|
|
454
653
|
}
|
|
455
654
|
if (result.kind === 'ambiguous') {
|
|
456
655
|
process.stdout.write(`存在 ${result.candidates.length} 份在用,无法代为选择:\n`)
|
|
457
|
-
|
|
458
|
-
process.stdout.write(` ${index + 1}. ${entry
|
|
656
|
+
payload.candidates.forEach((entry, index) => {
|
|
657
|
+
process.stdout.write(` ${index + 1}. ${entryLabel(entry)} — ${entry.status} · ${entry.updated} — ${entry.resolvedPath}${entry.exists ? '' : '(文件缺失)'}\n`)
|
|
459
658
|
})
|
|
460
659
|
process.stdout.write('请向用户确认目标,或使用短码直接指定路径。\n')
|
|
461
660
|
process.exitCode = 2
|
|
462
661
|
return
|
|
463
662
|
}
|
|
464
663
|
process.stdout.write('当前没有在用(未关闭)的交接。\n')
|
|
465
|
-
for (const entry of
|
|
664
|
+
for (const entry of payload.candidates) process.stdout.write(` 参考:${entryLabel(entry)} · ${entry.status} · ${entry.updated} — ${entry.resolvedPath}\n`)
|
|
466
665
|
process.exitCode = 1
|
|
467
666
|
}
|
|
468
667
|
|
|
@@ -471,18 +670,17 @@ async function cmdTouch(args) {
|
|
|
471
670
|
if (!file) fail('touch 需要文件路径')
|
|
472
671
|
const status = typeof args.status === 'string' ? args.status : undefined
|
|
473
672
|
if (status !== undefined && !STATUSES.includes(status)) fail(`--status 只能是 ${STATUSES.join(' / ')}`)
|
|
474
|
-
const
|
|
475
|
-
const
|
|
476
|
-
const result = await touchIndexEntry(dir, relative, status === undefined ? {} : { status })
|
|
673
|
+
const info = catalog(args)
|
|
674
|
+
const result = await touchIndexEntry(info.root, file, status === undefined ? {} : { status })
|
|
477
675
|
if (!result.updated) fail(`没改成:${result.reason}`, 1)
|
|
478
|
-
process.stdout.write(`已更新索引:${
|
|
676
|
+
process.stdout.write(`已更新索引:${file} → ${status ?? '仅刷新时间'}\n`)
|
|
479
677
|
}
|
|
480
678
|
|
|
481
679
|
async function cmdPrune(args) {
|
|
482
|
-
const
|
|
680
|
+
const info = catalog(args)
|
|
483
681
|
const olderThanDays = Number(args['older-than']) > 0 ? Number(args['older-than']) : 30
|
|
484
682
|
const apply = args.apply === true
|
|
485
|
-
const result = await pruneIndex(
|
|
683
|
+
const result = await pruneIndex(info.root, { olderThanDays, apply })
|
|
486
684
|
if (result.candidates.length === 0) {
|
|
487
685
|
process.stdout.write(`没有超过 ${olderThanDays} 天的已关闭条目\n`)
|
|
488
686
|
return
|
|
@@ -490,30 +688,38 @@ async function cmdPrune(args) {
|
|
|
490
688
|
if (!apply) {
|
|
491
689
|
process.stdout.write(`可清理 ${result.candidates.length} 条(已关闭且超过 ${olderThanDays} 天):\n`)
|
|
492
690
|
for (const entry of result.candidates) process.stdout.write(` ${entry.path} — ${entry.title} · ${entry.updated}\n`)
|
|
493
|
-
process.stdout.write('确认无误后添加 --apply
|
|
691
|
+
process.stdout.write('确认无误后添加 --apply:目录树内的文档移入 archive/,别处的只摘索引行、不动文件。\n')
|
|
494
692
|
return
|
|
495
693
|
}
|
|
496
694
|
process.stdout.write(`已移入 archive/ 共 ${result.moved.length} 份:\n`)
|
|
497
695
|
for (const moved of result.moved) process.stdout.write(` ${moved.from} → ${moved.to}\n`)
|
|
696
|
+
if (result.keptInPlace.length > 0) {
|
|
697
|
+
process.stdout.write(`另有 ${result.keptInPlace.length} 份文档不在交接目录内,只摘了索引行、文件保留在原处:\n`)
|
|
698
|
+
for (const path of result.keptInPlace) process.stdout.write(` ${path}\n`)
|
|
699
|
+
}
|
|
498
700
|
}
|
|
499
701
|
|
|
500
702
|
const commands = {
|
|
703
|
+
adopt: cmdAdopt,
|
|
704
|
+
archive: cmdArchive,
|
|
501
705
|
check: cmdCheck,
|
|
706
|
+
code: cmdCode,
|
|
707
|
+
doctor: cmdDoctor,
|
|
708
|
+
drift: cmdDrift,
|
|
502
709
|
entry: cmdEntry,
|
|
503
710
|
init: cmdInit,
|
|
504
|
-
write: cmdWrite,
|
|
505
|
-
new: cmdNew,
|
|
506
|
-
update: cmdUpdate,
|
|
507
|
-
verify: cmdVerify,
|
|
508
|
-
code: cmdCode,
|
|
509
|
-
archive: cmdArchive,
|
|
510
711
|
list: cmdList,
|
|
511
|
-
|
|
512
|
-
|
|
712
|
+
new: cmdNew,
|
|
713
|
+
prune: cmdPrune,
|
|
513
714
|
resolve: cmdResolve,
|
|
514
|
-
|
|
715
|
+
scan: cmdScan,
|
|
716
|
+
snippet: cmdSnippet,
|
|
717
|
+
status: cmdStatus,
|
|
515
718
|
sync: cmdSync,
|
|
516
|
-
|
|
719
|
+
touch: cmdTouch,
|
|
720
|
+
update: cmdUpdate,
|
|
721
|
+
verify: cmdVerify,
|
|
722
|
+
write: cmdWrite,
|
|
517
723
|
}
|
|
518
724
|
|
|
519
725
|
function main() {
|
|
@@ -522,22 +728,26 @@ function main() {
|
|
|
522
728
|
if (!command || command === '--help' || command === '-h') {
|
|
523
729
|
process.stdout.write(
|
|
524
730
|
'交接棒 baton CLI\n' +
|
|
525
|
-
' baton
|
|
731
|
+
' baton init [--dir <位置>] [--force] 首次使用:选定交接目录并写 location.json\n' +
|
|
732
|
+
' baton doctor [--json] 体检:目录 / 索引 / 路由 / AGENTS.md 规则块\n' +
|
|
733
|
+
' baton snippet 打印要贴进 AGENTS.md 的规则片段\n' +
|
|
734
|
+
' baton entry "<消息>" [--json] 识别接棒口令(⟦baton⟧ / <<baton>>)\n' +
|
|
526
735
|
' baton check --tokens N [--window M] [--json]\n' +
|
|
527
|
-
' baton
|
|
528
|
-
' baton
|
|
529
|
-
' baton
|
|
530
|
-
' baton
|
|
736
|
+
' baton write --json payload.json [--dir <文档目录>] [--catalog <索引目录>] [--dry-run] [--quiet]\n' +
|
|
737
|
+
' baton new --title "标题" 写一份待填的骨架(不登记索引)\n' +
|
|
738
|
+
' baton adopt <文件...> [--status 进行中] 把已有的交接文档登记进索引\n' +
|
|
739
|
+
' baton scan [--roots a;b] [--adopt] [--depth 4] 扫出没登记的交接文档\n' +
|
|
740
|
+
' baton update <file> --json payload.json 就地改写七节,保留 id/created\n' +
|
|
531
741
|
' baton verify <file> [--strict] [--json]\n' +
|
|
532
742
|
' baton code <file> [--workspace .] [--long] [--json]\n' +
|
|
533
|
-
' baton archive <file> [--
|
|
534
|
-
' baton list [--open] [--
|
|
743
|
+
' baton archive <file> [--status 进行中] 校验后登记进索引\n' +
|
|
744
|
+
' baton list [--open] [--json] 列出索引条目与文件是否存在\n' +
|
|
535
745
|
' baton status [--stale-days 7] [--closed-days 30] [--max-closed 20] [--json]\n' +
|
|
536
|
-
' baton drift <file> [--stale-hours 12] [--force]
|
|
537
|
-
' baton resolve [--
|
|
538
|
-
' baton touch <file
|
|
539
|
-
' baton sync [--fix]
|
|
540
|
-
' baton prune [--older-than 30] [--apply]
|
|
746
|
+
' baton drift <file> [--workspace <目录>] [--stale-hours 12] [--force]\n' +
|
|
747
|
+
' baton resolve [--json] 裸标记该接哪份(唯一 / 有歧义 / 没有)\n' +
|
|
748
|
+
' baton touch <file|路径> [--status 进行中|已接手|已关闭]\n' +
|
|
749
|
+
' baton sync [--fix] 目录与索引对账\n' +
|
|
750
|
+
' baton prune [--older-than 30] [--apply] 已关闭且过期的条目归档\n',
|
|
541
751
|
)
|
|
542
752
|
return
|
|
543
753
|
}
|