@mastra/redis 1.4.11-alpha.0 → 1.4.12-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -138,7 +138,7 @@ var RedisDB = class {
138
138
  * generation comes from the clock, so it still outranks every fence the expired
139
139
  * record issued, and a write checked against a missing record is rejected.
140
140
  */
141
- const RUN_CLAIM_RETENTION_MS = 10080 * 60 * 1e3;
141
+ const RUN_CLAIM_RETENTION_MS = 6048e5;
142
142
  function runClaimKey(claims, runId) {
143
143
  return `${claims}:run_id:${runId}`;
144
144
  }
@@ -1361,12 +1361,13 @@ var ScoresRedis = class extends ScoresStorage {
1361
1361
  }
1362
1362
  const now = /* @__PURE__ */ new Date();
1363
1363
  const id = crypto.randomUUID();
1364
- const { key, processedRecord } = processRecord(TABLE_SCORERS, {
1364
+ const scoreWithId = {
1365
1365
  ...validatedScore,
1366
1366
  id,
1367
1367
  createdAt: now,
1368
1368
  updatedAt: now
1369
- });
1369
+ };
1370
+ const { key, processedRecord } = processRecord(TABLE_SCORERS, scoreWithId);
1370
1371
  try {
1371
1372
  await this.client.set(key, JSON.stringify(processedRecord));
1372
1373
  return { score: {
@@ -1914,12 +1915,13 @@ var RedisStore = class extends MastraStorage {
1914
1915
  };
1915
1916
  }
1916
1917
  if (!config.host?.trim()) throw new Error("RedisStore: host is required and cannot be empty.");
1918
+ const url = this.createClientUrl({
1919
+ ...config,
1920
+ db: config.db ?? 0,
1921
+ port: config.port ?? 6379
1922
+ });
1917
1923
  return {
1918
- client: createClient({ url: this.createClientUrl({
1919
- ...config,
1920
- db: config.db ?? 0,
1921
- port: config.port ?? 6379
1922
- }) }),
1924
+ client: createClient({ url }),
1923
1925
  shouldManageConnection: true
1924
1926
  };
1925
1927
  }
@@ -1950,11 +1952,12 @@ const defaultEvalScript = (client, script, keys, args) => {
1950
1952
  return client.eval(script, keys.length, ...keys, ...args);
1951
1953
  };
1952
1954
  /**
1953
- * Atomic "allocate index + append + refresh TTLs" used by `listPushIndexed`.
1955
+ * Atomic "allocate index + append + refresh TTL" used by `listPushIndexed`.
1954
1956
  *
1955
1957
  * KEYS[1] = list key, KEYS[2] = counter key
1956
1958
  * ARGV[1] = JSON-serialized object WITHOUT an `index` property
1957
- * ARGV[2] = TTL in seconds (0 = no expiry)
1959
+ * ARGV[2] = TTL in seconds (0 = no expiry), applied to BOTH keys
1960
+
1958
1961
  *
1959
1962
  * The index is spliced into the JSON text as the first property rather than
1960
1963
  * decoded/re-encoded with cjson, which would silently alter large integers
@@ -2065,24 +2068,41 @@ var RedisServerCache = class extends MastraServerCache {
2065
2068
  }
2066
2069
  /**
2067
2070
  * Single round-trip index allocation + list append + TTL refresh via Lua.
2068
- * This is the durable stream per-chunk hot path (issue #22477): the composed
2069
- * default costs four awaited commands (INCR, EXPIRE, RPUSH, EXPIRE).
2071
+ * This is the durable stream per-chunk hot path (issue #22477).
2070
2072
  */
2071
- async listPushIndexed(listKey, counterKey, value) {
2072
- if (this.scriptingDisabled) return super.listPushIndexed(listKey, counterKey, value);
2073
+ async listPushIndexed(listKey, counterKey, value, ttlMs) {
2074
+ const ttlSeconds = this.ttlSeconds > 0 ? Math.max(this.ttlSeconds, Math.ceil((ttlMs ?? 0) / 1e3)) : 0;
2075
+ if (this.scriptingDisabled) return this.composedPushIndexed(listKey, counterKey, value, ttlSeconds);
2073
2076
  const { index: _ignored, ...body } = value;
2074
2077
  try {
2075
- const result = await this.evalScript(this.client, LIST_PUSH_INDEXED_SCRIPT, [this.getKey(listKey), this.getKey(counterKey)], [this.serialize(body), String(this.ttlSeconds)]);
2078
+ const result = await this.evalScript(this.client, LIST_PUSH_INDEXED_SCRIPT, [this.getKey(listKey), this.getKey(counterKey)], [this.serialize(body), String(ttlSeconds)]);
2076
2079
  return Number(result);
2077
2080
  } catch (error) {
2078
2081
  if (error instanceof Error && error.message.includes("CROSSSLOT")) {
2079
2082
  this.scriptingDisabled = true;
2080
2083
  this.logger.warn("[RedisServerCache] listPushIndexed script rejected with CROSSSLOT; falling back to increment + listPush");
2081
- return super.listPushIndexed(listKey, counterKey, value);
2084
+ return this.composedPushIndexed(listKey, counterKey, value, ttlSeconds);
2082
2085
  }
2083
2086
  throw error;
2084
2087
  }
2085
2088
  }
2089
+ /**
2090
+ * `INCR` then `RPUSH`, each followed by the same `EXPIRE` the script applies.
2091
+ * Not atomic: concurrent writers can interleave, so a list position may not
2092
+ * match its index (Redis Cluster atomicity is tracked in COR-1491).
2093
+ */
2094
+ async composedPushIndexed(listKey, counterKey, value, ttlSeconds) {
2095
+ const fullCounterKey = this.getKey(counterKey);
2096
+ const index = await this.client.incr(fullCounterKey) - 1;
2097
+ if (ttlSeconds > 0) await this.client.expire(fullCounterKey, ttlSeconds);
2098
+ const fullListKey = this.getKey(listKey);
2099
+ await this.pushToList(this.client, fullListKey, this.serialize({
2100
+ ...value,
2101
+ index
2102
+ }));
2103
+ if (ttlSeconds > 0) await this.client.expire(fullListKey, ttlSeconds);
2104
+ return index;
2105
+ }
2086
2106
  };
2087
2107
  const upstashPreset = {
2088
2108
  setWithExpiry: (client, key, value, seconds) => client.set(key, value, { ex: seconds }),