Hmbown/CodeWhale · error · RuntimeError

Codewhale terminal receipt contained an invalid count

Error message

Codewhale terminal receipt contained an invalid count

What it means

Count-like terminal receipt fields (token counts, durations, retries) must be non-negative integers that fit in a signed 64-bit range. This RuntimeError fires when one of those fields is negative or exceeds 2**63-1. The harness throws it because such values cannot be legitimate counters and would corrupt downstream metrics.

Solutions

  1. Print the raw metadata event to identify which count field is out of range
  2. Replace custom or mock binaries with the official pinned release from binary_path config
  3. Fix or report the emitting binary so unknown counters are omitted (None) instead of -1
  4. If legitimately large counts are expected, clamp/validate them upstream before the receipt is emitted

Example fix

// before (emitting binary)
"input_tokens": -1  // unknown
// after
"input_tokens": 0  // or omit the field
Defensive patterns

Strategy: validation

Validate before calling

def valid_count(v: object) -> bool:
    return isinstance(v, int) and not isinstance(v, bool) and 0 <= v <= 2**63 - 1
bad = [k for k, v in meta.items() if not valid_count(v)]
assert not bad, f"invalid count fields: {bad}"

Type guard

def is_valid_count(value: object) -> bool:
    return isinstance(value, int) and not isinstance(value, bool) and 0 <= value <= 2**63 - 1

Try / catch

try:
    receipt = await harness.launch(ctx, trace, runtime, endpoint, secret, mcp_urls)
except RuntimeError as e:
    if "invalid count" in str(e):
        logger.error("negative/oversized counter in receipt; emitter bug suspected")
    raise

Prevention

When it happens

Trigger: _bounded_terminal encountered an int field from _TERMINAL_FIELDS (e.g. input_tokens, duration_ms, retry_count) with value < 0 or > 2**63-1 in the metadata event's meta.

Common situations: A patched or buggy Codewhale build emitting -1 for unknown values; integer overflow from cumulative counters; a mock/fake binary writing sentinel values like -1.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

Thrown at integrations/verifiers-codewhale/codewhale_harness/harness.py:266

            rf"(?<![0-9A-Za-z.+-]){re.escape(version)}(?![0-9A-Za-z.+-])",
            output,
        )
        is not None
    )


def _bounded_terminal(meta: dict[str, Any]) -> dict[str, Any]:
    terminal: dict[str, Any] = {}
    for key in _TERMINAL_FIELDS:
        if key not in meta or meta[key] is None:
            continue
        value = meta[key]
        if isinstance(value, str):
            if len(value) > MAX_TERMINAL_STRING_CHARS:
                raise RuntimeError("Codewhale terminal receipt exceeded its string bound")
        elif isinstance(value, int) and not isinstance(value, bool):
            if value < 0 or value > 2**63 - 1:
                raise RuntimeError("Codewhale terminal receipt contained an invalid count")
        else:
            raise RuntimeError("Codewhale terminal receipt contained a non-scalar field")
        terminal[key] = value
    encoded = json.dumps(terminal, sort_keys=True, separators=(",", ":")).encode()
    if len(encoded) > MAX_TERMINAL_RECEIPT_BYTES:
        raise RuntimeError("Codewhale terminal receipt exceeded its total bound")
    return terminal


def _install_script(version: str) -> str:
    version_q = shlex.quote(version)
    install_q = shlex.quote(INSTALL_DIR)
    release_q = shlex.quote(RELEASE_ROOT)
    return f"""
set -eu
version={version_q}
install_dir={install_q}
release_root={release_q}

View on GitHub (pinned to 433685b202)