From 5088751306a8ffe766db2eac4cd9eba84b0b9e2e Mon Sep 17 00:00:00 2001 From: Garry Tan Date: Tue, 9 Jun 2026 21:11:57 -0700 Subject: [PATCH] test(isolation): fix shard-order flakes exposed by #1972's new test files Adding 3 new test files reshuffled the hash-based shards, exposing two pre-existing test-isolation bugs: - cycle-consolidate.test.ts assumed the global legacy-embedding preload's 1536-d gateway config still held at initSchema, but a co-sharded test that calls resetGateway() in teardown nulls it, so initSchema fell back to the 1280-d default and built a halfvec(1280) facts column its 1536-d fixtures can't fill. Re-pin the legacy OpenAI/1536 config in beforeAll (the pattern legacy-embedding-preload.ts documents for 1536-d fixture tests). - db-lock-heartbeat-takeover.test.ts (merged from master's #1794) mutated process.env.GBRAIN_LOCK_STEAL_GRACE_SECONDS raw, tripping check:test-isolation rule R1. Convert to withEnv(). --- test/cycle-consolidate.test.ts | 15 +++++++++++++++ test/db-lock-heartbeat-takeover.test.ts | 19 +++++++------------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/test/cycle-consolidate.test.ts b/test/cycle-consolidate.test.ts index 812bffcad..fe3462bc5 100644 --- a/test/cycle-consolidate.test.ts +++ b/test/cycle-consolidate.test.ts @@ -10,6 +10,7 @@ import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test'; import { PGLiteEngine } from '../src/core/pglite-engine.ts'; +import { configureGateway } from '../src/core/ai/gateway.ts'; import { runPhaseConsolidate } from '../src/core/cycle/phases/consolidate.ts'; let engine: PGLiteEngine; @@ -17,6 +18,20 @@ let engine: PGLiteEngine; beforeAll(async () => { engine = new PGLiteEngine(); await engine.connect({}); + // initSchema() bakes the facts.embedding dim from the gateway's configured + // embedding model; the default is now 1280-d (ZE). This file's fixtures are + // 1536-d, so pin the legacy 1536-d OpenAI config (matching + // test/helpers/legacy-embedding-preload.ts) right before initSchema. The + // global preload sets this, but a co-sharded test that calls resetGateway() + // in its teardown nulls it, leaving initSchema to fall back to the 1280-d + // default and build a halfvec(1280) column the 1536-d inserts can't fill. + // Re-pinning here makes the schema deterministic regardless of shard + // neighbors (surfaced when #1972's new test files reshuffled the shards). + configureGateway({ + embedding_model: 'openai:text-embedding-3-large', + embedding_dimensions: 1536, + env: { ...process.env }, + }); await engine.initSchema(); }); diff --git a/test/db-lock-heartbeat-takeover.test.ts b/test/db-lock-heartbeat-takeover.test.ts index fff0d4381..a4f479547 100644 --- a/test/db-lock-heartbeat-takeover.test.ts +++ b/test/db-lock-heartbeat-takeover.test.ts @@ -16,6 +16,7 @@ import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test'; import { hostname } from 'os'; import { PGLiteEngine } from '../src/core/pglite-engine.ts'; +import { withEnv } from './helpers/with-env.ts'; import { tryAcquireDbLock, resolveStealGraceSeconds, @@ -81,22 +82,16 @@ describe('resolveStealGraceSeconds', () => { expect(resolveStealGraceSeconds(1)).toBe(60); }); - test('env override wins', () => { - process.env.GBRAIN_LOCK_STEAL_GRACE_SECONDS = '123'; - try { + test('env override wins', async () => { + await withEnv({ GBRAIN_LOCK_STEAL_GRACE_SECONDS: '123' }, async () => { expect(resolveStealGraceSeconds(30)).toBe(123); - } finally { - delete process.env.GBRAIN_LOCK_STEAL_GRACE_SECONDS; - } + }); }); - test('bad env override falls back to derived', () => { - process.env.GBRAIN_LOCK_STEAL_GRACE_SECONDS = 'nope'; - try { + test('bad env override falls back to derived', async () => { + await withEnv({ GBRAIN_LOCK_STEAL_GRACE_SECONDS: 'nope' }, async () => { expect(resolveStealGraceSeconds(30)).toBe(600); - } finally { - delete process.env.GBRAIN_LOCK_STEAL_GRACE_SECONDS; - } + }); }); });