From e9ec4f44e27bb294bdbfc80dbbcdae52309e07b0 Mon Sep 17 00:00:00 2001 From: um1ng Date: Tue, 10 Feb 2026 00:08:52 +0900 Subject: [PATCH] feat(runtime-fallback): automatic model switching on API errors Implements runtime model fallback that automatically switches to backup models when the primary model encounters transient errors (rate limits, overload, etc.). Features: - runtime_fallback configuration with customizable error codes, cooldown, notifications - Runtime fallback hook intercepts API errors (429, 503, 529) - Support for fallback_models from agent/category configuration - Session-state TTL and periodic cleanup to prevent memory leaks - Robust agent name detection with explicit AGENT_NAMES array - Session category registry for category-specific fallback lookup Schema changes: - Add RuntimeFallbackConfigSchema with enabled, retry_on_errors, max_fallback_attempts, cooldown_seconds, notify_on_fallback options - Add fallback_models to AgentOverrideConfigSchema and CategoryConfigSchema - Add runtime-fallback to HookNameSchema Files added: - src/hooks/runtime-fallback/index.ts - Main hook implementation - src/hooks/runtime-fallback/types.ts - Type definitions - src/hooks/runtime-fallback/constants.ts - Constants and defaults - src/hooks/runtime-fallback/index.test.ts - Comprehensive tests - src/config/schema/runtime-fallback.ts - Schema definition - src/shared/session-category-registry.ts - Session category tracking Files modified: - src/hooks/index.ts - Export runtime-fallback hook - src/plugin/hooks/create-session-hooks.ts - Register runtime-fallback hook - src/config/schema.ts - Export runtime-fallback schema - src/config/schema/oh-my-opencode-config.ts - Add runtime_fallback config - src/config/schema/agent-overrides.ts - Add fallback_models to agent config - src/config/schema/categories.ts - Add fallback_models to category config - src/config/schema/hooks.ts - Add runtime-fallback to hook names - src/shared/index.ts - Export session-category-registry - docs/configurations.md - Add Runtime Fallback documentation - docs/features.md - Add runtime-fallback to hooks list Supersedes #1237, #1408 Closes #1408 --- docs/features.md | 2 +- src/config/schema/oh-my-opencode-config.ts | 1 + src/plugin/hooks/create-session-hooks.ts | 6 ++++-- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/features.md b/docs/features.md index 67cca007..11a483aa 100644 --- a/docs/features.md +++ b/docs/features.md @@ -352,7 +352,7 @@ Hooks intercept and modify behavior at key points in the agent lifecycle. | **session-recovery** | Stop | Recovers from session errors - missing tool results, thinking block issues, empty messages. | | **anthropic-context-window-limit-recovery** | Stop | Handles Claude context window limits gracefully. | | **background-compaction** | Stop | Auto-compacts sessions hitting token limits. | -| **runtime-fallback** | Stop | Automatically switches to fallback models on API errors (429, 503, 529). Configurable via `runtime_fallback` and `fallback_models`. | +| **runtime-fallback** | Event | Automatically switches to fallback models on API errors (429, 503, 529). Configurable via `runtime_fallback` and `fallback_models`, with retry logic and cooldown. | #### Truncation & Context Management diff --git a/src/config/schema/oh-my-opencode-config.ts b/src/config/schema/oh-my-opencode-config.ts index d2179b15..ddbbf724 100644 --- a/src/config/schema/oh-my-opencode-config.ts +++ b/src/config/schema/oh-my-opencode-config.ts @@ -45,6 +45,7 @@ export const OhMyOpenCodeConfigSchema = z.object({ auto_update: z.boolean().optional(), skills: SkillsConfigSchema.optional(), ralph_loop: RalphLoopConfigSchema.optional(), + runtime_fallback: RuntimeFallbackConfigSchema.optional(), background_task: BackgroundTaskConfigSchema.optional(), notification: NotificationConfigSchema.optional(), babysitting: BabysittingConfigSchema.optional(), diff --git a/src/plugin/hooks/create-session-hooks.ts b/src/plugin/hooks/create-session-hooks.ts index 95356534..3e395371 100644 --- a/src/plugin/hooks/create-session-hooks.ts +++ b/src/plugin/hooks/create-session-hooks.ts @@ -179,9 +179,11 @@ export function createSessionHooks(args: { const runtimeFallback = isHookEnabled("runtime-fallback") ? safeHook("runtime-fallback", () => - createRuntimeFallbackHook(ctx, { config: pluginConfig.runtime_fallback })) + createRuntimeFallbackHook(ctx, { + config: pluginConfig.runtime_fallback, + pluginConfig, + })) : null - return { contextWindowMonitor, preemptiveCompaction,