@mastra/redis 1.4.9-alpha.0 → 1.4.10

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 { MessageList } from "@mastra/core/agent";
2
2
  import { ErrorCategory, ErrorDomain, MastraError } from "@mastra/core/error";
3
- import { MastraStorage, MemoryStorage, ScoresStorage, TABLE_MESSAGES, TABLE_RESOURCES, TABLE_SCORERS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, WorkflowsStorage, calculatePagination, createStorageErrorId, ensureDate, filterByDateRange, jsonValueEquals, matchesExpectedWorkflowStatus, normalizePerPage, serializeDate, storageMessageMatchesMetadataFilter, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
3
+ import { MastraStorage, MemoryStorage, RunFenceConflictError, ScoresStorage, TABLE_MEMORY_RUN_FENCES, TABLE_MESSAGES, TABLE_RESOURCES, TABLE_SCORERS, TABLE_THREADS, TABLE_WORKFLOW_RUN_OWNERS, TABLE_WORKFLOW_SNAPSHOT, WorkflowsStorage, calculatePagination, createStorageErrorId, ensureDate, filterByDateRange, isRunFenceConflictError, jsonValueEquals, matchesExpectedWorkflowStatus, normalizePerPage, resolveRunFence, serializeDate, storageMessageMatchesMetadataFilter, transformScoreRow, validateStorageMetadataFilter } from "@mastra/core/storage";
4
4
  import { saveScorePayloadSchema } from "@mastra/core/evals";
5
5
  import { createClient } from "redis";
6
6
  import { MastraServerCache } from "@mastra/core/cache";
@@ -130,6 +130,237 @@ var RedisDB = class {
130
130
  }
131
131
  };
132
132
  //#endregion
