mastra-ai/mastra · error

No active run to approve tool call for

Error message

No active run to approve tool call for

What it means

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.

Source

Thrown at packages/core/src/agent-controller/session.ts:3749

    }

    await this.resumeToolCall({ resumeData: response, toolCallId, requestContext });
  }

  /**
   * Approve a parked tool call: drive the agent to execute it. Throws when there
   * is no active run.
   */
  async approveToolCall({
    toolCallId,
    requestContext: requestContextInput,
  }: {
    toolCallId?: string;
    requestContext?: RequestContext;
  }): Promise<void> {
    const runId = this.run.getRunId();
    if (!runId) {
      throw new Error('No active run to approve tool call for');
    }

    const agent = this.machinery.getAgent();
    const requestContext = await this.machinery.buildRequestContext(requestContextInput);
    const isYolo = (this.state.get() as Record<string, unknown>).yolo === true;
    const threadId = this.thread.getId();
    if (!threadId) {
      throw new Error('Cannot approve a tool call without a current thread');
    }
    const resourceId = this.identity.getResourceId();
    await agent.sendToolApproval({
      threadId,
      resourceId,
      runId,
      toolCallId,
      approved: true,
      requireToolApproval: !isYolo,
      memory: { thread: threadId, resource: resourceId },

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Check that a run is active before approving: read the run ID / run state from the session and only call approve when it exists.
  2. Re-check run lifecycle events — if the run already completed or was cancelled, discard the pending approval instead of approving.
  3. Ensure the run was actually started (await the generation/stream kickoff) before wiring approval UI.
  4. If the run ended unexpectedly, restart the run and re-trigger the tool call so a fresh approval request is issued.

Example fix

// before
await session.approveToolCall({ toolCallId }); // run may be gone

// after
const runId = session.getRunId?.();
if (runId) {
  await session.approveToolCall({ toolCallId });
} else {
  console.warn('Run no longer active; skipping approval');
}
Defensive patterns

Strategy: try-catch

Validate before calling

// before approving
const runId = session.getRunId?.();
if (!runId) throw new Error('Cannot approve: no active run');

Type guard

function hasActiveRun(session: { getRunId?: () => string | undefined }): session is { getRunId: () => string } {
  return typeof session.getRunId === 'function' && typeof session.getRunId() === 'string';
}

Try / catch

try {
  await session.approveToolCall({ toolCallId });
} catch (err) {
  if (err instanceof Error && err.message === 'No active run to approve tool call for') {
    // run already ended; clear pending approval UI
  } else throw err;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30). Data as JSON: /api/errors/2b3dff85a0f8bedb. Report an issue: GitHub.