@mastra/spanner 1.7.0 → 1.8.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Spanner } from "@google-cloud/spanner";
2
2
  import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
3
- import { AGENTS_SCHEMA, AGENT_VERSIONS_SCHEMA, AgentsStorage, BackgroundTasksStorage, BlobStore, ChannelsStorage, DatasetsStorage, ExperimentsStorage, FavoritesStorage, MCPClientsStorage, MCPServersStorage, MCP_CLIENTS_SCHEMA, MCP_CLIENT_VERSIONS_SCHEMA, MCP_SERVERS_SCHEMA, MCP_SERVER_VERSIONS_SCHEMA, METRIC_DISTINCT_COLUMNS, MastraCompositeStore, MemoryStorage, ObservabilityStorage, PROMPT_BLOCKS_SCHEMA, PROMPT_BLOCK_VERSIONS_SCHEMA, PromptBlocksStorage, SCORER_DEFINITIONS_SCHEMA, SCORER_DEFINITION_VERSIONS_SCHEMA, SKILLS_SCHEMA, SKILL_BLOBS_SCHEMA, SKILL_VERSIONS_SCHEMA, SchedulesStorage, ScorerDefinitionsStorage, ScoresStorage, SkillsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_BACKGROUND_TASKS, TABLE_CHANNEL_CONFIG, TABLE_CHANNEL_INSTALLATIONS, TABLE_CONFIGS, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, TABLE_FAVORITES, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, TABLE_MESSAGES, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, TABLE_RESOURCES, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, TABLE_SCHEMAS, TABLE_SCORERS, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, TABLE_SKILLS, TABLE_SKILL_BLOBS, TABLE_SKILL_VERSIONS, TABLE_SPANS, TABLE_THREADS, TABLE_WORKFLOW_DEFINITIONS, TABLE_WORKFLOW_SNAPSHOT, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, TraceStatus, WORKFLOW_DEFINITIONS_SCHEMA, WORKSPACES_SCHEMA, WORKSPACE_VERSIONS_SCHEMA, WorkflowDefinitionsStorage, WorkflowsStorage, WorkspacesStorage, calculatePagination, createStorageErrorId, getDefaultValue, hasErrorCode, listMetricsArgsSchema, listTracesArgsSchema, matchesExpectedWorkflowStatus, normalizePerPage, normalizeScheduleTarget, toTraceSpans, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
3
+ import { AGENTS_SCHEMA, AGENT_VERSIONS_SCHEMA, AgentsStorage, BackgroundTasksStorage, BlobStore, ChannelsStorage, DatasetsStorage, ExperimentsStorage, FavoritesStorage, MCPClientsStorage, MCPServersStorage, MCP_CLIENTS_SCHEMA, MCP_CLIENT_VERSIONS_SCHEMA, MCP_SERVERS_SCHEMA, MCP_SERVER_VERSIONS_SCHEMA, METRIC_DISTINCT_COLUMNS, MastraCompositeStore, MemoryStorage, ObservabilityStorage, PROMPT_BLOCKS_SCHEMA, PROMPT_BLOCK_VERSIONS_SCHEMA, PromptBlocksStorage, SCORER_DEFINITIONS_SCHEMA, SCORER_DEFINITION_VERSIONS_SCHEMA, SKILLS_SCHEMA, SKILL_BLOBS_SCHEMA, SKILL_VERSIONS_SCHEMA, SchedulesStorage, ScorerDefinitionsStorage, ScoresStorage, SkillsStorage, TABLE_AGENTS, TABLE_AGENT_VERSIONS, TABLE_BACKGROUND_TASKS, TABLE_CHANNEL_CONFIG, TABLE_CHANNEL_INSTALLATIONS, TABLE_CONFIGS, TABLE_DATASETS, TABLE_DATASET_ITEMS, TABLE_DATASET_VERSIONS, TABLE_EXPERIMENTS, TABLE_EXPERIMENT_RESULTS, TABLE_FAVORITES, TABLE_MCP_CLIENTS, TABLE_MCP_CLIENT_VERSIONS, TABLE_MCP_SERVERS, TABLE_MCP_SERVER_VERSIONS, TABLE_MESSAGES, TABLE_PROMPT_BLOCKS, TABLE_PROMPT_BLOCK_VERSIONS, TABLE_RESOURCES, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, TABLE_SCHEMAS, TABLE_SCORERS, TABLE_SCORER_DEFINITIONS, TABLE_SCORER_DEFINITION_VERSIONS, TABLE_SKILLS, TABLE_SKILL_BLOBS, TABLE_SKILL_VERSIONS, TABLE_SPANS, TABLE_THREADS, TABLE_WORKFLOW_DEFINITIONS, TABLE_WORKFLOW_SNAPSHOT, TABLE_WORKSPACES, TABLE_WORKSPACE_VERSIONS, TraceStatus, WORKFLOW_DEFINITIONS_SCHEMA, WORKSPACES_SCHEMA, WORKSPACE_VERSIONS_SCHEMA, WorkflowDefinitionsStorage, WorkflowsStorage, WorkspacesStorage, calculatePagination, createStorageErrorId, executeRetentionPrune, getDefaultValue, hasErrorCode, listMetricsArgsSchema, listTracesArgsSchema, matchesExpectedWorkflowStatus, normalizePerPage, normalizeScheduleTarget, resolveRetentionTargets, retentionCutoffMs, toTraceSpans, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
4
4
  import { MastraBase } from "@mastra/core/base";
5
5
  import { parseSqlIdentifier } from "@mastra/core/utils";
6
6
  import { randomUUID } from "crypto";
@@ -8861,6 +8861,18 @@ function invalidTraceFilterKey(kind, key) {
8861
8861
  * waterfall view, and root-span pagination with the listTraces filters.
8862
8862
  */
8863
8863
  var ObservabilitySpanner = class ObservabilitySpanner extends ObservabilityStorage {
8864
+ static retentionTables = {
8865
+ spans: {
8866
+ table: TABLE_SPANS,
8867
+ column: "startedAt",
8868
+ indexed: false
8869
+ },
8870
+ metrics: {
8871
+ table: TABLE_AI_METRICS,
8872
+ column: "timestamp",
8873
+ indexed: false
8874
+ }
8875
+ };
8864
8876
  database;
8865
8877
  db;
8866
8878
  skipDefaultIndexes;
@@ -8895,6 +8907,50 @@ var ObservabilitySpanner = class ObservabilitySpanner extends ObservabilityStora
8895
8907
  this.disableMetrics = disableMetrics ?? true;
8896
8908
  this.indexes = indexes?.filter((idx) => ObservabilitySpanner.MANAGED_TABLES.includes(idx.table));
8897
8909
  }
8910
+ async prune(policies, options) {
8911
+ return executeRetentionPrune({
8912
+ domain: "observability",
8913
+ targets: resolveRetentionTargets({
8914
+ policies,
8915
+ descriptor: this.disableMetrics ? { spans: {
8916
+ table: TABLE_SPANS,
8917
+ column: "startedAt",
8918
+ indexed: false
8919
+ } } : ObservabilitySpanner.retentionTables,
8920
+ order: ["spans", "metrics"]
8921
+ }),
8922
+ options,
8923
+ cutoffFor: (target, now) => new Date(retentionCutoffMs(target.policy, now)).toISOString(),
8924
+ deleteBatch: async (target, cutoff, limit) => {
8925
+ const table = quoteIdent(target.table, "table name");
8926
+ const column = quoteIdent(target.column, "column name");
8927
+ const keyPredicate = target.table === TABLE_SPANS ? `(traceId, spanId) IN (SELECT AS STRUCT traceId, spanId FROM ${table} WHERE ${column} < @cutoff ORDER BY ${column} LIMIT @limit)` : `metricId IN (SELECT metricId FROM ${table} WHERE ${column} < @cutoff ORDER BY ${column} LIMIT @limit)`;
8928
+ return this.db.runWithAbortRetry(async () => {
8929
+ let deleted = 0;
8930
+ await this.database.runTransactionAsync(async (tx) => {
8931
+ try {
8932
+ const [count] = await tx.runUpdate({
8933
+ sql: `DELETE FROM ${table} WHERE ${keyPredicate}`,
8934
+ params: {
8935
+ cutoff,
8936
+ limit
8937
+ },
8938
+ types: { cutoff: "timestamp" }
8939
+ });
8940
+ deleted = Number(count ?? 0);
8941
+ await tx.commit();
8942
+ } catch (error) {
8943
+ await tx.rollback().catch((rollbackError) => {
8944
+ throw new AggregateError([error, rollbackError], "Transaction and rollback both failed");
8945
+ });
8946
+ throw error;
8947
+ }
8948
+ });
8949
+ return deleted;
8950
+ });
8951
+ }
8952
+ });
8953
+ }
8898
8954
  /** Build the read-options object that every metrics read path threads through. */
8899
8955
  readOptions() {
8900
8956
  return { stalenessMs: this.dashboardStalenessMs };
@@ -13370,7 +13426,8 @@ var SpannerStore = class extends MastraCompositeStore {
13370
13426
  super({
13371
13427
  id: config.id,
13372
13428
  name: "SpannerStore",
13373
- disableInit: config.disableInit
13429
+ disableInit: config.disableInit,
13430
+ retention: config.retention
13374
13431
  });
13375
13432
  try {
13376
13433
  if (isPreConfiguredDatabase(config)) {