{"record":{"id":"cbb7507d7a6cfc50","repo":"Hmbown/CodeWhale","slug":"max-fragment-bytes-must-be-defined-as-max-fragment","errorCode":null,"errorMessage":"MAX_FRAGMENT_BYTES must be defined as MAX_FRAGMENT_TOKENS * 4 or as 40000","messagePattern":"MAX_FRAGMENT_BYTES must be defined as MAX_FRAGMENT_TOKENS \\* 4 or as 40000","errorType":"exception","errorClass":"RuntimeContractError","httpStatus":null,"severity":"error","filePath":"scripts/check-runtime-contract-budget.py","lineNumber":478,"sourceCode":"        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(\"_\", \"\"))\n        if default_bytes != FRAGMENT_DEFAULT_MAX_BYTES_CEILING:\n            raise RuntimeContractError(\n                f\"DEFAULT_FRAGMENT_MAX_BYTES must be {FRAGMENT_DEFAULT_MAX_BYTES_CEILING}, got {default_bytes}\"\n            )","sourceCodeStart":460,"sourceCodeEnd":496,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/8880682c63083a91624de936797efa3ce9e498fd/scripts/check-runtime-contract-budget.py#L460-L496","documentation":"check_fragment_caps() accepts MAX_FRAGMENT_BYTES only in one of two shapes: the canonical multiplication `pub const MAX_FRAGMENT_BYTES: usize = MAX_FRAGMENT_TOKENS * 4` or a plain digit literal (checked against 40_000 by error 217). This error fires when neither regex matches - the constant is defined via some other expression, wrong operand order, a function call, or is missing entirely while still present textually elsewhere.","triggerScenarios":"Rewriting the definition as 4 * MAX_FRAGMENT_TOKENS (reversed operands do not match the canonical regex), MAX_FRAGMENT_TOKENS * 4usize, a const fn call, or a cfg-dependent expression. Also a renamed constant that leaves no match for either pattern.","commonSituations":"rustfmt-adjacent tooling or hand edits that reorder multiplication operands; refactors introducing platform-dependent byte caps; partially applied renames.","solutions":["Restore the canonical definition verbatim: pub const MAX_FRAGMENT_BYTES: usize = MAX_FRAGMENT_TOKENS * 4;","Or use the literal form 40_000, which routes through the error-217 equality check instead","Keep any alternative expression out of this const; derive variants from it at use sites instead"],"exampleFix":"// before\npub const MAX_FRAGMENT_BYTES: usize = 4 * MAX_FRAGMENT_TOKENS; // operand order breaks the canonical regex\n\n// after\npub const MAX_FRAGMENT_BYTES: usize = MAX_FRAGMENT_TOKENS * 4; // 40_000","handlingStrategy":"validation","validationCode":"import re\nfrom pathlib import Path\n\nMULT = 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_shape_ok() -> bool:\n    text = Path(\"crates/core/src/fragments.rs\").read_text(encoding=\"utf-8\")\n    return MULT.search(text) is not None or LITERAL.search(text) is not None","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep the multiplication operand order exactly MAX_FRAGMENT_TOKENS * 4","Do not introduce cfg/platform-dependent expressions for this const","When renaming constants, grep scripts/check-runtime-contract-budget.py for dependent regexes"],"tags":["rust","constants","regex","static-analysis"],"backgroundTag":null,"analyzedSha":"8880682c63083a91624de936797efa3ce9e498fd","analyzedAt":"2026-08-16T11:31:27.956Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}