everruns-core 0.40.0

Transport-neutral agent execution contracts for Everruns
Documentation
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
//! Event conversion and hierarchy tests.

use super::*;

#[test]
fn test_listener_creation() {
    let listener = BraintrustListener::new(test_config()).unwrap();
    assert_eq!(listener.name(), "BraintrustListener");
}

/// Telemetry init must never panic the host process. `new` returns a
/// `Result` so a misconfigured TLS/proxy environment disables Braintrust
/// instead of aborting the process. With no env config, `from_env` yields
/// `None` (disabled) rather than panicking.
#[test]
fn test_init_never_panics_and_disables_on_missing_config() {
    // Valid config builds successfully (Ok, not a panic).
    assert!(BraintrustListener::new(test_config()).is_ok());

    // `from_env` must not panic; with the API key absent it returns None.
    // Guard against an ambient key in the environment so the test is
    // deterministic in any CI/runtime.
    let key_present = std::env::var("BRAINTRUST_API_KEY").is_ok();
    let listener = BraintrustListener::from_env();
    if !key_present {
        assert!(
            listener.is_none(),
            "from_env should disable Braintrust when no API key is configured"
        );
    }
}

#[test]
fn test_event_types() {
    let listener = BraintrustListener::new(test_config()).unwrap();
    let types = listener.event_types().unwrap();
    // 16 event types: 4 turn lifecycle + 4 atom lifecycle + 2 thinking + 1 llm + 2 tool + 3 session lifecycle
    assert_eq!(types.len(), 16);
    // Turn lifecycle
    assert!(types.contains(&TURN_STARTED));
    assert!(types.contains(&TURN_COMPLETED));
    assert!(types.contains(&TURN_FAILED));
    assert!(types.contains(&TURN_CANCELLED));
    // Atom lifecycle
    assert!(types.contains(&REASON_STARTED));
    assert!(types.contains(&REASON_COMPLETED));
    assert!(types.contains(&ACT_STARTED));
    assert!(types.contains(&ACT_COMPLETED));
    // Extended thinking
    assert!(types.contains(&REASON_THINKING_STARTED));
    assert!(types.contains(&REASON_THINKING_COMPLETED));
    // LLM
    assert!(types.contains(&LLM_GENERATION));
    // Tool
    assert!(types.contains(&TOOL_STARTED));
    assert!(types.contains(&TOOL_COMPLETED));
    // Session lifecycle
    assert!(types.contains(&SESSION_STARTED));
    assert!(types.contains(&SESSION_ACTIVATED));
    assert!(types.contains(&SESSION_IDLED));
}

#[test]
fn test_convert_turn_started() {
    let listener = BraintrustListener::new(test_config()).unwrap();

    let turn_id = TurnId::new();
    let message_id = MessageId::new();

    let data = TurnStartedData {
        turn_id,
        input_message_id: message_id,
        input_content: Some("Hello, how are you?".to_string()),
        agent_id: None,
        agent_name: None,
        agent_description: None,
    };

    let event = Event::new(
        SessionId::new(),
        EventContext::empty(),
        EventData::TurnStarted(data.clone()),
    );

    let bt_event = listener.convert_turn_started(&event, &data);

    assert_eq!(bt_event.id, turn_id.to_string());
    assert_eq!(bt_event.span_attributes.span_type, "task");
    assert_eq!(bt_event.span_attributes.name, "agent turn");
    // Root span self-references for proper trace correlation
    assert_eq!(bt_event.span_id, Some(turn_id.to_string()));
    assert_eq!(bt_event.root_span_id, Some(turn_id.to_string()));
    assert!(bt_event.span_parents.is_none()); // Root has no parents
}

