weavegraph 0.7.0

Graph-driven, concurrent agent workflow framework with versioned state, deterministic barrier merges, and rich diagnostics.
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
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
use chrono::{TimeZone, Utc};
use serde_json::json;

use weavegraph::channels::errors::*;
use weavegraph::channels::{Channel, ErrorsChannel};
use weavegraph::types::ChannelType;

// WeaveError tests

#[test]
fn ladder_error_msg_and_chain() {
    let base = WeaveError::msg("root cause").with_details(json!({"k":"v"}));
    let wrapped = WeaveError::msg("top").with_cause(base.clone());

    assert_eq!(base.message, "root cause");
    assert_eq!(wrapped.message, "top");
    assert!(wrapped.cause.is_some());
    assert_eq!(wrapped.cause.as_ref().unwrap().message, base.message);
    assert_eq!(base.details, json!({"k":"v"}));
}

#[test]
fn ladder_error_serde_roundtrip() {
    let err = WeaveError::msg("boom")
        .with_details(json!({"code": 500}))
        .with_cause(WeaveError::msg("inner"));

    let ser = serde_json::to_string(&err).expect("serialize");
    let de: WeaveError = serde_json::from_str(&ser).expect("deserialize");
    assert_eq!(de, err);
}

// ErrorEvent tests

#[test]
fn error_event_defaults_and_roundtrip() {
    let when = Utc.with_ymd_and_hms(2024, 1, 2, 3, 4, 5).unwrap();
    let ev = ErrorEvent {
        when,
        scope: ErrorScope::App,
        error: WeaveError::msg("oops"),
        tags: vec!["t1".into(), "t2".into()],
        context: json!({"info": true}),
    };

    let ser = serde_json::to_string(&ev).unwrap();
    let de: ErrorEvent = serde_json::from_str(&ser).unwrap();
    assert_eq!(de, ev);
}

#[test]
fn error_event_defaults_are_empty_when_missing() {
    let when = Utc.with_ymd_and_hms(2024, 5, 6, 7, 8, 9).unwrap();
    let v = json!({
        "when": when,
        "scope": {"scope": "app"},
        "error": {"message":"x"}
    });
    let de: ErrorEvent = serde_json::from_value(v).unwrap();
    assert!(de.tags.is_empty());
    assert!(de.context.is_null());
}

// ErrorEvent constructor tests

#[test]
fn error_event_node_constructor() {
    let err = ErrorEvent::node("Parser", 42, WeaveError::msg("parse failed"));

    assert!(matches!(err.scope, ErrorScope::Node { .. }));
    if let ErrorScope::Node { kind, step } = err.scope {
        assert_eq!(kind, "Parser");
        assert_eq!(step, 42);
    }
    assert_eq!(err.error.message, "parse failed");
    assert!(err.tags.is_empty());
    assert!(err.context.is_null());
}

#[test]
fn error_event_scheduler_constructor() {
    let err = ErrorEvent::scheduler(10, WeaveError::msg("scheduling conflict"));

    assert!(matches!(err.scope, ErrorScope::Scheduler { .. }));
    if let ErrorScope::Scheduler { step } = err.scope {
        assert_eq!(step, 10);
    }
    assert_eq!(err.error.message, "scheduling conflict");
    assert!(err.tags.is_empty());
    assert!(err.context.is_null());
}

#[test]
fn error_event_runner_constructor() {
    let err = ErrorEvent::runner("session-abc", 99, WeaveError::msg("runtime error"));

    assert!(matches!(err.scope, ErrorScope::Runner { .. }));
    if let ErrorScope::Runner { session, step } = err.scope {
        assert_eq!(session, "session-abc");
        assert_eq!(step, 99);
    }
    assert_eq!(err.error.message, "runtime error");
    assert!(err.tags.is_empty());
    assert!(err.context.is_null());
}

#[test]
fn error_event_app_constructor() {
    let err = ErrorEvent::app(WeaveError::msg("startup failed"));

    assert!(matches!(err.scope, ErrorScope::App));
    assert_eq!(err.error.message, "startup failed");
    assert!(err.tags.is_empty());
    assert!(err.context.is_null());
}

