@forwardimpact/libeval 0.1.6 → 0.1.9

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.
@@ -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
+ });