{"record":{"id":"7ab02847eb200292","repo":"Hmbown/CodeWhale","slug":"max-fragment-tokens-must-be-fragment-max-tokens-ceiling-got","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":467,"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":449,"sourceCodeEnd":485,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/scripts/check-runtime-contract-budget.py#L449-L485","documentation":"check_fragment_caps enforces one-way ceilings on the bounded-fragment constants: MAX_FRAGMENT_TOKENS must equal FRAGMENT_MAX_TOKENS_CEILING (10_000). If the module's parsed value differs, RuntimeContractError reports the expected ceiling and the actual value. This prevents silently raising the KV-cache fragment budget without an explicit, reviewed change to both the Rust source and the checker ceiling.","triggerScenarios":"Running the checker after MAX_FRAGMENT_TOKENS in the fragment module was changed to any value other than 10_000 — an intentional budget increase, a merge that took a different branch's value, or a typo while editing the constant.","commonSituations":"Someone raised the token cap to fit a larger prompt without updating the ceiling; a branch merge picked up a divergent cap; a bad cherry-pick altered the constant.","solutions":["Set `pub const MAX_FRAGMENT_TOKENS: usize = 10_000;` in the fragment module to match the ceiling.","If the raise is intentional and reviewed, update FRAGMENT_MAX_TOKENS_CEILING in check-runtime-contract-budget.py and the MAX_FRAGMENT_BYTES derivation (tokens * 4) in the same change.","Check git history of the fragment module to find which change moved the cap and confirm it was approved.","Re-run the checker to confirm all remaining caps (MAX_FRAGMENT_BYTES) also match."],"exampleFix":"// before\npub const MAX_FRAGMENT_TOKENS: usize = 12_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()\nv = int(re.search(r\"pub const MAX_FRAGMENT_TOKENS:\\\\s*usize\\\\s*=\\\\s*([0-9_]+)\", text).group(1).replace(\"_\", \"\"))\nassert v == 10_000, f\"MAX_FRAGMENT_TOKENS drifted to {v}; ceiling is 10_000\"","typeGuard":"def within_ceiling(tokens: int, ceiling: int = 10_000) -> bool:\n    return tokens == ceiling","tryCatchPattern":"try:\n    enforce_fragment_ceilings(text)\nexcept BudgetCeilingViolation as e:\n    print(f\"{e}; revert or update the ceiling with review\", file=sys.stderr)\n    sys.exit(1)","preventionTips":["Treat the ceiling as one-way: changes need a reviewed update to both Rust constant and FRAGMENT_MAX_TOKENS_CEILING.","Run the budget checker in CI to catch cap drift on every PR.","Check git blame on the constant when the checker fails to find the offending change."],"tags":["budget","contract-check","kv-cache","ceiling"],"backgroundTag":"value-out-of-range","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"}