Skip to content

Commit 588826c

Browse files
fix(sessions): reject blank SQLite branch names (#5179)
fix(memory): reject blank SQLite branch names
1 parent 8e338e5 commit 588826c

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,8 +1075,10 @@ async def create_branch_from_turn(
10751075
10761076
Raises:
10771077
ValueError: If turn doesn't exist, doesn't contain a user message, or
1078-
`branch_name` has already been used in this session
1078+
`branch_name` is blank or has already been used in this session.
10791079
"""
1080+
if branch_name is not None and not branch_name.strip():
1081+
raise ValueError("Branch name cannot be empty")
10801082

10811083
async def _create_and_switch() -> tuple[str, Any, str]:
10821084
# Copying the branch is the first durable side effect. Keep the
@@ -1129,8 +1131,8 @@ async def create_branch_from_content(
11291131
The branch_id of the newly created branch.
11301132
11311133
Raises:
1132-
ValueError: If no matching turns are found or `branch_name` has already been used
1133-
in this session.
1134+
ValueError: If no matching turns are found or `branch_name` is blank or has
1135+
already been used in this session.
11341136
"""
11351137
matching_turns = await self.find_turns_by_content(search_term)
11361138
if not matching_turns:

‎tests/extensions/memory/test_advanced_sqlite_session.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,33 @@ def _pop_item_in_process(
13331333
session.close()
13341334

13351335

1336+
@pytest.mark.parametrize("branch_name", ["", " ", "\t"])
1337+
@pytest.mark.parametrize("from_content", [False, True])
1338+
async def test_create_branch_rejects_blank_name(branch_name: str, from_content: bool):
1339+
session = AdvancedSQLiteSession(session_id="blank_branch_name", create_tables=True)
1340+
items: list[TResponseInputItem] = [
1341+
{"role": "user", "content": "First question"},
1342+
{"role": "assistant", "content": "First answer"},
1343+
{"role": "user", "content": "Second question"},
1344+
]
1345+
1346+
try:
1347+
await session.add_items(items)
1348+
branches_before = await session.list_branches()
1349+
1350+
with pytest.raises(ValueError, match="Branch name cannot be empty"):
1351+
if from_content:
1352+
await session.create_branch_from_content("Second question", branch_name)
1353+
else:
1354+
await session.create_branch_from_turn(2, branch_name)
1355+
1356+
assert await session.list_branches() == branches_before
1357+
assert session._current_branch_id == "main"
1358+
assert await session.get_items() == items
1359+
finally:
1360+
session.close()
1361+
1362+
13361363
@pytest.mark.parametrize("branch_id", ["main", "existing_branch"])
13371364
async def test_create_branch_rejects_populated_branch_id(branch_id: str):
13381365
"""Creating a branch must not append history to a populated branch."""

0 commit comments

Comments
 (0)