From 62ae125d943fce8d5fac6e72304d1b68d92becc8 Mon Sep 17 00:00:00 2001 From: "opencode-agent[bot]" <219766164+opencode-agent[bot]@users.noreply.github.com> Date: Wed, 28 Oct 2026 04:38:10 -0500 Subject: [PATCH] fix(stats): ignore requests rejected before routing (#59773) Co-authored-by: adamdotdevin <2363879+adamdotdevin@users.noreply.github.com> --- packages/stats/core/src/domain/inference.test.ts | 13 +++++++++++++ packages/stats/core/src/domain/inference.ts | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/packages/stats/core/src/domain/inference.test.ts b/packages/stats/core/src/domain/inference.test.ts index d4c6878bbda..17dd1c6eacb 100644 --- a/packages/stats/core/src/domain/inference.test.ts +++ b/packages/stats/core/src/domain/inference.test.ts @@ -283,6 +283,19 @@ describe("inference stat normalization", () => { queries.forEach((query) => expect(query).toContain("lower(model) NOT IN ('alpha-gpt-next', 'hidden-model')")) }) + test("excludes requests rejected before routing from stats and retention queries", () => { + const source = { namespace: "inference", table: "generation", dataset: "zen" } + const queries = [ + ...buildStatsQueries(new Date("2026-08-10T00:00:00.000Z"), new Date("2026-08-11T00:00:00.000Z"), source), + ...buildRetentionQueries(new Date("2026-08-10T00:00:00.000Z"), new Date("2026-08-24T00:00:00.000Z"), source).map( + (item) => item.query, + ), + ] + + expect(queries.length).toBeGreaterThan(0) + queries.forEach((query) => expect(query).toContain("AND attempts > 0\n")) + }) + test("builds bounded R2 SQL queries for each day and week", () => { const queries = buildStatsQueries(new Date("2026-08-10T00:00:00.000Z"), new Date("2026-08-12T12:00:00.000Z"), { namespace: "inference", diff --git a/packages/stats/core/src/domain/inference.ts b/packages/stats/core/src/domain/inference.ts index ba7a56dcc1d..3a7d45a13e5 100644 --- a/packages/stats/core/src/domain/inference.ts +++ b/packages/stats/core/src/domain/inference.ts @@ -43,6 +43,9 @@ const WEEK_MS = 7 * DAY_MS const LIVE_SOURCE_START = "2026-08-11T10:57:48.186Z" // Go Plus is a level of the Go subscription, but the lake records it as its own product. const GO_PRODUCTS = ["go", "go-plus"] +// Requests rejected before routing (unknown model, auth, rate limit) never reach an upstream +// and carry no usage. Counting them would publish any requested model name as a stats page. +const ROUTED_ONLY = "attempts > 0" // R2 SQL results are cursor-paginated after aggregation. Separate usage and geo // queries per day/week avoid combining costly distinct user/session aggregates @@ -138,6 +141,7 @@ WITH normalized AS ( OR (source = 'inference' AND started_at >= ${sqlString(LIVE_SOURCE_START)}) ) AND product IN (${GO_PRODUCTS.map(sqlString).join(", ")}) + AND ${ROUTED_ONLY} AND model_requested IS NOT NULL AND model_requested <> '' AND __ingest_ts >= ${scanStartValue} @@ -302,6 +306,7 @@ WITH normalized AS ( OR (source = 'inference' AND started_at >= ${sqlString(LIVE_SOURCE_START)}) ) AND (product IN (${GO_PRODUCTS.map(sqlString).join(", ")}) OR (${sourceFreeTier})) + AND ${ROUTED_ONLY} AND model_requested IS NOT NULL AND model_requested <> '' AND __ingest_ts >= ${periodStartValue}