Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export function createAutoRetryDispatcher(
newModel: string,
resolvedAgent: string | undefined,
source: string,
): Promise<void> => {
): Promise<{ accepted: boolean }> => {
if (sessionRetryInFlight.has(sessionID)) {
log(`[${HOOK_NAME}] Retry already in flight, skipping (${source})`, { sessionID })
return
return { accepted: false }
}

const agentSettings = resolvedAgent
Expand All @@ -53,7 +53,7 @@ export function createAutoRetryDispatcher(
if (state) {
state.pendingFallbackPromptMayHaveBeenAccepted = false
}
return
return { accepted: false }
}

const hadAwaitingFallbackResult = sessionAwaitingFallbackResult.has(sessionID)
Expand All @@ -62,6 +62,7 @@ export function createAutoRetryDispatcher(
sessionRetryInFlight.add(sessionID)
let retryDispatched = false
let retryMayHaveBeenAccepted = false
let result: { accepted: boolean } = { accepted: false }
try {
const messagesResp = await ctx.client.session.messages({
path: { id: sessionID },
Expand Down Expand Up @@ -175,14 +176,14 @@ export function createAutoRetryDispatcher(
sessionID,
status: reservedResult.status,
})
return
return { accepted: false }
}
} else if (!isInternalPromptDispatchAccepted(promptResult)) {
log(`[${HOOK_NAME}] Auto-retry skipped by promptAsync gate (${source})`, {
sessionID,
status: promptResult.status,
})
return
return { accepted: false }
}
sessionAwaitingFallbackResult.add(sessionID)
if (hadAwaitingFallbackResult) {
Expand All @@ -193,10 +194,11 @@ export function createAutoRetryDispatcher(
state.pendingFallbackPromptMayHaveBeenAccepted = false
}
retryDispatched = true
result = { accepted: true }
} catch (retryError) {
if (!(retryError instanceof Error)) {
log(`[${HOOK_NAME}] Auto-retry failed (${source})`, { sessionID, error: String(retryError) })
return
return { accepted: false }
}
log(`[${HOOK_NAME}] Auto-retry failed (${source})`, { sessionID, error: String(retryError) })
} finally {
Expand Down Expand Up @@ -226,5 +228,6 @@ export function createAutoRetryDispatcher(
}
}
}
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ export function createAutoRetryHelpers(deps: HookDeps) {
const { clearSessionFallbackTimeout, scheduleSessionFallbackTimeout } = createFallbackTimeoutHelpers(
deps,
abortSessionRequest,
(sessionID, newModel, resolvedAgent, source) =>
autoRetryWithFallback(sessionID, newModel, resolvedAgent, source),
async (sessionID, newModel, resolvedAgent, source) => {
await autoRetryWithFallback(sessionID, newModel, resolvedAgent, source)
},
)

autoRetryWithFallback = createAutoRetryDispatcher(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export function createChatMessageHandler(deps: HookDeps) {
if (
state.currentModel !== state.originalModel &&
!state.pendingFallbackModel &&
!state.failedModels.has(state.originalModel) && // don't restore if original model has failed
!isModelInCooldown(state.originalModel, state, config.cooldown_seconds)
) {
const activeModel = state.originalModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,45 @@ export async function dispatchFallbackRetry(
deps.config,
)

if (result.success && deps.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(
options.sessionID,
result.newModel,
options.resolvedAgent,
options.source,
)
if (!result.success || !result.newModel) {
log(`[${HOOK_NAME}] Fallback preparation failed`, {
sessionID: options.sessionID,
source: options.source,
error: result.error,
})
return
}

log(`[${HOOK_NAME}] Fallback preparation failed`, {
sessionID: options.sessionID,
source: options.source,
error: result.error,
})
const dispatchResult = await helpers.autoRetryWithFallback(
options.sessionID,
result.newModel,
options.resolvedAgent,
options.source,
)

if (deps.config.notify_on_fallback) {
if (dispatchResult.accepted) {
await deps.ctx.client.tui
.showToast({
body: {
title: "Model Fallback",
message: `Switched to ${result.newModel?.split("/").pop() || result.newModel} for next request`,
variant: "warning",
duration: 5000,
},
})
.catch(() => {})
} else {
await deps.ctx.client.tui
.showToast({
body: {
title: "Model Fallback Failed",
message: `Could not switch to ${result.newModel?.split("/").pop() || result.newModel}. Retry blocked.`,
variant: "error",
duration: 8000,
},
})
.catch(() => {})
}
}
}