dsh-workplan 2.3.9 → 3.0.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.
- package/CHANGELOG.md +115 -0
- package/README.en.md +2 -2
- package/README.md +2 -2
- package/README.zh.md +2 -2
- package/client.js +68 -8
- package/docs/cc/ADV-FSP-FUNCTIONAL-SPEC.md +3 -3
- package/docs/cc/AVA-VULNERABILITY-ANALYSIS.md +3 -3
- package/lib/client.js +68 -8
- package/lib/comm-format.js +58 -6
- package/lib/criterion.js +215 -0
- package/lib/dept-journal.js +319 -0
- package/lib/host.js +167 -35
- package/lib/llm-rules.js +281 -24
- package/lib/plan.js +1415 -168
- package/lib/replan.js +50 -39
- package/lib/runtime.js +3098 -639
- package/lib/three-phase.js +106 -20
- package/lib/understanding.js +18 -3
- package/lib/v3-contract.js +431 -0
- package/lib/watchdog.js +6 -1
- package/package.json +2 -2
- package/src/client.ts +68 -8
- package/src/comm-format.ts +63 -5
- package/src/criterion.ts +262 -0
- package/src/dept-journal.ts +373 -0
- package/src/host.ts +168 -36
- package/src/llm-rules.ts +349 -27
- package/src/plan.ts +1416 -161
- package/src/replan.ts +48 -37
- package/src/runtime.ts +2518 -139
- package/src/three-phase.ts +147 -20
- package/src/understanding.ts +20 -3
- package/src/v3-contract.ts +470 -0
- package/src/watchdog.ts +6 -1
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [3.0.0] 用户 2026-11-14 十三条底层机制优化中「可由代码判定」的那部分契约。
|
|
3
|
+
*
|
|
4
|
+
* 单一事实源:两条会话固定文案、部门任务质量要求、极简任务硬门槛、专家精简
|
|
5
|
+
* JSON 汇报、部门负责人精简 JSON 汇总。渲染函数与校验函数同源,保证提示词里
|
|
6
|
+
* 写的规则与门禁实际执行的规则不可能分叉(与 2.x 的 comm-format / three-phase
|
|
7
|
+
* 同一哲学)。
|
|
8
|
+
*
|
|
9
|
+
* 纯函数、零依赖(不 import plan.ts,避免循环依赖),可被 host/plan/runtime/
|
|
10
|
+
* llm-rules 与测试同口径消费。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/* ========================================================================== */
|
|
14
|
+
/* 1. 两条会话固定文案(用户要求 2、4) */
|
|
15
|
+
/* ========================================================================== */
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* 用户要求 2 的固定文案:从接收到用户任何任务开始,会话内必须输出这一句。
|
|
19
|
+
* 逐字冻结(含 6 个半角点),不得改写、不得拼接。
|
|
20
|
+
*/
|
|
21
|
+
export const STATUS_ASSIGNING_TEXT = '任务正在分配中,请稍等一下......'
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 用户要求 4 的固定文案:用户任务分配完成后,会话内必须输出这一句。
|
|
25
|
+
* 逐字冻结(含 6 个半角点),不得改写、不得拼接。
|
|
26
|
+
*/
|
|
27
|
+
export const STATUS_ASSIGNED_TEXT = '任务已经完成分配,正在执行中......'
|
|
28
|
+
|
|
29
|
+
/* ========================================================================== */
|
|
30
|
+
/* 2. 部门任务质量要求(用户要求 3、6) */
|
|
31
|
+
/* ========================================================================== */
|
|
32
|
+
|
|
33
|
+
/** 单个子任务质量要求的条数上限(多则失控,与「精简」冲突)。 */
|
|
34
|
+
export const MAX_QUALITY_REQUIREMENTS = 8
|
|
35
|
+
/** 单条质量要求的字符上限。 */
|
|
36
|
+
export const MAX_QUALITY_REQUIREMENT_CHARS = 400
|
|
37
|
+
/** 质量要求的最少条数:至少一条可检查的质量要求,否则等于没交代。 */
|
|
38
|
+
export const MIN_QUALITY_REQUIREMENTS = 1
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 读取并消毒质量要求列表(数组或单个字符串)。
|
|
42
|
+
*
|
|
43
|
+
* 空字符串、非字符串一律丢弃;重复项按规范化文本去重;条数与长度按上限截断。
|
|
44
|
+
* 返回空数组表示「没有任何可用质量要求」,由调用方决定是拒绝还是回落。
|
|
45
|
+
*/
|
|
46
|
+
export function normalizeQualityRequirements(value: unknown): string[] {
|
|
47
|
+
const raw = Array.isArray(value) ? value : (typeof value === 'string' ? [value] : [])
|
|
48
|
+
const seen = new Set<string>()
|
|
49
|
+
const out: string[] = []
|
|
50
|
+
for (const item of raw) {
|
|
51
|
+
if (typeof item !== 'string') continue
|
|
52
|
+
const text = item.replace(/\s+/g, ' ').trim()
|
|
53
|
+
if (text === '') continue
|
|
54
|
+
const key = text.toLowerCase()
|
|
55
|
+
if (seen.has(key)) continue
|
|
56
|
+
seen.add(key)
|
|
57
|
+
out.push(text.length > MAX_QUALITY_REQUIREMENT_CHARS
|
|
58
|
+
? `${text.slice(0, MAX_QUALITY_REQUIREMENT_CHARS - 3)}...`
|
|
59
|
+
: text)
|
|
60
|
+
if (out.length >= MAX_QUALITY_REQUIREMENTS) break
|
|
61
|
+
}
|
|
62
|
+
return out
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* 渲染「本次任务结果的质量要求」块(用户要求 3、6)。
|
|
67
|
+
*
|
|
68
|
+
* 主 Agent 派发给部门负责人、部门负责人派发给专家,都必须带这一块;
|
|
69
|
+
* 两处共用同一渲染,保证「负责人的要求」与「专家收到的要求」逐字一致。
|
|
70
|
+
*/
|
|
71
|
+
export function renderQualityRequirementsBlock(
|
|
72
|
+
requirements: readonly string[],
|
|
73
|
+
sourceLabel = '主 Agent',
|
|
74
|
+
): string {
|
|
75
|
+
const list = normalizeQualityRequirements(requirements)
|
|
76
|
+
if (list.length === 0) return ''
|
|
77
|
+
return [
|
|
78
|
+
`本次任务结果的质量要求(${sourceLabel}下达,属于验收的一部分,必须逐条达到,缺一不可):`,
|
|
79
|
+
...list.map((item, index) => ` ${index + 1}. ${item}`),
|
|
80
|
+
].join('\n')
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/* ========================================================================== */
|
|
84
|
+
/* 2b. 主 Agent→部门负责人派发的质量要求硬闸(2026-11-15 用户裁决) */
|
|
85
|
+
/* ========================================================================== */
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 质量闸生效时刻(2026-11-15 10:30 +08:00 部署本机制的会话时点)。
|
|
89
|
+
*
|
|
90
|
+
* 用户裁决:旧计划(在途/历史,无 qualityRequirements 字段)豁免、零回归;
|
|
91
|
+
* 只有 createdAt ≥ 该时刻的新计划在派发时才被本闸拦截。createdAt 缺失或
|
|
92
|
+
* 非法一律视为旧计划放行(与 criterion v2 的「不回溯」哲学一致)。
|
|
93
|
+
*/
|
|
94
|
+
export const QUALITY_GATE_ACTIVATED_AT = Date.parse('2026-11-15T02:30:00.000Z')
|
|
95
|
+
|
|
96
|
+
/** 该计划是否受「派发必须带质量要求」硬闸约束(true=新计划,拦截)。 */
|
|
97
|
+
export function isQualityGatePlan(plan: unknown): boolean {
|
|
98
|
+
const createdAt = Number((plan as any)?.createdAt ?? 0)
|
|
99
|
+
return Number.isFinite(createdAt) && createdAt > 0 && createdAt >= QUALITY_GATE_ACTIVATED_AT
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export interface SubtaskQualityCheck {
|
|
103
|
+
ok: boolean
|
|
104
|
+
code?: string
|
|
105
|
+
error?: string
|
|
106
|
+
hint?: string
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* 校验单条派发给部门负责人的子任务是否携带质量要求(用户裁决:缺即拒发)。
|
|
111
|
+
*
|
|
112
|
+
* 判据与 normalizeQualityRequirements 同口径:至少 1 条非空、去重后 ≤8 条、
|
|
113
|
+
* 每条 ≤400 字符;全部为空视为缺失。满足返回 ok:true(调用方照常派发)。
|
|
114
|
+
*/
|
|
115
|
+
export function checkSubtaskQualityRequirements(subtask: unknown): SubtaskQualityCheck {
|
|
116
|
+
const item = (subtask ?? {}) as any
|
|
117
|
+
if (normalizeQualityRequirements(item?.qualityRequirements).length >= MIN_QUALITY_REQUIREMENTS) {
|
|
118
|
+
return { ok: true }
|
|
119
|
+
}
|
|
120
|
+
const label = typeof item?.title === 'string' && item.title.trim() !== '' ? `「${item.title.trim()}」` : ''
|
|
121
|
+
return {
|
|
122
|
+
ok: false,
|
|
123
|
+
code: 'DISPATCH_QUALITY_REQUIREMENTS_MISSING',
|
|
124
|
+
error: `派发给部门负责人的子任务${label}(${String(item?.id ?? '?')})没有携带质量要求(qualityRequirements):`
|
|
125
|
+
+ '不带质量要求的任务直接拒发,该子任务不会启动部门 Agent。',
|
|
126
|
+
hint: '立刻用 dsh_workplan_replan 重新规划,为每个子任务补写 qualityRequirements'
|
|
127
|
+
+ '(至少 1 条可检查的质量要求:什么算合格、什么算不合格,每条 ≤400 字符),'
|
|
128
|
+
+ '然后重新调用 dsh_workplan_dispatch 派发;本次其余合格的子任务不受影响、正常执行。',
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/* ========================================================================== */
|
|
133
|
+
/* 3. 极简任务硬门槛(用户要求 5) */
|
|
134
|
+
/* ========================================================================== */
|
|
135
|
+
|
|
136
|
+
/** 单项任务书的字符上限:超过即判定「不是一个 1+1=2 的单项任务」。 */
|
|
137
|
+
export const MAX_ATOMIC_TASK_CHARS = 300
|
|
138
|
+
/** 单项任务书的最短字符数(太短无法判断做什么;与函数注释、CHANGELOG 3.0.0 一致)。 */
|
|
139
|
+
export const MIN_ATOMIC_TASK_CHARS = 8
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 并列/多目标的强信号词。命中 1 个以上即视为「一个任务里塞了多件事」。
|
|
143
|
+
* 只收强信号(明显的并列连词与小标题分隔),不收「和」这类高频虚词,
|
|
144
|
+
* 避免把正常表述误判成多任务。
|
|
145
|
+
*/
|
|
146
|
+
const MULTI_GOAL_MARKERS: readonly string[] = Object.freeze([
|
|
147
|
+
'并且', '同时', '另外', '还要', '以及', '分别', '顺便',
|
|
148
|
+
])
|
|
149
|
+
|
|
150
|
+
/** 句末终止符:原子任务必须是单句。 */
|
|
151
|
+
const SENTENCE_TERMINATORS: readonly RegExp[] = [new RegExp('[。!?!?]', 'g')]
|
|
152
|
+
|
|
153
|
+
export interface AtomicTaskCheck {
|
|
154
|
+
ok: boolean
|
|
155
|
+
code?: string
|
|
156
|
+
error?: string
|
|
157
|
+
hint?: string
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* 极简任务硬门槛(用户要求 5,用户裁决 A:不满足即拒绝派发并强制拆小)。
|
|
162
|
+
*
|
|
163
|
+
* 判据全部可机械复算,不看语义:
|
|
164
|
+
* 1) 非空且长度在 8–300 字符之间;
|
|
165
|
+
* 2) 只有一句话(不出现第二个句末标点,不含分号分句);
|
|
166
|
+
* 3) 不含并列强信号词(并且/同时/另外/还要/以及/分别/顺便);
|
|
167
|
+
* 4) 必须带非空的质量要求(用户要求 6:交代清楚成果必须达到的要求和质量)。
|
|
168
|
+
*
|
|
169
|
+
* 返回的 hint 是可直接照做的拆小指令,不是一句「不合格」。
|
|
170
|
+
*/
|
|
171
|
+
export function checkAtomicAssignment(task: string, qualityRequirements?: unknown): AtomicTaskCheck {
|
|
172
|
+
const text = String(task ?? '').replace(/\s+/g, ' ').trim()
|
|
173
|
+
if (text.length < MIN_ATOMIC_TASK_CHARS) {
|
|
174
|
+
return {
|
|
175
|
+
ok: false,
|
|
176
|
+
code: 'DELEGATE_TASK_NOT_ATOMIC',
|
|
177
|
+
error: `分到的任务太短(${text.length} 字符),必须写清「一个动作 + 一个主产物」(至少 ${MIN_ATOMIC_TASK_CHARS} 字符)。`,
|
|
178
|
+
hint: '把任务写成:对什么对象做什么动作 → 产出哪一个文件/结论。一条只做一件事。',
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (text.length > MAX_ATOMIC_TASK_CHARS) {
|
|
182
|
+
return {
|
|
183
|
+
ok: false,
|
|
184
|
+
code: 'DELEGATE_TASK_NOT_ATOMIC',
|
|
185
|
+
error: `分到的任务太长(${text.length} 字符 > ${MAX_ATOMIC_TASK_CHARS}),不是 1+1=2 的单项任务。`,
|
|
186
|
+
hint: '按「一个目标 / 一个主产物 / 一项可独立验收」拆成多条;每条只保留一个动作,其余另起一条。',
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
for (const pattern of SENTENCE_TERMINATORS) {
|
|
190
|
+
pattern.lastIndex = 0
|
|
191
|
+
const matches = text.match(pattern)
|
|
192
|
+
if (matches && matches.length > 1) {
|
|
193
|
+
return {
|
|
194
|
+
ok: false,
|
|
195
|
+
code: 'DELEGATE_TASK_NOT_ATOMIC',
|
|
196
|
+
error: '分到的任务里出现了多个句子,说明塞进了多件事。',
|
|
197
|
+
hint: '一个任务只写一句话;其余句子各自拆成独立任务。',
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
if (/[;;]/.test(text)) {
|
|
202
|
+
return {
|
|
203
|
+
ok: false,
|
|
204
|
+
code: 'DELEGATE_TASK_NOT_ATOMIC',
|
|
205
|
+
error: '分到的任务用分号并列了多个子任务,不是单项任务。',
|
|
206
|
+
hint: '分号前、分号后各拆成一条独立任务,各自带质量要求。',
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
const hit = MULTI_GOAL_MARKERS.filter((marker) => text.includes(marker))
|
|
210
|
+
if (hit.length > 0) {
|
|
211
|
+
return {
|
|
212
|
+
ok: false,
|
|
213
|
+
code: 'DELEGATE_TASK_NOT_ATOMIC',
|
|
214
|
+
error: `分到的任务含并列词「${hit.join('、')}」,说明一个任务里有多件事。`,
|
|
215
|
+
hint: '把并列词前后的内容各拆成一条独立任务,每条只做一个动作。',
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
if (normalizeQualityRequirements(qualityRequirements).length < MIN_QUALITY_REQUIREMENTS) {
|
|
219
|
+
return {
|
|
220
|
+
ok: false,
|
|
221
|
+
code: 'DELEGATE_QUALITY_REQUIREMENTS_MISSING',
|
|
222
|
+
error: '分到的任务没有交代「成果必须达到的要求和质量」,专家无法判断做到什么程度算完成。',
|
|
223
|
+
hint: '为这条任务补 qualityRequirements(至少 1 条可检查的质量要求,例如「必须是可执行命令并附真实输出」)。',
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return { ok: true }
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/* ========================================================================== */
|
|
230
|
+
/* 4. 专家精简 JSON 汇报(用户要求 7) */
|
|
231
|
+
/* ========================================================================== */
|
|
232
|
+
|
|
233
|
+
/** 单个汇报数组的条数上限:精简的量化口径。 */
|
|
234
|
+
export const MAX_REPORT_ITEMS = 6
|
|
235
|
+
/** 单条汇报文本的字符上限。 */
|
|
236
|
+
export const MAX_REPORT_ITEM_CHARS = 200
|
|
237
|
+
|
|
238
|
+
/** 专家汇报的四个必填字段(与用户要求 7 的四个方向一一对应)。 */
|
|
239
|
+
export const EXPERT_REPORT_FIELDS = Object.freeze([
|
|
240
|
+
Object.freeze({ name: 'keyResults', label: '关键成果', description: '本次任务真正做出来的结果(做了什么、得出什么),至少 1 条' }),
|
|
241
|
+
Object.freeze({ name: 'keyInfo', label: '关键信息', description: '支撑上述成果的关键事实/数据/结论,可为空数组' }),
|
|
242
|
+
Object.freeze({ name: 'importantInfo', label: '重要信息', description: '负责人必须知道的风险、限制、未决项,可为空数组' }),
|
|
243
|
+
Object.freeze({ name: 'leadRequirements', label: '负责人要求的东西', description: '负责人点名要的东西(文件路径/结论/证据)逐条回应,至少 1 条' }),
|
|
244
|
+
])
|
|
245
|
+
|
|
246
|
+
/** 部门汇总的必填字段(与用户要求 10 一一对应)。 */
|
|
247
|
+
export const DEPARTMENT_SUMMARY_FIELDS = Object.freeze([
|
|
248
|
+
Object.freeze({ name: 'keyResults', label: '关键成果', description: '本部门汇总后的关键成果,至少 1 条' }),
|
|
249
|
+
Object.freeze({ name: 'keyInfo', label: '关键信息', description: '支撑成果的关键事实/数据,可为空数组' }),
|
|
250
|
+
Object.freeze({ name: 'importantInfo', label: '重要信息', description: '主 Agent 必须知道的风险、限制、未决项,可为空数组' }),
|
|
251
|
+
Object.freeze({ name: 'sources', label: '覆盖的专家汇报', description: '逐条列明已汇总的专家:member + 其关键成果一句;必须覆盖本部门全部已派发专家' }),
|
|
252
|
+
])
|
|
253
|
+
|
|
254
|
+
function normalizeReportLine(value: unknown): string {
|
|
255
|
+
if (typeof value !== 'string') return ''
|
|
256
|
+
return value.replace(/\s+/g, ' ').trim()
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* 去重检测(用户要求 10 的「禁止内容累赘和重复」)。
|
|
261
|
+
*
|
|
262
|
+
* 两种重复都算:
|
|
263
|
+
* 1) 同一条信息重复出现——完全相同,或一条完整包含另一条且短的一条不少于
|
|
264
|
+
* 12 字符;
|
|
265
|
+
* 2) 近义重复——按 bigram 的 Dice 系数 ≥ 0.85 判定(与 T7 契约文档同一阈值),
|
|
266
|
+
* 用于拦「换个说法写两遍」。
|
|
267
|
+
* 条目支持「去重键|内容」写法(T7 契约):去重键相同即视为同一条信息,
|
|
268
|
+
* 与内容是否一致无关。
|
|
269
|
+
*/
|
|
270
|
+
const DEDUPE_SIMILARITY_THRESHOLD = 0.85
|
|
271
|
+
|
|
272
|
+
/** 拆出「去重键|内容」的去重键(没有分隔符时返回空串)。 */
|
|
273
|
+
export function entryDedupeKey(line: string): string {
|
|
274
|
+
const index = line.indexOf('|')
|
|
275
|
+
if (index <= 0) return ''
|
|
276
|
+
return line.slice(0, index).replace(/\s+/g, ' ').trim().toLowerCase()
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** bigram 集合(去掉空白与标点,中文按二字切片)。 */
|
|
280
|
+
function bigrams(text: string): string[] {
|
|
281
|
+
const cleaned = text.toLowerCase().replace(/[\s\p{P}\p{S}]/gu, '')
|
|
282
|
+
const out: string[] = []
|
|
283
|
+
for (let i = 0; i < cleaned.length - 1; i += 1) out.push(cleaned.slice(i, i + 2))
|
|
284
|
+
if (out.length === 0 && cleaned.length === 1) out.push(cleaned)
|
|
285
|
+
return out
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/** Dice 相似度:两个 bigram 多重集的重合程度(0–1)。 */
|
|
289
|
+
export function diceSimilarity(a: string, b: string): number {
|
|
290
|
+
const left = bigrams(a)
|
|
291
|
+
const right = bigrams(b)
|
|
292
|
+
if (left.length === 0 || right.length === 0) return a.trim() === b.trim() ? 1 : 0
|
|
293
|
+
const counts = new Map<string, number>()
|
|
294
|
+
for (const gram of left) counts.set(gram, (counts.get(gram) ?? 0) + 1)
|
|
295
|
+
let overlap = 0
|
|
296
|
+
for (const gram of right) {
|
|
297
|
+
const remaining = counts.get(gram) ?? 0
|
|
298
|
+
if (remaining > 0) {
|
|
299
|
+
overlap += 1
|
|
300
|
+
counts.set(gram, remaining - 1)
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return (2 * overlap) / (left.length + right.length)
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function duplicatePairs(lines: readonly string[]): Array<[string, string]> {
|
|
307
|
+
const found: Array<[string, string]> = []
|
|
308
|
+
const normalized = lines.map((line) => line.toLowerCase())
|
|
309
|
+
const keys = lines.map(entryDedupeKey)
|
|
310
|
+
for (let i = 0; i < normalized.length; i += 1) {
|
|
311
|
+
for (let j = i + 1; j < normalized.length; j += 1) {
|
|
312
|
+
const a = normalized[i]
|
|
313
|
+
const b = normalized[j]
|
|
314
|
+
if (a === '') continue
|
|
315
|
+
if (keys[i] !== '' && keys[i] === keys[j]) { found.push([lines[i], lines[j]]); continue }
|
|
316
|
+
if (a === b) { found.push([lines[i], lines[j]]); continue }
|
|
317
|
+
const [short, long] = a.length <= b.length ? [a, b] : [b, a]
|
|
318
|
+
if (short.length >= 12 && long.includes(short)) { found.push([lines[i], lines[j]]); continue }
|
|
319
|
+
if (short.length >= 12 && diceSimilarity(a, b) >= DEDUPE_SIMILARITY_THRESHOLD) {
|
|
320
|
+
found.push([lines[i], lines[j]])
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
return found
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function checkListField(
|
|
328
|
+
container: Record<string, unknown>,
|
|
329
|
+
name: string,
|
|
330
|
+
violations: string[],
|
|
331
|
+
options: { minItems: number; dedupeAcross?: readonly string[] },
|
|
332
|
+
): string[] {
|
|
333
|
+
const raw = container[name]
|
|
334
|
+
if (!Array.isArray(raw)) {
|
|
335
|
+
violations.push(`缺少 ${name} 数组(${name} 必须是字符串数组)`)
|
|
336
|
+
return []
|
|
337
|
+
}
|
|
338
|
+
const lines = raw.map(normalizeReportLine).filter((line) => line !== '')
|
|
339
|
+
if (lines.length < options.minItems) {
|
|
340
|
+
violations.push(`${name} 至少 ${options.minItems} 条非空内容,当前 ${lines.length} 条`)
|
|
341
|
+
}
|
|
342
|
+
if (lines.length > MAX_REPORT_ITEMS) {
|
|
343
|
+
violations.push(`${name} 最多 ${MAX_REPORT_ITEMS} 条(精简要求),当前 ${lines.length} 条,请合并去重`)
|
|
344
|
+
}
|
|
345
|
+
for (const line of lines) {
|
|
346
|
+
if (line.length > MAX_REPORT_ITEM_CHARS) {
|
|
347
|
+
violations.push(`${name} 有一条 ${line.length} 字符超上限(${MAX_REPORT_ITEM_CHARS}),请压缩成一句关键信息`)
|
|
348
|
+
break
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
const dups = duplicatePairs(lines)
|
|
352
|
+
if (dups.length > 0) {
|
|
353
|
+
violations.push(`${name} 内部有重复表述(例:「${dups[0][0].slice(0, 40)}」与「${dups[0][1].slice(0, 40)}」),禁止累赘重复`)
|
|
354
|
+
}
|
|
355
|
+
const across = options.dedupeAcross ?? []
|
|
356
|
+
for (const other of across) {
|
|
357
|
+
const otherLines = (Array.isArray(container[other]) ? container[other] : [])
|
|
358
|
+
.map(normalizeReportLine).filter((line) => line !== '')
|
|
359
|
+
const crossDups = duplicatePairs([...lines, ...otherLines])
|
|
360
|
+
.filter((pair) => lines.includes(pair[0]) && otherLines.includes(pair[1]))
|
|
361
|
+
if (crossDups.length > 0) {
|
|
362
|
+
violations.push(`${name} 与 ${other} 有重复项(例:「${crossDups[0][0].slice(0, 40)}」),同一信息只写一次`)
|
|
363
|
+
break
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return lines
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
export interface ReportCheck {
|
|
370
|
+
ok: boolean
|
|
371
|
+
code?: string
|
|
372
|
+
violations: string[]
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* 校验专家精简 JSON 汇报(用户要求 7)。
|
|
377
|
+
*
|
|
378
|
+
* 只做结构/精简判定,不判语义真伪(诚信由 F9 与三阶段门禁兜底)。
|
|
379
|
+
*/
|
|
380
|
+
export function checkExpertReport(report: unknown): ReportCheck {
|
|
381
|
+
if (!report || typeof report !== 'object' || Array.isArray(report)) {
|
|
382
|
+
return { ok: false, code: 'REPORT_INVALID', violations: ['缺少 report 对象:专家成果必须以 JSON 汇报(keyResults/keyInfo/importantInfo/leadRequirements)'] }
|
|
383
|
+
}
|
|
384
|
+
const container = report as Record<string, unknown>
|
|
385
|
+
const extra = Object.keys(container).filter((key) => !EXPERT_REPORT_FIELDS.some((field) => field.name === key))
|
|
386
|
+
const violations: string[] = []
|
|
387
|
+
checkListField(container, 'keyResults', violations, { minItems: 1, dedupeAcross: ['keyInfo', 'importantInfo', 'leadRequirements'] })
|
|
388
|
+
checkListField(container, 'keyInfo', violations, { minItems: 0, dedupeAcross: ['keyResults', 'importantInfo', 'leadRequirements'] })
|
|
389
|
+
checkListField(container, 'importantInfo', violations, { minItems: 0, dedupeAcross: ['keyResults', 'keyInfo', 'leadRequirements'] })
|
|
390
|
+
checkListField(container, 'leadRequirements', violations, { minItems: 1, dedupeAcross: ['keyResults', 'keyInfo', 'importantInfo'] })
|
|
391
|
+
if (extra.length > 0) {
|
|
392
|
+
violations.push(`report 只允许 keyResults/keyInfo/importantInfo/leadRequirements 四个字段,多出:${extra.join('、')}`)
|
|
393
|
+
}
|
|
394
|
+
return violations.length === 0
|
|
395
|
+
? { ok: true, violations: [] }
|
|
396
|
+
: { ok: false, code: violations.some((v) => v.startsWith('缺少 report')) ? 'REPORT_INVALID' : 'REPORT_NOT_CONCISE', violations }
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* 校验部门负责人精简 JSON 汇总(用户要求 8、10、11)。
|
|
401
|
+
*
|
|
402
|
+
* @param summary 汇总对象
|
|
403
|
+
* @param expectedExperts 本部门已派发给专家的名单(按 member 名称);给空数组时
|
|
404
|
+
* 不做覆盖度核对(负责人亲自执行、无下级的情形)
|
|
405
|
+
*/
|
|
406
|
+
export function checkDepartmentSummary(summary: unknown, expectedExperts: readonly string[] = []): ReportCheck {
|
|
407
|
+
if (!summary || typeof summary !== 'object' || Array.isArray(summary)) {
|
|
408
|
+
return { ok: false, code: 'DEPARTMENT_SUMMARY_INVALID', violations: ['缺少 departmentSummary 对象:部门汇总必须以 JSON 交回主 Agent(keyResults/keyInfo/importantInfo/sources)'] }
|
|
409
|
+
}
|
|
410
|
+
const container = summary as Record<string, unknown>
|
|
411
|
+
const extra = Object.keys(container).filter((key) => !DEPARTMENT_SUMMARY_FIELDS.some((field) => field.name === key))
|
|
412
|
+
const violations: string[] = []
|
|
413
|
+
checkListField(container, 'keyResults', violations, { minItems: 1, dedupeAcross: ['keyInfo', 'importantInfo'] })
|
|
414
|
+
checkListField(container, 'keyInfo', violations, { minItems: 0, dedupeAcross: ['keyResults', 'importantInfo'] })
|
|
415
|
+
checkListField(container, 'importantInfo', violations, { minItems: 0, dedupeAcross: ['keyResults', 'keyInfo'] })
|
|
416
|
+
const covered = checkListField(container, 'sources', violations, { minItems: 0 })
|
|
417
|
+
if (expectedExperts.length > 0) {
|
|
418
|
+
const joined = covered.join('\n').toLowerCase()
|
|
419
|
+
const missing = expectedExperts.filter((member) => member.trim() !== '' && !joined.includes(member.trim().toLowerCase()))
|
|
420
|
+
if (missing.length > 0) {
|
|
421
|
+
violations.push(`sources 未覆盖全部已派发专家,缺少:${missing.join('、')}(每条写「专家名:一句关键成果」)`)
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
if (extra.length > 0) {
|
|
425
|
+
violations.push(`departmentSummary 只允许 keyResults/keyInfo/importantInfo/sources 四个字段,多出:${extra.join('、')}`)
|
|
426
|
+
}
|
|
427
|
+
return violations.length === 0
|
|
428
|
+
? { ok: true, violations: [] }
|
|
429
|
+
: { ok: false, code: 'DEPARTMENT_SUMMARY_NOT_CONCISE', violations }
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/** 渲染专家汇报的 JSON 契约(嵌入专家任务书)。 */
|
|
433
|
+
export function renderExpertReportSpec(): string {
|
|
434
|
+
return [
|
|
435
|
+
'【专家成果汇报契约 · JSON mode · 用户强制要求 7】',
|
|
436
|
+
'完成后必须在证据块 JSON 里额外给出 report 字段,只写下面四项,禁止写长文、禁止前后重复:',
|
|
437
|
+
' "report": {',
|
|
438
|
+
' "keyResults": ["关键成果"],',
|
|
439
|
+
' "keyInfo": ["关键信息"],',
|
|
440
|
+
' "importantInfo": ["重要信息"],',
|
|
441
|
+
' "leadRequirements": ["负责人要求的东西"]',
|
|
442
|
+
' }',
|
|
443
|
+
...EXPERT_REPORT_FIELDS.map((field) => `- ${field.name}(${field.label}):${field.description}`),
|
|
444
|
+
`- 每个数组最多 ${MAX_REPORT_ITEMS} 条、每条最多 ${MAX_REPORT_ITEM_CHARS} 字符;同一信息只能出现一次(禁止累赘和重复)。`,
|
|
445
|
+
'- 门禁:缺 report、字段缺失、条数越界、单条超长、或出现重复项,结果一律退回重交(REPORT_INVALID / REPORT_NOT_CONCISE)。',
|
|
446
|
+
].join('\n')
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/** 渲染部门汇总的 JSON 契约(嵌入部门负责人任务书)。 */
|
|
450
|
+
export function renderDepartmentSummarySpec(): string {
|
|
451
|
+
return [
|
|
452
|
+
'【部门汇总契约 · JSON mode · 用户强制要求 8/10/11】',
|
|
453
|
+
'你的结果除了常规证据块,必须额外给出 departmentSummary 字段:全面、深入整理汇总本部门全部专家汇报,只保留关键信息、关键成果、重要信息与主 Agent 要求的东西,禁止累赘和重复;汇总完成后直接交回主 Agent。',
|
|
454
|
+
' "departmentSummary": {',
|
|
455
|
+
' "keyResults": ["关键成果"],',
|
|
456
|
+
' "keyInfo": ["关键信息"],',
|
|
457
|
+
' "importantInfo": ["重要信息"],',
|
|
458
|
+
' "sources": ["专家名:一句关键成果"]',
|
|
459
|
+
' }',
|
|
460
|
+
...DEPARTMENT_SUMMARY_FIELDS.map((field) => `- ${field.name}(${field.label}):${field.description}`),
|
|
461
|
+
`- 每个数组最多 ${MAX_REPORT_ITEMS} 条、每条最多 ${MAX_REPORT_ITEM_CHARS} 字符;sources 必须覆盖本部门全部已派发专家,同一信息只能出现一次。`,
|
|
462
|
+
'- 门禁:缺 departmentSummary、字段缺失、未覆盖全部专家、或出现重复项,结果一律退回重交(DEPARTMENT_SUMMARY_INVALID / DEPARTMENT_SUMMARY_NOT_CONCISE)。',
|
|
463
|
+
].join('\n')
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** 去掉首尾空白并截断(与 plan.ts truncate 同语义,此处自带以保持零依赖)。 */
|
|
467
|
+
export function clipReportText(value: unknown, limit: number): string {
|
|
468
|
+
const text = String(value ?? '').replace(/\s+/g, ' ').trim()
|
|
469
|
+
return text.length > limit ? `${text.slice(0, Math.max(0, limit - 3))}...` : text
|
|
470
|
+
}
|
package/src/watchdog.ts
CHANGED
|
@@ -69,7 +69,12 @@ export function describeError(value: unknown): string {
|
|
|
69
69
|
}
|
|
70
70
|
if (value instanceof Error) {
|
|
71
71
|
const message = typeof value.message === 'string' ? value.message.trim() : ''
|
|
72
|
-
|
|
72
|
+
const base = message !== '' ? message : (value.name || '未知失败')
|
|
73
|
+
// [2.5.5][T6] 堆栈留痕:毫秒级崩溃(如 TypeError reading 'id')旧实现只留
|
|
74
|
+
// message 一行,根因无法闭环(session-1513 缺口 G2)。附截断堆栈,durable
|
|
75
|
+
// 结果/看板可直接定位崩溃点。
|
|
76
|
+
const stack = typeof value.stack === 'string' ? value.stack.split('\n').slice(0, 6).join(' | ').trim() : ''
|
|
77
|
+
return stack ? `${base}[stack: ${stack.slice(0, 600)}]` : base
|
|
73
78
|
}
|
|
74
79
|
if (typeof value === 'object') {
|
|
75
80
|
const record = value as Record<string, unknown>
|