{"record":{"id":"2b3dff85a0f8bedb","repo":"mastra-ai/mastra","slug":"no-active-run-to-approve-tool-call-for","errorCode":null,"errorMessage":"No active run to approve tool call for","messagePattern":"No active run to approve tool call for","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/core/src/agent-controller/session.ts","lineNumber":3749,"sourceCode":"    }\n\n    await this.resumeToolCall({ resumeData: response, toolCallId, requestContext });\n  }\n\n  /**\n   * Approve a parked tool call: drive the agent to execute it. Throws when there\n   * is no active run.\n   */\n  async approveToolCall({\n    toolCallId,\n    requestContext: requestContextInput,\n  }: {\n    toolCallId?: string;\n    requestContext?: RequestContext;\n  }): Promise<void> {\n    const runId = this.run.getRunId();\n    if (!runId) {\n      throw new Error('No active run to approve 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 approve 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: true,\n      requireToolApproval: !isYolo,\n      memory: { thread: threadId, resource: resourceId },","sourceCodeStart":3731,"sourceCodeEnd":3767,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/packages/core/src/agent-controller/session.ts#L3731-L3767","documentation":"Thrown by the agent session's tool-call approval method when no agent run is currently active. Approving a tool call requires an in-flight run that has requested approval; the run ID is obtained from the session's run state (`this.run.getRunId()`). If the run has completed, been cancelled, or was never started, there is nothing to attach the approval to, so the library fails fast with this error.","triggerScenarios":"Calling the session's approve-tool-call method (which passes an optional toolCallId and requestContext) before starting a run, after the run finished, or after the run was cancelled/reset so `this.run.getRunId()` returns undefined.","commonSituations":"UI code that caches an approval handler and fires it after the agent run already completed or errored; user clicking 'Approve' twice and the second click arriving after run termination; starting a new session without launching a generation and then attempting an approval.","solutions":["Check that a run is active before approving: read the run ID / run state from the session and only call approve when it exists.","Re-check run lifecycle events — if the run already completed or was cancelled, discard the pending approval instead of approving.","Ensure the run was actually started (await the generation/stream kickoff) before wiring approval UI.","If the run ended unexpectedly, restart the run and re-trigger the tool call so a fresh approval request is issued."],"exampleFix":"// before\nawait session.approveToolCall({ toolCallId }); // run may be gone\n\n// after\nconst runId = session.getRunId?.();\nif (runId) {\n  await session.approveToolCall({ toolCallId });\n} else {\n  console.warn('Run no longer active; skipping approval');\n}","handlingStrategy":"try-catch","validationCode":"// before approving\nconst runId = session.getRunId?.();\nif (!runId) throw new Error('Cannot approve: no active run');","typeGuard":"function hasActiveRun(session: { getRunId?: () => string | undefined }): session is { getRunId: () => string } {\n  return typeof session.getRunId === 'function' && typeof session.getRunId() === 'string';\n}","tryCatchPattern":"try {\n  await session.approveToolCall({ toolCallId });\n} catch (err) {\n  if (err instanceof Error && err.message === 'No active run to approve tool call for') {\n    // run already ended; clear pending approval UI\n  } else throw err;\n}","preventionTips":["Gate approval UI on the run's active/streaming state.","Remove approval handlers on run completion and cancellation events.","Debounce or disable approval buttons after first click."],"tags":["agent","tool-approval","lifecycle"],"backgroundTag":"no-active-run","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}