dsh-workplan 3.0.2 → 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.
@@ -43,7 +43,7 @@ import { diceSimilarity } from './v3-contract.js';
43
43
  /** 三阶段顺序不可颠倒;这里也是结果块里 `phases` 的键名。 */
44
44
  export const THREE_PHASE_IDS = Object.freeze(['planning', 'execution', 'verification']);
45
45
  /** [2.5.1] 契约版本 2:规划阶段升级为两轮深度思考协议(新增必填 approachBasis)。 */
46
- export const THREE_PHASE_VERSION = 2;
46
+ export const THREE_PHASE_VERSION = 3;
47
47
  /**
48
48
  * 「全面、全盘、深入」无法由代码度量,但可以给一个不可忽略的下限:
49
49
  * 第一轮任务深研与第二轮方法、流程、步骤各至少 40 个字符,避免一个词就冒充
@@ -52,6 +52,10 @@ export const THREE_PHASE_VERSION = 2;
52
52
  export const MIN_PLANNING_PROBLEM_CHARS = 40;
53
53
  export const MIN_PLANNING_APPROACH_CHARS = 40;
54
54
  export const MIN_PLANNING_BASIS_CHARS = 20;
55
+ /** [3.1.0] 第二步资源与工具、第三步准备与就绪证据的下限(合计字符数)。 */
56
+ export const MIN_RESOURCE_CHARS = 20;
57
+ export const MIN_TOOL_CHARS = 10;
58
+ export const MIN_PREPARATION_CHARS = 20;
55
59
  export const MAX_PHASE_LIST_ITEMS = 20;
56
60
  export const MAX_PHASE_ITEM_CHARS = 800;
57
61
  export const MAX_PHASE_TOTAL_CHARS = 6000;
@@ -84,8 +88,8 @@ function totalChars(items) {
84
88
  return items.reduce((sum, item) => sum + item.length, 0);
85
89
  }
86
90
  /**
87
- * Same matching rules as `src/plan.ts#acceptanceCoverage` (Q6 / U-16 +
88
- * [2.0.4] 同义改写两条宽松规则,逐字对齐 plan.ts:3177-3202):
91
+ * Same matching rules as `src/plan.ts#acceptanceCoverage`([4th batch] 归一化逐字
92
+ * 对齐 plan.ts:3300-3309:全角折叠、完成态前缀剥离、去标点、去祈使前缀):
89
93
  * stable A1…An numbering, exact full text, a longer text containing the
90
94
  * full criterion, a near-full copy (≥85% of the criterion length), or a
91
95
  * paraphrase with Dice ≥ 0.70 and matching negation polarity. A partial
@@ -97,7 +101,11 @@ function totalChars(items) {
97
101
  */
98
102
  function normaliseCriterion(value) {
99
103
  return String(value ?? '')
104
+ .trim()
105
+ .replace(/[!-~]/g, (ch) => String.fromCharCode(ch.charCodeAt(0) - 0xFEE0))
106
+ .replace(/ /g, ' ')
100
107
  .toLowerCase()
108
+ .replace(/^(?:已完成|完成|已|done|yes)(?=[\s::,,。.!!??;;、\-—–]|$)|^(?:已完成|完成|已)/i, '')
101
109
  .replace(/[\s,。;、,.;::!!??"'“”‘’()()\[\]【】《》<>]/g, '')
102
110
  .replace(/^(必须|需要|应当|应该|请|务必)/, '');
103
111
  }
@@ -153,13 +161,16 @@ export function validatePhaseEvidence(value, expectedAcceptance = []) {
153
161
  return {
154
162
  ok: false,
155
163
  phases: null,
156
- errors: ['phases 缺失:必须提交 planning / execution / verification 三阶段证据'],
164
+ errors: ['phases 缺失:必须提交 planning(第一、二步)/ preparation(第三步)/ execution(第四步)/ verification(第四步验证)四组证据,第五步用 report / departmentSummary 交付'],
157
165
  failedPhase: 'shape',
158
166
  };
159
167
  }
160
168
  const planningRaw = isRecord(value.planning) ? value.planning : null;
161
169
  if (!planningRaw)
162
- fail('planning', 'phases.planning 缺失(阶段一 · 两轮深度思考:先全面、全盘、深入梳理、研究、思考自己的任务,再基于其全部结果理清方法、流程、步骤)');
170
+ fail('planning', 'phases.planning 缺失(第一步 · 任务梳理 + 第二步 · 资源/工具/实施方案研究:先全面、全盘、深入梳理研究自己的任务,再基于其结果研究需要哪些资源、哪些工具、具体实施方案)');
171
+ const preparationRaw = isRecord(value.preparation) ? value.preparation : null;
172
+ if (!preparationRaw)
173
+ fail('preparation', 'phases.preparation 缺失(第三步 · 准备:把第二步研究出的资源、工具、实施方案真正备齐,并给出就绪证据)');
163
174
  const executionRaw = isRecord(value.execution) ? value.execution : null;
164
175
  if (!executionRaw)
165
176
  fail('execution', 'phases.execution 缺失(阶段二:基于第二轮理清的方法、流程、步骤执行)');
@@ -181,6 +192,14 @@ export function validatePhaseEvidence(value, expectedAcceptance = []) {
181
192
  required: !!planningRaw,
182
193
  minItems: 1,
183
194
  }),
195
+ resources: readTextList(planningRaw?.resources, 'phases.planning.resources', errors, {
196
+ required: !!planningRaw,
197
+ minItems: 1,
198
+ }),
199
+ tools: readTextList(planningRaw?.tools, 'phases.planning.tools', errors, {
200
+ required: !!planningRaw,
201
+ minItems: 1,
202
+ }),
184
203
  };