133
+ //#region src/storage/domains/run-fencing.ts
134
+ /**
135
+ * A claim expires after this long without a claim, renewal or release, and a
136
+ * fence this long after it was retired. A running execution renews every few
137
+ * seconds, and a raised fence never expires. Expiring is safe: a later claim's
138
+ * generation comes from the clock, so it still outranks every fence the expired
139
+ * record issued, and a write checked against a missing record is rejected.
140
+ */
141
+ const RUN_CLAIM_RETENTION_MS = 10080 * 60 * 1e3;
142
+ function runClaimKey(claims, runId) {
143
+ return `${claims}:run_id:${runId}`;
144
+ }
145
+ const LIB = `
146
+ local function now()
147
+ local t = redis.call('TIME')
148
+ return tonumber(t[1]) * 1000 + math.floor(tonumber(t[2]) / 1000)
149
+ end
150
+ local function owner(flag, t)
151
+ local r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId', 'leaseExpiresAt')
152
+ return { flag, r[1], r[2], r[3], t }
153
+ end
154
+ `;
155
+ const CLAIM = `${LIB}
156
+ local t = now()
157
+ local stamp = string.format('%d', t)
158
+ local lease = string.format('%d', t + tonumber(ARGV[2]))
159
+ local generation = tonumber(redis.call('HGET', KEYS[1], 'generation'))
160
+ local expected = ARGV[4]
161
+ if not generation then
162
+ if expected ~= '' and expected ~= '0' then return owner(0, t) end
163
+ -- The first generation comes from the store's clock, so a claim after this
164
+ -- hash is removed still outranks a surviving fence.
165
+ redis.call('HSET', KEYS[1], 'generation', string.format('%d', math.max(1, t)), 'ownerId', ARGV[1], 'leaseExpiresAt', lease, 'updatedAt', stamp)
166
+ redis.call('PEXPIRE', KEYS[1], ARGV[5])
167
+ return owner(1, t)
168
+ end
169
+ if expected ~= '' and generation ~= tonumber(expected) then return owner(0, t) end
170
+ if ARGV[3] ~= '1' then
171
+ local expires = tonumber(redis.call('HGET', KEYS[1], 'leaseExpiresAt'))
172
+ if expires and expires > t then return owner(0, t) end
173
+ end
174
+ redis.call('HSET', KEYS[1], 'generation', string.format('%d', generation + 1), 'ownerId', ARGV[1], 'leaseExpiresAt', lease, 'updatedAt', stamp)
175
+ redis.call('PEXPIRE', KEYS[1], ARGV[5])
176
+ return owner(1, t)
177
+ `;
178
+ const RENEW = `${LIB}
179
+ local t = now()
180
+ local r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId', 'leaseExpiresAt')
181
+ if tonumber(r[1]) ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] or not r[3] then return owner(0, t) end
182
+ redis.call('HSET', KEYS[1], 'leaseExpiresAt', string.format('%d', t + tonumber(ARGV[3])), 'updatedAt', string.format('%d', t))
183
+ redis.call('PEXPIRE', KEYS[1], ARGV[4])
184
+ return owner(1, t)
185
+ `;
186
+ const RELEASE = `${LIB}
187
+ local t = now()
188
+ local r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId')
189
+ if tonumber(r[1]) ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] then return 0 end
190
+ redis.call('HDEL', KEYS[1], 'leaseExpiresAt')
191
+ redis.call('HSET', KEYS[1], 'updatedAt', string.format('%d', t))
192
+ redis.call('PEXPIRE', KEYS[1], ARGV[3])
193
+ return 1
194
+ `;
195
+ const READ = `${LIB}
196
+ return owner(1, now())
197
+ `;
198
+ const RAISE = `
199
+ local r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId')
200
+ local current = tonumber(r[1])
201
+ if not current or current < tonumber(ARGV[1]) then
202
+ redis.call('HSET', KEYS[1], 'generation', ARGV[1], 'ownerId', ARGV[2])
203
+ elseif current ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] then
204
+ return 0
205
+ end
206
+ -- Every write un-retires the fence.
207
+ redis.call('HDEL', KEYS[1], 'retiredAt')
208
+ redis.call('PERSIST', KEYS[1])
209
+ return 1
210
+ `;
211
+ const RETIRE = `${LIB}
212
+ local t = now()
213
+ local r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId')
214
+ if tonumber(r[1]) ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] then return 0 end
215
+ redis.call('HSET', KEYS[1], 'retiredAt', string.format('%d', t))
216
+ redis.call('PEXPIRE', KEYS[1], ARGV[3])
217
+ return 1
218
+ `;
219
+ const FENCED_WRITE = `
220
+ local r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId')
221
+ if tonumber(r[1]) ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] then return 0 end
222
+ local i = 3
223
+ while i <= #ARGV do
224
+ local n = tonumber(ARGV[i])
225
+ redis.call(unpack(ARGV, i + 1, i + n))
226
+ i = i + n + 1
227
+ end
228
+ return 1
229
+ `;
230
+ function evalScript(client, script, keys, args) {
231
+ return client.eval(script, {
232
+ keys,
233
+ arguments: args
234
+ });
235
+ }
236
+ function toRunOwnershipRecord(runId, reply) {
237
+ const [, generation, ownerId, leaseExpiresAt, now] = reply;
238
+ if (generation === null) return null;
239
+ const expiresAt = leaseExpiresAt === null ? null : Number(leaseExpiresAt);
240
+ return {
241
+ runId,
242
+ generation: Number(generation),
243
+ ownerId: String(ownerId),
244
+ leaseExpiresAt: expiresAt === null ? null : new Date(expiresAt),
245
+ live: expiresAt !== null && expiresAt > Number(now)
246
+ };
247
+ }
248
+ async function evalOwner(client, script, runId, args) {
249
+ const reply = await evalScript(client, script, [runClaimKey(TABLE_WORKFLOW_RUN_OWNERS, runId)], args);
250
+ return {
251
+ applied: Number(reply[0]) === 1,
252
+ record: toRunOwnershipRecord(runId, reply)
253
+ };
254
+ }
255
+ async function claimRunOwnership(client, { runId, ownerId, leaseMs, force, expectedGeneration }) {
256
+ const { applied, record } = await evalOwner(client, CLAIM, runId, [
257
+ ownerId,
258
+ String(leaseMs),
259
+ force ? "1" : "0",
260
+ expectedGeneration === void 0 ? "" : String(expectedGeneration),
261
+ String(RUN_CLAIM_RETENTION_MS)
262
+ ]);
263
+ return applied ? {
264
+ acquired: true,
265
+ record
266
+ } : {
267
+ acquired: false,
268
+ record
269
+ };
270
+ }
271
+ async function renewRunOwnership(client, { leaseMs, ...fence }) {
272
+ const { applied, record } = await evalOwner(client, RENEW, fence.runId, [
273
+ String(fence.generation),
274
+ fence.ownerId,
275
+ String(leaseMs),
276
+ String(RUN_CLAIM_RETENTION_MS)
277
+ ]);
278
+ return applied ? {
279
+ renewed: true,
280
+ record
281
+ } : {
282
+ renewed: false,
283
+ record
284
+ };
285
+ }
286
+ async function releaseRunOwnership(client, fence) {
287
+ const released = await evalScript(client, RELEASE, [runClaimKey(TABLE_WORKFLOW_RUN_OWNERS, fence.runId)], [
288
+ String(fence.generation),
289
+ fence.ownerId,
290
+ String(RUN_CLAIM_RETENTION_MS)
291
+ ]);
292
+ return Number(released) === 1;
293
+ }
294
+ async function getRunOwnership(client, runId) {
295
+ return (await evalOwner(client, READ, runId, [])).record;
296
+ }
297
+ async function raiseRunFence(client, fence) {
298
+ const raised = await evalScript(client, RAISE, [runClaimKey(TABLE_MEMORY_RUN_FENCES, fence.runId)], [String(fence.generation), fence.ownerId]);
299
+ return Number(raised) === 1;
300
+ }
301
+ async function retireRunFence(client, fence) {
302
+ const retired = await evalScript(client, RETIRE, [runClaimKey(TABLE_MEMORY_RUN_FENCES, fence.runId)], [
303
+ String(fence.generation),
304
+ fence.ownerId,
305
+ String(RUN_CLAIM_RETENTION_MS)
306
+ ]);
307
+ return Number(retired) === 1;
308
+ }
309
+ /**
310
+ * Collects writes and applies them in one script that first checks the run's
311
+ * claim against the fence. A fence that is no longer current makes `exec()`
312
+ * throw `RunFenceConflictError` without writing anything. Scripts run
313
+ * atomically, so a takeover lands either before the batch (failing its check)
314
+ * or after it.
315
+ */
316
+ var FencedWriteBatch = class {
317
+ client;
318
+ check;
319
+ #keys = /* @__PURE__ */ new Set();
320
+ #args = [];
321
+ constructor(client, check) {
322
+ this.client = client;
323
+ this.check = check;
324
+ }
325
+ #add(key, ...command) {
326
+ this.#keys.add(key);
327
+ this.#args.push(String(command.length), ...command);
328
+ return this;
329
+ }
330
+ set(key, value) {
331
+ return this.#add(key, "SET", key, value);
332
+ }
333
+ del(key) {
334
+ return this.#add(key, "DEL", key);
335
+ }
336
+ zAdd(key, { score, value }) {
337
+ return this.#add(key, "ZADD", key, String(score), value);
338
+ }
339
+ zRem(key, member) {
340
+ return this.#add(key, "ZREM", key, member);
341
+ }
342
+ async exec() {
343
+ const { claims, fence, operation } = this.check;
344
+ const applied = await evalScript(this.client, FENCED_WRITE, [runClaimKey(claims, fence.runId), ...this.#keys], [
345
+ String(fence.generation),
346
+ fence.ownerId,
347
+ ...this.#args
348
+ ]);
349
+ if (Number(applied) !== 1) throw new RunFenceConflictError(fence, operation);
350
+ }
351
+ };
352
+ /** A MULTI transaction, or a fenced batch when `check` is set. */
353
+ function writeBatch(client, check) {
354
+ return check ? new FencedWriteBatch(client, check) : client.multi();
355
+ }
356
+ /**
357
+ * Rejects a fenced write that turns out to have nothing to write when its
358
+ * fence is no longer current, as the write itself would have been rejected.
359
+ */
360
+ async function assertRunFence(client, check) {
361
+ if (check) await new FencedWriteBatch(client, check).exec();
362
+ }
363
+ //#endregion
133
364
  //#region src/storage/domains/memory/index.ts
134
365
  var StoreMemoryRedis = class extends MemoryStorage {
135
366
  supportsPartialThreadUpdate = true;
@@ -140,12 +371,48 @@ var StoreMemoryRedis = class extends MemoryStorage {
140
371
  this.client = config.client;
141
372
  this.db = new RedisDB({ client: config.client });
142
373
  }
374
+ supportsRunFencing() {
375
+ return true;
376
+ }
377
+ async raiseRunFence(fence) {
378
+ try {
379
+ return await raiseRunFence(this.client, fence);
380
+ } catch (error) {
381
+ throw new MastraError({
382
+ id: createStorageErrorId("REDIS", "RAISE_RUN_FENCE", "FAILED"),
383
+ domain: ErrorDomain.STORAGE,
384
+ category: ErrorCategory.THIRD_PARTY,
385
+ details: { runId: fence.runId }
386
+ }, error);
387
+ }
388
+ }
389
+ async retireRunFence(fence) {
390
+ try {
391
+ return await retireRunFence(this.client, fence);
392
+ } catch (error) {
393
+ throw new MastraError({
394
+ id: createStorageErrorId("REDIS", "RETIRE_RUN_FENCE", "FAILED"),
395
+ domain: ErrorDomain.STORAGE,
396
+ category: ErrorCategory.THIRD_PARTY,
397
+ details: { runId: fence.runId }
398
+ }, error);
399
+ }
400
+ }
401
+ #runFenceCheck(fence, operation) {
402
+ const resolved = resolveRunFence(this, fence);
403
+ return resolved && {
404
+ claims: TABLE_MEMORY_RUN_FENCES,
405
+ fence: resolved,
406
+ operation
407
+ };
408
+ }
143
409
  async dangerouslyClearAll() {
144
410
  await this.db.deleteData({ tableName: TABLE_THREADS });
145
411
  await this.db.deleteData({ tableName: TABLE_MESSAGES });
146
412
  await this.db.deleteData({ tableName: TABLE_RESOURCES });
147
413
  await this.db.scanAndDelete("msg-idx:*");
148
414
  await this.db.scanAndDelete("thread:*:messages");
415
+ await this.db.scanAndDelete(`${runClaimKey(TABLE_MEMORY_RUN_FENCES, "")}*`);
149
416
  }
