{"record":{"id":"a5133c9757320957","repo":"google-gemini/gemini-cli","slug":"a2aclient-canceltask-error-agentname-error","errorCode":null,"errorMessage":"A2AClient cancelTask Error [${agentName}]: ${error.message}","messagePattern":"A2AClient cancelTask Error \\[(.+?)\\]: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/agents/a2a-client-manager.ts","lineNumber":286,"sourceCode":"      throw new Error(`${prefix}: Unexpected error: ${String(error)}`);\n    }\n  }\n\n  /**\n   * Cancels a task on an agent.\n   * @param agentName The name of the agent.\n   * @param taskId The ID of the task to cancel.\n   * @returns The cancellation response.\n   */\n  async cancelTask(agentName: string, taskId: string): Promise<Task> {\n    const client = this.clients.get(agentName);\n    if (!client) throw new Error(`Agent '${agentName}' not found.`);\n    try {\n      return await client.cancelTask({ id: taskId });\n    } catch (error: unknown) {\n      const prefix = `A2AClient cancelTask Error [${agentName}]`;\n      if (error instanceof Error) {\n        throw new Error(`${prefix}: ${error.message}`, { cause: error });\n      }\n      throw new Error(`${prefix}: Unexpected error: ${String(error)}`);\n    }\n  }\n}\n","sourceCodeStart":268,"sourceCodeEnd":292,"githubUrl":"https://github.com/google-gemini/gemini-cli/blob/5024443c7217464a66e98f80d73172a26440bd8f/packages/core/src/agents/a2a-client-manager.ts#L268-L292","documentation":"cancelTask wraps any Error thrown by the underlying SDK client.cancelTask({ id: taskId }) with an 'A2AClient cancelTask Error [<agentName>]' prefix and preserves the original via { cause }. The inner message is the real signal: it is typically a JSON-RPC error, a network failure, or a task-state rejection from the remote agent. The wrapping makes the failing operation and agent traceable in logs.","triggerScenarios":"The task id does not exist on the remote agent; the task is already in a terminal state (completed/failed/canceled) and cannot be canceled; the remote A2A server returned a JSON-RPC error; a transient network error or timeout (up to the 30-minute A2A_TIMEOUT) interrupted the request; the auth token expired and retry was exhausted.","commonSituations":"Stale taskId persisted from a previous session and reused after the remote purged it; cancelling a task that already finished; remote agent restarted and lost in-memory task state; proxy or TLS misconfiguration causing the SDK fetch to reject.","solutions":["Inspect error.cause for the real upstream message and HTTP/JSON-RPC code before deciding to retry.","If the task is already terminal, treat the cancel as a no-op success rather than an error.","Verify the taskId came from a prior sendMessage/getTask response for the same agent and contextId.","For transient network errors, retry cancelTask with backoff (the SDK auth-retry is separate and only covers 401/403)."],"exampleFix":"// before\nconst task = await manager.cancelTask(name, taskId);\n\n// after\ntry {\n  const task = await manager.cancelTask(name, taskId);\n} catch (e) {\n  const msg = e instanceof Error ? e.cause instanceof Error ? e.cause.message : e.message : String(e);\n  if (/already.*(cancel|terminal|completed|failed)/i.test(msg)) {\n    // already done - ignore\n  } else {\n    throw e;\n  }\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  return await manager.cancelTask(agentName, taskId);\n} catch (e) {\n  const cause = e instanceof Error ? e.cause : undefined;\n  const inner = cause instanceof Error ? cause.message : String(e);\n  // Treat already-terminal as success\n  if (/cancel|terminal|completed|failed/i.test(inner)) {\n    return await manager.getTask(agentName, taskId);\n  }\n  throw e;\n}","preventionTips":["Always inspect error.cause to see the real upstream failure code.","Only persist taskIds from responses of the same agent/context to avoid stale ids.","Log the prefixed message for traceability across agents."],"tags":["a2a","task","network","error-wrapping"],"backgroundTag":null,"analyzedSha":"5024443c7217464a66e98f80d73172a26440bd8f","analyzedAt":"2026-08-12T06:01:53.711Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}