#[test]
fn test_convert_llm_generation_with_parent() {
    let listener = BraintrustListener::new(test_config()).unwrap();

    let turn_id = TurnId::new();

    let data = LlmGenerationData {
        messages: vec![
            RuntimeMessage::user("Hello"),
            RuntimeMessage::assistant("Hi there!"),
        ],
        tools: vec![],
        output: LlmGenerationOutput {
            text: Some("Hi there!".to_string()),
            tool_calls: vec![],
        },
        metadata: LlmGenerationMetadata {
            model: "gpt-4".to_string(),
            provider: Some("openai".to_string()),
            response_model: None,
            usage: Some(TokenUsage {
                input_tokens: 10,
                output_tokens: 5,
                cache_read_tokens: None,
                cache_creation_tokens: None,
                actual_cost_usd: None,
                estimated_cost_usd: None,
                effective_cost_usd: None,
            }),
            duration_ms: Some(100),
            time_to_first_token_ms: Some(25),
            success: true,
            error: None,
            finish_reasons: Some(vec!["stop".to_string()]),
            response_id: Some("resp_123".to_string()),
            retry: None,
            compaction: None,
            request_options: None,
            cost_components: Vec::new(),
        },
    };

    let mut context = EventContext::empty();
    context.turn_id = Some(turn_id);

    let event = Event::new(
        SessionId::new(),
        context,
        EventData::LlmGeneration(data.clone()),
    );

    let bt_event = listener.convert_llm_generation(&event, &data);

    assert_eq!(bt_event.span_attributes.name, "chat gpt-4");
    assert_eq!(bt_event.span_attributes.span_type, "llm");
    assert_eq!(bt_event.root_span_id, Some(turn_id.to_string()));
    assert_eq!(bt_event.span_parents, Some(vec![turn_id.to_string()]));
}

#[test]
fn test_convert_turn_completed_with_usage() {
    let listener = BraintrustListener::new(test_config()).unwrap();

    let turn_id = TurnId::new();

    let data = TurnCompletedData {
        turn_id,
        iterations: 3,
        duration_ms: Some(5000),
        usage: Some(TokenUsage {
            input_tokens: 100,
            output_tokens: 50,
            cache_read_tokens: None,
            cache_creation_tokens: None,
            actual_cost_usd: None,
            estimated_cost_usd: None,
            effective_cost_usd: None,
        }),
        input_content: Some("Hello, how are you?".to_string()),
        final_message_id: None,
        final_answer_preview: None,
        time_to_first_token_ms: None,
        tool_call_count: None,
        llm_call_count: None,
        status: None,
    };

    let event = Event::new(
        SessionId::new(),
        EventContext::empty(),
        EventData::TurnCompleted(data.clone()),
    );

    let bt_event = listener.convert_turn_completed(&event, &data);

    assert_eq!(bt_event.id, turn_id.to_string());
    assert!(bt_event.metrics.is_some());
    let metrics = bt_event.metrics.unwrap();
    assert_eq!(metrics.prompt_tokens, Some(100));
    assert_eq!(metrics.completion_tokens, Some(50));
    assert_eq!(metrics.tokens, Some(150));
}

// =============================================================================
// Event Hierarchy Tests
// =============================================================================
//
// These tests verify the span ID relationships required for proper trace hierarchy:
//
// agent turn (root)
// ├── reason (parent: turn)
// │   └── llm.generation (parent: reason)
// ├── act (parent: turn)
// │   └── tool.call (parent: act)
// └── ...

#[test]
fn test_turn_events_are_self_referencing_root_spans() {
    let listener = BraintrustListener::new(test_config()).unwrap();
    let turn_id = TurnId::new();
    let input_message_id = MessageId::new();

    // Test turn.started
    let started_data = TurnStartedData {
        turn_id,
        input_message_id,
        input_content: Some("Test input content".to_string()),
        agent_id: None,
        agent_name: None,
        agent_description: None,
    };
    let started_event = Event::new(
        SessionId::new(),
        EventContext::empty(),
        EventData::TurnStarted(started_data.clone()),
    );
    let bt_started = listener.convert_turn_started(&started_event, &started_data);

    // Root span: span_id = root_span_id = turn_id, no parents
    assert_eq!(
        bt_started.id,
        turn_id.to_string(),
        "turn.started id should be turn_id"
    );
    assert_eq!(
        bt_started.span_id,
        Some(turn_id.to_string()),
        "turn.started span_id should be turn_id"
    );
    assert_eq!(
        bt_started.root_span_id,
        Some(turn_id.to_string()),
        "turn.started root_span_id should be turn_id"
    );
    assert!(
        bt_started.span_parents.is_none(),
        "turn.started should have no parents (root span)"
    );

    // Test turn.completed uses same IDs (for merging)
    let completed_data = TurnCompletedData {
        turn_id,
        iterations: 1,
        duration_ms: Some(1000),
        usage: None,
        input_content: Some("Test input content".to_string()),
        final_message_id: None,
        final_answer_preview: None,
        time_to_first_token_ms: None,
        tool_call_count: None,
        llm_call_count: None,
        status: None,
    };
    let completed_event = Event::new(
        SessionId::new(),
        EventContext::empty(),
        EventData::TurnCompleted(completed_data.clone()),
    );
    let bt_completed = listener.convert_turn_completed(&completed_event, &completed_data);

    // Same IDs as started for proper merging
    assert_eq!(
        bt_completed.id,
        turn_id.to_string(),
        "turn.completed id should match turn.started"
    );
    assert_eq!(
        bt_completed.span_id,
        Some(turn_id.to_string()),
        "turn.completed span_id should match turn.started"
    );
    assert_eq!(bt_completed.root_span_id, Some(turn_id.to_string()));
}

