fix(gateway): suppress stream consumer on API failure and cap fallback chunks to prevent Telegram flood - #67887
Conversation
|
Closing as duplicate |
|
Thanks for isolating the accumulated-stream failure path and adding focused coverage. Problems
Suggested changes
Automated hermes-sweeper review. |
|
Thanks for the review — both points addressed in Dropped the unconditional fallback chunk cap. Added end-to-end coverage of the agent → gateway → consumer wiring. The old
A regression through the terminal API-error path (broken Test run: |
…k chunks to prevent Telegram flood
2a30155 to
fe41fa5
Compare
What does this PR do?
When the model API is down (rate-limit / 429 / connection drop), the
GatewayStreamConsumerflushes its accumulated streaming buffer to Telegram as individual messages — which can be ~100 messages containing the full system prompt / skill context. This PR introduces an API-failure guard that suppresses that flood.API-failure detection:
agent.conversation_looprecordsagent.api_failed_summaryon failure (reset toNoneat every turn start to prevent stale errors from suppressing a valid next turn). The gateway wires this into the stream consumer viaapi_error_fn. When the API failed at final flush, the consumer suppresses the accumulated buffer and delivers one clean error message instead.Related Issue
N/A — reported by multiple Telegram users in community channels.
Type of Change
Changes Made
agent/agent_init.py: Initializeagent.api_failed_summary = None(line ~679).agent/conversation_loop.py: Setagent.api_failed_summaryat both terminal-error exit points; reset it toNoneat every turn start.gateway/stream_consumer.py: Addapi_error_fnparam,_send_api_error_finalhelper, api-error guard at final flush and at mid-stream overflow split.gateway/run.py: Wireapi_error_fnlambda intoGatewayStreamConsumerconstructor, reading the liveagent.api_failed_summarylazily so a summary recorded between the laston_deltaand final flush is honored.tests/gateway/test_stream_consumer_flood_guard.py: New test file with 6 tests across 2 classes:TestApiErrorSuppression— direct consumer test: API failure suppresses buffer; no-error case still delivers.TestApiErrorEndToEndWiring— exercises the real lambda shape fromgateway/run.py(lazygetattron the liveagent_holder[0]) so a regression in either the agent-state lifecycle or the gateway wiring surfaces here. Covers: terminal API failure at final flush, late failure recorded after the last delta, no-failure case, and theagent_holder[0] is Nonedefensive path.How to Test
Reproduction (before the fix): Trigger a Telegram bot session while the model API returns 429/rate-limit. Observe ~100 messages dumped containing the full system prompt / skill context.
After the fix: Repeat step 1. Confirm:
⚠️ Model API error — no response was generated. <summary>Regression: Run
./venv/bin/python -m pytest tests/gateway/test_stream_consumer*.py -q. All 214 tests pass (208 existing + 6 new).Checklist
Code
fix(gateway): …)./venv/bin/python -m pytest tests/gateway/test_stream_consumer*.py -qand all 214 tests passTestApiErrorSuppressionandTestApiErrorEndToEndWiringDocumentation & Housekeeping
docs/, docstrings) — or N/A (no public API change; behavior described in docstrings and inline comments)cli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/A