juncture-core 0.2.0

Core types and traits for Juncture state machine framework
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
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
use std::backtrace::Backtrace;

/// Core error kind for Juncture
#[derive(Debug)]
pub(crate) enum ErrorKind {
    Graph(String),
    Execution(String),
    Checkpoint(String),
    Interrupt(String),
    Interrupted {
        index: usize,
    },
    Subgraph(String),
    InvalidUpdate(String),
    EmptyChannel,
    EmptyInput,
    TaskNotFound(String),
    Timeout(String),
    RecursionLimit {
        step: usize,
        limit: usize,
    },
    Cancelled,
    MultipleWriters {
        field_index: usize,
        writers: Vec<String>,
    },
    TaskPanicked(String),
    NodeTimeout(NodeTimeoutError),
    ParentCommand(String),
}

/// Error code categorizing the error type
///
/// Mirrors `ErrorKind` but is public, enabling callers to match on
/// error categories without accessing private implementation details.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ErrorCode {
    /// Graph construction error
    Graph,
    /// Graph execution error
    Execution,
    /// Checkpoint persistence error
    Checkpoint,
    /// Human-in-the-loop interrupt signal
    Interrupt,
    /// Human-in-the-loop interrupted execution
    Interrupted,
    /// Subgraph error
    Subgraph,
    /// Invalid state update
    InvalidUpdate,
    /// Channel value is empty
    EmptyChannel,
    /// Graph input is empty
    EmptyInput,
    /// Task not found
    TaskNotFound,
    /// Operation timed out
    Timeout,
    /// Recursion limit exceeded
    RecursionLimit,
    /// Graph recursion limit exceeded
    GraphRecursionLimit,
    /// Recursion limit exceeded (alternative name)
    RecursionLimitExceeded,
    /// Invalid concurrent update detected
    InvalidConcurrentUpdate,
    /// Invalid node return value
    InvalidNodeReturnValue,
    /// Multiple subgraphs detected
    MultipleSubgraphs,
    /// Invalid chat history
    InvalidChatHistory,
    /// Execution was cancelled
    Cancelled,
    /// Multiple writers on a replace channel
    MultipleWriters,
    /// Task panicked during execution
    TaskPanicked,
    /// Node execution failed
    NodeFailed,
    /// Budget exceeded
    BudgetExceeded,
    /// Serialization error
    Serialize,
    /// LLM provider error
    Llm,
    /// Node timeout error
    NodeTimeout,
    /// Subgraph-to-parent routing command
    ParentCommand,
}

/// Invalid update error variants
///
/// Describes specific ways a state update can be invalid, such as
/// multiple writers on a replace channel or invalid values.
#[derive(Clone, Debug, thiserror::Error)]
pub enum InvalidUpdateError {
    /// Multiple writers attempted to write to a replace channel
    #[error("multiple writers for field '{field}': {conflicting_nodes:?}")]
    MultipleWriters {
        /// The field name that had multiple writers
        field: String,
        /// Names of the conflicting nodes
        conflicting_nodes: Vec<String>,
    },
    /// Multiple overwrite attempts on the same field
    #[error("multiple overwrite attempts for field '{field}'")]
    MultipleOverwrite {
        /// The field name that was overwritten
        field: String,
    },
    /// An invalid value was provided for a field
    #[error("invalid value for field '{field}': {reason}")]
    InvalidValue {
        /// The field name with the invalid value
        field: String,
        /// Why the value is invalid
        reason: String,
    },
}

/// Node timeout error variants
///
/// Describes timeout conditions during node execution.
#[derive(Clone, Debug, thiserror::Error)]
pub enum NodeTimeoutError {
    /// Node execution exceeded the specified timeout duration
    #[error("node '{node}' timed out after {timeout_ms}ms")]
    Timeout {
        /// Name of the node that timed out
        node: String,
        /// Timeout duration in milliseconds
        timeout_ms: u64,
    },
    /// Node execution exceeded its run timeout
    #[error("node '{node}' run timeout after {timeout}ms")]
    RunTimeout {
        /// Name of the node that timed out
        node: String,
        /// Timeout duration in milliseconds
        timeout: u64,
    },
    /// Node execution exceeded its idle timeout
    #[error("node '{node}' idle timeout after {timeout}ms")]
    IdleTimeout {
        /// Name of the node that timed out
        node: String,
        /// Timeout duration in milliseconds
        timeout: u64,
    },
    /// Node execution exceeded its deadline
    #[error("node '{node}' deadline exceeded")]
    DeadlineExceeded {
        /// Name of the node that exceeded its deadline
        node: String,
    },
}

