@mastra/convex 1.7.3-alpha.0 → 1.7.3-alpha.1

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,7 +1,7 @@
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
  import { MastraServerCache } from "@mastra/core/cache";
4
- import { BackgroundTasksStorage, ChannelsStorage, MastraCompositeStore, MemoryStorage, SchedulesStorage, ScoresStorage, TABLE_BACKGROUND_TASKS as TABLE_BACKGROUND_TASKS$1, TABLE_CHANNEL_CONFIG as TABLE_CHANNEL_CONFIG$1, TABLE_CHANNEL_INSTALLATIONS as TABLE_CHANNEL_INSTALLATIONS$1, TABLE_MESSAGES as TABLE_MESSAGES$1, TABLE_RESOURCES as TABLE_RESOURCES$1, TABLE_SCHEDULES as TABLE_SCHEDULES$1, TABLE_SCHEDULE_TRIGGERS as TABLE_SCHEDULE_TRIGGERS$1, TABLE_SCORERS as TABLE_SCORERS$1, TABLE_THREADS as TABLE_THREADS$1, TABLE_WORKFLOW_SNAPSHOT as TABLE_WORKFLOW_SNAPSHOT$1, WorkflowsStorage, calculatePagination, createStorageErrorId, createVectorErrorId, filterByDateRange, normalizePerPage, normalizeScheduleTarget, safelyParseJSON, storageMessageMatchesMetadataFilter, validateStorageMetadataFilter } from "@mastra/core/storage";
4
+ import { BackgroundTasksStorage, ChannelsStorage, MastraCompositeStore, MemoryStorage, RUN_FENCE_CONFLICT_ERROR_ID, RunFenceConflictError, SchedulesStorage, ScoresStorage, TABLE_BACKGROUND_TASKS as TABLE_BACKGROUND_TASKS$1, TABLE_CHANNEL_CONFIG as TABLE_CHANNEL_CONFIG$1, TABLE_CHANNEL_INSTALLATIONS as TABLE_CHANNEL_INSTALLATIONS$1, TABLE_MEMORY_RUN_FENCES, TABLE_MESSAGES as TABLE_MESSAGES$1, TABLE_RESOURCES as TABLE_RESOURCES$1, TABLE_SCHEDULES as TABLE_SCHEDULES$1, TABLE_SCHEDULE_TRIGGERS as TABLE_SCHEDULE_TRIGGERS$1, TABLE_SCORERS as TABLE_SCORERS$1, TABLE_THREADS as TABLE_THREADS$1, TABLE_WORKFLOW_RUN_OWNERS, TABLE_WORKFLOW_SNAPSHOT as TABLE_WORKFLOW_SNAPSHOT$1, WorkflowsStorage, calculatePagination, createStorageErrorId, createVectorErrorId, filterByDateRange, normalizePerPage, normalizeScheduleTarget, resolveRunFence, safelyParseJSON, storageMessageMatchesMetadataFilter, validateStorageMetadataFilter } from "@mastra/core/storage";
5
5
  import { MastraBase } from "@mastra/core/base";
6
6
  import { MessageList } from "@mastra/core/agent";
7
7
  import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
@@ -252,6 +252,154 @@ var ConvexAdminClient = class {
252
252
  }
253
253
  };
254
254
  //#endregion
