Q70: provider cooldowns survive restarts

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-08 12:07:17 -04:00
1 parent c6a0460035
commit bb279ff85a
6 files changed
+101 -54

No files matched your search

+1 -1
View File
@@ -24,7 +24,7 @@ describe("domain services", () => {
const connection = createConnection();
const rows = connection.db.prepare("select version from schema_migrations order by version asc").all() as Array<{ version: number }>;
expect(rows.map((row) => row.version)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74]);
expect(rows.map((row) => row.version)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75]);
connection.close();
});
+38 -2
View File
@@ -1,13 +1,18 @@
import { randomUUID } from "node:crypto";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { describe, expect, test, vi } from "vitest";
import { DatabaseConnection } from "../../src/main/database/connection";
import { OrganizationService } from "../../src/main/services/organization-service";
import { ProviderCooldowns } from "../../src/main/services/provider-cooldowns";
describe("provider cooldown sizing", () => {
test("a provider-given wait is honored; otherwise parks back off and reset once a request is served", () => {
vi.spyOn(console, "warn").mockImplementation(() => {});
vi.spyOn(console, "info").mockImplementation(() => {});
const { connection, provider } = company();
let now = 0;
const cooldowns = new ProviderCooldowns(() => now);
const provider = { id: "prv_1", label: "OpenAI" };
const cooldowns = new ProviderCooldowns(connection.db, () => now);
const parkedFor = (): number => {
let elapsed = 0;
while (cooldowns.cooledProviderIds().includes(provider.id)) {
@@ -28,5 +33,36 @@ describe("provider cooldown sizing", () => {
cooldowns.park(provider, "429", 5 * 3_600_000);
expect(parkedFor()).toBe(60 * 60_000);
vi.restoreAllMocks();
connection.close();
});
test("a park survives a restart, so the queue does not hammer a provider that is still down", () => {
vi.spyOn(console, "warn").mockImplementation(() => {});
vi.spyOn(console, "info").mockImplementation(() => {});
const { connection, provider } = company();
let now = 1_000_000;
new ProviderCooldowns(connection.db, () => now).park(provider, "503", null);
new ProviderCooldowns(connection.db, () => now).park(provider, "503", null);
const afterRestart = new ProviderCooldowns(connection.db, () => now);
expect(afterRestart.cooledProviderIds()).toEqual([provider.id]);
now += 4 * 60_000 - 1;
expect(afterRestart.cooledProviderIds()).toEqual([provider.id]);
now += 1;
expect(afterRestart.cooledProviderIds()).toEqual([]);
afterRestart.park(provider, "503", null);
now += 8 * 60_000 - 1;
expect(new ProviderCooldowns(connection.db, () => now).cooledProviderIds()).toEqual([provider.id]);
vi.restoreAllMocks();
connection.close();
});
});
function company() {
const connection = new DatabaseConnection(join(tmpdir(), `ac-cooldown-${randomUUID()}.db`));
let orgId = "";
connection.transaction(() => {
orgId = new OrganizationService(connection.db).createOrganization({ organizationName: "Acme", employeeName: "Dana", mission: "m", goal: "g" });
});
const provider = connection.db.prepare("select id, label from providers where org_id = ? and provider_key = 'openai'").get(orgId) as { id: string; label: string };
return { connection, provider };
}