From c229a6731211aaca66891fff27de947df744371a Mon Sep 17 00:00:00 2001 From: Wang Date: Wed, 27 Aug 2025 06:16:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20Add=20`remove=5Fconversation`=20t?= =?UTF-8?q?o=20`ConversationManager`=20for=20ma=E2=80=A6=20(#2613)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### What this PR does This PR introduces a new public method, remove_conversation(conversation_id: Uuid), to the ConversationManager. This allows consumers of the codex-core library to manually remove a conversation from the manager's in-memory storage. ### Why this change is needed I am currently adapting the Codex client to run as a long-lived server application. In this server environment, ConversationManager instances persist for extended periods, and new conversations are created for each incoming user request. The current implementation of ConversationManager stores all created conversations in a HashMap indefinitely, with no mechanism for removal. This leads to unbounded memory growth in a server context, as every new conversation permanently occupies memory. While an automatic TTL-based cleanup mechanism could be one solution, a simpler, more direct remove_conversation method provides the necessary control for my use case. It allows my server application to explicitly manage the lifecycle of conversations, such as cleaning them up after a request is fully processed or after a period of inactivity is detected at the application level. This change provides a minimal, non-intrusive way to address the memory management issue for server-like applications built on top of codex-core, giving developers the flexibility to implement their own cleanup logic. Signed-off-by: M4n5ter Co-authored-by: Michael Bolin --- codex-rs/core/src/conversation_manager.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/codex-rs/core/src/conversation_manager.rs b/codex-rs/core/src/conversation_manager.rs index fd90f546..ae169ed5 100644 --- a/codex-rs/core/src/conversation_manager.rs +++ b/codex-rs/core/src/conversation_manager.rs @@ -110,6 +110,10 @@ impl ConversationManager { .ok_or_else(|| CodexErr::ConversationNotFound(conversation_id)) } + pub async fn remove_conversation(&self, conversation_id: Uuid) { + self.conversations.write().await.remove(&conversation_id); + } + /// Fork an existing conversation by dropping the last `drop_last_messages` /// user/assistant messages from its transcript and starting a new /// conversation with identical configuration (unless overridden by the