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