juglans 0.2.16

Compiler and runtime for Juglans Workflow Language
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
// src/core/parser.rs
use crate::core::graph::WorkflowGraph;
use crate::core::jwl_lexer::Lexer;
use crate::core::jwl_parser::JwlParser;
use anyhow::{anyhow, Result};
use std::collections::HashMap;

pub struct GraphParser;

impl GraphParser {
    /// Static helper: parse task parameter string
    /// Handles strings like `key1=value1, key2=[nested]`
    pub fn parse_arguments_str(args_str: &str) -> HashMap<String, String> {
        let mut params = HashMap::new();
        let mut buffer = String::new();
        let mut key = String::new();
        let mut depth = 0;
        let mut in_quote = false;
        let mut parsing_key = true;

        let chars: Vec<char> = args_str.chars().collect();
        let len = chars.len();
        let mut i = 0;

        while i < len {
            let c = chars[i];

            match c {
                // Handle key-value separator '='
                '=' if depth == 0 && !in_quote && parsing_key => {
                    key = buffer.trim().to_string();
                    buffer.clear();
                    parsing_key = false;
                }
                // Handle parameter separator ','
                ',' if depth == 0 && !in_quote => {
                    if !key.is_empty() {
                        params.insert(key.clone(), buffer.trim().to_string());
                    }
                    buffer.clear();
                    key.clear();
                    parsing_key = true;
                }
                // Handle quotes
                '"' if depth == 0 => {
                    in_quote = !in_quote;
                    buffer.push(c);
                }
                // Handle escaped quotes (inside quoted strings)
                '\\' if in_quote && i + 1 < len && chars[i + 1] == '"' => {
                    buffer.push(c);
                    buffer.push(chars[i + 1]);
                    i += 1; // skip next character
                }
                // Handle nested structures: parentheses, brackets, braces
                '(' | '{' | '[' if !in_quote => {
                    depth += 1;
                    buffer.push(c);
                }
                ')' | '}' | ']' if !in_quote => {
                    if depth > 0 {
                        depth -= 1;
                    }
                    buffer.push(c);
                }
                // Regular character
                _ => {
                    // All characters are collected; trimming handles leading/trailing whitespace
                    buffer.push(c);
                }
            }
            i += 1;
        }

        // Handle the last parameter
        if !key.is_empty() {
            params.insert(key, buffer.trim().to_string());
        }
        params
    }

    pub fn parse(content: &str) -> Result<WorkflowGraph> {
        Self::parse_rdp(content)
    }

    /// Parse .jgflow manifest file — standalone ManifestParser, returns Manifest
    pub fn parse_manifest(content: &str) -> Result<crate::core::graph::Manifest> {
        crate::core::manifest_parser::ManifestParser::parse(content)
    }

    /// Parse library file — allows containing only function definitions, no entry node or regular nodes required
    pub fn parse_lib(content: &str) -> Result<WorkflowGraph> {
        Self::parse_lib_rdp(content)
    }

    fn parse_rdp(content: &str) -> Result<WorkflowGraph> {
        let tokens = Lexer::new(content)
            .tokenize()
            .map_err(|e| anyhow!("JWL Compilation Syntax Error:\n{}", e))?;
        let mut parser = JwlParser::new(&tokens, content);
        let mut wf = parser.parse_workflow()?;

        if wf.entry_node.is_empty() {
            // Find topological entry: a node with in-degree 0
            let entry_idx = wf
                .graph
                .node_indices()
                .find(|&idx| {
                    wf.graph
                        .neighbors_directed(idx, petgraph::Direction::Incoming)
                        .next()
                        .is_none()
                })
                .or_else(|| wf.graph.node_indices().next());

            if let Some(idx) = entry_idx {
                wf.entry_node = wf.graph[idx].id.clone();
            } else {
                return Err(anyhow!(
                    "Architecture Error: Workflow must contain at least one valid node."
                ));
            }
        }
        Ok(wf)
    }

