{"record":{"id":"c81099c2b8058131","repo":"rohitg00/ai-engineering-from-scratch","slug":"attempt-must-be-non-negative","errorCode":null,"errorMessage":"attempt must be non-negative","messagePattern":"attempt must be non-negative","errorType":"exception","errorClass":"ReliabilityError","httpStatus":null,"severity":"error","filePath":"phases/13-tools-and-protocols/29-mcp-reliability-cancellation-and-flow-control/code/main.py","lineNumber":506,"sourceCode":"\n\ndef reconnect_plan(\n    previous_subscription_id: int | str,\n    affected_resources: list[str],\n    *,\n    unsafe_mutation_was_in_flight: bool,\n) -> dict[str, Any]:\n    return {\n        \"newSubscriptionId\": f\"{previous_subscription_id}-retry\",\n        \"sendLastEventId\": False,\n        \"refetch\": sorted(set(affected_resources)),\n        \"retryUnsafeMutation\": not unsafe_mutation_was_in_flight,\n    }\n\n\ndef retry_delay_ms(client_id: str, attempt: int) -> int:\n    if attempt < 0:\n        raise ReliabilityError(\"attempt must be non-negative\")\n    ceiling = min(8_000, 250 * (2**attempt))\n    floor = max(1, ceiling // 2)\n    digest = hashlib.sha256(f\"{client_id}:{attempt}\".encode(\"utf-8\")).digest()\n    jitter = int.from_bytes(digest[:4], \"big\") % max(1, ceiling - floor + 1)\n    return floor + jitter\n\n\ndef main() -> None:\n    coordinator = RequestCoordinator()\n    coordinator.start(\n        7,\n        \"tools/call\",\n        transport=STDIO,\n        started_at_ms=0,\n        idle_timeout_ms=500,\n        max_timeout_ms=2_000,\n        progress_token=\"progress-7\",\n    )","sourceCodeStart":488,"sourceCodeEnd":524,"githubUrl":"https://github.com/rohitg00/ai-engineering-from-scratch/blob/39ea8a1c6d0b61f071226eff7ede4d4105fed820/phases/13-tools-and-protocols/29-mcp-reliability-cancellation-and-flow-control/code/main.py#L488-L524","documentation":"Error \"attempt must be non-negative\" thrown in rohitg00/ai-engineering-from-scratch.","triggerScenarios":"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.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"39ea8a1c6d0b61f071226eff7ede4d4105fed820","analyzedAt":"2026-08-26T03:13:46.626Z","schemaVersion":2},"datasetVersion":"2026-08-26T07:17:17.940Z"}