#[test]
fn test_reason_events_have_turn_as_parent() {
    let listener = BraintrustListener::new(test_config()).unwrap();
    let turn_id = TurnId::new();
    let reason_span_id = Uuid::now_v7().to_string();

    // Create event context with proper span linkage
    let mut context = EventContext::empty();
    context.turn_id = Some(turn_id);
    context.trace_id = Some(turn_id.to_string());
    context.span_id = Some(reason_span_id.clone());
    context.parent_span_id = Some(turn_id.to_string());

    let data = ReasonStartedData {
        harness_id: HarnessId::from_seed(1),
        agent_id: Some(AgentId::new()),
        metadata: None,
    };
    let event = Event::new(
        SessionId::new(),
        context,
        EventData::ReasonStarted(data.clone()),
    );

    let bt_event = listener.convert_reason_started(&event, &data);

    // Reason should be child of turn
    assert_eq!(
        bt_event.span_id,
        Some(reason_span_id.clone()),
        "reason span_id should be the reason's span"
    );
    assert_eq!(
        bt_event.root_span_id,
        Some(turn_id.to_string()),
        "reason root_span_id should be turn_id"
    );
    assert_eq!(
        bt_event.span_parents,
        Some(vec![turn_id.to_string()]),
        "reason parent should be turn"
    );
}

#[test]
fn test_llm_generation_with_span_context_has_reason_as_parent() {
    let listener = BraintrustListener::new(test_config()).unwrap();
    let turn_id = TurnId::new();
    let reason_span_id = Uuid::now_v7().to_string();
    let llm_span_id = Uuid::now_v7().to_string();

    // LLM generation event with span context (parent is reason)
    let mut context = EventContext::empty();
    context.turn_id = Some(turn_id);
    context.trace_id = Some(turn_id.to_string());
    context.span_id = Some(llm_span_id.clone());
    context.parent_span_id = Some(reason_span_id.clone());

    let data = LlmGenerationData {
        messages: vec![RuntimeMessage::user("Hello")],
        tools: vec![],
        output: LlmGenerationOutput {
            text: Some("Hi!".to_string()),
            tool_calls: vec![],
        },
        metadata: bare_generation_metadata("gpt-4"),
    };
    let event = Event::new(
        SessionId::new(),
        context,
        EventData::LlmGeneration(data.clone()),
    );

    let bt_event = listener.convert_llm_generation(&event, &data);

    // LLM should be child of reason
    assert_eq!(
        bt_event.span_id,
        Some(llm_span_id),
        "llm span_id should be its own span"
    );
    assert_eq!(
        bt_event.root_span_id,
        Some(turn_id.to_string()),
        "llm root_span_id should be turn_id"
    );
    assert_eq!(
        bt_event.span_parents,
        Some(vec![reason_span_id]),
        "llm parent should be reason span"
    );
}

