{"record":{"id":"2a8702b1c4e93964","repo":"Hmbown/CodeWhale","slug":"codewhale-terminal-receipt-contained-an-invalid-count","errorCode":null,"errorMessage":"Codewhale terminal receipt contained an invalid count","messagePattern":"Codewhale terminal receipt contained an invalid count","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"integrations/verifiers-codewhale/codewhale_harness/harness.py","lineNumber":266,"sourceCode":"            rf\"(?<![0-9A-Za-z.+-]){re.escape(version)}(?![0-9A-Za-z.+-])\",\n            output,\n        )\n        is not None\n    )\n\n\ndef _bounded_terminal(meta: dict[str, Any]) -> dict[str, Any]:\n    terminal: dict[str, Any] = {}\n    for key in _TERMINAL_FIELDS:\n        if key not in meta or meta[key] is None:\n            continue\n        value = meta[key]\n        if isinstance(value, str):\n            if len(value) > MAX_TERMINAL_STRING_CHARS:\n                raise RuntimeError(\"Codewhale terminal receipt exceeded its string bound\")\n        elif isinstance(value, int) and not isinstance(value, bool):\n            if value < 0 or value > 2**63 - 1:\n                raise RuntimeError(\"Codewhale terminal receipt contained an invalid count\")\n        else:\n            raise RuntimeError(\"Codewhale terminal receipt contained a non-scalar field\")\n        terminal[key] = value\n    encoded = json.dumps(terminal, sort_keys=True, separators=(\",\", \":\")).encode()\n    if len(encoded) > MAX_TERMINAL_RECEIPT_BYTES:\n        raise RuntimeError(\"Codewhale terminal receipt exceeded its total bound\")\n    return terminal\n\n\ndef _install_script(version: str) -> str:\n    version_q = shlex.quote(version)\n    install_q = shlex.quote(INSTALL_DIR)\n    release_q = shlex.quote(RELEASE_ROOT)\n    return f\"\"\"\nset -eu\nversion={version_q}\ninstall_dir={install_q}\nrelease_root={release_q}","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/integrations/verifiers-codewhale/codewhale_harness/harness.py#L248-L284","documentation":"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.","triggerScenarios":"_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.","commonSituations":"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.","solutions":["Print the raw metadata event to identify which count field is out of range","Replace custom or mock binaries with the official pinned release from binary_path config","Fix or report the emitting binary so unknown counters are omitted (None) instead of -1","If legitimately large counts are expected, clamp/validate them upstream before the receipt is emitted"],"exampleFix":"// before (emitting binary)\n\"input_tokens\": -1  // unknown\n// after\n\"input_tokens\": 0  // or omit the field","handlingStrategy":"validation","validationCode":"def valid_count(v: object) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and 0 <= v <= 2**63 - 1\nbad = [k for k, v in meta.items() if not valid_count(v)]\nassert not bad, f\"invalid count fields: {bad}\"","typeGuard":"def is_valid_count(value: object) -> bool:\n    return isinstance(value, int) and not isinstance(value, bool) and 0 <= value <= 2**63 - 1","tryCatchPattern":"try:\n    receipt = await harness.launch(ctx, trace, runtime, endpoint, secret, mcp_urls)\nexcept RuntimeError as e:\n    if \"invalid count\" in str(e):\n        logger.error(\"negative/oversized counter in receipt; emitter bug suspected\")\n    raise","preventionTips":["Emit 0 or omit unknown counters instead of -1 sentinels","Avoid float accumulation that can overflow counters","Test custom binaries against the receipt schema before rollout","Cap cumulative counters in the emitter at int64 range"],"tags":["validation","limits","integers"],"backgroundTag":"value-out-of-range","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"}