vigiles 2.4.0 → 2.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -3
- package/dist/agent-result.d.ts +40 -0
- package/dist/agent-result.js +97 -0
- package/dist/agent-runtime.d.ts +64 -0
- package/dist/agent-runtime.js +147 -0
- package/dist/cli.js +106 -0
- package/dist/compile.d.ts +32 -3
- package/dist/compile.js +268 -0
- package/dist/eval-cache.d.ts +33 -0
- package/dist/eval-cache.js +94 -0
- package/dist/eval.d.ts +172 -9
- package/dist/eval.js +319 -58
- package/dist/harness-assert.d.ts +140 -12
- package/dist/harness-assert.js +283 -25
- package/dist/harness-test.d.ts +97 -11
- package/dist/harness-test.js +147 -37
- package/dist/judge.js +2 -0
- package/dist/linters.d.ts +6 -0
- package/dist/linters.js +1 -0
- package/dist/mock-entry.d.ts +2 -0
- package/dist/mock-entry.js +36 -0
- package/dist/mock-model.d.ts +29 -0
- package/dist/mock-model.js +40 -0
- package/dist/plugin-loader.js +51 -17
- package/dist/sandbox.d.ts +76 -0
- package/dist/sandbox.js +241 -0
- package/dist/spec.d.ts +130 -0
- package/dist/spec.js +55 -0
- package/dist/stats.d.ts +49 -0
- package/dist/stats.js +109 -0
- package/package.json +5 -3
package/dist/harness-assert.js
CHANGED
|
@@ -1,20 +1,40 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.vigilesMatchers = void 0;
|
|
3
|
+
exports.vigilesMatchers = exports.compareArms = void 0;
|
|
4
4
|
exports.withHarness = withHarness;
|
|
5
5
|
exports.assertCreated = assertCreated;
|
|
6
6
|
exports.assertNotCreated = assertNotCreated;
|
|
7
7
|
exports.assertServedTurns = assertServedTurns;
|
|
8
8
|
exports.assertHookBlocked = assertHookBlocked;
|
|
9
9
|
exports.assertHookAllowed = assertHookAllowed;
|
|
10
|
+
exports.assertAgentOk = assertAgentOk;
|
|
11
|
+
exports.assertAgentErr = assertAgentErr;
|
|
12
|
+
exports.assertAgentResult = assertAgentResult;
|
|
13
|
+
exports.usedTool = usedTool;
|
|
14
|
+
exports.toolCount = toolCount;
|
|
15
|
+
exports.skillResolved = skillResolved;
|
|
16
|
+
exports.toolUsedWith = toolUsedWith;
|
|
17
|
+
exports.outputContains = outputContains;
|
|
18
|
+
exports.requestContains = requestContains;
|
|
19
|
+
exports.hookFired = hookFired;
|
|
20
|
+
exports.hookBlocked = hookBlocked;
|
|
10
21
|
exports.assertToolUsed = assertToolUsed;
|
|
11
22
|
exports.assertToolNotUsed = assertToolNotUsed;
|
|
12
23
|
exports.assertSkillResolved = assertSkillResolved;
|
|
24
|
+
exports.assertToolUsedWith = assertToolUsedWith;
|
|
25
|
+
exports.assertOutputContains = assertOutputContains;
|
|
26
|
+
exports.assertRequestContains = assertRequestContains;
|
|
27
|
+
exports.assertHookFired = assertHookFired;
|
|
13
28
|
exports.assertToolCount = assertToolCount;
|
|
14
29
|
exports.assertToolSequence = assertToolSequence;
|
|
15
30
|
exports.assertToolCalls = assertToolCalls;
|
|
31
|
+
exports.reliable = reliable;
|
|
32
|
+
exports.assertReliable = assertReliable;
|
|
16
33
|
exports.improvement = improvement;
|
|
34
|
+
exports.significantlyBeats = significantlyBeats;
|
|
35
|
+
exports.assertSignificant = assertSignificant;
|
|
17
36
|
exports.assertImproves = assertImproves;
|
|
37
|
+
exports.assertTriggerRate = assertTriggerRate;
|
|
18
38
|
/**
|
|
19
39
|
* vigiles — runner-agnostic helpers for harness tests / evals.
|
|
20
40
|
*
|
|
@@ -31,11 +51,18 @@ exports.assertImproves = assertImproves;
|
|
|
31
51
|
* vitest and jest, so the same object supports both.
|
|
32
52
|
*/
|
|
33
53
|
const harness_test_js_1 = require("./harness-test.js");
|
|
54
|
+
const agent_result_js_1 = require("./agent-result.js");
|
|
55
|
+
const stats_js_1 = require("./stats.js");
|
|
56
|
+
// Re-export the significance primitives so the whole eval-analysis surface lives
|
|
57
|
+
// behind `vigiles/harness-assert` (no separate entry point).
|
|
58
|
+
var stats_js_2 = require("./stats.js");
|
|
59
|
+
Object.defineProperty(exports, "compareArms", { enumerable: true, get: function () { return stats_js_2.compareArms; } });
|
|
34
60
|
/**
|
|
35
61
|
* Run a harness test, hand the result to `fn`, and always clean up the sandbox.
|
|
36
62
|
* Returns whatever `fn` returns. Use this instead of calling `cleanup()` by
|
|
37
63
|
* hand — it survives assertion failures.
|
|
38
64
|
*/
|
|
65
|
+
/* v8 ignore start -- thin wrapper over runHarnessTest (spawns the real CLI) */
|
|
39
66
|
async function withHarness(spec, fn) {
|
|
40
67
|
const r = await (0, harness_test_js_1.runHarnessTest)(spec);
|
|
41
68
|
try {
|
|
@@ -45,6 +72,7 @@ async function withHarness(spec, fn) {
|
|
|
45
72
|
r.cleanup();
|
|
46
73
|
}
|
|
47
74
|
}
|
|
75
|
+
/* v8 ignore stop */
|
|
48
76
|
// --- Plain throwing assertions (any runner) --------------------------------
|
|
49
77
|
function fail(message) {
|
|
50
78
|
throw new Error(message);
|
|
@@ -77,11 +105,124 @@ function assertHookAllowed(r) {
|
|
|
77
105
|
fail(`expected the hook to allow, but it blocked (exit ${String(r.exitCode)}, decision ${String(r.decision)})`);
|
|
78
106
|
}
|
|
79
107
|
}
|
|
108
|
+
// --- subagent railway outcome (parse the worker's result block) ------------
|
|
109
|
+
//
|
|
110
|
+
// A subagent with a result() contract ends its turn with a vigiles:ok/err block.
|
|
111
|
+
// These wrap parseAgentResult so a test can assert the worker's *outcome* the
|
|
112
|
+
// same way it asserts a hook decision — the testing-framework payoff of the
|
|
113
|
+
// railway contract: `assertAgentOk(r.output)` instead of substring-matching prose.
|
|
114
|
+
/**
|
|
115
|
+
* Assert the worker's output is a success result, and return its `value`. With a
|
|
116
|
+
* `contract`, the value is validated against the success shape (a wrong/missing
|
|
117
|
+
* field fails the assertion). A malformed or error result throws.
|
|
118
|
+
*/
|
|
119
|
+
function assertAgentOk(output, contract) {
|
|
120
|
+
const r = (0, agent_result_js_1.parseAgentResult)(output, contract);
|
|
121
|
+
if (r.kind === "ok")
|
|
122
|
+
return r.value;
|
|
123
|
+
const why = r.kind === "err" ? "returned an error result" : r.reason;
|
|
124
|
+
return fail(`expected a success result from the subagent, but ${why}`);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Assert the worker's output is an error result, and return its `error`. The
|
|
128
|
+
* railway's error track — proves the worker reported failure with rich detail
|
|
129
|
+
* (not that it crashed or returned prose). A malformed or success result throws.
|
|
130
|
+
*/
|
|
131
|
+
function assertAgentErr(output, contract) {
|
|
132
|
+
const r = (0, agent_result_js_1.parseAgentResult)(output, contract);
|
|
133
|
+
if (r.kind === "err")
|
|
134
|
+
return r.error;
|
|
135
|
+
const why = r.kind === "ok" ? "returned a success result" : r.reason;
|
|
136
|
+
return fail(`expected an error result from the subagent, but ${why}`);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Assert the parsed result satisfies `predicate` — the general form, for
|
|
140
|
+
* checking rich detail (e.g. `(r) => r.kind === "ok" && r.value.files.length > 0`).
|
|
141
|
+
*/
|
|
142
|
+
function assertAgentResult(output, predicate, contract) {
|
|
143
|
+
const r = (0, agent_result_js_1.parseAgentResult)(output, contract);
|
|
144
|
+
if (!predicate(r)) {
|
|
145
|
+
const detail = r.kind === "malformed" ? ` (${r.reason})` : "";
|
|
146
|
+
fail(`subagent result did not satisfy the predicate: ${r.kind}${detail}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
80
149
|
function nameMatches(name, pat) {
|
|
81
150
|
return typeof pat === "string" ? name === pat : pat.test(name);
|
|
82
151
|
}
|
|
83
|
-
function toolNames(
|
|
84
|
-
return
|
|
152
|
+
function toolNames(trace) {
|
|
153
|
+
return trace.toolCalls.map((c) => c.name).join(", ") || "none";
|
|
154
|
+
}
|
|
155
|
+
// --- bare predicates over a Trace (the shared vocabulary, no throw) ---------
|
|
156
|
+
//
|
|
157
|
+
// Pure fns returning a value, so the SAME vocabulary runs in both consumers:
|
|
158
|
+
// the throwing `assert*` helpers below wrap them for the testing tier, and an
|
|
159
|
+
// eval `measure` reuses them directly as metrics (`measure: (t) => ({ safe:
|
|
160
|
+
// !usedTool(t, /merge|delete/) })`). Never one dual-purpose function.
|
|
161
|
+
/**
|
|
162
|
+
* Did the agent invoke a tool whose name matches `name` (string = exact,
|
|
163
|
+
* RegExp = test)? The predicate behind `assertToolUsed` / `assertToolNotUsed`.
|
|
164
|
+
*/
|
|
165
|
+
function usedTool(trace, name) {
|
|
166
|
+
return trace.toolCalls.some((c) => nameMatches(c.name, name));
|
|
167
|
+
}
|
|
168
|
+
/** How many tools matching `name` the agent invoked. Behind `assertToolCount`. */
|
|
169
|
+
function toolCount(trace, name) {
|
|
170
|
+
return trace.toolCalls.filter((c) => nameMatches(c.name, name)).length;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Did the `Skill` tool resolve `skill` (e.g. `"superpowers:test-driven-development"`)
|
|
174
|
+
* without error? The skill-activation predicate behind `assertSkillResolved`.
|
|
175
|
+
*/
|
|
176
|
+
function skillResolved(trace, skill) {
|
|
177
|
+
const call = trace.toolCalls.find((c) => c.name === "Skill" && c.input?.skill === skill);
|
|
178
|
+
return call !== undefined && !call.isError;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Did the agent invoke a tool matching `name` whose INPUT satisfies
|
|
182
|
+
* `inputMatcher` — a tool-ARGUMENT predicate (DeepEval-style), e.g. an `Edit`
|
|
183
|
+
* that targeted the right file. The predicate behind `assertToolUsedWith`.
|
|
184
|
+
*/
|
|
185
|
+
function toolUsedWith(trace, name, inputMatcher) {
|
|
186
|
+
return trace.toolCalls.some((c) => nameMatches(c.name, name) && inputMatcher(c.input));
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Does the agent's final answer (`trace.output`) contain `needle` (string =
|
|
190
|
+
* substring, RegExp = test)? The output predicate behind `assertOutputContains`
|
|
191
|
+
* — the DeepEval-style "what did the agent actually say" check.
|
|
192
|
+
*/
|
|
193
|
+
function outputContains(trace, needle) {
|
|
194
|
+
return typeof needle === "string"
|
|
195
|
+
? trace.output.includes(needle)
|
|
196
|
+
: needle.test(trace.output);
|
|
197
|
+
}
|
|
198
|
+
/** All text the model received across every request (system + every message). */
|
|
199
|
+
function requestText(trace) {
|
|
200
|
+
return trace.modelRequests
|
|
201
|
+
.map((r) => [r.system, ...r.messages.map((m) => m.text)].join("\n"))
|
|
202
|
+
.join("\n");
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Did ANY request the model received contain `needle` — searching the system
|
|
206
|
+
* prompt and every message across all requests? The predicate that proves
|
|
207
|
+
* injected context *reached the model*: a SessionStart hook's `additionalContext`
|
|
208
|
+
* or a slash command's expansion. Harness tier only — the eval tier drives the
|
|
209
|
+
* real API, so its `modelRequests` (and this) is empty. Behind `assertRequestContains`.
|
|
210
|
+
*/
|
|
211
|
+
function requestContains(trace, needle) {
|
|
212
|
+
const text = requestText(trace);
|
|
213
|
+
return typeof needle === "string" ? text.includes(needle) : needle.test(text);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Did a hook matching `name` fire? Matches against both the hook label
|
|
217
|
+
* (`"PreToolUse:Edit"`) and the bare event (`"PreToolUse"`), so `/PreToolUse/`
|
|
218
|
+
* or `"PreToolUse:Edit"` both work. The predicate behind `assertHookFired`.
|
|
219
|
+
*/
|
|
220
|
+
function hookFired(trace, name) {
|
|
221
|
+
return trace.hooks.some((h) => nameMatches(h.name, name) || nameMatches(h.event, name));
|
|
222
|
+
}
|
|
223
|
+
/** Did a hook matching `name` fire AND block (exit ≠ 0 / outcome "error")? */
|
|
224
|
+
function hookBlocked(trace, name) {
|
|
225
|
+
return trace.hooks.some((h) => (nameMatches(h.name, name) || nameMatches(h.event, name)) && h.blocked);
|
|
85
226
|
}
|
|
86
227
|
/**
|
|
87
228
|
* Assert the agent invoked a tool whose name matches `name` (string = exact,
|
|
@@ -89,9 +230,9 @@ function toolNames(r) {
|
|
|
89
230
|
* a subagent (`"Task"`). Needs `transcript: true`. The action invariant the
|
|
90
231
|
* skill/MCP/command surfaces are really about.
|
|
91
232
|
*/
|
|
92
|
-
function assertToolUsed(
|
|
93
|
-
if (!
|
|
94
|
-
fail(`expected a tool matching ${String(name)} to be used; tools used: [${toolNames(
|
|
233
|
+
function assertToolUsed(trace, name) {
|
|
234
|
+
if (!usedTool(trace, name)) {
|
|
235
|
+
fail(`expected a tool matching ${String(name)} to be used; tools used: [${toolNames(trace)}] (did you set transcript:true?)`);
|
|
95
236
|
}
|
|
96
237
|
}
|
|
97
238
|
/**
|
|
@@ -99,8 +240,9 @@ function assertToolUsed(r, name) {
|
|
|
99
240
|
* (e.g. a destructive MCP tool was never called). "File unchanged" can pass by
|
|
100
241
|
* accident; "the tool was never used" is the real invariant. Needs `transcript`.
|
|
101
242
|
*/
|
|
102
|
-
function assertToolNotUsed(
|
|
103
|
-
|
|
243
|
+
function assertToolNotUsed(trace, name) {
|
|
244
|
+
// `find` is the negative of `usedTool` and narrows the hit for the message.
|
|
245
|
+
const hit = trace.toolCalls.find((c) => nameMatches(c.name, name));
|
|
104
246
|
if (hit) {
|
|
105
247
|
fail(`expected no tool matching ${String(name)} to be used, but ${hit.name} was`);
|
|
106
248
|
}
|
|
@@ -109,17 +251,74 @@ function assertToolNotUsed(r, name) {
|
|
|
109
251
|
* Assert the `Skill` tool resolved `skill` (e.g. `"superpowers:test-driven-development"`)
|
|
110
252
|
* without error — the correct skill-activation invariant, vs. grepping the body.
|
|
111
253
|
*/
|
|
112
|
-
function assertSkillResolved(
|
|
113
|
-
|
|
254
|
+
function assertSkillResolved(trace, skill) {
|
|
255
|
+
if (skillResolved(trace, skill))
|
|
256
|
+
return;
|
|
257
|
+
// skillResolved is false → either no matching Skill call, or it errored.
|
|
258
|
+
// Reconstruct which, for a useful message.
|
|
259
|
+
const call = trace.toolCalls.find((c) => c.name === "Skill" && c.input?.skill === skill);
|
|
114
260
|
if (!call) {
|
|
115
|
-
const seen =
|
|
261
|
+
const seen = trace.toolCalls
|
|
116
262
|
.filter((c) => c.name === "Skill")
|
|
117
263
|
.map((c) => c.input?.skill ?? "?")
|
|
118
264
|
.join(", ");
|
|
119
265
|
fail(`expected the Skill tool to resolve "${skill}"; Skill calls: [${seen || "none"}]`);
|
|
120
266
|
}
|
|
121
|
-
|
|
122
|
-
|
|
267
|
+
fail(`the Skill "${skill}" was invoked but errored: ${call.resultText.slice(0, 200)}`);
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Assert the agent invoked a tool matching `name` whose INPUT satisfies
|
|
271
|
+
* `inputMatcher` — a tool-ARGUMENT invariant (DeepEval-style). Asserts not just
|
|
272
|
+
* *that* a tool ran but *with what args*, e.g. an `Edit` that targeted the right
|
|
273
|
+
* file: `assertToolUsedWith(r, "Edit", (i) => (i as { file_path?: string })
|
|
274
|
+
* .file_path === "src/x.ts")`. Needs `transcript`.
|
|
275
|
+
*/
|
|
276
|
+
function assertToolUsedWith(trace, name, inputMatcher, message) {
|
|
277
|
+
if (!toolUsedWith(trace, name, inputMatcher)) {
|
|
278
|
+
const seen = trace.toolCalls
|
|
279
|
+
.filter((c) => nameMatches(c.name, name))
|
|
280
|
+
.map((c) => JSON.stringify(c.input))
|
|
281
|
+
.join(", ");
|
|
282
|
+
fail(message ??
|
|
283
|
+
`expected a ${String(name)} call whose input matches; ${String(name)} inputs: [${seen || "none"}]`);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/** Assert the agent's final answer contains `needle` (string substring / RegExp). */
|
|
287
|
+
function assertOutputContains(trace, needle) {
|
|
288
|
+
if (!outputContains(trace, needle)) {
|
|
289
|
+
const shown = trace.output.slice(0, 200) || "(empty)";
|
|
290
|
+
fail(`expected the agent's final answer to contain ${String(needle)}; got: ${shown}`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Assert some request the model received contained `needle` — the "did the
|
|
295
|
+
* injected context land" invariant (SessionStart `additionalContext`, slash
|
|
296
|
+
* command expansion). Harness tier only; a zero-request trace fails with a hint
|
|
297
|
+
* that the eval tier can't capture requests.
|
|
298
|
+
*/
|
|
299
|
+
function assertRequestContains(trace, needle) {
|
|
300
|
+
if (!requestContains(trace, needle)) {
|
|
301
|
+
const n = trace.modelRequests.length;
|
|
302
|
+
const hint = n === 0
|
|
303
|
+
? " (no requests captured — modelRequests is harness-tier only)"
|
|
304
|
+
: "";
|
|
305
|
+
fail(`expected a model request to contain ${String(needle)}; ${String(n)} request(s) captured${hint}`);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function hookNames(trace) {
|
|
309
|
+
return trace.hooks.map((h) => h.name).join(", ") || "none";
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Assert a hook matching `name` fired (and, with `{ blocked: true }`, that it
|
|
313
|
+
* blocked) — the honest hook-firing check, recorded from the run's stream rather
|
|
314
|
+
* than inferred from a marker file the hook had to write. Needs `transcript`.
|
|
315
|
+
*/
|
|
316
|
+
function assertHookFired(trace, name, opts = {}) {
|
|
317
|
+
if (!hookFired(trace, name)) {
|
|
318
|
+
fail(`expected a hook matching ${String(name)} to fire; hooks fired: [${hookNames(trace)}] (did you set transcript:true?)`);
|
|
319
|
+
}
|
|
320
|
+
if (opts.blocked === true && !hookBlocked(trace, name)) {
|
|
321
|
+
fail(`expected a hook matching ${String(name)} to block, but none did; hooks fired: [${hookNames(trace)}]`);
|
|
123
322
|
}
|
|
124
323
|
}
|
|
125
324
|
// --- sequence / budget invariants over the agent's actions -----------------
|
|
@@ -128,13 +327,13 @@ function assertSkillResolved(r, skill) {
|
|
|
128
327
|
* budget invariant (e.g. `{ max: 1 }` = "at most one Write", `{ exactly: 0 }` =
|
|
129
328
|
* "never touched it"). Catches runaway loops and wasted work. Needs `transcript`.
|
|
130
329
|
*/
|
|
131
|
-
function assertToolCount(
|
|
132
|
-
const n =
|
|
330
|
+
function assertToolCount(trace, name, bounds) {
|
|
331
|
+
const n = toolCount(trace, name);
|
|
133
332
|
const ok = (bounds.exactly === undefined || n === bounds.exactly) &&
|
|
134
333
|
(bounds.min === undefined || n >= bounds.min) &&
|
|
135
334
|
(bounds.max === undefined || n <= bounds.max);
|
|
136
335
|
if (!ok) {
|
|
137
|
-
fail(`expected count of ${String(name)} to satisfy ${JSON.stringify(bounds)}, got ${String(n)} (tools: [${toolNames(
|
|
336
|
+
fail(`expected count of ${String(name)} to satisfy ${JSON.stringify(bounds)}, got ${String(n)} (tools: [${toolNames(trace)}])`);
|
|
138
337
|
}
|
|
139
338
|
}
|
|
140
339
|
/**
|
|
@@ -143,15 +342,15 @@ function assertToolCount(r, name, bounds) {
|
|
|
143
342
|
* Edit. For a stricter rule (every Edit preceded by a Read), use `assertToolCalls`.
|
|
144
343
|
* Needs `transcript`.
|
|
145
344
|
*/
|
|
146
|
-
function assertToolSequence(
|
|
345
|
+
function assertToolSequence(trace, names) {
|
|
147
346
|
let i = 0;
|
|
148
|
-
for (const c of
|
|
347
|
+
for (const c of trace.toolCalls) {
|
|
149
348
|
const want = names[i];
|
|
150
349
|
if (want !== undefined && nameMatches(c.name, want))
|
|
151
350
|
i++;
|
|
152
351
|
}
|
|
153
352
|
if (i < names.length) {
|
|
154
|
-
fail(`expected tools in order [${names.map((n) => String(n)).join(" → ")}]; got [${toolNames(
|
|
353
|
+
fail(`expected tools in order [${names.map((n) => String(n)).join(" → ")}]; got [${toolNames(trace)}]`);
|
|
155
354
|
}
|
|
156
355
|
}
|
|
157
356
|
/**
|
|
@@ -159,9 +358,27 @@ function assertToolSequence(r, names) {
|
|
|
159
358
|
* the agent made — for rules the helpers above don't express, e.g. "every Edit
|
|
160
359
|
* was preceded by a Read of that file". Needs `transcript`.
|
|
161
360
|
*/
|
|
162
|
-
function assertToolCalls(
|
|
163
|
-
if (!predicate(
|
|
164
|
-
fail(`${message}; tools used: [${toolNames(
|
|
361
|
+
function assertToolCalls(trace, predicate, message = "tool-call invariant failed") {
|
|
362
|
+
if (!predicate(trace.toolCalls)) {
|
|
363
|
+
fail(`${message}; tools used: [${toolNames(trace)}]`);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* Did `arm` succeed on EVERY trial for `metric` — τ-bench pass^k = 1? The
|
|
368
|
+
* reliability predicate over an eval report (vs. `improvement`, which reads the
|
|
369
|
+
* mean gap). Reads `report.arms[arm].stats[metric].passK`.
|
|
370
|
+
*/
|
|
371
|
+
function reliable(report, arm, metric) {
|
|
372
|
+
return report.arms[arm]?.stats[metric]?.passK === 1;
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Assert `arm` passed `metric` on every trial (pass^k = 1) — the reliability
|
|
376
|
+
* gate for a non-deterministic harness ("worked every time", not "on average").
|
|
377
|
+
*/
|
|
378
|
+
function assertReliable(report, opts) {
|
|
379
|
+
if (!reliable(report, opts.arm, opts.metric)) {
|
|
380
|
+
const pk = report.arms[opts.arm]?.stats[opts.metric]?.passK;
|
|
381
|
+
fail(`expected ${opts.arm} to pass ${opts.metric} on every trial (pass^k=1), got pass^k=${String(pk ?? "n/a")}`);
|
|
165
382
|
}
|
|
166
383
|
}
|
|
167
384
|
/** The gap on `metric` between two arms (arm − baseline). */
|
|
@@ -171,17 +388,58 @@ function improvement(report, baseline, arm, metric) {
|
|
|
171
388
|
return a - b;
|
|
172
389
|
}
|
|
173
390
|
/**
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
*
|
|
391
|
+
* Did `arm` *significantly* beat `baseline` on `metric` — a positive gap whose
|
|
392
|
+
* two-sided Welch t-test p-value is below `alpha` (default 0.05)? The grounded
|
|
393
|
+
* upgrade over `improvement`: the noise floor is computed from the arms' spread,
|
|
394
|
+
* not hand-fed. False when either arm/metric is missing. See `src/stats.ts`.
|
|
395
|
+
*/
|
|
396
|
+
// eslint-disable-next-line max-params -- positional predicate mirrors `improvement` + alpha
|
|
397
|
+
function significantlyBeats(report, baseline, arm, metric, alpha = 0.05) {
|
|
398
|
+
const c = (0, stats_js_1.compareArms)(report, baseline, arm, metric, alpha);
|
|
399
|
+
return c !== null && c.delta > 0 && c.significant;
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Assert `arm` significantly beats `baseline` on `metric` (positive gap, p < α).
|
|
403
|
+
* The statistical gate for a non-deterministic A/B — "the gap clears the noise",
|
|
404
|
+
* with the noise floor computed, not supplied. The honest version of
|
|
405
|
+
* `assertImproves(..., { by: se })`.
|
|
406
|
+
*/
|
|
407
|
+
function assertSignificant(report, opts) {
|
|
408
|
+
const c = (0, stats_js_1.compareArms)(report, opts.baseline, opts.arm, opts.metric, opts.alpha);
|
|
409
|
+
if (c === null) {
|
|
410
|
+
fail(`no data to compare ${opts.arm} vs ${opts.baseline} on ${opts.metric}`);
|
|
411
|
+
}
|
|
412
|
+
const alpha = opts.alpha ?? 0.05;
|
|
413
|
+
if (!(c.delta > 0 && c.significant)) {
|
|
414
|
+
fail(`expected ${opts.arm} to significantly beat ${opts.baseline} on ${opts.metric} (α=${String(alpha)}); Δ=${c.delta.toFixed(3)}, p=${c.pValue.toFixed(3)}`);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Assert `arm` beats `baseline` on `metric`. By default just a positive gap > `by`
|
|
419
|
+
* (pass the combined se to clear the noise floor by hand). Pass `{ significant:
|
|
420
|
+
* true }` to demand a Welch t-test at `alpha` instead — the computed noise floor.
|
|
177
421
|
*/
|
|
178
422
|
function assertImproves(report, opts) {
|
|
423
|
+
if (opts.significant === true) {
|
|
424
|
+
assertSignificant(report, opts);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
179
427
|
const by = opts.by ?? 0;
|
|
180
428
|
const delta = improvement(report, opts.baseline, opts.arm, opts.metric);
|
|
181
429
|
if (delta <= by) {
|
|
182
430
|
fail(`expected ${opts.arm} to beat ${opts.baseline} on ${opts.metric} by > ${String(by)}, got ${delta.toFixed(3)}`);
|
|
183
431
|
}
|
|
184
432
|
}
|
|
433
|
+
/**
|
|
434
|
+
* Assert a skill/behaviour triggered on at least `min` (0..1) of its runs — the
|
|
435
|
+
* reliability gate for a skill's *activation* (does its description fire on the
|
|
436
|
+
* task), over a {@link TriggerRateReport} from `measureTriggerRate`.
|
|
437
|
+
*/
|
|
438
|
+
function assertTriggerRate(report, opts) {
|
|
439
|
+
if (report.rate < opts.min) {
|
|
440
|
+
fail(`expected a trigger rate ≥ ${String(opts.min)}, got ${report.rate.toFixed(2)} (${String(report.n)} runs)`);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
185
443
|
/**
|
|
186
444
|
* Custom matchers compatible with both vitest and jest. Register once:
|
|
187
445
|
*
|
package/dist/harness-test.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import { type ModelTurn } from "./mock-model.js";
|
|
2
|
-
|
|
1
|
+
import { type ModelTurn, type ModelRequest } from "./mock-model.js";
|
|
2
|
+
import { type SandboxMode } from "./sandbox.js";
|
|
3
|
+
export { scriptModel, type ModelTurn, type ModelRequest, } from "./mock-model.js";
|
|
3
4
|
export { loadPlugin, resolveHarness } from "./plugin-loader.js";
|
|
5
|
+
export { decideSandbox, specTrusted, sandboxAvailable, type SandboxMode, } from "./sandbox.js";
|
|
4
6
|
export interface HarnessTestSpec {
|
|
5
7
|
/** Fixture files to write in a fresh temp working dir (path → contents). */
|
|
6
8
|
readonly files?: Record<string, string>;
|
|
@@ -37,8 +39,80 @@ export interface HarnessTestSpec {
|
|
|
37
39
|
readonly transcript?: boolean;
|
|
38
40
|
/** Per-run wall-clock timeout in ms. Default 60000. */
|
|
39
41
|
readonly timeoutMs?: number;
|
|
42
|
+
/**
|
|
43
|
+
* Confinement policy for the code this run executes (`src/sandbox.ts`).
|
|
44
|
+
* Default `"auto"` is safe-by-default: an inline-only spec (you authored it)
|
|
45
|
+
* runs directly, but an external `plugin` / `pluginDir` brings in untrusted
|
|
46
|
+
* third-party hooks and is run under bubblewrap — or, if no sandbox is
|
|
47
|
+
* available, the run REFUSES rather than executing unconfined. Pass `false` to
|
|
48
|
+
* opt out and run unconfined (you audited the code, or trust the outer
|
|
49
|
+
* container); `"strict"` to force confinement even for trusted code.
|
|
50
|
+
*
|
|
51
|
+
* NOTE: confined execution is **Linux only** (bubblewrap is a Linux tool). On
|
|
52
|
+
* macOS / Windows no sandbox is available, so an untrusted run will REFUSE
|
|
53
|
+
* under `"auto"`/`"strict"` — use `sandbox: false` there if you trust the code.
|
|
54
|
+
*/
|
|
55
|
+
readonly sandbox?: SandboxMode;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A hook invocation observed during the run, recorded (not inferred) from the
|
|
59
|
+
* `hook_response` system events the CLI emits in the stream — so a test can
|
|
60
|
+
* assert which hook fired and whether it blocked, instead of inferring it from a
|
|
61
|
+
* marker file the hook had to write.
|
|
62
|
+
*/
|
|
63
|
+
export interface HookFire {
|
|
64
|
+
/** The hook label, e.g. `"PreToolUse:Edit"` (`Event:Matcher`). */
|
|
65
|
+
readonly name: string;
|
|
66
|
+
/** The hook event, e.g. `"PreToolUse"`, `"PostToolUse"`, `"Stop"`. */
|
|
67
|
+
readonly event: string;
|
|
68
|
+
/** The hook process exit code (2 = block), or undefined if not reported. */
|
|
69
|
+
readonly exitCode: number | undefined;
|
|
70
|
+
/** Whether the hook blocked / errored (exit ≠ 0 or outcome "error"). */
|
|
71
|
+
readonly blocked: boolean;
|
|
72
|
+
/** What the hook printed (its block reason / diagnostic), or "". */
|
|
73
|
+
readonly output: string;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* The observable record of ONE run — the unified shape produced by BOTH testing
|
|
77
|
+
* tiers: `runHarnessTest`'s result and `runEval`'s `measure` ctx (`eval.ts`)
|
|
78
|
+
* both satisfy it. That's what lets the bare predicates in `harness-assert.ts`
|
|
79
|
+
* (`usedTool` / `skillResolved` / `toolCount` / `toolUsedWith` / `hookFired` /
|
|
80
|
+
* `outputContains`) run over either, with the testing helpers asserting and eval
|
|
81
|
+
* measuring over the same vocabulary.
|
|
82
|
+
*/
|
|
83
|
+
export interface Trace {
|
|
84
|
+
/**
|
|
85
|
+
* The tools the agent invoked, each paired with its result — parsed from the
|
|
86
|
+
* transcript. Empty unless the run captured the stream (`transcript: true` on
|
|
87
|
+
* the harness tier; always on the eval tier). Lets a test assert on the
|
|
88
|
+
* agent's *actions* (skills, MCP tools, subagents) instead of grepping stdout.
|
|
89
|
+
*/
|
|
90
|
+
readonly toolCalls: readonly ToolCall[];
|
|
91
|
+
/**
|
|
92
|
+
* The hooks that fired during the run, each with its decision — parsed from
|
|
93
|
+
* the CLI's `hook_response` stream events. Same capture requirement as
|
|
94
|
+
* `toolCalls` (empty without the stream). Lets a test assert hook firing
|
|
95
|
+
* honestly instead of via a marker file.
|
|
96
|
+
*/
|
|
97
|
+
readonly hooks: readonly HookFire[];
|
|
98
|
+
/** The agent's final answer text (the terminal `result` event), or "". */
|
|
99
|
+
readonly output: string;
|
|
100
|
+
/**
|
|
101
|
+
* The requests the model received, captured by the scripted mock — each with
|
|
102
|
+
* its `system` prompt and `messages`, flattened to text. Lets a test assert
|
|
103
|
+
* what actually reached the model (a SessionStart hook's injected context, a
|
|
104
|
+
* slash command's expansion), not just that a hook fired. **Harness tier
|
|
105
|
+
* only**: the mock sees the requests, so this is populated by `runHarnessTest`
|
|
106
|
+
* (with or without `transcript`); the eval tier drives the real API, so its
|
|
107
|
+
* `modelRequests` is always empty.
|
|
108
|
+
*/
|
|
109
|
+
readonly modelRequests: readonly ModelRequest[];
|
|
110
|
+
/** Number of model turns. */
|
|
111
|
+
readonly turns: number;
|
|
112
|
+
/** Final contents of a file under the working dir, or null if absent. */
|
|
113
|
+
file(path: string): string | null;
|
|
40
114
|
}
|
|
41
|
-
export interface HarnessTestResult {
|
|
115
|
+
export interface HarnessTestResult extends Trace {
|
|
42
116
|
readonly exitCode: number;
|
|
43
117
|
readonly stdout: string;
|
|
44
118
|
/** Hook block messages and diagnostics land here. */
|
|
@@ -47,14 +121,6 @@ export interface HarnessTestResult {
|
|
|
47
121
|
readonly cwd: string;
|
|
48
122
|
/** Number of model turns the agent took (mock turns served). */
|
|
49
123
|
readonly turns: number;
|
|
50
|
-
/**
|
|
51
|
-
* The tools the agent invoked, each paired with its result — parsed from the
|
|
52
|
-
* transcript. Empty unless `transcript: true`. Lets a test assert on the
|
|
53
|
-
* agent's *actions* (skills, MCP tools, subagents) instead of grepping stdout.
|
|
54
|
-
*/
|
|
55
|
-
readonly toolCalls: readonly ToolCall[];
|
|
56
|
-
/** Final contents of a file under the working dir, or null if absent. */
|
|
57
|
-
file(path: string): string | null;
|
|
58
124
|
/** Remove the temp working dir. */
|
|
59
125
|
cleanup(): void;
|
|
60
126
|
}
|
|
@@ -74,11 +140,31 @@ export interface ToolCall {
|
|
|
74
140
|
* actions, not a brittle stdout substring.
|
|
75
141
|
*/
|
|
76
142
|
export declare function parseToolCalls(streamJson: string): ToolCall[];
|
|
143
|
+
/**
|
|
144
|
+
* The terminal `result` event — present in BOTH `--output-format` shapes (a
|
|
145
|
+
* `{type:"result", …}` line in stream-json, the single object in `json`), or
|
|
146
|
+
* null. The seam for the final answer + turn count without parsing twice.
|
|
147
|
+
*/
|
|
148
|
+
export declare function parseResultEvent(stdout: string): Record<string, unknown> | null;
|
|
149
|
+
/** The agent's final answer text from a transcript / result object, or "". */
|
|
150
|
+
export declare function parseOutput(stdout: string): string;
|
|
151
|
+
export declare function parseHooks(stdout: string): HookFire[];
|
|
152
|
+
/**
|
|
153
|
+
* The `claude` CLI argv for a harness run (shared by the direct and sandboxed
|
|
154
|
+
* paths). `ANTHROPIC_BASE_URL` is set by the caller's environment / wrapper, not
|
|
155
|
+
* here. Pure, so the arg shape is unit-tested.
|
|
156
|
+
*/
|
|
157
|
+
export declare function buildClaudeArgs(spec: HarnessTestSpec, hasSettings: boolean): string[];
|
|
77
158
|
/** Whether the `claude` CLI is available — harness tests need it. */
|
|
78
159
|
export declare function claudeAvailable(): boolean;
|
|
79
160
|
/**
|
|
80
161
|
* Run the real `claude` CLI against a scripted mock model, with the given
|
|
81
162
|
* fixture and settings (hooks). Deterministic — same script, same result.
|
|
163
|
+
*
|
|
164
|
+
* Safe by default: an external `plugin` / `pluginDir` brings in untrusted
|
|
165
|
+
* third-party hooks and is confined under bubblewrap (`spec.sandbox`, default
|
|
166
|
+
* `"auto"`); if no sandbox is available the run REFUSES rather than executing
|
|
167
|
+
* unconfined. See `src/sandbox.ts`.
|
|
82
168
|
*/
|
|
83
169
|
export declare function runHarnessTest(spec: HarnessTestSpec): Promise<HarnessTestResult>;
|
|
84
170
|
//# sourceMappingURL=harness-test.d.ts.map
|