Hmbown/CodeWhale · error · RuntimeContractError

DEFAULT_FRAGMENT_MAX_BYTES must be {FRAGMENT_DEFAULT_MAX_BYT

Error message

DEFAULT_FRAGMENT_MAX_BYTES must be {FRAGMENT_DEFAULT_MAX_BYTES_CEILING}, got {default_bytes}

What it means

check_fragment_caps() validates DEFAULT_FRAGMENT_MAX_BYTES (the default per-fragment cap applied by Fragment::new) in one of two shapes: the canonical `4 * 1024` multiplication, or a digit literal that must equal FRAGMENT_DEFAULT_MAX_BYTES_CEILING = 4096. This error fires for the literal form with any other value; the default must stay at 4 KiB so typical fragments stay well under the 40_000 hard cap.

Source

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

            )
    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}"
            )
        if default_bytes > FRAGMENT_MAX_BYTES_CEILING:
            raise RuntimeContractError(
                f"DEFAULT_FRAGMENT_MAX_BYTES ({default_bytes}) must not exceed MAX_FRAGMENT_BYTES ({FRAGMENT_MAX_BYTES_CEILING})"
            )
    else:
        raise RuntimeContractError("DEFAULT_FRAGMENT_MAX_BYTES definition not found")

    max_count = const_value(
        r"pub const MAX_FRAGMENTS_PER_CONTEXT:\s*usize\s*=\s*([0-9_]+)"
    )
    if max_count != FRAGMENT_MAX_COUNT_CEILING:
        raise RuntimeContractError(
            f"MAX_FRAGMENTS_PER_CONTEXT must be {FRAGMENT_MAX_COUNT_CEILING}, got {max_count}"
        )
    if max_count > FRAGMENT_MAX_COUNT_CEILING:
        raise RuntimeContractError(

View on GitHub (pinned to 8880682c63)

Solutions

  1. Prefer the canonical form: pub const DEFAULT_FRAGMENT_MAX_BYTES: usize = 4 * 1024;
  2. Or restore the exact literal 4096
  3. If a larger default is truly needed, raise it as an explicit reviewed change together with FRAGMENT_DEFAULT_MAX_BYTES_CEILING in the checker, keeping it under MAX_FRAGMENT_BYTES

Example fix

// before (crates/core/src/fragments.rs)
pub const DEFAULT_FRAGMENT_MAX_BYTES: usize = 8_192;

// after
pub const DEFAULT_FRAGMENT_MAX_BYTES: usize = 4 * 1024;
Defensive patterns

Strategy: validation

Validate before calling

import re
from pathlib import Path

CANONICAL_DEFAULT = re.compile(
    r"pub const DEFAULT_FRAGMENT_MAX_BYTES:\s*usize\s*=\s*4\s*\*\s*1024"
)
DEFAULT_LITERAL = re.compile(
    r"pub const DEFAULT_FRAGMENT_MAX_BYTES:\s*usize\s*=\s*([0-9_]+)"
)


def default_cap_ok() -> bool:
    text = Path("crates/core/src/fragments.rs").read_text(encoding="utf-8")
    if CANONICAL_DEFAULT.search(text):
        return True
    m = DEFAULT_LITERAL.search(text)
    return m is not None and int(m.group(1).replace("_", "")) == 4096

Prevention

When it happens

Trigger: Editing pub const DEFAULT_FRAGMENT_MAX_BYTES: usize = 4096; to another literal such as 8_192 to make bigger default fragments fit. The multiplication form `4 * 1024` short-circuits as canonical and is not numerically re-checked; the literal form is.

Common situations: Raising the default during local development of large-file ingestion features; codemods normalizing `4 * 1024` to a literal with a wrong value.

Related errors


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