rohitg00/ai-engineering-from-scratch · error · ReliabilityError

attempt must be non-negative

Error message

attempt must be non-negative

What it means

Error "attempt must be non-negative" thrown in rohitg00/ai-engineering-from-scratch.

Source

Thrown at phases/13-tools-and-protocols/29-mcp-reliability-cancellation-and-flow-control/code/main.py:506


def reconnect_plan(
    previous_subscription_id: int | str,
    affected_resources: list[str],
    *,
    unsafe_mutation_was_in_flight: bool,
) -> dict[str, Any]:
    return {
        "newSubscriptionId": f"{previous_subscription_id}-retry",
        "sendLastEventId": False,
        "refetch": sorted(set(affected_resources)),
        "retryUnsafeMutation": not unsafe_mutation_was_in_flight,
    }


def retry_delay_ms(client_id: str, attempt: int) -> int:
    if attempt < 0:
        raise ReliabilityError("attempt must be non-negative")
    ceiling = min(8_000, 250 * (2**attempt))
    floor = max(1, ceiling // 2)
    digest = hashlib.sha256(f"{client_id}:{attempt}".encode("utf-8")).digest()
    jitter = int.from_bytes(digest[:4], "big") % max(1, ceiling - floor + 1)
    return floor + jitter


def main() -> None:
    coordinator = RequestCoordinator()
    coordinator.start(
        7,
        "tools/call",
        transport=STDIO,
        started_at_ms=0,
        idle_timeout_ms=500,
        max_timeout_ms=2_000,
        progress_token="progress-7",
    )

View on GitHub (pinned to 39ea8a1c6d)

When it happens

Trigger: Thrown at phases/13-tools-and-protocols/29-mcp-reliability-cancellation-and-flow-control/code/main.py:506 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/c81099c2b8058131. Report an issue: GitHub.