Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
223fab9
Fix AdvancedSQLiteSession clear_session and pop_item metadata leaks
okaditya84 Jul 8, 2026
2c97250
Clean up stale turn_usage in pop_item when a turn is emptied
okaditya84 Jul 8, 2026
50d2b2f
pop_item: snapshot current branch before dispatching to the worker
okaditya84 Jul 8, 2026
f44364f
clear_session: reset the branch pointer inside the locked operation
okaditya84 Jul 8, 2026
a1f9a5a
test: cover branch snapshot, locked clear reset, and shared copied-me…
okaditya84 Jul 8, 2026
1545357
Guard the branch pointer against stale switch/create after clear_session
okaditya84 Jul 9, 2026
9c5e44c
Skip stale turn-usage writes after pop_item/clear_session
okaditya84 Jul 9, 2026
a9d6a30
test: barrier-based interleaving tests for clear/switch/pop ordering
okaditya84 Jul 9, 2026
39b711a
Merge branch 'main' into fix/advanced-sqlite-clear-pop-metadata
okaditya84 Jul 9, 2026
3c3849d
Invalidate stale usage writes with a turn-usage version counter
okaditya84 Jul 9, 2026
38becd3
test: ABA regression for usage recorded against a reused turn number
okaditya84 Jul 9, 2026
d9f48a0
Scope stale-usage invalidation to the captured turn via a row anchor
okaditya84 Jul 9, 2026
7353a4a
test: usage write survives unrelated branch deletion (invalidation sc…
okaditya84 Jul 9, 2026
5c7be97
pop_item: roll back the delete sequence on failure
okaditya84 Jul 11, 2026
c770d0d
clear_session: roll back the delete sequence on failure
okaditya84 Jul 11, 2026
8b6dc90
test: rollback regression for pop_item and clear_session
okaditya84 Jul 11, 2026
a89a015
Merge remote-tracking branch 'upstream/main' into fix/advanced-sqlite…
okaditya84 Jul 11, 2026
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
123 changes: 123 additions & 0 deletions src/agents/extensions/memory/advanced_sqlite_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,129 @@ def _get_items_sync():

return await asyncio.to_thread(_get_items_sync)

async def pop_item(self) -> TResponseInputItem | None:
"""Remove and return the most recent item from the current branch.

Overrides the base implementation so the popped message's
`message_structure` row is removed in the same transaction and only the
current branch is affected. The underlying message row is deleted only
when no other branch still references it, mirroring `delete_branch`. When
popping empties a turn on the current branch, its `turn_usage` row is
removed as well so usage analytics do not report a turn that no longer
exists.
"""

# Snapshot the current branch at call time so a concurrent
# switch_to_branch() cannot redirect this pop to a different branch once
# it has been dispatched to the worker thread.
branch_id = self._current_branch_id

def _pop_item_sync():
with self._locked_connection() as conn:
while True:
with closing(conn.cursor()) as cursor:
# Find the most recent item on the snapshotted branch.
cursor.execute(
"""
SELECT id, message_id, user_turn_number FROM message_structure
WHERE session_id = ? AND branch_id = ?
ORDER BY sequence_number DESC
LIMIT 1
""",
(self.session_id, branch_id),
)
row = cursor.fetchone()
if row is None:
return None

structure_id, message_id, user_turn_number = row

# Read the message payload before removing anything.
cursor.execute(
f"SELECT message_data FROM {self.messages_table} WHERE id = ?",
(message_id,),
)
message_row = cursor.fetchone()

# Remove the structure row for this branch, then drop the
# underlying message only if no other branch references it.
cursor.execute(
"DELETE FROM message_structure WHERE id = ?",
(structure_id,),
)
self._cleanup_orphaned_messages_sync(conn)
Comment thread
seratch marked this conversation as resolved.
Outdated

# If this was the last item of the turn on this branch,
# drop the now-stale turn_usage row for that turn.
if user_turn_number is not None:
cursor.execute(
"""
SELECT COUNT(*) FROM message_structure
WHERE session_id = ? AND branch_id = ? AND user_turn_number = ?
""",
(self.session_id, branch_id, user_turn_number),
)
if cursor.fetchone()[0] == 0:
cursor.execute(
"""
DELETE FROM turn_usage
WHERE session_id = ? AND branch_id = ? AND user_turn_number = ?
""",
(self.session_id, branch_id, user_turn_number),
)

conn.commit()

if message_row is None:
# Structure row pointed at a missing message; keep looking.
continue

try:
return json.loads(message_row[0])
except (json.JSONDecodeError, TypeError):
# Drop corrupted JSON entries and keep looking for a valid item.
continue

return await asyncio.to_thread(_pop_item_sync)

async def clear_session(self) -> None:
"""Clear all items for this session.

Overrides the base implementation so the `message_structure` and
`turn_usage` metadata tables are cleared in the same transaction. Those
rows declare an `ON DELETE CASCADE` foreign key, but SQLite does not
enforce foreign keys unless `PRAGMA foreign_keys=ON` is set, so they must
be deleted explicitly to avoid leaking stale structure and usage data.
"""

def _clear_session_sync():
with self._locked_connection() as conn:
conn.execute(
f"DELETE FROM {self.messages_table} WHERE session_id = ?",
(self.session_id,),
)
conn.execute(
f"DELETE FROM {self.sessions_table} WHERE session_id = ?",
(self.session_id,),
)
conn.execute(
"DELETE FROM message_structure WHERE session_id = ?",
(self.session_id,),
)
conn.execute(
"DELETE FROM turn_usage WHERE session_id = ?",
(self.session_id,),
)
conn.commit()
# All branches were removed, so reset the in-memory pointer to
# 'main' while still holding the lock. Doing this inside the
# locked operation keeps the reset atomic with the clear, so no
# other locked operation observes the session as cleared while
# the pointer still references a deleted branch.
self._current_branch_id = "main"

await asyncio.to_thread(_clear_session_sync)

async def store_run_usage(self, result: RunResult) -> None:
"""Store usage data for the current conversation turn.

Expand Down
Loading