dsh-workplan 3.0.3 → 3.0.9

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.
@@ -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
  * ------------------------------------------
@@ -216,6 +222,11 @@ export interface PlanAuditContext {
216
222
  sourceMessageIds?: string[]
217
223
  /** `false` downgrades every blocking error to a warning (rollback switch). */
218
224
  strict?: boolean
225
+ /**
226
+ * [3.0.2] 澄清闸轮次计数按会话隔离:调用方传 sessionId 时,
227
+ * 上限/自愈均按该会话独立计算;缺省回落到共享默认键。
228
+ */
229
+ sessionId?: string
219
230
  }
220
231
 
221
232
  export interface PlanContextValidation {
@@ -731,7 +742,7 @@ export function resolvePronounAntecedentInline(token: string, taskText: string):
731
742
  * C4 missing UI object — a display change (折叠/展开/按钮…) that names no surface
732
743
  * (左侧栏/截图/插件…), so any implementation would be a guess.
733
744
  */
734
- export function auditClarifyNeed(
745
+ function auditClarifyNeedRules(
735
746
  taskText: unknown,
736
747
  conversationContext = '',
737
748
  ): { needed: boolean; rule: string; reason: string; question: string } {
@@ -792,6 +803,89 @@ export function auditClarifyNeed(
792
803
  return { needed: false, rule: '', reason: '', question: '' }
793
804
  }
794
805
 
806
+ /* ------------------------------------------------------------------------ */
807
+ /* [3.0.2] 澄清闸轮次上限:同一会话连问 CLARIFY_GATE_MAX_ROUNDS 轮后放行 */
808
+ /* ------------------------------------------------------------------------ */
809
+
810
+ /**
811
+ * [3.0.2] 同一会话最多向用户发起 CLARIFY_GATE_MAX_ROUNDS 轮澄清提问。
812
+ *
813
+ * 背景:澄清闸(runtime 规划前拦截 + validatePlanContext 的 CLARIFY 错误)
814
+ * 对判定永远成立的任务会无限「问一句→用户补一点→还是缺→再问一句」,
815
+ * 计划永远进不来。达到上限后,该会话后续 auditClarifyNeed 一律放行
816
+ * (needed:false),把任务交还规划路径,由规划器按 assumption 显式标注
817
+ * 推进,而不是把会话钉死在提问环里。
818
+ */
819
+ export const CLARIFY_GATE_MAX_ROUNDS = 2
820
+ /** 保守自愈:距上次触发超过 30 分钟视为新一轮会话目标,计数自动归零。 */
821
+ const CLARIFY_ROUNDS_TTL_MS = 30 * 60 * 1000
822
+ /** 调用方未提供 sessionId 时的共享计数键。 */
823
+ const DEFAULT_CLARIFY_SESSION = '__default__'
824
+
825
+ type ClarifyRoundEntry = { count: number; lastAt: number }
826
+ const clarifyRounds = new Map<string, ClarifyRoundEntry>()
827
+
828
+ /*
829
+ * 时钟可注入(测试用)。生产固定为 Date.now()。
830
+ * 说明:宿主 runtime 目前不传 sessionId(runtime.ts 归并行维护),因此
831
+ * 生产上所有会话共享默认键 + 30 分钟 TTL 自愈;若宿主侧能 hook 「会话
832
+ * 开始新规划」的时机,应调用 resetClarifyRounds(sessionId) 立即清零,
833
+ * 而不是等 30 分钟 TTL。
834
+ */
835
+ let clarifyGateClock: () => number = () => Date.now()
836
+
837
+ /** 测试专用:注入/恢复澄清闸时钟(不传参数则恢复真实时钟)。 */
838
+ export function __setClarifyGateClockForTests(clock?: () => number): void {
839
+ clarifyGateClock = clock ?? (() => Date.now())
840
+ }
841
+
842
+ /**
843
+ * 清空指定会话的澄清轮次计数。
844
+ *
845
+ * 意图调用时机:「会话开始新规划」的可感知点(新任务锚点落进规划器)。
846
+ * 当前宿主尚未 hook 该点(runtime.ts 不在本文件维护范围内),所以生产上
847
+ * 依赖 30 分钟 TTL 自愈;宿主一旦能感知规划起点,应调用本函数立即清零。
848
+ */
849
+ export function resetClarifyRounds(sessionId: string): void {
850
+ clarifyRounds.delete(String(sessionId ?? '').trim() || DEFAULT_CLARIFY_SESSION)
851
+ }
852
+
853
+ /**
854
+ * [3.0.2] 澄清判定对外唯一入口:规则判定 + 轮次上限。
855
+ *
856
+ * - 规则未命中(含空文本):纯透传,不计数;
857
+ * - 规则命中且轮次未达上限:计数 +1 并原样返回 needed:true;
858
+ * - 同一会话 30 分钟内已触发 CLARIFY_GATE_MAX_ROUNDS 轮:返回
859
+ * needed:false(放行进入规划),不再计数、不刷新时间戳——「被上限挡住」
860
+ * 不算一次触发,TTL 自上次真实提问起算。
861
+ */
862
+ export function auditClarifyNeed(
863
+ taskText: unknown,
864
+ conversationContext = '',
865
+ sessionId = '',
866
+ ): { needed: boolean; rule: string; reason: string; question: string } {
867
+ const verdict = auditClarifyNeedRules(taskText, conversationContext)
868
+ if (!verdict.needed) return verdict
869
+ const key = String(sessionId ?? '').trim() || DEFAULT_CLARIFY_SESSION
870
+ const now = clarifyGateClock()
871
+ let entry = clarifyRounds.get(key)
872
+ if (entry && now - entry.lastAt > CLARIFY_ROUNDS_TTL_MS) {
873
+ clarifyRounds.delete(key)
874
+ entry = undefined
875
+ }
876
+ if (entry && entry.count >= CLARIFY_GATE_MAX_ROUNDS) {
877
+ return { needed: false, rule: '', reason: '', question: '' }
878
+ }
879
+ clarifyRounds.set(key, { count: (entry?.count ?? 0) + 1, lastAt: now })
880
+ // 顺手清扫其他会话的过期条目,避免长进程里 Map 只增不减。
881
+ if (clarifyRounds.size > 16) {
882
+ for (const [otherKey, other] of clarifyRounds) {
883
+ if (now - other.lastAt > CLARIFY_ROUNDS_TTL_MS) clarifyRounds.delete(otherKey)
884
+ }
885
+ }
886
+ return verdict
887
+ }
888
+
795
889
  /**
796
890
  * Split one clause into candidate noun phrases.
797
891
  *
@@ -1381,7 +1475,7 @@ export function validatePlanContext(plan: any, context: PlanAuditContext = {}):
1381
1475
  * [P0-2c] Missing inputs must stop the plan before any department agent runs:
1382
1476
  * MS2 T05-04/09/13/20 were all planned and dispatched with the key input absent.
1383
1477
  */
1384
- const clarify = auditClarifyNeed(context.taskText ?? audit.anchor?.text ?? '', context.conversationContext ?? '')
1478
+ const clarify = auditClarifyNeed(context.taskText ?? audit.anchor?.text ?? '', context.conversationContext ?? '', context.sessionId ?? '')
1385
1479
  if (clarify.needed) {
1386
1480
  errors.push('CLARIFY: ' + clarify.reason + '(规则 ' + clarify.rule + ')')
1387
1481
  }
@@ -3,8 +3,8 @@
3
3
  *
4
4
  * 单一事实源:两条会话固定文案、部门任务质量要求、极简任务硬门槛、专家精简
5
5
  * JSON 汇报、部门负责人精简 JSON 汇总。渲染函数与校验函数同源,保证提示词里
6
- * 写的规则与门禁实际执行的规则不可能分叉(与 2.x 的 comm-format / three-phase
7
- * 同一哲学)。
6
+ * 写的规则与门禁实际执行的规则不可能分叉(与 2.x 的 comm-format / three-phase、
7
+ * 3.0.8 的 five-step 同一哲学)。
8
8
  *
9
9
  * 纯函数、零依赖(不 import plan.ts,避免循环依赖),可被 host/plan/runtime/
10
10
  * llm-rules 与测试同口径消费。
@@ -142,13 +142,30 @@ export const MIN_ATOMIC_TASK_CHARS = 8
142
142
  * 并列/多目标的强信号词。命中 1 个以上即视为「一个任务里塞了多件事」。
143
143
  * 只收强信号(明显的并列连词与小标题分隔),不收「和」这类高频虚词,
144
144
  * 避免把正常表述误判成多任务。
145
+ *
146
+ * [3.0.2] 补齐「并/然后/接着」:子 Agent 的任务书常写「改 A 并改 B」「先备份
147
+ * 然后上传」这类口语并列,旧词表全漏(英文盲区事故:英文句点 + 英文 then
148
+ * 都不在判据里,复合任务被当单句放行)。「并且」已含「并」,报错文案按
149
+ * 长词优先去重。
145
150
  */
146
151
  const MULTI_GOAL_MARKERS: readonly string[] = Object.freeze([
147
- '并且', '同时', '另外', '还要', '以及', '分别', '顺便',
152
+ '并且', '同时', '另外', '还要', '以及', '分别', '顺便', '并', '然后', '接着',
148
153
  ])
149
154
 
150
- /** 句末终止符:原子任务必须是单句。 */
151
- const SENTENCE_TERMINATORS: readonly RegExp[] = [new RegExp('[。!?!?]', 'g')]
155
+ /**
156
+ * [3.0.2] 「再」只在两个动作之间当连词时才算并列信号(「先备份再修改」)。
157
+ * 不再/再次/再三/再现/再也不/要再 等副词用法不算——「不要再等待」不会被误伤。
158
+ * 若后续实测「再」的误判率仍偏高,按用户裁决可仅保留 并/然后/接着 并删除
159
+ * 本规则。
160
+ */
161
+ const ZAI_CONJUNCTION_RE = /(?<![不未莫无勿别一要])再(?![次三现也])/
162
+
163
+ /**
164
+ * 句末终止符:原子任务必须是单句。
165
+ * [3.0.2] 英文句点 `.` 计入,但只认「后跟空白或文本结尾」的句读——文件名
166
+ * (a.ts)、版本号(1.12.0)、省略号(...)里的点不算句子分隔。
167
+ */
168
+ const SENTENCE_TERMINATORS: readonly RegExp[] = [new RegExp('[。!?!?]|\\.(?:\\s|$)', 'g')]
152
169
 
153
170
  export interface AtomicTaskCheck {
154
171
  ok: boolean
@@ -162,13 +179,15 @@ export interface AtomicTaskCheck {
162
179
  *
163
180
  * 判据全部可机械复算,不看语义:
164
181
  * 1) 非空且长度在 8–300 字符之间;
165
- * 2) 只有一句话(不出现第二个句末标点,不含分号分句);
166
- * 3) 不含并列强信号词(并且/同时/另外/还要/以及/分别/顺便);
182
+ * 2) 只有一句话(不出现第二个句末标点——含英文句点 `.`,不含分号分句);
183
+ * 3) 不含并列强信号词(并且/并/同时/另外/还要/以及/分别/顺便/然后/接着,
184
+ * 以及两动作之间的连词「再」);
167
185
  * 4) 必须带非空的质量要求(用户要求 6:交代清楚成果必须达到的要求和质量)。
