Hmbown/CodeWhale · error · RuntimeContractError

fragment cap missing

Error message

fragment cap missing: {pattern}

What it means

Inside check_fragment_caps, const_value(pattern) regex-searches the fragment module text for a `pub const ...: usize = N;` declaration. If the pattern does not match (the constant was renamed, reformatted, or removed), RuntimeContractError is raised with the pattern. The checker must find every expected cap constant before it can compare values against ceilings.

Solutions

  1. Restore the declaration to the canonical form, e.g. `pub const MAX_FRAGMENT_TOKENS: usize = 10_000;`.
  2. If the constant was renamed, update the corresponding regex in check_fragment_caps.
  3. Keep the value a plain integer literal (underscores allowed) so the regex and int() parse succeed.
  4. Run the checker after any edit to the fragment module to catch drift early.

Example fix

// before
pub const MAX_FRAGMENT_TOKENS: u32 = 10_000;
// after
pub const MAX_FRAGMENT_TOKENS: usize = 10_000;
Defensive patterns

Strategy: validation

Validate before calling

import re
text = open("crates/core/src/bounded_fragments.rs").read()
for name in ("MAX_FRAGMENT_TOKENS", "MAX_FRAGMENT_BYTES"):
    assert re.search(rf"pub const {name}:\\s*usize\\s*=\\s*[0-9_]+", text), f"{name} missing or non-canonical"

Type guard

def declares_cap(text: str, name: str) -> bool:
    import re
    return re.search(rf"pub const {name}:\\s*usize\\s*=\\s*([0-9_]+)", text) is not None

Try / catch

try:
    value = parse_fragment_caps(text)
except CapMissingError as e:
    print(f"cap declaration missing: {e.pattern}", file=sys.stderr)
    sys.exit(1)

Prevention

When it happens

Trigger: The fragment module no longer declares MAX_FRAGMENT_TOKENS (or another searched constant) with the exact expected `pub const NAME: usize = <digits>;` spelling — renamed constant, changed type annotation, computed expression, or non-numeric literal.

Common situations: A Rust refactor renamed the constant or changed its type (e.g. to u32 or a computed const); formatting tools changed the declaration shape; the constant was replaced by a runtime value.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


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

Appendix: source

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

def check_fragment_caps() -> None:
    """Gate the bounded fragment hard caps (issue #5264).

    Static check — no cargo needed. Fails closed if the fragment module is
    missing, if any cap has been raised without review, or if the
    project-instruction import is absent.
    """
    try:
        text = FRAGMENT_MODULE.read_text(encoding="utf-8")
    except FileNotFoundError as error:
        raise RuntimeContractError(
            f"missing bounded fragment module: {FRAGMENT_MODULE} ({error})"
        ) from error

    def const_value(pattern: str) -> int:
        match = re.search(pattern, text)
        if not match:
            raise RuntimeContractError(f"fragment cap missing: {pattern}")
        try:
            return int(match.group(1).replace("_", ""))
        except ValueError as error:
            raise RuntimeContractError(f"fragment cap not an int: {pattern}") from error

    max_tokens = const_value(r"pub const MAX_FRAGMENT_TOKENS:\s*usize\s*=\s*([0-9_]+)")
    if max_tokens != FRAGMENT_MAX_TOKENS_CEILING:
        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
    )

View on GitHub (pinned to 433685b202)