    fn parse_lib_rdp(content: &str) -> Result<WorkflowGraph> {
        let tokens = Lexer::new(content)
            .tokenize()
            .map_err(|e| anyhow!("JWL Compilation Syntax Error:\n{}", e))?;
        let mut parser = JwlParser::new(&tokens, content);
        let wf = parser.parse_workflow()?;

        if wf.entry_node.is_empty()
            && wf.graph.node_indices().next().is_none()
            && wf.functions.is_empty()
            && wf.classes.is_empty()
        {
            return Err(anyhow!(
                "Library Error: Library file must define at least one function or class."
            ));
        }
        Ok(wf)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::graph::NodeType;

    #[test]
    fn test_python_imports_parsing() {
        let content = r#"
python: ["pandas", "sklearn.ensemble", "./utils.py"]

[load]: pandas.read_csv(path="data.csv")
[train]: sklearn.ensemble.RandomForestClassifier()

[load] -> [train]
"#;
        let graph = GraphParser::parse(content).unwrap();

        assert_eq!(graph.python_imports.len(), 3);
        assert!(graph.python_imports.contains(&"pandas".to_string()));
        assert!(graph
            .python_imports
            .contains(&"sklearn.ensemble".to_string()));
        assert!(graph.python_imports.contains(&"./utils.py".to_string()));
    }

    #[test]
    fn test_scoped_identifier_call() {
        let content = r#"
python: ["pandas"]

[load]: pandas.read_csv(path="data.csv", encoding="utf-8")
"#;
        let graph = GraphParser::parse(content).unwrap();

        // Verify the node was parsed correctly
        let node = graph.graph.node_weights().next().unwrap();
        assert_eq!(node.id, "load");

        if let NodeType::Task(action) = &node.node_type {
            assert_eq!(action.name, "pandas.read_csv");
            assert_eq!(action.params.get("path"), Some(&"\"data.csv\"".to_string()));
            assert_eq!(
                action.params.get("encoding"),
                Some(&"\"utf-8\"".to_string())
            );
        } else {
            panic!("Expected Task node type");
        }
    }

    #[test]
    fn test_switch_syntax_parsing() {
        let content = r#"
[start]: notify(message="start")
[case_a]: notify(message="A")
[case_b]: notify(message="B")
[fallback]: notify(message="default")

[start] -> switch type {
    "a": [case_a]
    "b": [case_b]
    default: [fallback]
}
"#;
        let graph = GraphParser::parse(content).unwrap();

        // Verify switch route was created
        assert!(graph.switch_routes.contains_key("start"));
        let switch_route = graph.switch_routes.get("start").unwrap();
        assert_eq!(switch_route.subject.trim(), "type");
        assert_eq!(switch_route.cases.len(), 3);

        // Verify cases
        assert_eq!(switch_route.cases[0].value, Some("a".to_string()));
        assert_eq!(switch_route.cases[0].target, "case_a");
        assert_eq!(switch_route.cases[1].value, Some("b".to_string()));
        assert_eq!(switch_route.cases[1].target, "case_b");
        assert_eq!(switch_route.cases[2].value, None); // default
        assert_eq!(switch_route.cases[2].target, "fallback");
    }

    #[test]
    fn test_missing_comma_detected() {
        let content = r#"
[start]: notify(message="hello" status="ok")
"#;
        let result = GraphParser::parse(content);
        assert!(
            result.is_err(),
            "Missing comma between parameters should cause parse error"
        );
    }

    #[test]
    fn test_valid_comma_separated_params() {
        let content = r#"
[start]: notify(message="hello", status="ok")
"#;
        let result = GraphParser::parse(content);
        assert!(
            result.is_ok(),
            "Comma-separated params should parse: {:?}",
            result.err()
        );
    }

    #[test]
    fn test_comparison_in_expression() {
        // == in edge conditions should still work
        let content = r#"
[start]: notify(message="test")
[a]: notify(message="a")
[b]: notify(message="b")
[start] if output.category == "technical" -> [a]
[start] -> [b]
"#;
        let result = GraphParser::parse(content);
        assert!(
            result.is_ok(),
            "Comparison operators should be valid: {:?}",
            result.err()
        );
    }

    #[test]
    fn test_duplicate_param_detected() {
        let content = r#"
[start]: notify(message="first", message="second")
"#;
        let result = GraphParser::parse(content);
        assert!(
            result.is_err(),
            "Duplicate parameter keys should cause parse error"
        );
    }

    #[test]
    fn test_multiline_params_with_commas() {
        let content = r#"
[start]: chat(
  agent="helper",
  message=input.query
)
"#;
        let result = GraphParser::parse(content);
        assert!(
            result.is_ok(),
            "Multiline params should parse: {:?}",
            result.err()
        );
    }

    #[test]
    fn test_single_step_function() {
        let content = r#"
[greet(name)]: bash(command="echo Hello, " + name)
[step1]: greet(name="world")
"#;
        let graph = GraphParser::parse(content).unwrap();
        assert!(graph.functions.contains_key("greet"));
        let func = graph.functions.get("greet").unwrap();
        assert_eq!(func.params, vec!["name"]);
        assert_eq!(func.body.node_map.len(), 1);
    }

    #[test]
    fn test_multi_step_function() {
        let content = r#"
[build(dir)]: {
  bash(command="cd " + dir + " && make")
  bash(command="cd " + dir + " && make test")
}
[step1]: build(dir="/app")
"#;
        let graph = GraphParser::parse(content).unwrap();
        assert!(graph.functions.contains_key("build"));
        let func = graph.functions.get("build").unwrap();
        assert_eq!(func.params, vec!["dir"]);
        assert_eq!(func.body.node_map.len(), 2);
        // Verify sequential edge exists
        assert_eq!(func.body.graph.edge_count(), 1);
    }

    #[test]
    fn test_multi_step_function_with_semicolons() {
        let content = r#"
[build(a, b)]: { bash(command=a); bash(command=b) }
[step1]: build(a="foo", b="bar")
"#;
        let graph = GraphParser::parse(content).unwrap();
        let func = graph.functions.get("build").unwrap();
        assert_eq!(func.params, vec!["a", "b"]);
        assert_eq!(func.body.node_map.len(), 2);
    }

    #[test]
    fn test_function_not_in_main_graph() {
        let content = r#"
[greet(name)]: bash(command="echo " + name)
[step1]: greet(name="world")
"#;
        let graph = GraphParser::parse(content).unwrap();
        // Function node should NOT be in main graph
        assert!(!graph.node_map.contains_key("greet"));
        // But the caller should be
        assert!(graph.node_map.contains_key("step1"));
    }

    #[test]
    fn test_no_params_backward_compat() {
        let content = r#"
[start]: bash(command="echo hello")
"#;
        let graph = GraphParser::parse(content).unwrap();
        assert!(graph.node_map.contains_key("start"));
        assert!(graph.functions.is_empty());
    }

    #[test]
    fn test_string_concat_expression() {
        let content = r#"
[start]: chat(agent="helper", message="[Expert] " + input.query)
"#;
        let result = GraphParser::parse(content);
        assert!(
            result.is_ok(),
            "String concat should parse: {:?}",
            result.err()
        );
    }

    #[test]
    fn test_foreach_without_dollar() {
        let content = r#"
[loop]: foreach(item in input.items) {
    [step]: notify(message="ok")
}
"#;
        let graph = GraphParser::parse(content).unwrap();
        let node = graph.node_map.get("loop").unwrap();
        let node_data = &graph.graph[*node];
        if let NodeType::Foreach { item, list, .. } = &node_data.node_type {
            assert_eq!(item, "item");
            assert_eq!(list, "input.items");
        } else {
            panic!("Expected Foreach node");
        }
    }

    #[test]
    fn test_assignment_block_parsing() {
        let content = r#"
[init]: count = 0, name = "Alice"
[next]: notify(message="done")
[init] -> [next]
"#;
        let graph = GraphParser::parse(content).unwrap();
        let node = graph.node_map.get("init").unwrap();
        let node_data = &graph.graph[*node];
        if let NodeType::Task(action) = &node_data.node_type {
            assert_eq!(action.name, "set_context");
            assert_eq!(action.params.get("count").unwrap(), "0");
            assert_eq!(action.params.get("name").unwrap(), "\"Alice\"");
        } else {
            panic!("Expected Task node, got {:?}", node_data.node_type);
        }
    }

    #[test]
    fn test_assignment_single() {
        let content = r#"
[init]: result = output.data
"#;
        let graph = GraphParser::parse(content).unwrap();
        let node = graph.node_map.get("init").unwrap();
        let node_data = &graph.graph[*node];
        if let NodeType::Task(action) = &node_data.node_type {
            assert_eq!(action.name, "set_context");
            assert_eq!(action.params.get("result").unwrap(), "output.data");
        } else {
            panic!("Expected Task node");
        }
    }

    // ---- Triple-quoted strings ----

    #[test]
    fn test_triple_quoted_in_task_param() {
        let input = r#"
            [run]: bash(command="""echo "hello world" && echo '{"key":"value"}'""")
        "#;
        let wf = GraphParser::parse(input).unwrap();
        let node = &wf.graph[*wf.node_map.get("run").unwrap()];
        if let NodeType::Task(action) = &node.node_type {
            assert_eq!(action.name, "bash");
            let cmd = action.params.get("command").unwrap();
            assert!(cmd.contains(r#"echo "hello world""#));
        } else {
            panic!("Expected Task node");
        }
    }

    #[test]
    fn test_triple_quoted_multiline_param() {
        let input = "[run]: bash(command=\"\"\"line1\nline2\nline3\"\"\")";
        let wf = GraphParser::parse(input).unwrap();
        assert!(wf.node_map.contains_key("run"));
    }

    #[test]
    fn test_triple_quoted_with_regular_string() {
        let input = r#"
            [a]: bash(command="""echo "test" done""")
            [b]: bash(command="echo simple")
        "#;
        let wf = GraphParser::parse(input).unwrap();
        assert!(wf.node_map.contains_key("a"));
        assert!(wf.node_map.contains_key("b"));
    }

    #[test]
    fn test_triple_quoted_assignment() {
        let input = r#"
            [setup]: cmd = """curl -H "Auth: key" https://api.com"""
        "#;
        let wf = GraphParser::parse(input).unwrap();
        assert!(wf.node_map.contains_key("setup"));
    }
}