{"record":{"id":"95297f8b128374da","repo":"AlexsJones/llmfit","slug":"valid-mlx-suffix-regex","errorCode":null,"errorMessage":"valid MLX suffix regex","messagePattern":"valid MLX suffix regex","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"llmfit-core/src/providers.rs","lineNumber":3774,"sourceCode":"            // part of the canonical model name, so strip it too — otherwise the\n            // stem never reduces to the catalog id and the file reads as neither\n            // installed nor served.\n            let base = base.strip_suffix(\"-ud\").unwrap_or(base);\n            return Some(base.to_string());\n        }\n    }\n    None\n}\n\n/// Strip an MLX quantization suffix from a lowercased model stem, so\n/// mlx-community basenames reduce to catalog slugs (#854, #869).\n/// \"llama-3.2-1b-instruct-4bit\" → \"llama-3.2-1b-instruct\"\n///\n/// End-anchored, unlike the GGUF list above: mlx-community always places the\n/// quant scheme last, and dtype-like fragments can occur inside genuine model\n/// names. Strips, in order: the quant scheme (`-<N>bit` or `-mxfp4`, each\n/// with optional trailing variant markers such as `-4bit-dwq`, date-stamped\n/// `-4bit-dwq-05082025`, `-mxfp4-q8`, `-mxfp4-bf16`, or a plain `-fp16`),\n/// then a trailing `-mlx` marker (`...-instruct-mlx-8bit` → `...-instruct`).\n///\n/// Returns `None` when nothing was stripped, so lookup callers can tell \"no\n/// MLX suffix present\" apart from \"already reduced\".\npub fn strip_mlx_quant_suffix(stem: &str) -> Option<String> {\n    static MLX_QUANT_SUFFIX: OnceLock<Regex> = OnceLock::new();\n    let re = MLX_QUANT_SUFFIX.get_or_init(|| {\n        Regex::new(r\"-(?:\\d+bit(?:-[a-z0-9]+)*|mxfp4(?:-[a-z0-9]+)*|fp16)$\")\n            .expect(\"valid MLX suffix regex\")\n    });\n    let without_quant = match re.find(stem) {\n        Some(m) if m.start() > 0 => &stem[..m.start()],\n        _ => stem,\n    };\n    let base = match without_quant.strip_suffix(\"-mlx\") {\n        Some(b) if !b.is_empty() => b,\n        _ => without_quant,\n    };","sourceCodeStart":3756,"sourceCodeEnd":3792,"githubUrl":"https://github.com/AlexsJones/llmfit/blob/1e7bdb3ecf43071597ffd2eb2305dfac35e22a40/llmfit-core/src/providers.rs#L3756-L3792","documentation":"An .expect() panic string in strip_mlx_quant_suffix (llmfit-core/src/providers.rs). OnceLock lazily compiles the hardcoded regex -\\d+bit(?:-[a-z0-9]+)*$; Regex::new only fails on an invalid pattern, and this literal is a fixed, tested constant. The expect can therefore only fire if someone edits the pattern into an invalid regex — it is an internal-invariant assertion (per repo convention), not a runtime error users can trigger.","triggerScenarios":"A contributor changes the pattern string (e.g. introduces an unescaped '(' or mismatched brace) and the first call to strip_mlx_quant_suffix for a stem without the -mxfp4/-fp16 suffixes panics at regex compile time.","commonSituations":"Editing the MLX suffix rules without running the unit tests that exercise this function; cargo test suites that cover providers.rs would catch it before merge.","solutions":["Revert or fix the regex literal in providers.rs to a valid pattern (e.g. the original r\"-\\d+bit(?:-[a-z0-9]+)*$\")","Run `cargo test -p llmfit-core providers` after touching the pattern to catch compile-time regex breakage in CI"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// CI/test guard: fail early if the MLX suffix pattern is edited into an invalid regex\n#[test]\nfn mlx_suffix_regex_compiles() {\n    assert!(regex::Regex::new(r\"-\\d+bit(?:-[a-z0-9]+)*$\").is_ok());\n    assert_eq!(strip_mlx_quant_suffix(\"qwen3-4bit\").as_deref(), Some(\"qwen3\"));\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never edit the pattern literal without running the providers unit tests","Keep expect() messages tied to compile-time constants so reviewers see they are invariants","Add a compile-check test for every OnceLock<Regex> in the codebase"],"tags":["rust","regex","internal-invariant","panic"],"backgroundTag":"regex-compilation-failed","analyzedSha":"1e7bdb3ecf43071597ffd2eb2305dfac35e22a40","analyzedAt":"2026-08-17T10:35:29.658Z","contentChangedAt":"2026-08-17T10:35:29.658Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}