Files
OmniRoute/tests/unit/codex-session-affinity-reset-aware-5903.test.ts
Diego Rodrigues de Sa e Souza 3d4f3e4960 test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966) (#11968)
* test(infra): retry recursive temp-dir removal instead of failing a shard on ENOTEMPTY (#11966)

Two shards on release/v3.8.51 went red in one day with the same signature —
"ENOTEMPTY, Directory not empty: /tmp/omniroute-<test>-XXXXXX" — from
combo-same-provider-cascade (Unit Tests fast-path 4/4, on a PR that touches only
.github/) and auth-policy-embeddings-webfetch-7785 (the 20k-test TIA step). Both pass
alone and on re-run: the cleanup races something still writing into the directory
(SQLite WAL/-shm checkpoint, a worker, the backup) and under a loaded hosted runner
the window opens. 1154 test files do their own cleanup with
fs.rmSync(dir, { recursive: true, force: true }); 57 already asked for retries.

One-shot codemod (scripts/ad-hoc/codemod-rm-maxretries.mjs, kept for the record):
every rm / rmSync / rmdirSync option object with `recursive: true` and no
`maxRetries` gains `maxRetries: 5, retryDelay: 100` — Node itself then retries
ENOTEMPTY/EBUSY/EPERM for up to ~0.5 s before giving up. 2243 call sites in 1292
files under tests/, the shared tests/_setup/isolateDataDir.ts exit hook included.
Only the option object changes: no call site, assertion or import is touched.

Validation: prettier and ESLint (with the frozen suppressions) clean on all 1292
files; a random 20-file sample runs green (quota-redis-store hangs identically on
the untouched tree — it needs a Redis on localhost, an environment matter). The
four unit shards on this PR are the full run.

* fix(quality): let check-forgotten-sibling-tests read a 1,000-file diff

The gate shells out to `git diff` through execFileSync with Node's default 1 MB
maxBuffer; the 1,292-file codemod in this PR is the first diff large enough to
overflow it, and the gate died with `spawnSync git ENOBUFS` before comparing
anything. 64 MB is far above any real PR and costs nothing when unused.
2026-08-29 01:17:40 -03:00

190 lines
7.4 KiB
TypeScript

// #5903: Codex session affinity must win over a per-request reset-aware
// re-scoring. The reset-aware combo strategy (open-sse/services/combo/quotaStrategies.ts)
// recomputes its "winner" connection on every request and hands it to
// getProviderCredentials as forcedConnectionId (src/sse/handlers/chat.ts).
// Before the fix, forcedConnectionId narrowed the connection pool BEFORE
// session affinity was consulted, so a fresh quota-scoring winner silently
// evicted the existing pin (deleteSessionAccountAffinity) on every request —
// breaking "same session -> reuse pinned account".
//
// This test drives auth.getProviderCredentials directly (the same call shape
// chat.ts uses: sessionKey + forcedConnectionId together) to reproduce the
// bug without needing the full combo/quota-scoring machinery.
import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-codex-affinity-5903-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "codex-affinity-5903-test-secret";
const core = await import("../../src/lib/db/core.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const settingsDb = await import("../../src/lib/db/settings.ts");
const apiKeysDb = await import("../../src/lib/db/apiKeys.ts");
const affinityDb = await import("../../src/lib/db/sessionAccountAffinity.ts");
const auth = await import("../../src/sse/services/auth.ts");
async function resetStorage() {
core.resetDbInstance();
apiKeysDb.resetApiKeyState();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
async function seedConnection(provider: string, overrides: any = {}) {
return providersDb.createProviderConnection({
provider,
authType: overrides.authType || "oauth",
name: overrides.name || `${provider}-${Math.random().toString(16).slice(2, 8)}`,
accessToken: overrides.accessToken || `at-${Math.random().toString(16).slice(2, 10)}`,
refreshToken: overrides.refreshToken,
isActive: overrides.isActive ?? true,
testStatus: overrides.testStatus || "active",
priority: overrides.priority,
providerSpecificData: overrides.providerSpecificData || {},
});
}
test.beforeEach(async () => {
await resetStorage();
});
test.after(async () => {
core.resetDbInstance();
apiKeysDb.resetApiKeyState();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test("codex session affinity wins over a per-request reset-aware forcedConnectionId (#5903)", async () => {
await settingsDb.updateSettings({
fallbackStrategy: "reset-aware",
codexSessionAffinityTtlMs: 60_000,
});
const connectionA = await seedConnection("codex", { name: "codex-reset-aware-a" });
const connectionB = await seedConnection("codex", { name: "codex-reset-aware-b" });
// Request 1: reset-aware quota scoring picks A as the winner for session S.
const request1 = await auth.getProviderCredentials("codex", null, null, "gpt-5.5", {
sessionKey: "session-S",
forcedConnectionId: connectionA.id,
});
assert.equal(
request1?.connectionId,
connectionA.id,
"request 1 should pin to the scored winner A"
);
assert.equal(
affinityDb.getSessionAccountAffinity("session-S", "codex", 60_000)?.connectionId,
connectionA.id,
"affinity row must be created for session-S pointing at A"
);
// Request 2: quota state shifted and reset-aware now scores B higher for
// the SAME session. Without the fix, forcedConnectionId=B narrows the pool
// to just B before affinity is checked, evicting the A pin and re-pinning
// to B. With the fix, the existing active pin (A) must win.
const request2 = await auth.getProviderCredentials("codex", null, null, "gpt-5.5", {
sessionKey: "session-S",
forcedConnectionId: connectionB.id,
});
assert.equal(
request2?.connectionId,
connectionA.id,
"request 2 must still use the pinned connection A, not the freshly re-scored B"
);
assert.equal(
affinityDb.getSessionAccountAffinity("session-S", "codex", 60_000)?.connectionId,
connectionA.id,
"affinity row for session-S must remain pinned to A after re-scoring"
);
// A brand-new session (S2) has no existing pin, so the freshly re-scored
// winner (B) must be honored and a NEW pin created for S2.
const request3 = await auth.getProviderCredentials("codex", null, null, "gpt-5.5", {
sessionKey: "session-S2",
forcedConnectionId: connectionB.id,
});
assert.equal(
request3?.connectionId,
connectionB.id,
"a new session must honor the fresh re-scored pick"
);
assert.equal(
affinityDb.getSessionAccountAffinity("session-S2", "codex", 60_000)?.connectionId,
connectionB.id,
"a new affinity row for session-S2 must be created pointing at B"
);
// Session S must remain unaffected by S2's independent pin.
assert.equal(
affinityDb.getSessionAccountAffinity("session-S", "codex", 60_000)?.connectionId,
connectionA.id,
"session-S pin must stay isolated from session-S2"
);
});
test("reset-aware forcedConnectionId is honored when the pinned connection becomes ineligible (#5903)", async () => {
await settingsDb.updateSettings({
fallbackStrategy: "reset-aware",
codexSessionAffinityTtlMs: 60_000,
});
const connectionA = await seedConnection("codex", { name: "codex-reset-aware-ineligible-a" });
const connectionB = await seedConnection("codex", { name: "codex-reset-aware-ineligible-b" });
const request1 = await auth.getProviderCredentials("codex", null, null, "gpt-5.5", {
sessionKey: "session-failover",
forcedConnectionId: connectionA.id,
});
assert.equal(request1?.connectionId, connectionA.id);
// A becomes rate-limited (e.g. 429 handled by markAccountUnavailable in
// production). Reset-aware re-scores and now forces B. The pin (A) is no
// longer eligible, so the freshly forced B must be used instead of
// failing the whole request.
await providersDb.updateProviderConnection(connectionA.id, {
rateLimitedUntil: new Date(Date.now() + 60_000).toISOString(),
});
const request2 = await auth.getProviderCredentials("codex", null, null, "gpt-5.5", {
sessionKey: "session-failover",
forcedConnectionId: connectionB.id,
});
assert.equal(
request2?.connectionId,
connectionB.id,
"an ineligible pin must fall through to the freshly forced connection"
);
});
test("no session affinity configured: reset-aware forcedConnectionId applies exactly as before (#5903)", async () => {
await settingsDb.updateSettings({
fallbackStrategy: "reset-aware",
codexSessionAffinityTtlMs: 0,
});
const connectionA = await seedConnection("codex", { name: "codex-no-affinity-a" });
const connectionB = await seedConnection("codex", { name: "codex-no-affinity-b" });
const request1 = await auth.getProviderCredentials("codex", null, null, "gpt-5.5", {
sessionKey: "session-no-ttl",
forcedConnectionId: connectionA.id,
});
assert.equal(request1?.connectionId, connectionA.id);
const request2 = await auth.getProviderCredentials("codex", null, null, "gpt-5.5", {
sessionKey: "session-no-ttl",
forcedConnectionId: connectionB.id,
});
assert.equal(
request2?.connectionId,
connectionB.id,
"with affinity disabled (ttl=0) each request must honor the fresh forcedConnectionId"
);
});