@forwardimpact/libeval 0.1.5 → 0.1.8
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/bin/fit-eval.js +2 -2
- package/index.js +2 -0
- package/package.json +1 -1
- package/src/agent-runner.js +97 -39
- package/src/commands/run.js +43 -18
- package/src/commands/supervise.js +59 -37
- package/src/supervisor.js +320 -48
- package/src/trace-collector.js +7 -0
- package/test/mock-runner.js +101 -0
- package/test/supervisor-intervention.test.js +359 -0
- package/test/{supervisor.test.js → supervisor-output.test.js} +120 -245
- package/test/supervisor-run.test.js +310 -0
- package/test/trace-collector.test.js +96 -0
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { describe, test } from "node:test";
|
|
2
|
+
import assert from "node:assert";
|
|
3
|
+
import { PassThrough } from "node:stream";
|
|
4
|
+
|
|
5
|
+
import { Supervisor } from "@forwardimpact/libeval";
|
|
6
|
+
import { isComplete } from "../src/supervisor.js";
|
|
7
|
+
import { createMockRunner } from "./mock-runner.js";
|
|
8
|
+
|
|
9
|
+
describe("isComplete", () => {
|
|
10
|
+
test("detects EVALUATION_COMPLETE on its own line", () => {
|
|
11
|
+
assert.strictEqual(isComplete("EVALUATION_COMPLETE"), true);
|
|
12
|
+
assert.strictEqual(
|
|
13
|
+
isComplete("Some text\nEVALUATION_COMPLETE\nMore text"),
|
|
14
|
+
true,
|
|
15
|
+
);
|
|
16
|
+
assert.strictEqual(isComplete("Done.\n\nEVALUATION_COMPLETE"), true);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test("tolerates markdown formatting around the signal", () => {
|
|
20
|
+
assert.strictEqual(isComplete("**EVALUATION_COMPLETE**"), true);
|
|
21
|
+
assert.strictEqual(isComplete("*EVALUATION_COMPLETE*"), true);
|
|
22
|
+
assert.strictEqual(isComplete("__EVALUATION_COMPLETE__"), true);
|
|
23
|
+
assert.strictEqual(isComplete("_EVALUATION_COMPLETE_"), true);
|
|
24
|
+
assert.strictEqual(isComplete("`EVALUATION_COMPLETE`"), true);
|
|
25
|
+
assert.strictEqual(
|
|
26
|
+
isComplete("Good work.\n\n**EVALUATION_COMPLETE**\n\nNow filing issues."),
|
|
27
|
+
true,
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test("matches EVALUATION_COMPLETE anywhere in text", () => {
|
|
32
|
+
assert.strictEqual(isComplete("not EVALUATION_COMPLETE yet"), true);
|
|
33
|
+
assert.strictEqual(
|
|
34
|
+
isComplete("The agent is EVALUATION_COMPLETE done"),
|
|
35
|
+
true,
|
|
36
|
+
);
|
|
37
|
+
assert.strictEqual(
|
|
38
|
+
isComplete("Great work! EVALUATION_COMPLETE. Now filing issues."),
|
|
39
|
+
true,
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test("does not match empty or unrelated text", () => {
|
|
44
|
+
assert.strictEqual(isComplete(""), false);
|
|
45
|
+
assert.strictEqual(isComplete("All done!"), false);
|
|
46
|
+
assert.strictEqual(isComplete("DONE"), false);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("does not match old EVALUATION_SUCCESSFUL signal", () => {
|
|
50
|
+
assert.strictEqual(isComplete("EVALUATION_SUCCESSFUL"), false);
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe("Supervisor - run and turns", () => {
|
|
55
|
+
test("constructor throws on missing agentRunner", () => {
|
|
56
|
+
assert.throws(
|
|
57
|
+
() =>
|
|
58
|
+
new Supervisor({
|
|
59
|
+
supervisorRunner: createMockRunner([]),
|
|
60
|
+
output: new PassThrough(),
|
|
61
|
+
}),
|
|
62
|
+
/agentRunner is required/,
|
|
63
|
+
);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test("constructor throws on missing supervisorRunner", () => {
|
|
67
|
+
assert.throws(
|
|
68
|
+
() =>
|
|
69
|
+
new Supervisor({
|
|
70
|
+
agentRunner: createMockRunner([]),
|
|
71
|
+
output: new PassThrough(),
|
|
72
|
+
}),
|
|
73
|
+
/supervisorRunner is required/,
|
|
74
|
+
);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("constructor throws on missing output", () => {
|
|
78
|
+
assert.throws(
|
|
79
|
+
() =>
|
|
80
|
+
new Supervisor({
|
|
81
|
+
agentRunner: createMockRunner([]),
|
|
82
|
+
supervisorRunner: createMockRunner([]),
|
|
83
|
+
}),
|
|
84
|
+
/output is required/,
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("completes on EVALUATION_COMPLETE from supervisor at turn 0", async () => {
|
|
89
|
+
const agentRunner = createMockRunner([]);
|
|
90
|
+
|
|
91
|
+
const supervisorRunner = createMockRunner([
|
|
92
|
+
{ text: "EVALUATION_COMPLETE" },
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
const output = new PassThrough();
|
|
96
|
+
const supervisor = new Supervisor({
|
|
97
|
+
agentRunner,
|
|
98
|
+
supervisorRunner,
|
|
99
|
+
output,
|
|
100
|
+
maxTurns: 10,
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const result = await supervisor.run("Install stuff");
|
|
104
|
+
|
|
105
|
+
assert.strictEqual(result.success, true);
|
|
106
|
+
assert.strictEqual(result.turns, 0);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test("completes after one agent turn", async () => {
|
|
110
|
+
const agentRunner = createMockRunner([
|
|
111
|
+
{ text: "I installed the packages." },
|
|
112
|
+
]);
|
|
113
|
+
|
|
114
|
+
const supervisorRunner = createMockRunner([
|
|
115
|
+
{ text: "Welcome! Please install the packages." },
|
|
116
|
+
{ text: "Good work.\n\nEVALUATION_COMPLETE" },
|
|
117
|
+
]);
|
|
118
|
+
|
|
119
|
+
const output = new PassThrough();
|
|
120
|
+
const supervisor = new Supervisor({
|
|
121
|
+
agentRunner,
|
|
122
|
+
supervisorRunner,
|
|
123
|
+
output,
|
|
124
|
+
maxTurns: 10,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
const result = await supervisor.run("Install stuff");
|
|
128
|
+
|
|
129
|
+
assert.strictEqual(result.success, true);
|
|
130
|
+
assert.strictEqual(result.turns, 1);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test("detects EVALUATION_COMPLETE in streamed messages when result text differs", async () => {
|
|
134
|
+
const agentRunner = createMockRunner([
|
|
135
|
+
{ text: "I installed the packages." },
|
|
136
|
+
]);
|
|
137
|
+
|
|
138
|
+
const supervisorMessages = [
|
|
139
|
+
undefined,
|
|
140
|
+
[
|
|
141
|
+
{
|
|
142
|
+
type: "assistant",
|
|
143
|
+
message: {
|
|
144
|
+
content: [
|
|
145
|
+
{
|
|
146
|
+
type: "text",
|
|
147
|
+
text: "Good work.\n\nEVALUATION_COMPLETE\n\nNow filing issues.",
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
type: "assistant",
|
|
154
|
+
message: {
|
|
155
|
+
content: [
|
|
156
|
+
{ type: "text", text: "## Summary\n\nAll issues filed." },
|
|
157
|
+
],
|
|
158
|
+
},
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
];
|
|
162
|
+
|
|
163
|
+
const supervisorRunner = createMockRunner(
|
|
164
|
+
[
|
|
165
|
+
{ text: "Welcome! Please install the packages." },
|
|
166
|
+
{ text: "## Summary\n\nAll issues filed." },
|
|
167
|
+
],
|
|
168
|
+
supervisorMessages,
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
const output = new PassThrough();
|
|
172
|
+
const supervisor = new Supervisor({
|
|
173
|
+
agentRunner,
|
|
174
|
+
supervisorRunner,
|
|
175
|
+
output,
|
|
176
|
+
maxTurns: 10,
|
|
177
|
+
});
|
|
178
|
+
agentRunner.onLine = (line) => supervisor.emitLine(line);
|
|
179
|
+
supervisorRunner.onLine = (line) => supervisor.emitLine(line);
|
|
180
|
+
|
|
181
|
+
const result = await supervisor.run("Install stuff");
|
|
182
|
+
|
|
183
|
+
assert.strictEqual(result.success, true);
|
|
184
|
+
assert.strictEqual(result.turns, 1);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
test("relays only the last assistant text block to the agent", async () => {
|
|
188
|
+
// Supervisor emits reasoning text ("Let me research...") then a tool call,
|
|
189
|
+
// then a final task message. Only the final message should reach the agent.
|
|
190
|
+
const supervisorMessages = [
|
|
191
|
+
// Turn 0: multiple assistant messages with reasoning + task
|
|
192
|
+
[
|
|
193
|
+
{
|
|
194
|
+
type: "assistant",
|
|
195
|
+
message: {
|
|
196
|
+
content: [
|
|
197
|
+
{ type: "text", text: "Let me research the product first." },
|
|
198
|
+
],
|
|
199
|
+
},
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
type: "assistant",
|
|
203
|
+
message: {
|
|
204
|
+
content: [
|
|
205
|
+
{
|
|
206
|
+
type: "text",
|
|
207
|
+
text: "Hello! Here is your task: install the packages.",
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
},
|
|
211
|
+
},
|
|
212
|
+
],
|
|
213
|
+
// Turn 1: evaluation
|
|
214
|
+
undefined,
|
|
215
|
+
];
|
|
216
|
+
|
|
217
|
+
let capturedAgentPrompt = null;
|
|
218
|
+
const agentRunner = createMockRunner([
|
|
219
|
+
{ text: "I installed the packages." },
|
|
220
|
+
]);
|
|
221
|
+
const origRun = agentRunner.run;
|
|
222
|
+
agentRunner.run = async (task) => {
|
|
223
|
+
capturedAgentPrompt = task;
|
|
224
|
+
return origRun.call(agentRunner, task);
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const supervisorRunner = createMockRunner(
|
|
228
|
+
[
|
|
229
|
+
// SDK result text = last message text (but relay should use buffer)
|
|
230
|
+
{ text: "Hello! Here is your task: install the packages." },
|
|
231
|
+
{ text: "EVALUATION_COMPLETE" },
|
|
232
|
+
],
|
|
233
|
+
supervisorMessages,
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
const output = new PassThrough();
|
|
237
|
+
const supervisor = new Supervisor({
|
|
238
|
+
agentRunner,
|
|
239
|
+
supervisorRunner,
|
|
240
|
+
output,
|
|
241
|
+
maxTurns: 10,
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
await supervisor.run("Evaluate the product");
|
|
245
|
+
|
|
246
|
+
// Agent should receive only the final text, not the reasoning
|
|
247
|
+
assert.strictEqual(
|
|
248
|
+
capturedAgentPrompt,
|
|
249
|
+
"Hello! Here is your task: install the packages.",
|
|
250
|
+
);
|
|
251
|
+
assert.ok(
|
|
252
|
+
!capturedAgentPrompt.includes("research"),
|
|
253
|
+
"Reasoning text should not leak to agent",
|
|
254
|
+
);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
test("runs multiple turns before completion", async () => {
|
|
258
|
+
const agentRunner = createMockRunner([
|
|
259
|
+
{ text: "Started working." },
|
|
260
|
+
{ text: "Made progress." },
|
|
261
|
+
{ text: "Finished everything." },
|
|
262
|
+
]);
|
|
263
|
+
|
|
264
|
+
const supervisorRunner = createMockRunner([
|
|
265
|
+
{ text: "Here is your task. Do the work." },
|
|
266
|
+
{ text: "Keep going, you need to do more." },
|
|
267
|
+
{ text: "Almost there, continue." },
|
|
268
|
+
{ text: "EVALUATION_COMPLETE" },
|
|
269
|
+
]);
|
|
270
|
+
|
|
271
|
+
const output = new PassThrough();
|
|
272
|
+
const supervisor = new Supervisor({
|
|
273
|
+
agentRunner,
|
|
274
|
+
supervisorRunner,
|
|
275
|
+
output,
|
|
276
|
+
maxTurns: 10,
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
const result = await supervisor.run("Do the work");
|
|
280
|
+
|
|
281
|
+
assert.strictEqual(result.success, true);
|
|
282
|
+
assert.strictEqual(result.turns, 3);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
test("enforces maxTurns limit", async () => {
|
|
286
|
+
const agentRunner = createMockRunner([
|
|
287
|
+
{ text: "Turn 1" },
|
|
288
|
+
{ text: "Turn 2" },
|
|
289
|
+
]);
|
|
290
|
+
|
|
291
|
+
const supervisorRunner = createMockRunner([
|
|
292
|
+
{ text: "Start working." },
|
|
293
|
+
{ text: "Continue." },
|
|
294
|
+
{ text: "Continue." },
|
|
295
|
+
]);
|
|
296
|
+
|
|
297
|
+
const output = new PassThrough();
|
|
298
|
+
const supervisor = new Supervisor({
|
|
299
|
+
agentRunner,
|
|
300
|
+
supervisorRunner,
|
|
301
|
+
output,
|
|
302
|
+
maxTurns: 2,
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
const result = await supervisor.run("Endless task");
|
|
306
|
+
|
|
307
|
+
assert.strictEqual(result.success, false);
|
|
308
|
+
assert.strictEqual(result.turns, 2);
|
|
309
|
+
});
|
|
310
|
+
});
|
|
@@ -149,6 +149,102 @@ describe("TraceCollector", () => {
|
|
|
149
149
|
assert.strictEqual(trace.summary.tokenUsage.inputTokens, 5000);
|
|
150
150
|
});
|
|
151
151
|
|
|
152
|
+
test("unwraps combined supervised trace format {source, turn, event}", () => {
|
|
153
|
+
const collector = new TraceCollector();
|
|
154
|
+
|
|
155
|
+
// System init wrapped in supervisor envelope
|
|
156
|
+
collector.addLine(
|
|
157
|
+
JSON.stringify({
|
|
158
|
+
source: "agent",
|
|
159
|
+
turn: 0,
|
|
160
|
+
event: {
|
|
161
|
+
type: "system",
|
|
162
|
+
subtype: "init",
|
|
163
|
+
session_id: "sess-supervised",
|
|
164
|
+
model: "claude-opus-4-6",
|
|
165
|
+
tools: ["Bash"],
|
|
166
|
+
},
|
|
167
|
+
}),
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
// Assistant message wrapped in supervisor envelope
|
|
171
|
+
collector.addLine(
|
|
172
|
+
JSON.stringify({
|
|
173
|
+
source: "agent",
|
|
174
|
+
turn: 1,
|
|
175
|
+
event: {
|
|
176
|
+
type: "assistant",
|
|
177
|
+
message: {
|
|
178
|
+
content: [{ type: "text", text: "I ran the tests." }],
|
|
179
|
+
usage: { input_tokens: 100, output_tokens: 50 },
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
}),
|
|
183
|
+
);
|
|
184
|
+
|
|
185
|
+
// Tool result wrapped in supervisor envelope
|
|
186
|
+
collector.addLine(
|
|
187
|
+
JSON.stringify({
|
|
188
|
+
source: "agent",
|
|
189
|
+
turn: 1,
|
|
190
|
+
event: {
|
|
191
|
+
type: "user",
|
|
192
|
+
message: {
|
|
193
|
+
role: "user",
|
|
194
|
+
content: [
|
|
195
|
+
{
|
|
196
|
+
type: "tool_result",
|
|
197
|
+
tool_use_id: "toolu_sup",
|
|
198
|
+
content: "All tests passed",
|
|
199
|
+
},
|
|
200
|
+
],
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
}),
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
// Result event wrapped in supervisor envelope
|
|
207
|
+
collector.addLine(
|
|
208
|
+
JSON.stringify({
|
|
209
|
+
source: "supervisor",
|
|
210
|
+
turn: 1,
|
|
211
|
+
event: {
|
|
212
|
+
type: "result",
|
|
213
|
+
subtype: "success",
|
|
214
|
+
total_cost_usd: 0.44,
|
|
215
|
+
duration_ms: 30000,
|
|
216
|
+
num_turns: 2,
|
|
217
|
+
},
|
|
218
|
+
}),
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
const trace = collector.toJSON();
|
|
222
|
+
assert.strictEqual(trace.metadata.sessionId, "sess-supervised");
|
|
223
|
+
assert.strictEqual(trace.turns.length, 2);
|
|
224
|
+
assert.strictEqual(trace.turns[0].role, "assistant");
|
|
225
|
+
assert.strictEqual(trace.turns[0].content[0].text, "I ran the tests.");
|
|
226
|
+
assert.strictEqual(trace.turns[1].role, "tool_result");
|
|
227
|
+
assert.strictEqual(trace.turns[1].content, "All tests passed");
|
|
228
|
+
assert.strictEqual(trace.summary.result, "success");
|
|
229
|
+
assert.strictEqual(trace.summary.totalCostUsd, 0.44);
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
test("skips orchestrator summary lines from supervised traces", () => {
|
|
233
|
+
const collector = new TraceCollector();
|
|
234
|
+
collector.addLine(
|
|
235
|
+
JSON.stringify({
|
|
236
|
+
source: "orchestrator",
|
|
237
|
+
type: "summary",
|
|
238
|
+
success: true,
|
|
239
|
+
turns: 3,
|
|
240
|
+
}),
|
|
241
|
+
);
|
|
242
|
+
|
|
243
|
+
// Orchestrator summaries have no inner event and no recognized type
|
|
244
|
+
// after unwrap — they should be silently skipped.
|
|
245
|
+
assert.strictEqual(collector.toJSON().turns.length, 0);
|
|
246
|
+
});
|
|
247
|
+
|
|
152
248
|
test("skips rate_limit_event and unknown types", () => {
|
|
153
249
|
const collector = new TraceCollector();
|
|
154
250
|
collector.addLine(
|