mastra-ai/mastra · error · HTTPException

Version with id ${to} not found for agent ${agentId}

Error message

Version with id ${to} not found for agent ${agentId}

What it means

Thrown by the GET /stored/agents/:agentId/versions/compare handler when the `to` version exists in storage but belongs to a different agent than the :agentId path parameter. Returned as 404 to avoid leaking cross-agent version existence.

Source

Thrown at packages/server/src/server/handlers/agent-versions.ts:545

      }
      const agent = await agentsStore.getById(agentId);
      assertStoredResourceScope(agent, await getStoredResourceScope(mastra, requestContext));

      // Get both versions
      const fromVersion = await agentsStore.getVersion(from);
      if (!fromVersion) {
        throw new HTTPException(404, { message: `Version with id ${from} not found` });
      }
      if (fromVersion.agentId !== agentId) {
        throw new HTTPException(404, { message: `Version with id ${from} not found for agent ${agentId}` });
      }

      const toVersion = await agentsStore.getVersion(to);
      if (!toVersion) {
        throw new HTTPException(404, { message: `Version with id ${to} not found` });
      }
      if (toVersion.agentId !== agentId) {
        throw new HTTPException(404, { message: `Version with id ${to} not found for agent ${agentId}` });
      }

      // Extract config fields from both versions (top-level, no .snapshot)
      const fromConfig = extractConfigFromVersion(
        fromVersion as unknown as Record<string, unknown>,
        SNAPSHOT_CONFIG_FIELDS,
      );
      const toConfig = extractConfigFromVersion(
        toVersion as unknown as Record<string, unknown>,
        SNAPSHOT_CONFIG_FIELDS,
      );

      // Compute diffs on the config fields
      const diffs = computeVersionDiffs(fromConfig, toConfig);

      return {
        diffs,
        fromVersion,

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Ensure both `from` and `to` version ids belong to the same :agentId in the path
  2. Fix the path agentId if cross-agent comparison was unintentional via the wrong route
  3. Refetch version lists per-agent and compare only within one agent

Example fix

// before
GET /stored/agents/agent-a/versions/compare?from=va1&to=vb2  // vb2 belongs to agent-b
// after
GET /stored/agents/agent-b/versions/compare?from=vb1&to=vb2
Defensive patterns

Strategy: validation

Validate before calling

const [fromList, toList] = await Promise.all([
  fetch(`/api/stored/agents/${fromAgentId}/versions`).then(r => r.json()),
  fetch(`/api/stored/agents/${toAgentId}/versions`).then(r => r.json()),
]);
if (fromAgentId !== toAgentId) {
  throw new Error('Cross-agent version comparison is not supported');
}

Type guard

function sameAgent(from: { agentId: string }, to: { agentId: string }, agentId: string): boolean {
  return from.agentId === agentId && to.agentId === agentId;
}

Try / catch

try {
  const res = await compareVersions(agentId, fromId, toId);
} catch (e) {
  if (isHttpException(e, 404)) {
    // both ids must resolve within this agent's version list; reselect
  }
}

Prevention

When it happens

Trigger: Calling compare-versions with a `to` version id owned by another agent, e.g. GET /stored/agents/agent-a/versions/compare?from=<agent-a-version>&to=<agent-b-version>. Also occurs when both versions come from different agents' version lists.

Common situations: Comparing versions across two agents by mistake (e.g. diffing prod agent vs staging agent ids); copy/paste errors in multi-agent admin UIs; tests parameterized over versions of multiple agents.

Related errors


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