dsh-workplan 2.3.2 → 2.3.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.
package/src/plan.ts CHANGED
@@ -211,6 +211,75 @@ export const LIKELIHOODS = Object.freeze(['low', 'medium', 'high'])
211
211
  export const REASONING_EFFORTS = Object.freeze(['low', 'medium', 'high', 'xhigh', 'max'])
212
212
  export const DEFAULT_REASONING_EFFORT = 'high'
213
213
 
214
+ /*
215
+ * [2.3.9][T3] LLM 家族静态规则(用户 2026-11-10 强制要求 1/2,主 Agent 权威口径)。
216
+ *
217
+ * 单一事实源:家族判定与等级集合只在本处定义;runtime(派发/规划入口)、
218
+ * replan(校验)、client(UI Pill)一律从这里取数,禁止第二份表。
219
+ *
220
+ * 用户规则原文:
221
+ * 1. 字母 qwen 开头的模型,推理等级只包括 low、medium、xhigh——轻度任务
222
+ * 使用 low,比较复杂的任务使用 medium,非常复杂的任务使用 xhigh。
223
+ * 2. 非 qwen 开头的其他大模型,推理等级只包括 low、high、max——轻度任务
224
+ * 使用 low,比较复杂的任务使用 high,非常复杂的任务使用 max。
225
+ *
226
+ * 与存储全集的关系(不得删档):REASONING_EFFORTS 五档仍是**存储全集**(计划
227
+ * JSON、序列化回放、旧计划的合法域);本表是**执行前约束**——派发/规划发送
228
+ * 前按当前模型家族收窄,再与宿主能力清单取交集钳制(两段各自留痕)。
229
+ * 模型名缺失 ⇒ classifyModelFamily 返回 null ⇒ 不施加静态约束,保持旧行为
230
+ * (与 [2.2.7]「无从判定⇒不阻断」同风格,防凭空收紧误伤五档全支持模型)。
231
+ */
232
+ export type ModelFamily = 'qwen' | 'other'
233
+ export const QWEN_MODEL_EFFORTS: readonly string[] = Object.freeze(['low', 'medium', 'xhigh'])
234
+ export const OTHER_MODEL_EFFORTS: readonly string[] = Object.freeze(['low', 'high', 'max'])
235
+
236
+ /** 判定模型家族:qwen 开头(trim+小写)→ 'qwen';其余非空 → 'other';空/缺失 → null(不约束)。 */
237
+ export function classifyModelFamily(modelId?: string | null): ModelFamily | null {
238
+ const normalized = String(modelId ?? '').trim().toLowerCase()
239
+ if (!normalized) return null
240
+ return normalized.startsWith('qwen') ? 'qwen' : 'other'
241
+ }
242
+
243
+ /** 家族 → 允许等级集合(用户要求 1/2 硬白名单);未知家族 → null = 不施加约束。 */
244
+ export function effortsForModelFamily(family?: ModelFamily | null): readonly string[] | null {
245
+ if (family === 'qwen') return QWEN_MODEL_EFFORTS
246
+ if (family === 'other') return OTHER_MODEL_EFFORTS
247
+ return null
248
+ }
249
+
250
+ /** 一步式:按模型 id 得到等级集合;模型名缺失返回 null(不约束)。 */
251
+ export function allowedEffortsForModel(modelId?: string | null): readonly string[] | null {
252
+ return effortsForModelFamily(classifyModelFamily(modelId))
253
+ }
254
+
255
+ /**
256
+ * 复杂度 → 档位映射表(要求 1/2 的「轻度/比较复杂/非常复杂」数值化;
257
+ * 部门负责人与专家/组员选档的官方依据,要求 5)。
258
+ */
259
+ export type TaskComplexity = 'light' | 'moderate' | 'heavy'
260
+ export const COMPLEXITY_EFFORT_BY_FAMILY: Readonly<Record<ModelFamily, Record<TaskComplexity, string>>> =
261
+ Object.freeze({
262
+ qwen: Object.freeze({ light: 'low', moderate: 'medium', heavy: 'xhigh' }),
263
+ other: Object.freeze({ light: 'low', moderate: 'high', heavy: 'max' }),
264
+ })
265
+
266
+ /** 按家族+复杂度取应使用档位;家族未知返回 null。 */
267
+ export function effortForComplexity(family: ModelFamily | null, complexity: TaskComplexity): string | null {
268
+ if (!family) return null
269
+ return COMPLEXITY_EFFORT_BY_FAMILY[family][complexity]
270
+ }
271
+
272
+ /** 集合内最高档(强度序与 runtime EFFORT_STRENGTH_ORDER 同源);空集合返回 null。 */
273
+ export function highestEffortOf(levels: readonly string[] | null): string | null {
274
+ if (!levels || levels.length === 0) return null
275
+ const order = ['off', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max']
276
+ let best: string | null = null
277
+ for (const id of levels) {
278
+ if (best === null || order.indexOf(id) > order.indexOf(best)) best = id
279
+ }
280
+ return best
281
+ }
282
+
214
283
  export const DEPARTMENTS = Object.freeze({
215
284
  engineering: { zh: '工程研发', en: 'Engineering' },
216
285
  research: { zh: '研究分析', en: 'Research' },
@@ -1023,10 +1092,13 @@ function checkPlanGraph(plan: any, errors: string[]) {
1023
1092
  /**
1024
1093
  * Validate an untrusted planner object. Never throws for malformed input.
1025
1094
  *
1026
- * @param {unknown} raw
1027
- * @returns {{ok: true, plan: object} | {ok: false, errors: string[]}}
1095
+ * [2.3.9][T3] options.allowedEfforts:当前模型家族的等级集合(真源
1096
+ * allowedEffortsForModel)。传入时,规划器**显式写出**的集合外档位直接拒绝并
1097
+ * 点名(事前约束,要求 1/2;缺失/回落的 high 不在此列——派发侧静态收窄会带
1098
+ * qwen-rule 留痕降级,向后兼容旧回放零迁移)。null/未传 ⇒ 不施加(回放、旧
1099
+ * 调用零行为变化)。REASONING_EFFORTS 五档仍是存储全集,不得删档。
1028
1100
  */
1029
- export function validatePlan(raw: any) {
1101
+ export function validatePlan(raw: any, options?: { allowedEfforts?: readonly string[] | null }) {
1030
1102
  if (!isRecord(raw)) {
1031
1103
  return { ok: false, errors: ['计划必须是 JSON 对象'] }
1032
1104
  }
@@ -1043,6 +1115,31 @@ export function validatePlan(raw: any) {
1043
1115
  verification: readVerification(raw.verification, errors),
1044
1116
  practiceLoop: readPracticeLoop(raw.practiceLoop, errors),
1045
1117
  }
1118
+ const allowedEfforts = options && options.allowedEfforts ? options.allowedEfforts : null
1119
+ if (allowedEfforts !== null && Array.isArray(raw.stages)) {
1120
+ /*
1121
+ * [2.3.9][T3/llm-rules] 模型感知的校验收窄(用户要求 1/2,主 Agent 口径):
1122
+ * 五档仍是存储全集(不得删档),但调用方传入当前模型家族等级集合时,
1123
+ * 规划器**显式写出**的集合外档位拒绝并点名字段——错误经重试回路回喂
1124
+ * 规划器自纠,属于事前约束而非派发后静默钳制。缺失档位不在此拦(由
1125
+ * readSubtasks 回落 high、派发侧静态收窄带 qwen-rule 留痕降级);未传
1126
+ * options 的调用(回放/旧路径)零行为变化。
1127
+ */
1128
+ for (let stageIndex = 0; stageIndex < raw.stages.length; stageIndex += 1) {
1129
+ const stage = raw.stages[stageIndex]
1130
+ if (!isRecord(stage) || !Array.isArray(stage.subtasks)) continue
1131
+ for (let subtaskIndex = 0; subtaskIndex < stage.subtasks.length; subtaskIndex += 1) {
1132
+ const subtask = stage.subtasks[subtaskIndex]
1133
+ if (!isRecord(subtask)) continue
1134
+ const effort = subtask.reasoningEffort
1135
+ if (effort !== undefined && effort !== null
1136
+ && !(typeof effort === 'string' && allowedEfforts.includes(effort))) {
1137
+ push(errors, `stages[${stageIndex}].subtasks[${subtaskIndex}].reasoningEffort`,
1138
+ `"${String(effort)}" 不在当前模型家族的推理等级集合 {${allowedEfforts.join(' / ')}} 内(qwen 系仅 low / medium / xhigh;非 qwen 系仅 low / high / max),必须改用集合内档位或删除该字段`)
1139
+ }
1140
+ }
1141
+ }
1142
+ }
1046
1143
  checkPlanGraph(plan, errors)
1047
1144
  if (errors.length > 0) return { ok: false, errors }
1048
1145
  return { ok: true, plan }
@@ -3347,6 +3444,14 @@ export function buildUpdateMessage(update: any) {
3347
3444
  * deriveSubtaskTimeoutMs。
3348
3445
  */
3349
3446
  ...(update.timeoutMs === undefined ? {} : { timeoutMs: update.timeoutMs }),
3447
+ /*
3448
+ * [2.3.9][T4b] 梳理门禁与第三层分工必须随 durable workplan-update 回放:
3449
+ * 刷新后门禁判据(consolidation)若只剩内存一份,重启的负责人可以不经
3450
+ * 梳理直接 dispatch,硬门禁就只是「本进程内的礼貌」。delegates 是看板
3451
+ * 第三层的数据源,同理。
3452
+ */
3453
+ ...(update.consolidation === undefined ? {} : { consolidation: update.consolidation }),
3454
+ ...(update.delegates === undefined ? {} : { delegates: update.delegates }),
3350
3455
  ...(update.revision === undefined ? {} : { revision: update.revision }),
3351
3456
  at: update.at ?? Date.now(),
3352
3457
  }
@@ -3827,6 +3932,61 @@ export function applyStatus(statuses: any, subtaskId: any, patch: any) {
3827
3932
  .filter((item: any) => isRecord(item) && typeof item.id === 'string')
3828
3933
  .map((item: any) => ({ id: item.id, reason: truncate(String(item.reason ?? ''), 300) }))
3829
3934
  }
3935
+ /*
3936
+ * [2.3.9][T4b] 第三层派发的留档字段(用户要求 4/5:负责人「先全面梳理、
3937
+ * 再分配」的硬门禁产物 + 顶尖专家/组员的分工与实时状态)。
3938
+ *
3939
+ * `consolidation` 是梳理门禁的唯一判据:没有它,delegateTool 的 dispatch
3940
+ * 动作必须被拒(A4)。`delegates` 是看板第三层的渲染数据源(A3),其
3941
+ * requested/corrected 对就是等级钳制的留痕(A5)。两者只写同一条状态行、
3942
+ * 走同一个 durable workplan-update 回放,刷新/重启后门禁与看板不失效。
3943
+ */
3944
+ if (patch.consolidation !== undefined) {
3945
+ if (patch.consolidation === null) next.consolidation = null
3946
+ else if (isRecord(patch.consolidation)) {
3947
+ const briefStrings = (value: any, limit: number, itemMax: number) => (Array.isArray(value) ? value : [])
3948
+ .map((entry: any) => truncate(String(entry ?? '').trim(), itemMax))
3949
+ .filter(Boolean)
3950
+ .slice(0, limit)
3951
+ next.consolidation = {
3952
+ problemAnalysis: briefStrings(patch.consolidation.problemAnalysis, 12, 500),
3953
+ constraints: briefStrings(patch.consolidation.constraints, 12, 300),
3954
+ unknowns: briefStrings(patch.consolidation.unknowns, 12, 300),
3955
+ approach: briefStrings(patch.consolidation.approach, 12, 500),
3956
+ assignments: (Array.isArray(patch.consolidation.assignments) ? patch.consolidation.assignments : [])
3957
+ .slice(0, 6)
3958
+ .map((item: any) => ({
3959
+ member: truncate(String(item?.member ?? ''), 80),
3960
+ role: item?.role === 'expert' ? 'expert' : 'member',
3961
+ task: truncate(String(item?.task ?? ''), 800),
3962
+ reasoningEffort: REASONING_EFFORTS.includes(item?.reasoningEffort) ? item.reasoningEffort : 'low',
3963
+ ...(item?.requested && item.requested !== item.reasoningEffort
3964
+ ? { requested: String(item.requested) }
3965
+ : {}),
3966
+ })),
3967
+ submittedAt: numberOrNull(patch.consolidation.submittedAt) ?? Date.now(),
3968
+ }
3969
+ } else {
3970
+ throw new PlanError('consolidation 必须是对象或 null', 'INVALID_CONSOLIDATION')
3971
+ }
3972
+ }
3973
+ if (patch.delegates !== undefined) {
3974
+ next.delegates = (Array.isArray(patch.delegates) ? patch.delegates : [])
3975
+ .slice(0, 6)
3976
+ .map((item: any) => ({
3977
+ member: truncate(String(item?.member ?? ''), 80),
3978
+ role: item?.role === 'expert' ? 'expert' : 'member',
3979
+ task: truncate(String(item?.task ?? ''), 300),
3980
+ effort: REASONING_EFFORTS.includes(item?.effort) ? item.effort : 'low',
3981
+ ...(item?.requested && item.requested !== item.effort ? { requested: String(item.requested) } : {}),
3982
+ childId: typeof item?.childId === 'string' ? truncate(item.childId, 200) : null,
3983
+ status: ['running', 'done', 'failed', 'timeout', 'rejected'].includes(item?.status) ? item.status : 'running',
3984
+ summary: typeof item?.summary === 'string' ? truncate(item.summary, 300) : '',
3985
+ error: typeof item?.error === 'string' ? truncate(item.error, 300) : null,
3986
+ startedAt: numberOrNull(item?.startedAt),
3987
+ finishedAt: numberOrNull(item?.finishedAt),
3988
+ }))
3989
+ }
3830
3990
  /*
3831
3991
  * [1.7.0] Monotonic revision + author.
3832
3992
  *
@@ -3948,6 +4108,13 @@ export function applyStatus(statuses: any, subtaskId: any, patch: any) {
3948
4108
  if (patch.blockedBy === undefined) next.blockedBy = undefined
3949
4109
  // [1.10.6] A conflict belongs to one attempt; a reset starts a new one.
3950
4110
  if (patch.artifactConflict === undefined) next.artifactConflict = undefined
4111
+ /*
4112
+ * [2.3.9][T4b] 第三层痕迹属于上一次尝试:重置后新负责人必须重新梳理、
4113
+ * 旧专家/组员的子会话也已随上一attempt终止——留着它们会让新行「看起来
4114
+ * 已梳理/已在派」,门禁(consolidation)与看板(delegates)双双失真。
4115
+ */
4116
+ if (patch.consolidation === undefined) next.consolidation = undefined
4117
+ if (patch.delegates === undefined) next.delegates = undefined
3951
4118
  }
3952
4119
  if (patch.status === 'in_progress') {
3953
4120
  if (next.startedAt === null) next.startedAt = Date.now()
@@ -4951,6 +5118,13 @@ export function buildChildPrompt({
4951
5118
  */
4952
5119
  leadAgentId = '',
4953
5120
  leadAgentIdSource = 'none',
5121
+ /*
5122
+ * [2.3.9][T4b] 用户强制要求 3:分配任务时必须把要求 1/2 全文告知相关部门。
5123
+ * 注入唯一事实源在 llm-rules.ts(buildLlmRulesBrief,tier='department');
5124
+ * 由派发调用点传入(plan.ts 不能反向 import llm-rules.ts,会造成循环依赖)。
5125
+ * 空串 = 旧调用方(旧测试/旧路径)零变化;新派发链恒传非空。
5126
+ */
5127
+ llmRulesBrief = '',
4954
5128
  }: any) {
4955
5129
  const noticeTarget = readLeadAgentId(leadAgentId)
4956
5130
  const noticeCall = leadNoticeCallForm(noticeTarget)
@@ -5096,6 +5270,7 @@ export function buildChildPrompt({
5096
5270
  `全局任务:${understanding.originalRequest ?? '(未记录)'}`,
5097
5271
  `最终交付物:${understanding.deliverable ?? '(未记录)'}`,
5098
5272
  ...anchorBlock,
5273
+ ...(llmRulesBrief ? ['', String(llmRulesBrief), ''] : []),
5099
5274
  `主要矛盾:${contradiction}`,
5100
5275
  `当前阶段:${stageName}(${stageGoal})`,
5101
5276
  `工作目录:${cwd || '(未知)'}`,
package/src/replan.ts CHANGED
@@ -237,7 +237,7 @@ function mergeReplacement(old: any, replacement: any, id: string, reason: string
237
237
  * (T4 D-T4-03)。修复前 add 接受 14 条依赖、200 条 inputs 与 16 万字符
238
238
  * 载荷,这些计划再也回灌不了 validatePlan,却能被真实派发。
239
239
  */
240
- function replanFieldError(raw: any): string | null {
240
+ function replanFieldError(raw: any, allowedEfforts?: readonly string[] | null): string | null {
241
241
  const list = (key: string, max: number, itemMax: number, required = false): string | null => {
242
242
  const value = raw?.[key]
243
243
  if (value === undefined) return required ? `${key} 不能为空` : null
@@ -260,6 +260,10 @@ function replanFieldError(raw: any): string | null {
260
260
  }
261
261
  /*
262
262
  * [2.2.7][T4/五档+G1] reasoningEffort 入口白名单(replace/add 两路共用本函数):
263
+ * [2.3.9][T3/同步点] 此处按 REASONING_EFFORTS **存储全集五档**校验是刻意决策:
264
+ * 重规划的输入可能来自回放/他模型计划,删档会破坏零迁移契约;模型家族收窄
265
+ * 统一发生在派发入口(runtime.ts 静态收窄 + qwen-rule/non-qwen-rule 留痕)与
266
+ * 规划校验(validatePlan options.allowedEfforts),口径见 plan.ts L214-231 注释。
263
267
  * 与 plan.ts readSubtasks 语义对齐——缺失/显式 null 合法(replace 继承旧行、
264
268
  * add 留给派发期回落 high),非缺失值必须是 REASONING_EFFORTS 五档之一
265
269
  * (low / medium / high / xhigh / max),非法值拒绝、不改写。补上此校验前,
@@ -269,8 +273,15 @@ function replanFieldError(raw: any): string | null {
269
273
  const effortError = effort === undefined || effort === null
270
274
  ? null
271
275
  : typeof effort !== 'string' || !REASONING_EFFORTS.includes(effort)
272
- ? 'reasoningEffort 必须是 low / medium / high / xhigh / max 之一(缺失合法:replace 继承旧行、add 派发期回落;非法值拒绝,不改写)'
273
- : null
276
+ ? 'reasoningEffort 必须是 low / medium / high / xhigh / max 之一(缺失合法:replace 继承旧行、add 派发期回落;非法值拒绝,不改写;传入模型家族集合时进一步收窄为该集合)'
277
+ /*
278
+ * [2.3.9][T3] 模型感知收窄口径(用户要求 1/2;与 plan.ts validatePlan、
279
+ * runtime 派发侧同一真源):调用方传入当前模型家族等级集合时,五档合规
280
+ * 但家族越界的档位(如 qwen 系的 high/max)同样拒绝并点名集合,不改写。
281
+ */
282
+ : Array.isArray(allowedEfforts) && allowedEfforts.length > 0 && !allowedEfforts.includes(effort)
283
+ ? `reasoningEffort "${effort}" 不在当前模型家族的推理等级集合 {${allowedEfforts.join(' / ')}} 内(qwen 系仅 low / medium / xhigh;非 qwen 系仅 low / high / max),必须改用集合内档位`
284
+ : null
274
285
  return list('dependsOn', 12, 16)
275
286
  ?? list('method', 12, 800)
276
287
  ?? list('inputs', 12, 800)
@@ -360,7 +371,7 @@ export function planReplan(plan: any, changes: any, options: any = {}): ReplanRe
360
371
  if (replacement.department !== undefined && replanDepartment(replacement.department) === null) {
361
372
  return fail(REPLAN_CODES.INVALID_REPLACEMENT, `replacement.department 不是合法部门:${String(replacement.department)}`)
362
373
  }
363
- const replacementError = replanFieldError(replacement)
374
+ const replacementError = replanFieldError(replacement, options?.allowedEfforts)
364
375
  if (replacementError) return fail(REPLAN_CODES.INVALID_REPLACEMENT, `replacement 字段不合规:${replacementError}`)
365
376
  const requestedId = typeof replacement.id === 'string' ? replacement.id.trim() : ''
366
377
  if (requestedId && (requestedId === subtaskId || orderMap(next).has(requestedId))) {
@@ -414,7 +425,7 @@ export function planReplan(plan: any, changes: any, options: any = {}): ReplanRe
414
425
  if (department === null) {
415
426
  return fail(REPLAN_CODES.INVALID_SUBTASK, `newSubtask.department 不是合法部门:${String(raw.department)}`)
416
427
  }
417
- const fieldError = replanFieldError(raw)
428
+ const fieldError = replanFieldError(raw, options?.allowedEfforts)
418
429
  if (fieldError) return fail(REPLAN_CODES.INVALID_SUBTASK, `newSubtask 字段不合规:${fieldError}`)
419
430
  const goal = typeof raw.objective === 'string' ? raw.objective.trim() : ''
420
431
  const title = typeof raw.title === 'string' ? raw.title.trim() : ''