spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

MessageChatMemoryAdvisor,MessageWindowChatMemory currently performs full message rewrites on every before/after — suggest incremental updates and DB‑side windowing

Open wuyuandu opened this issue 5 months ago • 3 comments

Currently, MessageChatMemoryAdvisor performs a full overwrite of conversation window messages on every before/after operation, and MessageWindowChatMemory also causes duplicate DB writes. Could this be changed to incremental append? Window size control could be handled by DB query limits (e.g., LIMIT), which would also preserve full message history. What's the design rationale behind the current approach? Doesn't this introduce unnecessary DB write overhead?

wuyuandu avatar Jun 08 '26 08:06 wuyuandu

Yes, that seems to be by design. ChatMemory in Spring AI is intended to represent the current memory window used for prompt augmentation, not a durable full conversation history. If an application needs full chat history, it should persist messages separately in its own append-only history table. The current MessageWindowChatMemory stores and rewrites the active window state, which explains the overwrite behavior, although from a production persistence perspective an append-only design with query-time windowing would indeed reduce write amplification and preserve history.

wolf8334 avatar Jun 08 '26 08:06 wolf8334

Thanks for raising this.

I had a look at this area as well. My understanding is that the current behavior comes from the existing ChatMemoryRepository abstraction, where MessageWindowChatMemory loads the current conversation messages, applies the memory-window rules, and then saves the processed window back via saveAll.

I agree that incremental append + DB-side windowing could reduce write overhead for persistent repositories, but it may also require a broader design change to the repository contract, especially because the current ChatMemory abstraction is focused on managing the active memory window rather than preserving complete chat history.

Maybe a useful first step would be to clarify the intended design direction:

  • keep the current replacement-style repository contract,
  • add optional repository methods for append/windowed reads,
  • or document that full chat history should be stored separately from ChatMemory.

Happy to help with tests or a focused change once the preferred direction is clear.

codingkiddo avatar Jun 08 '26 09:06 codingkiddo

Perhaps you can review the https://github.com/spring-ai-community/spring-ai-session.
The intention is for SpringAI 2.1 the spring-ai-session or what it came out of it to replace/deprecate the existing ChatMemory/Repository/Advisor.

tzolov avatar Jun 09 '26 07:06 tzolov