dsh-workplan 1.12.3 → 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 +67 -0
- package/README.en.md +17 -2
- package/README.md +41 -3
- 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/release-notes-2.0.1.md +56 -0
- package/lib/host.js +195 -2
- package/lib/lessons.js +378 -0
- package/lib/plan.js +37 -5
- package/lib/runtime.js +178 -2
- package/lib/three-phase.js +287 -0
- package/package.json +1 -1
- package/src/host.ts +202 -2
- package/src/lessons.ts +445 -0
- package/src/plan.ts +37 -4
- package/src/runtime.ts +184 -1
- package/src/three-phase.ts +342 -0
package/lib/runtime.js
CHANGED
|
@@ -14,8 +14,10 @@
|
|
|
14
14
|
import { randomUUID } from 'node:crypto';
|
|
15
15
|
import { existsSync, realpathSync, statSync } from 'node:fs';
|
|
16
16
|
import { basename, dirname, isAbsolute, join, resolve } from 'node:path';
|
|
17
|
-
import { MAX_CONCURRENT, PlanError, aggregateInvariantViolations, applyStatus, attachPlanMeta, blockCancelledDependents, buildChildPrompt, buildCancelMessage, buildClarifyMessage, buildDegradedMessage, buildPlanMessage, buildPlannerPrompt, buildResultMessage, buildStopSteerText, createStatuses, deliveryReadiness, derivePlanSnapshot, extractJson, flattenSubtasks, isRecord, isSocialMessage, isTerminalPhase, isStatusQuestion, isVagueContinuation, makeUserMessage, markDependentsBlocked, messageText, readySubtasks, MAX_PLAN_RESUMES, PRESENT_DENY_CODES, canResumePlan, resetBlockedStatuses, parseCancelMessage, parseResultMessage, parseWorkplanMessage, planPhase, reconcilePhase, truncate, validatePlan, applyResultStatuses, adjudicateResult, activeDenyCode, MAX_CONVERSATION_CHARS, MAX_ESCALATION_OPTIONS, MAX_INSTRUCTION_CHARS, MAX_QUESTION_CHARS, parseUpdateMessage, buildUpdateMessage, normaliseHandoffs, openHandoffs, DEPARTMENTS, DEPARTMENT_IDS, MAX_HANDOFF_RESPONSE_CHARS, } from './plan.js';
|
|
17
|
+
import { MAX_CONCURRENT, PlanError, aggregateInvariantViolations, applyStatus, attachPlanMeta, blockCancelledDependents, buildChildPrompt, buildCancelMessage, buildClarifyMessage, buildDegradedMessage, buildPlanMessage, buildPlannerPrompt, buildResultMessage, buildStopSteerText, createStatuses, deliveryReadiness, derivePlanSnapshot, extractJson, flattenSubtasks, isRecord, isSocialMessage, isTerminalPhase, isStatusQuestion, isVagueContinuation, makeUserMessage, markDependentsBlocked, messageText, readySubtasks, MAX_PLAN_RESUMES, PRESENT_DENY_CODES, canResumePlan, resetBlockedStatuses, parseCancelMessage, parseResultMessage, parseWorkplanMessage, planPhase, reconcilePhase, truncate, validatePlan, applyResultStatuses, adjudicateResult, activeDenyCode, MAX_CONVERSATION_CHARS, MAX_ESCALATION_OPTIONS, MAX_INSTRUCTION_CHARS, MAX_QUESTION_CHARS, parseUpdateMessage, buildUpdateMessage, normaliseHandoffs, openHandoffs, DEPARTMENTS, DEPARTMENT_IDS, MAX_HANDOFF_RESPONSE_CHARS, findSubtask, } from './plan.js';
|
|
18
18
|
import { auditClarifyNeed, compactUnderstandingAudit, validatePlanContext } from './understanding.js';
|
|
19
|
+
import { phaseGateFromErrors, phaseSummaryLine, validatePhaseEvidence } from './three-phase.js';
|
|
20
|
+
import { lessonStats, rankLessons, readLessons, recordLesson, renderLessonsBrief, resolveLessonsFile, } from './lessons.js';
|
|
19
21
|
import { computeMappingDigest, verifyMappingDigest } from './supersede-digest.js';
|
|
20
22
|
import { supersedePlanRecord } from './supersede.js';
|
|
21
23
|
import { createProgressWatchdog, describeError } from './watchdog.js';
|
|
@@ -773,11 +775,40 @@ function settleFromOutput(record, subtask, output, guard, options) {
|
|
|
773
775
|
const checked = checkArtifacts(verdict.artifacts, options.cwd, options.artifactExists);
|
|
774
776
|
const escalated = verdict.status === 'blocked'
|
|
775
777
|
&& typeof verdict.question === 'string' && verdict.question.trim() !== '';
|
|
778
|
+
/*
|
|
779
|
+
* [2.0.0] 自我学习闭环的出口:完成且带复盘证据时,立刻写入永久记忆。
|
|
780
|
+
*
|
|
781
|
+
* 写入失败不改变本次判定(教训没记住是记忆层的问题,不能把已经做完的工作
|
|
782
|
+
* 判成未完成),但会把失败原因写进本条状态,避免「以为记住了其实没写」。
|
|
783
|
+
*/
|
|
784
|
+
let lessonNote = '';
|
|
785
|
+
if (verdict.status === 'done' && verdict.phases?.verification?.retrospective) {
|
|
786
|
+
try {
|
|
787
|
+
const saved = recordLesson({
|
|
788
|
+
file: resolveLessonsFile(),
|
|
789
|
+
planId: String(record.plan?.planId ?? ''),
|
|
790
|
+
subtaskId: subtask.id,
|
|
791
|
+
department: String(subtask.department ?? ''),
|
|
792
|
+
title: `${subtask.id} ${subtask.title}`,
|
|
793
|
+
cwd: typeof options.cwd === 'string' ? options.cwd : '',
|
|
794
|
+
retrospective: verdict.phases.verification.retrospective,
|
|
795
|
+
now: Date.now(),
|
|
796
|
+
});
|
|
797
|
+
if (saved) {
|
|
798
|
+
lessonNote = saved.mode === 'repeated'
|
|
799
|
+
? `已记住同类教训(第 ${saved.record.occurrences} 次,永久记忆)`
|
|
800
|
+
: '复盘已写入永久教训记忆';
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
catch (error) {
|
|
804
|
+
lessonNote = `复盘记忆写入失败:${truncate(error instanceof Error ? error.message : String(error), 120)}`;
|
|
805
|
+
}
|
|
806
|
+
}
|
|
776
807
|
writeSubtaskStatus(record, subtask.id, {
|
|
777
808
|
status: verdict.status,
|
|
778
809
|
progress: 100,
|
|
779
810
|
finishedAt: Date.now(),
|
|
780
|
-
summary: truncate(output, 3000),
|
|
811
|
+
summary: truncate(lessonNote ? `${output}\n[自我学习] ${lessonNote}` : output, 3000),
|
|
781
812
|
error: verdict.error,
|
|
782
813
|
verification: verdict.verification,
|
|
783
814
|
acceptance: verdict.acceptance,
|
|
@@ -912,6 +943,24 @@ async function executeSubtask(ctx, record, stage, subtask, options) {
|
|
|
912
943
|
if (child.controller.signal.aborted) {
|
|
913
944
|
throw child.controller.signal.reason ?? abortError();
|
|
914
945
|
}
|
|
946
|
+
/*
|
|
947
|
+
* [2.0.0] 自我学习闭环的入口:把与本子任务最相关的历史教训注入提示词。
|
|
948
|
+
*
|
|
949
|
+
* 记忆读取失败不能影响本次派发(宁可不带教训,也不能因此不做任务),
|
|
950
|
+
* 所以整段用 try/catch 兜底;文件不存在时 renderLessonsBrief 返回空串。
|
|
951
|
+
*/
|
|
952
|
+
let lessonsBrief = '';
|
|
953
|
+
try {
|
|
954
|
+
lessonsBrief = renderLessonsBrief({
|
|
955
|
+
department: subtask.department,
|
|
956
|
+
title: subtask.title,
|
|
957
|
+
objective: subtask.objective,
|
|
958
|
+
cwd: options.cwd,
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
catch {
|
|
962
|
+
lessonsBrief = '';
|
|
963
|
+
}
|
|
915
964
|
activation = await ctx.subagents.startActivation({
|
|
916
965
|
provider: 'spawn',
|
|
917
966
|
label: `${subtask.department}:${subtask.id} ${subtask.title}`.slice(0, 120),
|
|
@@ -944,6 +993,7 @@ async function executeSubtask(ctx, record, stage, subtask, options) {
|
|
|
944
993
|
conversationContext,
|
|
945
994
|
statuses: record.statuses,
|
|
946
995
|
previousFailure: attempt > 1 ? lastFailure : null,
|
|
996
|
+
lessonsBrief,
|
|
947
997
|
}),
|
|
948
998
|
}],
|
|
949
999
|
parent: options.agent,
|
|
@@ -3940,6 +3990,87 @@ export function createWorkplanRuntime(ctx, options = {}) {
|
|
|
3940
3990
|
};
|
|
3941
3991
|
}
|
|
3942
3992
|
}
|
|
3993
|
+
/**
|
|
3994
|
+
* [1.13.0] 阶段三(验证阶段)的插件核验入口(用户 2026-11-05)。
|
|
3995
|
+
*
|
|
3996
|
+
* 用户规则:第一、二阶段禁用 dsh-workplan 插件,第三阶段才可以用插件验证
|
|
3997
|
+
* 是否成功、是否完全满足要求。这里的实现方式是「按提交内容判阶段」:
|
|
3998
|
+
* - 只有 planning + execution 证据齐全,才会进入阶段三核验;
|
|
3999
|
+
* - 缺 planning/execution 的调用直接以 PHASE_GATE 拒绝,并说明插件在
|
|
4000
|
+
* 阶段一、二禁用 —— 子 Agent 无法用插件跳过梳理与规划;
|
|
4001
|
+
* - 阶段三核验会逐条检查:问题梳理/可执行思路是否有实质内容、执行步骤
|
|
4002
|
+
* 是否记录、验证是否全部通过、验证记录是否覆盖全部验收标准。
|
|
4003
|
+
*
|
|
4004
|
+
* 它不写状态、不替子 Agent 宣称完成:最终是否 done 仍由 `adjudicateResult`
|
|
4005
|
+
* 对同一份 phases 再判一次,避免「核验工具说通过」与「结果块没写」两套真相。
|
|
4006
|
+
*/
|
|
4007
|
+
async function verifySubtask(agent, args) {
|
|
4008
|
+
const childSession = agent?.session;
|
|
4009
|
+
if (!childSession?.id) {
|
|
4010
|
+
return { ok: false, code: 'NO_SESSION', passed: false, error: '只能在部门子会话内做阶段三核验' };
|
|
4011
|
+
}
|
|
4012
|
+
const parentId = childSession.header?.parentSession;
|
|
4013
|
+
if (!parentId) {
|
|
4014
|
+
return {
|
|
4015
|
+
ok: false,
|
|
4016
|
+
code: 'NOT_CHILD_SESSION',
|
|
4017
|
+
passed: false,
|
|
4018
|
+
error: '当前不是子会话,无法反查父计划;三阶段核验只对已派发的部门子任务开放',
|
|
4019
|
+
};
|
|
4020
|
+
}
|
|
4021
|
+
let record = state.records.get(parentId);
|
|
4022
|
+
if (!record?.plan) {
|
|
4023
|
+
const parent = ctx.sessions?.get?.(parentId);
|
|
4024
|
+
if (parent) {
|
|
4025
|
+
try {
|
|
4026
|
+
record = reconstructFromSession(parent);
|
|
4027
|
+
}
|
|
4028
|
+
catch (error) {
|
|
4029
|
+
pushRecordError({ errors: [] }, error);
|
|
4030
|
+
}
|
|
4031
|
+
}
|
|
4032
|
+
}
|
|
4033
|
+
if (!record?.plan) {
|
|
4034
|
+
return { ok: false, code: 'NO_PLAN', passed: false, error: '父会话没有可关联的计划,无法核验' };
|
|
4035
|
+
}
|
|
4036
|
+
const row = (record.statuses ?? []).find((item) => item?.childId === childSession.id);
|
|
4037
|
+
if (!row) {
|
|
4038
|
+
return { ok: false, code: 'NO_SUBTASK', passed: false, error: '计划里找不到与本子会话关联的子任务(childId 未登记)' };
|
|
4039
|
+
}
|
|
4040
|
+
if (row.status === 'done') {
|
|
4041
|
+
return { ok: false, code: 'ALREADY_DONE', passed: false, subtaskId: row.subtaskId, error: '该子任务已标记完成,无需再次核验' };
|
|
4042
|
+
}
|
|
4043
|
+
const found = findSubtask(record.plan, row.subtaskId);
|
|
4044
|
+
const expectedAcceptance = Array.isArray(found?.subtask?.acceptance) ? found.subtask.acceptance : [];
|
|
4045
|
+
const rawPhases = isRecord(args?.phases)
|
|
4046
|
+
? args.phases
|
|
4047
|
+
: { planning: args?.planning, execution: args?.execution, verification: args?.verification };
|
|
4048
|
+
const check = validatePhaseEvidence(rawPhases, expectedAcceptance);
|
|
4049
|
+
if (!check.ok || !check.phases) {
|
|
4050
|
+
const gate = phaseGateFromErrors(check.errors);
|
|
4051
|
+
return {
|
|
4052
|
+
ok: false,
|
|
4053
|
+
passed: false,
|
|
4054
|
+
code: gate === 'phase-1-2' ? 'PHASE_GATE' : 'VERIFICATION_INCOMPLETE',
|
|
4055
|
+
phase: gate,
|
|
4056
|
+
planId: record.plan.planId,
|
|
4057
|
+
subtaskId: row.subtaskId,
|
|
4058
|
+
gaps: check.errors,
|
|
4059
|
+
error: gate === 'phase-1-2'
|
|
4060
|
+
? `插件在阶段一、二禁用:先完成「梳理问题 → 形成可执行思路」并实际执行,阶段三才能用插件核验。${check.errors.slice(0, 5).join(';')}`
|
|
4061
|
+
: `阶段三核验未通过:${check.errors.slice(0, 5).join(';')}`,
|
|
4062
|
+
};
|
|
4063
|
+
}
|
|
4064
|
+
return {
|
|
4065
|
+
ok: true,
|
|
4066
|
+
passed: true,
|
|
4067
|
+
phase: 'verification',
|
|
4068
|
+
planId: record.plan.planId,
|
|
4069
|
+
subtaskId: row.subtaskId,
|
|
4070
|
+
summary: phaseSummaryLine(check.phases),
|
|
4071
|
+
message: '插件核验通过:三阶段证据齐全、验证全部通过。现在把同一份 phases 写进结果块并提交。',
|
|
4072
|
+
};
|
|
4073
|
+
}
|
|
3943
4074
|
/**
|
|
3944
4075
|
* [1.11.0] 子 → 父的唯一反向通道(T4 C-09 / T6 M5)。
|
|
3945
4076
|
*
|
|
@@ -5653,6 +5784,49 @@ export function createWorkplanRuntime(ctx, options = {}) {
|
|
|
5653
5784
|
});
|
|
5654
5785
|
}
|
|
5655
5786
|
}
|
|
5787
|
+
/**
|
|
5788
|
+
* [2.0.0] 只读的「永久教训记忆」入口。
|
|
5789
|
+
*
|
|
5790
|
+
* 用户要求这些经验必须能被复核。主 Agent 在阶段三可以用它查:记住了什么、
|
|
5791
|
+
* 哪些同类错误在重复发生、文件到底写在哪。它只读文件,不改任何计划状态。
|
|
5792
|
+
*/
|
|
5793
|
+
async function lessonsTool(_agent, args) {
|
|
5794
|
+
const parsed = isRecord(args) ? args : {};
|
|
5795
|
+
const action = String(parsed.action ?? 'list');
|
|
5796
|
+
const file = resolveLessonsFile();
|
|
5797
|
+
try {
|
|
5798
|
+
if (action === 'stats') {
|
|
5799
|
+
return { ok: true, ...lessonStats(file) };
|
|
5800
|
+
}
|
|
5801
|
+
if (action === 'search') {
|
|
5802
|
+
const query = String(parsed.query ?? '').trim();
|
|
5803
|
+
const rows = query
|
|
5804
|
+
? readLessons(file).filter((item) => `${item.title} ${item.problems.join(' ')} ${item.detours.join(' ')} ${item.prevention.join(' ')}`
|
|
5805
|
+
.toLowerCase()
|
|
5806
|
+
.includes(query.toLowerCase()))
|
|
5807
|
+
: readLessons(file);
|
|
5808
|
+
const limit = Math.min(Math.max(Number(parsed.limit) || 10, 1), 50);
|
|
5809
|
+
return { ok: true, file, total: rows.length, lessons: rows.slice(-limit).reverse() };
|
|
5810
|
+
}
|
|
5811
|
+
const department = typeof parsed.department === 'string' ? parsed.department.trim() : '';
|
|
5812
|
+
const limit = Math.min(Math.max(Number(parsed.limit) || 10, 1), 50);
|
|
5813
|
+
const lessons = department
|
|
5814
|
+
? readLessons(file).filter((item) => item.department === department)
|
|
5815
|
+
: readLessons(file);
|
|
5816
|
+
const ranked = department
|
|
5817
|
+
? rankLessons(lessons, { department, limit, now: Date.now() })
|
|
5818
|
+
: lessons.slice(-limit).reverse();
|
|
5819
|
+
return { ok: true, file, total: lessons.length, lessons: ranked };
|
|
5820
|
+
}
|
|
5821
|
+
catch (error) {
|
|
5822
|
+
return {
|
|
5823
|
+
ok: false,
|
|
5824
|
+
file,
|
|
5825
|
+
error: truncate(error instanceof Error ? error.message : String(error), 300),
|
|
5826
|
+
lessons: [],
|
|
5827
|
+
};
|
|
5828
|
+
}
|
|
5829
|
+
}
|
|
5656
5830
|
function dispose() {
|
|
5657
5831
|
// [1.10.4] 定时器不能吊住进程,也不能在记录清空后复活一个已销毁的计划。
|
|
5658
5832
|
for (const record of state.records.values())
|
|
@@ -5675,6 +5849,8 @@ export function createWorkplanRuntime(ctx, options = {}) {
|
|
|
5675
5849
|
statusTool,
|
|
5676
5850
|
followupTool,
|
|
5677
5851
|
escalateTool,
|
|
5852
|
+
verifyTool: verifySubtask,
|
|
5853
|
+
lessonsTool,
|
|
5678
5854
|
instructTool,
|
|
5679
5855
|
handoffTool,
|
|
5680
5856
|
replanPlan,
|
|
@@ -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/package.json
CHANGED