{"record":{"id":"8c1ac9ab5b1fdaab","repo":"yikart/AiToEarn","slug":"invalidaitaskid","errorCode":"InvalidAiTaskId","errorMessage":"InvalidAiTaskId","messagePattern":"InvalidAiTaskId","errorType":"error_code","errorClass":"AppException","httpStatus":400,"severity":"error","filePath":"project/aitoearn-backend/apps/aitoearn-ai/src/core/ai/video/dashscope/dashscope.service.ts","lineNumber":341,"sourceCode":"\n    if (!updatedAiLog) {\n      return aiLog.response!\n    }\n\n    await this.aiAvailability.recordAsyncComplete(\n      taskId,\n      { provider: 'dashscope', operation: 'videoGeneration', model: aiLog.request.providerModel ?? aiLog.model },\n      { success: false, latencyMs: elapsedMs, errorMessage },\n    )\n\n    return callbackData\n  }\n\n  async callback(queryResult: DashscopeQueryVideoTaskResponse): Promise<DashscopeVideoCallbackDto> {\n    const taskId = queryResult.output.task_id\n    const aiLog = await this.aiLogRepo.getByTaskId(taskId)\n    if (!aiLog || aiLog.channel !== AiLogChannel.Dashscope) {\n      throw new AppException(ResponseCode.InvalidAiTaskId)\n    }\n    const dashscopeAiLog = aiLog as DashscopeVideoAiLog\n\n    if (dashscopeAiLog.status !== AiLogStatus.Generating) {\n      return dashscopeAiLog.response!\n    }\n\n    const status = queryResult.output.task_status\n    if (status === DashscopeTaskStatus.Pending || status === DashscopeTaskStatus.Running) {\n      return {\n        id: taskId,\n        status,\n        requestId: queryResult.request_id,\n        providerModel: dashscopeAiLog.request.providerModel,\n      }\n    }\n\n    const elapsedMs = Date.now() - dashscopeAiLog.startedAt.getTime()","sourceCodeStart":323,"sourceCodeEnd":359,"githubUrl":"https://github.com/yikart/AiToEarn/blob/d3aa8bea5b146a8675607cf0144d891aad3e9683/project/aitoearn-backend/apps/aitoearn-ai/src/core/ai/video/dashscope/dashscope.service.ts#L323-L359","documentation":"In DashScopeVideoService.callback, the incoming query result's output.task_id is looked up via aiLogRepo.getByTaskId. If no AI log exists with that task id, or the found log's channel is not AiLogChannel.Dashscope, the service throws AppException(ResponseCode.InvalidAiTaskId) because the callback cannot be attributed to a known DashScope task.","triggerScenarios":"Callback/poll result arrives with a task_id that (a) was never created by this service, (b) belongs to a log persisted in another DB/environment, or (c) matches a log whose channel is not Dashscope (e.g. a Grok or other provider task id routed to the DashScope callback handler).","commonSituations":"Provider retries callbacks after the local log was deleted; environments mismatch (task created in staging, callback hits prod); misconfigured webhook routing a foreign task id to this endpoint; duplicate/replayed callbacks with fabricated ids.","solutions":["Verify the task_id in the callback exists in the ai_logs collection and has channel='dashscope'.","Confirm the callback is hitting the same environment/database where createFromRequest persisted the log.","Check webhook routing so only DashScope-originated results reach this callback method.","If the log was intentionally removed, ignore/ACK the callback instead of treating it as a client error."],"exampleFix":"// before\nconst aiLog = await this.aiLogRepo.getByTaskId(taskId)\nif (!aiLog || aiLog.channel !== AiLogChannel.Dashscope) {\n  throw new AppException(ResponseCode.InvalidAiTaskId)\n}\n// after (log the id before failing to speed diagnosis)\nconst aiLog = await this.aiLogRepo.getByTaskId(taskId)\nif (!aiLog || aiLog.channel !== AiLogChannel.Dashscope) {\n  this.logger.warn({ taskId }, 'Unknown DashScope task in callback')\n  throw new AppException(ResponseCode.InvalidAiTaskId)\n}","handlingStrategy":"validation","validationCode":"function isDashscopeCallback(res: { output?: { task_id?: string } }): boolean {\n  return typeof res.output?.task_id === 'string' && res.output.task_id.startsWith('') && res.output.task_id.length > 0\n}\n// before delivering the callback, confirm the task_id exists locally:\nconst aiLog = await aiLogRepo.getByTaskId(res.output.task_id)\nif (!aiLog || aiLog.channel !== 'dashscope') return // ignore unknown callback instead of crashing","typeGuard":null,"tryCatchPattern":"try {\n  await dashscopeVideoService.callback(queryResult)\n} catch (e) {\n  if (e instanceof AppException && e.code === ResponseCode.InvalidAiTaskId) {\n    // unknown/foreign task id: ACK the callback to stop provider retries, log for audit\n    return\n  }\n  throw e\n}","preventionTips":["Ensure webhook endpoints only receive results for tasks created in the same environment.","Keep ai_logs retention long enough to cover provider callback retries.","Route callbacks per provider channel; never share one endpoint across providers."],"tags":["dashscope","callback","invalid-task-id","webhook"],"backgroundTag":"invalid-task-id","analyzedSha":"d3aa8bea5b146a8675607cf0144d891aad3e9683","analyzedAt":"2026-08-31T14:19:24.185Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}