{"record":{"id":"fddc06bf2af4b50f","repo":"shareAI-lab/learn-claude-code","slug":"task-file-id-does-not-match-task-id-fddc06","errorCode":null,"errorMessage":"Task file ID does not match {task_id}","messagePattern":"Task file ID does not match (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"s13_agent_teams/code.py","lineNumber":181,"sourceCode":"        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:\n    with task_lock:\n        data = json.loads(_task_path(task_id).read_text(encoding=\"utf-8\"))\n        task = Task(**data)\n        if task.id != task_id:\n            raise ValueError(f\"Task file ID does not match {task_id}\")\n        if task.status not in {\"pending\", \"in_progress\", \"completed\"}:\n            raise ValueError(f\"Invalid task status: {task.status}\")\n        return task\n\n\ndef list_tasks() -> list[Task]:\n    with task_lock:\n        if not TASKS_DIR.exists():\n            return []\n        if not TASKS_ROOT.is_relative_to(WORKDIR.resolve()):\n            raise ValueError(\"Tasks directory escapes workspace\")\n        return [load_task(path.stem)\n                for path in sorted(TASKS_DIR.glob(\"task_*.json\"))]\n\n\ndef get_task(task_id: str) -> str:\n    \"\"\"Return full task details as JSON.\"\"\"\n    task = load_task(task_id)","sourceCodeStart":163,"sourceCodeEnd":199,"githubUrl":"https://github.com/shareAI-lab/learn-claude-code/blob/985456f4adea6f4df8fbad4112245dbd97444eae/s13_agent_teams/code.py#L163-L199","documentation":"load_task() reads .tasks/<task_id>.json, constructs Task(**data), and requires the 'id' field inside the JSON to equal the task_id used to locate the file. A mismatch means the file content and filename disagree — manual edits, a botched rename/copy, or two writers racing.","triggerScenarios":"Renaming a task file without editing its id field; copying task_a.json over task_b.json; hand-editing .tasks/*.json; a partial/interleaved write leaving stale content under a new filename.","commonSituations":"Users duplicating task files to clone tasks; external scripts writing the directory without using save_task(); editor auto-save during a save_task() atomic replace.","solutions":["Don't clone tasks by copying files; write a small script that loads, changes id, and saves via save_task().","Fix a mismatched file by editing its id field to match the filename (or renaming the file to the embedded id).","Keep all writes going through save_task(), which writes atomically via a temp file and os.replace."],"exampleFix":"# before (broken: copied file)\n# .tasks/task_aaaaaaaa.json contains \"id\": \"task_bbbbbbbb\"\n\n# after\nimport json, pathlib\np = pathlib.Path('.tasks/task_aaaaaaaa.json')\ndata = json.loads(p.read_text())\ndata['id'] = 'task_aaaaaaaa'\np.write_text(json.dumps(data, indent=2))","handlingStrategy":"validation","validationCode":"import json\nfrom pathlib import Path\n\ndef task_file_consistent(path: Path) -> bool:\n    try:\n        return json.loads(path.read_text())['id'] == path.stem\n    except (OSError, ValueError, KeyError):\n        return False","typeGuard":null,"tryCatchPattern":"try:\n    task = load_task(task_id)\nexcept ValueError as exc:\n    if 'does not match' in str(exc):\n        # repair: rewrite file's id to match filename, or drop the corrupt file\n        log_corrupt(task_id)\n        (TASKS_DIR / f'{task_id}.json').unlink(missing_ok=True)\n        return None\n    raise","preventionTips":["Never duplicate task files with cp; use load/modify/save via save_task.","Audit .tasks for filename/id mismatches after any manual intervention.","Write only through save_task() so id and filename stay in lockstep."],"tags":["data-integrity","task-id","file-corruption","load-task"],"backgroundTag":null,"analyzedSha":"985456f4adea6f4df8fbad4112245dbd97444eae","analyzedAt":"2026-08-14T22:02:26.028Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}