dsh-agent-team-playbook 1.23.0 → 1.24.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/README.md +5 -5
- package/bin/cli.mjs +286 -120
- package/cordis.patch.yml +5 -5
- package/lib/index.js +12 -9
- package/package.json +5 -5
- package/scripts/{sync-skill.mjs → sync-playbook.mjs} +22 -6
package/README.md
CHANGED
|
@@ -96,14 +96,14 @@ docs/workflow/practices/ 经验与做法库
|
|
|
96
96
|
dsh plugin --profile web add dsh-agent-team-playbook
|
|
97
97
|
```
|
|
98
98
|
|
|
99
|
-
|
|
99
|
+
装好后多一个只读命令 `/atp`(agent-team-playbook 的缩写;取名短而少见,因为 DSH 对重名命令是硬报错):
|
|
100
100
|
|
|
101
101
|
| 命令 | 作用 |
|
|
102
102
|
|---|---|
|
|
103
|
-
| `/
|
|
104
|
-
| `/
|
|
105
|
-
| `/
|
|
106
|
-
| `/
|
|
103
|
+
| `/atp` | 规程摘要(档位、四问、收尾三问)+ 安装方式 |
|
|
104
|
+
| `/atp core` | 输出核心全文,读完即在本轮按它协作 |
|
|
105
|
+
| `/atp paths` | 各分册的绝对路径,按需查阅 |
|
|
106
|
+
| `/atp help` | 用法 |
|
|
107
107
|
|
|
108
108
|
插件**不写任何文件**、也**不挂 skill**——skill 的目录摘要会常驻占用上下文(约 300 tokens/轮),而分册本来就不会自动进上下文,用命令按需取更划算。
|
|
109
109
|
|
package/bin/cli.mjs
CHANGED
|
@@ -2,21 +2,23 @@
|
|
|
2
2
|
/**
|
|
3
3
|
* dsh-agent-team-playbook CLI
|
|
4
4
|
*
|
|
5
|
-
*
|
|
5
|
+
* 插件安装本身**不写用户任何文件**(插件只注册只读命令)。本命令是唯一的写文件入口,
|
|
6
6
|
* 因此它必须对"覆盖用户已有文件"这件事极其保守:
|
|
7
7
|
*
|
|
8
|
-
* -
|
|
9
|
-
* -
|
|
10
|
-
* -
|
|
8
|
+
* - 每个冲突都单独询问:覆盖已有 / 改名已有(备份你的文件) / 改名我们的 / 跳过 / 中止
|
|
9
|
+
* - 选「改名我们的」时,写出的文件里对它的引用一并改写,不留断链
|
|
10
|
+
* - 选「改名已有」时,你的原文件被加上后缀保留,我们占用原路径
|
|
11
|
+
* - 非交互环境遇到冲突默认中止,必须显式 --force / --skip-conflicts / --backup-existing
|
|
11
12
|
*
|
|
12
13
|
* 用法:
|
|
13
14
|
* npx dsh-agent-team-playbook init [--dir <目标目录>] [--dry-run]
|
|
14
|
-
*
|
|
15
|
+
* [--force | --skip-conflicts | --backup-existing]
|
|
16
|
+
* npx dsh-agent-team-playbook init --global [--inline-core] [--yes] [--dry-run]
|
|
15
17
|
*/
|
|
16
18
|
import { createInterface } from 'node:readline/promises'
|
|
17
19
|
import os from 'node:os'
|
|
18
20
|
import { stdin, stdout } from 'node:process'
|
|
19
|
-
import { readFile, writeFile, mkdir, stat, readdir } from 'node:fs/promises'
|
|
21
|
+
import { readFile, writeFile, mkdir, stat, readdir, rename, rm, rmdir } from 'node:fs/promises'
|
|
20
22
|
import { existsSync } from 'node:fs'
|
|
21
23
|
import path from 'node:path'
|
|
22
24
|
import { fileURLToPath } from 'node:url'
|
|
@@ -30,7 +32,7 @@ process.stdout.on('error', (err) => {
|
|
|
30
32
|
})
|
|
31
33
|
const SOURCE_ROOT = path.join(PACKAGE_ROOT, 'playbook')
|
|
32
34
|
|
|
33
|
-
/** 会被安装进用户项目的条目:playbook
|
|
35
|
+
/** 会被安装进用户项目的条目:playbook 本体(核心 + 分册)。 */
|
|
34
36
|
const ROOT_FILE = 'AGENTS.md'
|
|
35
37
|
const ROOT_DIR = 'docs'
|
|
36
38
|
|
|
@@ -52,6 +54,9 @@ function parseArgs(argv) {
|
|
|
52
54
|
global: false,
|
|
53
55
|
yes: false,
|
|
54
56
|
inlineCore: false,
|
|
57
|
+
backupExisting: false,
|
|
58
|
+
renameCore: undefined,
|
|
59
|
+
renameDocs: undefined,
|
|
55
60
|
}
|
|
56
61
|
for (let i = 0; i < argv.length; i += 1) {
|
|
57
62
|
const a = argv[i]
|
|
@@ -62,6 +67,9 @@ function parseArgs(argv) {
|
|
|
62
67
|
else if (a === '--global' || a === '-g') opts.global = true
|
|
63
68
|
else if (a === '--yes' || a === '-y') opts.yes = true
|
|
64
69
|
else if (a === '--inline-core') opts.inlineCore = true
|
|
70
|
+
else if (a === '--backup-existing') opts.backupExisting = true
|
|
71
|
+
else if (a === '--rename-core') opts.renameCore = argv[(i += 1)]
|
|
72
|
+
else if (a === '--rename-docs') opts.renameDocs = argv[(i += 1)]
|
|
65
73
|
else if (a === '--help' || a === '-h') opts.help = true
|
|
66
74
|
else if (a === 'init') continue
|
|
67
75
|
else {
|
|
@@ -86,6 +94,9 @@ ${C.bold('选项')}
|
|
|
86
94
|
默认只写指针(约 200 tokens,agent 需先读一次文件)
|
|
87
95
|
--yes, -y 非交互确认(--global 追加区块用)
|
|
88
96
|
--dry-run 只显示计划,不写任何文件
|
|
97
|
+
--backup-existing 冲突时把已有项改名为 *<后缀>,再装我们的(不询问)
|
|
98
|
+
--rename-core <名> 非交互:把我们的核心装成这个名字(如 AGENTS.team.md),引用同步改写
|
|
99
|
+
--rename-docs <名> 非交互:把我们的分册目录装成这个名字(如 docs-playbook),引用同步改写
|
|
89
100
|
--force, -f 冲突时全部覆盖(不询问)
|
|
90
101
|
--skip-conflicts 冲突时全部跳过(不询问)
|
|
91
102
|
-h, --help 显示本帮助
|
|
@@ -98,7 +109,8 @@ ${C.bold('两种安装方式的区别(重要)')}
|
|
|
98
109
|
适用于"本机所有项目都按这套规程走"。
|
|
99
110
|
|
|
100
111
|
${C.bold('行为')}
|
|
101
|
-
·
|
|
112
|
+
· 交互模式下,每个冲突单独询问:
|
|
113
|
+
覆盖已有 / 改名已有(备份你的文件) / 改名我们的 / 跳过 / 剩余全部覆盖 / 中止
|
|
102
114
|
· 选"改名"时,文档里对它的引用会同步改写
|
|
103
115
|
· 非交互模式(管道、CI)遇到冲突默认中止,不会静默覆盖
|
|
104
116
|
`)
|
|
@@ -122,18 +134,24 @@ async function walk(relDir) {
|
|
|
122
134
|
return out.sort()
|
|
123
135
|
}
|
|
124
136
|
|
|
125
|
-
/**
|
|
126
|
-
|
|
137
|
+
/**
|
|
138
|
+
* 目标路径的冲突详情。`target` 是"改已有文件名"时要动的那个路径:
|
|
139
|
+
* - 同名文件已存在 → 改名该文件
|
|
140
|
+
* - 同名位置是目录 → 改名该目录(风险高,调用处需提示)
|
|
141
|
+
* - 上级是文件导致建不了目录 → 改名那个上级文件
|
|
142
|
+
*/
|
|
143
|
+
async function inspectConflict(destAbs) {
|
|
127
144
|
if (existsSync(destAbs)) {
|
|
128
145
|
const s = await stat(destAbs)
|
|
129
|
-
return s.isDirectory()
|
|
146
|
+
return s.isDirectory()
|
|
147
|
+
? { reason: '同名位置已是目录', kind: 'dir', target: destAbs }
|
|
148
|
+
: { reason: '文件已存在', kind: 'file', target: destAbs }
|
|
130
149
|
}
|
|
131
|
-
// 逐级检查父目录是否是文件
|
|
132
150
|
let p = path.dirname(destAbs)
|
|
133
151
|
while (p && p !== path.dirname(p)) {
|
|
134
152
|
if (existsSync(p)) {
|
|
135
153
|
const s = await stat(p)
|
|
136
|
-
if (!s.isDirectory()) return `上级 ${path.basename(p)}
|
|
154
|
+
if (!s.isDirectory()) return { reason: `上级 ${path.basename(p)} 是文件,无法创建目录`, kind: 'parent-file', target: p }
|
|
137
155
|
break
|
|
138
156
|
}
|
|
139
157
|
p = path.dirname(p)
|
|
@@ -141,6 +159,14 @@ async function describeConflict(destAbs) {
|
|
|
141
159
|
return null
|
|
142
160
|
}
|
|
143
161
|
|
|
162
|
+
/** 生成不冲突的备份路径。 */
|
|
163
|
+
function uniqueBackupPath(target, suffix) {
|
|
164
|
+
let to = `${target}${suffix}`
|
|
165
|
+
let i = 1
|
|
166
|
+
while (existsSync(to)) to = `${target}${suffix}-${i++}`
|
|
167
|
+
return to
|
|
168
|
+
}
|
|
169
|
+
|
|
144
170
|
/** 把引用改写应用到文本内容:先改目录前缀,再改根文件名。 */
|
|
145
171
|
function rewriteReferences(text, renames) {
|
|
146
172
|
let out = text
|
|
@@ -177,7 +203,6 @@ function managedBlock(destAbs, inlineCore, coreText) {
|
|
|
177
203
|
|
|
178
204
|
// 内联:核心全文进区块;相对路径改写为绝对路径,保证在任意工作区都能读到
|
|
179
205
|
const rewritten = coreText
|
|
180
|
-
.replace(/\(^\)/, '') // 不裁剪开头
|
|
181
206
|
.replace(/(^|[^/\w.-])AGENTS\.md(?![.\w-])/g, (_m, pre) => `${pre}${destAbs}/AGENTS.md`)
|
|
182
207
|
.split('docs/').join(`${destAbs}/docs/`)
|
|
183
208
|
|
|
@@ -215,7 +240,7 @@ async function runGlobalMode(opts) {
|
|
|
215
240
|
existing = await readFile(instrFile, 'utf8')
|
|
216
241
|
const hasBlock = existing.includes(BLOCK_START)
|
|
217
242
|
console.log(
|
|
218
|
-
C.dim(` ${instrFile} 已存在(${existing.length}
|
|
243
|
+
C.dim(` ${instrFile} 已存在(${existing.length} 字符),`) +
|
|
219
244
|
(hasBlock ? C.green('检测到本插件的受管区块 → 原地更新') : C.yellow('没有受管区块 → 需追加')),
|
|
220
245
|
)
|
|
221
246
|
if (!hasBlock && !opts.yes && !opts.force) {
|
|
@@ -291,91 +316,142 @@ async function main() {
|
|
|
291
316
|
console.error(C.red(`✗ 目标目录不存在:${opts.dir}`))
|
|
292
317
|
process.exit(1)
|
|
293
318
|
}
|
|
319
|
+
if (!(await stat(opts.dir)).isDirectory()) {
|
|
320
|
+
console.error(C.red(`✗ --dir 不是目录:${opts.dir}`))
|
|
321
|
+
process.exit(1)
|
|
322
|
+
}
|
|
294
323
|
|
|
295
|
-
// ── 1.
|
|
296
|
-
const
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
324
|
+
// ── 1. 组装安装单元:核心与分册是一个整体,必须同进同退 ─────────────
|
|
325
|
+
const docRels = await walk(ROOT_DIR)
|
|
326
|
+
if (docRels.length === 0) {
|
|
327
|
+
console.error(C.red(`✗ 包内缺少分册:${path.join(SOURCE_ROOT, ROOT_DIR)} 为空或不存在`))
|
|
328
|
+
process.exit(1)
|
|
329
|
+
}
|
|
330
|
+
const UNITS = [
|
|
331
|
+
{ key: ROOT_FILE, kind: 'file', files: [ROOT_FILE] },
|
|
332
|
+
{ key: ROOT_DIR, kind: 'dir', files: docRels },
|
|
333
|
+
]
|
|
334
|
+
|
|
335
|
+
// ── 2. 冲突检测(按单元归并) ──────────────────────────────────────
|
|
336
|
+
const conflicts = new Map()
|
|
337
|
+
for (const u of UNITS) {
|
|
338
|
+
for (const rel of u.files) {
|
|
339
|
+
const c = await inspectConflict(path.join(opts.dir, rel))
|
|
340
|
+
if (c === null) continue
|
|
341
|
+
if (!conflicts.has(u.key)) conflicts.set(u.key, { kind: u.kind, targets: new Set(), files: [], reasons: new Set() })
|
|
342
|
+
const entry = conflicts.get(u.key)
|
|
343
|
+
entry.targets.add(c.target)
|
|
344
|
+
entry.files.push(rel)
|
|
345
|
+
entry.reasons.add(c.reason)
|
|
346
|
+
}
|
|
307
347
|
}
|
|
308
348
|
|
|
309
|
-
if (
|
|
310
|
-
console.log(C.green('✓
|
|
349
|
+
if (conflicts.size === 0) {
|
|
350
|
+
console.log(C.green('✓ 目标目录没有同名内容,直接安装(核心 + 分册一起)。'))
|
|
311
351
|
} else {
|
|
312
|
-
console.log(C.yellow(
|
|
313
|
-
for (const [
|
|
314
|
-
console.log(` · ${C.bold(
|
|
352
|
+
console.log(C.yellow('⚠ 检测到冲突(核心与分册必须一起装,所以下面整体决策):'))
|
|
353
|
+
for (const [key, info] of conflicts) {
|
|
354
|
+
console.log(` · ${C.bold(key)} —— ${[...info.reasons].join(';')},涉及 ${info.files.length} 个文件`)
|
|
355
|
+
for (const target of info.targets) console.log(C.dim(` 已有:${path.relative(opts.dir, target)}`))
|
|
315
356
|
}
|
|
316
357
|
}
|
|
317
358
|
|
|
318
|
-
// ── 3.
|
|
319
|
-
const renames = []
|
|
320
|
-
const
|
|
321
|
-
const
|
|
322
|
-
|
|
359
|
+
// ── 3. 决策(只有"整体"的选项,不存在装一半) ──────────────────────
|
|
360
|
+
const renames = [] // 我们这侧的改名(会同步改写引用)
|
|
361
|
+
const existingRenames = [] // 已有项改名备份
|
|
362
|
+
const defaultSuffix = `.bak-${new Date().toISOString().replace(/[-:T]/g, '').slice(0, 14)}`
|
|
363
|
+
let install = true
|
|
323
364
|
const interactive = stdin.isTTY && stdout.isTTY
|
|
324
365
|
|
|
325
|
-
|
|
366
|
+
const backupAll = (suffix) => {
|
|
367
|
+
for (const [, info] of conflicts) {
|
|
368
|
+
for (const target of info.targets) {
|
|
369
|
+
existingRenames.push({ from: target, to: uniqueBackupPath(target, suffix) })
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// 非交互显式改名:只要每个冲突单元都被覆盖,就不再提问
|
|
375
|
+
if (opts.renameCore !== undefined) {
|
|
376
|
+
if (!/^[^/\\]+$/.test(opts.renameCore)) { console.error(C.red('✗ --rename-core 不能含路径分隔符')); process.exit(1) }
|
|
377
|
+
if (opts.renameCore === ROOT_FILE) { console.error(C.red(`✗ --rename-core 不能与原名相同`)); process.exit(1) }
|
|
378
|
+
if (existsSync(path.join(opts.dir, opts.renameCore))) {
|
|
379
|
+
console.error(C.red(`✗ --rename-core 目标已存在:${opts.renameCore}`)); process.exit(1)
|
|
380
|
+
}
|
|
381
|
+
renames.push({ from: ROOT_FILE, to: opts.renameCore })
|
|
382
|
+
}
|
|
383
|
+
if (opts.renameDocs !== undefined) {
|
|
384
|
+
if (!/^[^/\\]+$/.test(opts.renameDocs)) { console.error(C.red('✗ --rename-docs 不能含路径分隔符')); process.exit(1) }
|
|
385
|
+
if (opts.renameDocs === ROOT_DIR) { console.error(C.red(`✗ --rename-docs 不能与原名相同`)); process.exit(1) }
|
|
386
|
+
if (existsSync(path.join(opts.dir, opts.renameDocs))) {
|
|
387
|
+
console.error(C.red(`✗ --rename-docs 目标已存在:${opts.renameDocs}`)); process.exit(1)
|
|
388
|
+
}
|
|
389
|
+
renames.push({ from: `${ROOT_DIR}/`, to: `${opts.renameDocs}/` })
|
|
390
|
+
}
|
|
391
|
+
const unresolved = [...conflicts.keys()].filter(
|
|
392
|
+
(k) => !renames.some((r) => (k === ROOT_FILE ? r.from === ROOT_FILE : r.from === `${ROOT_DIR}/`)),
|
|
393
|
+
)
|
|
394
|
+
// 显式改名时提示一下(用户可能以为必须冲突才会改名)
|
|
395
|
+
for (const r of renames) {
|
|
396
|
+
if (r.from === ROOT_FILE && !conflicts.has(ROOT_FILE)) console.log(C.dim(` --rename-core:核心将装成 ${r.to}(原路径未冲突,仍按你的要求改名)`))
|
|
397
|
+
if (r.from === `${ROOT_DIR}/` && !conflicts.has(ROOT_DIR)) console.log(C.dim(` --rename-docs:分册将装到 ${r.to}/(原路径未冲突,仍按你的要求改名)`))
|
|
398
|
+
}
|
|
399
|
+
if (conflicts.size > 0 && unresolved.length === 0) {
|
|
400
|
+
console.log(C.dim(` 已按 --rename-* 处理全部冲突:${renames.map((r) => `${r.from} → ${r.to}`).join(',')}`))
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
if (unresolved.length > 0) {
|
|
326
404
|
if (opts.force) {
|
|
327
|
-
|
|
328
|
-
console.log(C.dim(' --force:全部覆盖'))
|
|
405
|
+
console.log(C.dim(' --force:覆盖已有内容'))
|
|
329
406
|
} else if (opts.skipConflicts) {
|
|
330
|
-
|
|
331
|
-
console.log(C.dim(' --skip-conflicts
|
|
407
|
+
install = false
|
|
408
|
+
console.log(C.dim(' --skip-conflicts:整体跳过(不会只装一半)'))
|
|
409
|
+
} else if (opts.backupExisting) {
|
|
410
|
+
backupAll(defaultSuffix)
|
|
411
|
+
console.log(C.dim(` --backup-existing:已有冲突项改名为 *${defaultSuffix} 后安装`))
|
|
332
412
|
} else if (!interactive) {
|
|
333
|
-
console.error(C.red('\n✗
|
|
334
|
-
console.error(' 可选:--force
|
|
413
|
+
console.error(C.red('\n✗ 非交互环境下检测到冲突,已中止(不会静默覆盖,也不会只装一半)。'))
|
|
414
|
+
console.error(' 可选:--force 覆盖|--skip-conflicts 跳过(整体不装)|--backup-existing 备份已有项后安装。')
|
|
335
415
|
process.exit(1)
|
|
336
416
|
} else {
|
|
337
417
|
const rl = createInterface({ input: stdin, output: stdout })
|
|
338
|
-
let overwriteRest = false
|
|
339
418
|
try {
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
continue
|
|
344
|
-
}
|
|
345
|
-
const isDir = g === ROOT_DIR
|
|
346
|
-
console.log(`\n${C.bold('冲突:')}${g}${isDir ? '(目录)' : ''}`)
|
|
347
|
-
const answer = (
|
|
348
|
-
await rl.question(
|
|
349
|
-
` ${C.bold('[o]')} 覆盖 ${C.bold('[r]')} 改名 ${C.bold('[s]')} 跳过 ${C.bold('[a]')} 剩余全部覆盖 ${C.bold('[q]')} 中止 > `,
|
|
350
|
-
)
|
|
419
|
+
const answer = (
|
|
420
|
+
await rl.question(
|
|
421
|
+
`\n${C.bold('怎么处理?')} ${C.bold('[o]')} 覆盖已有 ${C.bold('[e]')} 改名已有 ${C.bold('[r]')} 改名我们的 ${C.bold('[s]')} 跳过(整体不装) ${C.bold('[q]')} 中止 > `,
|
|
351
422
|
)
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
423
|
+
)
|
|
424
|
+
.trim()
|
|
425
|
+
.toLowerCase()
|
|
426
|
+
|
|
427
|
+
if (answer === 'o') {
|
|
428
|
+
// 覆盖
|
|
429
|
+
} else if (answer === 'e') {
|
|
430
|
+
const suffix = (await rl.question(` 给已有项加的后缀(默认 ${defaultSuffix})> `)).trim() || defaultSuffix
|
|
431
|
+
if (!/^[.A-Za-z0-9_-]+$/.test(suffix)) {
|
|
432
|
+
console.error(C.red(' 后缀只能含字母数字点下划线横线,已中止。'))
|
|
433
|
+
process.exit(1)
|
|
434
|
+
}
|
|
435
|
+
backupAll(suffix)
|
|
436
|
+
} else if (answer === 'r') {
|
|
437
|
+
// 只对我们这侧改名:逐个冲突单元问
|
|
438
|
+
for (const key of conflicts.keys()) {
|
|
439
|
+
const suggestion = key === ROOT_DIR ? 'docs-playbook' : 'AGENTS.team.md'
|
|
440
|
+
const raw = (await rl.question(` ${key} 的新名字(默认 ${suggestion})> `)).trim() || suggestion
|
|
363
441
|
const clean = raw.replace(/[/\\]/g, '-')
|
|
364
|
-
if (clean === path.basename(
|
|
365
|
-
console.
|
|
366
|
-
|
|
367
|
-
continue
|
|
442
|
+
if (clean === path.basename(key) || clean === '') {
|
|
443
|
+
console.error(C.red(' 名字未变化,已中止。'))
|
|
444
|
+
process.exit(1)
|
|
368
445
|
}
|
|
369
|
-
if (
|
|
370
|
-
else renames.push({ from:
|
|
371
|
-
console.log(C.dim(` → 将安装为 ${clean}
|
|
372
|
-
} else if (answer === 'q') {
|
|
373
|
-
console.log('已中止,未写入任何文件。')
|
|
374
|
-
process.exit(1)
|
|
375
|
-
} else {
|
|
376
|
-
console.log(C.yellow(' 未识别的选项,按跳过处理。'))
|
|
377
|
-
skipped.add(g)
|
|
446
|
+
if (key === ROOT_DIR) renames.push({ from: `${key}/`, to: `${clean}/` })
|
|
447
|
+
else renames.push({ from: key, to: clean })
|
|
448
|
+
console.log(C.dim(` → ${key} 将安装为 ${clean},文档内引用同步改写`))
|
|
378
449
|
}
|
|
450
|
+
} else if (answer === 's') {
|
|
451
|
+
install = false
|
|
452
|
+
} else {
|
|
453
|
+
console.log('已中止,未写入任何文件。')
|
|
454
|
+
process.exit(1)
|
|
379
455
|
}
|
|
380
456
|
} finally {
|
|
381
457
|
rl.close()
|
|
@@ -383,62 +459,152 @@ async function main() {
|
|
|
383
459
|
}
|
|
384
460
|
}
|
|
385
461
|
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
if (skipped.has(f.group)) continue
|
|
390
|
-
let rel = f.rel
|
|
391
|
-
for (const r of renames) {
|
|
392
|
-
if (r.group !== f.group) continue
|
|
393
|
-
rel = r.from === 'AGENTS.md' ? (rel === 'AGENTS.md' ? r.to : rel) : rel.startsWith(r.from) ? r.to + rel.slice(r.from.length) : rel
|
|
394
|
-
}
|
|
395
|
-
plan.push({ ...f, finalRel: rel, overwrite: overwrite.has(f.group) })
|
|
462
|
+
if (!install) {
|
|
463
|
+
console.log(C.yellow('\n已跳过:核心与分册都没有安装(不产生半成品)。'))
|
|
464
|
+
return
|
|
396
465
|
}
|
|
397
466
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
467
|
+
// ── 4. 生成最终计划(改名会同步改写引用) ──────────────────────────
|
|
468
|
+
const plan = []
|
|
469
|
+
for (const u of UNITS) {
|
|
470
|
+
for (const rel of u.files) {
|
|
471
|
+
let finalRel = rel
|
|
472
|
+
for (const r of renames) {
|
|
473
|
+
if (r.from === ROOT_FILE) {
|
|
474
|
+
if (finalRel === ROOT_FILE) finalRel = r.to
|
|
475
|
+
} else if (finalRel.startsWith(r.from)) {
|
|
476
|
+
finalRel = r.to + finalRel.slice(r.from.length)
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
plan.push({ srcRel: rel, finalRel })
|
|
480
|
+
}
|
|
401
481
|
}
|
|
402
482
|
|
|
403
|
-
// ── 5. 预览 / 写入 ───────────────────────────────────────────────
|
|
404
483
|
const refRenames = renames.map(({ from, to }) => ({ from, to }))
|
|
405
|
-
console.log(`\n${C.bold('安装计划')}${opts.dryRun ? C.dim('(--dry-run,不会写入)') : ''}
|
|
406
|
-
for (const item of plan) {
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
}
|
|
410
|
-
if (
|
|
411
|
-
console.log(C.dim(
|
|
484
|
+
console.log(`\n${C.bold('安装计划')}${opts.dryRun ? C.dim('(--dry-run,不会写入)') : ''} —— 共 ${plan.length} 个文件,整体生效`)
|
|
485
|
+
for (const item of plan.slice(0, 3)) console.log(` ${C.dim('新增')} ${item.finalRel}`)
|
|
486
|
+
if (plan.length > 6) console.log(C.dim(` …… 中间 ${plan.length - 6} 个文件省略……`))
|
|
487
|
+
for (const item of plan.slice(-3)) console.log(` ${C.dim('新增')} ${item.finalRel}`)
|
|
488
|
+
if (refRenames.length > 0) console.log(C.dim(`引用同步改写:${refRenames.map((r) => `${r.from} → ${r.to}`).join(',')}`))
|
|
489
|
+
if (existingRenames.length > 0) {
|
|
490
|
+
console.log(C.dim(`已有项改名:${existingRenames.map((r) => `${path.relative(opts.dir, r.from)} → ${path.relative(opts.dir, r.to)}`).join(',')}`))
|
|
412
491
|
}
|
|
413
492
|
|
|
414
493
|
if (opts.dryRun) {
|
|
415
|
-
console.log(C.dim('\n(dry-run
|
|
494
|
+
console.log(C.dim('\n(dry-run 结束,未写入任何文件)'))
|
|
416
495
|
return
|
|
417
496
|
}
|
|
418
497
|
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
498
|
+
// ── 5. 暂存 → 提交 → 失败回滚(保证要么全有、要么全无) ─────────────
|
|
499
|
+
const stageRoot = path.join(opts.dir, `.atp-stage-${process.pid}-${Date.now()}`)
|
|
500
|
+
const stashRoot = path.join(stageRoot, '.rollback')
|
|
501
|
+
const committed = []
|
|
502
|
+
const madeDirs = []
|
|
503
|
+
const doneBackups = []
|
|
504
|
+
const stashed = [] // 被覆盖的原件:安装失败时还原
|
|
505
|
+
|
|
506
|
+
const cleanupStage = async () => {
|
|
507
|
+
await rm(stageRoot, { recursive: true, force: true }).catch(() => {})
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
const rollback = async () => {
|
|
511
|
+
for (const f of committed.reverse()) {
|
|
512
|
+
await rm(f, { force: true, recursive: true }).catch(() => {})
|
|
426
513
|
}
|
|
427
|
-
|
|
514
|
+
for (const s of stashed.reverse()) {
|
|
515
|
+
await mkdir(path.dirname(s.from), { recursive: true }).catch(() => {})
|
|
516
|
+
await rename(s.to, s.from).catch(() => {})
|
|
517
|
+
}
|
|
518
|
+
for (const r of doneBackups.reverse()) {
|
|
519
|
+
await rename(r.to, r.from).catch(() => {})
|
|
520
|
+
}
|
|
521
|
+
for (const d of madeDirs.reverse()) {
|
|
522
|
+
await rmdir(d).catch(() => {})
|
|
523
|
+
}
|
|
524
|
+
await cleanupStage()
|
|
525
|
+
}
|
|
428
526
|
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
527
|
+
try {
|
|
528
|
+
// 5.1 全部写进暂存目录(此时用户目录还没被改动)
|
|
529
|
+
for (const item of plan) {
|
|
530
|
+
const dest = path.join(stageRoot, item.finalRel)
|
|
531
|
+
await mkdir(path.dirname(dest), { recursive: true })
|
|
532
|
+
const srcAbs = path.join(SOURCE_ROOT, item.srcRel)
|
|
533
|
+
const isText = /\.(md|svg|txt|yml|yaml|json)$/i.test(item.srcRel)
|
|
534
|
+
if (isText && refRenames.length > 0) {
|
|
535
|
+
await writeFile(dest, rewriteReferences(await readFile(srcAbs, 'utf8'), refRenames), 'utf8')
|
|
536
|
+
} else {
|
|
537
|
+
await writeFile(dest, await readFile(srcAbs))
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// 5.2 备份"已有项"(改名腾出原路径)
|
|
542
|
+
for (const r of existingRenames) {
|
|
543
|
+
await mkdir(path.dirname(r.to), { recursive: true })
|
|
544
|
+
await rename(r.from, r.to)
|
|
545
|
+
doneBackups.push(r)
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
// 5.3 覆盖前先"暂存"原件:这样即使后面失败,也能原样还原,不会丢用户数据
|
|
549
|
+
const planDestSet = new Set(plan.map((i) => path.join(opts.dir, i.finalRel)))
|
|
550
|
+
for (const [, info] of conflicts) {
|
|
551
|
+
for (const target of info.targets) {
|
|
552
|
+
if (!existsSync(target)) continue // 已被 [e] 备份走
|
|
553
|
+
if (!planDestSet.has(target)) continue // 我们改了自己的名字,没占用这个路径 → 不动用户文件
|
|
554
|
+
const rel = path.relative(opts.dir, target)
|
|
555
|
+
const to = path.join(stashRoot, rel)
|
|
556
|
+
await mkdir(path.dirname(to), { recursive: true })
|
|
557
|
+
await rename(target, to)
|
|
558
|
+
stashed.push({ from: target, to })
|
|
559
|
+
}
|
|
435
560
|
}
|
|
436
|
-
|
|
561
|
+
|
|
562
|
+
// 5.4 提交:逐个 rename 到最终位置
|
|
563
|
+
for (const item of plan) {
|
|
564
|
+
const dest = path.join(opts.dir, item.finalRel)
|
|
565
|
+
if (!dest.startsWith(opts.dir + path.sep)) throw new Error(`拒绝写入目标目录之外:${dest}`)
|
|
566
|
+
let dir = path.dirname(dest)
|
|
567
|
+
const missingDirs = []
|
|
568
|
+
while (!existsSync(dir)) {
|
|
569
|
+
missingDirs.unshift(dir)
|
|
570
|
+
const parent = path.dirname(dir)
|
|
571
|
+
if (parent === dir) break
|
|
572
|
+
dir = parent
|
|
573
|
+
}
|
|
574
|
+
for (const d of missingDirs) {
|
|
575
|
+
await mkdir(d)
|
|
576
|
+
madeDirs.push(d)
|
|
577
|
+
}
|
|
578
|
+
await rename(path.join(stageRoot, item.finalRel), dest)
|
|
579
|
+
committed.push(dest)
|
|
580
|
+
// 仅测试用故障注入:验证"中途失败也能完整回滚"
|
|
581
|
+
const failAfter = Number(process.env.ATP_FAIL_AFTER_COMMITS ?? 0)
|
|
582
|
+
if (failAfter > 0 && committed.length === failAfter) {
|
|
583
|
+
throw new Error(`injected failure after ${failAfter} commits (ATP_FAIL_AFTER_COMMITS)`)
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
// 5.5 校验:所有文件都在位,且核心与分册都不缺
|
|
588
|
+
const missingAfter = plan.filter((i) => !existsSync(path.join(opts.dir, i.finalRel)))
|
|
589
|
+
if (missingAfter.length > 0) {
|
|
590
|
+
throw new Error(`提交后缺少 ${missingAfter.length} 个文件:${missingAfter.map((i) => i.finalRel).join(', ')}`)
|
|
591
|
+
}
|
|
592
|
+
const coreRelFinal = plan.find((i) => i.srcRel === ROOT_FILE)?.finalRel
|
|
593
|
+
const docRelFinal = plan.find((i) => i.srcRel === `${ROOT_DIR}/workflow/rules/03-流水线.md`)?.finalRel
|
|
594
|
+
if (coreRelFinal === undefined || docRelFinal === undefined || !existsSync(path.join(opts.dir, coreRelFinal)) || !existsSync(path.join(opts.dir, docRelFinal))) {
|
|
595
|
+
throw new Error('核心与分册没有同时落地')
|
|
596
|
+
}
|
|
597
|
+
} catch (error) {
|
|
598
|
+
console.error(C.red(`\n✗ 安装失败,正在回滚:${error.message}`))
|
|
599
|
+
await rollback()
|
|
600
|
+
console.error(C.red(' 已回滚到安装前状态,未留下半成品。'))
|
|
601
|
+
process.exit(1)
|
|
437
602
|
}
|
|
438
603
|
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
console.log(C.
|
|
604
|
+
await cleanupStage()
|
|
605
|
+
console.log(C.green(`\n✓ 完成:${plan.length} 个文件已原子安装到 ${opts.dir}`))
|
|
606
|
+
if (existingRenames.length > 0) console.log(C.yellow(` 你的原文件已保留为:${existingRenames.map((r) => path.relative(opts.dir, r.to)).join(',')}`))
|
|
607
|
+
console.log(C.dim(' 提示:若项目已有自己的协作规则,建议把它们合并进新的 AGENTS.md,而不是两份并存。'))
|
|
442
608
|
}
|
|
443
609
|
|
|
444
610
|
await main()
|
package/cordis.patch.yml
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
# dsh-agent-team-playbook —— DSH bundle patch
|
|
2
2
|
#
|
|
3
|
-
# 挂载本插件,注册只读命令 `/
|
|
4
|
-
# /
|
|
5
|
-
# /
|
|
6
|
-
# /
|
|
7
|
-
# /
|
|
3
|
+
# 挂载本插件,注册只读命令 `/atp`:
|
|
4
|
+
# /atp 规程摘要 + 安装方式
|
|
5
|
+
# /atp core 核心全文(读完即在本轮遵循)
|
|
6
|
+
# /atp paths 分册绝对路径
|
|
7
|
+
# /atp help 用法
|
|
8
8
|
#
|
|
9
9
|
# 本插件不写任何文件,也不挂载 skill:
|
|
10
10
|
# · 不挂 skill 是因为 skill 的目录摘要会常驻占用上下文(约 300 tokens/轮),
|
package/lib/index.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* dsh-agent-team-playbook — DSH 命令插件(Host 半身)
|
|
3
3
|
*
|
|
4
|
-
* 提供一个只读命令 `/agent-team
|
|
5
|
-
* /
|
|
6
|
-
* /
|
|
7
|
-
* /
|
|
8
|
-
* /
|
|
4
|
+
* 提供一个只读命令 `/atp`(agent-team-playbook 缩写),把多智能体协作规程带进对话:
|
|
5
|
+
* /atp 摘要(档位、四问、收尾三问、如何装成常驻)
|
|
6
|
+
* /atp core 核心全文(读完即在本轮遵循)
|
|
7
|
+
* /atp paths 各分册的绝对路径
|
|
8
|
+
* /atp help 用法
|
|
9
|
+
*
|
|
10
|
+
* 命令名刻意取得短而少见:DSH 对重名命令是**硬报错**(plugin 加载失败),
|
|
11
|
+
* 用 `agent-team` 这种通用词容易被其他插件或后续官方功能占用。
|
|
9
12
|
*
|
|
10
13
|
* 本插件**不写任何文件**。要让规程成为每个 agent 每轮的固定上下文,
|
|
11
14
|
* 用 CLI 安装:`npx dsh-agent-team-playbook init`(项目级)或 `init --global`(全局)。
|
|
@@ -30,7 +33,7 @@ const PACKAGE_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)),
|
|
|
30
33
|
const PLAYBOOK_DIR = path.join(PACKAGE_ROOT, 'playbook')
|
|
31
34
|
|
|
32
35
|
const USAGE = [
|
|
33
|
-
'用法:/
|
|
36
|
+
'用法:/atp [core|paths|help]',
|
|
34
37
|
'',
|
|
35
38
|
' (无参数) 规程摘要与安装方式',
|
|
36
39
|
' core 输出核心全文(约 5k 字符),读完即在本轮按它协作',
|
|
@@ -65,7 +68,7 @@ function readPlaybook(rel) {
|
|
|
65
68
|
}
|
|
66
69
|
}
|
|
67
70
|
|
|
68
|
-
/** 列出分册路径(用于 /
|
|
71
|
+
/** 列出分册路径(用于 /atp paths)。 */
|
|
69
72
|
function renderPaths() {
|
|
70
73
|
const rulesDir = path.join(PLAYBOOK_DIR, 'docs/workflow/rules')
|
|
71
74
|
const lines = [`核心:\`${path.join(PLAYBOOK_DIR, 'AGENTS.md')}\``, '', '分册:']
|
|
@@ -137,8 +140,8 @@ function handleCommand(invocation) {
|
|
|
137
140
|
export function apply(ctx) {
|
|
138
141
|
ctx.commands.register({
|
|
139
142
|
definitionId: 'dsh-agent-team-playbook',
|
|
140
|
-
name: '
|
|
141
|
-
description: '
|
|
143
|
+
name: 'atp',
|
|
144
|
+
description: '多智能体协作规程(agent-team-playbook):摘要 / 核心全文 / 分册路径(只读)',
|
|
142
145
|
input: { hint: '[core|paths|help]' },
|
|
143
146
|
handler: handleCommand,
|
|
144
147
|
})
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-agent-team-playbook",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "DeepSeek Harness
|
|
3
|
+
"version": "1.24.0",
|
|
4
|
+
"description": "DeepSeek Harness plugin that brings an executable multi-agent collaboration workflow (charter, triage tiers, six-stage pipeline, audit gates, experience library): a read-only /agent-team command plus a collision-aware installer for the documents.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "ChongCyrus",
|
|
@@ -42,8 +42,8 @@
|
|
|
42
42
|
"node": ">=20"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
|
-
"sync": "node scripts/sync-
|
|
46
|
-
"check": "node scripts/sync-
|
|
47
|
-
"prepack": "node scripts/sync-
|
|
45
|
+
"sync": "node scripts/sync-playbook.mjs",
|
|
46
|
+
"check": "node scripts/sync-playbook.mjs --check",
|
|
47
|
+
"prepack": "node scripts/sync-playbook.mjs"
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
* 绝不碰手写文件。
|
|
11
11
|
*
|
|
12
12
|
* 用法:
|
|
13
|
-
* node scripts/sync-
|
|
14
|
-
* node scripts/sync-
|
|
13
|
+
* node scripts/sync-playbook.mjs # 生成/更新
|
|
14
|
+
* node scripts/sync-playbook.mjs --check # 只校验,不一致则退出码 1
|
|
15
15
|
*/
|
|
16
16
|
import { readFile, writeFile, mkdir, rm, readdir } from 'node:fs/promises'
|
|
17
17
|
import { existsSync } from 'node:fs'
|
|
@@ -20,7 +20,7 @@ import { fileURLToPath } from 'node:url'
|
|
|
20
20
|
|
|
21
21
|
const ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
|
|
22
22
|
const DEST_DIR = path.join(ROOT, 'playbook')
|
|
23
|
-
const MANIFEST = path.join(ROOT, '.
|
|
23
|
+
const MANIFEST = path.join(ROOT, '.playbook-sync.json')
|
|
24
24
|
const CHECK = process.argv.includes('--check')
|
|
25
25
|
|
|
26
26
|
/** 需要同步的源文件(相对仓库根),保持目录结构以让文档内的相对引用继续有效。 */
|
|
@@ -63,7 +63,7 @@ async function readManifest() {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
async function main() {
|
|
66
|
-
await mkdir(DEST_DIR, { recursive: true })
|
|
66
|
+
if (!CHECK) await mkdir(DEST_DIR, { recursive: true })
|
|
67
67
|
|
|
68
68
|
const wanted = new Set(SOURCES)
|
|
69
69
|
for (const dir of SOURCE_DIRS) {
|
|
@@ -108,12 +108,28 @@ async function main() {
|
|
|
108
108
|
}
|
|
109
109
|
|
|
110
110
|
if (CHECK) {
|
|
111
|
-
|
|
111
|
+
// 目标目录里不该有清单外的文件(防残留,例如已废弃的 SKILL.md)
|
|
112
|
+
const unexpected = []
|
|
113
|
+
const walkDest = async (relDir) => {
|
|
114
|
+
const abs = path.join(DEST_DIR, relDir)
|
|
115
|
+
if (!existsSync(abs)) return
|
|
116
|
+
for (const entry of await readdir(abs, { withFileTypes: true })) {
|
|
117
|
+
const rel = relDir === '' ? entry.name : path.posix.join(relDir, entry.name)
|
|
118
|
+
if (entry.isDirectory()) await walkDest(rel)
|
|
119
|
+
else if (!wanted.has(rel)) unexpected.push(rel)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
await walkDest('')
|
|
123
|
+
const problems = [
|
|
124
|
+
...missing,
|
|
125
|
+
...drifted.map((rel) => `未同步:${rel}`),
|
|
126
|
+
...unexpected.map((rel) => `多余文件:${rel}`),
|
|
127
|
+
]
|
|
112
128
|
if (stale.length > 0) problems.push(`残留:${stale.join(', ')}`)
|
|
113
129
|
if (problems.length > 0) {
|
|
114
130
|
console.error('✗ playbook/ 与源文档不一致:')
|
|
115
131
|
for (const p of problems) console.error(' -', p)
|
|
116
|
-
console.error(' 修复:node scripts/sync-
|
|
132
|
+
console.error(' 修复:node scripts/sync-playbook.mjs')
|
|
117
133
|
process.exit(1)
|
|
118
134
|
}
|
|
119
135
|
console.log(`✓ playbook 内容与源文档一致(${files.length} 个文件)`)
|