fix(monitoring): judge pool health on the exact unhealthy ratio

The pool verdict compared the ratio after rounding it to two decimals,
so 1 of 3 unhealthy (0.333...) passed a 0.33 threshold and answered 200.
Compare the exact ratio and round only the value shown in the body.
This commit is contained in:
serenity
2026-11-19 10:44:00 -03:00
committed by diegosouzapw
parent 83e890018d
commit a91c7b8c06
2 changed files with 27 additions and 2 deletions

View File

@@ -165,8 +165,10 @@ async function poolHealthResponse(request: Request, authError: Response | null)
Object.keys(summarizeConnectionCooldown([connection], nowMs)).length > 0
).length;
const total = connections.length;
const unhealthyRatio = total === 0 ? 1 : Math.round((unhealthy / total) * 100) / 100;
const summaryHealth = unhealthyRatio <= unhealthRatioThreshold;
// Judge on the exact ratio; round only the value shown in the body (1/3 must fail a 0.33 bar).
const exactRatio = total === 0 ? 1 : unhealthy / total;
const unhealthyRatio = Math.round(exactRatio * 100) / 100;
const summaryHealth = exactRatio <= unhealthRatioThreshold;
return NextResponse.json(
{
provider,

View File

@@ -82,6 +82,29 @@ test("a pool above the threshold answers 503 with summaryHealth false", async ()
assert.equal(body.unhealthRatioThreshold, 0.25);
});
test("the verdict compares the exact ratio, not the rounded one shown in the body", async () => {
// 1 of 3 unhealthy = 0.333..., shown as 0.33, but it exceeds a 0.33 threshold.
for (const row of [
{ name: "third-ok-1", testStatus: "active" },
{ name: "third-ok-2", testStatus: "active" },
{ name: "third-expired", testStatus: "expired" },
]) {
await providersDb.createProviderConnection({
provider: "mistral",
authType: "apikey",
apiKey: `sk-mistral-${row.name}`,
name: row.name,
isActive: true,
testStatus: row.testStatus,
});
}
const res = await asManager("provider=mistral&unhealthRatioThreshold=0.33");
const body = (await res.json()) as Record<string, unknown>;
assert.equal(body.unhealthyRatio, 0.33);
assert.equal(body.summaryHealth, false);
assert.equal(res.status, 503);
});
test("a pool with no active account is unhealthy (ratio 1), not silently fine", async () => {
const res = await asManager("provider=anthropic");
const body = (await res.json()) as Record<string, unknown>;