dsh-workplan 1.10.6 → 1.11.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 +130 -0
- package/README.en.md +8 -3
- package/README.md +9 -4
- package/client.js +142 -5
- package/docs/collaboration.md +16 -5
- package/docs/departments.md +2 -2
- package/docs/release-record-1.10.6-20261101.md +78 -0
- package/docs/release-record-1.11.0-20261102.md +63 -0
- package/lib/client.js +142 -5
- package/lib/host.js +422 -4
- package/lib/plan.js +869 -37
- package/lib/replan.js +678 -0
- package/lib/runtime.js +1316 -27
- package/package.json +1 -1
- package/src/client.ts +154 -4
- package/src/host.ts +453 -3
- package/src/plan.ts +827 -32
- package/src/replan.ts +708 -0
- package/src/runtime.ts +1305 -26
package/lib/plan.js
CHANGED
|
@@ -42,6 +42,8 @@ export const PRESENT_DENY_CODES = {
|
|
|
42
42
|
EVIDENCE_GAP: 'EVIDENCE_GAP',
|
|
43
43
|
CANCELLED_NAMED: 'CANCELLED_NAMED',
|
|
44
44
|
SUPERSEDED_STALE: 'SUPERSEDED_STALE',
|
|
45
|
+
/* [1.11.0] 子会话不是交付主体:present 只能由根会话发起(T7 P9)。 */
|
|
46
|
+
NOT_ROOT: 'NOT_ROOT',
|
|
45
47
|
};
|
|
46
48
|
/**
|
|
47
49
|
* P1 的单一映射:active 计划未就绪时,只由交付就绪结果决定拒绝码。
|
|
@@ -88,6 +90,71 @@ export const VERIFICATIONS = Object.freeze(['structured', 'legacy', 'unverified'
|
|
|
88
90
|
export const EVIDENCE_STATUSES = Object.freeze(['done', 'blocked', 'partial']);
|
|
89
91
|
export const ACCEPTANCE_RESULTS = Object.freeze(['pass', 'fail', 'unverified']);
|
|
90
92
|
export const STATUSES = Object.freeze(['pending', 'in_progress', 'done', 'blocked']);
|
|
93
|
+
/**
|
|
94
|
+
* [1.11.0] 正交闭环状态(T4 C-01 / T6 M1)。
|
|
95
|
+
*
|
|
96
|
+
* `status` 仍是唯一的呈现层四值枚举,左侧栏、计数、交付门禁全部只看它。
|
|
97
|
+
* `closure` 记录「这条子任务在双向闭环里走到哪一步」:是否被接收方确认、
|
|
98
|
+
* 是否在等待主 Agent 指示、是否已回报。两者同时写入同一条状态条目,但只有
|
|
99
|
+
* `applyStatus` 一个写入口,不引入第二份存储。
|
|
100
|
+
*
|
|
101
|
+
* 历史日志没有 `closure`(`undefined`)时按 `null` 解释,语义退回 1.10.6;
|
|
102
|
+
* 因此旧会话回放、旧测试夹具都不需要迁移。
|
|
103
|
+
*/
|
|
104
|
+
export const CLOSURES = Object.freeze([
|
|
105
|
+
'pending',
|
|
106
|
+
'assigned',
|
|
107
|
+
'acknowledged',
|
|
108
|
+
'instructed',
|
|
109
|
+
'escalated',
|
|
110
|
+
'blocked',
|
|
111
|
+
'done',
|
|
112
|
+
'reported',
|
|
113
|
+
'replanned',
|
|
114
|
+
]);
|
|
115
|
+
/** Why a `blocked` / `escalated` row stopped. Read by the dashboard and result message. */
|
|
116
|
+
export const BLOCKED_KINDS = Object.freeze(['failed', 'timeout', 'no-ack', 'escalated']);
|
|
117
|
+
/**
|
|
118
|
+
* [1.11.0] 合法闭环迁移(T4 §4 的 19 条,M1)。
|
|
119
|
+
*
|
|
120
|
+
* 没有这张表时,「已重规划」的行可以被重新派发、「已完成」的行能被写成
|
|
121
|
+
* 「等待指示」,状态机只是装饰。`applyStatus` 在这里拒绝契约外迁移并抛
|
|
122
|
+
* `INVALID_TRANSITION`;`null`(历史日志 / 全新行)不设入边限制。
|
|
123
|
+
*
|
|
124
|
+
* 注意 `pending` 仍可作为「重置」的目标:续跑会把未完成行重新排队,这是
|
|
125
|
+
* 1.10.x 就有的既有语义,不是新增的合法化。
|
|
126
|
+
*/
|
|
127
|
+
export const CLOSURE_TRANSITIONS = Object.freeze({
|
|
128
|
+
pending: Object.freeze(['assigned', 'blocked', 'instructed', 'escalated', 'done', 'reported', 'replanned']),
|
|
129
|
+
assigned: Object.freeze(['acknowledged', 'escalated', 'blocked', 'done', 'replanned', 'pending']),
|
|
130
|
+
acknowledged: Object.freeze(['escalated', 'blocked', 'done', 'replanned', 'pending']),
|
|
131
|
+
escalated: Object.freeze(['instructed', 'blocked', 'replanned', 'pending']),
|
|
132
|
+
instructed: Object.freeze(['acknowledged', 'done', 'blocked', 'escalated', 'replanned']),
|
|
133
|
+
// 'assigned' 是既有重试路径:同一行的第 2 次尝试会重新下发(1.10.3 行为)。
|
|
134
|
+
blocked: Object.freeze(['assigned', 'instructed', 'pending', 'escalated', 'done', 'replanned']),
|
|
135
|
+
done: Object.freeze(['reported', 'replanned', 'pending', 'blocked']),
|
|
136
|
+
reported: Object.freeze(['replanned', 'pending', 'blocked']),
|
|
137
|
+
replanned: Object.freeze([]),
|
|
138
|
+
});
|
|
139
|
+
export const MAX_QUESTION_CHARS = 2000;
|
|
140
|
+
export const MAX_ESCALATION_OPTIONS = 6;
|
|
141
|
+
export const MAX_INSTRUCTION_CHARS = 4000;
|
|
142
|
+
/*
|
|
143
|
+
* [1.11.0] 跨部门协作闭环(T2 G8 / 用户 m00687)。
|
|
144
|
+
*
|
|
145
|
+
* `needs` 原先只是一段被结果消息顺带转达的文本:主 Agent 忘了处理,计划照样
|
|
146
|
+
* 可交付;下游部门拿不到上游的交接物,也没人记录「这条需求最后怎么办了」。
|
|
147
|
+
* 这里把每条需求升级成一条有生命周期的 handoff,挂在提出需求的那一行上:
|
|
148
|
+
* - open :已记录,还没指派给任何部门(交付门禁拦截);
|
|
149
|
+
* - routed :已指派给某个部门,等工作产出(交付门禁拦截);
|
|
150
|
+
* - answered :已给出书面答复或已由对应子任务交付(解除拦截);
|
|
151
|
+
* - closed :经判定无需动作(解除拦截)。
|
|
152
|
+
* 不新建存储:handoff 与 status 同表、同写入口,刷新后随状态行一起回放。
|
|
153
|
+
*/
|
|
154
|
+
export const HANDOFF_STATUSES = Object.freeze(['open', 'routed', 'answered', 'closed']);
|
|
155
|
+
/* needs 上限是 10;handoff 上限更小时,第 9/10 条会「不登记、不拦门禁」地消失。 */
|
|
156
|
+
export const MAX_HANDOFFS = MAX_EVIDENCE_NEEDS;
|
|
157
|
+
export const MAX_HANDOFF_RESPONSE_CHARS = 4000;
|
|
91
158
|
export const LIKELIHOODS = Object.freeze(['low', 'medium', 'high']);
|
|
92
159
|
export const DEPARTMENTS = Object.freeze({
|
|
93
160
|
engineering: { zh: '工程研发', en: 'Engineering' },
|
|
@@ -741,9 +808,15 @@ export function extractJson(text) {
|
|
|
741
808
|
}
|
|
742
809
|
export function departmentLabel(id, language = 'zh') {
|
|
743
810
|
const entry = DEPARTMENTS[id];
|
|
744
|
-
if (
|
|
745
|
-
return
|
|
746
|
-
|
|
811
|
+
if (entry)
|
|
812
|
+
return language === 'en' ? entry.en : entry.zh;
|
|
813
|
+
/*
|
|
814
|
+
* [1.11.0] 部门名会进入子 Agent 提示词首行(「你是「X」部门的执行 Agent」)。
|
|
815
|
+
* 历史日志或 replan 里的非法值若原样回显,等于给提示词开了一个注入口;
|
|
816
|
+
* 这里只保留可打印的短标签,未知值统一显示为「未知部门」。
|
|
817
|
+
*/
|
|
818
|
+
const raw = typeof id === 'string' ? id.replace(/[\r\n`"'<>]/g, '').trim().slice(0, 40) : '';
|
|
819
|
+
return raw === '' ? '未知部门' : raw;
|
|
747
820
|
}
|
|
748
821
|
export function buildPlannerPrompt(taskText, cwd, conversationContext = '', budget = {}) {
|
|
749
822
|
const departments = DEPARTMENT_IDS
|
|
@@ -931,6 +1004,17 @@ export function buildPlanMessage(plan, options = {}) {
|
|
|
931
1004
|
summary,
|
|
932
1005
|
...(warning ? ['', warning] : []),
|
|
933
1006
|
'',
|
|
1007
|
+
/*
|
|
1008
|
+
* [1.11.0] 计划消息必须给出唯一的下一步。
|
|
1009
|
+
*
|
|
1010
|
+
* 1.10.6 只展示计划,模型有时把「已生成计划」当成「已下发」,用户看到的
|
|
1011
|
+
* 就是一条静止的分解。现在把「不派发就不启动」写成明确指令;plan 模式
|
|
1012
|
+
* 例外,否则会和「等用户审阅」互相矛盾。
|
|
1013
|
+
*/
|
|
1014
|
+
options.planMode === true
|
|
1015
|
+
? '下一步:当前处于 plan 模式,先把计划交给用户审阅;用户退出 plan 模式后,再调用 dsh_workplan_dispatch 执行。'
|
|
1016
|
+
: '下一步:必须调用 dsh_workplan_dispatch 派发本计划;只展示计划而不派发,部门 Agent 不会启动。',
|
|
1017
|
+
'',
|
|
934
1018
|
PLAN_MARKER_BEGIN,
|
|
935
1019
|
JSON.stringify(payload),
|
|
936
1020
|
PLAN_MARKER_END,
|
|
@@ -1006,7 +1090,11 @@ export function buildResultSummary(result) {
|
|
|
1006
1090
|
*/
|
|
1007
1091
|
const statuses = Array.isArray(result.statuses) ? result.statuses : [];
|
|
1008
1092
|
if (statuses.length > 0) {
|
|
1009
|
-
const readiness = deliveryReadiness(derivePlanSnapshot(statuses), {
|
|
1093
|
+
const readiness = deliveryReadiness(derivePlanSnapshot(statuses), {
|
|
1094
|
+
phase: result.phase,
|
|
1095
|
+
changePending: result.changePending === true,
|
|
1096
|
+
reportPending: result.reportPending === true,
|
|
1097
|
+
});
|
|
1010
1098
|
if (!readiness.ready) {
|
|
1011
1099
|
const shown = readiness.blockers.slice(0, 2).join(';');
|
|
1012
1100
|
return truncate(`【部门执行结果】未完成:完成 ${done}/${total} 但证据未通过${shown ? `(${shown}${readiness.blockers.length > 2 ? '…' : ''})` : ''};证据补齐前不得交付`, 300);
|
|
@@ -1025,7 +1113,11 @@ export function buildResultMessage(result) {
|
|
|
1025
1113
|
*/
|
|
1026
1114
|
const statuses = Array.isArray(result.statuses) ? result.statuses : [];
|
|
1027
1115
|
const readiness = statuses.length > 0
|
|
1028
|
-
? deliveryReadiness(derivePlanSnapshot(statuses), {
|
|
1116
|
+
? deliveryReadiness(derivePlanSnapshot(statuses), {
|
|
1117
|
+
phase: result.phase,
|
|
1118
|
+
changePending: result.changePending === true,
|
|
1119
|
+
reportPending: result.reportPending === true,
|
|
1120
|
+
})
|
|
1029
1121
|
: null;
|
|
1030
1122
|
const deliveryBlockers = readiness?.blockers ?? [];
|
|
1031
1123
|
const payload = {
|
|
@@ -1066,10 +1158,40 @@ export function buildResultMessage(result) {
|
|
|
1066
1158
|
const unverified = statuses.filter((item) => item?.verification === 'unverified');
|
|
1067
1159
|
const failed = statuses.filter((item) => item?.verification === 'failed');
|
|
1068
1160
|
const legacy = statuses.filter((item) => item?.status === 'done' && item?.verification === 'legacy');
|
|
1161
|
+
/*
|
|
1162
|
+
* [1.11.0] 待指示项必须在结果消息里单独成行。
|
|
1163
|
+
*
|
|
1164
|
+
* 它们和普通 blocked 不一样:普通 blocked 是“做失败了”,这些是“做到一半
|
|
1165
|
+
* 停下来等人拍板”。如果只混在 blocked 计数里,主 Agent 会直接 dispatch
|
|
1166
|
+
* 续跑,而 resetBlockedStatuses 现在会跳过它们,于是一轮什么也没发生。
|
|
1167
|
+
*/
|
|
1168
|
+
const escalations = statuses.filter((item) => item?.closure === 'escalated');
|
|
1069
1169
|
const missingArtifacts = statuses.flatMap((item) => (Array.isArray(item?.missingArtifacts) ? item.missingArtifacts : [])
|
|
1070
1170
|
.map((ref) => `${item.subtaskId}:${ref}`));
|
|
1071
|
-
|
|
1171
|
+
/*
|
|
1172
|
+
* [1.11.0] 结果消息里的跨部门需求必须带可操作编号(用户 m00687)。
|
|
1173
|
+
*
|
|
1174
|
+
* 只列一段需求原文时,主 Agent 只能「知道有这回事」,无法在下一轮用
|
|
1175
|
+
* `dsh_workplan_handoff` 精确处置。优先读 handoffs(带 H 编号与状态),
|
|
1176
|
+
* 没有 handoffs 的旧记录才退回 needs 原文。
|
|
1177
|
+
*/
|
|
1178
|
+
const needs = statuses.flatMap((item) => {
|
|
1179
|
+
const handoffs = Array.isArray(item?.handoffs) ? item.handoffs : [];
|
|
1180
|
+
if (handoffs.length > 0) {
|
|
1181
|
+
return handoffs.map((handoff) => `${item.subtaskId} ${String(handoff?.id ?? 'H?')}(${String(handoff?.status ?? 'open')}):${String(handoff?.request ?? '')}`);
|
|
1182
|
+
}
|
|
1183
|
+
return (Array.isArray(item?.needs) ? item.needs : []).map((need) => `${item.subtaskId}:${need}`);
|
|
1184
|
+
});
|
|
1072
1185
|
const review = [];
|
|
1186
|
+
if (escalations.length > 0) {
|
|
1187
|
+
const detail = escalations.slice(0, 5).map((item) => {
|
|
1188
|
+
const question = typeof item?.question === 'string' && item.question.trim() !== ''
|
|
1189
|
+
? `问题:${truncate(item.question, 120)}`
|
|
1190
|
+
: '未附问题文本(按受阻处理)';
|
|
1191
|
+
return `${item.subtaskId}(${question})`;
|
|
1192
|
+
}).join(';');
|
|
1193
|
+
review.push(`- 待指示 ${escalations.length} 条:${detail}。这些行不会自动续跑:先用 dsh_workplan_update 给出指示(status=in_progress + instruction)或明确放弃(status=blocked),再处理其余项。`);
|
|
1194
|
+
}
|
|
1073
1195
|
if (unverified.length > 0) {
|
|
1074
1196
|
review.push(`- 未验证 ${unverified.length} 条:${unverified.map((item) => item.subtaskId).join('、')}(完成但证据不足,需主 Agent 核对)`);
|
|
1075
1197
|
}
|
|
@@ -1110,13 +1232,16 @@ export function buildResultMessage(result) {
|
|
|
1110
1232
|
*/
|
|
1111
1233
|
const cancelled = result.phase === 'cancelled';
|
|
1112
1234
|
const shownBlockers = deliveryBlockers.slice(0, 8).join(';');
|
|
1113
|
-
const
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1235
|
+
const escalationIds = escalations.slice(0, 8).map((item) => item.subtaskId).join('、');
|
|
1236
|
+
const recovery = escalations.length > 0
|
|
1237
|
+
? `本轮有 ${escalations.length} 条待指示项(${escalationIds}),它们不会自动续跑:请先用 dsh_workplan_update 对每条给出指示(status='in_progress' + instruction,投递成功后 closure 变为 instructed)或明确放弃(status='blocked'),处理完这些请示后再 dispatch 其余项。`
|
|
1238
|
+
: blocked > 0
|
|
1239
|
+
? (result.resumeAvailable === false
|
|
1240
|
+
? `本计划已用尽自动续跑额度(${Number(result.resumeCount ?? 0)}/${MAX_PLAN_RESUMES}),受阻项不会再自动重跑:请主 Agent 直接接手完成剩余 ${remaining} 项,完成前同样不得交付。`
|
|
1241
|
+
: `受阻项可再次调用 dsh_workplan_dispatch 从受阻处续跑(已完成的不会重跑,本计划剩余续跑额度 ${MAX_PLAN_RESUMES - Number(result.resumeCount ?? 0)} 次)。`)
|
|
1242
|
+
: paused
|
|
1243
|
+
? `本轮执行预算用尽,剩余 ${Number(result.stats?.pending ?? 0)} 项未完成(不是失败、未计入续跑额度):再次调用 dsh_workplan_dispatch 即可从剩下的步骤继续,已完成的不重跑。`
|
|
1244
|
+
: `剩余 ${remaining} 项子任务还没有完成:再次调用 dsh_workplan_dispatch 即可继续跑,已完成的不重跑。`;
|
|
1120
1245
|
const lines = [
|
|
1121
1246
|
summary,
|
|
1122
1247
|
'',
|
|
@@ -1141,7 +1266,7 @@ export function buildResultMessage(result) {
|
|
|
1141
1266
|
: ['3. 给出最终答复,不要声称未验证的完成。']),
|
|
1142
1267
|
...(review.length > 0 ? ['', '验收核对:', ...review] : []),
|
|
1143
1268
|
...(needs.length > 0
|
|
1144
|
-
? ['', '跨部门待转达(子 Agent
|
|
1269
|
+
? ['', '跨部门待转达(子 Agent 提出的需求;用 dsh_workplan_handoff 逐条 assign/resolve/close,未处置会拦住交付):', ...needs.slice(0, 10).map((line) => `- ${line}`)]
|
|
1145
1270
|
: []),
|
|
1146
1271
|
'',
|
|
1147
1272
|
RESULT_MARKER_BEGIN,
|
|
@@ -1171,7 +1296,13 @@ export function buildStopSteerText(input) {
|
|
|
1171
1296
|
const remaining = Math.max(0, total - done);
|
|
1172
1297
|
const statuses = Array.isArray(input?.statuses) ? input.statuses : [];
|
|
1173
1298
|
const snapshot = statuses.length > 0 ? derivePlanSnapshot(statuses) : null;
|
|
1174
|
-
const readiness = snapshot
|
|
1299
|
+
const readiness = snapshot
|
|
1300
|
+
? deliveryReadiness(snapshot, {
|
|
1301
|
+
phase: input?.phase,
|
|
1302
|
+
changePending: input?.changePending === true,
|
|
1303
|
+
reportPending: input?.reportPending === true,
|
|
1304
|
+
})
|
|
1305
|
+
: null;
|
|
1175
1306
|
const blockers = readiness?.blockers ?? [];
|
|
1176
1307
|
const ids = statuses
|
|
1177
1308
|
.filter((item) => item?.status !== 'done')
|
|
@@ -1401,16 +1532,59 @@ export function parseCancelMessage(message) {
|
|
|
1401
1532
|
return null;
|
|
1402
1533
|
}
|
|
1403
1534
|
}
|
|
1535
|
+
/**
|
|
1536
|
+
* [1.11.0] 验收覆盖:计划写了什么,证据就必须逐条回应什么。
|
|
1537
|
+
*
|
|
1538
|
+
* 1.10.6 只检查证据自身的 result/evidence,于是子 Agent 写一条自创的
|
|
1539
|
+
* 「自创验收项:pass」就能把 5 条计划验收全部绕开(T5 C4 / T10 D-01)。
|
|
1540
|
+
* 这里用规范化后的包含关系做匹配:允许「必须给出命令与退出码」与
|
|
1541
|
+
* 「给出命令与退出码」这类措辞差异,但不允许完全不同的一条顶替。
|
|
1542
|
+
* 短文本(< 6 字)只接受等值,避免 "ok" 命中任意单词。
|
|
1543
|
+
*/
|
|
1544
|
+
export function normaliseCriterion(value) {
|
|
1545
|
+
return String(value ?? '')
|
|
1546
|
+
.toLowerCase()
|
|
1547
|
+
.replace(/[\s,。;、,.;::!!??"'“”‘’()()\[\]【】《》<>]/g, '')
|
|
1548
|
+
.replace(/^(必须|需要|应当|应该|请|务必)/, '');
|
|
1549
|
+
}
|
|
1550
|
+
export function acceptanceCoverage(expected, actual) {
|
|
1551
|
+
const want = (Array.isArray(expected) ? expected : []).map(normaliseCriterion).filter(Boolean);
|
|
1552
|
+
const got = (Array.isArray(actual) ? actual : [])
|
|
1553
|
+
.map((item) => normaliseCriterion(item?.criterion))
|
|
1554
|
+
.filter(Boolean);
|
|
1555
|
+
const missing = [];
|
|
1556
|
+
for (const [index, criterion] of want.entries()) {
|
|
1557
|
+
const covered = got.some((row) => {
|
|
1558
|
+
if (row === criterion)
|
|
1559
|
+
return true;
|
|
1560
|
+
if (Math.min(row.length, criterion.length) < 6)
|
|
1561
|
+
return false;
|
|
1562
|
+
return row.includes(criterion) || criterion.includes(row);
|
|
1563
|
+
});
|
|
1564
|
+
if (!covered)
|
|
1565
|
+
missing.push(String((Array.isArray(expected) ? expected : [])[index] ?? criterion));
|
|
1566
|
+
}
|
|
1567
|
+
return { total: want.length, covered: want.length - missing.length, missing };
|
|
1568
|
+
}
|
|
1404
1569
|
function readEvidenceAcceptance(value) {
|
|
1405
1570
|
if (!Array.isArray(value))
|
|
1406
1571
|
return [];
|
|
1407
1572
|
return value.slice(0, MAX_EVIDENCE_ACCEPTANCE).map((item) => {
|
|
1408
1573
|
const source = isRecord(item) ? item : {};
|
|
1409
|
-
const
|
|
1574
|
+
const stated = ACCEPTANCE_RESULTS.includes(source.result) ? source.result : 'unverified';
|
|
1575
|
+
const evidence = truncate(String(source.evidence ?? ''), 300);
|
|
1576
|
+
/*
|
|
1577
|
+
* [1.11.0] C-19:`pass` 必须有证据。
|
|
1578
|
+
*
|
|
1579
|
+
* 「验收:pass,证据:空」是自述而不是验证。1.10.6 把它当通过,于是
|
|
1580
|
+
* 子 Agent 只要写一排 pass 就能让验收表全绿。没有证据的 pass 降级为
|
|
1581
|
+
* `unverified`,交付门禁据此继续拦,而不是被一行空字符串骗过。
|
|
1582
|
+
*/
|
|
1583
|
+
const result = stated === 'pass' && evidence.trim() === '' ? 'unverified' : stated;
|
|
1410
1584
|
return {
|
|
1411
1585
|
criterion: truncate(String(source.criterion ?? ''), 500),
|
|
1412
1586
|
result,
|
|
1413
|
-
evidence
|
|
1587
|
+
evidence,
|
|
1414
1588
|
};
|
|
1415
1589
|
}).filter((item) => item.criterion !== '');
|
|
1416
1590
|
}
|
|
@@ -1433,6 +1607,92 @@ function readEvidenceNeeds(value) {
|
|
|
1433
1607
|
.map((item) => truncate(typeof item === 'string' ? item : String(item ?? ''), 300))
|
|
1434
1608
|
.filter(Boolean);
|
|
1435
1609
|
}
|
|
1610
|
+
/**
|
|
1611
|
+
* [1.11.0] 跨部门需求的唯一规范化入口(用户 m00687)。
|
|
1612
|
+
*
|
|
1613
|
+
* 去重键是 `request` 文本:同一条需求可能出现在结果块、人工改判、结果消息
|
|
1614
|
+
* 回放三条路径上,重复登记会让交付门禁数出 3 条未落实。状态、部门、答复
|
|
1615
|
+
* 都由服务端校验,客户端与模型只能提供文本。
|
|
1616
|
+
*/
|
|
1617
|
+
export function normaliseHandoffs(value) {
|
|
1618
|
+
const list = Array.isArray(value) ? value : [];
|
|
1619
|
+
const seen = new Set();
|
|
1620
|
+
const out = [];
|
|
1621
|
+
for (const raw of list.slice(0, MAX_HANDOFFS)) {
|
|
1622
|
+
if (!isRecord(raw))
|
|
1623
|
+
continue;
|
|
1624
|
+
const request = truncate(String(raw.request ?? ''), 300).trim();
|
|
1625
|
+
if (!request || seen.has(request))
|
|
1626
|
+
continue;
|
|
1627
|
+
seen.add(request);
|
|
1628
|
+
const status = HANDOFF_STATUSES.includes(raw.status) ? raw.status : 'open';
|
|
1629
|
+
const department = typeof raw.department === 'string' && DEPARTMENTS[raw.department]
|
|
1630
|
+
? raw.department
|
|
1631
|
+
: null;
|
|
1632
|
+
const response = truncate(String(raw.response ?? ''), MAX_HANDOFF_RESPONSE_CHARS).trim() || null;
|
|
1633
|
+
out.push({
|
|
1634
|
+
id: truncate(String(raw.id ?? ''), 16).trim() || `H${out.length + 1}`,
|
|
1635
|
+
request,
|
|
1636
|
+
department,
|
|
1637
|
+
status,
|
|
1638
|
+
response,
|
|
1639
|
+
at: numberOrNull(raw.at) ?? Date.now(),
|
|
1640
|
+
updatedAt: numberOrNull(raw.updatedAt) ?? numberOrNull(raw.at) ?? Date.now(),
|
|
1641
|
+
by: ['child', 'agent', 'system'].includes(raw.by) ? raw.by : 'system',
|
|
1642
|
+
});
|
|
1643
|
+
}
|
|
1644
|
+
return out;
|
|
1645
|
+
}
|
|
1646
|
+
/**
|
|
1647
|
+
* 每一条 `needs` 都必须变成一条可追踪的 handoff。
|
|
1648
|
+
*
|
|
1649
|
+
* 已经登记过的需求(无论最后是 answered 还是 closed)保留原状态,不会因为
|
|
1650
|
+
* 结果消息再次回放而退回 open;这是「处理过的事情不能被重放抹掉」的最基本
|
|
1651
|
+
* 要求,也是交付门禁能给出稳定结论的前提。
|
|
1652
|
+
*/
|
|
1653
|
+
export function mergeHandoffsFromNeeds(current, needs) {
|
|
1654
|
+
const next = normaliseHandoffs(current);
|
|
1655
|
+
const list = Array.isArray(needs) ? needs : [];
|
|
1656
|
+
for (const raw of list) {
|
|
1657
|
+
const request = truncate(String(raw ?? ''), 300).trim();
|
|
1658
|
+
if (!request)
|
|
1659
|
+
continue;
|
|
1660
|
+
if (next.some((item) => item.request === request))
|
|
1661
|
+
continue;
|
|
1662
|
+
if (next.length >= MAX_HANDOFFS)
|
|
1663
|
+
break;
|
|
1664
|
+
next.push({
|
|
1665
|
+
id: `H${next.length + 1}`,
|
|
1666
|
+
request,
|
|
1667
|
+
department: null,
|
|
1668
|
+
status: 'open',
|
|
1669
|
+
response: null,
|
|
1670
|
+
at: Date.now(),
|
|
1671
|
+
updatedAt: Date.now(),
|
|
1672
|
+
by: 'child',
|
|
1673
|
+
});
|
|
1674
|
+
}
|
|
1675
|
+
return next;
|
|
1676
|
+
}
|
|
1677
|
+
/** 还挡着交付的跨部门需求(open/routed),answered/closed 视为已处理。 */
|
|
1678
|
+
export function openHandoffs(status) {
|
|
1679
|
+
const list = Array.isArray(status?.handoffs) ? status.handoffs : [];
|
|
1680
|
+
return list.filter((item) => item?.status === 'open' || item?.status === 'routed');
|
|
1681
|
+
}
|
|
1682
|
+
/**
|
|
1683
|
+
* [1.11.0] Candidate decisions attached to an escalation.
|
|
1684
|
+
*
|
|
1685
|
+
* The main Agent is asked to decide, not to guess. Options are advisory text,
|
|
1686
|
+
* never auto-applied: an option the child invented must not become an
|
|
1687
|
+
* instruction just because it was listed first.
|
|
1688
|
+
*/
|
|
1689
|
+
function readEvidenceOptions(value) {
|
|
1690
|
+
if (!Array.isArray(value))
|
|
1691
|
+
return [];
|
|
1692
|
+
return value.slice(0, MAX_ESCALATION_OPTIONS)
|
|
1693
|
+
.map((item) => truncate(typeof item === 'string' ? item : String(item ?? ''), 300))
|
|
1694
|
+
.filter(Boolean);
|
|
1695
|
+
}
|
|
1436
1696
|
/**
|
|
1437
1697
|
* Parse the child's structured evidence block.
|
|
1438
1698
|
*
|
|
@@ -1470,7 +1730,7 @@ const BLOCKED_LINE = /^\s*(BLOCKED|受阻|无法完成)\s*[::]\s*/i;
|
|
|
1470
1730
|
* the acceptance table cannot drift apart between the runtime and the replay.
|
|
1471
1731
|
* It never invents evidence and never upgrades a failed self-check to done.
|
|
1472
1732
|
*/
|
|
1473
|
-
export function adjudicateResult({ output, strict = true }) {
|
|
1733
|
+
export function adjudicateResult({ output, strict = true, expectedAcceptance = [] }) {
|
|
1474
1734
|
const text = typeof output === 'string' ? output : '';
|
|
1475
1735
|
const parsed = parseEvidenceBlock(text);
|
|
1476
1736
|
if (!strict) {
|
|
@@ -1507,12 +1767,40 @@ export function adjudicateResult({ output, strict = true }) {
|
|
|
1507
1767
|
error: text.replace(BLOCKED_LINE, '').trim() || '子 Agent 报告受阻',
|
|
1508
1768
|
};
|
|
1509
1769
|
}
|
|
1510
|
-
|
|
1770
|
+
/*
|
|
1771
|
+
* [1.11.0] 沉默不是成功(C-05 / M3)。
|
|
1772
|
+
*
|
|
1773
|
+
* 1.10.6 在这里返回 `done/legacy`:子 Agent 只回一段散文、没有证据块、
|
|
1774
|
+
* 也没写 BLOCKED,就被记成「已完成」。进度栏先显示完成,交付门禁再事后
|
|
1775
|
+
* 拦一次,用户看到的是「明明做完了却交付不了」。完成必须有结构化回报,
|
|
1776
|
+
* 没有回报只能算未完成,并明确写出缺什么。
|
|
1777
|
+
*
|
|
1778
|
+
* `strict:false` 保留旧行为,用于回放 1.11.0 之前的会话日志。
|
|
1779
|
+
*/
|
|
1780
|
+
return {
|
|
1781
|
+
status: 'blocked',
|
|
1782
|
+
verification: 'unverified',
|
|
1783
|
+
acceptance: [],
|
|
1784
|
+
artifacts: [],
|
|
1785
|
+
needs: [],
|
|
1786
|
+
error: '子 Agent 未提供结构化证据块(完成必回报):结果块缺失,无法确认验收项是否真的通过',
|
|
1787
|
+
};
|
|
1511
1788
|
}
|
|
1512
1789
|
const payload = parsed.payload;
|
|
1513
1790
|
const acceptance = readEvidenceAcceptance(payload.acceptance);
|
|
1514
1791
|
const artifacts = readEvidenceArtifacts(payload.artifacts);
|
|
1515
1792
|
const needs = readEvidenceNeeds(payload.needs);
|
|
1793
|
+
/*
|
|
1794
|
+
* [1.11.0] A blocked result may carry the question it is waiting on.
|
|
1795
|
+
*
|
|
1796
|
+
* When the escalation tool is unavailable (a child session running an older
|
|
1797
|
+
* tool table), the prompt's text fallback writes `blockedReason` +
|
|
1798
|
+
* `question` + `options` into the same evidence block. Surfacing them here
|
|
1799
|
+
* keeps one parser for both paths; without this the runtime could only say
|
|
1800
|
+
* "blocked" and the pending decision was lost.
|
|
1801
|
+
*/
|
|
1802
|
+
const question = truncate(String(payload.question ?? ''), MAX_QUESTION_CHARS);
|
|
1803
|
+
const options = readEvidenceOptions(payload.options);
|
|
1516
1804
|
const blockedReason = truncate(String(payload.blockedReason ?? payload.error ?? ''), 2000);
|
|
1517
1805
|
if (payload.status === 'blocked') {
|
|
1518
1806
|
return {
|
|
@@ -1521,6 +1809,8 @@ export function adjudicateResult({ output, strict = true }) {
|
|
|
1521
1809
|
acceptance,
|
|
1522
1810
|
artifacts,
|
|
1523
1811
|
needs,
|
|
1812
|
+
question,
|
|
1813
|
+
options,
|
|
1524
1814
|
error: blockedReason || '子 Agent 报告受阻',
|
|
1525
1815
|
};
|
|
1526
1816
|
}
|
|
@@ -1532,6 +1822,8 @@ export function adjudicateResult({ output, strict = true }) {
|
|
|
1532
1822
|
acceptance,
|
|
1533
1823
|
artifacts,
|
|
1534
1824
|
needs,
|
|
1825
|
+
question,
|
|
1826
|
+
options,
|
|
1535
1827
|
error: detail ? `部分完成:${detail}` : '部分完成',
|
|
1536
1828
|
};
|
|
1537
1829
|
}
|
|
@@ -1543,6 +1835,8 @@ export function adjudicateResult({ output, strict = true }) {
|
|
|
1543
1835
|
acceptance,
|
|
1544
1836
|
artifacts,
|
|
1545
1837
|
needs,
|
|
1838
|
+
question,
|
|
1839
|
+
options,
|
|
1546
1840
|
error: `自检未通过:${failed.map((item) => item.criterion).join(';')}`,
|
|
1547
1841
|
};
|
|
1548
1842
|
}
|
|
@@ -1559,6 +1853,24 @@ export function adjudicateResult({ output, strict = true }) {
|
|
|
1559
1853
|
*/
|
|
1560
1854
|
const hasUnverified = acceptance.length === 0
|
|
1561
1855
|
|| acceptance.some((item) => item.result === 'unverified');
|
|
1856
|
+
/*
|
|
1857
|
+
* [1.11.0] 计划验收必须被逐条回应(T5 C4 / T10 D-01,阻断级)。
|
|
1858
|
+
*
|
|
1859
|
+
* 只有证据不完整时才算缺口:全部 pass 但少回一条,仍然是「没做完」。
|
|
1860
|
+
* 旧计划/旧日志没有 expectedAcceptance 时保持原语义,避免历史回放翻红。
|
|
1861
|
+
*/
|
|
1862
|
+
const coverage = acceptanceCoverage(expectedAcceptance, acceptance);
|
|
1863
|
+
if (coverage.total > 0 && coverage.missing.length > 0) {
|
|
1864
|
+
return {
|
|
1865
|
+
status: 'blocked',
|
|
1866
|
+
verification: 'unverified',
|
|
1867
|
+
acceptance,
|
|
1868
|
+
artifacts,
|
|
1869
|
+
needs,
|
|
1870
|
+
error: `验收项未被逐条回应(${coverage.covered}/${coverage.total}):缺少 ${coverage.missing
|
|
1871
|
+
.map((item) => truncate(String(item), 120)).slice(0, 5).join(';')}。请按计划验收逐条给出证据后再回报。`,
|
|
1872
|
+
};
|
|
1873
|
+
}
|
|
1562
1874
|
return {
|
|
1563
1875
|
status: 'done',
|
|
1564
1876
|
verification: hasUnverified ? 'unverified' : 'structured',
|
|
@@ -1591,6 +1903,34 @@ export function buildUpdateMessage(update) {
|
|
|
1591
1903
|
...(update.acceptance === undefined ? {} : { acceptance: update.acceptance }),
|
|
1592
1904
|
...(update.artifacts === undefined ? {} : { artifacts: update.artifacts }),
|
|
1593
1905
|
...(update.missingArtifacts === undefined ? {} : { missingArtifacts: update.missingArtifacts }),
|
|
1906
|
+
/*
|
|
1907
|
+
* [1.11.0] 闭环字段随人工改判一起留档。
|
|
1908
|
+
*
|
|
1909
|
+
* `escalate` 工具在子会话里立刻写一条这样的记录,所以即使页面在回合结束
|
|
1910
|
+
* 前刷新,「有一条请示在等指示」也不会退回 pending。旧版本的日志没有这些
|
|
1911
|
+
* 键,回放时按 undefined 跳过,不改变 1.10.6 的语义。
|
|
1912
|
+
*/
|
|
1913
|
+
...(update.closure === undefined ? {} : { closure: update.closure }),
|
|
1914
|
+
...(update.blockedKind === undefined ? {} : { blockedKind: update.blockedKind }),
|
|
1915
|
+
...(update.question === undefined ? {} : { question: update.question }),
|
|
1916
|
+
...(update.options === undefined ? {} : { options: update.options }),
|
|
1917
|
+
...(update.assignedAt === undefined ? {} : { assignedAt: update.assignedAt }),
|
|
1918
|
+
...(update.ackedAt === undefined ? {} : { ackedAt: update.ackedAt }),
|
|
1919
|
+
...(update.ackEvidence === undefined ? {} : { ackEvidence: update.ackEvidence }),
|
|
1920
|
+
...(update.ackOverdue === undefined ? {} : { ackOverdue: update.ackOverdue }),
|
|
1921
|
+
...(update.escalatedAt === undefined ? {} : { escalatedAt: update.escalatedAt }),
|
|
1922
|
+
...(update.escalationSeq === undefined ? {} : { escalationSeq: update.escalationSeq }),
|
|
1923
|
+
...(update.instruction === undefined ? {} : { instruction: update.instruction }),
|
|
1924
|
+
...(update.instructionSeq === undefined ? {} : { instructionSeq: update.instructionSeq }),
|
|
1925
|
+
/*
|
|
1926
|
+
* [1.11.0] 指示回执提醒也要能回放(T10 N4)。
|
|
1927
|
+
*
|
|
1928
|
+
* 否则刷新页面后「指示已发出、子会话还没动」这条事实只剩内存里的一份,
|
|
1929
|
+
* 记录重建时被静默抹掉,主 Agent 会以为指示已经被执行。
|
|
1930
|
+
*/
|
|
1931
|
+
...(update.instructionOverdue === undefined ? {} : { instructionOverdue: update.instructionOverdue }),
|
|
1932
|
+
...(update.handoffs === undefined ? {} : { handoffs: update.handoffs }),
|
|
1933
|
+
...(update.reportedAt === undefined ? {} : { reportedAt: update.reportedAt }),
|
|
1594
1934
|
...(update.revision === undefined ? {} : { revision: update.revision }),
|
|
1595
1935
|
at: update.at ?? Date.now(),
|
|
1596
1936
|
};
|
|
@@ -1678,6 +2018,19 @@ export function applyResultStatuses(statuses, result) {
|
|
|
1678
2018
|
if (Number.isInteger(entry.revision) && Number.isInteger(current.revision)
|
|
1679
2019
|
&& Number(entry.revision) <= Number(current.revision))
|
|
1680
2020
|
continue;
|
|
2021
|
+
/*
|
|
2022
|
+
* [1.11.0] 回放一条结果消息 = 「这条完成结果已经回报过」。
|
|
2023
|
+
*
|
|
2024
|
+
* 1.10.6 的结果载荷没有 `closure`。若原样交给 `applyStatus`,它的 done
|
|
2025
|
+
* 分支会把闭包补成 `done`(= 完成但未回报),而 `deliveryReadiness` 的
|
|
2026
|
+
* reportPending 拦截就会把**每一份历史计划**判成不可交付 —— 升级后用户
|
|
2027
|
+
* 手里所有旧会话都会突然“差一步回报”。结果消息本身就是回报的证据,
|
|
2028
|
+
* 所以旧载荷的 done 行按 `reported` 解释;新载荷已经显式带 `reported`
|
|
2029
|
+
* (finishRound 先 markReported 再构消息),不受影响。
|
|
2030
|
+
*/
|
|
2031
|
+
const replayedClosure = entry.status === 'done' && entry.closure === undefined
|
|
2032
|
+
? 'reported'
|
|
2033
|
+
: entry.closure;
|
|
1681
2034
|
try {
|
|
1682
2035
|
next = applyStatus(next, entry.subtaskId, {
|
|
1683
2036
|
status: entry.status,
|
|
@@ -1692,7 +2045,31 @@ export function applyResultStatuses(statuses, result) {
|
|
|
1692
2045
|
artifacts: entry.artifacts,
|
|
1693
2046
|
missingArtifacts: entry.missingArtifacts,
|
|
1694
2047
|
needs: entry.needs,
|
|
2048
|
+
handoffs: entry.handoffs,
|
|
1695
2049
|
blockedBy: entry.blockedBy,
|
|
2050
|
+
closure: replayedClosure,
|
|
2051
|
+
blockedKind: entry.blockedKind,
|
|
2052
|
+
question: entry.question,
|
|
2053
|
+
options: entry.options,
|
|
2054
|
+
assignedAt: entry.assignedAt,
|
|
2055
|
+
ackedAt: entry.ackedAt,
|
|
2056
|
+
ackEvidence: entry.ackEvidence,
|
|
2057
|
+
ackOverdue: entry.ackOverdue,
|
|
2058
|
+
escalatedAt: entry.escalatedAt,
|
|
2059
|
+
escalationSeq: entry.escalationSeq,
|
|
2060
|
+
instruction: entry.instruction,
|
|
2061
|
+
instructionSeq: entry.instructionSeq,
|
|
2062
|
+
instructionOverdue: entry.instructionOverdue,
|
|
2063
|
+
reportedAt: entry.reportedAt,
|
|
2064
|
+
// [1.11.0] 回报证据与重规划留档必须随结果快照回放(T7 C-06/C-11)。
|
|
2065
|
+
report: entry.report,
|
|
2066
|
+
replannedReason: entry.replannedReason,
|
|
2067
|
+
replannedAt: entry.replannedAt,
|
|
2068
|
+
replacedBy: entry.replacedBy,
|
|
2069
|
+
replannedFrom: entry.replannedFrom,
|
|
2070
|
+
replannedFromClosure: entry.replannedFromClosure,
|
|
2071
|
+
insertedBy: entry.insertedBy,
|
|
2072
|
+
replaces: entry.replaces,
|
|
1696
2073
|
revision: entry.revision,
|
|
1697
2074
|
updatedBy: entry.updatedBy,
|
|
1698
2075
|
});
|
|
@@ -1711,6 +2088,7 @@ export function createStatuses(plan) {
|
|
|
1711
2088
|
department: subtask.department,
|
|
1712
2089
|
role: subtask.role,
|
|
1713
2090
|
status: 'pending',
|
|
2091
|
+
closure: 'pending',
|
|
1714
2092
|
progress: 0,
|
|
1715
2093
|
childId: null,
|
|
1716
2094
|
startedAt: null,
|
|
@@ -1731,6 +2109,144 @@ export function applyStatus(statuses, subtaskId, patch) {
|
|
|
1731
2109
|
}
|
|
1732
2110
|
next.status = patch.status;
|
|
1733
2111
|
}
|
|
2112
|
+
/*
|
|
2113
|
+
* [1.11.0] 闭环字段与 status 同表、同写入口(T4 D1/D2)。
|
|
2114
|
+
*
|
|
2115
|
+
* 校验放在这里而不是各个调用点:escalate 工具、看门狗、结果回放、页面
|
|
2116
|
+
* 刷新后的重建都走 applyStatus,任何一条路径都无法绕过枚举检查,也不会
|
|
2117
|
+
* 出现「status 说完成、closure 说还在等指示」的双份真相。
|
|
2118
|
+
*/
|
|
2119
|
+
if (patch.closure !== undefined) {
|
|
2120
|
+
if (patch.closure === null)
|
|
2121
|
+
next.closure = null;
|
|
2122
|
+
else if (!CLOSURES.includes(patch.closure)) {
|
|
2123
|
+
throw new PlanError(`非法 closure:${String(patch.closure)}`, 'INVALID_CLOSURE');
|
|
2124
|
+
}
|
|
2125
|
+
else
|
|
2126
|
+
next.closure = patch.closure;
|
|
2127
|
+
}
|
|
2128
|
+
if (patch.blockedKind !== undefined) {
|
|
2129
|
+
if (patch.blockedKind === null)
|
|
2130
|
+
next.blockedKind = null;
|
|
2131
|
+
else if (!BLOCKED_KINDS.includes(patch.blockedKind)) {
|
|
2132
|
+
throw new PlanError(`非法 blockedKind:${String(patch.blockedKind)}`, 'INVALID_BLOCKED_KIND');
|
|
2133
|
+
}
|
|
2134
|
+
else
|
|
2135
|
+
next.blockedKind = patch.blockedKind;
|
|
2136
|
+
}
|
|
2137
|
+
if (patch.question !== undefined) {
|
|
2138
|
+
const text = truncate(String(patch.question ?? ''), MAX_QUESTION_CHARS);
|
|
2139
|
+
next.question = text || null;
|
|
2140
|
+
}
|
|
2141
|
+
if (patch.options !== undefined)
|
|
2142
|
+
next.options = readEvidenceOptions(patch.options);
|
|
2143
|
+
if (patch.assignedAt !== undefined)
|
|
2144
|
+
next.assignedAt = numberOrNull(patch.assignedAt);
|
|
2145
|
+
if (patch.ackedAt !== undefined)
|
|
2146
|
+
next.ackedAt = numberOrNull(patch.ackedAt);
|
|
2147
|
+
if (patch.ackEvidence !== undefined) {
|
|
2148
|
+
next.ackEvidence = isRecord(patch.ackEvidence)
|
|
2149
|
+
? {
|
|
2150
|
+
seq: Number.isInteger(Number(patch.ackEvidence.seq)) ? Number(patch.ackEvidence.seq) : null,
|
|
2151
|
+
elapsedMs: Number.isFinite(Number(patch.ackEvidence.elapsedMs))
|
|
2152
|
+
? Math.max(0, Number(patch.ackEvidence.elapsedMs))
|
|
2153
|
+
: null,
|
|
2154
|
+
}
|
|
2155
|
+
: undefined;
|
|
2156
|
+
}
|
|
2157
|
+
if (patch.ackOverdue !== undefined)
|
|
2158
|
+
next.ackOverdue = patch.ackOverdue === true;
|
|
2159
|
+
/*
|
|
2160
|
+
* [1.11.0] 指示回执状态也必须能在同一条状态条目上落盘(T10 N4)。
|
|
2161
|
+
*
|
|
2162
|
+
* `scheduleInstructionAck` 在 60s 写 `instructionOverdue:true`,但这里原先
|
|
2163
|
+
* 没有对应分支,字段被静默丢弃:界面看不到「指示已发出、子会话还没动」,
|
|
2164
|
+
* 主 Agent 也无从判断该催还是该重发。它和 `ackOverdue` 是同一类事实,
|
|
2165
|
+
* 只是方向相反(主 → 子)。
|
|
2166
|
+
*/
|
|
2167
|
+
if (patch.instructionOverdue !== undefined)
|
|
2168
|
+
next.instructionOverdue = patch.instructionOverdue === true;
|
|
2169
|
+
if (patch.escalatedAt !== undefined)
|
|
2170
|
+
next.escalatedAt = numberOrNull(patch.escalatedAt);
|
|
2171
|
+
if (patch.escalationSeq !== undefined) {
|
|
2172
|
+
next.escalationSeq = Number.isInteger(Number(patch.escalationSeq)) ? Number(patch.escalationSeq) : null;
|
|
2173
|
+
}
|
|
2174
|
+
if (patch.instruction !== undefined) {
|
|
2175
|
+
if (patch.instruction === null)
|
|
2176
|
+
next.instruction = null;
|
|
2177
|
+
else if (isRecord(patch.instruction)) {
|
|
2178
|
+
const text = truncate(String(patch.instruction.text ?? ''), MAX_INSTRUCTION_CHARS);
|
|
2179
|
+
if (!text)
|
|
2180
|
+
throw new PlanError('instruction.text 不能为空', 'INVALID_INSTRUCTION');
|
|
2181
|
+
const delivery = ['sent', 'failed', 'not-sent'].includes(patch.instruction.delivery)
|
|
2182
|
+
? patch.instruction.delivery
|
|
2183
|
+
: 'not-sent';
|
|
2184
|
+
next.instruction = {
|
|
2185
|
+
text,
|
|
2186
|
+
at: numberOrNull(patch.instruction.at) ?? Date.now(),
|
|
2187
|
+
by: ['agent', 'system', 'child'].includes(patch.instruction.by) ? patch.instruction.by : 'agent',
|
|
2188
|
+
delivery,
|
|
2189
|
+
...(typeof patch.instruction.messageId === 'string'
|
|
2190
|
+
? { messageId: truncate(patch.instruction.messageId, 200) }
|
|
2191
|
+
: {}),
|
|
2192
|
+
};
|
|
2193
|
+
}
|
|
2194
|
+
else {
|
|
2195
|
+
throw new PlanError('instruction 必须是对象或 null', 'INVALID_INSTRUCTION');
|
|
2196
|
+
}
|
|
2197
|
+
}
|
|
2198
|
+
if (patch.instructionSeq !== undefined) {
|
|
2199
|
+
next.instructionSeq = Number.isInteger(Number(patch.instructionSeq)) ? Number(patch.instructionSeq) : null;
|
|
2200
|
+
}
|
|
2201
|
+
if (patch.reportedAt !== undefined)
|
|
2202
|
+
next.reportedAt = numberOrNull(patch.reportedAt);
|
|
2203
|
+
/*
|
|
2204
|
+
* [1.11.0] 完成回报与重规划的留档字段(T7 C-06/C-11)。
|
|
2205
|
+
*
|
|
2206
|
+
* `report` 是「这条完成结果回报给主 Agent 了吗」的唯一证据,`ref` 指向本轮
|
|
2207
|
+
* `workplan-result` 消息的稳定标识;`replanned*` 让旧行在刷新后仍能显示
|
|
2208
|
+
* 「被哪次修订替换、替换前是什么状态」。两者都只写同一条状态条目,
|
|
2209
|
+
* 不新增存储。
|
|
2210
|
+
*/
|
|
2211
|
+
if (patch.report !== undefined) {
|
|
2212
|
+
if (patch.report === null)
|
|
2213
|
+
next.report = null;
|
|
2214
|
+
else if (isRecord(patch.report)) {
|
|
2215
|
+
const state = ['sent', 'failed', 'pending'].includes(patch.report.state)
|
|
2216
|
+
? patch.report.state
|
|
2217
|
+
: 'sent';
|
|
2218
|
+
next.report = {
|
|
2219
|
+
state,
|
|
2220
|
+
at: numberOrNull(patch.report.at),
|
|
2221
|
+
ref: typeof patch.report.ref === 'string' ? truncate(patch.report.ref, 300) : null,
|
|
2222
|
+
turn: Number.isInteger(Number(patch.report.turn)) ? Number(patch.report.turn) : null,
|
|
2223
|
+
};
|
|
2224
|
+
}
|
|
2225
|
+
else {
|
|
2226
|
+
throw new PlanError('report 必须是对象或 null', 'INVALID_REPORT');
|
|
2227
|
+
}
|
|
2228
|
+
}
|
|
2229
|
+
if (patch.replannedReason !== undefined) {
|
|
2230
|
+
next.replannedReason = truncate(String(patch.replannedReason ?? ''), 2000) || null;
|
|
2231
|
+
}
|
|
2232
|
+
if (patch.replannedAt !== undefined)
|
|
2233
|
+
next.replannedAt = numberOrNull(patch.replannedAt);
|
|
2234
|
+
if (patch.replacedBy !== undefined) {
|
|
2235
|
+
next.replacedBy = (Array.isArray(patch.replacedBy) ? patch.replacedBy : [])
|
|
2236
|
+
.slice(0, 8)
|
|
2237
|
+
.map((id) => truncate(String(id ?? ''), 16))
|
|
2238
|
+
.filter(Boolean);
|
|
2239
|
+
}
|
|
2240
|
+
if (patch.replannedFrom !== undefined) {
|
|
2241
|
+
next.replannedFrom = typeof patch.replannedFrom === 'string' ? patch.replannedFrom : null;
|
|
2242
|
+
}
|
|
2243
|
+
if (patch.replannedFromClosure !== undefined) {
|
|
2244
|
+
next.replannedFromClosure = typeof patch.replannedFromClosure === 'string' ? patch.replannedFromClosure : null;
|
|
2245
|
+
}
|
|
2246
|
+
if (patch.insertedBy !== undefined)
|
|
2247
|
+
next.insertedBy = truncate(String(patch.insertedBy), 40) || null;
|
|
2248
|
+
if (patch.replaces !== undefined)
|
|
2249
|
+
next.replaces = truncate(String(patch.replaces), 16) || null;
|
|
1734
2250
|
if (patch.progress !== undefined) {
|
|
1735
2251
|
if (!Number.isInteger(patch.progress) || patch.progress < 0 || patch.progress > 100) {
|
|
1736
2252
|
throw new PlanError('progress 必须是 0-100 的整数', 'INVALID_PROGRESS');
|
|
@@ -1780,8 +2296,19 @@ export function applyStatus(statuses, subtaskId, patch) {
|
|
|
1780
2296
|
return true;
|
|
1781
2297
|
});
|
|
1782
2298
|
}
|
|
1783
|
-
if (patch.needs !== undefined)
|
|
2299
|
+
if (patch.needs !== undefined) {
|
|
1784
2300
|
next.needs = readEvidenceNeeds(patch.needs);
|
|
2301
|
+
/*
|
|
2302
|
+
* [1.11.0] 需求登记是自动的,处理不是(用户 m00687)。
|
|
2303
|
+
*
|
|
2304
|
+
* 任何一条 needs 写进来都立刻得到一条 open handoff,模型无法通过「只在
|
|
2305
|
+
* 文本里提一句」绕过交付门禁;但把它变成 answered/closed 必须走主 Agent
|
|
2306
|
+
* 的显式工具调用,子 Agent 自己不能宣称「跨部门需求已解决」。
|
|
2307
|
+
*/
|
|
2308
|
+
next.handoffs = mergeHandoffsFromNeeds(next.handoffs, next.needs);
|
|
2309
|
+
}
|
|
2310
|
+
if (patch.handoffs !== undefined)
|
|
2311
|
+
next.handoffs = normaliseHandoffs(patch.handoffs);
|
|
1785
2312
|
/*
|
|
1786
2313
|
* [1.10.6] The files a retry refused to touch.
|
|
1787
2314
|
*
|
|
@@ -1814,6 +2341,21 @@ export function applyStatus(statuses, subtaskId, patch) {
|
|
|
1814
2341
|
next.updatedBy = ['child', 'agent', 'system'].includes(patch.updatedBy) ? patch.updatedBy : 'system';
|
|
1815
2342
|
if (patch.status === 'done') {
|
|
1816
2343
|
next.progress = 100;
|
|
2344
|
+
if (patch.closure === undefined)
|
|
2345
|
+
next.closure = 'done';
|
|
2346
|
+
/*
|
|
2347
|
+
* 一条已经完成的子任务不可能同时“在等指示”。旧的 question/options 是
|
|
2348
|
+
* 上一段受阻经历的证据,不能继续驱动模型把它当成未决请示;失败原因本身
|
|
2349
|
+
* 仍按既有规则迁入 lastError。
|
|
2350
|
+
*/
|
|
2351
|
+
if (patch.blockedKind === undefined)
|
|
2352
|
+
next.blockedKind = null;
|
|
2353
|
+
if (patch.question === undefined)
|
|
2354
|
+
next.question = null;
|
|
2355
|
+
if (patch.options === undefined)
|
|
2356
|
+
next.options = [];
|
|
2357
|
+
if (patch.ackOverdue === undefined)
|
|
2358
|
+
next.ackOverdue = false;
|
|
1817
2359
|
if (next.finishedAt === null)
|
|
1818
2360
|
next.finishedAt = Date.now();
|
|
1819
2361
|
/*
|
|
@@ -1871,6 +2413,37 @@ export function applyStatus(statuses, subtaskId, patch) {
|
|
|
1871
2413
|
next.startedAt = null;
|
|
1872
2414
|
next.finishedAt = null;
|
|
1873
2415
|
next.childId = null;
|
|
2416
|
+
/*
|
|
2417
|
+
* [1.11.0] A reset starts a new attempt; every closure-companion field of
|
|
2418
|
+
* the previous attempt goes with it, otherwise a re-queued row would still
|
|
2419
|
+
* advertise a stale ack deadline or a question nobody is waiting on.
|
|
2420
|
+
*/
|
|
2421
|
+
if (patch.closure === undefined)
|
|
2422
|
+
next.closure = 'pending';
|
|
2423
|
+
if (patch.assignedAt === undefined)
|
|
2424
|
+
next.assignedAt = null;
|
|
2425
|
+
if (patch.ackedAt === undefined)
|
|
2426
|
+
next.ackedAt = null;
|
|
2427
|
+
if (patch.ackEvidence === undefined)
|
|
2428
|
+
next.ackEvidence = undefined;
|
|
2429
|
+
if (patch.ackOverdue === undefined)
|
|
2430
|
+
next.ackOverdue = false;
|
|
2431
|
+
if (patch.escalatedAt === undefined)
|
|
2432
|
+
next.escalatedAt = null;
|
|
2433
|
+
if (patch.escalationSeq === undefined)
|
|
2434
|
+
next.escalationSeq = null;
|
|
2435
|
+
if (patch.question === undefined)
|
|
2436
|
+
next.question = null;
|
|
2437
|
+
if (patch.options === undefined)
|
|
2438
|
+
next.options = [];
|
|
2439
|
+
if (patch.blockedKind === undefined)
|
|
2440
|
+
next.blockedKind = null;
|
|
2441
|
+
if (patch.instruction === undefined)
|
|
2442
|
+
next.instruction = null;
|
|
2443
|
+
if (patch.instructionSeq === undefined)
|
|
2444
|
+
next.instructionSeq = null;
|
|
2445
|
+
if (patch.reportedAt === undefined)
|
|
2446
|
+
next.reportedAt = null;
|
|
1874
2447
|
if (patch.summary === undefined)
|
|
1875
2448
|
next.summary = '';
|
|
1876
2449
|
if (patch.error === undefined)
|
|
@@ -1908,8 +2481,45 @@ export function applyStatus(statuses, subtaskId, patch) {
|
|
|
1908
2481
|
if (Number(next.progress) >= 100)
|
|
1909
2482
|
next.progress = 99;
|
|
1910
2483
|
}
|
|
1911
|
-
if (patch.status === 'blocked'
|
|
1912
|
-
|
|
2484
|
+
if (patch.status === 'blocked') {
|
|
2485
|
+
/*
|
|
2486
|
+
* `escalated` is the one blocked reason that must not be flattened into a
|
|
2487
|
+
* plain failure: it is waiting for a decision, not out of options. A plain
|
|
2488
|
+
* blocked write (watchdog timeout, failed self-check) still gets a kind, so
|
|
2489
|
+
* the model can tell "timed out" from "the child said no".
|
|
2490
|
+
*/
|
|
2491
|
+
if (patch.closure === undefined && next.closure !== 'escalated')
|
|
2492
|
+
next.closure = 'blocked';
|
|
2493
|
+
if (patch.blockedKind === undefined && next.blockedKind === undefined)
|
|
2494
|
+
next.blockedKind = 'failed';
|
|
2495
|
+
if (next.finishedAt === null)
|
|
2496
|
+
next.finishedAt = Date.now();
|
|
2497
|
+
}
|
|
2498
|
+
/*
|
|
2499
|
+
* [1.11.0] 单一不变量:escalated 只能投影成 blocked。
|
|
2500
|
+
*
|
|
2501
|
+
* 若允许 `status:'in_progress'` + `closure:'escalated'`,左侧栏会显示
|
|
2502
|
+
* 「正在做」而实际没有任何子会话在跑;反过来 `done` + `escalated` 会让交付
|
|
2503
|
+
* 门禁把一条待指示的请示当成完成。要恢复推进必须先写 closure:'instructed'。
|
|
2504
|
+
*/
|
|
2505
|
+
/*
|
|
2506
|
+
* [1.11.0] 契约外迁移一律拒绝(M1)。
|
|
2507
|
+
*
|
|
2508
|
+
* 这条检查放在投影不变量之前:若先投影再查表,`replanned → assigned`
|
|
2509
|
+
* 会被下面两条不变量放行(它们只管 escalated/done 的组合),死状态就复活了。
|
|
2510
|
+
*/
|
|
2511
|
+
const previousClosure = current.closure;
|
|
2512
|
+
if (previousClosure != null
|
|
2513
|
+
&& next.closure != null
|
|
2514
|
+
&& next.closure !== previousClosure
|
|
2515
|
+
&& !(CLOSURE_TRANSITIONS[String(previousClosure)] ?? []).includes(String(next.closure))) {
|
|
2516
|
+
throw new PlanError(`非法闭环迁移:${String(previousClosure)} → ${String(next.closure)}`, 'INVALID_TRANSITION');
|
|
2517
|
+
}
|
|
2518
|
+
if (next.closure === 'escalated' && next.status !== 'blocked') {
|
|
2519
|
+
throw new PlanError('closure=escalated 必须同时是 status=blocked;恢复推进请写 closure=instructed', 'INVALID_CLOSURE_PROJECTION');
|
|
2520
|
+
}
|
|
2521
|
+
if (next.status === 'done' && ['assigned', 'acknowledged', 'instructed', 'escalated'].includes(next.closure)) {
|
|
2522
|
+
throw new PlanError(`status=done 不能与 closure=${String(next.closure)} 同时成立`, 'INVALID_CLOSURE_PROJECTION');
|
|
1913
2523
|
}
|
|
1914
2524
|
const copy = statuses.slice();
|
|
1915
2525
|
copy[index] = next;
|
|
@@ -1965,6 +2575,14 @@ export function normaliseStatusEntry(entry) {
|
|
|
1965
2575
|
const lastError = status === 'done'
|
|
1966
2576
|
? (typeof source.lastError === 'string' && source.lastError !== '' ? source.lastError : legacyError)
|
|
1967
2577
|
: (typeof source.lastError === 'string' ? source.lastError : null);
|
|
2578
|
+
/*
|
|
2579
|
+
* [1.11.0] 未知 closure 按 null 降级,而不是猜一个最接近的阶段。
|
|
2580
|
+
*
|
|
2581
|
+
* 「猜」会让一条 1.10.6 的 blocked 行在升级后显示成「等待指示」,主 Agent
|
|
2582
|
+
* 于是去处理一个从未存在过的请示。null 的语义就是「这条日志没有闭环信息」,
|
|
2583
|
+
* 界面按 status 原样展示,与旧版完全一致。
|
|
2584
|
+
*/
|
|
2585
|
+
const closure = CLOSURES.includes(source.closure) ? source.closure : null;
|
|
1968
2586
|
return {
|
|
1969
2587
|
...source,
|
|
1970
2588
|
subtaskId: stringOr(source.subtaskId, ''),
|
|
@@ -1973,6 +2591,7 @@ export function normaliseStatusEntry(entry) {
|
|
|
1973
2591
|
department: stringOr(source.department, ''),
|
|
1974
2592
|
role: stringOr(source.role, ''),
|
|
1975
2593
|
status,
|
|
2594
|
+
closure,
|
|
1976
2595
|
progress,
|
|
1977
2596
|
childId: status === 'pending' ? null : (source.childId ?? null),
|
|
1978
2597
|
startedAt: status === 'pending' ? null : numberOrNull(source.startedAt),
|
|
@@ -2018,7 +2637,14 @@ export function deriveAggregates(statuses, expectedTotal) {
|
|
|
2018
2637
|
let inProgress = 0;
|
|
2019
2638
|
let pending = 0;
|
|
2020
2639
|
let progressSum = 0;
|
|
2021
|
-
|
|
2640
|
+
/*
|
|
2641
|
+
* [1.11.0] 重规划后的旧行保留在数组里用于追溯,但不计入任何统计。
|
|
2642
|
+
*
|
|
2643
|
+
* 判据与 runtime.liveStatuses 同为 `closure === 'replanned'`;旧行若仍被
|
|
2644
|
+
* 计入分母,卡片会长期停在「差一项」而计划实际已可交付。
|
|
2645
|
+
*/
|
|
2646
|
+
const live = statuses.filter((item) => item?.closure !== 'replanned');
|
|
2647
|
+
for (const item of live) {
|
|
2022
2648
|
if (item.status === 'done') {
|
|
2023
2649
|
done += 1;
|
|
2024
2650
|
progressSum += 100;
|
|
@@ -2061,8 +2687,8 @@ export function deriveAggregates(statuses, expectedTotal) {
|
|
|
2061
2687
|
const defined = Number.isInteger(expectedTotal) && Number(expectedTotal) > 0
|
|
2062
2688
|
? Number(expectedTotal)
|
|
2063
2689
|
: 0;
|
|
2064
|
-
const total = Math.max(
|
|
2065
|
-
const missing = Math.max(0, total -
|
|
2690
|
+
const total = Math.max(live.length, defined);
|
|
2691
|
+
const missing = Math.max(0, total - live.length);
|
|
2066
2692
|
pending += missing;
|
|
2067
2693
|
const allDone = total > 0 && done === total;
|
|
2068
2694
|
/*
|
|
@@ -2267,13 +2893,20 @@ export function aggregateInvariantViolations(snapshot, phase) {
|
|
|
2267
2893
|
* 不再参与判定(旧的 integrating 窗口已由 dispatch 内的 `executing` 覆盖)。
|
|
2268
2894
|
*/
|
|
2269
2895
|
export function planPhase(statuses, _resultSeen = false) {
|
|
2270
|
-
|
|
2896
|
+
/*
|
|
2897
|
+
* [1.11.0] 只由「仍有效」的行决定轮次阶段:closure='replanned' 的旧行是
|
|
2898
|
+
* 审计记录,既不能拖住 done,也不能制造 executing。
|
|
2899
|
+
*/
|
|
2900
|
+
const live = Array.isArray(statuses)
|
|
2901
|
+
? statuses.filter((item) => item?.closure !== 'replanned')
|
|
2902
|
+
: [];
|
|
2903
|
+
if (live.length === 0)
|
|
2271
2904
|
return 'pending';
|
|
2272
|
-
if (
|
|
2905
|
+
if (live.every((item) => item.status === 'done'))
|
|
2273
2906
|
return 'done';
|
|
2274
|
-
if (
|
|
2907
|
+
if (live.some((item) => item.status === 'blocked'))
|
|
2275
2908
|
return 'blocked';
|
|
2276
|
-
if (
|
|
2909
|
+
if (live.some((item) => item.status === 'in_progress' || item.status === 'done'))
|
|
2277
2910
|
return 'executing';
|
|
2278
2911
|
return 'pending';
|
|
2279
2912
|
}
|
|
@@ -2308,6 +2941,18 @@ export function reconcilePhase(phase, statuses) {
|
|
|
2308
2941
|
if (phase === 'pending' || phase === 'planning' || phase === 'executing') {
|
|
2309
2942
|
return derived === 'pending' ? phase : derived;
|
|
2310
2943
|
}
|
|
2944
|
+
/*
|
|
2945
|
+
* [1.11.0] 「受阻」可以被指令解除(T10 N3)。
|
|
2946
|
+
*
|
|
2947
|
+
* 主 Agent 下发指示后,该行从 blocked/escalated 变成 in_progress/instructed,
|
|
2948
|
+
* 但缓存的 `phase` 仍是 blocked。规则 4 会把 blocked 原样保留,于是卡片
|
|
2949
|
+
* 继续显示「卡住了」,而事实是子会话已经在执行新的指示。派生值只会在
|
|
2950
|
+
* 有 in_progress/done 且没有任何 blocked 行时才是 executing,此时缓存
|
|
2951
|
+
* 的 blocked 已经过期,必须让位;仍有 blocked 行时派生值是 blocked,
|
|
2952
|
+
* 走不到这里。
|
|
2953
|
+
*/
|
|
2954
|
+
if (phase === 'blocked' && derived === 'executing')
|
|
2955
|
+
return 'executing';
|
|
2311
2956
|
return phase ?? derived;
|
|
2312
2957
|
}
|
|
2313
2958
|
function verificationGap(item) {
|
|
@@ -2328,7 +2973,12 @@ function verificationGap(item) {
|
|
|
2328
2973
|
}
|
|
2329
2974
|
export function deliveryReadiness(snapshot, options = {}) {
|
|
2330
2975
|
const phase = typeof options.phase === 'string' ? options.phase : '';
|
|
2331
|
-
const
|
|
2976
|
+
const allStatuses = Array.isArray(snapshot?.statuses) ? snapshot.statuses : [];
|
|
2977
|
+
/*
|
|
2978
|
+
* [1.11.0] 被重规划的旧行不参与交付判定:它们不是「没做完的剩余项」,
|
|
2979
|
+
* 而是已被新行接替的历史。旧行若仍进入 remaining,计划永远差一项。
|
|
2980
|
+
*/
|
|
2981
|
+
const statuses = allStatuses.filter((item) => item?.closure !== 'replanned');
|
|
2332
2982
|
const remaining = [];
|
|
2333
2983
|
const blockers = [];
|
|
2334
2984
|
for (const item of statuses) {
|
|
@@ -2341,6 +2991,18 @@ export function deliveryReadiness(snapshot, options = {}) {
|
|
|
2341
2991
|
const gap = verificationGap(item);
|
|
2342
2992
|
if (gap)
|
|
2343
2993
|
blockers.push(`${id} ${gap}`);
|
|
2994
|
+
/*
|
|
2995
|
+
* [1.11.0] C-17:子 Agent 完成 ≠ 主 Agent 已收到回报。
|
|
2996
|
+
*
|
|
2997
|
+
* `closure:'done'` 是完成路径在写结果消息之前的中间态;结果消息写成功
|
|
2998
|
+
* 后由 `markReported`(runtime.ts,唯一写入点)改成 `reported` 并带
|
|
2999
|
+
* `report{state,at,ref,turn}`。只拦 `updatedBy:'child'` 的行:主 Agent 用
|
|
3000
|
+
* `dsh_workplan_update` 亲自改判的 done 本身就是裁决行为,没有子会话回报
|
|
3001
|
+
* 可言,若一并拦住会让“人工补完最后一项”永远无法交付。
|
|
3002
|
+
*/
|
|
3003
|
+
if (item?.closure === 'done' && item?.updatedBy === 'child') {
|
|
3004
|
+
blockers.push(`${id} 已完成但尚未回报(closure=done):结果消息还没有写入会话`);
|
|
3005
|
+
}
|
|
2344
3006
|
const acceptance = Array.isArray(item?.acceptance) ? item.acceptance : [];
|
|
2345
3007
|
/*
|
|
2346
3008
|
* An empty acceptance list is not "everything passed"; it is the absence
|
|
@@ -2349,10 +3011,30 @@ export function deliveryReadiness(snapshot, options = {}) {
|
|
|
2349
3011
|
*/
|
|
2350
3012
|
if (acceptance.length === 0)
|
|
2351
3013
|
blockers.push(`${id} 未提供验收项(acceptance 为空)`);
|
|
3014
|
+
/*
|
|
3015
|
+
* [1.11.0] 交付门也看「计划写了什么」(T5 C4 / T10 D-01)。
|
|
3016
|
+
*
|
|
3017
|
+
* adjudicate 在收尾时已经拦一次,但主 Agent 的 `update` 改判与历史回放
|
|
3018
|
+
* 不经过 adjudicate;只在收尾拦,交付门仍会放过「5 条验收只回 1 条」的
|
|
3019
|
+
* 计划。这里按同一份 acceptanceCoverage 复核,两处结论不会分叉。
|
|
3020
|
+
* 只对自称 structured 的行生效:legacy 行本来就会被上面的 gap 拦住。
|
|
3021
|
+
*/
|
|
3022
|
+
const expected = options.expectedAcceptance?.[id];
|
|
3023
|
+
if (item?.verification === 'structured' && Array.isArray(expected) && expected.length > 0) {
|
|
3024
|
+
const coverage = acceptanceCoverage(expected, acceptance);
|
|
3025
|
+
if (coverage.missing.length > 0) {
|
|
3026
|
+
blockers.push(`${id} 验收项未被逐条回应(${coverage.covered}/${coverage.total}):${coverage.missing
|
|
3027
|
+
.map((entry) => truncate(entry, 80)).slice(0, 3).join(';')}`);
|
|
3028
|
+
}
|
|
3029
|
+
}
|
|
2352
3030
|
for (const entry of acceptance) {
|
|
2353
3031
|
if (entry?.result !== 'pass') {
|
|
2354
3032
|
blockers.push(`${id} 验收未通过:${truncate(String(entry?.criterion ?? '未命名验收项'), 80)}`);
|
|
2355
3033
|
}
|
|
3034
|
+
else if (String(entry?.evidence ?? '').trim() === '') {
|
|
3035
|
+
/* [1.11.0] C-19:自述通过但没有任何可复核证据,不算通过。 */
|
|
3036
|
+
blockers.push(`${id} 验收自称通过但没有证据:${truncate(String(entry?.criterion ?? '未命名验收项'), 80)}`);
|
|
3037
|
+
}
|
|
2356
3038
|
}
|
|
2357
3039
|
const missing = Array.isArray(item?.missingArtifacts) ? item.missingArtifacts : [];
|
|
2358
3040
|
if (missing.length > 0)
|
|
@@ -2363,6 +3045,17 @@ export function deliveryReadiness(snapshot, options = {}) {
|
|
|
2363
3045
|
blockers.push(`${id} 声明产物不存在:${truncate(String(artifact?.ref ?? '未命名产物'), 120)}`);
|
|
2364
3046
|
}
|
|
2365
3047
|
}
|
|
3048
|
+
/*
|
|
3049
|
+
* [1.11.0] 跨部门需求没有闭环就不能交付(用户 m00687)。
|
|
3050
|
+
*
|
|
3051
|
+
* 一条 open/routed 的 handoff 表示:某个部门明确说了「我需要谁提供什么」,
|
|
3052
|
+
* 而主 Agent 还没有指派或答复。此时说「全部做完、可以交付」是把未落实的
|
|
3053
|
+
* 协作当成不存在。answered/closed 才解除这条阻塞。
|
|
3054
|
+
*/
|
|
3055
|
+
for (const handoff of openHandoffs(item)) {
|
|
3056
|
+
const target = handoff?.department ? ` → ${handoff.department}` : '';
|
|
3057
|
+
blockers.push(`${id} 跨部门需求未落实(${String(handoff?.id ?? 'H?')}${target}):${truncate(String(handoff?.request ?? ''), 100)}`);
|
|
3058
|
+
}
|
|
2366
3059
|
}
|
|
2367
3060
|
const done = Number(snapshot?.stats?.done ?? 0);
|
|
2368
3061
|
const total = Number(snapshot?.stats?.total ?? 0);
|
|
@@ -2378,6 +3071,21 @@ export function deliveryReadiness(snapshot, options = {}) {
|
|
|
2378
3071
|
if (total > 0 && missingRows > 0) {
|
|
2379
3072
|
blockers.push(`状态明细缺少 ${missingRows} 行(计划定义 ${total} 项,实际只写 ${statuses.length} 行):缺失项按未完成计`);
|
|
2380
3073
|
}
|
|
3074
|
+
/*
|
|
3075
|
+
* [1.11.0] C-17:记录级闭环事实也必须进交付门。
|
|
3076
|
+
*
|
|
3077
|
+
* 这两项不在 `statuses` 里,但同样是“没闭环”:`changeRequest.pending`
|
|
3078
|
+
* 表示用户在执行中提的新要求还没被 replan/dispatch/cancel 判定,
|
|
3079
|
+
* `reportPending` 表示上一轮完成结果没写进会话(没有证据的完成)。
|
|
3080
|
+
* 由调用方(runtime 的 readinessOf / buildRoundResult)显式传入,避免
|
|
3081
|
+
* 在本函数里反查 record、把纯函数变成第二个状态源。
|
|
3082
|
+
*/
|
|
3083
|
+
if (options.changePending === true) {
|
|
3084
|
+
blockers.push('存在未落实的需求变更(changeRequest.pending):先用 dsh_workplan_replan 判定并重规划,或 dispatch/cancel 明确判定');
|
|
3085
|
+
}
|
|
3086
|
+
if (options.reportPending === true) {
|
|
3087
|
+
blockers.push('上一轮完成结果尚未成功写入会话(reportPending):先补齐回报再交付');
|
|
3088
|
+
}
|
|
2381
3089
|
if (phase === 'cancelled' && (remaining.length > 0 || blockers.length > 0)) {
|
|
2382
3090
|
blockers.unshift(`计划已取消但只完成 ${done}/${total}:取消只停止执行,不等于完成`);
|
|
2383
3091
|
}
|
|
@@ -2419,12 +3127,59 @@ export function readySubtasks(plan, statuses, options = {}) {
|
|
|
2419
3127
|
return flattenSubtasks(plan)
|
|
2420
3128
|
.map((entry) => entry.subtask)
|
|
2421
3129
|
.filter((subtask) => {
|
|
3130
|
+
// [1.11.0] 已被 replace/cancel 的旧定义不再可调度。
|
|
3131
|
+
if (subtask.supersededAt)
|
|
3132
|
+
return false;
|
|
2422
3133
|
const status = byId.get(subtask.id);
|
|
2423
3134
|
if (!status || status.status !== 'pending')
|
|
2424
3135
|
return false;
|
|
2425
3136
|
return subtask.dependsOn.every((dependency) => dependencyOk(byId.get(dependency), dependency));
|
|
2426
3137
|
});
|
|
2427
3138
|
}
|
|
3139
|
+
/**
|
|
3140
|
+
* [1.11.0] 重规划 cancel 掉的上游必须显式收束下游(T4 D-T4-02)。
|
|
3141
|
+
*
|
|
3142
|
+
* cancel 只把旧行标成 superseded,不会重写下游的 dependsOn(replace 会)。
|
|
3143
|
+
* 于是下游永远等一个不再调度的 ID:不报错、不 blocked、不进入交付阻塞——
|
|
3144
|
+
* 用户看到的是「一直 pending」。这里在每次派发与交付判定前把这类下游标成
|
|
3145
|
+
* blocked,并写明该用 replan 重新安排,而不是让它静默死锁。
|
|
3146
|
+
*/
|
|
3147
|
+
export function blockCancelledDependents(plan, statuses) {
|
|
3148
|
+
const list = Array.isArray(statuses) ? statuses : [];
|
|
3149
|
+
const cancelled = new Set();
|
|
3150
|
+
for (const { subtask } of flattenSubtasks(plan)) {
|
|
3151
|
+
if (!subtask?.supersededAt)
|
|
3152
|
+
continue;
|
|
3153
|
+
const replacedBy = Array.isArray(subtask.replacedBy) ? subtask.replacedBy : [];
|
|
3154
|
+
if (replacedBy.length === 0)
|
|
3155
|
+
cancelled.add(String(subtask.id));
|
|
3156
|
+
}
|
|
3157
|
+
if (cancelled.size === 0)
|
|
3158
|
+
return list;
|
|
3159
|
+
let next = list.slice();
|
|
3160
|
+
for (const { subtask } of flattenSubtasks(plan)) {
|
|
3161
|
+
if (subtask?.supersededAt)
|
|
3162
|
+
continue;
|
|
3163
|
+
const current = next.find((item) => item?.subtaskId === subtask.id);
|
|
3164
|
+
if (!current || current.status !== 'pending' || current.closure === 'replanned')
|
|
3165
|
+
continue;
|
|
3166
|
+
const hit = (Array.isArray(subtask.dependsOn) ? subtask.dependsOn : []).filter((id) => cancelled.has(String(id)));
|
|
3167
|
+
if (hit.length === 0)
|
|
3168
|
+
continue;
|
|
3169
|
+
try {
|
|
3170
|
+
next = applyStatus(next, subtask.id, {
|
|
3171
|
+
status: 'blocked',
|
|
3172
|
+
closure: 'blocked',
|
|
3173
|
+
error: `依赖已被重规划取消:${hit.join('、')}。请用 dsh_workplan_replan(action=replace) 重新安排这一项,或显式取消它。`,
|
|
3174
|
+
updatedBy: 'system',
|
|
3175
|
+
});
|
|
3176
|
+
}
|
|
3177
|
+
catch {
|
|
3178
|
+
// 单行冲突不应中断整轮;下一轮派发会再次尝试收束。
|
|
3179
|
+
}
|
|
3180
|
+
}
|
|
3181
|
+
return next;
|
|
3182
|
+
}
|
|
2428
3183
|
export function markDependentsBlocked(plan, statuses, blockedId, cause = null) {
|
|
2429
3184
|
let next = statuses.slice();
|
|
2430
3185
|
let changed = true;
|
|
@@ -2439,7 +3194,14 @@ export function markDependentsBlocked(plan, statuses, blockedId, cause = null) {
|
|
|
2439
3194
|
if (blocked.has(subtask.id))
|
|
2440
3195
|
continue;
|
|
2441
3196
|
const status = next.find((item) => item.subtaskId === subtask.id);
|
|
2442
|
-
|
|
3197
|
+
/*
|
|
3198
|
+
* [1.11.0] 已被重规划的旧行不再参与级联(T6 F6-01)。
|
|
3199
|
+
*
|
|
3200
|
+
* 它们的 status 仍是 pending,但 closure 是 replanned;对它写 blocked
|
|
3201
|
+
* 会触发 `replanned → blocked` 的非法迁移,异常会冒泡到整轮派发。
|
|
3202
|
+
* 这些行本来就不参与调度与交付,跳过即可。
|
|
3203
|
+
*/
|
|
3204
|
+
if (!status || status.status !== 'pending' || status.closure === 'replanned')
|
|
2443
3205
|
continue;
|
|
2444
3206
|
if (subtask.dependsOn.some((dependency) => blocked.has(dependency))) {
|
|
2445
3207
|
const direct = subtask.dependsOn.filter((id) => blocked.has(id));
|
|
@@ -2495,6 +3257,15 @@ export function resetBlockedStatuses(statuses) {
|
|
|
2495
3257
|
for (const item of statuses) {
|
|
2496
3258
|
if (item?.status !== 'blocked' && item?.status !== 'in_progress')
|
|
2497
3259
|
continue;
|
|
3260
|
+
/*
|
|
3261
|
+
* [1.11.0] 「等指示」不是「失败」,不能靠重新 dispatch 抹平。
|
|
3262
|
+
*
|
|
3263
|
+
* 旧逻辑把所有 blocked 行重置为 pending 再跑一遍,于是一条正等待主 Agent
|
|
3264
|
+
* 决策的请示会被静默重派、问题被丢掉。升级行必须由主 Agent 显式处理
|
|
3265
|
+
* (给出指示改判 instructed,或明确放弃改判 blocked)后才离开这个状态。
|
|
3266
|
+
*/
|
|
3267
|
+
if (item?.closure === 'escalated' || item?.closure === 'instructed')
|
|
3268
|
+
continue;
|
|
2498
3269
|
next = applyStatus(next, item.subtaskId, {
|
|
2499
3270
|
status: 'pending',
|
|
2500
3271
|
progress: 0,
|
|
@@ -2531,7 +3302,18 @@ export function canResumePlan(record, maxResumes = MAX_PLAN_RESUMES) {
|
|
|
2531
3302
|
* against the resume budget (it is a time slice, not a repair).
|
|
2532
3303
|
*/
|
|
2533
3304
|
const paused = record?.phase === 'paused';
|
|
3305
|
+
/*
|
|
3306
|
+
* [1.11.0] 「等指示」不是失败,不能触发续跑(T10 N2)。
|
|
3307
|
+
*
|
|
3308
|
+
* `closure:'escalated'` 的行 status 也是 blocked,旧过滤会把它算成待修复:
|
|
3309
|
+
* 计划明明在等主 Agent 拍板,卡片却显示「可续跑」,点下去零派发(每行都在
|
|
3310
|
+
* 等指示),还白白消耗一次续跑额度。它需要的是 instruct,不是 dispatch。
|
|
3311
|
+
* 该行在收到指示后由 `applyStatus` 迁移成 instructed/in_progress,届时才
|
|
3312
|
+
* 重新参与续跑判定。
|
|
3313
|
+
*/
|
|
2534
3314
|
const failed = statuses.filter((item) => {
|
|
3315
|
+
if (item?.closure === 'escalated')
|
|
3316
|
+
return false;
|
|
2535
3317
|
if (item?.status === 'blocked' || item?.status === 'in_progress')
|
|
2536
3318
|
return true;
|
|
2537
3319
|
return paused && item?.status === 'pending';
|
|
@@ -2581,7 +3363,11 @@ export function buildChildPrompt({ plan, subtask, stage, dependencyResults, cwd,
|
|
|
2581
3363
|
const dependencies = results.length === 0
|
|
2582
3364
|
? '无'
|
|
2583
3365
|
: results
|
|
2584
|
-
.map((item) =>
|
|
3366
|
+
.map((item) => {
|
|
3367
|
+
const refs = Array.isArray(item?.artifacts) ? item.artifacts.filter(Boolean).slice(0, 8) : [];
|
|
3368
|
+
return `- ${item.id} ${item.title}:${item.status}${item.summary ? `|${item.summary}` : ''}`
|
|
3369
|
+
+ (refs.length ? `|可消费产物:${refs.join('、')}` : '');
|
|
3370
|
+
})
|
|
2585
3371
|
.join('\n');
|
|
2586
3372
|
/*
|
|
2587
3373
|
* [1.7.0] No second truncation here.
|
|
@@ -2627,6 +3413,42 @@ export function buildChildPrompt({ plan, subtask, stage, dependencyResults, cwd,
|
|
|
2627
3413
|
'已经覆盖验收标准的部分保持原样,只补缺失部分;结尾按契约给出证据块。',
|
|
2628
3414
|
].join('\n')
|
|
2629
3415
|
: '';
|
|
3416
|
+
/*
|
|
3417
|
+
* [1.11.0] 主 Agent 已经回答过的请示必须随下一次激活送进子提示词(M6/P-04)。
|
|
3418
|
+
*
|
|
3419
|
+
* `dsh_workplan_instruct` 在子会话已结束时只能把指示写进状态行;若不在下一次
|
|
3420
|
+
* 激活的提示词里带上它,子 Agent 会重新从零猜测,指示等于没下发。这里读的是
|
|
3421
|
+
* 同一条状态行上的 `instruction.text`,与 `instructSubtask` 的写入同源。
|
|
3422
|
+
*/
|
|
3423
|
+
const pendingInstruction = previousStatus?.closure === 'instructed'
|
|
3424
|
+
&& typeof previousStatus?.instruction?.text === 'string'
|
|
3425
|
+
&& previousStatus.instruction.text.trim() !== ''
|
|
3426
|
+
? [
|
|
3427
|
+
'',
|
|
3428
|
+
'主 Agent 对你上一次请示的指示(这是决定,不是建议;必须按它执行):',
|
|
3429
|
+
truncate(String(previousStatus.instruction.text), MAX_INSTRUCTION_CHARS),
|
|
3430
|
+
'执行时不要再就同一问题重复请示;只有出现新的、未被这条指示覆盖的阻塞时才再次上报。',
|
|
3431
|
+
].join('\n')
|
|
3432
|
+
: '';
|
|
3433
|
+
/*
|
|
3434
|
+
* [1.11.0] 跨部门需求的答复必须随下一次激活回到提出方(用户 m00687)。
|
|
3435
|
+
*
|
|
3436
|
+
* 需求被主 Agent 指派或答复后只存在状态行里;若子 Agent 再次被唤醒时看不到
|
|
3437
|
+
* 这份答复,它会再提一次同样的需求,或者按错误的前提继续。这里同时列出
|
|
3438
|
+
* 「已答复的」与「仍在等待的」,后者明确要求不要重复上报、先做不依赖它的部分。
|
|
3439
|
+
*/
|
|
3440
|
+
const previousHandoffs = previousStatus && Array.isArray(previousStatus.handoffs)
|
|
3441
|
+
? previousStatus.handoffs
|
|
3442
|
+
: [];
|
|
3443
|
+
const answeredHandoffs = previousHandoffs.filter((item) => item?.status === 'answered' && item?.response);
|
|
3444
|
+
const waitingHandoffs = previousHandoffs.filter((item) => item?.status === 'open' || item?.status === 'routed');
|
|
3445
|
+
const handoffBrief = answeredHandoffs.length === 0 && waitingHandoffs.length === 0
|
|
3446
|
+
? ''
|
|
3447
|
+
: [
|
|
3448
|
+
'跨部门需求处置结果(这是事实,不是建议):',
|
|
3449
|
+
...answeredHandoffs.map((item) => `- ${item.id} 已答复(${item.department ?? '主 Agent'}):${truncate(String(item.response), 600)}`),
|
|
3450
|
+
...waitingHandoffs.map((item) => `- ${item.id} 仍在等待:${truncate(String(item.request), 300)}。不要重复上报;先完成不依赖它的部分,若要继续必须等主 Agent 答复。`),
|
|
3451
|
+
].join('\n');
|
|
2630
3452
|
/*
|
|
2631
3453
|
* [1.8.12] 提示词整体过一遍 session-reference 消毒。
|
|
2632
3454
|
*
|
|
@@ -2658,27 +3480,37 @@ export function buildChildPrompt({ plan, subtask, stage, dependencyResults, cwd,
|
|
|
2658
3480
|
'依赖子任务的已知结果:',
|
|
2659
3481
|
dependencies,
|
|
2660
3482
|
...(team ? ['', '兄弟部门进展(同一计划内的其他子任务;不要直接联系它们,需要协作请写进 needs 由主 Agent 转达):', team] : []),
|
|
3483
|
+
...(handoffBrief ? ['', handoffBrief] : []),
|
|
2661
3484
|
...(failure ? [failure] : []),
|
|
2662
3485
|
...(resumeHint ? [resumeHint] : []),
|
|
3486
|
+
...(pendingInstruction ? [pendingInstruction] : []),
|
|
2663
3487
|
'',
|
|
2664
3488
|
'执行要求:',
|
|
2665
3489
|
'- 只完成本子任务,不擅自扩大范围;不要派生新的 Agent。',
|
|
2666
|
-
'-
|
|
3490
|
+
'- 你没有派发权限,也不能重启或安装任何服务;遇到这类步骤,必须先上报(见下方「遇阻上报」),再停止推进等待指示,不要空转。',
|
|
2667
3491
|
'- 不得编造事实、文件、命令结果或证据;无法确认时明确写“未验证”。',
|
|
2668
3492
|
'- 可以读取工作目录、运行必要命令、修改与任务相关的文件。',
|
|
2669
3493
|
'- 优先使用给定上下文和本地工具;只有任务明确需要最新外部信息时才联网搜索。',
|
|
2670
|
-
'-
|
|
2671
|
-
'- 如果确实无法完成,第一行输出 `BLOCKED:
|
|
3494
|
+
'- 先把该做的检查做完再汇报:每条验收标准至少要有一处可复核的证据(命令输出/文件/引用),不要为了省时间跳过验证。',
|
|
3495
|
+
'- 如果确实无法完成,第一行输出 `BLOCKED: 原因`,并按下方「遇阻上报」给出 question。',
|
|
2672
3496
|
'- 完成后用简洁中文汇报:做了什么、证据/文件、验收结果、遗留问题。',
|
|
2673
3497
|
'',
|
|
3498
|
+
'遇阻上报(强制,先于继续动作):',
|
|
3499
|
+
'- 出现以下任一情况,第一动作是上报,不是猜测、绕过或静默继续:缺少权限/输入/依赖产物;需求自相矛盾;无法验证;同一命令连续两次失败;需要主 Agent 决定方向。',
|
|
3500
|
+
'- 优先调用工具:`dsh_workplan_escalate({ "subtaskId": "<你的子任务 ID>", "question": "需要主 Agent 回答的单个问题", "options": ["可选项 A", "可选项 B"], "needs": ["需要谁提供什么"] })`。调用成功后停止推进,只做只读核对。',
|
|
3501
|
+
'- 如果该工具不可用:在结果块里写 `"status":"blocked"`、`"blockedReason":"..."`,并加上 `"question":"..."` 与 `"options":[...]`。',
|
|
3502
|
+
'- 上报必须写清:卡在哪一步、已经尝试过什么、需要谁做什么决定;不要把“未验证”当成受阻上报。',
|
|
3503
|
+
'- 能独立完成的部分先做完再上报;但一旦需要外部决定,立即停止写入,等待指示,不得自行选定一个选项继续。',
|
|
3504
|
+
'',
|
|
2674
3505
|
'结果回传(必填):在汇报的最后附上一个结构化结果块,主 Agent 会按它逐项核对,不再只依赖自述。',
|
|
2675
3506
|
EVIDENCE_MARKER_BEGIN,
|
|
2676
|
-
'{"status":"done","blockedReason":"","acceptance":[{"criterion":"把上面的验收标准原文抄在这里","result":"pass","evidence":"用命令输出、文件路径或引用证明"}],"artifacts":[{"kind":"file","ref":"相对工作目录的路径"}],"needs":[]}',
|
|
3507
|
+
'{"status":"done","blockedReason":"","acceptance":[{"criterion":"把上面的验收标准原文抄在这里","result":"pass","evidence":"用命令输出、文件路径或引用证明"}],"artifacts":[{"kind":"file","ref":"相对工作目录的路径"}],"needs":[],"question":"","options":[]}',
|
|
2677
3508
|
EVIDENCE_MARKER_END,
|
|
2678
3509
|
'- status 只能是 done / blocked / partial;partial 会被当作受阻处理。',
|
|
2679
3510
|
'- acceptance 必须逐条覆盖上面的验收标准;result 只能是 pass / fail / unverified。无法确认就写 unverified,不要写 pass。',
|
|
2680
3511
|
'- artifacts 写你实际产出或修改的文件(kind=file,ref 用相对路径)、链接(kind=url)或说明(kind=note)。',
|
|
2681
|
-
'-
|
|
3512
|
+
'- needs 写需要其他部门配合的事项,每条写清「哪个部门 + 需要什么 + 用来做什么」;它会被登记为待处理需求,主 Agent 必须处置后计划才能交付。不要直连其他子 Agent。',
|
|
3513
|
+
'- question/options 只在受阻上报时填写;done 时保持空字符串/空数组。',
|
|
2682
3514
|
'- 整块不超过 8000 字符;不要把这段模板本身抄进结果块。',
|
|
2683
3515
|
].join('\n'));
|
|
2684
3516
|
}
|