{"record":{"id":"af8e2077cfe566d4","repo":"shareAI-lab/learn-claude-code","slug":"could-not-allocate-a-unique-task-id-af8e20","errorCode":null,"errorMessage":"Could not allocate a unique task ID","messagePattern":"Could not allocate a unique task ID","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"warning","filePath":"s15_integrated_harness/code.py","lineNumber":233,"sourceCode":"        for dependency in dependencies:\n            if not _task_path(dependency).is_file():\n                raise ValueError(f\"Dependency not found: {dependency}\")\n        for _ in range(100):\n            task = Task(\n                id=f\"task_{secrets.token_hex(4)}\",\n                subject=subject,\n                description=description,\n                status=\"pending\",\n                owner=None,\n                blockedBy=dependencies,\n            )\n            try:\n                with _task_path(task.id).open(\"x\", encoding=\"utf-8\") as handle:\n                    json.dump(asdict(task), handle, indent=2)\n                return task\n            except FileExistsError:\n                continue\n    raise RuntimeError(\"Could not allocate a unique task ID\")\n\n\ndef save_task(task: Task):\n    with task_store_lock():\n        path = _task_path(task.id)\n        temporary = path.with_name(\n            f\".{path.name}.{os.getpid()}.{threading.get_ident()}.tmp\"\n        )\n        try:\n            temporary.write_text(\n                json.dumps(asdict(task), indent=2), encoding=\"utf-8\"\n            )\n            os.replace(temporary, path)\n        finally:\n            temporary.unlink(missing_ok=True)\n\n\ndef load_task(task_id: str) -> Task:","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/shareAI-lab/learn-claude-code/blob/985456f4adea6f4df8fbad4112245dbd97444eae/s15_integrated_harness/code.py#L215-L251","documentation":"create_task() allocates IDs as task_ + secrets.token_hex(4) (8 hex chars, 4 billion space) and retries up to 100 times using exclusive open('x'). If every attempt hits FileExistsError, it gives up with this RuntimeError. In practice this indicates the tasks store is pathologically full or the filesystem is misbehaving, not bad luck.","triggerScenarios":"Hundreds of millions of task files saturating the ID space (essentially never); a filesystem or test setup where open('x') spuriously raises FileExistsError; monkeypatched secrets.token_hex returning a constant in tests.","commonSituations":"Unit tests patching secrets.token_hex to a fixed value with a leftover task file present; a fuzz/property test hammering create_task; extremely long-lived workspaces with enormous task counts.","solutions":["If this happens in tests, stop patching secrets.token_hex to a constant (or clean TASKS_DIR between tests).","Prune or archive the tasks directory if it has grown abnormally large.","As a last resort, retry create_task — the collision source is usually transient in misbehaving environments."],"exampleFix":null,"handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"for attempt in range(3):\n    try:\n        task = create_task(subject, description, blockedBy)\n        break\n    except RuntimeError as e:\n        if \"unique task ID\" not in str(e):\n            raise\n        import time; time.sleep(0.05 * (attempt + 1))\nelse:\n    raise","preventionTips":["Don't patch secrets.token_hex to constants in tests.","Clean TASKS_DIR between test runs.","Archive very large task stores."],"tags":["task","id-allocation","rare","exhaustion"],"backgroundTag":null,"analyzedSha":"985456f4adea6f4df8fbad4112245dbd97444eae","analyzedAt":"2026-08-14T22:02:26.028Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}