{"record":{"id":"cc3597e04cafbc49","repo":"Hmbown/CodeWhale","slug":"fragment-cap-missing-pattern","errorCode":null,"errorMessage":"fragment cap missing: {pattern}","messagePattern":"fragment cap missing: (.+?)","errorType":"exception","errorClass":"RuntimeContractError","httpStatus":null,"severity":"error","filePath":"scripts/check-runtime-contract-budget.py","lineNumber":452,"sourceCode":"\ndef check_fragment_caps() -> None:\n    \"\"\"Gate the bounded fragment hard caps (issue #5264).\n\n    Static check — no cargo needed. Fails closed if the fragment module is\n    missing, if any cap has been raised without review, or if the\n    project-instruction import is absent.\n    \"\"\"\n    try:\n        text = FRAGMENT_MODULE.read_text(encoding=\"utf-8\")\n    except FileNotFoundError as error:\n        raise RuntimeContractError(\n            f\"missing bounded fragment module: {FRAGMENT_MODULE} ({error})\"\n        ) from error\n\n    def const_value(pattern: str) -> int:\n        match = re.search(pattern, text)\n        if not match:\n            raise RuntimeContractError(f\"fragment cap missing: {pattern}\")\n        try:\n            return int(match.group(1).replace(\"_\", \"\"))\n        except ValueError as error:\n            raise RuntimeContractError(f\"fragment cap not an int: {pattern}\") from error\n\n    max_tokens = const_value(r\"pub const MAX_FRAGMENT_TOKENS:\\s*usize\\s*=\\s*([0-9_]+)\")\n    if max_tokens != FRAGMENT_MAX_TOKENS_CEILING:\n        raise RuntimeContractError(\n            f\"MAX_FRAGMENT_TOKENS must be {FRAGMENT_MAX_TOKENS_CEILING}, got {max_tokens}\"\n        )\n    # MAX_FRAGMENT_BYTES must be defined as MAX_FRAGMENT_TOKENS * 4 (canonical)\n    # or as a literal 40000. Either way the derived ceiling is 40_000.\n    has_multiplication = re.search(\n        r\"pub const MAX_FRAGMENT_BYTES:\\s*usize\\s*=\\s*MAX_FRAGMENT_TOKENS\\s*\\*\\s*4\", text\n    )\n    bytes_literal = re.search(\n        r\"pub const MAX_FRAGMENT_BYTES:\\s*usize\\s*=\\s*([0-9_]+)\", text\n    )","sourceCodeStart":434,"sourceCodeEnd":470,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/8880682c63083a91624de936797efa3ce9e498fd/scripts/check-runtime-contract-budget.py#L434-L470","documentation":"Inside check_fragment_caps(), the helper const_value() searches fragments.rs for the exact declaration pattern `pub const MAX_FRAGMENT_TOKENS: usize = <digits>` or `pub const MAX_FRAGMENTS_PER_CONTEXT: usize = <digits>` and raises this error when the regex finds nothing. The pattern (included verbatim in the message) encodes name, visibility, type, and literal form, so renames, type changes, moves, or expression-valued definitions all fail closed.","triggerScenarios":"Renaming the constant; changing the type away from usize; defining it as an expression (for example MAX_FRAGMENT_TOKENS: usize = 10 * 1000) which the ([0-9_]+) group cannot match; moving the const into another module while leaving fragments.rs present but cap-less.","commonSituations":"Refactors that centralize constants into a config module; well-meaning cleanups that switch literals to expressions; partial moves during a crate split.","solutions":["Restore a plain literal declaration in crates/core/src/fragments.rs: pub const MAX_FRAGMENT_TOKENS: usize = 10_000; and pub const MAX_FRAGMENTS_PER_CONTEXT: usize = 16;","If the constant must live elsewhere, re-export it verbatim from fragments.rs so the pattern still matches there, or update the regexes in check_fragment_caps in the same reviewed change","Avoid expression-valued definitions for these two constants; the checker only accepts digit literals (with underscores)"],"exampleFix":"// before (crates/core/src/fragments.rs)\npub const MAX_FRAGMENT_TOKENS: usize = 10 * 1000;\n\n// after\npub const MAX_FRAGMENT_TOKENS: usize = 10_000;","handlingStrategy":"validation","validationCode":"import re\nfrom pathlib import Path\n\nCAP_PATTERNS = (\n    r\"pub const MAX_FRAGMENT_TOKENS:\\s*usize\\s*=\\s*([0-9_]+)\",\n    r\"pub const MAX_FRAGMENTS_PER_CONTEXT:\\s*usize\\s*=\\s*([0-9_]+)\",\n)\n\n\ndef cap_declarations_present() -> bool:\n    text = Path(\"crates/core/src/fragments.rs\").read_text(encoding=\"utf-8\")\n    return all(re.search(p, text) for p in CAP_PATTERNS)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep the two capped constants as plain digit literals with underscores in fragments.rs","Avoid expression-valued or cfg-dependent definitions for capped constants","If constants move, update the checker's regexes in the same reviewed change"],"tags":["python","regex","rust","static-analysis"],"backgroundTag":null,"analyzedSha":"8880682c63083a91624de936797efa3ce9e498fd","analyzedAt":"2026-08-16T11:31:27.956Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}