feat(agent): turn-level retry with idle timeout, plus withToolRetry helper#52
Closed
robert-j-y wants to merge 2 commits into
Closed
feat(agent): turn-level retry with idle timeout, plus withToolRetry helper#52robert-j-y wants to merge 2 commits into
robert-j-y wants to merge 2 commits into
Conversation
…elper When a turn's response stream dies mid-loop (ends without a terminal event, fails, or hangs silently), the whole callModel promise rejected and callers could only restart from turn 0, re-running every tool call. retryTurn re-sends just the failed turn's request with the accumulated conversation state intact, and idleTimeoutMs converts silent stream hangs into retryable failures. Retries surface as turn.retry events. withToolRetry separately absorbs transient tool execute throws so they don't burn a model turn. Co-authored-by: Cursor <cursoragent@cursor.com>
Collaborator
Author
|
Parking this for now per team discussion — we'll handle recovery at the application layer (fusion/server-tools in openrouter-web) until the comments-based stream-liveness approach is designed for the SDK. Branch stays up for reference. |
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.
Summary
Fusion benchmark failures traced to mid-loop stream deaths: a turn's SSE stream opens fine, emits ~0–5 tokens, then either ends with no terminal event (
Follow-up stream ended without a completed response) or hangs silently forever. Today the wholecallModelpromise rejects and callers can only restart from turn 0 — re-running every tool call and losing all gathered context.retryTurnoption oncallModel—{ limit, isRetryable, backoffMs, idleTimeoutMs }. Wraps each turn's send+consume (initial, follow-up, and final-response turns) and re-sends just the failed turn's request with the accumulated conversation state intact. Typed errors (TurnStreamEndedError,TurnResponseFailedError,TurnIdleTimeoutError) drive the default retryable classification (5xx/transport/stream-death yes; 4xx account-state no).idleTimeoutMs— converts silent stream hangs (stream never closes, nothing throws) into retryable turn failures instead of relying on an external inactivity guard killing the request.turn.retryevent — emitted on the response stream before each retry attempt so consumers can observe retries.withToolRetryhelper — separately wraps a tool'sexecutewith bounded retries so transient tool throws are absorbed instead of being serialized as{"error": ...}results that burn a model turn (per Matt's suggestion).Includes changeset for a minor release (0.8.0). Note: branch builds on the two unpushed atomicity commits (
ce00bf9,7b6704a).Test plan
tests/unit/turn-retry.test.ts(follow-up/initial turn retries, idle timeout, retry limits, customisRetryable, backoff,turn.retryevent semantics)tests/unit/tool-retry.test.ts(regular + generator tools, limit exhaustion,isRetryable,onRetry, type preservation)Made with Cursor