fix(sessions): return True when add_items retry succeeds in strip_all_images_from_session

The unconditional re-raise after the suppressed retry meant a successful
retry still signalled failure (recovery in _run_cycle was discarded) and a
double failure swallowed the genuine second error behind the suppressed
original. Retry directly so a successful retry falls through to return True
and a failed retry propagates the real second exception.
This commit is contained in:
mitnick 2026-06-26 01:42:55 +00:00 • committed by Rome-1
parent 7141ccff62
commit 2e68e8baf5
2 changed files with 70 additions and 5 deletions

View file

@ -2,7 +2,6 @@
from __future__ import annotations
import contextlib
from typing import TYPE_CHECKING, Any, cast
from agents.memory import SQLiteSession
@ -58,8 +57,6 @@ async def strip_all_images_from_session(session: Session) -> bool:
await session.clear_session()
try:
await session.add_items(rebuilt_items)
except Exception:
with contextlib.suppress(Exception):
await session.add_items(rebuilt_items)
raise
except Exception: # noqa: BLE001
await session.add_items(rebuilt_items)
return True

68
tests/test_sessions.py Normal file
View file

@ -0,0 +1,68 @@
"""Tests for SDK session helpers in strix/core/sessions.py."""
from __future__ import annotations
from typing import Any
import pytest
from strix.core.sessions import strip_all_images_from_session
class _FakeSession:
"""Minimal Session stub whose add_items can be scripted to fail then succeed."""
def __init__(self, items: list[Any], add_items_errors: list[Exception | None]) -> None:
self._items = items
self._add_items_errors = add_items_errors
self.add_items_calls = 0
self.cleared = False
self.added_items: list[Any] | None = None
async def get_items(self) -> list[Any]:
return self._items
async def clear_session(self) -> None:
self.cleared = True
async def add_items(self, items: list[Any]) -> None:
index = self.add_items_calls
self.add_items_calls += 1
error = self._add_items_errors[index] if index < len(self._add_items_errors) else None
if error is not None:
raise error
self.added_items = items
def _image_item() -> dict[str, Any]:
return {
"type": "function_call_output",
"call_id": "call-1",
"output": [{"type": "input_image", "image_url": "data:image/png;base64,abc"}],
}
async def test_retry_success_returns_true() -> None:
session = _FakeSession(
items=[_image_item()],
add_items_errors=[RuntimeError("first attempt failed"), None],
)
result = await strip_all_images_from_session(session) # type: ignore[arg-type]
assert result is True
assert session.add_items_calls == 2
assert session.added_items is not None
async def test_retry_double_failure_propagates_second_error() -> None:
second_error = ValueError("second attempt failed")
session = _FakeSession(
items=[_image_item()],
add_items_errors=[RuntimeError("first attempt failed"), second_error],
)
with pytest.raises(ValueError, match="second attempt failed"):
await strip_all_images_from_session(session) # type: ignore[arg-type]
assert session.add_items_calls == 2