mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-17 04:32:31 +03:00
OpencodeExecutor and MimocodeExecutor rotated to the next account only on HTTP 429. A network exception (timeout, connection refused/reset) on one account instead propagated out of execute() and failed the whole request, even when other accounts remained available. Both executors now rotate on a network exception only when the failed account has its own dedicated proxy (account.proxy !== null) — a dead proxy is genuinely account-scoped, so rotating away from it is safe. Accounts sharing the default egress (no proxy configured) trigger the same cooldown and are skipped for the rest of the request once the shared egress is known down, but a later account with its own dedicated proxy is still tried normally — a throw on a proxy-less account no longer strands a proxied account further in the rotation. This behavior is gated behind NETWORK_ROTATION_SHARED_EGRESS_GUARD (Feature Flag, default on); disabled, it reproduces the immediate-propagation behavior this fix started from. The shared rotation mechanics (pickAccount/markCooldown/markSuccess) are extracted into executors/accountRotation.ts, used by both executors — they had independently implemented the same round-robin+cooldown skeleton. This also fixes an identical, pre-existing bug in MimocodeExecutor that predates this PR: its catch block called markCooldown unconditionally on any throw, with no proxy check and no warn log (a silent exception swallow on a path that influences the result). The cooldown formula for both the proxy and shared-egress cases reuses the repo's already-established "transient, not clearly attributable" constants (errorConfig.ts TRANSIENT_COOLDOWN_MS/COOLDOWN_MS.transientMax, already used by accountFallback.ts for network-error classification) instead of introducing a separate value. MimocodeExecutor's network-error 502 body also now goes through buildErrorBody()/sanitizeErrorMessage() instead of embedding the raw caught error message directly (Hard Rule #12), matching the sanitization already used on its #2101 malformed-request path. Validated by TDD (Hard Rule #18): tests/unit/account-rotation.test.ts covers the shared module directly; opencode-proxy-rotation-4954.test.ts and mimocode-executor.test.ts cover the proxy-configured rotation path, the mixed-fleet case, the shared-egress single-network-call case, and the NETWORK_ROTATION_SHARED_EGRESS_GUARD-disabled legacy path, for each executor. tsc, lint, and the provider golden-path gates (check:provider-consistency, check:provider-assets, provider-translate-path-golden.test.ts) are clean on all touched files. Co-authored-by: Max <maxmad64@gmail.com>
126 lines
3.8 KiB
TypeScript
126 lines
3.8 KiB
TypeScript
import { getFeatureFlagOverride } from "@/lib/db/featureFlags";
|
|
import {
|
|
FEATURE_FLAG_DEFINITIONS,
|
|
type FeatureFlagDefinition,
|
|
} from "@/shared/constants/featureFlagDefinitions";
|
|
|
|
/**
|
|
* Resolve the effective value of a feature flag.
|
|
* Priority: DB override > process.env > definition.defaultValue
|
|
*/
|
|
export function resolveFeatureFlag(key: string): string {
|
|
const dbOverride = getFeatureFlagOverride(key);
|
|
if (dbOverride !== undefined) return dbOverride;
|
|
|
|
const envValue = process.env[key];
|
|
if (envValue !== undefined && envValue !== "") return envValue;
|
|
|
|
const definition = FEATURE_FLAG_DEFINITIONS.find((d) => d.key === key);
|
|
return definition?.defaultValue ?? "false";
|
|
}
|
|
|
|
/**
|
|
* Check if a boolean feature flag is enabled.
|
|
* Treats "true", "1", "yes" as enabled.
|
|
*/
|
|
export function isFeatureFlagEnabled(key: string): boolean {
|
|
const value = resolveFeatureFlag(key);
|
|
return value === "true" || value === "1" || value === "yes";
|
|
}
|
|
|
|
/**
|
|
* Resolve all feature flags with their effective values and sources.
|
|
*/
|
|
export function resolveAllFeatureFlags(): Array<{
|
|
key: string;
|
|
effectiveValue: string;
|
|
source: "db" | "env" | "default";
|
|
definition: FeatureFlagDefinition;
|
|
}> {
|
|
return FEATURE_FLAG_DEFINITIONS.map((definition) => {
|
|
const dbOverride = getFeatureFlagOverride(definition.key);
|
|
if (dbOverride !== undefined) {
|
|
return { key: definition.key, effectiveValue: dbOverride, source: "db", definition };
|
|
}
|
|
const envValue = process.env[definition.key];
|
|
if (envValue !== undefined && envValue !== "") {
|
|
return { key: definition.key, effectiveValue: envValue, source: "env", definition };
|
|
}
|
|
return {
|
|
key: definition.key,
|
|
effectiveValue: definition.defaultValue,
|
|
source: "default",
|
|
definition,
|
|
};
|
|
});
|
|
}
|
|
|
|
// Backward-compatible wrappers
|
|
export function isRequireApiKeyEnabled(): boolean {
|
|
try {
|
|
return isFeatureFlagEnabled("REQUIRE_API_KEY");
|
|
} catch (error) {
|
|
console.error(
|
|
"[featureFlags] Failed to resolve REQUIRE_API_KEY, defaulting to required:",
|
|
error instanceof Error ? error.message : error
|
|
);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
export function isCcCompatibleProviderEnabled(): boolean {
|
|
return isFeatureFlagEnabled("ENABLE_CC_COMPATIBLE_PROVIDER");
|
|
}
|
|
|
|
export function isApiKeyRevealEnabledFlag(): boolean {
|
|
try {
|
|
return isFeatureFlagEnabled("ALLOW_API_KEY_REVEAL");
|
|
} catch (error) {
|
|
console.error(
|
|
"[featureFlags] Failed to resolve ALLOW_API_KEY_REVEAL, defaulting to disabled:",
|
|
error instanceof Error ? error.message : error
|
|
);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function isModelCatalogNamesEnabled(): boolean {
|
|
return isFeatureFlagEnabled("MODEL_CATALOG_INCLUDE_NAMES");
|
|
}
|
|
|
|
export type ModelsCatalogPrefixMode = "dual" | "alias" | "canonical";
|
|
|
|
export function getModelsCatalogPrefixMode(): ModelsCatalogPrefixMode {
|
|
const value = resolveFeatureFlag("MODELS_CATALOG_PREFIX_MODE");
|
|
if (value === "alias" || value === "canonical") return value;
|
|
return "dual";
|
|
}
|
|
|
|
export function isArenaEloSyncEnabled(): boolean {
|
|
return isFeatureFlagEnabled("ARENA_ELO_SYNC_ENABLED");
|
|
}
|
|
|
|
export function isControlPlaneProxyDirectFallbackEnabled(): boolean {
|
|
try {
|
|
return isFeatureFlagEnabled("OMNIROUTE_CONTROL_PLANE_PROXY_DIRECT_FALLBACK");
|
|
} catch (error) {
|
|
console.error(
|
|
"[featureFlags] Failed to resolve OMNIROUTE_CONTROL_PLANE_PROXY_DIRECT_FALLBACK, defaulting to disabled:",
|
|
error instanceof Error ? error.message : error
|
|
);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function isNetworkRotationSharedEgressGuardEnabled(): boolean {
|
|
try {
|
|
return isFeatureFlagEnabled("NETWORK_ROTATION_SHARED_EGRESS_GUARD");
|
|
} catch (error) {
|
|
console.error(
|
|
"[featureFlags] Failed to resolve NETWORK_ROTATION_SHARED_EGRESS_GUARD, defaulting to enabled:",
|
|
error instanceof Error ? error.message : error
|
|
);
|
|
return true;
|
|
}
|
|
}
|