@suifei/dsh-ai-team 0.4.143 → 0.4.144
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 +13 -0
- package/index.js +4 -3
- package/lib/skillopt/job-runner.js +5 -4
- package/lib/skillopt/judge.js +163 -143
- package/lib/skillopt/optimizer.js +183 -182
- package/lib/skillopt/reviewer.js +87 -85
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.4.144 - 2026-11-21
|
|
4
|
+
|
|
5
|
+
**SkillOpt:采纳闸门回归借自上游;评审记录把「评分解析失败」和「有效评分但没提升」分开**
|
|
6
|
+
|
|
7
|
+
- 夜间自优化(内置的 SkillOpt-Sleep 引擎)新增采纳闸门回归 `test/skillopt-sleep-gate.test.js`,借自上游 microsoft/SkillOpt 的 `TestVerifierDiscipline`(MIT):用真实的 `consolidate` 加脚本化后端跑三个场景,分别断言基线分、候选分和 `gate_action`,不只看最后有没有采纳。
|
|
8
|
+
- 奖励作弊:训练集 0→1、留出集 1→0,必须拒绝;
|
|
9
|
+
- 部分退化:训练集 0→1、留出集 1.0→0.5(两道题坏一道),必须拒绝;
|
|
10
|
+
- 真提升:训练集与留出集都 0→1,必须采纳为新最佳。
|
|
11
|
+
- 断言里写明一个容易看错的地方:拒绝后引擎回滚再测,顶层 `candidate_score` 等于基线;候选的真实分(0 / 0.5)只在 `gate_trials` 里。
|
|
12
|
+
- 变异实测:在临时副本里让闸门不看分数,三个场景的 `gate_action` 仍写 reject / accept_new_best,但 `accepted` 变成 true、试验里的候选被采纳。只看 `gate_action` 或只看最后结果都发现不了,这条回归能发现。
|
|
13
|
+
- 文渊阁自己的成对评审(优化循环、异步任务、评审卡三处写 `results.tsv`):以前评审输出解析不出、超时或缺票,与「三票都有效、评审认为没提升」都写 `revert`,看记录分不出是评审坏了还是改动没用。现在 status 列分三种:`keep`、`revert`(有效评分但没提升)、`inconclusive`(有效票不足,多半是评分解析失败);note 在有无效票时附「unparsed 无效票数/总票数」,例如 `0-1 inconclusive · unparsed 2/3`;诊断事件 `skillopt.job.done` 带 `unparsed`。三处写记录共用 `judgeRecord` 一个函数。评审卡里解析不出的回帖以前直接跳过,现在按应投数量补成无效票计入。采纳判据不变:只有 `keep` 能放行。
|
|
14
|
+
- 测试:全量 481。
|
|
15
|
+
|
|
3
16
|
## 0.4.143 - 2026-11-20
|
|
4
17
|
|
|
5
18
|
**「建议恢复」里的成员按钮改为「恢复 白砚」,直接打开成员资料窗**
|
package/index.js
CHANGED
|
@@ -51,6 +51,7 @@ import { HANDOFF_DIR, stepHandoff } from './lib/skillopt/handoff-answerer.js';
|
|
|
51
51
|
import { execFile } from 'node:child_process';
|
|
52
52
|
import { collectReview, startReview } from './lib/skillopt/review-runner.js';
|
|
53
53
|
import { REVIEWER_NAME } from './lib/skillopt/reviewer.js';
|
|
54
|
+
import { judgeRecord } from './lib/skillopt/judge.js';
|
|
54
55
|
import { createMemberPermissionGuard } from './lib/host/permission-guard.js';
|
|
55
56
|
import { installFaultAdapter } from './lib/host/fault-adapter.js';
|
|
56
57
|
import { buildModelCatalog, buildModelEfforts } from './lib/host/model-catalog.js';
|
|
@@ -1939,9 +1940,9 @@ export function apply(ctx, config = {}) {
|
|
|
1939
1940
|
job.skillName,
|
|
1940
1941
|
String(beforeScore ?? '-'),
|
|
1941
1942
|
String(afterScore ?? '-'),
|
|
1942
|
-
|
|
1943
|
+
judgeRecord(collected.decision).status,
|
|
1943
1944
|
job.dimension ?? '-',
|
|
1944
|
-
|
|
1945
|
+
judgeRecord(collected.decision).note,
|
|
1945
1946
|
'paired',
|
|
1946
1947
|
sha12(job.candidate),
|
|
1947
1948
|
].join('\t'));
|
|
@@ -1949,7 +1950,7 @@ export function apply(ctx, config = {}) {
|
|
|
1949
1950
|
// 评审卡必须收口:否则它保持 pending 进入 PMO 的 active 集合,
|
|
1950
1951
|
// 会被 heartbeat/stall/escalate 反复唤醒评审官(每 job 永久留一张卡)。
|
|
1951
1952
|
await closeReviewCard(runtime, job.reviewTaskId, `blind review collected: ${collected.decision.verdict}`);
|
|
1952
|
-
diag.append({ ev: 'skillopt.job.done', jobId: job.id, verdict: collected.decision.verdict, better: collected.decision.better, worse: collected.decision.worse, kept, source: 'reviewer-card' });
|
|
1953
|
+
diag.append({ ev: 'skillopt.job.done', jobId: job.id, verdict: collected.decision.verdict, better: collected.decision.better, worse: collected.decision.worse, unparsed: collected.decision.unparsed, kept, source: 'reviewer-card' });
|
|
1953
1954
|
}
|
|
1954
1955
|
if (jobs.length === 0) return '[skillopt jobs] none';
|
|
1955
1956
|
// 重新读一遍再打印:本次调用刚把某些 job 判成 keep/revert/done,
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
import { readFileSync, writeFileSync } from 'node:fs';
|
|
8
8
|
import { extractAssistantText } from './subagent-text.js';
|
|
9
9
|
import { isChildSettled, planJobStep, readJob, writeJob, listJobs } from './jobs.js';
|
|
10
|
-
import { decideByMajority, normalizeVoteCount, parseJudgeOutput, buildPairPrompt, voteFromParsed } from './judge.js';
|
|
10
|
+
import { decideByMajority, judgeRecord, normalizeVoteCount, parseJudgeOutput, buildPairPrompt, voteFromParsed } from './judge.js';
|
|
11
11
|
import { validateCandidate } from './optimizer.js';
|
|
12
12
|
import { sha12 } from './proposal-store.js';
|
|
13
13
|
|
|
@@ -131,19 +131,20 @@ export async function stepJob(input) {
|
|
|
131
131
|
const afterText = kept ? job.candidate : job.before;
|
|
132
132
|
const beforeScore = deps.score(job.before)?.total ?? null;
|
|
133
133
|
const afterScore = deps.score(afterText)?.total ?? null;
|
|
134
|
+
const record = judgeRecord(decision);
|
|
134
135
|
await deps.appendResult([
|
|
135
136
|
new Date(deps.now()).toISOString().slice(0, 19),
|
|
136
137
|
deps.vcs.kind === 'git' ? (job.commit ?? '-') : '-',
|
|
137
138
|
job.skillName,
|
|
138
139
|
String(beforeScore ?? '-'),
|
|
139
140
|
String(afterScore ?? '-'),
|
|
140
|
-
|
|
141
|
+
record.status,
|
|
141
142
|
job.dimension ?? '-',
|
|
142
|
-
|
|
143
|
+
record.note,
|
|
143
144
|
'paired',
|
|
144
145
|
sha12(job.candidate),
|
|
145
146
|
].join('\t'));
|
|
146
|
-
deps.diag?.({ ev: 'skillopt.job.done', jobId: job.id, verdict: decision.verdict, better: decision.better, worse: decision.worse, kept });
|
|
147
|
+
deps.diag?.({ ev: 'skillopt.job.done', jobId: job.id, verdict: decision.verdict, better: decision.better, worse: decision.worse, unparsed: decision.unparsed, kept });
|
|
147
148
|
}
|
|
148
149
|
}
|
|
149
150
|
// 失败/完成是终态:plan 是在 switch 之前算的,若拿它回写就会把刚判死的 job
|
package/lib/skillopt/judge.js
CHANGED
|
@@ -1,143 +1,163 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Darwin Skill 2.1 的 keep/revert 判据:**paired 同-judge 比较 + 奇数 N 多数决**。
|
|
3
|
-
*
|
|
4
|
-
* 为什么不比绝对分数:LLM judge 的绝对分住在「文字 × 该 judge 当下的标准尺」
|
|
5
|
-
* 里,不是文字属性——同一份未改文字换 judge 可摆 ±8,保守编辑的真实增益
|
|
6
|
-
* (+3~8) 会被换尺噪音淹没(darwin 实测 false-revert 来源)。paired 让同一个
|
|
7
|
-
* judge 在一次调用内读两版,用同一把不准的尺量两版,比较时误差相减抵销
|
|
8
|
-
* (within-judge cancellation)。
|
|
9
|
-
*
|
|
10
|
-
* 本模块只含协议与纯函数:A/B 随机化、prompt 构造、多数决、tie/margin 聚合。
|
|
11
|
-
* 真正的 judge 是**独立子代理**(不在优化器上下文里自评自改)——由调用方注入。
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/** 三次调用(奇数 N)的开销上限:N 必须是奇数,3 为默认,接近平局升 5。 */
|
|
15
|
-
export const DEFAULT_JUDGE_VOTES = 3;
|
|
16
|
-
|
|
17
|
-
const VERDICTS = new Set(['better', 'worse', 'tie']);
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* 盲评 prompt:不告诉 judge 哪版是"新版本",只看两版与 rubric 准则。
|
|
21
|
-
* @param {{before: string, after: string, order: 'ab'|'ba'}} input
|
|
22
|
-
* @returns {{prompt: string, mapping: {aIsAfter: boolean}}}
|
|
23
|
-
*/
|
|
24
|
-
export function buildPairPrompt({ before, after, order }) {
|
|
25
|
-
const aIsAfter = order === 'ba';
|
|
26
|
-
const versionA = aIsAfter ? after : before;
|
|
27
|
-
const versionB = aIsAfter ? before : after;
|
|
28
|
-
const prompt = [
|
|
29
|
-
'你是独立的技能质量评审(judge)。只做比较,不要改写任何文件。',
|
|
30
|
-
'',
|
|
31
|
-
'## 评估准则(Darwin 9 维 rubric)',
|
|
32
|
-
'结构:1 Frontmatter质量 7 · 2 工作流清晰度 12 · 3 失败模式编码 12 · 4 检查点设计 6 · 5 可执行具体性 18 · 6 资源整合度 4',
|
|
33
|
-
'效果:7 整体架构 12 · 8 实测表现 23',
|
|
34
|
-
'Meta:9 反例与黑名单 6',
|
|
35
|
-
'注意:明确写出失败分支(如果 X 失败 → Y)、显性检查点(🔴/STOP/CHECKPOINT)、具体参数与示例、反例清单者更优;',
|
|
36
|
-
'软化措辞(建议/可以考虑/根据情况/灵活把握/视情况而定)与空话(说白了/换句话说/首先其次/综上所述)是减分项。',
|
|
37
|
-
'',
|
|
38
|
-
'## 版本 A',
|
|
39
|
-
versionA,
|
|
40
|
-
'',
|
|
41
|
-
'## 版本 B',
|
|
42
|
-
versionB,
|
|
43
|
-
'',
|
|
44
|
-
'只输出一行 JSON:{"better":"A"|"B"|"tie","margin":"clear"|"slight","reason":"一句话理由"}',
|
|
45
|
-
].join('\n');
|
|
46
|
-
return { prompt, mapping: { aIsAfter } };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* 解析 judge 输出:容忍代码围栏与前后噪声,只认 better/margin/reason。
|
|
51
|
-
* @param {string} output
|
|
52
|
-
* @returns {{better: 'A'|'B'|'tie', margin: 'clear'|'slight', reason: string} | null}
|
|
53
|
-
*/
|
|
54
|
-
export function parseJudgeOutput(output) {
|
|
55
|
-
const text = typeof output === 'string' ? output : '';
|
|
56
|
-
// 逐个尝试文本里的 JSON 对象:模板回显/前置示例会让第一个 `{...}` 不是结论,
|
|
57
|
-
// 只解析第一处会在合规输出上直接判 null(等于丢掉这一票)。
|
|
58
|
-
const readObjectAt = (start) => {
|
|
59
|
-
let depth = 0;
|
|
60
|
-
let inString = false;
|
|
61
|
-
let escaped = false;
|
|
62
|
-
for (let index = start; index < text.length; index += 1) {
|
|
63
|
-
const char = text[index];
|
|
64
|
-
if (inString) {
|
|
65
|
-
if (escaped) escaped = false;
|
|
66
|
-
else if (char === '\\') escaped = true;
|
|
67
|
-
else if (char === '"') inString = false;
|
|
68
|
-
continue;
|
|
69
|
-
}
|
|
70
|
-
if (char === '"') { inString = true; continue; }
|
|
71
|
-
if (char === '{') depth += 1;
|
|
72
|
-
else if (char === '}') {
|
|
73
|
-
depth -= 1;
|
|
74
|
-
if (depth === 0) return text.slice(start, index + 1);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return null;
|
|
78
|
-
};
|
|
79
|
-
let found = null;
|
|
80
|
-
for (let at = text.indexOf('{'), attempts = 0; at !== -1 && attempts < 20; at = text.indexOf('{', at + 1), attempts += 1) {
|
|
81
|
-
const candidate = readObjectAt(at);
|
|
82
|
-
if (candidate === null) continue;
|
|
83
|
-
let row;
|
|
84
|
-
try { row = JSON.parse(candidate); } catch { continue; }
|
|
85
|
-
if (row?.better !== 'A' && row?.better !== 'B' && row?.better !== 'tie') continue;
|
|
86
|
-
// 取**最后**一个合法结论:模型常先回显模板或示例,真正的判决写在末尾。
|
|
87
|
-
found = {
|
|
88
|
-
better: row.better,
|
|
89
|
-
margin: row.margin === 'clear' ? 'clear' : 'slight',
|
|
90
|
-
reason: typeof row.reason === 'string' ? row.reason.slice(0, 300) : '',
|
|
91
|
-
// 评审通道的回帖带 orderId(这一票对应哪一个 A/B 顺序);judge 通道没有
|
|
92
|
-
// 这个字段,忽略即可。保留它才能让两条通道共用同一个解析器。
|
|
93
|
-
...(typeof row.orderId === 'string' && row.orderId !== '' ? { orderId: row.orderId } : {}),
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
return found;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* 把一次 judge 的输出转成「改后是否不差」的投票(keep 的依据)。
|
|
101
|
-
* @param {{better:string, margin:string, aIsAfter:boolean}|null} parsed
|
|
102
|
-
* @returns {'better'|'worse'|'tie'|null}
|
|
103
|
-
*/
|
|
104
|
-
export function voteFromParsed(parsed) {
|
|
105
|
-
if (parsed === null || parsed === undefined) return null;
|
|
106
|
-
if (parsed.better === 'tie') return 'tie';
|
|
107
|
-
const afterIsA = parsed.aIsAfter === true;
|
|
108
|
-
const afterWon = (parsed.better === 'A' && afterIsA) || (parsed.better === 'B' && !afterIsA);
|
|
109
|
-
return afterWon ? 'better' : 'worse';
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* 多数决:keep 当且仅当 better ≥ worse(含 tie)——与 darwin Phase 2 Step 5 一致。
|
|
114
|
-
* 无效投票(null)不计数;全部无效时判 'inconclusive'(不得据此 adopt)。
|
|
115
|
-
*
|
|
116
|
-
* 有效票**必须过半**:3 票里只剩 1 票 better、另 2 票超时/不可解析时,1-0
|
|
117
|
-
* 也是「better ≥ worse」,会把一次几乎没验成的改动判成 keep。
|
|
118
|
-
* @param {Array<'better'|'worse'|'tie'|null>} votes
|
|
119
|
-
* @param {{minValid?: number}} [options]
|
|
120
|
-
* @returns {{verdict: 'keep'|'revert'|'inconclusive', better:number, worse:number, ties:number, valid:number, required:number}}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
const
|
|
125
|
-
const
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Darwin Skill 2.1 的 keep/revert 判据:**paired 同-judge 比较 + 奇数 N 多数决**。
|
|
3
|
+
*
|
|
4
|
+
* 为什么不比绝对分数:LLM judge 的绝对分住在「文字 × 该 judge 当下的标准尺」
|
|
5
|
+
* 里,不是文字属性——同一份未改文字换 judge 可摆 ±8,保守编辑的真实增益
|
|
6
|
+
* (+3~8) 会被换尺噪音淹没(darwin 实测 false-revert 来源)。paired 让同一个
|
|
7
|
+
* judge 在一次调用内读两版,用同一把不准的尺量两版,比较时误差相减抵销
|
|
8
|
+
* (within-judge cancellation)。
|
|
9
|
+
*
|
|
10
|
+
* 本模块只含协议与纯函数:A/B 随机化、prompt 构造、多数决、tie/margin 聚合。
|
|
11
|
+
* 真正的 judge 是**独立子代理**(不在优化器上下文里自评自改)——由调用方注入。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** 三次调用(奇数 N)的开销上限:N 必须是奇数,3 为默认,接近平局升 5。 */
|
|
15
|
+
export const DEFAULT_JUDGE_VOTES = 3;
|
|
16
|
+
|
|
17
|
+
const VERDICTS = new Set(['better', 'worse', 'tie']);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 盲评 prompt:不告诉 judge 哪版是"新版本",只看两版与 rubric 准则。
|
|
21
|
+
* @param {{before: string, after: string, order: 'ab'|'ba'}} input
|
|
22
|
+
* @returns {{prompt: string, mapping: {aIsAfter: boolean}}}
|
|
23
|
+
*/
|
|
24
|
+
export function buildPairPrompt({ before, after, order }) {
|
|
25
|
+
const aIsAfter = order === 'ba';
|
|
26
|
+
const versionA = aIsAfter ? after : before;
|
|
27
|
+
const versionB = aIsAfter ? before : after;
|
|
28
|
+
const prompt = [
|
|
29
|
+
'你是独立的技能质量评审(judge)。只做比较,不要改写任何文件。',
|
|
30
|
+
'',
|
|
31
|
+
'## 评估准则(Darwin 9 维 rubric)',
|
|
32
|
+
'结构:1 Frontmatter质量 7 · 2 工作流清晰度 12 · 3 失败模式编码 12 · 4 检查点设计 6 · 5 可执行具体性 18 · 6 资源整合度 4',
|
|
33
|
+
'效果:7 整体架构 12 · 8 实测表现 23',
|
|
34
|
+
'Meta:9 反例与黑名单 6',
|
|
35
|
+
'注意:明确写出失败分支(如果 X 失败 → Y)、显性检查点(🔴/STOP/CHECKPOINT)、具体参数与示例、反例清单者更优;',
|
|
36
|
+
'软化措辞(建议/可以考虑/根据情况/灵活把握/视情况而定)与空话(说白了/换句话说/首先其次/综上所述)是减分项。',
|
|
37
|
+
'',
|
|
38
|
+
'## 版本 A',
|
|
39
|
+
versionA,
|
|
40
|
+
'',
|
|
41
|
+
'## 版本 B',
|
|
42
|
+
versionB,
|
|
43
|
+
'',
|
|
44
|
+
'只输出一行 JSON:{"better":"A"|"B"|"tie","margin":"clear"|"slight","reason":"一句话理由"}',
|
|
45
|
+
].join('\n');
|
|
46
|
+
return { prompt, mapping: { aIsAfter } };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 解析 judge 输出:容忍代码围栏与前后噪声,只认 better/margin/reason。
|
|
51
|
+
* @param {string} output
|
|
52
|
+
* @returns {{better: 'A'|'B'|'tie', margin: 'clear'|'slight', reason: string} | null}
|
|
53
|
+
*/
|
|
54
|
+
export function parseJudgeOutput(output) {
|
|
55
|
+
const text = typeof output === 'string' ? output : '';
|
|
56
|
+
// 逐个尝试文本里的 JSON 对象:模板回显/前置示例会让第一个 `{...}` 不是结论,
|
|
57
|
+
// 只解析第一处会在合规输出上直接判 null(等于丢掉这一票)。
|
|
58
|
+
const readObjectAt = (start) => {
|
|
59
|
+
let depth = 0;
|
|
60
|
+
let inString = false;
|
|
61
|
+
let escaped = false;
|
|
62
|
+
for (let index = start; index < text.length; index += 1) {
|
|
63
|
+
const char = text[index];
|
|
64
|
+
if (inString) {
|
|
65
|
+
if (escaped) escaped = false;
|
|
66
|
+
else if (char === '\\') escaped = true;
|
|
67
|
+
else if (char === '"') inString = false;
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (char === '"') { inString = true; continue; }
|
|
71
|
+
if (char === '{') depth += 1;
|
|
72
|
+
else if (char === '}') {
|
|
73
|
+
depth -= 1;
|
|
74
|
+
if (depth === 0) return text.slice(start, index + 1);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return null;
|
|
78
|
+
};
|
|
79
|
+
let found = null;
|
|
80
|
+
for (let at = text.indexOf('{'), attempts = 0; at !== -1 && attempts < 20; at = text.indexOf('{', at + 1), attempts += 1) {
|
|
81
|
+
const candidate = readObjectAt(at);
|
|
82
|
+
if (candidate === null) continue;
|
|
83
|
+
let row;
|
|
84
|
+
try { row = JSON.parse(candidate); } catch { continue; }
|
|
85
|
+
if (row?.better !== 'A' && row?.better !== 'B' && row?.better !== 'tie') continue;
|
|
86
|
+
// 取**最后**一个合法结论:模型常先回显模板或示例,真正的判决写在末尾。
|
|
87
|
+
found = {
|
|
88
|
+
better: row.better,
|
|
89
|
+
margin: row.margin === 'clear' ? 'clear' : 'slight',
|
|
90
|
+
reason: typeof row.reason === 'string' ? row.reason.slice(0, 300) : '',
|
|
91
|
+
// 评审通道的回帖带 orderId(这一票对应哪一个 A/B 顺序);judge 通道没有
|
|
92
|
+
// 这个字段,忽略即可。保留它才能让两条通道共用同一个解析器。
|
|
93
|
+
...(typeof row.orderId === 'string' && row.orderId !== '' ? { orderId: row.orderId } : {}),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return found;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* 把一次 judge 的输出转成「改后是否不差」的投票(keep 的依据)。
|
|
101
|
+
* @param {{better:string, margin:string, aIsAfter:boolean}|null} parsed
|
|
102
|
+
* @returns {'better'|'worse'|'tie'|null}
|
|
103
|
+
*/
|
|
104
|
+
export function voteFromParsed(parsed) {
|
|
105
|
+
if (parsed === null || parsed === undefined) return null;
|
|
106
|
+
if (parsed.better === 'tie') return 'tie';
|
|
107
|
+
const afterIsA = parsed.aIsAfter === true;
|
|
108
|
+
const afterWon = (parsed.better === 'A' && afterIsA) || (parsed.better === 'B' && !afterIsA);
|
|
109
|
+
return afterWon ? 'better' : 'worse';
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* 多数决:keep 当且仅当 better ≥ worse(含 tie)——与 darwin Phase 2 Step 5 一致。
|
|
114
|
+
* 无效投票(null)不计数;全部无效时判 'inconclusive'(不得据此 adopt)。
|
|
115
|
+
*
|
|
116
|
+
* 有效票**必须过半**:3 票里只剩 1 票 better、另 2 票超时/不可解析时,1-0
|
|
117
|
+
* 也是「better ≥ worse」,会把一次几乎没验成的改动判成 keep。
|
|
118
|
+
* @param {Array<'better'|'worse'|'tie'|null>} votes
|
|
119
|
+
* @param {{minValid?: number}} [options]
|
|
120
|
+
* @returns {{verdict: 'keep'|'revert'|'inconclusive', better:number, worse:number, ties:number, valid:number, unparsed:number, required:number}}
|
|
121
|
+
* unparsed = 没给出可用评分的票(输出解析不出、超时、缺票),与「有效评分但没提升」分开计。
|
|
122
|
+
*/
|
|
123
|
+
export function decideByMajority(votes, options = {}) {
|
|
124
|
+
const valid = votes.filter((vote) => vote === 'better' || vote === 'worse' || vote === 'tie');
|
|
125
|
+
const better = valid.filter((vote) => vote === 'better').length;
|
|
126
|
+
const worse = valid.filter((vote) => vote === 'worse').length;
|
|
127
|
+
const ties = valid.filter((vote) => vote === 'tie').length;
|
|
128
|
+
// 默认要求 ceil(n/2):3 票至少 2 票有效、5 票至少 3 票有效。
|
|
129
|
+
const required = Number.isSafeInteger(options.minValid)
|
|
130
|
+
? Math.max(1, options.minValid)
|
|
131
|
+
: Math.max(1, Math.ceil(Array.isArray(votes) ? votes.length / 2 : 1));
|
|
132
|
+
const unparsed = (Array.isArray(votes) ? votes.length : 0) - valid.length;
|
|
133
|
+
if (valid.length === 0) return { verdict: 'inconclusive', better, worse, ties, valid: 0, unparsed, required };
|
|
134
|
+
if (valid.length < required) return { verdict: 'inconclusive', better, worse, ties, valid: valid.length, unparsed, required };
|
|
135
|
+
return { verdict: better >= worse ? 'keep' : 'revert', better, worse, ties, valid: valid.length, unparsed, required };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* results.tsv 的 status 与 note(三处写记录共用)。用户 2026-11-21:排查评分时把「评分解析失败」和
|
|
140
|
+
* 「有效评分但没提升」分开记——以前两者都写 revert,看记录分不出是评审坏了还是改动真没用。
|
|
141
|
+
* - keep:有效票过半且 better ≥ worse
|
|
142
|
+
* - revert:有效票过半,评审认为改动没提升(真结论)
|
|
143
|
+
* - inconclusive:有效票不足,多半是评审输出解析不出/超时(评审坏了,不是改动的结论)
|
|
144
|
+
* note 在有无效票时附「unparsed 无效票数/总票数」。
|
|
145
|
+
* @param {{verdict:string, better:number, worse:number, valid:number, unparsed?:number}} decision
|
|
146
|
+
* @returns {{status:'keep'|'revert'|'inconclusive', note:string}}
|
|
147
|
+
*/
|
|
148
|
+
export function judgeRecord(decision) {
|
|
149
|
+
const status = decision?.verdict === 'keep' ? 'keep' : decision?.verdict === 'revert' ? 'revert' : 'inconclusive';
|
|
150
|
+
const unparsed = Number.isSafeInteger(decision?.unparsed) ? decision.unparsed : 0;
|
|
151
|
+
const total = (Number.isSafeInteger(decision?.valid) ? decision.valid : 0) + unparsed;
|
|
152
|
+
const note = `${decision?.better ?? 0}-${decision?.worse ?? 0} ${status}${unparsed > 0 ? ` · unparsed ${unparsed}/${total}` : ''}`;
|
|
153
|
+
return { status, note };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** 校验投票数:奇数且 ≥3(close call 可升 5)。 */
|
|
157
|
+
export function normalizeVoteCount(count) {
|
|
158
|
+
const value = Number.isSafeInteger(count) ? count : DEFAULT_JUDGE_VOTES;
|
|
159
|
+
if (value < 3) return 3;
|
|
160
|
+
return value % 2 === 1 ? value : value + 1;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export { VERDICTS };
|
|
@@ -1,182 +1,183 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Darwin Phase 2 自动优化循环(hill-climbing)——插件侧编排。
|
|
3
|
-
*
|
|
4
|
-
* 一轮 = 诊断(rubric 最大加权短板)→ 生成改进(独立生成器,非 judge)→
|
|
5
|
-
* 写入 → paired 独立 judge 多数决 → keep(保留)或 revert(回滚)→ 记录。
|
|
6
|
-
*
|
|
7
|
-
* 安全约束(对齐 darwin 与项目纪律):
|
|
8
|
-
* - 每轮只针对一个维度(避免多变量无法归因);
|
|
9
|
-
* - 体积上限 150%(超限直接拒绝,不进入 judge);
|
|
10
|
-
* - 棘轮用 paired 多数决(绝对分只做 triage);
|
|
11
|
-
* - 版本化:项目是干净 git 仓库时用 commit/revert,否则用 .bak 文件恢复;
|
|
12
|
-
* - 连续 2 轮 judge margin 全为 slight/tie → 触顶 break(见好就收,HL-4)。
|
|
13
|
-
*
|
|
14
|
-
* 纯逻辑与 I/O 分离:所有外部动作(读/写/git/生成/judge/评分/时钟)都注入,
|
|
15
|
-
* 因此循环可在测试里用一个内存文件系统完整验证。
|
|
16
|
-
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
if (gap
|
|
35
|
-
return
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (after ===
|
|
49
|
-
if (after
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* @param {
|
|
67
|
-
* @param {string}
|
|
68
|
-
* @param {
|
|
69
|
-
* @param {
|
|
70
|
-
* @param {(path:string
|
|
71
|
-
* @param {(
|
|
72
|
-
* @param {(
|
|
73
|
-
* @param {(input:{
|
|
74
|
-
* @param {
|
|
75
|
-
* @param {(
|
|
76
|
-
* @param {()
|
|
77
|
-
* @
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
const
|
|
87
|
-
const
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Darwin Phase 2 自动优化循环(hill-climbing)——插件侧编排。
|
|
3
|
+
*
|
|
4
|
+
* 一轮 = 诊断(rubric 最大加权短板)→ 生成改进(独立生成器,非 judge)→
|
|
5
|
+
* 写入 → paired 独立 judge 多数决 → keep(保留)或 revert(回滚)→ 记录。
|
|
6
|
+
*
|
|
7
|
+
* 安全约束(对齐 darwin 与项目纪律):
|
|
8
|
+
* - 每轮只针对一个维度(避免多变量无法归因);
|
|
9
|
+
* - 体积上限 150%(超限直接拒绝,不进入 judge);
|
|
10
|
+
* - 棘轮用 paired 多数决(绝对分只做 triage);
|
|
11
|
+
* - 版本化:项目是干净 git 仓库时用 commit/revert,否则用 .bak 文件恢复;
|
|
12
|
+
* - 连续 2 轮 judge margin 全为 slight/tie → 触顶 break(见好就收,HL-4)。
|
|
13
|
+
*
|
|
14
|
+
* 纯逻辑与 I/O 分离:所有外部动作(读/写/git/生成/judge/评分/时钟)都注入,
|
|
15
|
+
* 因此循环可在测试里用一个内存文件系统完整验证。
|
|
16
|
+
*/
|
|
17
|
+
import { judgeRecord } from './judge.js';
|
|
18
|
+
|
|
19
|
+
export const DEFAULT_ROUNDS = 3;
|
|
20
|
+
const MAX_GROWTH = 1.5;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 最大加权短板(weight × (10 - score) / 10;同分时取原始分更低者)。
|
|
24
|
+
*
|
|
25
|
+
* dim8「实测表现」被排除:它由测试 prompt 的真实运行决定,静态改写无法改进,
|
|
26
|
+
* 选它会让生成器空转(实测暴露的问题)。dim8 的改进走 full_test 流程。
|
|
27
|
+
*/
|
|
28
|
+
export function weakestDimension(rubric) {
|
|
29
|
+
const scored = (rubric?.dimensions ?? []).filter((dimension) => Number.isFinite(dimension?.score) && dimension?.id !== 8);
|
|
30
|
+
if (scored.length === 0) return null;
|
|
31
|
+
return scored.reduce((worst, dimension) => {
|
|
32
|
+
const gap = dimension.weight * (10 - dimension.score) / 10;
|
|
33
|
+
const worstGap = worst.weight * (10 - worst.score) / 10;
|
|
34
|
+
if (gap > worstGap) return dimension;
|
|
35
|
+
if (gap === worstGap && dimension.score < worst.score) return dimension;
|
|
36
|
+
return worst;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 触顶判据:连续 2 轮 judge 的 margin 全为 slight/tie。 */
|
|
41
|
+
export function isPlateau(recentMargins) {
|
|
42
|
+
const lastTwo = recentMargins.slice(-2);
|
|
43
|
+
return lastTwo.length === 2 && lastTwo.every((row) => row.every((margin) => margin === 'slight' || margin === 'tie'));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** 一轮候选的静态校验:不得超体积上限、必须真的变了。 */
|
|
47
|
+
export function validateCandidate({ before, after }) {
|
|
48
|
+
if (typeof after !== 'string' || after.trim() === '') return { ok: false, reason: 'generator returned an empty candidate' };
|
|
49
|
+
if (after === before) return { ok: false, reason: 'generator returned an unchanged candidate' };
|
|
50
|
+
if (after.length > before.length * MAX_GROWTH) {
|
|
51
|
+
return { ok: false, reason: `candidate exceeds 150% of the original size (${before.length} -> ${after.length}); trim before judging` };
|
|
52
|
+
}
|
|
53
|
+
return { ok: true, reason: '' };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** results.tsv 行(9 列,与 darwin 格式一致)。 */
|
|
57
|
+
export function resultRow({ timestamp, commit, skill, oldScore, newScore, status, dimension, note, evalMode }) {
|
|
58
|
+
return [timestamp, commit, skill, oldScore, newScore, status, dimension, note, evalMode].join('\t');
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const RESULTS_HEADER = ['timestamp', 'commit', 'skill', 'old_score', 'new_score', 'status', 'dimension', 'note', 'eval_mode'].join('\t');
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 跑一个 skill 的优化循环。
|
|
65
|
+
*
|
|
66
|
+
* @param {object} input
|
|
67
|
+
* @param {string} input.skillPath 目标 SKILL.md 绝对路径
|
|
68
|
+
* @param {string} [input.skillName] 记录用名字(默认取文件名所在目录名)
|
|
69
|
+
* @param {number} [input.rounds] MAX_ROUNDS(默认 3)
|
|
70
|
+
* @param {(path:string)=>Promise<string>} input.readText
|
|
71
|
+
* @param {(path:string, text:string)=>Promise<void>} input.writeText
|
|
72
|
+
* @param {(text:string, options:{measured:boolean})=>object} input.score 同步 rubric 评分
|
|
73
|
+
* @param {(input:{skillPath:string, before:string, dimension:object, round:number})=>Promise<string>} input.generate 生成候选全文
|
|
74
|
+
* @param {(input:{before:string, after:string, skillPath:string})=>Promise<{verdict:string, better:number, worse:number, ties:number, valid:number, votes:Array, errors:string[]}>} input.judge
|
|
75
|
+
* @param {object} input.vcs `{kind:'git', commit:(message)=>Promise<string>, revert:()=>Promise<void>}` 或 `{kind:'file', backup:(path:string,text:string)=>Promise<string>, restore:(path:string,backup:string)=>Promise<void>}`
|
|
76
|
+
* @param {(line:string)=>Promise<void>} [input.appendResult] 追加 results.tsv(含表头由实现方保证)
|
|
77
|
+
* @param {() => number} [input.now]
|
|
78
|
+
* @returns {Promise<{status:'completed'|'plateau'|'reverted'|'error', rounds:Array, finalScore:number|null, error?:string}>}
|
|
79
|
+
*/
|
|
80
|
+
export async function runOptimizeLoop(input) {
|
|
81
|
+
const {
|
|
82
|
+
skillPath, skillName, rounds = DEFAULT_ROUNDS, readText, writeText, score, generate, judge, vcs,
|
|
83
|
+
appendResult, now = () => Date.now(),
|
|
84
|
+
} = input;
|
|
85
|
+
const name = skillName ?? skillPath.split(/[\\/]/).slice(-2, -1)[0] ?? skillPath;
|
|
86
|
+
const history = [];
|
|
87
|
+
const staleness = [];
|
|
88
|
+
const stamp = () => new Date(now()).toISOString().slice(0, 19);
|
|
89
|
+
try {
|
|
90
|
+
for (let round = 1; round <= rounds; round++) {
|
|
91
|
+
const before = await readText(skillPath);
|
|
92
|
+
const baseline = score(before, { measured: false });
|
|
93
|
+
const dimension = weakestDimension(baseline);
|
|
94
|
+
if (dimension === null) {
|
|
95
|
+
await appendResult?.(resultRow({ timestamp: stamp(), commit: '-', skill: name, oldScore: String(baseline?.total ?? '-'), newScore: '-', status: 'error', dimension: '-', note: 'rubric produced no scorable dimensions', evalMode: 'structural' }));
|
|
96
|
+
return { status: 'error', rounds: history, finalScore: baseline?.total ?? null, error: 'no scorable dimensions' };
|
|
97
|
+
}
|
|
98
|
+
let candidate;
|
|
99
|
+
try {
|
|
100
|
+
candidate = await generate({ skillPath, before, dimension, round });
|
|
101
|
+
} catch (error) {
|
|
102
|
+
return { status: 'error', rounds: history, finalScore: baseline?.total ?? null, error: `generator failed: ${error?.message ?? error}` };
|
|
103
|
+
}
|
|
104
|
+
const validation = validateCandidate({ before, after: candidate });
|
|
105
|
+
if (!validation.ok) {
|
|
106
|
+
await appendResult?.(resultRow({ timestamp: stamp(), commit: '-', skill: name, oldScore: String(baseline.total), newScore: '-', status: 'skipped', dimension: dimension.name, note: validation.reason, evalMode: 'structural' }));
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
// 版本化:先记录基线,再写入候选。
|
|
110
|
+
let version = null;
|
|
111
|
+
if (vcs?.kind === 'git') {
|
|
112
|
+
try { version = await vcs.commit(`optimize ${name}: address dim${dimension.id} ${dimension.name}`); }
|
|
113
|
+
catch { version = null; }
|
|
114
|
+
} else if (vcs?.kind === 'file') {
|
|
115
|
+
version = await vcs.backup(skillPath, before);
|
|
116
|
+
}
|
|
117
|
+
await writeText(skillPath, candidate);
|
|
118
|
+
let verdict;
|
|
119
|
+
try {
|
|
120
|
+
verdict = await judge({ before, after: candidate, skillPath });
|
|
121
|
+
} catch (error) {
|
|
122
|
+
// judge 不可用:回滚,绝不保留未验证改动。
|
|
123
|
+
await restore(vcs, skillPath, before, version);
|
|
124
|
+
return { status: 'error', rounds: history, finalScore: baseline.total, error: `judge unavailable: ${error?.message ?? error}` };
|
|
125
|
+
}
|
|
126
|
+
const margins = (verdict.votes ?? []).map((vote) => vote.margin ?? 'slight');
|
|
127
|
+
staleness.push(margins);
|
|
128
|
+
const kept = verdict.verdict === 'keep';
|
|
129
|
+
if (!kept) {
|
|
130
|
+
await restore(vcs, skillPath, before, version);
|
|
131
|
+
} else if (vcs?.kind === 'git' && version !== null && typeof vcs.commitKept === 'function') {
|
|
132
|
+
await vcs.commitKept(version);
|
|
133
|
+
}
|
|
134
|
+
const afterScore = kept ? score(candidate, { measured: false }) : baseline;
|
|
135
|
+
history.push({
|
|
136
|
+
round,
|
|
137
|
+
dimension: `dim${dimension.id} ${dimension.name}`,
|
|
138
|
+
verdict: verdict.verdict,
|
|
139
|
+
better: verdict.better,
|
|
140
|
+
worse: verdict.worse,
|
|
141
|
+
ties: verdict.ties,
|
|
142
|
+
margins,
|
|
143
|
+
oldScore: baseline.total,
|
|
144
|
+
newScore: afterScore?.total ?? null,
|
|
145
|
+
kept,
|
|
146
|
+
});
|
|
147
|
+
await appendResult?.(resultRow({
|
|
148
|
+
timestamp: stamp(),
|
|
149
|
+
commit: version ?? '-',
|
|
150
|
+
skill: name,
|
|
151
|
+
oldScore: String(baseline.total),
|
|
152
|
+
newScore: String(afterScore?.total ?? '-'),
|
|
153
|
+
status: judgeRecord(verdict).status,
|
|
154
|
+
dimension: `dim${dimension.id}`,
|
|
155
|
+
note: `${judgeRecord(verdict).note}${verdict.errors?.length ? ` (${verdict.errors.join('; ')})` : ''}`,
|
|
156
|
+
evalMode: 'paired',
|
|
157
|
+
}));
|
|
158
|
+
if (!kept) {
|
|
159
|
+
return { status: 'reverted', rounds: history, finalScore: baseline.total };
|
|
160
|
+
}
|
|
161
|
+
if (isPlateau(staleness)) {
|
|
162
|
+
return { status: 'plateau', rounds: history, finalScore: (await readText(skillPath)).length > 0 ? (score(await readText(skillPath), { measured: false })?.total ?? null) : null };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
const finalText = await readText(skillPath);
|
|
166
|
+
return { status: 'completed', rounds: history, finalScore: score(finalText, { measured: false })?.total ?? null };
|
|
167
|
+
} catch (error) {
|
|
168
|
+
return { status: 'error', rounds: history, finalScore: null, error: error?.message ?? String(error) };
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
async function restore(vcs, skillPath, before, version) {
|
|
173
|
+
if (vcs?.kind === 'git') {
|
|
174
|
+
try { await vcs.revert(); } catch { /* fall through to file restore */ }
|
|
175
|
+
// 兜底:git revert 失败时用内存里的基线覆盖,保证不留下未验证改动。
|
|
176
|
+
}
|
|
177
|
+
if (vcs?.kind === 'file' && version !== null && typeof vcs.restore === 'function') {
|
|
178
|
+
try { await vcs.restore(skillPath, version); return; } catch { /* fall through */ }
|
|
179
|
+
}
|
|
180
|
+
if (vcs?.kind === 'git') {
|
|
181
|
+
try { await vcs.writeBaseline?.(skillPath, before); } catch { /* last resort */ }
|
|
182
|
+
}
|
|
183
|
+
}
|
package/lib/skillopt/reviewer.js
CHANGED
|
@@ -1,85 +1,87 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 评审通道:darwin 的「独立 judge」在插件里改为**专用评审成员**——
|
|
3
|
-
* 一个 role='评审' 的 agent 成员,不接任务,只做 A/B 盲评,用 team 工具把
|
|
4
|
-
* 结构化 verdict 回帖到一张评审卡(thread),插件从账本读回帖。
|
|
5
|
-
*
|
|
6
|
-
* 为什么不用一次性子代理:工具 execute 内读不到 startActivation 的输出
|
|
7
|
-
* (result 不 settle、agents/sessions 均不可见)。成员通道是插件既有且
|
|
8
|
-
* 已验证的(wake.delivered 数千次)。
|
|
9
|
-
*/
|
|
10
|
-
import { decideByMajority, normalizeVoteCount, parseJudgeOutput } from './judge.js';
|
|
11
|
-
|
|
12
|
-
export const REVIEWER_ROLE = '评审';
|
|
13
|
-
export const REVIEWER_NAME = '评审官';
|
|
14
|
-
|
|
15
|
-
/** 评审 prompt(盲评两版,输出一行 JSON)。 */
|
|
16
|
-
export function buildReviewRequest({ before, after, order, jobId, taskId, baseSeq = 0, orderId = 'v1' }) {
|
|
17
|
-
const aIsAfter = order === 'ba';
|
|
18
|
-
const versionA = aIsAfter ? after : before;
|
|
19
|
-
const versionB = aIsAfter ? before : after;
|
|
20
|
-
const card = typeof taskId === 'string' && taskId !== '' ? taskId : '<taskId>';
|
|
21
|
-
const verdict = `{"orderId":"${orderId}","better":"A","margin":"clear","reason":"一句话理由"}`;
|
|
22
|
-
return [
|
|
23
|
-
`【技能盲评 ${jobId}】你是独立评审。只做比较,不要修改任何文件。`,
|
|
24
|
-
'',
|
|
25
|
-
'准则(Darwin 9 维):结构——Frontmatter质量/工作流清晰度/失败模式编码/检查点设计/可执行具体性/资源整合度;效果——整体架构/实测表现;Meta——反例与黑名单。',
|
|
26
|
-
'加分:显式失败分支(如果 X 失败 → Y)、显性检查点(🔴/STOP/CHECKPOINT)、具体参数与示例、反例清单。',
|
|
27
|
-
'减分:软化措辞(建议/可以考虑/根据情况/灵活把握/视情况而定)、空话(说白了/换句话说/首先其次/综上所述)。',
|
|
28
|
-
'',
|
|
29
|
-
'## 版本 A',
|
|
30
|
-
versionA,
|
|
31
|
-
'',
|
|
32
|
-
'## 版本 B',
|
|
33
|
-
versionB,
|
|
34
|
-
'',
|
|
35
|
-
'## 如何提交结论(必须用 team 工具,两步)',
|
|
36
|
-
'1) 读取评审卡当前 headSeq:调用 team 工具',
|
|
37
|
-
` {"action":"thread","taskId":"${card}"}`,
|
|
38
|
-
'2) 用拿到的 headSeq 回帖(body 里只放一行 JSON,**必须原样带上 orderId**):',
|
|
39
|
-
` {"action":"reply","taskId":"${card}","baseSeq":${baseSeq},"body":"${verdict.replace(/"/g, '\\"')}"}`,
|
|
40
|
-
`注意:action 必须是 "reply"(不是 thread.post),参数名是 taskId / baseSeq / body;body 里的 orderId 必须是 "${orderId}",插件靠它把这一票对应到 A/B 顺序,缺了就无法计票。初始 baseSeq 通常为 ${baseSeq},若第 1 步返回的 headSeq 不同,用返回的 headSeq。`,
|
|
41
|
-
'除这次 team 回帖外不要做其它动作。',
|
|
42
|
-
].join('\n');
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* 从评审卡的 thread 条目里提取评审成员的票。
|
|
47
|
-
* @param {Array<{authorId:string, body:string}>} entries
|
|
48
|
-
* @param {string} reviewerId
|
|
49
|
-
* @returns {Array<{vote:'better'|'worse'|'tie'|null, margin:string, reason:string, aIsAfter:boolean}>}
|
|
50
|
-
*/
|
|
51
|
-
export function parseReviewReplies(entries, reviewerId) {
|
|
52
|
-
const votes = [];
|
|
53
|
-
for (const entry of Array.isArray(entries) ? entries : []) {
|
|
54
|
-
if (entry?.authorId !== reviewerId) continue;
|
|
55
|
-
const parsed = parseJudgeOutput(entry.body ?? '');
|
|
56
|
-
if (parsed === null) continue;
|
|
57
|
-
// 每一票的 A/B 顺序由投递时决定;这里无法从回帖里恢复,调用方按投递顺序对齐。
|
|
58
|
-
votes.push({ ...parsed });
|
|
59
|
-
}
|
|
60
|
-
return votes;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* 多数决(把票按投递时的顺序映射回 after 版本)。
|
|
65
|
-
* @param {Array<{better:string, margin:string, aIsAfter:boolean}>} votes
|
|
66
|
-
* @returns {{verdict:'keep'|'revert'|'inconclusive', better:number, worse:number, ties:number, valid:number}}
|
|
67
|
-
*/
|
|
68
|
-
export function decideReviewVotes(votes, expected = 0) {
|
|
69
|
-
const mapped = votes.map((vote) => {
|
|
70
|
-
if (vote.better === 'tie') return 'tie';
|
|
71
|
-
const afterWon = (vote.better === 'A' && vote.aIsAfter === true) || (vote.better === 'B' && vote.aIsAfter === false);
|
|
72
|
-
return afterWon ? 'better' : 'worse';
|
|
73
|
-
});
|
|
74
|
-
// 门槛按**应投数量**算,不按已收到的票数:后者会让 2/3 票时 required=1,
|
|
75
|
-
// 一张票就能定案。与 decideByMajority 的最少有效票契约保持一致。
|
|
76
|
-
const options = Number.isSafeInteger(expected) && expected > 0
|
|
77
|
-
? { minValid: Math.max(1, Math.ceil(expected / 2)) }
|
|
78
|
-
: {};
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
1
|
+
/**
|
|
2
|
+
* 评审通道:darwin 的「独立 judge」在插件里改为**专用评审成员**——
|
|
3
|
+
* 一个 role='评审' 的 agent 成员,不接任务,只做 A/B 盲评,用 team 工具把
|
|
4
|
+
* 结构化 verdict 回帖到一张评审卡(thread),插件从账本读回帖。
|
|
5
|
+
*
|
|
6
|
+
* 为什么不用一次性子代理:工具 execute 内读不到 startActivation 的输出
|
|
7
|
+
* (result 不 settle、agents/sessions 均不可见)。成员通道是插件既有且
|
|
8
|
+
* 已验证的(wake.delivered 数千次)。
|
|
9
|
+
*/
|
|
10
|
+
import { decideByMajority, normalizeVoteCount, parseJudgeOutput } from './judge.js';
|
|
11
|
+
|
|
12
|
+
export const REVIEWER_ROLE = '评审';
|
|
13
|
+
export const REVIEWER_NAME = '评审官';
|
|
14
|
+
|
|
15
|
+
/** 评审 prompt(盲评两版,输出一行 JSON)。 */
|
|
16
|
+
export function buildReviewRequest({ before, after, order, jobId, taskId, baseSeq = 0, orderId = 'v1' }) {
|
|
17
|
+
const aIsAfter = order === 'ba';
|
|
18
|
+
const versionA = aIsAfter ? after : before;
|
|
19
|
+
const versionB = aIsAfter ? before : after;
|
|
20
|
+
const card = typeof taskId === 'string' && taskId !== '' ? taskId : '<taskId>';
|
|
21
|
+
const verdict = `{"orderId":"${orderId}","better":"A","margin":"clear","reason":"一句话理由"}`;
|
|
22
|
+
return [
|
|
23
|
+
`【技能盲评 ${jobId}】你是独立评审。只做比较,不要修改任何文件。`,
|
|
24
|
+
'',
|
|
25
|
+
'准则(Darwin 9 维):结构——Frontmatter质量/工作流清晰度/失败模式编码/检查点设计/可执行具体性/资源整合度;效果——整体架构/实测表现;Meta——反例与黑名单。',
|
|
26
|
+
'加分:显式失败分支(如果 X 失败 → Y)、显性检查点(🔴/STOP/CHECKPOINT)、具体参数与示例、反例清单。',
|
|
27
|
+
'减分:软化措辞(建议/可以考虑/根据情况/灵活把握/视情况而定)、空话(说白了/换句话说/首先其次/综上所述)。',
|
|
28
|
+
'',
|
|
29
|
+
'## 版本 A',
|
|
30
|
+
versionA,
|
|
31
|
+
'',
|
|
32
|
+
'## 版本 B',
|
|
33
|
+
versionB,
|
|
34
|
+
'',
|
|
35
|
+
'## 如何提交结论(必须用 team 工具,两步)',
|
|
36
|
+
'1) 读取评审卡当前 headSeq:调用 team 工具',
|
|
37
|
+
` {"action":"thread","taskId":"${card}"}`,
|
|
38
|
+
'2) 用拿到的 headSeq 回帖(body 里只放一行 JSON,**必须原样带上 orderId**):',
|
|
39
|
+
` {"action":"reply","taskId":"${card}","baseSeq":${baseSeq},"body":"${verdict.replace(/"/g, '\\"')}"}`,
|
|
40
|
+
`注意:action 必须是 "reply"(不是 thread.post),参数名是 taskId / baseSeq / body;body 里的 orderId 必须是 "${orderId}",插件靠它把这一票对应到 A/B 顺序,缺了就无法计票。初始 baseSeq 通常为 ${baseSeq},若第 1 步返回的 headSeq 不同,用返回的 headSeq。`,
|
|
41
|
+
'除这次 team 回帖外不要做其它动作。',
|
|
42
|
+
].join('\n');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 从评审卡的 thread 条目里提取评审成员的票。
|
|
47
|
+
* @param {Array<{authorId:string, body:string}>} entries
|
|
48
|
+
* @param {string} reviewerId
|
|
49
|
+
* @returns {Array<{vote:'better'|'worse'|'tie'|null, margin:string, reason:string, aIsAfter:boolean}>}
|
|
50
|
+
*/
|
|
51
|
+
export function parseReviewReplies(entries, reviewerId) {
|
|
52
|
+
const votes = [];
|
|
53
|
+
for (const entry of Array.isArray(entries) ? entries : []) {
|
|
54
|
+
if (entry?.authorId !== reviewerId) continue;
|
|
55
|
+
const parsed = parseJudgeOutput(entry.body ?? '');
|
|
56
|
+
if (parsed === null) continue;
|
|
57
|
+
// 每一票的 A/B 顺序由投递时决定;这里无法从回帖里恢复,调用方按投递顺序对齐。
|
|
58
|
+
votes.push({ ...parsed });
|
|
59
|
+
}
|
|
60
|
+
return votes;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 多数决(把票按投递时的顺序映射回 after 版本)。
|
|
65
|
+
* @param {Array<{better:string, margin:string, aIsAfter:boolean}>} votes
|
|
66
|
+
* @returns {{verdict:'keep'|'revert'|'inconclusive', better:number, worse:number, ties:number, valid:number}}
|
|
67
|
+
*/
|
|
68
|
+
export function decideReviewVotes(votes, expected = 0) {
|
|
69
|
+
const mapped = votes.map((vote) => {
|
|
70
|
+
if (vote.better === 'tie') return 'tie';
|
|
71
|
+
const afterWon = (vote.better === 'A' && vote.aIsAfter === true) || (vote.better === 'B' && vote.aIsAfter === false);
|
|
72
|
+
return afterWon ? 'better' : 'worse';
|
|
73
|
+
});
|
|
74
|
+
// 门槛按**应投数量**算,不按已收到的票数:后者会让 2/3 票时 required=1,
|
|
75
|
+
// 一张票就能定案。与 decideByMajority 的最少有效票契约保持一致。
|
|
76
|
+
const options = Number.isSafeInteger(expected) && expected > 0
|
|
77
|
+
? { minValid: Math.max(1, Math.ceil(expected / 2)) }
|
|
78
|
+
: {};
|
|
79
|
+
// 解析不出的回帖在 parseReviewReplies 里已被跳过:按应投数量补成无效票,才能把它们记作 unparsed。
|
|
80
|
+
while (Number.isSafeInteger(expected) && mapped.length < expected) mapped.push(null);
|
|
81
|
+
return decideByMajority(mapped, options);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 评审所需的投递数量(奇数,默认 3)。 */
|
|
85
|
+
export function reviewVoteCount(value) {
|
|
86
|
+
return normalizeVoteCount(value);
|
|
87
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@suifei/dsh-ai-team",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.144",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "文渊阁 (Wenyuan Pavilion) — AI Team mode for DeepSeek Harness: concurrent projects, owned tasks, threads, inbox and an append-only team ledger",
|
|
6
6
|
"license": "MIT",
|