mastra-ai/mastra · error · HTTPException

Failed to retrieve created version

Error message

Failed to retrieve created version

What it means

A 500 thrown right after createVersionWithRetry succeeds: the handler calls agentsStore.getVersion(versionId) to return the newly created snapshot, and it comes back undefined. This is an internal consistency failure — the store reported a created versionId but cannot read it back.

Source

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

        : null;

      const changedFields = calculateChangedFields(previousConfig, currentConfig);

      // Create the new version with retry logic to handle race conditions
      // Config fields are passed top-level
      const { versionId } = await createVersionWithRetry(
        agentsStore as unknown as VersionedStoreInterface,
        agentId,
        'agentId',
        currentConfig,
        changedFields,
        { changeMessage },
      );

      // Get the created version to return
      const version = await agentsStore.getVersion(versionId);
      if (!version) {
        throw new HTTPException(500, { message: 'Failed to retrieve created version' });
      }

      // Enforce retention limit - delete oldest versions if we exceed the max
      await enforceRetentionLimit(
        agentsStore as unknown as VersionedStoreInterface,
        agentId,
        'agentId',
        agent.activeVersionId,
      );

      return version;
    } catch (error) {
      return handleError(error, 'Error creating agent version');
    }
  },
});

/**

View on GitHub (pinned to 75dd419e61)

Solutions

  1. Retry the request — transient read-after-write races usually resolve
  2. If using a custom VersionedStore, make getVersion immediately consistent after createVersion (read-your-writes)
  3. Check for concurrent deletion paths (retention enforcement, manual deletes) targeting the same agent
  4. Upgrade @mastra/core/server storage packages if using a maintained adapter with this bug
Defensive patterns

Strategy: retry

Try / catch

let lastErr: unknown;
for (let i = 0; i < 3; i++) {
  try {
    const res = await fetch(`/api/stored/agents/${agentId}/versions`, { method: 'POST', body });
    if (res.ok) return await res.json();
    if (res.status !== 500) throw new Error(`Create failed: ${res.status}`);
  } catch (e) { lastErr = e; }
  await new Promise(r => setTimeout(r, 2 ** i * 100));
}
throw new Error(`Failed to retrieve created version after retries: ${lastErr}`);

Prevention

When it happens

Trigger: createVersionWithRetry returns a versionId but a subsequent getVersion(versionId) finds no row — e.g. an eventual-consistency read lag, a concurrent retention-limit deletion, or a buggy custom VersionedStore whose create/get are inconsistent.

Common situations: Custom storage adapters with non-atomic create-then-read behavior; concurrent requests racing with enforceRetentionLimit deleting rows; replication lag on a read replica.

Related errors


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