@mastra/redis 1.4.12 → 1.4.13

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 +1 @@
1
- {"version":3,"file":"index.cjs","names":["TABLE_MESSAGES","TABLE_WORKFLOW_SNAPSHOT","serializeDate","MastraError","createStorageErrorId","ErrorDomain","ErrorCategory","TABLE_WORKFLOW_RUN_OWNERS","TABLE_MEMORY_RUN_FENCES","RunFenceConflictError","MemoryStorage","MastraError","createStorageErrorId","ErrorDomain","ErrorCategory","resolveRunFence","TABLE_MEMORY_RUN_FENCES","TABLE_THREADS","TABLE_MESSAGES","TABLE_RESOURCES","ensureDate","normalizePerPage","calculatePagination","jsonValueEquals","isRunFenceConflictError","MessageList","validateStorageMetadataFilter","filterByDateRange","storageMessageMatchesMetadataFilter","ScoresStorage","TABLE_SCORERS","transformScoreRow","MastraError","createStorageErrorId","ErrorDomain","ErrorCategory","saveScorePayloadSchema","normalizePerPage","calculatePagination","ensureDate","TABLE_WORKFLOW_SNAPSHOT","WorkflowsStorage","TABLE_WORKFLOW_RUN_OWNERS","resolveRunFence","MastraError","createStorageErrorId","ErrorDomain","ErrorCategory","matchesExpectedWorkflowStatus","isRunFenceConflictError","normalizePerPage","MastraStorage","createClient","MastraServerCache"],"sources":["../src/storage/domains/utils.ts","../src/storage/db/index.ts","../src/storage/domains/run-fencing.ts","../src/storage/domains/memory/index.ts","../src/storage/domains/scores/index.ts","../src/storage/domains/workflows/index.ts","../src/storage/utils.ts","../src/storage/store.ts","../src/cache.ts"],"sourcesContent":["import { serializeDate, TABLE_MESSAGES, TABLE_WORKFLOW_SNAPSHOT } from '@mastra/core/storage';\nimport type { TABLE_NAMES } from '@mastra/core/storage';\n\n/**\n * Generate a Redis key from table name and key parts.\n *\n * @example\n * ```typescript\n * getKey('mastra_threads', { id: 'thread-123' });\n * // Returns: 'mastra_threads:id:thread-123'\n *\n * getKey('mastra_messages', { threadId: 'thread-123', id: 'msg-456' });\n * // Returns: 'mastra_messages:threadId:thread-123:id:msg-456'\n * ```\n */\nexport function getKey(tableName: TABLE_NAMES, keys: Record<string, unknown>): string {\n const keyParts = Object.entries(keys)\n .filter(([_, value]) => value !== undefined)\n .map(([key, value]) => {\n if (value && typeof value === 'object') {\n return `${key}:${JSON.stringify(value)}`;\n }\n\n return `${key}:${value}`;\n });\n\n return `${tableName}:${keyParts.join(':')}`;\n}\n\n/**\n * Process a record for storage, generating the appropriate key and serializing dates.\n */\nexport function processRecord(tableName: TABLE_NAMES, record: Record<string, unknown>) {\n let key: string;\n\n if (tableName === TABLE_MESSAGES) {\n key = getKey(tableName, { threadId: record.threadId, id: record.id });\n } else if (tableName === TABLE_WORKFLOW_SNAPSHOT) {\n key = getKey(tableName, {\n namespace: record.namespace || 'workflows',\n workflow_name: record.workflow_name,\n run_id: record.run_id,\n });\n } else {\n key = getKey(tableName, { id: record.id });\n }\n\n const processedRecord = {\n ...record,\n createdAt: serializeDate(record.createdAt as Date | string | undefined),\n updatedAt: serializeDate(record.updatedAt as Date | string | undefined),\n };\n\n return { key, processedRecord };\n}\n","import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';\nimport { createStorageErrorId } from '@mastra/core/storage';\nimport type { TABLE_NAMES } from '@mastra/core/storage';\n\nimport { getKey, processRecord } from '../domains/utils';\nimport type { RedisClient } from '../types';\n\nexport class RedisDB {\n private client: RedisClient;\n\n constructor({ client }: { client: RedisClient }) {\n this.client = client;\n }\n\n getClient(): RedisClient {\n return this.client;\n }\n\n async insert({ tableName, record }: { tableName: TABLE_NAMES; record: Record<string, unknown> }): Promise<void> {\n const { key, processedRecord } = processRecord(tableName, record);\n\n try {\n await this.client.set(key, JSON.stringify(processedRecord));\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'INSERT', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n tableName,\n },\n },\n error,\n );\n }\n }\n\n async get<R>({ tableName, keys }: { tableName: TABLE_NAMES; keys: Record<string, string> }): Promise<R | null> {\n const key = getKey(tableName, keys);\n try {\n const data = await this.client.get(key);\n if (!data) {\n return null;\n }\n return JSON.parse(data) as R;\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LOAD', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n tableName,\n },\n },\n error,\n );\n }\n }\n\n async scanAndDelete(pattern: string, batchSize = 10000): Promise<number> {\n let cursor = '0';\n let totalDeleted = 0;\n\n do {\n const result = await this.client.scan(cursor, { MATCH: pattern, COUNT: batchSize });\n\n if (result.keys.length > 0) {\n await this.client.del(result.keys);\n totalDeleted += result.keys.length;\n }\n\n cursor = result.cursor;\n } while (cursor !== '0');\n\n return totalDeleted;\n }\n\n async scanKeys(pattern: string, batchSize = 10000): Promise<string[]> {\n let cursor = '0';\n const keys: string[] = [];\n\n do {\n const result = await this.client.scan(cursor, { MATCH: pattern, COUNT: batchSize });\n\n keys.push(...result.keys);\n cursor = result.cursor;\n } while (cursor !== '0');\n\n return keys;\n }\n\n async deleteData({ tableName }: { tableName: TABLE_NAMES }): Promise<void> {\n const pattern = `${tableName}:*`;\n try {\n await this.scanAndDelete(pattern);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'CLEAR_TABLE', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n tableName,\n },\n },\n error,\n );\n }\n }\n}\n\nexport interface RedisDomainConfig {\n client: RedisClient;\n}\n","import { RunFenceConflictError, TABLE_MEMORY_RUN_FENCES, TABLE_WORKFLOW_RUN_OWNERS } from '@mastra/core/storage';\nimport type {\n ClaimRunOwnershipInput,\n ClaimRunOwnershipResult,\n RenewRunOwnershipInput,\n RenewRunOwnershipResult,\n RunFence,\n RunOwnershipRecord,\n} from '@mastra/core/storage';\n\nimport type { RedisClient } from '../types';\n\n/** Table holding a run's current claim: its ownership hash (workflows) or raised fence (memory). */\nexport type RunClaimTable = typeof TABLE_WORKFLOW_RUN_OWNERS | typeof TABLE_MEMORY_RUN_FENCES;\n\n/** A write's fence, and the table holding the run's current claim to check it against. */\nexport interface RunFenceCheck {\n claims: RunClaimTable;\n fence: RunFence;\n operation: string;\n}\n\n/** The subset of a MULTI transaction the domains write through. */\nexport interface WriteBatch {\n set(key: string, value: string): unknown;\n del(key: string): unknown;\n zAdd(key: string, member: { score: number; value: string }): unknown;\n zRem(key: string, member: string): unknown;\n exec(): Promise<unknown>;\n}\n\n/**\n * A claim expires after this long without a claim, renewal or release, and a\n * fence this long after it was retired. A running execution renews every few\n * seconds, and a raised fence never expires. Expiring is safe: a later claim's\n * generation comes from the clock, so it still outranks every fence the expired\n * record issued, and a write checked against a missing record is rejected.\n */\nexport const RUN_CLAIM_RETENTION_MS = 7 * 24 * 60 * 60 * 1000;\n\nexport function runClaimKey(claims: RunClaimTable, runId: string): string {\n return `${claims}:run_id:${runId}`;\n}\n\n// A run's claim is a hash of generation, ownerId, updatedAt and, while\n// leased, leaseExpiresAt. A raised fence also holds retiredAt once retired.\n// Every script reads and writes it in one atomic step, and leases and\n// timestamps are measured on the store's clock.\nconst LIB = `\nlocal function now()\n local t = redis.call('TIME')\n return tonumber(t[1]) * 1000 + math.floor(tonumber(t[2]) / 1000)\nend\nlocal function owner(flag, t)\n local r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId', 'leaseExpiresAt')\n return { flag, r[1], r[2], r[3], t }\nend\n`;\n\n// ARGV: ownerId, leaseMs, force ('1' or '0'), expectedGeneration ('' when unset), retentionMs\nconst CLAIM = `${LIB}\nlocal t = now()\nlocal stamp = string.format('%d', t)\nlocal lease = string.format('%d', t + tonumber(ARGV[2]))\nlocal generation = tonumber(redis.call('HGET', KEYS[1], 'generation'))\nlocal expected = ARGV[4]\nif not generation then\n if expected ~= '' and expected ~= '0' then return owner(0, t) end\n -- The first generation comes from the store's clock, so a claim after this\n -- hash is removed still outranks a surviving fence.\n redis.call('HSET', KEYS[1], 'generation', string.format('%d', math.max(1, t)), 'ownerId', ARGV[1], 'leaseExpiresAt', lease, 'updatedAt', stamp)\n redis.call('PEXPIRE', KEYS[1], ARGV[5])\n return owner(1, t)\nend\nif expected ~= '' and generation ~= tonumber(expected) then return owner(0, t) end\nif ARGV[3] ~= '1' then\n local expires = tonumber(redis.call('HGET', KEYS[1], 'leaseExpiresAt'))\n if expires and expires > t then return owner(0, t) end\nend\nredis.call('HSET', KEYS[1], 'generation', string.format('%d', generation + 1), 'ownerId', ARGV[1], 'leaseExpiresAt', lease, 'updatedAt', stamp)\nredis.call('PEXPIRE', KEYS[1], ARGV[5])\nreturn owner(1, t)\n`;\n\n// ARGV: generation, ownerId, leaseMs, retentionMs\nconst RENEW = `${LIB}\nlocal t = now()\nlocal r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId', 'leaseExpiresAt')\nif tonumber(r[1]) ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] or not r[3] then return owner(0, t) end\nredis.call('HSET', KEYS[1], 'leaseExpiresAt', string.format('%d', t + tonumber(ARGV[3])), 'updatedAt', string.format('%d', t))\nredis.call('PEXPIRE', KEYS[1], ARGV[4])\nreturn owner(1, t)\n`;\n\n// ARGV: generation, ownerId, retentionMs\nconst RELEASE = `${LIB}\nlocal t = now()\nlocal r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId')\nif tonumber(r[1]) ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] then return 0 end\nredis.call('HDEL', KEYS[1], 'leaseExpiresAt')\nredis.call('HSET', KEYS[1], 'updatedAt', string.format('%d', t))\nredis.call('PEXPIRE', KEYS[1], ARGV[3])\nreturn 1\n`;\n\nconst READ = `${LIB}\nreturn owner(1, now())\n`;\n\n// ARGV: generation, ownerId\nconst RAISE = `\nlocal r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId')\nlocal current = tonumber(r[1])\nif not current or current < tonumber(ARGV[1]) then\n redis.call('HSET', KEYS[1], 'generation', ARGV[1], 'ownerId', ARGV[2])\nelseif current ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] then\n return 0\nend\n-- Every write un-retires the fence.\nredis.call('HDEL', KEYS[1], 'retiredAt')\nredis.call('PERSIST', KEYS[1])\nreturn 1\n`;\n\n// ARGV: generation, ownerId, retentionMs\nconst RETIRE = `${LIB}\nlocal t = now()\nlocal r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId')\nif tonumber(r[1]) ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] then return 0 end\nredis.call('HSET', KEYS[1], 'retiredAt', string.format('%d', t))\nredis.call('PEXPIRE', KEYS[1], ARGV[3])\nreturn 1\n`;\n\n// ARGV: generation, ownerId, then each command as its argument count followed by its arguments\nconst FENCED_WRITE = `\nlocal r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId')\nif tonumber(r[1]) ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] then return 0 end\nlocal i = 3\nwhile i <= #ARGV do\n local n = tonumber(ARGV[i])\n redis.call(unpack(ARGV, i + 1, i + n))\n i = i + n + 1\nend\nreturn 1\n`;\n\ntype OwnerReply = [number, string | null, string | null, string | null, number];\n\nfunction evalScript(client: RedisClient, script: string, keys: string[], args: string[]): Promise<unknown> {\n return client.eval(script, { keys, arguments: args });\n}\n\nfunction toRunOwnershipRecord(runId: string, reply: OwnerReply): RunOwnershipRecord | null {\n const [, generation, ownerId, leaseExpiresAt, now] = reply;\n if (generation === null) return null;\n const expiresAt = leaseExpiresAt === null ? null : Number(leaseExpiresAt);\n return {\n runId,\n generation: Number(generation),\n ownerId: String(ownerId),\n leaseExpiresAt: expiresAt === null ? null : new Date(expiresAt),\n live: expiresAt !== null && expiresAt > Number(now),\n };\n}\n\nasync function evalOwner(\n client: RedisClient,\n script: string,\n runId: string,\n args: string[],\n): Promise<{ applied: boolean; record: RunOwnershipRecord | null }> {\n const reply = (await evalScript(client, script, [runClaimKey(TABLE_WORKFLOW_RUN_OWNERS, runId)], args)) as OwnerReply;\n return { applied: Number(reply[0]) === 1, record: toRunOwnershipRecord(runId, reply) };\n}\n\nexport async function claimRunOwnership(\n client: RedisClient,\n { runId, ownerId, leaseMs, force, expectedGeneration }: ClaimRunOwnershipInput,\n): Promise<ClaimRunOwnershipResult> {\n const { applied, record } = await evalOwner(client, CLAIM, runId, [\n ownerId,\n String(leaseMs),\n force ? '1' : '0',\n expectedGeneration === undefined ? '' : String(expectedGeneration),\n String(RUN_CLAIM_RETENTION_MS),\n ]);\n return applied ? { acquired: true, record: record! } : { acquired: false, record };\n}\n\nexport async function renewRunOwnership(\n client: RedisClient,\n { leaseMs, ...fence }: RenewRunOwnershipInput,\n): Promise<RenewRunOwnershipResult> {\n const { applied, record } = await evalOwner(client, RENEW, fence.runId, [\n String(fence.generation),\n fence.ownerId,\n String(leaseMs),\n String(RUN_CLAIM_RETENTION_MS),\n ]);\n return applied ? { renewed: true, record: record! } : { renewed: false, record };\n}\n\nexport async function releaseRunOwnership(client: RedisClient, fence: RunFence): Promise<boolean> {\n const released = await evalScript(\n client,\n RELEASE,\n [runClaimKey(TABLE_WORKFLOW_RUN_OWNERS, fence.runId)],\n [String(fence.generation), fence.ownerId, String(RUN_CLAIM_RETENTION_MS)],\n );\n return Number(released) === 1;\n}\n\nexport async function getRunOwnership(client: RedisClient, runId: string): Promise<RunOwnershipRecord | null> {\n return (await evalOwner(client, READ, runId, [])).record;\n}\n\nexport async function raiseRunFence(client: RedisClient, fence: RunFence): Promise<boolean> {\n const raised = await evalScript(\n client,\n RAISE,\n [runClaimKey(TABLE_MEMORY_RUN_FENCES, fence.runId)],\n [String(fence.generation), fence.ownerId],\n );\n return Number(raised) === 1;\n}\n\nexport async function retireRunFence(client: RedisClient, fence: RunFence): Promise<boolean> {\n const retired = await evalScript(\n client,\n RETIRE,\n [runClaimKey(TABLE_MEMORY_RUN_FENCES, fence.runId)],\n [String(fence.generation), fence.ownerId, String(RUN_CLAIM_RETENTION_MS)],\n );\n return Number(retired) === 1;\n}\n\n/**\n * Collects writes and applies them in one script that first checks the run's\n * claim against the fence. A fence that is no longer current makes `exec()`\n * throw `RunFenceConflictError` without writing anything. Scripts run\n * atomically, so a takeover lands either before the batch (failing its check)\n * or after it.\n */\nclass FencedWriteBatch implements WriteBatch {\n readonly #keys = new Set<string>();\n readonly #args: string[] = [];\n\n constructor(\n private readonly client: RedisClient,\n private readonly check: RunFenceCheck,\n ) {}\n\n #add(key: string, ...command: string[]): this {\n this.#keys.add(key);\n this.#args.push(String(command.length), ...command);\n return this;\n }\n\n set(key: string, value: string) {\n return this.#add(key, 'SET', key, value);\n }\n\n del(key: string) {\n return this.#add(key, 'DEL', key);\n }\n\n zAdd(key: string, { score, value }: { score: number; value: string }) {\n return this.#add(key, 'ZADD', key, String(score), value);\n }\n\n zRem(key: string, member: string) {\n return this.#add(key, 'ZREM', key, member);\n }\n\n async exec(): Promise<void> {\n const { claims, fence, operation } = this.check;\n const applied = await evalScript(\n this.client,\n FENCED_WRITE,\n [runClaimKey(claims, fence.runId), ...this.#keys],\n [String(fence.generation), fence.ownerId, ...this.#args],\n );\n if (Number(applied) !== 1) throw new RunFenceConflictError(fence, operation);\n }\n}\n\n/** A MULTI transaction, or a fenced batch when `check` is set. */\nexport function writeBatch(client: RedisClient, check: RunFenceCheck | undefined): WriteBatch {\n return check ? new FencedWriteBatch(client, check) : client.multi();\n}\n\n/**\n * Rejects a fenced write that turns out to have nothing to write when its\n * fence is no longer current, as the write itself would have been rejected.\n */\nexport async function assertRunFence(client: RedisClient, check: RunFenceCheck | undefined): Promise<void> {\n if (check) await new FencedWriteBatch(client, check).exec();\n}\n","import { MessageList } from '@mastra/core/agent';\nimport type { MastraMessageContentV2 } from '@mastra/core/agent';\nimport { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';\nimport type { MastraDBMessage, StorageThreadType } from '@mastra/core/memory';\nimport {\n isRunFenceConflictError,\n MemoryStorage,\n resolveRunFence,\n TABLE_MEMORY_RUN_FENCES,\n TABLE_RESOURCES,\n TABLE_THREADS,\n TABLE_MESSAGES,\n normalizePerPage,\n calculatePagination,\n createStorageErrorId,\n ensureDate,\n filterByDateRange,\n jsonValueEquals,\n storageMessageMatchesMetadataFilter,\n validateStorageMetadataFilter,\n} from '@mastra/core/storage';\nimport type {\n RunFence,\n StorageResourceType,\n StorageListMessagesInput,\n StorageListMessagesOutput,\n StorageListThreadsInput,\n StorageListThreadsOutput,\n ThreadOrderBy,\n ThreadSortDirection,\n StorageCloneThreadInput,\n StorageCloneThreadOutput,\n ThreadCloneMetadata,\n} from '@mastra/core/storage';\n\nimport { RedisDB } from '../../db';\nimport type { RedisDomainConfig } from '../../db';\nimport type { RedisClient } from '../../types';\nimport { assertRunFence, raiseRunFence, retireRunFence, runClaimKey, writeBatch } from '../run-fencing';\nimport type { RunFenceCheck } from '../run-fencing';\nimport { getKey, processRecord } from '../utils';\n\nexport class StoreMemoryRedis extends MemoryStorage {\n override readonly supportsPartialThreadUpdate = true;\n private client: RedisClient;\n private db: RedisDB;\n\n constructor(config: RedisDomainConfig) {\n super();\n this.client = config.client;\n this.db = new RedisDB({ client: config.client });\n }\n\n public override supportsRunFencing(): boolean {\n return true;\n }\n\n public override async raiseRunFence(fence: RunFence): Promise<boolean> {\n try {\n return await raiseRunFence(this.client, fence);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'RAISE_RUN_FENCE', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { runId: fence.runId },\n },\n error,\n );\n }\n }\n\n public override async retireRunFence(fence: RunFence): Promise<boolean> {\n try {\n return await retireRunFence(this.client, fence);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'RETIRE_RUN_FENCE', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { runId: fence.runId },\n },\n error,\n );\n }\n }\n\n #runFenceCheck(fence: RunFence | undefined, operation: string): RunFenceCheck | undefined {\n const resolved = resolveRunFence(this, fence);\n return resolved && { claims: TABLE_MEMORY_RUN_FENCES, fence: resolved, operation };\n }\n\n public async dangerouslyClearAll(): Promise<void> {\n await this.db.deleteData({ tableName: TABLE_THREADS });\n await this.db.deleteData({ tableName: TABLE_MESSAGES });\n await this.db.deleteData({ tableName: TABLE_RESOURCES });\n await this.db.scanAndDelete('msg-idx:*');\n await this.db.scanAndDelete('thread:*:messages');\n await this.db.scanAndDelete(`${runClaimKey(TABLE_MEMORY_RUN_FENCES, '')}*`);\n }\n\n public async getThreadById({\n threadId,\n resourceId,\n }: {\n threadId: string;\n resourceId?: string;\n }): Promise<StorageThreadType | null> {\n try {\n const thread = await this.db.get<StorageThreadType>({\n tableName: TABLE_THREADS,\n keys: { id: threadId },\n });\n\n if (!thread || (resourceId !== undefined && thread.resourceId !== resourceId)) {\n return null;\n }\n\n return {\n ...thread,\n createdAt: ensureDate(thread.createdAt)!,\n updatedAt: ensureDate(thread.updatedAt)!,\n metadata: typeof thread.metadata === 'string' ? JSON.parse(thread.metadata) : thread.metadata,\n };\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'GET_THREAD_BY_ID', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n threadId,\n },\n },\n error,\n );\n }\n }\n\n public async listThreadsByResourceId(args: StorageListThreadsInput): Promise<StorageListThreadsOutput> {\n return this.listThreads(args);\n }\n\n public async listThreads(args: StorageListThreadsInput): Promise<StorageListThreadsOutput> {\n const { page = 0, perPage: perPageInput, orderBy, filter } = args;\n const { field, direction } = this.parseOrderBy(orderBy);\n\n try {\n this.validatePaginationInput(page, perPageInput ?? 100);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_THREADS', 'INVALID_PAGE'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n details: { page, ...(perPageInput !== undefined && { perPage: perPageInput }) },\n },\n error instanceof Error ? error : new Error('Invalid pagination parameters'),\n );\n }\n\n const perPage = normalizePerPage(perPageInput, 100);\n\n try {\n this.validateMetadataKeys(filter?.metadata);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_THREADS', 'INVALID_METADATA_KEY'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n details: { metadataKeys: filter?.metadata ? Object.keys(filter.metadata).join(', ') : '' },\n },\n error instanceof Error ? error : new Error('Invalid metadata key'),\n );\n }\n\n const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);\n\n try {\n let allThreads: StorageThreadType[] = [];\n const pattern = `${TABLE_THREADS}:*`;\n const keys = await this.db.scanKeys(pattern);\n\n if (keys.length === 0) {\n return {\n threads: [],\n total: 0,\n page,\n perPage: perPageForResponse,\n hasMore: false,\n };\n }\n\n const results = await this.client.mGet(keys);\n\n for (let i = 0; i < results.length; i++) {\n const data = results[i];\n if (!data) {\n continue;\n }\n const thread = JSON.parse(data) as StorageThreadType;\n\n if (filter?.resourceId && thread.resourceId !== filter.resourceId) {\n continue;\n }\n\n if (filter?.metadata && Object.keys(filter.metadata).length > 0) {\n const threadMetadata = typeof thread.metadata === 'string' ? JSON.parse(thread.metadata) : thread.metadata;\n const matches = Object.entries(filter.metadata).every(([key, value]) =>\n jsonValueEquals(threadMetadata?.[key], value),\n );\n if (!matches) {\n continue;\n }\n }\n\n allThreads.push({\n ...thread,\n createdAt: ensureDate(thread.createdAt)!,\n updatedAt: ensureDate(thread.updatedAt)!,\n metadata: typeof thread.metadata === 'string' ? JSON.parse(thread.metadata) : thread.metadata,\n });\n }\n\n const sortedThreads = this.sortThreads(allThreads, field, direction);\n const total = sortedThreads.length;\n const end = perPageInput === false ? total : offset + perPage;\n const paginatedThreads = sortedThreads.slice(offset, end);\n const hasMore = perPageInput === false ? false : end < total;\n\n return {\n threads: paginatedThreads,\n total,\n page,\n perPage: perPageForResponse,\n hasMore,\n };\n } catch (error) {\n // Re-throw USER errors (validation errors) directly so callers get proper 400 responses\n if (error instanceof MastraError && error.category === ErrorCategory.USER) {\n throw error;\n }\n const mastraError = new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_THREADS', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n ...(filter?.resourceId && { resourceId: filter.resourceId }),\n hasMetadataFilter: !!filter?.metadata,\n page,\n perPage,\n },\n },\n error,\n );\n this.logger.trackException(mastraError);\n this.logger.error(mastraError.toString());\n throw mastraError;\n }\n }\n\n public async saveThread({\n thread,\n fence,\n }: {\n thread: StorageThreadType;\n fence?: RunFence;\n }): Promise<StorageThreadType> {\n const check = this.#runFenceCheck(fence, 'saveThread');\n try {\n const { key, processedRecord } = processRecord(TABLE_THREADS, thread);\n const batch = writeBatch(this.client, check);\n batch.set(key, JSON.stringify(processedRecord));\n await batch.exec();\n return thread;\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n const mastraError = new MastraError(\n {\n id: createStorageErrorId('REDIS', 'SAVE_THREAD', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n threadId: thread.id,\n },\n },\n error,\n );\n this.logger.trackException(mastraError);\n this.logger.error(mastraError.toString());\n throw mastraError;\n }\n }\n\n public async updateThread({\n id,\n title,\n metadata,\n fence,\n }: {\n id: string;\n title?: string;\n metadata?: Record<string, unknown>;\n fence?: RunFence;\n }): Promise<StorageThreadType> {\n const thread = await this.getThreadById({ threadId: id });\n if (!thread) {\n throw new MastraError({\n id: createStorageErrorId('REDIS', 'UPDATE_THREAD', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n text: `Thread ${id} not found`,\n details: {\n threadId: id,\n },\n });\n }\n\n const updatedThread = {\n ...thread,\n title: title ?? thread.title,\n metadata: {\n ...thread.metadata,\n ...metadata,\n },\n updatedAt: new Date(),\n };\n\n try {\n await this.saveThread({ thread: updatedThread, fence });\n return updatedThread;\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'UPDATE_THREAD', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n threadId: id,\n },\n },\n error,\n );\n }\n }\n\n public async deleteThread({ threadId }: { threadId: string }): Promise<void> {\n const threadKey = getKey(TABLE_THREADS, { id: threadId });\n const threadMessagesKey = getThreadMessagesKey(threadId);\n\n try {\n const messageIds = await this.client.zRange(threadMessagesKey, 0, -1);\n\n const multi = this.client.multi();\n multi.del(threadKey);\n multi.del(threadMessagesKey);\n\n for (const messageId of messageIds) {\n const messageKey = getMessageKey(threadId, messageId);\n multi.del(messageKey);\n multi.del(getMessageIndexKey(messageId));\n }\n\n await multi.exec();\n await this.db.scanAndDelete(getMessageKey(threadId, '*'));\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'DELETE_THREAD', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n threadId,\n },\n },\n error,\n );\n }\n }\n\n public async saveMessages(args: {\n messages: MastraDBMessage[];\n fence?: RunFence;\n }): Promise<{ messages: MastraDBMessage[] }> {\n const { messages } = args;\n const check = this.#runFenceCheck(args.fence, 'saveMessages');\n if (messages.length === 0) {\n return { messages: [] };\n }\n\n const threadId = messages[0]?.threadId;\n try {\n if (!threadId) {\n throw new Error('Thread ID is required');\n }\n const thread = await this.getThreadById({ threadId });\n if (!thread) {\n throw new Error(`Thread ${threadId} not found`);\n }\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'SAVE_MESSAGES', 'INVALID_ARGS'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n },\n error,\n );\n }\n\n const messagesWithIndex = messages.map((message, index) => {\n if (!message.threadId) {\n throw new Error(\n `Expected to find a threadId for message, but couldn't find one. An unexpected error has occurred.`,\n );\n }\n if (!message.resourceId) {\n throw new Error(\n `Expected to find a resourceId for message, but couldn't find one. An unexpected error has occurred.`,\n );\n }\n return {\n ...message,\n _index: index,\n };\n });\n\n const threadKey = getKey(TABLE_THREADS, { id: threadId });\n const existingThreadData = await this.client.get(threadKey);\n const existingThread = existingThreadData ? (JSON.parse(existingThreadData) as StorageThreadType) : null;\n\n try {\n const batchSize = 1000;\n const existingThreadIds = await this.client.mGet(\n messagesWithIndex.map(message => getMessageIndexKey(message.id)),\n );\n\n for (let i = 0; i < messagesWithIndex.length; i += batchSize) {\n const batch = messagesWithIndex.slice(i, i + batchSize);\n const batchExistingThreadIds = existingThreadIds.slice(i, i + batch.length);\n const multi = writeBatch(this.client, check);\n\n for (const [batchIndex, message] of batch.entries()) {\n const key = getMessageKey(message.threadId!, message.id);\n const score = getMessageScore(message);\n const existingThreadId = batchExistingThreadIds[batchIndex];\n\n if (existingThreadId && existingThreadId !== message.threadId) {\n const existingMessageKey = getMessageKey(existingThreadId, message.id);\n multi.del(existingMessageKey);\n multi.zRem(getThreadMessagesKey(existingThreadId), message.id);\n }\n\n multi.set(key, JSON.stringify(message));\n multi.set(getMessageIndexKey(message.id), message.threadId!);\n multi.zAdd(getThreadMessagesKey(message.threadId!), { score, value: message.id });\n }\n\n if (i === 0 && existingThread) {\n const updatedThread = {\n ...existingThread,\n updatedAt: new Date(),\n };\n multi.set(threadKey, JSON.stringify(processRecord(TABLE_THREADS, updatedThread).processedRecord));\n }\n\n await multi.exec();\n }\n\n const list = new MessageList().add(messages as Parameters<MessageList['add']>[0], 'memory');\n return { messages: list.get.all.db() };\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'SAVE_MESSAGES', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n threadId,\n },\n },\n error,\n );\n }\n }\n\n private async getThreadIdForMessage(messageId: string): Promise<string | null> {\n const indexedThreadId = await this.client.get(getMessageIndexKey(messageId));\n if (indexedThreadId) {\n return indexedThreadId;\n }\n\n const keys = await this.db.scanKeys(getMessageKey('*', messageId));\n if (keys.length === 0) {\n return null;\n }\n\n const messageData = await this.client.get(keys[0] as string);\n if (!messageData) {\n return null;\n }\n\n const message = JSON.parse(messageData) as MastraDBMessage;\n if (message.threadId) {\n await this.client.set(getMessageIndexKey(messageId), message.threadId);\n }\n\n return message.threadId || null;\n }\n\n /**\n * Fetches the messages named by `include` together with their surrounding context.\n *\n * @param include - Message ids to pin, each with an optional before/after window.\n * @param resourceId - When set, drops any pinned or context message owned by another\n * resource so an id from another resource returns nothing.\n */\n private async getIncludedMessages(\n include: StorageListMessagesInput['include'],\n resourceId?: string,\n ): Promise<MastraDBMessage[]> {\n if (!include?.length) {\n return [];\n }\n\n const messageIds = new Set<string>();\n const messageIdToThreadIds: Record<string, string> = {};\n\n for (const item of include) {\n const itemThreadId = await this.getThreadIdForMessage(item.id);\n if (!itemThreadId) {\n continue;\n }\n\n const itemThreadMessagesKey = getThreadMessagesKey(itemThreadId);\n\n if (resourceId !== undefined) {\n const threadMessageIds = await this.client.zRange(itemThreadMessagesKey, 0, -1);\n const threadMessages = (await this.client.mGet(threadMessageIds.map(id => getMessageKey(itemThreadId, id))))\n .filter((data): data is string => data !== null)\n .map(data => JSON.parse(data) as MastraDBMessage)\n .filter(message => message.resourceId === resourceId);\n const targetIndex = threadMessages.findIndex(message => message.id === item.id);\n if (targetIndex === -1) {\n continue;\n }\n\n const start = Math.max(0, targetIndex - (item.withPreviousMessages ?? 0));\n const end = Math.min(threadMessages.length, targetIndex + (item.withNextMessages ?? 0) + 1);\n for (const message of threadMessages.slice(start, end)) {\n messageIds.add(message.id);\n messageIdToThreadIds[message.id] = itemThreadId;\n }\n continue;\n }\n\n messageIds.add(item.id);\n messageIdToThreadIds[item.id] = itemThreadId;\n const rank = await this.client.zRank(itemThreadMessagesKey, item.id);\n if (rank === null) {\n continue;\n }\n\n if (item.withPreviousMessages) {\n const start = Math.max(0, rank - item.withPreviousMessages);\n const prevIds = rank === 0 ? [] : await this.client.zRange(itemThreadMessagesKey, start, rank - 1);\n prevIds.forEach(id => {\n messageIds.add(id);\n messageIdToThreadIds[id] = itemThreadId;\n });\n }\n\n if (item.withNextMessages) {\n const nextIds = await this.client.zRange(itemThreadMessagesKey, rank + 1, rank + item.withNextMessages);\n nextIds.forEach(id => {\n messageIds.add(id);\n messageIdToThreadIds[id] = itemThreadId;\n });\n }\n }\n\n if (messageIds.size === 0) {\n return [];\n }\n\n const keysToFetch = Array.from(messageIds).map(id => getMessageKey(messageIdToThreadIds[id]!, id));\n const results = await this.client.mGet(keysToFetch);\n\n const includedMessages = results\n .filter((data): data is string => data !== null)\n .map(data => JSON.parse(data) as MastraDBMessage);\n return resourceId ? includedMessages.filter(message => message.resourceId === resourceId) : includedMessages;\n }\n\n private parseStoredMessage(storedMessage: MastraDBMessage & { _index?: number }): MastraDBMessage {\n const defaultMessageContent = { format: 2, parts: [{ type: 'text', text: '' }] };\n const { _index, ...rest } = storedMessage;\n return {\n ...rest,\n createdAt: new Date(rest.createdAt),\n content: rest.content || defaultMessageContent,\n } satisfies MastraDBMessage;\n }\n\n public async listMessagesById({ messageIds }: { messageIds: string[] }): Promise<{ messages: MastraDBMessage[] }> {\n if (messageIds.length === 0) {\n return { messages: [] };\n }\n\n try {\n const rawMessages: (MastraDBMessage & { _index?: number })[] = [];\n\n const indexKeys = messageIds.map(id => getMessageIndexKey(id));\n const indexResults = await this.client.mGet(indexKeys);\n\n const indexedIds: { messageId: string; threadId: string }[] = [];\n const unindexedIds: string[] = [];\n\n messageIds.forEach((id, i) => {\n const threadId = indexResults[i];\n if (threadId) {\n indexedIds.push({ messageId: id, threadId });\n return;\n }\n unindexedIds.push(id);\n });\n\n if (indexedIds.length > 0) {\n const messageKeys = indexedIds.map(({ messageId, threadId }) => getMessageKey(threadId, messageId));\n const messageResults = await this.client.mGet(messageKeys);\n for (const data of messageResults) {\n if (data) {\n rawMessages.push(JSON.parse(data) as MastraDBMessage & { _index?: number });\n }\n }\n }\n\n if (unindexedIds.length > 0) {\n const threadKeys = await this.db.scanKeys('thread:*:messages');\n\n const result = await Promise.all(\n threadKeys.map(async threadKey => {\n const threadId = threadKey.split(':')[1];\n if (!threadId) {\n throw new Error(`Failed to parse thread ID from thread key \"${threadKey}\"`);\n }\n const msgKeys = unindexedIds.map(id => getMessageKey(threadId, id));\n return this.client.mGet(msgKeys);\n }),\n );\n\n const foundMessages = result\n .flat(1)\n .filter((data): data is string => !!data)\n .map(data => JSON.parse(data) as MastraDBMessage & { _index?: number });\n rawMessages.push(...foundMessages);\n\n if (foundMessages.length > 0) {\n const multi = this.client.multi();\n foundMessages.forEach(msg => {\n if (msg.threadId) {\n multi.set(getMessageIndexKey(msg.id), msg.threadId);\n }\n });\n await multi.exec();\n }\n }\n\n const list = new MessageList().add(rawMessages.map(this.parseStoredMessage), 'memory');\n return { messages: list.get.all.db() };\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_MESSAGES_BY_ID', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n messageIds: JSON.stringify(messageIds),\n },\n },\n error,\n );\n }\n }\n\n public async listMessages(args: StorageListMessagesInput): Promise<StorageListMessagesOutput> {\n const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;\n\n const threadIds = Array.isArray(threadId) ? threadId : [threadId];\n const threadIdsSet = new Set(threadIds);\n\n if (threadIds.length === 0 || threadIds.some(id => !id.trim())) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_MESSAGES', 'INVALID_THREAD_ID'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n details: { threadId: Array.isArray(threadId) ? threadId.join(',') : threadId },\n },\n new Error('threadId must be a non-empty string or array of non-empty strings'),\n );\n }\n\n const perPage = normalizePerPage(perPageInput, 40);\n const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);\n const metadataFilter = validateStorageMetadataFilter(filter?.metadata);\n\n try {\n if (page < 0) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_MESSAGES', 'INVALID_PAGE'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n details: { page },\n },\n new Error('page must be >= 0'),\n );\n }\n\n const { field, direction } = this.parseOrderBy(orderBy, 'ASC');\n\n const getFieldValue = (msg: MastraDBMessage): number => {\n if (field === 'createdAt') {\n return new Date(msg.createdAt).getTime();\n }\n\n const value = (msg as Record<string, unknown>)[field];\n if (typeof value === 'number') {\n return value;\n }\n if (value instanceof Date) {\n return value.getTime();\n }\n return 0;\n };\n\n if (perPage === 0 && (!include || include.length === 0)) {\n return {\n messages: [],\n total: 0,\n page,\n perPage: perPageForResponse,\n hasMore: false,\n };\n }\n\n let includedMessages: MastraDBMessage[] = [];\n if (include && include.length > 0) {\n const included = (await this.getIncludedMessages(include, resourceId)) as MastraDBMessage[];\n includedMessages = included.map(this.parseStoredMessage);\n }\n\n if (perPage === 0 && include && include.length > 0) {\n const list = new MessageList().add(includedMessages, 'memory');\n const messages = list.get.all.db().sort((a, b) => {\n const aValue = getFieldValue(a);\n const bValue = getFieldValue(b);\n return direction === 'ASC' ? aValue - bValue : bValue - aValue;\n });\n\n return {\n messages,\n total: 0,\n page,\n perPage: perPageForResponse,\n hasMore: false,\n };\n }\n\n const perThreadIds = await Promise.all(\n threadIds.map(tid => this.client.zRange(getThreadMessagesKey(tid), 0, -1)),\n );\n const allMessageIdsWithThreads: { threadId: string; messageId: string }[] = [];\n threadIds.forEach((tid, i) => {\n for (const mid of perThreadIds[i]!) {\n allMessageIdsWithThreads.push({ threadId: tid, messageId: mid });\n }\n });\n\n if (allMessageIdsWithThreads.length === 0) {\n return {\n messages: [],\n total: 0,\n page,\n perPage: perPageForResponse,\n hasMore: false,\n };\n }\n\n const messageKeys = allMessageIdsWithThreads.map(({ threadId: tid, messageId }) => getMessageKey(tid, messageId));\n const results = await this.client.mGet(messageKeys);\n\n let messagesData = results\n .filter((data): data is string => data !== null)\n .map(data => JSON.parse(data) as MastraDBMessage & { _index?: number })\n .map(this.parseStoredMessage);\n\n if (resourceId) {\n messagesData = messagesData.filter(msg => msg.resourceId === resourceId);\n }\n\n messagesData = filterByDateRange(\n messagesData,\n (msg: MastraDBMessage) => new Date(msg.createdAt),\n filter?.dateRange,\n );\n\n messagesData = messagesData.filter(message =>\n storageMessageMatchesMetadataFilter(message.content, metadataFilter),\n );\n\n messagesData.sort((a, b) => {\n const aValue = getFieldValue(a);\n const bValue = getFieldValue(b);\n return direction === 'ASC' ? aValue - bValue : bValue - aValue;\n });\n\n const total = messagesData.length;\n const start = offset;\n const end = perPageInput === false ? total : start + perPage;\n const paginatedMessages = messagesData.slice(start, end);\n\n const messageIdsSet = new Set<string>();\n const allMessages: MastraDBMessage[] = [];\n\n for (const msg of paginatedMessages) {\n if (messageIdsSet.has(msg.id)) {\n continue;\n }\n allMessages.push(msg);\n messageIdsSet.add(msg.id);\n }\n\n for (const msg of includedMessages) {\n if (messageIdsSet.has(msg.id)) {\n continue;\n }\n allMessages.push(msg);\n messageIdsSet.add(msg.id);\n }\n\n const list = new MessageList().add(allMessages, 'memory');\n let finalMessages = list.get.all.db();\n\n finalMessages = finalMessages.sort((a, b) => {\n const aValue = getFieldValue(a);\n const bValue = getFieldValue(b);\n return direction === 'ASC' ? aValue - bValue : bValue - aValue;\n });\n\n const returnedThreadMessageIds = new Set(\n finalMessages\n .filter(message => message.threadId && threadIdsSet.has(message.threadId))\n .map(message => message.id),\n );\n const hasMore =\n perPageInput !== false &&\n (metadataFilter || returnedThreadMessageIds.size < total) &&\n offset + paginatedMessages.length < total;\n\n return {\n messages: finalMessages,\n total,\n page,\n perPage: perPageForResponse,\n hasMore,\n };\n } catch (error) {\n // Re-throw USER errors (validation errors) directly so callers get proper 400 responses\n if (error instanceof MastraError && error.category === ErrorCategory.USER) {\n throw error;\n }\n const mastraError = new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_MESSAGES', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n threadId: Array.isArray(threadId) ? threadId.join(',') : threadId,\n resourceId: resourceId ?? '',\n },\n },\n error,\n );\n this.logger.error(mastraError.toString());\n this.logger.trackException(mastraError);\n throw mastraError;\n }\n }\n\n public async getResourceById({ resourceId }: { resourceId: string }): Promise<StorageResourceType | null> {\n try {\n const key = `${TABLE_RESOURCES}:${resourceId}`;\n const data = await this.client.get(key);\n if (!data) {\n return null;\n }\n\n const resource = JSON.parse(data) as StorageResourceType;\n\n return {\n ...resource,\n createdAt: new Date(resource.createdAt),\n updatedAt: new Date(resource.updatedAt),\n workingMemory:\n typeof resource.workingMemory === 'object' ? JSON.stringify(resource.workingMemory) : resource.workingMemory,\n metadata: typeof resource.metadata === 'string' ? JSON.parse(resource.metadata) : resource.metadata,\n };\n } catch (error) {\n this.logger.error('Error getting resource by ID:', error);\n throw error;\n }\n }\n\n public async saveResource({ resource }: { resource: StorageResourceType }): Promise<StorageResourceType> {\n return this.#saveResource(resource, undefined);\n }\n\n async #saveResource(resource: StorageResourceType, check: RunFenceCheck | undefined): Promise<StorageResourceType> {\n try {\n const key = `${TABLE_RESOURCES}:${resource.id}`;\n const serializedResource = {\n ...resource,\n metadata: JSON.stringify(resource.metadata),\n createdAt: resource.createdAt.toISOString(),\n updatedAt: resource.updatedAt.toISOString(),\n };\n\n const batch = writeBatch(this.client, check);\n batch.set(key, JSON.stringify(serializedResource));\n await batch.exec();\n\n return resource;\n } catch (error) {\n if (!isRunFenceConflictError(error)) this.logger.error('Error saving resource:', error);\n throw error;\n }\n }\n\n public async updateResource({\n resourceId,\n workingMemory,\n metadata,\n fence,\n }: {\n resourceId: string;\n workingMemory?: string;\n metadata?: Record<string, unknown>;\n fence?: RunFence;\n }): Promise<StorageResourceType> {\n const check = this.#runFenceCheck(fence, 'updateResource');\n try {\n const existingResource = await this.getResourceById({ resourceId });\n\n if (!existingResource) {\n const newResource: StorageResourceType = {\n id: resourceId,\n workingMemory,\n metadata: metadata || {},\n createdAt: new Date(),\n updatedAt: new Date(),\n };\n return await this.#saveResource(newResource, check);\n }\n\n const updatedResource = {\n ...existingResource,\n workingMemory: workingMemory !== undefined ? workingMemory : existingResource.workingMemory,\n metadata: {\n ...existingResource.metadata,\n ...metadata,\n },\n updatedAt: new Date(),\n };\n\n await this.#saveResource(updatedResource, check);\n return updatedResource;\n } catch (error) {\n if (!isRunFenceConflictError(error)) this.logger.error('Error updating resource:', error);\n throw error;\n }\n }\n\n public async updateMessages(args: {\n messages: (Partial<Omit<MastraDBMessage, 'createdAt'>> & {\n id: string;\n content?: { metadata?: MastraMessageContentV2['metadata']; content?: MastraMessageContentV2['content'] };\n })[];\n fence?: RunFence;\n }): Promise<MastraDBMessage[]> {\n const { messages } = args;\n if (messages.length === 0) {\n return [];\n }\n const check = this.#runFenceCheck(args.fence, 'updateMessages');\n\n try {\n const messageIds = messages.map(m => m.id);\n const existingMessages: MastraDBMessage[] = [];\n const messageIdToKey: Record<string, string> = {};\n\n for (const messageId of messageIds) {\n const pattern = getMessageKey('*', messageId);\n const keys = await this.db.scanKeys(pattern);\n\n for (const key of keys) {\n const data = await this.client.get(key);\n if (!data) {\n continue;\n }\n const message = JSON.parse(data) as MastraDBMessage;\n if (message && message.id === messageId) {\n existingMessages.push(message);\n messageIdToKey[messageId] = key;\n break;\n }\n }\n }\n\n if (existingMessages.length === 0) {\n await assertRunFence(this.client, check);\n return [];\n }\n\n const threadIdsToUpdate = new Set<string>();\n const multi = writeBatch(this.client, check);\n\n for (const existingMessage of existingMessages) {\n const updatePayload = messages.find(m => m.id === existingMessage.id);\n if (!updatePayload) {\n continue;\n }\n\n const { id, ...fieldsToUpdate } = updatePayload;\n if (Object.keys(fieldsToUpdate).length === 0) {\n continue;\n }\n\n threadIdsToUpdate.add(existingMessage.threadId!);\n if (updatePayload.threadId && updatePayload.threadId !== existingMessage.threadId) {\n threadIdsToUpdate.add(updatePayload.threadId);\n }\n\n const updatedMessage = { ...existingMessage };\n\n if (fieldsToUpdate.content) {\n const existingContent = existingMessage.content as MastraMessageContentV2;\n const newContent = {\n ...existingContent,\n ...fieldsToUpdate.content,\n ...(existingContent?.metadata && fieldsToUpdate.content.metadata\n ? {\n metadata: {\n ...existingContent.metadata,\n ...fieldsToUpdate.content.metadata,\n },\n }\n : {}),\n };\n updatedMessage.content = newContent;\n }\n\n for (const key in fieldsToUpdate) {\n if (Object.prototype.hasOwnProperty.call(fieldsToUpdate, key) && key !== 'content') {\n (updatedMessage as Record<string, unknown>)[key] = fieldsToUpdate[key as keyof typeof fieldsToUpdate];\n }\n }\n\n const key = messageIdToKey[id];\n if (!key) {\n continue;\n }\n\n if (updatePayload.threadId && updatePayload.threadId !== existingMessage.threadId) {\n multi.zRem(getThreadMessagesKey(existingMessage.threadId!), id);\n multi.del(key);\n\n const newKey = getMessageKey(updatePayload.threadId, id);\n multi.set(newKey, JSON.stringify(updatedMessage));\n multi.set(getMessageIndexKey(id), updatePayload.threadId);\n\n const score = getMessageScore(updatedMessage as MastraDBMessage & { _index?: number });\n multi.zAdd(getThreadMessagesKey(updatePayload.threadId), { score, value: id });\n\n messageIdToKey[id] = newKey;\n continue;\n }\n\n multi.set(key, JSON.stringify(updatedMessage));\n }\n\n const now = new Date();\n for (const threadId of threadIdsToUpdate) {\n if (threadId) {\n const threadKey = getKey(TABLE_THREADS, { id: threadId });\n const existingThreadData = await this.client.get(threadKey);\n if (existingThreadData) {\n const existingThread = JSON.parse(existingThreadData) as StorageThreadType;\n const updatedThread = {\n ...existingThread,\n updatedAt: now,\n };\n multi.set(threadKey, JSON.stringify(processRecord(TABLE_THREADS, updatedThread).processedRecord));\n }\n }\n }\n\n await multi.exec();\n\n const updatedMessages: MastraDBMessage[] = [];\n for (const messageId of messageIds) {\n const key = messageIdToKey[messageId];\n if (key) {\n const data = await this.client.get(key);\n if (data) {\n updatedMessages.push(JSON.parse(data) as MastraDBMessage);\n }\n }\n }\n\n return updatedMessages;\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'UPDATE_MESSAGES', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n messageIds: messages.map(m => m.id).join(','),\n },\n },\n error,\n );\n }\n }\n\n public async deleteMessages(messageIds: string[], options?: { fence?: RunFence }): Promise<void> {\n if (!messageIds || messageIds.length === 0) {\n return;\n }\n const check = this.#runFenceCheck(options?.fence, 'deleteMessages');\n\n try {\n const threadIds = new Set<string>();\n const messageKeys: string[] = [];\n const foundMessageIds: string[] = [];\n const messageIdToThreadId = new Map<string, string>();\n\n const indexKeys = messageIds.map(id => getMessageIndexKey(id));\n const indexResults = await this.client.mGet(indexKeys);\n\n const indexedMessages: { messageId: string; threadId: string }[] = [];\n const unindexedMessageIds: string[] = [];\n\n messageIds.forEach((id, i) => {\n const threadId = indexResults[i];\n if (threadId) {\n indexedMessages.push({ messageId: id, threadId });\n return;\n }\n unindexedMessageIds.push(id);\n });\n\n for (const { messageId, threadId } of indexedMessages) {\n messageKeys.push(getMessageKey(threadId, messageId));\n foundMessageIds.push(messageId);\n messageIdToThreadId.set(messageId, threadId);\n threadIds.add(threadId);\n }\n\n for (const messageId of unindexedMessageIds) {\n const pattern = getMessageKey('*', messageId);\n const keys = await this.db.scanKeys(pattern);\n\n for (const key of keys) {\n const data = await this.client.get(key);\n if (!data) {\n continue;\n }\n const message = JSON.parse(data) as MastraDBMessage;\n if (message && message.id === messageId) {\n messageKeys.push(key);\n foundMessageIds.push(messageId);\n if (message.threadId) {\n messageIdToThreadId.set(messageId, message.threadId);\n threadIds.add(message.threadId);\n }\n break;\n }\n }\n }\n\n if (messageKeys.length === 0) {\n await assertRunFence(this.client, check);\n return;\n }\n\n const multi = writeBatch(this.client, check);\n\n for (const key of messageKeys) {\n multi.del(key);\n }\n\n for (const messageId of foundMessageIds) {\n multi.del(getMessageIndexKey(messageId));\n }\n\n if (threadIds.size > 0) {\n for (const threadId of threadIds) {\n for (const [msgId, msgThreadId] of messageIdToThreadId) {\n if (msgThreadId === threadId) {\n multi.zRem(getThreadMessagesKey(threadId), msgId);\n }\n }\n\n const threadKey = getKey(TABLE_THREADS, { id: threadId });\n const threadData = await this.client.get(threadKey);\n if (!threadData) {\n continue;\n }\n\n const thread = JSON.parse(threadData) as StorageThreadType;\n const updatedThread = { ...thread, updatedAt: new Date() };\n multi.set(threadKey, JSON.stringify(processRecord(TABLE_THREADS, updatedThread).processedRecord));\n }\n }\n\n await multi.exec();\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'DELETE_MESSAGES', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { messageIds: messageIds.join(', ') },\n },\n error,\n );\n }\n }\n\n private sortThreads(\n threads: StorageThreadType[],\n field: ThreadOrderBy,\n direction: ThreadSortDirection,\n ): StorageThreadType[] {\n return threads.sort((a, b) => {\n const aValue = new Date(a[field]).getTime();\n const bValue = new Date(b[field]).getTime();\n return direction === 'ASC' ? aValue - bValue : bValue - aValue;\n });\n }\n\n public async cloneThread(args: StorageCloneThreadInput): Promise<StorageCloneThreadOutput> {\n const { sourceThreadId, newThreadId: providedThreadId, resourceId, title, metadata, options } = args;\n\n const sourceThread = await this.getThreadById({ threadId: sourceThreadId });\n if (!sourceThread) {\n throw new MastraError({\n id: createStorageErrorId('REDIS', 'CLONE_THREAD', 'SOURCE_NOT_FOUND'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n text: `Source thread with id ${sourceThreadId} not found`,\n details: { sourceThreadId },\n });\n }\n\n const newThreadId = providedThreadId || crypto.randomUUID();\n\n const existingThread = await this.getThreadById({ threadId: newThreadId });\n if (existingThread) {\n throw new MastraError({\n id: createStorageErrorId('REDIS', 'CLONE_THREAD', 'THREAD_EXISTS'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n text: `Thread with id ${newThreadId} already exists`,\n details: { newThreadId },\n });\n }\n\n try {\n const threadMessagesKey = getThreadMessagesKey(sourceThreadId);\n const msgIds = await this.client.zRange(threadMessagesKey, 0, -1);\n\n const messageKeys = msgIds.map(mid => getMessageKey(sourceThreadId, mid));\n let sourceMessages: (MastraDBMessage & { _index?: number })[] = [];\n\n if (messageKeys.length > 0) {\n const results = await this.client.mGet(messageKeys);\n sourceMessages = results\n .filter((data): data is string => data !== null)\n .map(data => {\n const msg = JSON.parse(data) as MastraDBMessage & { _index?: number };\n return { ...msg, createdAt: new Date(msg.createdAt) };\n });\n }\n\n if (options?.messageFilter?.startDate || options?.messageFilter?.endDate) {\n sourceMessages = filterByDateRange(sourceMessages, (msg: MastraDBMessage) => new Date(msg.createdAt), {\n start: options.messageFilter?.startDate,\n end: options.messageFilter?.endDate,\n });\n }\n\n if (options?.messageFilter?.messageIds && options.messageFilter.messageIds.length > 0) {\n const messageIdSet = new Set(options.messageFilter.messageIds);\n sourceMessages = sourceMessages.filter(msg => messageIdSet.has(msg.id));\n }\n\n sourceMessages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());\n\n if (options?.messageLimit && options.messageLimit > 0 && sourceMessages.length > options.messageLimit) {\n sourceMessages = sourceMessages.slice(-options.messageLimit);\n }\n\n const now = new Date();\n const lastMessageId = sourceMessages.length > 0 ? sourceMessages[sourceMessages.length - 1]!.id : undefined;\n\n const cloneMetadata: ThreadCloneMetadata = {\n sourceThreadId,\n clonedAt: now,\n ...(lastMessageId && { lastMessageId }),\n };\n\n const newThread: StorageThreadType = {\n id: newThreadId,\n resourceId: resourceId || sourceThread.resourceId,\n title: title || (sourceThread.title ? `Clone of ${sourceThread.title}` : undefined),\n metadata: { ...metadata, clone: cloneMetadata },\n createdAt: now,\n updatedAt: now,\n };\n\n const multi = this.client.multi();\n const threadKey = getKey(TABLE_THREADS, { id: newThreadId });\n multi.set(threadKey, JSON.stringify(processRecord(TABLE_THREADS, newThread).processedRecord));\n\n const clonedMessages: MastraDBMessage[] = [];\n const messageIdMap: Record<string, string> = Object.create(null);\n const targetResourceId = resourceId || sourceThread.resourceId;\n const newThreadMessagesKey = getThreadMessagesKey(newThreadId);\n\n for (let i = 0; i < sourceMessages.length; i++) {\n const sourceMsg = sourceMessages[i]!;\n const newMessageId = crypto.randomUUID();\n const { _index, ...restMsg } = sourceMsg;\n\n const newMessage: MastraDBMessage = {\n ...restMsg,\n id: newMessageId,\n threadId: newThreadId,\n resourceId: targetResourceId,\n };\n\n const messageKey = getMessageKey(newThreadId, newMessageId);\n multi.set(messageKey, JSON.stringify(newMessage));\n multi.set(getMessageIndexKey(newMessageId), newThreadId);\n const score = getMessageScore({ createdAt: newMessage.createdAt, _index: i });\n multi.zAdd(newThreadMessagesKey, { score, value: newMessageId });\n\n clonedMessages.push(newMessage);\n messageIdMap[sourceMsg.id] = newMessageId;\n }\n\n await multi.exec();\n\n return {\n thread: newThread,\n clonedMessages,\n messageIdMap,\n };\n } catch (error) {\n if (error instanceof MastraError) {\n throw error;\n }\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'CLONE_THREAD', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { sourceThreadId, newThreadId },\n },\n error,\n );\n }\n }\n}\n\nfunction getThreadMessagesKey(threadId: string): string {\n return `thread:${threadId}:messages`;\n}\n\nfunction getMessageKey(threadId: string, messageId: string): string {\n return getKey(TABLE_MESSAGES, { threadId, id: messageId });\n}\n\nfunction getMessageIndexKey(messageId: string): string {\n return `msg-idx:${messageId}`;\n}\n\nfunction getMessageScore(message: { createdAt: Date | string; _index?: number }): number {\n const createdAtScore = new Date(message.createdAt).getTime();\n const index = typeof message._index === 'number' ? message._index : 0;\n return createdAtScore * 1000 + index;\n}\n","import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';\nimport type { SaveScorePayload, ScoreRowData, ScoringSource } from '@mastra/core/evals';\nimport { saveScorePayloadSchema } from '@mastra/core/evals';\nimport {\n calculatePagination,\n normalizePerPage,\n ScoresStorage,\n TABLE_SCORERS,\n transformScoreRow,\n createStorageErrorId,\n} from '@mastra/core/storage';\nimport type { PaginationInfo, StoragePagination, ScoreTenancyFilters } from '@mastra/core/storage';\n\nimport { RedisDB } from '../../db';\nimport type { RedisDomainConfig } from '../../db';\nimport type { RedisClient } from '../../types';\nimport { processRecord } from '../utils';\n\n/** Returns true when a row matches the multi-tenant scope filters (or none provided). */\nfunction matchesTenancy(row: Record<string, unknown>, filters?: ScoreTenancyFilters): boolean {\n if (filters?.organizationId !== undefined && row.organizationId !== filters.organizationId) return false;\n if (filters?.projectId !== undefined && row.projectId !== filters.projectId) return false;\n return true;\n}\n\nexport class ScoresRedis extends ScoresStorage {\n private client: RedisClient;\n private db: RedisDB;\n\n constructor(config: RedisDomainConfig) {\n super();\n this.client = config.client;\n this.db = new RedisDB({ client: config.client });\n }\n\n public async dangerouslyClearAll(): Promise<void> {\n await this.db.deleteData({ tableName: TABLE_SCORERS });\n }\n\n public async getScoreById({ id }: { id: string }): Promise<ScoreRowData | null> {\n try {\n const data = await this.db.get<ScoreRowData>({\n tableName: TABLE_SCORERS,\n keys: { id },\n });\n\n if (!data) {\n return null;\n }\n\n return transformScoreRow(data as Record<string, unknown>);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'GET_SCORE_BY_ID', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n ...(id && { id }),\n },\n },\n error,\n );\n }\n }\n\n public async listScoresByScorerId({\n scorerId,\n entityId,\n entityType,\n source,\n pagination = { page: 0, perPage: 20 },\n filters,\n }: {\n scorerId: string;\n entityId?: string;\n entityType?: string;\n source?: ScoringSource;\n pagination?: StoragePagination;\n filters?: ScoreTenancyFilters;\n }): Promise<{\n scores: ScoreRowData[];\n pagination: PaginationInfo;\n }> {\n return this.fetchAndFilterScores(pagination, row => {\n if (row.scorerId !== scorerId) {\n return false;\n }\n if (entityId && row.entityId !== entityId) {\n return false;\n }\n if (entityType && row.entityType !== entityType) {\n return false;\n }\n if (source && row.source !== source) {\n return false;\n }\n if (!matchesTenancy(row, filters)) {\n return false;\n }\n return true;\n });\n }\n\n public async saveScore(score: SaveScorePayload): Promise<{ score: ScoreRowData }> {\n let validatedScore: SaveScorePayload;\n try {\n validatedScore = saveScorePayloadSchema.parse(score);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'SAVE_SCORE', 'VALIDATION_FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n details: {\n scorer: typeof score.scorer?.id === 'string' ? score.scorer.id : String(score.scorer?.id ?? 'unknown'),\n entityId: score.entityId ?? 'unknown',\n entityType: score.entityType ?? 'unknown',\n traceId: score.traceId ?? '',\n spanId: score.spanId ?? '',\n },\n },\n error,\n );\n }\n\n const now = new Date();\n const id = crypto.randomUUID();\n\n const scoreWithId = {\n ...validatedScore,\n id,\n createdAt: now,\n updatedAt: now,\n };\n\n const { key, processedRecord } = processRecord(TABLE_SCORERS, scoreWithId);\n try {\n await this.client.set(key, JSON.stringify(processedRecord));\n return { score: { ...validatedScore, id, createdAt: now, updatedAt: now } as ScoreRowData };\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'SAVE_SCORE', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { id },\n },\n error,\n );\n }\n }\n\n public async listScoresByRunId({\n runId,\n pagination = { page: 0, perPage: 20 },\n filters,\n }: {\n runId: string;\n pagination?: StoragePagination;\n filters?: ScoreTenancyFilters;\n }): Promise<{\n scores: ScoreRowData[];\n pagination: PaginationInfo;\n }> {\n return this.fetchAndFilterScores(pagination, row => row.runId === runId && matchesTenancy(row, filters));\n }\n\n public async listScoresByEntityId({\n entityId,\n entityType,\n pagination = { page: 0, perPage: 20 },\n filters,\n }: {\n entityId: string;\n entityType?: string;\n pagination?: StoragePagination;\n filters?: ScoreTenancyFilters;\n }): Promise<{\n scores: ScoreRowData[];\n pagination: PaginationInfo;\n }> {\n return this.fetchAndFilterScores(pagination, row => {\n if (row.entityId !== entityId) {\n return false;\n }\n if (entityType && row.entityType !== entityType) {\n return false;\n }\n if (!matchesTenancy(row, filters)) {\n return false;\n }\n return true;\n });\n }\n\n public async listScoresBySpan({\n traceId,\n spanId,\n pagination = { page: 0, perPage: 20 },\n filters,\n }: {\n traceId: string;\n spanId: string;\n pagination?: StoragePagination;\n filters?: ScoreTenancyFilters;\n }): Promise<{\n scores: ScoreRowData[];\n pagination: PaginationInfo;\n }> {\n return this.fetchAndFilterScores(\n pagination,\n row => row.traceId === traceId && row.spanId === spanId && matchesTenancy(row, filters),\n );\n }\n\n private async fetchAndFilterScores(\n pagination: StoragePagination,\n filterFn: (row: Record<string, unknown>) => boolean,\n ): Promise<{ scores: ScoreRowData[]; pagination: PaginationInfo }> {\n const { page, perPage: perPageInput } = pagination;\n const keys = await this.db.scanKeys(`${TABLE_SCORERS}:*`);\n\n if (keys.length === 0) {\n return {\n scores: [],\n pagination: { total: 0, page, perPage: perPageInput, hasMore: false },\n };\n }\n\n const results = await this.client.mGet(keys);\n\n const filtered = results\n .map((data): Record<string, unknown> | null => {\n if (!data) {\n return null;\n }\n try {\n return JSON.parse(data) as Record<string, unknown>;\n } catch {\n return null;\n }\n })\n .filter((row): row is Record<string, unknown> => !!row && typeof row === 'object' && filterFn(row));\n\n const total = filtered.length;\n const perPage = normalizePerPage(perPageInput, 100);\n const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);\n const end = perPageInput === false ? total : start + perPage;\n const scores = filtered.slice(start, end).map(row => transformScoreRow(row));\n\n return {\n scores,\n pagination: {\n total,\n page,\n perPage: perPageForResponse,\n hasMore: end < total,\n },\n };\n }\n}\n","import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';\nimport {\n createStorageErrorId,\n isRunFenceConflictError,\n normalizePerPage,\n resolveRunFence,\n TABLE_WORKFLOW_RUN_OWNERS,\n TABLE_WORKFLOW_SNAPSHOT,\n matchesExpectedWorkflowStatus,\n WorkflowsStorage,\n ensureDate,\n} from '@mastra/core/storage';\nimport type {\n ClaimRunOwnershipInput,\n ClaimRunOwnershipResult,\n RenewRunOwnershipInput,\n RenewRunOwnershipResult,\n RunFence,\n RunOwnershipRecord,\n StorageListWorkflowRunsInput,\n WorkflowRun,\n WorkflowRuns,\n UpdateWorkflowStateOptions,\n} from '@mastra/core/storage';\nimport type { StepResult, WorkflowRunState } from '@mastra/core/workflows';\n\nimport { RedisDB } from '../../db';\nimport type { RedisDomainConfig } from '../../db';\nimport type { RedisClient } from '../../types';\nimport {\n claimRunOwnership,\n getRunOwnership,\n releaseRunOwnership,\n renewRunOwnership,\n runClaimKey,\n writeBatch,\n} from '../run-fencing';\nimport type { RunFenceCheck } from '../run-fencing';\nimport { getKey, processRecord } from '../utils';\n\nfunction parseWorkflowRun(row: Record<string, unknown>): WorkflowRun {\n let parsedSnapshot: WorkflowRunState | string = row.snapshot as string;\n if (typeof parsedSnapshot === 'string') {\n try {\n parsedSnapshot = JSON.parse(row.snapshot as string) as WorkflowRunState;\n } catch (e) {\n console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);\n }\n }\n\n return {\n workflowName: row.workflow_name as string,\n runId: row.run_id as string,\n snapshot: parsedSnapshot,\n createdAt: ensureDate(row.createdAt as string | Date)!,\n updatedAt: ensureDate(row.updatedAt as string | Date)!,\n resourceId: row.resourceId as string | undefined,\n };\n}\n\ntype SnapshotRecord = {\n namespace: string;\n workflow_name: string;\n run_id: string;\n resourceId?: string;\n snapshot: WorkflowRunState;\n createdAt: string | Date;\n updatedAt: string | Date;\n};\n\nfunction escapeGlob(value: string): string {\n return value.replace(/[*?[\\]\\\\]/g, '\\\\$&');\n}\n\nfunction snapshotKey(namespace: string, workflowName: string, runId: string): string {\n return getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace, workflow_name: workflowName, run_id: runId });\n}\n\n/**\n * Earlier versions appended `:resourceId:<id>` to snapshot keys. Matches those keys for a run.\n */\nfunction legacySnapshotKeyPattern(namespace: string, workflowName: string, runId: string): string {\n return `${escapeGlob(snapshotKey(namespace, workflowName, runId))}:resourceId:*`;\n}\n\nexport class WorkflowsRedis extends WorkflowsStorage {\n private client: RedisClient;\n private db: RedisDB;\n\n constructor(config: RedisDomainConfig) {\n super();\n this.client = config.client;\n this.db = new RedisDB({ client: config.client });\n }\n\n /**\n * Reads a run's snapshot record by its canonical key, falling back to legacy\n * `:resourceId:`-suffixed keys only when the canonical key is missing.\n */\n private async getSnapshotRecord(\n namespace: string,\n workflowName: string,\n runId: string,\n ): Promise<SnapshotRecord | null> {\n const data = await this.client.get(snapshotKey(namespace, workflowName, runId));\n if (data) {\n return JSON.parse(data) as SnapshotRecord;\n }\n\n const legacyKeys = await this.db.scanKeys(legacySnapshotKeyPattern(namespace, workflowName, runId));\n if (legacyKeys.length === 0) {\n return null;\n }\n const legacyData = await this.client.get(legacyKeys[0]!);\n return legacyData ? (JSON.parse(legacyData) as SnapshotRecord) : null;\n }\n\n public supportsConcurrentUpdates(): boolean {\n return false;\n }\n\n public override supportsRunFencing(): boolean {\n return true;\n }\n\n public async dangerouslyClearAll(): Promise<void> {\n await this.db.deleteData({ tableName: TABLE_WORKFLOW_SNAPSHOT });\n await this.db.scanAndDelete(`${escapeGlob(runClaimKey(TABLE_WORKFLOW_RUN_OWNERS, ''))}*`);\n }\n\n #runFenceCheck(fence: RunFence | undefined, runId: string, operation: string): RunFenceCheck | undefined {\n const resolved = resolveRunFence(this, fence, runId);\n return resolved && { claims: TABLE_WORKFLOW_RUN_OWNERS, fence: resolved, operation };\n }\n\n #ownershipError(operation: string, runId: string, error: unknown): MastraError {\n return new MastraError(\n {\n id: createStorageErrorId('REDIS', operation, 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { runId },\n },\n error,\n );\n }\n\n // Each operation is one script over the run's ownership hash, so racing\n // claimers and renewals serialize on Redis and resolve to a single winner.\n public override async claimRunOwnership(args: ClaimRunOwnershipInput): Promise<ClaimRunOwnershipResult> {\n try {\n return await claimRunOwnership(this.client, args);\n } catch (error) {\n throw this.#ownershipError('CLAIM_RUN_OWNERSHIP', args.runId, error);\n }\n }\n\n public override async renewRunOwnership(args: RenewRunOwnershipInput): Promise<RenewRunOwnershipResult> {\n try {\n return await renewRunOwnership(this.client, args);\n } catch (error) {\n throw this.#ownershipError('RENEW_RUN_OWNERSHIP', args.runId, error);\n }\n }\n\n public override async releaseRunOwnership(fence: RunFence): Promise<boolean> {\n try {\n return await releaseRunOwnership(this.client, fence);\n } catch (error) {\n throw this.#ownershipError('RELEASE_RUN_OWNERSHIP', fence.runId, error);\n }\n }\n\n public override async getRunOwnership({ runId }: { runId: string }): Promise<RunOwnershipRecord | null> {\n try {\n return await getRunOwnership(this.client, runId);\n } catch (error) {\n throw this.#ownershipError('GET_RUN_OWNERSHIP', runId, error);\n }\n }\n\n public async updateWorkflowResults({\n workflowName,\n runId,\n stepId,\n result,\n requestContext,\n state,\n fence,\n }: {\n workflowName: string;\n runId: string;\n stepId: string;\n result: StepResult<unknown, unknown, unknown, unknown>;\n requestContext: Record<string, unknown>;\n state?: Record<string, unknown>;\n fence?: RunFence;\n }): Promise<Record<string, StepResult<unknown, unknown, unknown, unknown>>> {\n const resolvedFence = resolveRunFence(this, fence, runId);\n try {\n const existingRecord = await this.getSnapshotRecord('workflows', workflowName, runId);\n\n const existingSnapshot = existingRecord?.snapshot;\n let snapshot = existingSnapshot;\n\n if (!snapshot) {\n snapshot = {\n context: {},\n activePaths: [],\n timestamp: Date.now(),\n suspendedPaths: {},\n activeStepsPath: {},\n resumeLabels: {},\n serializedStepGraph: [],\n status: 'pending',\n value: {},\n waitingPaths: {},\n runId,\n requestContext: {},\n } as WorkflowRunState;\n }\n\n snapshot.context[stepId] = result;\n if (state !== undefined) {\n (snapshot.context as Record<string, unknown>).__state = state;\n }\n snapshot.requestContext = { ...snapshot.requestContext, ...requestContext };\n\n await this.persistWorkflowSnapshot({\n namespace: 'workflows',\n workflowName,\n runId,\n resourceId: existingRecord?.resourceId,\n snapshot,\n createdAt: existingRecord?.createdAt ? ensureDate(existingRecord.createdAt) : undefined,\n fence: resolvedFence,\n });\n\n return snapshot.context;\n } catch (error) {\n if (error instanceof MastraError) {\n throw error;\n }\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'UPDATE_WORKFLOW_RESULTS', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { workflowName, runId, stepId },\n },\n error,\n );\n }\n }\n\n public async updateWorkflowState({\n workflowName,\n runId,\n opts,\n fence,\n }: {\n workflowName: string;\n runId: string;\n opts: UpdateWorkflowStateOptions;\n fence?: RunFence;\n }): Promise<WorkflowRunState | undefined> {\n const resolvedFence = resolveRunFence(this, fence, runId);\n try {\n const existingRecord = await this.getSnapshotRecord('workflows', workflowName, runId);\n\n const existingSnapshot = existingRecord?.snapshot;\n\n if (!existingSnapshot || !existingSnapshot.context) {\n return undefined;\n }\n\n // Best-effort only: this store reports `supportsConcurrentUpdates() === false`, so the\n // read and the write are not a single critical section.\n const { expectedStatus, ...state } = opts;\n if (!matchesExpectedWorkflowStatus(existingSnapshot.status, expectedStatus)) {\n return undefined;\n }\n\n const updatedSnapshot = { ...existingSnapshot, ...state };\n\n await this.persistWorkflowSnapshot({\n namespace: 'workflows',\n workflowName,\n runId,\n resourceId: existingRecord.resourceId,\n snapshot: updatedSnapshot,\n createdAt: existingRecord?.createdAt ? ensureDate(existingRecord.createdAt) : undefined,\n fence: resolvedFence,\n });\n\n return updatedSnapshot;\n } catch (error) {\n if (error instanceof MastraError) {\n throw error;\n }\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'UPDATE_WORKFLOW_STATE', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { workflowName, runId },\n },\n error,\n );\n }\n }\n\n public async persistWorkflowSnapshot(params: {\n namespace?: string;\n workflowName: string;\n runId: string;\n resourceId?: string;\n snapshot: WorkflowRunState;\n createdAt?: Date;\n updatedAt?: Date;\n fence?: RunFence;\n }): Promise<void> {\n const { namespace = 'workflows', workflowName, runId, resourceId, snapshot, createdAt, updatedAt } = params;\n const check = this.#runFenceCheck(params.fence, runId, 'persistWorkflowSnapshot');\n try {\n let finalCreatedAt = createdAt;\n let finalResourceId = resourceId;\n if (!finalCreatedAt || !finalResourceId) {\n // No SCAN here (it would run on every new run); a legacy key is only reachable when resourceId is known.\n const canonicalKey = snapshotKey(namespace, workflowName, runId);\n const existingData =\n (await this.client.get(canonicalKey)) ??\n (resourceId ? await this.client.get(`${canonicalKey}:resourceId:${resourceId}`) : null);\n const existing = existingData ? (JSON.parse(existingData) as SnapshotRecord) : null;\n finalCreatedAt ??= existing?.createdAt ? ensureDate(existing.createdAt) : new Date();\n finalResourceId ??= existing?.resourceId;\n }\n\n const { key, processedRecord } = processRecord(TABLE_WORKFLOW_SNAPSHOT, {\n namespace,\n workflow_name: workflowName,\n run_id: runId,\n resourceId: finalResourceId,\n snapshot,\n createdAt: finalCreatedAt,\n updatedAt: updatedAt ?? new Date(),\n });\n const batch = writeBatch(this.client, check);\n batch.set(key, JSON.stringify(processedRecord));\n await batch.exec();\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'PERSIST_WORKFLOW_SNAPSHOT', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n namespace,\n workflowName,\n runId,\n },\n },\n error,\n );\n }\n }\n\n public async loadWorkflowSnapshot(params: {\n namespace: string;\n workflowName: string;\n runId: string;\n }): Promise<WorkflowRunState | null> {\n const { namespace = 'workflows', workflowName, runId } = params;\n try {\n const record = await this.getSnapshotRecord(namespace, workflowName, runId);\n return record?.snapshot ?? null;\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LOAD_WORKFLOW_SNAPSHOT', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n namespace,\n workflowName,\n runId,\n },\n },\n error,\n );\n }\n }\n\n public async getWorkflowRunById({\n runId,\n workflowName,\n }: {\n runId: string;\n workflowName?: string;\n }): Promise<WorkflowRun | null> {\n try {\n const workflowNamePattern = workflowName ? escapeGlob(workflowName) : '*';\n const canonicalPattern = `${escapeGlob(getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: 'workflows' }))}:workflow_name:${workflowNamePattern}:run_id:${escapeGlob(runId)}`;\n const keys = [\n ...(await this.db.scanKeys(canonicalPattern)),\n ...(await this.db.scanKeys(`${canonicalPattern}:resourceId:*`)),\n ];\n\n if (keys.length === 0) {\n return null;\n }\n\n const results = await this.client.mGet(keys);\n const workflows = results\n .filter((data): data is string => data !== null)\n .map(\n data =>\n JSON.parse(data) as {\n workflow_name: string;\n run_id: string;\n snapshot: WorkflowRunState | string;\n createdAt: string | Date;\n updatedAt: string | Date;\n resourceId: string;\n },\n );\n\n const data = workflows.find(workflow => {\n if (!workflow) {\n return false;\n }\n\n const runIdMatch = workflow.run_id === runId;\n\n if (workflowName) {\n return runIdMatch && workflow.workflow_name === workflowName;\n }\n\n return runIdMatch;\n });\n\n if (!data) {\n return null;\n }\n\n return parseWorkflowRun(data as Record<string, unknown>);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'GET_WORKFLOW_RUN_BY_ID', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n namespace: 'workflows',\n runId,\n workflowName: workflowName || '',\n },\n },\n error,\n );\n }\n }\n\n public async deleteWorkflowRunById({\n runId,\n workflowName,\n fence,\n }: {\n runId: string;\n workflowName: string;\n fence?: RunFence;\n }): Promise<void> {\n const check = this.#runFenceCheck(fence, runId, 'deleteWorkflowRunById');\n try {\n const legacyKeys = await this.db.scanKeys(legacySnapshotKeyPattern('workflows', workflowName, runId));\n const batch = writeBatch(this.client, check);\n for (const key of [snapshotKey('workflows', workflowName, runId), ...legacyKeys]) batch.del(key);\n await batch.exec();\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'DELETE_WORKFLOW_RUN_BY_ID', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n namespace: 'workflows',\n runId,\n workflowName,\n },\n },\n error,\n );\n }\n }\n\n public async listWorkflowRuns({\n workflowName,\n fromDate,\n toDate,\n perPage,\n page,\n resourceId,\n status,\n }: StorageListWorkflowRunsInput = {}): Promise<WorkflowRuns> {\n try {\n if (page !== undefined && page < 0) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_WORKFLOW_RUNS', 'INVALID_PAGE'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n details: { page },\n },\n new Error('page must be >= 0'),\n );\n }\n\n const normalizedFrom = fromDate ? ensureDate(fromDate) : undefined;\n const normalizedTo = toDate ? ensureDate(toDate) : undefined;\n\n const pattern = workflowName\n ? `${escapeGlob(getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: 'workflows', workflow_name: workflowName }))}:*`\n : `${getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: 'workflows' })}:*`;\n const keys = await this.db.scanKeys(pattern);\n\n if (keys.length === 0) {\n return { runs: [], total: 0 };\n }\n\n const results = await this.client.mGet(keys);\n\n // A run can exist under both its canonical key and a legacy `:resourceId:` key; the canonical one wins.\n const recordsByRun = new Map<string, { record: Record<string, unknown>; legacy: boolean }>();\n results.forEach((data, index) => {\n if (data === null) return;\n const record = JSON.parse(data) as Record<string, unknown>;\n if (record === null || typeof record !== 'object' || !('workflow_name' in record)) return;\n const legacy =\n keys[index] !==\n snapshotKey(\n (record.namespace as string | undefined) ?? 'workflows',\n record.workflow_name as string,\n record.run_id as string,\n );\n const runKey = `${record.workflow_name}\\u0000${record.run_id}`;\n const current = recordsByRun.get(runKey);\n if (!current || (current.legacy && !legacy)) {\n recordsByRun.set(runKey, { record, legacy });\n }\n });\n\n let runs = [...recordsByRun.values()]\n .map(({ record }) => record)\n .filter(record => !workflowName || record.workflow_name === workflowName)\n .filter(record => !resourceId || record.resourceId === resourceId)\n .map(w => parseWorkflowRun(w))\n .filter(w => {\n if (normalizedFrom && w.createdAt < normalizedFrom) {\n return false;\n }\n if (normalizedTo && w.createdAt > normalizedTo) {\n return false;\n }\n if (status) {\n let snapshot = w.snapshot;\n if (typeof snapshot === 'string') {\n try {\n snapshot = JSON.parse(snapshot) as WorkflowRunState;\n } catch (e) {\n console.warn(`Failed to parse snapshot for workflow ${w.workflowName}: ${e}`);\n return false;\n }\n }\n return snapshot.status === status;\n }\n return true;\n })\n .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());\n\n const total = runs.length;\n\n if (typeof perPage === 'number' && typeof page === 'number') {\n const normalizedPerPage = normalizePerPage(perPage, Number.MAX_SAFE_INTEGER);\n const offset = page * normalizedPerPage;\n runs = runs.slice(offset, offset + normalizedPerPage);\n }\n\n return { runs, total };\n } catch (error) {\n if (error instanceof MastraError) {\n throw error;\n }\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_WORKFLOW_RUNS', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n namespace: 'workflows',\n workflowName: workflowName || '',\n resourceId: resourceId || '',\n },\n },\n error,\n );\n }\n }\n}\n","import type { RedisClient, RedisConfig } from './types';\n\nexport function isClientConfig(config: RedisConfig): config is RedisConfig & { client: RedisClient } {\n return 'client' in config;\n}\n\nexport function isConnectionStringConfig(config: RedisConfig): config is RedisConfig & { connectionString: string } {\n return 'connectionString' in config;\n}\n","import { MastraStorage } from '@mastra/core/storage';\nimport type { StorageDomains } from '@mastra/core/storage';\nimport { createClient } from 'redis';\n\nimport { StoreMemoryRedis } from './domains/memory';\nimport { ScoresRedis } from './domains/scores';\nimport { WorkflowsRedis } from './domains/workflows';\nimport type { RedisClient, RedisConfig } from './types';\nimport { isClientConfig, isConnectionStringConfig } from './utils';\n\n/**\n * Redis storage adapter for Mastra.\n *\n * Provides storage functionality for direct Redis connections using the official redis package.\n *\n * Access domain-specific storage via `getStore()`:\n *\n * @example\n * ```typescript\n * // Using connection string\n * const storage = new RedisStore({\n * id: 'my-store',\n * connectionString: 'redis://localhost:6379',\n * });\n *\n * // Using host/port\n * const storage = new RedisStore({\n * id: 'my-store',\n * host: 'localhost',\n * port: 6379,\n * password: 'secret',\n * });\n *\n * // Access memory domain\n * const memory = await storage.getStore('memory');\n * await memory?.saveThread({ thread });\n *\n * // Access workflows domain\n * const workflows = await storage.getStore('workflows');\n * await workflows?.persistWorkflowSnapshot({ workflowName, runId, snapshot });\n * ```\n *\n * @example\n * ```typescript\n * // Using a pre-configured client for advanced features\n * import { createClient } from 'redis';\n *\n * const client = createClient({\n * url: 'redis://localhost:6379',\n * socket: {\n * reconnectStrategy: (retries) => Math.min(retries * 50, 2000),\n * },\n * });\n * await client.connect();\n *\n * const storage = new RedisStore({\n * id: 'my-store',\n * client,\n * });\n * ```\n */\nexport class RedisStore extends MastraStorage {\n private client: RedisClient;\n private shouldManageConnection: boolean;\n public stores: StorageDomains;\n\n constructor(config: RedisConfig) {\n super({ id: config.id, name: 'Redis', disableInit: config.disableInit });\n\n const { client, shouldManageConnection } = this.createClient(config);\n this.client = client;\n this.shouldManageConnection = shouldManageConnection;\n\n this.stores = {\n scores: new ScoresRedis({ client: this.client }),\n workflows: new WorkflowsRedis({ client: this.client }),\n memory: new StoreMemoryRedis({ client: this.client }),\n };\n }\n\n public override async init(): Promise<void> {\n if (this.shouldManageConnection && !this.client.isOpen) {\n await this.client.connect();\n }\n await super.init();\n }\n\n public getClient(): RedisClient {\n return this.client;\n }\n\n public async close(): Promise<void> {\n if (this.shouldManageConnection && this.client.isOpen) {\n await this.client.quit();\n }\n }\n\n private createClient(config: RedisConfig): { client: RedisClient; shouldManageConnection: boolean } {\n if (isClientConfig(config)) {\n return { client: config.client, shouldManageConnection: false };\n }\n\n if (isConnectionStringConfig(config)) {\n if (!config.connectionString?.trim()) {\n throw new Error('RedisStore: connectionString is required and cannot be empty.');\n }\n return {\n client: createClient({ url: config.connectionString }) as RedisClient,\n shouldManageConnection: true,\n };\n }\n\n if (!config.host?.trim()) {\n throw new Error('RedisStore: host is required and cannot be empty.');\n }\n\n const url = this.createClientUrl({\n ...config,\n db: config.db ?? 0,\n port: config.port ?? 6379,\n });\n\n return {\n client: createClient({ url }) as RedisClient,\n shouldManageConnection: true,\n };\n }\n\n private createClientUrl(config: { host: string; password?: string; port: number; db: number }): string {\n const encodedPassword = config.password ? encodeURIComponent(config.password) : null;\n\n if (config.password) {\n return `redis://:${encodedPassword}@${config.host}:${config.port || 6379}/${config.db || 0}`;\n }\n\n return `redis://${config.host}:${config.port || 6379}/${config.db || 0}`;\n }\n}\n","import { MastraServerCache } from '@mastra/core/cache';\n\nexport interface RedisClient {\n get(key: string): Promise<unknown>;\n set(key: string, value: unknown, ...args: unknown[]): Promise<unknown>;\n llen(key: string): Promise<number>;\n rpush(key: string, ...values: unknown[]): Promise<number>;\n lrange(key: string, start: number, stop: number): Promise<unknown[]>;\n del(...keys: string[]): Promise<number>;\n expire(key: string, seconds: number): Promise<number | boolean>;\n scan(cursor: string | number, ...args: unknown[]): Promise<[string | number, string[]]>;\n incr(key: string): Promise<number>;\n /**\n * Server-side Lua scripting. Optional: clients without it fall back to the\n * two-round-trip `increment` + `listPush` path for `listPushIndexed`.\n */\n eval?(...args: unknown[]): Promise<unknown>;\n}\n\nexport interface RedisServerCacheOptions {\n keyPrefix?: string;\n /**\n * Lifetime of cached values in seconds, refreshed on every write. Defaults to\n * 300. `0` means no expiry.\n *\n * A cache list and its index counter always share this TTL: if the counter\n * outlived the list, index allocation would restart at 0 while subscribers\n * still expect the previous numbering, and every new frame would be dropped\n * as a duplicate.\n */\n ttlSeconds?: number;\n /**\n * Run a Lua script with the given KEYS and ARGV. Defaults to the ioredis\n * calling convention (`eval(script, numKeys, ...keys, ...args)`); use\n * `nodeRedisPreset` / `upstashPreset` for those clients.\n */\n evalScript?: (client: RedisClient, script: string, keys: string[], args: string[]) => Promise<unknown>;\n setWithExpiry?: (client: RedisClient, key: string, value: unknown, seconds: number) => Promise<unknown>;\n scanKeys?: (\n client: RedisClient,\n cursor: string | number,\n pattern: string,\n count: number,\n ) => Promise<[string | number, string[]]>;\n getListLength?: (client: RedisClient, key: string) => Promise<number>;\n pushToList?: (client: RedisClient, key: string, value: unknown) => Promise<number>;\n getListRange?: (client: RedisClient, key: string, start: number, stop: number) => Promise<unknown[]>;\n}\n\nconst defaultSetWithExpiry = (client: RedisClient, key: string, value: unknown, seconds: number): Promise<unknown> => {\n return client.set(key, value, 'EX', seconds);\n};\n\nconst defaultScanKeys = (\n client: RedisClient,\n cursor: string | number,\n pattern: string,\n count: number,\n): Promise<[string | number, string[]]> => {\n return client.scan(cursor, 'MATCH', pattern, 'COUNT', count);\n};\n\nconst defaultGetListLength = (client: RedisClient, key: string): Promise<number> => {\n return client.llen(key);\n};\n\nconst defaultPushToList = (client: RedisClient, key: string, value: unknown): Promise<number> => {\n return client.rpush(key, value);\n};\n\nconst defaultGetListRange = (client: RedisClient, key: string, start: number, stop: number): Promise<unknown[]> => {\n return client.lrange(key, start, stop);\n};\n\nconst defaultEvalScript = (client: RedisClient, script: string, keys: string[], args: string[]): Promise<unknown> => {\n return client.eval!(script, keys.length, ...keys, ...args);\n};\n\n/**\n * Atomic \"allocate index + append + refresh TTL\" used by `listPushIndexed`.\n *\n * KEYS[1] = list key, KEYS[2] = counter key\n * ARGV[1] = JSON-serialized object WITHOUT an `index` property\n * ARGV[2] = TTL in seconds (0 = no expiry), applied to BOTH keys\n\n *\n * The index is spliced into the JSON text as the first property rather than\n * decoded/re-encoded with cjson, which would silently alter large integers\n * and sparse arrays. `JSON.stringify` of an object always starts with `{`,\n * so `{...}` → `{\"index\":N,...}` and `{}` → `{\"index\":N}`.\n */\nexport const LIST_PUSH_INDEXED_SCRIPT = `\nlocal i = redis.call('INCR', KEYS[2]) - 1\nlocal body = ARGV[1]\nlocal doc\nif #body <= 2 then\n doc = '{\"index\":' .. i .. '}'\nelse\n doc = '{\"index\":' .. i .. ',' .. string.sub(body, 2)\nend\nredis.call('RPUSH', KEYS[1], doc)\nlocal ttl = tonumber(ARGV[2])\nif ttl > 0 then\n redis.call('EXPIRE', KEYS[1], ttl)\n redis.call('EXPIRE', KEYS[2], ttl)\nend\nreturn i\n`.trim();\n\nexport class RedisServerCache extends MastraServerCache {\n private client: RedisClient;\n private keyPrefix: string;\n private ttlSeconds: number;\n private setWithExpiry: (client: RedisClient, key: string, value: unknown, seconds: number) => Promise<unknown>;\n private scanKeys: (\n client: RedisClient,\n cursor: string | number,\n pattern: string,\n count: number,\n ) => Promise<[string | number, string[]]>;\n private getListLength: (client: RedisClient, key: string) => Promise<number>;\n private pushToList: (client: RedisClient, key: string, value: unknown) => Promise<number>;\n private getListRange: (client: RedisClient, key: string, start: number, stop: number) => Promise<unknown[]>;\n private evalScript: (client: RedisClient, script: string, keys: string[], args: string[]) => Promise<unknown>;\n /** Set once scripting is known to be unusable (no `eval`, or Cluster CROSSSLOT). */\n private scriptingDisabled: boolean;\n\n constructor(config: { client: RedisClient }, options: RedisServerCacheOptions = {}) {\n super({ name: 'RedisServerCache' });\n\n this.client = config.client;\n this.keyPrefix = options.keyPrefix ?? 'mastra:cache:';\n this.ttlSeconds = options.ttlSeconds ?? 300;\n this.setWithExpiry = options.setWithExpiry ?? defaultSetWithExpiry;\n this.scanKeys = options.scanKeys ?? defaultScanKeys;\n this.getListLength = options.getListLength ?? defaultGetListLength;\n this.pushToList = options.pushToList ?? defaultPushToList;\n this.getListRange = options.getListRange ?? defaultGetListRange;\n this.evalScript = options.evalScript ?? defaultEvalScript;\n this.scriptingDisabled = typeof this.client.eval !== 'function';\n }\n\n private getKey(key: string): string {\n return `${this.keyPrefix}${key}`;\n }\n\n private serialize(value: unknown): string {\n return JSON.stringify(value);\n }\n\n private deserialize(value: unknown): unknown {\n if (typeof value === 'string') {\n try {\n return JSON.parse(value);\n } catch {\n return value;\n }\n }\n return value;\n }\n\n async get(key: string): Promise<unknown> {\n const fullKey = this.getKey(key);\n const value = await this.client.get(fullKey);\n if (value === null) {\n return null;\n }\n return this.deserialize(value);\n }\n\n async set(key: string, value: unknown, ttlMs?: number): Promise<void> {\n const fullKey = this.getKey(key);\n const serialized = this.serialize(value);\n const overrideSeconds = ttlMs !== undefined ? Math.max(1, Math.ceil(ttlMs / 1000)) : undefined;\n const effectiveSeconds = overrideSeconds ?? this.ttlSeconds;\n if (effectiveSeconds > 0) {\n await this.setWithExpiry(this.client, fullKey, serialized, effectiveSeconds);\n } else {\n await this.client.set(fullKey, serialized);\n }\n }\n\n async listLength(key: string): Promise<number> {\n const fullKey = this.getKey(key);\n return this.getListLength(this.client, fullKey);\n }\n\n async listPush(key: string, value: unknown): Promise<void> {\n const fullKey = this.getKey(key);\n const serialized = this.serialize(value);\n await this.pushToList(this.client, fullKey, serialized);\n\n if (this.ttlSeconds > 0) {\n await this.client.expire(fullKey, this.ttlSeconds);\n }\n }\n\n async listFromTo(key: string, from: number, to: number = -1): Promise<unknown[]> {\n const fullKey = this.getKey(key);\n const values = await this.getListRange(this.client, fullKey, from, to);\n return values.map(v => this.deserialize(v));\n }\n\n async delete(key: string): Promise<void> {\n const fullKey = this.getKey(key);\n await this.client.del(fullKey);\n }\n\n async clear(): Promise<void> {\n const pattern = `${this.keyPrefix}*`;\n let cursor: string | number = '0';\n\n do {\n const [nextCursor, keys] = await this.scanKeys(this.client, cursor, pattern, 100);\n\n if (keys.length > 0) {\n await this.client.del(...keys);\n }\n\n cursor = nextCursor;\n } while (cursor !== '0' && cursor !== 0);\n }\n\n async increment(key: string): Promise<number> {\n const fullKey = this.getKey(key);\n const value = await this.client.incr(fullKey);\n\n if (this.ttlSeconds > 0) {\n await this.client.expire(fullKey, this.ttlSeconds);\n }\n\n return value;\n }\n\n /**\n * Single round-trip index allocation + list append + TTL refresh via Lua.\n * This is the durable stream per-chunk hot path (issue #22477).\n */\n async listPushIndexed(\n listKey: string,\n counterKey: string,\n value: Record<string, unknown>,\n ttlMs?: number,\n ): Promise<number> {\n // A cache without expiry stays that way; otherwise `ttlMs` can only raise\n // the default.\n const ttlSeconds = this.ttlSeconds > 0 ? Math.max(this.ttlSeconds, Math.ceil((ttlMs ?? 0) / 1000)) : 0;\n if (this.scriptingDisabled) {\n return this.composedPushIndexed(listKey, counterKey, value, ttlSeconds);\n }\n\n const { index: _ignored, ...body } = value;\n try {\n const result = await this.evalScript(\n this.client,\n LIST_PUSH_INDEXED_SCRIPT,\n [this.getKey(listKey), this.getKey(counterKey)],\n [this.serialize(body), String(ttlSeconds)],\n );\n return Number(result);\n } catch (error) {\n // Redis Cluster rejects multi-key scripts whose keys hash to different\n // slots. The list and counter keys are not hash-tagged (changing that\n // would rename existing keys), so degrade to the composed path instead\n // of losing replay caching entirely.\n if (error instanceof Error && error.message.includes('CROSSSLOT')) {\n this.scriptingDisabled = true;\n this.logger.warn(\n '[RedisServerCache] listPushIndexed script rejected with CROSSSLOT; falling back to increment + listPush',\n );\n return this.composedPushIndexed(listKey, counterKey, value, ttlSeconds);\n }\n throw error;\n }\n }\n\n /**\n * `INCR` then `RPUSH`, each followed by the same `EXPIRE` the script applies.\n * Not atomic: concurrent writers can interleave, so a list position may not\n * match its index (Redis Cluster atomicity is tracked in COR-1491).\n */\n private async composedPushIndexed(\n listKey: string,\n counterKey: string,\n value: Record<string, unknown>,\n ttlSeconds: number,\n ): Promise<number> {\n const fullCounterKey = this.getKey(counterKey);\n const index = (await this.client.incr(fullCounterKey)) - 1;\n if (ttlSeconds > 0) await this.client.expire(fullCounterKey, ttlSeconds);\n\n const fullListKey = this.getKey(listKey);\n await this.pushToList(this.client, fullListKey, this.serialize({ ...value, index }));\n if (ttlSeconds > 0) await this.client.expire(fullListKey, ttlSeconds);\n return index;\n }\n}\n\nexport const upstashPreset: Pick<RedisServerCacheOptions, 'setWithExpiry' | 'scanKeys' | 'evalScript'> = {\n setWithExpiry: (client, key, value, seconds) => client.set(key, value, { ex: seconds } as any),\n scanKeys: (client, cursor, pattern, count) =>\n client.scan(cursor, { match: pattern, count } as any) as Promise<[string | number, string[]]>,\n evalScript: (client, script, keys, args) => client.eval!(script, keys, args),\n};\n\n// node-redis v4+ exposes Redis multi-word commands as camelCase only\n// (lLen / rPush / lRange), not as lowercase aliases. The defaults in this\n// module use ioredis-style lowercase, so node-redis users need adapters that\n// forward to the camelCase methods. Single-word commands (set, scan, del,\n// expire, incr, get) work in lowercase under node-redis and don't need\n// adapters; the existing setWithExpiry / scanKeys adapters only exist to\n// reshape arguments, not to alias method names.\nexport const nodeRedisPreset: Pick<\n RedisServerCacheOptions,\n 'setWithExpiry' | 'scanKeys' | 'getListLength' | 'pushToList' | 'getListRange' | 'evalScript'\n> = {\n setWithExpiry: (client, key, value, seconds) => client.set(key, value, { EX: seconds } as any),\n scanKeys: (client, cursor, pattern, count) =>\n client.scan(cursor, { MATCH: pattern, COUNT: count } as any) as Promise<[string | number, string[]]>,\n getListLength: (client, key) => (client as any).lLen(key),\n pushToList: (client, key, value) => (client as any).rPush(key, value),\n getListRange: (client, key, start, stop) => (client as any).lRange(key, start, stop),\n evalScript: (client, script, keys, args) => client.eval!(script, { keys, arguments: args }),\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAeA,SAAgB,OAAO,WAAwB,MAAuC;CAWpF,OAAO,GAAG,UAAU,GAVH,OAAO,QAAQ,IAAI,CAAC,CAClC,QAAQ,CAAC,GAAG,WAAW,UAAU,K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AG;GAAO;EAAM,CAAC,CAAC;EACnF,IAAI,aAAa,GAAG,MAAM,KAAK,OAAO,OAAO,aAAa,UAAU;EACpE,OAAO;CACT;AACF;AAEA,MAAa,gBAA4F;CACvG,gBAAgB,QAAQ,KAAK,OAAO,YAAY,OAAO,IAAI,KAAK,OAAO,EAAE,IAAI,QAAQ,CAAQ;CAC7F,WAAW,QAAQ,QAAQ,SAAS,UAClC,OAAO,KAAK,QAAQ;EAAE,OAAO;EAAS;CAAM,CAAQ;CACtD,aAAa,QAAQ,QAAQ,MAAM,SAAS,OAAO,KAAM,QAAQ,MAAM,IAAI;AAC7E;AASA,MAAa,kBAGT;CACF,gBAAgB,QAAQ,KAAK,OAAO,YAAY,OAAO,IAAI,KAAK,OAAO,EAAE,IAAI,QAAQ,CAAQ;CAC7F,WAAW,QAAQ,QAAQ,SAAS,UAClC,OAAO,KAAK,QAAQ;EAAE,OAAO;EAAS,OAAO;CAAM,CAAQ;CAC7D,gBAAgB,QAAQ,QAAS,OAAe,KAAK,GAAG;CACxD,aAAa,QAAQ,KAAK,UAAW,OAAe,MAAM,KAAK,KAAK;CACpE,eAAe,QAAQ,KAAK,OAAO,SAAU,OAAe,OAAO,KAAK,OAAO,IAAI;CACnF,aAAa,QAAQ,QAAQ,MAAM,SAAS,OAAO,KAAM,QAAQ;EAAE;EAAM,WAAW;CAAK,CAAC;AAC5F"}
1
+ {"version":3,"file":"index.cjs","names":["TABLE_MESSAGES","TABLE_WORKFLOW_SNAPSHOT","serializeDate","MastraError","createStorageErrorId","ErrorDomain","ErrorCategory","TABLE_WORKFLOW_RUN_OWNERS","TABLE_MEMORY_RUN_FENCES","RunFenceConflictError","MemoryStorage","MastraError","createStorageErrorId","ErrorDomain","ErrorCategory","resolveRunFence","TABLE_MEMORY_RUN_FENCES","TABLE_THREADS","TABLE_MESSAGES","TABLE_RESOURCES","ensureDate","normalizePerPage","calculatePagination","jsonValueEquals","isRunFenceConflictError","MessageList","validateStorageMetadataFilter","filterByDateRange","storageMessageMatchesMetadataFilter","ScoresStorage","TABLE_SCORERS","transformScoreRow","MastraError","createStorageErrorId","ErrorDomain","ErrorCategory","saveScorePayloadSchema","normalizePerPage","calculatePagination","ensureDate","TABLE_WORKFLOW_SNAPSHOT","WorkflowsStorage","TABLE_WORKFLOW_RUN_OWNERS","resolveRunFence","MastraError","createStorageErrorId","ErrorDomain","ErrorCategory","matchesExpectedWorkflowStatus","isRunFenceConflictError","normalizePerPage","MastraStorage","createClient","MastraServerCache"],"sources":["../src/storage/domains/utils.ts","../src/storage/db/index.ts","../src/storage/domains/run-fencing.ts","../src/storage/domains/memory/index.ts","../src/storage/domains/scores/index.ts","../src/storage/domains/workflows/index.ts","../src/storage/utils.ts","../src/storage/store.ts","../src/cache.ts"],"sourcesContent":["import { serializeDate, TABLE_MESSAGES, TABLE_WORKFLOW_SNAPSHOT } from '@mastra/core/storage';\nimport type { TABLE_NAMES } from '@mastra/core/storage';\n\n/**\n * Generate a Redis key from table name and key parts.\n *\n * @example\n * ```typescript\n * getKey('mastra_threads', { id: 'thread-123' });\n * // Returns: 'mastra_threads:id:thread-123'\n *\n * getKey('mastra_messages', { threadId: 'thread-123', id: 'msg-456' });\n * // Returns: 'mastra_messages:threadId:thread-123:id:msg-456'\n * ```\n */\nexport function getKey(tableName: TABLE_NAMES, keys: Record<string, unknown>): string {\n const keyParts = Object.entries(keys)\n .filter(([_, value]) => value !== undefined)\n .map(([key, value]) => {\n if (value && typeof value === 'object') {\n return `${key}:${JSON.stringify(value)}`;\n }\n\n return `${key}:${value}`;\n });\n\n return `${tableName}:${keyParts.join(':')}`;\n}\n\n/**\n * Process a record for storage, generating the appropriate key and serializing dates.\n */\nexport function processRecord(tableName: TABLE_NAMES, record: Record<string, unknown>) {\n let key: string;\n\n if (tableName === TABLE_MESSAGES) {\n key = getKey(tableName, { threadId: record.threadId, id: record.id });\n } else if (tableName === TABLE_WORKFLOW_SNAPSHOT) {\n key = getKey(tableName, {\n namespace: record.namespace || 'workflows',\n workflow_name: record.workflow_name,\n run_id: record.run_id,\n });\n } else {\n key = getKey(tableName, { id: record.id });\n }\n\n const processedRecord = {\n ...record,\n createdAt: serializeDate(record.createdAt as Date | string | undefined),\n updatedAt: serializeDate(record.updatedAt as Date | string | undefined),\n };\n\n return { key, processedRecord };\n}\n","import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';\nimport { createStorageErrorId } from '@mastra/core/storage';\nimport type { TABLE_NAMES } from '@mastra/core/storage';\n\nimport { getKey, processRecord } from '../domains/utils';\nimport type { RedisClient } from '../types';\n\nexport class RedisDB {\n private client: RedisClient;\n\n constructor({ client }: { client: RedisClient }) {\n this.client = client;\n }\n\n getClient(): RedisClient {\n return this.client;\n }\n\n async insert({ tableName, record }: { tableName: TABLE_NAMES; record: Record<string, unknown> }): Promise<void> {\n const { key, processedRecord } = processRecord(tableName, record);\n\n try {\n await this.client.set(key, JSON.stringify(processedRecord));\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'INSERT', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n tableName,\n },\n },\n error,\n );\n }\n }\n\n async get<R>({ tableName, keys }: { tableName: TABLE_NAMES; keys: Record<string, string> }): Promise<R | null> {\n const key = getKey(tableName, keys);\n try {\n const data = await this.client.get(key);\n if (!data) {\n return null;\n }\n return JSON.parse(data) as R;\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LOAD', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n tableName,\n },\n },\n error,\n );\n }\n }\n\n async scanAndDelete(pattern: string, batchSize = 10000): Promise<number> {\n let cursor = '0';\n let totalDeleted = 0;\n\n do {\n const result = await this.client.scan(cursor, { MATCH: pattern, COUNT: batchSize });\n\n if (result.keys.length > 0) {\n await this.client.del(result.keys);\n totalDeleted += result.keys.length;\n }\n\n cursor = result.cursor;\n } while (cursor !== '0');\n\n return totalDeleted;\n }\n\n async scanKeys(pattern: string, batchSize = 10000): Promise<string[]> {\n let cursor = '0';\n const keys: string[] = [];\n\n do {\n const result = await this.client.scan(cursor, { MATCH: pattern, COUNT: batchSize });\n\n keys.push(...result.keys);\n cursor = result.cursor;\n } while (cursor !== '0');\n\n return keys;\n }\n\n async deleteData({ tableName }: { tableName: TABLE_NAMES }): Promise<void> {\n const pattern = `${tableName}:*`;\n try {\n await this.scanAndDelete(pattern);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'CLEAR_TABLE', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n tableName,\n },\n },\n error,\n );\n }\n }\n}\n\nexport interface RedisDomainConfig {\n client: RedisClient;\n}\n","import { RunFenceConflictError, TABLE_MEMORY_RUN_FENCES, TABLE_WORKFLOW_RUN_OWNERS } from '@mastra/core/storage';\nimport type {\n ClaimRunOwnershipInput,\n ClaimRunOwnershipResult,\n RenewRunOwnershipInput,\n RenewRunOwnershipResult,\n RunFence,\n RunOwnershipRecord,\n} from '@mastra/core/storage';\n\nimport type { RedisClient } from '../types';\n\n/** Table holding a run's current claim: its ownership hash (workflows) or raised fence (memory). */\nexport type RunClaimTable = typeof TABLE_WORKFLOW_RUN_OWNERS | typeof TABLE_MEMORY_RUN_FENCES;\n\n/** A write's fence, and the table holding the run's current claim to check it against. */\nexport interface RunFenceCheck {\n claims: RunClaimTable;\n fence: RunFence;\n operation: string;\n}\n\n/** The subset of a MULTI transaction the domains write through. */\nexport interface WriteBatch {\n set(key: string, value: string): unknown;\n del(key: string): unknown;\n zAdd(key: string, member: { score: number; value: string }): unknown;\n zRem(key: string, member: string): unknown;\n exec(): Promise<unknown>;\n}\n\n/**\n * A claim expires after this long without a claim, renewal or release, and a\n * fence this long after it was retired. A running execution renews every few\n * seconds, and a raised fence never expires. Expiring is safe: a later claim's\n * generation comes from the clock, so it still outranks every fence the expired\n * record issued, and a write checked against a missing record is rejected.\n */\nexport const RUN_CLAIM_RETENTION_MS = 7 * 24 * 60 * 60 * 1000;\n\nexport function runClaimKey(claims: RunClaimTable, runId: string): string {\n return `${claims}:run_id:${runId}`;\n}\n\n// A run's claim is a hash of generation, ownerId, updatedAt and, while\n// leased, leaseExpiresAt. A raised fence also holds retiredAt once retired.\n// Every script reads and writes it in one atomic step, and leases and\n// timestamps are measured on the store's clock.\nconst LIB = `\nlocal function now()\n local t = redis.call('TIME')\n return tonumber(t[1]) * 1000 + math.floor(tonumber(t[2]) / 1000)\nend\nlocal function owner(flag, t)\n local r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId', 'leaseExpiresAt')\n return { flag, r[1], r[2], r[3], t }\nend\n`;\n\n// ARGV: ownerId, leaseMs, force ('1' or '0'), expectedGeneration ('' when unset), retentionMs\nconst CLAIM = `${LIB}\nlocal t = now()\nlocal stamp = string.format('%d', t)\nlocal lease = string.format('%d', t + tonumber(ARGV[2]))\nlocal generation = tonumber(redis.call('HGET', KEYS[1], 'generation'))\nlocal expected = ARGV[4]\nif not generation then\n if expected ~= '' and expected ~= '0' then return owner(0, t) end\n -- The first generation comes from the store's clock, so a claim after this\n -- hash is removed still outranks a surviving fence.\n redis.call('HSET', KEYS[1], 'generation', string.format('%d', math.max(1, t)), 'ownerId', ARGV[1], 'leaseExpiresAt', lease, 'updatedAt', stamp)\n redis.call('PEXPIRE', KEYS[1], ARGV[5])\n return owner(1, t)\nend\nif expected ~= '' and generation ~= tonumber(expected) then return owner(0, t) end\nif ARGV[3] ~= '1' then\n local expires = tonumber(redis.call('HGET', KEYS[1], 'leaseExpiresAt'))\n if expires and expires > t then return owner(0, t) end\nend\nredis.call('HSET', KEYS[1], 'generation', string.format('%d', generation + 1), 'ownerId', ARGV[1], 'leaseExpiresAt', lease, 'updatedAt', stamp)\nredis.call('PEXPIRE', KEYS[1], ARGV[5])\nreturn owner(1, t)\n`;\n\n// ARGV: generation, ownerId, leaseMs, retentionMs\nconst RENEW = `${LIB}\nlocal t = now()\nlocal r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId', 'leaseExpiresAt')\nif tonumber(r[1]) ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] or not r[3] then return owner(0, t) end\nredis.call('HSET', KEYS[1], 'leaseExpiresAt', string.format('%d', t + tonumber(ARGV[3])), 'updatedAt', string.format('%d', t))\nredis.call('PEXPIRE', KEYS[1], ARGV[4])\nreturn owner(1, t)\n`;\n\n// ARGV: generation, ownerId, retentionMs\nconst RELEASE = `${LIB}\nlocal t = now()\nlocal r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId')\nif tonumber(r[1]) ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] then return 0 end\nredis.call('HDEL', KEYS[1], 'leaseExpiresAt')\nredis.call('HSET', KEYS[1], 'updatedAt', string.format('%d', t))\nredis.call('PEXPIRE', KEYS[1], ARGV[3])\nreturn 1\n`;\n\nconst READ = `${LIB}\nreturn owner(1, now())\n`;\n\n// ARGV: generation, ownerId\nconst RAISE = `\nlocal r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId')\nlocal current = tonumber(r[1])\nif not current or current < tonumber(ARGV[1]) then\n redis.call('HSET', KEYS[1], 'generation', ARGV[1], 'ownerId', ARGV[2])\nelseif current ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] then\n return 0\nend\n-- Every write un-retires the fence.\nredis.call('HDEL', KEYS[1], 'retiredAt')\nredis.call('PERSIST', KEYS[1])\nreturn 1\n`;\n\n// ARGV: generation, ownerId, retentionMs\nconst RETIRE = `${LIB}\nlocal t = now()\nlocal r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId')\nif tonumber(r[1]) ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] then return 0 end\nredis.call('HSET', KEYS[1], 'retiredAt', string.format('%d', t))\nredis.call('PEXPIRE', KEYS[1], ARGV[3])\nreturn 1\n`;\n\n// ARGV: generation, ownerId, then each command as its argument count followed by its arguments\nconst FENCED_WRITE = `\nlocal r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId')\nif tonumber(r[1]) ~= tonumber(ARGV[1]) or r[2] ~= ARGV[2] then return 0 end\nlocal i = 3\nwhile i <= #ARGV do\n local n = tonumber(ARGV[i])\n redis.call(unpack(ARGV, i + 1, i + n))\n i = i + n + 1\nend\nreturn 1\n`;\n\ntype OwnerReply = [number, string | null, string | null, string | null, number];\n\nfunction evalScript(client: RedisClient, script: string, keys: string[], args: string[]): Promise<unknown> {\n return client.eval(script, { keys, arguments: args });\n}\n\nfunction toRunOwnershipRecord(runId: string, reply: OwnerReply): RunOwnershipRecord | null {\n const [, generation, ownerId, leaseExpiresAt, now] = reply;\n if (generation === null) return null;\n const expiresAt = leaseExpiresAt === null ? null : Number(leaseExpiresAt);\n return {\n runId,\n generation: Number(generation),\n ownerId: String(ownerId),\n leaseExpiresAt: expiresAt === null ? null : new Date(expiresAt),\n live: expiresAt !== null && expiresAt > Number(now),\n };\n}\n\nasync function evalOwner(\n client: RedisClient,\n script: string,\n runId: string,\n args: string[],\n): Promise<{ applied: boolean; record: RunOwnershipRecord | null }> {\n const reply = (await evalScript(client, script, [runClaimKey(TABLE_WORKFLOW_RUN_OWNERS, runId)], args)) as OwnerReply;\n return { applied: Number(reply[0]) === 1, record: toRunOwnershipRecord(runId, reply) };\n}\n\nexport async function claimRunOwnership(\n client: RedisClient,\n { runId, ownerId, leaseMs, force, expectedGeneration }: ClaimRunOwnershipInput,\n): Promise<ClaimRunOwnershipResult> {\n const { applied, record } = await evalOwner(client, CLAIM, runId, [\n ownerId,\n String(leaseMs),\n force ? '1' : '0',\n expectedGeneration === undefined ? '' : String(expectedGeneration),\n String(RUN_CLAIM_RETENTION_MS),\n ]);\n return applied ? { acquired: true, record: record! } : { acquired: false, record };\n}\n\nexport async function renewRunOwnership(\n client: RedisClient,\n { leaseMs, ...fence }: RenewRunOwnershipInput,\n): Promise<RenewRunOwnershipResult> {\n const { applied, record } = await evalOwner(client, RENEW, fence.runId, [\n String(fence.generation),\n fence.ownerId,\n String(leaseMs),\n String(RUN_CLAIM_RETENTION_MS),\n ]);\n return applied ? { renewed: true, record: record! } : { renewed: false, record };\n}\n\nexport async function releaseRunOwnership(client: RedisClient, fence: RunFence): Promise<boolean> {\n const released = await evalScript(\n client,\n RELEASE,\n [runClaimKey(TABLE_WORKFLOW_RUN_OWNERS, fence.runId)],\n [String(fence.generation), fence.ownerId, String(RUN_CLAIM_RETENTION_MS)],\n );\n return Number(released) === 1;\n}\n\nexport async function getRunOwnership(client: RedisClient, runId: string): Promise<RunOwnershipRecord | null> {\n return (await evalOwner(client, READ, runId, [])).record;\n}\n\nexport async function raiseRunFence(client: RedisClient, fence: RunFence): Promise<boolean> {\n const raised = await evalScript(\n client,\n RAISE,\n [runClaimKey(TABLE_MEMORY_RUN_FENCES, fence.runId)],\n [String(fence.generation), fence.ownerId],\n );\n return Number(raised) === 1;\n}\n\nexport async function retireRunFence(client: RedisClient, fence: RunFence): Promise<boolean> {\n const retired = await evalScript(\n client,\n RETIRE,\n [runClaimKey(TABLE_MEMORY_RUN_FENCES, fence.runId)],\n [String(fence.generation), fence.ownerId, String(RUN_CLAIM_RETENTION_MS)],\n );\n return Number(retired) === 1;\n}\n\n/**\n * Collects writes and applies them in one script that first checks the run's\n * claim against the fence. A fence that is no longer current makes `exec()`\n * throw `RunFenceConflictError` without writing anything. Scripts run\n * atomically, so a takeover lands either before the batch (failing its check)\n * or after it.\n */\nclass FencedWriteBatch implements WriteBatch {\n readonly #keys = new Set<string>();\n readonly #args: string[] = [];\n\n constructor(\n private readonly client: RedisClient,\n private readonly check: RunFenceCheck,\n ) {}\n\n #add(key: string, ...command: string[]): this {\n this.#keys.add(key);\n this.#args.push(String(command.length), ...command);\n return this;\n }\n\n set(key: string, value: string) {\n return this.#add(key, 'SET', key, value);\n }\n\n del(key: string) {\n return this.#add(key, 'DEL', key);\n }\n\n zAdd(key: string, { score, value }: { score: number; value: string }) {\n return this.#add(key, 'ZADD', key, String(score), value);\n }\n\n zRem(key: string, member: string) {\n return this.#add(key, 'ZREM', key, member);\n }\n\n async exec(): Promise<void> {\n const { claims, fence, operation } = this.check;\n const applied = await evalScript(\n this.client,\n FENCED_WRITE,\n [runClaimKey(claims, fence.runId), ...this.#keys],\n [String(fence.generation), fence.ownerId, ...this.#args],\n );\n if (Number(applied) !== 1) throw new RunFenceConflictError(fence, operation);\n }\n}\n\n/** A MULTI transaction, or a fenced batch when `check` is set. */\nexport function writeBatch(client: RedisClient, check: RunFenceCheck | undefined): WriteBatch {\n return check ? new FencedWriteBatch(client, check) : client.multi();\n}\n\n/**\n * Rejects a fenced write that turns out to have nothing to write when its\n * fence is no longer current, as the write itself would have been rejected.\n */\nexport async function assertRunFence(client: RedisClient, check: RunFenceCheck | undefined): Promise<void> {\n if (check) await new FencedWriteBatch(client, check).exec();\n}\n","import { MessageList } from '@mastra/core/agent';\nimport type { MastraMessageContentV2 } from '@mastra/core/agent';\nimport { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';\nimport type { MastraDBMessage, StorageThreadType } from '@mastra/core/memory';\nimport {\n isRunFenceConflictError,\n MemoryStorage,\n resolveRunFence,\n TABLE_MEMORY_RUN_FENCES,\n TABLE_RESOURCES,\n TABLE_THREADS,\n TABLE_MESSAGES,\n normalizePerPage,\n calculatePagination,\n createStorageErrorId,\n ensureDate,\n filterByDateRange,\n jsonValueEquals,\n storageMessageMatchesMetadataFilter,\n validateStorageMetadataFilter,\n} from '@mastra/core/storage';\nimport type {\n RunFence,\n StorageResourceType,\n StorageListMessagesInput,\n StorageListMessagesOutput,\n StorageListThreadsInput,\n StorageListThreadsOutput,\n ThreadOrderBy,\n ThreadSortDirection,\n StorageCloneThreadInput,\n StorageCloneThreadOutput,\n ThreadCloneMetadata,\n} from '@mastra/core/storage';\n\nimport { RedisDB } from '../../db';\nimport type { RedisDomainConfig } from '../../db';\nimport type { RedisClient } from '../../types';\nimport { assertRunFence, raiseRunFence, retireRunFence, runClaimKey, writeBatch } from '../run-fencing';\nimport type { RunFenceCheck } from '../run-fencing';\nimport { getKey, processRecord } from '../utils';\n\nexport class StoreMemoryRedis extends MemoryStorage {\n override readonly listThreadsPaginatesInMemory = true;\n override readonly supportsPartialThreadUpdate = true;\n private client: RedisClient;\n private db: RedisDB;\n\n constructor(config: RedisDomainConfig) {\n super();\n this.client = config.client;\n this.db = new RedisDB({ client: config.client });\n }\n\n public override supportsRunFencing(): boolean {\n return true;\n }\n\n public override async raiseRunFence(fence: RunFence): Promise<boolean> {\n try {\n return await raiseRunFence(this.client, fence);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'RAISE_RUN_FENCE', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { runId: fence.runId },\n },\n error,\n );\n }\n }\n\n public override async retireRunFence(fence: RunFence): Promise<boolean> {\n try {\n return await retireRunFence(this.client, fence);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'RETIRE_RUN_FENCE', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { runId: fence.runId },\n },\n error,\n );\n }\n }\n\n #runFenceCheck(fence: RunFence | undefined, operation: string): RunFenceCheck | undefined {\n const resolved = resolveRunFence(this, fence);\n return resolved && { claims: TABLE_MEMORY_RUN_FENCES, fence: resolved, operation };\n }\n\n public async dangerouslyClearAll(): Promise<void> {\n await this.db.deleteData({ tableName: TABLE_THREADS });\n await this.db.deleteData({ tableName: TABLE_MESSAGES });\n await this.db.deleteData({ tableName: TABLE_RESOURCES });\n await this.db.scanAndDelete('msg-idx:*');\n await this.db.scanAndDelete('thread:*:messages');\n await this.db.scanAndDelete(`${runClaimKey(TABLE_MEMORY_RUN_FENCES, '')}*`);\n }\n\n public async getThreadById({\n threadId,\n resourceId,\n }: {\n threadId: string;\n resourceId?: string;\n }): Promise<StorageThreadType | null> {\n try {\n const thread = await this.db.get<StorageThreadType>({\n tableName: TABLE_THREADS,\n keys: { id: threadId },\n });\n\n if (!thread || (resourceId !== undefined && thread.resourceId !== resourceId)) {\n return null;\n }\n\n return {\n ...thread,\n createdAt: ensureDate(thread.createdAt)!,\n updatedAt: ensureDate(thread.updatedAt)!,\n metadata: typeof thread.metadata === 'string' ? JSON.parse(thread.metadata) : thread.metadata,\n };\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'GET_THREAD_BY_ID', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n threadId,\n },\n },\n error,\n );\n }\n }\n\n public async listThreadsByResourceId(args: StorageListThreadsInput): Promise<StorageListThreadsOutput> {\n return this.listThreads(args);\n }\n\n public async listThreads(args: StorageListThreadsInput): Promise<StorageListThreadsOutput> {\n const { page = 0, perPage: perPageInput, orderBy, filter } = args;\n const { field, direction } = this.parseOrderBy(orderBy);\n\n try {\n this.validatePaginationInput(page, perPageInput ?? 100);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_THREADS', 'INVALID_PAGE'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n details: { page, ...(perPageInput !== undefined && { perPage: perPageInput }) },\n },\n error instanceof Error ? error : new Error('Invalid pagination parameters'),\n );\n }\n\n const perPage = normalizePerPage(perPageInput, 100);\n\n try {\n this.validateMetadataKeys(filter?.metadata);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_THREADS', 'INVALID_METADATA_KEY'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n details: { metadataKeys: filter?.metadata ? Object.keys(filter.metadata).join(', ') : '' },\n },\n error instanceof Error ? error : new Error('Invalid metadata key'),\n );\n }\n\n const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);\n\n try {\n let allThreads: StorageThreadType[] = [];\n const pattern = `${TABLE_THREADS}:*`;\n const keys = await this.db.scanKeys(pattern);\n\n if (keys.length === 0) {\n return {\n threads: [],\n total: 0,\n page,\n perPage: perPageForResponse,\n hasMore: false,\n };\n }\n\n const results = await this.client.mGet(keys);\n\n for (let i = 0; i < results.length; i++) {\n const data = results[i];\n if (!data) {\n continue;\n }\n const thread = JSON.parse(data) as StorageThreadType;\n\n if (filter?.resourceId && thread.resourceId !== filter.resourceId) {\n continue;\n }\n\n if (filter?.metadata && Object.keys(filter.metadata).length > 0) {\n const threadMetadata = typeof thread.metadata === 'string' ? JSON.parse(thread.metadata) : thread.metadata;\n const matches = Object.entries(filter.metadata).every(([key, value]) =>\n jsonValueEquals(threadMetadata?.[key], value),\n );\n if (!matches) {\n continue;\n }\n }\n\n allThreads.push({\n ...thread,\n createdAt: ensureDate(thread.createdAt)!,\n updatedAt: ensureDate(thread.updatedAt)!,\n metadata: typeof thread.metadata === 'string' ? JSON.parse(thread.metadata) : thread.metadata,\n });\n }\n\n const sortedThreads = this.sortThreads(allThreads, field, direction);\n const total = sortedThreads.length;\n const end = perPageInput === false ? total : offset + perPage;\n const paginatedThreads = sortedThreads.slice(offset, end);\n const hasMore = perPageInput === false ? false : end < total;\n\n return {\n threads: paginatedThreads,\n total,\n page,\n perPage: perPageForResponse,\n hasMore,\n };\n } catch (error) {\n // Re-throw USER errors (validation errors) directly so callers get proper 400 responses\n if (error instanceof MastraError && error.category === ErrorCategory.USER) {\n throw error;\n }\n const mastraError = new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_THREADS', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n ...(filter?.resourceId && { resourceId: filter.resourceId }),\n hasMetadataFilter: !!filter?.metadata,\n page,\n perPage,\n },\n },\n error,\n );\n this.logger.trackException(mastraError);\n this.logger.error(mastraError.toString());\n throw mastraError;\n }\n }\n\n public async saveThread({\n thread,\n fence,\n }: {\n thread: StorageThreadType;\n fence?: RunFence;\n }): Promise<StorageThreadType> {\n const check = this.#runFenceCheck(fence, 'saveThread');\n try {\n const { key, processedRecord } = processRecord(TABLE_THREADS, thread);\n const batch = writeBatch(this.client, check);\n batch.set(key, JSON.stringify(processedRecord));\n await batch.exec();\n return thread;\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n const mastraError = new MastraError(\n {\n id: createStorageErrorId('REDIS', 'SAVE_THREAD', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n threadId: thread.id,\n },\n },\n error,\n );\n this.logger.trackException(mastraError);\n this.logger.error(mastraError.toString());\n throw mastraError;\n }\n }\n\n public async updateThread({\n id,\n title,\n metadata,\n fence,\n }: {\n id: string;\n title?: string;\n metadata?: Record<string, unknown>;\n fence?: RunFence;\n }): Promise<StorageThreadType> {\n const thread = await this.getThreadById({ threadId: id });\n if (!thread) {\n throw new MastraError({\n id: createStorageErrorId('REDIS', 'UPDATE_THREAD', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n text: `Thread ${id} not found`,\n details: {\n threadId: id,\n },\n });\n }\n\n const updatedThread = {\n ...thread,\n title: title ?? thread.title,\n metadata: {\n ...thread.metadata,\n ...metadata,\n },\n updatedAt: new Date(),\n };\n\n try {\n await this.saveThread({ thread: updatedThread, fence });\n return updatedThread;\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'UPDATE_THREAD', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n threadId: id,\n },\n },\n error,\n );\n }\n }\n\n public async deleteThread({ threadId }: { threadId: string }): Promise<void> {\n const threadKey = getKey(TABLE_THREADS, { id: threadId });\n const threadMessagesKey = getThreadMessagesKey(threadId);\n\n try {\n const messageIds = await this.client.zRange(threadMessagesKey, 0, -1);\n\n const multi = this.client.multi();\n multi.del(threadKey);\n multi.del(threadMessagesKey);\n\n for (const messageId of messageIds) {\n const messageKey = getMessageKey(threadId, messageId);\n multi.del(messageKey);\n multi.del(getMessageIndexKey(messageId));\n }\n\n await multi.exec();\n await this.db.scanAndDelete(getMessageKey(threadId, '*'));\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'DELETE_THREAD', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n threadId,\n },\n },\n error,\n );\n }\n }\n\n public async saveMessages(args: {\n messages: MastraDBMessage[];\n fence?: RunFence;\n }): Promise<{ messages: MastraDBMessage[] }> {\n const { messages } = args;\n const check = this.#runFenceCheck(args.fence, 'saveMessages');\n if (messages.length === 0) {\n return { messages: [] };\n }\n\n const threadId = messages[0]?.threadId;\n try {\n if (!threadId) {\n throw new Error('Thread ID is required');\n }\n const thread = await this.getThreadById({ threadId });\n if (!thread) {\n throw new Error(`Thread ${threadId} not found`);\n }\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'SAVE_MESSAGES', 'INVALID_ARGS'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n },\n error,\n );\n }\n\n const messagesWithIndex = messages.map((message, index) => {\n if (!message.threadId) {\n throw new Error(\n `Expected to find a threadId for message, but couldn't find one. An unexpected error has occurred.`,\n );\n }\n if (!message.resourceId) {\n throw new Error(\n `Expected to find a resourceId for message, but couldn't find one. An unexpected error has occurred.`,\n );\n }\n return {\n ...message,\n _index: index,\n };\n });\n\n const threadKey = getKey(TABLE_THREADS, { id: threadId });\n const existingThreadData = await this.client.get(threadKey);\n const existingThread = existingThreadData ? (JSON.parse(existingThreadData) as StorageThreadType) : null;\n\n try {\n const batchSize = 1000;\n const existingThreadIds = await this.client.mGet(\n messagesWithIndex.map(message => getMessageIndexKey(message.id)),\n );\n\n for (let i = 0; i < messagesWithIndex.length; i += batchSize) {\n const batch = messagesWithIndex.slice(i, i + batchSize);\n const batchExistingThreadIds = existingThreadIds.slice(i, i + batch.length);\n const multi = writeBatch(this.client, check);\n\n for (const [batchIndex, message] of batch.entries()) {\n const key = getMessageKey(message.threadId!, message.id);\n const score = getMessageScore(message);\n const existingThreadId = batchExistingThreadIds[batchIndex];\n\n if (existingThreadId && existingThreadId !== message.threadId) {\n const existingMessageKey = getMessageKey(existingThreadId, message.id);\n multi.del(existingMessageKey);\n multi.zRem(getThreadMessagesKey(existingThreadId), message.id);\n }\n\n multi.set(key, JSON.stringify(message));\n multi.set(getMessageIndexKey(message.id), message.threadId!);\n multi.zAdd(getThreadMessagesKey(message.threadId!), { score, value: message.id });\n }\n\n if (i === 0 && existingThread) {\n const updatedThread = {\n ...existingThread,\n updatedAt: new Date(),\n };\n multi.set(threadKey, JSON.stringify(processRecord(TABLE_THREADS, updatedThread).processedRecord));\n }\n\n await multi.exec();\n }\n\n const list = new MessageList().add(messages as Parameters<MessageList['add']>[0], 'memory');\n return { messages: list.get.all.db() };\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'SAVE_MESSAGES', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n threadId,\n },\n },\n error,\n );\n }\n }\n\n private async getThreadIdForMessage(messageId: string): Promise<string | null> {\n const indexedThreadId = await this.client.get(getMessageIndexKey(messageId));\n if (indexedThreadId) {\n return indexedThreadId;\n }\n\n const keys = await this.db.scanKeys(getMessageKey('*', messageId));\n if (keys.length === 0) {\n return null;\n }\n\n const messageData = await this.client.get(keys[0] as string);\n if (!messageData) {\n return null;\n }\n\n const message = JSON.parse(messageData) as MastraDBMessage;\n if (message.threadId) {\n await this.client.set(getMessageIndexKey(messageId), message.threadId);\n }\n\n return message.threadId || null;\n }\n\n /**\n * Fetches the messages named by `include` together with their surrounding context.\n *\n * @param include - Message ids to pin, each with an optional before/after window.\n * @param resourceId - When set, drops any pinned or context message owned by another\n * resource so an id from another resource returns nothing.\n */\n private async getIncludedMessages(\n include: StorageListMessagesInput['include'],\n resourceId?: string,\n ): Promise<MastraDBMessage[]> {\n if (!include?.length) {\n return [];\n }\n\n const messageIds = new Set<string>();\n const messageIdToThreadIds: Record<string, string> = {};\n\n for (const item of include) {\n const itemThreadId = await this.getThreadIdForMessage(item.id);\n if (!itemThreadId) {\n continue;\n }\n\n const itemThreadMessagesKey = getThreadMessagesKey(itemThreadId);\n\n if (resourceId !== undefined) {\n const threadMessageIds = await this.client.zRange(itemThreadMessagesKey, 0, -1);\n const threadMessages = (await this.client.mGet(threadMessageIds.map(id => getMessageKey(itemThreadId, id))))\n .filter((data): data is string => data !== null)\n .map(data => JSON.parse(data) as MastraDBMessage)\n .filter(message => message.resourceId === resourceId);\n const targetIndex = threadMessages.findIndex(message => message.id === item.id);\n if (targetIndex === -1) {\n continue;\n }\n\n const start = Math.max(0, targetIndex - (item.withPreviousMessages ?? 0));\n const end = Math.min(threadMessages.length, targetIndex + (item.withNextMessages ?? 0) + 1);\n for (const message of threadMessages.slice(start, end)) {\n messageIds.add(message.id);\n messageIdToThreadIds[message.id] = itemThreadId;\n }\n continue;\n }\n\n messageIds.add(item.id);\n messageIdToThreadIds[item.id] = itemThreadId;\n const rank = await this.client.zRank(itemThreadMessagesKey, item.id);\n if (rank === null) {\n continue;\n }\n\n if (item.withPreviousMessages) {\n const start = Math.max(0, rank - item.withPreviousMessages);\n const prevIds = rank === 0 ? [] : await this.client.zRange(itemThreadMessagesKey, start, rank - 1);\n prevIds.forEach(id => {\n messageIds.add(id);\n messageIdToThreadIds[id] = itemThreadId;\n });\n }\n\n if (item.withNextMessages) {\n const nextIds = await this.client.zRange(itemThreadMessagesKey, rank + 1, rank + item.withNextMessages);\n nextIds.forEach(id => {\n messageIds.add(id);\n messageIdToThreadIds[id] = itemThreadId;\n });\n }\n }\n\n if (messageIds.size === 0) {\n return [];\n }\n\n const keysToFetch = Array.from(messageIds).map(id => getMessageKey(messageIdToThreadIds[id]!, id));\n const results = await this.client.mGet(keysToFetch);\n\n const includedMessages = results\n .filter((data): data is string => data !== null)\n .map(data => JSON.parse(data) as MastraDBMessage);\n return resourceId ? includedMessages.filter(message => message.resourceId === resourceId) : includedMessages;\n }\n\n private parseStoredMessage(storedMessage: MastraDBMessage & { _index?: number }): MastraDBMessage {\n const defaultMessageContent = { format: 2, parts: [{ type: 'text', text: '' }] };\n const { _index, ...rest } = storedMessage;\n return {\n ...rest,\n createdAt: new Date(rest.createdAt),\n content: rest.content || defaultMessageContent,\n } satisfies MastraDBMessage;\n }\n\n public async listMessagesById({ messageIds }: { messageIds: string[] }): Promise<{ messages: MastraDBMessage[] }> {\n if (messageIds.length === 0) {\n return { messages: [] };\n }\n\n try {\n const rawMessages: (MastraDBMessage & { _index?: number })[] = [];\n\n const indexKeys = messageIds.map(id => getMessageIndexKey(id));\n const indexResults = await this.client.mGet(indexKeys);\n\n const indexedIds: { messageId: string; threadId: string }[] = [];\n const unindexedIds: string[] = [];\n\n messageIds.forEach((id, i) => {\n const threadId = indexResults[i];\n if (threadId) {\n indexedIds.push({ messageId: id, threadId });\n return;\n }\n unindexedIds.push(id);\n });\n\n if (indexedIds.length > 0) {\n const messageKeys = indexedIds.map(({ messageId, threadId }) => getMessageKey(threadId, messageId));\n const messageResults = await this.client.mGet(messageKeys);\n for (const data of messageResults) {\n if (data) {\n rawMessages.push(JSON.parse(data) as MastraDBMessage & { _index?: number });\n }\n }\n }\n\n if (unindexedIds.length > 0) {\n const threadKeys = await this.db.scanKeys('thread:*:messages');\n\n const result = await Promise.all(\n threadKeys.map(async threadKey => {\n const threadId = threadKey.split(':')[1];\n if (!threadId) {\n throw new Error(`Failed to parse thread ID from thread key \"${threadKey}\"`);\n }\n const msgKeys = unindexedIds.map(id => getMessageKey(threadId, id));\n return this.client.mGet(msgKeys);\n }),\n );\n\n const foundMessages = result\n .flat(1)\n .filter((data): data is string => !!data)\n .map(data => JSON.parse(data) as MastraDBMessage & { _index?: number });\n rawMessages.push(...foundMessages);\n\n if (foundMessages.length > 0) {\n const multi = this.client.multi();\n foundMessages.forEach(msg => {\n if (msg.threadId) {\n multi.set(getMessageIndexKey(msg.id), msg.threadId);\n }\n });\n await multi.exec();\n }\n }\n\n const list = new MessageList().add(rawMessages.map(this.parseStoredMessage), 'memory');\n return { messages: list.get.all.db() };\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_MESSAGES_BY_ID', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n messageIds: JSON.stringify(messageIds),\n },\n },\n error,\n );\n }\n }\n\n public async listMessages(args: StorageListMessagesInput): Promise<StorageListMessagesOutput> {\n const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;\n\n const threadIds = Array.isArray(threadId) ? threadId : [threadId];\n const threadIdsSet = new Set(threadIds);\n\n if (threadIds.length === 0 || threadIds.some(id => !id.trim())) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_MESSAGES', 'INVALID_THREAD_ID'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n details: { threadId: Array.isArray(threadId) ? threadId.join(',') : threadId },\n },\n new Error('threadId must be a non-empty string or array of non-empty strings'),\n );\n }\n\n const perPage = normalizePerPage(perPageInput, 40);\n const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);\n const metadataFilter = validateStorageMetadataFilter(filter?.metadata);\n\n try {\n if (page < 0) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_MESSAGES', 'INVALID_PAGE'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n details: { page },\n },\n new Error('page must be >= 0'),\n );\n }\n\n const { field, direction } = this.parseOrderBy(orderBy, 'ASC');\n\n const getFieldValue = (msg: MastraDBMessage): number => {\n if (field === 'createdAt') {\n return new Date(msg.createdAt).getTime();\n }\n\n const value = (msg as Record<string, unknown>)[field];\n if (typeof value === 'number') {\n return value;\n }\n if (value instanceof Date) {\n return value.getTime();\n }\n return 0;\n };\n\n if (perPage === 0 && (!include || include.length === 0)) {\n return {\n messages: [],\n total: 0,\n page,\n perPage: perPageForResponse,\n hasMore: false,\n };\n }\n\n let includedMessages: MastraDBMessage[] = [];\n if (include && include.length > 0) {\n const included = (await this.getIncludedMessages(include, resourceId)) as MastraDBMessage[];\n includedMessages = included.map(this.parseStoredMessage);\n }\n\n if (perPage === 0 && include && include.length > 0) {\n const list = new MessageList().add(includedMessages, 'memory');\n const messages = list.get.all.db().sort((a, b) => {\n const aValue = getFieldValue(a);\n const bValue = getFieldValue(b);\n return direction === 'ASC' ? aValue - bValue : bValue - aValue;\n });\n\n return {\n messages,\n total: 0,\n page,\n perPage: perPageForResponse,\n hasMore: false,\n };\n }\n\n const perThreadIds = await Promise.all(\n threadIds.map(tid => this.client.zRange(getThreadMessagesKey(tid), 0, -1)),\n );\n const allMessageIdsWithThreads: { threadId: string; messageId: string }[] = [];\n threadIds.forEach((tid, i) => {\n for (const mid of perThreadIds[i]!) {\n allMessageIdsWithThreads.push({ threadId: tid, messageId: mid });\n }\n });\n\n if (allMessageIdsWithThreads.length === 0) {\n return {\n messages: [],\n total: 0,\n page,\n perPage: perPageForResponse,\n hasMore: false,\n };\n }\n\n const messageKeys = allMessageIdsWithThreads.map(({ threadId: tid, messageId }) => getMessageKey(tid, messageId));\n const results = await this.client.mGet(messageKeys);\n\n let messagesData = results\n .filter((data): data is string => data !== null)\n .map(data => JSON.parse(data) as MastraDBMessage & { _index?: number })\n .map(this.parseStoredMessage);\n\n if (resourceId) {\n messagesData = messagesData.filter(msg => msg.resourceId === resourceId);\n }\n\n messagesData = filterByDateRange(\n messagesData,\n (msg: MastraDBMessage) => new Date(msg.createdAt),\n filter?.dateRange,\n );\n\n messagesData = messagesData.filter(message =>\n storageMessageMatchesMetadataFilter(message.content, metadataFilter),\n );\n\n messagesData.sort((a, b) => {\n const aValue = getFieldValue(a);\n const bValue = getFieldValue(b);\n return direction === 'ASC' ? aValue - bValue : bValue - aValue;\n });\n\n const total = messagesData.length;\n const start = offset;\n const end = perPageInput === false ? total : start + perPage;\n const paginatedMessages = messagesData.slice(start, end);\n\n const messageIdsSet = new Set<string>();\n const allMessages: MastraDBMessage[] = [];\n\n for (const msg of paginatedMessages) {\n if (messageIdsSet.has(msg.id)) {\n continue;\n }\n allMessages.push(msg);\n messageIdsSet.add(msg.id);\n }\n\n for (const msg of includedMessages) {\n if (messageIdsSet.has(msg.id)) {\n continue;\n }\n allMessages.push(msg);\n messageIdsSet.add(msg.id);\n }\n\n const list = new MessageList().add(allMessages, 'memory');\n let finalMessages = list.get.all.db();\n\n finalMessages = finalMessages.sort((a, b) => {\n const aValue = getFieldValue(a);\n const bValue = getFieldValue(b);\n return direction === 'ASC' ? aValue - bValue : bValue - aValue;\n });\n\n const returnedThreadMessageIds = new Set(\n finalMessages\n .filter(message => message.threadId && threadIdsSet.has(message.threadId))\n .map(message => message.id),\n );\n const hasMore =\n perPageInput !== false &&\n (metadataFilter || returnedThreadMessageIds.size < total) &&\n offset + paginatedMessages.length < total;\n\n return {\n messages: finalMessages,\n total,\n page,\n perPage: perPageForResponse,\n hasMore,\n };\n } catch (error) {\n // Re-throw USER errors (validation errors) directly so callers get proper 400 responses\n if (error instanceof MastraError && error.category === ErrorCategory.USER) {\n throw error;\n }\n const mastraError = new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_MESSAGES', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n threadId: Array.isArray(threadId) ? threadId.join(',') : threadId,\n resourceId: resourceId ?? '',\n },\n },\n error,\n );\n this.logger.error(mastraError.toString());\n this.logger.trackException(mastraError);\n throw mastraError;\n }\n }\n\n public async getResourceById({ resourceId }: { resourceId: string }): Promise<StorageResourceType | null> {\n try {\n const key = `${TABLE_RESOURCES}:${resourceId}`;\n const data = await this.client.get(key);\n if (!data) {\n return null;\n }\n\n const resource = JSON.parse(data) as StorageResourceType;\n\n return {\n ...resource,\n createdAt: new Date(resource.createdAt),\n updatedAt: new Date(resource.updatedAt),\n workingMemory:\n typeof resource.workingMemory === 'object' ? JSON.stringify(resource.workingMemory) : resource.workingMemory,\n metadata: typeof resource.metadata === 'string' ? JSON.parse(resource.metadata) : resource.metadata,\n };\n } catch (error) {\n this.logger.error('Error getting resource by ID:', error);\n throw error;\n }\n }\n\n public async saveResource({ resource }: { resource: StorageResourceType }): Promise<StorageResourceType> {\n return this.#saveResource(resource, undefined);\n }\n\n async #saveResource(resource: StorageResourceType, check: RunFenceCheck | undefined): Promise<StorageResourceType> {\n try {\n const key = `${TABLE_RESOURCES}:${resource.id}`;\n const serializedResource = {\n ...resource,\n metadata: JSON.stringify(resource.metadata),\n createdAt: resource.createdAt.toISOString(),\n updatedAt: resource.updatedAt.toISOString(),\n };\n\n const batch = writeBatch(this.client, check);\n batch.set(key, JSON.stringify(serializedResource));\n await batch.exec();\n\n return resource;\n } catch (error) {\n if (!isRunFenceConflictError(error)) this.logger.error('Error saving resource:', error);\n throw error;\n }\n }\n\n public async updateResource({\n resourceId,\n workingMemory,\n metadata,\n fence,\n }: {\n resourceId: string;\n workingMemory?: string;\n metadata?: Record<string, unknown>;\n fence?: RunFence;\n }): Promise<StorageResourceType> {\n const check = this.#runFenceCheck(fence, 'updateResource');\n try {\n const existingResource = await this.getResourceById({ resourceId });\n\n if (!existingResource) {\n const newResource: StorageResourceType = {\n id: resourceId,\n workingMemory,\n metadata: metadata || {},\n createdAt: new Date(),\n updatedAt: new Date(),\n };\n return await this.#saveResource(newResource, check);\n }\n\n const updatedResource = {\n ...existingResource,\n workingMemory: workingMemory !== undefined ? workingMemory : existingResource.workingMemory,\n metadata: {\n ...existingResource.metadata,\n ...metadata,\n },\n updatedAt: new Date(),\n };\n\n await this.#saveResource(updatedResource, check);\n return updatedResource;\n } catch (error) {\n if (!isRunFenceConflictError(error)) this.logger.error('Error updating resource:', error);\n throw error;\n }\n }\n\n public async updateMessages(args: {\n messages: (Partial<Omit<MastraDBMessage, 'createdAt'>> & {\n id: string;\n content?: { metadata?: MastraMessageContentV2['metadata']; content?: MastraMessageContentV2['content'] };\n })[];\n fence?: RunFence;\n }): Promise<MastraDBMessage[]> {\n const { messages } = args;\n if (messages.length === 0) {\n return [];\n }\n const check = this.#runFenceCheck(args.fence, 'updateMessages');\n\n try {\n const messageIds = messages.map(m => m.id);\n const existingMessages: MastraDBMessage[] = [];\n const messageIdToKey: Record<string, string> = {};\n\n for (const messageId of messageIds) {\n const pattern = getMessageKey('*', messageId);\n const keys = await this.db.scanKeys(pattern);\n\n for (const key of keys) {\n const data = await this.client.get(key);\n if (!data) {\n continue;\n }\n const message = JSON.parse(data) as MastraDBMessage;\n if (message && message.id === messageId) {\n existingMessages.push(message);\n messageIdToKey[messageId] = key;\n break;\n }\n }\n }\n\n if (existingMessages.length === 0) {\n await assertRunFence(this.client, check);\n return [];\n }\n\n const threadIdsToUpdate = new Set<string>();\n const multi = writeBatch(this.client, check);\n\n for (const existingMessage of existingMessages) {\n const updatePayload = messages.find(m => m.id === existingMessage.id);\n if (!updatePayload) {\n continue;\n }\n\n const { id, ...fieldsToUpdate } = updatePayload;\n if (Object.keys(fieldsToUpdate).length === 0) {\n continue;\n }\n\n threadIdsToUpdate.add(existingMessage.threadId!);\n if (updatePayload.threadId && updatePayload.threadId !== existingMessage.threadId) {\n threadIdsToUpdate.add(updatePayload.threadId);\n }\n\n const updatedMessage = { ...existingMessage };\n\n if (fieldsToUpdate.content) {\n const existingContent = existingMessage.content as MastraMessageContentV2;\n const newContent = {\n ...existingContent,\n ...fieldsToUpdate.content,\n ...(existingContent?.metadata && fieldsToUpdate.content.metadata\n ? {\n metadata: {\n ...existingContent.metadata,\n ...fieldsToUpdate.content.metadata,\n },\n }\n : {}),\n };\n updatedMessage.content = newContent;\n }\n\n for (const key in fieldsToUpdate) {\n if (Object.prototype.hasOwnProperty.call(fieldsToUpdate, key) && key !== 'content') {\n (updatedMessage as Record<string, unknown>)[key] = fieldsToUpdate[key as keyof typeof fieldsToUpdate];\n }\n }\n\n const key = messageIdToKey[id];\n if (!key) {\n continue;\n }\n\n if (updatePayload.threadId && updatePayload.threadId !== existingMessage.threadId) {\n multi.zRem(getThreadMessagesKey(existingMessage.threadId!), id);\n multi.del(key);\n\n const newKey = getMessageKey(updatePayload.threadId, id);\n multi.set(newKey, JSON.stringify(updatedMessage));\n multi.set(getMessageIndexKey(id), updatePayload.threadId);\n\n const score = getMessageScore(updatedMessage as MastraDBMessage & { _index?: number });\n multi.zAdd(getThreadMessagesKey(updatePayload.threadId), { score, value: id });\n\n messageIdToKey[id] = newKey;\n continue;\n }\n\n multi.set(key, JSON.stringify(updatedMessage));\n }\n\n const now = new Date();\n for (const threadId of threadIdsToUpdate) {\n if (threadId) {\n const threadKey = getKey(TABLE_THREADS, { id: threadId });\n const existingThreadData = await this.client.get(threadKey);\n if (existingThreadData) {\n const existingThread = JSON.parse(existingThreadData) as StorageThreadType;\n const updatedThread = {\n ...existingThread,\n updatedAt: now,\n };\n multi.set(threadKey, JSON.stringify(processRecord(TABLE_THREADS, updatedThread).processedRecord));\n }\n }\n }\n\n await multi.exec();\n\n const updatedMessages: MastraDBMessage[] = [];\n for (const messageId of messageIds) {\n const key = messageIdToKey[messageId];\n if (key) {\n const data = await this.client.get(key);\n if (data) {\n updatedMessages.push(JSON.parse(data) as MastraDBMessage);\n }\n }\n }\n\n return updatedMessages;\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'UPDATE_MESSAGES', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n messageIds: messages.map(m => m.id).join(','),\n },\n },\n error,\n );\n }\n }\n\n public async deleteMessages(messageIds: string[], options?: { fence?: RunFence }): Promise<void> {\n if (!messageIds || messageIds.length === 0) {\n return;\n }\n const check = this.#runFenceCheck(options?.fence, 'deleteMessages');\n\n try {\n const threadIds = new Set<string>();\n const messageKeys: string[] = [];\n const foundMessageIds: string[] = [];\n const messageIdToThreadId = new Map<string, string>();\n\n const indexKeys = messageIds.map(id => getMessageIndexKey(id));\n const indexResults = await this.client.mGet(indexKeys);\n\n const indexedMessages: { messageId: string; threadId: string }[] = [];\n const unindexedMessageIds: string[] = [];\n\n messageIds.forEach((id, i) => {\n const threadId = indexResults[i];\n if (threadId) {\n indexedMessages.push({ messageId: id, threadId });\n return;\n }\n unindexedMessageIds.push(id);\n });\n\n for (const { messageId, threadId } of indexedMessages) {\n messageKeys.push(getMessageKey(threadId, messageId));\n foundMessageIds.push(messageId);\n messageIdToThreadId.set(messageId, threadId);\n threadIds.add(threadId);\n }\n\n for (const messageId of unindexedMessageIds) {\n const pattern = getMessageKey('*', messageId);\n const keys = await this.db.scanKeys(pattern);\n\n for (const key of keys) {\n const data = await this.client.get(key);\n if (!data) {\n continue;\n }\n const message = JSON.parse(data) as MastraDBMessage;\n if (message && message.id === messageId) {\n messageKeys.push(key);\n foundMessageIds.push(messageId);\n if (message.threadId) {\n messageIdToThreadId.set(messageId, message.threadId);\n threadIds.add(message.threadId);\n }\n break;\n }\n }\n }\n\n if (messageKeys.length === 0) {\n await assertRunFence(this.client, check);\n return;\n }\n\n const multi = writeBatch(this.client, check);\n\n for (const key of messageKeys) {\n multi.del(key);\n }\n\n for (const messageId of foundMessageIds) {\n multi.del(getMessageIndexKey(messageId));\n }\n\n if (threadIds.size > 0) {\n for (const threadId of threadIds) {\n for (const [msgId, msgThreadId] of messageIdToThreadId) {\n if (msgThreadId === threadId) {\n multi.zRem(getThreadMessagesKey(threadId), msgId);\n }\n }\n\n const threadKey = getKey(TABLE_THREADS, { id: threadId });\n const threadData = await this.client.get(threadKey);\n if (!threadData) {\n continue;\n }\n\n const thread = JSON.parse(threadData) as StorageThreadType;\n const updatedThread = { ...thread, updatedAt: new Date() };\n multi.set(threadKey, JSON.stringify(processRecord(TABLE_THREADS, updatedThread).processedRecord));\n }\n }\n\n await multi.exec();\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'DELETE_MESSAGES', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { messageIds: messageIds.join(', ') },\n },\n error,\n );\n }\n }\n\n private sortThreads(\n threads: StorageThreadType[],\n field: ThreadOrderBy,\n direction: ThreadSortDirection,\n ): StorageThreadType[] {\n return threads.sort((a, b) => {\n const aValue = new Date(a[field]).getTime();\n const bValue = new Date(b[field]).getTime();\n return direction === 'ASC' ? aValue - bValue : bValue - aValue;\n });\n }\n\n public async cloneThread(args: StorageCloneThreadInput): Promise<StorageCloneThreadOutput> {\n const { sourceThreadId, newThreadId: providedThreadId, resourceId, title, metadata, options } = args;\n\n const sourceThread = await this.getThreadById({ threadId: sourceThreadId });\n if (!sourceThread) {\n throw new MastraError({\n id: createStorageErrorId('REDIS', 'CLONE_THREAD', 'SOURCE_NOT_FOUND'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n text: `Source thread with id ${sourceThreadId} not found`,\n details: { sourceThreadId },\n });\n }\n\n const newThreadId = providedThreadId || crypto.randomUUID();\n\n const existingThread = await this.getThreadById({ threadId: newThreadId });\n if (existingThread) {\n throw new MastraError({\n id: createStorageErrorId('REDIS', 'CLONE_THREAD', 'THREAD_EXISTS'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n text: `Thread with id ${newThreadId} already exists`,\n details: { newThreadId },\n });\n }\n\n try {\n const threadMessagesKey = getThreadMessagesKey(sourceThreadId);\n const msgIds = await this.client.zRange(threadMessagesKey, 0, -1);\n\n const messageKeys = msgIds.map(mid => getMessageKey(sourceThreadId, mid));\n let sourceMessages: (MastraDBMessage & { _index?: number })[] = [];\n\n if (messageKeys.length > 0) {\n const results = await this.client.mGet(messageKeys);\n sourceMessages = results\n .filter((data): data is string => data !== null)\n .map(data => {\n const msg = JSON.parse(data) as MastraDBMessage & { _index?: number };\n return { ...msg, createdAt: new Date(msg.createdAt) };\n });\n }\n\n if (options?.messageFilter?.startDate || options?.messageFilter?.endDate) {\n sourceMessages = filterByDateRange(sourceMessages, (msg: MastraDBMessage) => new Date(msg.createdAt), {\n start: options.messageFilter?.startDate,\n end: options.messageFilter?.endDate,\n });\n }\n\n if (options?.messageFilter?.messageIds && options.messageFilter.messageIds.length > 0) {\n const messageIdSet = new Set(options.messageFilter.messageIds);\n sourceMessages = sourceMessages.filter(msg => messageIdSet.has(msg.id));\n }\n\n sourceMessages.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());\n\n if (options?.messageLimit && options.messageLimit > 0 && sourceMessages.length > options.messageLimit) {\n sourceMessages = sourceMessages.slice(-options.messageLimit);\n }\n\n const now = new Date();\n const lastMessageId = sourceMessages.length > 0 ? sourceMessages[sourceMessages.length - 1]!.id : undefined;\n\n const cloneMetadata: ThreadCloneMetadata = {\n sourceThreadId,\n clonedAt: now,\n ...(lastMessageId && { lastMessageId }),\n };\n\n const newThread: StorageThreadType = {\n id: newThreadId,\n resourceId: resourceId || sourceThread.resourceId,\n title: title || (sourceThread.title ? `Clone of ${sourceThread.title}` : undefined),\n metadata: { ...metadata, clone: cloneMetadata },\n createdAt: now,\n updatedAt: now,\n };\n\n const multi = this.client.multi();\n const threadKey = getKey(TABLE_THREADS, { id: newThreadId });\n multi.set(threadKey, JSON.stringify(processRecord(TABLE_THREADS, newThread).processedRecord));\n\n const clonedMessages: MastraDBMessage[] = [];\n const messageIdMap: Record<string, string> = Object.create(null);\n const targetResourceId = resourceId || sourceThread.resourceId;\n const newThreadMessagesKey = getThreadMessagesKey(newThreadId);\n\n for (let i = 0; i < sourceMessages.length; i++) {\n const sourceMsg = sourceMessages[i]!;\n const newMessageId = crypto.randomUUID();\n const { _index, ...restMsg } = sourceMsg;\n\n const newMessage: MastraDBMessage = {\n ...restMsg,\n id: newMessageId,\n threadId: newThreadId,\n resourceId: targetResourceId,\n };\n\n const messageKey = getMessageKey(newThreadId, newMessageId);\n multi.set(messageKey, JSON.stringify(newMessage));\n multi.set(getMessageIndexKey(newMessageId), newThreadId);\n const score = getMessageScore({ createdAt: newMessage.createdAt, _index: i });\n multi.zAdd(newThreadMessagesKey, { score, value: newMessageId });\n\n clonedMessages.push(newMessage);\n messageIdMap[sourceMsg.id] = newMessageId;\n }\n\n await multi.exec();\n\n return {\n thread: newThread,\n clonedMessages,\n messageIdMap,\n };\n } catch (error) {\n if (error instanceof MastraError) {\n throw error;\n }\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'CLONE_THREAD', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { sourceThreadId, newThreadId },\n },\n error,\n );\n }\n }\n}\n\nfunction getThreadMessagesKey(threadId: string): string {\n return `thread:${threadId}:messages`;\n}\n\nfunction getMessageKey(threadId: string, messageId: string): string {\n return getKey(TABLE_MESSAGES, { threadId, id: messageId });\n}\n\nfunction getMessageIndexKey(messageId: string): string {\n return `msg-idx:${messageId}`;\n}\n\nfunction getMessageScore(message: { createdAt: Date | string; _index?: number }): number {\n const createdAtScore = new Date(message.createdAt).getTime();\n const index = typeof message._index === 'number' ? message._index : 0;\n return createdAtScore * 1000 + index;\n}\n","import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';\nimport type { SaveScorePayload, ScoreRowData, ScoringSource } from '@mastra/core/evals';\nimport { saveScorePayloadSchema } from '@mastra/core/evals';\nimport {\n calculatePagination,\n normalizePerPage,\n ScoresStorage,\n TABLE_SCORERS,\n transformScoreRow,\n createStorageErrorId,\n} from '@mastra/core/storage';\nimport type { PaginationInfo, StoragePagination, ScoreTenancyFilters } from '@mastra/core/storage';\n\nimport { RedisDB } from '../../db';\nimport type { RedisDomainConfig } from '../../db';\nimport type { RedisClient } from '../../types';\nimport { processRecord } from '../utils';\n\n/** Returns true when a row matches the multi-tenant scope filters (or none provided). */\nfunction matchesTenancy(row: Record<string, unknown>, filters?: ScoreTenancyFilters): boolean {\n if (filters?.organizationId !== undefined && row.organizationId !== filters.organizationId) return false;\n if (filters?.projectId !== undefined && row.projectId !== filters.projectId) return false;\n return true;\n}\n\nexport class ScoresRedis extends ScoresStorage {\n private client: RedisClient;\n private db: RedisDB;\n\n constructor(config: RedisDomainConfig) {\n super();\n this.client = config.client;\n this.db = new RedisDB({ client: config.client });\n }\n\n public async dangerouslyClearAll(): Promise<void> {\n await this.db.deleteData({ tableName: TABLE_SCORERS });\n }\n\n public async getScoreById({ id }: { id: string }): Promise<ScoreRowData | null> {\n try {\n const data = await this.db.get<ScoreRowData>({\n tableName: TABLE_SCORERS,\n keys: { id },\n });\n\n if (!data) {\n return null;\n }\n\n return transformScoreRow(data as Record<string, unknown>);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'GET_SCORE_BY_ID', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n ...(id && { id }),\n },\n },\n error,\n );\n }\n }\n\n public async listScoresByScorerId({\n scorerId,\n entityId,\n entityType,\n source,\n pagination = { page: 0, perPage: 20 },\n filters,\n }: {\n scorerId: string;\n entityId?: string;\n entityType?: string;\n source?: ScoringSource;\n pagination?: StoragePagination;\n filters?: ScoreTenancyFilters;\n }): Promise<{\n scores: ScoreRowData[];\n pagination: PaginationInfo;\n }> {\n return this.fetchAndFilterScores(pagination, row => {\n if (row.scorerId !== scorerId) {\n return false;\n }\n if (entityId && row.entityId !== entityId) {\n return false;\n }\n if (entityType && row.entityType !== entityType) {\n return false;\n }\n if (source && row.source !== source) {\n return false;\n }\n if (!matchesTenancy(row, filters)) {\n return false;\n }\n return true;\n });\n }\n\n public async saveScore(score: SaveScorePayload): Promise<{ score: ScoreRowData }> {\n let validatedScore: SaveScorePayload;\n try {\n validatedScore = saveScorePayloadSchema.parse(score);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'SAVE_SCORE', 'VALIDATION_FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n details: {\n scorer: typeof score.scorer?.id === 'string' ? score.scorer.id : String(score.scorer?.id ?? 'unknown'),\n entityId: score.entityId ?? 'unknown',\n entityType: score.entityType ?? 'unknown',\n traceId: score.traceId ?? '',\n spanId: score.spanId ?? '',\n },\n },\n error,\n );\n }\n\n const now = new Date();\n const id = crypto.randomUUID();\n\n const scoreWithId = {\n ...validatedScore,\n id,\n createdAt: now,\n updatedAt: now,\n };\n\n const { key, processedRecord } = processRecord(TABLE_SCORERS, scoreWithId);\n try {\n await this.client.set(key, JSON.stringify(processedRecord));\n return { score: { ...validatedScore, id, createdAt: now, updatedAt: now } as ScoreRowData };\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'SAVE_SCORE', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { id },\n },\n error,\n );\n }\n }\n\n public async listScoresByRunId({\n runId,\n pagination = { page: 0, perPage: 20 },\n filters,\n }: {\n runId: string;\n pagination?: StoragePagination;\n filters?: ScoreTenancyFilters;\n }): Promise<{\n scores: ScoreRowData[];\n pagination: PaginationInfo;\n }> {\n return this.fetchAndFilterScores(pagination, row => row.runId === runId && matchesTenancy(row, filters));\n }\n\n public async listScoresByEntityId({\n entityId,\n entityType,\n pagination = { page: 0, perPage: 20 },\n filters,\n }: {\n entityId: string;\n entityType?: string;\n pagination?: StoragePagination;\n filters?: ScoreTenancyFilters;\n }): Promise<{\n scores: ScoreRowData[];\n pagination: PaginationInfo;\n }> {\n return this.fetchAndFilterScores(pagination, row => {\n if (row.entityId !== entityId) {\n return false;\n }\n if (entityType && row.entityType !== entityType) {\n return false;\n }\n if (!matchesTenancy(row, filters)) {\n return false;\n }\n return true;\n });\n }\n\n public async listScoresBySpan({\n traceId,\n spanId,\n pagination = { page: 0, perPage: 20 },\n filters,\n }: {\n traceId: string;\n spanId: string;\n pagination?: StoragePagination;\n filters?: ScoreTenancyFilters;\n }): Promise<{\n scores: ScoreRowData[];\n pagination: PaginationInfo;\n }> {\n return this.fetchAndFilterScores(\n pagination,\n row => row.traceId === traceId && row.spanId === spanId && matchesTenancy(row, filters),\n );\n }\n\n private async fetchAndFilterScores(\n pagination: StoragePagination,\n filterFn: (row: Record<string, unknown>) => boolean,\n ): Promise<{ scores: ScoreRowData[]; pagination: PaginationInfo }> {\n const { page, perPage: perPageInput } = pagination;\n const keys = await this.db.scanKeys(`${TABLE_SCORERS}:*`);\n\n if (keys.length === 0) {\n return {\n scores: [],\n pagination: { total: 0, page, perPage: perPageInput, hasMore: false },\n };\n }\n\n const results = await this.client.mGet(keys);\n\n const filtered = results\n .map((data): Record<string, unknown> | null => {\n if (!data) {\n return null;\n }\n try {\n return JSON.parse(data) as Record<string, unknown>;\n } catch {\n return null;\n }\n })\n .filter((row): row is Record<string, unknown> => !!row && typeof row === 'object' && filterFn(row));\n\n const total = filtered.length;\n const perPage = normalizePerPage(perPageInput, 100);\n const { offset: start, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);\n const end = perPageInput === false ? total : start + perPage;\n const scores = filtered.slice(start, end).map(row => transformScoreRow(row));\n\n return {\n scores,\n pagination: {\n total,\n page,\n perPage: perPageForResponse,\n hasMore: end < total,\n },\n };\n }\n}\n","import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';\nimport {\n createStorageErrorId,\n isRunFenceConflictError,\n normalizePerPage,\n resolveRunFence,\n TABLE_WORKFLOW_RUN_OWNERS,\n TABLE_WORKFLOW_SNAPSHOT,\n matchesExpectedWorkflowStatus,\n WorkflowsStorage,\n ensureDate,\n} from '@mastra/core/storage';\nimport type {\n ClaimRunOwnershipInput,\n ClaimRunOwnershipResult,\n RenewRunOwnershipInput,\n RenewRunOwnershipResult,\n RunFence,\n RunOwnershipRecord,\n StorageListWorkflowRunsInput,\n WorkflowRun,\n WorkflowRuns,\n UpdateWorkflowStateOptions,\n} from '@mastra/core/storage';\nimport type { StepResult, WorkflowRunState } from '@mastra/core/workflows';\n\nimport { RedisDB } from '../../db';\nimport type { RedisDomainConfig } from '../../db';\nimport type { RedisClient } from '../../types';\nimport {\n claimRunOwnership,\n getRunOwnership,\n releaseRunOwnership,\n renewRunOwnership,\n runClaimKey,\n writeBatch,\n} from '../run-fencing';\nimport type { RunFenceCheck } from '../run-fencing';\nimport { getKey, processRecord } from '../utils';\n\nfunction parseWorkflowRun(row: Record<string, unknown>): WorkflowRun {\n let parsedSnapshot: WorkflowRunState | string = row.snapshot as string;\n if (typeof parsedSnapshot === 'string') {\n try {\n parsedSnapshot = JSON.parse(row.snapshot as string) as WorkflowRunState;\n } catch (e) {\n console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);\n }\n }\n\n return {\n workflowName: row.workflow_name as string,\n runId: row.run_id as string,\n snapshot: parsedSnapshot,\n createdAt: ensureDate(row.createdAt as string | Date)!,\n updatedAt: ensureDate(row.updatedAt as string | Date)!,\n resourceId: row.resourceId as string | undefined,\n };\n}\n\ntype SnapshotRecord = {\n namespace: string;\n workflow_name: string;\n run_id: string;\n resourceId?: string;\n snapshot: WorkflowRunState;\n createdAt: string | Date;\n updatedAt: string | Date;\n};\n\nfunction escapeGlob(value: string): string {\n return value.replace(/[*?[\\]\\\\]/g, '\\\\$&');\n}\n\nfunction snapshotKey(namespace: string, workflowName: string, runId: string): string {\n return getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace, workflow_name: workflowName, run_id: runId });\n}\n\n/**\n * Earlier versions appended `:resourceId:<id>` to snapshot keys. Matches those keys for a run.\n */\nfunction legacySnapshotKeyPattern(namespace: string, workflowName: string, runId: string): string {\n return `${escapeGlob(snapshotKey(namespace, workflowName, runId))}:resourceId:*`;\n}\n\nexport class WorkflowsRedis extends WorkflowsStorage {\n private client: RedisClient;\n private db: RedisDB;\n\n constructor(config: RedisDomainConfig) {\n super();\n this.client = config.client;\n this.db = new RedisDB({ client: config.client });\n }\n\n /**\n * Reads a run's snapshot record by its canonical key, falling back to legacy\n * `:resourceId:`-suffixed keys only when the canonical key is missing.\n */\n private async getSnapshotRecord(\n namespace: string,\n workflowName: string,\n runId: string,\n ): Promise<SnapshotRecord | null> {\n const data = await this.client.get(snapshotKey(namespace, workflowName, runId));\n if (data) {\n return JSON.parse(data) as SnapshotRecord;\n }\n\n const legacyKeys = await this.db.scanKeys(legacySnapshotKeyPattern(namespace, workflowName, runId));\n if (legacyKeys.length === 0) {\n return null;\n }\n const legacyData = await this.client.get(legacyKeys[0]!);\n return legacyData ? (JSON.parse(legacyData) as SnapshotRecord) : null;\n }\n\n public supportsConcurrentUpdates(): boolean {\n return false;\n }\n\n public override supportsRunFencing(): boolean {\n return true;\n }\n\n public async dangerouslyClearAll(): Promise<void> {\n await this.db.deleteData({ tableName: TABLE_WORKFLOW_SNAPSHOT });\n await this.db.scanAndDelete(`${escapeGlob(runClaimKey(TABLE_WORKFLOW_RUN_OWNERS, ''))}*`);\n }\n\n #runFenceCheck(fence: RunFence | undefined, runId: string, operation: string): RunFenceCheck | undefined {\n const resolved = resolveRunFence(this, fence, runId);\n return resolved && { claims: TABLE_WORKFLOW_RUN_OWNERS, fence: resolved, operation };\n }\n\n #ownershipError(operation: string, runId: string, error: unknown): MastraError {\n return new MastraError(\n {\n id: createStorageErrorId('REDIS', operation, 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { runId },\n },\n error,\n );\n }\n\n // Each operation is one script over the run's ownership hash, so racing\n // claimers and renewals serialize on Redis and resolve to a single winner.\n public override async claimRunOwnership(args: ClaimRunOwnershipInput): Promise<ClaimRunOwnershipResult> {\n try {\n return await claimRunOwnership(this.client, args);\n } catch (error) {\n throw this.#ownershipError('CLAIM_RUN_OWNERSHIP', args.runId, error);\n }\n }\n\n public override async renewRunOwnership(args: RenewRunOwnershipInput): Promise<RenewRunOwnershipResult> {\n try {\n return await renewRunOwnership(this.client, args);\n } catch (error) {\n throw this.#ownershipError('RENEW_RUN_OWNERSHIP', args.runId, error);\n }\n }\n\n public override async releaseRunOwnership(fence: RunFence): Promise<boolean> {\n try {\n return await releaseRunOwnership(this.client, fence);\n } catch (error) {\n throw this.#ownershipError('RELEASE_RUN_OWNERSHIP', fence.runId, error);\n }\n }\n\n public override async getRunOwnership({ runId }: { runId: string }): Promise<RunOwnershipRecord | null> {\n try {\n return await getRunOwnership(this.client, runId);\n } catch (error) {\n throw this.#ownershipError('GET_RUN_OWNERSHIP', runId, error);\n }\n }\n\n public async updateWorkflowResults({\n workflowName,\n runId,\n stepId,\n result,\n requestContext,\n state,\n fence,\n }: {\n workflowName: string;\n runId: string;\n stepId: string;\n result: StepResult<unknown, unknown, unknown, unknown>;\n requestContext: Record<string, unknown>;\n state?: Record<string, unknown>;\n fence?: RunFence;\n }): Promise<Record<string, StepResult<unknown, unknown, unknown, unknown>>> {\n const resolvedFence = resolveRunFence(this, fence, runId);\n try {\n const existingRecord = await this.getSnapshotRecord('workflows', workflowName, runId);\n\n const existingSnapshot = existingRecord?.snapshot;\n let snapshot = existingSnapshot;\n\n if (!snapshot) {\n snapshot = {\n context: {},\n activePaths: [],\n timestamp: Date.now(),\n suspendedPaths: {},\n activeStepsPath: {},\n resumeLabels: {},\n serializedStepGraph: [],\n status: 'pending',\n value: {},\n waitingPaths: {},\n runId,\n requestContext: {},\n } as WorkflowRunState;\n }\n\n snapshot.context[stepId] = result;\n if (state !== undefined) {\n (snapshot.context as Record<string, unknown>).__state = state;\n }\n snapshot.requestContext = { ...snapshot.requestContext, ...requestContext };\n\n await this.persistWorkflowSnapshot({\n namespace: 'workflows',\n workflowName,\n runId,\n resourceId: existingRecord?.resourceId,\n snapshot,\n createdAt: existingRecord?.createdAt ? ensureDate(existingRecord.createdAt) : undefined,\n fence: resolvedFence,\n });\n\n return snapshot.context;\n } catch (error) {\n if (error instanceof MastraError) {\n throw error;\n }\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'UPDATE_WORKFLOW_RESULTS', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { workflowName, runId, stepId },\n },\n error,\n );\n }\n }\n\n public async updateWorkflowState({\n workflowName,\n runId,\n opts,\n fence,\n }: {\n workflowName: string;\n runId: string;\n opts: UpdateWorkflowStateOptions;\n fence?: RunFence;\n }): Promise<WorkflowRunState | undefined> {\n const resolvedFence = resolveRunFence(this, fence, runId);\n try {\n const existingRecord = await this.getSnapshotRecord('workflows', workflowName, runId);\n\n const existingSnapshot = existingRecord?.snapshot;\n\n if (!existingSnapshot || !existingSnapshot.context) {\n return undefined;\n }\n\n // Best-effort only: this store reports `supportsConcurrentUpdates() === false`, so the\n // read and the write are not a single critical section.\n const { expectedStatus, ...state } = opts;\n if (!matchesExpectedWorkflowStatus(existingSnapshot.status, expectedStatus)) {\n return undefined;\n }\n\n const updatedSnapshot = { ...existingSnapshot, ...state };\n\n await this.persistWorkflowSnapshot({\n namespace: 'workflows',\n workflowName,\n runId,\n resourceId: existingRecord.resourceId,\n snapshot: updatedSnapshot,\n createdAt: existingRecord?.createdAt ? ensureDate(existingRecord.createdAt) : undefined,\n fence: resolvedFence,\n });\n\n return updatedSnapshot;\n } catch (error) {\n if (error instanceof MastraError) {\n throw error;\n }\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'UPDATE_WORKFLOW_STATE', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: { workflowName, runId },\n },\n error,\n );\n }\n }\n\n public async persistWorkflowSnapshot(params: {\n namespace?: string;\n workflowName: string;\n runId: string;\n resourceId?: string;\n snapshot: WorkflowRunState;\n createdAt?: Date;\n updatedAt?: Date;\n fence?: RunFence;\n }): Promise<void> {\n const { namespace = 'workflows', workflowName, runId, resourceId, snapshot, createdAt, updatedAt } = params;\n const check = this.#runFenceCheck(params.fence, runId, 'persistWorkflowSnapshot');\n try {\n let finalCreatedAt = createdAt;\n let finalResourceId = resourceId;\n if (!finalCreatedAt || !finalResourceId) {\n // No SCAN here (it would run on every new run); a legacy key is only reachable when resourceId is known.\n const canonicalKey = snapshotKey(namespace, workflowName, runId);\n const existingData =\n (await this.client.get(canonicalKey)) ??\n (resourceId ? await this.client.get(`${canonicalKey}:resourceId:${resourceId}`) : null);\n const existing = existingData ? (JSON.parse(existingData) as SnapshotRecord) : null;\n finalCreatedAt ??= existing?.createdAt ? ensureDate(existing.createdAt) : new Date();\n finalResourceId ??= existing?.resourceId;\n }\n\n const { key, processedRecord } = processRecord(TABLE_WORKFLOW_SNAPSHOT, {\n namespace,\n workflow_name: workflowName,\n run_id: runId,\n resourceId: finalResourceId,\n snapshot,\n createdAt: finalCreatedAt,\n updatedAt: updatedAt ?? new Date(),\n });\n const batch = writeBatch(this.client, check);\n batch.set(key, JSON.stringify(processedRecord));\n await batch.exec();\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'PERSIST_WORKFLOW_SNAPSHOT', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n namespace,\n workflowName,\n runId,\n },\n },\n error,\n );\n }\n }\n\n public async loadWorkflowSnapshot(params: {\n namespace: string;\n workflowName: string;\n runId: string;\n }): Promise<WorkflowRunState | null> {\n const { namespace = 'workflows', workflowName, runId } = params;\n try {\n const record = await this.getSnapshotRecord(namespace, workflowName, runId);\n return record?.snapshot ?? null;\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LOAD_WORKFLOW_SNAPSHOT', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n namespace,\n workflowName,\n runId,\n },\n },\n error,\n );\n }\n }\n\n public async getWorkflowRunById({\n runId,\n workflowName,\n }: {\n runId: string;\n workflowName?: string;\n }): Promise<WorkflowRun | null> {\n try {\n const workflowNamePattern = workflowName ? escapeGlob(workflowName) : '*';\n const canonicalPattern = `${escapeGlob(getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: 'workflows' }))}:workflow_name:${workflowNamePattern}:run_id:${escapeGlob(runId)}`;\n const keys = [\n ...(await this.db.scanKeys(canonicalPattern)),\n ...(await this.db.scanKeys(`${canonicalPattern}:resourceId:*`)),\n ];\n\n if (keys.length === 0) {\n return null;\n }\n\n const results = await this.client.mGet(keys);\n const workflows = results\n .filter((data): data is string => data !== null)\n .map(\n data =>\n JSON.parse(data) as {\n workflow_name: string;\n run_id: string;\n snapshot: WorkflowRunState | string;\n createdAt: string | Date;\n updatedAt: string | Date;\n resourceId: string;\n },\n );\n\n const data = workflows.find(workflow => {\n if (!workflow) {\n return false;\n }\n\n const runIdMatch = workflow.run_id === runId;\n\n if (workflowName) {\n return runIdMatch && workflow.workflow_name === workflowName;\n }\n\n return runIdMatch;\n });\n\n if (!data) {\n return null;\n }\n\n return parseWorkflowRun(data as Record<string, unknown>);\n } catch (error) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'GET_WORKFLOW_RUN_BY_ID', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n namespace: 'workflows',\n runId,\n workflowName: workflowName || '',\n },\n },\n error,\n );\n }\n }\n\n public async deleteWorkflowRunById({\n runId,\n workflowName,\n fence,\n }: {\n runId: string;\n workflowName: string;\n fence?: RunFence;\n }): Promise<void> {\n const check = this.#runFenceCheck(fence, runId, 'deleteWorkflowRunById');\n try {\n const legacyKeys = await this.db.scanKeys(legacySnapshotKeyPattern('workflows', workflowName, runId));\n const batch = writeBatch(this.client, check);\n for (const key of [snapshotKey('workflows', workflowName, runId), ...legacyKeys]) batch.del(key);\n await batch.exec();\n } catch (error) {\n if (isRunFenceConflictError(error)) throw error;\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'DELETE_WORKFLOW_RUN_BY_ID', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n namespace: 'workflows',\n runId,\n workflowName,\n },\n },\n error,\n );\n }\n }\n\n public async listWorkflowRuns({\n workflowName,\n fromDate,\n toDate,\n perPage,\n page,\n resourceId,\n status,\n }: StorageListWorkflowRunsInput = {}): Promise<WorkflowRuns> {\n try {\n if (page !== undefined && page < 0) {\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_WORKFLOW_RUNS', 'INVALID_PAGE'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.USER,\n details: { page },\n },\n new Error('page must be >= 0'),\n );\n }\n\n const normalizedFrom = fromDate ? ensureDate(fromDate) : undefined;\n const normalizedTo = toDate ? ensureDate(toDate) : undefined;\n\n const pattern = workflowName\n ? `${escapeGlob(getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: 'workflows', workflow_name: workflowName }))}:*`\n : `${getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: 'workflows' })}:*`;\n const keys = await this.db.scanKeys(pattern);\n\n if (keys.length === 0) {\n return { runs: [], total: 0 };\n }\n\n const results = await this.client.mGet(keys);\n\n // A run can exist under both its canonical key and a legacy `:resourceId:` key; the canonical one wins.\n const recordsByRun = new Map<string, { record: Record<string, unknown>; legacy: boolean }>();\n results.forEach((data, index) => {\n if (data === null) return;\n const record = JSON.parse(data) as Record<string, unknown>;\n if (record === null || typeof record !== 'object' || !('workflow_name' in record)) return;\n const legacy =\n keys[index] !==\n snapshotKey(\n (record.namespace as string | undefined) ?? 'workflows',\n record.workflow_name as string,\n record.run_id as string,\n );\n const runKey = `${record.workflow_name}\\u0000${record.run_id}`;\n const current = recordsByRun.get(runKey);\n if (!current || (current.legacy && !legacy)) {\n recordsByRun.set(runKey, { record, legacy });\n }\n });\n\n let runs = [...recordsByRun.values()]\n .map(({ record }) => record)\n .filter(record => !workflowName || record.workflow_name === workflowName)\n .filter(record => !resourceId || record.resourceId === resourceId)\n .map(w => parseWorkflowRun(w))\n .filter(w => {\n if (normalizedFrom && w.createdAt < normalizedFrom) {\n return false;\n }\n if (normalizedTo && w.createdAt > normalizedTo) {\n return false;\n }\n if (status) {\n let snapshot = w.snapshot;\n if (typeof snapshot === 'string') {\n try {\n snapshot = JSON.parse(snapshot) as WorkflowRunState;\n } catch (e) {\n console.warn(`Failed to parse snapshot for workflow ${w.workflowName}: ${e}`);\n return false;\n }\n }\n return snapshot.status === status;\n }\n return true;\n })\n .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());\n\n const total = runs.length;\n\n if (typeof perPage === 'number' && typeof page === 'number') {\n const normalizedPerPage = normalizePerPage(perPage, Number.MAX_SAFE_INTEGER);\n const offset = page * normalizedPerPage;\n runs = runs.slice(offset, offset + normalizedPerPage);\n }\n\n return { runs, total };\n } catch (error) {\n if (error instanceof MastraError) {\n throw error;\n }\n throw new MastraError(\n {\n id: createStorageErrorId('REDIS', 'LIST_WORKFLOW_RUNS', 'FAILED'),\n domain: ErrorDomain.STORAGE,\n category: ErrorCategory.THIRD_PARTY,\n details: {\n namespace: 'workflows',\n workflowName: workflowName || '',\n resourceId: resourceId || '',\n },\n },\n error,\n );\n }\n }\n}\n","import type { RedisClient, RedisConfig } from './types';\n\nexport function isClientConfig(config: RedisConfig): config is RedisConfig & { client: RedisClient } {\n return 'client' in config;\n}\n\nexport function isConnectionStringConfig(config: RedisConfig): config is RedisConfig & { connectionString: string } {\n return 'connectionString' in config;\n}\n","import { MastraStorage } from '@mastra/core/storage';\nimport type { StorageDomains } from '@mastra/core/storage';\nimport { createClient } from 'redis';\n\nimport { StoreMemoryRedis } from './domains/memory';\nimport { ScoresRedis } from './domains/scores';\nimport { WorkflowsRedis } from './domains/workflows';\nimport type { RedisClient, RedisConfig } from './types';\nimport { isClientConfig, isConnectionStringConfig } from './utils';\n\n/**\n * Redis storage adapter for Mastra.\n *\n * Provides storage functionality for direct Redis connections using the official redis package.\n *\n * Access domain-specific storage via `getStore()`:\n *\n * @example\n * ```typescript\n * // Using connection string\n * const storage = new RedisStore({\n * id: 'my-store',\n * connectionString: 'redis://localhost:6379',\n * });\n *\n * // Using host/port\n * const storage = new RedisStore({\n * id: 'my-store',\n * host: 'localhost',\n * port: 6379,\n * password: 'secret',\n * });\n *\n * // Access memory domain\n * const memory = await storage.getStore('memory');\n * await memory?.saveThread({ thread });\n *\n * // Access workflows domain\n * const workflows = await storage.getStore('workflows');\n * await workflows?.persistWorkflowSnapshot({ workflowName, runId, snapshot });\n * ```\n *\n * @example\n * ```typescript\n * // Using a pre-configured client for advanced features\n * import { createClient } from 'redis';\n *\n * const client = createClient({\n * url: 'redis://localhost:6379',\n * socket: {\n * reconnectStrategy: (retries) => Math.min(retries * 50, 2000),\n * },\n * });\n * await client.connect();\n *\n * const storage = new RedisStore({\n * id: 'my-store',\n * client,\n * });\n * ```\n */\nexport class RedisStore extends MastraStorage {\n private client: RedisClient;\n private shouldManageConnection: boolean;\n public stores: StorageDomains;\n\n constructor(config: RedisConfig) {\n super({ id: config.id, name: 'Redis', disableInit: config.disableInit });\n\n const { client, shouldManageConnection } = this.createClient(config);\n this.client = client;\n this.shouldManageConnection = shouldManageConnection;\n\n this.stores = {\n scores: new ScoresRedis({ client: this.client }),\n workflows: new WorkflowsRedis({ client: this.client }),\n memory: new StoreMemoryRedis({ client: this.client }),\n };\n }\n\n public override async init(): Promise<void> {\n if (this.shouldManageConnection && !this.client.isOpen) {\n await this.client.connect();\n }\n await super.init();\n }\n\n public getClient(): RedisClient {\n return this.client;\n }\n\n public async close(): Promise<void> {\n if (this.shouldManageConnection && this.client.isOpen) {\n await this.client.quit();\n }\n }\n\n private createClient(config: RedisConfig): { client: RedisClient; shouldManageConnection: boolean } {\n if (isClientConfig(config)) {\n return { client: config.client, shouldManageConnection: false };\n }\n\n if (isConnectionStringConfig(config)) {\n if (!config.connectionString?.trim()) {\n throw new Error('RedisStore: connectionString is required and cannot be empty.');\n }\n return {\n client: createClient({ url: config.connectionString }) as RedisClient,\n shouldManageConnection: true,\n };\n }\n\n if (!config.host?.trim()) {\n throw new Error('RedisStore: host is required and cannot be empty.');\n }\n\n const url = this.createClientUrl({\n ...config,\n db: config.db ?? 0,\n port: config.port ?? 6379,\n });\n\n return {\n client: createClient({ url }) as RedisClient,\n shouldManageConnection: true,\n };\n }\n\n private createClientUrl(config: { host: string; password?: string; port: number; db: number }): string {\n const encodedPassword = config.password ? encodeURIComponent(config.password) : null;\n\n if (config.password) {\n return `redis://:${encodedPassword}@${config.host}:${config.port || 6379}/${config.db || 0}`;\n }\n\n return `redis://${config.host}:${config.port || 6379}/${config.db || 0}`;\n }\n}\n","import { MastraServerCache } from '@mastra/core/cache';\n\nexport interface RedisClient {\n get(key: string): Promise<unknown>;\n set(key: string, value: unknown, ...args: unknown[]): Promise<unknown>;\n llen(key: string): Promise<number>;\n rpush(key: string, ...values: unknown[]): Promise<number>;\n lrange(key: string, start: number, stop: number): Promise<unknown[]>;\n del(...keys: string[]): Promise<number>;\n expire(key: string, seconds: number): Promise<number | boolean>;\n scan(cursor: string | number, ...args: unknown[]): Promise<[string | number, string[]]>;\n incr(key: string): Promise<number>;\n /**\n * Server-side Lua scripting. Optional: clients without it fall back to the\n * two-round-trip `increment` + `listPush` path for `listPushIndexed`.\n */\n eval?(...args: unknown[]): Promise<unknown>;\n}\n\nexport interface RedisServerCacheOptions {\n keyPrefix?: string;\n /**\n * Lifetime of cached values in seconds, refreshed on every write. Defaults to\n * 300. `0` means no expiry.\n *\n * A cache list and its index counter always share this TTL: if the counter\n * outlived the list, index allocation would restart at 0 while subscribers\n * still expect the previous numbering, and every new frame would be dropped\n * as a duplicate.\n */\n ttlSeconds?: number;\n /**\n * Run a Lua script with the given KEYS and ARGV. Defaults to the ioredis\n * calling convention (`eval(script, numKeys, ...keys, ...args)`); use\n * `nodeRedisPreset` / `upstashPreset` for those clients.\n */\n evalScript?: (client: RedisClient, script: string, keys: string[], args: string[]) => Promise<unknown>;\n setWithExpiry?: (client: RedisClient, key: string, value: unknown, seconds: number) => Promise<unknown>;\n scanKeys?: (\n client: RedisClient,\n cursor: string | number,\n pattern: string,\n count: number,\n ) => Promise<[string | number, string[]]>;\n getListLength?: (client: RedisClient, key: string) => Promise<number>;\n pushToList?: (client: RedisClient, key: string, value: unknown) => Promise<number>;\n getListRange?: (client: RedisClient, key: string, start: number, stop: number) => Promise<unknown[]>;\n}\n\nconst defaultSetWithExpiry = (client: RedisClient, key: string, value: unknown, seconds: number): Promise<unknown> => {\n return client.set(key, value, 'EX', seconds);\n};\n\nconst defaultScanKeys = (\n client: RedisClient,\n cursor: string | number,\n pattern: string,\n count: number,\n): Promise<[string | number, string[]]> => {\n return client.scan(cursor, 'MATCH', pattern, 'COUNT', count);\n};\n\nconst defaultGetListLength = (client: RedisClient, key: string): Promise<number> => {\n return client.llen(key);\n};\n\nconst defaultPushToList = (client: RedisClient, key: string, value: unknown): Promise<number> => {\n return client.rpush(key, value);\n};\n\nconst defaultGetListRange = (client: RedisClient, key: string, start: number, stop: number): Promise<unknown[]> => {\n return client.lrange(key, start, stop);\n};\n\nconst defaultEvalScript = (client: RedisClient, script: string, keys: string[], args: string[]): Promise<unknown> => {\n return client.eval!(script, keys.length, ...keys, ...args);\n};\n\n/**\n * Atomic \"allocate index + append + refresh TTL\" used by `listPushIndexed`.\n *\n * KEYS[1] = list key, KEYS[2] = counter key\n * ARGV[1] = JSON-serialized object WITHOUT an `index` property\n * ARGV[2] = TTL in seconds (0 = no expiry), applied to BOTH keys\n\n *\n * The index is spliced into the JSON text as the first property rather than\n * decoded/re-encoded with cjson, which would silently alter large integers\n * and sparse arrays. `JSON.stringify` of an object always starts with `{`,\n * so `{...}` → `{\"index\":N,...}` and `{}` → `{\"index\":N}`.\n */\nexport const LIST_PUSH_INDEXED_SCRIPT = `\nlocal i = redis.call('INCR', KEYS[2]) - 1\nlocal body = ARGV[1]\nlocal doc\nif #body <= 2 then\n doc = '{\"index\":' .. i .. '}'\nelse\n doc = '{\"index\":' .. i .. ',' .. string.sub(body, 2)\nend\nredis.call('RPUSH', KEYS[1], doc)\nlocal ttl = tonumber(ARGV[2])\nif ttl > 0 then\n redis.call('EXPIRE', KEYS[1], ttl)\n redis.call('EXPIRE', KEYS[2], ttl)\nend\nreturn i\n`.trim();\n\nexport class RedisServerCache extends MastraServerCache {\n private client: RedisClient;\n private keyPrefix: string;\n private ttlSeconds: number;\n private setWithExpiry: (client: RedisClient, key: string, value: unknown, seconds: number) => Promise<unknown>;\n private scanKeys: (\n client: RedisClient,\n cursor: string | number,\n pattern: string,\n count: number,\n ) => Promise<[string | number, string[]]>;\n private getListLength: (client: RedisClient, key: string) => Promise<number>;\n private pushToList: (client: RedisClient, key: string, value: unknown) => Promise<number>;\n private getListRange: (client: RedisClient, key: string, start: number, stop: number) => Promise<unknown[]>;\n private evalScript: (client: RedisClient, script: string, keys: string[], args: string[]) => Promise<unknown>;\n /** Set once scripting is known to be unusable (no `eval`, or Cluster CROSSSLOT). */\n private scriptingDisabled: boolean;\n\n constructor(config: { client: RedisClient }, options: RedisServerCacheOptions = {}) {\n super({ name: 'RedisServerCache' });\n\n this.client = config.client;\n this.keyPrefix = options.keyPrefix ?? 'mastra:cache:';\n this.ttlSeconds = options.ttlSeconds ?? 300;\n this.setWithExpiry = options.setWithExpiry ?? defaultSetWithExpiry;\n this.scanKeys = options.scanKeys ?? defaultScanKeys;\n this.getListLength = options.getListLength ?? defaultGetListLength;\n this.pushToList = options.pushToList ?? defaultPushToList;\n this.getListRange = options.getListRange ?? defaultGetListRange;\n this.evalScript = options.evalScript ?? defaultEvalScript;\n this.scriptingDisabled = typeof this.client.eval !== 'function';\n }\n\n private getKey(key: string): string {\n return `${this.keyPrefix}${key}`;\n }\n\n private serialize(value: unknown): string {\n return JSON.stringify(value);\n }\n\n private deserialize(value: unknown): unknown {\n if (typeof value === 'string') {\n try {\n return JSON.parse(value);\n } catch {\n return value;\n }\n }\n return value;\n }\n\n async get(key: string): Promise<unknown> {\n const fullKey = this.getKey(key);\n const value = await this.client.get(fullKey);\n if (value === null) {\n return null;\n }\n return this.deserialize(value);\n }\n\n async set(key: string, value: unknown, ttlMs?: number): Promise<void> {\n const fullKey = this.getKey(key);\n const serialized = this.serialize(value);\n const overrideSeconds = ttlMs !== undefined ? Math.max(1, Math.ceil(ttlMs / 1000)) : undefined;\n const effectiveSeconds = overrideSeconds ?? this.ttlSeconds;\n if (effectiveSeconds > 0) {\n await this.setWithExpiry(this.client, fullKey, serialized, effectiveSeconds);\n } else {\n await this.client.set(fullKey, serialized);\n }\n }\n\n async listLength(key: string): Promise<number> {\n const fullKey = this.getKey(key);\n return this.getListLength(this.client, fullKey);\n }\n\n async listPush(key: string, value: unknown): Promise<void> {\n const fullKey = this.getKey(key);\n const serialized = this.serialize(value);\n await this.pushToList(this.client, fullKey, serialized);\n\n if (this.ttlSeconds > 0) {\n await this.client.expire(fullKey, this.ttlSeconds);\n }\n }\n\n async listFromTo(key: string, from: number, to: number = -1): Promise<unknown[]> {\n const fullKey = this.getKey(key);\n const values = await this.getListRange(this.client, fullKey, from, to);\n return values.map(v => this.deserialize(v));\n }\n\n async delete(key: string): Promise<void> {\n const fullKey = this.getKey(key);\n await this.client.del(fullKey);\n }\n\n async clear(): Promise<void> {\n const pattern = `${this.keyPrefix}*`;\n let cursor: string | number = '0';\n\n do {\n const [nextCursor, keys] = await this.scanKeys(this.client, cursor, pattern, 100);\n\n if (keys.length > 0) {\n await this.client.del(...keys);\n }\n\n cursor = nextCursor;\n } while (cursor !== '0' && cursor !== 0);\n }\n\n async increment(key: string): Promise<number> {\n const fullKey = this.getKey(key);\n const value = await this.client.incr(fullKey);\n\n if (this.ttlSeconds > 0) {\n await this.client.expire(fullKey, this.ttlSeconds);\n }\n\n return value;\n }\n\n /**\n * Single round-trip index allocation + list append + TTL refresh via Lua.\n * This is the durable stream per-chunk hot path (issue #22477).\n */\n async listPushIndexed(\n listKey: string,\n counterKey: string,\n value: Record<string, unknown>,\n ttlMs?: number,\n ): Promise<number> {\n // A cache without expiry stays that way; otherwise `ttlMs` can only raise\n // the default.\n const ttlSeconds = this.ttlSeconds > 0 ? Math.max(this.ttlSeconds, Math.ceil((ttlMs ?? 0) / 1000)) : 0;\n if (this.scriptingDisabled) {\n return this.composedPushIndexed(listKey, counterKey, value, ttlSeconds);\n }\n\n const { index: _ignored, ...body } = value;\n try {\n const result = await this.evalScript(\n this.client,\n LIST_PUSH_INDEXED_SCRIPT,\n [this.getKey(listKey), this.getKey(counterKey)],\n [this.serialize(body), String(ttlSeconds)],\n );\n return Number(result);\n } catch (error) {\n // Untagged list and counter keys can still hit CROSSSLOT. Keep the\n // fallback for callers that have not opted into tagged cache keys.\n if (error instanceof Error && error.message.includes('CROSSSLOT')) {\n this.scriptingDisabled = true;\n this.logger.warn(\n '[RedisServerCache] listPushIndexed script rejected with CROSSSLOT; falling back to increment + listPush',\n );\n return this.composedPushIndexed(listKey, counterKey, value, ttlSeconds);\n }\n throw error;\n }\n }\n\n /**\n * `INCR` then `RPUSH`, each followed by the same `EXPIRE` the script applies.\n * Not atomic: concurrent writers can interleave, so a list position may not\n * match its index (Redis Cluster atomicity is tracked in COR-1491).\n */\n private async composedPushIndexed(\n listKey: string,\n counterKey: string,\n value: Record<string, unknown>,\n ttlSeconds: number,\n ): Promise<number> {\n const fullCounterKey = this.getKey(counterKey);\n const index = (await this.client.incr(fullCounterKey)) - 1;\n if (ttlSeconds > 0) await this.client.expire(fullCounterKey, ttlSeconds);\n\n const fullListKey = this.getKey(listKey);\n await this.pushToList(this.client, fullListKey, this.serialize({ ...value, index }));\n if (ttlSeconds > 0) await this.client.expire(fullListKey, ttlSeconds);\n return index;\n }\n}\n\nexport const upstashPreset: Pick<RedisServerCacheOptions, 'setWithExpiry' | 'scanKeys' | 'evalScript'> = {\n setWithExpiry: (client, key, value, seconds) => client.set(key, value, { ex: seconds } as any),\n scanKeys: (client, cursor, pattern, count) =>\n client.scan(cursor, { match: pattern, count } as any) as Promise<[string | number, string[]]>,\n evalScript: (client, script, keys, args) => client.eval!(script, keys, args),\n};\n\n// node-redis v4+ exposes Redis multi-word commands as camelCase only\n// (lLen / rPush / lRange), not as lowercase aliases. The defaults in this\n// module use ioredis-style lowercase, so node-redis users need adapters that\n// forward to the camelCase methods. Single-word commands (set, scan, del,\n// expire, incr, get) work in lowercase under node-redis and don't need\n// adapters; the existing setWithExpiry / scanKeys adapters only exist to\n// reshape arguments, not to alias method names.\nexport const nodeRedisPreset: Pick<\n RedisServerCacheOptions,\n 'setWithExpiry' | 'scanKeys' | 'getListLength' | 'pushToList' | 'getListRange' | 'evalScript'\n> = {\n setWithExpiry: (client, key, value, seconds) => client.set(key, value, { EX: seconds } as any),\n scanKeys: (client, cursor, pattern, count) =>\n client.scan(cursor, { MATCH: pattern, COUNT: count } as any) as Promise<[string | number, string[]]>,\n getListLength: (client, key) => (client as any).lLen(key),\n pushToList: (client, key, value) => (client as any).rPush(key, value),\n getListRange: (client, key, start, stop) => (client as any).lRange(key, start, stop),\n evalScript: (client, script, keys, args) => client.eval!(script, { keys, arguments: args }),\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAeA,SAAgB,OAAO,WAAwB,MAAuC;CAWpF,OAAO,GAAG,UAAU,GAVH,OAAO,QAAQ,IAAI,CAAC,CAClC,QAAQ,CAAC,GAAG,WAAW,UAAU,KAAA,CAAS,CAAC,CAC3C,KAAK,CAAC,KAAK,WAAW;EACrB,IAAI,SAAS,OAAO,UAAU,UAC5B,OAAO,GAAG,IAAI,GAAG,KAAK,UAAU,KAAK;EAGvC,OAAO,GAAG,IAAI,GAAG;CACnB,CAE4B,CAAC,CAAC,KAAK,GAAG;AAC1C;;;;AAKA,SAAgB,cAAc,WAAwB,QAAiC;CACrF,IAAI;CAEJ,IAAI,cAAcA,qBAAAA,gBAChB,MAAM,OAAO,WAAW;EAAE,UAAU,OAAO;EAAU,IAAI,OAAO;CAAG,CAAC;MAC/D,IAAI,cAAcC,qBAAAA,yBACvB,MAAM,OAAO,WAAW;EACtB,WAAW,OAAO,aAAa;EAC/B,eAAe,OAAO;EACtB,QAAQ,OAAO;CACjB,CAAC;MAED,MAAM,OAAO,WAAW,EAAE,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