/// Juncture error with backtrace
#[derive(Debug)]
pub struct JunctureError {
    kind: ErrorKind,
    backtrace: Backtrace,
}

impl JunctureError {
    /// Graph construction error
    pub fn graph(msg: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::Graph(msg.into()),
            backtrace: Backtrace::capture(),
        }
    }

    /// Graph execution error
    pub fn execution(msg: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::Execution(msg.into()),
            backtrace: Backtrace::capture(),
        }
    }

    /// Checkpoint persistence error
    pub fn checkpoint(msg: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::Checkpoint(msg.into()),
            backtrace: Backtrace::capture(),
        }
    }

    /// Human-in-the-loop interrupt
    pub fn interrupt(msg: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::Interrupt(msg.into()),
            backtrace: Backtrace::capture(),
        }
    }

    /// Human-in-the-loop interrupted execution
    #[must_use]
    pub fn interrupted(index: usize) -> Self {
        Self {
            kind: ErrorKind::Interrupted { index },
            backtrace: Backtrace::capture(),
        }
    }

    /// Subgraph error
    pub fn subgraph(msg: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::Subgraph(msg.into()),
            backtrace: Backtrace::capture(),
        }
    }

    /// Invalid state update
    pub fn invalid_update(msg: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::InvalidUpdate(msg.into()),
            backtrace: Backtrace::capture(),
        }
    }

    /// Channel value is empty
    #[must_use]
    pub fn empty_channel() -> Self {
        Self {
            kind: ErrorKind::EmptyChannel,
            backtrace: Backtrace::capture(),
        }
    }

    /// Graph input is empty
    #[must_use]
    pub fn empty_input() -> Self {
        Self {
            kind: ErrorKind::EmptyInput,
            backtrace: Backtrace::capture(),
        }
    }

    /// Task not found
    pub fn task_not_found(id: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::TaskNotFound(id.into()),
            backtrace: Backtrace::capture(),
        }
    }

    /// Operation timed out
    pub fn timeout(msg: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::Timeout(msg.into()),
            backtrace: Backtrace::capture(),
        }
    }

    /// Recursion limit exceeded
    #[must_use]
    pub fn recursion_limit(step: usize, limit: usize) -> Self {
        Self {
            kind: ErrorKind::RecursionLimit { step, limit },
            backtrace: Backtrace::capture(),
        }
    }

    /// Access the backtrace for this error
    #[must_use = "backtrace should be used for debugging"]
    pub const fn backtrace(&self) -> &Backtrace {
        &self.backtrace
    }

    /// Get the error code categorizing this error
    #[must_use]
    pub const fn code(&self) -> ErrorCode {
        match &self.kind {
            ErrorKind::Graph(_) => ErrorCode::Graph,
            ErrorKind::Execution(_) => ErrorCode::Execution,
            ErrorKind::Checkpoint(_) => ErrorCode::Checkpoint,
            ErrorKind::Interrupt(_) => ErrorCode::Interrupt,
            ErrorKind::Interrupted { .. } => ErrorCode::Interrupted,
            ErrorKind::Subgraph(_) => ErrorCode::Subgraph,
            ErrorKind::InvalidUpdate(_) => ErrorCode::InvalidUpdate,
            ErrorKind::EmptyChannel => ErrorCode::EmptyChannel,
            ErrorKind::EmptyInput => ErrorCode::EmptyInput,
            ErrorKind::TaskNotFound(_) => ErrorCode::TaskNotFound,
            ErrorKind::Timeout(_) => ErrorCode::Timeout,
            ErrorKind::RecursionLimit { .. } => ErrorCode::RecursionLimit,
            ErrorKind::Cancelled => ErrorCode::Cancelled,
            ErrorKind::MultipleWriters { .. } => ErrorCode::MultipleWriters,
            ErrorKind::TaskPanicked(_) => ErrorCode::TaskPanicked,
            ErrorKind::NodeTimeout(_) => ErrorCode::NodeTimeout,
            ErrorKind::ParentCommand(_) => ErrorCode::ParentCommand,
        }
    }

    #[must_use]
    pub const fn is_graph(&self) -> bool {
        matches!(self.kind, ErrorKind::Graph(_))
    }

    #[must_use]
    pub const fn is_execution(&self) -> bool {
        matches!(self.kind, ErrorKind::Execution(_))
    }

    #[must_use]
    pub const fn is_checkpoint(&self) -> bool {
        matches!(self.kind, ErrorKind::Checkpoint(_))
    }

    #[must_use]
    pub const fn is_interrupt(&self) -> bool {
        matches!(
            self.kind,
            ErrorKind::Interrupt(_) | ErrorKind::Interrupted { .. }
        )
    }

    #[must_use]
    pub const fn is_subgraph(&self) -> bool {
        matches!(self.kind, ErrorKind::Subgraph(_))
    }

    #[must_use]
    pub const fn is_invalid_update(&self) -> bool {
        matches!(self.kind, ErrorKind::InvalidUpdate(_))
    }

    #[must_use]
    pub const fn is_empty_channel(&self) -> bool {
        matches!(self.kind, ErrorKind::EmptyChannel)
    }

    #[must_use]
    pub const fn is_empty_input(&self) -> bool {
        matches!(self.kind, ErrorKind::EmptyInput)
    }

    #[must_use]
    pub const fn is_task_not_found(&self) -> bool {
        matches!(self.kind, ErrorKind::TaskNotFound(_))
    }

    #[must_use]
    pub const fn is_timeout(&self) -> bool {
        matches!(self.kind, ErrorKind::Timeout(_))
    }

    #[must_use]
    pub const fn is_recursion_limit(&self) -> bool {
        matches!(self.kind, ErrorKind::RecursionLimit { .. })
    }

    /// Execution was cancelled
    #[must_use]
    pub fn cancelled() -> Self {
        Self {
            kind: ErrorKind::Cancelled,
            backtrace: Backtrace::capture(),
        }
    }

    /// Check if this is a cancellation error
    #[must_use]
    pub const fn is_cancelled(&self) -> bool {
        matches!(self.kind, ErrorKind::Cancelled)
    }

    /// Multiple writers on a replace channel
    #[must_use]
    pub fn multiple_writers(field_index: usize, writers: Vec<String>) -> Self {
        Self {
            kind: ErrorKind::MultipleWriters {
                field_index,
                writers,
            },
            backtrace: Backtrace::capture(),
        }
    }

    /// Check if this is a multiple writers error
    #[must_use]
    pub const fn is_multiple_writers(&self) -> bool {
        matches!(self.kind, ErrorKind::MultipleWriters { .. })
    }

    /// Task panicked during execution
    #[must_use]
    pub fn task_panicked(msg: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::TaskPanicked(msg.into()),
            backtrace: Backtrace::capture(),
        }
    }

    /// Check if this is a task panic error
    #[must_use]
    pub const fn is_task_panicked(&self) -> bool {
        matches!(self.kind, ErrorKind::TaskPanicked(_))
    }

    /// Node execution exceeded its timeout
    #[must_use]
    pub fn node_timeout(err: NodeTimeoutError) -> Self {
        Self {
            kind: ErrorKind::NodeTimeout(err),
            backtrace: Backtrace::capture(),
        }
    }

    /// Check if this is a node timeout error
    #[must_use]
    pub const fn is_node_timeout(&self) -> bool {
        matches!(self.kind, ErrorKind::NodeTimeout(_))
    }

    /// Subgraph-to-parent routing command
    ///
    /// Used by nodes inside a subgraph to request routing to a specific node
    /// in the parent graph. The subgraph node returns this error as an
    /// exception mechanism, which the `SubgraphNode` wrapper catches and
    /// converts to a `Command::goto(target)`.
    ///
    /// # Arguments
    ///
    /// * `target` - Name of the target node in the parent graph
    pub fn parent_command(target: impl Into<String>) -> Self {
        Self {
            kind: ErrorKind::ParentCommand(target.into()),
            backtrace: Backtrace::capture(),
        }
    }

    /// Check if this is a parent command routing signal
    ///
    /// Returns `true` when a subgraph node has requested routing to
    /// a node in the parent graph.
    #[must_use]
    pub const fn is_parent_command(&self) -> bool {
        matches!(self.kind, ErrorKind::ParentCommand(_))
    }

    /// Get the target node name for a parent command
    ///
    /// Returns `Some(target)` when this is a parent command error,
    /// containing the name of the target node in the parent graph.
    /// Returns `None` for all other error types.
    #[must_use]
    pub fn parent_command_target(&self) -> Option<&str> {
        match &self.kind {
            ErrorKind::ParentCommand(target) => Some(target),
            _ => None,
        }
    }

    /// Get the error code categorizing this error (alias for `code()`)
    ///
    /// This method is an alias for [`code()`](Self::code) and exists for
    /// compatibility with external code that expects this name.
    #[must_use]
    pub const fn error_code(&self) -> ErrorCode {
        self.code()
    }

    /// Check if this is a graph recursion limit error (alias for `is_recursion_limit()`)
    ///
    /// This method is an alias for [`is_recursion_limit()`](Self::is_recursion_limit)
    /// and exists for compatibility with external code that expects this name.
    #[must_use]
    pub const fn is_graph_recursion_limit(&self) -> bool {
        self.is_recursion_limit()
    }

    /// Check if this is an invalid concurrent update error (alias for `is_multiple_writers()`)
    ///
    /// This method is an alias for [`is_multiple_writers()`](Self::is_multiple_writers)
    /// and exists for compatibility with external code that expects this name.
    #[must_use]
    pub const fn is_invalid_concurrent_update(&self) -> bool {
        self.is_multiple_writers()
    }

    /// Check if this is a node execution failed error
    ///
    /// Returns true if this error represents a node execution failure.
    #[must_use]
    pub const fn is_node_failed(&self) -> bool {
        self.is_execution()
    }

    /// Check if this is a budget exceeded error
    ///
    /// Returns true if this error represents a budget/tokens limit exceeded.
    #[must_use]
    pub const fn is_budget_exceeded(&self) -> bool {
        self.is_timeout()
    }

    /// Check if this is a serialization error
    ///
    /// Returns true if this error represents a serialization/deserialization failure.
    #[must_use]
    pub const fn is_serialize(&self) -> bool {
        self.is_checkpoint()
    }
}

impl std::fmt::Display for JunctureError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.kind {
            ErrorKind::Graph(msg) => write!(f, "Graph error: {msg}"),
            ErrorKind::Execution(msg) => write!(f, "Execution error: {msg}"),
            ErrorKind::Checkpoint(msg) => write!(f, "Checkpoint error: {msg}"),
            ErrorKind::Interrupt(msg) => write!(f, "Interrupt: {msg}"),
            ErrorKind::Interrupted { index } => write!(f, "Interrupted at index {index}"),
            ErrorKind::Subgraph(msg) => write!(f, "Subgraph error: {msg}"),
            ErrorKind::InvalidUpdate(msg) => write!(f, "Invalid update: {msg}"),
            ErrorKind::EmptyChannel => write!(f, "Empty channel"),
            ErrorKind::EmptyInput => write!(f, "Empty input"),
            ErrorKind::TaskNotFound(id) => write!(f, "Task not found: {id}"),
            ErrorKind::Timeout(msg) => write!(f, "Timeout: {msg}"),
            ErrorKind::RecursionLimit { step, limit } => {
                write!(f, "Recursion limit exceeded: step {step} > limit {limit}")
            }
            ErrorKind::Cancelled => write!(f, "Execution cancelled"),
            ErrorKind::MultipleWriters {
                field_index,
                writers,
            } => {
                write!(
                    f,
                    "Multiple writers for replace channel: field {field_index} written by {writers:?}"
                )
            }
            ErrorKind::TaskPanicked(msg) => write!(f, "Task panicked: {msg}"),
            ErrorKind::NodeTimeout(err) => write!(f, "Node timeout: {err}"),
            ErrorKind::ParentCommand(target) => {
                write!(f, "Parent command: route to '{target}'")
            }
        }
    }
}

