Hmbown/CodeWhale · error · PersistenceBacklogError

must be a non-negative integer

Error message

{field} must be a non-negative integer

What it means

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.

Solutions

  1. Find the offending field named in the message and correct its value to a plain non-negative integer
  2. Fix the producer so it clamps/validates counts before serialization (e.g. max(0, n) and int(n))
  3. Remove quotes/null so the value serializes as a JSON number, not string/null
  4. Re-run the checker after regenerating the receipt

Example fix

// before
{"backlog_items": -3}
// after
{"backlog_items": 0}
Defensive patterns

Strategy: validation

Validate before calling

def check_count(doc: dict, field: str) -> int:
    v = doc.get(field)
    assert isinstance(v, int) and not isinstance(v, bool) and v >= 0, \
        f"{field} must be a non-negative integer, got {v!r}"
    return v

Type guard

def is_non_negative_int(v: object) -> bool:
    return isinstance(v, int) and not isinstance(v, bool) and v >= 0

Try / catch

try:
    validate_receipt(doc)
except PersistenceBacklogError as exc:
    print(f"receipt rejected: {exc}"); sys.exit(1)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at scripts/check-persistence-backlog-budget.py:98


class PersistenceBacklogError(ValueError):
    """A receipt or budget broke the measurement contract."""


def load_json(path: Path, label: str) -> dict[str, Any]:
    try:
        value = json.loads(path.read_text(encoding="utf-8"))
    except (OSError, json.JSONDecodeError) as error:
        raise PersistenceBacklogError(f"invalid {label} {path}: {error}") from error
    if not isinstance(value, dict):
        raise PersistenceBacklogError(f"{label} must be a JSON object")
    return value


def non_negative_integer(value: Any, field: str) -> int:
    if isinstance(value, bool) or not isinstance(value, int) or value < 0:
        raise PersistenceBacklogError(f"{field} must be a non-negative integer")
    return value


def validate_frozen_field(field: str, value: Any, expected: Any) -> None:
    if type(value) is not type(expected) or value != expected:
        raise PersistenceBacklogError(
            f"receipt {field} must remain {expected!r}, got {value!r}"
        )


def current_source_identity() -> dict[str, Any]:
    def run(command: list[str]) -> str:
        result = subprocess.run(
            command,
            cwd=ROOT,
            text=True,
            capture_output=True,
            check=False,

View on GitHub (pinned to 433685b202)