{"record":{"id":"b6bbd72ef57804b5","repo":"BoundaryML/baml","slug":"validated-int-sort-value-should-be-int","errorCode":null,"errorMessage":"validated int sort value should be int","messagePattern":"validated int sort value should be int","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_vm/src/package_baml/root.rs","lineNumber":70,"sourceCode":"            };\n            if values.len() <= 1 {\n                return NativeCallResult::Done(*arr);\n            }\n            match validate_natural_order_with_vm(vm, \"sort\", &values) {\n                Ok(domain) => domain,\n                Err(e) => return NativeCallResult::Error(e),\n            }\n        };\n        if matches!(domain, NaturalDomain::Int) {\n            // Int values are tag-only (no heap reads), so the comparator\n            // needs no `&BexVm` and the backing Vec sorts truly in place.\n            let mut values = match vm.as_array_mut(arr) {\n                Ok(guard) => guard,\n                Err(e) => return NativeCallResult::Error(e.into()),\n            };\n            values.sort_by(|left, right| {\n                left.as_int()\n                    .expect(\"validated int sort value should be int\")\n                    .cmp(\n                        &right\n                            .as_int()\n                            .expect(\"validated int sort value should be int\"),\n                    )\n            });\n            return NativeCallResult::Done(*arr);\n        }\n        // Heap-read domains (float/bigint/string): the comparator needs\n        // `&BexVm`, which conflicts with holding the array's mutable guard —\n        // sort a snapshot of the backing Vec, then swap it back in. One Vec\n        // (re)allocation; still no per-element BAML round trips or re-push.\n        let mut values = match vm.as_array(arr) {\n            Ok(guard) => guard.to_vec(),\n            Err(e) => return NativeCallResult::Error(e.into()),\n        };\n        values.sort_by(|left, right| compare_natural_values(vm, domain, *left, *right));\n        match vm.as_array_mut(arr) {","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_vm/src/package_baml/root.rs#L52-L88","documentation":"Panic inside the native `sort` implementation for `int[]` when a comparator encounters an element that is not an int. The array was supposed to be validated as an int array before reaching the native path, so a non-int element means an upstream invariant was broken (e.g. the array contents changed after validation).","triggerScenarios":"Calling sort on an `int[]` where an element's underlying tag is not an int at sort time — typically via mutation of the array between validation and sorting, or a VM bug bypassing element validation.","commonSituations":"Mutating a shared array (from another thread, alias, or callback) while sorting; type-checker gaps that let a mixed-type array be labeled int[]; VM bugs.","solutions":["Verify the array is not mutated concurrently or aliased while being sorted.","Re-check how the array was constructed; rebuild it with only int elements before sorting.","Report to maintainers if inputs are statically int[] — this is an internal invariant break, not a user error."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// before sort\nif (!arr.every(v => typeof v === 'number' && Number.isInteger(v))) throw new Error('sort: array must contain only ints');","typeGuard":"const isIntArray = (a) => Array.isArray(a) && a.every(v => Number.isInteger(v));","tryCatchPattern":null,"preventionTips":["Never mutate an array while sorting it.","Validate element types at array construction time.","Avoid aliasing arrays across concurrent tasks."],"tags":["vm-panic","invariant","sort","type-safety"],"backgroundTag":"internal-invariant-violation","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}