150
417
  async getThreadById({ threadId, resourceId }) {
151
418
  try {
@@ -256,14 +523,16 @@ var StoreMemoryRedis = class extends MemoryStorage {
256
523
  throw mastraError;
257
524
  }
258
525
  }
259
- async saveThread({ thread }) {
526
+ async saveThread({ thread, fence }) {
527
+ const check = this.#runFenceCheck(fence, "saveThread");
260
528
  try {
261
- await this.db.insert({
262
- tableName: TABLE_THREADS,
263
- record: thread
264
- });
529
+ const { key, processedRecord } = processRecord(TABLE_THREADS, thread);
530
+ const batch = writeBatch(this.client, check);
531
+ batch.set(key, JSON.stringify(processedRecord));
532
+ await batch.exec();
265
533
  return thread;
266
534
  } catch (error) {
535
+ if (isRunFenceConflictError(error)) throw error;
267
536
  const mastraError = new MastraError({
268
537
  id: createStorageErrorId("REDIS", "SAVE_THREAD", "FAILED"),
269
538
  domain: ErrorDomain.STORAGE,
@@ -275,7 +544,7 @@ var StoreMemoryRedis = class extends MemoryStorage {
275
544
  throw mastraError;
276
545
  }
277
546
  }
278
- async updateThread({ id, title, metadata }) {
547
+ async updateThread({ id, title, metadata, fence }) {
279
548
  const thread = await this.getThreadById({ threadId: id });
280
549
  if (!thread) throw new MastraError({
281
550
  id: createStorageErrorId("REDIS", "UPDATE_THREAD", "FAILED"),
@@ -294,9 +563,13 @@ var StoreMemoryRedis = class extends MemoryStorage {
294
563
  updatedAt: /* @__PURE__ */ new Date()
295
564
  };
296
565
  try {
297
- await this.saveThread({ thread: updatedThread });
566
+ await this.saveThread({
567
+ thread: updatedThread,
568
+ fence
569
+ });
298
570
  return updatedThread;
299
571
  } catch (error) {
572
+ if (isRunFenceConflictError(error)) throw error;
300
573
  throw new MastraError({
301
574
  id: createStorageErrorId("REDIS", "UPDATE_THREAD", "FAILED"),
302
575
  domain: ErrorDomain.STORAGE,
@@ -331,6 +604,7 @@ var StoreMemoryRedis = class extends MemoryStorage {
331
604
  }
332
605
  async saveMessages(args) {
333
606
  const { messages } = args;
607
+ const check = this.#runFenceCheck(args.fence, "saveMessages");
334
608
  if (messages.length === 0) return { messages: [] };
335
609
  const threadId = messages[0]?.threadId;
336
610
  try {
@@ -360,7 +634,7 @@ var StoreMemoryRedis = class extends MemoryStorage {
360
634
  for (let i = 0; i < messagesWithIndex.length; i += batchSize) {
361
635
  const batch = messagesWithIndex.slice(i, i + batchSize);
362
636
  const batchExistingThreadIds = existingThreadIds.slice(i, i + batch.length);
363
- const multi = this.client.multi();
637
+ const multi = writeBatch(this.client, check);
364
638
  for (const [batchIndex, message] of batch.entries()) {
365
639
  const key = getMessageKey(message.threadId, message.id);
366
640
  const score = getMessageScore(message);
@@ -388,6 +662,7 @@ var StoreMemoryRedis = class extends MemoryStorage {
388
662
  }
389
663
  return { messages: new MessageList().add(messages, "memory").get.all.db() };
390
664
  } catch (error) {
665
+ if (isRunFenceConflictError(error)) throw error;
391
666
  throw new MastraError({
392
667
  id: createStorageErrorId("REDIS", "SAVE_MESSAGES", "FAILED"),
393
668
  domain: ErrorDomain.STORAGE,
@@ -661,6 +936,9 @@ var StoreMemoryRedis = class extends MemoryStorage {
661
936
  }
662
937
  }
663
938
  async saveResource({ resource }) {
939
+ return this.#saveResource(resource, void 0);
940
+ }
941
+ async #saveResource(resource, check) {
664
942
  try {
665
943
  const key = `${TABLE_RESOURCES}:${resource.id}`;
666
944
  const serializedResource = {
@@ -669,14 +947,17 @@ var StoreMemoryRedis = class extends MemoryStorage {
669
947
  createdAt: resource.createdAt.toISOString(),
670
948
  updatedAt: resource.updatedAt.toISOString()
671
949
  };
672
- await this.client.set(key, JSON.stringify(serializedResource));
950
+ const batch = writeBatch(this.client, check);
951
+ batch.set(key, JSON.stringify(serializedResource));
952
+ await batch.exec();
673
953
  return resource;
674
954
  } catch (error) {
675
- this.logger.error("Error saving resource:", error);
955
+ if (!isRunFenceConflictError(error)) this.logger.error("Error saving resource:", error);
676
956
  throw error;
677
957
  }
678
958
  }
679
- async updateResource({ resourceId, workingMemory, metadata }) {
959
+ async updateResource({ resourceId, workingMemory, metadata, fence }) {
960
+ const check = this.#runFenceCheck(fence, "updateResource");
680
961
  try {
681
962
  const existingResource = await this.getResourceById({ resourceId });
682
963
  if (!existingResource) {
@@ -687,7 +968,7 @@ var StoreMemoryRedis = class extends MemoryStorage {
687
968
  createdAt: /* @__PURE__ */ new Date(),
688
969
  updatedAt: /* @__PURE__ */ new Date()
689
970
  };
690
- return this.saveResource({ resource: newResource });
971
+ return await this.#saveResource(newResource, check);
691
972
  }
692
973
  const updatedResource = {
693
974
  ...existingResource,
@@ -698,16 +979,17 @@ var StoreMemoryRedis = class extends MemoryStorage {
698
979
  },
699
980
  updatedAt: /* @__PURE__ */ new Date()
700
981
  };
701
- await this.saveResource({ resource: updatedResource });
982
+ await this.#saveResource(updatedResource, check);
702
983
  return updatedResource;
703
984
  } catch (error) {
704
- this.logger.error("Error updating resource:", error);
985
+ if (!isRunFenceConflictError(error)) this.logger.error("Error updating resource:", error);
705
986
  throw error;
706
987
  }
707
988
  }
708
989
  async updateMessages(args) {
709
990
  const { messages } = args;
710
991
  if (messages.length === 0) return [];
992
+ const check = this.#runFenceCheck(args.fence, "updateMessages");
711
993
  try {
712
994
  const messageIds = messages.map((m) => m.id);
713
995
  const existingMessages = [];
@@ -726,9 +1008,12 @@ var StoreMemoryRedis = class extends MemoryStorage {
726
1008
  }
727
1009
  }
728
1010
  }
729
- if (existingMessages.length === 0) return [];
1011
+ if (existingMessages.length === 0) {
1012
+ await assertRunFence(this.client, check);
1013
+ return [];
1014
+ }
730
1015
  const threadIdsToUpdate = /* @__PURE__ */ new Set();
731
- const multi = this.client.multi();
1016
+ const multi = writeBatch(this.client, check);
732
1017
  for (const existingMessage of existingMessages) {
733
1018
  const updatePayload = messages.find((m) => m.id === existingMessage.id);
734
1019
  if (!updatePayload) continue;
@@ -790,6 +1075,7 @@ var StoreMemoryRedis = class extends MemoryStorage {
790
1075
  }
791
1076
  return updatedMessages;
792
1077
  } catch (error) {
1078
+ if (isRunFenceConflictError(error)) throw error;
793
1079
  throw new MastraError({
794
1080
  id: createStorageErrorId("REDIS", "UPDATE_MESSAGES", "FAILED"),
795
1081
  domain: ErrorDomain.STORAGE,
@@ -798,8 +1084,9 @@ var StoreMemoryRedis = class extends MemoryStorage {
798
1084
  }, error);
799
1085
  }
800
1086
  }
801
- async deleteMessages(messageIds) {
1087
+ async deleteMessages(messageIds, options) {
802
1088
  if (!messageIds || messageIds.length === 0) return;
1089
+ const check = this.#runFenceCheck(options?.fence, "deleteMessages");
803
1090
  try {
804
1091
  const threadIds = /* @__PURE__ */ new Set();
805
1092
  const messageKeys = [];
@@ -844,8 +1131,11 @@ var StoreMemoryRedis = class extends MemoryStorage {
844
1131
  }
845
1132
  }
846
1133
  }
847
- if (messageKeys.length === 0) return;
848
- const multi = this.client.multi();
1134
+ if (messageKeys.length === 0) {
1135
+ await assertRunFence(this.client, check);
1136
+ return;
1137
+ }
1138
+ const multi = writeBatch(this.client, check);
849
1139
  for (const key of messageKeys) multi.del(key);
850
1140
  for (const messageId of foundMessageIds) multi.del(getMessageIndexKey(messageId));
851
1141
  if (threadIds.size > 0) for (const threadId of threadIds) {
@@ -861,6 +1151,7 @@ var StoreMemoryRedis = class extends MemoryStorage {
861
1151
  }
862
1152
  await multi.exec();
863
1153
  } catch (error) {
1154
+ if (isRunFenceConflictError(error)) throw error;
864
1155
  throw new MastraError({
865
1156
  id: createStorageErrorId("REDIS", "DELETE_MESSAGES", "FAILED"),
866
1157
  domain: ErrorDomain.STORAGE,
@@ -1208,10 +1499,59 @@ var WorkflowsRedis = class extends WorkflowsStorage {
1208
1499
  supportsConcurrentUpdates() {
1209
1500
  return false;
1210
1501
  }
1502
+ supportsRunFencing() {
1503
+ return true;
1504
+ }
1211
1505
  async dangerouslyClearAll() {
1212
1506
  await this.db.deleteData({ tableName: TABLE_WORKFLOW_SNAPSHOT });
1507
+ await this.db.scanAndDelete(`${escapeGlob(runClaimKey(TABLE_WORKFLOW_RUN_OWNERS, ""))}*`);
1508
+ }
1509
+ #runFenceCheck(fence, runId, operation) {
1510
+ const resolved = resolveRunFence(this, fence, runId);
1511
+ return resolved && {
1512
+ claims: TABLE_WORKFLOW_RUN_OWNERS,
1513
+ fence: resolved,
1514
+ operation
1515
+ };
1516
+ }
1517
+ #ownershipError(operation, runId, error) {
1518
+ return new MastraError({
1519
+ id: createStorageErrorId("REDIS", operation, "FAILED"),
1520
+ domain: ErrorDomain.STORAGE,
1521
+ category: ErrorCategory.THIRD_PARTY,
1522
+ details: { runId }
1523
+ }, error);
1524
+ }
1525
+ async claimRunOwnership(args) {
1526
+ try {
1527
+ return await claimRunOwnership(this.client, args);
1528
+ } catch (error) {
1529
+ throw this.#ownershipError("CLAIM_RUN_OWNERSHIP", args.runId, error);
1530
+ }
1531
+ }
1532
+ async renewRunOwnership(args) {
1533
+ try {
1534
+ return await renewRunOwnership(this.client, args);
1535
+ } catch (error) {
1536
+ throw this.#ownershipError("RENEW_RUN_OWNERSHIP", args.runId, error);
1537
+ }
1213
1538
  }
1214
- async updateWorkflowResults({ workflowName, runId, stepId, result, requestContext, state }) {
1539
+ async releaseRunOwnership(fence) {
1540
+ try {
1541
+ return await releaseRunOwnership(this.client, fence);
1542
+ } catch (error) {
1543
+ throw this.#ownershipError("RELEASE_RUN_OWNERSHIP", fence.runId, error);
1544
+ }
1545
+ }
1546
+ async getRunOwnership({ runId }) {
1547
+ try {
1548
+ return await getRunOwnership(this.client, runId);
1549
+ } catch (error) {
1550
+ throw this.#ownershipError("GET_RUN_OWNERSHIP", runId, error);
1551
+ }
1552
+ }
1553
+ async updateWorkflowResults({ workflowName, runId, stepId, result, requestContext, state, fence }) {
1554
+ const resolvedFence = resolveRunFence(this, fence, runId);
1215
1555
  try {
1216
1556
  const existingRecord = await this.getSnapshotRecord("workflows", workflowName, runId);
1217
1557
  let snapshot = existingRecord?.snapshot;
@@ -1241,7 +1581,8 @@ var WorkflowsRedis = class extends WorkflowsStorage {
1241
1581
  runId,
1242
1582
  resourceId: existingRecord?.resourceId,
1243
1583
  snapshot,
1244
- createdAt: existingRecord?.createdAt ? ensureDate(existingRecord.createdAt) : void 0
1584
+ createdAt: existingRecord?.createdAt ? ensureDate(existingRecord.createdAt) : void 0,
1585
+ fence: resolvedFence
1245
1586
  });
1246
1587
  return snapshot.context;
1247
1588
  } catch (error) {
@@ -1258,7 +1599,8 @@ var WorkflowsRedis = class extends WorkflowsStorage {
1258
1599
  }, error);
1259
1600
  }
1260
1601
  }
1261
- async updateWorkflowState({ workflowName, runId, opts }) {
1602
+ async updateWorkflowState({ workflowName, runId, opts, fence }) {
1603
+ const resolvedFence = resolveRunFence(this, fence, runId);
1262
1604
  try {
1263
1605
  const existingRecord = await this.getSnapshotRecord("workflows", workflowName, runId);
1264
1606
  const existingSnapshot = existingRecord?.snapshot;
@@ -1275,7 +1617,8 @@ var WorkflowsRedis = class extends WorkflowsStorage {
1275
1617
  runId,
1276
1618
  resourceId: existingRecord.resourceId,
1277
1619
  snapshot: updatedSnapshot,
1278
- createdAt: existingRecord?.createdAt ? ensureDate(existingRecord.createdAt) : void 0
1620
+ createdAt: existingRecord?.createdAt ? ensureDate(existingRecord.createdAt) : void 0,
1621
+ fence: resolvedFence
1279
1622
  });
1280
1623
  return updatedSnapshot;
1281
1624
  } catch (error) {
@@ -1293,6 +1636,7 @@ var WorkflowsRedis = class extends WorkflowsStorage {
1293
1636
  }
1294
1637
  async persistWorkflowSnapshot(params) {
1295
1638
  const { namespace = "workflows", workflowName, runId, resourceId, snapshot, createdAt, updatedAt } = params;
1639
+ const check = this.#runFenceCheck(params.fence, runId, "persistWorkflowSnapshot");
1296
1640
  try {
1297
1641
  let finalCreatedAt = createdAt;
1298
1642
  let finalResourceId = resourceId;
@@ -1303,19 +1647,20 @@ var WorkflowsRedis = class extends WorkflowsStorage {
1303
1647
  finalCreatedAt ??= existing?.createdAt ? ensureDate(existing.createdAt) : /* @__PURE__ */ new Date();
1304
1648
  finalResourceId ??= existing?.resourceId;
1305
1649
  }
1306
- await this.db.insert({
1307
- tableName: TABLE_WORKFLOW_SNAPSHOT,
1308
- record: {
1309
- namespace,
1310
- workflow_name: workflowName,
1311
- run_id: runId,
1312
- resourceId: finalResourceId,
1313
- snapshot,
1314
- createdAt: finalCreatedAt,
1315
- updatedAt: updatedAt ?? /* @__PURE__ */ new Date()
1316
- }
1650
+ const { key, processedRecord } = processRecord(TABLE_WORKFLOW_SNAPSHOT, {
1651
+ namespace,
1652
+ workflow_name: workflowName,
1653
+ run_id: runId,
1654
+ resourceId: finalResourceId,
1655
+ snapshot,
1656
+ createdAt: finalCreatedAt,
1657
+ updatedAt: updatedAt ?? /* @__PURE__ */ new Date()
1317
1658
  });
1659
+ const batch = writeBatch(this.client, check);
1660
+ batch.set(key, JSON.stringify(processedRecord));
1661
+ await batch.exec();
1318
1662
  } catch (error) {
1663
+ if (isRunFenceConflictError(error)) throw error;
1319
1664
  throw new MastraError({
1320
1665
  id: createStorageErrorId("REDIS", "PERSIST_WORKFLOW_SNAPSHOT", "FAILED"),
1321
1666
  domain: ErrorDomain.STORAGE,
@@ -1372,11 +1717,15 @@ var WorkflowsRedis = class extends WorkflowsStorage {
1372
1717
  }, error);
1373
1718
  }
1374
1719
  }
1375
- async deleteWorkflowRunById({ runId, workflowName }) {
1720
+ async deleteWorkflowRunById({ runId, workflowName, fence }) {
1721
+ const check = this.#runFenceCheck(fence, runId, "deleteWorkflowRunById");
1376
1722
  try {
1377
1723
  const legacyKeys = await this.db.scanKeys(legacySnapshotKeyPattern("workflows", workflowName, runId));
1378
- await this.client.del([snapshotKey("workflows", workflowName, runId), ...legacyKeys]);
1724
+ const batch = writeBatch(this.client, check);
1725
+ for (const key of [snapshotKey("workflows", workflowName, runId), ...legacyKeys]) batch.del(key);
1726
+ await batch.exec();
1379
1727
  } catch (error) {
1728
+ if (isRunFenceConflictError(error)) throw error;
1380
1729
  throw new MastraError({
1381
1730
  id: createStorageErrorId("REDIS", "DELETE_WORKFLOW_RUN_BY_ID", "FAILED"),
1382
1731
  domain: ErrorDomain.STORAGE,