{"record":{"id":"3822d8c0d1c94627","repo":"shareAI-lab/learn-claude-code","slug":"could-not-allocate-a-unique-task-id-3822d8","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":"critical","filePath":"s13_agent_teams/code.py","lineNumber":158,"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":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/shareAI-lab/learn-claude-code/blob/985456f4adea6f4df8fbad4112245dbd97444eae/s13_agent_teams/code.py#L140-L176","documentation":"create_task() tries up to 100 times to allocate an ID whose file does not yet exist, using exclusive open('x'). With an 8-hex-char space (4 billion IDs) this is practically unreachable unless the filesystem misreports FileExistsError for every candidate — e.g. the .tasks directory is not writable in a way that surfaces as FileExistsError, or something (antivirus, sync client, a stuck .tmp naming scheme) creates files matching every probe.","triggerScenarios":"100 consecutive collisions of secrets.token_hex(4); or the exclusive-create call failing with FileExistsError for unrelated filesystem reasons (read-only or corrupted directory mapped to that errno by a FUSE mount).","commonSituations":"Essentially never in normal use. Seen with exotic filesystems (network/FUSE mounts) that return EEXIST for failed creates, or in fault-injection tests.","solutions":["Check .tasks for tens of thousands of task files or foreign files matching task_*.json and clean up.","Verify the workspace filesystem supports O_EXCL properly (local disk vs FUSE/network mount); move the workspace local.","If genuinely at capacity, widen the ID (token_hex(8)) and update TASK_ID_PATTERN."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"import os, stat\n\ndef tasks_dir_writable_exclusive(tasks_dir) -> bool:\n    try:\n        if not tasks_dir.exists():\n            return False\n        probe = tasks_dir / '.probe_exclusive'\n        fd = os.open(probe, os.O_CREAT | os.O_EXCL | os.O_WRONLY)\n        os.close(fd); probe.unlink()\n        return True\n    except OSError:\n        return False","typeGuard":null,"tryCatchPattern":"for attempt in range(3):\n    try:\n        return create_task(subject, blockedBy=deps)\n    except RuntimeError as exc:\n        if 'unique task ID' not in str(exc) or attempt == 2:\n            raise\n        time.sleep(0.05 * (attempt + 1))","preventionTips":["Keep the workspace on a local filesystem that honors O_EXCL.","Periodically prune task_*.json files that were not created by the app."],"tags":["id-allocation","filesystem","unrecoverable","create-task"],"backgroundTag":null,"analyzedSha":"985456f4adea6f4df8fbad4112245dbd97444eae","analyzedAt":"2026-08-14T22:02:26.028Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}