fix(fallback): guard duplicate prompt injections

This commit is contained in:
YeonGyu-Kim
2026-05-14 01:03:19 +09:00
parent ea55c385bb
commit 5ffbe0e24e
7 changed files with 257 additions and 15 deletions
@@ -12,6 +12,19 @@ type ModelFallbackStateLike = {
pending: boolean
}
function canonicalizeModelIDForDuplicateCheck(modelID: string): string {
return modelID.toLowerCase().replace(/\./g, "-")
}
function isSameFailedModel(
state: ModelFallbackStateLike,
providerID: string,
modelID: string,
): boolean {
return state.providerID.toLowerCase() === providerID.toLowerCase()
&& canonicalizeModelIDForDuplicateCheck(state.modelID) === canonicalizeModelIDForDuplicateCheck(modelID)
}
export type ModelFallbackStateController = {
lastToastKey: Map<string, string>
setSessionFallbackChain: (sessionID: string, fallbackChain: FallbackEntry[] | undefined) => void
@@ -84,6 +97,11 @@ export function createModelFallbackStateController(input: {
return false
}
if (existing.attemptCount > 0 && isSameFailedModel(existing, currentProviderID, currentModelID)) {
log(`[model-fallback] Ignoring duplicate fallback arm for already handled model in session: ${sessionID}`)
return false
}
existing.providerID = currentProviderID
existing.modelID = currentModelID
existing.pending = true