dsh-workplan 1.12.2 → 2.0.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
@@ -126,6 +126,14 @@ const PROMPT_SECTION = [
126
126
  '子 Agent 通过 `needs` 提出的跨部门需求会列在结果消息的「跨部门待转达」里,并自动登记为待处理需求、计入交付门禁:用 `dsh_workplan_handoff` 逐条处置(list 查看、assign 登记责任部门、resolve 写入答复或产物引用、close 判定无需动作),不要忽略。注意:assign 不产生工作(没有常驻部门代理),需要产出就用 `dsh_workplan_replan(action=add)` 建该部门的子任务或自己完成;对「待指示」的行 resolve 会直接把答复投递给子会话。每条需求都必须有处置结论,否则 present 会被拒绝。',
127
127
  '长任务可以用 `dsh_workplan_dispatch({ mode: "async" })` 先开跑并立即答复用户,再用 `dsh_workplan_status` 查看进度;async 返回的是“已开跑”,不是“已完成”。',
128
128
  '如果自动分解降级,先向用户说明失败原因并询问是否重试,在用户确认前不要开始执行任务。',
129
+ /*
130
+ * [2.0.0] 自我学习与自我迭代(用户 2026-11-05 强制要求)。
131
+ *
132
+ * 部门 Agent 在阶段三必须复盘并落盘为永久记忆;这条提示词让主 Agent 知道
133
+ * 记忆存在、存在哪里、怎么复核,而不是把「会学习」当成不可见的口号。
134
+ */
135
+ '每个部门完成工作后,必须在阶段三提交复盘(问题 / 弯路 / 解决 / 防再犯规则),插件会自动把它写入永久教训记忆文件(默认 ~/.dsh/workplan/lessons.jsonl,可用 DSH_WORKPLAN_LESSONS_FILE 覆盖);下次派发同类子任务时,相关历史教训会自动出现在子 Agent 提示词里。',
136
+ '需要复核「到底记住了什么、哪些错误在重复发生」时,调用 `dsh_workplan_lessons`(action: list / search / stats)。它是只读工具;记忆里反复出现的教训(occurrences ≥ 2)必须在下一次计划里变成明确约束,不能让同类错误第三次发生。',
129
137
  ].join('\n')
130
138
 
131
139
  /**
@@ -194,6 +202,16 @@ export type WorkplanConfig = {
194
202
  * 关闭方式:`autoDispatch: false` 或环境变量 `DSH_WORKPLAN_AUTO_DISPATCH=0`。
195
203
  */
196
204
  autoDispatch?: boolean
205
+ /**
206
+ * [1.12.3] headless「未完成不得成功收尾」硬闸门。
207
+ *
208
+ * `'auto'`(不配置时的默认):只在 headless 运行中启用,交互式 web/tui 会话
209
+ * 行为完全不变。`true` 强制开启(任何运行方式都硬失败),`false` 关闭并回到
210
+ * 1.12.2 的「steer 用尽即放行」。
211
+ *
212
+ * 环境变量:`DSH_WORKPLAN_FAIL_CLOSED_ON_INCOMPLETE=1|0`。
213
+ */
214
+ failClosedOnIncomplete?: boolean
197
215
  }
198
216
 
199
217
  function createUpdateTool(runtime: any) {
@@ -701,6 +719,194 @@ function createEscalateTool(runtime: any) {
701
719
  }
702
720
  }
703
721
 
