{"record":{"id":"d9b706fa8cd798d0","repo":"datawhalechina/hello-agents","slug":"task-id","errorCode":null,"errorMessage":"任务不存在: {task_id}","messagePattern":"任务不存在: (.+?)","errorType":"exception","errorClass":"AgentException","httpStatus":500,"severity":"error","filePath":"Co-creation-projects/Apricity-InnocoreAI/agents/controller.py","lineNumber":107,"sourceCode":"            \"callback\": callback,\n            \"created_at\": datetime.now(),\n            \"started_at\": None,\n            \"completed_at\": None,\n            \"result\": None,\n            \"error\": None,\n            \"agent_results\": {}\n        }\n        \n        self.active_tasks[task_id] = task\n        await self.task_queue.put((priority, task))\n        \n        logger.info(f\"任务已提交: {task_id}, 类型: {task_type.value}\")\n        return task_id\n    \n    async def execute_task(self, task_id: str) -> Dict[str, Any]:\n        \"\"\"执行单个任务\"\"\"\n        if task_id not in self.active_tasks:\n            raise AgentException(f\"任务不存在: {task_id}\")\n        \n        task = self.active_tasks[task_id]\n        \n        async with self.semaphore:  # 并发控制\n            try:\n                task[\"status\"] = TaskStatus.RUNNING\n                task[\"started_at\"] = datetime.now()\n                \n                await self._trigger_event(\"task_started\", task)\n                \n                # 根据任务类型执行相应的逻辑\n                if task[\"type\"] == TaskType.PAPER_HUNTING:\n                    result = await self._execute_paper_hunting(task)\n                elif task[\"type\"] == TaskType.PAPER_ANALYSIS:\n                    result = await self._execute_paper_analysis(task)\n                elif task[\"type\"] == TaskType.WRITING_ASSISTANCE:\n                    result = await self._execute_writing_assistance(task)\n                elif task[\"type\"] == TaskType.CITATION_VALIDATION:","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/datawhalechina/hello-agents/blob/606a07d341a47be773fab7f4b71177f53f96b2c3/Co-creation-projects/Apricity-InnocoreAI/agents/controller.py#L89-L125","documentation":"Lookup guard in ControllerAgent.execute_task: the given task_id is not a key in self.active_tasks. Tasks enter that dict only via submit_task and are removed in the finally block of execute_task, so the ID is unknown (never submitted, typo'd) or already finished — after completion the task moves to task_history and a second execute call with the same ID fails here.","triggerScenarios":"Calling execute_task('abc') with an ID not returned by submit_task; retrying execute_task after a successful first run (ID moved to task_history in finally); racing a background worker that executed the queued task first; string-vs-int ID mismatch from JSON deserialization.","commonSituations":"API handler extracting task_id from URL with wrong type/whitespace; double-click on an 'execute' endpoint; frontend caching an old task ID after backend restart wiped in-memory state (active_tasks is not persisted).","solutions":["Use the exact ID returned by submit_task, kept as a string, and execute it once.","For re-execution, submit a new task instead of reusing a completed ID.","Before raising, fall back: if task_id in self.task_history, report 'task already finished' instead of 'not found' for clearer UX.","Persist task registry (or query task_history) if the controller outlives requests."],"exampleFix":"# before\nif task_id not in self.active_tasks:\n    raise AgentException(f\"任务不存在: {task_id}\")\n\n# after\nif task_id not in self.active_tasks:\n    done = next((t for t in self.task_history if t[\"id\"] == task_id), None)\n    if done:\n        raise AgentException(f\"任务 {task_id} 已结束(状态: {done['status']})，不能重复执行\")\n    raise AgentException(f\"任务不存在: {task_id}\"); ","handlingStrategy":"validation","validationCode":"# Caller-side: only execute ids you got from submit_task, exactly once\nif not isinstance(task_id, str) or not task_id:\n    raise ValueError(\"task_id must be a non-empty string\")\nif task_id not in controller.active_tasks:\n    done = any(t[\"id\"] == task_id for t in controller.task_history)\n    status = \"already finished\" if done else \"unknown id\"\n    raise ValueError(f\"cannot execute {task_id!r}: {status}\")","typeGuard":"def is_active_task(controller, task_id: str) -> bool:\n    return isinstance(task_id, str) and task_id in controller.active_tasks","tryCatchPattern":"try:\n    result = await controller.execute_task(task_id)\nexcept AgentException as e:\n    if \"任务不存在\" in str(e):\n        task_id = await controller.submit_task(...)  # resubmit and execute the new id\n        result = await controller.execute_task(task_id)\n    else:\n        raise","preventionTips":["Treat submit_task's return value as an opaque, single-use handle.","Never retry execute_task with the same id after completion; submit a new task.","Persist or query task_history for idempotent APIs."],"tags":["python","orchestration","task-queue","lifecycle","validation"],"backgroundTag":null,"analyzedSha":"606a07d341a47be773fab7f4b71177f53f96b2c3","analyzedAt":"2026-08-14T22:57:27.446Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}