{"record":{"id":"427e38f20a8260ef","repo":"Hmbown/CodeWhale","slug":"max-fragment-bytes-must-be-fragment-max-bytes-cei","errorCode":null,"errorMessage":"MAX_FRAGMENT_BYTES must be {FRAGMENT_MAX_BYTES_CEILING}, got {literal}","messagePattern":"MAX_FRAGMENT_BYTES must be (.+?), got (.+?)","errorType":"exception","errorClass":"RuntimeContractError","httpStatus":null,"severity":"error","filePath":"scripts/check-runtime-contract-budget.py","lineNumber":474,"sourceCode":"            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    )\n    if bytes_literal:\n        literal = int(bytes_literal.group(1).replace(\"_\", \"\"))\n        if literal != FRAGMENT_MAX_BYTES_CEILING:\n            raise RuntimeContractError(\n                f\"MAX_FRAGMENT_BYTES must be {FRAGMENT_MAX_BYTES_CEILING}, got {literal}\"\n            )\n    elif not has_multiplication:\n        raise RuntimeContractError(\n            \"MAX_FRAGMENT_BYTES must be defined as MAX_FRAGMENT_TOKENS * 4 or as 40000\"\n        )\n    # DEFAULT is defined as 4 * 1024 (canonical) or 4096 literal\n    has_default_multiplication = re.search(\n        r\"pub const DEFAULT_FRAGMENT_MAX_BYTES:\\s*usize\\s*=\\s*4\\s*\\*\\s*1024\", text\n    )\n    default_literal = re.search(\n        r\"pub const DEFAULT_FRAGMENT_MAX_BYTES:\\s*usize\\s*=\\s*([0-9_]+)\", text\n    )\n    if has_default_multiplication:\n        # canonical 4*1024 == 4096, which equals ceiling\n        pass\n    elif default_literal:\n        default_bytes = int(default_literal.group(1).replace(\"_\", \"\"))","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/8880682c63083a91624de936797efa3ce9e498fd/scripts/check-runtime-contract-budget.py#L456-L492","documentation":"When MAX_FRAGMENT_BYTES is defined as a numeric literal (rather than the canonical MAX_FRAGMENT_TOKENS * 4 form), check_fragment_caps() requires the literal to equal FRAGMENT_MAX_BYTES_CEILING = 40_000. The byte cap is the hard clamp enforced by enforce_byte_cap, so a literal drift here changes runtime truncation behavior without any review.","triggerScenarios":"Editing pub const MAX_FRAGMENT_BYTES: usize = 40_000; to any other number - for example 80_000 while debugging truncation, or 40_001 by typo. Note that both the literal form and the multiplication form are accepted, but the literal must be exactly 40_000.","commonSituations":"Debugging a fragment-truncation bug by raising the byte cap locally and committing it; codemods converting the multiplication form to a literal with a wrong value.","solutions":["Prefer the canonical form: pub const MAX_FRAGMENT_BYTES: usize = MAX_FRAGMENT_TOKENS * 4;","Or restore the exact literal 40_000 (or 40_000 with underscores)","If a raise is intentional, make it an explicit reviewed change that also updates FRAGMENT_MAX_BYTES_CEILING in the checker"],"exampleFix":"// before (crates/core/src/fragments.rs)\npub const MAX_FRAGMENT_BYTES: usize = 80_000;\n\n// after - canonical form\npub const MAX_FRAGMENT_BYTES: usize = MAX_FRAGMENT_TOKENS * 4; // 40_000","handlingStrategy":"validation","validationCode":"import re\nfrom pathlib import Path\n\nEXPECTED_MAX_FRAGMENT_BYTES = 40_000\nCANONICAL = re.compile(\n    r\"pub const MAX_FRAGMENT_BYTES:\\s*usize\\s*=\\s*MAX_FRAGMENT_TOKENS\\s*\\*\\s*4\"\n)\nLITERAL = re.compile(r\"pub const MAX_FRAGMENT_BYTES:\\s*usize\\s*=\\s*([0-9_]+)\")\n\n\ndef byte_cap_ok() -> bool:\n    text = Path(\"crates/core/src/fragments.rs\").read_text(encoding=\"utf-8\")\n    if CANONICAL.search(text):\n        return True\n    m = LITERAL.search(text)\n    return m is not None and int(m.group(1).replace(\"_\", \"\")) == EXPECTED_MAX_FRAGMENT_BYTES","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer the canonical MAX_FRAGMENT_TOKENS * 4 form so the byte cap tracks the token cap automatically","If you use a literal, keep it exactly 40_000","Revert debugging raises before committing; run this gate locally to catch leftovers"],"tags":["rust","constants","budget","static-analysis"],"backgroundTag":null,"analyzedSha":"8880682c63083a91624de936797efa3ce9e498fd","analyzedAt":"2026-08-16T11:31:27.956Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}