Skip to content

Commit 65e5fbd

Browse files
authored
Merge pull request #8 from John-Lin/revert-lru-eviction
Remove LRU chat eviction to keep demo simple
2 parents 0c21714 + 5efc194 commit 65e5fbd

2 files changed

Lines changed: 1 addition & 87 deletions

File tree

bot/agents.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import asyncio
4-
import collections
54
import logging
65
import os
76
from typing import Any
@@ -22,7 +21,6 @@
2221
)
2322

2423
MAX_TURNS = 25
25-
MAX_CHATS = 200
2624
MCP_SESSION_TIMEOUT_SECONDS = 30.0
2725

2826

@@ -59,30 +57,19 @@ def __init__(
5957
mcp_servers=(mcp_servers if mcp_servers is not None else []),
6058
)
6159
self.name = name
62-
self._conversations: collections.OrderedDict[int, list[TResponseInputItem]] = collections.OrderedDict()
60+
self._conversations: dict[int, list[TResponseInputItem]] = {}
6361
self._locks: dict[int, asyncio.Lock] = {}
6462

65-
def _evict_oldest(self) -> None:
66-
"""Remove the least-recently-used chat when over MAX_CHATS."""
67-
while len(self._conversations) > MAX_CHATS:
68-
evicted_id, _ = self._conversations.popitem(last=False)
69-
self._locks.pop(evicted_id, None)
70-
7163
def get_messages(self, chat_id: int) -> list[TResponseInputItem]:
7264
return self._conversations.get(chat_id, [])
7365

7466
def set_messages(self, chat_id: int, messages: list[TResponseInputItem]) -> None:
75-
# Move to end on update so recently-active chats survive eviction
7667
self._conversations[chat_id] = messages
77-
self._conversations.move_to_end(chat_id)
78-
self._evict_oldest()
7968

8069
def append_user_message(self, chat_id: int, message: str) -> None:
8170
if chat_id not in self._conversations:
8271
self._conversations[chat_id] = []
8372
self._conversations[chat_id].append({"role": "user", "content": message})
84-
self._conversations.move_to_end(chat_id)
85-
self._evict_oldest()
8673

8774
def truncate_history(self, chat_id: int) -> None:
8875
"""Keep only the last MAX_TURNS turns of conversation history.

tests/test_agents.py

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
from __future__ import annotations
22

3-
import asyncio
43
from unittest.mock import create_autospec
54

65
import pytest
76
from agents.models.interface import Model
87

98
from bot.agents import DEFAULT_INSTRUCTIONS
10-
from bot.agents import MAX_CHATS
119
from bot.agents import MAX_TURNS
1210
from bot.agents import OpenAIAgent
1311

@@ -154,74 +152,3 @@ def test_no_truncation_when_under_limit(self):
154152
msgs = agent.get_messages(chat_id=100)
155153
user_msgs = [m for m in msgs if m["role"] == "user"]
156154
assert len(user_msgs) == 3
157-
158-
159-
class TestChatEviction:
160-
def test_default_max_chats(self):
161-
assert MAX_CHATS == 200
162-
163-
def test_evicts_oldest_chat_when_limit_exceeded(self, monkeypatch):
164-
monkeypatch.setattr("bot.agents.MAX_CHATS", 3)
165-
agent = OpenAIAgent(name="test")
166-
167-
agent.set_messages(100, [{"role": "user", "content": "a"}])
168-
agent.set_messages(200, [{"role": "user", "content": "b"}])
169-
agent.set_messages(300, [{"role": "user", "content": "c"}])
170-
# At limit — all present
171-
assert len(agent._conversations) == 3
172-
173-
# Adding a 4th should evict the oldest (100)
174-
agent.set_messages(400, [{"role": "user", "content": "d"}])
175-
assert 100 not in agent._conversations
176-
assert len(agent._conversations) == 3
177-
assert set(agent._conversations.keys()) == {200, 300, 400}
178-
179-
def test_updating_existing_chat_does_not_evict(self, monkeypatch):
180-
monkeypatch.setattr("bot.agents.MAX_CHATS", 2)
181-
agent = OpenAIAgent(name="test")
182-
183-
agent.set_messages(100, [{"role": "user", "content": "a"}])
184-
agent.set_messages(200, [{"role": "user", "content": "b"}])
185-
# Updating chat 100 should not trigger eviction
186-
agent.set_messages(100, [{"role": "user", "content": "updated"}])
187-
assert len(agent._conversations) == 2
188-
assert agent.get_messages(100)[0]["content"] == "updated"
189-
190-
def test_append_to_new_chat_triggers_eviction(self, monkeypatch):
191-
monkeypatch.setattr("bot.agents.MAX_CHATS", 2)
192-
agent = OpenAIAgent(name="test")
193-
194-
agent.set_messages(100, [{"role": "user", "content": "a"}])
195-
agent.set_messages(200, [{"role": "user", "content": "b"}])
196-
agent.append_user_message(300, "c")
197-
198-
assert 100 not in agent._conversations
199-
assert len(agent._conversations) == 2
200-
201-
def test_accessing_chat_refreshes_its_position(self, monkeypatch):
202-
monkeypatch.setattr("bot.agents.MAX_CHATS", 3)
203-
agent = OpenAIAgent(name="test")
204-
205-
agent.set_messages(100, [{"role": "user", "content": "a"}])
206-
agent.set_messages(200, [{"role": "user", "content": "b"}])
207-
agent.set_messages(300, [{"role": "user", "content": "c"}])
208-
209-
# Access chat 100 to refresh it (move to end)
210-
agent.set_messages(100, [{"role": "user", "content": "refreshed"}])
211-
212-
# Now 200 is oldest — adding 400 should evict 200, not 100
213-
agent.set_messages(400, [{"role": "user", "content": "d"}])
214-
assert 200 not in agent._conversations
215-
assert 100 in agent._conversations
216-
217-
def test_eviction_also_cleans_up_lock(self, monkeypatch):
218-
monkeypatch.setattr("bot.agents.MAX_CHATS", 2)
219-
agent = OpenAIAgent(name="test")
220-
221-
agent._locks[100] = asyncio.Lock()
222-
agent.set_messages(100, [{"role": "user", "content": "a"}])
223-
agent._locks[200] = asyncio.Lock()
224-
agent.set_messages(200, [{"role": "user", "content": "b"}])
225-
226-
agent.set_messages(300, [{"role": "user", "content": "c"}])
227-
assert 100 not in agent._locks

0 commit comments

Comments
 (0)