@mastra/convex 1.7.3-alpha.0 → 1.7.3

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.
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_server = require("../server-cdXFJzWz.cjs");
2
+ const require_server = require("../server-Bcgr5CcA.cjs");
3
3
  const require_schema = require("../schema.cjs");
4
4
  let _mastra_core_storage_constants = require("@mastra/core/storage/constants");
5
5
  exports.TABLE_BACKGROUND_TASKS = _mastra_core_storage_constants.TABLE_BACKGROUND_TASKS;
@@ -1,3 +1,3 @@
1
- import { a as mastraCache, i as mastraStorage, n as mastraNativeVectorMutation, r as mastraNativeVectorQuery, t as mastraNativeVectorAction } from "../server-DozOFGfO.js";
1
+ import { a as mastraCache, i as mastraStorage, n as mastraNativeVectorMutation, r as mastraNativeVectorQuery, t as mastraNativeVectorAction } from "../server-B8RSiOmu.js";
2
2
  import { TABLE_BACKGROUND_TASKS, TABLE_CHANNEL_CONFIG, TABLE_CHANNEL_INSTALLATIONS, TABLE_MESSAGES, TABLE_OBSERVATIONAL_MEMORY, TABLE_RESOURCES, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, TABLE_SCORERS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, defineMastraNativeVectorTable, mastraBackgroundTasksTable, mastraCacheListItemsTable, mastraCacheTable, mastraChannelConfigTable, mastraChannelInstallationsTable, mastraDocumentsTable, mastraMessagesTable, mastraObservationalMemoryTable, mastraResourcesTable, mastraScheduleTriggersTable, mastraSchedulesTable, mastraScoresTable, mastraThreadsTable, mastraVectorIndexesTable, mastraVectorsTable, mastraWorkflowSnapshotsTable } from "../schema.js";
3
3
  export { TABLE_BACKGROUND_TASKS, TABLE_CHANNEL_CONFIG, TABLE_CHANNEL_INSTALLATIONS, TABLE_MESSAGES, TABLE_OBSERVATIONAL_MEMORY, TABLE_RESOURCES, TABLE_SCHEDULES, TABLE_SCHEDULE_TRIGGERS, TABLE_SCORERS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, defineMastraNativeVectorTable, mastraBackgroundTasksTable, mastraCache, mastraCacheListItemsTable, mastraCacheTable, mastraChannelConfigTable, mastraChannelInstallationsTable, mastraDocumentsTable, mastraMessagesTable, mastraNativeVectorAction, mastraNativeVectorMutation, mastraNativeVectorQuery, mastraObservationalMemoryTable, mastraResourcesTable, mastraScheduleTriggersTable, mastraSchedulesTable, mastraScoresTable, mastraStorage, mastraThreadsTable, mastraVectorIndexesTable, mastraVectorsTable, mastraWorkflowSnapshotsTable };
@@ -0,0 +1,14 @@
1
+ import type { GenericMutationCtx as MutationCtx } from 'convex/server';
2
+ import type { RunClaimRequest, StorageRequest, StorageResponse } from '../storage/types.js';
3
+ export declare const RUN_FENCE_CONFLICT_CODE = "STORAGE_RUN_FENCE_CONFLICT";
4
+ export declare function isRunClaimRequest(request: StorageRequest | RunClaimRequest): request is RunClaimRequest;
5
+ /**
6
+ * Runs a run fencing operation. Each reads and writes the run's claim in the
7
+ * calling mutation, so Convex serializes it against every other claim
8
+ * operation and fenced write on the same run. Leases are measured on the
9
+ * mutation's clock.
10
+ *
11
+ * `route` runs the request a fenced operation wraps.
12
+ */
13
+ export declare function handleRunClaimOperation(ctx: MutationCtx<any>, request: RunClaimRequest, route: (ctx: MutationCtx<any>, request: StorageRequest) => Promise<StorageResponse>): Promise<StorageResponse>;
14
+ //# sourceMappingURL=run-fencing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run-fencing.d.ts","sourceRoot":"","sources":["../../src/server/run-fencing.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,IAAI,WAAW,EAAE,MAAM,eAAe,CAAC;AAGvE,OAAO,KAAK,EAEV,eAAe,EAGf,cAAc,EACd,eAAe,EAEhB,MAAM,kBAAkB,CAAC;AAI1B,eAAO,MAAM,uBAAuB,+BAA+B,CAAC;AAgBpE,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,cAAc,GAAG,eAAe,GAAG,OAAO,IAAI,eAAe,CAEvG;AAiCD;;;;;;;GAOG;AACH,wBAAsB,uBAAuB,CAC3C,GAAG,EAAE,WAAW,CAAC,GAAG,CAAC,EACrB,OAAO,EAAE,eAAe,EACxB,KAAK,EAAE,CAAC,GAAG,EAAE,WAAW,CAAC,GAAG,CAAC,EAAE,OAAO,EAAE,cAAc,KAAK,OAAO,CAAC,eAAe,CAAC,GAClF,OAAO,CAAC,eAAe,CAAC,CA6F1B"}
@@ -1,10 +1,10 @@
1
1
  import type { GenericMutationCtx as MutationCtx } from 'convex/server';