#[test]
fn error_event_with_tag_builder() {
    let err = ErrorEvent::node("Validator", 1, WeaveError::msg("invalid")).with_tag("validation");

    assert_eq!(err.tags, vec!["validation"]);
}

#[test]
fn error_event_with_multiple_tags_chained() {
    let err = ErrorEvent::scheduler(5, WeaveError::msg("error"))
        .with_tag("critical")
        .with_tag("retry");

    assert_eq!(err.tags, vec!["critical", "retry"]);
}

#[test]
fn error_event_with_tags_builder() {
    let err = ErrorEvent::runner("sess-1", 3, WeaveError::msg("failed"))
        .with_tags(vec!["urgent".to_string(), "logged".to_string()]);

    assert_eq!(err.tags, vec!["urgent", "logged"]);
}

#[test]
fn error_event_with_context_builder() {
    let err = ErrorEvent::app(WeaveError::msg("config error"))
        .with_context(json!({"config_file": "/etc/app.conf", "line": 42}));

    assert_eq!(err.context["config_file"], "/etc/app.conf");
    assert_eq!(err.context["line"], 42);
}

#[test]
fn error_event_full_builder_chain() {
    let err = ErrorEvent::node(
        "Analyzer",
        7,
        WeaveError::msg("analysis failed")
            .with_cause(WeaveError::msg("missing data"))
            .with_details(json!({"field": "input"})),
    )
    .with_tag("retryable")
    .with_tag("logged")
    .with_context(json!({"attempt": 3, "max_attempts": 5}));

    // Verify scope
    if let ErrorScope::Node { kind, step } = err.scope {
        assert_eq!(kind, "Analyzer");
        assert_eq!(step, 7);
    } else {
        panic!("Expected Node scope");
    }

    // Verify error chain
    assert_eq!(err.error.message, "analysis failed");
    assert!(err.error.cause.is_some());
    assert_eq!(err.error.cause.as_ref().unwrap().message, "missing data");
    assert_eq!(err.error.details["field"], "input");

    // Verify tags
    assert_eq!(err.tags, vec!["retryable", "logged"]);

    // Verify context
    assert_eq!(err.context["attempt"], 3);
    assert_eq!(err.context["max_attempts"], 5);
}

#[test]
fn error_event_string_into_conversions() {
    // Test that Into<String> works for both &str and String
    let from_str = ErrorEvent::node("literal", 1, WeaveError::msg("test"));
    let from_string = ErrorEvent::node(String::from("owned"), 1, WeaveError::msg("test"));

    if let ErrorScope::Node { kind, .. } = from_str.scope {
        assert_eq!(kind, "literal");
    }

    if let ErrorScope::Node { kind, .. } = from_string.scope {
        assert_eq!(kind, "owned");
    }

    let runner_str = ErrorEvent::runner("session", 1, WeaveError::msg("test"));
    let runner_string = ErrorEvent::runner(String::from("session_id"), 1, WeaveError::msg("test"));

    if let ErrorScope::Runner { session, .. } = runner_str.scope {
        assert_eq!(session, "session");
    }

    if let ErrorScope::Runner { session, .. } = runner_string.scope {
        assert_eq!(session, "session_id");
    }
}

// Serialization tests