168
186
  *
169
187
  * 返回的 hint 是可直接照做的拆小指令,不是一句「不合格」。
170
188
  */
171
189
  export function checkAtomicAssignment(task: string, qualityRequirements?: unknown): AtomicTaskCheck {
190
+ const rawLength = String(task ?? '').trim().length
172
191
  const text = String(task ?? '').replace(/\s+/g, ' ').trim()
173
192
  if (text.length < MIN_ATOMIC_TASK_CHARS) {
174
193
  return {
@@ -182,7 +201,9 @@ export function checkAtomicAssignment(task: string, qualityRequirements?: unknow
182
201
  return {
183
202
  ok: false,
184
203
  code: 'DELEGATE_TASK_NOT_ATOMIC',
185
- error: `分到的任务太长(${text.length} 字符 > ${MAX_ATOMIC_TASK_CHARS}),不是 1+1=2 的单项任务。`,
204
+ // [3.0.2] 如实报告截断后实际长度:调用方(llm-rules truncate(1500))先截断
205
+ // 再送检,这里的数字是被判定文本的真实长度,不是原始输入的长度。
206
+ error: `分到的任务太长(截断后实际 ${rawLength} 字符 > ${MAX_ATOMIC_TASK_CHARS}),不是 1+1=2 的单项任务。`,
186
207
  hint: '按「一个目标 / 一个主产物 / 一项可独立验收」拆成多条;每条只保留一个动作,其余另起一条。',
187
208
  }
188
209
  }
@@ -207,11 +228,14 @@ export function checkAtomicAssignment(task: string, qualityRequirements?: unknow
207
228
  }
208
229
  }
209
230
  const hit = MULTI_GOAL_MARKERS.filter((marker) => text.includes(marker))
210
- if (hit.length > 0) {
231
+ // 「并且」与「并」同命中时只报长词,报错文案不重复列同一个信号。
232
+ const markers = hit.filter((marker) => !hit.some((other) => other !== marker && other.includes(marker)))
233
+ if (ZAI_CONJUNCTION_RE.test(text)) markers.push('再')
234
+ if (markers.length > 0) {
211
235
  return {
212
236
  ok: false,
213
237
  code: 'DELEGATE_TASK_NOT_ATOMIC',
214
- error: `分到的任务含并列词「${hit.join('、')}」,说明一个任务里有多件事。`,
238
+ error: `分到的任务含并列词「${markers.join('、')}」,说明一个任务里有多件事。`,
215
239
  hint: '把并列词前后的内容各拆成一条独立任务,每条只做一个动作。',
216
240
  }
217
241
  }
@@ -232,6 +256,14 @@ export function checkAtomicAssignment(task: string, qualityRequirements?: unknow
232
256
 
233
257
  /** 单个汇报数组的条数上限:精简的量化口径。 */
234
258
  export const MAX_REPORT_ITEMS = 6
259
+ /**
260
+ * [3.0.6][第 3 批③] sources(部门汇总「覆盖的专家汇报」)专用上限。
261
+ * 死锁修复:复杂任务按用户裁决 D1/D2 强制 ≥8 名专家(上限 12),而 sources
262
+ * 必须覆盖全部已派发专家——若与其他数组共用 6 条上限,7–12 人的部门永远
263
+ * 交不出合法汇总(覆盖诚实要求 vs 精简上限互相矛盾)。覆盖是硬要求,
264
+ * 故 sources 单独放宽到 16(≥ 复杂任务 12 人上限 + 余量),其余数组仍 6 条。
265
+ */
266
+ export const MAX_SOURCES_ITEMS = 16
235
267
  /** 单条汇报文本的字符上限。 */
236
268
  export const MAX_REPORT_ITEM_CHARS = 200
237
269
 
@@ -328,7 +360,7 @@ function checkListField(
328
360
  container: Record<string, unknown>,
329
361
  name: string,
330
362
  violations: string[],
331
- options: { minItems: number; dedupeAcross?: readonly string[] },
363
+ options: { minItems: number; maxItems?: number; dedupeAcross?: readonly string[] },
332
364
  ): string[] {
333
365
  const raw = container[name]
334
366
  if (!Array.isArray(raw)) {
@@ -339,8 +371,9 @@ function checkListField(
339
371
  if (lines.length < options.minItems) {
340
372
  violations.push(`${name} 至少 ${options.minItems} 条非空内容,当前 ${lines.length} 条`)
341
373
  }
342
- if (lines.length > MAX_REPORT_ITEMS) {
343
- violations.push(`${name} 最多 ${MAX_REPORT_ITEMS} 条(精简要求),当前 ${lines.length} 条,请合并去重`)
374
+ const maxItems = options.maxItems ?? MAX_REPORT_ITEMS
375
+ if (lines.length > maxItems) {
376
+ violations.push(`${name} 最多 ${maxItems} 条(精简要求),当前 ${lines.length} 条,请合并去重`)
344
377
  }
345
378
  for (const line of lines) {
346
379
  if (line.length > MAX_REPORT_ITEM_CHARS) {
@@ -375,7 +408,7 @@ export interface ReportCheck {
375
408
  /**
376
409
  * 校验专家精简 JSON 汇报(用户要求 7)。
377
410
  *
378
- * 只做结构/精简判定,不判语义真伪(诚信由 F9 与三阶段门禁兜底)。
411
+ * 只做结构/精简判定,不判语义真伪(诚信由 F9 与五步证据门禁兜底)。
379
412
  */
380
413
  export function checkExpertReport(report: unknown): ReportCheck {
381
414
  if (!report || typeof report !== 'object' || Array.isArray(report)) {
@@ -413,7 +446,7 @@ export function checkDepartmentSummary(summary: unknown, expectedExperts: readon
413
446
  checkListField(container, 'keyResults', violations, { minItems: 1, dedupeAcross: ['keyInfo', 'importantInfo'] })
414
447
  checkListField(container, 'keyInfo', violations, { minItems: 0, dedupeAcross: ['keyResults', 'importantInfo'] })
415
448
  checkListField(container, 'importantInfo', violations, { minItems: 0, dedupeAcross: ['keyResults', 'keyInfo'] })
416
- const covered = checkListField(container, 'sources', violations, { minItems: 0 })
449
+ const covered = checkListField(container, 'sources', violations, { minItems: 0, maxItems: MAX_SOURCES_ITEMS })
417
450
  if (expectedExperts.length > 0) {
418
451
  const joined = covered.join('\n').toLowerCase()
419
452
  const missing = expectedExperts.filter((member) => member.trim() !== '' && !joined.includes(member.trim().toLowerCase()))
@@ -458,7 +491,7 @@ export function renderDepartmentSummarySpec(): string {
458
491
  ' "sources": ["专家名:一句关键成果"]',
459
492
  ' }',
460
493
  ...DEPARTMENT_SUMMARY_FIELDS.map((field) => `- ${field.name}(${field.label}):${field.description}`),
461
- `- 每个数组最多 ${MAX_REPORT_ITEMS} 条、每条最多 ${MAX_REPORT_ITEM_CHARS} 字符;sources 必须覆盖本部门全部已派发专家,同一信息只能出现一次。`,
494
+ `- keyResults/keyInfo/importantInfo 每个数组最多 ${MAX_REPORT_ITEMS} 条、每条最多 ${MAX_REPORT_ITEM_CHARS} 字符;sources 最多 ${MAX_SOURCES_ITEMS} 条(不受 6 条精简上限约束——必须逐人覆盖本部门全部已派发专家,覆盖是诚实硬要求),同一信息只能出现一次。`,
462
495
  '- 门禁:缺 departmentSummary、字段缺失、未覆盖全部专家、或出现重复项,结果一律退回重交(DEPARTMENT_SUMMARY_INVALID / DEPARTMENT_SUMMARY_NOT_CONCISE)。',
463
496
  ].join('\n')
464
497
  }