dsh-workplan 1.12.2 → 2.0.1
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 +97 -0
- package/README.en.md +17 -2
- package/README.md +41 -3
- package/README.zh.md +2 -2
- package/docs/cc/ADV-FSP-FUNCTIONAL-SPEC.md +3 -1
- package/docs/cc/AVA-VULNERABILITY-ANALYSIS.md +9 -0
- package/docs/cc/SECURITY-TARGET.md +3 -1
- package/docs/collaboration.md +27 -2
- package/docs/decision-clarify-gate-20261104.md +41 -0
- package/docs/release-notes-2.0.1.md +56 -0
- package/lib/departments.js +7 -0
- package/lib/host.js +219 -2
- package/lib/lessons.js +378 -0
- package/lib/plan.js +62 -5
- package/lib/runtime.js +263 -4
- package/lib/three-phase.js +287 -0
- package/lib/understanding.js +258 -2
- package/package.json +1 -1
- package/src/departments.ts +7 -0
- package/src/host.ts +237 -2
- package/src/lessons.ts +445 -0
- package/src/plan.ts +63 -4
- package/src/runtime.ts +295 -3
- package/src/three-phase.ts +342 -0
- package/src/understanding.ts +256 -6
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* [1.13.0] 三阶段工作法:部门 Agent 的强制执行顺序。
|
|
3
|
+
*
|
|
4
|
+
* 用户要求(2026-11-05):每个部门人员收到分配的工作后,必须
|
|
5
|
+
* 1. 先全面、深入梳理「要解决的问题」;
|
|
6
|
+
* 2. 再全面、深入思考,整理出能完美完成自身工作的完整、可执行思路;
|
|
7
|
+
* 3. 只有前两步完成后,才按思路执行与验证。
|
|
8
|
+
* 全过程按先后顺序分为三个阶段:规划阶段 → 执行阶段 → 验证阶段。
|
|
9
|
+
* 第一、二阶段禁用 dsh-workplan 插件,第三阶段才允许用插件核验。
|
|
10
|
+
*
|
|
11
|
+
* 这个模块只做纯函数与常量,不依赖 harness 服务:
|
|
12
|
+
* - 校验子 Agent 回报的 `phases` 证据(缺一阶段就判未完成);
|
|
13
|
+
* - 生成阶段摘要(写进结果与日志);
|
|
14
|
+
* - 生成提示词段落(由 `buildChildPrompt` 拼进子 Agent 的提示词)。
|
|
15
|
+
*
|
|
16
|
+
* 为什么用「结构化证据」而不是只写一句提示:
|
|
17
|
+
* 只写提示词时,模型可以直接动手、最后补一句「我已规划」。把规划内容变成
|
|
18
|
+
* 收尾证据的一部分后,缺规划/缺验证的结果无法被记成 done,主 Agent 和
|
|
19
|
+
* 回放日志都能看到每个部门到底梳理了什么、按什么思路做、验证了什么。
|
|
20
|
+
*/
|
|
21
|
+
import { validateRetrospective } from './lessons.js';
|
|
22
|
+
/** 三阶段顺序不可颠倒;这里也是结果块里 `phases` 的键名。 */
|
|
23
|
+
export const THREE_PHASE_IDS = Object.freeze(['planning', 'execution', 'verification']);
|
|
24
|
+
export const THREE_PHASE_VERSION = 1;
|
|
25
|
+
/**
|
|
26
|
+
* 「全面、深入」无法由代码度量,但可以给一个不可忽略的下限:
|
|
27
|
+
* 问题梳理与可执行思路各至少 40 个字符,避免一个词就冒充规划。
|
|
28
|
+
*/
|
|
29
|
+
export const MIN_PLANNING_PROBLEM_CHARS = 40;
|
|
30
|
+
export const MIN_PLANNING_APPROACH_CHARS = 40;
|
|
31
|
+
export const MAX_PHASE_LIST_ITEMS = 20;
|
|
32
|
+
export const MAX_PHASE_ITEM_CHARS = 800;
|
|
33
|
+
export const MAX_PHASE_TOTAL_CHARS = 6000;
|
|
34
|
+
function isRecord(value) {
|
|
35
|
+
return !!value && typeof value === 'object' && !Array.isArray(value);
|
|
36
|
+
}
|
|
37
|
+
function readTextList(value, field, errors, options) {
|
|
38
|
+
if (value === undefined || value === null) {
|
|
39
|
+
if (options.required)
|
|
40
|
+
errors.push(`${field} 缺失`);
|
|
41
|
+
return [];
|
|
42
|
+
}
|
|
43
|
+
if (!Array.isArray(value)) {
|
|
44
|
+
errors.push(`${field} 必须是数组`);
|
|
45
|
+
return [];
|
|
46
|
+
}
|
|
47
|
+
const items = value
|
|
48
|
+
.map((item) => (typeof item === 'string' ? item.trim() : ''))
|
|
49
|
+
.filter(Boolean);
|
|
50
|
+
if (options.required && items.length < (options.minItems ?? 1)) {
|
|
51
|
+
errors.push(`${field} 至少需要 ${options.minItems ?? 1} 条非空内容`);
|
|
52
|
+
return items.slice(0, MAX_PHASE_LIST_ITEMS);
|
|
53
|
+
}
|
|
54
|
+
if (items.length > MAX_PHASE_LIST_ITEMS) {
|
|
55
|
+
errors.push(`${field} 超过 ${MAX_PHASE_LIST_ITEMS} 条上限`);
|
|
56
|
+
}
|
|
57
|
+
return items.slice(0, MAX_PHASE_LIST_ITEMS).map((item) => item.slice(0, MAX_PHASE_ITEM_CHARS));
|
|
58
|
+
}
|
|
59
|
+
function totalChars(items) {
|
|
60
|
+
return items.reduce((sum, item) => sum + item.length, 0);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Same matching rules as `src/plan.ts#acceptanceCoverage` (Q6 / U-16):
|
|
64
|
+
* stable A1…An numbering, exact full text, or a longer text containing the
|
|
65
|
+
* full criterion. A partial copy must not count as coverage.
|
|
66
|
+
*
|
|
67
|
+
* Kept local to avoid importing plan.ts (which imports this module); the
|
|
68
|
+
* rules are pinned by tests on both sides.
|
|
69
|
+
*/
|
|
70
|
+
function normaliseCriterion(value) {
|
|
71
|
+
return String(value ?? '')
|
|
72
|
+
.toLowerCase()
|
|
73
|
+
.replace(/[\s,。;、,.;::!!??"'“”‘’()()\[\]【】《》<>]/g, '')
|
|
74
|
+
.replace(/^(必须|需要|应当|应该|请|务必)/, '');
|
|
75
|
+
}
|
|
76
|
+
function missingCriteria(expected, checks) {
|
|
77
|
+
const want = (Array.isArray(expected) ? expected : []).map(normaliseCriterion);
|
|
78
|
+
const got = checks.map((check) => normaliseCriterion(check.criterion)).filter(Boolean);
|
|
79
|
+
const missing = [];
|
|
80
|
+
for (const [index, criterion] of want.entries()) {
|
|
81
|
+
if (!criterion)
|
|
82
|
+
continue;
|
|
83
|
+
const id = new RegExp(`(^|[^a-z0-9])a${index + 1}([^0-9]|$)`);
|
|
84
|
+
const covered = got.some((row) => {
|
|
85
|
+
if (row === criterion)
|
|
86
|
+
return true;
|
|
87
|
+
if (id.test(row))
|
|
88
|
+
return true;
|
|
89
|
+
if (Math.min(row.length, criterion.length) < 6)
|
|
90
|
+
return false;
|
|
91
|
+
return row.includes(criterion);
|
|
92
|
+
});
|
|
93
|
+
if (!covered)
|
|
94
|
+
missing.push(`A${index + 1}`);
|
|
95
|
+
}
|
|
96
|
+
return missing;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Parse + validate the `phases` object from a child's evidence block.
|
|
100
|
+
*
|
|
101
|
+
* `expectedAcceptance` is the plan's own acceptance table. When present, every
|
|
102
|
+
* criterion must have a verification check; a shorter list is treated as an
|
|
103
|
+
* incomplete phase-3 verification rather than "some checks are better than none".
|
|
104
|
+
*/
|
|
105
|
+
export function validatePhaseEvidence(value, expectedAcceptance = []) {
|
|
106
|
+
const errors = [];
|
|
107
|
+
let failedPhase = null;
|
|
108
|
+
const fail = (phase, message) => {
|
|
109
|
+
errors.push(message);
|
|
110
|
+
if (failedPhase === null)
|
|
111
|
+
failedPhase = phase;
|
|
112
|
+
};
|
|
113
|
+
if (!isRecord(value)) {
|
|
114
|
+
return {
|
|
115
|
+
ok: false,
|
|
116
|
+
phases: null,
|
|
117
|
+
errors: ['phases 缺失:必须提交 planning / execution / verification 三阶段证据'],
|
|
118
|
+
failedPhase: 'shape',
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
const planningRaw = isRecord(value.planning) ? value.planning : null;
|
|
122
|
+
if (!planningRaw)
|
|
123
|
+
fail('planning', 'phases.planning 缺失(阶段一:先梳理问题,再形成可执行思路)');
|
|
124
|
+
const executionRaw = isRecord(value.execution) ? value.execution : null;
|
|
125
|
+
if (!executionRaw)
|
|
126
|
+
fail('execution', 'phases.execution 缺失(阶段二:按阶段一的思路执行)');
|
|
127
|
+
const verificationRaw = isRecord(value.verification) ? value.verification : null;
|
|
128
|
+
if (!verificationRaw)
|
|
129
|
+
fail('verification', 'phases.verification 缺失(阶段三:逐条验证)');
|
|
130
|
+
const planning = {
|
|
131
|
+
problemAnalysis: readTextList(planningRaw?.problemAnalysis, 'phases.planning.problemAnalysis', errors, {
|
|
132
|
+
required: !!planningRaw,
|
|
133
|
+
minItems: 1,
|
|
134
|
+
}),
|
|
135
|
+
constraints: readTextList(planningRaw?.constraints, 'phases.planning.constraints', errors, { required: false }),
|
|
136
|
+
unknowns: readTextList(planningRaw?.unknowns, 'phases.planning.unknowns', errors, { required: false }),
|
|
137
|
+
approach: readTextList(planningRaw?.approach, 'phases.planning.approach', errors, {
|
|
138
|
+
required: !!planningRaw,
|
|
139
|
+
minItems: 1,
|
|
140
|
+
}),
|
|
141
|
+
};
|
|
142
|
+
if (errors.some((item) => item.startsWith('phases.planning.problemAnalysis')))
|
|
143
|
+
failedPhase ||= 'planning';
|
|
144
|
+
if (errors.some((item) => item.startsWith('phases.planning.approach')))
|
|
145
|
+
failedPhase ||= 'planning';
|
|
146
|
+
if (planning.problemAnalysis.length > 0 && totalChars(planning.problemAnalysis) < MIN_PLANNING_PROBLEM_CHARS) {
|
|
147
|
+
fail('planning', `phases.planning.problemAnalysis 太薄(合计 ${totalChars(planning.problemAnalysis)} 字,至少 ${MIN_PLANNING_PROBLEM_CHARS} 字):要逐条写清现状、约束、未知与主要矛盾`);
|
|
148
|
+
}
|
|
149
|
+
if (planning.approach.length > 0 && totalChars(planning.approach) < MIN_PLANNING_APPROACH_CHARS) {
|
|
150
|
+
fail('planning', `phases.planning.approach 太薄(合计 ${totalChars(planning.approach)} 字,至少 ${MIN_PLANNING_APPROACH_CHARS} 字):要写出完整可执行的步骤,而不是一句目标`);
|
|
151
|
+
}
|
|
152
|
+
const execution = {
|
|
153
|
+
steps: readTextList(executionRaw?.steps, 'phases.execution.steps', errors, {
|
|
154
|
+
required: !!executionRaw,
|
|
155
|
+
minItems: 1,
|
|
156
|
+
}),
|
|
157
|
+
deviations: readTextList(executionRaw?.deviations, 'phases.execution.deviations', errors, { required: false }),
|
|
158
|
+
};
|
|
159
|
+
if (errors.some((item) => item.startsWith('phases.execution')))
|
|
160
|
+
failedPhase ||= 'execution';
|
|
161
|
+
const retroCheck = validateRetrospective(verificationRaw?.retrospective);
|
|
162
|
+
for (const error of retroCheck.errors)
|
|
163
|
+
fail('verification', error);
|
|
164
|
+
const verification = {
|
|
165
|
+
allPassed: verificationRaw?.allPassed === true,
|
|
166
|
+
checks: [],
|
|
167
|
+
requirementCheck: typeof verificationRaw?.requirementCheck === 'string'
|
|
168
|
+
? verificationRaw.requirementCheck.trim().slice(0, MAX_PHASE_ITEM_CHARS)
|
|
169
|
+
: '',
|
|
170
|
+
retrospective: retroCheck.retrospective,
|
|
171
|
+
};
|
|
172
|
+
if (verificationRaw && verificationRaw.allPassed !== true) {
|
|
173
|
+
fail('verification', 'phases.verification.allPassed 必须为 true:只要有一条验收标准未通过或未验证,就不得报 done');
|
|
174
|
+
}
|
|
175
|
+
if (!Array.isArray(verificationRaw?.checks)) {
|
|
176
|
+
if (verificationRaw)
|
|
177
|
+
fail('verification', 'phases.verification.checks 必须是数组');
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
verification.checks = verificationRaw.checks
|
|
181
|
+
.slice(0, MAX_PHASE_LIST_ITEMS)
|
|
182
|
+
.map((item, index) => {
|
|
183
|
+
if (!isRecord(item)) {
|
|
184
|
+
fail('verification', `phases.verification.checks[${index}] 必须是对象(criterion / method / evidence)`);
|
|
185
|
+
return { criterion: '', method: '', evidence: '' };
|
|
186
|
+
}
|
|
187
|
+
const check = {
|
|
188
|
+
criterion: typeof item.criterion === 'string' ? item.criterion.trim().slice(0, MAX_PHASE_ITEM_CHARS) : '',
|
|
189
|
+
method: typeof item.method === 'string' ? item.method.trim().slice(0, MAX_PHASE_ITEM_CHARS) : '',
|
|
190
|
+
evidence: typeof item.evidence === 'string' ? item.evidence.trim().slice(0, MAX_PHASE_ITEM_CHARS) : '',
|
|
191
|
+
};
|
|
192
|
+
if (!check.criterion)
|
|
193
|
+
fail('verification', `phases.verification.checks[${index}].criterion 不能为空`);
|
|
194
|
+
if (!check.method)
|
|
195
|
+
fail('verification', `phases.verification.checks[${index}].method 不能为空(用什么方式验证)`);
|
|
196
|
+
if (!check.evidence)
|
|
197
|
+
fail('verification', `phases.verification.checks[${index}].evidence 不能为空(命令输出/文件/引用)`);
|
|
198
|
+
return check;
|
|
199
|
+
});
|
|
200
|
+
if (verification.checks.length === 0) {
|
|
201
|
+
fail('verification', 'phases.verification.checks 至少需要一条逐项验证记录');
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const expected = Array.isArray(expectedAcceptance) ? expectedAcceptance.filter(Boolean) : [];
|
|
205
|
+
if (expected.length > 0 && verification.checks.length < expected.length) {
|
|
206
|
+
fail('verification', `phases.verification.checks 只验证了 ${verification.checks.length} 项,少于验收标准的 ${expected.length} 项:每条验收标准都要有验证记录`);
|
|
207
|
+
}
|
|
208
|
+
if (expected.length > 0) {
|
|
209
|
+
const missing = missingCriteria(expected, verification.checks);
|
|
210
|
+
if (missing.length > 0) {
|
|
211
|
+
fail('verification', `phases.verification.checks 未逐条覆盖验收标准:缺少 ${missing.join('、')}(可按 A1/A2 编号对号入座,或写完整原文;只写一部分不算覆盖)`);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
const phases = {
|
|
215
|
+
version: THREE_PHASE_VERSION,
|
|
216
|
+
planning,
|
|
217
|
+
execution,
|
|
218
|
+
verification,
|
|
219
|
+
};
|
|
220
|
+
const total = [
|
|
221
|
+
...planning.problemAnalysis,
|
|
222
|
+
...planning.constraints,
|
|
223
|
+
...planning.unknowns,
|
|
224
|
+
...planning.approach,
|
|
225
|
+
...execution.steps,
|
|
226
|
+
...execution.deviations,
|
|
227
|
+
verification.requirementCheck,
|
|
228
|
+
].reduce((sum, item) => sum + item.length, 0);
|
|
229
|
+
if (total > MAX_PHASE_TOTAL_CHARS) {
|
|
230
|
+
fail('shape', `phases 合计超过 ${MAX_PHASE_TOTAL_CHARS} 字,请压缩到可核对的事实,不要粘贴大段日志`);
|
|
231
|
+
}
|
|
232
|
+
return { ok: errors.length === 0, phases: errors.length === 0 ? phases : null, errors, failedPhase };
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Which stage a caller is allowed to be in, judged only by the evidence it sent.
|
|
236
|
+
*
|
|
237
|
+
* Used by `dsh_workplan_verify`: planning/execution gaps mean the caller is
|
|
238
|
+
* still in phase 1 or 2, where the plugin is disabled by the user's rule.
|
|
239
|
+
*/
|
|
240
|
+
export function phaseGateFromErrors(errors) {
|
|
241
|
+
return errors.some((item) => /^phases(\.| 缺失)/.test(item) && /planning|execution/.test(item))
|
|
242
|
+
? 'phase-1-2'
|
|
243
|
+
: 'verification';
|
|
244
|
+
}
|
|
245
|
+
/** One-line, model-readable summary of what the three phases contain. */
|
|
246
|
+
export function phaseSummaryLine(phases) {
|
|
247
|
+
const deviations = phases.execution.deviations.length;
|
|
248
|
+
return [
|
|
249
|
+
`阶段一 规划(问题梳理 ${phases.planning.problemAnalysis.length} 条 / 可执行思路 ${phases.planning.approach.length} 步)`,
|
|
250
|
+
`阶段二 执行(${phases.execution.steps.length} 步${deviations ? ` / 偏离 ${deviations} 处` : ''})`,
|
|
251
|
+
`阶段三 验证(${phases.verification.checks.length} 项${phases.verification.allPassed ? ',全部通过' : ',未全部通过'};`
|
|
252
|
+
+ `复盘 ${phases.verification.retrospective.problems.length} 个问题 / `
|
|
253
|
+
+ `${phases.verification.retrospective.detours.length} 条弯路 / `
|
|
254
|
+
+ `${phases.verification.retrospective.prevention.length} 条防再犯规则)`,
|
|
255
|
+
].join(';');
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Prompt paragraphs injected into every department child.
|
|
259
|
+
*
|
|
260
|
+
* Kept in this module so the wording and the validator above are one contract:
|
|
261
|
+
* changing what is demanded without changing what is checked would make the
|
|
262
|
+
* prompt lie about completion.
|
|
263
|
+
*/
|
|
264
|
+
export function threePhasePromptLines() {
|
|
265
|
+
return [
|
|
266
|
+
'三阶段工作法(用户强制要求;顺序不可颠倒,阶段一没有产出思路之前禁止动手执行):',
|
|
267
|
+
'【阶段一 · 规划阶段】',
|
|
268
|
+
' 1) 先全面、深入梳理要解决的问题:逐条写清现状与已知事实、约束条件、未知/待确认项、主要矛盾、工作边界;',
|
|
269
|
+
' 2) 再全面、深入思考,整理出能完美完成本子任务的完整、可执行思路:每一步做什么、产出什么、依赖什么、风险与对策、如何证明做对了;',
|
|
270
|
+
' 3) 自检:任何一条验收标准或用户原要求没有落在思路上,就回到第 1 步重做;思路不完整不得进入阶段二。',
|
|
271
|
+
'【阶段二 · 执行阶段】',
|
|
272
|
+
' 严格按阶段一整理出的思路执行。出现新事实需要偏离时,必须记录偏离原因、证据与调整后的做法,不得静默改道。',
|
|
273
|
+
'【阶段三 · 验证阶段】',
|
|
274
|
+
' 执行完成后逐条验证验收标准与用户原要求:每条都要有可复核的证据(命令输出/文件/引用);验证未通过不得报 done,无法确认写 unverified。',
|
|
275
|
+
'【阶段三收尾 · 自我学习复盘(强制)】',
|
|
276
|
+
' 报 done 之前必须把 phases.verification.retrospective 写全,四项缺一不可:',
|
|
277
|
+
' 1) problems:本次全过程遇到的所有问题(现象 + 根因),至少 1 条;',
|
|
278
|
+
' 2) detours:走过的弯路、返工与无效尝试(确实没有就写空数组 [],不要编造);',
|
|
279
|
+
' 3) resolutions:最终是如何解决上述问题的,至少 1 条,可与 problems 一一对应;',
|
|
280
|
+
' 4) prevention:以后如何预防再犯,至少 1 条,必须写成「遇到…先…」的可执行硬规则。',
|
|
281
|
+
' 插件会在完成判定时复核这四项,并在通过后把复盘写入永久记忆文件;下次派发同类任务时,历史教训会自动出现在提示词里。',
|
|
282
|
+
'插件使用规则(严格执行):',
|
|
283
|
+
' - 阶段一、二:dsh-workplan 插件禁用。不得调用任何 dsh_workplan_* 工具(派发、查询、改判、验证等一律不用);唯一例外是遇阻时的 dsh_workplan_escalate 安全上报通道,它不是计划/执行/验证能力。',
|
|
284
|
+
' - 阶段三:可以调用 dsh_workplan_verify 让插件做最终核验:提交 planning / execution / verification 三阶段证据,插件返回 passed 或仍需补齐的 gaps;未通过先修复再回报。',
|
|
285
|
+
' - 结果块里的 phases 是必填项:缺任一阶段、内容为空、或验证未全部通过,插件都会判为未完成,不能用 done 收尾。',
|
|
286
|
+
];
|
|
287
|
+
}
|
package/lib/understanding.js
CHANGED
|
@@ -386,6 +386,245 @@ export function resolvePronounAntecedent(token, conversationContext) {
|
|
|
386
386
|
}
|
|
387
387
|
return null;
|
|
388
388
|
}
|
|
389
|
+
/**
|
|
390
|
+
* [T3] Resolve a pronoun from the task text itself, in the sentence that holds
|
|
391
|
+
* it or the sentence immediately before it.
|
|
392
|
+
*
|
|
393
|
+
* MS2 T05-08 read 「…159667工业母机ETF国泰,下月全月…?准确预判后…告诉我,它每天…」
|
|
394
|
+
* — the name sits in the PREVIOUS sentence while the pronoun's own sentence is
|
|
395
|
+
* pure prose, so conversation-only resolution rejected the task as "no
|
|
396
|
+
* antecedent". A name the user wrote in the task is the strongest possible
|
|
397
|
+
* antecedent, so the task text is searched too:
|
|
398
|
+
*
|
|
399
|
+
* 1. the current sentence, before the pronoun;
|
|
400
|
+
* 2. only if that has no referent, the sentence right before it.
|
|
401
|
+
*
|
|
402
|
+
* Only explicit referents qualify: a quoted name, or an identifier-like span
|
|
403
|
+
* (「159667工业母机ETF国泰」/「159667」/「dsh-workplan」) — the boundaries a reader
|
|
404
|
+
* would use. Ordinary prose never qualifies, so a pronoun with no real referent
|
|
405
|
+
* still blocks (`needs-context`) instead of being resolved to filler.
|
|
406
|
+
*
|
|
407
|
+
* The scope is deliberately narrow: reaching further produced wrong antecedents
|
|
408
|
+
* in replay ("…昨天涨了" taken as a name). A blocked plan is recoverable; a plan
|
|
409
|
+
* dispatched on a guessed referent is not.
|
|
410
|
+
*/
|
|
411
|
+
const INLINE_SENTENCE_BOUNDARY = /[。!?!?\n]/;
|
|
412
|
+
const INLINE_NAME_CHAR = /[\u4e00-\u9fffA-Za-z0-9._-]/;
|
|
413
|
+
const INLINE_ENTITY_STOP = new Set('的是了在有我你他她它们这那吗呢吧啊呀就都也还又很太更最把被让给从到与和及或而但却则其该此每各么怎如何走涨跌买卖加减看好'.split(''));
|
|
414
|
+
const LEADING_REQUEST = /^(?:看一下|看下|看看|帮我|帮忙|麻烦|给我|我们|必须|需要|想要|关注|推荐|分析|预判|预测|研究|介绍|说说|告诉|查一下|查查|对比|比较|关于|针对|请|我|你|想|要|把|将|对)\s*/;
|
|
415
|
+
function isInlineSentenceBoundary(char) {
|
|
416
|
+
return !!char && INLINE_SENTENCE_BOUNDARY.test(char);
|
|
417
|
+
}
|
|
418
|
+
function isInlineNameChar(char) {
|
|
419
|
+
return !!char && INLINE_NAME_CHAR.test(char);
|
|
420
|
+
}
|
|
421
|
+
/** Drop request lead-ins so "我关注159667…" reads as the name, not the sentence. */
|
|
422
|
+
function stripRequestPrefixes(fragment) {
|
|
423
|
+
let value = fragment;
|
|
424
|
+
for (let round = 0; round < 3; round += 1) {
|
|
425
|
+
const next = value.replace(LEADING_REQUEST, '');
|
|
426
|
+
if (next === value)
|
|
427
|
+
break;
|
|
428
|
+
value = next;
|
|
429
|
+
}
|
|
430
|
+
return value.trim();
|
|
431
|
+
}
|
|
432
|
+
/**
|
|
433
|
+
* Is this span an explicit referent rather than a piece of prose?
|
|
434
|
+
*
|
|
435
|
+
* An explicit referent carries an identifier signal (a 4+ digit code such as
|
|
436
|
+
* 159667, or a Latin run such as ETF / dsh-workplan) and contains no function
|
|
437
|
+
* word. That keeps 「159667工业母机ETF国泰」 and drops 「是我重点关注的基金」.
|
|
438
|
+
* Dates (「11月」/「2026年」) and bare numbers are not referents.
|
|
439
|
+
*/
|
|
440
|
+
function isExplicitEntityToken(value) {
|
|
441
|
+
const token = value.trim();
|
|
442
|
+
if (token.length < 2 || token.length > 24)
|
|
443
|
+
return false;
|
|
444
|
+
if (/^(?:它|他|她|其)(?:们)?$/.test(token))
|
|
445
|
+
return false;
|
|
446
|
+
if (/^\d{1,4}(?:年|月|日|号)/.test(token))
|
|
447
|
+
return false;
|
|
448
|
+
if (/^\d{1,2}$/.test(token))
|
|
449
|
+
return false;
|
|
450
|
+
// A bare instrument category names a class, not one instrument.
|
|
451
|
+
if (/^(?:ETF|LOF|QDII|A股|港股|美股)$/.test(token))
|
|
452
|
+
return false;
|
|
453
|
+
const hasCode = /\d{4,}/.test(token);
|
|
454
|
+
const hasLatin = /[A-Za-z]{2,}/.test(token);
|
|
455
|
+
const mixedSignal = /[A-Za-z]\d|\d[A-Za-z]/.test(token);
|
|
456
|
+
if (!hasCode && !hasLatin && !mixedSignal)
|
|
457
|
+
return false;
|
|
458
|
+
for (const char of token) {
|
|
459
|
+
if (INLINE_ENTITY_STOP.has(char))
|
|
460
|
+
return false;
|
|
461
|
+
}
|
|
462
|
+
return !FUNCTION_WORD_ONLY.test(token);
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* Take the explicit referent out of one punctuation/space-delimited fragment.
|
|
466
|
+
*
|
|
467
|
+
* A clean fragment is kept whole; otherwise the identifier and the name
|
|
468
|
+
* characters attached to it are cut out, stopping at the first function word
|
|
469
|
+
* or verb ("159667工业母机ETF国泰是我重点…" -> "159667工业母机ETF国泰").
|
|
470
|
+
*/
|
|
471
|
+
function inlineEntityCore(fragment) {
|
|
472
|
+
const raw = stripRequestPrefixes(fragment).replace(/[。!?!?…]+$/, '').trim();
|
|
473
|
+
if (!raw)
|
|
474
|
+
return null;
|
|
475
|
+
if (isExplicitEntityToken(raw))
|
|
476
|
+
return raw;
|
|
477
|
+
const anchor = /\d{2,}|[A-Za-z]{2,}/.exec(raw);
|
|
478
|
+
if (!anchor)
|
|
479
|
+
return null;
|
|
480
|
+
let start = anchor.index;
|
|
481
|
+
while (start > 0 && isInlineNameChar(raw[start - 1]) && !INLINE_ENTITY_STOP.has(raw[start - 1]))
|
|
482
|
+
start -= 1;
|
|
483
|
+
let end = anchor.index + anchor[0].length;
|
|
484
|
+
while (end < raw.length && isInlineNameChar(raw[end]) && !INLINE_ENTITY_STOP.has(raw[end]))
|
|
485
|
+
end += 1;
|
|
486
|
+
const value = raw.slice(start, end).replace(/[.\-_]+$/, '').trim();
|
|
487
|
+
return isExplicitEntityToken(value) ? value : null;
|
|
488
|
+
}
|
|
489
|
+
/** Find the last explicit referent inside one sentence span. */
|
|
490
|
+
function findExplicitEntity(span) {
|
|
491
|
+
const text = span.trim();
|
|
492
|
+
if (!text)
|
|
493
|
+
return null;
|
|
494
|
+
const quoted = [...text.matchAll(/[「『“"《]([^」』”"》\n]{2,24})[」』”"》]/g)];
|
|
495
|
+
for (let i = quoted.length - 1; i >= 0; i -= 1) {
|
|
496
|
+
const value = quoted[i][1].trim();
|
|
497
|
+
if (isExplicitEntityToken(value))
|
|
498
|
+
return { antecedent: value, source: text.slice(-80) };
|
|
499
|
+
}
|
|
500
|
+
const fragments = text.split(/[,,、;;::\s]+/);
|
|
501
|
+
for (let i = fragments.length - 1; i >= 0; i -= 1) {
|
|
502
|
+
const value = inlineEntityCore(fragments[i]);
|
|
503
|
+
if (value)
|
|
504
|
+
return { antecedent: value, source: text.slice(-80) };
|
|
505
|
+
}
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
508
|
+
/** Bounds of the sentence around `index`, plus the sentence immediately before. */
|
|
509
|
+
function inlineSentenceBounds(text, index) {
|
|
510
|
+
let start = 0;
|
|
511
|
+
for (let i = index - 1; i >= 0; i -= 1) {
|
|
512
|
+
if (isInlineSentenceBoundary(text[i])) {
|
|
513
|
+
start = i + 1;
|
|
514
|
+
break;
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
let prevEnd = start;
|
|
518
|
+
while (prevEnd > 0 && isInlineSentenceBoundary(text[prevEnd - 1]))
|
|
519
|
+
prevEnd -= 1;
|
|
520
|
+
let prevStart = 0;
|
|
521
|
+
for (let i = prevEnd - 1; i >= 0; i -= 1) {
|
|
522
|
+
if (isInlineSentenceBoundary(text[i])) {
|
|
523
|
+
prevStart = i + 1;
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
return { start, prevStart, prevEnd };
|
|
528
|
+
}
|
|
529
|
+
/** 「其它」 is a word, not a pronoun: skip it and use the real occurrence. */
|
|
530
|
+
function lastInlineReferenceIndex(text, token) {
|
|
531
|
+
let index = text.lastIndexOf(token);
|
|
532
|
+
while (index > 0 && token === '它' && text[index - 1] === '其') {
|
|
533
|
+
index = text.lastIndexOf(token, index - 1);
|
|
534
|
+
}
|
|
535
|
+
return index;
|
|
536
|
+
}
|
|
537
|
+
export function resolvePronounAntecedentInline(token, taskText) {
|
|
538
|
+
const text = String(taskText ?? '');
|
|
539
|
+
if (!text || text.length > MAX_TASK_ANCHOR_CHARS)
|
|
540
|
+
return null;
|
|
541
|
+
const index = lastInlineReferenceIndex(text, token);
|
|
542
|
+
if (index <= 0)
|
|
543
|
+
return null;
|
|
544
|
+
const bounds = inlineSentenceBounds(text, index);
|
|
545
|
+
const current = findExplicitEntity(text.slice(bounds.start, index));
|
|
546
|
+
if (current)
|
|
547
|
+
return current;
|
|
548
|
+
if (bounds.prevEnd > bounds.prevStart) {
|
|
549
|
+
const previous = findExplicitEntity(text.slice(bounds.prevStart, bounds.prevEnd));
|
|
550
|
+
if (previous)
|
|
551
|
+
return previous;
|
|
552
|
+
}
|
|
553
|
+
return null;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* [P0-2c] Decide whether a task is missing information the user must supply.
|
|
557
|
+
*
|
|
558
|
+
* MS2's four clarify cases (T05-04/09/13/20) were planned and dispatched anyway.
|
|
559
|
+
* Each failed for a *checkable* reason, so each gets a deterministic rule instead
|
|
560
|
+
* of a vibe:
|
|
561
|
+
*
|
|
562
|
+
* C1 bare agreement — the text is an answer to a question the model never saw
|
|
563
|
+
* (「同意移入」) and carries an identifier it never defines (R-W5-01).
|
|
564
|
+
* C2 dangling artifact — an internal reference (T4 草稿, W5) that is not defined
|
|
565
|
+
* in the task and not present in the conversation.
|
|
566
|
+
* C3 missing target — a forecast/trend request with no identifiable subject
|
|
567
|
+
* (「准确预测11月全月详细、具体走势」: which instrument? which year?).
|
|
568
|
+
* C4 missing UI object — a display change (折叠/展开/按钮…) that names no surface
|
|
569
|
+
* (左侧栏/截图/插件…), so any implementation would be a guess.
|
|
570
|
+
*/
|
|
571
|
+
export function auditClarifyNeed(taskText, conversationContext = '') {
|
|
572
|
+
const text = String(taskText ?? '').trim();
|
|
573
|
+
const conversation = String(conversationContext ?? '');
|
|
574
|
+
if (!text)
|
|
575
|
+
return { needed: false, rule: '', reason: '', question: '' };
|
|
576
|
+
const both = text + '\n' + conversation;
|
|
577
|
+
const AGREEMENT = /(?:^|[。;;!!\n])\s*(?:同意|赞成|可以|好的|没问题|就按|按你说的|确认)/;
|
|
578
|
+
// No trailing \b: a CJK char is a non-word char, so 「T4 草稿」 has no word
|
|
579
|
+
// boundary after 稿 and the id would silently never match.
|
|
580
|
+
const DANGLING_ID = /(?:R-[A-Z]{1,4}-\d{1,4}|W\d{1,3}\b|T\d{1,3}\s*草稿)/;
|
|
581
|
+
if (AGREEMENT.test(text) && (DANGLING_ID.test(text) || text.length <= 24) && !DANGLING_ID.test(conversation)) {
|
|
582
|
+
return {
|
|
583
|
+
needed: true,
|
|
584
|
+
rule: 'C1',
|
|
585
|
+
reason: '任务只是对上文问题的表态,且引用了任务里没有定义的编号,缺上文',
|
|
586
|
+
question: '你这句话是在回答哪个问题?请把上文的问题、或你要「同意/移入」的具体对象说清楚。',
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
const dangling = text.match(DANGLING_ID);
|
|
590
|
+
if (dangling && !conversation.includes(dangling[0])) {
|
|
591
|
+
return {
|
|
592
|
+
needed: true,
|
|
593
|
+
rule: 'C2',
|
|
594
|
+
reason: '任务引用了「' + dangling[0] + '」这一内部编号/草稿,但任务原文没有定义它,也找不到上文',
|
|
595
|
+
question: '「' + dangling[0] + '」指的是什么?请给出文件路径、链接或把内容贴进来。',
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
const FORECAST = /(预测|预判|走势|涨跌|行情)/;
|
|
599
|
+
const SUBJECT = /(?:\d{6}|[A-Z]{2,}\d+|[「『“"《][^」』”"》]{2,20}[」』”"》])/;
|
|
600
|
+
// The missing subject may already be named in the conversation (「它走势如何」 after
|
|
601
|
+
// the user quoted a ticker). Only the task's own words make it a forecast request.
|
|
602
|
+
if (FORECAST.test(text) && !SUBJECT.test(both)) {
|
|
603
|
+
return {
|
|
604
|
+
needed: true,
|
|
605
|
+
rule: 'C3',
|
|
606
|
+
reason: '要求预测走势,但任务里没有可识别标的(股票代码/名称),也没有年份,缺关键输入',
|
|
607
|
+
question: '要预测哪只标的(股票代码或名称)?看哪个时间段?',
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
const UI_VERB = /(折叠|展开|按钮|显示|布局|样式|颜色|排序|置顶|收起)/;
|
|
611
|
+
// 红圈/红框/圈出 name a surface the same way 截图 does: the user has pointed at
|
|
612
|
+
// something concrete. Without them 「把红圈处改为默认收起」 was C4-blocked even
|
|
613
|
+
// though the sentence is unambiguous (caught by department-fit.test.ts).
|
|
614
|
+
const UI_OBJECT = /(左侧栏|右侧栏|侧边栏|侧栏|面板|对话框|弹窗|菜单|工具栏|页面|截图|画圈|红圈|红框|圈出|插件|顶栏|底栏|任务进度)/;
|
|
615
|
+
// Same rule as C3: 「把它改成默认收起」 is not missing a surface when the previous
|
|
616
|
+
// turn said 「截图红圈处的分步进度」. The verb must be in the task, the object may
|
|
617
|
+
// come from either side (plan-anchor.test.ts 'strict mode blocks…' covers this).
|
|
618
|
+
if (UI_VERB.test(text) && !UI_OBJECT.test(both)) {
|
|
619
|
+
return {
|
|
620
|
+
needed: true,
|
|
621
|
+
rule: 'C4',
|
|
622
|
+
reason: '要求改动界面显示,但没有说明改哪个界面/哪个板块,缺指代对象',
|
|
623
|
+
question: '要改的是哪个界面/哪个板块?(例如左侧栏、任务进度面板、某个按钮;有截图更好)',
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
return { needed: false, rule: '', reason: '', question: '' };
|
|
627
|
+
}
|
|
389
628
|
/**
|
|
390
629
|
* Split one clause into candidate noun phrases.
|
|
391
630
|
*
|
|
@@ -836,15 +1075,24 @@ export function auditTaskUnderstanding(plan, context = {}) {
|
|
|
836
1075
|
const resolvedByContext = extracted.references.map((reference) => {
|
|
837
1076
|
if (reference.resolved)
|
|
838
1077
|
return reference;
|
|
839
|
-
const
|
|
1078
|
+
const fromConversation = resolvePronounAntecedent(reference.token, conversation);
|
|
1079
|
+
const found = fromConversation ?? resolvePronounAntecedentInline(reference.token, taskText);
|
|
840
1080
|
if (!found)
|
|
841
1081
|
return reference;
|
|
842
1082
|
// Option letters are resolved only when the conversation lists that option
|
|
843
1083
|
// as a listed choice ("A 方案" / "B 选项"), not a bare letter in a word.
|
|
1084
|
+
// Task-text entities never satisfy this rule on their own: the option must
|
|
1085
|
+
// be defined where the user chose it, i.e. in the conversation.
|
|
844
1086
|
const listedOption = new RegExp('(?:^|[^A-Za-z])' + reference.token + '\\s*(?:方案|选项)');
|
|
845
1087
|
if (/^[A-D]$/.test(reference.token) && !listedOption.test(conversation))
|
|
846
1088
|
return reference;
|
|
847
|
-
|
|
1089
|
+
/*
|
|
1090
|
+
* [T3] Record where the referent came from. Reusing 'context' for an
|
|
1091
|
+
* antecedent found inside the task text would misreport an empty
|
|
1092
|
+
* conversation as the source, so the audit keeps the two apart.
|
|
1093
|
+
*/
|
|
1094
|
+
const resolvedBy = fromConversation ? 'context' : 'task-text';
|
|
1095
|
+
return { ...reference, resolved: true, resolvedBy, antecedent: found.antecedent };
|
|
848
1096
|
});
|
|
849
1097
|
const unresolved = resolvedByContext.filter((reference) => !reference.resolved);
|
|
850
1098
|
const reasons = [];
|
|
@@ -938,6 +1186,14 @@ export function validatePlanContext(plan, context = {}) {
|
|
|
938
1186
|
if (audit.stage3.verdict === 'needs-clarification') {
|
|
939
1187
|
errors.push(`CLARIFY: ${audit.stage3.reasons.find((reason) => reason.includes('自相矛盾')) ?? '用户要求存在矛盾,必须先向用户确认'}`);
|
|
940
1188
|
}
|
|
1189
|
+
/*
|
|
1190
|
+
* [P0-2c] Missing inputs must stop the plan before any department agent runs:
|
|
1191
|
+
* MS2 T05-04/09/13/20 were all planned and dispatched with the key input absent.
|
|
1192
|
+
*/
|
|
1193
|
+
const clarify = auditClarifyNeed(context.taskText ?? audit.anchor?.text ?? '', context.conversationContext ?? '');
|
|
1194
|
+
if (clarify.needed) {
|
|
1195
|
+
errors.push('CLARIFY: ' + clarify.reason + '(规则 ' + clarify.rule + ')');
|
|
1196
|
+
}
|
|
941
1197
|
if (audit.stage3.verdict === 'needs-context') {
|
|
942
1198
|
errors.push(`CONTEXT: ${audit.stage3.reasons.find((reason) => reason.includes('指代')) ?? '任务含指代但在最近对话中找不到先行词,必须先向用户确认'}`);
|
|
943
1199
|
}
|
package/package.json
CHANGED
package/src/departments.ts
CHANGED
|
@@ -173,6 +173,13 @@ export const DEPARTMENT_CAPABILITIES: readonly DepartmentCapability[] = Object.f
|
|
|
173
173
|
keywords: [
|
|
174
174
|
'法务', '合同', '版权', '许可', '合规', '条款', '法律', '隐私', '授权', '侵权', '国际标准',
|
|
175
175
|
'ISO/IEC 15408', '15408', 'ISO/IEC', 'ISO 标准', 'CC 标准',
|
|
176
|
+
// [P0-4 recall] T05-12 「会不会窃取用户电脑和移动终端里的文件和数据」 is a
|
|
177
|
+
// privacy/authorisation claim and matched nothing: the list had 隐私/授权 but
|
|
178
|
+
// not the verbs a user actually writes. These are plain words, so the
|
|
179
|
+
// intrusion guard keeps legal out of unrelated plans.
|
|
180
|
+
// 「泄露」 alone would also fire on 「内存泄露」 (a memory leak is engineering,
|
|
181
|
+
// not legal); 「信息泄露」 keeps the privacy sense and the keyword count at 22.
|
|
182
|
+
'窃取', '信息泄露', '个人信息', '数据安全', '隐私风险', '移动终端',
|
|
176
183
|
],
|
|
177
184
|
},
|
|
178
185
|
{
|