#[test]
fn all_scope_variants_produce_correct_event_json() {
    let when = Utc.with_ymd_and_hms(2024, 1, 1, 12, 0, 0).unwrap();

    // Node scope
    let mut node_event = ErrorEvent::node("TestNode", 42, WeaveError::msg("node error"))
        .with_tag("test")
        .with_context(json!({"node_id": 42}));
    node_event.when = when;

    let node_json = serde_json::to_value(&node_event).unwrap();
    assert_eq!(node_json["scope"]["scope"], "node");
    assert_eq!(node_json["scope"]["kind"], "TestNode");
    assert_eq!(node_json["scope"]["step"], 42);
    assert_eq!(node_json["error"]["message"], "node error");
    assert_eq!(node_json["tags"], json!(["test"]));
    assert_eq!(node_json["context"]["node_id"], 42);

    let node_deserialized: ErrorEvent = serde_json::from_value(node_json).unwrap();
    assert_eq!(node_deserialized.scope, node_event.scope);
    assert_eq!(node_deserialized.error, node_event.error);
    assert_eq!(node_deserialized.tags, node_event.tags);
    assert_eq!(node_deserialized.context, node_event.context);

    // Scheduler scope
    let mut scheduler_event = ErrorEvent::scheduler(10, WeaveError::msg("scheduler error"));
    scheduler_event.when = when;

    let scheduler_json = serde_json::to_value(&scheduler_event).unwrap();
    assert_eq!(scheduler_json["scope"]["scope"], "scheduler");
    assert_eq!(scheduler_json["scope"]["step"], 10);

    let scheduler_deserialized: ErrorEvent = serde_json::from_value(scheduler_json).unwrap();
    assert_eq!(scheduler_deserialized.scope, scheduler_event.scope);

    // Runner scope
    let mut runner_event = ErrorEvent::runner("session-123", 5, WeaveError::msg("runner error"));
    runner_event.when = when;

    let runner_json = serde_json::to_value(&runner_event).unwrap();
    assert_eq!(runner_json["scope"]["scope"], "runner");
    assert_eq!(runner_json["scope"]["session"], "session-123");
    assert_eq!(runner_json["scope"]["step"], 5);

    let runner_deserialized: ErrorEvent = serde_json::from_value(runner_json).unwrap();
    assert_eq!(runner_deserialized.scope, runner_event.scope);

    // App scope
    let mut app_event = ErrorEvent::app(WeaveError::msg("app error"));
    app_event.when = when;

    let app_json = serde_json::to_value(&app_event).unwrap();
    assert_eq!(app_json["scope"]["scope"], "app");

    let app_deserialized: ErrorEvent = serde_json::from_value(app_json).unwrap();
    assert_eq!(app_deserialized.scope, app_event.scope);
}

#[test]
fn weave_error_nested_cause_chain_serializes() {
    let simple_error = WeaveError::msg("simple error");
    let simple_json = serde_json::to_value(&simple_error).unwrap();
    assert_eq!(simple_json["message"], "simple error");
    assert!(simple_json["cause"].is_null());
    assert!(simple_json["details"].is_null());

    let error_with_details =
        WeaveError::msg("error with details").with_details(json!({"code": 500, "retry": true}));
    let details_json = serde_json::to_value(&error_with_details).unwrap();
    assert_eq!(details_json["message"], "error with details");
    assert_eq!(details_json["details"]["code"], 500);
    assert_eq!(details_json["details"]["retry"], true);
    let details_deserialized: WeaveError = serde_json::from_value(details_json).unwrap();
    assert_eq!(details_deserialized, error_with_details);

    let error_with_cause =
        WeaveError::msg("outer error").with_cause(WeaveError::msg("inner error"));
    let cause_json = serde_json::to_value(&error_with_cause).unwrap();
    assert_eq!(cause_json["message"], "outer error");
    assert_eq!(cause_json["cause"]["message"], "inner error");
    assert!(cause_json["cause"]["cause"].is_null());
    let cause_deserialized: WeaveError = serde_json::from_value(cause_json).unwrap();
    assert_eq!(cause_deserialized, error_with_cause);

    let deep_error = WeaveError::msg("level 1").with_cause(
        WeaveError::msg("level 2")
            .with_cause(WeaveError::msg("level 3").with_details(json!({"deep": true}))),
    );
    let deep_json = serde_json::to_value(&deep_error).unwrap();
    assert_eq!(deep_json["message"], "level 1");
    assert_eq!(deep_json["cause"]["message"], "level 2");
    assert_eq!(deep_json["cause"]["cause"]["message"], "level 3");
    assert_eq!(deep_json["cause"]["cause"]["details"]["deep"], true);
    let deep_deserialized: WeaveError = serde_json::from_value(deep_json).unwrap();
    assert_eq!(deep_deserialized, deep_error);
}

