vigiles 2.3.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 +214 -190
- 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 +155 -1
- 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 +180 -9
- package/dist/eval.js +319 -57
- package/dist/harness-assert.d.ts +175 -6
- package/dist/harness-assert.js +355 -4
- package/dist/harness-test.d.ts +130 -5
- package/dist/harness-test.js +205 -32
- package/dist/judge.js +2 -0
- package/dist/linters.d.ts +6 -0
- package/dist/linters.js +1 -0
- package/dist/mcp.d.ts +48 -0
- package/dist/mcp.js +247 -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 +7 -3
package/dist/harness-assert.js
CHANGED
|
@@ -1,14 +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;
|
|
21
|
+
exports.assertToolUsed = assertToolUsed;
|
|
22
|
+
exports.assertToolNotUsed = assertToolNotUsed;
|
|
23
|
+
exports.assertSkillResolved = assertSkillResolved;
|
|
24
|
+
exports.assertToolUsedWith = assertToolUsedWith;
|
|
25
|
+
exports.assertOutputContains = assertOutputContains;
|
|
26
|
+
exports.assertRequestContains = assertRequestContains;
|
|
27
|
+
exports.assertHookFired = assertHookFired;
|
|
28
|
+
exports.assertToolCount = assertToolCount;
|
|
29
|
+
exports.assertToolSequence = assertToolSequence;
|
|
30
|
+
exports.assertToolCalls = assertToolCalls;
|
|
31
|
+
exports.reliable = reliable;
|
|
32
|
+
exports.assertReliable = assertReliable;
|
|
10
33
|
exports.improvement = improvement;
|
|
34
|
+
exports.significantlyBeats = significantlyBeats;
|
|
35
|
+
exports.assertSignificant = assertSignificant;
|
|
11
36
|
exports.assertImproves = assertImproves;
|
|
37
|
+
exports.assertTriggerRate = assertTriggerRate;
|
|
12
38
|
/**
|
|
13
39
|
* vigiles — runner-agnostic helpers for harness tests / evals.
|
|
14
40
|
*
|
|
@@ -25,11 +51,18 @@ exports.assertImproves = assertImproves;
|
|
|
25
51
|
* vitest and jest, so the same object supports both.
|
|
26
52
|
*/
|
|
27
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; } });
|
|
28
60
|
/**
|
|
29
61
|
* Run a harness test, hand the result to `fn`, and always clean up the sandbox.
|
|
30
62
|
* Returns whatever `fn` returns. Use this instead of calling `cleanup()` by
|
|
31
63
|
* hand — it survives assertion failures.
|
|
32
64
|
*/
|
|
65
|
+
/* v8 ignore start -- thin wrapper over runHarnessTest (spawns the real CLI) */
|
|
33
66
|
async function withHarness(spec, fn) {
|
|
34
67
|
const r = await (0, harness_test_js_1.runHarnessTest)(spec);
|
|
35
68
|
try {
|
|
@@ -39,6 +72,7 @@ async function withHarness(spec, fn) {
|
|
|
39
72
|
r.cleanup();
|
|
40
73
|
}
|
|
41
74
|
}
|
|
75
|
+
/* v8 ignore stop */
|
|
42
76
|
// --- Plain throwing assertions (any runner) --------------------------------
|
|
43
77
|
function fail(message) {
|
|
44
78
|
throw new Error(message);
|
|
@@ -71,6 +105,282 @@ function assertHookAllowed(r) {
|
|
|
71
105
|
fail(`expected the hook to allow, but it blocked (exit ${String(r.exitCode)}, decision ${String(r.decision)})`);
|
|
72
106
|
}
|
|
73
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
|
+
}
|
|
149
|
+
function nameMatches(name, pat) {
|
|
150
|
+
return typeof pat === "string" ? name === pat : pat.test(name);
|
|
151
|
+
}
|
|
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);
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Assert the agent invoked a tool whose name matches `name` (string = exact,
|
|
229
|
+
* RegExp = test) — e.g. a skill (`"Skill"`), an MCP tool (`/^mcp__github__/`), or
|
|
230
|
+
* a subagent (`"Task"`). Needs `transcript: true`. The action invariant the
|
|
231
|
+
* skill/MCP/command surfaces are really about.
|
|
232
|
+
*/
|
|
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?)`);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Assert the agent did NOT invoke any tool matching `name` — the safety negative
|
|
240
|
+
* (e.g. a destructive MCP tool was never called). "File unchanged" can pass by
|
|
241
|
+
* accident; "the tool was never used" is the real invariant. Needs `transcript`.
|
|
242
|
+
*/
|
|
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));
|
|
246
|
+
if (hit) {
|
|
247
|
+
fail(`expected no tool matching ${String(name)} to be used, but ${hit.name} was`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Assert the `Skill` tool resolved `skill` (e.g. `"superpowers:test-driven-development"`)
|
|
252
|
+
* without error — the correct skill-activation invariant, vs. grepping the body.
|
|
253
|
+
*/
|
|
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);
|
|
260
|
+
if (!call) {
|
|
261
|
+
const seen = trace.toolCalls
|
|
262
|
+
.filter((c) => c.name === "Skill")
|
|
263
|
+
.map((c) => c.input?.skill ?? "?")
|
|
264
|
+
.join(", ");
|
|
265
|
+
fail(`expected the Skill tool to resolve "${skill}"; Skill calls: [${seen || "none"}]`);
|
|
266
|
+
}
|
|
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)}]`);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
// --- sequence / budget invariants over the agent's actions -----------------
|
|
325
|
+
/**
|
|
326
|
+
* Assert how many tools matching `name` the agent invoked is within bounds — a
|
|
327
|
+
* budget invariant (e.g. `{ max: 1 }` = "at most one Write", `{ exactly: 0 }` =
|
|
328
|
+
* "never touched it"). Catches runaway loops and wasted work. Needs `transcript`.
|
|
329
|
+
*/
|
|
330
|
+
function assertToolCount(trace, name, bounds) {
|
|
331
|
+
const n = toolCount(trace, name);
|
|
332
|
+
const ok = (bounds.exactly === undefined || n === bounds.exactly) &&
|
|
333
|
+
(bounds.min === undefined || n >= bounds.min) &&
|
|
334
|
+
(bounds.max === undefined || n <= bounds.max);
|
|
335
|
+
if (!ok) {
|
|
336
|
+
fail(`expected count of ${String(name)} to satisfy ${JSON.stringify(bounds)}, got ${String(n)} (tools: [${toolNames(trace)}])`);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Assert the named tools occurred in this order (as a subsequence — gaps allowed)
|
|
341
|
+
* — an ordering invariant. e.g. `["Read", "Edit"]` checks a Read came before an
|
|
342
|
+
* Edit. For a stricter rule (every Edit preceded by a Read), use `assertToolCalls`.
|
|
343
|
+
* Needs `transcript`.
|
|
344
|
+
*/
|
|
345
|
+
function assertToolSequence(trace, names) {
|
|
346
|
+
let i = 0;
|
|
347
|
+
for (const c of trace.toolCalls) {
|
|
348
|
+
const want = names[i];
|
|
349
|
+
if (want !== undefined && nameMatches(c.name, want))
|
|
350
|
+
i++;
|
|
351
|
+
}
|
|
352
|
+
if (i < names.length) {
|
|
353
|
+
fail(`expected tools in order [${names.map((n) => String(n)).join(" → ")}]; got [${toolNames(trace)}]`);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* The escape hatch: assert any custom invariant over the full list of tool calls
|
|
358
|
+
* the agent made — for rules the helpers above don't express, e.g. "every Edit
|
|
359
|
+
* was preceded by a Read of that file". Needs `transcript`.
|
|
360
|
+
*/
|
|
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")}`);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
74
384
|
/** The gap on `metric` between two arms (arm − baseline). */
|
|
75
385
|
function improvement(report, baseline, arm, metric) {
|
|
76
386
|
const a = report.arms[arm]?.metrics[metric] ?? 0;
|
|
@@ -78,17 +388,58 @@ function improvement(report, baseline, arm, metric) {
|
|
|
78
388
|
return a - b;
|
|
79
389
|
}
|
|
80
390
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
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.
|
|
84
421
|
*/
|
|
85
422
|
function assertImproves(report, opts) {
|
|
423
|
+
if (opts.significant === true) {
|
|
424
|
+
assertSignificant(report, opts);
|
|
425
|
+
return;
|
|
426
|
+
}
|
|
86
427
|
const by = opts.by ?? 0;
|
|
87
428
|
const delta = improvement(report, opts.baseline, opts.arm, opts.metric);
|
|
88
429
|
if (delta <= by) {
|
|
89
430
|
fail(`expected ${opts.arm} to beat ${opts.baseline} on ${opts.metric} by > ${String(by)}, got ${delta.toFixed(3)}`);
|
|
90
431
|
}
|
|
91
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
|
+
}
|
|
92
443
|
/**
|
|
93
444
|
* Custom matchers compatible with both vitest and jest. Register once:
|
|
94
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>;
|
|
@@ -12,16 +14,105 @@ export interface HarnessTestSpec {
|
|
|
12
14
|
* subset. Inline `settings`/`files` layer on top. See src/plugin-loader.ts.
|
|
13
15
|
*/
|
|
14
16
|
readonly plugin?: string;
|
|
17
|
+
/**
|
|
18
|
+
* Path to a plugin dir to install NATIVELY via `claude --plugin-dir`, so its
|
|
19
|
+
* skills / commands / agents / hooks register and ACTIVATE the real way — a
|
|
20
|
+
* scripted `Skill` tool_use resolves, and the real model can trigger them.
|
|
21
|
+
* Unlike `plugin` (which materializes a file subset that does NOT register
|
|
22
|
+
* skills for the `Skill` tool), this is the real install path, so point it at a
|
|
23
|
+
* COMPLETE plugin (internal references resolve). Inline `settings`/`files` and
|
|
24
|
+
* `plugin` still layer on top. Resolved to an absolute path.
|
|
25
|
+
*/
|
|
26
|
+
readonly pluginDir?: string;
|
|
15
27
|
/** The scripted model turns the agent will take. */
|
|
16
28
|
readonly model: readonly ModelTurn[];
|
|
17
29
|
/** The user prompt. Default: "go". */
|
|
18
30
|
readonly prompt?: string;
|
|
19
31
|
/** Tools the agent may use. Default: Read Edit Write Bash. */
|
|
20
32
|
readonly allowedTools?: readonly string[];
|
|
33
|
+
/**
|
|
34
|
+
* Capture the full event transcript (`--output-format stream-json`) into
|
|
35
|
+
* `stdout`, instead of just the final result object, so you can assert on what
|
|
36
|
+
* the agent's tools returned — e.g. the body a `Skill` tool_use resolved. With
|
|
37
|
+
* this on, `stdout` is newline-delimited JSON events, not a single object.
|
|
38
|
+
*/
|
|
39
|
+
readonly transcript?: boolean;
|
|
21
40
|
/** Per-run wall-clock timeout in ms. Default 60000. */
|
|
22
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;
|
|
23
114
|
}
|
|
24
|
-
export interface HarnessTestResult {
|
|
115
|
+
export interface HarnessTestResult extends Trace {
|
|
25
116
|
readonly exitCode: number;
|
|
26
117
|
readonly stdout: string;
|
|
27
118
|
/** Hook block messages and diagnostics land here. */
|
|
@@ -30,16 +121,50 @@ export interface HarnessTestResult {
|
|
|
30
121
|
readonly cwd: string;
|
|
31
122
|
/** Number of model turns the agent took (mock turns served). */
|
|
32
123
|
readonly turns: number;
|
|
33
|
-
/** Final contents of a file under the working dir, or null if absent. */
|
|
34
|
-
file(path: string): string | null;
|
|
35
124
|
/** Remove the temp working dir. */
|
|
36
125
|
cleanup(): void;
|
|
37
126
|
}
|
|
127
|
+
/** A tool the agent invoked, paired with its result (transcript mode only). */
|
|
128
|
+
export interface ToolCall {
|
|
129
|
+
readonly name: string;
|
|
130
|
+
readonly input: unknown;
|
|
131
|
+
/** The tool_result text ("" if none / not captured). */
|
|
132
|
+
readonly resultText: string;
|
|
133
|
+
/** Whether the tool_result came back flagged as an error. */
|
|
134
|
+
readonly isError: boolean;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Parse `--output-format stream-json` (the `transcript: true` output) into the
|
|
138
|
+
* tools the agent invoked, each joined to its result by id. Returns [] for the
|
|
139
|
+
* non-stream `json` output. The seam that lets a test assert on the agent's
|
|
140
|
+
* actions, not a brittle stdout substring.
|
|
141
|
+
*/
|
|
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[];
|
|
38
158
|
/** Whether the `claude` CLI is available — harness tests need it. */
|
|
39
159
|
export declare function claudeAvailable(): boolean;
|
|
40
160
|
/**
|
|
41
161
|
* Run the real `claude` CLI against a scripted mock model, with the given
|
|
42
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`.
|
|
43
168
|
*/
|
|
44
169
|
export declare function runHarnessTest(spec: HarnessTestSpec): Promise<HarnessTestResult>;
|
|
45
170
|
//# sourceMappingURL=harness-test.d.ts.map
|