dsh-workplan 3.0.3 → 3.0.6
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 +54 -0
- package/README.en.md +15 -7
- package/README.md +18 -8
- package/README.zh.md +8 -7
- package/client.js +181 -27
- package/docs/release-notes-3.0.3.md +7 -0
- package/lib/approval-gate.js +79 -3
- package/lib/client.js +181 -27
- package/lib/comm-format.js +1 -1
- package/lib/dept-journal.js +223 -6
- package/lib/host.js +199 -7
- package/lib/lessons.js +83 -15
- package/lib/llm-rules.js +3 -2
- package/lib/notice-ledger.js +238 -0
- package/lib/plan.js +120 -3
- package/lib/runtime.js +440 -28
- package/lib/three-phase.js +6 -2
- package/lib/understanding.js +85 -6
- package/lib/v3-contract.js +45 -12
- package/package.json +1 -1
- package/src/approval-gate.ts +77 -3
- package/src/client.ts +178 -24
- package/src/comm-format.ts +1 -1
- package/src/dept-journal.ts +219 -7
- package/src/host.ts +199 -7
- package/src/lessons.ts +88 -15
- package/src/llm-rules.ts +3 -2
- package/src/notice-ledger.ts +251 -0
- package/src/plan.ts +124 -3
- package/src/runtime.ts +482 -26
- package/src/three-phase.ts +6 -2
- package/src/understanding.ts +100 -6
- package/src/v3-contract.ts +46 -13
package/src/dept-journal.ts
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
* 由 channel-expert 按契约 §3 实现)。
|
|
17
17
|
*/
|
|
18
18
|
import { randomBytes } from 'node:crypto'
|
|
19
|
-
import { appendFileSync, existsSync, mkdirSync, readFileSync } from 'node:fs'
|
|
19
|
+
import { appendFileSync, chmodSync, closeSync, existsSync, lstatSync, mkdirSync, openSync, readdirSync, readFileSync, readSync, statSync, unlinkSync } from 'node:fs'
|
|
20
20
|
import { homedir } from 'node:os'
|
|
21
21
|
import { dirname, join } from 'node:path'
|
|
22
22
|
|
|
@@ -285,11 +285,12 @@ export function deptJournalPath(planId: string): string {
|
|
|
285
285
|
|
|
286
286
|
/**
|
|
287
287
|
* 追加一条留痕(契约 §2):先 validate,再 mkdirSync(recursive)+appendFileSync。
|
|
288
|
-
* 任何失败(校验不过 / planId 非法 /
|
|
288
|
+
* 任何失败(校验不过 / planId 非法 / 写盘失败 / 回读不一致)显式返回
|
|
289
|
+
* JOURNAL_WRITE_FAILED / JOURNAL_READBACK_FAILED,绝不静默。
|
|
289
290
|
*/
|
|
290
291
|
export function appendDeptMessage(
|
|
291
292
|
msg: unknown,
|
|
292
|
-
): { ok: true; path: string } | { ok: false; code: 'JOURNAL_WRITE_FAILED'; error: string } {
|
|
293
|
+
): { ok: true; path: string } | { ok: false; code: 'JOURNAL_WRITE_FAILED' | 'JOURNAL_READBACK_FAILED'; error: string } {
|
|
293
294
|
const verdict = validateDeptMessage(msg)
|
|
294
295
|
if (!verdict.ok) return { ok: false, code: 'JOURNAL_WRITE_FAILED', error: verdict.error }
|
|
295
296
|
let path: string
|
|
@@ -299,25 +300,157 @@ export function appendDeptMessage(
|
|
|
299
300
|
return { ok: false, code: 'JOURNAL_WRITE_FAILED', error: errorText(error) }
|
|
300
301
|
}
|
|
301
302
|
try {
|
|
302
|
-
|
|
303
|
-
|
|
303
|
+
/*
|
|
304
|
+
* [安全加固] 防符号链接投毒:journal 文件若是符号链接则跳过写入并告警——
|
|
305
|
+
* 绝不跟随链接把审计内容写到链接指向的任意路径;失败显式返回(绝不静默)。
|
|
306
|
+
* lstatSync 抛错(文件不存在等)属正常首写场景,交给 appendFileSync 处理。
|
|
307
|
+
*/
|
|
308
|
+
try {
|
|
309
|
+
if (lstatSync(path).isSymbolicLink()) {
|
|
310
|
+
console.warn(`dsh-workplan: journal 文件是符号链接,跳过写入 path=${path}`)
|
|
311
|
+
return {
|
|
312
|
+
ok: false,
|
|
313
|
+
code: 'JOURNAL_WRITE_FAILED',
|
|
314
|
+
error: `JOURNAL_WRITE_FAILED: journal 文件是符号链接,已跳过写入 path=${path}(防链接投毒)。`,
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
} catch {
|
|
318
|
+
/* 文件不存在(首写)或 lstat 失败:继续走正常写入,错误由 appendFileSync 显式上报。 */
|
|
319
|
+
}
|
|
320
|
+
mkdirSync(dirname(path), { recursive: true, mode: 0o700 })
|
|
321
|
+
appendFileSync(path, `${JSON.stringify(verdict.value)}\n`, { encoding: 'utf8', mode: 0o600 })
|
|
322
|
+
try {
|
|
323
|
+
// 既有文件权限收紧为 0o600(best-effort;新建文件已由 mode 覆盖)。
|
|
324
|
+
chmodSync(path, 0o600)
|
|
325
|
+
} catch {
|
|
326
|
+
/* best-effort:权限收紧失败不阻断追加。 */
|
|
327
|
+
}
|
|
304
328
|
} catch (error) {
|
|
305
329
|
return { ok: false, code: 'JOURNAL_WRITE_FAILED', error: errorText(error) }
|
|
306
330
|
}
|
|
331
|
+
/* [T3][回读校验] 写后读回比对关键字段(见下方 helper 注释),不一致绝不静默。 */
|
|
332
|
+
const readback = verifyJournalAppendReadback(path, verdict.value)
|
|
333
|
+
if (!readback.ok) return { ok: false, code: 'JOURNAL_READBACK_FAILED', error: readback.error }
|
|
307
334
|
return { ok: true, path }
|
|
308
335
|
}
|
|
309
336
|
|
|
337
|
+
/*
|
|
338
|
+
* [T3][回读校验] journal 追加写后读回。依据:reports/wp-member-expert-persist-call-T1.json
|
|
339
|
+
* 写盘点⑦(dept-journal.ts:290-308;2026-11-17 复核 mkdirSync:302、appendFileSync:303
|
|
340
|
+
* 无漂移)与 reports/wp-member-expert-storage-T1-storage.json(落盘路径
|
|
341
|
+
* <journalDir>/<planId>.jsonl,事件流追加、非整文件快照)。
|
|
342
|
+
* appendFileSync 成功后读回文件末尾窗口,JSON 解析后比对关键字段(v/id/planId/
|
|
343
|
+
* subtaskId/kind/ts/body 头 120 字摘要)。找不到匹配行 → 返回显式错误码
|
|
344
|
+
* JOURNAL_READBACK_FAILED(本模块「绝不静默」契约=显式错误码+处置指引,与
|
|
345
|
+
* JOURNAL_WRITE_FAILED 同口径;负责人硬性要求③的「抛显式错误」在本模块以
|
|
346
|
+
* 显式错误码实现,错误信息含文件路径与期望/实际摘要及指引:重试或让主 Agent
|
|
347
|
+
* 手动制定计划)。纯读校验、不写盘 → 幂等(硬性要求④)。
|
|
348
|
+
*
|
|
349
|
+
* [复审修复][O(n²)→O(窗口)] journal 默认永久保留,文件会涨到 MB 级;旧实现每次
|
|
350
|
+
* 追加后 readFileSync 整个文件逐行 parse,热路径每次请示/指示/完成/预估都全文件
|
|
351
|
+
* 重读。改为只读末尾一个窗口(JOURNAL_READBACK_WINDOW_BYTES):
|
|
352
|
+
* - O_APPEND 保证刚写入的行必在文件末尾;窗口 ≥ 单行最大体积(body ≤4000 字符 +
|
|
353
|
+
* data JSON ≤4000 字符,单 line 远小于 64KB)即可命中;
|
|
354
|
+
* - 窗口起点可能落在行中间 → 首行 parse 失败直接跳过该坏行继续;
|
|
355
|
+
* - 并发追加交错不会把新行挤出窗口(需同一瞬时有 64KB 更新行写入才会,
|
|
356
|
+
* 概率可忽略;且失败显式可重试)。
|
|
357
|
+
*/
|
|
358
|
+
function deptMessageKeyDigest(record: any): string {
|
|
359
|
+
return JSON.stringify({
|
|
360
|
+
v: record?.v,
|
|
361
|
+
id: record?.id,
|
|
362
|
+
planId: record?.planId,
|
|
363
|
+
subtaskId: record?.subtaskId,
|
|
364
|
+
kind: record?.kind,
|
|
365
|
+
ts: record?.ts,
|
|
366
|
+
bodyHead: String(record?.body ?? '').slice(0, 120),
|
|
367
|
+
})
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/** 回读窗口字节数:64KB。单行最大体积(body 4000 + data JSON 4000 + 结构开销)远小于此。 */
|
|
371
|
+
const JOURNAL_READBACK_WINDOW_BYTES = 64 * 1024
|
|
372
|
+
|
|
373
|
+
/** 只读文件末尾一个窗口(按字节);文件小于窗口则全读。读失败由调用方捕获。 */
|
|
374
|
+
function readJournalTailWindow(path: string, windowBytes: number): string {
|
|
375
|
+
const { size } = statSync(path)
|
|
376
|
+
const start = Math.max(0, size - windowBytes)
|
|
377
|
+
const length = size - start
|
|
378
|
+
if (length <= 0) return ''
|
|
379
|
+
const buffer = Buffer.alloc(length)
|
|
380
|
+
const fd = openSync(path, 'r')
|
|
381
|
+
try {
|
|
382
|
+
let offset = 0
|
|
383
|
+
while (offset < length) {
|
|
384
|
+
const read = readSync(fd, buffer, offset, length - offset, start + offset)
|
|
385
|
+
if (read <= 0) break
|
|
386
|
+
offset += read
|
|
387
|
+
}
|
|
388
|
+
} finally {
|
|
389
|
+
closeSync(fd)
|
|
390
|
+
}
|
|
391
|
+
return buffer.toString('utf8')
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function verifyJournalAppendReadback(
|
|
395
|
+
path: string,
|
|
396
|
+
expected: DeptMessage,
|
|
397
|
+
): { ok: true } | { ok: false; error: string } {
|
|
398
|
+
let text: string
|
|
399
|
+
try {
|
|
400
|
+
text = readJournalTailWindow(path, JOURNAL_READBACK_WINDOW_BYTES)
|
|
401
|
+
} catch (error) {
|
|
402
|
+
return {
|
|
403
|
+
ok: false,
|
|
404
|
+
error: `dsh-workplan: journal 回读校验读不到文件 path=${path}(${errorText(error)})。`
|
|
405
|
+
+ '请重试;若多次失败请让主 Agent 手动制定计划。',
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
const lines = text.split('\n').filter((line) => line.trim() !== '')
|
|
409
|
+
const want = deptMessageKeyDigest(expected)
|
|
410
|
+
/*
|
|
411
|
+
* 只扫末尾窗口:窗口首行可能被截断(窗口从行中间字节开始),parse 失败跳过该
|
|
412
|
+
* 坏行继续;新追加的行(digest 匹配)由 O_APPEND 保证在文件末尾、必在窗口内。
|
|
413
|
+
*/
|
|
414
|
+
for (const line of lines) {
|
|
415
|
+
let parsed: any = null
|
|
416
|
+
try {
|
|
417
|
+
parsed = JSON.parse(line)
|
|
418
|
+
} catch {
|
|
419
|
+
continue
|
|
420
|
+
}
|
|
421
|
+
if (deptMessageKeyDigest(parsed) === want) return { ok: true }
|
|
422
|
+
}
|
|
423
|
+
const tail = lines.slice(-5)
|
|
424
|
+
const actual = tail
|
|
425
|
+
.map((line) => {
|
|
426
|
+
try {
|
|
427
|
+
return deptMessageKeyDigest(JSON.parse(line))
|
|
428
|
+
} catch {
|
|
429
|
+
return '<不可解析行>'
|
|
430
|
+
}
|
|
431
|
+
})
|
|
432
|
+
.join(' | ')
|
|
433
|
+
return {
|
|
434
|
+
ok: false,
|
|
435
|
+
error: `dsh-workplan: journal 落盘回读校验失败 path=${path}:写入内容与读回内容关键字段不一致。`
|
|
436
|
+
+ `期望=${want.slice(0, 300)} 实际(末${tail.length}行)=${actual.slice(0, 300)}。`
|
|
437
|
+
+ '请重试;若多次失败请让主 Agent 手动制定计划。',
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
310
441
|
export interface DeptReadFilter {
|
|
311
442
|
kind?: DeptKind
|
|
312
443
|
since?: number
|
|
313
444
|
limit?: number
|
|
445
|
+
/** true=取最近 limit 条(尾部);默认 false=按追加序取最早 limit 条。 */
|
|
446
|
+
newest?: boolean
|
|
314
447
|
}
|
|
315
448
|
|
|
316
449
|
/**
|
|
317
450
|
* 按追加序回放留痕(契约 §2):
|
|
318
451
|
* - 文件不存在 → 空列表(ok:true);
|
|
319
452
|
* - 坏行(JSON 损坏或校验不过)计入 skipped,不静默吞;
|
|
320
|
-
* - kind/since 过滤后按 limit 截取:默认 500、上限 5000;
|
|
453
|
+
* - kind/since 过滤后按 limit 截取:默认 500、上限 5000;newest=true 取最近,否则取最早;
|
|
321
454
|
* - planId 非法或读盘失败 → 显式 JOURNAL_READ_FAILED。
|
|
322
455
|
*/
|
|
323
456
|
export function readDeptMessages(
|
|
@@ -369,5 +502,84 @@ export function readDeptMessages(
|
|
|
369
502
|
if (since !== null && message.ts < since) continue
|
|
370
503
|
matched.push(message)
|
|
371
504
|
}
|
|
372
|
-
return { ok: true, messages: matched.slice(0, limit), skipped }
|
|
505
|
+
return { ok: true, messages: filter?.newest ? matched.slice(-limit) : matched.slice(0, limit), skipped }
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/**
|
|
509
|
+
* [3.0.6][第 2 批] 留痕目录的可选保留策略(默认关闭)。
|
|
510
|
+
*
|
|
511
|
+
* 审计证据不该被默认删除,所以本函数只在显式配置
|
|
512
|
+
* `journalRetentionDays > 0`(或环境变量
|
|
513
|
+
* `DSH_WORKPLAN_JOURNAL_RETENTION_DAYS`)时被调用。它只删除同时满足以下
|
|
514
|
+
* 条件的文件:目录里直接的 `.jsonl` 普通文件、文件名是合法 planId、
|
|
515
|
+
* mtime 早于保留天数。活跃计划会持续追加,mtime 保持新,因此不会被误删。
|
|
516
|
+
*
|
|
517
|
+
* 单次最多删 `maxFiles`(默认 200,上限 1000)个最旧文件,避免一次启动
|
|
518
|
+
* 长时间阻塞;删除失败不再静默跳过——逐个 console.warn 一行并收集进
|
|
519
|
+
* 返回值的 `errors[]`(ok 保持 true,兼容既有调用方)。
|
|
520
|
+
*
|
|
521
|
+
* [复审修复] `excludePlanIds`(可选):文件名(去 .jsonl)在集合内的一律
|
|
522
|
+
* 跳过删除,防「活跃但安静」(久无新消息、mtime 变旧)的计划被误删。
|
|
523
|
+
*/
|
|
524
|
+
export function pruneJournal(options: {
|
|
525
|
+
retentionDays: number
|
|
526
|
+
now?: number
|
|
527
|
+
maxFiles?: number
|
|
528
|
+
/** 活跃计划保护名单:journal 文件名(去 .jsonl)在集合内的跳过删除。 */
|
|
529
|
+
excludePlanIds?: ReadonlySet<string>
|
|
530
|
+
}): { ok: boolean; scanned: number; deleted: string[]; error?: string; errors?: string[] } {
|
|
531
|
+
const days = Number(options?.retentionDays)
|
|
532
|
+
if (!Number.isFinite(days) || days <= 0) return { ok: true, scanned: 0, deleted: [] }
|
|
533
|
+
const dir = resolveJournalDir()
|
|
534
|
+
let names: string[]
|
|
535
|
+
try {
|
|
536
|
+
names = readdirSync(dir)
|
|
537
|
+
} catch (error: any) {
|
|
538
|
+
if (error?.code === 'ENOENT') return { ok: true, scanned: 0, deleted: [] }
|
|
539
|
+
return { ok: false, scanned: 0, deleted: [], error: errorText(error) }
|
|
540
|
+
}
|
|
541
|
+
const now = Number.isFinite(options?.now) ? Number(options.now) : Date.now()
|
|
542
|
+
const cutoff = now - days * 24 * 60 * 60 * 1000
|
|
543
|
+
const maxFiles = Math.max(0, Math.min(1000, Math.trunc(options?.maxFiles ?? 200)))
|
|
544
|
+
const exclude = options?.excludePlanIds
|
|
545
|
+
const candidates: Array<{ name: string; path: string; mtimeMs: number }> = []
|
|
546
|
+
for (const name of names) {
|
|
547
|
+
if (!name.endsWith('.jsonl')) continue
|
|
548
|
+
const planId = name.slice(0, -'.jsonl'.length)
|
|
549
|
+
if (!PLAN_ID_PATTERN.test(planId)) continue
|
|
550
|
+
// 活跃但安静的保护名单:在册计划即使 mtime 超期也不删。
|
|
551
|
+
if (exclude?.has(planId)) continue
|
|
552
|
+
const path = join(dir, name)
|
|
553
|
+
try {
|
|
554
|
+
const stat = lstatSync(path)
|
|
555
|
+
// 只删普通文件;符号链接一律跳过(防越界删除)。
|
|
556
|
+
if (!stat.isFile() || stat.isSymbolicLink()) continue
|
|
557
|
+
if (stat.mtimeMs >= cutoff) continue
|
|
558
|
+
candidates.push({ name, path, mtimeMs: stat.mtimeMs })
|
|
559
|
+
} catch {
|
|
560
|
+
// 单个文件 stat 失败不影响其他文件,最后以 scanned 如实反映总数。
|
|
561
|
+
continue
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
candidates.sort((a, b) => a.mtimeMs - b.mtimeMs)
|
|
565
|
+
const deleted: string[] = []
|
|
566
|
+
const errors: string[] = []
|
|
567
|
+
for (const candidate of candidates.slice(0, maxFiles)) {
|
|
568
|
+
try {
|
|
569
|
+
unlinkSync(candidate.path)
|
|
570
|
+
deleted.push(candidate.name)
|
|
571
|
+
} catch (error) {
|
|
572
|
+
// 单个 unlink 失败不再静默:收集原因 + 逐条 warn(ok 保持 true 兼容)。
|
|
573
|
+
const reason = `${candidate.name}: ${errorText(error)}`
|
|
574
|
+
errors.push(reason)
|
|
575
|
+
console.warn(`dsh-workplan: journal 保留清理删除失败 ${reason} path=${candidate.path}`)
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
const result: { ok: boolean; scanned: number; deleted: string[]; error?: string; errors?: string[] } = {
|
|
579
|
+
ok: true,
|
|
580
|
+
scanned: names.length,
|
|
581
|
+
deleted,
|
|
582
|
+
}
|
|
583
|
+
if (errors.length > 0) result.errors = errors
|
|
584
|
+
return result
|
|
373
585
|
}
|
package/src/host.ts
CHANGED
|
@@ -24,6 +24,7 @@ import {
|
|
|
24
24
|
} from './runtime.js'
|
|
25
25
|
import { DEPARTMENT_IDS, isRecord } from './plan.js'
|
|
26
26
|
import { autoResumeEnabledFrom } from './auto-resume.js'
|
|
27
|
+
import { pruneJournal } from './dept-journal.js'
|
|
27
28
|
|
|
28
29
|
/*
|
|
29
30
|
* [1.8.14] Identity of the modules this process actually loaded (N1).
|
|
@@ -66,6 +67,14 @@ const LOADED_BUILD = (() => {
|
|
|
66
67
|
*/
|
|
67
68
|
const PROCESS_STARTED_AT = Date.now() - Math.round(process.uptime() * 1000)
|
|
68
69
|
|
|
70
|
+
/*
|
|
71
|
+
* [3.0.6][第 2 批] 静态图表资产只读一次。
|
|
72
|
+
* 旧实现每次请求都 readFileSync 210KB(看板每次加载都拉一次),在请求路径上
|
|
73
|
+
* 做磁盘 I/O 没有必要:vendor 文件随插件发布、运行期不会变。缓存 Buffer 后
|
|
74
|
+
* 后续请求直接复用;读取失败不缓存,下次仍会重试。
|
|
75
|
+
*/
|
|
76
|
+
let chartVendorBytes: Buffer | null = null
|
|
77
|
+
|
|
69
78
|
function phaseLabel(phase: any) {
|
|
70
79
|
if (phase === 'paused') return '本轮预算用尽,可续跑'
|
|
71
80
|
if (phase === 'blocked') return '卡住了'
|
|
@@ -94,7 +103,7 @@ const PROMPT_SECTION = [
|
|
|
94
103
|
'3.0.0 任务书质量要求(用户要求 3/6):插件派给部门负责人的每个子任务都携带 qualityRequirements(结果质量要求),部门负责人派给专家的每条任务也必须携带 qualityRequirements。负责人→专家缺它平台直接拒绝派发(DELEGATE_QUALITY_REQUIREMENTS_MISSING);主 Agent→部门负责人缺它则该子任务在派发时被直接拒发(DISPATCH_QUALITY_REQUIREMENTS_MISSING,不启动部门 Agent),须 replan 补齐后重派。你审阅计划时先看每个子任务是否写了清楚的质量要求。',
|
|
95
104
|
'3.0.0 极简任务(用户要求 5,用户裁决 A=硬门槛):部门负责人派出的每条任务必须简单到像 1+1=2——一个动作、一个主产物、一项可独立验收,单句、≤300 字符、不含「并且/同时/另外/还要/以及/分别/顺便」等并列词;不满足即被拒绝并给出拆分提示(DELEGATE_TASK_NOT_ATOMIC)。',
|
|
96
105
|
'3.0.0 角色与汇报(用户要求 7/8/9/10/11/12):部门组员角色已取消,只保留部门负责人与全球顶尖专家(role 只允许 expert);专家完成后必须以 JSON 汇报(证据块里的 report:keyResults/keyInfo/importantInfo/leadRequirements,精简、禁止重复,F10);部门负责人必须全面汇总本部门全部专家汇报后,以 JSON 交回主 Agent(departmentSummary:keyResults/keyInfo/importantInfo/sources,精简、禁止累赘重复,F11);等所有部门的汇总都交回后,由你再全面、深入整理汇总成最终成果交付给用户(用户要求 12)。',
|
|
97
|
-
'3.0.
|
|
106
|
+
'3.0.6 版本(用户要求 13):本插件当前版本为 3.0.6。涉及版本号的对外表述一律写 3.0.6。',
|
|
98
107
|
/*
|
|
99
108
|
* [2.5.1] 主 Agent 两轮深度思考协议(用户 2026-11-11 补充要求):部门三层
|
|
100
109
|
* 执行者已有结算/再分配双门禁,主 Agent 自己也必须做到同一协议——提示词
|
|
@@ -256,6 +265,19 @@ export type WorkplanConfig = {
|
|
|
256
265
|
* 环境变量:`DSH_WORKPLAN_FAIL_CLOSED_ON_INCOMPLETE=1|0`。
|
|
257
266
|
*/
|
|
258
267
|
failClosedOnIncomplete?: boolean | 'auto'
|
|
268
|
+
/**
|
|
269
|
+
* [3.0.6][第 3 批①] `agent/turn-stopping` 等待在途规划/派发的上限(毫秒)。
|
|
270
|
+
* 默认 60 分钟;0 = 无限等待(旧行为)。只约束收尾等待,不构成执行时限。
|
|
271
|
+
*/
|
|
272
|
+
turnStoppingMaxWaitMs?: number
|
|
273
|
+
/**
|
|
274
|
+
* [3.0.6][第 2 批] 留痕保留天数。**0(默认)= 永久保留,不自动删除**。
|
|
275
|
+
*
|
|
276
|
+
* 审计证据默认不该被清理;只有部署方明确配置正数(或环境变量
|
|
277
|
+
* `DSH_WORKPLAN_JOURNAL_RETENTION_DAYS`)时,才会在启动时和之后每 24 小时
|
|
278
|
+
* 删除超过该天数未更新的 `<planId>.jsonl`(单次最多 200 个最旧文件)。
|
|
279
|
+
*/
|
|
280
|
+
journalRetentionDays?: number
|
|
259
281
|
}
|
|
260
282
|
|
|
261
283
|
function createUpdateTool(runtime: any) {
|
|
@@ -748,8 +770,9 @@ function createEscalateTool(runtime: any) {
|
|
|
748
770
|
description:
|
|
749
771
|
'子 Agent 遇阻时向父会话上报请示,并把该子任务置为「待指示」。'
|
|
750
772
|
+ '第三层专家(叶子)同样可用:请示会经 delegate 登记落到所属部门行的第三层台账,由部门负责人用 dsh_workplan_instruct 答复。'
|
|
773
|
+
+ '3.0.5 硬规则:第三层专家上报后,部门负责人禁止另派给其他专家,必须梳理该问题与本部门全部任务执行状态、研究方案后,用 dsh_workplan_instruct 答复原专家,原专家继续执行。'
|
|
751
774
|
+ '遇阻(缺权限/缺输入/依赖产物缺失/需求自相矛盾/无法验证/同一命令连续两次失败/需要主 Agent 决定方向)时,'
|
|
752
|
-
+ '第一动作是调用本工具,然后停止推进、只做只读核对,等待主 Agent
|
|
775
|
+
+ '第一动作是调用本工具,然后停止推进、只做只读核对,等待主 Agent/负责人指示。'
|
|
753
776
|
+ '不需要外部决定时不要调用;调用后不要自行选定一个选项继续。',
|
|
754
777
|
parameters: {
|
|
755
778
|
type: 'object',
|
|
@@ -805,7 +828,7 @@ function createEscalateTool(runtime: any) {
|
|
|
805
828
|
`${String(result.subtaskId ?? '')} 已登记为「待指示」`
|
|
806
829
|
+ (result.alreadyEscalated ? '(此前已登记,未重复提交)' : '')
|
|
807
830
|
+ `:${String(result.question ?? '')}`
|
|
808
|
-
+ '|现在停止推进,只做只读核对;等待主 Agent
|
|
831
|
+
+ '|现在停止推进,只做只读核对;等待主 Agent/部门负责人指示后再继续。第三层请示在答复前禁止改派给其他专家。',
|
|
809
832
|
)
|
|
810
833
|
},
|
|
811
834
|
},
|
|
@@ -857,7 +880,8 @@ function createDelegateTool(runtime: any) {
|
|
|
857
880
|
+ '平台按当前模型家族合法集合自动钳制越界档并留痕,等级在其任务全生命周期生效。'
|
|
858
881
|
+ 'action="status" 查看梳理登记与下级进度。第三层专家叶子用 action="self_effort" 自配推理等级并提交执行前两轮深度思考产物'
|
|
859
882
|
+ '(先配置+先思考后开工:phase-0 以 low 启动,唯一职责=评估自身任务复杂度并提交 reasoningEffort+complexityRationale+deepThink{problemAnalysis,approach,approachBasis}+开工前预估 timeNotice{declaredMinutes:正整数分钟,reason?};'
|
|
860
|
-
+ '缺交/不合格 deepThink 硬拒 DEEP_THINK_MISSING/DEEP_THINK_INCOMPLETE、不接续;两项齐全登记后平台立即以自配档接续同一任务进入 phase-1,正式执行必须按第二轮方法流程步骤进行、全程少走弯路;成员自配为权威、分工表档仅建议)。'
|
|
883
|
+
+ '缺交/不合格 deepThink 硬拒 DEEP_THINK_MISSING/DEEP_THINK_INCOMPLETE、不接续;两项齐全登记后平台立即以自配档接续同一任务进入 phase-1,正式执行必须按第二轮方法流程步骤进行、全程少走弯路;成员自配为权威、分工表档仅建议)。'
|
|
884
|
+
+ '3.0.5 硬规则:任一第三层成员处于待指示(escalated)时,review/dispatch/direct 一律拒绝改派、重派或降级;负责人必须梳理该成员问题与本部门全部任务状态,研究方案后用 dsh_workplan_instruct 答复原成员,原成员继续执行。',
|
|
861
885
|
parameters: {
|
|
862
886
|
type: 'object',
|
|
863
887
|
properties: {
|
|
@@ -1220,6 +1244,7 @@ function createInstructTool(runtime: any) {
|
|
|
1220
1244
|
description:
|
|
1221
1245
|
'向一条「待指示」子任务下发决定:投递给对应子会话,并把该行恢复为执行中(closure=instructed)。'
|
|
1222
1246
|
+ '根会话可答复第一层部门行;部门负责人可答复自己派出的第三层专家(subtaskId 填自己的部门行 ID 或省略,用 member 或 sessionId 指定目标,二者至少其一)。'
|
|
1247
|
+
+ '3.0.5 硬规则:第三层只能答复已经提交请示的原专家,投递成功后必须原专家继续执行;待指示期间负责人不得 review/dispatch/direct 改派。'
|
|
1223
1248
|
+ '只有目标子会话的直接父会话可调用,非直系一律拒绝;根路径只接受 closure=escalated 的行。'
|
|
1224
1249
|
+ '投递失败时状态不变(仍是待指示),按返回的 hint 重试或改用 dsh_workplan_update 明确放弃。',
|
|
1225
1250
|
parameters: {
|
|
@@ -1875,8 +1900,45 @@ export function apply(ctx: any, config: WorkplanConfig = {}) {
|
|
|
1875
1900
|
config.failClosedOnIncomplete,
|
|
1876
1901
|
settingEnv('DSH_WORKPLAN_FAIL_CLOSED_ON_INCOMPLETE'),
|
|
1877
1902
|
),
|
|
1903
|
+
turnStoppingMaxWaitMs: numericSetting(config.turnStoppingMaxWaitMs, 'DSH_WORKPLAN_TURN_STOPPING_MAX_WAIT_MS', true),
|
|
1878
1904
|
})
|
|
1879
1905
|
|
|
1906
|
+
/*
|
|
1907
|
+
* [3.0.6][第 2 批] 可选留痕清理:默认关闭(0 = 永久保留证据)。
|
|
1908
|
+
* 只有显式配置正数时才启用;启动先跑一次,之后每 24 小时一次。
|
|
1909
|
+
*/
|
|
1910
|
+
const journalRetentionDays = (() => {
|
|
1911
|
+
const configured = numericSetting(config.journalRetentionDays, 'DSH_WORKPLAN_JOURNAL_RETENTION_DAYS', true)
|
|
1912
|
+
return configured !== undefined && configured > 0 ? Math.min(configured, 3650) : 0
|
|
1913
|
+
})()
|
|
1914
|
+
if (journalRetentionDays > 0) {
|
|
1915
|
+
const prune = () => {
|
|
1916
|
+
const result = pruneJournal({ retentionDays: journalRetentionDays, excludePlanIds: activePlanIdSet(runtime) })
|
|
1917
|
+
if (!result.ok) ctx.logger?.warn?.(`[dsh-workplan] journal 清理失败:${result.error}`)
|
|
1918
|
+
// 删除失败逐条 warn(ok 仍为 true,errors[] 是弱错误通道,不得静默)。
|
|
1919
|
+
if (Array.isArray(result.errors) && result.errors.length > 0) {
|
|
1920
|
+
for (const line of result.errors) console.warn(`[dsh-workplan] journal 清理异常:${String(line)}`)
|
|
1921
|
+
}
|
|
1922
|
+
return result
|
|
1923
|
+
}
|
|
1924
|
+
ctx.effect(() => {
|
|
1925
|
+
/*
|
|
1926
|
+
* [复审修复] 首次清理从激活同步路径挪到 10s 后:激活是同步路径,不能
|
|
1927
|
+
* 当场扫盘(readdir/stat 大目录会拖住插件注册)。卸载时清除——
|
|
1928
|
+
* journalRetentionTimer 同款的 ctx.effect 清理模式。
|
|
1929
|
+
*/
|
|
1930
|
+
const firstPruneTimer = setTimeout(prune, 10_000)
|
|
1931
|
+
if (typeof firstPruneTimer?.unref === 'function') firstPruneTimer.unref()
|
|
1932
|
+
return () => clearTimeout(firstPruneTimer)
|
|
1933
|
+
})
|
|
1934
|
+
ctx.effect(() => {
|
|
1935
|
+
// 卫生类:不触发只少一次清理,不得拖住正在关闭的宿主。
|
|
1936
|
+
const journalRetentionTimer = setInterval(prune, 24 * 60 * 60 * 1000)
|
|
1937
|
+
if (typeof journalRetentionTimer?.unref === 'function') journalRetentionTimer.unref()
|
|
1938
|
+
return () => clearInterval(journalRetentionTimer)
|
|
1939
|
+
})
|
|
1940
|
+
}
|
|
1941
|
+
|
|
1880
1942
|
ctx.effect(() =>
|
|
1881
1943
|
ctx.systemPrompt.section({
|
|
1882
1944
|
name: 'dsh-workplan',
|
|
@@ -1989,7 +2051,10 @@ export function apply(ctx: any, config: WorkplanConfig = {}) {
|
|
|
1989
2051
|
return
|
|
1990
2052
|
}
|
|
1991
2053
|
try {
|
|
1992
|
-
|
|
2054
|
+
if (!chartVendorBytes) {
|
|
2055
|
+
chartVendorBytes = readFileSync(new URL('../vendor/chart.umd.js', import.meta.url))
|
|
2056
|
+
}
|
|
2057
|
+
const body = chartVendorBytes
|
|
1993
2058
|
res.statusCode = 200
|
|
1994
2059
|
res.setHeader('content-type', 'text/javascript; charset=utf-8')
|
|
1995
2060
|
res.setHeader('cache-control', 'public, max-age=3600')
|
|
@@ -2005,11 +2070,18 @@ export function apply(ctx: any, config: WorkplanConfig = {}) {
|
|
|
2005
2070
|
})
|
|
2006
2071
|
|
|
2007
2072
|
ctx.effect(() => () => runtime.dispose())
|
|
2073
|
+
|
|
2074
|
+
/*
|
|
2075
|
+
* [3.0.6][复审修复] 把 runtime 句柄交还给调用方:宿主契约不消费本返回值
|
|
2076
|
+
* (清理一律经上方 effect),测试与宿主编排需要观察 state.records /
|
|
2077
|
+
* 替换 startAutoDispatch 时可直接拿到同一实例,不必从工具闭包里挖。
|
|
2078
|
+
*/
|
|
2079
|
+
return runtime
|
|
2008
2080
|
}
|
|
2009
2081
|
|
|
2010
2082
|
export const meta = {
|
|
2011
2083
|
name,
|
|
2012
|
-
version: '3.0.
|
|
2084
|
+
version: '3.0.6',
|
|
2013
2085
|
promptOrder: PROMPT_ORDER,
|
|
2014
2086
|
routes: ['/dsh-workplan/plans', '/dsh-workplan/vendor/chart.umd.js'],
|
|
2015
2087
|
}
|
|
@@ -2030,7 +2102,14 @@ export function fileWriteGuardReason(
|
|
|
2030
2102
|
writeTargets(agent: any, args: any): string[]
|
|
2031
2103
|
},
|
|
2032
2104
|
) {
|
|
2033
|
-
|
|
2105
|
+
/*
|
|
2106
|
+
* [复审修复] bash 写保护绕过:shell 类工具的 command 字符串(重定向/heredoc/
|
|
2107
|
+
* tee/sed -i/cp/mv)此前因参数形状不认(无 file_path/path)而整类绕过闸门。
|
|
2108
|
+
* 先解析出 shell 写目标(非 shell 调用为 null),形状闸门与 shell 闸门至少
|
|
2109
|
+
* 命中其一才继续。
|
|
2110
|
+
*/
|
|
2111
|
+
const shellTargets = shellWriteTargets(exec)
|
|
2112
|
+
if (!isWriteShapedCall(exec) && shellTargets === null) return undefined
|
|
2034
2113
|
// The three historical write tools pass their arguments through untouched, so
|
|
2035
2114
|
// their behaviour stays exactly as before (argument-shape matching is only an
|
|
2036
2115
|
// EXTENSION: a tool that used to be denied is still denied the same way).
|
|
@@ -2043,9 +2122,47 @@ export function fileWriteGuardReason(
|
|
|
2043
2122
|
const extra = runtime.presentedWriteDenialReason(exec?.agent, { file_path: target })
|
|
2044
2123
|
if (typeof extra === 'string') return extra
|
|
2045
2124
|
}
|
|
2125
|
+
/*
|
|
2126
|
+
* [复审修复] shell 写目标:与上面同一判定出口——只拦「写入已交付文件」,
|
|
2127
|
+
* 纯读命令(cat/grep/less 该路径)解析不出写目标,天然放行。
|
|
2128
|
+
* 启发式局限(保守模式):只认 > / >> 重定向、tee、sed -i、mv/cp/install/dd
|
|
2129
|
+
* 的目标;命令拼接变量(cmd=path; $cmd)、eval、find -delete 等动态写法
|
|
2130
|
+
* 不在解析范围内——见 shellWriteTargets 注释。
|
|
2131
|
+
*/
|
|
2132
|
+
if (shellTargets) {
|
|
2133
|
+
for (const target of shellTargets) {
|
|
2134
|
+
const extra = runtime.presentedWriteDenialReason(exec?.agent, { file_path: target })
|
|
2135
|
+
if (typeof extra === 'string') return extra
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2046
2138
|
return undefined
|
|
2047
2139
|
}
|
|
2048
2140
|
|
|
2141
|
+
/*
|
|
2142
|
+
* [复审修复] 活跃 planId 集合:pruneJournal 只看 mtime,「活跃但安静」的计划
|
|
2143
|
+
* (久无新留痕、mtime 变旧)会被误删;把当前活跃 planId 传给 excludePlanIds
|
|
2144
|
+
* 即可保住。planning 在途时尚未铸出 planId(planId 在规划完成后才生成,占位
|
|
2145
|
+
* 记录 planId=null),因此 runtime.state.records 就是全部活跃 planId 的事实源。
|
|
2146
|
+
* 导出以便直接单测;读取失败返回空集——宁可少排除,也不让清理路径抛错。
|
|
2147
|
+
*/
|
|
2148
|
+
export function activePlanIdSet(runtime: any): ReadonlySet<string> {
|
|
2149
|
+
const ids = new Set<string>()
|
|
2150
|
+
try {
|
|
2151
|
+
const records = runtime?.state?.records
|
|
2152
|
+
if (records instanceof Map) {
|
|
2153
|
+
for (const record of records.values()) {
|
|
2154
|
+
const direct = typeof record?.planId === 'string' ? record.planId.trim() : ''
|
|
2155
|
+
const nested = typeof record?.plan?.planId === 'string' ? record.plan.planId.trim() : ''
|
|
2156
|
+
const id = direct !== '' ? direct : (nested !== '' ? nested : null)
|
|
2157
|
+
if (id) ids.add(id)
|
|
2158
|
+
}
|
|
2159
|
+
}
|
|
2160
|
+
} catch {
|
|
2161
|
+
// 同上:名单读取失败不阻断清理。
|
|
2162
|
+
}
|
|
2163
|
+
return ids
|
|
2164
|
+
}
|
|
2165
|
+
|
|
2049
2166
|
/**
|
|
2050
2167
|
* [1.12.2] G2: recognise write calls by their argument shape, not by a tool-name
|
|
2051
2168
|
* allow-list. The 1.8.18 version named write/edit/str_replace_editor only, so a
|
|
@@ -2072,3 +2189,78 @@ function isWriteShapedCall(exec: any) {
|
|
|
2072
2189
|
// Every other write form names a single target: file_path or path.
|
|
2073
2190
|
return typeof args.file_path === 'string' || typeof args.path === 'string'
|
|
2074
2191
|
}
|
|
2192
|
+
|
|
2193
|
+
/*
|
|
2194
|
+
* [复审修复] shell 类工具的识别:exec.name 为 bash/shell/exec,或以它们为
|
|
2195
|
+
* 前缀且前缀后是 [-_] 分隔符或结尾(bash_exec、shell-tool 等宿主工具名变体)。
|
|
2196
|
+
* 要求分隔符是为了避免 'executor' 这类普通名字被误判进 shell 闸门。
|
|
2197
|
+
*/
|
|
2198
|
+
function isShellLikeToolName(name: any): boolean {
|
|
2199
|
+
if (typeof name !== 'string' || name === '') return false
|
|
2200
|
+
if (name === 'bash' || name === 'shell' || name === 'exec') return true
|
|
2201
|
+
return /^(?:bash|shell|exec)(?:[-_]|$)/.test(name)
|
|
2202
|
+
}
|
|
2203
|
+
|
|
2204
|
+
/*
|
|
2205
|
+
* [复审修复] 从 shell 调用的 command 字符串解析「写目标路径」。
|
|
2206
|
+
*
|
|
2207
|
+
* 返回 null = 这不是 shell 类调用(或没有 command 参数),调用方据此跳过
|
|
2208
|
+
* shell 闸门;返回数组则可能为空(shell 调用但不含写形态,如纯读的
|
|
2209
|
+
* cat/grep/less)——空数组同样放行。多提取无害:每个候选最终仍走
|
|
2210
|
+
* presentedWriteDenialReason 的「已交付且计划未就绪」判定,普通路径直接
|
|
2211
|
+
* 放行;要防的是漏提取,所以解析宁可宽进。
|
|
2212
|
+
*
|
|
2213
|
+
* 保守模式只认这些强写信号:
|
|
2214
|
+
* - `>` / `>>` 重定向(含 heredoc:`cat <<EOF > path` 的重定向部分);
|
|
2215
|
+
* - `tee [-a] files...`(tee 是纯写管道,必须整组检查);
|
|
2216
|
+
* - `sed -i[.bak] ... files`(段内 -i 之后的全部非选项 token);
|
|
2217
|
+
* - `mv/cp/install 源... 目标`(只取最后一个非选项 token——源是读,只有
|
|
2218
|
+
* 目标是写,全取会把「复制自受保护文件」误拦成写);
|
|
2219
|
+
* - `dd of=path`。
|
|
2220
|
+
*
|
|
2221
|
+
* 启发式局限(有意保守,备查):
|
|
2222
|
+
* - 变量间接(cmd="> $f"; eval、sh -c 嵌套命令)不做展开;
|
|
2223
|
+
* - find -delete、perl -i、crontab 等少见写法不覆盖;
|
|
2224
|
+
* - 引号内带空格的路径按空白截断,不做完整 shell 词法分析;
|
|
2225
|
+
* - 超长命令只解析前 20k 字符。
|
|
2226
|
+
*/
|
|
2227
|
+
const SHELL_COMMAND_PARSE_LIMIT = 20_000
|
|
2228
|
+
function shellWriteTargets(exec: any): string[] | null {
|
|
2229
|
+
if (!isShellLikeToolName(exec?.name)) return null
|
|
2230
|
+
const command = exec?.arguments?.command
|
|
2231
|
+
if (typeof command !== 'string' || command.trim() === '') return null
|
|
2232
|
+
const text = command.length > SHELL_COMMAND_PARSE_LIMIT ? command.slice(0, SHELL_COMMAND_PARSE_LIMIT) : command
|
|
2233
|
+
const targets = new Set<string>()
|
|
2234
|
+
const stripQuotes = (token: string) => token.replace(/^['"]+|['"]+$/g, '')
|
|
2235
|
+
const isOption = (token: string) => token === '-' || token.startsWith('-') || token === '--'
|
|
2236
|
+
const add = (token: string | undefined) => {
|
|
2237
|
+
const cleaned = stripQuotes(String(token ?? '')).trim()
|
|
2238
|
+
if (cleaned !== '') targets.add(cleaned)
|
|
2239
|
+
}
|
|
2240
|
+
// > / >> 重定向(兼容 fd 前缀 2>file、1> file;&> 合并重定向跳过——歧义大)。
|
|
2241
|
+
for (const match of text.matchAll(/(?<![&\d])\d*>>?\s*([^\s;|&<>]+)/g)) add(match[1])
|
|
2242
|
+
// 按命令段分隔:; | &&。单 &(2>&1 的合并目标)不拆,避免把 fd 描述切碎。
|
|
2243
|
+
for (const segment of text.split(/&&|[;|]/)) {
|
|
2244
|
+
const trimmed = segment.trim()
|
|
2245
|
+
if (trimmed === '') continue
|
|
2246
|
+
// tee [-a] files...:tee 之后到段尾的全部 token 都是写目标。
|
|
2247
|
+
const tee = /(?:^|\s)tee(?:\s+-[a-zA-Z]+)*\s+(\S(?:[\s\S]*?))?$/.exec(trimmed)
|
|
2248
|
+
if (tee && /\btee\b/.test(trimmed)) {
|
|
2249
|
+
for (const token of (tee[1] ?? '').split(/\s+/)) if (!isOption(token)) add(token)
|
|
2250
|
+
}
|
|
2251
|
+
// sed -i[.bak] ...:段内含 sed 且带 -i 选项时,取其后的全部非选项 token。
|
|
2252
|
+
const sed = /(?:^|\s)sed(?:\s+-[a-zA-Z]+)*\s+(-i\S*)(?:\s+([\s\S]*))?$/.exec(trimmed)
|
|
2253
|
+
if (sed) {
|
|
2254
|
+
for (const token of (sed[2] ?? '').split(/\s+/)) if (!isOption(token)) add(token)
|
|
2255
|
+
}
|
|
2256
|
+
// mv/cp/install 源... 目标:只取最后一个非选项 token(目标是写、源是读)。
|
|
2257
|
+
const mover = /(?:^|\s)(?:mv|cp|install)(?:\s+-[a-zA-Z]+)*\s+([\s\S]+)$/.exec(trimmed)
|
|
2258
|
+
if (mover) {
|
|
2259
|
+
const tokens = (mover[1] ?? '').split(/\s+/).filter((token) => !isOption(token))
|
|
2260
|
+
add(tokens[tokens.length - 1])
|
|
2261
|
+
}
|
|
2262
|
+
// dd of=path(of= 后到空白为止)。
|
|
2263
|
+
for (const match of trimmed.matchAll(/(?:^|\s)of=([^\s;|&]+)/g)) add(match[1])
|
|
2264
|
+
}
|
|
2265
|
+
return [...targets]
|
|
2266
|
+
}
|