{"record":{"id":"a33d091ea55f886b","repo":"Hmbown/CodeWhale","slug":"bounded-fragment-module-must-enforce-byte-caps-via","errorCode":null,"errorMessage":"bounded fragment module must enforce byte caps via enforce_byte_cap and MAX_FRAGMENT_BYTES","messagePattern":"bounded fragment module must enforce byte caps via enforce_byte_cap and MAX_FRAGMENT_BYTES","errorType":"exception","errorClass":"RuntimeContractError","httpStatus":null,"severity":"error","filePath":"scripts/check-runtime-contract-budget.py","lineNumber":582,"sourceCode":"    for candidate in required_candidates:\n        if candidate not in text:\n            raise RuntimeContractError(\n                f\"PROJECT_INSTRUCTION_CANDIDATES missing required entry {candidate!r}\"\n            )\n\n    # matches_text recognizer must exist on the fragment trait\n    if \"fn matches_text\" not in text:\n        raise RuntimeContractError(\n            \"bounded fragment module must define a matches_text recognizer on the fragment trait\"\n        )\n    if \"trait ContextFragment\" not in text:\n        raise RuntimeContractError(\n            \"bounded fragment module must define trait ContextFragment with matches_text\"\n        )\n\n    # No unbounded fragment — enforce that creation clamps to MAX_FRAGMENT_BYTES\n    if \"MAX_FRAGMENT_BYTES\" not in text or \"enforce_byte_cap\" not in text:\n        raise RuntimeContractError(\n            \"bounded fragment module must enforce byte caps via enforce_byte_cap and MAX_FRAGMENT_BYTES\"\n        )\n\n    # TUI must be unified with the core boundary (shared crates/core module)\n    tui_fragment = REPO_ROOT / \"crates\" / \"tui\" / \"src\" / \"model_context\" / \"fragment.rs\"\n    try:\n        tui_text = tui_fragment.read_text(encoding=\"utf-8\")\n    except FileNotFoundError as error:\n        raise RuntimeContractError(\n            f\"missing TUI fragment module: {tui_fragment} ({error})\"\n        ) from error\n    if \"codewhale_core::fragments\" not in tui_text:\n        raise RuntimeContractError(\n            \"TUI model_context/fragment.rs must re-export caps from codewhale_core::fragments (shared crates/core boundary)\"\n        )\n    if \"ProjectInstructions\" not in tui_text:\n        raise RuntimeContractError(\n            \"TUI fragment module must include ProjectInstructions variant (unified with core)\"","sourceCodeStart":564,"sourceCodeEnd":600,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/8880682c63083a91624de936797efa3ce9e498fd/scripts/check-runtime-contract-budget.py#L564-L600","documentation":"Enforces the 'no unbounded fragment' rule: fragments.rs must reference both MAX_FRAGMENT_BYTES and a clamp helper named enforce_byte_cap (fn enforce_byte_cap(raw, max_bytes) truncates fragment content at creation). Since MAX_FRAGMENT_BYTES is already validated earlier in the function, this practically fires when enforce_byte_cap is renamed, inlined away, or deleted so fragment construction no longer clamps.","triggerScenarios":"Inlining truncation at call sites with str::truncate; renaming the helper to clamp_bytes; removing the helper after switching to a different bounding strategy.","commonSituations":"Performance or cleanliness refactors of fragment constructors; unifying clamping into generic newtypes.","solutions":["Restore 'fn enforce_byte_cap(raw: String, max_bytes: usize) -> String' in crates/core/src/fragments.rs and route fragment construction through it","If the helper must be renamed or shared, keep a thin wrapper named enforce_byte_cap - the gate is a substring check","Verify: 'grep -n enforce_byte_cap crates/core/src/fragments.rs'"],"exampleFix":"// before (crates/core/src/fragments.rs)\nlet content = raw.truncate(max_bytes); // inlined clamp, helper deleted\n\n// after\nfn enforce_byte_cap(raw: String, max_bytes: usize) -> String { /* truncate */ }\nlet content = enforce_byte_cap(raw, MAX_FRAGMENT_BYTES);","handlingStrategy":"try-catch","validationCode":"from pathlib import Path\ntext = Path(\"crates/core/src/fragments.rs\").read_text(encoding=\"utf-8\")\nassert \"MAX_FRAGMENT_BYTES\" in text and \"enforce_byte_cap\" in text, \"byte-cap enforcement missing\"","typeGuard":"def is_runtime_contract_error(exc: BaseException) -> bool:\n    return isinstance(exc, ValueError) and type(exc).__name__ == \"RuntimeContractError\"","tryCatchPattern":"try:\n    check_fragment_caps()\nexcept RuntimeContractError as error:\n    print(f\"[gate] {error}\", file=sys.stderr)\n    raise SystemExit(2)","preventionTips":["Route every fragment constructor through enforce_byte_cap(raw, MAX_FRAGMENT_BYTES) - no inline truncation","If renaming/sharing the helper, keep a wrapper named enforce_byte_cap","Add a Rust test that oversized fragment content is clamped to 40_000 bytes"],"tags":["python","ci","static-analysis","rust","resource-limits"],"backgroundTag":null,"analyzedSha":"8880682c63083a91624de936797efa3ce9e498fd","analyzedAt":"2026-08-16T11:31:27.956Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}