255
+ //#region src/storage/run-fencing.ts
256
+ const READ_OPS = /* @__PURE__ */ new Set([
257
+ "load",
258
+ "loadMany",
259
+ "queryTable",
260
+ "omGetLatest",
261
+ "omGetHistory",
262
+ "listDueSchedules",
263
+ "listScheduleTriggers"
264
+ ]);
265
+ async function callRunClaim(client, request, operation) {
266
+ try {
267
+ return await client.callStorageRaw(request);
268
+ } catch (error) {
269
+ const { code, message } = error;
270
+ if (code === RUN_FENCE_CONFLICT_ERROR_ID && request.op === "fenced") throw new RunFenceConflictError(request.fence, operation);
271
+ if (typeof message === "string" && message.startsWith(`Unsupported operation ${request.op}`)) throw new Error(`The deployed Convex storage function does not support run fencing (${request.op}). Redeploy your Convex functions with the installed version of @mastra/convex.`, { cause: error });
272
+ throw error;
273
+ }
274
+ }
275
+ /** Sends writes inside a fenced operation, so each is applied only while `check.fence` holds the run. */
276
+ var FencedStorageCaller = class {
277
+ client;
278
+ check;
279
+ constructor(client, check) {
280
+ this.client = client;
281
+ this.check = check;
282
+ }
283
+ callStorageRaw(request) {
284
+ if (READ_OPS.has(request.op)) return this.client.callStorageRaw(request);
285
+ return callRunClaim(this.client, {
286
+ op: "fenced",
287
+ tableName: this.check.claims,
288
+ fence: toFence(this.check.fence),
289
+ request
290
+ }, this.check.operation);
291
+ }
292
+ async callStorage(request) {
293
+ return (await this.callStorageRaw(request)).result;
294
+ }
295
+ };
296
+ /** The client, or one that fences every write when `check` is set. */
297
+ function fencedCaller(client, check) {
298
+ return check ? new FencedStorageCaller(client, check) : client;
299
+ }
300
+ /**
301
+ * Rejects a fenced write that turns out to have nothing to write when its
302
+ * fence is no longer current, as the write itself would have been rejected.
303
+ */
304
+ async function assertRunFence(client, check) {
305
+ if (!check) return;
306
+ await callRunClaim(client, {
307
+ op: "fenced",
308
+ tableName: check.claims,
309
+ fence: toFence(check.fence)
310
+ }, check.operation);
311
+ }
312
+ function toFence({ runId, generation, ownerId }) {
313
+ return {
314
+ runId,
315
+ generation,
316
+ ownerId
317
+ };
318
+ }
319
+ function toRecord(claim, now) {
320
+ if (!claim) return null;
321
+ const { runId, generation, ownerId, leaseExpiresAt } = claim;
322
+ return {
323
+ runId,
324
+ generation,
325
+ ownerId,
326
+ leaseExpiresAt: leaseExpiresAt === null ? null : new Date(leaseExpiresAt),
327
+ live: leaseExpiresAt !== null && leaseExpiresAt > now
328
+ };
329
+ }
330
+ async function ownerOperation(client, request) {
331
+ const { result } = await callRunClaim(client, request, request.op);
332
+ return {
333
+ applied: result.applied,
334
+ record: toRecord(result.claim, result.now)
335
+ };
336
+ }
337
+ async function claimRunOwnership(client, { runId, ownerId, leaseMs, force, expectedGeneration }) {
338
+ const { applied, record } = await ownerOperation(client, {
339
+ op: "claimRunOwnership",
340
+ tableName: TABLE_WORKFLOW_RUN_OWNERS,
341
+ runId,
342
+ ownerId,
343
+ leaseMs,
344
+ force: force ?? false,
345
+ ...expectedGeneration !== void 0 ? { expectedGeneration } : {}
346
+ });
347
+ return applied ? {
348
+ acquired: true,
349
+ record
350
+ } : {
351
+ acquired: false,
352
+ record
353
+ };
354
+ }
355
+ async function renewRunOwnership(client, { leaseMs, ...fence }) {
356
+ const { applied, record } = await ownerOperation(client, {
357
+ op: "renewRunOwnership",
358
+ tableName: TABLE_WORKFLOW_RUN_OWNERS,
359
+ fence: toFence(fence),
360
+ leaseMs
361
+ });
362
+ return applied ? {
363
+ renewed: true,
364
+ record
365
+ } : {
366
+ renewed: false,
367
+ record
368
+ };
369
+ }
370
+ async function releaseRunOwnership(client, fence) {
371
+ const { applied } = await ownerOperation(client, {
372
+ op: "releaseRunOwnership",
373
+ tableName: TABLE_WORKFLOW_RUN_OWNERS,
374
+ fence: toFence(fence)
375
+ });
376
+ return applied;
377
+ }
378
+ async function getRunOwnership(client, runId) {
379
+ const { record } = await ownerOperation(client, {
380
+ op: "getRunOwnership",
381
+ tableName: TABLE_WORKFLOW_RUN_OWNERS,
382
+ runId
383
+ });
384
+ return record;
385
+ }
386
+ async function raiseRunFence(client, fence) {
387
+ const { applied } = await ownerOperation(client, {
388
+ op: "raiseRunFence",
389
+ tableName: TABLE_MEMORY_RUN_FENCES,
390
+ fence: toFence(fence)
391
+ });
392
+ return applied;
393
+ }
394
+ async function retireRunFence(client, fence) {
395
+ const { applied } = await ownerOperation(client, {
396
+ op: "retireRunFence",
397
+ tableName: TABLE_MEMORY_RUN_FENCES,
398
+ fence: toFence(fence)
399
+ });
400
+ return applied;
401
+ }
402
+ //#endregion
255
403
  //#region src/storage/types.ts
