Merge release/v3.8.55 into feat/task-aware-routing-model-scoping-20294

This commit is contained in:
diegosouzapw
2026-11-19 18:31:19 -03:00
34 changed files with 1193 additions and 820 deletions

View File

@@ -515,7 +515,8 @@ async function pollHealthOnce(port, probeTimeoutMs = INITIAL_PROBE_TIMEOUT_MS) {
const results = await Promise.all(
hosts.map(async (host) => {
try {
const res = await fetch(`http://${host}:${port}/api/monitoring/health`, {
const urlHost = host.includes(":") ? `[${host}]` : host;
const res = await fetch(`http://${urlHost}:${port}/api/monitoring/health`, {
signal: AbortSignal.timeout(probeTimeoutMs),
});
return { host, outcome: res.ok ? "ready" : "fast-reject" };

View File

@@ -0,0 +1 @@
- **fix(deps):** Route every `glob` to the root version and every `minimatch` at or above 9 to `^11.0.4`, clearing GHSA-3g3m-2fjr-6pw4 and GHSA-g3hv-75wg-m249 from the dev tooling in the root lockfile ([#20670](https://github.com/cheaper-inference/OmniRoute/pull/20670))

View File

@@ -0,0 +1 @@
- **fix(sse):** The quota fetch throttle measures spacing with a monotonic clock and gates the Codex usage fetch after proxy resolution, so a wall-clock jump, an early timer wake or a lazy proxy setup cannot shorten the interval or dispatch a request whose proxy already failed; the CLI health check also brackets IPv6 hosts ([#20677](https://github.com/cheaper-inference/OmniRoute/pull/20677))

View File

@@ -1,4 +1,4 @@
{"recordType": "manifest", "schemaVersion": 1, "expectedAssetCount": 146, "auditedCommit": "895c0f3571a9265a757f27c8700a1a4b971b0092", "auditedAt": "2026-11-08", "scope": "Every regular file directly under public/providers at the audited commit.", "statusSemantics": {"proven": "Immutable source plus byte-exact or SVG path-data match.", "probable": "Repository evidence suggests provenance, but no immutable upstream match is proven.", "unresolved": "No sufficient immutable provenance evidence is recorded."}, "enforcement": "All physical files, hashes, magic MIME values, statuses, and duplicate aliases are blocking. Probable and unresolved statuses are recorded but non-blocking in schema version 1.", "legalScope": "Provenance records source matching only; it does not establish copyright or trademark clearance."}
{"recordType": "manifest", "schemaVersion": 1, "expectedAssetCount": 146, "auditedCommit": "895c0f3571a9265a757f27c8700a1a4b971b0092", "auditedAt": "2026-11-08", "scope": "Every regular file directly under public/providers at the audited commit.", "statusSemantics": {"proven": "Immutable source plus byte-exact or SVG path-data match.", "probable": "Repository evidence suggests provenance, but no immutable upstream match is proven.", "unresolved": "No sufficient immutable provenance evidence is recorded."}, "enforcement": "The audited commit must be an ancestor of HEAD. All physical files, hashes, magic MIME values, statuses, and duplicate aliases are blocking. Probable and unresolved statuses are recorded but non-blocking in schema version 1.", "legalScope": "Provenance records source matching only; it does not establish copyright or trademark clearance."}
{"recordType":"asset","path":"public/providers/chatgpt-logo-black.svg","mediaType":"image/svg+xml","sha256":"b8b9aba7ba591f54bf1805f422bc23f32c44ee993643cc378cc77f766061f7a1","provenanceStatus":"probable","source":null,"upstreamLicenseClaim":null,"trademarkClearance":null,"evidenceNote":"Retrieved byte-for-byte on 2026-09-30 from https://developers.openai.com/assets/siwc/sign-in-buttons/chatgpt-logo-black.svg, referenced by the official SIWC button examples at https://developers.openai.com/siwc/website. Used to identify the ChatGPT sign-in provider. Official URL is mutable; no immutable Git/npm provenance or independent rights clearance is claimed."}
{"recordType":"asset","path":"public/providers/chatgpt-logo-white.svg","mediaType":"image/svg+xml","sha256":"568ce2e8701934856aaafc0ed1c3d0300f6c2ff384d0ae743dd1dea08570e12c","provenanceStatus":"probable","source":null,"upstreamLicenseClaim":null,"trademarkClearance":null,"evidenceNote":"Retrieved byte-for-byte on 2026-09-30 from https://developers.openai.com/assets/siwc/sign-in-buttons/chatgpt-logo-white.svg, referenced by the official SIWC button examples at https://developers.openai.com/siwc/website. Used to identify the ChatGPT sign-in provider. Official URL is mutable; no immutable Git/npm provenance or independent rights clearance is claimed."}
{"recordType": "asset", "path": "public/providers/360ai.svg", "mediaType": "image/svg+xml", "sha256": "59366fe04a4336518b8277b430f4a464a91e7ec944c9cf6f40a945c74002386d", "provenanceStatus": "proven", "source": {"kind": "npm", "url": "https://registry.npmjs.org/@lobehub/icons/-/icons-5.10.0.tgz", "ref": "5.10.0", "path": "package/es/Ai360/components/Color.js", "integrity": "sha512-CIpjkISCLRK7haDtSugGFd0o3odaJts8ewJOkUiEFtns3xvsqbl8i24eowBnjw+yMDQVQyNONlhqTD58YC6Ljg==", "packageShasum": "add1baced073a60157d39c7820b8d5c1928a1054", "match": "svg-path-data", "matchDetail": "All 5/5 local SVG path d values match the pinned Color component."}, "upstreamLicenseClaim": {"value": "MIT", "assertedBy": "@lobehub/icons@5.10.0 package/LICENSE", "evidence": "https://registry.npmjs.org/@lobehub/icons/-/icons-5.10.0.tgz#package/LICENSE", "independentlyVerified": true, "scope": "Pinned package distribution only; no trademark clearance."}, "trademarkClearance": null, "evidenceNote": "All local SVG path data matches the pinned LobeHub Color component. This proves source provenance only, not trademark clearance."}

View File

@@ -24,6 +24,8 @@ export interface ProxyResolver {
interface ConnectionProxyFetchDeps {
resolveProxyForConnection?: ProxyResolver["resolveProxyForConnection"];
fetchFn?: typeof globalThis.fetch;
/** Optional async gate run immediately before the fetch is dispatched. */
beforeFetch?: () => Promise<void>;
}
async function defaultResolveProxyForConnection(
@@ -98,11 +100,20 @@ export async function fetchWithConnectionProxy(
);
return null;
}
const task = () => fetchFn(url, init);
let requestActive = true;
const task = async (): Promise<Response | null> => {
// Keep the active check adjacent to fetchFn: runWithProxyContext may report
// an unreachable proxy while this gate is pending, and that settled failure
// must invalidate the task before it can dispatch later.
if (deps.beforeFetch) await deps.beforeFetch();
if (!requestActive) return null;
return fetchFn(url, init);
};
if (!proxy) return task();
try {
return await runWithProxyContext(proxy, task);
} catch (error) {
requestActive = false;
console.warn(
`[ConnectionProxyFetch] Proxied quota fetch failed for ${connectionId}: ${sanitizeErrorMessage(
error instanceof Error ? error.message : String(error)

View File

@@ -14,18 +14,23 @@
* `minIntervalMs = 0` disables throttling entirely (byte-identical to before).
*/
import { performance } from "node:perf_hooks";
const DEFAULT_MIN_INTERVAL_MS = 250;
const MAX_MIN_INTERVAL_MS = 60_000;
const DEFAULT_JITTER_MS = 120;
const MAX_JITTER_MS = 60_000;
/** A real timer can wake at most one loop tick before its monotonic deadline. */
const TIMER_EARLY_WAKE_TOLERANCE_MS = 1;
export interface ThrottleClock {
/** Monotonic milliseconds used to measure spacing inside this process. */
now: () => number;
sleep: (ms: number) => Promise<void>;
}
const realClock: ThrottleClock = {
now: () => Date.now(),
now: () => performance.now(),
sleep: (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)),
};
@@ -76,7 +81,17 @@ export class MinIntervalThrottle {
? now
: Math.max(now, this.lastStart + this.minIntervalMs + this.nextJitter());
const wait = startAt - now;
if (wait > 0) await this.clock.sleep(wait);
if (wait > 0) await this.clock.sleep(Math.max(1, Math.ceil(wait)));
// Timers fire on whole milliseconds of loop time, so a real setTimeout can
// wake up to one tick before the monotonic deadline. Recheck only that
// sub-tick shortfall; every iteration awaits a positive sleep, so this never
// spins. A larger shortfall means an injected clock stepped backward, and
// the scheduled window below already keeps the spacing.
let shortfall = startAt - this.clock.now();
while (shortfall > 0 && shortfall <= TIMER_EARLY_WAKE_TOLERANCE_MS) {
await this.clock.sleep(Math.max(1, Math.ceil(shortfall)));
shortfall = startAt - this.clock.now();
}
// Spacing is measured between real starts. A timer that wakes late (a
// loaded CI box, a paused process) would otherwise shorten the next gap,
// because the next caller computes its wait from the scheduled instant

View File

@@ -43,7 +43,6 @@ import {
import { getCursorUsage } from "./usage/cursor.ts";
import { getKimiUsage } from "./usage/kimi.ts";
import { getCodexUsage } from "./usage/codex.ts";
import { throttleQuotaFetch } from "./quotaFetchThrottle.ts";
import { getClaudeUsage, getClaudePlanLabel } from "./usage/claude.ts";
import { getKiroUsage, buildKiroUsageResult, discoverKiroProfileArn } from "./usage/kiro.ts";
// Re-exported para os testes kiro-* (importam de services/usage).
@@ -175,11 +174,8 @@ export async function getUsageForProvider(
forceRefresh: options.forceRefresh === true,
});
case "codex":
// /me/status and the quota-cache refresh reach this fetch with no
// caller-side pacing. Gate the start here so those paths, and any later
// one, cannot burst the usage endpoint. Callers that already acquired the
// shared gate wait at most one more interval.
await throttleQuotaFetch();
// The Codex leaf gates at the actual fetch boundary, after proxy
// resolution, so lazy setup cannot consume part of the spacing interval.
return await getCodexUsage(accessToken, providerSpecificData, id || "");
case "cursor":
return await getCursorUsage(accessToken || "", providerSpecificData, id || "");

View File

@@ -15,6 +15,7 @@ import {
} from "../codexUsageQuotas.ts";
import { isCodexLimitReached } from "../codexQuotaSignals.ts";
import { fetchWithConnectionProxy } from "../connectionProxyFetch.ts";
import { throttleQuotaFetch } from "../quotaFetchThrottle.ts";
import { getCodexBackendIdentityHeaders } from "../../config/codexClient.ts";
import { getFieldValue } from "./scalars.ts";
@@ -69,20 +70,27 @@ export async function getCodexUsage(
headers["chatgpt-account-id"] = accountId;
}
const response = connectionId
? await fetchWithConnectionProxy(
connectionId,
CODEX_CONFIG.usageUrl,
{
method: "GET",
headers,
},
{ fetchFn: codexUsageFetch }
)
: await codexUsageFetch(CODEX_CONFIG.usageUrl, {
let response: Response | null;
if (connectionId) {
// Keep the gate immediately adjacent to the network boundary, after proxy
// resolution. The helper also invalidates a gated task when proxy
// fast-fail settles first, so a reported failure cannot dispatch later.
response = await fetchWithConnectionProxy(
connectionId,
CODEX_CONFIG.usageUrl,
{
method: "GET",
headers,
});
},
{ fetchFn: codexUsageFetch, beforeFetch: throttleQuotaFetch }
);
} else {
await throttleQuotaFetch();
response = await codexUsageFetch(CODEX_CONFIG.usageUrl, {
method: "GET",
headers,
});
}
if (!response) {
throw new Error("Codex API error: proxied request failed");
}

787
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -607,14 +607,10 @@
"socket.io-parser": "^4.2.7",
"tar": "^7.5.21",
"libxmljs2": {
"minimatch": {
"brace-expansion": "^2.1.4"
}
"minimatch": "^11.0.4"
},
"rimraf": {
"minimatch@9": {
"brace-expansion": "^2.1.4"
}
"minimatch@9": "^11.0.4"
},
"@apidevtools/json-schema-ref-parser": {
"js-yaml": "^5.4.2"
@@ -638,6 +634,8 @@
},
"get-uri": {
"basic-ftp": "^6.2.3"
}
},
"glob": "$glob",
"minimatch@>=9.0.0": "^11.0.4"
}
}

View File

@@ -49,6 +49,11 @@ const IGNORE_FROM_CODE = new Set([
"LC_MESSAGES",
"CI",
"GITHUB_ACTIONS",
// Desktop release uploader context supplied by electron-release.yml (#18165).
// GITHUB_REPOSITORY is runner-owned; VERSION is the validated release tag.
// Neither is OmniRoute application configuration or belongs in a user's .env.
"GITHUB_REPOSITORY",
"VERSION",
"RUNNER_OS",
// Quality-gate harness knobs (optional cache/report paths for CI scripts — not product config).
"ESLINT_RESULTS_JSON",

View File

@@ -29,7 +29,7 @@ function getArg(name, fallbackValue = "") {
}
function runGit(args) {
return execFileSync("git", args, { encoding: "utf8" }).trim();
return execFileSync("git", args, { encoding: "utf8" });
}
function isSourceFile(filePath) {
@@ -78,14 +78,36 @@ if (!baseRef) {
}
const baseTarget = process.env.GITHUB_BASE_SHA || `origin/${baseRef}`;
const changedFiles = runGit(["diff", "--name-only", "--diff-filter=ACMR", `${baseTarget}...HEAD`])
.split(/\r?\n/)
.map((line) => line.trim())
// NUL separators preserve whitespace, Unicode and newlines in Git paths.
const changedFiles = runGit([
"diff",
"--name-only",
"-z",
"--diff-filter=ACMR",
`${baseTarget}...HEAD`,
])
.split("\0")
.filter(Boolean);
// A rename or copy introduces a new destination too. Existing misplaced tests
// remain visible legacy debt; this rule prevents newly introduced locations.
const addedFiles = runGit([
"diff",
"--no-renames",
"--name-only",
"-z",
"--diff-filter=A",
`${baseTarget}...HEAD`,
])
.split("\0")
.filter(Boolean);
const misplacedNewTests = addedFiles.filter(
(filePath) => isTestFile(filePath) && !filePath.startsWith("tests/")
);
const changedSourceFiles = changedFiles.filter(isSourceFile);
const changedTestFiles = changedFiles.filter(isTestFile);
const hasRequiredTests = changedSourceFiles.length === 0 || changedTestFiles.length > 0;
const passesPolicy = hasRequiredTests && misplacedNewTests.length === 0;
const reportLines = [
"## PR Test Policy",
@@ -93,6 +115,7 @@ const reportLines = [
`Base ref: \`${baseRef}\``,
`Changed production files: ${changedSourceFiles.length}`,
`Changed automated test files: ${changedTestFiles.length}`,
`New test files outside tests/: ${misplacedNewTests.length}`,
"",
];
@@ -112,14 +135,24 @@ if (changedTestFiles.length > 0) {
reportLines.push("");
}
if (hasRequiredTests) {
if (passesPolicy) {
reportLines.push("Result: PASS");
} else {
reportLines.push(
"Result: FAIL",
"",
"This PR changes production code under `src/`, `open-sse/`, `electron/`, or `bin/` but does not add or update automated tests."
);
reportLines.push("Result: FAIL", "");
if (!hasRequiredTests) {
reportLines.push(
"This PR changes production code under `src/`, `open-sse/`, `electron/`, or `bin/` but does not add or update automated tests."
);
}
if (misplacedNewTests.length > 0) {
reportLines.push(
"New test files must be under `tests/`, including renamed or copied tests.",
""
);
for (const filePath of misplacedNewTests.slice(0, 20)) {
reportLines.push(`- ${JSON.stringify(filePath)}`);
}
}
}
const report = buildReport(reportLines);
@@ -131,6 +164,6 @@ if (summaryFile) {
process.stdout.write(report);
if (!hasRequiredTests) {
if (!passesPolicy) {
process.exit(1);
}

View File

@@ -199,6 +199,16 @@ function inspectGitCommit(objectId) {
return null;
}
function verifyAuditedCommitAncestry(commit) {
const result = spawnSync("git", ["-C", repoRoot, "merge-base", "--is-ancestor", commit, "HEAD"], {
encoding: "utf8",
});
if (result.error || ![0, 1].includes(result.status)) {
return `unable to verify auditedCommit ancestry with Git: ${result.error?.message || result.stderr.trim() || "git merge-base failed"}`;
}
return result.status === 0 ? null : `auditedCommit must be an ancestor of HEAD: ${commit}`;
}
function verifyAuditedProviderSnapshot(commit, physicalFiles) {
const failures = [];
const treeResult = spawnSync(
@@ -330,6 +340,8 @@ async function main() {
failures.push(gitCommitFailure);
} else if (providersDir === defaultProvidersDir) {
failures.push(...verifyAuditedProviderSnapshot(header.auditedCommit, physicalFiles));
const ancestryFailure = verifyAuditedCommitAncestry(header.auditedCommit);
if (ancestryFailure) failures.push(ancestryFailure);
}
}
if (typeof header.auditedAt !== "string" || !/^\d{4}-\d{2}-\d{2}$/.test(header.auditedAt)) {

View File

@@ -0,0 +1,74 @@
import { Socket } from "node:net";
import { getGlobalDispatcher, MockAgent, setGlobalDispatcher } from "undici";
const HORDE_IMAGE_CATALOG_URL = "https://aihorde.net/api/v2/status/models?type=image";
/** Network boundary for catalog contracts that do not exercise live provider discovery. */
export function installOfflineCatalogNetwork(
hordeResponse: () => Promise<Response> = async () => Response.json([])
) {
const previousFetch = globalThis.fetch;
const previousConnect = Socket.prototype.connect;
const previousDispatcher = getGlobalDispatcher();
const dispatcher = new MockAgent();
dispatcher.disableNetConnect();
const unexpectedRequests: string[] = [];
let hordeRequests = 0;
let restored = false;
// Native fetch captured before this fixture also has to fail closed.
const dispatch = dispatcher.dispatch.bind(dispatcher);
dispatcher.dispatch = (options, handler) => {
const url = new URL(options.path, String(options.origin));
unexpectedRequests.push(`dispatcher: ${url.origin}${url.pathname}`);
return dispatch(options, handler);
};
const rejectSocket: typeof Socket.prototype.connect = function () {
unexpectedRequests.push("node:net socket connection");
throw new Error("Offline catalog fixture forbids socket connections");
};
const fixtureFetch: typeof fetch = async (input, init) => {
let url: URL;
try {
url = new URL(input instanceof Request ? input.url : String(input));
} catch {
unexpectedRequests.push("invalid HTTP request URL");
throw new Error("Offline catalog fixture forbids invalid HTTP requests");
}
const method = init?.method ?? (input instanceof Request ? input.method : "GET");
if (url.href === HORDE_IMAGE_CATALOG_URL && method.toUpperCase() === "GET") {
hordeRequests++;
return hordeResponse();
}
unexpectedRequests.push(`${url.origin}${url.pathname}`);
throw new Error("Offline catalog fixture forbids unexpected HTTP requests");
};
function activate() {
if (restored) throw new Error("Offline catalog fixture has already been restored");
globalThis.fetch = fixtureFetch;
Socket.prototype.connect = rejectSocket;
setGlobalDispatcher(dispatcher);
}
activate();
return {
unexpectedRequests,
get hordeRequests() {
return hordeRequests;
},
// proxyFetch installs a global wrapper during application imports. Select the
// fixture transport again after those imports, before exercising the catalog.
activate,
async restore() {
if (restored) return;
restored = true;
globalThis.fetch = previousFetch;
Socket.prototype.connect = previousConnect;
setGlobalDispatcher(previousDispatcher);
await dispatcher.close();
},
};
}

View File

@@ -397,6 +397,17 @@ test("chatCore returns a pre-content Grok failure to the outer fallback contract
test("chatCore converts a Grok post-content failure into terminal wire error and failed persistence", async () => {
await settingsDb.updateSettings({ call_log_pipeline_enabled: true });
// The assertion below retains its 3 s steady-state persistence contract. Prime the real
// source-checkout artifact worker by awaiting its exact first write instead of charging its
// tsx/worker_threads cold start to that contract (which flakes under a loaded CI scheduler).
await callLogs.saveCallLog({
id: "grok-stream-error-worker-warmup",
timestamp: new Date().toISOString(),
status: 200,
model: "fixture-warmup",
provider: "fixture-warmup",
requestBody: { warmup: true },
});
const streamFailures: Array<Record<string, unknown>> = [];
const requestBody = {
model: "grok-4.1-fast",

View File

@@ -0,0 +1,53 @@
import assert from "node:assert/strict";
import { Socket } from "node:net";
import test from "node:test";
import { getGlobalDispatcher } from "undici";
import { installOfflineCatalogNetwork } from "../_helpers/offlineCatalogNetwork.ts";
test("offline catalog fixture answers only image discovery and restores network globals", async () => {
const originalFetch = globalThis.fetch;
const originalConnect = Socket.prototype.connect;
const originalDispatcher = getGlobalDispatcher();
const network = installOfflineCatalogNetwork();
try {
const response = await fetch("https://aihorde.net/api/v2/status/models?type=image");
assert.equal(response.status, 200);
assert.deepEqual(await response.json(), []);
assert.equal(network.hordeRequests, 1);
assert.deepEqual(network.unexpectedRequests, []);
await assert.rejects(fetch("https://unexpected.invalid/models"), /unexpected HTTP/);
await assert.rejects(fetch("invalid-url"), /invalid HTTP/);
await assert.rejects(
fetch("https://aihorde.net/api/v2/status/models?type=text"),
/unexpected HTTP/
);
await assert.rejects(
fetch("https://aihorde.net/api/v2/status/models?type=image", { method: "POST" }),
/unexpected HTTP/
);
await assert.rejects(originalFetch("https://native-bypass.invalid/models"), /fetch failed/);
const socket = new Socket();
try {
assert.throws(() => socket.connect(80, "socket-bypass.invalid"), /forbids socket/);
} finally {
socket.destroy();
}
assert.deepEqual(network.unexpectedRequests, [
"https://unexpected.invalid/models",
"invalid HTTP request URL",
"https://aihorde.net/api/v2/status/models",
"https://aihorde.net/api/v2/status/models",
"dispatcher: https://native-bypass.invalid/models",
"node:net socket connection",
]);
assert.equal(network.hordeRequests, 1);
} finally {
await network.restore();
}
assert.equal(globalThis.fetch, originalFetch);
assert.equal(Socket.prototype.connect, originalConnect);
assert.equal(getGlobalDispatcher(), originalDispatcher);
await network.restore();
assert.throws(() => network.activate(), /already been restored/);
});

View File

@@ -208,6 +208,18 @@ test("runEnvDocSync: shipped allowlist ignores ad-hoc BOT_TOKEN and BOT_URL", ()
assert.deepEqual(shipped.problems.codeMissingEnv, []);
});
test("desktop release context is not app configuration, while undocumented app vars still fail", () => {
const result = runEnvDocSync({
envExampleText: "",
envDocText: "",
codeVars: new Set(["GITHUB_REPOSITORY", "VERSION", "OMNIROUTE_UNDOCUMENTED_APP_OPTION"]),
docOnlyAllowlist: new Set(),
envOnlyAllowlist: new Set(),
});
assert.equal(result.ok, false);
assert.deepEqual(result.problems.codeMissingEnv, ["OMNIROUTE_UNDOCUMENTED_APP_OPTION"]);
});
test("repository contract is in sync (live data)", () => {
// Uses the real .env.example, docs/ENVIRONMENT.md, and the bundled
// allowlists. This is the same check that runs in pre-commit / CI.

View File

@@ -150,6 +150,32 @@ function emptyTreeCommit() {
return commit.stdout.trim();
}
function detachedSnapshotCommit() {
const result = spawnSync(
"git",
[
"-C",
REPO_ROOT,
"commit-tree",
gitObjectId("HEAD^{tree}"),
"-m",
"provenance fixture: matching snapshot without candidate ancestry",
],
{
encoding: "utf8",
env: {
...process.env,
GIT_AUTHOR_NAME: "provenance-fixture",
GIT_AUTHOR_EMAIL: "provenance-fixture@example.invalid",
GIT_COMMITTER_NAME: "provenance-fixture",
GIT_COMMITTER_EMAIL: "provenance-fixture@example.invalid",
},
}
);
assert.equal(result.status, 0, result.stderr);
return result.stdout.trim();
}
function workflowJob(source: string, name: string) {
const lines = source.split("\n");
const start = lines.indexOf(` ${name}:`);
@@ -513,6 +539,40 @@ test("provider asset provenance gate rejects a missing or non-commit auditedComm
}
});
test("provider asset provenance rejects a matching snapshot outside candidate ancestry", () => {
const fixture = makeFixture();
try {
const records = readFileSync(
join(REPO_ROOT, "config/quality/provider-assets-provenance.jsonl"),
"utf8"
)
.trim()
.split("\n")
.map((line) => JSON.parse(line));
records[0] = { ...records[0], auditedCommit: detachedSnapshotCommit() };
writeFileSync(
fixture.manifestPath,
`${records.map((record) => JSON.stringify(record)).join("\n")}\n`
);
const result = runGate(join(REPO_ROOT, "public/providers"), fixture.manifestPath);
assert.equal(result.status, 1, `${result.stdout}\n${result.stderr}`);
assert.match(`${result.stdout}\n${result.stderr}`, /auditedCommit must be an ancestor of HEAD/);
// Matching bytes on the actual candidate remain a valid positive control.
records[0] = { ...records[0], auditedCommit: gitObjectId("HEAD") };
writeFileSync(
fixture.manifestPath,
`${records.map((record) => JSON.stringify(record)).join("\n")}\n`
);
const accepted = runGate(join(REPO_ROOT, "public/providers"), fixture.manifestPath);
assert.equal(accepted.status, 0, `${accepted.stdout}\n${accepted.stderr}`);
} finally {
rmSync(fixture.root, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
}
});
test("provider asset provenance gate binds auditedCommit to the physical provider snapshot", () => {
const fixture = makeFixture();
try {

View File

@@ -21,9 +21,16 @@ test("issue #10508: pollHealthOnce must target 127.0.0.1, not localhost", () =>
assert.ok(hosts, "pollHealthOnce host list not found");
assert.match(hosts[1], /"127\.0\.0\.1"/, "readiness poll must probe the literal 127.0.0.1");
assert.doesNotMatch(hosts[1], /localhost/, "readiness poll must never resolve localhost");
// The ::1 probe needs URL brackets: `http://::1:port/` is not a valid URL, so
// an unbracketed IPv6 host would make that probe fail on every poll.
assert.match(
source,
/fetch\(`http:\/\/\$\{host\}:\$\{port\}\/api\/monitoring\/health`/,
/const urlHost = host\.includes\(":"\) \? `\[\$\{host\}\]` : host;/,
"pollHealthOnce must bracket IPv6 loopback hosts"
);
assert.match(
source,
/fetch\(`http:\/\/\$\{urlHost\}:\$\{port\}\/api\/monitoring\/health`/,
"pollHealthOnce fetch call not found"
);
});

View File

@@ -81,10 +81,12 @@ test("waitForServer returns false when the port accepts TCP but never answers a
test("waitForServer detects IPv6 loopback health endpoint when IPv4 is unavailable (#11766)", async () => {
const port = await freePort();
let healthRequests = 0;
const server = net.createServer((socket) => {
socket.on("data", (data) => {
const request = data.toString();
if (request.includes("GET /api/monitoring/health")) {
healthRequests += 1;
socket.end("HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nok");
}
});
@@ -97,12 +99,17 @@ test("waitForServer detects IPv6 loopback health endpoint when IPv4 is unavailab
});
try {
const result = await waitForServer(port, 8000);
const outcomes = [];
const result = await waitForServer(port, 8000, {
onOutcome: (outcome) => outcomes.push(outcome),
});
assert.equal(
result,
true,
"expected waitForServer to detect health endpoint on IPv6 loopback even when IPv4 is unavailable"
);
assert.equal(outcomes[0], "ready", "IPv6 health must succeed without the TCP grace fallback");
assert.ok(healthRequests > 0, "the IPv6 health endpoint must receive an HTTP request");
} finally {
await new Promise((resolve) => server.close(() => resolve()));
}

View File

@@ -22,6 +22,10 @@ import {
createQuotaAutoPingState,
runQuotaAutoPingTick,
} from "../../src/lib/services/quotaAutoPing.ts";
import {
__setProxyHealthTcpCheckForTesting,
invalidateProxyHealth,
} from "../../src/lib/proxyHealth.ts";
const INTERVAL_MS = 250;
@@ -205,3 +209,64 @@ test("#15172 Auto-Ping's own gate adds at most one extra interval", async () =>
restoreThrottle();
}
});
test("#15172 proxy fast-fail invalidates a Codex fetch still waiting on the gate", async () => {
const proxyUrl = "http://127.0.0.1:1";
let now = 1_000;
let releaseSleep: (() => void) | null = null;
let markSleepStarted!: () => void;
const sleepStarted = new Promise<void>((resolve) => {
markSleepStarted = resolve;
});
const clock: ThrottleClock = {
now: () => now,
sleep: (ms: number) => {
markSleepStarted();
return new Promise<void>((resolve) => {
releaseSleep = () => {
now += ms;
resolve();
};
});
},
};
const throttle = new MinIntervalThrottle({ minIntervalMs: INTERVAL_MS, jitterMs: 0, clock });
setQuotaFetchThrottleForTests(throttle);
await throttle.acquire();
let fetchCalls = 0;
setCodexUsageFetchForTests(async () => {
fetchCalls++;
return new Response("{}", { status: 200 });
});
connectionProxyTestOverride.resolveProxyForConnection = async () => ({
proxy: { type: "http", host: "127.0.0.1", port: 1 },
});
invalidateProxyHealth(proxyUrl);
__setProxyHealthTcpCheckForTesting(async () => false);
try {
const resultPromise = getUsageForProvider({
id: "codex-proxy-fast-fail",
provider: "codex",
accessToken: "fixture-token",
providerSpecificData: { workspaceId: "fixture-workspace" },
});
await sleepStarted;
const result = (await resultPromise) as { message?: string };
assert.match(String(result.message), /proxied request failed/);
assert.equal(fetchCalls, 0, "fast-fail must settle before the gate is released");
releaseSleep?.();
await new Promise<void>((resolve) => setImmediate(resolve));
await new Promise<void>((resolve) => setImmediate(resolve));
assert.equal(fetchCalls, 0, "a reported proxy failure must not dispatch a late fetch");
} finally {
releaseSleep?.();
__setProxyHealthTcpCheckForTesting(null);
invalidateProxyHealth(proxyUrl);
connectionProxyTestOverride.resolveProxyForConnection = null;
setCodexUsageFetchForTests(null);
resetQuotaFetchThrottle();
}
});

View File

@@ -107,9 +107,12 @@ describe("Golden Eval: long-coding-session", () => {
);
});
it("latency under 50ms for 50-message session", () => {
it("uses under 50ms of synchronous CPU for the coding-session fixture", () => {
const messages = loadFixture();
const start = performance.now();
const threadCpuClock =
typeof process.threadCpuUsage === "function" ? process.threadCpuUsage : process.cpuUsage;
const cpuStart = threadCpuClock();
const wallStart = performance.now();
compressAggressive(
messages as Array<{
role: string;
@@ -117,7 +120,13 @@ describe("Golden Eval: long-coding-session", () => {
[key: string]: unknown;
}>
);
const elapsed = performance.now() - start;
assert.ok(elapsed < 50, `Expected < 50ms, got ${elapsed.toFixed(1)}ms`);
const wallMs = performance.now() - wallStart;
const cpu = threadCpuClock(cpuStart);
const cpuMs = (cpu.user + cpu.system) / 1_000;
assert.ok(
cpuMs < 50,
`Expected < 50ms synchronous CPU, got ${cpuMs.toFixed(1)}ms ` +
`(wall ${wallMs.toFixed(1)}ms)`
);
});
});

View File

@@ -1,7 +1,10 @@
import { describe, it } from "node:test";
import { after, describe, it } from "node:test";
import assert from "node:assert/strict";
import { DefaultExecutor } from "../../open-sse/executors/default.ts";
import { resetDbInstance } from "../../src/lib/db/core.ts";
after(() => resetDbInstance());
// The native `deepseek` provider (alias `ds/`) documents only
// `response_format: { type: "json_object" }`; a `json_schema` request is
@@ -78,6 +81,51 @@ describe("native DeepSeek json_schema fallback", () => {
assert.equal(body.messages[0].content, "You are concise.");
});
for (const frozen of [false, true]) {
it(`preserves the caller's ${frozen ? "frozen" : "mutable"} system content array`, () => {
const content = [
{ type: "text", text: "You are concise." },
{ type: "text", text: "Keep the original instructions." },
];
if (frozen) {
content.forEach(Object.freeze);
Object.freeze(content);
}
const body = {
model: "deepseek-v4-flash",
messages: [
{ role: "system", content },
{ role: "user", content: "x" },
],
response_format: { type: "json_schema", json_schema: { name: "t", schema } },
};
const before = structuredClone(body);
const result = transform("deepseek", "deepseek-v4-flash", body);
assert.deepEqual(body, before);
assert.deepEqual(result.response_format, { type: "json_object" });
assert.equal(result.messages?.filter((message) => message.role === "system").length, 1);
const transformedContent = result.messages?.[0].content;
assert.ok(Array.isArray(transformedContent));
assert.notStrictEqual(transformedContent, content);
assert.equal(transformedContent.length, content.length + 1);
assert.deepEqual(transformedContent.slice(0, -1), content);
assert.equal(transformedContent.at(-1)?.type, "text");
assert.match(String(transformedContent.at(-1)?.text), /strictly follows this JSON schema/);
assert.ok(String(transformedContent.at(-1)?.text).includes(JSON.stringify(schema, null, 2)));
assert.deepEqual(result.messages?.[1], before.messages[1]);
const repeated = transform("deepseek", "deepseek-v4-flash", body);
assert.deepEqual(repeated.messages, result.messages);
assert.deepEqual(
body,
before,
"retries must not accumulate schema instructions on the input"
);
});
}
it("leaves a json_object response_format untouched (DeepSeek accepts it natively)", () => {
const body = {
model: "deepseek-v4-flash",

View File

@@ -0,0 +1,65 @@
import assert from "node:assert/strict";
import { after, describe, it } from "node:test";
import { DefaultExecutor } from "../../open-sse/executors/default.ts";
import { resetDbInstance } from "../../src/lib/db/core.ts";
after(() => resetDbInstance());
// These providers used the common fallback before native DeepSeek support.
// Keep this regression portable to branches without the native DeepSeek gate.
describe("JSON schema fallback system content arrays", () => {
for (const provider of ["openai-compatible-chat-fixture", "kilocode"]) {
for (const frozen of [false, true]) {
it(`${provider} preserves ${frozen ? "frozen" : "mutable"} caller content across repeated transforms`, () => {
const schema = {
type: "object",
properties: { ok: { type: "boolean" } },
required: ["ok"],
};
const content = [
{ type: "text", text: "Keep the first instruction." },
{ type: "text", text: "Keep the second instruction." },
];
if (frozen) {
content.forEach(Object.freeze);
Object.freeze(content);
}
const body = {
model: "fixture-model",
messages: [
{ role: "system", content },
{ role: "user", content: "Return the result." },
],
response_format: { type: "json_schema", json_schema: { name: "fixture", schema } },
};
const before = structuredClone(body);
const executor = new DefaultExecutor(provider);
const result = executor.transformRequest("fixture-model", body, false, {}) as typeof body;
assert.deepEqual(body, before);
assert.deepEqual(
result.response_format,
provider === "kilocode" ? undefined : { type: "json_object" }
);
assert.equal(result.messages.filter((message) => message.role === "system").length, 1);
const transformedContent = result.messages[0].content;
assert.ok(Array.isArray(transformedContent));
assert.notStrictEqual(transformedContent, content);
assert.equal(transformedContent.length, content.length + 1);
assert.deepEqual(transformedContent.slice(0, -1), content);
assert.equal(transformedContent.at(-1)?.type, "text");
assert.match(String(transformedContent.at(-1)?.text), /strictly follows this JSON schema/);
assert.ok(
String(transformedContent.at(-1)?.text).includes(JSON.stringify(schema, null, 2))
);
assert.deepEqual(result.messages[1], before.messages[1]);
const repeated = executor.transformRequest("fixture-model", body, false, {}) as typeof body;
assert.deepEqual(repeated.messages, result.messages);
assert.deepEqual(body, before);
});
}
}
});

View File

@@ -12,6 +12,7 @@ import https from "node:https";
import net from "node:net";
import os from "node:os";
import path from "node:path";
import { TLSSocket } from "node:tls";
import { spawn, type ChildProcess } from "node:child_process";
import { fileURLToPath } from "node:url";
@@ -78,10 +79,17 @@ function sendIntercepted(port: number): Promise<void> {
method: "POST",
path: "/v1beta/models/gemini-x:generateContent",
headers: { host: TARGET_HOST, "content-type": "application/json" },
rejectUnauthorized: false,
// Trust only this test's CA and verify the hostname issued through SNI.
servername: TARGET_HOST,
ca: fs.readFileSync(path.join(dataDir, "mitm", "ca.crt")),
agent: clientAgent,
},
(res) => {
if (!(res.socket instanceof TLSSocket) || !res.socket.authorized) {
res.resume();
reject(new Error("The test proxy certificate must be verified by its temporary CA"));
return;
}
res.resume();
res.on("end", resolve);
}

View File

@@ -3,6 +3,9 @@ import assert from "node:assert/strict";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { installOfflineCatalogNetwork } from "../_helpers/offlineCatalogNetwork.ts";
const network = installOfflineCatalogNetwork();
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-combo-metadata-"));
process.env.DATA_DIR = TEST_DATA_DIR;
@@ -12,6 +15,7 @@ const core = await import("../../src/lib/db/core.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const combosDb = await import("../../src/lib/db/combos.ts");
const modelsDb = await import("../../src/lib/db/models.ts");
const settingsDb = await import("../../src/lib/db/settings.ts");
const contextOverrides = await import("../../src/lib/db/modelContextOverrides.ts");
const capabilityOverrides = await import("../../src/lib/db/modelCapabilityOverrides.ts");
const overrideRoute = await import("../../src/app/api/model-capability-overrides/route.ts");
@@ -20,9 +24,26 @@ const { getComboBuilderOptions } = await import("../../src/lib/combos/builderOpt
const { buildGlobalModelList, buildManualComboModelStep } =
await import("../../src/lib/combos/builderDraft.ts");
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
network.activate();
test.before(async () => {
// These contracts cover saved combos. Built-in auto/* synthesis also refreshes
// live provider quota APIs; it has separate coverage and is outside this fixture.
await settingsDb.updateSettings({ hideAutoCombos: true });
});
test.after(async () => {
try {
await catalog.__flushCatalogBackgroundRefreshForTest();
assert.deepEqual(network.unexpectedRequests, [], "catalog contracts must stay offline");
} finally {
try {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
} finally {
await network.restore();
}
}
});
test("builder-saved provider-node combos preserve metadata with a public model prefix", async () => {
@@ -85,8 +106,8 @@ test("builder-saved provider-node combos preserve metadata with a public model p
new Request("http://localhost/api/v1/models")
);
const body = (await response.json()) as { data: Array<Record<string, unknown>> };
assert.equal(response.status, 200, JSON.stringify(body));
const direct = body.data.find((item) => item.id === `${prefix}/${modelId}`);
assert.equal(response.status, 200);
assert.ok(direct);
assert.ok(Number(direct.max_output_tokens) > 0);
for (const suffix of ["global", "manual", "legacy"]) {

View File

@@ -3,6 +3,9 @@ import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import { installOfflineCatalogNetwork } from "../_helpers/offlineCatalogNetwork.ts";
const network = installOfflineCatalogNetwork();
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-custom-node-prefix-"));
process.env.DATA_DIR = TEST_DATA_DIR;
@@ -11,9 +14,12 @@ const aliasesDb = await import("../../src/lib/db/models/aliases.ts");
const core = await import("../../src/lib/db/core.ts");
const modelsDb = await import("../../src/lib/db/models.ts");
const providersDb = await import("../../src/lib/db/providers.ts");
const settingsDb = await import("../../src/lib/db/settings.ts");
const modelsCatalog = await import("../../src/app/api/v1/models/catalog.ts");
const modelsRoute = await import("../../src/app/api/v1/models/route.ts");
network.activate();
const NODE_ID = "openai-compatible-chat-550e8400-e29b-41d4-a716-446655440000";
const PREFIX = "infrex";
const EXPECTED_IDS = [
@@ -27,6 +33,8 @@ async function resetStorage(): Promise<void> {
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
modelsCatalog.__resetCatalogBuilderRunsForTest();
// Prefix semantics do not depend on synthesized auto/* models or live quota APIs.
await settingsDb.updateSettings({ hideAutoCombos: true });
}
async function seedCustomNode(): Promise<void> {
@@ -67,8 +75,8 @@ async function getCatalogIds(mode: "alias" | "canonical" | "dual"): Promise<stri
const response = await modelsRoute.GET(
new Request(`http://localhost/api/v1/models?prefix=${mode}`)
);
assert.equal(response.status, 200);
const body = (await response.json()) as { data: Array<{ id: string }> };
assert.equal(response.status, 200, JSON.stringify(body));
return body.data.map((model) => model.id);
}
@@ -91,9 +99,18 @@ test.beforeEach(async () => {
await seedCustomNode();
});
test.after(() => {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
test.after(async () => {
try {
await modelsCatalog.__flushCatalogBackgroundRefreshForTest();
assert.deepEqual(network.unexpectedRequests, [], "catalog contracts must stay offline");
} finally {
try {
core.resetDbInstance();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
} finally {
await network.restore();
}
}
});
test("alias mode exposes every custom node model under its configured prefix", async () => {

View File

@@ -0,0 +1,185 @@
import assert from "node:assert/strict";
import { execFileSync, spawnSync } from "node:child_process";
import {
copyFileSync,
mkdirSync,
mkdtempSync,
readFileSync,
renameSync,
rmSync,
writeFileSync,
} from "node:fs";
import os from "node:os";
import path from "node:path";
import test from "node:test";
import { fileURLToPath } from "node:url";
const policy = fileURLToPath(
new URL("../../scripts/check/check-pr-test-policy.mjs", import.meta.url)
);
function fixture(t, initial = {}) {
const directory = mkdtempSync(path.join(os.tmpdir(), "omni-test-placement-"));
t.after(() => rmSync(directory, { recursive: true, force: true }));
const repo = path.join(directory, "repo");
const templates = path.join(directory, "templates");
mkdirSync(repo);
mkdirSync(templates);
const env = {
PATH: process.env.PATH,
HOME: directory,
LANG: "C.UTF-8",
GIT_CONFIG_NOSYSTEM: "1",
GIT_CONFIG_GLOBAL: os.devNull,
GIT_TERMINAL_PROMPT: "0",
};
const git = (...args) => execFileSync("git", args, { cwd: repo, env, encoding: "utf8" }).trim();
const write = (name, contents = "// test fixture\n") => {
const target = path.join(repo, name);
mkdirSync(path.dirname(target), { recursive: true });
writeFileSync(target, contents);
};
const commit = () => {
git("add", "--all");
git("commit", "--quiet", "-m", "test: update fixture");
return git("rev-parse", "HEAD");
};
git("init", "--quiet", "--initial-branch=main", `--template=${templates}`);
git("config", "user.name", "Test Fixture");
git("config", "user.email", "fixture@example.invalid");
write("README.md", "Fixture repository\n");
for (const [name, contents] of Object.entries(initial)) write(name, contents);
const base = commit();
git("update-ref", "refs/remotes/origin/main", base);
const run = (overrides = {}) => {
const summary = path.join(directory, "summary.md");
const commandEnv = { ...env, GITHUB_BASE_REF: "main", GITHUB_BASE_SHA: base, ...overrides };
for (const name of Object.keys(commandEnv)) {
if (commandEnv[name] === undefined) delete commandEnv[name];
}
const result = spawnSync(process.execPath, [policy, "--summary-file", summary], {
cwd: repo,
env: commandEnv,
encoding: "utf8",
timeout: 30000,
maxBuffer: 1024 * 1024,
});
assert.equal(result.error, undefined);
assert.equal(result.signal, null);
assert.equal(result.stderr, "");
assert.equal(readFileSync(summary, "utf8"), result.stdout);
return { status: result.status, report: result.stdout };
};
return { repo, git, write, commit, run };
}
for (const name of [
"src/feature/__tests__/feature.test.ts",
"root-regression.test.mjs",
"src/café regression.test.ts",
"src/line\nbreak.test.ts",
]) {
test(`rejects a newly introduced test outside tests/: ${JSON.stringify(name)}`, (t) => {
const f = fixture(t);
f.write(name);
f.commit();
const result = f.run();
assert.equal(result.status, 1);
assert.match(result.report, /must be under `tests\//);
});
}
test("accepts production changes accompanied by a test under tests/", (t) => {
const f = fixture(t);
f.write("src/feature.ts", "export const value = 1;\n");
f.write("tests/unit/feature.test.ts");
f.commit();
assert.equal(f.run().status, 0);
});
test("continues to reject production changes with no automated test", (t) => {
const f = fixture(t);
f.write("src/feature.ts", "export const value = 1;\n");
f.commit();
assert.equal(f.run().status, 1);
});
test("a valid test does not hide another newly misplaced test", (t) => {
const f = fixture(t);
f.write("src/feature.ts");
f.write("tests/unit/feature.test.ts");
f.write("src/feature.test.ts");
f.commit();
assert.equal(f.run().status, 1);
});
test("detects a test renamed from tests/ into production directories", (t) => {
const f = fixture(t, { "tests/unit/feature.test.ts": "// unchanged test fixture\n" });
mkdirSync(path.join(f.repo, "src"));
renameSync(
path.join(f.repo, "tests/unit/feature.test.ts"),
path.join(f.repo, "src/feature.test.ts")
);
f.commit();
assert.equal(f.run().status, 1);
});
test("detects a copied test outside tests/", (t) => {
const f = fixture(t, { "tests/unit/feature.test.ts": "// unchanged test fixture\n" });
mkdirSync(path.join(f.repo, "src"));
copyFileSync(
path.join(f.repo, "tests/unit/feature.test.ts"),
path.join(f.repo, "src/feature.test.ts")
);
f.commit();
assert.equal(f.run().status, 1);
});
test("does not classify an updated legacy test as a newly introduced location", (t) => {
const f = fixture(t, { "src/__tests__/legacy.test.ts": "// existing fixture\n" });
f.write("src/__tests__/legacy.test.ts", "// updated fixture\n");
f.commit();
assert.equal(f.run().status, 0);
});
test("accepts migration of a legacy test into tests/", (t) => {
const f = fixture(t, { "src/__tests__/legacy.test.ts": "// existing fixture\n" });
mkdirSync(path.join(f.repo, "tests/unit"), { recursive: true });
renameSync(
path.join(f.repo, "src/__tests__/legacy.test.ts"),
path.join(f.repo, "tests/unit/legacy.test.ts")
);
f.commit();
assert.equal(f.run().status, 0);
});
test("accepts documentation-only changes", (t) => {
const f = fixture(t);
f.write("README.md", "Updated fixture documentation\n");
f.commit();
assert.equal(f.run().status, 0);
});
test("uses the explicit base SHA even when the remote-tracking branch advanced", (t) => {
const f = fixture(t);
f.write("src/feature.test.ts");
const head = f.commit();
f.git("update-ref", "refs/remotes/origin/main", head);
assert.equal(f.run().status, 1);
});
test("retains the origin/base-ref fallback when no explicit SHA is supplied", (t) => {
const f = fixture(t);
f.write("src/feature.test.ts");
f.commit();
assert.equal(f.run({ GITHUB_BASE_SHA: undefined }).status, 1);
});
test("preserves the non-PR skip behavior", (t) => {
const f = fixture(t);
f.write("src/feature.test.ts");
f.commit();
const result = f.run({ GITHUB_BASE_REF: undefined, GITHUB_BASE_SHA: undefined });
assert.equal(result.status, 0);
assert.match(result.report, /Skipped: not running in a pull request context/);
});

View File

@@ -5,45 +5,118 @@
* before it is wired into `syncAllProviderLimits()`'s OAuth and non-OAuth
* paths.
*/
import test from "node:test";
import test, { type TestContext } from "node:test";
import assert from "node:assert/strict";
import { setImmediate as nextTurn } from "node:timers/promises";
import { syncInChunksWithSpacing } from "../../src/lib/usage/providerLimits/chunkedSpacingSync.ts";
test("waits between chunks but not after the last chunk when spacingMs > 0", async () => {
function controlSpacingClock(context: TestContext) {
const nativeDate = globalThis.Date;
const nativeTimeout = globalThis.setTimeout;
context.mock.timers.enable({ apis: ["Date", "setTimeout"], now: 1_800_000_000_000 });
const timeout = context.mock.method(globalThis, "setTimeout");
let settled = true;
context.after(async () => {
try {
// At most three chunks: drain a failed assertion's held waits before restoring globals.
for (let remaining = 3; !settled && remaining > 0; remaining--) {
context.mock.timers.runAll();
await nextTurn();
}
assert.equal(settled, true, "the controlled sync must settle before restoring globals");
} finally {
// Restore the spy before the clock, and remove both from the context's tracker.
context.mock.reset();
assert.equal(globalThis.Date, nativeDate);
assert.equal(globalThis.setTimeout, nativeTimeout);
}
});
return {
timeout,
track<T>(pending: Promise<T>): Promise<T> {
settled = false;
void pending.then(
() => {
settled = true;
},
() => {
settled = true;
}
);
return pending;
},
};
}
test("waits between chunks but not after the last chunk when spacingMs > 0", async (t) => {
const { timeout, track } = controlSpacingClock(t);
const items = [1, 2, 3, 4];
const chunkStarts: number[] = [];
const start = Date.now();
await syncInChunksWithSpacing(
items,
2,
40,
async (item) => {
chunkStarts.push(Date.now() - start);
return item;
},
() => {}
const pending = track(
syncInChunksWithSpacing(
items,
2,
40,
async (item) => {
chunkStarts.push(Date.now() - start);
return item;
},
() => {}
)
);
await nextTurn();
assert.deepEqual(chunkStarts, [0, 0], "only the first chunk may start before the wait");
assert.deepEqual(
timeout.mock.calls.map((call) => call.arguments[1]),
[40]
);
// 2 chunks of 2 → chunkStarts has 4 entries (2 per chunk, same start time).
assert.equal(chunkStarts.length, 4);
const chunk1Start = Math.min(chunkStarts[0], chunkStarts[1]);
const chunk2Start = Math.min(chunkStarts[2], chunkStarts[3]);
assert.ok(
chunk2Start - chunk1Start >= 35,
`expected >=35ms gap between chunks, got ${chunk2Start - chunk1Start}`
);
t.mock.timers.tick(39);
await nextTurn();
assert.deepEqual(chunkStarts, [0, 0], "the second chunk must not start before 40ms");
t.mock.timers.tick(1);
await nextTurn();
assert.deepEqual(chunkStarts, [0, 0, 40, 40]);
assert.equal(timeout.mock.callCount(), 1, "there must be no wait after the last chunk");
await pending;
});
test("never waits when spacingMs === 0 (opt-out, preserves fast path)", async () => {
test("never waits when spacingMs === 0 (opt-out, preserves fast path)", async (t) => {
const { timeout, track } = controlSpacingClock(t);
const items = [1, 2, 3, 4];
const chunks: number[][] = [];
const start = Date.now();
await syncInChunksWithSpacing(items, 2, 0, async (item) => item, () => {});
const elapsed = Date.now() - start;
assert.ok(elapsed < 30, `expected near-instant run with spacingMs=0, took ${elapsed}ms`);
const pending = track(
syncInChunksWithSpacing(
items,
2,
0,
async (item) => item,
(chunk) => {
chunks.push([...chunk]);
// Elapsed work between chunks is not a request for a spacing timer.
if (chunks.length === 1) t.mock.timers.setTime(Date.now() + 48);
}
)
);
await nextTurn();
assert.equal(timeout.mock.callCount(), 0, "spacingMs=0 must not schedule a spacing timer");
assert.deepEqual(
chunks,
[
[1, 2],
[3, 4],
],
"both chunks must finish without ticking timers"
);
await pending;
assert.equal(Date.now() - start, 48, "elapsed work does not change the zero-wait contract");
});
test("chunkSize=1 processes items strictly one at a time (reproduces OAuth semantics)", async () => {

View File

@@ -6,10 +6,12 @@
* connections in `concurrency`-sized chunks with NO spacing at all between
* chunks, so setting `PROVIDER_LIMITS_SYNC_SPACING_MS` had no effect on that
* path. This is the direct regression guard: forces >1 chunk (concurrency=1)
* and asserts a measured gap >= spacingMs between chunk start times.
* and controls the spacing clock: zero requests no wait; positive waits block
* the next chunk until the configured timer fires, without a final wait.
*/
import test from "node:test";
import test, { type TestContext } from "node:test";
import assert from "node:assert/strict";
import { setImmediate as nextTurn } from "node:timers/promises";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
@@ -71,55 +73,114 @@ function glmQuotaResponse() {
);
}
test("syncAllProviderLimits spaces chunks for local/API-key connections when spacingMs is set", async () => {
function controlSpacingClock(context: TestContext) {
const nativeDate = globalThis.Date;
const nativeTimeout = globalThis.setTimeout;
context.mock.timers.enable({ apis: ["Date", "setTimeout"], now: 1_800_000_000_000 });
const timeout = context.mock.method(globalThis, "setTimeout");
let settled = true;
context.after(async () => {
try {
// At most three chunks: drain a failed assertion's held waits before restoring globals.
for (let remaining = 3; !settled && remaining > 0; remaining--) {
context.mock.timers.runAll();
await nextTurn();
}
assert.equal(settled, true, "the controlled sync must settle before restoring globals");
} finally {
// Restore the spy before the clock, and remove both from the context's tracker.
context.mock.reset();
assert.equal(globalThis.Date, nativeDate);
assert.equal(globalThis.setTimeout, nativeTimeout);
}
});
return {
timeout,
track<T>(pending: Promise<T>): Promise<T> {
settled = false;
void pending.then(
() => {
settled = true;
},
() => {
settled = true;
}
);
return pending;
},
};
}
test("syncAllProviderLimits spaces chunks for local/API-key connections when spacingMs is set", async (t) => {
process.env.PROVIDER_LIMITS_SYNC_SPACING_MS = "60";
for (let i = 0; i < 3; i++) await createGlmApiKeyConnection(i);
const { timeout, track } = controlSpacingClock(t);
const chunkStarts: number[] = [];
const start = Date.now();
globalThis.fetch = (async (input: string | URL | Request) => {
// #12754 added a second per-connection call (customer-package-reset/list)
// after the quota fetch. A chunk starts at the QUOTA request; counting every
// fetch would read the reset-card follow-up as a fourth-sixth chunk.
t.mock.method(globalThis, "fetch", async (input: string | URL | Request) => {
// Count only the quota fetch, not GLM's reset-card follow-up (#12754).
if (String(input).includes("/quota/limit")) chunkStarts.push(Date.now() - start);
return glmQuotaResponse();
}) as typeof fetch;
});
// concurrency: 1 forces 3 chunks of size 1 → 2 gaps must be >= spacingMs.
await providerLimits.syncAllProviderLimits({ source: "scheduled", concurrency: 1 });
assert.equal(chunkStarts.length, 3, "expected 3 fetches, one per connection");
const gaps: number[] = [];
for (let i = 1; i < chunkStarts.length; i++) gaps.push(chunkStarts[i] - chunkStarts[i - 1]);
assert.ok(
gaps.every((g) => g >= 50),
`every chunk gap must be >= configured spacing (~60ms), gaps=${gaps.join(",")}`
const pending = track(
providerLimits.syncAllProviderLimits({ source: "scheduled", concurrency: 1 })
);
await nextTurn(); // Drain the in-memory fetch/SQLite promise chain; no clock advance.
assert.deepEqual(chunkStarts, [0], "the next chunk must remain behind the first wait");
for (let completed = 1; completed < 3; completed++) {
assert.equal(timeout.mock.callCount(), completed);
assert.equal(timeout.mock.calls.at(-1)?.arguments[1], 60);
t.mock.timers.tick(59);
await nextTurn();
assert.equal(chunkStarts.length, completed, "a chunk must not start before 60ms");
t.mock.timers.tick(1);
await nextTurn();
assert.equal(chunkStarts.length, completed + 1, "the next chunk must start at 60ms");
}
assert.deepEqual(chunkStarts, [0, 60, 120]);
assert.deepEqual(
timeout.mock.calls.map((call) => call.arguments[1]),
[60, 60],
"three chunks require two waits and no wait after the last chunk"
);
const result = await pending;
assert.equal(result.succeeded, 3);
assert.equal(result.failed, 0);
});
test("syncAllProviderLimits does not space local/API-key chunks when spacingMs=0 (opt-out)", async () => {
test("syncAllProviderLimits does not space local/API-key chunks when spacingMs=0 (opt-out)", async (t) => {
process.env.PROVIDER_LIMITS_SYNC_SPACING_MS = "0";
for (let i = 0; i < 3; i++) await createGlmApiKeyConnection(i);
const { timeout, track } = controlSpacingClock(t);
const chunkStarts: number[] = [];
const start = Date.now();
const nonSpacingWorkMs = [2, 48, 0];
globalThis.fetch = (async (input: string | URL | Request) => {
// #12754 added a second per-connection call (customer-package-reset/list)
// after the quota fetch. A chunk starts at the QUOTA request; counting every
// fetch would read the reset-card follow-up as a fourth-sixth chunk.
if (String(input).includes("/quota/limit")) chunkStarts.push(Date.now() - start);
t.mock.method(globalThis, "fetch", async (input: string | URL | Request) => {
// Count only the quota fetch, not GLM's reset-card follow-up (#12754).
if (String(input).includes("/quota/limit")) {
chunkStarts.push(Date.now() - start);
// Model elapsed work between origins without scheduling or firing a timer.
t.mock.timers.setTime(Date.now() + (nonSpacingWorkMs[chunkStarts.length - 1] ?? 0));
}
return glmQuotaResponse();
}) as typeof fetch;
});
await providerLimits.syncAllProviderLimits({ source: "scheduled", concurrency: 1 });
assert.equal(chunkStarts.length, 3);
const gaps: number[] = [];
for (let i = 1; i < chunkStarts.length; i++) gaps.push(chunkStarts[i] - chunkStarts[i - 1]);
assert.ok(
gaps.every((g) => g < 40),
`spacingMs=0 must not introduce a forced gap, gaps=${gaps.join(",")}`
const pending = track(
providerLimits.syncAllProviderLimits({ source: "scheduled", concurrency: 1 })
);
await nextTurn();
assert.equal(timeout.mock.callCount(), 0, "spacingMs=0 must not schedule a spacing timer");
assert.deepEqual(chunkStarts, [0, 2, 50], "all three chunks must run without ticking timers");
const result = await pending;
assert.equal(result.succeeded, 3);
assert.equal(result.failed, 0);
});

View File

@@ -115,6 +115,67 @@ test("#6009 a single fetch is never delayed", async () => {
assert.equal(clock.now(), 1000);
});
test("#15172 wall-clock jumps cannot bypass the default throttle interval", async () => {
const originalDateNow = Date.now;
const throttle = new MinIntervalThrottle({ minIntervalMs: 20, jitterMs: 0 });
try {
const firstAcquireStart = performance.now();
await throttle.acquire();
Date.now = () => originalDateNow() + 60_000;
await throttle.acquire();
const secondRelease = performance.now();
assert.ok(
secondRelease - firstAcquireStart >= 20,
"wall-clock jumps must not shorten the monotonic release interval"
);
} finally {
Date.now = originalDateNow;
}
});
test("#15172 an early timer wake is rechecked before the throttle releases", async () => {
let now = 0.1;
let sleepCalls = 0;
const requestedDelays: number[] = [];
const clock: ThrottleClock = {
now: () => now,
sleep: async (ms: number) => {
requestedDelays.push(ms);
// Model timer implementations that quantize fractional delays and may
// wake one tick before the requested monotonic deadline.
now += Math.max(0, Math.floor(ms) - (sleepCalls++ === 0 ? 1 : 0));
},
};
const throttle = new MinIntervalThrottle({ minIntervalMs: 250, jitterMs: 0, clock });
await throttle.acquire();
const first = clock.now();
now += 0.3;
await throttle.acquire();
assert.ok(clock.now() - first >= 250, "an early timer wake must not shorten the interval");
assert.deepEqual(requestedDelays, [250, 1]);
});
test("#15172 a late timer cannot release already-queued acquisitions as a burst", async () => {
const clock = new FakeClock();
let sleepCalls = 0;
clock.sleep = async (ms: number) => {
clock.slept.push(ms);
clock.t += ms + (sleepCalls++ === 0 ? 600 : 0);
};
const throttle = new MinIntervalThrottle({ minIntervalMs: 250, jitterMs: 0, clock });
await Promise.all(Array.from({ length: 4 }, () => throttle.acquire()));
assert.deepEqual(
clock.slept,
[250, 250, 250],
"every queued acquisition must retain a complete interval after a late wake-up"
);
});
test("#6009 jitter adds a bounded extra spacing on top of the min gap", async () => {
const clock = new FakeClock();
// Deterministic rand → always 0.5, so jitter = floor(0.5 * 100) = 50.

View File

@@ -4,9 +4,9 @@
import React, { act } from "react";
import { createRoot } from "react-dom/client";
import { afterEach, describe, expect, it, vi } from "vitest";
import EmptyConnectionsPlaceholder from "../EmptyConnectionsPlaceholder";
import ProviderParamFilterSection from "../ProviderParamFilterSection";
import type { ProviderMessageTranslator } from "../../providerPageHelpers";
import EmptyConnectionsPlaceholder from "@/app/(dashboard)/dashboard/providers/[id]/components/EmptyConnectionsPlaceholder";
import ProviderParamFilterSection from "@/app/(dashboard)/dashboard/providers/[id]/components/ProviderParamFilterSection";
import type { ProviderMessageTranslator } from "@/app/(dashboard)/dashboard/providers/[id]/providerPageHelpers";
const stableTranslate = (key: string) => key;
vi.mock("next-intl", () => ({

View File

@@ -4,7 +4,7 @@
import React, { act } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import ModelCompatPopover from "../ModelCompatPopover";
import ModelCompatPopover from "@/app/(dashboard)/dashboard/providers/[id]/components/ModelCompatPopover";
vi.mock("next-intl", () => ({
useTranslations: () => (key: string) => key,

View File

@@ -197,6 +197,7 @@ test("usage leaves route every proxied fetch through the connection proxy helper
test("the helper reports a missing identifier instead of fetching directly", async () => {
let calls = 0;
let gateCalls = 0;
const fetchFn = (async (): Promise<Response> => {
calls += 1;
return balanceResponse();
@@ -208,8 +209,79 @@ test("the helper reports a missing identifier instead of fetching directly", asy
{
resolveProxyForConnection: async () => ({ proxy: ASSIGNED_PROXY }),
fetchFn,
beforeFetch: async () => {
gateCalls += 1;
},
}
);
assert.equal(response, null);
assert.equal(calls, 0);
assert.equal(gateCalls, 0);
});
test("the helper runs beforeFetch immediately before a no-proxy fetch", async () => {
const order: string[] = [];
const response = await fetchWithConnectionProxy(
"direct-gated",
"https://console.example/api/balance",
undefined,
{
resolveProxyForConnection: async () => ({ proxy: null }),
beforeFetch: async () => {
order.push("gate");
},
fetchFn: (async () => {
order.push("fetch");
return balanceResponse();
}) as typeof globalThis.fetch,
}
);
assert.equal(response?.status, 200);
assert.deepEqual(order, ["gate", "fetch"]);
});
test("the helper preserves callers without beforeFetch", async () => {
let calls = 0;
const response = await fetchWithConnectionProxy(
"direct-ungated",
"https://console.example/api/balance",
undefined,
{
resolveProxyForConnection: async () => ({ proxy: null }),
fetchFn: (async () => {
calls += 1;
return balanceResponse();
}) as typeof globalThis.fetch,
}
);
assert.equal(response?.status, 200);
assert.equal(calls, 1);
});
test("proxy resolution failure consumes neither the gate nor the fetch", async () => {
let gateCalls = 0;
let fetchCalls = 0;
const response = await fetchWithConnectionProxy(
"resolution-failure",
"https://console.example/api/balance",
undefined,
{
resolveProxyForConnection: async () => {
throw new Error("synthetic proxy resolution failure");
},
beforeFetch: async () => {
gateCalls += 1;
},
fetchFn: (async () => {
fetchCalls += 1;
return balanceResponse();
}) as typeof globalThis.fetch,
}
);
assert.equal(response, null);
assert.equal(gateCalls, 0);
assert.equal(fetchCalls, 0);
});