Repository navigation
fix: Redis outage rate-limit fallback and provider-valid session history - #56
Merged
Merged
Conversation
- Catch RedisError (not just ResponseError) around the sliding-window script so connection failures degrade to the in-memory window instead of surfacing as 500 - Extract the in-memory window into _check_window_in_memory shared by both paths - Add unit and API-level regression tests for an unreachable Redis client
- Repair the trailing assistant tool-call turn before saving session messages so timeouts, duplicate-loop aborts, and unexpected errors cannot leave dangling tool_calls that OpenAI and Gemini reject on the next run - Append synthetic error tool results for unanswered call ids, preserving any partial results already recorded - Add regression tests covering timeout, duplicate abort, and mid-batch abort
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #55
Summary
Resolves the two medium-severity bugs reported in #55.
1. Rate limiter degrades gracefully on Redis outage (
agentkit/api/ratelimit.py)_check_windowonly caughtResponseError, so a genuine outage (ConnectionError/TimeoutError, siblings ofResponseError) escaped therate_limitdependency and surfaced as HTTP 500 on/runsand/tools.check()now catchesRedisError, logs a warning, and falls back to the in-memory sliding window (extracted into_check_window_in_memory, shared with the no-Redis path). When Redis is reachable, behavior is unchanged.2. Aborted runs persist provider-valid session history (
agentkit/core/agent.py)A run-level timeout mid-tool, a duplicate-loop abort mid-batch, or an unexpected error could end the run after the assistant tool-call turn was appended but before all tool results were. The saved history then contained dangling
assistant.tool_calls, which OpenAI rejects with a 400 and Gemini requires afunction_responsefor — poisoning the session until TTL expiry._repair_trailing_tool_callsnow appends synthetic error tool results for unanswered call ids (preserving partial results) beforesave_messageson every terminal path, includingraise_on_failure=True.Tests
tests/unit/test_api_ratelimit.py: unreachable-Redis stub — limiter enforces the limit via memory; API-level 200/200/429 (previously 500).tests/unit/test_agent_guards.py: timeout mid-tool persists a valid history; follow-up run in the same session succeeds.tests/unit/test_agent_loop_detector.py: duplicate abort (whole turn + mid-batch) answers every call id.Verification
pytest— full hermetic suite green;pytest --cov=agentkit --cov-fail-under=85passes (92% total).ruff check .,ruff format --check .,mypy agentkit tests— clean.