fix(providers): clear stale locks after validation (#3830)

Clear stale connection health state (modelLock_*, backoffLevel,
rateLimitedUntil, errorCode) whenever a connection is explicitly
marked active after successful validation or OAuth re-login.

Closes #3810
This commit is contained in:
Sutarto Jordan Chrisfivo
2026-09-09 10:24:30 +07:00
parent 4ad1e7a4ba
commit 7fee56bacd
2 changed files with 124 additions and 2 deletions

View File

@@ -10,6 +10,28 @@ const OPTIONAL_FIELDS = [
"consecutiveUseCount", "idToken", "lastRefreshAt",
];
const MODEL_LOCK_PREFIX = "modelLock_";
function resetHealthStateOnActivation(existing, patch) {
if (patch?.testStatus !== "active") return patch;
const normalized = {
...patch,
testStatus: "active",
lastError: Object.hasOwn(patch, "lastError") ? patch.lastError : null,
lastErrorAt: Object.hasOwn(patch, "lastErrorAt") ? patch.lastErrorAt : null,
errorCode: null,
rateLimitedUntil: null,
backoffLevel: 0,
};
for (const key of Object.keys(existing || {})) {
if (key.startsWith(MODEL_LOCK_PREFIX)) normalized[key] = null;
}
return normalized;
}
function rowToConn(row) {
if (!row) return null;
const extra = parseJson(row.data, {});
@@ -147,7 +169,8 @@ export async function createProviderConnection(data) {
// access_token: never dedup — user manages duplicates manually
if (existing) {
const merged = { ...existing, ...data, updatedAt: now };
const normalized = resetHealthStateOnActivation(existing, data);
const merged = { ...existing, ...normalized, updatedAt: now };
upsert(db, merged);
result = merged;
return;
@@ -196,7 +219,8 @@ export async function updateProviderConnection(id, data) {
const row = db.get(`SELECT * FROM providerConnections WHERE id = ?`, [id]);
if (!row) { result = null; return; }
const existing = rowToConn(row);
const merged = { ...existing, ...data, updatedAt: new Date().toISOString() };
const normalized = resetHealthStateOnActivation(existing, data);
const merged = { ...existing, ...normalized, updatedAt: new Date().toISOString() };
upsert(db, merged);
if (data.priority !== undefined) reorderInTx(db, existing.provider);
result = merged;