diff --git a/py/src/braintrust/test_trace.py b/py/src/braintrust/test_trace.py index a16ae042..080c88ad 100644 --- a/py/src/braintrust/test_trace.py +++ b/py/src/braintrust/test_trace.py @@ -1,5 +1,6 @@ """Tests for Trace functionality.""" +import asyncio import os import braintrust @@ -551,6 +552,36 @@ def fake_invoke(**kwargs): assert calls[0]["global_function"] == "custom_preprocessor" assert calls[0]["function_type"] == "preprocessor" + @pytest.mark.asyncio + @pytest.mark.parametrize("failure", ["flush", "invoke"]) + async def test_retries_failed_thread_fetch(self, monkeypatch, failure): + flushes = 0 + invokes = 0 + messages = [{"role": "user", "content": "retry succeeded"}] + + async def flush(): + nonlocal flushes + flushes += 1 + if failure == "flush" and flushes == 1: + raise RuntimeError("transient flush failure") + + def fake_invoke(**kwargs): + nonlocal invokes + invokes += 1 + if failure == "invoke" and invokes == 1: + raise RuntimeError("transient invoke failure") + return messages + + monkeypatch.setattr("braintrust.trace.invoke", fake_invoke) + trace = LocalTrace("experiment", "exp-123", "root-456", flush, _DummyState()) + with pytest.raises(RuntimeError, match=f"transient {failure} failure"): + await trace.get_thread() + + assert await asyncio.gather(trace.get_thread(), trace.get_thread()) == [messages, messages] + assert await trace.get_thread() == messages + assert flushes == (2 if failure == "flush" else 1) + assert invokes == (2 if failure == "invoke" else 1) + @pytest.mark.asyncio async def test_caches_by_preprocessor(self, monkeypatch): call_count = 0 diff --git a/py/src/braintrust/trace.py b/py/src/braintrust/trace.py index cb3b7d25..3fd32997 100644 --- a/py/src/braintrust/trace.py +++ b/py/src/braintrust/trace.py @@ -594,9 +594,12 @@ async def get_thread(self, options: GetThreadOptions | None = None) -> list[Any] """ preprocessor = options.get("preprocessor") if options and options.get("preprocessor") else None cache_key = preprocessor or "project_default" - if cache_key not in self._thread_cache: - self._thread_cache[cache_key] = asyncio.create_task(self._fetch_thread(options)) - return await self._thread_cache[cache_key] + task = self._thread_cache.get(cache_key) + # Share in-flight work and successful results, not a previous failed attempt. + if task is None or (task.done() and (task.cancelled() or task.exception() is not None)): + task = asyncio.create_task(self._fetch_thread(options)) + self._thread_cache[cache_key] = task + return await task async def _fetch_thread(self, options: GetThreadOptions | None = None) -> list[Any]: """Fetch thread messages via preprocessor invocation."""