-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix: handle session.status cooldown retries in runtime fallback #2368
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
Open
code-yeongyu
wants to merge
7
commits into
dev
Choose a base branch
from
fix/issue-2301
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
80dee4d
fix(shared): add stable retry status dedupe helpers
code-yeongyu 2c6ba98
fix(runtime-fallback): match cooldown retry status messages
code-yeongyu 7f86103
fix(runtime-fallback): add session.status retry handler
code-yeongyu e7e6bd0
fix(runtime-fallback): route session.status events
code-yeongyu af32f2e
test(runtime-fallback): cover session.status retry fallback
code-yeongyu 6edbc96
fix(plugin): gate model-fallback session.status retries
code-yeongyu b1946a6
test(plugin): cover session.status dedupe and runtime fallback gating
code-yeongyu 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
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
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,160 @@ | ||
| import type { HookDeps } from "./types" | ||
| import type { AutoRetryHelpers } from "./auto-retry" | ||
| import { HOOK_NAME } from "./constants" | ||
| import { log } from "../../shared/logger" | ||
| import { isRetryableError } from "./error-classifier" | ||
| import { createFallbackState, prepareFallback } from "./fallback-state" | ||
| import { getFallbackModelsForSession } from "./fallback-models" | ||
| import { extractRetryAttempt, extractRetryStatusModel, normalizeRetryStatusMessage } from "../../shared/retry-status-utils" | ||
|
|
||
| type SessionStatus = { | ||
| type?: string | ||
| message?: string | ||
| attempt?: number | ||
| } | ||
|
|
||
| function resolveInitialModel( | ||
| props: Record<string, unknown> | undefined, | ||
| retryMessage: string, | ||
| resolvedAgent: string | undefined, | ||
| pluginConfig: HookDeps["pluginConfig"], | ||
| ): string | undefined { | ||
| const eventModel = typeof props?.model === "string" ? props.model : undefined | ||
| if (eventModel) { | ||
| return eventModel | ||
| } | ||
|
|
||
| const retryModel = extractRetryStatusModel(retryMessage) | ||
| if (retryModel) { | ||
| return retryModel | ||
| } | ||
|
|
||
| const agentConfig = resolvedAgent | ||
| ? pluginConfig?.agents?.[resolvedAgent as keyof typeof pluginConfig.agents] | ||
| : undefined | ||
|
|
||
| return typeof agentConfig?.model === "string" ? agentConfig.model : undefined | ||
| } | ||
|
|
||
| export function createSessionStatusHandler(deps: HookDeps, helpers: AutoRetryHelpers): { | ||
| clearRetryKey: (sessionID: string) => void | ||
| handleSessionStatus: (props: Record<string, unknown> | undefined) => Promise<void> | ||
| } { | ||
| const { | ||
| config, | ||
| pluginConfig, | ||
| sessionStates, | ||
| sessionLastAccess, | ||
| sessionRetryInFlight, | ||
| sessionAwaitingFallbackResult, | ||
| } = deps | ||
| const sessionStatusRetryKeys = new Map<string, string>() | ||
|
|
||
| const clearRetryKey = (sessionID: string): void => { | ||
| sessionStatusRetryKeys.delete(sessionID) | ||
| } | ||
|
|
||
| const handleSessionStatus = async (props: Record<string, unknown> | undefined): Promise<void> => { | ||
| const sessionID = props?.sessionID as string | undefined | ||
| const status = props?.status as SessionStatus | undefined | ||
| const agent = props?.agent as string | undefined | ||
| const timeoutEnabled = config.timeout_seconds > 0 | ||
|
|
||
| if (!sessionID || status?.type !== "retry" || !timeoutEnabled) { | ||
| return | ||
| } | ||
|
|
||
| const retryMessage = typeof status.message === "string" ? status.message : "" | ||
| if (!retryMessage || !isRetryableError({ message: retryMessage }, config.retry_on_errors)) { | ||
| return | ||
| } | ||
|
|
||
| const currentState = sessionStates.get(sessionID) | ||
| const retryAttempt = extractRetryAttempt(status.attempt, retryMessage) | ||
| const retryModel = | ||
| (typeof props?.model === "string" ? props.model : undefined) ?? | ||
| extractRetryStatusModel(retryMessage) ?? | ||
| currentState?.currentModel ?? | ||
| "unknown-model" | ||
| const retryKey = `${retryAttempt}:${retryModel}:${normalizeRetryStatusMessage(retryMessage)}` | ||
|
|
||
| if (sessionStatusRetryKeys.get(sessionID) === retryKey) { | ||
| return | ||
| } | ||
| sessionStatusRetryKeys.set(sessionID, retryKey) | ||
|
|
||
| if (sessionRetryInFlight.has(sessionID)) { | ||
| log(`[${HOOK_NAME}] Overriding in-flight retry due to provider session.status retry signal`, { | ||
| sessionID, | ||
| retryModel, | ||
| }) | ||
| await helpers.abortSessionRequest(sessionID, "session.status.retry-signal") | ||
| sessionRetryInFlight.delete(sessionID) | ||
| } | ||
|
|
||
| sessionAwaitingFallbackResult.delete(sessionID) | ||
|
|
||
| const resolvedAgent = await helpers.resolveAgentForSessionFromContext(sessionID, agent) | ||
| const fallbackModels = getFallbackModelsForSession(sessionID, resolvedAgent, pluginConfig) | ||
|
|
||
| if (fallbackModels.length === 0) { | ||
| log(`[${HOOK_NAME}] No fallback models configured`, { sessionID, agent: resolvedAgent ?? agent }) | ||
| return | ||
| } | ||
|
|
||
| let state = currentState | ||
| if (!state) { | ||
| const initialModel = resolveInitialModel(props, retryMessage, resolvedAgent, pluginConfig) | ||
| if (!initialModel) { | ||
| log(`[${HOOK_NAME}] session.status retry missing model info, cannot fallback`, { sessionID }) | ||
| return | ||
| } | ||
|
|
||
| state = createFallbackState(initialModel) | ||
| sessionStates.set(sessionID, state) | ||
| } | ||
|
|
||
| sessionLastAccess.set(sessionID, Date.now()) | ||
|
|
||
| if (state.pendingFallbackModel) { | ||
| log(`[${HOOK_NAME}] Clearing pending fallback due to provider session.status retry signal`, { | ||
| sessionID, | ||
| pendingFallbackModel: state.pendingFallbackModel, | ||
| }) | ||
| state.pendingFallbackModel = undefined | ||
| } | ||
|
|
||
| log(`[${HOOK_NAME}] Detected provider auto-retry signal in session.status`, { | ||
| sessionID, | ||
| model: state.currentModel, | ||
| retryAttempt, | ||
| }) | ||
|
|
||
| const result = prepareFallback(sessionID, state, fallbackModels, config) | ||
|
|
||
| if (result.success && config.notify_on_fallback) { | ||
| await deps.ctx.client.tui | ||
| .showToast({ | ||
| body: { | ||
| title: "Model Fallback", | ||
| message: `Switching to ${result.newModel?.split("/").pop() || result.newModel} for next request`, | ||
| variant: "warning", | ||
| duration: 5000, | ||
| }, | ||
| }) | ||
| .catch(() => {}) | ||
| } | ||
|
|
||
| if (result.success && result.newModel) { | ||
| await helpers.autoRetryWithFallback(sessionID, result.newModel, resolvedAgent, "session.status") | ||
| return | ||
| } | ||
|
|
||
| log(`[${HOOK_NAME}] Fallback preparation failed`, { sessionID, error: result.error }) | ||
| } | ||
|
|
||
| return { | ||
| clearRetryKey, | ||
| handleSessionStatus, | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
P2: Clear the session status retry key when a request completes (
session.idle,session.stop,session.error) to avoid incorrectly deduplicating retry events across different requests in the same session.Prompt for AI agents