2
- import type { StorageRequest, StorageResponse } from '../storage/types.js';
2
+ import type { RunClaimRequest, StorageRequest, StorageResponse } from '../storage/types.js';
3
3
  /**
4
4
  * Main storage mutation handler.
5
5
  * Routes operations to the appropriate typed table.
6
6
  */
7
- export declare const mastraStorage: import("convex/server").RegisteredMutation<"public", StorageRequest, Promise<StorageResponse>>;
7
+ export declare const mastraStorage: import("convex/server").RegisteredMutation<"public", RunClaimRequest | StorageRequest, Promise<StorageResponse>>;
8
8
  /**
9
9
  * Handle operations on typed tables (threads, messages, etc.)
10
10
  * Records are stored with their `id` field as a regular field (not _id).
@@ -1 +1 @@
1
- {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../src/server/storage.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,kBAAkB,IAAI,WAAW,EAAE,MAAM,eAAe,CAAC;AAIvE,OAAO,KAAK,EAAkB,cAAc,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAkRxF;;;GAGG;AACH,eAAO,MAAM,aAAa,gGA+BxB,CAAC;AA8BH;;;;GAIG;AACH,wBAAsB,oBAAoB,CACxC,GAAG,EAAE,WAAW,CAAC,GAAG,CAAC,EACrB,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,eAAe,CAAC,CA+hB1B"}
1
+ {"version":3,"file":"storage.d.ts","sourceRoot":"","sources":["../../src/server/storage.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EAAE,kBAAkB,IAAI,WAAW,EAAE,MAAM,eAAe,CAAC;AAIvE,OAAO,KAAK,EAAkB,eAAe,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAmRzG;;;GAGG;AACH,eAAO,MAAM,aAAa,kHAOzB,CAAC;AA+DF;;;;GAIG;AACH,wBAAsB,oBAAoB,CACxC,GAAG,EAAE,WAAW,CAAC,GAAG,CAAC,EACrB,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,eAAe,CAAC,CA+hB1B"}
@@ -1022,6 +1022,152 @@ async function handleObservationalMemoryOperation(ctx, convexTable, request) {
1022
1022
  }
1023
1023
  }
1024
1024
  //#endregion
1025
+ //#region src/server/run-fencing.ts
1026
+ const RUN_FENCE_CONFLICT_CODE = "STORAGE_RUN_FENCE_CONFLICT";
1027
+ const RUN_CLAIM_OPS = /* @__PURE__ */ new Set([
1028
+ "claimRunOwnership",
1029
+ "renewRunOwnership",
1030
+ "releaseRunOwnership",
1031
+ "getRunOwnership",
1032
+ "raiseRunFence",
1033
+ "retireRunFence",
1034
+ "fenced"
1035
+ ]);
1036
+ const RUN_CLAIM_TABLES = /* @__PURE__ */ new Set(["mastra_workflow_run_owners", "mastra_memory_run_fences"]);
1037
+ const CLAIMS_TABLE = "mastra_documents";
1038
+ function isRunClaimRequest(request) {
1039
+ return RUN_CLAIM_OPS.has(request.op);
1040
+ }
1041
+ async function findClaim(ctx, table, runId) {
1042
+ return ctx.db.query(CLAIMS_TABLE).withIndex("by_table_primary", (q) => q.eq("table", table).eq("primaryKey", runId)).unique();
1043
+ }
1044
+ async function writeClaim(ctx, table, doc, claim) {
1045
+ const record = {
1046
+ id: claim.runId,
1047
+ ...claim
1048
+ };
1049
+ if (doc) await ctx.db.patch(doc._id, { record });
1050
+ else await ctx.db.insert(CLAIMS_TABLE, {
1051
+ table,
1052
+ primaryKey: claim.runId,
1053
+ record
1054
+ });
1055
+ return claim;
1056
+ }
1057
+ function holds(claim, fence) {
1058
+ return claim !== null && claim.generation === fence.generation && claim.ownerId === fence.ownerId;
1059
+ }
1060
+ function reply(applied, claim, now) {
1061
+ return {
1062
+ ok: true,
1063
+ result: {
1064
+ applied,
1065
+ claim,
1066
+ now
1067
+ }
1068
+ };
1069
+ }
1070
+ /**
1071
+ * Runs a run fencing operation. Each reads and writes the run's claim in the
1072
+ * calling mutation, so Convex serializes it against every other claim
1073
+ * operation and fenced write on the same run. Leases are measured on the
1074
+ * mutation's clock.
1075
+ *
1076
+ * `route` runs the request a fenced operation wraps.
1077
+ */
1078
+ async function handleRunClaimOperation(ctx, request, route) {
1079
+ if (!RUN_CLAIM_TABLES.has(request.tableName)) return {
1080
+ ok: false,
1081
+ error: `Unsupported run claim table ${request.tableName}`
1082
+ };
1083
+ const now = Date.now();
1084
+ switch (request.op) {
1085
+ case "claimRunOwnership": {
1086
+ const { tableName, runId, ownerId, leaseMs, force, expectedGeneration } = request;
1087
+ const doc = await findClaim(ctx, tableName, runId);
1088
+ const current = doc?.record ?? null;
1089
+ const leaseExpiresAt = now + leaseMs;
1090
+ if (!current) {
1091
+ if (expectedGeneration !== void 0 && expectedGeneration !== 0) return reply(false, null, now);
1092
+ return reply(true, await writeClaim(ctx, tableName, doc, {
1093
+ runId,
1094
+ generation: Math.max(1, now),
1095
+ ownerId,
1096
+ leaseExpiresAt,
1097
+ updatedAt: now
1098
+ }), now);
1099
+ }
1100
+ if (expectedGeneration !== void 0 && current.generation !== expectedGeneration) return reply(false, current, now);
1101
+ if (!force && current.leaseExpiresAt !== null && current.leaseExpiresAt > now) return reply(false, current, now);
1102
+ return reply(true, await writeClaim(ctx, tableName, doc, {
1103
+ runId,
1104
+ generation: current.generation + 1,
1105
+ ownerId,
1106
+ leaseExpiresAt,
1107
+ updatedAt: now
1108
+ }), now);
1109
+ }
1110
+ case "renewRunOwnership": {
1111
+ const { tableName, fence, leaseMs } = request;
1112
+ const doc = await findClaim(ctx, tableName, fence.runId);
1113
+ const current = doc?.record ?? null;
1114
+ if (!holds(current, fence) || current.leaseExpiresAt === null) return reply(false, current, now);
1115
+ return reply(true, await writeClaim(ctx, tableName, doc, {
1116
+ ...current,
1117
+ leaseExpiresAt: now + leaseMs,
1118
+ updatedAt: now
1119
+ }), now);
1120
+ }
1121
+ case "releaseRunOwnership": {
1122
+ const { tableName, fence } = request;
1123
+ const doc = await findClaim(ctx, tableName, fence.runId);
1124
+ const current = doc?.record ?? null;
1125
+ if (!holds(current, fence)) return reply(false, current, now);
1126
+ return reply(true, await writeClaim(ctx, tableName, doc, {
1127
+ ...current,
1128
+ leaseExpiresAt: null,
1129
+ updatedAt: now
1130
+ }), now);
1131
+ }
1132
+ case "getRunOwnership": return reply(true, (await findClaim(ctx, request.tableName, request.runId))?.record ?? null, now);
1133
+ case "raiseRunFence": {
1134
+ const { tableName, fence } = request;
1135
+ const doc = await findClaim(ctx, tableName, fence.runId);
1136
+ const current = doc?.record ?? null;
1137
+ if (!current || current.generation < fence.generation || holds(current, fence)) return reply(true, await writeClaim(ctx, tableName, doc, {
1138
+ ...fence,
1139
+ leaseExpiresAt: null,
1140
+ retiredAt: null
1141
+ }), now);
1142
+ return reply(false, current, now);
1143
+ }
1144
+ case "retireRunFence": {
1145
+ const { tableName, fence } = request;
1146
+ const doc = await findClaim(ctx, tableName, fence.runId);
1147
+ const current = doc?.record ?? null;
1148
+ if (!holds(current, fence)) return reply(false, current, now);
1149
+ return reply(true, await writeClaim(ctx, tableName, doc, {
1150
+ ...current,
1151
+ retiredAt: now
1152
+ }), now);
1153
+ }
1154
+ case "fenced": {
1155
+ const { tableName, fence, request: inner } = request;
1156
+ if (!holds((await findClaim(ctx, tableName, fence.runId))?.record ?? null, fence)) return {
1157
+ ok: false,
1158
+ error: `Run ${fence.runId} is no longer claimed by generation ${fence.generation} of ${fence.ownerId}`,
1159
+ code: RUN_FENCE_CONFLICT_CODE
1160
+ };
1161
+ if (!inner) return { ok: true };
1162
+ if (RUN_CLAIM_OPS.has(inner.op)) return {
1163
+ ok: false,
1164
+ error: `Unsupported fenced operation ${inner.op}`
1165
+ };
1166
+ return route(ctx, inner);
1167
+ }
1168
+ }
1169
+ }
1170
+ //#endregion
1025
1171
  //#region src/server/workflow-snapshot.ts
