Hmbown/CodeWhale · error · RuntimeContractError

MAX_FRAGMENTS_PER_CONTEXT must be

Error message

MAX_FRAGMENTS_PER_CONTEXT must be {FRAGMENT_MAX_COUNT_CEILING}, got {max_count}

What it means

MAX_FRAGMENTS_PER_CONTEXT is pinned to exactly 16 (FRAGMENT_MAX_COUNT_CEILING). The checker parses its literal from fragments.rs and fails if it differs. This cap bounds how many typed fragments can be injected into one context, protecting the prompt budget from unbounded growth.

Solutions

  1. Set the literal in crates/core/src/fragments.rs back to 16
  2. If more fragments are genuinely needed, raise FRAGMENT_MAX_COUNT_CEILING in scripts/check-runtime-contract-budget.py as a reviewed budget change and align the constant
  3. Reduce the number of injected fragments (dedupe types in FragmentId::all()) instead of raising the cap

Example fix

// before
pub const MAX_FRAGMENTS_PER_CONTEXT: usize = 24;
// after
pub const MAX_FRAGMENTS_PER_CONTEXT: usize = 16;
Defensive patterns

Strategy: validation

Validate before calling

import re
src = open('crates/core/src/fragments.rs').read()
m = re.search(r'pub const MAX_FRAGMENTS_PER_CONTEXT:\s*usize\s*=\s*([0-9_]+)', src)
if m and int(m.group(1).replace('_','')) != 16:
    raise SystemExit('MAX_FRAGMENTS_PER_CONTEXT must be 16')

Prevention

When it happens

Trigger: Running scripts/check-runtime-contract-budget.py when fragments.rs contains `pub const MAX_FRAGMENTS_PER_CONTEXT: usize = <literal>` with a value other than 16 — e.g. raised to 24 to add new fragment types.

Common situations: Adding a new fragment kind and bumping the cap instead of fitting the budget; merging a branch with a different cap; tuning the count for larger agent topologies.

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/97c1f088f738a724. Report an issue: GitHub.

Appendix: source

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

        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(
            f"MAX_FRAGMENTS_PER_CONTEXT ({max_count}) must not exceed {FRAGMENT_MAX_COUNT_CEILING}"
        )

    # Ensure every injection type is in FragmentId::all() and the
    # project-instruction import is present as a typed fragment.
    required_fragments = [
        "Workspace",
        "Permissions",
        "Route",
        "AgentTopology",
        "SkillsTools",
        "TokenBudget",
        "ProjectInstructions",
        "Constitution",

View on GitHub (pinned to 433685b202)