-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy patheval.go
More file actions
639 lines (537 loc) · 18.1 KB
/
eval.go
File metadata and controls
639 lines (537 loc) · 18.1 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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
// Package evaluation provides an evaluation framework for testing agents.
package evaluation
import (
"bufio"
"cmp"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"os"
"os/exec"
"path/filepath"
"slices"
"strings"
"sync"
"time"
"github.com/google/uuid"
"golang.org/x/sync/singleflight"
"github.com/docker/docker-agent/pkg/config"
"github.com/docker/docker-agent/pkg/config/latest"
"github.com/docker/docker-agent/pkg/environment"
"github.com/docker/docker-agent/pkg/model/provider"
"github.com/docker/docker-agent/pkg/model/provider/options"
"github.com/docker/docker-agent/pkg/session"
)
// Runner runs evaluations against an agent.
type Runner struct {
Config
agentSource config.Source
judge *Judge
runConfig *config.RuntimeConfig
// imageCache caches built Docker images by working directory.
// Key is the working directory (empty string for no working dir).
imageCache map[string]string
imageCacheMu sync.Mutex
// imageBuildGroup deduplicates concurrent image builds for the same working directory.
imageBuildGroup singleflight.Group
}
// newRunner creates a new evaluation runner.
func newRunner(agentSource config.Source, runConfig *config.RuntimeConfig, judgeModel provider.Provider, cfg Config) *Runner {
var judge *Judge
if judgeModel != nil {
judge = NewJudge(judgeModel, cfg.Concurrency)
}
return &Runner{
Config: cfg,
agentSource: agentSource,
judge: judge,
runConfig: runConfig,
imageCache: make(map[string]string),
}
}
// Evaluate runs evaluations with a specified run name.
// ttyOut is used for progress bar rendering (should be the console/TTY).
// out is used for results and status messages (can be tee'd to a log file).
func Evaluate(ctx context.Context, ttyOut, out io.Writer, isTTY bool, runName string, runConfig *config.RuntimeConfig, cfg Config) (*EvalRun, error) {
agentSource, err := config.Resolve(cfg.AgentFilename, nil)
if err != nil {
return nil, fmt.Errorf("resolving agent: %w", err)
}
// Create judge model provider for relevance checking
judgeModel, err := createJudgeModel(ctx, cfg.JudgeModel, runConfig)
if err != nil {
return nil, err
}
runner := newRunner(agentSource, runConfig, judgeModel, cfg)
fmt.Fprintf(out, "Evaluation run: %s\n", runName)
startTime := time.Now()
results, err := runner.Run(ctx, ttyOut, out, isTTY)
duration := time.Since(startTime)
summary := computeSummary(results)
printSummary(out, summary, duration)
run := &EvalRun{
Name: runName,
Timestamp: startTime,
Duration: duration,
Results: results,
Summary: summary,
}
if err != nil {
return run, fmt.Errorf("running evaluations: %w", err)
}
return run, nil
}
// workItem represents a single evaluation to be processed.
type workItem struct {
index int
eval *InputSession
}
// Run executes all evaluations concurrently and returns results.
// ttyOut is used for progress bar rendering (should be the console/TTY).
// out is used for results and status messages (can be tee'd to a log file).
func (r *Runner) Run(ctx context.Context, ttyOut, out io.Writer, isTTY bool) ([]Result, error) {
fmt.Fprintln(out, "Loading evaluation sessions...")
evals, err := r.loadEvalSessions(ctx)
if err != nil {
return nil, fmt.Errorf("loading evaluations: %w", err)
}
// Check whether any evaluations require relevance checking.
// If so, the judge must be configured and working; validate eagerly
// to fail fast on configuration issues (bad API key, wrong model, etc.)
// instead of silently producing zero-relevance results.
if needsJudge(evals) {
if r.judge == nil {
return nil, errors.New("some evaluations have relevance criteria but no judge model is configured (use --judge-model)")
}
fmt.Fprintln(out, "Validating judge model...")
if err := r.judge.Validate(ctx); err != nil {
return nil, fmt.Errorf("%w", err)
}
}
// Pre-build all unique Docker images in parallel before running evaluations.
// This avoids serialized builds when multiple workers need the same image.
if err := r.preBuildImages(ctx, out, evals); err != nil {
return nil, fmt.Errorf("pre-building images: %w", err)
}
fmt.Fprintf(out, "Running %d evaluations with concurrency %d\n\n", len(evals), r.Concurrency)
progress := newProgressBar(ttyOut, out, r.TTYFd, len(evals), isTTY)
progress.start()
defer progress.stop()
results := make([]Result, len(evals))
work := make(chan workItem, len(evals))
for i := range evals {
work <- workItem{index: i, eval: &evals[i]}
}
close(work)
var wg sync.WaitGroup
for range r.Concurrency {
wg.Go(func() {
for item := range work {
if ctx.Err() != nil {
return
}
progress.setRunning(item.eval.Title)
result, runErr := r.runSingleEval(ctx, item.eval)
if runErr != nil {
result.Error = runErr.Error()
slog.Error("Evaluation failed", "title", item.eval.Title, "error", runErr)
}
results[item.index] = result
_, failures := result.checkResults()
progress.complete(result.Title, len(failures) == 0)
progress.printResult(result)
}
})
}
wg.Wait()
if ctx.Err() != nil {
return results, ctx.Err()
}
return results, nil
}
func (r *Runner) loadEvalSessions(ctx context.Context) ([]InputSession, error) {
entries, err := os.ReadDir(r.EvalsDir)
if err != nil {
return nil, err
}
var evals []InputSession
for _, entry := range entries {
if ctx.Err() != nil {
return nil, ctx.Err()
}
// Filter by --only patterns against file name if specified
fileName := entry.Name()
if len(r.Only) > 0 && !matchesAnyPattern(fileName, r.Only) {
continue
}
if entry.IsDir() || !strings.HasSuffix(fileName, ".json") {
continue
}
data, err := os.ReadFile(filepath.Join(r.EvalsDir, fileName))
if err != nil {
return nil, err
}
var evalSess session.Session
if err := json.Unmarshal(data, &evalSess); err != nil {
return nil, err
}
evals = append(evals, InputSession{
Session: &evalSess,
SourcePath: filepath.Join(r.EvalsDir, fileName),
})
}
// Sort by duration (longest first) to avoid long tail
slices.SortFunc(evals, func(a, b InputSession) int {
return cmp.Compare(b.Duration(), a.Duration())
})
return evals, nil
}
// preBuildImages pre-builds all unique Docker images needed for the evaluations.
// This is done in parallel to avoid serialized builds during evaluation.
func (r *Runner) preBuildImages(ctx context.Context, out io.Writer, evals []InputSession) error {
// Collect unique working directories
workingDirs := make(map[string]struct{})
for _, eval := range evals {
if eval.Evals != nil {
workingDirs[eval.Evals.WorkingDir] = struct{}{}
}
}
if len(workingDirs) == 0 {
return nil
}
fmt.Fprintf(out, "Pre-building %d Docker image(s)...\n", len(workingDirs))
// Build images in parallel with limited concurrency
type buildResult struct {
workingDir string
err error
}
work := make(chan string, len(workingDirs))
for wd := range workingDirs {
work <- wd
}
close(work)
results := make(chan buildResult, len(workingDirs))
// Use same concurrency as evaluation runs for image builds
buildWorkers := min(r.Concurrency, len(workingDirs))
var wg sync.WaitGroup
for range buildWorkers {
wg.Go(func() {
for wd := range work {
if ctx.Err() != nil {
results <- buildResult{workingDir: wd, err: ctx.Err()}
continue
}
_, err := r.getOrBuildImage(ctx, wd)
results <- buildResult{workingDir: wd, err: err}
}
})
}
// Wait for all builds to complete
go func() {
wg.Wait()
close(results)
}()
// Collect errors
var errs []error
for result := range results {
if result.err != nil {
errs = append(errs, fmt.Errorf("building image for %q: %w", result.workingDir, result.err))
}
}
if len(errs) > 0 {
return fmt.Errorf("failed to build %d image(s): %w", len(errs), errs[0])
}
return nil
}
func (r *Runner) runSingleEval(ctx context.Context, evalSess *InputSession) (Result, error) {
startTime := time.Now()
slog.Debug("Starting evaluation", "title", evalSess.Title)
var evals *session.EvalCriteria
if evalSess.Evals != nil {
evals = evalSess.Evals
} else {
evals = &session.EvalCriteria{}
}
userMessages := getUserMessages(evalSess.Session)
result := Result{
InputPath: evalSess.SourcePath,
Title: evalSess.Title,
Question: strings.Join(userMessages, "\n"),
SizeExpected: evals.Size,
RelevanceExpected: float64(len(evals.Relevance)),
}
expectedToolCalls := extractToolCalls(evalSess.Messages)
if len(expectedToolCalls) > 0 {
result.ToolCallsExpected = 1.0
}
workingDir := evals.WorkingDir
imageID, err := r.getOrBuildImage(ctx, workingDir)
if err != nil {
return result, fmt.Errorf("building eval image: %w", err)
}
events, err := r.runDockerAgentInContainer(ctx, imageID, userMessages, evals.Setup)
if err != nil {
return result, fmt.Errorf("running docker agent in container: %w", err)
}
response, cost, outputTokens, actualToolCalls := parseContainerEvents(events)
result.Response = response
result.Cost = cost
result.OutputTokens = outputTokens
result.Size = getResponseSize(result.Response)
// Build session from events for database storage
result.Session = SessionFromEvents(events, evalSess.Title, userMessages)
result.Session.Evals = evals
if len(expectedToolCalls) > 0 || len(actualToolCalls) > 0 {
result.ToolCallsScore = toolCallF1Score(expectedToolCalls, actualToolCalls)
}
if r.judge != nil && len(evals.Relevance) > 0 {
// Use transcript for relevance checking to preserve temporal ordering
transcript := buildTranscript(events)
passed, failed, err := r.judge.CheckRelevance(ctx, transcript, evals.Relevance)
if err != nil {
return result, fmt.Errorf("relevance check failed: %w", err)
}
result.RelevancePassed = float64(passed)
result.FailedRelevance = failed
}
slog.Debug("Evaluation complete", "title", evalSess.Title, "duration", time.Since(startTime))
return result, nil
}
func (r *Runner) runDockerAgentInContainer(ctx context.Context, imageID string, questions []string, setup string) ([]map[string]any, error) {
agentDir := r.agentSource.ParentDir()
agentFile := filepath.Base(r.agentSource.Name())
containerName := fmt.Sprintf("docker-agent-eval-%d", uuid.New().ID())
args := []string{
"run",
"--name", containerName,
"--privileged",
"--init",
}
if !r.KeepContainers {
args = append(args, "--rm")
}
args = append(args,
"-i",
"-v", agentDir+":/configs:ro",
)
var env []string
for _, name := range []string{"OPENAI_API_KEY", "ANTHROPIC_API_KEY", "GOOGLE_API_KEY", "MISTRAL_API_KEY", "XAI_API_KEY", "NEBIUS_API_KEY"} {
if val, ok := r.runConfig.EnvProvider().Get(ctx, name); ok && val != "" {
args = append(args, "-e", name)
env = append(env, name+"="+val)
}
}
if r.runConfig.ModelsGateway != "" {
args = append(args, "-e", "DOCKER_AGENT_MODELS_GATEWAY")
env = append(env, "DOCKER_AGENT_MODELS_GATEWAY="+r.runConfig.ModelsGateway)
if token, ok := r.runConfig.EnvProvider().Get(ctx, environment.DockerDesktopTokenEnv); ok && token != "" {
args = append(args, "-e", environment.DockerDesktopTokenEnv)
env = append(env, environment.DockerDesktopTokenEnv+"="+token)
}
}
// Pass additional environment variables specified via -e flag
// Format: KEY or KEY=VALUE
for _, entry := range r.EnvVars {
if key, val, hasValue := strings.Cut(entry, "="); hasValue && key != "" {
args = append(args, "-e", key)
env = append(env, key+"="+val)
} else if val, ok := r.runConfig.EnvProvider().Get(ctx, entry); ok && entry != "" {
args = append(args, "-e", entry)
env = append(env, entry+"="+val)
}
}
// When a setup script is provided, mount it into the container and
// override the entrypoint to run it before docker agent run --exec.
// The default entrypoint is: /run.sh /docker-agent run --exec --yolo --json
// /run.sh starts dockerd then exec's "$@".
if setup != "" {
setupFile := filepath.Join(os.TempDir(), fmt.Sprintf("docker-agent-eval-setup-%d.sh", uuid.New().ID()))
if err := os.WriteFile(setupFile, []byte(setup), 0o600); err != nil {
return nil, fmt.Errorf("writing setup script: %w", err)
}
defer os.Remove(setupFile)
args = append(args,
"-v", setupFile+":/setup.sh:ro",
"--entrypoint", "/run.sh",
)
}
args = append(args, imageID)
if setup != "" {
// Run setup script, then docker agent run --exec with the original arguments.
args = append(args, "sh", "-c", "sh /setup.sh && exec /docker-agent run --exec --yolo --json \"$@\"", "--", "/configs/"+agentFile)
} else {
args = append(args, "/configs/"+agentFile)
}
args = append(args, questions...)
cmd := exec.CommandContext(ctx, "docker", args...)
cmd.Env = append(env, os.Environ()...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("creating stdout pipe: %w", err)
}
stderr, err := cmd.StderrPipe()
if err != nil {
return nil, fmt.Errorf("creating stderr pipe: %w", err)
}
if err := cmd.Start(); err != nil {
return nil, fmt.Errorf("starting docker run: %w", err)
}
var stderrData []byte
go func() {
stderrData, _ = io.ReadAll(stderr)
}()
var events []map[string]any
scanner := bufio.NewScanner(stdout)
scanner.Buffer(make([]byte, 0, 1024*1024), 10*1024*1024)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
var event map[string]any
if err := json.Unmarshal([]byte(line), &event); err != nil {
slog.Debug("Failed to parse JSON event", "line", line, "error", err)
continue
}
events = append(events, event)
}
if err := scanner.Err(); err != nil {
slog.Warn("Error reading container output", "error", err)
}
waitErr := cmd.Wait()
if waitErr != nil {
slog.Debug("Container exited with error", "stderr", string(stderrData), "error", waitErr)
}
if len(events) == 0 {
stderrStr := strings.TrimSpace(string(stderrData))
if waitErr != nil {
return nil, fmt.Errorf("container failed: %w (stderr: %s)", waitErr, stderrStr)
}
if stderrStr != "" {
return nil, fmt.Errorf("no events received from container (stderr: %s)", stderrStr)
}
return nil, errors.New("no events received from container")
}
return events, nil
}
func parseContainerEvents(events []map[string]any) (response string, cost float64, outputTokens int64, toolCalls []string) {
var responseBuf strings.Builder
for _, event := range events {
eventType, _ := event["type"].(string)
switch eventType {
case "agent_choice":
if content, ok := event["content"].(string); ok {
responseBuf.WriteString(content)
}
case "tool_call":
if tc, ok := event["tool_call"].(map[string]any); ok {
if fn, ok := tc["function"].(map[string]any); ok {
if name, ok := fn["name"].(string); ok {
toolCalls = append(toolCalls, name)
}
}
}
case "token_usage":
if usage, ok := event["usage"].(map[string]any); ok {
if c, ok := usage["cost"].(float64); ok {
cost = c
}
if tokens, ok := usage["output_tokens"].(float64); ok {
outputTokens += int64(tokens)
}
}
}
}
return responseBuf.String(), cost, outputTokens, toolCalls
}
// buildTranscript creates a chronological transcript of agent interactions.
// Unlike parseContainerEvents which only extracts text, this preserves the
// temporal sequence of events, enabling evaluation of criteria like
// "explains before executing" or "announces tool usage beforehand".
func buildTranscript(events []map[string]any) string {
var transcript strings.Builder
var pendingText strings.Builder
var currentAgent string
flushText := func() {
if pendingText.Len() == 0 {
return
}
fmt.Fprintf(&transcript, "[Agent %s says]:\n%s\n\n", cmp.Or(currentAgent, "unknown"), pendingText.String())
pendingText.Reset()
}
for _, event := range events {
switch event["type"] {
case "agent_choice":
if agentName, _ := event["agent_name"].(string); agentName != "" {
currentAgent = agentName
}
if content, _ := event["content"].(string); content != "" {
pendingText.WriteString(content)
}
case "tool_call":
flushText()
name, args := getToolCallInfo(event)
if agentName, _ := event["agent_name"].(string); agentName != "" {
currentAgent = agentName
}
fmt.Fprintf(&transcript, "[Agent %s calls tool %q with arguments: %s]\n\n", cmp.Or(currentAgent, "unknown"), name, args)
case "tool_call_response":
name, _ := getToolCallInfo(event)
response, _ := event["response"].(string)
if len(response) > 500 {
response = response[:500] + "...(truncated)"
}
fmt.Fprintf(&transcript, "[Tool %q returns: %s]\n\n", name, response)
}
}
flushText()
return transcript.String()
}
// getToolCallInfo extracts the tool name and arguments from an event.
func getToolCallInfo(event map[string]any) (name, args string) {
tc, _ := event["tool_call"].(map[string]any)
fn, _ := tc["function"].(map[string]any)
name, _ = fn["name"].(string)
args, _ = fn["arguments"].(string)
return name, args
}
// matchesAnyPattern returns true if the name contains any of the patterns (case-insensitive).
func matchesAnyPattern(name string, patterns []string) bool {
nameLower := strings.ToLower(name)
return slices.ContainsFunc(patterns, func(pattern string) bool {
return strings.Contains(nameLower, strings.ToLower(pattern))
})
}
// needsJudge returns true if any evaluation session has relevance criteria,
// meaning a judge model is required to evaluate them.
func needsJudge(evals []InputSession) bool {
return slices.ContainsFunc(evals, func(s InputSession) bool {
return s.Evals != nil && len(s.Evals.Relevance) > 0
})
}
// createJudgeModel creates a provider.Provider from a model string (format: provider/model).
// Returns nil if judgeModel is empty.
func createJudgeModel(ctx context.Context, judgeModel string, runConfig *config.RuntimeConfig) (provider.Provider, error) {
if judgeModel == "" {
return nil, nil
}
cfg, err := latest.ParseModelRef(judgeModel)
if err != nil {
return nil, fmt.Errorf("invalid judge model format %q: expected 'provider/model'", judgeModel)
}
opts := []options.Opt{
options.WithThinking(false),
options.WithStructuredOutput(judgeResponseSchema),
}
if runConfig.ModelsGateway != "" {
opts = append(opts, options.WithGateway(runConfig.ModelsGateway))
}
judge, err := provider.New(ctx, &cfg, runConfig.EnvProvider(), opts...)
if err != nil {
return nil, fmt.Errorf("creating judge model: %w", err)
}
return judge, nil
}