{"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":459,"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":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/scripts/check-runtime-contract-budget.py#L441-L477","documentation":"Inside check_fragment_caps, const_value(pattern) regex-searches the fragment module text for a `pub const ...: usize = N;` declaration. If the pattern does not match (the constant was renamed, reformatted, or removed), RuntimeContractError is raised with the pattern. The checker must find every expected cap constant before it can compare values against ceilings.","triggerScenarios":"The fragment module no longer declares MAX_FRAGMENT_TOKENS (or another searched constant) with the exact expected `pub const NAME: usize = <digits>;` spelling — renamed constant, changed type annotation, computed expression, or non-numeric literal.","commonSituations":"A Rust refactor renamed the constant or changed its type (e.g. to u32 or a computed const); formatting tools changed the declaration shape; the constant was replaced by a runtime value.","solutions":["Restore the declaration to the canonical form, e.g. `pub const MAX_FRAGMENT_TOKENS: usize = 10_000;`.","If the constant was renamed, update the corresponding regex in check_fragment_caps.","Keep the value a plain integer literal (underscores allowed) so the regex and int() parse succeed.","Run the checker after any edit to the fragment module to catch drift early."],"exampleFix":"// before\npub const MAX_FRAGMENT_TOKENS: u32 = 10_000;\n// after\npub const MAX_FRAGMENT_TOKENS: usize = 10_000;","handlingStrategy":"validation","validationCode":"import re\ntext = open(\"crates/core/src/bounded_fragments.rs\").read()\nfor name in (\"MAX_FRAGMENT_TOKENS\", \"MAX_FRAGMENT_BYTES\"):\n    assert re.search(rf\"pub const {name}:\\\\s*usize\\\\s*=\\\\s*[0-9_]+\", text), f\"{name} missing or non-canonical\"","typeGuard":"def declares_cap(text: str, name: str) -> bool:\n    import re\n    return re.search(rf\"pub const {name}:\\\\s*usize\\\\s*=\\\\s*([0-9_]+)\", text) is not None","tryCatchPattern":"try:\n    value = parse_fragment_caps(text)\nexcept CapMissingError as e:\n    print(f\"cap declaration missing: {e.pattern}\", file=sys.stderr)\n    sys.exit(1)","preventionTips":["Keep cap constants as `pub const NAME: usize = <int literal>;` exactly.","Update the checker's regexes whenever renaming Rust constants.","Run the budget checker as a post-edit hook on the fragment module."],"tags":["regex","contract-check","rust","constant-missing"],"backgroundTag":"missing-required-config-field","analyzedSha":"433685b2024e7bc4c99e1e2e326bcad39b4d9d65","analyzedAt":"2026-09-15T12:24:24.634Z","contentChangedAt":"2026-09-15T12:24:24.634Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}