diff --git a/open-sse/services/antigravityProjectPersist.ts b/open-sse/services/antigravityProjectPersist.ts index 1068c4d3ec..2ebddb97c0 100644 --- a/open-sse/services/antigravityProjectPersist.ts +++ b/open-sse/services/antigravityProjectPersist.ts @@ -14,6 +14,25 @@ import { updateProviderConnection } from "@/lib/db/providers"; +/** + * Keep only Antigravity connections that already have a stored projectId when + * building the reset-aware pool (#8894). A connection without a projectId cannot + * be quota-scored, so leaving it in the pool only produces unrankable candidates. + */ +export function preferAntigravityConnectionsWithStoredProject( + connections: Array> +): Array> { + return connections.filter((connection) => { + const direct = connection.projectId; + const psd = connection.providerSpecificData as Record | undefined; + const psdProjectId = psd?.projectId; + return ( + (typeof direct === "string" && direct.trim().length > 0) || + (typeof psdProjectId === "string" && psdProjectId.trim().length > 0) + ); + }); +} + /** * Write `discoveredProjectId` onto both the `projectId` column and * `providerSpecificData.projectId` for `connectionId`, preserving any other diff --git a/tests/unit/antigravity-project-persist-pool-filter.test.ts b/tests/unit/antigravity-project-persist-pool-filter.test.ts new file mode 100644 index 0000000000..db3deeb5a9 --- /dev/null +++ b/tests/unit/antigravity-project-persist-pool-filter.test.ts @@ -0,0 +1,32 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { preferAntigravityConnectionsWithStoredProject } from "@omniroute/open-sse/services/antigravityProjectPersist.ts"; + +test("keeps connections with a projectId on the column or in providerSpecificData", () => { + const kept = preferAntigravityConnectionsWithStoredProject([ + { id: "a", projectId: "projects/a" }, + { id: "b", providerSpecificData: { projectId: "projects/b" } }, + ]); + + assert.deepEqual( + kept.map((c) => c.id), + ["a", "b"] + ); +}); + +test("drops connections whose projectId is missing, blank or not a string", () => { + const kept = preferAntigravityConnectionsWithStoredProject([ + { id: "missing" }, + { id: "blank", projectId: " " }, + { id: "null", projectId: null }, + { id: "numeric", projectId: 42 }, + { id: "blank-psd", providerSpecificData: { projectId: "" } }, + { id: "null-psd", providerSpecificData: null }, + ]); + + assert.deepEqual(kept, []); +}); + +test("returns an empty pool for an empty input instead of throwing", () => { + assert.deepEqual(preferAntigravityConnectionsWithStoredProject([]), []); +});