Hmbown/CodeWhale · error · RuntimeContractError

MAX_FRAGMENT_BYTES must be defined as MAX_FRAGMENT_TOKENS *…

Error message

MAX_FRAGMENT_BYTES must be defined as MAX_FRAGMENT_TOKENS * 4 or as 40000

What it means

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.

Solutions

  1. Write the constant exactly as `pub const MAX_FRAGMENT_BYTES: usize = MAX_FRAGMENT_TOKENS * 4;` in crates/core/src/fragments.rs
  2. Or use the literal form `pub const MAX_FRAGMENT_BYTES: usize = 40000;`
  3. Check for reformatting/renaming that broke the regex — the const name and `* 4` must appear verbatim on the definition line

Example fix

// before
pub const MAX_FRAGMENT_BYTES: usize = MAX_FRAGMENT_TOKENS << 2;
// after
pub const MAX_FRAGMENT_BYTES: usize = MAX_FRAGMENT_TOKENS * 4;
Defensive patterns

Strategy: validation

Validate before calling

import re
src = open('crates/core/src/fragments.rs').read()
if 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):
    raise SystemExit('MAX_FRAGMENT_BYTES not in a recognized form')

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@433685b202 (2026-09-15). Data as JSON: /api/errors/cbb7507d7a6cfc50. Report an issue: GitHub.

Appendix: source

Thrown at scripts/check-runtime-contract-budget.py:485

        raise RuntimeContractError(
            f"MAX_FRAGMENT_TOKENS must be {FRAGMENT_MAX_TOKENS_CEILING}, got {max_tokens}"
        )
    # MAX_FRAGMENT_BYTES must be defined as MAX_FRAGMENT_TOKENS * 4 (canonical)
    # or as a literal 40000. Either way the derived ceiling is 40_000.
    has_multiplication = re.search(
        r"pub const MAX_FRAGMENT_BYTES:\s*usize\s*=\s*MAX_FRAGMENT_TOKENS\s*\*\s*4", text
    )
    bytes_literal = re.search(
        r"pub const MAX_FRAGMENT_BYTES:\s*usize\s*=\s*([0-9_]+)", text
    )
    if bytes_literal:
        literal = int(bytes_literal.group(1).replace("_", ""))
        if literal != FRAGMENT_MAX_BYTES_CEILING:
            raise RuntimeContractError(
                f"MAX_FRAGMENT_BYTES must be {FRAGMENT_MAX_BYTES_CEILING}, got {literal}"
            )
    elif not has_multiplication:
        raise RuntimeContractError(
            "MAX_FRAGMENT_BYTES must be defined as MAX_FRAGMENT_TOKENS * 4 or as 40000"
        )
    # DEFAULT is defined as 4 * 1024 (canonical) or 4096 literal
    has_default_multiplication = re.search(
        r"pub const DEFAULT_FRAGMENT_MAX_BYTES:\s*usize\s*=\s*4\s*\*\s*1024", text
    )
    default_literal = re.search(
        r"pub const DEFAULT_FRAGMENT_MAX_BYTES:\s*usize\s*=\s*([0-9_]+)", text
    )
    if has_default_multiplication:
        # canonical 4*1024 == 4096, which equals ceiling
        pass
    elif default_literal:
        default_bytes = int(default_literal.group(1).replace("_", ""))
        if default_bytes != FRAGMENT_DEFAULT_MAX_BYTES_CEILING:
            raise RuntimeContractError(
                f"DEFAULT_FRAGMENT_MAX_BYTES must be {FRAGMENT_DEFAULT_MAX_BYTES_CEILING}, got {default_bytes}"
            )

View on GitHub (pinned to 433685b202)