#[test]
fn error_event_with_complex_nested_fields_round_trips() {
    let original = ErrorEvent::node(
        "ComplexNode",
        99,
        WeaveError::msg("complex error")
            .with_cause(WeaveError::msg("root cause"))
            .with_details(json!({"severity": "high"})),
    )
    .with_tags(vec!["critical".to_string(), "network".to_string()])
    .with_context(json!({
        "user_id": 12345,
        "request_id": "req-abc-123",
        "endpoint": "/api/process",
        "metadata": {
            "version": "1.2.3",
            "environment": "production"
        }
    }));

    // Serialize to JSON
    let json_str = serde_json::to_string(&original).unwrap();
    let json_value: serde_json::Value = serde_json::from_str(&json_str).unwrap();

    assert_eq!(json_value["scope"]["scope"], "node");
    assert_eq!(json_value["scope"]["kind"], "ComplexNode");
    assert_eq!(json_value["scope"]["step"], 99);
    assert_eq!(json_value["error"]["message"], "complex error");
    assert_eq!(json_value["error"]["cause"]["message"], "root cause");
    assert_eq!(json_value["error"]["details"]["severity"], "high");
    assert_eq!(json_value["tags"], json!(["critical", "network"]));
    assert_eq!(json_value["context"]["user_id"], 12345);
    assert_eq!(json_value["context"]["request_id"], "req-abc-123");
    assert_eq!(json_value["context"]["endpoint"], "/api/process");
    assert_eq!(json_value["context"]["metadata"]["version"], "1.2.3");
    assert_eq!(
        json_value["context"]["metadata"]["environment"],
        "production"
    );

    let deserialized: ErrorEvent = serde_json::from_str(&json_str).unwrap();
    assert_eq!(deserialized.scope, original.scope);
    assert_eq!(deserialized.error, original.error);
    assert_eq!(deserialized.tags, original.tags);
    assert_eq!(deserialized.context, original.context);
}

#[test]
fn error_event_json_has_required_top_level_fields() {
    // Regression guard: if the schema changes unexpectedly, this test will catch it.
    let event = ErrorEvent::node("SchemaTest", 1, WeaveError::msg("test"))
        .with_tag("regression")
        .with_context(json!({"test": true}));

    let json = serde_json::to_value(&event).unwrap();

    assert!(json.get("when").is_some(), "Missing 'when' field");
    assert!(json.get("scope").is_some(), "Missing 'scope' field");
    assert!(json.get("error").is_some(), "Missing 'error' field");
    assert!(json.get("tags").is_some(), "Missing 'tags' field");
    assert!(json.get("context").is_some(), "Missing 'context' field");

    let scope = &json["scope"];
    assert!(
        scope.get("scope").is_some(),
        "Missing 'scope.scope' discriminator"
    );

    let error = &json["error"];
    assert!(
        error.get("message").is_some(),
        "Missing 'error.message' field"
    );
    // Note: cause and details may be omitted when null due to skip_serializing_if

    assert!(json["tags"].is_array(), "tags should be an array");
    assert!(
        json["context"].is_object() || json["context"].is_null(),
        "context should be object or null"
    );
}

#[test]
fn all_error_scope_variants_serialize_and_round_trip() {
    let test_cases = vec![
        (
            ErrorScope::Node {
                kind: "MyNode".to_string(),
                step: 1,
            },
            json!({"scope": "node", "kind": "MyNode", "step": 1}),
        ),
        (
            ErrorScope::Scheduler { step: 5 },
            json!({"scope": "scheduler", "step": 5}),
        ),
        (
            ErrorScope::Runner {
                session: "sess-x".to_string(),
                step: 10,
            },
            json!({"scope": "runner", "session": "sess-x", "step": 10}),
        ),
        (ErrorScope::App, json!({"scope": "app"})),
    ];

    for (scope, expected_json) in test_cases {
        let serialized = serde_json::to_value(&scope).unwrap();
        assert_eq!(
            serialized, expected_json,
            "Serialization mismatch for {:?}",
            scope
        );
        let deserialized: ErrorScope = serde_json::from_value(serialized).unwrap();
        assert_eq!(deserialized, scope, "Round-trip failed for {:?}", scope);
    }
}

// pretty_print tests

#[test]
fn pretty_print_renders_usefully() {
    let when = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();
    let mut event = ErrorEvent::runner(
        "sess-1",
        3,
        WeaveError::msg("failed").with_cause(WeaveError::msg("io")),
    )
    .with_tag("urgent")
    .with_context(json!({"path":"/tmp/x"}));

    // Override timestamp for test consistency
    event.when = when;

    let events = vec![event];

    let out = pretty_print(&events);
    assert!(out.contains("failed"));
    assert!(out.contains("cause: io"));
    assert!(out.contains("Runner"));
    assert!(out.contains("sess-1"));
    assert!(out.contains("/tmp/x"));
}

