Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/agents/extensions/memory/async_sqlite_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ async def get_items(self, limit: int | None = None) -> list[TResponseInputItem]:

session_limit = resolve_session_limit(limit, self.session_settings)

if session_limit is not None and session_limit <= 0:
return []

async with self._locked_connection() as conn:
if session_limit is None:
cursor = await conn.execute(
Expand Down Expand Up @@ -259,5 +262,7 @@ async def close(self) -> None:
if self._connection is None:
return
async with self._lock:
if self._connection is None:
return
await self._connection.close()
self._connection = None
3 changes: 3 additions & 0 deletions src/agents/extensions/memory/sqlalchemy_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ async def get_items(self, limit: int | None = None) -> list[TResponseInputItem]:

session_limit = resolve_session_limit(limit, self.session_settings)

if session_limit is not None and session_limit <= 0:
return []

async with self._session_factory() as sess:
if session_limit is None:
stmt = (
Expand Down
6 changes: 6 additions & 0 deletions tests/extensions/memory/test_async_sqlite_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ async def test_async_sqlite_session_get_items_limit():
none = await session.get_items(limit=0)
assert none == []

# Negative limit must also return [] to match MongoDB/Redis parity.
# SQLite treats LIMIT -1 as "no limit", so without an explicit guard
# a negative limit would incorrectly return all rows instead of [].
neg = await session.get_items(limit=-1)
assert neg == []

await session.close()


Expand Down
4 changes: 4 additions & 0 deletions tests/extensions/memory/test_sqlalchemy_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ async def test_get_items_with_limit(agent: Agent):
more_than_all = await session.get_items(limit=10)
assert len(more_than_all) == 4

# Non-positive limits must return [] for parity with MongoDB/Redis backends.
assert await session.get_items(limit=0) == []
assert await session.get_items(limit=-1) == []


async def test_pop_from_empty_session():
"""Test that pop_item returns None on an empty session."""
Expand Down