722
+ /**
723
+ * [1.13.0] 阶段三(验证阶段)的插件核验工具(用户 2026-11-05)。
724
+ *
725
+ * 用户规则:部门 Agent 收到工作后必须先梳理问题、再形成可执行思路、最后
726
+ * 执行与验证;第一、二阶段禁用 dsh-workplan 插件,第三阶段才可以用插件
727
+ * 验证是否成功、是否完全满足要求。
728
+ *
729
+ * 因此这个工具只做一件事:接收三阶段证据并给出核验结论。阶段一/二的调用
730
+ * 会被 `runtime.verifySubtask` 以 `PHASE_GATE` 拒绝(缺 planning/execution
731
+ * 证据),不会替子 Agent 跳过梳理与规划。
732
+ */
733
+ export function createVerifyTool(runtime: any) {
734
+ return {
735
+ name: 'dsh_workplan_verify',
736
+ description:
737
+ '部门子 Agent 在第三阶段(验证阶段)使用的插件核验:提交 planning / execution / verification 三阶段证据,'
738
+ + '插件逐项核验并返回 passed 或仍需补齐的 gaps。阶段一、二禁用本插件,调用会返回 PHASE_GATE;'
739
+ + '只有「问题梳理 + 可执行思路 + 实际执行步骤」齐全后,才会进入阶段三核验。'
740
+ + '核验通过不代表完成:最终仍要在结果块里写同一份 phases,由主计划按三阶段契约判定。',
741
+ parameters: {
742
+ type: 'object',
743
+ properties: {
744
+ subtaskId: {
745
+ type: 'string',
746
+ description: '你的子任务 ID(仅用于回显核对;服务端按父子会话关系反查,自报值不作为定位依据)。',
747
+ },
748
+ phases: {
749
+ type: 'object',
750
+ description: '三阶段证据:planning(问题梳理 + 可执行思路)、execution(执行步骤)、verification(逐项验证)。',
751
+ properties: {
752
+ planning: {
753
+ type: 'object',
754
+ properties: {
755
+ problemAnalysis: { type: 'array', items: { type: 'string' }, description: '要解决的问题逐条梳理(现状/事实/约束/未知/主要矛盾/边界)' },
756
+ constraints: { type: 'array', items: { type: 'string' }, description: '约束条件' },
757
+ unknowns: { type: 'array', items: { type: 'string' }, description: '未知或待确认项' },
758
+ approach: { type: 'array', items: { type: 'string' }, description: '完整可执行的思路:每一步做什么、产出什么、如何证明' },
759
+ },
760
+ required: ['problemAnalysis', 'approach'],
761
+ },
762
+ execution: {
763
+ type: 'object',
764
+ properties: {
765
+ steps: { type: 'array', items: { type: 'string' }, description: '实际执行的步骤' },
766
+ deviations: { type: 'array', items: { type: 'string' }, description: '偏离原思路的地方与原因' },
767
+ },
768
+ required: ['steps'],
769
+ },
770
+ verification: {
771
+ type: 'object',
772
+ properties: {
773
+ allPassed: { type: 'boolean', description: '是否全部验证通过;未全部通过必须为 false' },
774
+ checks: {
775
+ type: 'array',
776
+ items: {
777
+ type: 'object',
778
+ properties: {
779
+ criterion: { type: 'string', description: '对应的验收标准(A1/A2…)' },
780
+ method: { type: 'string', description: '用什么方式验证' },
781
+ evidence: { type: 'string', description: '命令输出/文件/引用' },
782
+ },
783
+ required: ['criterion', 'method', 'evidence'],
784
+ },
785
+ },
786
+ requirementCheck: { type: 'string', description: '逐条核对用户原要求的结果' },
787
+ retrospective: {
788
+ type: 'object',
789
+ description: '阶段三收尾的自我学习复盘:本次遇到的问题、走过的弯路、如何解决、以后如何预防再犯',
790
+ properties: {
791
+ problems: { type: 'array', items: { type: 'string' }, description: '本次全过程遇到的问题(现象 + 根因),至少 1 条' },
792
+ detours: { type: 'array', items: { type: 'string' }, description: '走过的弯路/返工/无效尝试;确实没有就写空数组' },
793
+ resolutions: { type: 'array', items: { type: 'string' }, description: '最终是如何解决的,至少 1 条' },
794
+ prevention: { type: 'array', items: { type: 'string' }, description: '以后如何预防再犯(遇到…先…),至少 1 条' },
795
+ },
796
+ required: ['problems', 'detours', 'resolutions', 'prevention'],
797
+ },
798
+ },
799
+ required: ['allPassed', 'checks', 'retrospective'],
800
+ },
801
+ },
802
+ required: ['planning', 'execution', 'verification'],
803
+ },
804
+ },
805
+ required: ['phases'],
806
+ additionalProperties: false,
807
+ },
808
+ output: {
809
+ schema: {
810
+ type: 'object',
811
+ properties: {
812
+ ok: { type: 'boolean' },
813
+ passed: { type: 'boolean' },
814
+ phase: { type: 'string' },
815
+ planId: { type: 'string' },
816
+ subtaskId: { type: 'string' },
817
+ summary: { type: 'string' },
818
+ gaps: { type: 'array', items: { type: 'string' } },
819
+ code: { type: 'string' },
820
+ error: { type: 'string' },
821
+ message: { type: 'string' },
822
+ },
823
+ required: ['ok'],
824
+ additionalProperties: true,
825
+ },
826
+ render(_args: any, value: any) {
827
+ const result = isRecord(value) ? value : {}
828
+ if (!result.ok) {
829
+ const gaps = Array.isArray(result.gaps) ? result.gaps.slice(0, 5).join(';') : ''
830
+ return textContent(
831
+ `插件核验未通过(${String(result.code ?? 'FAILED')}):${String(result.error ?? '未知原因')}`
832
+ + (gaps ? `|需补齐:${gaps}` : '')
833
+ + '|先修复再回报,不要用 done 收尾。',
834
+ )
835
+ }
836
+ return textContent(
837
+ `${String(result.subtaskId ?? '')} 阶段三核验通过:${String(result.summary ?? '')}`
838
+ + '|把同一份 phases 写进结果块后提交。',
839
+ )
840
+ },
841
+ },
842
+ async execute(args: any, exec: any) {
843
+ const parsed = asObject(args) ?? {}
844
+ return runtime.verifyTool(exec.agent, parsed)
845
+ },
846
+ }
847
+ }
848
+
849
+ /**
850
+ * [2.0.0] 永久教训记忆的只读入口(用户 2026-11-05 自我学习要求)。
851
+ *
852
+ * 不提供写动作:记忆只能由「阶段三复盘通过 → 完成判定」这条链写入,
853
+ * 防止主 Agent 事后手写一条“教训”绕过真实复盘证据。
854
+ */
855
+ function createLessonsTool(runtime: any) {
856
+ return {
857
+ name: 'dsh_workplan_lessons',
858
+ description:
859
+ '查看 dsh-workplan 的永久教训记忆(部门完成工作后自动落盘的复盘):'
860
+ + 'action=list 按部门取最相关教训,action=search 按关键词检索,action=stats 看总数/重复数与文件位置。'
861
+ + '只读,不修改计划状态;阶段三验证时用它核对记住了什么、哪些同类错误在重复出现。',
862
+ parameters: {
863
+ type: 'object',
864
+ properties: {
865
+ action: { type: 'string', enum: ['list', 'search', 'stats'], description: '默认 list' },
866
+ department: { type: 'string', description: 'list 时按部门筛选(如 engineering)' },
867
+ query: { type: 'string', description: 'search 时的关键词' },
868
+ limit: { type: 'number', description: '最多返回多少条(1–50,默认 10)' },
869
+ },
870
+ additionalProperties: false,
871
+ },
872
+ output: {
873
+ schema: {
874
+ type: 'object',
875
+ properties: {
876
+ ok: { type: 'boolean' },
877
+ file: { type: 'string' },
878
+ total: { type: 'number' },
879
+ repeated: { type: 'number' },
880
+ lessons: { type: 'array', items: { type: 'object' } },
881
+ departments: { type: 'array', items: { type: 'object' } },
882
+ error: { type: 'string' },
883
+ },
884
+ required: ['ok'],
885
+ additionalProperties: true,
886
+ },
887
+ render(_args: any, value: any) {
888
+ const result = isRecord(value) ? value : {}
889
+ if (!result.ok) return textContent(`教训记忆读取失败:${String(result.error ?? '未知原因')}`)
890
+ const file = String(result.file ?? '')
891
+ if (Array.isArray(result.departments)) {
892
+ const rows = result.departments.map((item: any) => `${item.department}:${item.count}`).join('、') || '(空)'
893
+ return textContent(`永久教训记忆:共 ${String(result.total ?? 0)} 条,其中重复发生 ${String(result.repeated ?? 0)} 类;分布 ${rows};文件 ${file}`)
894
+ }
895
+ const lessons = Array.isArray(result.lessons) ? result.lessons : []
896
+ if (lessons.length === 0) return textContent(`永久教训记忆为空(${file})`)
897
+ const lines = lessons.slice(0, 8).map((item: any) =>
898
+ `- [${String(item.department ?? '')}] ${String(item.title ?? '')}(${String(item.occurrences ?? 1)} 次)`
899
+ + `|问题:${String((item.problems ?? []).join(';'))}`
900
+ + `|预览:${String((item.prevention ?? []).join(';'))}`)
901
+ return textContent(`永久教训记忆(${lessons.length}/${String(result.total ?? lessons.length)} 条,文件 ${file}):\n${lines.join('\n')}`)
902
+ },
903
+ },
904
+ async execute(args: any, exec: any) {
905
+ return runtime.lessonsTool(exec.agent, asObject(args) ?? {})
906
+ },
907
+ }
908
+ }
909
+
704
910
  /**
705
911
  * [1.11.0] 主 → 子的指示通道(T4 C-10 / T6 M6)。
706
912
  *
@@ -1060,6 +1266,23 @@ function strictCompletionEnabledFrom(configValue: any, envValue: any) {
1060
1266
  return true
1061
1267
  }
1062
1268
 
1269
+ /**
1270
+ * [1.12.3] `failClosedOnIncomplete` 的三态解析:显式配置 > 显式环境变量 > 'auto'。
1271
+ *
1272
+ * 与 `strictCompletionEnabledFrom` 的区别只在默认值:这里未配置时返回 `'auto'`,
1273
+ * 语义是「headless 才硬失败」,不能退化成 `false`(那等于默认关闭,事故会复发),
1274
+ * 也不能退化成 `true`(那会让交互式会话在收尾时抛错)。
1275
+ */
1276
+ function failClosedOnIncompleteFrom(configValue: any, envValue: any): boolean | 'auto' {
1277
+ if (typeof configValue === 'boolean') return configValue
1278
+ const raw = envValue === undefined || envValue === null ? '' : String(envValue).trim()
1279
+ if (raw !== '') {
1280
+ if (/^(1|true|yes|on)$/i.test(raw)) return true
1281
+ if (/^(0|false|no|off)$/i.test(raw)) return false
1282
+ }
1283
+ return 'auto'
1284
+ }
1285
+
1063
1286
  function booleanSetting(configValue: any, envName: string) {
1064
1287
  if (typeof configValue === 'boolean') return configValue
1065
1288
  const raw = typeof process !== 'undefined' ? process.env?.[envName] : undefined
@@ -1139,6 +1362,14 @@ export function apply(ctx: any, config: WorkplanConfig = {}) {
1139
1362
  * 与 autoResume / strictCompletion 同一写法,避免「没配置就等于关闭」。
1140
1363
  */
1141
1364
  autoDispatch: strictCompletionEnabledFrom(config.autoDispatch, settingEnv('DSH_WORKPLAN_AUTO_DISPATCH')),
1365
+ /*
1366
+ * [1.12.3] headless「未完成不得成功收尾」。默认 'auto':只有 headless
1367
+ * 运行上下文(提供 `headlessStartup` 服务)才硬失败,交互式会话不受影响。
1368
+ */
1369
+ failClosedOnIncomplete: failClosedOnIncompleteFrom(
1370
+ config.failClosedOnIncomplete,
1371
+ settingEnv('DSH_WORKPLAN_FAIL_CLOSED_ON_INCOMPLETE'),
1372
+ ),
1142
1373
  })
