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

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.

Source

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

        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 8880682c63)

Solutions

  1. Restore the canonical definition verbatim: pub const MAX_FRAGMENT_BYTES: usize = MAX_FRAGMENT_TOKENS * 4;
  2. Or use the literal form 40_000, which routes through the error-217 equality check instead
  3. Keep any alternative expression out of this const; derive variants from it at use sites instead

Example fix

// before
pub const MAX_FRAGMENT_BYTES: usize = 4 * MAX_FRAGMENT_TOKENS; // operand order breaks the canonical regex

// after
pub const MAX_FRAGMENT_BYTES: usize = MAX_FRAGMENT_TOKENS * 4; // 40_000
Defensive patterns

Strategy: validation

Validate before calling

import re
from pathlib import Path

MULT = re.compile(
    r"pub const MAX_FRAGMENT_BYTES:\s*usize\s*=\s*MAX_FRAGMENT_TOKENS\s*\*\s*4"
)
LITERAL = re.compile(r"pub const MAX_FRAGMENT_BYTES:\s*usize\s*=\s*([0-9_]+)")


def byte_cap_shape_ok() -> bool:
    text = Path("crates/core/src/fragments.rs").read_text(encoding="utf-8")
    return MULT.search(text) is not None or LITERAL.search(text) is not None

Prevention

When it happens

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

Common situations: rustfmt-adjacent tooling or hand edits that reorder multiplication operands; refactors introducing platform-dependent byte caps; partially applied renames.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@8880682c63 (2026-08-16). Data as JSON: /api/errors/cbb7507d7a6cfc50. Report an issue: GitHub.