Files
OmniRoute/tests/unit/vertex-passthrough-model-lockout.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

293 lines
10 KiB
TypeScript

// Regression guard: after adding passthroughModels: true to Vertex's registry entry, a 404 on
// one Vertex model (e.g. a stale/synthetic model id) must lock out only that model, not cool
// down the whole connection — mirrors the existing ollama-cloud/bedrock protection. Before this
// fix, hasPerModelQuota("vertex", ...) was false, so any 404 on Vertex cooled the whole
// connection for COOLDOWN_MS.notFound (2 minutes), per errorConfig.ts's generic status_404 rule.
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-vertex-404-"));
process.env.DATA_DIR = TEST_DATA_DIR;
const core = await import("../../src/lib/db/core.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const auth = await import("../../src/sse/services/auth.ts");
const accountFallback = await import("../../open-sse/services/accountFallback.ts");
async function resetStorage() {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
async function seedVertex() {
return providersDb.createProviderConnection({
provider: "vertex",
authType: "apikey",
apiKey: "vertex-key",
isActive: true,
testStatus: "active",
});
}
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
test('hasPerModelQuota("vertex", ...) is true after the passthroughModels registry flag', () => {
assert.equal(accountFallback.hasPerModelQuota("vertex", "claude-sonnet-5"), true);
});
test("404 on one Vertex model locks only that model, connection stays active", async () => {
await resetStorage();
const conn = await seedVertex();
const result = await auth.markAccountUnavailable(
conn.id,
404,
"model not found",
"vertex",
"claude-sonnet-5-high"
);
assert.equal(result.shouldFallback, true);
const after = await providersDb.getProviderConnectionById(conn.id);
assert.equal(after.testStatus, "active");
assert.ok(!after.rateLimitedUntil, "connection must not be rate-limited");
const lockout = accountFallback.getModelLockoutInfo("vertex", conn.id, "claude-sonnet-5-high");
assert.equal(lockout?.reason, "not_found");
// A sibling model on the same connection must remain immediately eligible.
const sibling = accountFallback.getModelLockoutInfo("vertex", conn.id, "gemini-3.1-pro-preview");
assert.equal(sibling, null);
});
test("503 on one Vertex model locks only that model, connection stays active (proves the fix isn't 404-specific)", async () => {
// hasPerModelQuota's gate covers status === 404 || status === 429 || status >= 500
// (auth.ts:2024) in one shared branch — 502/503/504 keep the model-lockout path (only
// the exact 500 is exempted per #5976). This mirrors the 404 test above with a 5xx to
// confirm passthroughModels doesn't just fix the specific 404 symptom reported.
await resetStorage();
const conn = await seedVertex();
const result = await auth.markAccountUnavailable(
conn.id,
503,
"Service Unavailable",
"vertex",
"claude-sonnet-5-high"
);
assert.equal(result.shouldFallback, true);
const after = await providersDb.getProviderConnectionById(conn.id);
assert.equal(after.testStatus, "active");
assert.ok(!after.rateLimitedUntil, "connection must not be rate-limited");
const lockout = accountFallback.getModelLockoutInfo("vertex", conn.id, "claude-sonnet-5-high");
assert.equal(lockout?.reason, "server_error");
const sibling = accountFallback.getModelLockoutInfo("vertex", conn.id, "gemini-3.1-pro-preview");
assert.equal(sibling, null);
});
test("403 PERMISSION_DENIED on Vertex locks only that model too (accepted trade-off, see plan)", async () => {
await resetStorage();
const conn = await seedVertex();
// Google Cloud uses the literal "PERMISSION_DENIED" status name for BOTH a
// model-specific denial and a connection-wide IAM/API-disabled failure — this
// fix cannot distinguish them (no live Vertex credential test in this plan), so
// it intentionally treats both as a per-model lockout post-passthroughModels.
const result = await auth.markAccountUnavailable(
conn.id,
403,
"PERMISSION_DENIED: the caller does not have permission",
"vertex",
"claude-sonnet-5-high"
);
assert.equal(result.shouldFallback, true);
const after = await providersDb.getProviderConnectionById(conn.id);
assert.equal(after.testStatus, "active");
assert.ok(!after.rateLimitedUntil, "connection must not be rate-limited");
const lockout = accountFallback.getModelLockoutInfo("vertex", conn.id, "claude-sonnet-5-high");
assert.equal(lockout?.reason, "forbidden");
});
// ── 403 disambiguation via google.rpc.ErrorInfo (Task 7) ────────────────────
// Google Cloud's documented error format (https://cloud.google.com/apis/design/errors#error_info)
// lets us tell a genuinely connection-wide PERMISSION_DENIED (API disabled, or a
// project-level IAM denial) apart from one scoped to a single model — the former must
// fall through to the existing connection-wide cooldown instead of the #3027 per-model
// lockout path, since a per-model lockout would leave a broken connection "active".
test("403 with SERVICE_DISABLED ErrorInfo reason cools down the whole Vertex connection", async () => {
await resetStorage();
const conn = await seedVertex();
const body = JSON.stringify({
error: {
status: "PERMISSION_DENIED",
details: [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
reason: "SERVICE_DISABLED",
domain: "googleapis.com",
metadata: { service: "aiplatform.googleapis.com", consumer: "projects/12345" },
},
],
},
});
const result = await auth.markAccountUnavailable(
conn.id,
403,
body,
"vertex",
"claude-sonnet-5-high"
);
assert.equal(result.shouldFallback, true);
const after = await providersDb.getProviderConnectionById(conn.id);
assert.equal(after.testStatus, "unavailable");
assert.ok(after.rateLimitedUntil, "connection must be cooled down, not left active");
// The #3027 per-model lockout path must NOT have run.
const lockout = accountFallback.getModelLockoutInfo("vertex", conn.id, "claude-sonnet-5-high");
assert.equal(lockout, null);
});
test("403 with IAM_PERMISSION_DENIED reason and a model-scoped resource still locks only that model", async () => {
await resetStorage();
const conn = await seedVertex();
const body = JSON.stringify({
error: {
status: "PERMISSION_DENIED",
details: [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
reason: "IAM_PERMISSION_DENIED",
domain: "iam.googleapis.com",
metadata: {
permission: "aiplatform.endpoints.predict",
resource:
"projects/12345/locations/us-central1/publishers/google/models/claude-sonnet-5",
},
},
],
},
});
const result = await auth.markAccountUnavailable(
conn.id,
403,
body,
"vertex",
"claude-sonnet-5-high"
);
assert.equal(result.shouldFallback, true);
const after = await providersDb.getProviderConnectionById(conn.id);
assert.equal(after.testStatus, "active");
assert.ok(!after.rateLimitedUntil, "connection must not be rate-limited");
const lockout = accountFallback.getModelLockoutInfo("vertex", conn.id, "claude-sonnet-5-high");
assert.equal(lockout?.reason, "forbidden");
});
test("403 with IAM_PERMISSION_DENIED reason and a project-level resource cools down the whole connection", async () => {
await resetStorage();
const conn = await seedVertex();
const body = JSON.stringify({
error: {
status: "PERMISSION_DENIED",
details: [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
reason: "IAM_PERMISSION_DENIED",
domain: "iam.googleapis.com",
metadata: {
permission: "aiplatform.googleapis.com/models.predict",
resource: "projects/12345",
},
},
],
},
});
const result = await auth.markAccountUnavailable(
conn.id,
403,
body,
"vertex",
"claude-sonnet-5-high"
);
assert.equal(result.shouldFallback, true);
const after = await providersDb.getProviderConnectionById(conn.id);
assert.equal(after.testStatus, "unavailable");
assert.ok(after.rateLimitedUntil, "connection must be cooled down, not left active");
const lockout = accountFallback.getModelLockoutInfo("vertex", conn.id, "claude-sonnet-5-high");
assert.equal(lockout, null);
});
test("multi-detail error body correlates reason+resource per-detail, not across details", async () => {
// Adversarial case: detail[0] has a model-scoped resource under an unrelated reason,
// detail[1] carries the actual IAM_PERMISSION_DENIED with a project-level resource. A
// naive independent-regex scan would match detail[0]'s resource against detail[1]'s
// reason and wrongly conclude "model-scoped" — this must resolve to connection-wide.
await resetStorage();
const conn = await seedVertex();
const body = JSON.stringify({
error: {
status: "PERMISSION_DENIED",
details: [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
reason: "SOME_OTHER_REASON",
domain: "iam.googleapis.com",
metadata: {
resource:
"projects/12345/locations/us-central1/publishers/google/models/claude-sonnet-5",
},
},
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
reason: "IAM_PERMISSION_DENIED",
domain: "iam.googleapis.com",
metadata: {
permission: "aiplatform.googleapis.com/models.predict",
resource: "projects/12345",
},
},
],
},
});
const result = await auth.markAccountUnavailable(
conn.id,
403,
body,
"vertex",
"claude-sonnet-5-high"
);
assert.equal(result.shouldFallback, true);
const after2 = await providersDb.getProviderConnectionById(conn.id);
assert.equal(after2.testStatus, "unavailable");
assert.ok(after2.rateLimitedUntil, "connection must be cooled down, not left active");
const lockout2 = accountFallback.getModelLockoutInfo("vertex", conn.id, "claude-sonnet-5-high");
assert.equal(lockout2, null);
});