newton-regorus 0.2.0

A fast, lightweight Rego (OPA policy language) interpreter with Newton extensions
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#![allow(
    clippy::panic,
    clippy::panic_in_result_fn,
    clippy::unwrap_used,
    clippy::manual_assert,
    clippy::indexing_slicing,
    clippy::option_if_let_else,
    clippy::semicolon_if_nothing_returned,
    clippy::unseparated_literal_suffix,
    clippy::use_debug,
    clippy::unused_trait_names,
    clippy::as_conversions,
    clippy::pattern_type_mismatch
)] // VM tests assert/unwrap and use manual panics to validate scenarios

#[cfg(test)]
mod tests {
    use crate::{
        rvm::tests::{
            instruction_parser::{parse_instruction, parse_loop_mode},
            test_utils::test_round_trip_serialization,
        },
        utils::limits::ExecutionTimerConfig,
    };
    use core::{num::NonZeroU32, time::Duration};
    #[derive(Debug, Clone, Deserialize, Serialize, Default)]
    struct RuleInfoSpec {
        rule_type: String,
        definitions: Vec<Vec<u16>>,
        #[serde(default)]
        default_rule_index: Option<u16>,
        #[serde(default)]
        default_literal_index: Option<u16>,
        #[serde(default)]
        destructuring_blocks: Option<Vec<Option<u16>>>,
    }

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct DefaultRuleSpec {
        rule_name: String,
        default_value: crate::Value,
    }

    use crate::{
        rvm::vm::{ExecutionMode, ExecutionState, RegoVM, SuspendReason, VmError},
        test_utils::process_value,
        value::Value,
    };
    use alloc::{
        collections::{BTreeMap, VecDeque},
        string::{String, ToString},
        sync::Arc,
        vec::Vec,
    };
    use anyhow::Result;
    use serde::{Deserialize, Serialize};
    use std::fs;
    use test_generator::test_resources;

    extern crate alloc;
    extern crate std;

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct HostAwaitResponseSpec {
        id: crate::Value,
        #[serde(default)]
        value: Option<crate::Value>,
        #[serde(default)]
        values: Vec<crate::Value>,
    }

    fn default_vm_test_execution_timer_config() -> ExecutionTimerConfig {
        ExecutionTimerConfig {
            limit: Duration::from_secs(1),
            check_interval: NonZeroU32::new(100).unwrap_or(NonZeroU32::MIN),
        }
    }

    #[derive(Debug, Deserialize, Serialize)]
    struct VmTestCase {
        note: String,
        #[serde(default)]
        description: Option<String>,
        #[serde(default)]
        example_rego: Option<String>,
        #[serde(default)]
        data: Option<crate::Value>,
        #[serde(default)]
        input: Option<crate::Value>,
        literals: Vec<crate::Value>,
        #[serde(default)]
        rule_infos: Vec<RuleInfoSpec>,
        #[serde(default)]
        default_rules: Vec<DefaultRuleSpec>,
        #[serde(default)]
        rule_tree: Option<crate::Value>,
        #[serde(default)]
        instruction_params: Option<InstructionParamsSpec>,
        #[serde(default)]
        max_instructions: Option<usize>,
        #[serde(default)]
        host_await_responses: Option<Vec<HostAwaitResponseSpec>>,
        #[serde(default)]
        host_await_responses_run_to_completion: Option<Vec<HostAwaitResponseSpec>>,
        #[serde(default)]
        host_await_responses_suspendable: Option<Vec<HostAwaitResponseSpec>>,
        #[serde(default)]
        ignore_run_to_completion_hostawait_failure: bool,
        instructions: Vec<String>,
        #[serde(default, deserialize_with = "deserialize_optional_value")]
        want_result: Option<crate::Value>,
        #[serde(default)]
        want_error: Option<String>,
        #[serde(default, deserialize_with = "deserialize_optional_value")]
        want_result_strict: Option<crate::Value>,
        #[serde(default)]
        want_error_strict: Option<String>,
    }

