goal-contract.test.js
21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* goal-contract.js 单测(R5-F3)
*
* 覆盖 27-P3/P4 §六 验收标准 1:
* 「手写一份 goal-benchmark + goal-test-data + goal-test-script,
* 执行器能对一个被测产物跑出明确的通过/不通过判决,判决落库可查」
*
* 三段各跑一次「通过」和一次「不通过」;test_script 起真实 node 子进程,
* 被测脚本就地生成(不依赖外部环境)。最后一组用例直接 spawn CLI,
* 跑的就是三个 validator 模板 tools.md 里印的那几行命令——「实现在、零调用方」到此为止。
*/
import { describe, it, expect, beforeEach, afterAll } from 'vitest';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { execFileSync } from 'child_process';
const gc = require('../../services/goal-contract');
const CLI = path.resolve(__dirname, '../../services/goal-contract.js');
const tmpRoots = [];
/** 一个干净的 run 目录:data/companies/{cid}/runs/{rootRunId} 的等价物 */
function newRunDir() {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'goal-contract-'));
tmpRoots.push(root);
const runDir = path.join(root, 'runs', 'run_test');
fs.mkdirSync(path.join(runDir, 'artifacts'), { recursive: true });
return runDir;
}
function writeJson(abs, json) {
fs.mkdirSync(path.dirname(abs), { recursive: true });
fs.writeFileSync(abs, JSON.stringify(json, null, 2));
return abs;
}
/** 被测产物:nodes/{nodeRunId}/attempt-{n}.json —— 就是 output_ref 指向的那个 */
function writeTarget(runDir, nodeRunId, attempt, json) {
return writeJson(path.join(runDir, 'nodes', nodeRunId, `attempt-${attempt}.json`), json);
}
/** 就地生成一个最小 node 脚本当验收脚本;不依赖任何外部环境 */
function writeScript(runDir, rel, body) {
const abs = path.join(runDir, 'artifacts', rel);
fs.mkdirSync(path.dirname(abs), { recursive: true });
fs.writeFileSync(abs, body);
return abs;
}
const BENCHMARK = {
dimensions: [
{ name: 'coverage', weight: 0.6, pass_threshold: 80 },
{ name: 'readability', weight: 0.4, pass_threshold: 70 },
],
overall_pass_threshold: 78,
};
const TEST_DATA = {
match: 'deep-equal',
cases: [
{ id: 'c-normal', input: { n: 2 }, expect: { out: 4 } },
{ id: 'c-edge', input: { n: 0 }, expect: { out: 0 } },
],
};
/** ContractValidationError 的 message 是固定的,细节在 .errors 里 */
function errorsOf(fn) {
try { fn(); } catch (e) { return e.errors || [{ rule: '?', detail: e.message }]; }
throw new Error('expected to throw');
}
function runCli(args, opts = {}) {
try {
const stdout = execFileSync(process.execPath, [CLI, ...args], { encoding: 'utf8', ...opts });
return { code: 0, stdout, stderr: '' };
} catch (e) {
return { code: e.status, stdout: String(e.stdout || ''), stderr: String(e.stderr || '') };
}
}
afterAll(() => {
for (const r of tmpRoots) fs.rmSync(r, { recursive: true, force: true });
});
// ────────────────────────────────────────────────────────────────
describe('契约 schema 校验', () => {
it('合法契约通过(三段齐全,benchmark 走 $ref 外置)', () => {
const runDir = newRunDir();
writeJson(path.join(runDir, 'artifacts', 'goal-benchmark.json'), BENCHMARK);
writeScript(runDir, 'goal-test-script/acceptance.js', 'process.exit(0)\n');
const contract = {
version: '1',
goal_id: 'GOAL-ok',
benchmark: { $ref: 'goal-benchmark.json' },
test_data: TEST_DATA,
test_script: { runner: 'node', path: 'goal-test-script/acceptance.js' },
};
expect(gc.validateContract(contract)).toEqual({ ok: true, errors: [] });
const { contract_hash } = gc.writeContract(runDir, contract);
expect(contract_hash).toMatch(/^sha256:/);
// 读回时 $ref 已解开,且 hash 与写入时一致(内联写和 $ref 写必须同一个 hash)
const loaded = gc.readContract(runDir);
expect(loaded.contract.benchmark).toEqual(BENCHMARK);
expect(gc.contractHash(loaded.contract)).toBe(contract_hash);
});
it('非法契约被逐条挡下:空契约 / runner 越界 / 路径逃逸 / 权重不足 1 / 未知字段', () => {
const rules = (c) => gc.validateContract(c).errors.map((e) => e.rule);
// 空契约(三段全 null)——「平凡通过」的漏洞在格式层就堵掉
expect(rules({ version: '1', goal_id: 'G' })).toContain('section');
// runner 白名单
expect(rules({ version: '1', goal_id: 'G', test_script: { runner: 'bash', path: 'a.sh' } })).toContain('runner');
// 路径逃逸
expect(rules({
version: '1', goal_id: 'G',
test_script: { runner: 'node', path: '../../../etc/passwd' },
})).toContain('reference');
// 权重之和 ≠ 1
expect(rules({
version: '1', goal_id: 'G',
benchmark: { dimensions: [{ name: 'a', weight: 0.3 }] },
})).toContain('section');
// 版本 / 缺 goal_id / 未知字段
expect(rules({ version: '2', test_data: TEST_DATA, wat: 1 }).filter((r) => r === 'schema').length).toBe(3);
// created_by 支持 {section: producer} 映射形态(三个产出节点各写一段)
expect(gc.validateContract({
version: '1', goal_id: 'G', created_by: { benchmark: 'goal-benchmark' }, test_data: TEST_DATA,
}).ok).toBe(true);
expect(rules({ version: '1', goal_id: 'G', created_by: [1], test_data: TEST_DATA })).toContain('schema');
});
it('writeContract 拒绝非法契约,且 $ref 指向的文件不存在时就报错(不等到执行时才炸)', () => {
const runDir = newRunDir();
expect(() => gc.writeContract(runDir, { version: '1', goal_id: 'G' }))
.toThrow(gc.ContractValidationError);
expect(() => gc.writeContract(runDir, {
version: '1', goal_id: 'G', benchmark: { $ref: 'nope.json' },
})).toThrow(gc.ContractValidationError);
expect(errorsOf(() => gc.writeContract(runDir, {
version: '1', goal_id: 'G', benchmark: { $ref: 'nope.json' },
}))[0].detail).toMatch(/unreadable/);
expect(fs.existsSync(gc.resolveContractPath(runDir))).toBe(false);
});
});
// ────────────────────────────────────────────────────────────────
describe('三个产出节点各写一段,互不覆盖', () => {
it('writeContractSection 合并写入,后写的不抹掉先写的', () => {
const runDir = newRunDir();
writeJson(path.join(runDir, 'artifacts', 'goal-benchmark.json'), BENCHMARK);
writeScript(runDir, 'goal-test-script/acceptance.js', 'process.exit(0)\n');
gc.writeContractSection(runDir, {
section: 'benchmark', content: { $ref: 'goal-benchmark.json' },
goalId: 'GOAL-merge', createdBy: 'goal-benchmark',
});
gc.writeContractSection(runDir, {
section: 'test_data', content: TEST_DATA, goalId: 'GOAL-merge', createdBy: 'goal-test-data',
});
const last = gc.writeContractSection(runDir, {
section: 'test_script', content: { runner: 'node', path: 'goal-test-script/acceptance.js' },
goalId: 'GOAL-merge', createdBy: 'goal-test-script',
});
expect(last.sections).toEqual(['benchmark', 'test_data', 'test_script']);
const raw = JSON.parse(fs.readFileSync(gc.resolveContractPath(runDir), 'utf8'));
expect(raw.benchmark).toEqual({ $ref: 'goal-benchmark.json' }); // 合并没有把 $ref 抄开
expect(raw.test_data).toEqual(TEST_DATA);
expect(raw.created_by).toEqual({
benchmark: 'goal-benchmark', test_data: 'goal-test-data', test_script: 'goal-test-script',
});
});
it('goal_id 串台直接拒绝(拿错 runDir 时不静默覆盖别人的契约)', () => {
const runDir = newRunDir();
gc.writeContractSection(runDir, { section: 'test_data', content: TEST_DATA, goalId: 'GOAL-a' });
expect(errorsOf(() => gc.writeContractSection(runDir, {
section: 'benchmark', content: BENCHMARK, goalId: 'GOAL-b',
}))[0].detail).toMatch(/goal_id mismatch/);
expect(errorsOf(() => gc.writeContractSection(runDir, { section: 'nope', content: {} }))[0].detail)
.toMatch(/section must be one of/);
// 被拒之后原契约一字未动
expect(gc.readContract(runDir).contract.goal_id).toBe('GOAL-a');
expect(gc.readContract(runDir).contract.benchmark).toBeUndefined();
});
});
// ────────────────────────────────────────────────────────────────
describe('执行器:benchmark', () => {
const contract = { version: '1', goal_id: 'GOAL-bm', benchmark: BENCHMARK };
it('通过:逐维度过线且加权总分达标', async () => {
const runDir = newRunDir();
const target = writeTarget(runDir, 'rn_a', 1, { scores: { coverage: 92, readability: 85 } });
const v = await gc.executeContract(contract, target, { runDir });
expect(v.ok).toBe(true);
expect(v.fail_reason).toBe(null);
expect(v.sections.benchmark.score).toBeCloseTo(89.2, 4);
expect(v.failures).toEqual([]);
expect(v.target).toBe(path.join('nodes', 'rn_a', 'attempt-1.json')); // run 目录内 → 相对路径
});
it('不通过:一维没过线 + 一维缺分', async () => {
const runDir = newRunDir();
const target = writeTarget(runDir, 'rn_a', 1, { scores: { coverage: 61 } });
const v = await gc.executeContract(contract, target, { runDir });
expect(v.ok).toBe(false);
expect(v.fail_reason).toBe('acceptance_not_met');
expect(v.failures.map((f) => f.case)).toEqual(['coverage', 'readability']);
expect(v.failures[1].hint).toMatch(/missing score/);
});
it('被测产物根本不存在 → 不通过(不是崩溃,也不是平凡通过)', async () => {
const runDir = newRunDir();
const v = await gc.executeContract(contract, path.join(runDir, 'nodes/rn_x/attempt-1.json'), { runDir });
expect(v.ok).toBe(false);
expect(v.sections.benchmark.reason).toBe('target_not_json');
});
});
// ────────────────────────────────────────────────────────────────
describe('执行器:test_data', () => {
it('通过(subset 模式忽略额外键)', async () => {
const runDir = newRunDir();
const target = writeTarget(runDir, 'rn_b', 1, {
results: { 'c-normal': { out: 4, extra: 'ignored' }, 'c-edge': { out: 0 } },
});
const v = await gc.executeContract(
{ version: '1', goal_id: 'G', test_data: { ...TEST_DATA, match: 'subset' } }, target, { runDir },
);
expect(v.ok).toBe(true);
expect(v.sections.test_data).toMatchObject({ total: 2, passed: 2, failed: 0, match: 'subset' });
});
it('不通过:一个值错、一个 case 缺结果(两者 hint 要分得开)', async () => {
const runDir = newRunDir();
const target = writeTarget(runDir, 'rn_b', 1, { results: [{ id: 'c-normal', output: { out: 5 } }] });
const v = await gc.executeContract({ version: '1', goal_id: 'G', test_data: TEST_DATA }, target, { runDir });
expect(v.ok).toBe(false);
expect(v.fail_reason).toBe('acceptance_not_met');
expect(v.sections.test_data.failed).toBe(2);
expect(v.sections.test_data.failures[0].hint).toMatch(/mismatch/);
expect(v.sections.test_data.failures[1].hint).toMatch(/no result for case c-edge/);
});
});
// ────────────────────────────────────────────────────────────────
describe('执行器:test_script(真子进程)', () => {
it('通过:脚本读到 ACCEPTANCE_TARGET 并 exit 0', async () => {
const runDir = newRunDir();
const target = writeTarget(runDir, 'rn_c', 1, { answer: 42 });
writeScript(runDir, 'goal-test-script/acceptance.js', `
const fs = require('fs');
const t = JSON.parse(fs.readFileSync(process.env.ACCEPTANCE_TARGET, 'utf8'));
process.exit(t.answer === 42 ? 0 : 1);
`);
const v = await gc.executeContract({
version: '1', goal_id: 'G',
test_script: { runner: 'node', path: 'goal-test-script/acceptance.js', timeout_ms: 20000 },
}, target, { runDir });
expect(v.ok).toBe(true);
expect(v.sections.test_script.exit_code).toBe(0);
});
it('不通过:断言不成立 → exit 1,stderr 进 hint', async () => {
const runDir = newRunDir();
const target = writeTarget(runDir, 'rn_c', 1, { answer: 41 });
writeScript(runDir, 'goal-test-script/acceptance.js', `
const fs = require('fs');
const t = JSON.parse(fs.readFileSync(process.env.ACCEPTANCE_TARGET, 'utf8'));
if (t.answer !== 42) { console.error('answer=' + t.answer + ' expected 42'); process.exit(1); }
`);
const v = await gc.executeContract({
version: '1', goal_id: 'G',
test_script: { runner: 'node', path: 'goal-test-script/acceptance.js', timeout_ms: 20000 },
}, target, { runDir });
expect(v.ok).toBe(false);
expect(v.fail_reason).toBe('acceptance_not_met');
expect(v.sections.test_script.exit_code).toBe(1);
expect(v.failures[0].hint).toMatch(/answer=41/);
});
it('超时 → infra_flake(引擎侧原地重跑、不烧计数的那一档)', async () => {
const runDir = newRunDir();
const target = writeTarget(runDir, 'rn_c', 1, {});
writeScript(runDir, 'goal-test-script/hang.js', 'setTimeout(() => {}, 30000);\n');
const v = await gc.executeContract({
version: '1', goal_id: 'G',
test_script: { runner: 'node', path: 'goal-test-script/hang.js', timeout_ms: 300 },
}, target, { runDir });
expect(v.ok).toBe(false);
expect(v.sections.test_script.timed_out).toBe(true);
expect(v.fail_reason).toBe('infra_flake');
}, 20000);
it('脚本文件缺失 / 路径逃逸都不起进程', async () => {
const runDir = newRunDir();
const target = writeTarget(runDir, 'rn_c', 1, {});
const v = await gc.executeContract({
version: '1', goal_id: 'G', test_script: { runner: 'node', path: 'goal-test-script/nope.js' },
}, target, { runDir });
expect(v.sections.test_script.reason).toBe('script_missing');
});
it('契约本身不合法 → input_underspecified(不是产物的错)', async () => {
const runDir = newRunDir();
const target = writeTarget(runDir, 'rn_c', 1, {});
const v = await gc.executeContract({ version: '1', goal_id: 'G' }, target, { runDir });
expect(v.ok).toBe(false);
expect(v.fail_reason).toBe('input_underspecified');
});
});
// ────────────────────────────────────────────────────────────────
describe('判决持久化:落库后能读回', () => {
let runDir;
beforeEach(() => {
runDir = newRunDir();
writeJson(path.join(runDir, 'artifacts', 'goal-benchmark.json'), BENCHMARK);
gc.writeContract(runDir, {
version: '1', goal_id: 'GOAL-persist', benchmark: { $ref: 'goal-benchmark.json' },
});
});
it('runAcceptance 读契约 → 执行 → 落盘,判决与索引都可读回', async () => {
const good = writeTarget(runDir, 'rn_ok', 1, { scores: { coverage: 92, readability: 85 } });
const bad = writeTarget(runDir, 'rn_bad', 1, { scores: { coverage: 10, readability: 10 } });
const a = await gc.runAcceptance({ runDir, targetPath: good, runId: 'run_1', nodeRunId: 'rn_ok', attempt: 0 });
const b = await gc.runAcceptance({ runDir, targetPath: bad, runId: 'run_1', nodeRunId: 'rn_bad', attempt: 2 });
expect(a.verdict.ok).toBe(true);
expect(b.verdict.ok).toBe(false);
expect(fs.existsSync(a.path)).toBe(true);
// 完整判决读得回,且四元组齐(attempt=0 是**第一次尝试**,不能被吞成 null)
const back = gc.readVerdict(runDir, a.verdict.verdict_id);
expect(back.contract_hash).toBe(a.verdict.contract_hash);
expect(back.attempt).toBe(0);
expect(back.run_id).toBe('run_1');
expect(back.node_run_id).toBe('rn_ok');
// 索引也留着 attempt=0,否则「给一个 node_run_id 查历次判决」少一环
const all = gc.listVerdicts(runDir);
expect(all.map((r) => r.verdict_id)).toEqual([a.verdict.verdict_id, b.verdict.verdict_id]);
expect(all[0].attempt).toBe(0);
expect(gc.listVerdicts(runDir, { onlyFailed: true }).map((r) => r.verdict_id))
.toEqual([b.verdict.verdict_id]);
expect(gc.listVerdicts(runDir, { nodeRunId: 'rn_bad' }).map((r) => r.verdict_id))
.toEqual([b.verdict.verdict_id]);
expect(gc.listVerdicts(runDir, { limit: 1 }).map((r) => r.verdict_id))
.toEqual([b.verdict.verdict_id]);
expect(gc.readVerdict(runDir, 'vd-nope')).toBe(null);
});
it('没有契约时 runAcceptance 明确报错(而不是判通过)', async () => {
const empty = newRunDir();
await expect(gc.runAcceptance({ runDir: empty, targetPath: 'x' }))
.rejects.toThrow(gc.ContractValidationError);
});
});
// ────────────────────────────────────────────────────────────────
// 生产路径:模板里印的就是这几行命令
// ────────────────────────────────────────────────────────────────
describe('CLI(validator / 产出节点在 bash 里真正调的那一层)', () => {
it('put ×3 → validate → run,与模板 tools.md 的命令一致', () => {
const runDir = newRunDir();
const bmFile = writeJson(path.join(runDir, 'artifacts', 'goal-benchmark.json'), BENCHMARK);
const tdFile = writeJson(path.join(runDir, 'workspace-test-data.json'), TEST_DATA);
writeScript(runDir, 'goal-test-script/acceptance.js', `
const fs = require('fs');
const t = JSON.parse(fs.readFileSync(process.env.ACCEPTANCE_TARGET, 'utf8'));
if (!t.scores) { console.error('no scores'); process.exit(1); }
`);
const scriptFile = writeJson(path.join(runDir, 'workspace-script-section.json'), {
runner: 'node', path: 'goal-test-script/acceptance.js', timeout_ms: 20000,
});
// 产出侧:三个节点各写一段(benchmark 用 --ref 走外置大文件)
expect(runCli(['put', '--run-dir', runDir, '--section', 'benchmark',
'--ref', 'goal-benchmark.json', '--goal-id', 'GOAL-cli', '--created-by', 'goal-benchmark']).code).toBe(0);
expect(runCli(['put', '--run-dir', runDir, '--section', 'test_data',
'--file', tdFile, '--created-by', 'goal-test-data']).code).toBe(0);
const put3 = runCli(['put', '--run-dir', runDir, '--section', 'test_script',
'--file', scriptFile, '--created-by', 'goal-test-script']);
expect(put3.code).toBe(0);
expect(put3.stdout).toMatch(/sections: benchmark, test_data, test_script/);
expect(fs.existsSync(bmFile)).toBe(true);
// planner-validator 侧:只校验契约本身
const valid = runCli(['validate', '--run-dir', runDir]);
expect(valid.code).toBe(0);
expect(valid.stdout).toMatch(/契约合法/);
// org-validator / coder-validator 侧:跑契约。run-dir / node-run-id / attempt 都从 target 推
const good = writeTarget(runDir, 'rn_cli', 3, {
scores: { coverage: 92, readability: 85 },
results: { 'c-normal': { out: 4 }, 'c-edge': { out: 0 } },
});
const pass = runCli(['run', '--target', good, '--run-id', 'run_cli']);
expect(pass.code).toBe(0);
expect(pass.stdout).toMatch(/VERDICT pass/);
const bad = writeTarget(runDir, 'rn_cli', 4, {
scores: { coverage: 10, readability: 10 }, results: {},
});
const fail = runCli(['run', '--target', bad, '--run-id', 'run_cli']);
expect(fail.code).toBe(1); // 1 = 判决为 fail,不是工具坏了
expect(fail.stdout).toMatch(/VERDICT fail\s+fail_reason=acceptance_not_met/);
// 判决落库可查,且 attempt 是从 target 文件名推出来的
const rows = gc.listVerdicts(runDir, { nodeRunId: 'rn_cli' });
expect(rows.map((r) => r.attempt)).toEqual([3, 4]);
expect(rows.map((r) => r.ok)).toEqual([true, false]);
expect(runCli(['list', '--run-dir', runDir, '--only-failed']).stdout).toMatch(/fail/);
expect(runCli(['show', '--run-dir', runDir, '--verdict-id', rows[0].verdict_id]).code).toBe(0);
}, 30000);
it('契约非法 / 没契约 → 退出码 2(与「判决 fail」的 1 区分开)', () => {
const runDir = newRunDir();
// 没契约
expect(runCli(['validate', '--run-dir', runDir]).code).toBe(2);
// 契约在但非法(绕过 writeContract 直接落一份空契约,模拟产出节点写坏)
writeJson(gc.resolveContractPath(runDir), { version: '1', goal_id: 'G' });
const r = runCli(['validate', '--run-dir', runDir]);
expect(r.code).toBe(1);
expect(r.stdout).toMatch(/input_underspecified/);
// 参数错
expect(runCli(['run', '--run-dir', runDir]).code).toBe(2);
expect(runCli(['nosuchcmd']).code).toBe(2);
}, 20000);
it('deriveRunDir / deriveTargetIds:agent 只有产物路径时也能定位', () => {
const runDir = newRunDir();
gc.writeContract(runDir, { version: '1', goal_id: 'G', test_data: TEST_DATA });
const target = writeTarget(runDir, 'rn_d', 7, {});
expect(gc.deriveRunDir(target)).toBe(path.resolve(runDir));
expect(gc.deriveTargetIds(target)).toEqual({ nodeRunId: 'rn_d', attempt: 7 });
expect(gc.deriveRunDir(path.join(os.tmpdir(), 'nowhere', 'x.json'))).toBe(null);
});
});