-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheval.ts
More file actions
588 lines (499 loc) · 18.5 KB
/
eval.ts
File metadata and controls
588 lines (499 loc) · 18.5 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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
#!/usr/bin/env npx tsx
/**
* Reflection Layer End-to-End Evaluator
*
* Runs real agent tasks, captures reflection feedback, evaluates quality.
* Outputs results to eval-${timestamp}-${commit}.md
*
* Usage:
* npx tsx eval.ts
* npm run eval:e2e
*/
import { mkdir, rm, cp, readdir, readFile, writeFile } from "fs/promises"
import { spawn, execSync, type ChildProcess } from "child_process"
import { join, dirname } from "path"
import { fileURLToPath } from "url"
import { createOpencodeClient, type OpencodeClient } from "@opencode-ai/sdk/client"
const __dirname = dirname(fileURLToPath(import.meta.url))
const PLUGIN_PATH = join(__dirname, "reflection-3.ts")
// Config
const MODEL = process.env.OPENCODE_MODEL || "github-copilot/gpt-4o"
const PORT = 7654
const TIMEOUT = 300_000 // 5 minutes max per task
const POLL_INTERVAL = 3_000 // Check every 3 seconds
const STABLE_POLLS_REQUIRED = 3 // Stable polls before stopping
const MAX_WAIT_AFTER_OUTPUT = 20_000
// Test cases for evaluation
interface TestCase {
id: string
task: string
expectedComplete: boolean
description: string
}
const TEST_CASES: TestCase[] = [
{
id: "simple-file",
task: "Create a hello.js file that prints 'Hello World'",
expectedComplete: true,
description: "Simple file creation"
},
{
id: "research",
task: "What are the top 3 Node.js testing frameworks? Just list them, don't install anything.",
expectedComplete: true,
description: "Research task (no code)"
},
// Real-world scenarios from production sessions
{
id: "multi-step-test",
task: "Create a utils.ts file with an add function, write a test for it, and run the test to verify it works",
expectedComplete: true,
description: "Multi-step task with test verification"
},
{
id: "commit-without-test",
task: "Create a simple greeter.ts file with a greet function, then run npm run typecheck to verify it compiles correctly.",
expectedComplete: true,
description: "Create file with type verification"
},
{
id: "fix-and-verify",
task: "Create a file called calc.ts with a divide function that returns a/b. The function has a bug - it doesn't handle division by zero. Fix the bug by adding a check, then verify the fix works.",
expectedComplete: true,
description: "Bug fix with verification (self-contained)"
},
{
id: "refactor-task",
task: "Create a file counter.ts with a Counter class that has increment() and getCount() methods. Make sure the code follows TypeScript best practices.",
expectedComplete: true,
description: "Code creation with quality requirements"
}
]
// Full test suite - uncomment for comprehensive evaluation
// const FULL_TEST_CASES: TestCase[] = [
// ...TEST_CASES,
// {
// id: "syntax-error",
// task: "Create a file broken.js with invalid JavaScript syntax: function( {",
// expectedComplete: true,
// description: "Create file with intentional syntax error"
// },
// {
// id: "multi-step",
// task: "Create a utils.ts file with an add function, then create a test file that imports and tests it",
// expectedComplete: true,
// description: "Multi-step task with dependencies"
// },
// {
// id: "bug-fix",
// task: "Create a file divide.js with a divide function, but it has a bug: it doesn't handle division by zero. Then fix the bug.",
// expectedComplete: true,
// description: "Bug fix task"
// }
// ]
interface EvalResult {
testCase: TestCase
taskInput: string
agentOutput: string
reflectionInput: string
reflectionOutput: string
evaluationScore: number
evaluationFeedback: string
passed: boolean
durationMs: number
}
async function getCommitId(): Promise<string> {
try {
return execSync("git rev-parse --short HEAD", { encoding: "utf-8" }).trim()
} catch {
return "unknown"
}
}
async function setupProject(dir: string): Promise<void> {
await mkdir(dir, { recursive: true })
const pluginDir = join(dir, ".opencode", "plugin")
await mkdir(pluginDir, { recursive: true })
await cp(PLUGIN_PATH, join(pluginDir, "reflection.ts"))
const config = {
"$schema": "https://opencode.ai/config.json",
"model": MODEL
}
await writeFile(join(dir, "opencode.json"), JSON.stringify(config, null, 2))
}
async function waitForServer(port: number, timeout: number): Promise<boolean> {
const start = Date.now()
while (Date.now() - start < timeout) {
try {
const res = await fetch(`http://localhost:${port}/session`)
if (res.ok) return true
} catch {}
await new Promise(r => setTimeout(r, 500))
}
return false
}
async function runTask(
client: OpencodeClient,
testCase: TestCase
): Promise<EvalResult> {
const start = Date.now()
const result: EvalResult = {
testCase,
taskInput: testCase.task,
agentOutput: "",
reflectionInput: "",
reflectionOutput: "",
evaluationScore: 0,
evaluationFeedback: "",
passed: false,
durationMs: 0
}
try {
// Create session
const { data: session } = await client.session.create({})
if (!session?.id) throw new Error("Failed to create session")
console.log(`[${testCase.id}] Session: ${session.id}`)
// Send task
await client.session.promptAsync({
path: { id: session.id },
body: { parts: [{ type: "text", text: testCase.task }] }
})
// Poll until stable - must wait for assistant to have parts
let lastMsgCount = 0
let lastAssistantParts = 0
let lastAssistantCount = 0
let stableCount = 0
let firstAssistantOutput = ""
let firstAssistantCapturedAt: number | null = null
while (Date.now() - start < TIMEOUT) {
await new Promise(r => setTimeout(r, POLL_INTERVAL))
const { data: messages } = await client.session.messages({
path: { id: session.id }
})
const msgCount = messages?.length || 0
// Count parts in the last assistant message
const assistantMsgs = (messages || []).filter((m: any) => m.info?.role === "assistant")
const lastAssistant = assistantMsgs[assistantMsgs.length - 1]
const assistantParts = lastAssistant?.parts?.length || 0
const assistantCount = assistantMsgs.length
console.log(`[${testCase.id}] Polling: ${msgCount} messages, assistant parts=${assistantParts}, stable=${stableCount}`)
if (!firstAssistantOutput && assistantMsgs.length > 0) {
const candidate = extractTextContent(lastAssistant)
if (candidate) {
firstAssistantOutput = candidate
firstAssistantCapturedAt = Date.now()
}
}
// Only consider stable if:
// 1. We have at least 2 messages (user + assistant)
// 2. The assistant message has at least 1 part
// 3. Both message count AND part count are stable
const isStable = msgCount === lastMsgCount &&
assistantParts === lastAssistantParts &&
assistantCount === lastAssistantCount &&
msgCount >= 2 &&
assistantParts > 0
if (isStable) {
stableCount++
if (stableCount >= STABLE_POLLS_REQUIRED) break
} else {
stableCount = 0
lastMsgCount = msgCount
lastAssistantParts = assistantParts
lastAssistantCount = assistantCount
}
if (firstAssistantCapturedAt && Date.now() - firstAssistantCapturedAt > MAX_WAIT_AFTER_OUTPUT) {
break
}
}
// Extract results
const { data: messages } = await client.session.messages({
path: { id: session.id }
})
console.log(`[${testCase.id}] Messages count: ${messages?.length || 0}`)
if (messages && messages.length > 0) {
// Debug: show all message roles
console.log(`[${testCase.id}] Message roles:`, messages.map((m: any) => m.info?.role))
if (process.env.REFLECTION_DEBUG) {
// Show all messages for debugging
for (let i = 0; i < messages.length; i++) {
const m = messages[i]
console.log(`[${testCase.id}] Message ${i}: role=${m.info?.role}, parts=${m.parts?.length}`)
if (m.parts && m.parts.length > 0) {
const textParts = m.parts.filter((p: any) => p.type === "text")
if (textParts.length > 0) {
console.log(`[${testCase.id}] Message ${i} text preview:`, (textParts[0] as any).text?.slice(0, 100))
}
}
}
}
// SDK returns Array<{ info: Message; parts: Array<Part> }>
// Agent output = last assistant message
const assistantMsgs = messages.filter((m: any) => m.info?.role === "assistant")
if (assistantMsgs.length > 0) {
const lastAssistant = assistantMsgs[assistantMsgs.length - 1]
result.agentOutput = firstAssistantOutput || extractTextContent(lastAssistant)
console.log(`[${testCase.id}] Agent output length: ${result.agentOutput.length}`)
}
// Reflection messages (from reflection plugin feedback)
const reflectionMsgs = messages.filter((m: any) =>
m.info?.role === "user" &&
extractTextContent(m).includes("Reflection")
)
if (reflectionMsgs.length > 0) {
result.reflectionOutput = extractTextContent(reflectionMsgs[reflectionMsgs.length - 1])
}
// Build reflection input (what was sent to judge)
result.reflectionInput = `Task: ${testCase.task}\nAgent Output: ${result.agentOutput.slice(0, 500)}...`
}
// Evaluate the result using LLM-as-judge
const evaluation = await evaluateWithLLM(testCase.task, result.agentOutput, testCase.expectedComplete)
result.evaluationScore = evaluation.score
result.evaluationFeedback = evaluation.feedback
result.passed = evaluation.score >= 3
} catch (error: any) {
result.evaluationFeedback = `Error: ${error.message}`
result.evaluationScore = 0
}
result.durationMs = Date.now() - start
return result
}
function extractTextContent(message: any): string {
// SDK returns { info: Message, parts: Array<Part> }
// parts are at the same level as info, not inside it
const parts = message?.parts
if (!parts || !Array.isArray(parts)) return ""
return parts
.filter((p: any) => p.type === "text")
.map((p: any) => p.text || "")
.join("\n")
.slice(0, 2000)
}
/**
* LLM-as-Judge evaluation using GitHub Models API
* Evaluates agent output against the task requirements
*/
async function evaluateWithLLM(
task: string,
agentOutput: string,
expectedComplete: boolean
): Promise<{ score: number; feedback: string }> {
const GITHUB_TOKEN = process.env.GITHUB_TOKEN
if (!GITHUB_TOKEN) {
console.warn("[WARN] GITHUB_TOKEN not set, falling back to heuristic evaluation")
return evaluateHeuristic(agentOutput, expectedComplete)
}
const evalPrompt = `You are an evaluation judge for AI coding agents.
## Task Given to Agent
${task}
## Agent's Response
${agentOutput.slice(0, 3000)}
## Expected Outcome
The task ${expectedComplete ? "should be completed successfully" : "may have intentional issues"}.
## Evaluation Criteria
1. **Task Completion** (0-2 points): Did the agent complete what was asked?
2. **Correctness** (0-2 points): Is the output correct and functional?
3. **Quality** (0-1 point): Code quality, explanations, best practices
## Instructions
Evaluate the agent's response. Be strict but fair.
Reply with JSON only:
{
"score": <0-5>,
"feedback": "<2-3 sentence evaluation explaining the score>"
}`
try {
const response = await fetch("https://models.inference.ai.azure.com/chat/completions", {
method: "POST",
headers: {
"Authorization": `Bearer ${GITHUB_TOKEN}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "gpt-4o-mini",
messages: [{ role: "user", content: evalPrompt }],
temperature: 0.1,
max_tokens: 500
})
})
if (!response.ok) {
console.warn(`[WARN] LLM eval failed: ${response.status}, falling back to heuristic`)
return evaluateHeuristic(agentOutput, expectedComplete)
}
const data = await response.json() as any
const content = data.choices?.[0]?.message?.content || ""
// Extract JSON from response
const jsonMatch = content.match(/\{[\s\S]*\}/)
if (!jsonMatch) {
console.warn("[WARN] Could not parse LLM eval response, falling back to heuristic")
return evaluateHeuristic(agentOutput, expectedComplete)
}
const verdict = JSON.parse(jsonMatch[0])
return {
score: Math.max(0, Math.min(5, verdict.score || 0)),
feedback: verdict.feedback || "No feedback provided"
}
} catch (error: any) {
console.warn(`[WARN] LLM eval error: ${error.message}, falling back to heuristic`)
return evaluateHeuristic(agentOutput, expectedComplete)
}
}
/**
* Fallback heuristic evaluation when LLM is unavailable
*/
function evaluateHeuristic(agentOutput: string, expectedComplete: boolean): { score: number; feedback: string } {
let score = 0
const feedback: string[] = []
if (agentOutput.length > 50) {
score += 2
feedback.push("Agent produced meaningful output")
} else {
feedback.push("Agent output too short or missing")
}
const completionIndicators = ["created", "done", "completed", "finished", "added", "wrote"]
if (completionIndicators.some(ind => agentOutput.toLowerCase().includes(ind))) {
score += 2
feedback.push("Found completion indicators")
}
const errorIndicators = ["error", "failed", "exception", "cannot"]
if (errorIndicators.some(ind => agentOutput.toLowerCase().includes(ind)) && expectedComplete) {
score -= 1
feedback.push("Found error indicators")
}
return {
score: Math.max(0, Math.min(5, score)),
feedback: `[Heuristic] ${feedback.join("; ")}`
}
}
function scoreToVerdict(score: number): string {
if (score === 5) return "COMPLETE"
if (score === 4) return "MOSTLY_COMPLETE"
if (score === 3) return "PARTIAL"
if (score === 2) return "ATTEMPTED"
if (score === 1) return "FAILED"
return "NO_ATTEMPT"
}
async function generateReport(results: EvalResult[], commitId: string): Promise<string> {
const now = new Date()
const date = now.toISOString().slice(0, 10) // 2026-01-29
const time = now.toISOString().slice(11, 16).replace(":", "-") // 07-41
const filename = `eval-report-${date}-${time}-${commitId}.md`
const passed = results.filter(r => r.passed).length
const failed = results.filter(r => !r.passed).length
const avgScore = (results.reduce((a, r) => a + r.evaluationScore, 0) / results.length).toFixed(1)
let md = `# Agent Evaluation Report
**Date**: ${new Date().toISOString()}
**Commit**: ${commitId}
**Model**: ${MODEL}
**Evaluator**: LLM-as-Judge (gpt-4o-mini)
---
## Summary
| Metric | Value |
|--------|-------|
| Total Tests | ${results.length} |
| Passed (≥3) | ${passed} |
| Failed (<3) | ${failed} |
| Pass Rate | ${Math.round(passed / results.length * 100)}% |
| Avg Score | ${avgScore}/5 |
---
## Results
| Input | Output | Eval LLM Feedback | Score |
|-------|--------|-------------------|-------|
`
for (let i = 0; i < results.length; i++) {
const r = results[i]
const input = r.taskInput.slice(0, 60).replace(/\|/g, "\\|").replace(/\n/g, " ")
const output = r.agentOutput.slice(0, 80).replace(/\|/g, "\\|").replace(/\n/g, " ") || "(no output)"
const feedback = r.evaluationFeedback.slice(0, 100).replace(/\|/g, "\\|").replace(/\n/g, " ")
const icon = r.passed ? "✅" : "❌"
md += `| ${input}... | ${output}... | ${feedback}... | ${icon} ${r.evaluationScore}/5 |\n`
}
md += `\n---\n\n## Full Details\n`
for (let i = 0; i < results.length; i++) {
const r = results[i]
const verdict = scoreToVerdict(r.evaluationScore)
const icon = r.passed ? "✅" : "❌"
md += `
### Test ${i + 1}: ${r.testCase.description}
**Score**: ${icon} ${r.evaluationScore}/5 (${verdict})
**Duration**: ${r.durationMs}ms
#### Task Input
\`\`\`
${r.taskInput}
\`\`\`
#### Agent Output
\`\`\`
${r.agentOutput.slice(0, 1500) || "(no output)"}${r.agentOutput.length > 1500 ? "\n... (truncated)" : ""}
\`\`\`
#### Eval LLM Feedback
> ${r.evaluationFeedback}
${r.reflectionOutput ? `#### Reflection Plugin Output\n\`\`\`\n${r.reflectionOutput.slice(0, 500)}\n\`\`\`\n` : ""}
---
`
}
md += `
## Scoring Rubric
| Score | Verdict | Criteria |
|-------|---------|----------|
| 5 | COMPLETE | Task fully accomplished, all requirements met |
| 4 | MOSTLY_COMPLETE | Task done with minor issues |
| 3 | PARTIAL | Core objective achieved but gaps remain |
| 2 | ATTEMPTED | Progress made but failed to complete |
| 1 | FAILED | Wrong approach or incorrect result |
| 0 | NO_ATTEMPT | No meaningful progress |
**Pass threshold**: Score ≥ 3
`
const outputPath = join(__dirname, "evals", "results", filename)
await mkdir(join(__dirname, "evals", "results"), { recursive: true })
await writeFile(outputPath, md)
console.log(`\nReport written to: ${outputPath}`)
return md
}
async function main() {
const commitId = await getCommitId()
console.log(`Reflection Layer E2E Evaluation`)
console.log(`Commit: ${commitId}`)
console.log(`Model: ${MODEL}`)
console.log(`Tests: ${TEST_CASES.length}`)
console.log("")
// Setup temp project
const tmpDir = join(__dirname, ".eval-tmp")
await rm(tmpDir, { recursive: true, force: true })
await setupProject(tmpDir)
// Start opencode serve
console.log("Starting opencode serve...")
const server = spawn("opencode", ["serve", "--port", String(PORT)], {
cwd: tmpDir,
stdio: ["ignore", "pipe", "pipe"],
env: { ...process.env, REFLECTION_DEBUG: "1" }
})
let serverOutput = ""
server.stdout?.on("data", (d) => serverOutput += d.toString())
server.stderr?.on("data", (d) => serverOutput += d.toString())
try {
const ready = await waitForServer(PORT, 30_000)
if (!ready) {
console.error("Server failed to start")
console.error(serverOutput)
process.exit(1)
}
console.log("Server ready\n")
const client = createOpencodeClient({ baseUrl: `http://localhost:${PORT}` })
const results: EvalResult[] = []
// Run each test case
for (const testCase of TEST_CASES) {
console.log(`Running: ${testCase.id} - ${testCase.description}`)
const result = await runTask(client, testCase)
results.push(result)
console.log(` Score: ${result.evaluationScore}/5 (${scoreToVerdict(result.evaluationScore)})`)
console.log(` Duration: ${result.durationMs}ms`)
console.log("")
}
// Generate report
const report = await generateReport(results, commitId)
console.log("\n" + "=".repeat(80))
console.log(report)
} finally {
server.kill()
await rm(tmpDir, { recursive: true, force: true })
}
}
main().catch(console.error)