185
204
  if (errors.some((item) => item.startsWith('phases.planning.problemAnalysis')))
186
205
  failedPhase ||= 'planning';
@@ -195,6 +214,30 @@ export function validatePhaseEvidence(value, expectedAcceptance = []) {
195
214
  if (planning.approachBasis.length > 0 && totalChars(planning.approachBasis) < MIN_PLANNING_BASIS_CHARS) {
196
215
  fail('planning', `phases.planning.approachBasis 太薄(合计 ${totalChars(planning.approachBasis)} 字,至少 ${MIN_PLANNING_BASIS_CHARS} 字):要写清第二轮的方法、流程、步骤如何基于第一轮梳理、研究、思考完成好的所有结果`);
197
216
  }
217
+ if (planning.resources.length > 0 && totalChars(planning.resources) < MIN_RESOURCE_CHARS) {
218
+ fail('planning', `phases.planning.resources 太薄(合计 ${totalChars(planning.resources)} 字,至少 ${MIN_RESOURCE_CHARS} 字):第二步要逐项写清完成本任务需要哪些资源(数据/文件/权限/人/知识/时间),缺一项就会在执行中卡住`);
219
+ }
220
+ if (planning.tools.length > 0 && totalChars(planning.tools) < MIN_TOOL_CHARS) {
221
+ fail('planning', `phases.planning.tools 太薄(合计 ${totalChars(planning.tools)} 字,至少 ${MIN_TOOL_CHARS} 字):第二步要逐项写清需要哪些工具、每个工具用来做什么、为什么选它`);
222
+ }
223
+ const preparation = {
224
+ prepared: readTextList(preparationRaw?.prepared, 'phases.preparation.prepared', errors, {
225
+ required: !!preparationRaw,
226
+ minItems: 1,
227
+ }),
228
+ evidence: readTextList(preparationRaw?.evidence, 'phases.preparation.evidence', errors, {
229
+ required: !!preparationRaw,
230
+ minItems: 1,
231
+ }),
232
+ };
233
+ if (errors.some((item) => item.startsWith('phases.preparation')))
234
+ failedPhase ||= 'preparation';
235
+ if (preparation.prepared.length > 0 && totalChars(preparation.prepared) < MIN_PREPARATION_CHARS) {
236
+ fail('preparation', `phases.preparation.prepared 太薄(合计 ${totalChars(preparation.prepared)} 字,至少 ${MIN_PREPARATION_CHARS} 字):第三步要把第二步研究出的资源与工具真正备齐,逐项写清准备了什么、在哪里、是否可用`);
237
+ }
238
+ if (preparation.evidence.length > 0 && totalChars(preparation.evidence) < MIN_PREPARATION_CHARS) {
239
+ fail('preparation', `phases.preparation.evidence 太薄(合计 ${totalChars(preparation.evidence)} 字,至少 ${MIN_PREPARATION_CHARS} 字):每一项就绪状态都要有可复核的证据(命令输出/文件路径/配置项/版本号/引用)`);
240
+ }
198
241
  const execution = {
199
242
  steps: readTextList(executionRaw?.steps, 'phases.execution.steps', errors, {
200
243
  required: !!executionRaw,
@@ -260,6 +303,7 @@ export function validatePhaseEvidence(value, expectedAcceptance = []) {
260
303
  const phases = {
261
304
  version: THREE_PHASE_VERSION,
262
305
  planning,
306
+ preparation,
263
307
  execution,
264
308
  verification,
265
309
  };
@@ -269,6 +313,10 @@ export function validatePhaseEvidence(value, expectedAcceptance = []) {
269
313
  ...planning.unknowns,
270
314
  ...planning.approach,
271
315
  ...planning.approachBasis,
316
+ ...planning.resources,
317
+ ...planning.tools,
318
+ ...preparation.prepared,
319
+ ...preparation.evidence,
272
320
  ...execution.steps,
273
321
  ...execution.deviations,
274
322
  verification.requirementCheck,
@@ -285,20 +333,22 @@ export function validatePhaseEvidence(value, expectedAcceptance = []) {
285
333
  * still in phase 1 or 2, where the plugin is disabled by the user's rule.
286
334
  */
287
335
  export function phaseGateFromErrors(errors) {
288
- return errors.some((item) => /^phases(\.| 缺失)/.test(item) && /planning|execution/.test(item))
289
- ? 'phase-1-2'
336
+ return errors.some((item) => /^phases(\.| 缺失)/.test(item) && /planning|preparation|execution/.test(item))
337
+ ? 'phase-1-3'
290
338
  : 'verification';
291
339
  }
292
340
  /** One-line, model-readable summary of what the three phases contain. */
293
341
  export function phaseSummaryLine(phases) {
294
342
  const deviations = phases.execution.deviations.length;
295
343
  return [
296
- `阶段一 规划 · 两轮深度思考(第一轮任务深研 ${phases.planning.problemAnalysis.length} 条 / 第二轮方法流程步骤 ${phases.planning.approach.length} 步 / 第二轮依据 ${phases.planning.approachBasis.length} 条)`,
297
- `阶段二 执行(${phases.execution.steps.length} 步${deviations ? ` / 偏离 ${deviations} 处` : ''})`,
298
- `阶段三 验证(${phases.verification.checks.length} 项${phases.verification.allPassed ? ',全部通过' : ',未全部通过'};`
344
+ `第一步 任务梳理(${phases.planning.problemAnalysis.length} 条)`,
345
+ `第二步 资源/工具/方案研究(资源 ${phases.planning.resources.length} 项 / 工具 ${phases.planning.tools.length} 项 / 方法 ${phases.planning.approach.length} 步 / 依据 ${phases.planning.approachBasis.length} 条)`,
346
+ `第三步 准备(已备 ${phases.preparation.prepared.length} 项 / 就绪证据 ${phases.preparation.evidence.length} 条)`,
347
+ `第四步 执行与验证(执行 ${phases.execution.steps.length} 步${deviations ? ` / 偏离 ${deviations} 处` : ''};验证 ${phases.verification.checks.length} 项${phases.verification.allPassed ? ',全部通过' : ',未全部通过'};`
299
348
  + `复盘 ${phases.verification.retrospective.problems.length} 个问题 / `
300
349
  + `${phases.verification.retrospective.detours.length} 条弯路 / `
301
350
  + `${phases.verification.retrospective.prevention.length} 条防再犯规则)`,
351
+ '第五步 JSON 交付(report / departmentSummary,只含关键信息)',
302
352
  ].join(';');
303
353
  }
304
354
  /**
@@ -310,26 +360,29 @@ export function phaseSummaryLine(phases) {
310
360
  */
311
361
  export function threePhasePromptLines() {
312
362
  return [
313
- '三阶段工作法(用户强制要求;顺序不可颠倒,阶段一的两轮深度思考没有完成之前禁止动手执行):',
314
- '【阶段一 · 规划阶段 = 两轮深度思考(部门负责人、全球顶尖专家执行自己的任务时同一口径,顺序与依赖不可颠倒)】',
315
- ' 第一轮 · 任务深研:先全面、全盘、深入梳理、研究、思考自己的任务——逐条写清任务目标与验收标准、现状与已知事实、约束条件、未知/待确认项、主要矛盾、工作边界与依赖,结果写入 phases.planning.problemAnalysis;',
316
- ' 第二轮 · 理清方法流程步骤:基于刚才第一轮完成好的所有结果,再全面、全盘、深入地开展梳理、研究和思考,理清自己的思路,理清自己该具体如何一步步以最高效率执行、确保完美完成自己任务的方法、流程和步骤——每一步做什么、产出什么、依赖什么、风险与对策、如何证明做对了,并写清全程如何少走弯路(先查历史教训记忆、先验证再动手、不重复同样的试错),结果写入 phases.planning.approach;同时把第二轮如何基于第一轮全部结果写入 phases.planning.approachBasis(逐条对应:第一轮的哪些事实/约束/矛盾决定了第二轮的哪些方法/流程/步骤);',
317
- ' 自检:任何一条验收标准或用户原要求没有落在第二轮的方法、流程、步骤上,就回到第一轮重做;两轮深度思考没有完成并写全(problemAnalysis + approach + approachBasis)之前,不得进入阶段二。',
318
- '【阶段二 · 执行阶段】',
319
- ' 最后,基于刚才第二轮理清楚的所有方法、流程、步骤,开始具体的执行工作:严格按 phases.planning.approach 执行。出现新事实需要偏离时,必须记录偏离原因、证据与调整后的做法,不得静默改道。',
320
- '【阶段三 · 验证阶段】',
321
- ' 执行完成后逐条验证验收标准与用户原要求:每条都要有可复核的证据(命令输出/文件/引用);验证未通过不得报 done,无法确认写 unverified。',
322
- '【阶段三收尾 · 自我学习复盘(强制)】',
363
+ '五步工作法(用户 2026-11-17 强制要求,取代旧的三阶段口径;五个步骤必须按顺序执行,前一步没有完成并写好证据之前,禁止进入下一步):',
364
+ '【第一步 · 任务梳理(部门负责人、相关领域全球顶尖专家执行自己的任务时同一口径)】',
365
+ ' 先全面、全盘、深入梳理、研究、思考自己的任务:逐条写清任务目标与验收标准、现状与已知事实、约束条件、未知/待确认项、主要矛盾、工作边界与依赖,结果写入 phases.planning.problemAnalysis(约束写 constraints,未知写 unknowns)。',
366
+ '【第二步 · 资源、工具与实施方案研究】',
367
+ ' 基于第一步完成好的所有结果,再全面、全盘、深入地梳理、研究、思考:完成本任务需要哪些资源(数据/文件/权限/人/知识/时间)写入 phases.planning.resources;需要哪些工具、每个工具用来做什么写入 phases.planning.tools;具体实施方案写成自己该具体如何一步步以最高效率执行、确保完美完成自己任务的方法、流程和步骤(每一步做什么、产出什么、依赖什么、风险与对策、如何证明做对了,以及全程如何少走弯路:先查历史教训、先验证再动手、不重复试错)写入 phases.planning.approach;同时把第二步如何基于第一步全部结果逐条对应写入 phases.planning.approachBasis。',
368
+ ' 自检:任何一条验收标准或用户原要求没有落在第二步的资源、工具与实施方案上,就回到第一步重做。',
369
+ '【第三步 · 准备】',
370
+ ' 基于第二步的全部结果,把需要的资源、工具、实施方案真正准备到位,而不是「打算用」:逐项写清准备了什么、在哪里、是否可用,写入 phases.preparation.prepared;每一项就绪状态都必须给出可复核的证据(命令输出/文件路径/配置项/版本号/引用),写入 phases.preparation.evidence。缺资源、缺工具、缺证据时,先补齐或如实上报受阻,不得带着缺口进入第四步。',
371
+ '【第四步 · 执行与验证】',
372
+ ' 基于第三步备齐的成果和既定实施方案,开始具体执行,严格按 phases.planning.approach 落地;出现新事实需要偏离时,必须记录偏离原因、证据与调整后的做法,不得静默改道,执行步骤写入 phases.execution.steps。',
373
+ ' 执行完成后逐条验证验收标准与用户原要求:每条都要有可复核的证据(命令输出/文件/引用);验证未通过不得报 done,无法确认写 unverified;结果写入 phases.verification。',
374
+ '【第四步收尾 · 自我学习复盘(强制)】',
323
375
  ' 报 done 之前必须把 phases.verification.retrospective 写全,四项缺一不可:',
324
376
  ' 1) problems:本次全过程遇到的所有问题(现象 + 根因),至少 1 条;',
325
377
  ' 2) detours:走过的弯路、返工与无效尝试(确实没有就写空数组 [],不要编造);',
326
378
  ' 3) resolutions:最终是如何解决上述问题的,至少 1 条,可与 problems 一一对应;',
327
379
  ' 4) prevention:以后如何预防再犯,至少 1 条,必须写成「遇到…先…」的可执行硬规则。',
328
- ' 插件会在完成判定时复核这四项,并在通过后把复盘写入永久记忆文件;下次派发同类任务时,历史教训会自动出现在提示词里。',
329
- '插件使用说明(三阶段证据门禁):',
330
- ' - 阶段一、二:先把两轮深度思考产物(第一轮任务深研 problemAnalysis、第二轮方法流程步骤 approach、第二轮依据 approachBasis)与执行记录写全;此时不要调用 dsh_workplan_verify,它会因缺少前两阶段证据返回 PHASE_GATE 与待补项(这是证据门禁,不是禁用插件)。',
331
- ' - 阶段三:可以调用 dsh_workplan_verify 让插件做最终核验:提交 planning / execution / verification 三阶段证据,插件返回 passed 或仍需补齐的 gaps;未通过先修复再回报。遇阻时可用 dsh_workplan_escalate 上报,它是安全通道,不属于三阶段产物。',
332
- ' - 结果块里的 phases 是必填项:缺任一阶段、内容为空、或验证未全部通过,插件都会判为未完成,不能用 done 收尾。',
380
+ '【第五步 · JSON mode 结构化交付给本部门负责人】',
381
+ ' 执行完美完成后,必须把成果以 JSON mode 结构化交回本部门负责人:专家写入 report 对象(只允许 keyResults / keyInfo / importantInfo / leadRequirements 四个字符串数组字段,只写关键信息、关键成果、重要信息和负责人要求的东西,禁止过程流水账与重复表述);部门负责人写入 departmentSummary 对象(keyResults / keyInfo / importantInfo / sources,汇总本部门全部专家汇报,sources 必须覆盖全部已派发专家)。成果块必须放在 <!-- WORKPLAN_EVIDENCE:START --> 与 <!-- WORKPLAN_EVIDENCE:END --> 之间,且是合法 JSON,不得用散文代替。',
382
+ '插件使用说明(五步证据门禁):',
383
+ ' - 第一、二、三步:先把问题梳理、资源/工具/实施方案、准备与就绪证据写全;此时不要调用 dsh_workplan_verify,它会因缺少前几步证据返回 PHASE_GATE 与待补项(这是证据门禁,不是禁用插件)。',
384
+ ' - 第四步:可以调用 dsh_workplan_verify 让插件做最终核验:提交 planning / preparation / execution / verification 证据,插件返回 passed 或仍需补齐的 gaps;未通过先修复再回报。遇阻时可用 dsh_workplan_escalate 上报,它是安全通道,不属于五步产物。',
385
+ ' - 结果块里的 phases 是必填项:缺 planning / preparation / execution / verification 任一组、内容为空、或验证未全部通过,插件都会判为未完成,不能用 done 收尾;第五步缺 report / departmentSummary 同样判为未完成。',
333
386
  ];
334
387
  }
335
388
  /**
@@ -25,10 +25,16 @@
25
25
  * 3. check — compare the plan (and the model's own restatement) against the
26
26
  * read-only anchor, and report every dropped hard element.
27
27
  *
28
- * Everything here is pure: no model call, no network, no clock, no filesystem.
29
- * The same inputs always produce the same audit, which makes the result
30
- * reproducible and unit-testable. A model-based reviewer would be *another*
31
- * opinion, not evidence — so it is deliberately not part of this path.
28
+ * Almost everything here is pure: no model call, no network, no filesystem.
29
+ * The audit functions always produce the same result for the same inputs,
30
+ * which makes them reproducible and unit-testable. A model-based reviewer
31
+ * would be *another* opinion, not evidence — so it is deliberately not part
32
+ * of this path.
33
+ *
34
+ * The one deliberate exception is the clarify gate (auditClarifyNeed): it
35
+ * keeps a small in-memory round counter per session with a wall-clock TTL so
36
+ * the gate cannot ask the user the same class of question forever (see
37
+ * CLARIFY_GATE_MAX_ROUNDS below). The audit stages themselves stay pure.
32
38
  *
33
39
  * Failure policy (why not everything blocks)
34
40
  * ------------------------------------------
@@ -568,7 +574,7 @@ export function resolvePronounAntecedentInline(token, taskText) {
568
574
  * C4 missing UI object — a display change (折叠/展开/按钮…) that names no surface
569
575
  * (左侧栏/截图/插件…), so any implementation would be a guess.
570
576
  */
571
- export function auditClarifyNeed(taskText, conversationContext = '') {
577
+ function auditClarifyNeedRules(taskText, conversationContext = '') {
572
578
  const text = String(taskText ?? '').trim();
573
579
  const conversation = String(conversationContext ?? '');
574
580
  if (!text)
@@ -625,6 +631,79 @@ export function auditClarifyNeed(taskText, conversationContext = '') {
625
631
  }
626
632
  return { needed: false, rule: '', reason: '', question: '' };
627
633
  }
634
+ /* ------------------------------------------------------------------------ */
635
+ /* [3.0.2] 澄清闸轮次上限:同一会话连问 CLARIFY_GATE_MAX_ROUNDS 轮后放行 */
636
+ /* ------------------------------------------------------------------------ */
637
+ /**
638
+ * [3.0.2] 同一会话最多向用户发起 CLARIFY_GATE_MAX_ROUNDS 轮澄清提问。
639
+ *
640
+ * 背景:澄清闸(runtime 规划前拦截 + validatePlanContext 的 CLARIFY 错误)
641
+ * 对判定永远成立的任务会无限「问一句→用户补一点→还是缺→再问一句」,
642
+ * 计划永远进不来。达到上限后,该会话后续 auditClarifyNeed 一律放行
643
+ * (needed:false),把任务交还规划路径,由规划器按 assumption 显式标注
644
+ * 推进,而不是把会话钉死在提问环里。
645
+ */
646
+ export const CLARIFY_GATE_MAX_ROUNDS = 2;
647
+ /** 保守自愈:距上次触发超过 30 分钟视为新一轮会话目标,计数自动归零。 */
648
+ const CLARIFY_ROUNDS_TTL_MS = 30 * 60 * 1000;
649
+ /** 调用方未提供 sessionId 时的共享计数键。 */
650
+ const DEFAULT_CLARIFY_SESSION = '__default__';
651
+ const clarifyRounds = new Map();
652
+ /*
653
+ * 时钟可注入(测试用)。生产固定为 Date.now()。
654
+ * 说明:宿主 runtime 目前不传 sessionId(runtime.ts 归并行维护),因此
655
+ * 生产上所有会话共享默认键 + 30 分钟 TTL 自愈;若宿主侧能 hook 「会话
656
+ * 开始新规划」的时机,应调用 resetClarifyRounds(sessionId) 立即清零,
657
+ * 而不是等 30 分钟 TTL。
658
+ */
659
+ let clarifyGateClock = () => Date.now();
660
+ /** 测试专用:注入/恢复澄清闸时钟(不传参数则恢复真实时钟)。 */
661
+ export function __setClarifyGateClockForTests(clock) {
662
+ clarifyGateClock = clock ?? (() => Date.now());
663
+ }
664
+ /**
665
+ * 清空指定会话的澄清轮次计数。
666
+ *
667
+ * 意图调用时机:「会话开始新规划」的可感知点(新任务锚点落进规划器)。
668
+ * 当前宿主尚未 hook 该点(runtime.ts 不在本文件维护范围内),所以生产上
669
+ * 依赖 30 分钟 TTL 自愈;宿主一旦能感知规划起点,应调用本函数立即清零。
670
+ */
671
+ export function resetClarifyRounds(sessionId) {
672
+ clarifyRounds.delete(String(sessionId ?? '').trim() || DEFAULT_CLARIFY_SESSION);
673
+ }
674
+ /**
675
+ * [3.0.2] 澄清判定对外唯一入口:规则判定 + 轮次上限。
676
+ *
677
+ * - 规则未命中(含空文本):纯透传,不计数;
678
+ * - 规则命中且轮次未达上限:计数 +1 并原样返回 needed:true;
679
+ * - 同一会话 30 分钟内已触发 CLARIFY_GATE_MAX_ROUNDS 轮:返回
680
+ * needed:false(放行进入规划),不再计数、不刷新时间戳——「被上限挡住」
681
+ * 不算一次触发,TTL 自上次真实提问起算。
682
+ */
683
+ export function auditClarifyNeed(taskText, conversationContext = '', sessionId = '') {
684
+ const verdict = auditClarifyNeedRules(taskText, conversationContext);
685
+ if (!verdict.needed)
686
+ return verdict;
687
+ const key = String(sessionId ?? '').trim() || DEFAULT_CLARIFY_SESSION;
688
+ const now = clarifyGateClock();
689
+ let entry = clarifyRounds.get(key);
690
+ if (entry && now - entry.lastAt > CLARIFY_ROUNDS_TTL_MS) {
691
+ clarifyRounds.delete(key);
692
+ entry = undefined;
693
+ }
694
+ if (entry && entry.count >= CLARIFY_GATE_MAX_ROUNDS) {
695
+ return { needed: false, rule: '', reason: '', question: '' };
696
+ }
697
+ clarifyRounds.set(key, { count: (entry?.count ?? 0) + 1, lastAt: now });
698
+ // 顺手清扫其他会话的过期条目,避免长进程里 Map 只增不减。
699
+ if (clarifyRounds.size > 16) {
700
+ for (const [otherKey, other] of clarifyRounds) {
701
+ if (now - other.lastAt > CLARIFY_ROUNDS_TTL_MS)
702
+ clarifyRounds.delete(otherKey);
703
+ }
704
+ }
705
+ return verdict;
706
+ }
628
707
  /**
629
708
  * Split one clause into candidate noun phrases.
630
709
  *
@@ -1211,7 +1290,7 @@ export function validatePlanContext(plan, context = {}) {
1211
1290
  * [P0-2c] Missing inputs must stop the plan before any department agent runs:
1212
1291
  * MS2 T05-04/09/13/20 were all planned and dispatched with the key input absent.
1213
1292
  */
1214
- const clarify = auditClarifyNeed(context.taskText ?? audit.anchor?.text ?? '', context.conversationContext ?? '');
1293
+ const clarify = auditClarifyNeed(context.taskText ?? audit.anchor?.text ?? '', context.conversationContext ?? '', context.sessionId ?? '');
1215
1294
  if (clarify.needed) {
1216
1295
  errors.push('CLARIFY: ' + clarify.reason + '(规则 ' + clarify.rule + ')');
1217
1296
  }
@@ -123,24 +123,42 @@ export const MIN_ATOMIC_TASK_CHARS = 8;
123
123
  * 并列/多目标的强信号词。命中 1 个以上即视为「一个任务里塞了多件事」。
124
124
  * 只收强信号(明显的并列连词与小标题分隔),不收「和」这类高频虚词,
125
125
  * 避免把正常表述误判成多任务。
126
+ *
127
+ * [3.0.2] 补齐「并/然后/接着」:子 Agent 的任务书常写「改 A 并改 B」「先备份
128
+ * 然后上传」这类口语并列,旧词表全漏(英文盲区事故:英文句点 + 英文 then
129
+ * 都不在判据里,复合任务被当单句放行)。「并且」已含「并」,报错文案按
130
+ * 长词优先去重。
126
131
  */
127
132
  const MULTI_GOAL_MARKERS = Object.freeze([
128
- '并且', '同时', '另外', '还要', '以及', '分别', '顺便',
133
+ '并且', '同时', '另外', '还要', '以及', '分别', '顺便', '并', '然后', '接着',
129
134
  ]);
130
- /** 句末终止符:原子任务必须是单句。 */
131
- const SENTENCE_TERMINATORS = [new RegExp('[。!?!?]', 'g')];
135
+ /**
136
+ * [3.0.2] 「再」只在两个动作之间当连词时才算并列信号(「先备份再修改」)。
137
+ * 不再/再次/再三/再现/再也不/要再 等副词用法不算——「不要再等待」不会被误伤。
138
+ * 若后续实测「再」的误判率仍偏高,按用户裁决可仅保留 并/然后/接着 并删除
139
+ * 本规则。
140
+ */
141
+ const ZAI_CONJUNCTION_RE = /(?<![不未莫无勿别一要])再(?![次三现也])/;
142
+ /**
143
+ * 句末终止符:原子任务必须是单句。
144
+ * [3.0.2] 英文句点 `.` 计入,但只认「后跟空白或文本结尾」的句读——文件名
145
+ * (a.ts)、版本号(1.12.0)、省略号(...)里的点不算句子分隔。
146
+ */
147
+ const SENTENCE_TERMINATORS = [new RegExp('[。!?!?]|\\.(?:\\s|$)', 'g')];
132
148
  /**
133
149
  * 极简任务硬门槛(用户要求 5,用户裁决 A:不满足即拒绝派发并强制拆小)。
134
150
  *
135
151
  * 判据全部可机械复算,不看语义:
136
152
  * 1) 非空且长度在 8–300 字符之间;
137
- * 2) 只有一句话(不出现第二个句末标点,不含分号分句);
138
- * 3) 不含并列强信号词(并且/同时/另外/还要/以及/分别/顺便);
153
+ * 2) 只有一句话(不出现第二个句末标点——含英文句点 `.`,不含分号分句);
154
+ * 3) 不含并列强信号词(并且/并/同时/另外/还要/以及/分别/顺便/然后/接着,
155
+ * 以及两动作之间的连词「再」);
139
156
  * 4) 必须带非空的质量要求(用户要求 6:交代清楚成果必须达到的要求和质量)。
140
157
  *
141
158
  * 返回的 hint 是可直接照做的拆小指令,不是一句「不合格」。
142
159
  */
143
160
  export function checkAtomicAssignment(task, qualityRequirements) {
161
+ const rawLength = String(task ?? '').trim().length;
144
162
  const text = String(task ?? '').replace(/\s+/g, ' ').trim();
145
163
  if (text.length < MIN_ATOMIC_TASK_CHARS) {
146
164
  return {
@@ -154,7 +172,9 @@ export function checkAtomicAssignment(task, qualityRequirements) {
154
172
  return {
155
173
  ok: false,
156
174
  code: 'DELEGATE_TASK_NOT_ATOMIC',
157
- error: `分到的任务太长(${text.length} 字符 > ${MAX_ATOMIC_TASK_CHARS}),不是 1+1=2 的单项任务。`,
175
+ // [3.0.2] 如实报告截断后实际长度:调用方(llm-rules truncate(1500))先截断
176
+ // 再送检,这里的数字是被判定文本的真实长度,不是原始输入的长度。
177
+ error: `分到的任务太长(截断后实际 ${rawLength} 字符 > ${MAX_ATOMIC_TASK_CHARS}),不是 1+1=2 的单项任务。`,
158
178
  hint: '按「一个目标 / 一个主产物 / 一项可独立验收」拆成多条;每条只保留一个动作,其余另起一条。',
159
179
  };
160
180
  }
@@ -179,11 +199,15 @@ export function checkAtomicAssignment(task, qualityRequirements) {
179
199
  };
180
200
  }
181
201
  const hit = MULTI_GOAL_MARKERS.filter((marker) => text.includes(marker));
182
- if (hit.length > 0) {
202
+ // 「并且」与「并」同命中时只报长词,报错文案不重复列同一个信号。
203
+ const markers = hit.filter((marker) => !hit.some((other) => other !== marker && other.includes(marker)));
204
+ if (ZAI_CONJUNCTION_RE.test(text))
205
+ markers.push('再');
206
+ if (markers.length > 0) {
183
207
  return {
184
208
  ok: false,
185
209
  code: 'DELEGATE_TASK_NOT_ATOMIC',
186
- error: `分到的任务含并列词「${hit.join('、')}」,说明一个任务里有多件事。`,
210
+ error: `分到的任务含并列词「${markers.join('、')}」,说明一个任务里有多件事。`,
187
211
  hint: '把并列词前后的内容各拆成一条独立任务,每条只做一个动作。',
188
212
  };
189
213
  }
@@ -202,6 +226,14 @@ export function checkAtomicAssignment(task, qualityRequirements) {
202
226
  /* ========================================================================== */
203
227
  /** 单个汇报数组的条数上限:精简的量化口径。 */
204
228
  export const MAX_REPORT_ITEMS = 6;
229
+ /**
230
+ * [3.0.6][第 3 批③] sources(部门汇总「覆盖的专家汇报」)专用上限。
231
+ * 死锁修复:复杂任务按用户裁决 D1/D2 强制 ≥8 名专家(上限 12),而 sources
232
+ * 必须覆盖全部已派发专家——若与其他数组共用 6 条上限,7–12 人的部门永远
233
+ * 交不出合法汇总(覆盖诚实要求 vs 精简上限互相矛盾)。覆盖是硬要求,
234
+ * 故 sources 单独放宽到 16(≥ 复杂任务 12 人上限 + 余量),其余数组仍 6 条。
235
+ */
236
+ export const MAX_SOURCES_ITEMS = 16;
205
237
  /** 单条汇报文本的字符上限。 */
206
238
  export const MAX_REPORT_ITEM_CHARS = 200;
207
239
  /** 专家汇报的四个必填字段(与用户要求 7 的四个方向一一对应)。 */
@@ -311,8 +343,9 @@ function checkListField(container, name, violations, options) {
311
343
  if (lines.length < options.minItems) {
312
344
  violations.push(`${name} 至少 ${options.minItems} 条非空内容,当前 ${lines.length} 条`);
313
345
  }
314
- if (lines.length > MAX_REPORT_ITEMS) {
315
- violations.push(`${name} 最多 ${MAX_REPORT_ITEMS} 条(精简要求),当前 ${lines.length} 条,请合并去重`);
346
+ const maxItems = options.maxItems ?? MAX_REPORT_ITEMS;
347
+ if (lines.length > maxItems) {
348
+ violations.push(`${name} 最多 ${maxItems} 条(精简要求),当前 ${lines.length} 条,请合并去重`);
316
349
  }
317
350
  for (const line of lines) {
318
351
  if (line.length > MAX_REPORT_ITEM_CHARS) {
@@ -377,7 +410,7 @@ export function checkDepartmentSummary(summary, expectedExperts = []) {
377
410
  checkListField(container, 'keyResults', violations, { minItems: 1, dedupeAcross: ['keyInfo', 'importantInfo'] });
378
411
  checkListField(container, 'keyInfo', violations, { minItems: 0, dedupeAcross: ['keyResults', 'importantInfo'] });
379
412
  checkListField(container, 'importantInfo', violations, { minItems: 0, dedupeAcross: ['keyResults', 'keyInfo'] });
380
- const covered = checkListField(container, 'sources', violations, { minItems: 0 });
413
+ const covered = checkListField(container, 'sources', violations, { minItems: 0, maxItems: MAX_SOURCES_ITEMS });
381
414
  if (expectedExperts.length > 0) {
382
415
  const joined = covered.join('\n').toLowerCase();
383
416
  const missing = expectedExperts.filter((member) => member.trim() !== '' && !joined.includes(member.trim().toLowerCase()));
@@ -420,7 +453,7 @@ export function renderDepartmentSummarySpec() {
420
453
  ' "sources": ["专家名:一句关键成果"]',
421
454
  ' }',
422
455
  ...DEPARTMENT_SUMMARY_FIELDS.map((field) => `- ${field.name}(${field.label}):${field.description}`),
423
- `- 每个数组最多 ${MAX_REPORT_ITEMS} 条、每条最多 ${MAX_REPORT_ITEM_CHARS} 字符;sources 必须覆盖本部门全部已派发专家,同一信息只能出现一次。`,
456
+ `- keyResults/keyInfo/importantInfo 每个数组最多 ${MAX_REPORT_ITEMS} 条、每条最多 ${MAX_REPORT_ITEM_CHARS} 字符;sources 最多 ${MAX_SOURCES_ITEMS} 条(不受 6 条精简上限约束——必须逐人覆盖本部门全部已派发专家,覆盖是诚实硬要求),同一信息只能出现一次。`,
424
457
  '- 门禁:缺 departmentSummary、字段缺失、未覆盖全部专家、或出现重复项,结果一律退回重交(DEPARTMENT_SUMMARY_INVALID / DEPARTMENT_SUMMARY_NOT_CONCISE)。',
425
458
  ].join('\n');
426
459
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-workplan",
3
- "version": "3.0.2",
3
+ "version": "3.0.6",
4
4
  "description": "Automatic task decomposition, real department subagents, and a responsive workplan dashboard for DeepSeek Harness.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -69,6 +69,66 @@ const APPROVAL_PATTERNS: Array<{ re: RegExp; label: string }> = [
69
69
  },
70
70
  ]
71
71
 
72
+ /*
73
+ * [3.0.2] 命中候选的语境排除:否定 / 引用 / 规章制度转述。
74
+ *
75
+ * 正则只看字面,两种常见文本会被误按:
76
+ * 1. 否定句——「不用经用户认可再执行,直接干」:用户恰恰在**取消**门槛,
77
+ * 字面却完整命中「经…认可再…」;
78
+ * 2. 引用/转述——用户粘贴规章制度原文(「制度规定:须经用户批准后再执行」)
79
+ * 或把门槛语当引文讨论,不是在给自己的任务设门槛。
80
+ *
81
+ * 排除判据(命中位置之前约 40 字符窗口内,命中任一则该条候选不触发):
82
+ * - 否定词:不用/无需/不必/不需要/禁止/谈不上;单字 勿/别 须紧贴命中
83
+ * (6 字内且无句断),避免「识别/分别/级别」里的「别」误伤;
84
+ * - 引号包裹:「」“”‘’以及直引号成对出现、且命中片段位于引号内;
85
+ * - 转述字样:原文/制度/规定/条款。
86
+ * 窗口只回看、不越过句读推断语义——拿不准时维持原判定(宁漏勿冤的
87
+ * 方向对漏判更有利:漏判只是回到默认自动派发,误判才会把普通任务按住)。
88
+ */
89
+ const NEGATION_WORDS: readonly string[] = Object.freeze(['不用', '无需', '不必', '不需要', '禁止', '谈不上'])
90
+ /** 单字否定必须紧贴命中(其后 ≤6 字即命中、中间无句末符),否则视为普通用字。 */
91
+ const SINGLE_CHAR_NEGATION = /[勿别][^。;!?\n]{0,6}$/
92
+ /** 用户转述规章制度时的常见字样(40 字窗口内出现即视为引用而非本任务门槛)。 */
93
+ const RULES_QUOTE_WORDS: readonly string[] = Object.freeze(['原文', '制度', '规定', '条款'])
94
+ /** 成对引号:「」“”‘’为不对称对;直引号 " 与 ' 按出现次数奇偶判定。 */
95
+ const QUOTE_PAIRS: ReadonlyArray<readonly [string, string]> = Object.freeze([
96
+ ['「', '」'], ['“', '”'], ['‘', '’'], ['"', '"'], ["'", "'"],
97
+ ])
98
+
99
+ function hasNegationBefore(window: string): boolean {
100
+ for (const word of NEGATION_WORDS) {
101
+ if (window.includes(word)) return true
102
+ }
103
+ return SINGLE_CHAR_NEGATION.test(window)
104
+ }
105
+
106
+ function isInsideQuotes(text: string, index: number): boolean {
107
+ const before = text.slice(0, index)
108
+ for (const [open, close] of QUOTE_PAIRS) {
109
+ if (open === close) {
110
+ let count = 0
111
+ for (let position = 0; position < before.length; position += 1) {
112
+ if (before[position] === open) count += 1
113
+ }
114
+ if (count % 2 === 1) return true
115
+ } else if (before.lastIndexOf(open) > before.lastIndexOf(close)) {
116
+ return true
117
+ }
118
+ }
119
+ return false
120
+ }
121
+
122
+ /** 命中位置之前约 40 字符窗口内是否存在否定/引用/转述标记。 */
123
+ function hasExemptContext(text: string, index: number): boolean {
124
+ const window = text.slice(Math.max(0, index - 40), index)
125
+ if (hasNegationBefore(window)) return true
126
+ for (const word of RULES_QUOTE_WORDS) {
127
+ if (window.includes(word)) return true
128
+ }
129
+ return isInsideQuotes(text, index)
130
+ }
131
+
72
132
  function excerptOf(text: string, match: RegExpExecArray, max = 60): string {
73
133
  const start = Math.max(0, match.index - 6)
74
134
  const end = Math.min(text.length, match.index + match[0].length + 10)
@@ -85,9 +145,23 @@ export function detectApprovalGate(taskText: string): ApprovalGateVerdict {
85
145
  if (!text) return { held: false, matched: [], reason: '' }
86
146
  const matched: ApprovalGateMatch[] = []
87
147
  for (const { re, label } of APPROVAL_PATTERNS) {
88
- const found = re.exec(text)
89
- if (found) {
90
- matched.push({ label, excerpt: excerptOf(text, found) })
148
+ // [3.0.2] 逐处匹配并跳过带否定/引用语境的候选;同一条规则后一处干净命中
149
+ // 仍可触发(如先转述制度、再给自己的任务立门槛)。
150
+ const scanner = new RegExp(re.source, re.flags.includes('g') ? re.flags : `${re.flags}g`)
151
+ let found: RegExpExecArray | null
152
+ let hit: RegExpExecArray | null = null
153
+ while ((found = scanner.exec(text)) !== null) {
154
+ if (found[0] === '') {
155
+ scanner.lastIndex += 1
156
+ continue
157
+ }
158
+ if (!hasExemptContext(text, found.index)) {
159
+ hit = found
160
+ break
161
+ }
162
+ }
163
+ if (hit) {
164
+ matched.push({ label, excerpt: excerptOf(text, hit) })
91
165
  // 一条规则命中即可;继续跑完所有规则只为汇总证据,不去重(同义重复无害)。
92
166
  }
93
167
  }