{"record":{"id":"ade588857ed011ed","repo":"Hmbown/CodeWhale","slug":"tui-fragment-module-must-enforce-max-fragment-bytes-10k","errorCode":null,"errorMessage":"TUI fragment module must enforce MAX_FRAGMENT_BYTES (10K-token ceiling)","messagePattern":"TUI fragment module must enforce MAX_FRAGMENT_BYTES \\(10K-token ceiling\\)","errorType":"exception","errorClass":"RuntimeContractError","httpStatus":null,"severity":"error","filePath":"scripts/check-runtime-contract-budget.py","lineNumber":610,"sourceCode":"\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)\"\n        )\n    if \"MAX_FRAGMENT_BYTES\" not in tui_text:\n        raise RuntimeContractError(\n            \"TUI fragment module must enforce MAX_FRAGMENT_BYTES (10K-token ceiling)\"\n        )\n    if \"matches_text\" not in tui_text:\n        raise RuntimeContractError(\n            \"TUI fragment module must expose a matches_text recognizer\"\n        )\n\n\ndef main(argv: Sequence[str] | None = None) -> int:\n    parser = argparse.ArgumentParser(description=__doc__)\n    parser.add_argument(\n        \"--receipt\",\n        type=Path,\n        help=\"check an existing measurement JSON instead of compiling\",\n    )\n    parser.add_argument(\n        \"--budget\",\n        type=Path,","sourceCodeStart":592,"sourceCodeEnd":628,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/scripts/check-runtime-contract-budget.py#L592-L628","documentation":"The TUI fragment module must reference MAX_FRAGMENT_BYTES, proving the TUI applies the same 40_000-byte (10K-token) fragment ceiling as core rather than allowing larger local fragments. If the literal is absent from crates/tui/src/model_context/fragment.rs, check_fragment_caps raises this error. It fails closed on the shared cap contract from issue #5264.","triggerScenarios":"Running the contract check when the TUI fragment module exists and has ProjectInstructions but never mentions MAX_FRAGMENT_BYTES — caps imported under different names, cap logic removed locally, or fragments built without clamping in the TUI path.","commonSituations":"A TUI-side refactor replaces the imported constant with a local limit under a different name; a cleanup removes 'unused' imports leaving fragments unclamped; a contributor implements TUI fragments without knowing the shared-cap contract.","solutions":["Import and use MAX_FRAGMENT_BYTES from codewhale_core::fragments in crates/tui/src/model_context/fragment.rs, clamping fragment content via enforce_byte_cap.","If the import was removed as unused, restore it — it is contractually required to appear in this file.","Run `python scripts/check-runtime-contract-budget.py` to verify the whole fragment-cap section passes."],"exampleFix":"// before\nfn build_fragment(text: String) -> Fragment { Fragment::Retrieved(text) }\n\n// after\nuse codewhale_core::fragments::{enforce_byte_cap, MAX_FRAGMENT_BYTES};\n\nfn build_fragment(text: String) -> Fragment {\n    let _ = MAX_FRAGMENT_BYTES;\n    Fragment::Retrieved(enforce_byte_cap(text))\n}","handlingStrategy":"validation","validationCode":"text = open('crates/tui/src/model_context/fragment.rs').read()\nassert 'MAX_FRAGMENT_BYTES' in text, 'TUI fragment module must reference MAX_FRAGMENT_BYTES'","typeGuard":"def tui_enforces_fragment_cap(tui_text: str) -> bool:\n    return 'MAX_FRAGMENT_BYTES' in tui_text","tryCatchPattern":"try:\n    check_fragment_caps()\nexcept RuntimeContractError as e:\n    if 'MAX_FRAGMENT_BYTES (10K-token ceiling)' in str(e) and 'model_context' in str(e):\n        import_max_fragment_bytes_in_tui_module()","preventionTips":["Clamp all TUI-side fragment construction with enforce_byte_cap so the MAX_FRAGMENT_BYTES import stays live and meaningful.","Do not let lint cleanup remove the cap import from model_context/fragment.rs; the checker requires it there.","Run the contract check in CI so cap regressions fail before merge."],"tags":["rust","static-analysis","contract-check","tui","byte-cap"],"backgroundTag":"file-size-limit-exceeded","analyzedSha":"433685b2024e7bc4c99e1e2e326bcad39b4d9d65","analyzedAt":"2026-09-15T12:24:24.634Z","contentChangedAt":"2026-09-15T12:24:24.634Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}