Hmbown/CodeWhale · error · RuntimeContractError

runtime-contract measurement failed with exit code

Error message

runtime-contract measurement failed with exit code {proc.returncode}

What it means

run_measurement shells out to scripts/measure-runtime-contract.py (with Cargo forced offline) and requires exit code 0, because the checker can only interpret a successfully produced receipt. If the measurement subprocess exits non-zero, the captured stdout is flushed for debugging and RuntimeContractError is raised with the exit code.

Solutions

  1. Read the measurement's stderr (reprinted to your terminal) to find the underlying cargo/script failure and fix it.
  2. Run `python3 scripts/measure-runtime-contract.py` directly to see the full error interactively.
  3. Ensure the Rust toolchain is installed and `cargo build` works offline (`cargo build --offline`) or vendor/fetch dependencies where network is allowed.
  4. If you only want to validate an existing artifact, pass `--receipt <file>` to skip compilation entirely.

Example fix

// before: full run failing offline
python3 scripts/check-runtime-contract-budget.py
// after: check existing receipt instead, or fix cargo first
python3 scripts/check-runtime-contract-budget.py --receipt receipt.json
Defensive patterns

Strategy: try-catch

Validate before calling

import shutil, subprocess
assert shutil.which("cargo"), "cargo not on PATH"
subprocess.run(["cargo", "build", "--offline"], check=True, capture_output=True)

Type guard

def can_measure() -> bool:
    import shutil
    return shutil.which("cargo") is not None and MEASURE_SCRIPT.exists()

Try / catch

try:
    receipt = run_measurement()
except RuntimeContractError as e:
    print(f"measurement failed: {e}; see stderr above", file=sys.stderr)
    sys.exit(e.exit_code)

Prevention

When it happens

Trigger: Running the checker without `--receipt` when measure-runtime-contract.py fails: cargo build errors, offline Cargo cannot fetch dependencies, missing toolchain, or the measure script itself raising (e.g. it cannot locate crates/tui sources).

Common situations: No network and no vendored cargo deps in a fresh clone; Rust toolchain absent or wrong version; running the script from a context where REPO_ROOT-relative paths or cargo are unavailable; a genuine compile error on the branch.

Related errors


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

Appendix: source

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

        temporary_path.unlink(missing_ok=True)
        raise


def run_measurement() -> dict[str, Any]:
    env = os.environ.copy()
    env["CARGO_NET_OFFLINE"] = "true"
    proc = subprocess.run(
        [sys.executable, str(MEASURE_SCRIPT)],
        cwd=REPO_ROOT,
        env=env,
        capture_output=True,
        text=True,
        check=False,
    )
    sys.stderr.write(proc.stderr)
    if proc.returncode != 0:
        sys.stdout.write(proc.stdout)
        raise RuntimeContractError(
            f"runtime-contract measurement failed with exit code {proc.returncode}"
        )
    try:
        receipt = json.loads(proc.stdout)
    except json.JSONDecodeError as error:
        raise RuntimeContractError(f"measurement emitted invalid JSON: {error}") from error
    if not isinstance(receipt, dict):
        raise RuntimeContractError("measurement top level must be an object")
    validate_receipt(receipt)
    return receipt


def update_command(receipt_path: Path | None, budget_path: Path) -> str:
    parts = ["python3", "scripts/check-runtime-contract-budget.py"]
    if receipt_path is not None:
        parts.extend(["--receipt", str(receipt_path)])
    if budget_path != BUDGET_PATH:
        parts.extend(["--budget", str(budget_path)])

View on GitHub (pinned to 433685b202)