@mastra/memory 1.5.0-alpha.0 → 1.5.1-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.
Files changed (50) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/dist/{chunk-DF7NDDSM.js → chunk-6PKWQ3GH.js} +28 -11
  3. package/dist/chunk-6PKWQ3GH.js.map +1 -0
  4. package/dist/{chunk-LLTHE64H.cjs → chunk-6XVTMLW4.cjs} +28 -11
  5. package/dist/chunk-6XVTMLW4.cjs.map +1 -0
  6. package/dist/index.cjs +1 -1
  7. package/dist/index.js +1 -1
  8. package/dist/{observational-memory-ZNTAIUGT.js → observational-memory-AJWSMZVP.js} +3 -3
  9. package/dist/{observational-memory-ZNTAIUGT.js.map → observational-memory-AJWSMZVP.js.map} +1 -1
  10. package/dist/{observational-memory-4PCXEZIS.cjs → observational-memory-Q5TO525O.cjs} +17 -17
  11. package/dist/{observational-memory-4PCXEZIS.cjs.map → observational-memory-Q5TO525O.cjs.map} +1 -1
  12. package/dist/processors/index.cjs +15 -15
  13. package/dist/processors/index.js +1 -1
  14. package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
  15. package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
  16. package/package.json +11 -11
  17. package/dist/chunk-DF7NDDSM.js.map +0 -1
  18. package/dist/chunk-LLTHE64H.cjs.map +0 -1
  19. package/dist/docs/SKILL.md +0 -54
  20. package/dist/docs/assets/SOURCE_MAP.json +0 -103
  21. package/dist/docs/references/docs-agents-agent-approval.md +0 -377
  22. package/dist/docs/references/docs-agents-agent-memory.md +0 -212
  23. package/dist/docs/references/docs-agents-network-approval.md +0 -275
  24. package/dist/docs/references/docs-agents-networks.md +0 -290
  25. package/dist/docs/references/docs-memory-memory-processors.md +0 -316
  26. package/dist/docs/references/docs-memory-message-history.md +0 -260
  27. package/dist/docs/references/docs-memory-observational-memory.md +0 -246
  28. package/dist/docs/references/docs-memory-overview.md +0 -45
  29. package/dist/docs/references/docs-memory-semantic-recall.md +0 -272
  30. package/dist/docs/references/docs-memory-storage.md +0 -261
  31. package/dist/docs/references/docs-memory-working-memory.md +0 -400
  32. package/dist/docs/references/reference-core-getMemory.md +0 -50
  33. package/dist/docs/references/reference-core-listMemory.md +0 -56
  34. package/dist/docs/references/reference-memory-clone-utilities.md +0 -199
  35. package/dist/docs/references/reference-memory-cloneThread.md +0 -130
  36. package/dist/docs/references/reference-memory-createThread.md +0 -68
  37. package/dist/docs/references/reference-memory-getThreadById.md +0 -24
  38. package/dist/docs/references/reference-memory-listThreads.md +0 -145
  39. package/dist/docs/references/reference-memory-memory-class.md +0 -147
  40. package/dist/docs/references/reference-memory-observational-memory.md +0 -565
  41. package/dist/docs/references/reference-processors-token-limiter-processor.md +0 -113
  42. package/dist/docs/references/reference-storage-dynamodb.md +0 -282
  43. package/dist/docs/references/reference-storage-libsql.md +0 -135
  44. package/dist/docs/references/reference-storage-mongodb.md +0 -262
  45. package/dist/docs/references/reference-storage-postgresql.md +0 -529
  46. package/dist/docs/references/reference-storage-upstash.md +0 -160
  47. package/dist/docs/references/reference-vectors-libsql.md +0 -305
  48. package/dist/docs/references/reference-vectors-mongodb.md +0 -295
  49. package/dist/docs/references/reference-vectors-pg.md +0 -408
  50. package/dist/docs/references/reference-vectors-upstash.md +0 -294
package/CHANGELOG.md CHANGED
@@ -1,5 +1,44 @@
1
1
  # @mastra/memory
2
2
 
