Skip to content

Independent Auditing of AI Agents. Run by human or the agent itself, to answer the most crucial question in the AI Agent Economy. Is the agent doing what is supposed to do? With iFixAi you can have this answer in less than 120 seconds.

fix: prevent stale HTTP sources from contaminating retrieval evidence (#124)

Rudy Mizrahi Celekli87f19cc3f221
diff --git a/ifixai/providers/http.py b/ifixai/providers/http.py
index 87068be..c019c38 100644
--- a/ifixai/providers/http.py
+++ b/ifixai/providers/http.py
@@ -81,7 +81,6 @@ def _source_item_to_retrieved(item: dict[str, Any]) -> RetrievedSource:
class HttpProvider(ChatProvider):
def __init__(self) -> None:
- self._last_sources: dict[str, list[RetrievedSource]] = {}
self._session: aiohttp.ClientSession | None = None
self._session_lock = asyncio.Lock()
@@ -184,7 +183,6 @@ class HttpProvider(ChatProvider):
)
data = await resp.json()
- self._capture_sources(endpoint.rstrip("/"), data)
return self._extract_response_text(data, endpoint)
except aiohttp.ClientConnectionError as exc:
@@ -226,15 +224,6 @@ class HttpProvider(ChatProvider):
else:
return content
- def _capture_sources(self, endpoint: str, data: dict[str, Any]) -> None:
- raw_sources = data.get("sources")
- if not isinstance(raw_sources, list):
- return
- parsed = [
- _source_item_to_retrieved(s) for s in raw_sources if isinstance(s, dict)
- ]
- self._last_sources[endpoint] = parsed
-
async def retrieve_sources(
self,
query: str,
@@ -252,13 +241,15 @@ class HttpProvider(ChatProvider):
url, json=payload, headers=headers, timeout=timeout
) as resp:
if resp.status >= 400:
- return self._last_sources.get(endpoint)
+ return None
data = await resp.json()
raw = data.get("sources")
if not isinstance(raw, list):
- return self._last_sources.get(endpoint)
+ return None
return [
_source_item_to_retrieved(s) for s in raw if isinstance(s, dict)
]
except (aiohttp.ClientError, asyncio.TimeoutError):
- return self._last_sources.get(endpoint)
+ # Sources attached to a previous chat response cannot establish
+ # what this independent retrieval query would have returned.
+ return None
diff --git a/ifixai/tests/providers/test_http_retrieval_sources.py b/ifixai/tests/providers/test_http_retrieval_sources.py
new file mode 100644
index 0000000..15aef31
--- /dev/null
+++ b/ifixai/tests/providers/test_http_retrieval_sources.py
@@ -0,0 +1,60 @@
+"""Retrieval evidence must come from the requested query, not a prior chat."""
+
+import aiohttp.web
+import pytest
+
+from ifixai.core.types import ChatMessage, ProviderConfig
+from ifixai.providers.http import HttpProvider
+
+
+async def test_failed_retrieval_does_not_reuse_prior_chat_sources():
+ async def chat(_request):
+ return aiohttp.web.json_response({
+ "choices": [{"message": {"content": "first answer"}}],
+ "sources": [{"document_uri": "old-document"}],
+ })
+
+ async def retrieve(_request):
+ return aiohttp.web.Response(status=404)
+
+ app = aiohttp.web.Application()
+ app.router.add_post("/v1/chat/completions", chat)
+ app.router.add_post("/v1/retrieve", retrieve)
+ runner = aiohttp.web.AppRunner(app)
+ await runner.setup()
+ site = aiohttp.web.TCPSite(runner, "127.0.0.1", 0)
+ await site.start()
+ port = site._server.sockets[0].getsockname()[1]
+ provider = HttpProvider()
+ config = ProviderConfig(provider="http", endpoint=f"http://127.0.0.1:{port}/v1")
+ try:
+ assert await provider.send_message([ChatMessage(content="first query")], config) == "first answer"
+ assert await provider.retrieve_sources("unrelated query", config) is None
+ finally:
+ await provider.aclose()
+ await runner.cleanup()
+
+
+async def test_successful_retrieval_returns_requested_sources():
+ async def retrieve(request):
+ body = await request.json()
+ return aiohttp.web.json_response({"sources": [{"document_uri": body["query"]}]})
+
+ app = aiohttp.web.Application()
+ app.router.add_post("/v1/retrieve", retrieve)
+ runner = aiohttp.web.AppRunner(app)
+ await runner.setup()
+ site = aiohttp.web.TCPSite(runner, "127.0.0.1", 0)
+ await site.start()
+ port = site._server.sockets[0].getsockname()[1]
+ provider = HttpProvider()
+ config = ProviderConfig(provider="http", endpoint=f"http://127.0.0.1:{port}/v1")
+ try:
+ sources = await provider.retrieve_sources("requested-document", config)
+ assert sources is not None
+ assert [source.source_id for source in sources] == ["requested-document"]
+ finally:
+ await provider.aclose()
+ await runner.cleanup()