{"record":{"id":"d23c8d17f5c30465","repo":"mastra-ai/mastra","slug":"no-active-run-to-decline-tool-call-for","errorCode":null,"errorMessage":"No active run to decline tool call for","messagePattern":"No active run to decline tool call for","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/agent-controller/session.ts","lineNumber":3789,"sourceCode":"    });\n  }\n\n  /**\n   * Decline a parked tool call: drive the agent to reject it. Throws when there\n   * is no active run.\n   */\n  async declineToolCall({\n    toolCallId,\n    requestContext: requestContextInput,\n    declineContext,\n  }: {\n    toolCallId?: string;\n    requestContext?: RequestContext;\n    declineContext?: { reason?: string; message?: string };\n  }): Promise<void> {\n    const runId = this.run.getRunId();\n    if (!runId) {\n      throw new Error('No active run to decline tool call for');\n    }\n\n    const agent = this.machinery.getAgent();\n    const requestContext = await this.machinery.buildRequestContext(requestContextInput);\n    const isYolo = (this.state.get() as Record<string, unknown>).yolo === true;\n    const threadId = this.thread.getId();\n    if (!threadId) {\n      throw new Error('Cannot decline a tool call without a current thread');\n    }\n    const resourceId = this.identity.getResourceId();\n    await agent.sendToolApproval({\n      threadId,\n      resourceId,\n      runId,\n      toolCallId,\n      approved: false,\n      declineContext,\n      requireToolApproval: !isYolo,","sourceCodeStart":3771,"sourceCodeEnd":3807,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/agent-controller/session.ts#L3771-L3807","documentation":"Thrown by the session's tool-call decline method when there is no active run. Declining mirrors approval: it needs a live run ID to route the decline decision through `agent.sendToolApproval`. When `this.run.getRunId()` is undefined (run never started, finished, or was reset), the library throws instead of silently dropping the decline.","triggerScenarios":"Calling the decline-tool-call method (with optional toolCallId, requestContext, declineContext) before a run starts, after it completes, or after cancellation so `this.run.getRunId()` returns undefined.","commonSituations":"User clicking 'Decline' after the agent run already terminated; duplicate decline handlers firing post-run; decline UI rendered from stale state after a session reset.","solutions":["Verify an active run exists (run ID present) before invoking decline.","If the run already ended, discard the pending decline — there is nothing to decline against.","Ensure the run kickoff is awaited before enabling decline UI handlers.","Restart the run if the decision must be delivered, letting the tool request re-issue."],"exampleFix":"// before\nawait session.declineToolCall({ toolCallId, declineContext: { reason: 'no' } });\n\n// after\nif (session.getRunId?.()) {\n  await session.declineToolCall({ toolCallId, declineContext: { reason: 'no' } });\n} else {\n  session.clearPendingToolRequest(toolCallId);\n}","handlingStrategy":"try-catch","validationCode":"// before declining\nconst runId = session.getRunId?.();\nif (!runId) {\n  console.warn('Run inactive; dropping decline for', toolCallId);\n  return;\n}","typeGuard":"function canDecline(session: { getRunId?: () => string | undefined }): boolean {\n  return typeof session.getRunId === 'function' && typeof session.getRunId() === 'string';\n}","tryCatchPattern":"try {\n  await session.declineToolCall({ toolCallId, declineContext });\n} catch (err) {\n  if (err instanceof Error && err.message === 'No active run to decline tool call for') {\n    // safe to ignore: nothing to decline against\n  } else throw err;\n}","preventionTips":["Subscribe to run lifecycle events and tear down decline handlers when the run ends.","Guard against duplicate decline submissions for the same toolCallId.","Render decline UI only while the run is awaiting tool approval."],"tags":["agent","tool-approval","lifecycle"],"backgroundTag":"no-active-run","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T08:17:16.595Z"}