dsh-workplan 1.11.1 → 1.12.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.
@@ -0,0 +1,244 @@
1
+ /**
2
+ * Department capability catalog (T10 / M2).
3
+ *
4
+ * Before 1.12.0 a department was nothing but a `{zh, en}` label: the planner
5
+ * prompt listed twelve names and `validatePlan` only checked that the value was
6
+ * one of the twelve keys. Nothing described what a department is responsible
7
+ * for, what it produces, or which task vocabulary belongs to it — so "部门分配
8
+ * 是否偏离" was not a question the code could answer.
9
+ *
10
+ * This module is the single source of truth for that missing semantic layer:
11
+ *
12
+ * - `responsibilities` — what the department owns (used by the planner prompt
13
+ * and by the child-agent prompt, so a department actually changes execution);
14
+ * - `outputs` — the typical deliverables (used to explain a match);
15
+ * - `keywords` — the vocabulary that identifies the department's work. These
16
+ * are deliberately concrete nouns/verbs, not generic words, because the
17
+ * matcher must be explainable and must not fire on every task.
18
+ *
19
+ * The catalog is pure data: no model call, no I/O, no configuration. That keeps
20
+ * the audit deterministic and testable, which is exactly what the acceptance
21
+ * criterion ("分配理由可追溯、可复跑") requires.
22
+ *
23
+ * Important boundary: a match is *evidence*, not authority. The model still
24
+ * chooses the department. The runtime only uses this catalog to detect a
25
+ * contradiction that is strong enough to be worth a retry (e.g. a picture-book
26
+ * task growing a penetration-testing subtask).
27
+ */
28
+
29
+ export interface DepartmentCapability {
30
+ id: string
31
+ zh: string
32
+ en: string
33
+ responsibilities: readonly string[]
34
+ outputs: readonly string[]
35
+ keywords: readonly string[]
36
+ }
37
+
38
+ /**
39
+ * Twelve departments, same ids and labels as `DEPARTMENTS` in plan.ts.
40
+ * A unit test asserts the two tables never drift.
41
+ */
42
+ export const DEPARTMENT_CAPABILITIES: readonly DepartmentCapability[] = Object.freeze([
43
+ {
44
+ id: 'engineering',
45
+ zh: '工程研发',
46
+ en: 'Engineering',
47
+ responsibilities: ['代码实现与缺陷修复', '构建、编译与静态检查', '补丁应用与回归用例'],
48
+ outputs: ['代码或补丁', '构建产物', '回归测试结果'],
49
+ keywords: [
50
+ '代码', '实现', '修复', '补丁', 'patch', 'rebase', '构建', '编译', '重构',
51
+ '函数', '接口', '模块', '依赖', '提交', '合并', '分支', 'bug', 'tsc', '单元测试',
52
+ ],
53
+ },
54
+ {
55
+ id: 'research',
56
+ zh: '研究分析',
57
+ en: 'Research',
58
+ responsibilities: ['调研与根因分析', '证据收集与交叉核对', '结论评估与方案对比'],
59
+ outputs: ['诊断报告', '证据索引', '分析与结论'],
60
+ keywords: [
61
+ '调研', '研究', '分析', '诊断', '证据', '评估', '根因', '审计', '对比',
62
+ '假设', '梳理', '阅读', '排查', '定性', '判定', '走查',
63
+ ],
64
+ },
65
+ {
66
+ id: 'design',
67
+ zh: '设计',
68
+ en: 'Design',
69
+ responsibilities: ['交互与信息架构设计', '视觉、布局与样式', '原型与可用性验证'],
70
+ outputs: ['设计稿或原型', '交互说明', '视觉规范'],
71
+ keywords: [
72
+ '设计', '交互', '视觉', '布局', '原型', '样式', '配色', 'UI', 'UX',
73
+ '截图', '红圈', '折叠', '展开', '收起', '点击', '按钮', '页面', '组件', '渲染',
74
+ ],
75
+ },
76
+ {
77
+ id: 'product',
78
+ zh: '产品',
79
+ en: 'Product',
80
+ responsibilities: ['需求澄清与范围界定', '优先级与取舍', '验收标准与用户问题'],
81
+ outputs: ['需求单', '验收标准', '决策问题单'],
82
+ keywords: [
83
+ '需求', '范围', '优先级', '验收标准', '产品', '决策', '用户问题', '方案',
84
+ '取舍', '边界', '排期', '目标',
85
+ ],
86
+ },
87
+ {
88
+ id: 'data',
89
+ zh: '数据',
90
+ en: 'Data',
91
+ responsibilities: ['数据采集与清洗', '指标定义与统计', '数据集与可复现口径'],
92
+ outputs: ['数据集', '指标表', '统计脚本'],
93
+ keywords: [
94
+ '数据', '指标', '统计', '采集', '清洗', 'CSV', '行情', '回测', '量化',
95
+ '数据集', '样本', '口径', '权重', '收益率',
96
+ ],
97
+ },
98
+ {
99
+ id: 'qa',
100
+ zh: '质量验证',
101
+ en: 'QA',
102
+ responsibilities: ['测试设计与执行', '回归与验收证据', '缺陷复现与质量门禁'],
103
+ outputs: ['测试报告', '用例集', '缺陷清单'],
104
+ keywords: [
105
+ '测试', '用例', '回归', '验收', '复现', '缺陷', '质量', '断言',
106
+ '通过率', '门禁', 'npm test', 'fail', '端到端', '推演', '演练',
107
+ ],
108
+ },
109
+ {
110
+ id: 'security',
111
+ zh: '安全',
112
+ en: 'Security',
113
+ responsibilities: ['安全审计与威胁建模', '漏洞与越权验证', '密钥与权限加固'],
114
+ outputs: ['安全报告', '漏洞清单', '加固建议'],
115
+ keywords: [
116
+ '安全', '漏洞', '渗透', '攻击', '越权', '权限', '密钥', '加密',
117
+ '威胁', '加固', '审计日志',
118
+ ],
119
+ },
120
+ {
121
+ id: 'devops',
122
+ zh: '运维',
123
+ en: 'DevOps',
124
+ responsibilities: ['构建发布与制品管理', '流水线与部署', '监控与回滚'],
125
+ outputs: ['发布记录', '流水线配置', '回滚方案'],
126
+ keywords: [
127
+ '发布', '部署', '上线', 'CI', '流水线', '监控', '回滚', '镜像', '制品',
128
+ 'npm', 'registry', 'tag', 'release', 'publish', '索引源',
129
+ ],
130
+ },
131
+ {
132
+ id: 'writing',
133
+ zh: '文档写作',
134
+ en: 'Writing',
135
+ responsibilities: ['文档与报告撰写', '结构、措辞与排版', '版本说明与对外文案'],
136
+ outputs: ['文档或报告', 'README 或 CHANGELOG', '对外说明'],
137
+ keywords: [
138
+ '文档', '报告', '说明', '写作', '润色', 'README', 'CHANGELOG',
139
+ '排版', '措辞', '摘要', '变更说明', '对账', '对照表', '验收表',
140
+ ],
141
+ },
142
+ {
143
+ id: 'operations',
144
+ zh: '运营',
145
+ en: 'Operations',
146
+ responsibilities: ['运营方案与活动策划', '用户增长与渠道', '客服与留存'],
147
+ outputs: ['运营方案', '活动计划', '渠道分析'],
148
+ keywords: ['运营', '活动', '增长', '渠道', '客服', '留存', '拉新', '转化'],
149
+ },
150
+ {
151
+ id: 'legal',
152
+ zh: '法务合规',
153
+ en: 'Legal',
154
+ responsibilities: ['合同与条款审查', '版权、许可与合规', '隐私与授权风险'],
155
+ outputs: ['法务意见', '合同条款', '合规清单'],
156
+ keywords: ['法务', '合同', '版权', '许可', '合规', '条款', '法律', '隐私', '授权', '侵权'],
157
+ },
158
+ {
159
+ id: 'finance',
160
+ zh: '财务',
161
+ en: 'Finance',
162
+ responsibilities: ['预算与成本核算', '结算与资金管理', '财务合规与凭证'],
163
+ outputs: ['预算表', '成本核算', '结算记录'],
164
+ keywords: ['财务', '预算', '成本', '结算', '发票', '报销', '资金', '利润', '现金流'],
165
+ },
166
+ ])
167
+
168
+ /** Departments whose domain keywords are strong enough to detect intrusion. */
169
+ export const DEPARTMENT_INTRUSION_GUARDED: readonly string[] = Object.freeze([
170
+ 'security',
171
+ 'legal',
172
+ 'finance',
173
+ 'operations',
174
+ ])
175
+
176
+ const CAPABILITY_MAP: Readonly<Record<string, DepartmentCapability>> = Object.freeze(
177
+ Object.fromEntries(DEPARTMENT_CAPABILITIES.map((entry) => [entry.id, entry])),
178
+ )
179
+
180
+ export const DEPARTMENT_CAPABILITY_IDS: readonly string[] = Object.freeze(
181
+ DEPARTMENT_CAPABILITIES.map((entry) => entry.id),
182
+ )
183
+
184
+ export function departmentCapability(id: unknown): DepartmentCapability | null {
185
+ if (typeof id !== 'string') return null
186
+ return CAPABILITY_MAP[id] ?? null
187
+ }
188
+
189
+ function escapeRegExp(value: string): string {
190
+ return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
191
+ }
192
+
193
+ /**
194
+ * Keyword match that understands the difference between a CJK substring and a
195
+ * short ASCII token. `ui` must not match inside `build`; `rebase` may match
196
+ * inside `rebased` because that is still the same concept.
197
+ */
198
+ export function keywordHit(text: string, keyword: string): boolean {
199
+ if (typeof text !== 'string' || typeof keyword !== 'string') return false
200
+ const haystack = text.toLowerCase()
201
+ const needle = keyword.trim().toLowerCase()
202
+ if (!needle) return false
203
+ if (/^[a-z0-9+#./-]+$/.test(needle) && needle.length <= 3) {
204
+ return new RegExp(`(^|[^a-z0-9])${escapeRegExp(needle)}([^a-z0-9]|$)`).test(haystack)
205
+ }
206
+ return haystack.includes(needle)
207
+ }
208
+
209
+ /** All catalog keywords of one department that occur in `text`, in catalog order. */
210
+ export function matchDepartmentKeywords(text: string, departmentId: string): string[] {
211
+ const capability = departmentCapability(departmentId)
212
+ if (!capability) return []
213
+ return capability.keywords.filter((keyword) => keywordHit(text, keyword))
214
+ }
215
+
216
+ export interface DepartmentScore {
217
+ id: string
218
+ zh: string
219
+ en: string
220
+ score: number
221
+ matched: string[]
222
+ }
223
+
224
+ /**
225
+ * Rank every department against one piece of text.
226
+ *
227
+ * The score is the number of distinct catalog keywords found. It is a
228
+ * deliberately simple, explainable number: every point maps to a keyword the
229
+ * reader can see in the output. No weights are hidden.
230
+ */
231
+ export function scoreDepartments(text: string): DepartmentScore[] {
232
+ return DEPARTMENT_CAPABILITIES
233
+ .map((capability) => {
234
+ const matched = capability.keywords.filter((keyword) => keywordHit(text, keyword))
235
+ return {
236
+ id: capability.id,
237
+ zh: capability.zh,
238
+ en: capability.en,
239
+ score: matched.length,
240
+ matched,
241
+ }
242
+ })
243
+ .sort((a, b) => b.score - a.score || a.id.localeCompare(b.id))
244
+ }
package/src/host.ts CHANGED
@@ -22,7 +22,7 @@ import {
22
22
  statusToolOutputSchema,
23
23
  updateToolOutputSchema,
24
24
  } from './runtime.js'
25
- import { isRecord } from './plan.js'
25
+ import { DEPARTMENT_IDS, isRecord } from './plan.js'
26
26
  import { autoResumeEnabledFrom } from './auto-resume.js'
27
27
 
28
28
  /*
@@ -166,11 +166,12 @@ export type WorkplanConfig = {
166
166
  totalTimeoutMs?: number
167
167
  maxResumes?: number
168
168
  /**
169
- * [1.10.4] 整轮预算到点后自动开始下一轮。默认 **false**。
169
+ * [1.10.4][1.12.0] 整轮预算到点后自动开始下一轮。默认 **true**(自 1.10.6)。
170
170
  *
171
- * 打开方式(部署时二选一):profile 配置 `autoResume: true`,或环境变量
172
- * `DSH_WORKPLAN_AUTO_RESUME=1`。默认关闭是刻意的:自动续跑会自己消耗
173
- * 模型额度,必须先在小流量 profile 上验证过状态机与证据门禁再开。
171
+ * 关闭方式(部署时二选一):profile 配置 `autoResume: false`,或环境变量
172
+ * `DSH_WORKPLAN_AUTO_RESUME=0`。默认开启是用户 2026-11-01 的明确要求:
173
+ * 一个没人被告知要打开的恢复开关等于没有。取消、blocked、等指示、无进展、
174
+ * 轮次上限等安全分支仍然生效。
174
175
  */
175
176
  autoResume?: boolean
176
177
  /** 自动续跑轮次上限(0–4,默认 2);环境变量 `DSH_WORKPLAN_AUTO_RESUME_MAX`。 */
@@ -184,6 +185,15 @@ export type WorkplanConfig = {
184
185
  * 或部署方明确只想看松散结果。环境变量 `DSH_WORKPLAN_STRICT_COMPLETION=0`。
185
186
  */
186
187
  strictCompletion?: boolean
188
+ /**
189
+ * [1.12.0] 计划生成后是否自动派发(T6 Q2 / 用户三项机制体检)。
190
+ *
191
+ * 默认 **true**:计划一生成就自动开跑,不再依赖主模型额外调用
192
+ * `dsh_workplan_dispatch`。以下边界仍然生效:plan 模式不自动派发(等用户
193
+ * 审阅);已有计划执行中不重复派发;预算、取消、幂等与交付门禁全部保留。
194
+ * 关闭方式:`autoDispatch: false` 或环境变量 `DSH_WORKPLAN_AUTO_DISPATCH=0`。
195
+ */
196
+ autoDispatch?: boolean
187
197
  }
188
198
 
189
199
  function createUpdateTool(runtime: any) {
@@ -272,6 +282,17 @@ function createUpdateTool(runtime: any) {
272
282
  type: 'string',
273
283
  description: '补充说明;会显示在任务卡片中。',
274
284
  },
285
+ /*
286
+ * [1.12.0][D-28] 结果消息指引的 `update + instruction` 必须真的能通过
287
+ * schema 校验。runtime 的 updateStatus 一直读 args.instruction,但这里
288
+ * 因 additionalProperties:false 从未声明该字段:严格宿主会直接拒绝,
289
+ * 模型只能看到「该动作不存在」。与 dsh_workplan_instruct 等价,二选一。
290
+ */
291
+ instruction: {
292
+ type: 'string',
293
+ description: '待指示项(closure=escalated)的答复文本:与 status="in_progress" 一起使用,'
294
+ + '会投递给子会话并把该行恢复为执行中。等价于 dsh_workplan_instruct。',
295
+ },
275
296
  },
276
297
  required: ['subtaskId', 'status'],
277
298
  additionalProperties: false,
@@ -362,7 +383,12 @@ function createReplanTool(runtime: any) {
362
383
  properties: {
363
384
  id: { type: 'string', description: '接替行 ID(默认在旧 ID 后加 b);最长 16 字符,不能与现有 ID 重复。' },
364
385
  title: { type: 'string' },
365
- department: { type: 'string' },
386
+ department: {
387
+ type: 'string',
388
+ /* [1.12.0][D-07] 与规划入口同源:模型先选合法 key,而不是先写自由文本再被运行期拒绝。 */
389
+ enum: [...DEPARTMENT_IDS],
390
+ description: '受影响的部门 key(必须是规划器枚举之一)。',
391
+ },
366
392
  role: { type: 'string' },
367
393
  objective: { type: 'string' },
368
394
  method: { type: 'array', items: { type: 'string' } },
@@ -380,7 +406,12 @@ function createReplanTool(runtime: any) {
380
406
  id: { type: 'string', description: '新行 ID;省略时自动生成 R<修订号>-<序号>。' },
381
407
  title: { type: 'string' },
382
408
  objective: { type: 'string' },
383
- department: { type: 'string' },
409
+ department: {
410
+ type: 'string',
411
+ /* [1.12.0][D-07] 同 replacement.department:枚举与规划入口同源。 */
412
+ enum: [...DEPARTMENT_IDS],
413
+ description: '新行所属部门 key(必须是规划器枚举之一)。',
414
+ },
384
415
  role: { type: 'string' },
385
416
  method: { type: 'array', items: { type: 'string' } },
386
417
  inputs: { type: 'array', items: { type: 'string' } },
@@ -1103,6 +1134,11 @@ export function apply(ctx: any, config: WorkplanConfig = {}) {
1103
1134
  * 与 autoResume 用同一种写法:未配置时走默认值,而不是悄悄等于 false。
1104
1135
  */
1105
1136
  strictEvidence: strictCompletionEnabledFrom(config.strictCompletion, settingEnv('DSH_WORKPLAN_STRICT_COMPLETION')),
1137
+ /*
1138
+ * [1.12.0] 自动派发三态:显式配置 > 显式环境变量 > 默认 true。
1139
+ * 与 autoResume / strictCompletion 同一写法,避免「没配置就等于关闭」。
1140
+ */
1141
+ autoDispatch: strictCompletionEnabledFrom(config.autoDispatch, settingEnv('DSH_WORKPLAN_AUTO_DISPATCH')),
1106
1142
  })
1107
1143
 
1108
1144
  ctx.effect(() =>
@@ -1219,7 +1255,7 @@ export function apply(ctx: any, config: WorkplanConfig = {}) {
1219
1255
 
1220
1256
  export const meta = {
1221
1257
  name,
1222
- version: '1.11.1',
1258
+ version: '1.12.0',
1223
1259
  promptOrder: PROMPT_ORDER,
1224
1260
  routes: ['/dsh-workplan/plans', '/dsh-workplan/vendor/chart.umd.js'],
1225
1261
  }