diff --git a/packages/backend/src/cloud/credential.service.ts b/packages/backend/src/cloud/credential.service.ts index f079de9..c78e63b 100644 --- a/packages/backend/src/cloud/credential.service.ts +++ b/packages/backend/src/cloud/credential.service.ts @@ -456,8 +456,15 @@ export async function getAndValidateCredential(cloudType: string, ipAddress?: st const ipTodayCount = ipCountRow?.total || 0; - if (ipTodayCount < 3) { - // First 2 saves — use a primary account (is_primary=1), fallback to any healthy + // How many primary accounts does this cloud type have? + const primaryCountRow = db.prepare( + `SELECT COUNT(*) as c FROM cloud_configs WHERE cloud_type = ? AND is_primary = 1 AND is_active = 1` + ).get(cloudType) as { c: number }; + const primaryCount = primaryCountRow?.c || 0; + const primaryThreshold = primaryCount * 2; // Each primary account gets 2 uses per IP + + if (ipTodayCount < primaryThreshold) { + // First N saves (primaryCount × 2) — use primary accounts (is_primary=1), fallback to any healthy config = db.prepare( `SELECT * FROM cloud_configs WHERE cloud_type = ? AND is_active = 1 @@ -466,7 +473,7 @@ export async function getAndValidateCredential(cloudType: string, ipAddress?: st LIMIT 1` ).get(cloudType) as CloudConfig | undefined; } else { - // 3rd+ save — exclude accounts this IP has already used today, + // After primary threshold — exclude accounts this IP has already used today, // fall back to other available accounts round-robin const usedConfigIds = db.prepare( `SELECT DISTINCT config_id FROM ip_daily_save_counts