Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Keep decision endpoints' response bodies out of logs and Test results

agent-runner353e777b3010206
diff --git a/core/switch_core/decision/client.py b/core/switch_core/decision/client.py
index 6498c1f73..19b9dbd51 100644
--- a/core/switch_core/decision/client.py
+++ b/core/switch_core/decision/client.py
@@ -140,6 +140,23 @@ FailureReason = Literal[
BackendLabel = Literal["server_default", "endpoint", "haiku_anthropic", "haiku_bedrock"]
+# What an error answer's body said, classified as it arrives. The body itself
+# is never kept: a workspace's endpoint chooses it, so it stays out of the log
+# and out of what Test shows.
+ErrorSignal = Literal["backend_unavailable", "auth"]
+
+_AUTH_WORDS = ("401", "auth", "api key", "api_key", "x-api-key")
+
+
+def _error_signal(body: str) -> ErrorSignal | None:
+ text = body.lower()
+ if "backend_unavailable" in text:
+ return "backend_unavailable"
+ if any(word in text for word in _AUTH_WORDS):
+ return "auth"
+ return None
+
+
class DecisionServiceError(Exception):
"""The service gave no usable answer: it could not be reached, did not
answer in time, answered with an error, or answered something else than
@@ -151,12 +168,12 @@ class DecisionServiceError(Exception):
message: str,
*,
status: int | None = None,
- body: str | None = None,
+ signal: ErrorSignal | None = None,
) -> None:
super().__init__(message)
self.reason: FailureReason = reason
self.status = status
- self.body = body
+ self.signal: ErrorSignal | None = signal
@dataclass(frozen=True)
@@ -281,9 +298,9 @@ async def post_decision(
if response.status_code != 200:
raise DecisionServiceError(
"error_status",
- f"POST {url} answered {response.status_code}: {response.text[:200]}",
+ f"POST {url} answered with status {response.status_code}",
status=response.status_code,
- body=response.text[:2000],
+ signal=_error_signal(response.text[:2000]),
)
try:
return DecisionAnswer.model_validate_json(response.content)
@@ -634,18 +651,11 @@ def _test_error_code(
if failure.reason in ("timeout", "unreachable", "bad_answer"):
return failure.reason
status = failure.status
- body = (failure.body or "").lower()
if status in (401, 403):
return "unauthorized"
- if status == 400 and "backend_unavailable" in body:
+ if status == 400 and failure.signal == "backend_unavailable":
return "backend_unavailable"
- if (
- backend == "haiku_anthropic"
- and status == 502
- and any(
- word in body for word in ("401", "auth", "api key", "api_key", "x-api-key")
- )
- ):
+ if backend == "haiku_anthropic" and status == 502 and failure.signal == "auth":
return "key_refused"
return "bad_answer"
diff --git a/core/tests/switch_core/decision/test_client.py b/core/tests/switch_core/decision/test_client.py
index 5e57b04a9..4a529159e 100644
--- a/core/tests/switch_core/decision/test_client.py
+++ b/core/tests/switch_core/decision/test_client.py
@@ -360,8 +360,11 @@ class TestFailures:
outcome = await _decide(_client(session_factory, http, stub.url))
assert isinstance(outcome, Failed) and outcome.reason == "error_status"
- assert f"answered {status}" in caplog.text
- assert "model server unreachable" in caplog.text
+ assert f"answered with status {status}" in caplog.text
+ assert f"answered with status {status}" in outcome.message
+ # The endpoint's own words are never echoed into the log.
+ assert "model server unreachable" not in caplog.text
+ assert "model server unreachable" not in outcome.message
@pytest.mark.parametrize(
"body",
@@ -689,6 +692,30 @@ class TestTheTestButton:
assert not outcome.ok and outcome.error_code == code
assert outcome.error_message
+ assert body.decode() not in outcome.error_message
+ if status != 200:
+ assert f"status {status}" in outcome.error_message
+
+ async def test_an_error_page_is_not_echoed_into_the_log_or_the_result(
+ self,
+ session_factory: async_sessionmaker[AsyncSession],
+ stub: _Stub,
+ http: httpx.AsyncClient,
+ caplog: pytest.LogCaptureFixture,
+ ) -> None:
+ stub.status = 500
+ stub.body = b"<html><body>internal-detail-Xq7 stack trace</body></html>"
+
+ with caplog.at_level(logging.DEBUG):
+ outcome = await _client(session_factory, http, None).test(
+ backend="endpoint", endpoint=stub.url, api_key=None, threshold=None
+ )
+
+ assert outcome.error_code == "bad_answer"
+ assert outcome.error_message is not None
+ assert "status 500" in outcome.error_message
+ assert "internal-detail-Xq7" not in outcome.error_message
+ assert "internal-detail-Xq7" not in caplog.text
async def test_haiku_without_a_server_service_or_key(
self,