-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDevserverTest.java
More file actions
1324 lines (1159 loc) · 62.2 KB
/
Copy pathDevserverTest.java
File metadata and controls
1324 lines (1159 loc) · 62.2 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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package dev.braintrust.devserver;
import static dev.braintrust.json.BraintrustJsonMapper.fromJson;
import static org.junit.jupiter.api.Assertions.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import dev.braintrust.BraintrustUtils;
import dev.braintrust.TestHarness;
import dev.braintrust.eval.Score;
import dev.braintrust.eval.Scorer;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.sdk.trace.data.SpanData;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.*;
@Slf4j
class DevserverTest {
private static Devserver server;
private static Thread serverThread;
private static TestHarness testHarness;
// private static final int TEST_PORT = TestUtils.getRandomOpenPort();
private static final int TEST_PORT = 8301;
private static final String TEST_URL = "http://localhost:" + TEST_PORT;
private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
private static final String REMOTE_EVAL_NAME = "food-type-classifier";
private static final String PARAM_EVAL_NAME = "param-eval";
private static final String TASK_ERROR_EVAL_NAME = "task-error-eval";
private static final String SCORER_ERROR_EVAL_NAME = "scorer-error-eval";
private static final BraintrustUtils.Parent PLAYGROUND_PARENT =
new BraintrustUtils.Parent("playground_id", "ceea7422-3507-4d1c-a5f7-7acf41d9fac2");
// Remote scorer created in the java-unit-test project via the test harness. Its latest version
// returns 1.0 for an exact match and 0.0 otherwise. The score is keyed in results by the
// scorer's resolved name: "invoke-<project>-<slug>-<version>".
private static final String REMOTE_SCORER_SLUG = "typescript-exact-match";
private static String remoteScorerFunctionId;
// Resolved scorer name (set in setUp once the project name is known).
private static String REMOTE_SCORER_NAME;
// The name embedded in the remote scorer's return payload (see REMOTE_SCORER_CODE). When the
// remote invoke actually executes (VCR record/off), the score is keyed by this name. In replay
// mode the invoke request cannot be matched by a cassette (its body embeds live span IDs via
// `parent`), so the scorer falls back to scoreForScorerException and the score is keyed by
// REMOTE_SCORER_NAME instead. Assertions must accept both.
private static final String REMOTE_SCORER_PAYLOAD_NAME = "typescript exact match";
@BeforeAll
static void setUp() throws Exception {
// Set up test harness with VCR (records/replays HTTP interactions)
testHarness = TestHarness.setup();
// Ensure the remote code scorer exists and resolve its function ID. The latest version
// returns {name: "typescript exact match", score: output === expected ? 1.0 : 0.0}.
var scorerInfo = testHarness.ensureRemoteCodeScorer(REMOTE_SCORER_SLUG, REMOTE_SCORER_CODE);
remoteScorerFunctionId =
lookupFunctionId(TestHarness.defaultProjectName(), scorerInfo.slug());
REMOTE_SCORER_NAME =
"invoke-" + TestHarness.defaultProjectName() + "-" + scorerInfo.slug() + "-latest";
// Create a shared eval for all tests
RemoteEval<String, String> testEval =
RemoteEval.<String, String>builder()
.name(REMOTE_EVAL_NAME)
.taskFunction(
input -> {
// Create a span inside the task to test baggage propagation
var tracer = dev.braintrust.trace.BraintrustTracing.getTracer();
var span = tracer.spanBuilder("custom-task-span").startSpan();
try (var scope =
io.opentelemetry.context.Context.current()
.with(span)
.makeCurrent()) {
// Do some work
return "java-fruit";
} finally {
span.end();
}
})
.scorer(Scorer.of("simple_scorer", (expected, result) -> 0.7))
.build();
// Eval whose task throws for "bad-input"
RemoteEval<String, String> taskErrorEval =
RemoteEval.<String, String>builder()
.name(TASK_ERROR_EVAL_NAME)
.taskFunction(
input -> {
if ("bad-input".equals(input)) {
throw new RuntimeException("task failed on bad-input");
}
return "result";
})
.scorer(Scorer.of("exact_match", (expected, result) -> 0.7))
.build();
// Eval with a scorer that always throws
RemoteEval<String, String> scorerErrorEval =
RemoteEval.<String, String>builder()
.name(SCORER_ERROR_EVAL_NAME)
.taskFunction(input -> "result")
.scorer(
new Scorer<String, String>() {
@Override
public String getName() {
return "broken_scorer";
}
@Override
public List<Score> score(
dev.braintrust.eval.TaskResult<String, String>
taskResult) {
throw new RuntimeException("scorer is broken");
}
})
.scorer(Scorer.of("working_scorer", (expected, result) -> 1.0))
.build();
// Eval with parameters — task returns the model param as its output
RemoteEval<String, String> paramEval =
RemoteEval.<String, String>builder()
.name(PARAM_EVAL_NAME)
.parameter(dev.braintrust.eval.ParameterDef.data("model", "gpt-4"))
.parameter(dev.braintrust.eval.ParameterDef.data("temperature", 0.5))
.task(
new dev.braintrust.eval.Task<>() {
@Override
public dev.braintrust.eval.TaskResult<String, String> apply(
dev.braintrust.eval.DatasetCase<String, String>
datasetCase,
dev.braintrust.eval.Parameters parameters)
throws Exception {
// Echo both params so tests can verify defaults + overrides
String model = parameters.get("model", String.class);
Double temp = parameters.get("temperature", Double.class);
String output = model + ":" + temp;
return new dev.braintrust.eval.TaskResult<>(
output, datasetCase, parameters);
}
})
.scorer(Scorer.of("static_scorer", (expected, result) -> 1.0))
.build();
server =
Devserver.builder()
.config(testHarness.braintrust().config())
.registerEval(testEval)
.registerEval(paramEval)
.registerEval(taskErrorEval)
.registerEval(scorerErrorEval)
.host("localhost")
.port(TEST_PORT)
.build();
// Start server in background thread
serverThread =
new Thread(
() -> {
try {
server.start();
} catch (Exception e) {
log.error("unable to start dev server", e);
}
});
serverThread.start();
// Give server time to start
Thread.sleep(1000);
}
@AfterAll
@SneakyThrows
static void tearDown() {
if (server != null) {
server.stop();
}
if (serverThread != null) {
serverThread.join(30_000);
if (serverThread.isAlive()) {
serverThread.interrupt();
}
}
}
@Test
void testHealthCheck() throws Exception {
// Test health check endpoint using the shared devserver
HttpClient client = HttpClient.newHttpClient();
HttpRequest request =
HttpRequest.newBuilder().uri(URI.create(TEST_URL + "/")).GET().build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(200, response.statusCode());
assertEquals("Hello, world!", response.body());
assertEquals("text/plain", response.headers().firstValue("Content-Type").orElse(""));
}
@Test
void testStreamingEval() throws Exception {
// Create eval request with inline data using EvalRequest types
EvalRequest evalRequest = new EvalRequest();
evalRequest.setName(REMOTE_EVAL_NAME);
evalRequest.setStream(true);
// Create inline data
EvalRequest.DataSpec dataSpec = new EvalRequest.DataSpec();
EvalRequest.EvalCaseData case1 = new EvalRequest.EvalCaseData();
case1.setInput("apple");
case1.setExpected("fruit");
EvalRequest.EvalCaseData case2 = new EvalRequest.EvalCaseData();
case2.setInput("carrot");
case2.setExpected("vegetable");
dataSpec.setData(List.of(case1, case2));
evalRequest.setData(dataSpec);
Map<String, Object> parentSpec =
Map.of(
"object_type", PLAYGROUND_PARENT.type(),
"object_id", PLAYGROUND_PARENT.id(),
"propagated_event",
Map.of("span_attributes", Map.of("generation", "test-gen-1")));
evalRequest.setParent(parentSpec);
// Add remote scorer from Braintrust
EvalRequest.RemoteScorer remoteScorer = new EvalRequest.RemoteScorer();
remoteScorer.setName(REMOTE_SCORER_NAME);
EvalRequest.FunctionId functionId = new EvalRequest.FunctionId();
functionId.setFunctionId(remoteScorerFunctionId);
remoteScorer.setFunctionId(functionId);
evalRequest.setScores(List.of(remoteScorer));
String requestBody = JSON_MAPPER.writeValueAsString(evalRequest);
// Make POST request to /eval with auth headers
HttpURLConnection conn =
(HttpURLConnection) new URI(TEST_URL + "/eval").toURL().openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("x-bt-auth-token", testHarness.braintrustApiKey());
conn.setRequestProperty("x-bt-project-id", TestHarness.defaultProjectId());
conn.setRequestProperty("x-bt-org-name", TestHarness.defaultOrgName());
conn.setDoOutput(true);
// Write request body
conn.getOutputStream().write(requestBody.getBytes(StandardCharsets.UTF_8));
conn.getOutputStream().flush();
// Read SSE response
assertEquals(200, conn.getResponseCode());
assertEquals("text/event-stream", conn.getHeaderField("Content-Type"));
BufferedReader reader =
new BufferedReader(
new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8));
List<Map<String, String>> events = new ArrayList<>();
String line;
String currentEvent = null;
StringBuilder currentData = new StringBuilder();
while ((line = reader.readLine()) != null) {
if (line.startsWith("event: ")) {
currentEvent = line.substring(7);
} else if (line.startsWith("data: ")) {
currentData.append(line.substring(6));
} else if (line.isEmpty() && currentEvent != null) {
// End of event
events.add(Map.of("event", currentEvent, "data", currentData.toString()));
currentEvent = null;
currentData = new StringBuilder();
}
}
reader.close();
// Assert event structure
assertFalse(events.isEmpty(), "Should have received events");
// Count events by type
List<Map<String, String>> progressEvents =
events.stream().filter(e -> "progress".equals(e.get("event"))).toList();
List<Map<String, String>> summaryEvents =
events.stream().filter(e -> "summary".equals(e.get("event"))).toList();
List<Map<String, String>> doneEvents =
events.stream().filter(e -> "done".equals(e.get("event"))).toList();
// Should have 1 start event, 2 progress events (one per dataset case), 1 summary, 1 done
assertEquals(2, progressEvents.size(), "Should have 2 progress events");
assertEquals(1, summaryEvents.size(), "Should have 1 summary event");
assertEquals(1, doneEvents.size(), "Should have 1 done event");
// Verify progress events match expected structure
for (Map<String, String> progressEvent : progressEvents) {
String dataJson = progressEvent.get("data");
JsonNode progressData = JSON_MAPPER.readTree(dataJson);
// Assert expected fields in progress event
assertTrue(progressData.has("id"), "Progress event should have id");
assertEquals("task", progressData.get("object_type").asText());
assertEquals(REMOTE_EVAL_NAME, progressData.get("name").asText());
assertEquals("code", progressData.get("format").asText());
assertEquals("completion", progressData.get("output_type").asText());
assertEquals("json_delta", progressData.get("event").asText());
// Assert data field contains the task result
String taskResultJson = progressData.get("data").asText();
assertEquals("\"java-fruit\"", taskResultJson);
}
{ // Verify summary event
Map<String, String> summaryEvent = summaryEvents.get(0);
JsonNode summaryData = JSON_MAPPER.readTree(summaryEvent.get("data"));
assertEquals(TestHarness.defaultProjectName(), summaryData.get("projectName").asText());
assertTrue(summaryData.has("projectId"));
assertEquals(REMOTE_EVAL_NAME, summaryData.get("experimentName").asText());
// Verify scores in summary
assertTrue(summaryData.has("scores"));
JsonNode scores = summaryData.get("scores");
// Verify local scorer
assertTrue(scores.has("simple_scorer"), "Summary should have simple_scorer");
JsonNode simpleScorer = scores.get("simple_scorer");
assertEquals("simple_scorer", simpleScorer.get("name").asText());
assertEquals(0.7, simpleScorer.get("score").asDouble(), 0.001);
// Verify remote scorer (returns 0.0 because output "java-fruit" != expected
// "fruit"/"vegetable"). Keyed by the payload name on a real invoke (record/off) or
// by the resolved scorer name when replay falls back on an unmatched invoke request.
String remoteScorerKey =
scores.has(REMOTE_SCORER_PAYLOAD_NAME)
? REMOTE_SCORER_PAYLOAD_NAME
: REMOTE_SCORER_NAME;
assertTrue(
scores.has(remoteScorerKey),
"Summary should have remote scorer under '%s' or '%s' -- got: %s"
.formatted(REMOTE_SCORER_PAYLOAD_NAME, REMOTE_SCORER_NAME, scores));
JsonNode remoteScorerResult = scores.get(remoteScorerKey);
assertEquals(remoteScorerKey, remoteScorerResult.get("name").asText());
assertEquals(0.0, remoteScorerResult.get("score").asDouble(), 0.001);
}
// Get exported spans from test harness (since devserver uses global tracer)
// Filter to only spans belonging to this test by finding trace IDs from eval spans
// with the right generation tag
List<SpanData> allSpans = testHarness.awaitExportedSpans();
var traceIds =
allSpans.stream()
.filter(
s -> {
var attrs =
s.getAttributes()
.get(
AttributeKey.stringKey(
"braintrust.span_attributes"));
return attrs != null && attrs.contains("test-gen-1");
})
.map(s -> s.getTraceId())
.collect(java.util.stream.Collectors.toSet());
List<SpanData> exportedSpans =
allSpans.stream().filter(s -> traceIds.contains(s.getTraceId())).toList();
assertFalse(exportedSpans.isEmpty(), "Should have exported spans");
// We should have 2 eval traces (one per dataset case), each with task, scores, and custom
// spans
// Each trace has: 1 eval span, 1 task span, 2 score spans (local + remote), 1
// custom-task-span = 5 spans
// per case
// Total: 2 cases * 5 spans = 10 spans
assertEquals(10, exportedSpans.size(), "Should have 10 spans (5 per dataset case)");
// Verify span types
var evalSpans = exportedSpans.stream().filter(s -> s.getName().equals("eval")).toList();
var taskSpans = exportedSpans.stream().filter(s -> s.getName().equals("task")).toList();
var scoreSpans = exportedSpans.stream().filter(s -> s.getName().equals("score")).toList();
var customSpans =
exportedSpans.stream().filter(s -> s.getName().equals("custom-task-span")).toList();
assertEquals(2, evalSpans.size(), "Should have 2 eval spans");
assertEquals(2, taskSpans.size(), "Should have 2 task spans");
assertEquals(4, scoreSpans.size(), "Should have 4 score spans (2 scorers x 2 cases)");
assertEquals(2, customSpans.size(), "Should have 2 custom-task-span spans");
// Verify eval spans have all required attributes
for (SpanData evalSpan : evalSpans) {
// Verify braintrust.parent
String parent =
evalSpan.getAttributes()
.get(
io.opentelemetry.api.common.AttributeKey.stringKey(
"braintrust.parent"));
assertEquals(
PLAYGROUND_PARENT.toParentValue(),
parent,
"Eval span should have parent attribute");
String spanAttrsJson =
evalSpan.getAttributes()
.get(
io.opentelemetry.api.common.AttributeKey.stringKey(
"braintrust.span_attributes"));
assertNotNull(spanAttrsJson, "Eval span should have span_attributes");
JsonNode spanAttrs = JSON_MAPPER.readTree(spanAttrsJson);
assertEquals("eval", spanAttrs.get("type").asText());
assertEquals("eval", spanAttrs.get("name").asText());
assertEquals("test-gen-1", spanAttrs.get("generation").asText());
String inputJson =
evalSpan.getAttributes()
.get(
io.opentelemetry.api.common.AttributeKey.stringKey(
"braintrust.input_json"));
assertNotNull(inputJson, "Eval span should have input_json");
String expectedJson =
evalSpan.getAttributes()
.get(
io.opentelemetry.api.common.AttributeKey.stringKey(
"braintrust.expected_json"));
assertNotNull(expectedJson, "Eval span should have expected_json");
String outputJson =
evalSpan.getAttributes()
.get(
io.opentelemetry.api.common.AttributeKey.stringKey(
"braintrust.output_json"));
assertNotNull(outputJson, "Eval span should have output_json");
JsonNode output = JSON_MAPPER.readTree(outputJson);
assertEquals("java-fruit", output.get("output").asText());
}
for (SpanData taskSpan : taskSpans) {
// Verify braintrust.parent
String parent =
taskSpan.getAttributes()
.get(
io.opentelemetry.api.common.AttributeKey.stringKey(
"braintrust.parent"));
assertEquals(PLAYGROUND_PARENT.toParentValue(), parent);
String spanAttrsJson =
taskSpan.getAttributes()
.get(
io.opentelemetry.api.common.AttributeKey.stringKey(
"braintrust.span_attributes"));
assertNotNull(spanAttrsJson, "Task span should have span_attributes");
JsonNode spanAttrs = JSON_MAPPER.readTree(spanAttrsJson);
assertEquals("task", spanAttrs.get("type").asText());
assertEquals("task", spanAttrs.get("name").asText());
assertEquals("test-gen-1", spanAttrs.get("generation").asText());
String inputJson =
taskSpan.getAttributes()
.get(
io.opentelemetry.api.common.AttributeKey.stringKey(
"braintrust.input_json"));
assertNotNull(inputJson, "Task span should have input_json");
String outputJson =
taskSpan.getAttributes()
.get(
io.opentelemetry.api.common.AttributeKey.stringKey(
"braintrust.output_json"));
assertNotNull(outputJson, "Task span should have output_json");
JsonNode output = JSON_MAPPER.readTree(outputJson);
assertEquals("java-fruit", output.get("output").asText());
}
for (SpanData scoreSpan : scoreSpans) {
// Verify braintrust.parent
String parent =
scoreSpan
.getAttributes()
.get(
io.opentelemetry.api.common.AttributeKey.stringKey(
"braintrust.parent"));
assertEquals(PLAYGROUND_PARENT.toParentValue(), parent);
// Verify braintrust.span_attributes
String spanAttrsJson =
scoreSpan
.getAttributes()
.get(
io.opentelemetry.api.common.AttributeKey.stringKey(
"braintrust.span_attributes"));
assertNotNull(spanAttrsJson, "Score span should have span_attributes");
JsonNode spanAttrs = JSON_MAPPER.readTree(spanAttrsJson);
assertEquals("score", spanAttrs.get("type").asText());
assertEquals("scorer", spanAttrs.get("purpose").asText());
assertEquals("test-gen-1", spanAttrs.get("generation").asText());
// Scorer name should be either simple_scorer or the remote scorer. The remote
// scorer's span name is "invoke-<project>-<slug>-<version>".
String scorerName = spanAttrs.get("name").asText();
assertTrue(
scorerName.contains("simple_scorer") || scorerName.contains(REMOTE_SCORER_SLUG),
"Score span name should be simple_scorer or contain %s -- got: %s"
.formatted(REMOTE_SCORER_SLUG, scorerName));
// Verify braintrust.output_json contains scores
String outputJson =
scoreSpan
.getAttributes()
.get(
io.opentelemetry.api.common.AttributeKey.stringKey(
"braintrust.output_json"));
assertNotNull(outputJson, "Score span should have output_json");
JsonNode output = JSON_MAPPER.readTree(outputJson);
if (scorerName.equals("simple_scorer")) {
assertTrue(
output.has("simple_scorer"), "Output should contain simple_scorer results");
assertEquals(0.7, output.get("simple_scorer").asDouble(), 0.001);
} else {
// Keyed by the payload name on a real invoke (record/off) or by the resolved
// scorer name when replay falls back on an unmatched invoke request.
String remoteScorerKey =
output.has(REMOTE_SCORER_PAYLOAD_NAME)
? REMOTE_SCORER_PAYLOAD_NAME
: REMOTE_SCORER_NAME;
assertTrue(
output.has(remoteScorerKey),
"Output should contain remote scorer results under '%s' or '%s' -- got: %s"
.formatted(REMOTE_SCORER_PAYLOAD_NAME, REMOTE_SCORER_NAME, output));
assertEquals(0.0, output.get(remoteScorerKey).asDouble(), 0.001);
}
}
for (SpanData customSpan : customSpans) {
// Verify it has a parent span (is not a root span)
assertTrue(
customSpan.getParentSpanContext().isValid(),
"Custom span should have a valid parent span context");
assertNotEquals(
io.opentelemetry.api.trace.SpanId.getInvalid(),
customSpan.getParentSpanId(),
"Custom span should not be a root span (should have parent span ID)");
// Verify it has braintrust.parent attribute from baggage propagation
String parent =
customSpan
.getAttributes()
.get(
io.opentelemetry.api.common.AttributeKey.stringKey(
"braintrust.parent"));
assertEquals(PLAYGROUND_PARENT.toParentValue(), parent);
}
}
@Test
void testExperimentEval() throws Exception {
// A remote eval triggered as an *Experiment* (snapshot) from the UI sends no playground
// parent — instead an experiment_name (+ project via headers). The devserver should build
// and run a standard Eval, which creates a fresh experiment and parents spans to
// experiment_id:<id>. It then streams only summary + done (no per-case progress).
final String experimentName = "java-experiment-repro";
EvalRequest evalRequest = new EvalRequest();
evalRequest.setName(REMOTE_EVAL_NAME);
evalRequest.setStream(true);
evalRequest.setExperimentName(experimentName);
// Note: no parent set -> experiment-snapshot path.
EvalRequest.DataSpec dataSpec = new EvalRequest.DataSpec();
EvalRequest.EvalCaseData case1 = new EvalRequest.EvalCaseData();
case1.setInput("apple");
case1.setExpected("fruit");
EvalRequest.EvalCaseData case2 = new EvalRequest.EvalCaseData();
case2.setInput("carrot");
case2.setExpected("vegetable");
dataSpec.setData(List.of(case1, case2));
evalRequest.setData(dataSpec);
String requestBody = JSON_MAPPER.writeValueAsString(evalRequest);
HttpURLConnection conn =
(HttpURLConnection) new URI(TEST_URL + "/eval").toURL().openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("x-bt-auth-token", testHarness.braintrustApiKey());
conn.setRequestProperty("x-bt-project-id", TestHarness.defaultProjectId());
conn.setRequestProperty("x-bt-org-name", TestHarness.defaultOrgName());
conn.setDoOutput(true);
conn.getOutputStream().write(requestBody.getBytes(StandardCharsets.UTF_8));
conn.getOutputStream().flush();
assertEquals(200, conn.getResponseCode());
List<Map<String, String>> events = readSSEEvents(conn);
List<Map<String, String>> progressEvents =
events.stream().filter(e -> "progress".equals(e.get("event"))).toList();
List<Map<String, String>> summaryEvents =
events.stream().filter(e -> "summary".equals(e.get("event"))).toList();
List<Map<String, String>> doneEvents =
events.stream().filter(e -> "done".equals(e.get("event"))).toList();
// Snapshots don't stream per-case progress: only a terminating summary + done.
assertEquals(0, progressEvents.size(), "Snapshot runs should not send progress events");
assertEquals(1, summaryEvents.size(), "Should have 1 summary event");
assertEquals(1, doneEvents.size(), "Should have 1 done event");
// The summary should reference the created experiment (unlike playground runs). The id is
// whatever the backend assigned — derive it from the summary rather than hardcoding.
JsonNode summaryData = JSON_MAPPER.readTree(summaryEvents.get(0).get("data"));
assertEquals(TestHarness.defaultProjectName(), summaryData.get("projectName").asText());
assertTrue(summaryData.get("experimentName").asText().startsWith(experimentName));
assertFalse(
summaryData.get("experimentId").isNull(),
"experiment run should have an experimentId");
assertFalse(
summaryData.get("experimentUrl").isNull(),
"experiment run should have an experimentUrl");
String experimentId = summaryData.get("experimentId").asText();
// Spans should be parented to experiment_id:<id> using the standard Eval span shape.
List<SpanData> allSpans = testHarness.awaitExportedSpans();
String expectedParent = "experiment_id:" + experimentId;
var evalSpans =
allSpans.stream()
.filter(s -> s.getName().equals("eval"))
.filter(
s ->
expectedParent.equals(
s.getAttributes()
.get(
AttributeKey.stringKey(
"braintrust.parent"))))
.toList();
assertEquals(2, evalSpans.size(), "Should have 2 eval spans parented to the experiment");
for (SpanData evalSpan : evalSpans) {
JsonNode spanAttrs =
JSON_MAPPER.readTree(
evalSpan.getAttributes()
.get(AttributeKey.stringKey("braintrust.span_attributes")));
assertEquals("eval", spanAttrs.get("type").asText());
// Standard Eval span shape uses braintrust.expected (not the playground's
// expected_json) and carries no playground-specific "generation" key.
assertFalse(
spanAttrs.has("generation"), "experiment spans should not carry generation");
assertNotNull(
evalSpan.getAttributes().get(AttributeKey.stringKey("braintrust.expected")),
"standard Eval decorator should set braintrust.expected");
}
}
@Test
void testMalformedParent() throws Exception {
// A parent with only one of object_type/object_id set is neither a valid playground run
// nor an experiment snapshot (which sends no parent at all). extractPlaygroundParent
// rejects it with an IllegalArgumentException, which is streamed back as an SSE error
// event.
EvalRequest evalRequest = new EvalRequest();
evalRequest.setName(REMOTE_EVAL_NAME);
evalRequest.setStream(true);
EvalRequest.DataSpec dataSpec = new EvalRequest.DataSpec();
EvalRequest.EvalCaseData case1 = new EvalRequest.EvalCaseData();
case1.setInput("apple");
case1.setExpected("fruit");
dataSpec.setData(List.of(case1));
evalRequest.setData(dataSpec);
// object_type present but object_id missing -> malformed parent.
Map<String, Object> parentSpec = Map.of("object_type", PLAYGROUND_PARENT.type());
evalRequest.setParent(parentSpec);
String requestBody = JSON_MAPPER.writeValueAsString(evalRequest);
HttpURLConnection conn =
(HttpURLConnection) new URI(TEST_URL + "/eval").toURL().openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("x-bt-auth-token", testHarness.braintrustApiKey());
conn.setRequestProperty("x-bt-project-id", TestHarness.defaultProjectId());
conn.setRequestProperty("x-bt-org-name", TestHarness.defaultOrgName());
conn.setDoOutput(true);
conn.getOutputStream().write(requestBody.getBytes(StandardCharsets.UTF_8));
conn.getOutputStream().flush();
// Streaming has already begun, so the failure surfaces as an SSE error event (not an HTTP
// error status).
assertEquals(200, conn.getResponseCode());
List<Map<String, String>> events = readSSEEvents(conn);
List<Map<String, String>> errorEvents =
events.stream().filter(e -> "error".equals(e.get("event"))).toList();
assertEquals(1, errorEvents.size(), "Malformed parent should produce a single error event");
assertTrue(
errorEvents.get(0).get("data").contains("malformed braintrust parent"),
"Error message should mention the malformed parent");
// A malformed parent should abort before any experiment is created or summarized.
assertTrue(
events.stream().noneMatch(e -> "summary".equals(e.get("event"))),
"Malformed parent should not produce a summary event");
}
@Test
void testEvaluatorNotFound() throws Exception {
EvalRequest request = new EvalRequest();
request.setName("non-existent-eval");
EvalRequest.DataSpec dataSpec = new EvalRequest.DataSpec();
EvalRequest.EvalCaseData case1 = new EvalRequest.EvalCaseData();
case1.setInput("test");
dataSpec.setData(List.of(case1));
request.setData(dataSpec);
String requestJson = JSON_MAPPER.writeValueAsString(request);
HttpClient client = HttpClient.newHttpClient();
HttpRequest httpRequest =
HttpRequest.newBuilder()
.uri(URI.create(TEST_URL + "/eval"))
.POST(HttpRequest.BodyPublishers.ofString(requestJson))
.header("Content-Type", "application/json")
.header("x-bt-auth-token", testHarness.braintrustApiKey())
.header("x-bt-project-id", TestHarness.defaultProjectId())
.header("x-bt-org-name", TestHarness.defaultOrgName())
.build();
HttpResponse<String> response =
client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
assertEquals(404, response.statusCode());
assertTrue(response.body().contains("Evaluator not found"));
}
@Test
void testEvalMethodNotAllowed() throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request =
HttpRequest.newBuilder().uri(URI.create(TEST_URL + "/eval")).GET().build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(405, response.statusCode());
}
@Test
void testListEndpoint() throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request =
HttpRequest.newBuilder()
.uri(URI.create(TEST_URL + "/list"))
.GET()
.header("x-bt-auth-token", testHarness.braintrustApiKey())
.header("x-bt-project-id", TestHarness.defaultProjectId())
.header("x-bt-org-name", TestHarness.defaultOrgName())
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(200, response.statusCode());
assertEquals(
"application/json", response.headers().firstValue("Content-Type").orElse(null));
// Parse and validate JSON response
JsonNode root = JSON_MAPPER.readTree(response.body());
// Should have one evaluator
assertTrue(root.has(REMOTE_EVAL_NAME));
JsonNode eval = root.get(REMOTE_EVAL_NAME);
assertFalse(
eval.has("parameters"),
"parameters field must be omitted (not null) when the eval has no parameters");
// Check scores
assertTrue(eval.has("scores"));
JsonNode scores = eval.get("scores");
assertEquals(1, scores.size());
assertEquals("simple_scorer", scores.get(0).get("name").asText());
}
@Test
void testListMethodNotAllowed() throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request =
HttpRequest.newBuilder()
.uri(URI.create(TEST_URL + "/list"))
.POST(HttpRequest.BodyPublishers.noBody())
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(405, response.statusCode());
}
@Test
void testListEndpointWithCors() throws Exception {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request =
HttpRequest.newBuilder()
.uri(URI.create(TEST_URL + "/list"))
.GET()
.header("Origin", "https://www.braintrust.dev")
.header("x-bt-auth-token", testHarness.braintrustApiKey())
.header("x-bt-project-id", TestHarness.defaultProjectId())
.header("x-bt-org-name", TestHarness.defaultOrgName())
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
assertEquals(200, response.statusCode());
assertEquals(
"https://www.braintrust.dev",
response.headers().firstValue("Access-Control-Allow-Origin").orElse(null));
}
@Test
void testTaskErrorHandling() throws Exception {
// Send an eval request with two cases: one good input and one that triggers a task error
EvalRequest evalRequest = new EvalRequest();
evalRequest.setName(TASK_ERROR_EVAL_NAME);
evalRequest.setStream(true);
EvalRequest.DataSpec dataSpec = new EvalRequest.DataSpec();
EvalRequest.EvalCaseData goodCase = new EvalRequest.EvalCaseData();
goodCase.setInput("good-input");
goodCase.setExpected("expected");
EvalRequest.EvalCaseData badCase = new EvalRequest.EvalCaseData();
badCase.setInput("bad-input");
badCase.setExpected("expected");
dataSpec.setData(List.of(goodCase, badCase));
evalRequest.setData(dataSpec);
Map<String, Object> parentSpec =
Map.of(
"object_type", PLAYGROUND_PARENT.type(),
"object_id", PLAYGROUND_PARENT.id(),
"propagated_event",
Map.of("span_attributes", Map.of("generation", "test-gen-err")));
evalRequest.setParent(parentSpec);
String requestBody = JSON_MAPPER.writeValueAsString(evalRequest);
HttpURLConnection conn =
(HttpURLConnection) new URI(TEST_URL + "/eval").toURL().openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("x-bt-auth-token", testHarness.braintrustApiKey());
conn.setRequestProperty("x-bt-project-id", TestHarness.defaultProjectId());
conn.setRequestProperty("x-bt-org-name", TestHarness.defaultOrgName());
conn.setDoOutput(true);
conn.getOutputStream().write(requestBody.getBytes(StandardCharsets.UTF_8));
conn.getOutputStream().flush();
// Read SSE response
assertEquals(200, conn.getResponseCode());
List<Map<String, String>> events = readSSEEvents(conn);
// Should complete with summary + done (eval continues despite the task error)
List<Map<String, String>> summaryEvents =
events.stream().filter(e -> "summary".equals(e.get("event"))).toList();
List<Map<String, String>> doneEvents =
events.stream().filter(e -> "done".equals(e.get("event"))).toList();
assertEquals(1, summaryEvents.size(), "Should have 1 summary event");
assertEquals(1, doneEvents.size(), "Should have 1 done event");
// Both cases should produce a progress event (error case sends progress with null output
// so the Playground can link to the trace)
List<Map<String, String>> progressEvents =
events.stream().filter(e -> "progress".equals(e.get("event"))).toList();
assertEquals(2, progressEvents.size(), "Both cases should send a progress event");
// Verify summary includes the fallback score from scoreForTaskException (default 0.0)
JsonNode summaryData = JSON_MAPPER.readTree(summaryEvents.get(0).get("data"));
assertTrue(summaryData.has("scores"));
JsonNode scores = summaryData.get("scores");
assertTrue(scores.has("exact_match"), "Summary should include exact_match scorer");
// Verify spans
List<SpanData> allSpans = testHarness.awaitExportedSpans();
// Filter eval spans belonging to this test: either has generation tag (success path)
// or has error status with our specific message (error path, since setEvalSpanAttributes
// is not reached when the task throws)
List<SpanData> evalSpans =
allSpans.stream()
.filter(s -> s.getName().equals("eval"))
.filter(
s -> {
var attrs =
s.getAttributes()
.get(
AttributeKey.stringKey(
"braintrust.span_attributes"));
boolean hasGenTag =
attrs != null && attrs.contains("test-gen-err");
boolean isOurError =
s.getStatus().getStatusCode() == StatusCode.ERROR
&& s.getStatus()
.getDescription()
.contains("task failed on bad-input");
return hasGenTag || isOurError;
})
.toList();
assertEquals(2, evalSpans.size(), "Should have 2 eval spans (one per case)");
// Find the errored eval span
var erroredEvalSpan =
evalSpans.stream()
.filter(s -> s.getStatus().getStatusCode() == StatusCode.ERROR)
.findFirst()
.orElseThrow(() -> new AssertionError("expected an errored eval span"));
assertTrue(
erroredEvalSpan.getStatus().getDescription().contains("task failed on bad-input"),
"eval span error should contain the exception message");
// The errored eval span should have output: null
@SuppressWarnings("unchecked")
Map<String, Object> erroredOutputJson =
fromJson(
erroredEvalSpan
.getAttributes()
.get(AttributeKey.stringKey("braintrust.output_json")),
Map.class);
assertNull(erroredOutputJson.get("output"), "errored case output should be null");
// Find the task span for the errored case (should have ERROR status)
var erroredTaskSpan =
allSpans.stream()
.filter(s -> s.getName().equals("task"))
.filter(s -> s.getStatus().getStatusCode() == StatusCode.ERROR)
.filter(
s ->
s.getParentSpanContext()
.getSpanId()
.equals(
erroredEvalSpan
.getSpanContext()
.getSpanId()))
.findFirst()
.orElseThrow(() -> new AssertionError("expected an errored task span"));
assertFalse(
erroredTaskSpan.getEvents().isEmpty(), "task span should have exception events");
assertTrue(
erroredTaskSpan.getEvents().stream().anyMatch(e -> e.getName().equals("exception")),
"task span should have an exception event");
// The errored case should still have a score span (from scoreForTaskException default 0.0)
// The score span is a child of the task span (since the task scope is still active when
// runScoreForTaskException is called from the catch block)
var erroredScoreSpans =
allSpans.stream()
.filter(s -> s.getName().equals("score"))
.filter(
s ->
s.getParentSpanContext()
.getSpanId()
.equals(
erroredTaskSpan
.getSpanContext()
.getSpanId()))
.toList();
assertEquals(1, erroredScoreSpans.size(), "errored case should have a score span");
@SuppressWarnings("unchecked")
Map<String, Object> fallbackScoresJson =
fromJson(
erroredScoreSpans
.get(0)
.getAttributes()
.get(AttributeKey.stringKey("braintrust.scores")),
Map.class);
assertEquals(
0.0,
((Number) fallbackScoresJson.get("exact_match")).doubleValue(),
"scoreForTaskException default should produce 0.0");
// Verify the successful case has a non-error eval span
var successEvalSpan =
evalSpans.stream()
.filter(s -> s.getStatus().getStatusCode() != StatusCode.ERROR)
.findFirst()