feat(memory): add ConversationMemory trait and InMemoryConversation#1501
Open
Mattbusel wants to merge 1 commit into0xPlaygrounds:mainfrom
Open
feat(memory): add ConversationMemory trait and InMemoryConversation#1501Mattbusel wants to merge 1 commit into0xPlaygrounds:mainfrom
Mattbusel wants to merge 1 commit into0xPlaygrounds:mainfrom
Conversation
Introduces a pluggable memory abstraction for agents — the gap described in issue 0xPlaygrounds#373. - ConversationMemory trait: push / history / clear / len / is_empty all take &self so implementations are Arc-shareable across tasks - InMemoryConversation: bounded VecDeque, evicts oldest on overflow capacity=0 (or ::unbounded()) retains every message - SlidingWindowMemory: type alias for explicit window-size semantics - NoMemory: no-op default, matches current stateless Agent behaviour - 6 unit tests: push/retrieve, eviction, clear, unbounded, NoMemory, Arc shared ownership Backends (Redis, Postgres, vector store) can implement the trait without touching rig-core. Designed and implemented by Matthew Busel.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #373
This PR implements the
ConversationMemoryabstraction proposed in issue #373.Problem
Rig agents are currently stateless per call —
chat()takes aVec<Message>that the caller must manage manually. There is no standard way to plug in a memory backend. Issue #373 has been open since early 2025; the proposed interface was aMemoryStrategytrait withfetch_memory/insert_memory. This PR provides a concrete implementation.What this adds
rig::memory::ConversationMemorytraitAll methods take
&self(not&mut self) so implementations are trivially wrappable inArcand shareable across agent handles and tokio tasks.InMemoryConversationBounded
VecDeque— keeps the lastNmessages, evicting oldest on overflow:InMemoryConversation::unbounded()retains every messageSlidingWindowMemorytype alias for explicit window-size semanticsNoMemoryNo-op default — identical to current Agent behaviour. Useful as a default type parameter so memory is opt-in with zero overhead:
Custom backends
Any storage backend just implements the trait:
Tests (6)
push_and_retrieve— basic store/fetchcapacity_evicts_oldest— overflow evictionclear_empties_storeunbounded_keeps_all— 200 messages, none droppedno_memory_always_empty— NoMemory is truly statelessshared_via_arc— two Arc handles, writes visible to bothWhat this does NOT do (intentional scope)
This PR adds the trait and the in-process implementation. Wiring
ConversationMemoryintoAgentBuilderandAgent::chat()is a separate concern that touches more of the API surface — happy to follow up in a second PR once the trait shape is agreed on.Designed and implemented by Matthew Busel.