#[test]
fn test_act_events_have_turn_as_parent() {
    use crate::events::ToolCallSummary;

    let listener = BraintrustListener::new(test_config()).unwrap();
    let turn_id = TurnId::new();
    let act_span_id = Uuid::now_v7().to_string();

    // Create event context with proper span linkage
    let mut context = EventContext::empty();
    context.turn_id = Some(turn_id);
    context.trace_id = Some(turn_id.to_string());
    context.span_id = Some(act_span_id.clone());
    context.parent_span_id = Some(turn_id.to_string());

    let data = ActStartedData {
        tool_calls: vec![
            ToolCallSummary {
                id: "call_1".to_string(),
                name: "search".to_string(),
                display_name: None,
                narration: None,
                completed_narration: None,
            },
            ToolCallSummary {
                id: "call_2".to_string(),
                name: "fetch".to_string(),
                display_name: None,
                narration: None,
                completed_narration: None,
            },
        ],
        headline: None,
    };
    let event = Event::new(
        SessionId::new(),
        context,
        EventData::ActStarted(data.clone()),
    );

    let bt_event = listener.convert_act_started(&event, &data);

    // Act should be child of turn
    assert_eq!(
        bt_event.span_id,
        Some(act_span_id.clone()),
        "act span_id should be the act's span"
    );
    assert_eq!(
        bt_event.root_span_id,
        Some(turn_id.to_string()),
        "act root_span_id should be turn_id"
    );
    assert_eq!(
        bt_event.span_parents,
        Some(vec![turn_id.to_string()]),
        "act parent should be turn"
    );
}

#[test]
fn test_tool_call_events_have_act_as_parent() {
    let listener = BraintrustListener::new(test_config()).unwrap();
    let turn_id = TurnId::new();
    let act_span_id = Uuid::now_v7().to_string();
    let tool_span_id = Uuid::now_v7().to_string();

    // Tool call event with span context (parent is act)
    let mut context = EventContext::empty();
    context.turn_id = Some(turn_id);
    context.trace_id = Some(turn_id.to_string());
    context.span_id = Some(tool_span_id.clone());
    context.parent_span_id = Some(act_span_id.clone());

    let data = ToolCompletedData {
        tool_call_id: "call_123".to_string(),
        tool_name: "search".to_string(),
        tool_call_fingerprint: None,
        tool_result_fingerprint: None,
        display_name: None,
        success: true,
        status: "success".to_string(),
        result: None,
        error: None,
        duration_ms: Some(200),
        capability_id: None,
        capability_name: None,
        narration: None,
    };
    let event = Event::new(
        SessionId::new(),
        context,
        EventData::ToolCompleted(data.clone()),
    );

    let bt_event = listener.convert_tool_call_completed(&event, &data);

    // Tool should be child of act
    assert_eq!(
        bt_event.span_id,
        Some(tool_span_id),
        "tool span_id should be its own span"
    );
    assert_eq!(
        bt_event.root_span_id,
        Some(turn_id.to_string()),
        "tool root_span_id should be turn_id"
    );
    assert_eq!(
        bt_event.span_parents,
        Some(vec![act_span_id]),
        "tool parent should be act span"
    );
}

#[test]
fn test_started_completed_pairs_share_span_id() {
    let listener = BraintrustListener::new(test_config()).unwrap();
    let turn_id = TurnId::new();
    let shared_span_id = Uuid::now_v7().to_string();

    // Both started and completed should use the same span_id
    let mut context = EventContext::empty();
    context.turn_id = Some(turn_id);
    context.trace_id = Some(turn_id.to_string());
    context.span_id = Some(shared_span_id.clone());
    context.parent_span_id = Some(turn_id.to_string());

    // reason.started
    let started_data = ReasonStartedData {
        harness_id: HarnessId::from_seed(1),
        agent_id: Some(AgentId::new()),
        metadata: None,
    };
    let started_event = Event::new(
        SessionId::new(),
        context.clone(),
        EventData::ReasonStarted(started_data.clone()),
    );
    let bt_started = listener.convert_reason_started(&started_event, &started_data);

    // reason.completed with same span context
    let completed_data = ReasonCompletedData {
        success: true,
        text_preview: Some("Hello".to_string()),
        has_tool_calls: false,
        tool_call_count: 0,
        error: None,
        duration_ms: Some(1500),
        usage: None,
    };
    let completed_event = Event::new(
        SessionId::new(),
        context.clone(),
        EventData::ReasonCompleted(completed_data.clone()),
    );
    let bt_completed = listener.convert_reason_completed(&completed_event, &completed_data);

    // Both should have same span_id for Braintrust to merge them
    assert_eq!(
        bt_started.span_id, bt_completed.span_id,
        "started and completed should share span_id"
    );
    assert_eq!(
        bt_started.id, bt_completed.id,
        "started and completed should share log id"
    );
}