Skip to content

Mastra native memory retains conversation protection - #874

Merged
zlareb1 merged 5 commits into
mainfrom
feat/mastra-memory-session-protection
Nov 18, 2026
Merged

zlareb1 merged 5 commits into
mainfrom
feat/mastra-memory-session-protection

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Related: #849

Related: #878

Changelog category (leave one):

  • Improvement

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

Mastra native memory accepts an app-owned conversation capture and retains its protection across asynchronous callbacks, batches and conditional updates.

Why

Native memory calls the Loom client directly. A callback can outlive the context that created it, so choosing its family after hashing or a credential await can lose the family that supplied its content. The storage snapshots the supplied capture and contributing client families before asynchronous work, then carries that scope through every request and continuation. Completed callbacks retain their contributors for later callbacks, so rebinding cannot discard an earlier family.

Scope and risk

LoomMemoryStorageOptions.capture accepts an optional SessionCapture. With it, native callbacks are automatic: protected families refuse before dispatch, response marks commit to every contributing family before content returns, and a marked write refuses acknowledgement. Batches, pagination, conditional-update retries and thread timestamp updates stop when protection changes. Earlier writes in a refused batch can remain stored; the adapter provides no transaction.

Calls without capture remain explicitly owned CRUD and retain response observers. Model-selected loomTools calls remain explicit. Resource, store and thread IDs identify rows; they cannot restore protection authority. Each automatic storage instance belongs to one end-user and one complete conversation family, whose durable state the app restores.

The sender inventory retains the same classifications and body producers. The adapter creates no family or durable backend and makes no client capability declaration.

The deep-key cost check also uses the existing CPU measurement context around its deliberate trace function. This excludes unrelated heap collections while retaining the input sizes, samples and strict cost bound.

Compatibility, rollout and rollback

  • API: optional LoomMemoryStorageOptions.capture; existing explicit CRUD remains supported.
  • Wire and storage: no operation, header, row, migration or stored-format change.
  • Operator steps: none. Apps using automatic native memory supply the complete restored conversation capture.
  • Rollback: reverting leaves stored threads, messages and resource working memory unchanged.

Tests

  • The original 21 selected cases passed. Their original RED run reproduced 18 protection failures while three ordinary-operation controls passed. Scoped formatting and local binding-name fixes have a matching TypeScript AST proof.
  • Two additional regressions fail before the completed-callback retention fix: a protected earlier contributor does not suppress the next send after rebinding, and a later marked recall returns without committing protection to that earlier contributor. The durable-mark case checks the actual mark-entry and output promises, with no timing assumption.
  • The 23-case cohort passed on the prior composition in 75 milliseconds, including the original controls and both completed-callback regressions. The directly changed sender-inventory assertion passed in 4.78 seconds on that head. The adapter, tests and inventory are unchanged in this composition; these checks were not rerun.
  • Coverage includes real native Memory calls, captured clients, constructor and callback rebinding, credential waits, durable multi-family marking, unavailable protection storage, marked writes, listing continuation, conditional-update conflicts, pending thread touches and explicit CRUD.
  • Full SDK, unit and integration gates run in GitHub CI and must pass on the final head. All required final-head GitHub CI checks and applicable workflows passed; fresh independent whole-diff and current-main composition reviews have no findings or nits. No broad local suite ran.
cd sdk/typescript
node node_modules/vitest/vitest.mjs run --pool=forks --maxWorkers=1 --no-file-parallelism src/integrations/mastra-memory.test.ts -t 'LoomMemoryStorage conversation capture ownership|serves native Memory save, recall, working-memory and delete methods without a vector store|reapplies a partial metadata update after a concurrent title change without clobbering it'
  • The changed deep-key cost node and its existing measurement-state restoration control passed together: two tests in 2.53 seconds. Their sources are unchanged from the published cost-check fix. The earlier unit-shard failure remains recorded; the final-head unit shard and remaining CI checks passed.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated: An agent wrote the change; fresh independent whole-diff review and final-head CI are required before merge.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • Required final-head CI passes.
  • Fresh whole-diff review has no findings or nits.
  • Sender fingerprints and source hash are updated with unchanged classifications.
  • Docs explain caller-owned capture, explicit CRUD and partial-write behavior; no migration, plugin, frontend or runtime change.
  • Public text follows repository copy rules.
  • Commits are signed off.

…tra-memory-session-protection

Signed-off-by: zlareb1 <[email protected]>

# Conflicts:
#	tests/unit/session_content_senders.json
Keep the deliberate trace function inside the existing CPU measurement context so collections of prior test heaps do not enter the ratio. Preserve every input and assertion.

Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 merged commit 30b0d13 into main Nov 18, 2026
45 checks passed
@zlareb1 zlareb1 mentioned this pull request Nov 18, 2026
6 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant