busbar-sf-agentscript 0.0.2

AgentScript parser, graph analysis, and LSP for Salesforce Agentforce
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
//! Validation and analysis of reference graphs.

use super::edges::RefEdge;
use super::error::ValidationError;
use super::nodes::RefNode;
use super::RefGraph;
use petgraph::algo::{is_cyclic_directed, tarjan_scc};
use petgraph::graph::NodeIndex;
use petgraph::visit::EdgeRef;
use petgraph::Direction;
use std::collections::HashSet;

/// Result of validating a reference graph.
#[derive(Debug, Default)]
pub struct ValidationResult {
    /// All validation errors found
    pub errors: Vec<ValidationError>,
    /// All validation warnings found
    pub warnings: Vec<ValidationError>,
}

impl ValidationResult {
    /// Check if validation passed (no errors).
    pub fn is_ok(&self) -> bool {
        self.errors.is_empty()
    }

    /// Check if there are any issues (errors or warnings).
    pub fn has_issues(&self) -> bool {
        !self.errors.is_empty() || !self.warnings.is_empty()
    }

    /// Get all issues (errors and warnings combined).
    pub fn all_issues(&self) -> impl Iterator<Item = &ValidationError> {
        self.errors.iter().chain(self.warnings.iter())
    }
}

impl RefGraph {
    /// Perform full validation of the reference graph.
    ///
    /// Returns errors for issues that would cause runtime failures,
    /// and warnings for issues that may indicate problems.
    pub fn validate(&self) -> ValidationResult {
        let mut result = ValidationResult::default();

        // Report unresolved references found during graph build
        result.errors.extend(self.unresolved_references.clone());

        // Check for cycles
        result.errors.extend(self.find_cycles());

        // Check for unreachable topics
        result.warnings.extend(self.find_unreachable_topics());

        // Check for unused definitions
        result.warnings.extend(self.find_unused_actions());
        result.warnings.extend(self.find_unused_variables());

        result
    }

    /// Find cycles in topic transitions.
    ///
    /// Topic transitions should form a DAG. Cycles indicate infinite loops.
    pub fn find_cycles(&self) -> Vec<ValidationError> {
        if !is_cyclic_directed(&self.graph) {
            return vec![];
        }

        // Find strongly connected components to identify cycles
        let sccs = tarjan_scc(&self.graph);
        let mut errors = Vec::new();

        for scc in sccs {
            // A SCC with more than one node indicates a cycle
            if scc.len() > 1 {
                let path: Vec<String> = scc
                    .iter()
                    .filter_map(|&idx| {
                        if let Some(RefNode::Topic { name, .. }) = self.graph.node_weight(idx) {
                            Some(name.clone())
                        } else {
                            None
                        }
                    })
                    .collect();

                if !path.is_empty() {
                    errors.push(ValidationError::CycleDetected { path });
                }
            }
        }

        errors
    }

    /// Find topics that are unreachable from start_agent.
    pub fn find_unreachable_topics(&self) -> Vec<ValidationError> {
        let start_idx = match self.start_agent {
            Some(idx) => idx,
            None => return vec![], // No start_agent to check from
        };

        // Find all topics reachable from start_agent
        let reachable = self.find_reachable_from(start_idx);

        // Check each topic
        self.topics
            .iter()
            .filter_map(|(name, &idx)| {
                if !reachable.contains(&idx) {
                    if let Some(RefNode::Topic { span, .. }) = self.graph.node_weight(idx) {
                        Some(ValidationError::UnreachableTopic {
                            name: name.clone(),
                            span: *span,
                        })
                    } else {
                        None
                    }
                } else {
                    None
                }
            })
            .collect()
    }

    /// Find action definitions that are never invoked.
    pub fn find_unused_actions(&self) -> Vec<ValidationError> {
        self.action_defs
            .iter()
            .filter_map(|((topic, name), &idx)| {
                // Check if any edge points to this action
                let has_incoming = self
                    .graph
                    .edges_directed(idx, Direction::Incoming)
                    .any(|e| matches!(e.weight(), RefEdge::Invokes));

                if !has_incoming {
                    if let Some(RefNode::ActionDef { span, .. }) = self.graph.node_weight(idx) {
                        Some(ValidationError::UnusedActionDef {
                            name: name.clone(),
                            topic: topic.clone(),
                            span: *span,
                        })
                    } else {
                        None
                    }
                } else {
                    None
                }
            })
            .collect()
    }

