dsh-workplan 1.8.4 → 1.9.1

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/src/host.ts CHANGED
@@ -1,4 +1,7 @@
1
+ import { createHash } from 'node:crypto'
1
2
  import { readFileSync } from 'node:fs'
3
+ import { dirname, join } from 'node:path'
4
+ import { fileURLToPath } from 'node:url'
2
5
  /**
3
6
  * Host half of dsh-workplan.
4
7
  *
@@ -20,6 +23,47 @@ import {
20
23
  } from './runtime.js'
21
24
  import { isRecord } from './plan.js'
22
25
 
26
+ /*
27
+ * [1.8.14] Identity of the modules this process actually loaded (N1).
28
+ *
29
+ * `version` alone cannot answer "is the running process the build I just
30
+ * shipped": the same version can be rebuilt, and a profile recomposition can
31
+ * leave stale modules in memory while the files on disk are new. Hashing the
32
+ * compiled entry files that make up the plugin at activation time gives a
33
+ * value that changes whenever the loaded bytes change. It is computed once, so
34
+ * it cannot drift during the process's life.
35
+ *
36
+ * [1.8.15] `watchdog.js` was missing from the list even though runtime.ts
37
+ * imports it and the build copies it next to the other entries. Appending a
38
+ * line to it left the fingerprint unchanged (T5c §6.1) — a build identity that
39
+ * ignores a loaded module is a false negative, exactly what it exists to catch.
40
+ */
41
+ const LOADED_BUILD = (() => {
42
+ try {
43
+ const entryPath = fileURLToPath(import.meta.url)
44
+ const dir = dirname(entryPath)
45
+ const hash = createHash('sha256')
46
+ for (const file of ['host.js', 'runtime.js', 'plan.js', 'watchdog.js']) {
47
+ hash.update(readFileSync(join(dir, file)))
48
+ }
49
+ return { entryPath, buildHash: hash.digest('hex').slice(0, 16) }
50
+ } catch {
51
+ /*
52
+ * Never fail activation for a diagnostic. An unknown fingerprint is still a
53
+ * truthful answer ("I cannot prove which bytes I am"), and `status` will
54
+ * show `unknown` instead of a fabricated hash.
55
+ */
56
+ return { entryPath: '', buildHash: 'unknown' }
57
+ }
58
+ })()
59
+
60
+ /*
61
+ * Process start time derived from `process.uptime()`, so it is the actual OS
62
+ * process start, not the moment the plugin was constructed. Used to tell
63
+ * "restarted after the fix" from "same process, rebuilt files on disk".
64
+ */
65
+ const PROCESS_STARTED_AT = Date.now() - Math.round(process.uptime() * 1000)
66
+
23
67
  function phaseLabel(phase: any) {
24
68
  if (phase === 'paused') return '本轮预算用尽,可续跑'
25
69
  if (phase === 'blocked') return '卡住了'
@@ -48,7 +92,25 @@ const PROMPT_SECTION = [
48
92
  '2. 核对每个完成子任务的证据与验收标准,不合格的要用 dsh_workplan_update 标记为 blocked 或 in_progress;',
49
93
  '3. 对受阻项向用户说明,必要时先询问再继续;',
50
94
  '4. 不要声称未经验证的完成,不要重复派发已完成的子任务。',
51
- '结果消息里每个完成项都带 verification 与 acceptance:`structured` 是子 Agent 逐条自检通过,`unverified` 是自述但未逐条确认,`failed` 是自检失败(已按受阻处理),`legacy` 是旧格式回传。只把 structured 当作可直接采信,其余必须你自己核对产物。',
95
+ /*
96
+ * [1.8.7] 硬规则:ok:true 不等于完成。
97
+ *
98
+ * 事故(session-99984ebd…)里模型把「本轮预算用尽,可续跑(10/13)」当成
99
+ * 成功,直接 present 并收尾。提示词必须把「complete 字段才是完成」写到
100
+ * 不可误解,并且与 runtime 的回合收尾闸门(agent/turn-stopping)形成双保险。
101
+ */
102
+ /*
103
+ * [1.8.13] 用户明确取消不是完成。
104
+ *
105
+ * 1.8.7 的原文是「全部 done(或用户明确取消)」,T5-C 就是拿这句话把
106
+ * “取消 1/3 再 present”走通的:提示词把它写成完成等价物,present 的
107
+ * planIncomplete 又对 cancelled 短路。现在完成只有一个判据:全部 done
108
+ * 且 deliveryReady=true。
109
+ */
110
+ '5. 「完成」只有一个判据:计划里全部子任务都是 done,**且** deliveryReady=true(每个 done 项 verification="structured"、acceptance 非空且逐项 result="pass"、无缺失产物)。用户取消 ≠ 完成:取消只停止执行,未完成项仍然是未完成。`dsh_workplan_dispatch` 返回 ok:true / complete:true 只表示调用成功或状态全 done,必须再看 deliveryReady 与 完成 X/Y;只要 deliveryReady=false 或还有 pending/blocked,就禁止 present 交付物、禁止写“已完成/最终版/已交付”、禁止给收尾性最终答复,必须先再次调用 dsh_workplan_dispatch 续跑剩余项。',
111
+ '6. 如果续跑被拒绝、受阻项无法恢复、证据补不齐、计划已取消但未完成,或用户要求先看中间结果:答复开头必须写「未完成:剩余 N 项(列出 ID)与原因」,正文只能按中间结果表述,不得包装成成品。取消不会给你交付许可。',
112
+ '7. 子任务有硬执行上限(默认 15 分钟/步)与整轮预算(默认 20 分钟):子 Agent 接近上限时应先把已完成内容落盘并在结论里列出路径。任何「达到执行上限」「本轮预算用尽」都不等于「什么都没做」:状态里的 error 会列出「声明产物已在盘上」,必须先用文件核对;若产物已覆盖验收标准,用 dsh_workplan_update 复核改判,只补缺失部分再续跑,禁止无视盘上产物把整步重跑。',
113
+ '结果消息里每个完成项都带 verification 与 acceptance:`structured` 是子 Agent 逐条自检通过,`unverified` 是自述但未逐条确认,`failed` 是自检失败(已按受阻处理),`legacy` 是旧格式回传。只有 structured 且 acceptance 每项 pass 才算可交付;其余必须你自己核对产物,并用 dsh_workplan_update 的 verification/acceptance/artifacts 字段把核对结果写回(这些字段会持久化、刷新后仍生效),不要把“看着没问题”当成证据。',
52
114
  '子 Agent 通过 `needs` 提出的跨部门需求会列在结果消息的「跨部门待转达」里:由你逐条处理(补派、向用户确认或直接完成),不要忽略。',
53
115
  '长任务可以用 `dsh_workplan_dispatch({ mode: "async" })` 先开跑并立即答复用户,再用 `dsh_workplan_status` 查看进度;async 返回的是“已开跑”,不是“已完成”。',
54
116
  '如果自动分解降级,先向用户说明失败原因并询问是否重试,在用户确认前不要开始执行任务。',
@@ -98,7 +160,10 @@ function createUpdateTool(runtime: any) {
98
160
  name: 'dsh_workplan_update',
99
161
  description:
100
162
  '更新当前任务分解计划中一个子任务的状态与证据。仅用于本会话已生成的主计划;子 Agent 不可调用。'
101
- + '当主 Agent 核对子任务结果、发现完成不实或需要修正时使用。',
163
+ + '当主 Agent 核对子任务结果、发现完成不实或需要修正时使用。'
164
+ + '把子任务标成 done 时,必须同时给出真实的 verification 与 acceptance(每项 result="pass"),'
165
+ + '否则计划不会变成可交付状态(deliveryReady=false,present 会被拒绝)。'
166
+ + '这些字段会写入会话日志,刷新/重启后仍然生效。',
102
167
  parameters: {
103
168
  type: 'object',
104
169
  properties: {
@@ -122,7 +187,47 @@ function createUpdateTool(runtime: any) {
122
187
  evidence: {
123
188
  type: 'array',
124
189
  items: { type: 'string' },
125
- description: '支持该状态的证据(文件、命令输出、验收结果)。',
190
+ description: '支持该状态的证据(文件、命令输出、验收结果)。只用于说明,不参与可交付判定。',
191
+ },
192
+ verification: {
193
+ type: 'string',
194
+ enum: ['structured', 'legacy', 'unverified', 'failed'],
195
+ description: '证据等级:structured=逐条自检通过;unverified=自述未逐条确认;'
196
+ + 'legacy=旧格式回传(没有结构化证据);failed=自检失败。'
197
+ + '可交付要求 structured。',
198
+ },
199
+ acceptance: {
200
+ type: 'array',
201
+ description: '验收项及逐项结果;可交付要求非空且每项 result="pass"。',
202
+ items: {
203
+ type: 'object',
204
+ properties: {
205
+ criterion: { type: 'string', description: '验收标准。' },
206
+ result: { type: 'string', enum: ['pass', 'fail', 'unverified'], description: '该项结果。' },
207
+ evidence: { type: 'string', description: '支持该结果的证据(文件、命令输出、测试结果)。' },
208
+ },
209
+ required: ['criterion', 'result'],
210
+ additionalProperties: false,
211
+ },
212
+ },
213
+ artifacts: {
214
+ type: 'array',
215
+ description: '声明产物;kind=file/url/note,exists=false 表示产物缺失。',
216
+ items: {
217
+ type: 'object',
218
+ properties: {
219
+ kind: { type: 'string', enum: ['file', 'url', 'note'] },
220
+ ref: { type: 'string' },
221
+ exists: { type: 'boolean' },
222
+ },
223
+ required: ['kind', 'ref'],
224
+ additionalProperties: false,
225
+ },
226
+ },
227
+ missingArtifacts: {
228
+ type: 'array',
229
+ items: { type: 'string' },
230
+ description: '已知缺失的产物路径;非空时计划不可交付。',
126
231
  },
127
232
  expectedRevision: {
128
233
  type: 'integer',
@@ -166,7 +271,14 @@ function createCancelTool(runtime: any) {
166
271
  name: 'dsh_workplan_cancel',
167
272
  description:
168
273
  '取消当前会话尚未派发/正在等待派发的任务分解计划。仅取消计划记录,不伪造任何完成;'
169
- + '取消后计划在面板中显示为“已取消”,不会启动任何部门 Agent。',
274
+ + '取消后计划在面板中显示为“已取消”,不会启动任何部门 Agent。'
275
+ + '注意:取消不等于完成。取消后如仍有未完成项或证据缺口,present 仍会被拒绝,'
276
+ + '答复必须按「未完成:剩余 N 项与原因」说明。'
277
+ + 'disposition="superseded" 是另一种处置:把交付责任转移给一个可验证的承接计划。'
278
+ + '运行时逐条复算(V1–V8):源计划必须已取消且未全 done、承接计划必须定义更晚且非终态、'
279
+ + '映射必须覆盖源计划全部子任务、每个映射目标必须有 done+structured 证据与全通过的验收、'
280
+ + '且本会话存在同时含两个 planId 的用户确认消息。任一不满足即拒绝并返回 SUPERSEDE_* 码,'
281
+ + '状态不变;被取代不等于完成。',
170
282
  parameters: {
171
283
  type: 'object',
172
284
  properties: {
@@ -176,7 +288,39 @@ function createCancelTool(runtime: any) {
176
288
  },
177
289
  reason: {
178
290
  type: 'string',
179
- description: '取消原因,会写入会话记录。',
291
+ description: '取消/取代原因,会写入会话记录。',
292
+ },
293
+ disposition: {
294
+ type: 'string',
295
+ enum: ['cancelled', 'superseded'],
296
+ description: '处置方式:cancelled=用户撤销(默认);superseded=责任已转移给承接计划。',
297
+ },
298
+ supersededBy: {
299
+ type: 'object',
300
+ description: 'disposition=superseded 时必填:承接计划与逐项覆盖映射。'
301
+ + '只给映射意图,requiredCount/coveredCount/coverage/digest 由运行时复算,不接受自报。',
302
+ properties: {
303
+ planId: {
304
+ type: 'string',
305
+ description: '承接计划的 planId(必须定义得过且未终止)。',
306
+ },
307
+ mapping: {
308
+ type: 'array',
309
+ description: '源计划每个定义子任务一条映射;from 必须恰好覆盖全部源项(各一次)。',
310
+ items: {
311
+ type: 'object',
312
+ properties: {
313
+ from: { type: 'string', description: '源计划子任务 ID。' },
314
+ to: { type: 'string', description: '承接计划子任务 ID。' },
315
+ basis: { type: 'string', description: '覆盖依据(为什么这两项是同一责任)。' },
316
+ },
317
+ required: ['from', 'to', 'basis'],
318
+ additionalProperties: false,
319
+ },
320
+ },
321
+ },
322
+ required: ['planId', 'mapping'],
323
+ additionalProperties: false,
180
324
  },
181
325
  },
182
326
  additionalProperties: false,
@@ -185,10 +329,27 @@ function createCancelTool(runtime: any) {
185
329
  schema: cancelToolOutputSchema(),
186
330
  render(_args: any, value: any) {
187
331
  const result = isRecord(value) ? value : {}
332
+ if (!result.ok) {
333
+ return textContent(`处置失败${result.code ? `(${String(result.code)})` : ''}:`
334
+ + `${String(result.error ?? '未知错误')}`)
335
+ }
336
+ const stats = isRecord(result.stats) ? result.stats : {}
337
+ if (result.phase === 'superseded') {
338
+ const by = isRecord(result.supersededBy) ? result.supersededBy : {}
339
+ return textContent(
340
+ `计划 ${String(result.planId ?? '')} 已被取代:承接计划 ${String(by.planId ?? '')}`
341
+ + `(映射 ${Number(by.coveredCount ?? 0)}/${Number(by.requiredCount ?? 0)},digest 已复算)`
342
+ + `|源计划仍是 ${Number(stats.done ?? 0)}/${Number(stats.total ?? 0)}:被取代不等于完成,`
343
+ + '不得 present 源计划,只能使用承接计划通过交付就绪检查的产物。',
344
+ )
345
+ }
346
+ const cancelledIncomplete = result.phase !== 'done'
188
347
  return textContent(
189
- result.ok
190
- ? `已取消计划 ${String(result.planId ?? '')}:${String(result.reason ?? '用户取消')}`
191
- : `取消失败:${String(result.error ?? '未知错误')}`,
348
+ `已取消计划 ${String(result.planId ?? '')}:${String(result.reason ?? '用户取消')}`
349
+ + `(完成 ${Number(stats.done ?? 0)}/${Number(stats.total ?? 0)})`
350
+ + (cancelledIncomplete
351
+ ? '|计划仍未完成:不要 present,不要写“已完成”;答复按「未完成:剩余 N 项与原因」开头。'
352
+ : '|计划此前已全部 done。'),
192
353
  )
193
354
  },
194
355
  },
@@ -200,6 +361,64 @@ function createCancelTool(runtime: any) {
200
361
  }
201
362
  }
202
363
 
364
+ function createFollowupTool(runtime: any) {
365
+ return {
366
+ name: 'dsh_workplan_followup',
367
+ description:
368
+ '把一条说明投递给本会话的某个**直接子会话**,让它继续跑一个正常回合。'
369
+ + '用途仅限:子任务被预算/超时中止、但产物已在盘上,且用户明确同意补发 follow-up 时,'
370
+ + '让子会话自己收尾,从而让它自己的 turn/end 变成 completed。'
371
+ + '它不写计划状态、不触发交付、也不改变界面判定;投递本身不等于完成。'
372
+ + '不是直接子会话(父会话 catalog 未登记)一律拒绝。',
373
+ parameters: {
374
+ type: 'object',
375
+ properties: {
376
+ sessionId: {
377
+ type: 'string',
378
+ description: '直接子会话 ID(父会话 subagent/catalog 中登记的那一个)。',
379
+ },
380
+ message: {
381
+ type: 'string',
382
+ description: '发给子会话的说明:确认已有产物、只补缺口、不要整步重跑;不要要求它伪造完成。',
383
+ },
384
+ },
385
+ required: ['sessionId', 'message'],
386
+ additionalProperties: false,
387
+ },
388
+ output: {
389
+ schema: {
390
+ type: 'object',
391
+ properties: {
392
+ ok: { type: 'boolean' },
393
+ sessionId: { type: 'string' },
394
+ messageId: { type: 'string' },
395
+ code: { type: 'string' },
396
+ error: { type: 'string' },
397
+ },
398
+ required: ['ok'],
399
+ additionalProperties: true,
400
+ },
401
+ render(_args: any, value: any) {
402
+ const result = isRecord(value) ? value : {}
403
+ if (!result.ok) {
404
+ return textContent(
405
+ `follow-up 未投递:${String(result.error ?? result.code ?? '未知错误')}`,
406
+ )
407
+ }
408
+ return textContent(
409
+ `已向子会话 ${String(result.sessionId ?? '')} 投递 follow-up`
410
+ + (result.messageId ? `(messageId ${String(result.messageId)})` : '')
411
+ + '。投递只代表消息被接收;界面状态要等该子会话自己的回合正常结束才会变化。',
412
+ )
413
+ },
414
+ },
415
+ async execute(args: any, exec: any) {
416
+ const parsed = asObject(args) ?? {}
417
+ return runtime.followupTool(exec.agent, parsed, exec.signal)
418
+ },
419
+ }
420
+ }
421
+
203
422
  function createStatusTool(runtime: any) {
204
423
  return {
205
424
  name: 'dsh_workplan_status',
@@ -221,15 +440,38 @@ function createStatusTool(runtime: any) {
221
440
  render(_args: any, value: any) {
222
441
  const result = isRecord(value) ? value : {}
223
442
  const stats = isRecord(result.stats) ? result.stats : {}
224
- if (!result.ok) return textContent(`查询失败:${String(result.error ?? '未知错误')}`)
443
+ const runtimeInfo = isRecord(result.runtime) ? result.runtime : {}
444
+ /*
445
+ * [1.8.14] Every answer names the loaded build, including the failure
446
+ * path. The restart verification in the incident report (§14.6 step 2)
447
+ * called `status` before any plan existed and got bare `NO_PLAN`, so it
448
+ * could not confirm which build was answering.
449
+ */
450
+ const startedAt = Number(runtimeInfo.startedAt) || 0
451
+ const identity = [
452
+ typeof result.version === 'string' ? `v${result.version}` : '',
453
+ typeof runtimeInfo.buildHash === 'string' ? `build ${runtimeInfo.buildHash}` : '',
454
+ Number(runtimeInfo.pid) > 0 ? `pid ${Number(runtimeInfo.pid)}` : '',
455
+ startedAt > 0
456
+ ? `启动 ${new Date(startedAt).toLocaleString('zh-CN', { hour12: false })}`
457
+ : '',
458
+ ].filter(Boolean).join('|')
459
+ if (!result.ok) {
460
+ return textContent(
461
+ `查询失败:${String(result.error ?? '未知错误')}`
462
+ + (identity ? `(${identity})` : ''),
463
+ )
464
+ }
225
465
  const running = result.running === true
226
466
  return textContent(
227
- `计划 ${String(result.planId ?? '')}|${running ? '执行中' : '未在执行'}`
467
+ `计划 ${String(result.planId ?? '')}|${running ? '执行中' : '未在执行'}`
228
468
  + `|阶段:${phaseLabel(result.phase)}`
229
469
  + `|完成 ${Number(stats.done ?? 0)}/${Number(stats.total ?? 0)},受阻 ${Number(stats.blocked ?? 0)}`
470
+ + `|${result.deliveryReady === true ? '可交付' : '未就绪'}`
230
471
  + (running && Number(result.elapsedMs ?? 0) > 0
231
472
  ? `|已 ${Math.round(Number(result.elapsedMs) / 1000)}s`
232
- : ''),
473
+ : '')
474
+ + (identity ? `|${identity}` : ''),
233
475
  )
234
476
  },
235
477
  },
@@ -244,8 +486,10 @@ function createDispatchTool(runtime: any) {
244
486
  return {
245
487
  name: 'dsh_workplan_dispatch',
246
488
  description:
247
- '派发当前会话尚未执行的任务分解计划。计划显示后必须先调用本工具;它会等待各部门 Agent 完成,'
248
- + '把 durable 结果写入会话并返回每个子任务的状态/摘要。重复调用是幂等的。',
489
+ '派发当前会话尚未执行的任务分解计划。计划显示后必须先调用本工具;wait 会等到本轮结束,'
490
+ + '把 durable 结果写入会话并返回每个子任务的状态/摘要。'
491
+ + '返回值里的 complete=false 表示计划还没做完(可能只是本轮预算用尽),此时必须再次派发续跑,'
492
+ + '不得把当前产物当作最终结果。重复调用是幂等的。',
249
493
  parameters: {
250
494
  type: 'object',
251
495
  properties: {
@@ -267,18 +511,58 @@ function createDispatchTool(runtime: any) {
267
511
  render(_args: any, value: any) {
268
512
  const result = isRecord(value) ? value : {}
269
513
  const stats = isRecord(result.stats) ? result.stats : {}
514
+ const done = Number(stats.done ?? 0)
515
+ const total = Number(stats.total ?? 0)
516
+ const blocked = Number(stats.blocked ?? 0)
517
+ const remaining = Math.max(0, total - done)
518
+ const name = `计划 ${String(result.planId ?? '')}`
519
+ if (!result.ok) {
520
+ // The hint carries the recovery instruction (resume, or take the work
521
+ // over directly); showing it in the rendered line keeps it visible
522
+ // even when only the summary is read.
523
+ return textContent(`派发失败:${String(result.error ?? '未知错误')}${result.hint ? `|下一步:${String(result.hint)}` : ''}`)
524
+ }
525
+ /*
526
+ * [1.8.7] The first word decides how the model reads the round.
527
+ *
528
+ * Incident: `ok:true` + "已加入进行中的派发并等到结果(完成 10/13)"
529
+ * read as "done", so the model presented a half-finished report. Now an
530
+ * unfinished round starts with 未完成 and states the only legal next
531
+ * move; a finished one is the only place the word 已完成 appears.
532
+ */
533
+ if (result.mode === 'async' || (result.running === true && result.joined !== true)) {
534
+ return textContent(
535
+ `${name} 已开跑,尚未完成:当前完成 ${done}/${total},剩余 ${remaining} 项由本轮执行`
536
+ + `(${Math.round(elapsedSeconds(result) / 1000)}s)`
537
+ + '|不要重复派发;结果消息会在本轮结束时自动写入。在结果消息到达并确认 complete=true 之前,不要给最终答复。',
538
+ )
539
+ }
540
+ /*
541
+ * [1.8.13] All rows done is not the finish line.
542
+ *
543
+ * T5-B/D: every row said done, no structured evidence, and this branch
544
+ * printed 已完成 — the model then presented. Only deliveryReady=true
545
+ * reaches the completion sentence; every other state names the gaps and
546
+ * the legal next move (dispatch, fix evidence, or report unfinished).
547
+ */
548
+ if (result.deliveryReady === true) {
549
+ return textContent(
550
+ `${name} 已完成且可交付:完成 ${done}/${total},证据通过`
551
+ + (result.joined ? '|已加入进行中的派发并等到结果' : ''),
552
+ )
553
+ }
554
+ const blockers = Array.isArray(result.deliveryBlockers) ? result.deliveryBlockers.slice(0, 6).join(';') : ''
555
+ const cancelled = result.phase === 'cancelled'
556
+ const next = cancelled
557
+ ? '计划已取消且未完成:不要再次 dispatch,不要 present;答复按「未完成:剩余 N 项与原因」开头。'
558
+ : remaining > 0
559
+ ? '下一步:再次调用 dsh_workplan_dispatch 续跑剩余项;全部 done 之前不要 present。'
560
+ : "全部子任务虽已 done,但证据未通过:用 dsh_workplan_update 补 verification='structured' 与 acceptance(每项 result='pass'),或把对应项改回 blocked;不要 present。"
270
561
  return textContent(
271
- result.ok
272
- ? `计划 ${String(result.planId ?? '')} 当前阶段:${phaseLabel(result.phase)}`
273
- + `(完成 ${Number(stats.done ?? 0)}/${Number(stats.total ?? 0)},受阻 ${Number(stats.blocked ?? 0)})`
274
- + (result.alreadyRunning || result.mode === 'async'
275
- ? `|已在执行(${Math.round(elapsedSeconds(result) / 1000)}s)`
276
- : '')
277
- + (result.joined ? '|已加入进行中的派发并等到结果' : '')
278
- // The hint carries the recovery instruction (resume, or take the work
279
- // over directly); showing it in the rendered line keeps it visible
280
- // even when only the summary is read.
281
- : `派发失败:${String(result.error ?? '未知错误')}${result.hint ? `|下一步:${String(result.hint)}` : ''}`,
562
+ `${name} 未就绪:完成 ${done}/${total},剩余 ${remaining} 项,受阻 ${blocked}(${phaseLabel(result.phase)})`
563
+ + (blockers ? `|证据缺口:${blockers}` : '')
564
+ + (result.joined ? '|本轮已返回,但计划没有达到可交付状态' : '')
565
+ + `|${next}|不要写“已完成/最终版/已交付”,不要给收尾性最终答复。`,
282
566
  )
283
567
  },
284
568
  },
@@ -313,8 +597,40 @@ function numericSetting(configValue: any, envName: string) {
313
597
  return Number.isFinite(numeric) && numeric > 0 ? numeric : undefined
314
598
  }
315
599
 
600
+ /**
601
+ * Hard, tool-level guards. Kept as a pure function so the contracts can be
602
+ * pinned without mounting the whole plugin:
603
+ * - plan mode:只拦住 dispatch,其他工具保持可用(避免死锁);
604
+ * - `present`:计划未完成时由 runtime 拒绝(事故中模型在收尾闸门之前
605
+ * present,卡片一旦出现就无法撤回)。
606
+ */
607
+ export function toolGuardReason(
608
+ exec: any,
609
+ runtime: { planModeActive(session: any): boolean; presentDenialReason(agent: any): string | undefined },
610
+ ) {
611
+ if (exec?.name === 'dsh_workplan_dispatch') {
612
+ const session = exec?.agent?.session
613
+ if (session && runtime.planModeActive(session)) {
614
+ return '当前是 plan 模式:计划已生成,请先让用户审阅/退出 plan 模式,再调用 dsh_workplan_dispatch。'
615
+ }
616
+ return undefined
617
+ }
618
+ if (exec?.name === 'present') return runtime.presentDenialReason(exec?.agent)
619
+ return undefined
620
+ }
621
+
316
622
  export function apply(ctx: any, config: WorkplanConfig = {}) {
317
623
  const runtime = createWorkplanRuntime(ctx, {
624
+ // [1.8.13] The answering version must be visible in status/dispatch output,
625
+ // not inferred from the PID.
626
+ version: meta.version,
627
+ // [1.8.14] ...and so must the exact loaded build (see LOADED_BUILD above).
628
+ build: {
629
+ pid: process.pid,
630
+ startedAt: PROCESS_STARTED_AT,
631
+ entryPath: LOADED_BUILD.entryPath,
632
+ buildHash: LOADED_BUILD.buildHash,
633
+ },
318
634
  cwd: typeof config.cwd === 'string' ? config.cwd : undefined,
319
635
  childTimeoutMs: numericSetting(config.childTimeoutMs, 'DSH_WORKPLAN_CHILD_TIMEOUT_MS'),
320
636
  idleTimeoutMs: numericSetting(config.idleTimeoutMs, 'DSH_WORKPLAN_IDLE_TIMEOUT_MS'),
@@ -333,23 +649,29 @@ export function apply(ctx: any, config: WorkplanConfig = {}) {
333
649
  ctx.effect(() => ctx.tools.register(createUpdateTool(runtime)))
334
650
  ctx.effect(() => ctx.tools.register(createCancelTool(runtime)))
335
651
  ctx.effect(() => ctx.tools.register(createStatusTool(runtime)))
652
+ ctx.effect(() => ctx.tools.register(createFollowupTool(runtime)))
336
653
  ctx.effect(() => ctx.tools.register(createDispatchTool(runtime)))
337
654
 
338
655
  /*
339
656
  * Plan mode produces a plan for review, not department execution. Denying
340
657
  * only the dispatch tool keeps every other tool available (no deadlock)
341
658
  * and gives the model a precise reason instead of a silent no-op.
659
+ *
660
+ * [1.8.8] `present` 也在执行前拦截:事故里模型在回合中途就把未完成
661
+ * 的产物 present 出去,回合收尾闸门再拦已经撤不回卡片。计划未完成时
662
+ * 拒绝交付,模型必须先续跑或显式取消计划。
342
663
  */
343
- ctx.effect(() => ctx.tools.guard((exec: any) => {
344
- if (exec?.name !== 'dsh_workplan_dispatch') return undefined
345
- const session = exec?.agent?.session
346
- if (session && runtime.planModeActive(session)) {
347
- return '当前是 plan 模式:计划已生成,请先让用户审阅/退出 plan 模式,再调用 dsh_workplan_dispatch。'
348
- }
349
- return undefined
350
- }))
664
+ ctx.effect(() => ctx.tools.guard((exec: any) => toolGuardReason(exec, runtime)))
351
665
 
352
666
  ctx.effect(() => ctx.on('agent/pre-step', runtime.onPreStep))
667
+ /*
668
+ * [1.8.7] 回合收尾前的闸门。官方 `agent/turn-stopping` 是 serial 事件、
669
+ * 在边界提交前被 await;监听器 `agent.steer()` 后机器会重读 inbox,于是
670
+ * 「计划没做完」时回合不会被关闭。这是「未完成即输出」的唯一强制点:
671
+ * 提示词和工具文案只能说服模型,这个事件能拦住收尾本身。
672
+ */
673
+ ctx.effect(() => ctx.on('agent/turn-stopping', runtime.onTurnStopping))
674
+ ctx.effect(() => ctx.tools.guard((exec: any) => fileWriteGuardReason(exec, runtime)))
353
675
 
354
676
  ctx.inject(['webServer', 'connection'], (scope: any) => {
355
677
  scope.effect(() =>
@@ -408,9 +730,24 @@ export function apply(ctx: any, config: WorkplanConfig = {}) {
408
730
 
409
731
  export const meta = {
410
732
  name,
411
- version: '1.8.4',
733
+ version: '1.9.1',
412
734
  promptOrder: PROMPT_ORDER,
413
735
  routes: ['/dsh-workplan/plans', '/dsh-workplan/vendor/chart.umd.js'],
414
736
  }
415
737
 
416
738
  export { PROMPT_SECTION, createCancelTool, createDispatchTool, createStatusTool, createUpdateTool, plannerOutputSchema }
739
+
740
+ /**
741
+ * [1.8.18] 写工具的路径级闸门(T7-N2),与 `toolGuardReason` 分开注册。
742
+ *
743
+ * present/dispatch 的语义是「计划未就绪不能交付」;这里管的是「已交付的
744
+ * 文件不能在计划未就绪时被原地改写」。两条守卫都返回拒绝原因,harness
745
+ * 取第一个非空原因,互不覆盖。
746
+ */
747
+ export function fileWriteGuardReason(
748
+ exec: any,
749
+ runtime: { presentedWriteDenialReason(agent: any, args: any): string | undefined },
750
+ ) {
751
+ if (exec?.name !== 'write' && exec?.name !== 'edit' && exec?.name !== 'str_replace_editor') return undefined
752
+ return runtime.presentedWriteDenialReason(exec?.agent, exec?.arguments)
753
+ }