// ErrorsChannel tests

#[test]
fn errors_channel_basics() {
    let mut ch = ErrorsChannel::default();
    assert_eq!(ch.get_channel_type(), ChannelType::Error);
    assert!(ch.persistent());
    assert_eq!(ch.version(), 1);
    assert_eq!(ch.len(), 0);
    assert!(ch.is_empty());

    let when = Utc::now();

    let err1 = ErrorEvent::scheduler(1, WeaveError::msg("first"));
    let mut err1_with_time = err1;
    err1_with_time.when = when;
    ch.get_mut().push(err1_with_time);

    let err2 = ErrorEvent::node("Start", 2, WeaveError::msg("second"))
        .with_tag("retryable")
        .with_context(json!({"try":2}));
    let mut err2_with_time = err2;
    err2_with_time.when = when;
    ch.get_mut().push(err2_with_time);

    assert_eq!(ch.len(), 2);
    assert!(!ch.is_empty());

    let snap = ch.snapshot();
    assert_eq!(snap.len(), 2);
    assert_eq!(snap[0].error.message, "first");
    assert_eq!(snap[1].tags, vec!["retryable"]);

    ch.set_version(5);
    assert_eq!(ch.version(), 5);
}

#[test]
fn errors_channel_new_constructor() {
    let when = Utc::now();
    let mut e = ErrorEvent::app(WeaveError::msg("boom"));
    e.when = when;

    let ch = ErrorsChannel::new(vec![e.clone()], 7);
    assert_eq!(ch.version(), 7);
    assert_eq!(ch.snapshot(), vec![e]);
}

#[test]
fn pretty_print_formats_app_event_with_context() {
    let when = Utc.with_ymd_and_hms(2024, 2, 2, 2, 2, 2).unwrap();
    let mut event = ErrorEvent::app(WeaveError::msg("display"))
        .with_tag("cli")
        .with_context(json!({}));
    event.when = when;

    let out = pretty_print(&[event]);
    assert!(out.contains("display"));
}

#[test]
fn pretty_print_with_mode_colored_includes_ansi_codes() {
    use weavegraph::telemetry::FormatterMode;

    let event =
        ErrorEvent::node("parser", 1, WeaveError::msg("Parse failed")).with_tag("validation");
    let events = vec![event];

    let output = pretty_print_with_mode(&events, FormatterMode::Colored);

    // Should contain ANSI escape codes
    assert!(
        output.contains('\x1b'),
        "Colored mode should include ANSI escape codes"
    );
    assert!(
        output.contains("Parse failed"),
        "Should include error message"
    );
    assert!(output.contains("validation"), "Should include tags");
}

#[test]
fn pretty_print_with_mode_plain_excludes_ansi_codes() {
    use weavegraph::telemetry::FormatterMode;

    let nested_error =
        WeaveError::msg("Top level error").with_cause(WeaveError::msg("Nested cause"));
    let event = ErrorEvent::scheduler(5, nested_error)
        .with_tag("critical")
        .with_context(json!({"attempt": 3}));
    let events = vec![event];

    let output = pretty_print_with_mode(&events, FormatterMode::Plain);

    // Should NOT contain any ANSI escape codes
    assert!(
        !output.contains('\x1b'),
        "Plain mode should not include ANSI escape codes"
    );

    // Should still contain all content
    assert!(
        output.contains("Top level error"),
        "Should include root error"
    );
    assert!(
        output.contains("Nested cause"),
        "Should include nested cause"
    );
    assert!(output.contains("critical"), "Should include tags");
    assert!(output.contains("attempt"), "Should include context");
}

#[test]
fn pretty_print_uses_auto_mode_by_default() {
    use weavegraph::telemetry::FormatterMode;

    let event = ErrorEvent::app(WeaveError::msg("Test error"));
    let events = vec![event.clone()];

    let auto_output = pretty_print(&events);
    let explicit_auto_output = pretty_print_with_mode(&events, FormatterMode::Auto);

    // Both should produce identical output
    assert_eq!(
        auto_output, explicit_auto_output,
        "pretty_print should be equivalent to pretty_print_with_mode(Auto)"
    );
}