    fn deserialize_optional_value<'de, D>(deserializer: D) -> Result<Option<crate::Value>, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        // If the field is present, always return Some, even if the value is null
        crate::Value::deserialize(deserializer).map(Some)
    }

    #[derive(Debug, Clone, Deserialize, Serialize, Default)]
    struct InstructionParamsSpec {
        #[serde(default)]
        loop_params: Vec<LoopStartParamsSpec>,
        #[serde(default)]
        call_params: Vec<CallParamsSpec>,
        #[serde(default)]
        builtin_call_params: Vec<BuiltinCallParamsSpec>,
        #[serde(default)]
        function_call_params: Vec<FunctionCallParamsSpec>,
        #[serde(default)]
        builtin_infos: Vec<BuiltinInfoSpec>,
        #[serde(default)]
        object_create_params: Vec<ObjectCreateParamsSpec>,
        #[serde(default)]
        array_create_params: Vec<ArrayCreateParamsSpec>,
        #[serde(default)]
        set_create_params: Vec<SetCreateParamsSpec>,
        #[serde(default)]
        virtual_data_document_lookup_params: Vec<VirtualDataDocumentLookupParamsSpec>,
        #[serde(default)]
        chained_index_params: Vec<ChainedIndexParamsSpec>,
        #[serde(default, alias = "comprehension_start_params")]
        comprehension_begin_params: Vec<ComprehensionBeginParamsSpec>,
    }

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct LoopStartParamsSpec {
        mode: String,
        collection: u16,
        key_reg: u16,
        value_reg: u16,
        result_reg: u16,
        body_start: u16,
        loop_end: u16,
    }

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct CallParamsSpec {
        dest: u16,
        func: u16,
        args_start: u16,
        args_count: u16,
    }

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct BuiltinCallParamsSpec {
        dest: u16,
        builtin_index: u16,
        args: Vec<u16>,
    }

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct FunctionCallParamsSpec {
        func: u16,
        dest: u16,
        args: Vec<u16>,
    }

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct BuiltinInfoSpec {
        name: String,
        num_args: u16,
    }

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct ObjectCreateParamsSpec {
        dest: u16,
        template_literal_idx: u16,
        literal_key_fields: Vec<(u16, u16)>,
        fields: Vec<(u16, u16)>,
    }

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct ArrayCreateParamsSpec {
        dest: u16,
        elements: Vec<u16>,
    }

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct SetCreateParamsSpec {
        dest: u16,
        elements: Vec<u16>,
    }

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct LiteralOrRegisterSpec {
        #[serde(default, alias = "literal")]
        literal_idx: Option<u16>,
        #[serde(default, alias = "reg")]
        register: Option<u16>,
    }

    impl LiteralOrRegisterSpec {
        fn into_literal_or_register(self) -> crate::rvm::instructions::LiteralOrRegister {
            if let Some(literal_idx) = self.literal_idx {
                crate::rvm::instructions::LiteralOrRegister::Literal(literal_idx)
            } else if let Some(register) = self.register {
                crate::rvm::instructions::LiteralOrRegister::Register(register.try_into().unwrap())
            } else {
                panic!("LiteralOrRegisterSpec must specify either literal_idx or register");
            }
        }
    }

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct VirtualDataDocumentLookupParamsSpec {
        dest: u16,
        path_components: Vec<LiteralOrRegisterSpec>,
    }

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct ChainedIndexParamsSpec {
        dest: u16,
        root: u16,
        path_components: Vec<LiteralOrRegisterSpec>,
    }

    #[derive(Debug, Clone, Deserialize, Serialize)]
    struct ComprehensionBeginParamsSpec {
        mode: String,
        collection_reg: u16,
        #[serde(default)]
        result_reg: Option<u16>,
        key_reg: u16,
        value_reg: u16,
        body_start: u16,
        comprehension_end: u16,
    }

    #[derive(Debug, Deserialize, Serialize)]
    struct VmTestSuite {
        cases: Vec<VmTestCase>,
    }

    /// Execute VM instructions directly from parsed instructions and literals
    #[allow(clippy::too_many_arguments)]
    fn execute_vm_instructions(
        instructions: Vec<crate::rvm::instructions::Instruction>,
        literals: Vec<Value>,
        rule_infos: Vec<RuleInfoSpec>,
        rule_tree: Option<Value>,
        instruction_params: Option<InstructionParamsSpec>,
        data: Option<Value>,
        input: Option<Value>,
        max_instructions: Option<usize>,
        host_await_responses: Option<Vec<HostAwaitResponseSpec>>,
        host_await_responses_run_to_completion: Option<Vec<HostAwaitResponseSpec>>,
        host_await_responses_suspendable: Option<Vec<HostAwaitResponseSpec>>,
        ignore_run_to_completion_hostawait_failure: bool,
        strict: bool,
    ) -> Result<Value> {
        let processed_data = if let Some(ref data_value) = data {
            Some(process_value(data_value)?)
        } else {
            None
        };

        let processed_input = if let Some(ref input_value) = input {
            Some(process_value(input_value)?)
        } else {
            None
        };

        let processed_rule_tree = if let Some(ref tree_value) = rule_tree {
            Some(process_value(tree_value)?)
        } else {
            None
        };

        let process_responses =
            |responses: Vec<HostAwaitResponseSpec>| -> Result<BTreeMap<Value, Vec<Value>>> {
                let mut processed: BTreeMap<Value, Vec<Value>> = BTreeMap::new();

                for spec in responses {
                    let identifier = process_value(&spec.id)?;
                    let mut values: Vec<Value> = Vec::new();

                    if let Some(single) = spec.value.as_ref() {
                        values.push(process_value(single)?);
                    }

                    for value in &spec.values {
                        values.push(process_value(value)?);
                    }

                    if values.is_empty() {
                        return Err(anyhow::anyhow!(
                            "HostAwait response specification for id {:?} has no values",
                            identifier
                        ));
                    }

                    processed.entry(identifier).or_default().extend(values);
                }

                Ok(processed)
            };

        let processed_host_responses = if let Some(responses) = host_await_responses {
            Some(process_responses(responses)?)
        } else {
            None
        };

        let processed_host_responses_run_to_completion =
            if let Some(responses) = host_await_responses_run_to_completion {
                Some(process_responses(responses)?)
            } else {
                processed_host_responses.clone()
            };

        let processed_host_responses_suspendable =
            if let Some(responses) = host_await_responses_suspendable {
                Some(process_responses(responses)?)
            } else {
                processed_host_responses.clone()
            };

        // Create a Program from instructions and literals
        let mut program = crate::rvm::program::Program::new();
        program.instructions = instructions;

        // Process literals through the value converter to handle special syntax like set!
        let mut processed_literals = Vec::new();
        for literal in &literals {
            processed_literals.push(process_value(literal)?);
        }
        program.literals = processed_literals;

        if let Some(tree) = processed_rule_tree {
            program.rule_tree = tree;
        } else {
            program.rule_tree = Value::new_object();
        }

        // Convert rule infos
        for rule_info_spec in rule_infos.iter() {
            use crate::rvm::program::{RuleInfo, RuleType};

            let rule_type = match rule_info_spec.rule_type.as_str() {
                "Complete" => RuleType::Complete,
                "PartialSet" => RuleType::PartialSet,
                "PartialObject" => RuleType::PartialObject,
                _ => {
                    return Err(anyhow::anyhow!(
                        "Unknown rule type: {}",
                        rule_info_spec.rule_type
                    ))
                }
            };

            // Convert Vec<Vec<u16>> to Vec<Vec<u32>>
            let definitions: Vec<Vec<u32>> = rule_info_spec
                .definitions
                .iter()
                .map(|def| def.iter().map(|&x| x as u32).collect())
                .collect();

            let mut destructuring_blocks: Vec<Option<u32>> = rule_info_spec
                .destructuring_blocks
                .clone()
                .map(|blocks| {
                    blocks
                        .into_iter()
                        .map(|entry| entry.map(|value| value as u32))
                        .collect()
                })
                .unwrap_or_else(|| alloc::vec![None; definitions.len()]);

            if destructuring_blocks.len() != definitions.len() {
                destructuring_blocks.resize(definitions.len(), None);
            }

            // For function calls, use result_reg 0; for other rules, use result_reg 1
            let result_reg = if instruction_params
                .as_ref()
                .is_some_and(|params| !params.function_call_params.is_empty())
            {
                0 // Function calls use register 0 as return register
            } else {
                1 // Regular rules use register 1
            };

            let rule_info = RuleInfo {
                name: String::from("test_rule"),
                rule_type,
                definitions: crate::Rc::new(definitions.clone()),
                function_info: None,
                default_literal_index: rule_info_spec.default_literal_index,
                result_reg,
                num_registers: 50, // Increased to accommodate test cases with higher register indices
                destructuring_blocks,
            };

            program.rule_infos.push(rule_info);
        }

        // Build instruction data from params specification
        if let Some(params_spec) = instruction_params {
            // Convert loop params
            for loop_param_spec in params_spec.loop_params {
                let mode = parse_loop_mode(&loop_param_spec.mode)?;
                let loop_params = crate::rvm::instructions::LoopStartParams {
                    mode,
                    collection: loop_param_spec.collection.try_into().unwrap(),
                    key_reg: loop_param_spec.key_reg.try_into().unwrap(),
                    value_reg: loop_param_spec.value_reg.try_into().unwrap(),
                    result_reg: loop_param_spec.result_reg.try_into().unwrap(),
                    body_start: loop_param_spec.body_start,
                    loop_end: loop_param_spec.loop_end,
                };
                program.add_loop_params(loop_params);
            }

            // Convert call params
            // Legacy call_params support removed - use builtin_call_params or function_call_params instead
            if !params_spec.call_params.is_empty() {
                // Legacy call parameters are no longer supported
                // Convert to BuiltinCall or FunctionCall instructions instead
                panic!("Legacy call_params are no longer supported. Use builtin_call_params or function_call_params instead.");
            }

            // Convert builtin info specs to program builtin info table
            for builtin_info_spec in params_spec.builtin_infos {
                let builtin_info = crate::rvm::program::BuiltinInfo {
                    name: builtin_info_spec.name,
                    num_args: builtin_info_spec.num_args,
                };
                program.add_builtin_info(builtin_info);
            }

            // Convert builtin call params
            for builtin_call_spec in params_spec.builtin_call_params {
                use crate::rvm::instructions::BuiltinCallParams;

                // Convert Vec<u16> to fixed array (unused slots are irrelevant due to num_args)
                let mut args_array = [0u8; 8];
                for (i, &arg) in builtin_call_spec.args.iter().enumerate() {
                    if i < 8 {
                        args_array[i] = arg.try_into().unwrap();
                    }
                }

                let builtin_call_params = BuiltinCallParams {
                    dest: builtin_call_spec.dest.try_into().unwrap(),
                    builtin_index: builtin_call_spec.builtin_index,
                    num_args: builtin_call_spec.args.len() as u8,
                    args: args_array,
                };
                program.add_builtin_call_params(builtin_call_params);
            }

            // Convert function call params
            for function_call_spec in params_spec.function_call_params {
                use crate::rvm::instructions::FunctionCallParams;

                // Convert Vec<u16> to fixed array (unused slots are irrelevant due to num_args)
                let mut args_array = [0u8; 8];
                for (i, &arg) in function_call_spec.args.iter().enumerate() {
                    if i < 8 {
                        args_array[i] = arg.try_into().unwrap();
                    }
                }

                let function_call_params = FunctionCallParams {
                    func_rule_index: function_call_spec.func,
                    dest: function_call_spec.dest.try_into().unwrap(),
                    num_args: function_call_spec.args.len() as u8,
                    args: args_array,
                };
                program.add_function_call_params(function_call_params);
            }

            // Convert object create params
            for object_create_spec in params_spec.object_create_params {
                use crate::rvm::instructions::ObjectCreateParams;

                let object_create_params = ObjectCreateParams {
                    dest: object_create_spec.dest.try_into().unwrap(),
                    template_literal_idx: object_create_spec.template_literal_idx,
                    literal_key_fields: object_create_spec
                        .literal_key_fields
                        .into_iter()
                        .map(|(k, v)| (k, v.try_into().unwrap()))
                        .collect(),
                    fields: object_create_spec
                        .fields
                        .into_iter()
                        .map(|(k, v)| (k.try_into().unwrap(), v.try_into().unwrap()))
                        .collect(),
                };
                program
                    .instruction_data
                    .add_object_create_params(object_create_params);
            }

            // Convert array create params
            for array_create_spec in params_spec.array_create_params {
                use crate::rvm::instructions::ArrayCreateParams;

                let array_create_params = ArrayCreateParams {
                    dest: array_create_spec.dest.try_into().unwrap(),
                    elements: array_create_spec
                        .elements
                        .into_iter()
                        .map(|reg| reg.try_into().unwrap())
                        .collect(),
                };
                program
                    .instruction_data
                    .add_array_create_params(array_create_params);
            }

            // Convert set create params
            for set_create_spec in params_spec.set_create_params {
                use crate::rvm::instructions::SetCreateParams;

                let set_create_params = SetCreateParams {
                    dest: set_create_spec.dest.try_into().unwrap(),
                    elements: set_create_spec
                        .elements
                        .into_iter()
                        .map(|reg| reg.try_into().unwrap())
                        .collect(),
                };
                program
                    .instruction_data
                    .add_set_create_params(set_create_params);
            }

            // Convert virtual data document lookup params
            for virtual_spec in params_spec.virtual_data_document_lookup_params {
                use crate::rvm::instructions::{
                    LiteralOrRegister, VirtualDataDocumentLookupParams,
                };

                let path_components: Vec<LiteralOrRegister> = virtual_spec
                    .path_components
                    .into_iter()
                    .map(|component| component.into_literal_or_register())
                    .collect();

                let params = VirtualDataDocumentLookupParams {
                    dest: virtual_spec.dest.try_into().unwrap(),
                    path_components,
                };

                program
                    .instruction_data
                    .add_virtual_data_document_lookup_params(params);
            }

            // Convert chained index params
            for chained_spec in params_spec.chained_index_params {
                use crate::rvm::instructions::{ChainedIndexParams, LiteralOrRegister};

                let path_components: Vec<LiteralOrRegister> = chained_spec
                    .path_components
                    .into_iter()
                    .map(|component| component.into_literal_or_register())
                    .collect();

                let params = ChainedIndexParams {
                    dest: chained_spec.dest.try_into().unwrap(),
                    root: chained_spec.root.try_into().unwrap(),
                    path_components,
                };

                program.instruction_data.add_chained_index_params(params);
            }

            // Convert comprehension start params
            for comprehension_spec in params_spec.comprehension_begin_params {
                use crate::rvm::instructions::{ComprehensionBeginParams, ComprehensionMode};

                let mode = match comprehension_spec.mode.as_str() {
                    "Array" => ComprehensionMode::Array,
                    "Set" => ComprehensionMode::Set,
                    "Object" => ComprehensionMode::Object,
                    _ => panic!("Invalid comprehension mode: {}", comprehension_spec.mode),
                };

                let comprehension_params = ComprehensionBeginParams {
                    mode,
                    collection_reg: comprehension_spec.collection_reg.try_into().unwrap(),
                    result_reg: comprehension_spec
                        .result_reg
                        .unwrap_or(comprehension_spec.collection_reg)
                        .try_into()
                        .unwrap(),
                    key_reg: comprehension_spec.key_reg.try_into().unwrap(),
                    value_reg: comprehension_spec.value_reg.try_into().unwrap(),
                    body_start: comprehension_spec.body_start,
                    comprehension_end: comprehension_spec.comprehension_end,
                };
                program
                    .instruction_data
                    .add_comprehension_begin_params(comprehension_params);
            }
        }

        program.main_entry_point = 0;

        // Set a reasonable default for register window size in VM tests
        // Most tests use registers 0-10, so we'll allocate 255 registers (u8 max)
        program.max_rule_window_size = 255;
        program.dispatch_window_size = 50;

        // Initialize resolved builtins if we have builtin info
        if !program.builtin_info_table.is_empty() {
            if let Err(e) = program.initialize_resolved_builtins() {
                return Err(anyhow::anyhow!(
                    "Failed to initialize resolved builtins: {}",
                    e
                ));
            }
        }

        // Ensure program artifacts survive binary round-tripping
        test_round_trip_serialization(&program)
            .map_err(|e| anyhow::anyhow!("Program serialization round-trip failed: {}", e))?;

        let program = Arc::new(program);

        let run_with_mode = |mode: ExecutionMode,
                             use_step_mode: bool,
                             host_responses_template: Option<BTreeMap<Value, Vec<Value>>>|
         -> Result<Result<Value>> {
            let mut vm = RegoVM::new();
            vm.set_execution_mode(mode);
            vm.set_step_mode(use_step_mode);
            vm.set_strict_builtin_errors(strict);
            vm.set_execution_timer_config(Some(default_vm_test_execution_timer_config()));

            if let Some(data_value) = processed_data.clone() {
                vm.set_data(data_value)?;
            }
            if let Some(input_value) = processed_input.clone() {
                vm.set_input(input_value);
            }

            if let Some(limit) = max_instructions {
                vm.set_max_instructions(limit);
            }

            if matches!(mode, ExecutionMode::RunToCompletion) {
                if let Some(responses) = host_responses_template.clone() {
                    vm.set_host_await_responses(responses);
                }
            }

            vm.load_program(program.clone());

            let mut response_map = host_responses_template.clone().map(|map| {
                map.into_iter()
                    .map(|(identifier, values)| (identifier, VecDeque::from(values)))
                    .collect::<BTreeMap<_, _>>()
            });

            let mut last_result = vm.execute().map_err(|e| anyhow::anyhow!("{}", e));

            loop {
                match vm.execution_state() {
                    ExecutionState::Completed { result } => {
                        return Ok(Ok(result.clone()));
                    }
                    ExecutionState::Error { error } => {
                        return Ok(Err(anyhow::anyhow!("{}", error)));
                    }
                    ExecutionState::Suspended { reason, .. } => {
                        if mode != ExecutionMode::Suspendable {
                            return Ok(Err(anyhow::anyhow!(
                                "Run-to-completion execution unexpectedly suspended: {:?}",
                                reason
                            )));
                        }

                        match reason {
                            SuspendReason::HostAwait {
                                dest, identifier, ..
                            } => {
                                let dest = *dest;
                                let identifier = identifier.clone();

                                let response = {
                                    let map = response_map.as_mut().ok_or_else(|| {
                                        anyhow::anyhow!(
                                            "{}",
                                            VmError::HostAwaitResponseMissing {
                                                dest,
                                                identifier: identifier.clone(),
                                                pc: 0,
                                            }
                                        )
                                    })?;

                                    let queue = map.get_mut(&identifier).ok_or_else(|| {
                                        anyhow::anyhow!(
                                            "{}",
                                            VmError::HostAwaitResponseMissing {
                                                dest,
                                                identifier: identifier.clone(),
                                                pc: 0,
                                            }
                                        )
                                    })?;

                                    let response = queue.pop_front().ok_or_else(|| {
                                        anyhow::anyhow!(
                                            "{}",
                                            VmError::HostAwaitResponseMissing {
                                                dest,
                                                identifier: identifier.clone(),
                                                pc: 0,
                                            }
                                        )
                                    })?;

                                    if queue.is_empty() {
                                        map.remove(&identifier);
                                    }

                                    response
                                };

                                last_result = vm
                                    .resume(Some(response))
                                    .map_err(|e| anyhow::anyhow!("{}", e));
                            }
                            SuspendReason::Step => {
                                if !use_step_mode {
                                    return Ok(Err(anyhow::anyhow!(
                                        "Suspendable execution unexpectedly suspended: {:?}",
                                        reason
                                    )));
                                }
                                last_result = vm.resume(None).map_err(|e| anyhow::anyhow!("{}", e));
                            }
                            _ => {
                                last_result = vm.resume(None).map_err(|e| anyhow::anyhow!("{}", e));
                            }
                        }
                    }
                    _ => match &last_result {
                        Ok(value) => return Ok(Ok(value.clone())),
                        Err(err) => return Ok(Err(anyhow::anyhow!("{}", err))),
                    },
                }
            }
        };

        let compare_results = |baseline_name: &str,
                               baseline: &Result<Value>,
                               other_name: &str,
                               other: &Result<Value>|
         -> Result<()> {
            match (baseline, other) {
                (Ok(expected), Ok(actual)) => {
                    if expected != actual {
                        return Err(anyhow::anyhow!(
                            "{} execution result {:?} differed from {} {:?}",
                            other_name,
                            actual,
                            baseline_name,
                            expected
                        ));
                    }
                    Ok(())
                }
                (Err(expected_err), Err(other_err)) => {
                    let expected_msg = expected_err.to_string();
                    let other_msg = other_err.to_string();
                    if expected_msg != other_msg {
                        return Err(anyhow::anyhow!(
                            "{} execution error '{}' differed from {} '{}'",
                            other_name,
                            other_msg,
                            baseline_name,
                            expected_msg
                        ));
                    }
                    Ok(())
                }
                (Ok(expected), Err(other_err)) => Err(anyhow::anyhow!(
                    "{} execution failed with '{}' while {} succeeded with {:?}",
                    other_name,
                    other_err,
                    baseline_name,
                    expected
                )),
                (Err(expected_err), Ok(actual)) => Err(anyhow::anyhow!(
                    "{} execution succeeded with {:?} while {} failed with '{}'",
                    other_name,
                    actual,
                    baseline_name,
                    expected_err
                )),
            }
        };

        let run_to_completion = run_with_mode(
            ExecutionMode::RunToCompletion,
            false,
            processed_host_responses_run_to_completion.clone(),
        )?;
        let suspendable = run_with_mode(
            ExecutionMode::Suspendable,
            false,
            processed_host_responses_suspendable.clone(),
        )?;
        let stepwise = run_with_mode(
            ExecutionMode::Suspendable,
            true,
            processed_host_responses_suspendable.clone(),
        )?;

        const HOST_AWAIT_RESPONSE_MISSING: &str = "HostAwait executed but no response provided";

        let ignore_run_to_completion = ignore_run_to_completion_hostawait_failure
            && matches!(
                &run_to_completion,
                Err(err) if err.to_string().contains(HOST_AWAIT_RESPONSE_MISSING)
            );

        if ignore_run_to_completion {
            compare_results("suspendable", &suspendable, "step-by-step", &stepwise)?;
            return suspendable;
        }

        compare_results(
            "run-to-completion",
            &run_to_completion,
            "suspendable",
            &suspendable,
        )?;
        compare_results(
            "run-to-completion",
            &run_to_completion,
            "step-by-step",
            &stepwise,
        )?;

        run_to_completion
    }

    fn run_vm_test_suite(file: &str) -> Result<()> {
        std::println!("Running VM test suite: {}", file);
        let yaml_content = fs::read_to_string(file)?;
        let test_suite: VmTestSuite = serde_yaml::from_str(&yaml_content)?;

        for test_case in test_suite.cases {
            std::println!("Running VM test case: {}", test_case.note);

            let instructions = test_case
                .instructions
                .iter()
                .map(|instruction_str| parse_instruction(instruction_str))
                .collect::<Result<Vec<_>>>()?;

            let ignore_hostawait_failure = test_case.ignore_run_to_completion_hostawait_failure;

            struct ModeExpectation<'a> {
                strict: bool,
                want_result: Option<&'a crate::Value>,
                want_error: Option<&'a String>,
            }

            let mut expectations = Vec::new();

            if test_case.want_result.is_some() || test_case.want_error.is_some() {
                expectations.push(ModeExpectation {
                    strict: false,
                    want_result: test_case.want_result.as_ref(),
                    want_error: test_case.want_error.as_ref(),
                });
            }

            if test_case.want_result_strict.is_some() || test_case.want_error_strict.is_some() {
                expectations.push(ModeExpectation {
                    strict: true,
                    want_result: test_case.want_result_strict.as_ref(),
                    want_error: test_case.want_error_strict.as_ref(),
                });
            }

            if expectations.is_empty() {
                panic!(
                    "Test case '{}' must specify expectations for at least one mode",
                    test_case.note
                );
            }

            for expectation in expectations {
                let mode_label = if expectation.strict {
                    "strict"
                } else {
                    "non-strict"
                };
                std::println!("  Mode: {}", mode_label);

                let execution_result = execute_vm_instructions(
                    instructions.clone(),
                    test_case.literals.clone(),
                    test_case.rule_infos.clone(),
                    test_case.rule_tree.clone(),
                    test_case.instruction_params.clone(),
                    test_case.data.clone(),
                    test_case.input.clone(),
                    test_case.max_instructions,
                    test_case.host_await_responses.clone(),
                    test_case.host_await_responses_run_to_completion.clone(),
                    test_case.host_await_responses_suspendable.clone(),
                    ignore_hostawait_failure,
                    expectation.strict,
                );

                if expectation.want_error.is_some() && expectation.want_result.is_some() {
                    panic!(
                        "Test case '{}' cannot specify both want_result and want_error for {} mode",
                        test_case.note, mode_label
                    );
                }

                if let Some(expected_error) = expectation.want_error {
                    match execution_result {
                        Err(e) => {
                            let error_msg = std::format!("{}", e);
                            if !error_msg.contains(expected_error) {
                                std::println!(
                                    "Test case '{}' failed ({} mode):",
                                    test_case.note,
                                    mode_label
                                );
                                std::println!("  Expected error containing: '{}'", expected_error);
                                std::println!("  Actual error: '{}'", error_msg);
                                panic!("VM test case failed: {}", test_case.note);
                            }
                        }
                        Ok(result) => {
                            std::println!(
                                "Test case '{}' failed ({} mode):",
                                test_case.note,
                                mode_label
                            );
                            std::println!("  Expected error containing: '{}'", expected_error);
                            std::println!("  But got successful result: {:?}", result);
                            panic!("VM test case failed: {}", test_case.note);
                        }
                    }
                } else if let Some(want_result) = expectation.want_result {
                    let expected_result = process_value(want_result)?;

                    let actual_result = match execution_result {
                        Ok(result) => result,
                        Err(e) => {
                            if std::format!("{}", e).contains("Assertion failed") {
                                Value::Undefined
                            } else {
                                return Err(e);
                            }
                        }
                    };

                    if actual_result != expected_result {
                        std::println!(
                            "Test case '{}' failed ({} mode):",
                            test_case.note,
                            mode_label
                        );
                        std::println!("  Expected: {:?}", expected_result);
                        std::println!("  Actual: {:?}", actual_result);
                        panic!("VM test case failed: {}", test_case.note);
                    }
                } else {
                    panic!(
                        "Test case '{}' must specify either want_result or want_error for {} mode",
                        test_case.note, mode_label
                    );
                }

                std::println!("  ✓ {} mode passed", mode_label);
            }

            std::println!("✓ Test case '{}' passed", test_case.note);
        }
        std::println!("✓ Test suite '{}' completed successfully", file);

        Ok(())
    }

    #[test_resources("tests/rvm/vm/suites/*.yaml")]
    fn run_vm_test_file(file: &str) {
        run_vm_test_suite(file).unwrap()
    }

    #[test_resources("tests/rvm/vm/suites/loops/*.yaml")]
    fn run_loop_test_file(file: &str) {
        run_vm_test_suite(file).unwrap()
    }
}