1026
1172
  const PENDING_MARKER_KEY = "__mastra_pending__";
1027
1173
  function isPendingMarker(val) {
@@ -1303,6 +1449,10 @@ function resolveTable(tableName) {
1303
1449
  * Routes operations to the appropriate typed table.
1304
1450
  */
1305
1451
  const mastraStorage = mutationGeneric(async (ctx, request) => {
1452
+ if (isRunClaimRequest(request)) return handleRunClaimOperation(ctx, request, routeStorageRequest);
1453
+ return routeStorageRequest(ctx, request);
1454
+ });
1455
+ async function routeStorageRequest(ctx, request) {
1306
1456
  try {
1307
1457
  const { convexTable, isTyped } = resolveTable(request.tableName);
1308
1458
  if (request.tableName.startsWith(VECTOR_TABLE_PREFIX) && request.tableName !== TABLE_VECTOR_INDEXES) return await handleVectorOperation(ctx, request);
@@ -1322,7 +1472,7 @@ const mastraStorage = mutationGeneric(async (ctx, request) => {
1322
1472
  error: error.message
1323
1473
  };
1324
1474
  }
1325
- });
1475
+ }
1326
1476
  function parseStoredSnapshot(stored, runId) {
1327
1477
  if (typeof stored === "string") return JSON.parse(stored);
1328
1478
  return JSON.parse(JSON.stringify(stored ?? createEmptyWorkflowSnapshot(runId)));
@@ -2143,4 +2293,4 @@ const mastraNativeVectorMutation = mutationGeneric({
2143
2293
  //#endregion
2144
2294
  export { mastraCache as a, mastraStorage as i, mastraNativeVectorMutation as n, mastraNativeVectorQuery as r, mastraNativeVectorAction as t };
2145
2295
 
2146
- //# sourceMappingURL=server-DozOFGfO.js.map
2296
+ //# sourceMappingURL=server-B8RSiOmu.js.map