dsh-workplan 2.0.3 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +106 -0
- package/README.en.md +2 -2
- package/README.md +2 -2
- package/README.zh.md +1 -1
- package/docs/release-record-2.0.3-20261105.md +47 -0
- package/lib/comm-format.js +184 -0
- package/lib/host.js +1 -1
- package/lib/plan.js +525 -23
- package/lib/runtime.js +76 -2
- package/package.json +1 -1
- package/src/comm-format.ts +215 -0
- package/src/host.ts +1 -1
- package/src/plan.ts +499 -20
- package/src/runtime.ts +82 -2
package/src/plan.ts
CHANGED
|
@@ -13,6 +13,11 @@ import { DEPARTMENT_CAPABILITIES, departmentCapability } from './departments.js'
|
|
|
13
13
|
import { MAX_TASK_ANCHOR_CHARS } from './understanding.js'
|
|
14
14
|
import { isMappingDigest, verifyMappingDigest } from './supersede-digest.js'
|
|
15
15
|
import { phaseSummaryLine, threePhasePromptLines, validatePhaseEvidence } from './three-phase.js'
|
|
16
|
+
import {
|
|
17
|
+
checkCommFormatCompliance,
|
|
18
|
+
renderCommFormatRejection,
|
|
19
|
+
renderCommFormatSpec,
|
|
20
|
+
} from './comm-format.js'
|
|
16
21
|
|
|
17
22
|
export const PLAN_VERSION = 1
|
|
18
23
|
|
|
@@ -137,7 +142,7 @@ export const CLOSURES = Object.freeze([
|
|
|
137
142
|
'replanned',
|
|
138
143
|
])
|
|
139
144
|
/** Why a `blocked` / `escalated` row stopped. Read by the dashboard and result message. */
|
|
140
|
-
export const BLOCKED_KINDS = Object.freeze(['failed', 'timeout', 'no-ack', 'escalated'])
|
|
145
|
+
export const BLOCKED_KINDS = Object.freeze(['failed', 'timeout', 'no-ack', 'escalated', 'comm-format'])
|
|
141
146
|
/**
|
|
142
147
|
* [1.11.0] 合法闭环迁移(T4 §4 的 19 条,M1)。
|
|
143
148
|
*
|
|
@@ -913,6 +918,119 @@ export function flattenSubtasks(plan: any): Array<{ stage: any; subtask: any }>
|
|
|
913
918
|
return out
|
|
914
919
|
}
|
|
915
920
|
|
|
921
|
+
/**
|
|
922
|
+
* [2.1.0] 只补结构、不补内容。
|
|
923
|
+
*
|
|
924
|
+
* 规划器返回的 JSON 常见三类损伤:少一个 `}`、多一个尾逗号、字符串里有裸换行。
|
|
925
|
+
* 旧实现直接 JSON.parse,一个字符之差就抛 INVALID_JSON,整个计划降级。
|
|
926
|
+
* 这里做的是纯结构修复:配对括号、去掉尾逗号、把字符串里的裸控制符转义。
|
|
927
|
+
* 绝不猜测、绝不编造任何字段或值——修不好就照旧抛错。
|
|
928
|
+
*/
|
|
929
|
+
export function repairJsonStructure(input: string): string {
|
|
930
|
+
const stack: string[] = []
|
|
931
|
+
let inString = false
|
|
932
|
+
let escaped = false
|
|
933
|
+
let out = ''
|
|
934
|
+
for (let i = 0; i < input.length; i += 1) {
|
|
935
|
+
const ch = input[i] as string
|
|
936
|
+
if (inString) {
|
|
937
|
+
if (escaped) {
|
|
938
|
+
out += ch
|
|
939
|
+
escaped = false
|
|
940
|
+
continue
|
|
941
|
+
}
|
|
942
|
+
if (ch === '\\') {
|
|
943
|
+
out += ch
|
|
944
|
+
escaped = true
|
|
945
|
+
continue
|
|
946
|
+
}
|
|
947
|
+
if (ch === '"') {
|
|
948
|
+
inString = false
|
|
949
|
+
out += ch
|
|
950
|
+
continue
|
|
951
|
+
}
|
|
952
|
+
if (ch === '\n') {
|
|
953
|
+
out += '\\n'
|
|
954
|
+
continue
|
|
955
|
+
}
|
|
956
|
+
if (ch === '\r') {
|
|
957
|
+
out += '\\r'
|
|
958
|
+
continue
|
|
959
|
+
}
|
|
960
|
+
if (ch === '\t') {
|
|
961
|
+
out += '\\t'
|
|
962
|
+
continue
|
|
963
|
+
}
|
|
964
|
+
if (ch < ' ') {
|
|
965
|
+
out += `\\u${ch.charCodeAt(0).toString(16).padStart(4, '0')}`
|
|
966
|
+
continue
|
|
967
|
+
}
|
|
968
|
+
out += ch
|
|
969
|
+
continue
|
|
970
|
+
}
|
|
971
|
+
if (ch === '"') {
|
|
972
|
+
inString = true
|
|
973
|
+
out += ch
|
|
974
|
+
continue
|
|
975
|
+
}
|
|
976
|
+
if (ch === '{' || ch === '[') {
|
|
977
|
+
stack.push(ch)
|
|
978
|
+
out += ch
|
|
979
|
+
continue
|
|
980
|
+
}
|
|
981
|
+
if (ch === '}' || ch === ']') {
|
|
982
|
+
const want = ch === '}' ? '{' : '['
|
|
983
|
+
while (stack.length > 0 && stack[stack.length - 1] !== want) {
|
|
984
|
+
out += stack.pop() === '{' ? '}' : ']'
|
|
985
|
+
}
|
|
986
|
+
if (stack.length > 0) stack.pop()
|
|
987
|
+
out += ch
|
|
988
|
+
continue
|
|
989
|
+
}
|
|
990
|
+
out += ch
|
|
991
|
+
}
|
|
992
|
+
if (inString) out += '"'
|
|
993
|
+
while (stack.length > 0) out += stack.pop() === '{' ? '}' : ']'
|
|
994
|
+
return stripTrailingCommas(out)
|
|
995
|
+
}
|
|
996
|
+
|
|
997
|
+
function stripTrailingCommas(input: string): string {
|
|
998
|
+
return input.replace(/,(\s*[}\]])/g, '$1')
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
/** 找到最后一个「成员边界」逗号(在任何嵌套深度上、且不在字符串内)。 */
|
|
1002
|
+
function lastMemberComma(input: string): number {
|
|
1003
|
+
let inString = false
|
|
1004
|
+
let escaped = false
|
|
1005
|
+
let index = -1
|
|
1006
|
+
for (let i = 0; i < input.length; i += 1) {
|
|
1007
|
+
const ch = input[i] as string
|
|
1008
|
+
if (inString) {
|
|
1009
|
+
if (escaped) escaped = false
|
|
1010
|
+
else if (ch === '\\') escaped = true
|
|
1011
|
+
else if (ch === '"') inString = false
|
|
1012
|
+
continue
|
|
1013
|
+
}
|
|
1014
|
+
if (ch === '"') inString = true
|
|
1015
|
+
else if (ch === ',') index = i
|
|
1016
|
+
}
|
|
1017
|
+
return index
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
/**
|
|
1021
|
+
* 截断修复:规划器被 max tokens 截断时,最后一个成员通常是半截的。
|
|
1022
|
+
* 从尾部逐个丢弃不完整成员,再补齐括号——丢掉的都是「本来就残缺、不能用」的内容。
|
|
1023
|
+
*/
|
|
1024
|
+
function* truncatedCandidates(input: string, rounds = 24): Generator<string> {
|
|
1025
|
+
let working = input
|
|
1026
|
+
for (let round = 0; round < rounds; round += 1) {
|
|
1027
|
+
const cut = lastMemberComma(working)
|
|
1028
|
+
if (cut <= 0) return
|
|
1029
|
+
working = working.slice(0, cut)
|
|
1030
|
+
yield repairJsonStructure(working)
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
|
|
916
1034
|
export function extractJson(text: any) {
|
|
917
1035
|
if (typeof text !== 'string' || text.trim() === '') {
|
|
918
1036
|
throw new PlanError('规划器没有返回内容', 'EMPTY_OUTPUT')
|
|
@@ -921,14 +1039,32 @@ export function extractJson(text: any) {
|
|
|
921
1039
|
const fenced = value.match(/```(?:json)?\s*([\s\S]*?)```/i)
|
|
922
1040
|
if (fenced) value = fenced[1].trim()
|
|
923
1041
|
const first = value.indexOf('{')
|
|
924
|
-
|
|
925
|
-
if (first < 0 || last <= first) {
|
|
1042
|
+
if (first < 0) {
|
|
926
1043
|
throw new PlanError('规划器返回内容中没有 JSON 对象', 'NO_JSON')
|
|
927
1044
|
}
|
|
928
|
-
const
|
|
1045
|
+
const last = value.lastIndexOf('}')
|
|
1046
|
+
// [2.1.0] 输出被截断时没有结尾 `}`,旧实现这里直接判 NO_JSON —— 改成仍以 `{` 起头做修复。
|
|
1047
|
+
const candidate = last > first ? value.slice(first, last + 1) : value.slice(first)
|
|
1048
|
+
|
|
929
1049
|
try {
|
|
930
1050
|
return JSON.parse(candidate)
|
|
931
1051
|
} catch (error) {
|
|
1052
|
+
const repaired = repairJsonStructure(candidate)
|
|
1053
|
+
try {
|
|
1054
|
+
const value2 = JSON.parse(repaired)
|
|
1055
|
+
console.warn('[dsh-workplan] 规划器 JSON 结构已修复(只补括号/逗号,未改内容)')
|
|
1056
|
+
return value2
|
|
1057
|
+
} catch {
|
|
1058
|
+
for (const attempt of truncatedCandidates(repaired)) {
|
|
1059
|
+
try {
|
|
1060
|
+
const value3 = JSON.parse(attempt)
|
|
1061
|
+
console.warn('[dsh-workplan] 规划器 JSON 被截断,已丢弃末尾残缺成员后解析成功')
|
|
1062
|
+
return value3
|
|
1063
|
+
} catch {
|
|
1064
|
+
continue
|
|
1065
|
+
}
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
932
1068
|
throw new PlanError(`规划器返回的 JSON 无法解析:${error instanceof Error ? error.message : String(error)}`, 'INVALID_JSON')
|
|
933
1069
|
}
|
|
934
1070
|
}
|
|
@@ -1755,6 +1891,50 @@ export function normaliseCriterion(value: any) {
|
|
|
1755
1891
|
.replace(/^(必须|需要|应当|应该|请|务必)/, '')
|
|
1756
1892
|
}
|
|
1757
1893
|
|
|
1894
|
+
/**
|
|
1895
|
+
* [2.0.4] 两条验收描述的相似度(0–1)。
|
|
1896
|
+
*
|
|
1897
|
+
* 用字符二元组 Dice 系数而不是编辑距离:中文短句里「逐项」「一致性」这类
|
|
1898
|
+
* 词被增删时,二元组重合度比字符级编辑距离更接近人的判断,也不需要引入
|
|
1899
|
+
* 任何依赖。空串与单字返回 0,交给调用方的长度门槛处理。
|
|
1900
|
+
*/
|
|
1901
|
+
export function criterionSimilarity(a: string, b: string) {
|
|
1902
|
+
if (!a || !b) return 0
|
|
1903
|
+
if (a === b) return 1
|
|
1904
|
+
const grams = (text: string) => {
|
|
1905
|
+
const chars = Array.from(text)
|
|
1906
|
+
const out = new Map<string, number>()
|
|
1907
|
+
for (let i = 0; i + 1 < chars.length; i += 1) {
|
|
1908
|
+
const gram = chars[i] + chars[i + 1]
|
|
1909
|
+
out.set(gram, (out.get(gram) ?? 0) + 1)
|
|
1910
|
+
}
|
|
1911
|
+
return out
|
|
1912
|
+
}
|
|
1913
|
+
const left = grams(a)
|
|
1914
|
+
const right = grams(b)
|
|
1915
|
+
let leftTotal = 0
|
|
1916
|
+
let rightTotal = 0
|
|
1917
|
+
for (const count of left.values()) leftTotal += count
|
|
1918
|
+
for (const count of right.values()) rightTotal += count
|
|
1919
|
+
if (leftTotal === 0 || rightTotal === 0) return 0
|
|
1920
|
+
let overlap = 0
|
|
1921
|
+
for (const [gram, count] of left) {
|
|
1922
|
+
overlap += Math.min(count, right.get(gram) ?? 0)
|
|
1923
|
+
}
|
|
1924
|
+
return (2 * overlap) / (leftTotal + rightTotal)
|
|
1925
|
+
}
|
|
1926
|
+
|
|
1927
|
+
/**
|
|
1928
|
+
* [2.0.4] 验收文本里是否带否定/禁止语气。
|
|
1929
|
+
*
|
|
1930
|
+
* 「不得超过 5%」与「超过 5%」的二元组相似度可以很高,但语义相反。
|
|
1931
|
+
* 相似度只用来判「是不是同一条」,判不了「是不是同一个约束」,所以要单独
|
|
1932
|
+
* 把否定词拎出来比较:两边不一致就直接判不覆盖,不交给阈值碰运气。
|
|
1933
|
+
*/
|
|
1934
|
+
export function hasNegation(text: string) {
|
|
1935
|
+
return /不得|禁止|严禁|不能|不可|不允许|不应|无需|避免|排除|不超过|不低于|不高于|不等于|未通过|未完成/.test(String(text ?? ''))
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1758
1938
|
export function acceptanceCoverage(expected: any, actual: any) {
|
|
1759
1939
|
const want = (Array.isArray(expected) ? expected : []).map(normaliseCriterion).filter(Boolean)
|
|
1760
1940
|
const got = (Array.isArray(actual) ? actual : [])
|
|
@@ -1779,7 +1959,25 @@ export function acceptanceCoverage(expected: any, actual: any) {
|
|
|
1779
1959
|
if (row === criterion) return true
|
|
1780
1960
|
if (idPattern.test(row)) return true
|
|
1781
1961
|
if (Math.min(row.length, criterion.length) < 6) return false
|
|
1782
|
-
|
|
1962
|
+
if (row.includes(criterion)) return true
|
|
1963
|
+
/*
|
|
1964
|
+
* [2.0.4] 同义改写必须算同一条验收(用户 m00302 / 2026-11-05)。
|
|
1965
|
+
*
|
|
1966
|
+
* 1.12.0 只认「证据原文包含计划原文」,于是主 Agent 把
|
|
1967
|
+
* 「数字与 T9 逐项一致」写成「数字与 T9 一致」就会被判成「没回应」,
|
|
1968
|
+
* 交付门禁反复重开。这不是证据不合格,是**字面匹配**在惩罚正常表述。
|
|
1969
|
+
*
|
|
1970
|
+
* [2.0.4] 补充规则(阈值与注释一致,不再出现 2/3 对 0.65、0.75 对 0.70):
|
|
1971
|
+
* 1. 证据几乎抄全了计划原文(≥85%)→ 覆盖;
|
|
1972
|
+
* 2. 字符二元组 Dice 相似度 ≥ 0.70,且两边否定词状态一致 → 覆盖。
|
|
1973
|
+
* 否定词必须单独看:T4 用真实差异对复现出「删掉『不得超过』」这种
|
|
1974
|
+
* 改写会被纯相似度放过,那是把约束反过来的假阳性,必须挡住。
|
|
1975
|
+
* 只回应一半的情况(如「登录测试」对「登录测试与越权测试」,Dice 0.667)
|
|
1976
|
+
* 仍然不覆盖,Q6/U-16 的本意不变。
|
|
1977
|
+
*/
|
|
1978
|
+
if (criterion.includes(row) && row.length / criterion.length >= 0.85) return true
|
|
1979
|
+
if (hasNegation(criterion) !== hasNegation(row)) return false
|
|
1980
|
+
return criterionSimilarity(row, criterion) >= 0.7
|
|
1783
1981
|
})
|
|
1784
1982
|
if (!covered) missing.push(String((Array.isArray(expected) ? expected : [])[index] ?? criterion))
|
|
1785
1983
|
}
|
|
@@ -1920,6 +2118,46 @@ function readEvidenceOptions(value: any) {
|
|
|
1920
2118
|
.filter(Boolean)
|
|
1921
2119
|
}
|
|
1922
2120
|
|
|
2121
|
+
/**
|
|
2122
|
+
* [2.1.0] 顶层证据字段被误写进 `phases` 时,把它们提回顶层(用户 2026-11-05)。
|
|
2123
|
+
*
|
|
2124
|
+
* 事故(子会话 13b45af6 原文复核):子 Agent 的回执像这样——
|
|
2125
|
+
* {"status":"done","phases":{"planning":{…},"execution":{…},"verification":{…},
|
|
2126
|
+
* "acceptance":[…4 条…],"artifacts":[…]}}
|
|
2127
|
+
* 少了一个 `}`,于是 acceptance/artifacts 全部落进 `phases`。`closeOpenJson`
|
|
2128
|
+
* 在末尾补一个 `}` 后 JSON 合法,`readEvidenceAcceptance(payload.acceptance)`
|
|
2129
|
+
* 读到空数组,最终判成「验收项未被逐条回应(0/4)」——证据一条不缺,却被
|
|
2130
|
+
* 说成「没做验收」,且把责任推回子 Agent。这不是证据问题,是层级错位。
|
|
2131
|
+
*
|
|
2132
|
+
* 这里只做**位置归位**:字段名与值原样搬回顶层,不改内容、不补内容;顶层
|
|
2133
|
+
* 已有非空值时不覆盖(避免把真值盖掉)。归位后仍走完整门禁。
|
|
2134
|
+
*/
|
|
2135
|
+
const HOISTABLE_EVIDENCE_KEYS = ['acceptance', 'artifacts', 'needs', 'question', 'options'] as const
|
|
2136
|
+
|
|
2137
|
+
export function hoistEvidenceFields(payload: any) {
|
|
2138
|
+
if (!isRecord(payload) || !isRecord(payload.phases)) return { payload, hoisted: [] as string[] }
|
|
2139
|
+
const phases: any = payload.phases
|
|
2140
|
+
const next: any = { ...payload, phases: { ...phases } }
|
|
2141
|
+
const hoisted: string[] = []
|
|
2142
|
+
for (const key of HOISTABLE_EVIDENCE_KEYS) {
|
|
2143
|
+
const top = next[key]
|
|
2144
|
+
const topEmpty = top === undefined || top === null
|
|
2145
|
+
|| (Array.isArray(top) && top.length === 0)
|
|
2146
|
+
|| (typeof top === 'string' && top.trim() === '')
|
|
2147
|
+
if (!topEmpty) continue
|
|
2148
|
+
if (!(key in phases)) continue
|
|
2149
|
+
const value = phases[key]
|
|
2150
|
+
const hasValue = Array.isArray(value)
|
|
2151
|
+
? value.length > 0
|
|
2152
|
+
: typeof value === 'string' ? value.trim() !== '' : false
|
|
2153
|
+
if (!hasValue) continue
|
|
2154
|
+
next[key] = value
|
|
2155
|
+
delete (next.phases as any)[key]
|
|
2156
|
+
hoisted.push(key)
|
|
2157
|
+
}
|
|
2158
|
+
return hoisted.length > 0 ? { payload: next, hoisted } : { payload, hoisted }
|
|
2159
|
+
}
|
|
2160
|
+
|
|
1923
2161
|
/**
|
|
1924
2162
|
* Parse the child's structured evidence block.
|
|
1925
2163
|
*
|
|
@@ -1928,22 +2166,172 @@ function readEvidenceOptions(value: any) {
|
|
|
1928
2166
|
* A malformed block is reported as such instead of being silently ignored —
|
|
1929
2167
|
* "cannot tell" must never default to "done".
|
|
1930
2168
|
*/
|
|
2169
|
+
/**
|
|
2170
|
+
* [2.0.4] 把一段被截断的 JSON 补成可解析的对象(用户 m00302)。
|
|
2171
|
+
*
|
|
2172
|
+
* 事故(T1 报告 §2.3 复核后的真实形态):5 个子 Agent 的产物都已落盘,
|
|
2173
|
+
* 其中 3 项(T4/T6/T10)的回执**结尾各少一个 `}`**,2 项(T5/T7)JSON 合法
|
|
2174
|
+
* 但漏写规定标记。旧实现把前者判成「无法解析」、后者判成「未提供证据块」,
|
|
2175
|
+
* 证据在盘上,判定却拿不到,只能人工兜底。
|
|
2176
|
+
*
|
|
2177
|
+
* 注意:早期口头归因写的「内容太长被截断」是错的——会话里 3000 字符是
|
|
2178
|
+
* 结果写回时的展示上限,不是解析输入;同样被判 done 的 T1/T2/T3 也被截到
|
|
2179
|
+
* 3000 字却解析正常。这里做的是**有界修复**,不是放宽验收:
|
|
2180
|
+
* - 只补结构(收尾引号、闭合括号),绝不补内容,也不改任何字段值;
|
|
2181
|
+
* - 修复后的对象仍然走完整的 status/acceptance/phases 门禁;
|
|
2182
|
+
* - 修不好就如实返回 null,绝不用「猜」的内容顶替。
|
|
2183
|
+
*/
|
|
2184
|
+
function closeOpenJson(text: string): string | null {
|
|
2185
|
+
const chars = Array.from(text)
|
|
2186
|
+
const stack: string[] = []
|
|
2187
|
+
let inString = false
|
|
2188
|
+
let escaped = false
|
|
2189
|
+
for (const char of chars) {
|
|
2190
|
+
if (inString) {
|
|
2191
|
+
if (escaped) escaped = false
|
|
2192
|
+
else if (char === '\\') escaped = true
|
|
2193
|
+
else if (char === '"') inString = false
|
|
2194
|
+
continue
|
|
2195
|
+
}
|
|
2196
|
+
if (char === '"') inString = true
|
|
2197
|
+
else if (char === '{') stack.push('}')
|
|
2198
|
+
else if (char === '[') stack.push(']')
|
|
2199
|
+
else if (char === '}' || char === ']') {
|
|
2200
|
+
if (stack.pop() !== char) return null
|
|
2201
|
+
}
|
|
2202
|
+
}
|
|
2203
|
+
let closed = text
|
|
2204
|
+
if (inString) closed += '"'
|
|
2205
|
+
while (stack.length > 0) closed += stack.pop()
|
|
2206
|
+
return closed
|
|
2207
|
+
}
|
|
2208
|
+
|
|
2209
|
+
/** 最后一个「切掉它之后仍可能闭合」的位置:字符串外的逗号或左括号。 */
|
|
2210
|
+
function lastSafeCut(text: string): number {
|
|
2211
|
+
const chars = Array.from(text)
|
|
2212
|
+
let inString = false
|
|
2213
|
+
let escaped = false
|
|
2214
|
+
let cut = -1
|
|
2215
|
+
for (let i = 0; i < chars.length; i += 1) {
|
|
2216
|
+
const char = chars[i]
|
|
2217
|
+
if (inString) {
|
|
2218
|
+
if (escaped) escaped = false
|
|
2219
|
+
else if (char === '\\') escaped = true
|
|
2220
|
+
else if (char === '"') inString = false
|
|
2221
|
+
continue
|
|
2222
|
+
}
|
|
2223
|
+
if (char === '"') inString = true
|
|
2224
|
+
else if (char === ',' || char === '{' || char === '[') cut = inString ? cut : i
|
|
2225
|
+
}
|
|
2226
|
+
return cut
|
|
2227
|
+
}
|
|
2228
|
+
|
|
2229
|
+
export function repairEvidenceJson(text: string): any {
|
|
2230
|
+
let body = String(text ?? '').trim()
|
|
2231
|
+
for (let attempt = 0; attempt < 24 && body.length > 0; attempt += 1) {
|
|
2232
|
+
const closed = closeOpenJson(body)
|
|
2233
|
+
if (closed) {
|
|
2234
|
+
try {
|
|
2235
|
+
const value = JSON.parse(closed)
|
|
2236
|
+
if (isRecord(value)) return value
|
|
2237
|
+
} catch {
|
|
2238
|
+
/* 继续往前切 */
|
|
2239
|
+
}
|
|
2240
|
+
}
|
|
2241
|
+
const cut = lastSafeCut(body)
|
|
2242
|
+
if (cut <= 0) return null
|
|
2243
|
+
body = body.slice(0, cut)
|
|
2244
|
+
}
|
|
2245
|
+
return null
|
|
2246
|
+
}
|
|
2247
|
+
|
|
2248
|
+
/**
|
|
2249
|
+
* [2.0.4] 没写规定标记、但内容其实交齐了的回执(用户 m00302)。
|
|
2250
|
+
*
|
|
2251
|
+
* 子会话原文核查(T1 报告 §2.3)显示:T5/T7 的证据块 **JSON 完全合法**,
|
|
2252
|
+
* status/phases/acceptance/artifacts 一样不缺,只是被放进了 Markdown 代码块,
|
|
2253
|
+
* 没有写 `WORKPLAN_EVIDENCE:START/END`。旧实现直接判「结果块缺失」,把
|
|
2254
|
+
* 「格式没按规定」说成「没交作业」。
|
|
2255
|
+
*
|
|
2256
|
+
* 这里做的是**有界认领**:只认带 `status` 且至少还有一个证据字段的 JSON
|
|
2257
|
+
* 对象,绝不把任意 JSON(比如计划载荷、配置片段)当成回执。
|
|
2258
|
+
*/
|
|
2259
|
+
function rescueEvidence(output: any): { payload: any; raw: string; repaired: boolean } | null {
|
|
2260
|
+
const text = typeof output === 'string' ? output : ''
|
|
2261
|
+
if (!text.includes('{')) return null
|
|
2262
|
+
const candidates: string[] = []
|
|
2263
|
+
const fence = /```(?:json|JSON)?\s*\n([\s\S]*?)```/g
|
|
2264
|
+
let match: RegExpExecArray | null
|
|
2265
|
+
while ((match = fence.exec(text)) !== null) candidates.push(match[1])
|
|
2266
|
+
const first = text.indexOf('{')
|
|
2267
|
+
const last = text.lastIndexOf('}')
|
|
2268
|
+
if (first >= 0 && last > first) candidates.push(text.slice(first, last + 1))
|
|
2269
|
+
for (const candidate of candidates) {
|
|
2270
|
+
const body = String(candidate).trim()
|
|
2271
|
+
let value: any = null
|
|
2272
|
+
let repaired = false
|
|
2273
|
+
try {
|
|
2274
|
+
value = JSON.parse(body)
|
|
2275
|
+
} catch {
|
|
2276
|
+
value = repairEvidenceJson(body)
|
|
2277
|
+
repaired = value !== null
|
|
2278
|
+
}
|
|
2279
|
+
if (!isRecord(value)) continue
|
|
2280
|
+
if (!EVIDENCE_STATUSES.includes(value.status)) continue
|
|
2281
|
+
const hasEvidence = Array.isArray(value.acceptance) || Array.isArray(value.artifacts)
|
|
2282
|
+
|| Array.isArray(value.needs) || isRecord(value.phases) || typeof value.summary === 'string'
|
|
2283
|
+
if (!hasEvidence) continue
|
|
2284
|
+
return { payload: value, raw: body, repaired }
|
|
2285
|
+
}
|
|
2286
|
+
return null
|
|
2287
|
+
}
|
|
2288
|
+
|
|
2289
|
+
export function rescueEvidenceFromFence(output: any): any {
|
|
2290
|
+
const rescued = rescueEvidence(output)
|
|
2291
|
+
return rescued ? rescued.payload : null
|
|
2292
|
+
}
|
|
2293
|
+
|
|
1931
2294
|
export function parseEvidenceBlock(output: any) {
|
|
1932
2295
|
const text = typeof output === 'string' ? output : ''
|
|
1933
2296
|
const begin = text.lastIndexOf(EVIDENCE_MARKER_BEGIN)
|
|
1934
|
-
if (begin < 0)
|
|
1935
|
-
|
|
1936
|
-
|
|
1937
|
-
|
|
1938
|
-
|
|
1939
|
-
|
|
1940
|
-
|
|
2297
|
+
if (begin < 0) {
|
|
2298
|
+
const rescued = rescueEvidence(text)
|
|
2299
|
+
if (rescued) {
|
|
2300
|
+
return {
|
|
2301
|
+
found: true,
|
|
2302
|
+
malformed: false,
|
|
2303
|
+
truncated: false,
|
|
2304
|
+
repaired: rescued.repaired,
|
|
2305
|
+
rescued: true,
|
|
2306
|
+
payload: rescued.payload,
|
|
2307
|
+
raw: rescued.raw,
|
|
2308
|
+
}
|
|
2309
|
+
}
|
|
2310
|
+
return { found: false, malformed: false, truncated: false, repaired: false, rescued: false, payload: null, raw: '' }
|
|
2311
|
+
}
|
|
2312
|
+
const bodyStart = begin + EVIDENCE_MARKER_BEGIN.length
|
|
2313
|
+
const end = text.indexOf(EVIDENCE_MARKER_END, bodyStart)
|
|
2314
|
+
/*
|
|
2315
|
+
* [2.0.4] 结束标记缺失 = 输出被截断,不是「格式错误」。
|
|
2316
|
+
*
|
|
2317
|
+
* 旧实现遇到这种情况直接判 malformed,还把 `truncated` 写成 false,于是
|
|
2318
|
+
* 上层拿到的是「无法解析」,看不出真正原因。现在先把标记之后的全部内容
|
|
2319
|
+
* 当作候选块,交给有界修复;修得回来就是可用证据,修不回来才报截断。
|
|
2320
|
+
*/
|
|
2321
|
+
const raw = (end < 0 ? text.slice(bodyStart) : text.slice(bodyStart, end)).trim()
|
|
2322
|
+
const truncated = end < 0 || raw.length > MAX_EVIDENCE_BLOCK_CHARS
|
|
2323
|
+
const accept = (payload: any, repaired: boolean) => {
|
|
1941
2324
|
if (!isRecord(payload) || !EVIDENCE_STATUSES.includes(payload.status)) {
|
|
1942
|
-
return { found: true, malformed: true, truncated, payload: null, raw }
|
|
2325
|
+
return { found: true, malformed: true, truncated, repaired, rescued: false, payload: null, raw }
|
|
1943
2326
|
}
|
|
1944
|
-
return { found: true, malformed: false, truncated, payload, raw }
|
|
2327
|
+
return { found: true, malformed: false, truncated, repaired, rescued: false, payload, raw }
|
|
2328
|
+
}
|
|
2329
|
+
try {
|
|
2330
|
+
return accept(JSON.parse(raw), false)
|
|
1945
2331
|
} catch {
|
|
1946
|
-
|
|
2332
|
+
const repaired = repairEvidenceJson(raw)
|
|
2333
|
+
if (repaired) return accept(repaired, true)
|
|
2334
|
+
return { found: true, malformed: true, truncated, repaired: false, payload: null, raw }
|
|
1947
2335
|
}
|
|
1948
2336
|
}
|
|
1949
2337
|
|
|
@@ -1979,7 +2367,17 @@ export function adjudicateResult({ output, strict = true, expectedAcceptance = [
|
|
|
1979
2367
|
acceptance: [],
|
|
1980
2368
|
artifacts: [],
|
|
1981
2369
|
needs: [],
|
|
1982
|
-
|
|
2370
|
+
/*
|
|
2371
|
+
* [2.0.4] 失败信息必须能指导下一步动作(用户 m00302)。
|
|
2372
|
+
*
|
|
2373
|
+
* 「无法解析,请主 Agent 核对」把诊断责任推回给人:既不说是不是截断,
|
|
2374
|
+
* 也不说该重发还是该改格式,于是只能靠读原始输出猜。现在按截断/非法
|
|
2375
|
+
* 分档给出可执行提示。
|
|
2376
|
+
*/
|
|
2377
|
+
error: parsed.truncated
|
|
2378
|
+
? '结果块被截断(缺少结束标记或超过长度上限):请让该子 Agent 重新回报,'
|
|
2379
|
+
+ '只保留 status/summary/acceptance/artifacts/needs 字段,删掉大段执行日志后重发'
|
|
2380
|
+
: '结果块 JSON 非法(不是截断):请检查引号、逗号与括号是否成对,修正后重新回报',
|
|
1983
2381
|
}
|
|
1984
2382
|
}
|
|
1985
2383
|
if (!parsed.found) {
|
|
@@ -2012,7 +2410,8 @@ export function adjudicateResult({ output, strict = true, expectedAcceptance = [
|
|
|
2012
2410
|
error: '子 Agent 未提供结构化证据块(完成必回报):结果块缺失,无法确认验收项是否真的通过',
|
|
2013
2411
|
}
|
|
2014
2412
|
}
|
|
2015
|
-
const
|
|
2413
|
+
const hoistedEvidence = hoistEvidenceFields(parsed.payload)
|
|
2414
|
+
const payload: any = hoistedEvidence.payload
|
|
2016
2415
|
const acceptance = readEvidenceAcceptance(payload.acceptance)
|
|
2017
2416
|
const artifacts = readEvidenceArtifacts(payload.artifacts)
|
|
2018
2417
|
const needs = readEvidenceNeeds(payload.needs)
|
|
@@ -2087,6 +2486,19 @@ export function adjudicateResult({ output, strict = true, expectedAcceptance = [
|
|
|
2087
2486
|
*/
|
|
2088
2487
|
const coverage = acceptanceCoverage(expectedAcceptance, acceptance)
|
|
2089
2488
|
if (coverage.total > 0 && coverage.missing.length > 0) {
|
|
2489
|
+
/*
|
|
2490
|
+
* [2.1.0] 先把「为什么没读到」说清楚,再要求补证据。
|
|
2491
|
+
*
|
|
2492
|
+
* 旧文案只有一句「验收项未被逐条回应」,于是「真没回」「层级写错」
|
|
2493
|
+
* 「被截断」三种完全不同的原因长得一模一样,主 Agent 只能反复重派。
|
|
2494
|
+
*/
|
|
2495
|
+
const rawText = String(parsed.raw ?? '')
|
|
2496
|
+
const rawHasAcceptance = /"acceptance"\s*:/.test(rawText)
|
|
2497
|
+
const hint = acceptance.length === 0 && rawHasAcceptance
|
|
2498
|
+
? '(原始结果块里确实写了 acceptance,但没有落在顶层:通常是 phases 少一个 } 导致层级错位,已尝试自动归位仍未读到)'
|
|
2499
|
+
: parsed.repaired
|
|
2500
|
+
? '(结果块 JSON 有语法错误,已修复;若 acceptance 位于错误点之后,会被一起裁掉)'
|
|
2501
|
+
: ''
|
|
2090
2502
|
return {
|
|
2091
2503
|
status: 'blocked',
|
|
2092
2504
|
verification: 'unverified',
|
|
@@ -2094,7 +2506,7 @@ export function adjudicateResult({ output, strict = true, expectedAcceptance = [
|
|
|
2094
2506
|
artifacts,
|
|
2095
2507
|
needs,
|
|
2096
2508
|
error: `验收项未被逐条回应(${coverage.covered}/${coverage.total}):缺少 ${coverage.missing
|
|
2097
|
-
.map((item: string) => truncate(String(item), 120)).slice(0, 5).join(';')}。请按计划验收逐条给出证据后再回报。`,
|
|
2509
|
+
.map((item: string) => truncate(String(item), 120)).slice(0, 5).join(';')}${hint}。请按计划验收逐条给出证据后再回报。`,
|
|
2098
2510
|
}
|
|
2099
2511
|
}
|
|
2100
2512
|
/*
|
|
@@ -2121,6 +2533,46 @@ export function adjudicateResult({ output, strict = true, expectedAcceptance = [
|
|
|
2121
2533
|
phases: null,
|
|
2122
2534
|
}
|
|
2123
2535
|
}
|
|
2536
|
+
/*
|
|
2537
|
+
* [2.1.0] 沟通交流格式门禁(用户 2026-11-06 的强制要求,契约见
|
|
2538
|
+
* `src/comm-format.ts`)。
|
|
2539
|
+
*
|
|
2540
|
+
* 主 Agent 派发任务时随提示词附带《沟通交流格式规范》(renderCommFormatSpec),
|
|
2541
|
+
* 部门人员提交前须逐条自查;这里按同一份规范对「其它门禁都已通过、
|
|
2542
|
+
* 即将判 done」的结果做最终核验:任一不合规即退回(blocked/comm-format),
|
|
2543
|
+
* 不合规条目随下一次激活的 previousFailure 发回,通知其重新自查后重交。
|
|
2544
|
+
*
|
|
2545
|
+
* 只拦 done 路径:解析失败、验收缺口、三阶段缺失等已被上方门禁拦下的结果,
|
|
2546
|
+
* 维持原受阻原因,不用格式条目覆盖更有针对性的诊断。
|
|
2547
|
+
*/
|
|
2548
|
+
const commFormat = requirePhases === false
|
|
2549
|
+
/*
|
|
2550
|
+
* [2.1.0] 兼容边界:`requirePhases:false` 是「历史回放 / 明确关闭新契约」
|
|
2551
|
+
* 的既有开关(1.13.0 语义)。格式契约只对实时执行生效,不得追溯否决
|
|
2552
|
+
* 旧会话日志里按当时规则合法的 done。
|
|
2553
|
+
*/
|
|
2554
|
+
? { compliant: true as const, violations: [], version: 0 }
|
|
2555
|
+
: checkCommFormatCompliance(
|
|
2556
|
+
payload,
|
|
2557
|
+
text,
|
|
2558
|
+
Array.isArray(expectedAcceptance) ? expectedAcceptance.length : 0,
|
|
2559
|
+
)
|
|
2560
|
+
if (!commFormat.compliant) {
|
|
2561
|
+
return {
|
|
2562
|
+
status: 'blocked',
|
|
2563
|
+
verification: 'unverified',
|
|
2564
|
+
acceptance,
|
|
2565
|
+
artifacts,
|
|
2566
|
+
needs,
|
|
2567
|
+
error: `沟通交流格式不合规,结果已退回重交:${commFormat.violations
|
|
2568
|
+
.map((v) => `${v.ruleId} ${v.message}`)
|
|
2569
|
+
.slice(0, 6)
|
|
2570
|
+
.join(';')}`,
|
|
2571
|
+
blockedKind: 'comm-format',
|
|
2572
|
+
commFormatViolations: commFormat.violations,
|
|
2573
|
+
commFormatRejection: renderCommFormatRejection(commFormat),
|
|
2574
|
+
}
|
|
2575
|
+
}
|
|
2124
2576
|
return {
|
|
2125
2577
|
status: 'done',
|
|
2126
2578
|
verification: hasUnverified ? 'unverified' : 'structured',
|
|
@@ -2603,7 +3055,22 @@ export function applyStatus(statuses: any, subtaskId: any, patch: any) {
|
|
|
2603
3055
|
next.updatedBy = ['child', 'agent', 'system'].includes(patch.updatedBy) ? patch.updatedBy : 'system'
|
|
2604
3056
|
if (patch.status === 'done') {
|
|
2605
3057
|
next.progress = 100
|
|
2606
|
-
|
|
3058
|
+
/*
|
|
3059
|
+
* [2.0.4] 「已回报」是事实,不是阶段(用户 m00302 / 2026-11-05)。
|
|
3060
|
+
*
|
|
3061
|
+
* `closure:'reported'` 的意思是「这条完成结果已经通过结果消息回报过」,
|
|
3062
|
+
* 它是交付门禁 `reportPending` 读的事实。主 Agent 事后复核证据、把
|
|
3063
|
+
* verification/acceptance 补齐时,status 仍然是 done —— 旧代码却把
|
|
3064
|
+
* closure 默认改回 `done`,于是撞上「非法闭环迁移:reported → done」,
|
|
3065
|
+
* 逼着人先写一次假的 blocked 再写 done。审计链被污染,界面还会短暂
|
|
3066
|
+
* 显示「受阻」。
|
|
3067
|
+
*
|
|
3068
|
+
* 现在:没有显式指定 closure 的复核写入,保留已回报事实;只有调用方
|
|
3069
|
+
* 明确要把它退回未回报时,迁移表才继续拒绝并在文案里给出出路。
|
|
3070
|
+
*/
|
|
3071
|
+
if (patch.closure === undefined) {
|
|
3072
|
+
next.closure = current.closure === 'reported' ? 'reported' : 'done'
|
|
3073
|
+
}
|
|
2607
3074
|
/*
|
|
2608
3075
|
* 一条已经完成的子任务不可能同时“在等指示”。旧的 question/options 是
|
|
2609
3076
|
* 上一段受阻经历的证据,不能继续驱动模型把它当成未决请示;失败原因本身
|
|
@@ -2743,7 +3210,8 @@ export function applyStatus(statuses: any, subtaskId: any, patch: any) {
|
|
|
2743
3210
|
&& !(CLOSURE_TRANSITIONS[String(previousClosure)] ?? []).includes(String(next.closure))
|
|
2744
3211
|
) {
|
|
2745
3212
|
throw new PlanError(
|
|
2746
|
-
`非法闭环迁移:${String(previousClosure)} → ${String(next.closure)}
|
|
3213
|
+
`非法闭环迁移:${String(previousClosure)} → ${String(next.closure)};`
|
|
3214
|
+
+ '「已回报」的行若要再次交付,请直接派发新一轮,不要把它退回未回报',
|
|
2747
3215
|
'INVALID_TRANSITION',
|
|
2748
3216
|
)
|
|
2749
3217
|
}
|
|
@@ -3876,5 +4344,16 @@ export function buildChildPrompt({
|
|
|
3876
4344
|
'- needs 写需要其他部门配合的事项,每条写清「哪个部门 + 需要什么 + 用来做什么」;它会被登记为待处理需求,主 Agent 必须处置后计划才能交付。不要直连其他子 Agent。',
|
|
3877
4345
|
'- question/options 只在受阻上报时填写;done 时保持空字符串/空数组。',
|
|
3878
4346
|
'- 整块不超过 12000 字符;不要把这段模板本身抄进结果块。',
|
|
4347
|
+
'',
|
|
4348
|
+
/*
|
|
4349
|
+
* [2.1.0] 派发工作时附带统一沟通交流格式要求(用户 2026-11-06 的强制要求)。
|
|
4350
|
+
*
|
|
4351
|
+
* 规范文本与主 Agent 核验结果用的是同一份数据(src/comm-format.ts),
|
|
4352
|
+
* 「附带的要求」与「核验的尺子」由此不可能长成两个版本。
|
|
4353
|
+
*/
|
|
4354
|
+
renderCommFormatSpec(),
|
|
4355
|
+
'提交前自查(强制):逐条对照上面《沟通交流格式规范》F1–F9,确认全部满足后再提交;'
|
|
4356
|
+
+ '主 Agent 收到结果后按同一份规范逐条核验,任一不合规即立刻退回,你会收到列明不合规条目的退回通知,'
|
|
4357
|
+
+ '须逐条修正、重新自查通过后再重新提交完整结果。',
|
|
3879
4358
|
].join('\n'))
|
|
3880
4359
|
}
|