256
404
  /**
257
405
  * Observational memory table name. Defined locally (not value-imported from
@@ -270,11 +418,17 @@ function resolveConvexConfig(config) {
270
418
  if ("client" in config) return config.client;
271
419
  return new ConvexAdminClient(config);
272
420
  }
273
- var ConvexDB = class extends MastraBase {
421
+ var ConvexDB = class ConvexDB extends MastraBase {
422
+ adminClient;
274
423
  client;
275
- constructor(client) {
424
+ constructor(adminClient, check) {
276
425
  super({ name: "convex-db" });
277
- this.client = client;
426
+ this.adminClient = adminClient;
427
+ this.client = fencedCaller(adminClient, check);
428
+ }
429
+ /** This store with every write applied only while `check.fence` holds the run, or itself without a check. */
430
+ fenced(check) {
431
+ return check ? new ConvexDB(this.adminClient, check) : this;
278
432
  }
279
433
  async hasColumn(_table, _column) {
280
434
  return true;
@@ -1023,10 +1177,32 @@ var MemoryConvex = class extends MemoryStorage {
1023
1177
  supportsObservationalMemory = true;
1024
1178
  supportsObservationalMemoryHistorySearch = true;
1025
1179
  #db;
1180
+ #client;
1026
1181
  constructor(config) {
1027
1182
  super();
1028
- const client = resolveConvexConfig(config);
1029
- this.#db = new ConvexDB(client);
1183
+ this.#client = resolveConvexConfig(config);
1184
+ this.#db = new ConvexDB(this.#client);
1185
+ }
1186
+ supportsRunFencing() {
1187
+ return true;
1188
+ }
1189
+ async raiseRunFence(fence) {
1190
+ return raiseRunFence(this.#client, fence);
1191
+ }
1192
+ async retireRunFence(fence) {
1193
+ return retireRunFence(this.#client, fence);
1194
+ }
1195
+ #runFenceCheck(fence, operation) {
1196
+ const resolved = resolveRunFence(this, fence);
1197
+ return resolved && {
1198
+ claims: TABLE_MEMORY_RUN_FENCES,
1199
+ fence: resolved,
1200
+ operation
1201
+ };
1202
+ }
1203
+ /** The store to write through: fenced when the write carries or inherits a fence. */
1204
+ #dbFor(fence, operation) {
1205
+ return this.#db.fenced(this.#runFenceCheck(fence, operation));
1030
1206
  }
1031
1207
  async init() {}
1032
1208
  async dangerouslyClearAll() {
@@ -1034,6 +1210,7 @@ var MemoryConvex = class extends MemoryStorage {
1034
1210
  await this.#db.clearTable({ tableName: TABLE_MESSAGES$1 });
1035
1211
  await this.#db.clearTable({ tableName: TABLE_RESOURCES$1 });
1036
1212
  await this.#db.clearTable({ tableName: TABLE_OBSERVATIONAL_MEMORY$1 });
1213
+ await this.#db.clearTable({ tableName: TABLE_MEMORY_RUN_FENCES });
1037
1214
  }
1038
1215
  async getThreadById({ threadId, resourceId }) {
1039
1216
  const row = await this.#db.load({
@@ -1043,8 +1220,8 @@ var MemoryConvex = class extends MemoryStorage {
1043
1220
  if (!row || resourceId !== void 0 && row.resourceId !== resourceId) return null;
1044
1221
  return parseStoredThread(row);
1045
1222
  }
1046
- async saveThread({ thread }) {
1047
- await this.#db.insert({
1223
+ async saveThread({ thread, fence }) {
1224
+ await this.#dbFor(fence, "saveThread").insert({
1048
1225
  tableName: TABLE_THREADS$1,
1049
1226
  record: {
1050
1227
  ...thread,
@@ -1053,8 +1230,8 @@ var MemoryConvex = class extends MemoryStorage {
1053
1230
  });
1054
1231
  return thread;
1055
1232
  }
1056
- async updateThread({ id, title, metadata }) {
1057
- const updated = await this.#db.updateThread({
1233
+ async updateThread({ id, title, metadata, fence }) {
1234
+ const updated = await this.#dbFor(fence, "updateThread").updateThread({
1058
1235
  id,
1059
1236
  title,
1060
1237
  metadata,
@@ -1244,8 +1421,9 @@ var MemoryConvex = class extends MemoryStorage {
1244
1421
  const filtered = (await this.#db.loadMany(TABLE_MESSAGES$1, messageIds)).map((row) => this.parseStoredMessage(row));
1245
1422
  return { messages: new MessageList().add(filtered, "memory").get.all.db() };
1246
1423
  }
1247
- async saveMessages({ messages }) {
1424
+ async saveMessages({ messages, fence }) {
1248
1425
  if (messages.length === 0) return { messages: [] };
1426
+ const db = this.#dbFor(fence, "saveMessages");
1249
1427
  const normalized = messages.map((message) => {
1250
1428
  if (!message.threadId) throw new Error("Thread ID is required");
1251
1429
  if (!message.resourceId) throw new Error("Resource ID is required");
@@ -1260,21 +1438,23 @@ var MemoryConvex = class extends MemoryStorage {
1260
1438
  resourceId: message.resourceId
1261
1439
  };
1262
1440
  });
1263
- await this.#db.batchInsert({
1441
+ await db.batchInsert({
1264
1442
  tableName: TABLE_MESSAGES$1,
1265
1443
  records: normalized
1266
1444
  });
1267
1445
  const threadIds = [...new Set(messages.map((m) => m.threadId).filter(Boolean))];
1268
1446
  const now = /* @__PURE__ */ new Date();
1269
- for (const threadId of threadIds) await this.#db.patch({
1447
+ for (const threadId of threadIds) await db.patch({
1270
1448
  tableName: TABLE_THREADS$1,
1271
1449
  id: threadId,
1272
1450
  record: { updatedAt: now.toISOString() }
1273
1451
  });
1274
1452
  return { messages: new MessageList().add(messages, "memory").get.all.db() };
1275
1453
  }
1276
- async updateMessages({ messages }) {
1454
+ async updateMessages({ messages, fence }) {
1277
1455
  if (messages.length === 0) return [];
1456
+ const check = this.#runFenceCheck(fence, "updateMessages");
1457
+ const db = this.#db.fenced(check);
1278
1458
  const existingRows = await this.#db.loadMany(TABLE_MESSAGES$1, messages.map((message) => message.id));
1279
1459
  const existing = new Map(existingRows.map((row) => [row.id, row]));
1280
1460
  const updated = [];
@@ -1302,22 +1482,26 @@ var MemoryConvex = class extends MemoryStorage {
1302
1482
  };
1303
1483
  current.content = JSON.stringify(mergedContent);
1304
1484
  }
1305
- await this.#db.insert({
1485
+ await db.insert({
1306
1486
  tableName: TABLE_MESSAGES$1,
1307
1487
  record: current
1308
1488
  });
1309
1489
  updated.push(this.parseStoredMessage(current));
1310
1490
  }
1491
+ if (updated.length === 0) {
1492
+ await assertRunFence(this.#client, check);
1493
+ return updated;
1494
+ }
1311
1495
  const now = /* @__PURE__ */ new Date();
1312
- for (const threadId of affectedThreadIds) await this.#db.patch({
1496
+ for (const threadId of affectedThreadIds) await db.patch({
1313
1497
  tableName: TABLE_THREADS$1,
1314
1498
  id: threadId,
1315
1499
  record: { updatedAt: now.toISOString() }
1316
1500
  });
1317
1501
  return updated;
1318
1502
  }
1319
- async deleteMessages(messageIds) {
1320
- await this.#db.deleteMany(TABLE_MESSAGES$1, messageIds);
1503
+ async deleteMessages(messageIds, options) {
1504
+ await this.#dbFor(options?.fence, "deleteMessages").deleteMany(TABLE_MESSAGES$1, messageIds);
1321
1505
  }
1322
1506
  async saveResource({ resource }) {
1323
1507
  const record = {
@@ -1340,9 +1524,9 @@ var MemoryConvex = class extends MemoryStorage {
1340
1524
  if (!record) return null;
1341
1525
  return parseStoredResource(record);
1342
1526
  }
1343
- async updateResource({ resourceId, workingMemory, metadata }) {
1527
+ async updateResource({ resourceId, workingMemory, metadata, fence }) {
1344
1528
  const now = /* @__PURE__ */ new Date();
1345
- return parseStoredResource(await this.#db.updateResource({
1529
+ return parseStoredResource(await this.#dbFor(fence, "updateResource").updateResource({
1346
1530
  resourceId,
1347
1531
  workingMemory,
1348
1532
  metadata,
@@ -1602,7 +1786,7 @@ var MemoryConvex = class extends MemoryStorage {
1602
1786
  assertActiveObservationsApplied(await this.commitActiveObservations(input), input.id);
1603
1787
  }
1604
1788
  async commitActiveObservations(input) {
1605
- return await this.#db.omUpdateActive({
1789
+ return await this.#dbFor(void 0, "commitActiveObservations").omUpdateActive({
1606
1790
  id: input.id,
1607
1791
  observations: input.observations,
1608
1792
  tokenCount: input.tokenCount,
@@ -1631,7 +1815,7 @@ var MemoryConvex = class extends MemoryStorage {
1631
1815
  extractedValues: input.chunk.extractedValues,
1632
1816
  extractionFailures: input.chunk.extractionFailures
1633
1817
  };
1634
- return await this.#db.omAppendBufferedChunk({
1818
+ return await this.#dbFor(void 0, "appendBufferedObservations").omAppendBufferedChunk({
1635
1819
  id: input.id,
1636
1820
  chunk,
1637
1821
  lastBufferedAtTime: input.lastBufferedAtTime ? toISO(input.lastBufferedAtTime) : void 0,
@@ -1642,7 +1826,7 @@ var MemoryConvex = class extends MemoryStorage {
1642
1826
  };
1643
1827
  }
1644
1828
  async swapBufferedToActive(input) {
1645
- return this.#db.omSwapBuffered({
1829
+ return this.#dbFor(void 0, "swapBufferedToActive").omSwapBuffered({
1646
1830
  id: input.id,
1647
1831
  activationRatio: input.activationRatio,
1648
1832
  messageTokensThreshold: input.messageTokensThreshold,
@@ -1672,7 +1856,7 @@ var MemoryConvex = class extends MemoryStorage {
1672
1856
  };
1673
1857
  }
1674
1858
  async createReflectionGeneration(input) {
1675
- const doc = await this.#db.omCreateReflectionGeneration({
1859
+ const doc = await this.#dbFor(void 0, "createReflectionGeneration").omCreateReflectionGeneration({
1676
1860
  currentRecord: this.#serializeCurrentRecord(input.currentRecord),
1677
1861
  newId: input.newRecordId ?? crypto.randomUUID(),
1678
1862
  reflection: input.reflection,
@@ -1682,7 +1866,7 @@ var MemoryConvex = class extends MemoryStorage {
1682
1866
  return doc ? parseStoredOMRecord(doc) : input.currentRecord;
1683
1867
  }
1684
1868
  async updateBufferedReflection(input) {
1685
- await this.#db.omUpdateBufferedReflection({
1869
+ await this.#dbFor(void 0, "updateBufferedReflection").omUpdateBufferedReflection({
1686
1870
  id: input.id,
1687
1871
  reflection: input.reflection,
1688
1872
  tokenCount: input.tokenCount,
@@ -1692,7 +1876,7 @@ var MemoryConvex = class extends MemoryStorage {
1692
1876
  });
1693
1877
  }
1694
1878
  async swapBufferedReflectionToActive(input) {
1695
- const doc = await this.#db.omSwapBufferedReflection({
1879
+ const doc = await this.#dbFor(void 0, "swapBufferedReflectionToActive").omSwapBufferedReflection({
1696
1880
  currentRecord: this.#serializeCurrentRecord(input.currentRecord),
1697
1881
  newId: input.newRecordId ?? crypto.randomUUID(),
1698
1882
  tokenCount: input.tokenCount,
@@ -2088,25 +2272,52 @@ var ScoresConvex = class extends ScoresStorage {
2088
2272
  //#region src/storage/domains/workflows/index.ts
2089
2273
  var WorkflowsConvex = class extends WorkflowsStorage {
2090
2274
  #db;
2275
+ #client;
2091
2276
  constructor(config) {
2092
2277
  super();
2093
- const client = resolveConvexConfig(config);
2094
- this.#db = new ConvexDB(client);
2278
+ this.#client = resolveConvexConfig(config);
2279
+ this.#db = new ConvexDB(this.#client);
2095
2280
  }
2096
2281
  supportsConcurrentUpdates() {
2097
2282
  return true;
2098
2283
  }
2284
+ supportsRunFencing() {
2285
+ return true;
2286
+ }
2099
2287
  async init() {}
2100
2288
  async dangerouslyClearAll() {
2101
2289
  await this.#db.clearTable({ tableName: TABLE_WORKFLOW_SNAPSHOT$1 });
2290
+ await this.#db.clearTable({ tableName: TABLE_WORKFLOW_RUN_OWNERS });
2291
+ }
2292
+ /** The store to write a run through: fenced when the write carries or inherits a fence. */
2293
+ #dbFor(fence, runId, operation) {
2294
+ const resolved = resolveRunFence(this, fence, runId);
2295
+ const check = resolved && {
2296
+ claims: TABLE_WORKFLOW_RUN_OWNERS,
2297
+ fence: resolved,
2298
+ operation
2299
+ };
2300
+ return this.#db.fenced(check);
2301
+ }
2302
+ async claimRunOwnership(args) {
2303
+ return claimRunOwnership(this.#client, args);
2304
+ }
2305
+ async renewRunOwnership(args) {
2306
+ return renewRunOwnership(this.#client, args);
2102
2307
  }
2103
- async updateWorkflowResults(args) {
2104
- return this.#db.mergeWorkflowStepResult(args);
2308
+ async releaseRunOwnership(fence) {
2309
+ return releaseRunOwnership(this.#client, fence);
2105
2310
  }
2106
- async updateWorkflowState(args) {
2107
- return this.#db.mergeWorkflowState(args);
2311
+ async getRunOwnership({ runId }) {
2312
+ return getRunOwnership(this.#client, runId);
2108
2313
  }
2109
- async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt }) {
2314
+ async updateWorkflowResults({ fence, ...args }) {
2315
+ return this.#dbFor(fence, args.runId, "updateWorkflowResults").mergeWorkflowStepResult(args);
2316
+ }
2317
+ async updateWorkflowState({ fence, ...args }) {
2318
+ return this.#dbFor(fence, args.runId, "updateWorkflowState").mergeWorkflowState(args);
2319
+ }
2320
+ async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt, fence }) {
2110
2321
  const now = /* @__PURE__ */ new Date();
2111
2322
  const existing = await this.#db.load({
2112
2323
  tableName: TABLE_WORKFLOW_SNAPSHOT$1,
@@ -2115,7 +2326,7 @@ var WorkflowsConvex = class extends WorkflowsStorage {
2115
2326
  run_id: runId
2116
2327
  }
2117
2328
  });
2118
- await this.#db.insert({
2329
+ await this.#dbFor(fence, runId, "persistWorkflowSnapshot").insert({
2119
2330
  tableName: TABLE_WORKFLOW_SNAPSHOT$1,
2120
2331
  record: {
2121
2332
  workflow_name: workflowName,
@@ -2197,8 +2408,8 @@ var WorkflowsConvex = class extends WorkflowsStorage {
2197
2408
  resourceId: match.resourceId
2198
2409
  };
2199
2410
  }
2200
- async deleteWorkflowRunById({ runId, workflowName }) {
2201
- await this.#db.deleteMany(TABLE_WORKFLOW_SNAPSHOT$1, [`${workflowName}-${runId}`]);
2411
+ async deleteWorkflowRunById({ runId, workflowName, fence }) {
2412
+ await this.#dbFor(fence, runId, "deleteWorkflowRunById").deleteMany(TABLE_WORKFLOW_SNAPSHOT$1, [`${workflowName}-${runId}`]);
2202
2413
  }
2203
2414
  async getRun(workflowName, runId) {
2204
2415
  return this.#db.load({