[PR #157] [CLOSED] Fix memory loading for StateBackend invocations #164

Closed
opened 2026-02-16 06:17:19 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/deepagentsjs/pull/157
Author: @maahir30
Created: 1/30/2026
Status: Closed

Base: mainHead: fix-memory-load-state


📝 Commits (1)

  • 323405c Update memory middleware/tests

📊 Changes

2 files changed (+93 additions, -27 deletions)

View changed files

📝 libs/deepagents/src/middleware/memory.test.ts (+37 -8)
📝 libs/deepagents/src/middleware/memory.ts (+56 -19)

📄 Description

Summary

  • Fix memory loading when using the StateBackend by lazily loading memory inside wrapModelCall once files are actually present in request state.
  • Cache loaded memory so the very first model call sees the memory section in the system prompt.
  • Update memory middleware tests to await async wrapModelCall and add a StateBackend‑specific regression test.

Problem & reasoning

When using the StateBackend, memory files like /AGENTS.md are provided at invoke time via the files input. The memory middleware tries to load memory in beforeAgent, but at that moment the input files are not yet merged into state, so the backend can’t read them and memoryContents becomes {}. Later in the run, tools can read /AGENTS.md because the files are merged by then, but the system prompt already missed the memory content.

This issue does not appear with the StoreBackend because the file already exists in the persistent store before the run starts.

We considered “moving beforeAgent later,” but that would require changing LangGraph’s invocation pipeline. Instead, we implemented the smallest fix: lazy‑load memory right before the model call, when request state already includes the input files. We cache the loaded memory so it’s reused for subsequent calls in the same run.

Fix (conceptual)

  • Keep beforeAgent as‑is for backends that already have files.
  • If memory is still empty at wrapModelCall time and files are present, load memory then.
  • Inject the loaded memory into the system prompt for the very first model call.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langchain-ai/deepagentsjs/pull/157 **Author:** [@maahir30](https://github.com/maahir30) **Created:** 1/30/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `fix-memory-load-state` --- ### 📝 Commits (1) - [`323405c`](https://github.com/langchain-ai/deepagentsjs/commit/323405cc96ac42f9662b9637e7776ead26ce2d3a) Update memory middleware/tests ### 📊 Changes **2 files changed** (+93 additions, -27 deletions) <details> <summary>View changed files</summary> 📝 `libs/deepagents/src/middleware/memory.test.ts` (+37 -8) 📝 `libs/deepagents/src/middleware/memory.ts` (+56 -19) </details> ### 📄 Description ## Summary - Fix memory loading when using the `StateBackend` by lazily loading memory inside `wrapModelCall` once `files` are actually present in request state. - Cache loaded memory so the very first model call sees the memory section in the system prompt. - Update memory middleware tests to await async `wrapModelCall` and add a StateBackend‑specific regression test. ## Problem & reasoning When using the `StateBackend`, memory files like `/AGENTS.md` are provided **at invoke time** via the `files` input. The memory middleware tries to load memory in `beforeAgent`, but **at that moment the input files are not yet merged into state**, so the backend can’t read them and `memoryContents` becomes `{}`. Later in the run, tools can read `/AGENTS.md` because the files are merged by then, but the system prompt already missed the memory content. This issue does **not** appear with the StoreBackend because the file already exists in the persistent store before the run starts. We considered “moving `beforeAgent` later,” but that would require changing LangGraph’s invocation pipeline. Instead, we implemented the smallest fix: **lazy‑load memory right before the model call**, when request state already includes the input files. We cache the loaded memory so it’s reused for subsequent calls in the same run. ## Fix (conceptual) - Keep `beforeAgent` as‑is for backends that already have files. - If memory is still empty at `wrapModelCall` time and files are present, load memory then. - Inject the loaded memory into the system prompt for the very first model call. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 06:17:19 -05:00
yindo closed this issue 2026-02-16 06:17:19 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagentsjs#164