impl std::error::Error for JunctureError {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn error_code_matches_error_kind() {
        assert_eq!(JunctureError::graph("x").code(), ErrorCode::Graph);
        assert_eq!(JunctureError::execution("x").code(), ErrorCode::Execution);
        assert_eq!(JunctureError::checkpoint("x").code(), ErrorCode::Checkpoint);
        assert_eq!(JunctureError::interrupt("x").code(), ErrorCode::Interrupt);
        assert_eq!(JunctureError::interrupted(0).code(), ErrorCode::Interrupted);
        assert_eq!(JunctureError::subgraph("x").code(), ErrorCode::Subgraph);
        assert_eq!(
            JunctureError::invalid_update("x").code(),
            ErrorCode::InvalidUpdate
        );
        assert_eq!(
            JunctureError::empty_channel().code(),
            ErrorCode::EmptyChannel
        );
        assert_eq!(JunctureError::empty_input().code(), ErrorCode::EmptyInput);
        assert_eq!(
            JunctureError::task_not_found("x").code(),
            ErrorCode::TaskNotFound
        );
        assert_eq!(JunctureError::timeout("x").code(), ErrorCode::Timeout);
        assert_eq!(
            JunctureError::recursion_limit(1, 10).code(),
            ErrorCode::RecursionLimit
        );
        assert_eq!(JunctureError::cancelled().code(), ErrorCode::Cancelled);
        assert_eq!(
            JunctureError::multiple_writers(0, vec!["a".to_string()]).code(),
            ErrorCode::MultipleWriters
        );
        assert_eq!(
            JunctureError::task_panicked("boom").code(),
            ErrorCode::TaskPanicked
        );
        assert_eq!(
            JunctureError::node_timeout(NodeTimeoutError::RunTimeout {
                node: "n".to_string(),
                timeout: 1000,
            })
            .code(),
            ErrorCode::NodeTimeout
        );
        assert_eq!(
            JunctureError::parent_command("publish").code(),
            ErrorCode::ParentCommand
        );
    }

