Skip to content

Commit 581863a

Browse files
authored
fix(sessions): match literal Unicode content in AdvancedSQLiteSession (#5143)
1 parent 29aa40b commit 581863a

2 files changed

Lines changed: 125 additions & 2 deletions

File tree

‎src/agents/extensions/memory/advanced_sqlite_session.py‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import asyncio
44
import json
55
import logging
6+
import re
67
import sqlite3
78
import time
89
from contextlib import closing
@@ -1589,6 +1590,11 @@ async def find_turns_by_content(
15891590

15901591
def _search_sync():
15911592
"""Synchronous helper to search turns by content."""
1593+
encoded_term = json.dumps(search_term)[1:-1]
1594+
for char in ("\\", "%", "_"):
1595+
encoded_term = encoded_term.replace(char, "\\" + char)
1596+
# Preserve SQLite LIKE's ASCII-only case-insensitive matching.
1597+
search_pattern = re.compile(re.escape(search_term), re.IGNORECASE | re.ASCII)
15921598
with self._locked_connection() as conn:
15931599
resolved_branch_id = self._resolve_read_branch(conn, branch_id)
15941600
with closing(conn.cursor()) as cursor:
@@ -1602,17 +1608,25 @@ def _search_sync():
16021608
JOIN {self.messages_table} am ON ms.message_id = am.id
16031609
WHERE ms.session_id = ? AND ms.branch_id = ?
16041610
AND ms.message_type = 'user'
1605-
AND am.message_data LIKE ?
1611+
AND am.message_data LIKE ? ESCAPE '\\'
16061612
ORDER BY ms.branch_turn_number
16071613
""",
1608-
(self.session_id, resolved_branch_id, f"%{search_term}%"),
1614+
(self.session_id, resolved_branch_id, f"%{encoded_term}%"),
16091615
)
16101616

16111617
matches = []
16121618
for row in cursor.fetchall():
16131619
turn_num, message_data, created_at = row
16141620
try:
16151621
content = json.loads(message_data).get("content", "")
1622+
# An encoded match can start inside a literal JSON escape.
1623+
texts = (
1624+
[content]
1625+
if isinstance(content, str)
1626+
else [part.get("text", "") for part in content]
1627+
)
1628+
if not any(search_pattern.search(text) for text in texts):
1629+
continue
16161630
matches.append(
16171631
{
16181632
"turn": turn_num,

‎tests/extensions/memory/test_advanced_sqlite_session.py‎

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,115 @@ async def test_find_turns_by_content():
19121912
session.close()
19131913

19141914

1915+
@pytest.mark.parametrize(
1916+
("earlier_content", "matching_content", "search_term"),
1917+
[
1918+
("Dogs", "Tell me about cats", "CATS"),
1919+
("Tokyo", "大阪の天気", "大阪"),
1920+
("Hello", "你好世界", "你好"),
1921+
(r"Literal \u00e9", "café", "é"),
1922+
("café", r"Literal \u00e9", r"\u00e9"),
1923+
("CAFÉ", "café", "é"),
1924+
(r"Literal \n", "First\nsecond", "\n"),
1925+
("First\nsecond", r"Literal \n", r"\n"),
1926+
("No quotes", 'Say "hello"', '"hello"'),
1927+
("Plain path", r"C:\notes", r"C:\notes"),
1928+
("100 dollars 東京", "100% 東京", "100% 東京"),
1929+
("axb 東京", "a_b 東京", "a_b 東京"),
1930+
("No percent", "50%", "%"),
1931+
("No underscore", "a_b", "_"),
1932+
],
1933+
)
1934+
async def test_find_turns_by_content_matches_literal_text(
1935+
earlier_content: str, matching_content: str, search_term: str
1936+
):
1937+
"""Search decoded content, without interpreting JSON escapes or SQL wildcards."""
1938+
session = AdvancedSQLiteSession(session_id="literal_search", create_tables=True)
1939+
try:
1940+
await session.add_items([{"role": "user", "content": earlier_content}])
1941+
await session.add_items([{"role": "user", "content": matching_content}])
1942+
1943+
matches = await session.find_turns_by_content(search_term)
1944+
assert [turn["turn"] for turn in matches] == [2]
1945+
assert matches[0]["full_content"] == matching_content
1946+
assert matches[0]["content"] == matching_content
1947+
finally:
1948+
session.close()
1949+
1950+
1951+
@pytest.mark.parametrize("structured", [False, True], ids=["string", "text_parts"])
1952+
async def test_create_branch_from_content_uses_decoded_match(structured: bool):
1953+
"""A literal escape in an earlier turn must not become the branch point."""
1954+
session = AdvancedSQLiteSession(session_id="decoded_branch", create_tables=True)
1955+
earlier_item: TResponseInputItem = {"role": "user", "content": r"Literal \u00e9"}
1956+
matching_item: TResponseInputItem = {"role": "user", "content": "café"}
1957+
if structured:
1958+
earlier_item["content"] = [{"type": "input_text", "text": r"Literal \u00e9"}]
1959+
matching_item["content"] = [{"type": "input_text", "text": "café"}]
1960+
try:
1961+
await session.add_items([earlier_item])
1962+
await session.add_items([matching_item])
1963+
1964+
matches = await session.find_turns_by_content("é")
1965+
assert [turn["turn"] for turn in matches] == [2]
1966+
assert matches[0]["full_content"] == matching_item["content"]
1967+
1968+
assert await session.create_branch_from_content("é", "cafe_branch") == "cafe_branch"
1969+
assert await session.get_items() == [earlier_item]
1970+
finally:
1971+
session.close()
1972+
1973+
1974+
async def test_find_turns_by_content_with_escaped_text_parts():
1975+
"""Structured text is searched after decoding, without matching the message envelope."""
1976+
session = AdvancedSQLiteSession(session_id="escaped_text_parts", create_tables=True)
1977+
try:
1978+
await session.add_items(
1979+
[{"role": "user", "content": [{"type": "input_text", "text": r"Literal \n"}]}]
1980+
)
1981+
await session.add_items(
1982+
[
1983+
{
1984+
"role": "user",
1985+
"content": [
1986+
{"type": "input_text", "text": '東京 "café"\n50% a_b'},
1987+
{"type": "input_image", "image_url": "https://example.com/image.png"},
1988+
],
1989+
}
1990+
]
1991+
)
1992+
1993+
for search_term in ('東京 "café"', "\n", "50% a_b"):
1994+
assert [turn["turn"] for turn in await session.find_turns_by_content(search_term)] == [
1995+
2
1996+
]
1997+
assert [turn["turn"] for turn in await session.find_turns_by_content(r"\n")] == [1]
1998+
assert await session.find_turns_by_content("user") == []
1999+
finally:
2000+
session.close()
2001+
2002+
2003+
async def test_find_turns_by_content_filters_session_and_branch(tmp_path: Path):
2004+
"""Decoded matching retains the query's session and branch boundaries."""
2005+
db_path = tmp_path / "content_search.db"
2006+
session = AdvancedSQLiteSession(session_id="search", db_path=db_path, create_tables=True)
2007+
other = AdvancedSQLiteSession(session_id="other", db_path=db_path, create_tables=True)
2008+
try:
2009+
await other.add_items([{"role": "user", "content": "東京 other session"}])
2010+
await session.add_items([{"role": "user", "content": "First turn"}])
2011+
await session.add_items([{"role": "user", "content": "東京 main"}])
2012+
await session.create_branch_from_turn(2, "alternate")
2013+
await session.add_items([{"role": "user", "content": "東京 alternate"}])
2014+
2015+
matches = await session.find_turns_by_content("東京")
2016+
assert [turn["full_content"] for turn in matches] == ["東京 alternate"]
2017+
matches = await session.find_turns_by_content("東京", branch_id="main")
2018+
assert [turn["full_content"] for turn in matches] == ["東京 main"]
2019+
finally:
2020+
session.close()
2021+
other.close()
2022+
2023+
19152024
async def test_get_conversation_turns_with_list_content():
19162025
"""List (multimodal) content is previewed as a string instead of crashing or leaking a list."""
19172026
session_id = "conversation_turns_list_content_test"

0 commit comments

Comments
 (0)