{"record":{"id":"cbb7507d7a6cfc50","repo":"Hmbown/CodeWhale","slug":"max-fragment-bytes-must-be-defined-as-max-fragment-tokens-4","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":485,"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":467,"sourceCodeEnd":503,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/scripts/check-runtime-contract-budget.py#L467-L503","documentation":"The checker accepts MAX_FRAGMENT_BYTES only in two canonical spellings: an arithmetic definition `MAX_FRAGMENT_TOKENS * 4` or a plain numeric literal. When no literal is present AND the multiplication form does not match the expected regex, the gate cannot confirm the cap is 40,000, so it fails closed. This blocks obfuscated or renamed definitions that would evade the budget contract.","triggerScenarios":"Running the check when fragments.rs defines MAX_FRAGMENT_BYTES via an expression other than `MAX_FRAGMENT_TOKENS * 4` (e.g. `MAX_FRAGMENT_TOKENS << 2`, a computed expression, a different multiplier, or the const is commented out/renamed) with no matching numeric literal.","commonSituations":"Refactoring the constant to `MAX_FRAGMENT_TOKENS * BYTES_PER_TOKEN`; reformatting the line so the regex no longer matches; deleting the constant during a rework of the fragment module.","solutions":["Write the constant exactly as `pub const MAX_FRAGMENT_BYTES: usize = MAX_FRAGMENT_TOKENS * 4;` in crates/core/src/fragments.rs","Or use the literal form `pub const MAX_FRAGMENT_BYTES: usize = 40000;`","Check for reformatting/renaming that broke the regex — the const name and `* 4` must appear verbatim on the definition line"],"exampleFix":"// before\npub const MAX_FRAGMENT_BYTES: usize = MAX_FRAGMENT_TOKENS << 2;\n// after\npub const MAX_FRAGMENT_BYTES: usize = MAX_FRAGMENT_TOKENS * 4;","handlingStrategy":"validation","validationCode":"import re\nsrc = open('crates/core/src/fragments.rs').read()\nif not re.search(r'pub const MAX_FRAGMENT_BYTES:\\s*usize\\s*=\\s*MAX_FRAGMENT_TOKENS\\s*\\*\\s*4', src) and not re.search(r'pub const MAX_FRAGMENT_BYTES:\\s*usize\\s*=\\s*([0-9_]+)', src):\n    raise SystemExit('MAX_FRAGMENT_BYTES not in a recognized form')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep the definition on one line in the exact `MAX_FRAGMENT_TOKENS * 4` spelling","Avoid reformatting tools that split or rewrite const definition lines","Document in the module that cap constants are contract-checked verbatim"],"tags":["static-check","rust","config","ci"],"backgroundTag":"invalid-config-value","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"}