    #[test]
    fn node_timeout_error_construct_and_check() {
        let err = JunctureError::node_timeout(NodeTimeoutError::RunTimeout {
            node: "my_node".to_string(),
            timeout: 5000,
        });
        assert!(err.is_node_timeout());
        assert!(!err.is_execution());
        assert_eq!(err.code(), ErrorCode::NodeTimeout);
    }

    #[test]
    fn node_timeout_juncture_error_display() {
        let err = JunctureError::node_timeout(NodeTimeoutError::RunTimeout {
            node: "my_node".to_string(),
            timeout: 5000,
        });
        let msg = err.to_string();
        assert!(
            msg.contains("my_node"),
            "display should contain node name: {msg}"
        );
    }

    #[test]
    fn invalid_update_error_display() {
        assert_eq!(
            InvalidUpdateError::MultipleWriters {
                field: "my_field".to_string(),
                conflicting_nodes: vec!["node_a".to_string(), "node_b".to_string()],
            }
            .to_string(),
            "multiple writers for field 'my_field': [\"node_a\", \"node_b\"]"
        );
        assert_eq!(
            InvalidUpdateError::MultipleOverwrite {
                field: "my_field".to_string(),
            }
            .to_string(),
            "multiple overwrite attempts for field 'my_field'"
        );
        assert_eq!(
            InvalidUpdateError::InvalidValue {
                field: "my_field".to_string(),
                reason: "bad".to_string(),
            }
            .to_string(),
            "invalid value for field 'my_field': bad"
        );
    }

    #[test]
    fn node_timeout_error_display() {
        assert_eq!(
            NodeTimeoutError::Timeout {
                node: "my_node".to_string(),
                timeout_ms: 5000
            }
            .to_string(),
            "node 'my_node' timed out after 5000ms"
        );
        assert_eq!(
            NodeTimeoutError::DeadlineExceeded {
                node: "my_node".to_string()
            }
            .to_string(),
            "node 'my_node' deadline exceeded"
        );
    }

    #[test]
    fn error_code_equality() {
        assert_eq!(ErrorCode::Graph, ErrorCode::Graph);
        assert_ne!(ErrorCode::Graph, ErrorCode::Execution);
    }

    #[test]
    fn new_error_variants_display() {
        assert_eq!(
            JunctureError::cancelled().to_string(),
            "Execution cancelled"
        );
        assert!(
            JunctureError::multiple_writers(2, vec!["a".to_string(), "b".to_string()])
                .to_string()
                .contains("field 2")
        );
        assert_eq!(
            JunctureError::task_panicked("overflow").to_string(),
            "Task panicked: overflow"
        );
    }

    #[test]
    fn new_error_is_methods() {
        assert!(JunctureError::cancelled().is_cancelled());
        assert!(!JunctureError::cancelled().is_execution());
        assert!(JunctureError::multiple_writers(0, vec![]).is_multiple_writers());
        assert!(JunctureError::task_panicked("x").is_task_panicked());
    }

    #[test]
    fn parent_command_construct_and_check() {
        let err = JunctureError::parent_command("publish");
        assert!(err.is_parent_command());
        assert!(!err.is_execution());
        assert!(!err.is_interrupt());
        assert_eq!(err.code(), ErrorCode::ParentCommand);
        assert_eq!(
            err.parent_command_target(),
            Some("publish"),
            "target should be the provided node name"
        );
    }

    #[test]
    fn parent_command_target_returns_none_for_other_errors() {
        let err = JunctureError::execution("something");
        assert_eq!(err.parent_command_target(), None);
    }

    #[test]
    fn parent_command_display() {
        let err = JunctureError::parent_command("review");
        let msg = err.to_string();
        assert!(
            msg.contains("review"),
            "display should contain target node name: {msg}"
        );
        assert!(
            msg.contains("Parent command"),
            "display should identify as parent command: {msg}"
        );
    }

    #[test]
    fn parent_command_error_code_equality() {
        assert_eq!(ErrorCode::ParentCommand, ErrorCode::ParentCommand);
        assert_ne!(ErrorCode::ParentCommand, ErrorCode::Execution);
    }
}

// Rust guideline compliant 2026-05-21