    /// Find variables that are never read.
    pub fn find_unused_variables(&self) -> Vec<ValidationError> {
        self.variables
            .iter()
            .filter_map(|(name, &idx)| {
                // Check if any edge reads from this variable
                let has_readers = self
                    .graph
                    .edges_directed(idx, Direction::Incoming)
                    .any(|e| matches!(e.weight(), RefEdge::Reads));

                if !has_readers {
                    if let Some(RefNode::Variable { span, .. }) = self.graph.node_weight(idx) {
                        Some(ValidationError::UnusedVariable {
                            name: name.clone(),
                            span: *span,
                        })
                    } else {
                        None
                    }
                } else {
                    None
                }
            })
            .collect()
    }

    /// Find all nodes reachable from a starting node.
    fn find_reachable_from(&self, start: NodeIndex) -> HashSet<NodeIndex> {
        let mut reachable = HashSet::new();
        let mut stack = vec![start];

        while let Some(idx) = stack.pop() {
            if reachable.insert(idx) {
                // Add all outgoing neighbors
                for edge in self.graph.edges_directed(idx, Direction::Outgoing) {
                    stack.push(edge.target());
                }
            }
        }

        reachable
    }
}

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

    fn parse_and_build(source: &str) -> RefGraph {
        let ast = crate::parse(source).expect("Failed to parse");
        RefGraph::from_ast(&ast).expect("Failed to build graph")
    }

    #[test]
    fn test_no_cycles() {
        let source = r#"config:
   agent_name: "Test"

start_agent topic_selector:
   description: "Route to topics"
   reasoning:
      instructions: "Select the best topic"
      actions:
         go_help: @utils.transition to @topic.help
            description: "Go to help topic"

topic help:
   description: "Help topic"
   reasoning:
      instructions: "Provide help"
"#;
        let graph = parse_and_build(source);
        let result = graph.validate();
        assert!(result.errors.is_empty());
    }

    #[test]
    fn test_cycle_detected_between_two_topics() {
        // topic_a transitions to topic_b and topic_b transitions back to topic_a,
        // forming a cycle that should be detected.
        let source = r#"config:
   agent_name: "Test"

start_agent selector:
   description: "Route"
   reasoning:
      instructions: "Select"
      actions:
         go_a: @utils.transition to @topic.topic_a
            description: "Go to A"

topic topic_a:
   description: "Topic A"
   reasoning:
      instructions: "In A"
      actions:
         go_b: @utils.transition to @topic.topic_b
            description: "Go to B"

topic topic_b:
   description: "Topic B"
   reasoning:
      instructions: "In B"
      actions:
         go_a: @utils.transition to @topic.topic_a
            description: "Back to A"
"#;
        let graph = parse_and_build(source);
        let cycles = graph.find_cycles();
        assert!(!cycles.is_empty(), "Expected a cycle between topic_a and topic_b");
        let cycle_names: Vec<_> = cycles
            .iter()
            .flat_map(|e| {
                if let ValidationError::CycleDetected { path } = e {
                    path.clone()
                } else {
                    vec![]
                }
            })
            .collect();
        assert!(
            cycle_names.contains(&"topic_a".to_string())
                || cycle_names.contains(&"topic_b".to_string()),
            "Cycle should involve topic_a and/or topic_b, got: {:?}",
            cycle_names
        );
    }

    #[test]
    fn test_unreachable_topic_detected() {
        // topic_orphan is never the target of any transition, so it is unreachable
        // from start_agent and should be reported as a warning.
        let source = r#"config:
   agent_name: "Test"

start_agent selector:
   description: "Route"
   reasoning:
      instructions: "Select"
      actions:
         go_help: @utils.transition to @topic.help
            description: "Go to help"

topic help:
   description: "Help topic"
   reasoning:
      instructions: "Provide help"

topic orphan:
   description: "This topic is never reached by any transition"
   reasoning:
      instructions: "Orphan"
"#;
        let graph = parse_and_build(source);
        let unreachable = graph.find_unreachable_topics();
        assert!(!unreachable.is_empty(), "Expected 'orphan' to be detected as unreachable");
        let unreachable_names: Vec<_> = unreachable
            .iter()
            .filter_map(|e| {
                if let ValidationError::UnreachableTopic { name, .. } = e {
                    Some(name.clone())
                } else {
                    None
                }
            })
            .collect();
        assert!(
            unreachable_names.contains(&"orphan".to_string()),
            "Expected 'orphan' in unreachable topics, got: {:?}",
            unreachable_names
        );
        // 'help' IS reachable so it should not appear
        assert!(!unreachable_names.contains(&"help".to_string()), "'help' should be reachable");
    }

    #[test]
    fn test_unused_action_def_detected() {
        // get_data is defined in the actions block but no reasoning action invokes it,
        // so it should be reported as an unused action definition.
        let source = r#"config:
   agent_name: "Test"

topic main:
   description: "Main topic"

   actions:
      get_data:
         description: "Retrieves data from backend"
         inputs:
            record_id: string
               description: "Record identifier"
         outputs:
            result: string
               description: "Query result"
         target: "flow://GetData"

   reasoning:
      instructions: "Help the user with their request"
"#;
        let graph = parse_and_build(source);
        let unused = graph.find_unused_actions();
        assert!(!unused.is_empty(), "Expected 'get_data' to be detected as unused");
        let unused_names: Vec<_> = unused
            .iter()
            .filter_map(|e| {
                if let ValidationError::UnusedActionDef { name, topic, .. } = e {
                    Some((topic.clone(), name.clone()))
                } else {
                    None
                }
            })
            .collect();
        assert!(
            unused_names.contains(&("main".to_string(), "get_data".to_string())),
            "Expected ('main', 'get_data') in unused actions, got: {:?}",
            unused_names
        );
    }

    #[test]
    fn test_unused_variable_detected() {
        // customer_name is declared in the variables block but is never read by
        // any reasoning action, so it should be reported as an unused variable.
        let source = r#"config:
   agent_name: "Test"

variables:
   customer_name: mutable string = ""
      description: "The customer's name — declared but never read"

topic main:
   description: "Main topic"
   reasoning:
      instructions: "Help the user"
"#;
        let graph = parse_and_build(source);
        let unused = graph.find_unused_variables();
        assert!(!unused.is_empty(), "Expected 'customer_name' to be detected as unused");
        let unused_names: Vec<_> = unused
            .iter()
            .filter_map(|e| {
                if let ValidationError::UnusedVariable { name, .. } = e {
                    Some(name.clone())
                } else {
                    None
                }
            })
            .collect();
        assert!(
            unused_names.contains(&"customer_name".to_string()),
            "Expected 'customer_name' in unused variables, got: {:?}",
            unused_names
        );
    }

    #[test]
    fn test_unresolved_topic_reference_detected() {
        // The start_agent transitions to @topic.nonexistent which is never defined,
        // so the reference should surface as an error during validation.
        let source = r#"config:
   agent_name: "Test"

start_agent selector:
   description: "Route"
   reasoning:
      instructions: "Select"
      actions:
         go_missing: @utils.transition to @topic.nonexistent
            description: "Go to a topic that does not exist"

topic real_topic:
   description: "The only real topic"
   reasoning:
      instructions: "Real"
"#;
        let graph = parse_and_build(source);
        let result = graph.validate();
        // Unresolved references are collected as errors
        let unresolved: Vec<_> = result
            .errors
            .iter()
            .filter(|e| matches!(e, ValidationError::UnresolvedReference { .. }))
            .collect();
        assert!(
            !unresolved.is_empty(),
            "Expected an unresolved reference error for @topic.nonexistent"
        );
    }

    #[test]
    fn test_validate_returns_ok_for_fully_connected_graph() {
        // All topics reachable, all action defs invoked, no cycles — validate() should
        // return no errors and no warnings.
        let source = r#"config:
   agent_name: "Test"

start_agent selector:
   description: "Route to main"
   reasoning:
      instructions: "Select"
      actions:
         go_main: @utils.transition to @topic.main
            description: "Enter main"

topic main:
   description: "Main topic"

   actions:
      lookup:
         description: "Look up a record"
         inputs:
            id: string
               description: "Record ID"
         outputs:
            name: string
               description: "Record name"
         target: "flow://Lookup"

   reasoning:
      instructions: "Help"
      actions:
         do_lookup: @actions.lookup
            description: "Perform the lookup"
"#;
        let graph = parse_and_build(source);
        let result = graph.validate();
        assert!(result.errors.is_empty(), "Expected no errors, got: {:?}", result.errors);
        // The action is invoked, so no unused-action warnings expected
        let unused_action_warns: Vec<_> = result
            .warnings
            .iter()
            .filter(|w| matches!(w, ValidationError::UnusedActionDef { .. }))
            .collect();
        assert!(
            unused_action_warns.is_empty(),
            "Expected no unused-action warnings, got: {:?}",
            unused_action_warns
        );
    }

    #[test]
    fn test_three_node_cycle_detected() {
        // topic_a → topic_b → topic_c → topic_a forms a three-node cycle.
        // The graph contains a cycle involving all three topics and
        // find_cycles() should surface it.
        let source = r#"config:
   agent_name: "Test"

start_agent selector:
   description: "Route"
   reasoning:
      instructions: "Select"
      actions:
         go_a: @utils.transition to @topic.topic_a
            description: "Go to A"

topic topic_a:
   description: "Topic A"
   reasoning:
      instructions: "In A"
      actions:
         go_b: @utils.transition to @topic.topic_b
            description: "Go to B"

topic topic_b:
   description: "Topic B"
   reasoning:
      instructions: "In B"
      actions:
         go_c: @utils.transition to @topic.topic_c
            description: "Go to C"

topic topic_c:
   description: "Topic C"
   reasoning:
      instructions: "In C"
      actions:
         back_to_a: @utils.transition to @topic.topic_a
            description: "Back to A"
"#;
        let graph = parse_and_build(source);
        let cycles = graph.find_cycles();
        assert!(!cycles.is_empty(), "Expected a cycle among topic_a, topic_b, topic_c");
        let cycle_names: Vec<_> = cycles
            .iter()
            .flat_map(|e| {
                if let ValidationError::CycleDetected { path } = e {
                    path.clone()
                } else {
                    vec![]
                }
            })
            .collect();
        // At least one of the three topics should appear in the reported cycle path
        assert!(
            cycle_names
                .iter()
                .any(|n| { n == "topic_a" || n == "topic_b" || n == "topic_c" }),
            "Cycle should involve topic_a/b/c, got: {:?}",
            cycle_names
        );
    }

    #[test]
    fn test_invalid_property_access_on_non_object_variable() {
        // Accessing a property on a number variable should produce an
        // InvalidPropertyAccess error.
        let source = r#"config:
   agent_name: "Test"

variables:
   count: mutable number = 0
      description: "A counter"

start_agent selector:
   description: "Route"
   reasoning:
      instructions:->
         | Value: {!@variables.count.value}
      actions:
         go_main: @utils.transition to @topic.main
            description: "Go to main"

topic main:
   description: "Main"
   reasoning:
      instructions: "Help"
      actions:
         stay: @utils.transition to @topic.main
            description: "Stay"
"#;
        let graph = parse_and_build(source);
        let result = graph.validate();
        let invalid_access: Vec<_> = result
            .errors
            .iter()
            .filter(|e| matches!(e, ValidationError::InvalidPropertyAccess { .. }))
            .collect();
        assert!(
            !invalid_access.is_empty(),
            "Expected InvalidPropertyAccess for @variables.count.value on number type"
        );
    }

    #[test]
    fn test_valid_property_access_on_object_variable() {
        // Accessing a property on an object variable should NOT produce errors.
        let source = r#"config:
   agent_name: "Test"

variables:
   stats: mutable object = {}
      description: "Stats"

start_agent selector:
   description: "Route"
   reasoning:
      instructions:->
         | Total: {!@variables.stats.total}
      actions:
         go_main: @utils.transition to @topic.main
            description: "Go to main"

topic main:
   description: "Main"
   reasoning:
      instructions: "Help"
      actions:
         stay: @utils.transition to @topic.main
            description: "Stay"
"#;
        let graph = parse_and_build(source);
        let result = graph.validate();
        let invalid_access: Vec<_> = result
            .errors
            .iter()
            .filter(|e| matches!(e, ValidationError::InvalidPropertyAccess { .. }))
            .collect();
        assert!(
            invalid_access.is_empty(),
            "Expected no InvalidPropertyAccess for @variables.stats.total on object type, got: {:?}",
            invalid_access
        );
    }

    #[test]
    fn test_unresolved_variable_reference_detected() {
        // A reasoning action binds @variables.nonexistent_var which is never declared
        // in the variables block.  The unresolved reference should surface as an error.
        let source = r#"config:
   agent_name: "Test"

variables:
   real_var: mutable string = ""

start_agent selector:
   description: "Route"
   reasoning:
      instructions: "Select"
      actions:
         go_main: @utils.transition to @topic.main
            description: "Go to main"

topic main:
   description: "Main"
   reasoning:
      instructions: "Help"
      actions:
         do_thing: @actions.do_thing
            description: "Do a thing"
            with id=@variables.nonexistent_var
"#;
        let graph = parse_and_build(source);
        let result = graph.validate();
        let unresolved: Vec<_> = result
            .errors
            .iter()
            .filter(|e| matches!(e, ValidationError::UnresolvedReference { .. }))
            .collect();
        assert!(
            !unresolved.is_empty(),
            "Expected an unresolved reference error for @variables.nonexistent_var"
        );
    }
}