3
+ ## 1.5.1-alpha.0
4
+
5
+ ### Patch Changes
6
+
7
+ - **Fixed memory leak in Observational Memory** ([#13425](https://github.com/mastra-ai/mastra/pull/13425))
8
+
9
+ Fixed several memory management issues that could cause OOM crashes in long-running processes with Observational Memory enabled:
10
+ - **Shared tokenizer**: The default Tiktoken encoder (~80-120 MB heap) is now shared across all OM instances instead of being allocated per request. This is the primary fix — previously each request allocated two encoders that persisted in memory due to async buffering promise retention.
11
+ - **Cleanup key fix**: Fixed a bug where reflection cycle IDs were not properly cleaned up due to using the wrong map key in `cleanupStaticMaps`.
12
+
13
+ - Fixed PostgreSQL deadlock when parallel agents with different threadIds share the same resourceId while using Observational Memory. Thread scope now requires a valid threadId and throws a clear error if one is missing. Also fixed the database lock ordering in synchronous observation to prevent lock inversions. ([#13436](https://github.com/mastra-ai/mastra/pull/13436))
14
+
15
+ - Updated dependencies [[`24284ff`](https://github.com/mastra-ai/mastra/commit/24284ffae306ddf0ab83273e13f033520839ef40), [`f5097cc`](https://github.com/mastra-ai/mastra/commit/f5097cc8a813c82c3378882c31178320cadeb655), [`71e237f`](https://github.com/mastra-ai/mastra/commit/71e237fa852a3ad9a50a3ddb3b5f3b20b9a8181c), [`13a291e`](https://github.com/mastra-ai/mastra/commit/13a291ebb9f9bca80befa0d9166b916bb348e8e9), [`397af5a`](https://github.com/mastra-ai/mastra/commit/397af5a69f34d4157f51a7c8da3f1ded1e1d611c), [`d4701f7`](https://github.com/mastra-ai/mastra/commit/d4701f7e24822b081b70f9c806c39411b1a712e7), [`2b40831`](https://github.com/mastra-ai/mastra/commit/2b40831dcca2275c9570ddf09b7f25ba3e8dc7fc), [`6184727`](https://github.com/mastra-ai/mastra/commit/6184727e812bf7a65cee209bacec3a2f5a16e923), [`6f6385b`](https://github.com/mastra-ai/mastra/commit/6f6385be5b33687cd21e71fc27e972e6928bb34c), [`14aba61`](https://github.com/mastra-ai/mastra/commit/14aba61b9cff76d72bc7ef6f3a83ae2c5d059193), [`dd9dd1c`](https://github.com/mastra-ai/mastra/commit/dd9dd1c9ae32ae79093f8c4adde1732ac6357233)]:
16
+ - @mastra/core@1.7.0-alpha.0
17
+
18
+ ## 1.5.0
19
+
20
+ ### Minor Changes
21
+
22
+ - Improved conversational continuity when the message window shrinks during Observational Memory activation. The agent now preserves its suggested next response and current task across activation, so it maintains context instead of losing track of the conversation. ([#13354](https://github.com/mastra-ai/mastra/pull/13354))
23
+
24
+ Also improved the Observer to capture user messages more faithfully, reduce repetitive observations, and treat the most recent user message as the highest-priority signal.
25
+
26
+ ### Patch Changes
27
+
28
+ - Improved Observational Memory priority handling. User messages and task completions are now always treated as high priority, ensuring the observer captures the most relevant context during conversations. ([#13329](https://github.com/mastra-ai/mastra/pull/13329))
29
+
30
+ - Improved Observational Memory activation to preserve more usable context after activation. Previously, activation could leave the agent with too much or too little context depending on how chunks aligned with the retention target. ([#13305](https://github.com/mastra-ai/mastra/pull/13305))
31
+ - Activation now lands closer to the retention target by biasing chunk selection to slightly overshoot rather than undershoot
32
+ - Added safeguards to prevent activation from consuming too much context (95% ceiling and 1000-token floor)
33
+ - When pending tokens exceed `blockAfter`, activation now aggressively reduces context to unblock the conversation
34
+ - `bufferActivation` now accepts absolute token values (>= 1000) in addition to ratios (0–1), giving more precise control over when activation triggers
35
+
36
+ - Observations no longer inflate token counts from degenerate LLM output. Runaway or repetitive observer/reflector output is automatically detected and retried, preventing excessive context usage after activation. ([#13354](https://github.com/mastra-ai/mastra/pull/13354))
37
+
38
+ - Updated dependencies [[`0d9efb4`](https://github.com/mastra-ai/mastra/commit/0d9efb47992c34aa90581c18b9f51f774f6252a5), [`7184d87`](https://github.com/mastra-ai/mastra/commit/7184d87c9237d26862f500ccfd0c9f9eadd38ddf), [`5caa13d`](https://github.com/mastra-ai/mastra/commit/5caa13d1b2a496e2565ab124a11de9a51ad3e3b9), [`940163f`](https://github.com/mastra-ai/mastra/commit/940163fc492401d7562301e6f106ccef4fefe06f), [`47892c8`](https://github.com/mastra-ai/mastra/commit/47892c85708eac348209f99f10f9a5f5267e11c0), [`45bb78b`](https://github.com/mastra-ai/mastra/commit/45bb78b70bd9db29678fe49476cd9f4ed01bfd0b), [`70eef84`](https://github.com/mastra-ai/mastra/commit/70eef84b8f44493598fdafa2980a0e7283415eda), [`d84e52d`](https://github.com/mastra-ai/mastra/commit/d84e52d0f6511283ddd21ed5fe7f945449d0f799), [`24b80af`](https://github.com/mastra-ai/mastra/commit/24b80af87da93bb84d389340181e17b7477fa9ca), [`608e156`](https://github.com/mastra-ai/mastra/commit/608e156def954c9604c5e3f6d9dfce3bcc7aeab0), [`2b2e157`](https://github.com/mastra-ai/mastra/commit/2b2e157a092cd597d9d3f0000d62b8bb4a7348ed), [`59d30b5`](https://github.com/mastra-ai/mastra/commit/59d30b5d0cb44ea7a1c440e7460dfb57eac9a9b5), [`453693b`](https://github.com/mastra-ai/mastra/commit/453693bf9e265ddccecef901d50da6caaea0fbc6), [`78d1c80`](https://github.com/mastra-ai/mastra/commit/78d1c808ad90201897a300af551bcc1d34458a20), [`c204b63`](https://github.com/mastra-ai/mastra/commit/c204b632d19e66acb6d6e19b11c4540dd6ad5380), [`742a417`](https://github.com/mastra-ai/mastra/commit/742a417896088220a3b5560c354c45c5ca6d88b9)]:
39
+ - @mastra/core@1.6.0
40
+ - @mastra/schema-compat@1.1.2
41
+
3
42
  ## 1.5.0-alpha.0
4
43
 
5
44
  ### Minor Changes
@@ -909,6 +909,13 @@ function extractReflectorListItems(content) {
909
909
  function validateCompression(reflectedTokens, targetThreshold) {
910
910
  return reflectedTokens < targetThreshold;
911
911
  }
912
+ var sharedDefaultEncoder;
913
+ function getDefaultEncoder() {
914
+ if (!sharedDefaultEncoder) {
915
+ sharedDefaultEncoder = new Tiktoken(o200k_base);
916
+ }
917
+ return sharedDefaultEncoder;
918
+ }
912
919
  var TokenCounter = class _TokenCounter {
913
920
  encoder;
914
921
  // Per-message overhead: accounts for role tokens, message framing, and separators.
@@ -918,7 +925,7 @@ var TokenCounter = class _TokenCounter {
918
925
  // Conversation-level overhead: system prompt framing, reply priming tokens, etc.
919
926
  static TOKENS_PER_CONVERSATION = 24;
920
927
  constructor(encoding) {
921
- this.encoder = new Tiktoken(encoding || o200k_base);
928
+ this.encoder = encoding ? new Tiktoken(encoding) : getDefaultEncoder();
922
929
  }
923
930
  /**
924
931
  * Count tokens in a plain string
@@ -1373,7 +1380,7 @@ var ObservationalMemory = class _ObservationalMemory {
1373
1380
  _ObservationalMemory.lastBufferedBoundary.delete(reflBufKey);
1374
1381
  _ObservationalMemory.asyncBufferingOps.delete(obsBufKey);
1375
1382
  _ObservationalMemory.asyncBufferingOps.delete(reflBufKey);
1376
- _ObservationalMemory.reflectionBufferCycleIds.delete(obsBufKey);
1383
+ _ObservationalMemory.reflectionBufferCycleIds.delete(reflBufKey);
1377
1384
  }
1378
1385
  }
1379
1386
  /**
@@ -1925,6 +1932,11 @@ Async buffering is enabled by default \u2014 this opt-out is only needed when us
1925
1932
  resourceId: resourceId ?? threadId
1926
1933
  };
1927
1934
  }
1935
+ if (!threadId) {
1936
+ throw new Error(
1937
+ `ObservationalMemory (scope: 'thread') requires a threadId, but received an empty value. This is a bug \u2014 getThreadContext should have caught this earlier.`
1938
+ );
1939
+ }
1928
1940
  return {
1929
1941
  threadId,
1930
1942
  resourceId: resourceId ?? threadId
@@ -2683,6 +2695,11 @@ ${suggestedResponse}
2683
2695
  resourceId: serialized.memoryInfo.resourceId
2684
2696
  };
2685
2697
  }
2698
+ if (this.scope === "thread") {
2699
+ throw new Error(
2700
+ `ObservationalMemory (scope: 'thread') requires a threadId, but none was found in RequestContext or MessageList. Ensure the agent is configured with Memory and a valid threadId is provided.`
2701
+ );
2702
+ }
2686
2703
  return null;
2687
2704
  }
2688
2705
  // ══════════════════════════════════════════════════════════════════════════
@@ -3759,13 +3776,6 @@ ${result.observations}` : result.observations;
3759
3776
  const newMessageIds = messagesToObserve.map((m) => m.id);
3760
3777
  const existingIds = freshRecord?.observedMessageIds ?? record.observedMessageIds ?? [];
3761
3778
  const allObservedIds = [.../* @__PURE__ */ new Set([...Array.isArray(existingIds) ? existingIds : [], ...newMessageIds])];
3762
- await this.storage.updateActiveObservations({
3763
- id: record.id,
3764
- observations: newObservations,
3765
- tokenCount: totalTokenCount,
3766
- lastObservedAt,
3767
- observedMessageIds: allObservedIds
3768
- });
3769
3779
  if (result.suggestedContinuation || result.currentTask) {
3770
3780
  const thread = await this.storage.getThreadById({ threadId });
3771
3781
  if (thread) {
@@ -3780,6 +3790,13 @@ ${result.observations}` : result.observations;
3780
3790
  });
3781
3791
  }
3782
3792
  }
3793
+ await this.storage.updateActiveObservations({
3794
+ id: record.id,
3795
+ observations: newObservations,
3796
+ tokenCount: totalTokenCount,
3797
+ lastObservedAt,
3798
+ observedMessageIds: allObservedIds
3799
+ });
3783
3800
  const actualTokensObserved = this.tokenCounter.countMessages(messagesToObserve);
3784
3801
  if (lastMessage?.id) {
3785
3802
  const endMarker = this.createObservationEndMarker({
@@ -5048,5 +5065,5 @@ ${unreflectedContent}` : freshRecord.bufferedReflection;
5048
5065
  };
5049
5066
 
5050
5067
  export { OBSERVATIONAL_MEMORY_DEFAULTS, OBSERVATION_CONTEXT_INSTRUCTIONS, OBSERVATION_CONTEXT_PROMPT, OBSERVATION_CONTINUATION_HINT, OBSERVER_SYSTEM_PROMPT, ObservationalMemory, TokenCounter, buildObserverPrompt, buildObserverSystemPrompt, extractCurrentTask, formatMessagesForObserver, hasCurrentTaskSection, optimizeObservationsForContext, parseObserverOutput };
5051
- //# sourceMappingURL=chunk-DF7NDDSM.js.map
5052
- //# sourceMappingURL=chunk-DF7NDDSM.js.map
5068
+ //# sourceMappingURL=chunk-6PKWQ3GH.js.map
5069
+ //# sourceMappingURL=chunk-6PKWQ3GH.js.map