1143
1374
 
1144
1375
  ctx.effect(() =>
@@ -1162,6 +1393,10 @@ export function apply(ctx: any, config: WorkplanConfig = {}) {
1162
1393
  * 侧不做过滤;权限判断一律落在 execute 内部。
1163
1394
  */
1164
1395
  ctx.effect(() => ctx.tools.register(createEscalateTool(runtime)))
1396
+ /* [1.13.0] 阶段三核验:子会话唯一可用的插件工作能力,阶段一/二会被 PHASE_GATE 拒绝。 */
1397
+ ctx.effect(() => ctx.tools.register(createVerifyTool(runtime)))
1398
+ /* [2.0.0] 永久教训记忆的只读复核入口(阶段三验证用)。 */
1399
+ ctx.effect(() => ctx.tools.register(createLessonsTool(runtime)))
1165
1400
  ctx.effect(() => ctx.tools.register(createInstructTool(runtime)))
1166
1401
  /*
1167
1402
  * [1.11.0] 需求变更的重规划入口(T4 C-11 / T7)。
@@ -1255,12 +1490,12 @@ export function apply(ctx: any, config: WorkplanConfig = {}) {
1255
1490
 
1256
1491
  export const meta = {
1257
1492
  name,
1258
- version: '1.12.2',
1493
+ version: '2.0.1',
1259
1494
  promptOrder: PROMPT_ORDER,
1260
1495
  routes: ['/dsh-workplan/plans', '/dsh-workplan/vendor/chart.umd.js'],
1261
1496
  }
1262
1497
 
1263
- export { PROMPT_SECTION, createCancelTool, createDispatchTool, createEscalateTool, createHandoffTool, createInstructTool, createReplanTool, createStatusTool, createUpdateTool, plannerOutputSchema }
1498
+ export { PROMPT_SECTION, createCancelTool, createDispatchTool, createEscalateTool, createHandoffTool, createInstructTool, createLessonsTool, createReplanTool, createStatusTool, createUpdateTool, plannerOutputSchema }
1264
1499
 
1265
1500
  /**
1266
1501
  * [1.8.18] 写工具的路径级闸门(T7-N2),与 `toolGuardReason` 分开注册。