{"record":{"id":"7ab02847eb200292","repo":"Hmbown/CodeWhale","slug":"max-fragment-tokens-must-be-fragment-max-tokens-c","errorCode":null,"errorMessage":"MAX_FRAGMENT_TOKENS must be {FRAGMENT_MAX_TOKENS_CEILING}, got {max_tokens}","messagePattern":"MAX_FRAGMENT_TOKENS must be (.+?), got (.+?)","errorType":"exception","errorClass":"RuntimeContractError","httpStatus":null,"severity":"error","filePath":"scripts/check-runtime-contract-budget.py","lineNumber":460,"sourceCode":"    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    )\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(","sourceCodeStart":442,"sourceCodeEnd":478,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/8880682c63083a91624de936797efa3ce9e498fd/scripts/check-runtime-contract-budget.py#L442-L478","documentation":"check_fragment_caps() parses MAX_FRAGMENT_TOKENS out of crates/core/src/fragments.rs and requires it to equal FRAGMENT_MAX_TOKENS_CEILING = 10_000 exactly. Raising (or lowering) the token ceiling is a reviewed maintainer decision, so the gate fails closed on any unilateral change; the message reports both the expected and actual values.","triggerScenarios":"A PR edits pub const MAX_FRAGMENT_TOKENS: usize to anything other than 10_000 - for example 20_000 to accommodate larger fragments. Downstream, MAX_FRAGMENT_BYTES (defined as MAX_FRAGMENT_TOKENS * 4) would silently double, which is precisely what this gate prevents.","commonSituations":"Developers bumping the cap to make a large context fragment fit during local testing and committing it accidentally; rebasing over a branch that carried a temporary raise.","solutions":["Restore pub const MAX_FRAGMENT_TOKENS: usize = 10_000; in crates/core/src/fragments.rs","If a raise is genuinely needed, open it as an explicit decision: change the cap and FRAGMENT_MAX_TOKENS_CEILING together with review, and note the rationale (mirroring the budget _comment convention)","Shrink the oversized fragment (chunking/truncation via enforce_byte_cap) instead of raising the ceiling"],"exampleFix":"// before (crates/core/src/fragments.rs)\npub const MAX_FRAGMENT_TOKENS: usize = 20_000;\n\n// after\npub const MAX_FRAGMENT_TOKENS: usize = 10_000;","handlingStrategy":"validation","validationCode":"import re\nfrom pathlib import Path\n\nEXPECTED_MAX_FRAGMENT_TOKENS = 10_000\n\n\ndef token_cap_ok() -> bool:\n    text = Path(\"crates/core/src/fragments.rs\").read_text(encoding=\"utf-8\")\n    m = re.search(r\"pub const MAX_FRAGMENT_TOKENS:\\s*usize\\s*=\\s*([0-9_]+)\", text)\n    if m is None:\n        return False\n    return int(m.group(1).replace(\"_\", \"\")) == EXPECTED_MAX_FRAGMENT_TOKENS","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not raise the token cap to fit content - shrink or chunk the fragment instead","Cap changes are maintainer decisions: pair any change with the checker's ceiling constant and a written rationale","Watch rebases that reintroduce a temporary local raise of 10_000"],"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"}