@suifei/dsh-ai-team 0.4.145 → 0.4.146
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 +5 -0
- package/LICENSE +21 -21
- package/README.md +19 -1
- package/client.js +118 -17
- package/cordis.patch.yml +31 -31
- package/icon.svg +12 -12
- package/index.js +7 -1
- package/lib/domain/ids.js +26 -26
- package/lib/domain/inbox.js +93 -93
- package/lib/domain/member-config.js +168 -168
- package/lib/host/decision-report.js +72 -72
- package/lib/host/default-workspace.js +69 -69
- package/lib/host/fault-adapter.js +50 -50
- package/lib/host/people-view.js +9 -2
- package/lib/host/permission-guard.js +61 -61
- package/lib/host/project-registry.js +104 -104
- package/lib/host/risk-view.js +99 -99
- package/lib/host/weekly-view.js +86 -86
- package/lib/host/workspace-path.js +94 -94
- package/lib/ledger/checkpoint.js +415 -415
- package/lib/ledger/events.js +398 -398
- package/lib/ledger/namespace.js +124 -124
- package/lib/ledger/tools.js +118 -118
- package/lib/ledger/writer.js +1266 -1266
- package/lib/runtime/error-chain.js +31 -31
- package/lib/runtime/shell-guard.js +102 -102
- package/lib/runtime/skill-catalog.js +41 -41
- package/lib/runtime/skill-policy.js +12 -12
- package/lib/skillopt/adopt-gate.js +108 -108
- package/lib/skillopt/gate-wiring.js +146 -146
- package/lib/skillopt/jobs.js +135 -135
- package/lib/skillopt/proposal-store.js +33 -33
- package/lib/skillopt/review-runner.js +107 -107
- package/lib/skillopt/rubric.js +189 -189
- package/lib/skillopt/subagent-text.js +101 -101
- package/lib/skillopt/tool-access.js +56 -56
- package/locale/en.json +6 -6
- package/locale/zh.json +6 -6
- package/package.json +1 -1
|
@@ -1,107 +1,107 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 评审执行:把 A/B 盲评投递给**专用评审成员**,从评审卡的 thread 回帖收票。
|
|
3
|
-
*
|
|
4
|
-
* 通道依据:`member.message`(Human→成员)会写账本并触发 waker 唤醒成员,
|
|
5
|
-
* 这是插件既有且长期运行的通道(wake.delivered 数千次)。评审成员被要求在
|
|
6
|
-
* 回复里用 team 工具把一行 JSON 发到评审卡;插件读 `state.threads[taskId]`
|
|
7
|
-
* 的条目即可拿到 verdict,**不依赖一次性子代理的输出**。
|
|
8
|
-
*/
|
|
9
|
-
import { REVIEWER_NAME, REVIEWER_ROLE, buildReviewRequest, decideReviewVotes, reviewVoteCount } from './reviewer.js';
|
|
10
|
-
import { parseJudgeOutput } from './judge.js';
|
|
11
|
-
|
|
12
|
-
const REVIEW_TASK_PREFIX = 'skillopt-review';
|
|
13
|
-
|
|
14
|
-
/** 在项目里查/建专用评审成员。返回 member 投影。 */
|
|
15
|
-
export async function ensureReviewer({ service, projectRuntime, actor }) {
|
|
16
|
-
const members = Object.values(service.state.members ?? {});
|
|
17
|
-
const existing = members.find((member) => member.kind === 'agent' && (member.role === REVIEWER_ROLE || member.name === REVIEWER_NAME));
|
|
18
|
-
if (existing !== undefined) return existing;
|
|
19
|
-
const created = await service.addMember(
|
|
20
|
-
{ actor, projectId: projectRuntime.id },
|
|
21
|
-
{
|
|
22
|
-
name: REVIEWER_NAME,
|
|
23
|
-
kind: 'agent',
|
|
24
|
-
role: REVIEWER_ROLE,
|
|
25
|
-
personality: '你只做独立盲评:按 Darwin 9 维 rubric 比较两个版本,只输出一行 JSON 结论;不接任务、不改文件。',
|
|
26
|
-
status: 'idle',
|
|
27
|
-
},
|
|
28
|
-
);
|
|
29
|
-
return created?.member ?? service.state.members?.[created?.member?.id ?? ''];
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** 创建评审卡(task)并投递 N 条盲评请求给评审成员。 */
|
|
33
|
-
export async function startReview({ service, projectRuntime, job, actor, votes, onCard = null }) {
|
|
34
|
-
const reviewer = await ensureReviewer({ service, projectRuntime, actor });
|
|
35
|
-
if (reviewer === undefined || reviewer === null) throw new Error('reviewer member unavailable');
|
|
36
|
-
const count = reviewVoteCount(votes ?? 3);
|
|
37
|
-
const taskTitle = `${REVIEW_TASK_PREFIX} ${job.id}`;
|
|
38
|
-
const created = await service.dispatch({
|
|
39
|
-
type: 'task.create',
|
|
40
|
-
payload: {
|
|
41
|
-
title: taskTitle,
|
|
42
|
-
description: `Darwin paired review for ${job.skillName ?? job.skillPath} (job ${job.id})`,
|
|
43
|
-
ownerId: reviewer.id,
|
|
44
|
-
// 评审卡的验收标准:一行 JSON verdict(better/margin/reason)。
|
|
45
|
-
resultDefinition: '回复一行 JSON(better=A|B|tie、margin=clear|slight、reason=一句话理由),不修改任何文件。',
|
|
46
|
-
kind: 'review',
|
|
47
|
-
},
|
|
48
|
-
actor,
|
|
49
|
-
projectId: projectRuntime.id,
|
|
50
|
-
});
|
|
51
|
-
const reviewTaskId = created?.task?.id ?? created?.id ?? null;
|
|
52
|
-
if (typeof reviewTaskId !== 'string') throw new Error('review card creation failed');
|
|
53
|
-
// 卡片一创建就把 id 交回调用方落盘:进程若在「投递评审请求」期间崩溃,
|
|
54
|
-
// 调用方手里已经有卡 id,随后的超时终态能把它收口,而不是留下孤儿卡。
|
|
55
|
-
if (typeof onCard === 'function') await onCard(reviewTaskId);
|
|
56
|
-
const orders = [];
|
|
57
|
-
for (let index = 0; index < count; index++) {
|
|
58
|
-
const order = Math.random() < 0.5 ? 'ab' : 'ba';
|
|
59
|
-
// 每一票带独立 orderId:投递顺序与回帖顺序无关,靠 id 才能把票对应回
|
|
60
|
-
// 该票的 A/B 顺序;否则一次乱序/重复回帖就会整票移位甚至反转 keep/revert。
|
|
61
|
-
const orderId = `v${index + 1}`;
|
|
62
|
-
const body = buildReviewRequest({ before: job.before, after: job.candidate, order, jobId: job.id, taskId: reviewTaskId, baseSeq: 0, orderId });
|
|
63
|
-
await service.dispatch({
|
|
64
|
-
type: 'member.message',
|
|
65
|
-
payload: { memberId: reviewer.id, body },
|
|
66
|
-
actor,
|
|
67
|
-
projectId: projectRuntime.id,
|
|
68
|
-
});
|
|
69
|
-
orders.push({ orderId, order, aIsAfter: order === 'ba' });
|
|
70
|
-
}
|
|
71
|
-
return { reviewTaskId, reviewerId: reviewer.id, orders };
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/** 收票并判多数决。 */
|
|
75
|
-
export function collectReview({ service, job }) {
|
|
76
|
-
const thread = service.state.threads?.[job.reviewTaskId];
|
|
77
|
-
const entries = Array.isArray(thread?.entries) ? thread.entries : [];
|
|
78
|
-
const reviewerId = job.reviewerId;
|
|
79
|
-
const orders = Array.isArray(job.reviewOrders) ? job.reviewOrders : [];
|
|
80
|
-
const byOrderId = new Map(orders.filter((order) => typeof order?.orderId === 'string').map((order) => [order.orderId, order]));
|
|
81
|
-
const used = new Set();
|
|
82
|
-
const votes = [];
|
|
83
|
-
for (const entry of entries) {
|
|
84
|
-
if (votes.length >= orders.length) break;
|
|
85
|
-
if (entry?.authorId !== reviewerId) continue;
|
|
86
|
-
// 复用 judge 的容错解析:自己的「第一个 { 到第一个 }」正则会把模板回显
|
|
87
|
-
// 计成票,也会在正文含 } 时整票丢弃。
|
|
88
|
-
const parsed = parseJudgeOutput(entry.body);
|
|
89
|
-
if (parsed === null || (parsed.better !== 'A' && parsed.better !== 'B' && parsed.better !== 'tie')) continue;
|
|
90
|
-
let order = typeof parsed.orderId === 'string' ? byOrderId.get(parsed.orderId) : undefined;
|
|
91
|
-
// 旧 job 的 orders 没有 orderId:保留按序对齐的兼容路径,但仍受票数上限约束。
|
|
92
|
-
if (order === undefined && typeof parsed.orderId !== 'string' && byOrderId.size === 0) {
|
|
93
|
-
order = orders[votes.length];
|
|
94
|
-
}
|
|
95
|
-
if (order === undefined) continue;
|
|
96
|
-
const key = order.orderId ?? `#${votes.length}`;
|
|
97
|
-
if (used.has(key)) continue; // 同一票重复回帖只计一次
|
|
98
|
-
used.add(key);
|
|
99
|
-
votes.push({
|
|
100
|
-
better: parsed.better,
|
|
101
|
-
margin: parsed.margin === 'clear' ? 'clear' : 'slight',
|
|
102
|
-
reason: typeof parsed.reason === 'string' ? parsed.reason.slice(0, 300) : '',
|
|
103
|
-
aIsAfter: order?.aIsAfter === true,
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
return { votes, decision: decideReviewVotes(votes, orders.length), expected: orders.length };
|
|
107
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* 评审执行:把 A/B 盲评投递给**专用评审成员**,从评审卡的 thread 回帖收票。
|
|
3
|
+
*
|
|
4
|
+
* 通道依据:`member.message`(Human→成员)会写账本并触发 waker 唤醒成员,
|
|
5
|
+
* 这是插件既有且长期运行的通道(wake.delivered 数千次)。评审成员被要求在
|
|
6
|
+
* 回复里用 team 工具把一行 JSON 发到评审卡;插件读 `state.threads[taskId]`
|
|
7
|
+
* 的条目即可拿到 verdict,**不依赖一次性子代理的输出**。
|
|
8
|
+
*/
|
|
9
|
+
import { REVIEWER_NAME, REVIEWER_ROLE, buildReviewRequest, decideReviewVotes, reviewVoteCount } from './reviewer.js';
|
|
10
|
+
import { parseJudgeOutput } from './judge.js';
|
|
11
|
+
|
|
12
|
+
const REVIEW_TASK_PREFIX = 'skillopt-review';
|
|
13
|
+
|
|
14
|
+
/** 在项目里查/建专用评审成员。返回 member 投影。 */
|
|
15
|
+
export async function ensureReviewer({ service, projectRuntime, actor }) {
|
|
16
|
+
const members = Object.values(service.state.members ?? {});
|
|
17
|
+
const existing = members.find((member) => member.kind === 'agent' && (member.role === REVIEWER_ROLE || member.name === REVIEWER_NAME));
|
|
18
|
+
if (existing !== undefined) return existing;
|
|
19
|
+
const created = await service.addMember(
|
|
20
|
+
{ actor, projectId: projectRuntime.id },
|
|
21
|
+
{
|
|
22
|
+
name: REVIEWER_NAME,
|
|
23
|
+
kind: 'agent',
|
|
24
|
+
role: REVIEWER_ROLE,
|
|
25
|
+
personality: '你只做独立盲评:按 Darwin 9 维 rubric 比较两个版本,只输出一行 JSON 结论;不接任务、不改文件。',
|
|
26
|
+
status: 'idle',
|
|
27
|
+
},
|
|
28
|
+
);
|
|
29
|
+
return created?.member ?? service.state.members?.[created?.member?.id ?? ''];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** 创建评审卡(task)并投递 N 条盲评请求给评审成员。 */
|
|
33
|
+
export async function startReview({ service, projectRuntime, job, actor, votes, onCard = null }) {
|
|
34
|
+
const reviewer = await ensureReviewer({ service, projectRuntime, actor });
|
|
35
|
+
if (reviewer === undefined || reviewer === null) throw new Error('reviewer member unavailable');
|
|
36
|
+
const count = reviewVoteCount(votes ?? 3);
|
|
37
|
+
const taskTitle = `${REVIEW_TASK_PREFIX} ${job.id}`;
|
|
38
|
+
const created = await service.dispatch({
|
|
39
|
+
type: 'task.create',
|
|
40
|
+
payload: {
|
|
41
|
+
title: taskTitle,
|
|
42
|
+
description: `Darwin paired review for ${job.skillName ?? job.skillPath} (job ${job.id})`,
|
|
43
|
+
ownerId: reviewer.id,
|
|
44
|
+
// 评审卡的验收标准:一行 JSON verdict(better/margin/reason)。
|
|
45
|
+
resultDefinition: '回复一行 JSON(better=A|B|tie、margin=clear|slight、reason=一句话理由),不修改任何文件。',
|
|
46
|
+
kind: 'review',
|
|
47
|
+
},
|
|
48
|
+
actor,
|
|
49
|
+
projectId: projectRuntime.id,
|
|
50
|
+
});
|
|
51
|
+
const reviewTaskId = created?.task?.id ?? created?.id ?? null;
|
|
52
|
+
if (typeof reviewTaskId !== 'string') throw new Error('review card creation failed');
|
|
53
|
+
// 卡片一创建就把 id 交回调用方落盘:进程若在「投递评审请求」期间崩溃,
|
|
54
|
+
// 调用方手里已经有卡 id,随后的超时终态能把它收口,而不是留下孤儿卡。
|
|
55
|
+
if (typeof onCard === 'function') await onCard(reviewTaskId);
|
|
56
|
+
const orders = [];
|
|
57
|
+
for (let index = 0; index < count; index++) {
|
|
58
|
+
const order = Math.random() < 0.5 ? 'ab' : 'ba';
|
|
59
|
+
// 每一票带独立 orderId:投递顺序与回帖顺序无关,靠 id 才能把票对应回
|
|
60
|
+
// 该票的 A/B 顺序;否则一次乱序/重复回帖就会整票移位甚至反转 keep/revert。
|
|
61
|
+
const orderId = `v${index + 1}`;
|
|
62
|
+
const body = buildReviewRequest({ before: job.before, after: job.candidate, order, jobId: job.id, taskId: reviewTaskId, baseSeq: 0, orderId });
|
|
63
|
+
await service.dispatch({
|
|
64
|
+
type: 'member.message',
|
|
65
|
+
payload: { memberId: reviewer.id, body },
|
|
66
|
+
actor,
|
|
67
|
+
projectId: projectRuntime.id,
|
|
68
|
+
});
|
|
69
|
+
orders.push({ orderId, order, aIsAfter: order === 'ba' });
|
|
70
|
+
}
|
|
71
|
+
return { reviewTaskId, reviewerId: reviewer.id, orders };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** 收票并判多数决。 */
|
|
75
|
+
export function collectReview({ service, job }) {
|
|
76
|
+
const thread = service.state.threads?.[job.reviewTaskId];
|
|
77
|
+
const entries = Array.isArray(thread?.entries) ? thread.entries : [];
|
|
78
|
+
const reviewerId = job.reviewerId;
|
|
79
|
+
const orders = Array.isArray(job.reviewOrders) ? job.reviewOrders : [];
|
|
80
|
+
const byOrderId = new Map(orders.filter((order) => typeof order?.orderId === 'string').map((order) => [order.orderId, order]));
|
|
81
|
+
const used = new Set();
|
|
82
|
+
const votes = [];
|
|
83
|
+
for (const entry of entries) {
|
|
84
|
+
if (votes.length >= orders.length) break;
|
|
85
|
+
if (entry?.authorId !== reviewerId) continue;
|
|
86
|
+
// 复用 judge 的容错解析:自己的「第一个 { 到第一个 }」正则会把模板回显
|
|
87
|
+
// 计成票,也会在正文含 } 时整票丢弃。
|
|
88
|
+
const parsed = parseJudgeOutput(entry.body);
|
|
89
|
+
if (parsed === null || (parsed.better !== 'A' && parsed.better !== 'B' && parsed.better !== 'tie')) continue;
|
|
90
|
+
let order = typeof parsed.orderId === 'string' ? byOrderId.get(parsed.orderId) : undefined;
|
|
91
|
+
// 旧 job 的 orders 没有 orderId:保留按序对齐的兼容路径,但仍受票数上限约束。
|
|
92
|
+
if (order === undefined && typeof parsed.orderId !== 'string' && byOrderId.size === 0) {
|
|
93
|
+
order = orders[votes.length];
|
|
94
|
+
}
|
|
95
|
+
if (order === undefined) continue;
|
|
96
|
+
const key = order.orderId ?? `#${votes.length}`;
|
|
97
|
+
if (used.has(key)) continue; // 同一票重复回帖只计一次
|
|
98
|
+
used.add(key);
|
|
99
|
+
votes.push({
|
|
100
|
+
better: parsed.better,
|
|
101
|
+
margin: parsed.margin === 'clear' ? 'clear' : 'slight',
|
|
102
|
+
reason: typeof parsed.reason === 'string' ? parsed.reason.slice(0, 300) : '',
|
|
103
|
+
aIsAfter: order?.aIsAfter === true,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
return { votes, decision: decideReviewVotes(votes, orders.length), expected: orders.length };
|
|
107
|
+
}
|
package/lib/skillopt/rubric.js
CHANGED
|
@@ -1,189 +1,189 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Darwin Skill 9 维 rubric(对齐 alchaincyf/darwin-skill v2.1 的权威定义):
|
|
3
|
-
*
|
|
4
|
-
* 结构维度(59):1 Frontmatter 7 / 2 工作流清晰度 12 / 3 失败模式编码 12 /
|
|
5
|
-
* 4 检查点设计 6 / 5 可执行具体性 18 / 6 资源整合度 4
|
|
6
|
-
* 效果维度(35):7 整体架构 12 / 8 实测表现 23
|
|
7
|
-
* Meta-skill(6):9 反例与黑名单 6
|
|
8
|
-
*
|
|
9
|
-
* 评分规则:维度 1-7、9 打 1-10 分 × 权重;维度 8(实测表现)由调用方注入
|
|
10
|
-
* (跑测试 prompt)或按干跑打分。总分 = Σ(维度分 × 权重) / 10,满分 100。
|
|
11
|
-
*
|
|
12
|
-
* ⚠️ 绝对总分只用于 triage(先改哪支最弱),**绝不用于 keep/revert**——
|
|
13
|
-
* LLM judge 换尺 ±8 噪音会淹没保守编辑的真实增益。keep/revert 一律走
|
|
14
|
-
* judge.js 的 paired 同-judge 多数决。
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
export const NINE_DIMENSIONS = Object.freeze([
|
|
18
|
-
{ id: 1, name: 'Frontmatter质量', weight: 7, group: 'structure' },
|
|
19
|
-
{ id: 2, name: '工作流清晰度', weight: 12, group: 'structure' },
|
|
20
|
-
{ id: 3, name: '失败模式编码', weight: 12, group: 'structure' },
|
|
21
|
-
{ id: 4, name: '检查点设计', weight: 6, group: 'structure' },
|
|
22
|
-
{ id: 5, name: '可执行具体性', weight: 18, group: 'structure' },
|
|
23
|
-
{ id: 6, name: '资源整合度', weight: 4, group: 'structure' },
|
|
24
|
-
{ id: 7, name: '整体架构', weight: 12, group: 'effect' },
|
|
25
|
-
{ id: 8, name: '实测表现', weight: 23, group: 'effect' },
|
|
26
|
-
{ id: 9, name: '反例与黑名单', weight: 6, group: 'meta' },
|
|
27
|
-
]);
|
|
28
|
-
|
|
29
|
-
/** 软化措辞(dim5 扣分项):出现 ≥3 处扣 ≥3 分。 */
|
|
30
|
-
const SOFT_WORDS = ['建议', '可以考虑', '根据情况', '灵活把握', '视情况而定', '酌情', '尽量'];
|
|
31
|
-
/** AI 腔/废话词(dim7 扣分项):出现一处扣 1 分。 */
|
|
32
|
-
const FILLER_WORDS = ['说白了', '换句话说', '首先其次', '综上所述', '总而言之'];
|
|
33
|
-
/** 失败模式句式(dim3):显式「如果 X → Y」分支,含缺失/为空/超时等触发条件。 */
|
|
34
|
-
const FAILURE_PATTERNS = [
|
|
35
|
-
/如果[^\n]{0,60}(失败|缺失|为空|不存在|不一致|超时|报错)/,
|
|
36
|
-
/兜底/,
|
|
37
|
-
/fallback/i,
|
|
38
|
-
/异常处理/,
|
|
39
|
-
/恢复/,
|
|
40
|
-
];
|
|
41
|
-
/** 检查点显性标记(dim4):仅靠『如果…建议…』措辞不算。 */
|
|
42
|
-
const CHECKPOINT_PATTERNS = [/🔴/, /🛑/, /CHECKPOINT/, /STOP/];
|
|
43
|
-
/** 反例/黑名单章节(dim9)。 */
|
|
44
|
-
const COUNTEREXAMPLE_PATTERNS = [/反例/, /黑名单/, /不要做/, /禁止/, /反模式/];
|
|
45
|
-
|
|
46
|
-
const clampScore = (value) => Math.max(1, Math.min(10, value));
|
|
47
|
-
// 只数**完整匹配**:`String.match` 在带捕获组时会把分组也塞进返回数组,长度
|
|
48
|
-
// 因此虚高(`/(a)(b)/g` 命中一次返回 3 个元素),dim3 分数被系统性抬高。
|
|
49
|
-
const countMatches = (text, patterns) => patterns.reduce((total, pattern) => {
|
|
50
|
-
const flags = pattern.flags.includes('g') ? pattern.flags : `${pattern.flags}g`;
|
|
51
|
-
const global = new RegExp(pattern.source, flags);
|
|
52
|
-
let hits = 0;
|
|
53
|
-
for (const _ of String(text).matchAll(global)) hits += 1;
|
|
54
|
-
return total + hits;
|
|
55
|
-
}, 0);
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 静态评分(dims 1-7、9)。dim8 由调用方注入(measured dim8 干跑/实测分)。
|
|
59
|
-
*
|
|
60
|
-
* @param {string} text SKILL.md 全文
|
|
61
|
-
* @param {{dim8?: number, measured?: boolean, resourcePaths?: string[]}} [options]
|
|
62
|
-
* @returns {{dimensions: Array<{id:number,name:string,weight:number,score:number,evidence:string}>, total:number, evaluated:boolean, triageOnly:boolean}}
|
|
63
|
-
*/
|
|
64
|
-
export function scoreSkillRubric(text, options = {}) {
|
|
65
|
-
const source = typeof text === 'string' ? text : '';
|
|
66
|
-
// NaN/Infinity 会穿透 clampScore(Math.min/max 对 NaN 返回 NaN)→ 总分变 NaN。
|
|
67
|
-
const dim8 = Number.isFinite(options.dim8) ? clampScore(options.dim8) : 5;
|
|
68
|
-
const evidence = [];
|
|
69
|
-
const scores = new Map();
|
|
70
|
-
|
|
71
|
-
// dim1 Frontmatter:name/description/触发词/长度/空话尾巴
|
|
72
|
-
{
|
|
73
|
-
const hasName = /^---\s*[\s\S]*?name:/m.test(source);
|
|
74
|
-
const hasDescription = /^---\s*[\s\S]*?description:/m.test(source);
|
|
75
|
-
const description = source.match(/description:\s*([^\n]+)/)?.[1] ?? '';
|
|
76
|
-
const hasTriggers = /触发|trigger|当用户|使用场景/i.test(description);
|
|
77
|
-
const vagueTail = /(灵活应用|根据情况判断|视情况)/.test(description.slice(-80));
|
|
78
|
-
let score = 4;
|
|
79
|
-
if (hasName) score += 2;
|
|
80
|
-
if (hasDescription) score += 1;
|
|
81
|
-
if (hasTriggers) score += 2;
|
|
82
|
-
if (description.length > 0 && description.length <= 1024) score += 1;
|
|
83
|
-
if (vagueTail) score -= 2;
|
|
84
|
-
scores.set(1, clampScore(score));
|
|
85
|
-
evidence.push(`dim1 frontmatter: name=${hasName} desc=${hasDescription} triggers=${hasTriggers} vagueTail=${vagueTail}`);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// dim2 工作流:编号/标题步骤数
|
|
89
|
-
{
|
|
90
|
-
const steps = (source.match(/^\s{0,4}(\d+[.)、]|#{2,4}\s*(Step|步骤|Phase|阶段)\s*\d)/gim) ?? []).length;
|
|
91
|
-
const score = steps === 0 ? 3 : steps <= 2 ? 5 : steps <= 5 ? 7 : 9;
|
|
92
|
-
scores.set(2, clampScore(score));
|
|
93
|
-
evidence.push(`dim2 workflow steps=${steps}`);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// dim3 失败模式编码:显式失败分支;只写正向流程扣分
|
|
97
|
-
{
|
|
98
|
-
const hits = countMatches(source, FAILURE_PATTERNS);
|
|
99
|
-
const score = hits === 0 ? 2 : hits <= 2 ? 6 : hits <= 5 ? 8 : 9;
|
|
100
|
-
scores.set(3, clampScore(score));
|
|
101
|
-
evidence.push(`dim3 failure patterns=${hits}`);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// dim4 检查点:显性标记(🔴/STOP/CHECKPOINT)
|
|
105
|
-
{
|
|
106
|
-
const hits = countMatches(source, CHECKPOINT_PATTERNS);
|
|
107
|
-
const score = hits === 0 ? 2 : hits >= 3 ? 9 : 7;
|
|
108
|
-
scores.set(4, clampScore(score));
|
|
109
|
-
evidence.push(`dim4 explicit checkpoints=${hits}`);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
// dim5 可执行具体性:软化措辞扣分 + 具体参数/示例加分
|
|
113
|
-
{
|
|
114
|
-
const soft = SOFT_WORDS.reduce((total, word) => total + (source.split(word).length - 1), 0);
|
|
115
|
-
const concrete = (source.match(/```|`[^`\n]+`|--[a-z-]+|\d+(\.\d+)?(ms|s|px|%|KB|MB)/g) ?? []).length;
|
|
116
|
-
let score = 6 + Math.min(3, Math.floor(concrete / 8));
|
|
117
|
-
if (soft >= 3) score -= Math.min(5, soft);
|
|
118
|
-
scores.set(5, clampScore(score));
|
|
119
|
-
evidence.push(`dim5 softWords=${soft} concreteHints=${concrete}`);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// dim6 资源整合度:references/scripts/assets 引用是否真的可达
|
|
123
|
-
{
|
|
124
|
-
const refs = (source.match(/(references|scripts|assets)\/[^\s)`"']+/g) ?? []).length;
|
|
125
|
-
// 没给 resourcePaths 时**不能**把「引用数」当成「可达数」:那会让任何
|
|
126
|
-
// 含 references/ 的文本恒得 9 分(自证可达)。没有可达清单时按「未验证」
|
|
127
|
-
// 走中性分,并在证据里标注。
|
|
128
|
-
const hasKnown = Array.isArray(options.resourcePaths);
|
|
129
|
-
const known = hasKnown ? options.resourcePaths.length : 0;
|
|
130
|
-
const score = refs === 0
|
|
131
|
-
? (hasKnown ? 6 : 5)
|
|
132
|
-
: hasKnown
|
|
133
|
-
? (known >= refs ? 9 : 6)
|
|
134
|
-
: 6;
|
|
135
|
-
scores.set(6, clampScore(score));
|
|
136
|
-
evidence.push(`dim6 resource refs=${refs} reachable=${hasKnown ? known : 'unverified'}`);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// dim7 整体架构:层级 + 冗余扣分
|
|
140
|
-
{
|
|
141
|
-
const headings = (source.match(/^#{1,4}\s+/gm) ?? []).length;
|
|
142
|
-
const filler = FILLER_WORDS.reduce((total, word) => total + (source.split(word).length - 1), 0);
|
|
143
|
-
let score = headings >= 5 ? 8 : headings >= 2 ? 6 : 4;
|
|
144
|
-
score -= Math.min(4, filler);
|
|
145
|
-
scores.set(7, clampScore(score));
|
|
146
|
-
evidence.push(`dim7 headings=${headings} filler=${filler}`);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// dim8 实测表现:调用方注入(measured=true 时才是实测)
|
|
150
|
-
scores.set(8, dim8);
|
|
151
|
-
|
|
152
|
-
// dim9 反例与黑名单
|
|
153
|
-
{
|
|
154
|
-
const hits = countMatches(source, COUNTEREXAMPLE_PATTERNS);
|
|
155
|
-
const score = hits === 0 ? 2 : hits >= 2 ? 9 : 7;
|
|
156
|
-
scores.set(9, clampScore(score));
|
|
157
|
-
evidence.push(`dim9 counterexamples=${hits}`);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
const dimensions = NINE_DIMENSIONS.map((dimension) => ({
|
|
161
|
-
...dimension,
|
|
162
|
-
score: scores.get(dimension.id),
|
|
163
|
-
evidence: evidence.find((line) => line.startsWith(`dim${dimension.id} `)) ?? '',
|
|
164
|
-
}));
|
|
165
|
-
const total = Math.round(dimensions.reduce((sum, dimension) => sum + dimension.score * dimension.weight, 0) / 10 * 10) / 10;
|
|
166
|
-
return {
|
|
167
|
-
dimensions,
|
|
168
|
-
total,
|
|
169
|
-
// dim8 未经实测(干跑)时标注,避免把 triage 分当真实效果。
|
|
170
|
-
evaluated: options.measured === true,
|
|
171
|
-
triageOnly: true,
|
|
172
|
-
};
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
/** 输出可读的 triage 摘要(工具返回值用纯文本时)。 */
|
|
176
|
-
export function formatRubric(result) {
|
|
177
|
-
const rows = result.dimensions.map((dimension) => ` dim${dimension.id} ${dimension.name.padEnd(12)} ${String(dimension.score).padStart(2)}/10 ×${String(dimension.weight).padStart(2)} ${dimension.evidence}`);
|
|
178
|
-
// weakest 必须与 weakestDimension 同口径:dim8 是实测表现,不能靠静态改写
|
|
179
|
-
// 提升,混进来会把优化目标指向一个改不动的维度。
|
|
180
|
-
const editable = result.dimensions.filter((dimension) => dimension.id !== 8);
|
|
181
|
-
const weakest = (editable.length > 0 ? editable : result.dimensions)
|
|
182
|
-
.reduce((worst, dimension) => (dimension.weight * (10 - dimension.score) > worst.weight * (10 - worst.score) ? dimension : worst));
|
|
183
|
-
return [
|
|
184
|
-
`[skillopt rubric] total=${result.total}/100${result.evaluated ? '' : ' (dim8 dry-run: NOT a measured effect score)'}`,
|
|
185
|
-
' ⚠️ absolute totals are TRIAGE-ONLY; keep/revert must use paired judge majority',
|
|
186
|
-
...rows,
|
|
187
|
-
` weakest dim: ${weakest.name}`,
|
|
188
|
-
].join('\n');
|
|
189
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Darwin Skill 9 维 rubric(对齐 alchaincyf/darwin-skill v2.1 的权威定义):
|
|
3
|
+
*
|
|
4
|
+
* 结构维度(59):1 Frontmatter 7 / 2 工作流清晰度 12 / 3 失败模式编码 12 /
|
|
5
|
+
* 4 检查点设计 6 / 5 可执行具体性 18 / 6 资源整合度 4
|
|
6
|
+
* 效果维度(35):7 整体架构 12 / 8 实测表现 23
|
|
7
|
+
* Meta-skill(6):9 反例与黑名单 6
|
|
8
|
+
*
|
|
9
|
+
* 评分规则:维度 1-7、9 打 1-10 分 × 权重;维度 8(实测表现)由调用方注入
|
|
10
|
+
* (跑测试 prompt)或按干跑打分。总分 = Σ(维度分 × 权重) / 10,满分 100。
|
|
11
|
+
*
|
|
12
|
+
* ⚠️ 绝对总分只用于 triage(先改哪支最弱),**绝不用于 keep/revert**——
|
|
13
|
+
* LLM judge 换尺 ±8 噪音会淹没保守编辑的真实增益。keep/revert 一律走
|
|
14
|
+
* judge.js 的 paired 同-judge 多数决。
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export const NINE_DIMENSIONS = Object.freeze([
|
|
18
|
+
{ id: 1, name: 'Frontmatter质量', weight: 7, group: 'structure' },
|
|
19
|
+
{ id: 2, name: '工作流清晰度', weight: 12, group: 'structure' },
|
|
20
|
+
{ id: 3, name: '失败模式编码', weight: 12, group: 'structure' },
|
|
21
|
+
{ id: 4, name: '检查点设计', weight: 6, group: 'structure' },
|
|
22
|
+
{ id: 5, name: '可执行具体性', weight: 18, group: 'structure' },
|
|
23
|
+
{ id: 6, name: '资源整合度', weight: 4, group: 'structure' },
|
|
24
|
+
{ id: 7, name: '整体架构', weight: 12, group: 'effect' },
|
|
25
|
+
{ id: 8, name: '实测表现', weight: 23, group: 'effect' },
|
|
26
|
+
{ id: 9, name: '反例与黑名单', weight: 6, group: 'meta' },
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
/** 软化措辞(dim5 扣分项):出现 ≥3 处扣 ≥3 分。 */
|
|
30
|
+
const SOFT_WORDS = ['建议', '可以考虑', '根据情况', '灵活把握', '视情况而定', '酌情', '尽量'];
|
|
31
|
+
/** AI 腔/废话词(dim7 扣分项):出现一处扣 1 分。 */
|
|
32
|
+
const FILLER_WORDS = ['说白了', '换句话说', '首先其次', '综上所述', '总而言之'];
|
|
33
|
+
/** 失败模式句式(dim3):显式「如果 X → Y」分支,含缺失/为空/超时等触发条件。 */
|
|
34
|
+
const FAILURE_PATTERNS = [
|
|
35
|
+
/如果[^\n]{0,60}(失败|缺失|为空|不存在|不一致|超时|报错)/,
|
|
36
|
+
/兜底/,
|
|
37
|
+
/fallback/i,
|
|
38
|
+
/异常处理/,
|
|
39
|
+
/恢复/,
|
|
40
|
+
];
|
|
41
|
+
/** 检查点显性标记(dim4):仅靠『如果…建议…』措辞不算。 */
|
|
42
|
+
const CHECKPOINT_PATTERNS = [/🔴/, /🛑/, /CHECKPOINT/, /STOP/];
|
|
43
|
+
/** 反例/黑名单章节(dim9)。 */
|
|
44
|
+
const COUNTEREXAMPLE_PATTERNS = [/反例/, /黑名单/, /不要做/, /禁止/, /反模式/];
|
|
45
|
+
|
|
46
|
+
const clampScore = (value) => Math.max(1, Math.min(10, value));
|
|
47
|
+
// 只数**完整匹配**:`String.match` 在带捕获组时会把分组也塞进返回数组,长度
|
|
48
|
+
// 因此虚高(`/(a)(b)/g` 命中一次返回 3 个元素),dim3 分数被系统性抬高。
|
|
49
|
+
const countMatches = (text, patterns) => patterns.reduce((total, pattern) => {
|
|
50
|
+
const flags = pattern.flags.includes('g') ? pattern.flags : `${pattern.flags}g`;
|
|
51
|
+
const global = new RegExp(pattern.source, flags);
|
|
52
|
+
let hits = 0;
|
|
53
|
+
for (const _ of String(text).matchAll(global)) hits += 1;
|
|
54
|
+
return total + hits;
|
|
55
|
+
}, 0);
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* 静态评分(dims 1-7、9)。dim8 由调用方注入(measured dim8 干跑/实测分)。
|
|
59
|
+
*
|
|
60
|
+
* @param {string} text SKILL.md 全文
|
|
61
|
+
* @param {{dim8?: number, measured?: boolean, resourcePaths?: string[]}} [options]
|
|
62
|
+
* @returns {{dimensions: Array<{id:number,name:string,weight:number,score:number,evidence:string}>, total:number, evaluated:boolean, triageOnly:boolean}}
|
|
63
|
+
*/
|
|
64
|
+
export function scoreSkillRubric(text, options = {}) {
|
|
65
|
+
const source = typeof text === 'string' ? text : '';
|
|
66
|
+
// NaN/Infinity 会穿透 clampScore(Math.min/max 对 NaN 返回 NaN)→ 总分变 NaN。
|
|
67
|
+
const dim8 = Number.isFinite(options.dim8) ? clampScore(options.dim8) : 5;
|
|
68
|
+
const evidence = [];
|
|
69
|
+
const scores = new Map();
|
|
70
|
+
|
|
71
|
+
// dim1 Frontmatter:name/description/触发词/长度/空话尾巴
|
|
72
|
+
{
|
|
73
|
+
const hasName = /^---\s*[\s\S]*?name:/m.test(source);
|
|
74
|
+
const hasDescription = /^---\s*[\s\S]*?description:/m.test(source);
|
|
75
|
+
const description = source.match(/description:\s*([^\n]+)/)?.[1] ?? '';
|
|
76
|
+
const hasTriggers = /触发|trigger|当用户|使用场景/i.test(description);
|
|
77
|
+
const vagueTail = /(灵活应用|根据情况判断|视情况)/.test(description.slice(-80));
|
|
78
|
+
let score = 4;
|
|
79
|
+
if (hasName) score += 2;
|
|
80
|
+
if (hasDescription) score += 1;
|
|
81
|
+
if (hasTriggers) score += 2;
|
|
82
|
+
if (description.length > 0 && description.length <= 1024) score += 1;
|
|
83
|
+
if (vagueTail) score -= 2;
|
|
84
|
+
scores.set(1, clampScore(score));
|
|
85
|
+
evidence.push(`dim1 frontmatter: name=${hasName} desc=${hasDescription} triggers=${hasTriggers} vagueTail=${vagueTail}`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// dim2 工作流:编号/标题步骤数
|
|
89
|
+
{
|
|
90
|
+
const steps = (source.match(/^\s{0,4}(\d+[.)、]|#{2,4}\s*(Step|步骤|Phase|阶段)\s*\d)/gim) ?? []).length;
|
|
91
|
+
const score = steps === 0 ? 3 : steps <= 2 ? 5 : steps <= 5 ? 7 : 9;
|
|
92
|
+
scores.set(2, clampScore(score));
|
|
93
|
+
evidence.push(`dim2 workflow steps=${steps}`);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// dim3 失败模式编码:显式失败分支;只写正向流程扣分
|
|
97
|
+
{
|
|
98
|
+
const hits = countMatches(source, FAILURE_PATTERNS);
|
|
99
|
+
const score = hits === 0 ? 2 : hits <= 2 ? 6 : hits <= 5 ? 8 : 9;
|
|
100
|
+
scores.set(3, clampScore(score));
|
|
101
|
+
evidence.push(`dim3 failure patterns=${hits}`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// dim4 检查点:显性标记(🔴/STOP/CHECKPOINT)
|
|
105
|
+
{
|
|
106
|
+
const hits = countMatches(source, CHECKPOINT_PATTERNS);
|
|
107
|
+
const score = hits === 0 ? 2 : hits >= 3 ? 9 : 7;
|
|
108
|
+
scores.set(4, clampScore(score));
|
|
109
|
+
evidence.push(`dim4 explicit checkpoints=${hits}`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// dim5 可执行具体性:软化措辞扣分 + 具体参数/示例加分
|
|
113
|
+
{
|
|
114
|
+
const soft = SOFT_WORDS.reduce((total, word) => total + (source.split(word).length - 1), 0);
|
|
115
|
+
const concrete = (source.match(/```|`[^`\n]+`|--[a-z-]+|\d+(\.\d+)?(ms|s|px|%|KB|MB)/g) ?? []).length;
|
|
116
|
+
let score = 6 + Math.min(3, Math.floor(concrete / 8));
|
|
117
|
+
if (soft >= 3) score -= Math.min(5, soft);
|
|
118
|
+
scores.set(5, clampScore(score));
|
|
119
|
+
evidence.push(`dim5 softWords=${soft} concreteHints=${concrete}`);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// dim6 资源整合度:references/scripts/assets 引用是否真的可达
|
|
123
|
+
{
|
|
124
|
+
const refs = (source.match(/(references|scripts|assets)\/[^\s)`"']+/g) ?? []).length;
|
|
125
|
+
// 没给 resourcePaths 时**不能**把「引用数」当成「可达数」:那会让任何
|
|
126
|
+
// 含 references/ 的文本恒得 9 分(自证可达)。没有可达清单时按「未验证」
|
|
127
|
+
// 走中性分,并在证据里标注。
|
|
128
|
+
const hasKnown = Array.isArray(options.resourcePaths);
|
|
129
|
+
const known = hasKnown ? options.resourcePaths.length : 0;
|
|
130
|
+
const score = refs === 0
|
|
131
|
+
? (hasKnown ? 6 : 5)
|
|
132
|
+
: hasKnown
|
|
133
|
+
? (known >= refs ? 9 : 6)
|
|
134
|
+
: 6;
|
|
135
|
+
scores.set(6, clampScore(score));
|
|
136
|
+
evidence.push(`dim6 resource refs=${refs} reachable=${hasKnown ? known : 'unverified'}`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// dim7 整体架构:层级 + 冗余扣分
|
|
140
|
+
{
|
|
141
|
+
const headings = (source.match(/^#{1,4}\s+/gm) ?? []).length;
|
|
142
|
+
const filler = FILLER_WORDS.reduce((total, word) => total + (source.split(word).length - 1), 0);
|
|
143
|
+
let score = headings >= 5 ? 8 : headings >= 2 ? 6 : 4;
|
|
144
|
+
score -= Math.min(4, filler);
|
|
145
|
+
scores.set(7, clampScore(score));
|
|
146
|
+
evidence.push(`dim7 headings=${headings} filler=${filler}`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// dim8 实测表现:调用方注入(measured=true 时才是实测)
|
|
150
|
+
scores.set(8, dim8);
|
|
151
|
+
|
|
152
|
+
// dim9 反例与黑名单
|
|
153
|
+
{
|
|
154
|
+
const hits = countMatches(source, COUNTEREXAMPLE_PATTERNS);
|
|
155
|
+
const score = hits === 0 ? 2 : hits >= 2 ? 9 : 7;
|
|
156
|
+
scores.set(9, clampScore(score));
|
|
157
|
+
evidence.push(`dim9 counterexamples=${hits}`);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const dimensions = NINE_DIMENSIONS.map((dimension) => ({
|
|
161
|
+
...dimension,
|
|
162
|
+
score: scores.get(dimension.id),
|
|
163
|
+
evidence: evidence.find((line) => line.startsWith(`dim${dimension.id} `)) ?? '',
|
|
164
|
+
}));
|
|
165
|
+
const total = Math.round(dimensions.reduce((sum, dimension) => sum + dimension.score * dimension.weight, 0) / 10 * 10) / 10;
|
|
166
|
+
return {
|
|
167
|
+
dimensions,
|
|
168
|
+
total,
|
|
169
|
+
// dim8 未经实测(干跑)时标注,避免把 triage 分当真实效果。
|
|
170
|
+
evaluated: options.measured === true,
|
|
171
|
+
triageOnly: true,
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** 输出可读的 triage 摘要(工具返回值用纯文本时)。 */
|
|
176
|
+
export function formatRubric(result) {
|
|
177
|
+
const rows = result.dimensions.map((dimension) => ` dim${dimension.id} ${dimension.name.padEnd(12)} ${String(dimension.score).padStart(2)}/10 ×${String(dimension.weight).padStart(2)} ${dimension.evidence}`);
|
|
178
|
+
// weakest 必须与 weakestDimension 同口径:dim8 是实测表现,不能靠静态改写
|
|
179
|
+
// 提升,混进来会把优化目标指向一个改不动的维度。
|
|
180
|
+
const editable = result.dimensions.filter((dimension) => dimension.id !== 8);
|
|
181
|
+
const weakest = (editable.length > 0 ? editable : result.dimensions)
|
|
182
|
+
.reduce((worst, dimension) => (dimension.weight * (10 - dimension.score) > worst.weight * (10 - worst.score) ? dimension : worst));
|
|
183
|
+
return [
|
|
184
|
+
`[skillopt rubric] total=${result.total}/100${result.evaluated ? '' : ' (dim8 dry-run: NOT a measured effect score)'}`,
|
|
185
|
+
' ⚠️ absolute totals are TRIAGE-ONLY; keep/revert must use paired judge majority',
|
|
186
|
+
...rows,
|
|
187
|
+
` weakest dim: ${weakest.name}`,
|
|
188
|
+
].join('\n');
|
|
189
|
+
}
|