-
Notifications
You must be signed in to change notification settings - Fork 0
fix(agents,api): surface fallback model failures with classified details (#335) #383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
a060ff6
fix(agents,api): surface fallback model failures with classified deta…
w7-mgfcode 7c57641
docs(repo): track reliability E2 prp for surfacing fallback failures …
w7-mgfcode a6907c6
test(agents): cover review feedback on failure classification edges (…
w7-mgfcode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| """Unit tests for RFC 7807 problem_response extension members (issue #335). | ||
|
|
||
| The `extensions` channel lets a ForecastLabError surface client-safe data | ||
| (e.g. classified per-model failures) in the problem+json body without going | ||
| through the log-only `details` attribute. | ||
| """ | ||
|
|
||
| import json | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
| from fastapi import Request | ||
|
|
||
| from app.core.exceptions import ( | ||
| AgentFallbackExhaustedError, | ||
| forecastlab_exception_handler, | ||
| ) | ||
| from app.core.problem_details import problem_response | ||
|
|
||
|
|
||
| def _body(response: Any) -> dict[str, Any]: | ||
| """Decode a ProblemDetailResponse body.""" | ||
| decoded: dict[str, Any] = json.loads(response.body) | ||
| return decoded | ||
|
|
||
|
|
||
| def test_problem_response_without_extensions_unchanged() -> None: | ||
| """Default (no extensions) output keeps the existing shape exactly.""" | ||
| response = problem_response( | ||
| status=404, | ||
| title="Not Found", | ||
| detail="Resource not found", | ||
| error_code="NOT_FOUND", | ||
| ) | ||
|
|
||
| body = _body(response) | ||
| assert response.status_code == 404 | ||
| assert body["status"] == 404 | ||
| assert body["code"] == "NOT_FOUND" | ||
| assert body["type"] == "/errors/not-found" | ||
| assert "failures" not in body | ||
|
|
||
|
|
||
| def test_problem_response_merges_extensions() -> None: | ||
| """Extension members are merged into the serialized body.""" | ||
| response = problem_response( | ||
| status=502, | ||
| title="Agent Fallback Exhausted", | ||
| detail="All configured agent models failed", | ||
| error_code="AGENT_FALLBACK_EXHAUSTED", | ||
| extensions={"failures": [{"model_name": "m1", "reason": "model_not_found"}]}, | ||
| ) | ||
|
|
||
| body = _body(response) | ||
| assert body["code"] == "AGENT_FALLBACK_EXHAUSTED" | ||
| assert body["type"] == "/errors/agent-fallback-exhausted" | ||
| assert body["failures"] == [{"model_name": "m1", "reason": "model_not_found"}] | ||
|
|
||
|
|
||
| def test_problem_response_extensions_cannot_override_reserved() -> None: | ||
| """Reserved base-field keys in extensions are silently dropped.""" | ||
| response = problem_response( | ||
| status=502, | ||
| title="Agent Fallback Exhausted", | ||
| detail="real detail", | ||
| error_code="AGENT_FALLBACK_EXHAUSTED", | ||
| extensions={ | ||
| "status": 200, | ||
| "code": "HACK", | ||
| "detail": "spoofed", | ||
| "type": "about:blank", | ||
| "title": "spoofed", | ||
| "safe_key": "kept", | ||
| }, | ||
| ) | ||
|
|
||
| body = _body(response) | ||
| assert response.status_code == 502 | ||
| assert body["status"] == 502 | ||
| assert body["code"] == "AGENT_FALLBACK_EXHAUSTED" | ||
| assert body["detail"] == "real detail" | ||
| assert body["type"] == "/errors/agent-fallback-exhausted" | ||
| assert body["title"] == "Agent Fallback Exhausted" | ||
| assert body["safe_key"] == "kept" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_exception_handler_propagates_extensions() -> None: | ||
| """The full exception → handler → problem+json path carries extensions. | ||
|
|
||
| Guards the wiring: ForecastLabError.extensions must reach the response | ||
| body via forecastlab_exception_handler's pass-through (issue #335). | ||
| """ | ||
| failures = [ | ||
| {"model_name": "m1", "status_code": 404, "reason": "model_not_found", "detail": ""}, | ||
| {"model_name": "m2", "status_code": 429, "reason": "quota_exhausted", "detail": ""}, | ||
| ] | ||
| exc = AgentFallbackExhaustedError("All configured agent models failed", failures=failures) | ||
| request = Request(scope={"type": "http", "method": "POST", "path": "/", "headers": []}) | ||
|
|
||
| response = await forecastlab_exception_handler(request, exc) | ||
|
|
||
| body = _body(response) | ||
| assert response.status_code == 502 | ||
| assert body["status"] == 502 | ||
| assert body["code"] == "AGENT_FALLBACK_EXHAUSTED" | ||
| assert body["type"] == "/errors/agent-fallback-exhausted" | ||
| assert body["title"] == "Agent Fallback Exhausted" | ||
| assert body["detail"] == "All configured agent models failed" | ||
| assert body["failures"] == failures | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (testing): Add a test that exercises problem_response via a ForecastLabError with extensions
Since
ForecastLabErrornow carries anextensionsdict and the handler passes it through toproblem_response, consider adding a test that raises a minimalForecastLabError(orAgentFallbackExhaustedError) with non-emptyextensions, runs it throughforecastlab_exception_handler, and asserts that the response body contains those extension fields and the expectedtype/code. This would exercise the full path from exception to problem+json and help catch future wiring regressions.Suggested implementation:
To fully wire this up you will likely need to:
app/core/tests/test_problem_details.py, for example:from starlette.requests import Requestfrom app.core.exceptions import ForecastLabErrorfrom app.core.exception_handlers import forecastlab_exception_handlerAdjust module paths to match your actual project layout.
pytest, either:@pytest.mark.anyio/@pytest.mark.asyncio, orforecastlab_exception_handleris synchronous in your codebase, removeasyncfrom the test definition and theawaitbefore calling the handler.ForecastLabErroruses different parameter names (e.g.statusinstead ofstatus_code,error_codevscode, or a different way to passextensions), adjust the constructor call in the test accordingly so that:status_codeof the HTTP response is 400."code": "BAD_REQUEST"and the other fields."type"field (e.g./errors/bad-request) and you want to assert it as well, add:assert body["type"] == "/errors/bad-request"once you confirm the exact value in your implementation.