{"record":{"id":"1d89c4e36083a136","repo":"Hmbown/CodeWhale","slug":"field-must-be-a-non-negative-integer","errorCode":null,"errorMessage":"{field} must be a non-negative integer","messagePattern":"(.+?) must be a non-negative integer","errorType":"validation","errorClass":"PersistenceBacklogError","httpStatus":null,"severity":"error","filePath":"scripts/check-persistence-backlog-budget.py","lineNumber":98,"sourceCode":"\n\nclass PersistenceBacklogError(ValueError):\n    \"\"\"A receipt or budget broke the measurement contract.\"\"\"\n\n\ndef load_json(path: Path, label: str) -> dict[str, Any]:\n    try:\n        value = json.loads(path.read_text(encoding=\"utf-8\"))\n    except (OSError, json.JSONDecodeError) as error:\n        raise PersistenceBacklogError(f\"invalid {label} {path}: {error}\") from error\n    if not isinstance(value, dict):\n        raise PersistenceBacklogError(f\"{label} must be a JSON object\")\n    return value\n\n\ndef non_negative_integer(value: Any, field: str) -> int:\n    if isinstance(value, bool) or not isinstance(value, int) or value < 0:\n        raise PersistenceBacklogError(f\"{field} must be a non-negative integer\")\n    return value\n\n\ndef validate_frozen_field(field: str, value: Any, expected: Any) -> None:\n    if type(value) is not type(expected) or value != expected:\n        raise PersistenceBacklogError(\n            f\"receipt {field} must remain {expected!r}, got {value!r}\"\n        )\n\n\ndef current_source_identity() -> dict[str, Any]:\n    def run(command: list[str]) -> str:\n        result = subprocess.run(\n            command,\n            cwd=ROOT,\n            text=True,\n            capture_output=True,\n            check=False,","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/scripts/check-persistence-backlog-budget.py#L80-L116","documentation":"non_negative_integer validates that a numeric field in the persistence-backlog receipt or budget is a true int >= 0. Booleans are explicitly rejected (bool is a subclass of int in Python), as are negatives and non-int types (float, str, null), raising PersistenceBacklogError when the contract is violated.","triggerScenarios":"A receipt or budget JSON contains a field that is negative (e.g. a counter went backwards), a float like 3.0, a string \"5\", null, or true/false where a count was expected.","commonSituations":"A producer computing a delta that can be negative; JavaScript tooling serializing integers as floats; hand-edited JSON using quotes around numbers; a field defaulting to null when unset.","solutions":["Find the offending field named in the message and correct its value to a plain non-negative integer","Fix the producer so it clamps/validates counts before serialization (e.g. max(0, n) and int(n))","Remove quotes/null so the value serializes as a JSON number, not string/null","Re-run the checker after regenerating the receipt"],"exampleFix":"// before\n{\"backlog_items\": -3}\n// after\n{\"backlog_items\": 0}","handlingStrategy":"validation","validationCode":"def check_count(doc: dict, field: str) -> int:\n    v = doc.get(field)\n    assert isinstance(v, int) and not isinstance(v, bool) and v >= 0, \\\n        f\"{field} must be a non-negative integer, got {v!r}\"\n    return v","typeGuard":"def is_non_negative_int(v: object) -> bool:\n    return isinstance(v, int) and not isinstance(v, bool) and v >= 0","tryCatchPattern":"try:\n    validate_receipt(doc)\nexcept PersistenceBacklogError as exc:\n    print(f\"receipt rejected: {exc}\"); sys.exit(1)","preventionTips":["Clamp and int()-cast counters in the producer before serializing","Never hand-edit numeric fields in the receipt JSON","Serialize with a typed schema (dataclass -> json) so floats/strings can't slip in","Watch for deltas that can go negative; clamp to 0 at the source"],"tags":["json","validation","type-check"],"backgroundTag":"invalid-argument-value","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"}