diff --git a/docs/docs/data-engineering/tools/finops-tools.md b/docs/docs/data-engineering/tools/finops-tools.md index b984c10c7e..575c4f057c 100644 --- a/docs/docs/data-engineering/tools/finops-tools.md +++ b/docs/docs/data-engineering/tools/finops-tools.md @@ -206,6 +206,37 @@ Classic (sql warehouse, workspace 525779153077873) · Migrate Classic/Pro to Ser $29.29 in DBUs over the window; serverless would bill $4.44 for the busy time alone ``` + +--- + +## finops_snowflake_savings + +Check a Snowflake account for savings with Altimate's rules. It lists warehouses with `SHOW WAREHOUSES` and, to report on seven days, reads up to eleven days of `SNOWFLAKE.ACCOUNT_USAGE` (`WAREHOUSE_METERING_HISTORY`, `WAREHOUSE_EVENTS_HISTORY`, `QUERY_HISTORY`), read-only, on the connection's warehouse. The role needs `IMPORTED PRIVILEGES` on the `SNOWFLAKE` database, and sees only the warehouses it has access to; warehouses that billed credits but cannot be listed are named as not checked. + +- **High Idle Time:** on days a warehouse sat idle for more than 40% of its active time, the idle share of that day's cost. +- **Downsize Warehouse:** nearly all queries finish within 5 seconds, or nearly all active periods last 80 seconds or less, so one size smaller would do. +- **Underutilized Cluster:** the highest cluster of a multi-cluster warehouse is barely needed. It is reported without a dollar estimate. +- **Frequent Restart Overcharge:** a warehouse that suspends and resumes within one billed minute pays the overlap twice. + +The tool measures each warehouse's usage; altimate-core decides what qualifies and what it saves. Credits are priced from `ORGANIZATION_USAGE.RATE_SHEET_DAILY` when the role can read it in dollars, otherwise at $3.00 (also for an account billed in another currency), and the output says which. Each warehouse is counted once in the total. + +**What it does not assess.** These are four of Altimate's warehouse checks. Storage, serverless features and individual query tuning are not covered. A warehouse that never suspended in the window has no completed active period, so it gets no idle finding. Days are UTC, and the window ends at 00:00 UTC on the latest day at least six hours past, because `ACCOUNT_USAGE` lags by up to that long. + +**Asks first, and stops long queries.** The check runs only when called with `confirm: true`; without it, the agent is told to ask you first, since a run can resume a suspended warehouse that then bills. It runs six queries and stops after 9 minutes, connecting included, cancelling any query still running on Snowflake (or sooner, at an `altimate run --time-budget` cutoff); stopping the session cancels the running query too. + +**Assessment mode.** `ALTIMATE_ASSESSMENT_MODE=true` works as it does for `finops_databricks_savings`: the run contacts your Snowflake account, and turns off the Snowflake driver's cloud-platform detection. If a result is large enough for Snowflake to deliver it from its own cloud storage, the driver fetches it from there too. + +``` +> finops_snowflake_savings --warehouse prod_sf --confirm + +Snowflake savings: 12 warehouses checked, the 7 days to 2026-11-18, at $3.00 a credit from the account's rate sheet. +Total: $54.00–$90.00 a month on 1 warehouse, each counted once at its largest saving. + +BI_WH (warehouse) · Downsize Warehouse · $54.00–$90.00 a month (medium) + Large could be Medium: 95.0% of its active periods lasted 80 seconds or less + The saving is a share of $42.00 over the window +``` + --- ## finops_role_grants diff --git a/packages/drivers/src/snowflake.ts b/packages/drivers/src/snowflake.ts index 24aba33688..52334c2314 100644 --- a/packages/drivers/src/snowflake.ts +++ b/packages/drivers/src/snowflake.ts @@ -5,6 +5,8 @@ import * as fs from "fs" import type { ConnectionConfig, Connector, ConnectorResult, ExecuteOptions, SchemaColumn } from "./types" import { toPkcs8Pem } from "./snowflake-key" +import { QueryTimeoutError } from "./types" +import { MAX_TIMER_MS } from "./query-timeout" import { loadOptionalDriver } from "./resolve" import { emitBrowserSignIn, openInBrowser } from "./sign-in" import { emitReconnect, type ReconnectEvent } from "./reconnect-events" @@ -54,6 +56,26 @@ export function suppressSnowflakeLogging(snowflake: any): void { }) } +/** + * In assessment mode nothing but the warehouse may be contacted. When it loads, snowflake-sdk otherwise probes + * the AWS, Azure and GCP metadata and identity services to report the platform it runs on. Its certificate + * revocation requests need no switch here: they start from an HTTPS agent that Bun, which runs the packaged + * CLI, does not use. + */ +export function keepSdkOffCloudMetadata(env: NodeJS.ProcessEnv = process.env): void { + const mode = env.ALTIMATE_ASSESSMENT_MODE?.trim().toLowerCase() + if (mode && mode !== "0" && mode !== "false") env.SNOWFLAKE_DISABLE_PLATFORM_DETECTION = "true" +} + +/** How long to wait for Snowflake to acknowledge a cancel before reporting the statement as possibly still running. */ +const CANCEL_WAIT_MS = 30_000 + +/** + * How a caller's `cancelAfterMs` reaches what it covers: why it was stopped, and every statement sent. A read + * retried after a closed-session error leaves two, and the first may still be running. + */ +type StatementStop = { stopped?: "deadline" | "aborted"; statements: any[] } + /** * Errors meaning the session is gone: 407002 is raised client-side for any * request on a terminated connection; 390111 (session no longer exists), @@ -183,10 +205,12 @@ function describeAccount(account: unknown): string { export async function connect( config: ConnectionConfig, sdk?: unknown, - seams?: { connectTimeoutMs?: number }, + seams?: { connectTimeoutMs?: number; cancelWaitMs?: number }, ): Promise { const connectTimeoutMs = seams?.connectTimeoutMs ?? CONNECT_TIMEOUT_MS + const cancelWaitMs = seams?.cancelWaitMs ?? CANCEL_WAIT_MS let snowflake: any + keepSdkOffCloudMetadata() snowflake = sdk ?? (await loadOptionalDriver("snowflake", "snowflake-sdk")) snowflake = snowflake.default || snowflake @@ -468,7 +492,11 @@ export async function connect( * reopened and a read-only statement is retried once; a write is not, because * it may already have run (see `isRetrySafe`). */ - async function executeQuery(sql: string, binds?: any[]): Promise<{ columns: string[]; rows: any[][] }> { + async function executeQuery( + sql: string, + binds?: any[], + stop?: StatementStop, + ): Promise<{ columns: string[]; rows: any[][] }> { // Captured before any wait: a statement queued behind a reconnect started on the old session's assumptions. const started = generation const startedInEpoch = epoch @@ -477,7 +505,7 @@ export async function connect( const used = connection const ranIn = generation try { - const result = await runTracked(used, sql, binds, ranIn) + const result = await runTracked(used, sql, binds, ranIn, stop) await noteSession(sql, binds, ranIn, startedInEpoch) return result } catch (err) { @@ -496,7 +524,7 @@ export async function connect( ) } const ranAgainIn = generation - const result = await runTracked(connection, sql, binds, ranAgainIn) + const result = await runTracked(connection, sql, binds, ranAgainIn, stop) await noteSession(sql, binds, ranAgainIn, startedInEpoch) return result } @@ -511,11 +539,11 @@ export async function connect( /** `runQuery`, counting a statement that may hold session state against the session it was sent to (`ran`) for * as long as it runs. Counted only once sent: one still waiting on a reconnect will run on the new session. */ - async function runTracked(conn: any, sql: string, binds: any[] | undefined, ran: number) { - if (!mayHoldState(sql, binds)) return runQuery(conn, sql, binds) + async function runTracked(conn: any, sql: string, binds: any[] | undefined, ran: number, stop?: StatementStop) { + if (!mayHoldState(sql, binds)) return runQuery(conn, sql, binds, stop) stateStatementsRunning.set(ran, (stateStatementsRunning.get(ran) ?? 0) + 1) try { - return await runQuery(conn, sql, binds) + return await runQuery(conn, sql, binds, stop) } finally { const left = (stateStatementsRunning.get(ran) ?? 1) - 1 if (left > 0) stateStatementsRunning.set(ran, left) @@ -523,8 +551,15 @@ export async function connect( } } - function runQuery(conn: any, sql: string, binds?: any[]): Promise<{ columns: string[]; rows: any[][] }> { + function runQuery( + conn: any, + sql: string, + binds?: any[], + stop?: StatementStop, + ): Promise<{ columns: string[]; rows: any[][] }> { return new Promise((resolve, reject) => { + // Stopped while waiting on a reconnect: the statement is never sent. + if (stop?.stopped) return reject(new Error("Snowflake statement stopped before it was sent")) const options: Record = { sqlText: sql, complete(err: Error | null, _stmt: any, rows: any[]) { @@ -541,10 +576,63 @@ export async function connect( }, } if (binds && binds.length > 0) options.binds = binds - conn.execute(options) + const statement = conn.execute(options) + stop?.statements.push(statement) + }) + } + + function cancelStatement(statement: any): Promise { + return new Promise((resolve) => { + const timer = setTimeout(() => resolve(false), cancelWaitMs) + const finish = (cancelled: boolean) => { + clearTimeout(timer) + resolve(cancelled) + } + try { + statement.cancel((err: Error | null | undefined) => finish(!err)) + } catch { + finish(false) + } }) } + /** + * `cancelAfterMs` is an opt-in deadline: past it, or when `signal` aborts, the statement is cancelled on + * Snowflake. One still waiting on a reconnect is never sent, so nothing is left running. + */ + async function executeWithCancel( + sql: string, + binds: any[] | undefined, + deadlineMs: number, + signal?: AbortSignal, + ): Promise<{ columns: string[]; rows: any[][] }> { + signal?.throwIfAborted() + const stop: StatementStop = { statements: [] } + const STOPPED = Symbol("stopped") + let wake: () => void = () => {} + const stopping = new Promise((resolve) => (wake = () => resolve(STOPPED))) + const halt = (why: "deadline" | "aborted") => { + stop.stopped ??= why + wake() + } + const timer = setTimeout(() => halt("deadline"), Math.min(deadlineMs, MAX_TIMER_MS)) + const onAbort = () => halt("aborted") + signal?.addEventListener("abort", onAbort, { once: true }) + const work = executeQuery(sql, binds, stop) + let outcome: { columns: string[]; rows: any[][] } | typeof STOPPED + try { + outcome = await Promise.race([work, stopping]) + } finally { + clearTimeout(timer) + signal?.removeEventListener("abort", onAbort) + } + if (outcome !== STOPPED) return outcome + work.catch(() => {}) + const cancelled = (await Promise.all(stop.statements.map(cancelStatement))).every(Boolean) + if (stop.stopped === "aborted") throw signal!.reason + throw new QueryTimeoutError({ engine: "snowflake", timeoutMs: deadlineMs, cancelled }) + } + return { async connect() { closed = false @@ -710,7 +798,11 @@ export async function connect( query = `${sql.replace(/;\s*$/, "")} LIMIT ${effectiveLimit + 1}` } - const result = await executeQuery(query, binds) + const deadlineMs = options?.cancelAfterMs + const result = + deadlineMs && deadlineMs > 0 + ? await executeWithCancel(query, binds, deadlineMs, options?.signal) + : await executeQuery(query, binds) const truncated = effectiveLimit > 0 && result.rows.length > effectiveLimit const rows = truncated ? result.rows.slice(0, effectiveLimit) diff --git a/packages/drivers/src/types.ts b/packages/drivers/src/types.ts index 00ec89cf00..e3046e0057 100644 --- a/packages/drivers/src/types.ts +++ b/packages/drivers/src/types.ts @@ -46,7 +46,7 @@ export interface ExecuteOptions { queryTimeoutMs?: number /** Cancel the statement on the server once this many milliseconds pass, or when * `signal` aborts. Opt-in: `executeQuery` fills in `queryTimeoutMs` for every - * call, so only callers that set this get the server-side cancel. Databricks only. */ + * call, so only callers that set this get the server-side cancel. Databricks and Snowflake. */ cancelAfterMs?: number /** DuckDB only: called once after handing this query to its helper; callback errors do not affect execution. */ onDispatch?: () => void diff --git a/packages/drivers/test/snowflake-deadline.test.ts b/packages/drivers/test/snowflake-deadline.test.ts new file mode 100644 index 0000000000..ba1a3d25b2 --- /dev/null +++ b/packages/drivers/test/snowflake-deadline.test.ts @@ -0,0 +1,239 @@ +/** + * Snowflake server-side cancel: a caller-named `cancelAfterMs` cancels the statement when it runs + * out or when `signal` aborts; without one, execution is unchanged. + */ +import { beforeEach, describe, expect, test } from "bun:test" +import { connect, keepSdkOffCloudMetadata } from "../src/snowflake" +import { isQueryTimeoutError } from "../src/types" + +const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) + +let finishAfterMs: number | undefined +let cancelError: Error | undefined +let cancelSilently = false +let queryError: Error | undefined +let cancels = 0 +let sent: string[] = [] +let up = true +let holdConnect: Promise | undefined +let failFirstWith: { code: string; message: string } | undefined + +beforeEach(() => { + finishAfterMs = undefined + cancelError = undefined + cancelSilently = false + queryError = undefined + cancels = 0 + sent = [] + up = true + holdConnect = undefined + failFirstWith = undefined +}) + +function fakeSdk() { + return { + configure() {}, + createConnection() { + return { + connect(cb: (err: Error | null) => void) { + if (holdConnect) holdConnect.then(() => cb(null)) + else cb(null) + }, + isUp: () => up, + execute(options: { sqlText: string; complete: (err: Error | null, stmt: unknown, rows: unknown[]) => void }) { + sent.push(options.sqlText) + if (failFirstWith) { + const failure = Object.assign(new Error(failFirstWith.message), { code: failFirstWith.code }) + failFirstWith = undefined + setTimeout(() => options.complete(failure, null, []), 1) + } else if (queryError) setTimeout(() => options.complete(queryError!, null, []), 1) + else if (finishAfterMs !== undefined) { + setTimeout(() => options.complete(null, null, [{ N: 1 }]), finishAfterMs) + } + return { + cancel(cb: (err?: Error) => void) { + cancels++ + if (!cancelSilently) cb(cancelError) + }, + } + }, + destroy(cb: (err: Error | null) => void) { + cb(null) + }, + } + }, + } +} + +async function connector(seams?: { cancelWaitMs?: number }) { + const one = await connect({ type: "snowflake", account: "a", user: "u", password: "p" } as never, fakeSdk(), seams) + await one.connect() + return one +} + +describe("Snowflake SDK in assessment mode", () => { + test("cloud metadata probes are switched off only when the mode is on", () => { + for (const value of ["true", "1", "yes"]) { + const env: NodeJS.ProcessEnv = { ALTIMATE_ASSESSMENT_MODE: value } + keepSdkOffCloudMetadata(env) + expect(env.SNOWFLAKE_DISABLE_PLATFORM_DETECTION).toBe("true") + } + // The SDK skips detection only on "true", so another value left in the environment is replaced. + const preset: NodeJS.ProcessEnv = { ALTIMATE_ASSESSMENT_MODE: "true", SNOWFLAKE_DISABLE_PLATFORM_DETECTION: "false" } + keepSdkOffCloudMetadata(preset) + expect(preset.SNOWFLAKE_DISABLE_PLATFORM_DETECTION).toBe("true") + for (const env of [{}, { ALTIMATE_ASSESSMENT_MODE: "" }, { ALTIMATE_ASSESSMENT_MODE: "0" }, { ALTIMATE_ASSESSMENT_MODE: "false" }] as NodeJS.ProcessEnv[]) { + keepSdkOffCloudMetadata(env) + expect(env.SNOWFLAKE_DISABLE_PLATFORM_DETECTION).toBeUndefined() + } + }) + + test("connecting applies it before the SDK is used", async () => { + const saved = { mode: process.env.ALTIMATE_ASSESSMENT_MODE, off: process.env.SNOWFLAKE_DISABLE_PLATFORM_DETECTION } + try { + process.env.ALTIMATE_ASSESSMENT_MODE = "true" + delete process.env.SNOWFLAKE_DISABLE_PLATFORM_DETECTION + await connect({ type: "snowflake", account: "a", user: "u", password: "p" } as never, fakeSdk()) + expect(process.env.SNOWFLAKE_DISABLE_PLATFORM_DETECTION).toBe("true") + } finally { + for (const [key, value] of [["ALTIMATE_ASSESSMENT_MODE", saved.mode], ["SNOWFLAKE_DISABLE_PLATFORM_DETECTION", saved.off]] as const) { + if (value === undefined) delete process.env[key] + else process.env[key] = value + } + } + }) +}) + +describe("Snowflake server-side cancel", () => { + test("a statement past `cancelAfterMs` is cancelled on Snowflake and reported as a timeout", async () => { + const db = await connector() + const error = await db.execute("SELECT 1", 10, [], { cancelAfterMs: 30 }).catch((e) => e) + expect(isQueryTimeoutError(error)).toBe(true) + expect(error.cancelled).toBe(true) + expect(error.timeoutMs).toBe(30) + expect(error.engine).toBe("snowflake") + expect(cancels).toBe(1) + }) + + test("`queryTimeoutMs` alone never cancels: a slow statement still returns its rows", async () => { + finishAfterMs = 80 + const db = await connector() + const result = await db.execute("SELECT 1", 10, [], { queryTimeoutMs: 30 }) + expect(result.rows).toEqual([[1]]) + expect(cancels).toBe(0) + }) + + test("an aborted signal cancels the statement and rejects with the abort reason", async () => { + const controller = new AbortController() + const db = await connector() + const pending = db.execute("SELECT 1", 10, [], { cancelAfterMs: 60_000, signal: controller.signal }).catch((e) => e) + await sleep(10) + controller.abort(new Error("session stopped")) + const error = await pending + expect(isQueryTimeoutError(error)).toBe(false) + expect(error.message).toBe("session stopped") + expect(cancels).toBe(1) + }) + + test("an already-aborted signal sends nothing", async () => { + const controller = new AbortController() + controller.abort(new Error("session stopped")) + const db = await connector() + const error = await db.execute("SELECT 1", 10, [], { cancelAfterMs: 1_000, signal: controller.signal }).catch((e) => e) + expect(error.message).toBe("session stopped") + expect(sent).toEqual([]) + }) + + test("a statement still waiting on a reconnect at the deadline is never sent", async () => { + const db = await connector() + let release: () => void = () => {} + holdConnect = new Promise((resolve) => (release = resolve)) + up = false + const error = await db.execute("SELECT 1", 10, [], { cancelAfterMs: 30 }).catch((e) => e) + expect(isQueryTimeoutError(error)).toBe(true) + // Nothing reached Snowflake, so nothing is left running there. + expect(error.cancelled).toBe(true) + expect(cancels).toBe(0) + up = true + release() + await sleep(20) + expect(sent).toEqual([]) + }) + + test("when the cancel fails, the error says the statement may still be running", async () => { + cancelError = new Error("network") + const db = await connector() + const error = await db.execute("SELECT 1", 10, [], { cancelAfterMs: 30 }).catch((e) => e) + expect(isQueryTimeoutError(error)).toBe(true) + expect(error.cancelled).toBe(false) + expect(error.message).toContain("it may still be running on the server") + }) + + test("when Snowflake never answers the cancel, the error says the statement may still be running", async () => { + cancelSilently = true + const db = await connector({ cancelWaitMs: 10 }) + const error = await db.execute("SELECT 1", 10, [], { cancelAfterMs: 20 }).catch((e) => e) + expect(isQueryTimeoutError(error)).toBe(true) + expect(error.cancelled).toBe(false) + expect(cancels).toBe(1) + }) + + test("a read retried after the session closed is still covered by the deadline", async () => { + failFirstWith = { code: "407002", message: "Unable to perform operation using terminated connection." } + const db = await connector() + const error = await db.execute("SELECT 1", 10, [], { cancelAfterMs: 40 }).catch((e) => e) + expect(isQueryTimeoutError(error)).toBe(true) + expect(sent).toEqual(["SELECT 1 LIMIT 11", "SELECT 1 LIMIT 11"]) + // The first copy may still be running, so both are cancelled. + expect(cancels).toBe(2) + }) + + test("a statement whose session closed may still be running, so the deadline tries to cancel it", async () => { + // A closed-session error can arrive while the statement keeps running on Snowflake. + failFirstWith = { code: "407002", message: "Unable to perform operation using terminated connection." } + cancelError = new Error("session is gone") + const db = await connector() + let release: () => void = () => {} + holdConnect = new Promise((resolve) => (release = resolve)) + const error = await db.execute("SELECT 1", 10, [], { cancelAfterMs: 30 }).catch((e) => e) + expect(isQueryTimeoutError(error)).toBe(true) + expect(error.cancelled).toBe(false) + expect(cancels).toBe(1) + release() + await sleep(10) + // The retry waiting on the reconnect is never sent. + expect(sent).toEqual(["SELECT 1 LIMIT 11"]) + }) + + test("the abort listener does not outlive the statement", async () => { + finishAfterMs = 1 + const controller = new AbortController() + const added: unknown[] = [] + const removed: unknown[] = [] + const add = controller.signal.addEventListener.bind(controller.signal) + const remove = controller.signal.removeEventListener.bind(controller.signal) + controller.signal.addEventListener = ((type: string, fn: never, options: never) => (added.push(fn), add(type, fn, options))) as never + controller.signal.removeEventListener = ((type: string, fn: never) => (removed.push(fn), remove(type, fn))) as never + const db = await connector() + await db.execute("SELECT 1", 10, [], { cancelAfterMs: 5_000, signal: controller.signal }) + expect(added.length).toBe(1) + expect(removed).toEqual(added) + }) + + test("a statement that finishes in time returns its rows and is not cancelled", async () => { + finishAfterMs = 1 + const db = await connector() + const result = await db.execute("SELECT 1", 10, [], { cancelAfterMs: 5_000 }) + expect(result.rows).toEqual([[1]]) + expect(cancels).toBe(0) + }) + + test("a query error inside the deadline is the query's own error, not a timeout", async () => { + queryError = new Error("SQL compilation error") + const db = await connector() + const error = await db.execute("SELECT 1", 10, [], { cancelAfterMs: 5_000 }).catch((e) => e) + expect(isQueryTimeoutError(error)).toBe(false) + expect(error.message).toContain("SQL compilation error") + expect(cancels).toBe(0) + }) +}) diff --git a/packages/opencode/src/agent/agent.ts b/packages/opencode/src/agent/agent.ts index 2ac7aec2a7..100566f899 100644 --- a/packages/opencode/src/agent/agent.ts +++ b/packages/opencode/src/agent/agent.ts @@ -268,6 +268,7 @@ export const layer = Layer.effect( finops_expensive_queries: "allow", finops_warehouse_advice: "allow", finops_databricks_savings: "allow", + finops_snowflake_savings: "allow", finops_unused_resources: "allow", finops_role_grants: "allow", finops_role_hierarchy: "allow", diff --git a/packages/opencode/src/altimate/native/finops/databricks-savings.ts b/packages/opencode/src/altimate/native/finops/databricks-savings.ts index d0c2a012ff..d0061befc9 100644 --- a/packages/opencode/src/altimate/native/finops/databricks-savings.ts +++ b/packages/opencode/src/altimate/native/finops/databricks-savings.ts @@ -554,11 +554,18 @@ export async function databricksSavings( clusters.length = 0 jobs.length = 0 } + // Every workspace a checked row came from, whether or not it has a finding or a SQL warehouse. + const workspaces = new Set( + [...warehouses, ...clusters, ...jobs] + .map((row) => (row.workspace_id == null ? "" : String(row.workspace_id).trim())) + .filter((id) => id !== ""), + ) return { success: true, window, warehouse: resolved.warehouse, ran_on: runsOn, + workspaces_checked: workspaces.size, warehouses_checked: warehouses.length, clusters_checked: clusters.length, // The query gives a row per task, cluster and trigger: each job is counted once. diff --git a/packages/opencode/src/altimate/native/finops/register.ts b/packages/opencode/src/altimate/native/finops/register.ts index 43d4a263d5..bdefd31e49 100644 --- a/packages/opencode/src/altimate/native/finops/register.ts +++ b/packages/opencode/src/altimate/native/finops/register.ts @@ -7,6 +7,7 @@ import { getQueryHistory } from "./query-history" import { analyzeCredits, getExpensiveQueries } from "./credit-analyzer" import { adviseWarehouse } from "./warehouse-advisor" import { databricksSavings } from "./databricks-savings" +import { snowflakeSavings } from "./snowflake-savings" import { findUnusedResources } from "./unused-resources" import { queryGrants, queryRoleHierarchy, queryUserRoles } from "./role-access" import type { @@ -20,6 +21,8 @@ import type { WarehouseAdvisorResult, DatabricksSavingsParams, DatabricksSavingsResult, + SnowflakeSavingsParams, + SnowflakeSavingsResult, UnusedResourcesParams, UnusedResourcesResult, RoleGrantsParams, @@ -53,6 +56,10 @@ register("finops.databricks_savings", async (params: DatabricksSavingsParams, co return databricksSavings(params, undefined, context) }) +register("finops.snowflake_savings", async (params: SnowflakeSavingsParams, context): Promise => { + return snowflakeSavings(params, undefined, context) +}) + register("finops.unused_resources", async (params: UnusedResourcesParams): Promise => { return findUnusedResources(params) }) diff --git a/packages/opencode/src/altimate/native/finops/snowflake-savings.ts b/packages/opencode/src/altimate/native/finops/snowflake-savings.ts new file mode 100644 index 0000000000..fb0d3e0739 --- /dev/null +++ b/packages/opencode/src/altimate/native/finops/snowflake-savings.ts @@ -0,0 +1,570 @@ +// altimate_change - new file +/** + * Snowflake savings: measures usage facts per warehouse from `SHOW WAREHOUSES` and + * `SNOWFLAKE.ACCOUNT_USAGE`, and hands them to altimate-core, which decides what qualifies and what + * that saves. The rules live in core; this file only measures. + * + * Days are UTC. Every window ends at 00:00 UTC on the window's end date, and a run happens at least + * six hours after that, past the latency of each view read here: WAREHOUSE_METERING_HISTORY (three + * hours, six for cloud services credits), WAREHOUSE_EVENTS_HISTORY (three) and QUERY_HISTORY (45 + * minutes). + */ + +import * as core from "@altimateai/altimate-core" +import { isQueryTimeoutError } from "@altimateai/drivers/types" +import { Flag } from "@opencode-ai/core/flag/flag" +import * as Registry from "../connections/registry" +import type { QueryContext } from "../query-execution" +import { RunBudget } from "../../run-budget" +import { savingsWindow } from "./databricks-savings" +import { resolveFinopsWarehouse } from "./warehouse-resolver" +import type { SnowflakeSavingsParams, SnowflakeSavingsReport, SnowflakeSavingsResult } from "../types" + +const SUPPORTED_TYPES = ["snowflake"] as const + +/** Days of usage the facts cover; the rules price a day of this window. */ +const LOOKBACK_DAYS = 7 + +/** Rows read per query; a query that fills it says so in the result. */ +export const ROW_LIMIT = 100_000 + +/** The driver asks for one row more than its limit, and `SHOW` accepts a `LIMIT` of at most 10,000. */ +export const SHOW_ROW_LIMIT = 9_999 + +/** Dollars per credit when the account's rate sheet cannot be read: Enterprise list price. */ +export const LIST_PRICE_PER_CREDIT = 3 + +// The whole check stops after this long, inside the 10 minutes a Claude Code plugin's process may +// run, so a running query is cancelled here and not left behind when the process is killed. +export const SAVINGS_RUN_LIMIT_MS = 9 * 60_000 + +/** The end of the window as a timestamp for filtering `ACCOUNT_USAGE`, and as UTC wall-clock time. */ +const WINDOW_END = `TO_TIMESTAMP_TZ(? || ' 00:00:00 +0000', 'YYYY-MM-DD HH24:MI:SS TZHTZM')` +const WINDOW_END_UTC = `TO_TIMESTAMP_NTZ(?, 'YYYY-MM-DD')` + +export const SNOWFLAKE_WAREHOUSES_SQL = `SHOW WAREHOUSES` + +/** + * Credits per warehouse by UTC day (`usage_day`), and by the 24 hours from 23:00 UTC the day before + * (`idle_day`), the window the idle check reads. + */ +export const SNOWFLAKE_METERING_FACTS_SQL = ` +WITH m AS ( + SELECT WAREHOUSE_ID AS warehouse_id, WAREHOUSE_NAME AS warehouse_name, + TO_TIMESTAMP_NTZ(CONVERT_TIMEZONE('UTC', START_TIME)) AS hour_start, CREDITS_USED AS credits_used + FROM SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_METERING_HISTORY + WHERE START_TIME >= DATEADD(hour, -169, ${WINDOW_END}) AND START_TIME < ${WINDOW_END} AND WAREHOUSE_ID > 0 +) +SELECT warehouse_id, warehouse_name, + IFF(hour_start >= DATEADD(day, -7, ${WINDOW_END_UTC}), TO_VARCHAR(hour_start::DATE, 'YYYY-MM-DD'), NULL) AS usage_day, + IFF(hour_start < DATEADD(hour, -1, ${WINDOW_END_UTC}), TO_VARCHAR(DATEADD(hour, 1, hour_start)::DATE, 'YYYY-MM-DD'), NULL) AS idle_day, + SUM(credits_used) AS credits_used +FROM m GROUP BY 1, 2, 3, 4 ORDER BY 2, 3, 4 +` + +/** + * Per warehouse, cluster and day: the periods from each cluster's resume to its next event that ran at + * least one query, their seconds, and the seconds with no query running. Overlapping queries count as + * one stretch of work. Seconds are `DATEDIFF('second', ...)`, which counts second boundaries crossed: + * this is how the idle times stored by Altimate's hosted product are counted. + */ +export const SNOWFLAKE_PERIOD_FACTS_SQL = ` +WITH ev AS ( + SELECT WAREHOUSE_ID AS warehouse_id, WAREHOUSE_NAME AS warehouse_name, CLUSTER_NUMBER AS cluster_number, + TO_TIMESTAMP_NTZ(CONVERT_TIMEZONE('UTC', TIMESTAMP)) AS ts, EVENT_NAME AS event_name + FROM SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_EVENTS_HISTORY + WHERE TIMESTAMP >= DATEADD(day, -9, ${WINDOW_END}) AND TIMESTAMP < DATEADD(day, 1, ${WINDOW_END}) + AND EVENT_NAME IN ('RESUME_CLUSTER', 'SUSPEND_CLUSTER') AND EVENT_STATE = 'STARTED' +), +status AS ( + SELECT warehouse_id, warehouse_name, cluster_number, ts AS valid_from, event_name, + LEAD(ts) OVER (PARTITION BY warehouse_id, cluster_number ORDER BY ts) AS valid_to + FROM ev +), +periods AS ( + SELECT warehouse_id, warehouse_name, cluster_number, valid_from, valid_to + FROM status + WHERE event_name = 'RESUME_CLUSTER' AND valid_to IS NOT NULL + AND DATEDIFF('millisecond', valid_from, valid_to) >= 1000 + AND valid_from >= DATEADD(hour, -169, ${WINDOW_END_UTC}) AND valid_from < ${WINDOW_END_UTC} +), +q AS ( + SELECT WAREHOUSE_ID AS warehouse_id, CLUSTER_NUMBER AS cluster_number, + TO_TIMESTAMP_NTZ(CONVERT_TIMEZONE('UTC', START_TIME)) AS st, + TO_TIMESTAMP_NTZ(CONVERT_TIMEZONE('UTC', END_TIME)) AS et + FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY + WHERE START_TIME >= DATEADD(day, -10, ${WINDOW_END}) AND START_TIME < DATEADD(day, 1, ${WINDOW_END}) + AND WAREHOUSE_ID IS NOT NULL AND WAREHOUSE_SIZE IS NOT NULL AND CLUSTER_NUMBER IS NOT NULL +), +adjusted AS ( + SELECT p.warehouse_id, p.warehouse_name, p.cluster_number, p.valid_from, p.valid_to, + GREATEST(q.st, p.valid_from) AS query_start, LEAST(q.et, p.valid_to) AS query_end + FROM periods p JOIN q + ON q.warehouse_id = p.warehouse_id AND q.cluster_number = p.cluster_number + AND q.st < p.valid_to AND q.et > p.valid_from +), +flagged AS ( + SELECT *, IFF(query_start > MAX(query_end) OVER ( + PARTITION BY warehouse_id, cluster_number, valid_from ORDER BY query_start, query_end + ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING), 1, 0) AS later_group, + ROW_NUMBER() OVER (PARTITION BY warehouse_id, cluster_number, valid_from ORDER BY query_start, query_end) AS rn + FROM adjusted +), +grouped AS ( + SELECT *, SUM(IFF(rn = 1, 1, later_group)) OVER ( + PARTITION BY warehouse_id, cluster_number, valid_from ORDER BY rn + ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS group_id + FROM flagged +), +windows AS ( + SELECT warehouse_id, warehouse_name, cluster_number, valid_from, valid_to, group_id, + MIN(query_start) AS group_start, MAX(query_end) AS group_end + FROM grouped GROUP BY 1, 2, 3, 4, 5, 6 +), +gaps AS ( + SELECT *, LAG(group_end) OVER (PARTITION BY warehouse_id, cluster_number, valid_from ORDER BY group_start) AS prev_end, + LEAD(group_start) OVER (PARTITION BY warehouse_id, cluster_number, valid_from ORDER BY group_start) AS next_start + FROM windows +), +per_period AS ( + SELECT warehouse_id, warehouse_name, cluster_number, valid_from, + DATEDIFF('second', valid_from, valid_to) AS active_seconds, + SUM(DATEDIFF('second', COALESCE(prev_end, valid_from), group_start) + + IFF(next_start IS NULL, DATEDIFF('second', group_end, valid_to), 0)) AS idle_seconds + FROM gaps GROUP BY 1, 2, 3, 4, 5 +) +SELECT warehouse_id, warehouse_name, cluster_number, + IFF(valid_from >= DATEADD(day, -7, ${WINDOW_END_UTC}), TO_VARCHAR(valid_from::DATE, 'YYYY-MM-DD'), NULL) AS usage_day, + IFF(valid_from < DATEADD(hour, -1, ${WINDOW_END_UTC}), TO_VARCHAR(DATEADD(hour, 1, valid_from)::DATE, 'YYYY-MM-DD'), NULL) AS idle_day, + COUNT(*) AS periods, COUNT_IF(active_seconds <= 80) AS short_periods, + SUM(active_seconds) AS active_seconds, SUM(idle_seconds) AS idle_seconds +FROM per_period GROUP BY 1, 2, 3, 4, 5 ORDER BY 2, 3, 4, 5 +` + +/** + * Per warehouse and day: how many times it resumed, suspended and resumed again less than a minute after + * the first resume, by the whole seconds elapsed between the two resumes. The event log + * repeats each warehouse-level event, so events are made distinct first. + */ +export const SNOWFLAKE_RESTART_FACTS_SQL = ` +WITH e AS ( + SELECT DISTINCT WAREHOUSE_ID AS warehouse_id, WAREHOUSE_NAME AS warehouse_name, + TO_TIMESTAMP_NTZ(CONVERT_TIMEZONE('UTC', TIMESTAMP)) AS ts, EVENT_NAME AS event_name + FROM SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_EVENTS_HISTORY + WHERE TIMESTAMP >= DATEADD(day, -7, ${WINDOW_END}) AND TIMESTAMP < ${WINDOW_END} + AND EVENT_NAME IN ('RESUME_WAREHOUSE', 'SUSPEND_WAREHOUSE', 'WAREHOUSE_CONSISTENT') +), +s AS ( + SELECT warehouse_id, warehouse_name, ts, event_name, + LEAD(event_name, 1) OVER (PARTITION BY warehouse_id, ts::DATE ORDER BY ts, event_name) AS n1, + LEAD(event_name, 2) OVER (PARTITION BY warehouse_id, ts::DATE ORDER BY ts, event_name) AS n2, + LEAD(event_name, 3) OVER (PARTITION BY warehouse_id, ts::DATE ORDER BY ts, event_name) AS n3, + LEAD(event_name, 4) OVER (PARTITION BY warehouse_id, ts::DATE ORDER BY ts, event_name) AS n4, + LEAD(ts, 4) OVER (PARTITION BY warehouse_id, ts::DATE ORDER BY ts, event_name) AS t4 + FROM e +) +SELECT warehouse_id, warehouse_name, TO_VARCHAR(ts::DATE, 'YYYY-MM-DD') AS day, + TRUNC(DATEDIFF('millisecond', ts, t4) / 1000) AS gap_seconds, COUNT(*) AS bounces +FROM s +WHERE event_name = 'RESUME_WAREHOUSE' AND n1 = 'WAREHOUSE_CONSISTENT' AND n2 = 'SUSPEND_WAREHOUSE' + AND n3 = 'WAREHOUSE_CONSISTENT' AND n4 = 'RESUME_WAREHOUSE' + AND DATEDIFF('millisecond', ts, t4) < 60000 +GROUP BY 1, 2, 3, 4 ORDER BY 2, 3, 4 +` + +/** + * Per warehouse: queries that ended in the window, those that executed for five seconds or less, and the + * credits attributed to those: each metered hour's compute credits shared by execution time, plus the + * query's share of the day's billable cloud services credits. + */ +export const SNOWFLAKE_QUERY_FACTS_SQL = ` +WITH metered AS ( + SELECT WAREHOUSE_ID AS warehouse_id, TO_TIMESTAMP_NTZ(CONVERT_TIMEZONE('UTC', START_TIME)) AS hour_start, + CREDITS_USED_COMPUTE AS compute_credits, CREDITS_USED_CLOUD_SERVICES AS cloud_credits + FROM SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_METERING_HISTORY + WHERE START_TIME >= DATEADD(day, -9, ${WINDOW_END}) AND START_TIME < ${WINDOW_END} +), +hourly AS (SELECT warehouse_id, hour_start, SUM(compute_credits) AS compute_credits FROM metered WHERE warehouse_id > 0 GROUP BY 1, 2), +daily AS ( + SELECT hour_start::DATE AS day, SUM(cloud_credits) AS cloud_credits, + GREATEST(SUM(cloud_credits) - SUM(compute_credits) * 0.1, 0) AS billable_cloud_credits + FROM metered GROUP BY 1 +), +q AS ( + SELECT QUERY_ID AS query_id, WAREHOUSE_ID AS warehouse_id, WAREHOUSE_NAME AS warehouse_name, WAREHOUSE_SIZE AS warehouse_size, + EXECUTION_TIME AS execution_ms, CREDITS_USED_CLOUD_SERVICES AS cloud_credits, + TO_TIMESTAMP_NTZ(CONVERT_TIMEZONE('UTC', START_TIME)) AS st, + TO_TIMESTAMP_NTZ(CONVERT_TIMEZONE('UTC', END_TIME)) AS et, + DATEADD('millisecond', COALESCE(QUEUED_OVERLOAD_TIME, 0) + COALESCE(COMPILATION_TIME, 0) + COALESCE(QUEUED_PROVISIONING_TIME, 0) + + COALESCE(QUEUED_REPAIR_TIME, 0) + COALESCE(LIST_EXTERNAL_FILES_TIME, 0), + TO_TIMESTAMP_NTZ(CONVERT_TIMEZONE('UTC', START_TIME))) AS exec_start + FROM SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY + WHERE START_TIME >= DATEADD(day, -9, ${WINDOW_END}) AND START_TIME < ${WINDOW_END} AND WAREHOUSE_ID IS NOT NULL +), +overlap AS ( + SELECT q.query_id, q.warehouse_id, h.hour_start, h.compute_credits, + DATEDIFF('millisecond', GREATEST(q.exec_start, h.hour_start), LEAST(q.et, DATEADD(hour, 1, h.hour_start))) AS ran_ms + FROM q JOIN hourly h + ON h.warehouse_id = q.warehouse_id AND h.hour_start >= DATE_TRUNC('hour', q.exec_start) AND h.hour_start < q.et + WHERE q.warehouse_size IS NOT NULL AND q.et > q.st +), +allocated AS ( + SELECT query_id, SUM(compute_credits * ran_ms / total_ms) AS compute_credits + FROM (SELECT *, SUM(ran_ms) OVER (PARTITION BY warehouse_id, hour_start) AS total_ms FROM overlap WHERE ran_ms > 0) + GROUP BY 1 +) +SELECT q.warehouse_id, q.warehouse_name, + COUNT(*) AS query_count, + COUNT_IF(q.execution_ms <= 5000) AS sub_five_second_queries, + COALESCE(SUM(IFF(q.execution_ms <= 5000, + COALESCE(a.compute_credits, 0) + + IFF(d.cloud_credits > 0, q.cloud_credits / d.cloud_credits * d.billable_cloud_credits, 0), 0)), 0) AS sub_five_second_credits +FROM q +LEFT JOIN allocated a ON a.query_id = q.query_id +LEFT JOIN daily d ON d.day = q.st::DATE +WHERE q.et >= DATEADD(day, -7, ${WINDOW_END_UTC}) AND q.et < ${WINDOW_END_UTC} +GROUP BY 1, 2 ORDER BY 2 +` + +/** The account's contracted price of a warehouse credit: the latest above zero before the window's end, the lowest if that date has several. */ +export const SNOWFLAKE_RATE_SQL = ` +SELECT EFFECTIVE_RATE AS effective_rate, CURRENCY AS currency, TO_VARCHAR(DATE, 'YYYY-MM-DD') AS rate_date +FROM SNOWFLAKE.ORGANIZATION_USAGE.RATE_SHEET_DAILY +WHERE ACCOUNT_NAME = CURRENT_ACCOUNT_NAME() AND SERVICE_TYPE = 'WAREHOUSE_METERING' AND USAGE_TYPE = 'compute' AND DATE < ${WINDOW_END_UTC}::DATE + AND EFFECTIVE_RATE > 0 +ORDER BY DATE DESC, EFFECTIVE_RATE LIMIT 1 +` + +/** Core's rules: a `SnowflakeSavingsInput` as JSON in, the report out. */ +export type RulesEngine = (input: string) => SnowflakeSavingsReport + +type CoreModule = { snowflakeSavings?: unknown; initSdk?: (...args: [string, string, string, boolean, boolean]) => void } + +/** The installed core's rules, or null when its binary predates them. See `databricks-savings.ts` for assessment mode. */ +export function rulesEngine(lib: CoreModule = core as unknown as CoreModule): RulesEngine | null { + const found = lib.snowflakeSavings + if (typeof found !== "function") return null + if (Flag.ALTIMATE_ASSESSMENT_MODE) lib.initSdk?.("", "", "", false, false) + return found as RulesEngine +} + +type Row = Record + +function num(value: unknown): number { + const parsed = typeof value === "bigint" ? Number(value) : Number(value ?? 0) + return Number.isFinite(parsed) ? parsed : 0 +} + +const whole = (value: unknown) => Math.max(0, Math.round(num(value))) + +function optionalWhole(value: unknown): number | null { + return value == null || value === "" ? null : whole(value) +} + +function records(result: { columns: string[]; rows: unknown[][] }): Row[] { + return result.rows.map((row) => Object.fromEntries(result.columns.map((name, at) => [name, row[at]]))) +} + +/** A map entry, created on first use. */ +function entry(map: Map, key: string, create: () => V): V { + let found = map.get(key) + if (found === undefined) { + found = create() + map.set(key, found) + } + return found +} + +export interface FactRows { + warehouses: Row[] + metering: Row[] + periods: Row[] + restarts: Row[] + queries: Row[] +} + +/** Milliseconds of a `created_on` value, or NaN when it cannot be read. */ +function timeOf(value: unknown): number { + if (value instanceof Date) return value.getTime() + const json = (value as { toJSON?: () => unknown } | null | undefined)?.toJSON?.() + return Date.parse(String(json ?? value ?? "")) +} + +/** + * The input core reads, one entry per warehouse `SHOW WAREHOUSES` lists, which gives names and no ids. + * Usage belongs to a warehouse id: a renamed warehouse keeps its id, so an id's usage goes to the last + * name it was seen under. A dropped warehouse's name can come back on a new one, so a name keeps its + * newest id, and usage that ended before the listed warehouse was created is its predecessor's. + * `unlisted` names the warehouses that billed credits but are not listed: dropped, or hidden from the role. + */ +export function buildFacts(rows: FactRows) { + const listed = new Set(rows.warehouses.map((row) => String(row.name ?? ""))) + const lastSeen = new Map() + const see = (row: Row, day: unknown) => { + const id = num(row.warehouse_id) + const name = String(row.warehouse_name ?? "") + const at = String(day ?? "") + const known = lastSeen.get(id) + if (!known || at > known.day || (at === known.day && listed.has(name) && !listed.has(known.name))) { + lastSeen.set(id, { day: at, name }) + } + } + for (const row of [...rows.metering, ...rows.periods]) see(row, row.usage_day ?? row.idle_day) + for (const row of rows.restarts) see(row, row.day) + for (const row of rows.queries) see(row, "") + + const newestId = new Map() + for (const [id, { name }] of lastSeen) newestId.set(name, Math.max(newestId.get(name) ?? 0, id)) + const nameOf = (row: Row) => lastSeen.get(num(row.warehouse_id))?.name ?? "" + const of = (list: Row[]) => { + const byName = new Map() + for (const row of list) { + const name = nameOf(row) + if (newestId.get(name) === num(row.warehouse_id)) entry(byName, name, () => []).push(row) + } + return byName + } + const metering = of(rows.metering) + const periods = of(rows.periods) + const restarts = of(rows.restarts) + const queries = of(rows.queries) + + /** The listed warehouse's id, unless its usage all predates it. */ + const idFor = (row: Row): number | undefined => { + const id = newestId.get(String(row.name ?? "")) + if (id === undefined) return undefined + const created = timeOf(row.created_on) + const day = lastSeen.get(id)?.day + if (day && Number.isFinite(created) && day < new Date(created).toISOString().slice(0, 10)) return undefined + return id + } + + const facts = rows.warehouses.map((row) => { + const name = String(row.name ?? "") + const id = idFor(row) + const usedBy = (byName: Map) => (id === undefined ? [] : (byName.get(name) ?? [])) + const usage = new Map() + const idle = new Map() + const clusters = new Map>() + const usageDay = (day: string) => entry(usage, day, () => ({ day, credits_used: null, active_periods: 0, short_periods: 0 })) + const idleDay = (day: string) => entry(idle, day, () => ({ day, active_seconds: 0, idle_seconds: 0, credits_used: 0 })) + + for (const one of usedBy(metering)) { + const credits = num(one.credits_used) + if (one.usage_day != null) { + const day = usageDay(String(one.usage_day)) + day.credits_used = (day.credits_used ?? 0) + credits + } + if (one.idle_day != null) idleDay(String(one.idle_day)).credits_used += credits + } + for (const one of usedBy(periods)) { + const active = whole(one.active_seconds) + const idleSeconds = whole(one.idle_seconds) + if (one.usage_day != null) { + const key = String(one.usage_day) + const day = usageDay(key) + day.active_periods += whole(one.periods) + day.short_periods += whole(one.short_periods) + const number = whole(one.cluster_number) + const cluster = entry(entry(clusters, key, () => new Map()), String(number), () => ({ + cluster_number: number, + idle_seconds: 0, + busy_seconds: 0, + })) + cluster.idle_seconds += idleSeconds + cluster.busy_seconds += active - idleSeconds + } + if (one.idle_day != null) { + const day = idleDay(String(one.idle_day)) + day.active_seconds += active + day.idle_seconds += idleSeconds + } + } + // A rename splits a day's or the window's rows by name; they are one warehouse's. + const gaps = new Map>() + for (const one of usedBy(restarts)) { + const seconds = whole(one.gap_seconds) + const gap = entry(entry(gaps, String(one.day), () => new Map()), String(seconds), () => ({ gap_seconds: seconds, bounces: 0 })) + gap.bounces += whole(one.bounces) + } + const query = { count: 0, short: 0, credits: 0 } + for (const one of usedBy(queries)) { + query.count += whole(one.query_count) + query.short += whole(one.sub_five_second_queries) + query.credits += num(one.sub_five_second_credits) + } + return { + // Prefixed so an unused warehouse named like another's numeric id cannot collide with it. + warehouse_id: id ? String(id) : `unused:${name}`, + warehouse_name: name, + warehouse_size: row.size == null ? null : String(row.size), + warehouse_type: row.type == null ? null : String(row.type), + min_cluster_count: optionalWhole(row.min_cluster_count), + max_cluster_count: optionalWhole(row.max_cluster_count), + query_count: query.count, + sub_five_second_queries: query.short, + sub_five_second_credits: query.credits, + usage_days: [...usage.values()], + idle_days: [...idle.values()], + cluster_days: [...clusters].map(([day, byNumber]) => ({ day, clusters: [...byNumber.values()] })), + restart_days: [...gaps].map(([day, byGap]) => ({ day, gaps: [...byGap.values()] })), + } + }) + const unlisted = [...metering.keys()].filter((name) => !listed.has(name)).sort() + return { facts, unlisted } +} + +/** The price of a credit: the account's rate when it can be read in dollars, otherwise list price. */ +export function creditPrice(rate: Row | undefined): { per_credit: number; source: "rate_sheet" | "list_price"; as_of?: string } { + const value = num(rate?.effective_rate) + if (value > 0 && String(rate?.currency ?? "").toUpperCase() === "USD") { + return { per_credit: value, source: "rate_sheet", as_of: String(rate?.rate_date ?? "") } + } + return { per_credit: LIST_PRICE_PER_CREDIT, source: "list_price" } +} + +/** `rules` defaults to the installed core's; tests pass their own. */ +export async function snowflakeSavings( + params: SnowflakeSavingsParams, + rules: RulesEngine | null = rulesEngine(), + context?: QueryContext, +): Promise { + const window = savingsWindow(params.now_ms ?? Date.now()) + const failed = (error: string): SnowflakeSavingsResult => ({ success: false, window, error }) + + const resolved = resolveFinopsWarehouse({ + requested: params.warehouse?.trim() || undefined, + supportedTypes: SUPPORTED_TYPES, + operationName: "The Snowflake savings check", + }) + if (resolved.kind === "error") return failed(resolved.error) + + if (!rules) { + return failed("This altimate-code build's altimate-core has no Snowflake savings rules. Update altimate-code, then try again.") + } + + // Stopping the session cancels the running query and starts no other. The check stops at its own + // limit, or earlier at the session's query cutoff (`altimate run --time-budget`). + const signal = context?.signal + const ownLimitAt = Date.now() + SAVINGS_RUN_LIMIT_MS + const sessionCutoff = context?.sessionID ? RunBudget.queryDeadline(context.sessionID) : undefined + const stopsAt = Math.min(ownLimitAt, sessionCutoff ?? Infinity) + const limitName = stopsAt < ownLimitAt ? "the run's time budget" : "the check's 9-minute limit" + + // Connecting can wait on the network or a sign-in: it counts toward the limit and yields to a stop. + const beforeStop = async (work: Promise, what: string): Promise => { + // A stop can come first, and a later failure of `work` must not go unhandled. + work.catch(() => {}) + signal?.throwIfAborted() + let timer: ReturnType | undefined + let onAbort: (() => void) | undefined + try { + return await Promise.race([ + work, + new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error(`${what} took longer than ${limitName}.`)), Math.max(0, stopsAt - Date.now())) + }), + new Promise((_, reject) => { + onAbort = () => reject(signal!.reason) + signal?.addEventListener("abort", onAbort, { once: true }) + }), + ]) + } finally { + clearTimeout(timer) + if (onAbort) signal?.removeEventListener("abort", onAbort) + } + } + + try { + const connector = await beforeStop(Registry.get(resolved.warehouse), "Connecting to the warehouse") + const config = Registry.getConfig(resolved.warehouse) as Record | undefined + const limited: string[] = [] + const read = async (what: string, sql: string, rowLimit = ROW_LIMIT) => { + signal?.throwIfAborted() + const left = stopsAt - Date.now() + if (left <= 0) throw new Error(`The ${what} query was not started: it would run past ${limitName}.`) + const binds = Array.from({ length: sql.split("?").length - 1 }, () => window.end) + try { + const result = await connector.execute(sql, rowLimit, binds, { cancelAfterMs: left, signal }) + if (result.truncated) limited.push(what) + return records(result) + } catch (e) { + if (!isQueryTimeoutError(e)) throw e + const outcome = (e as { cancelled?: boolean }).cancelled + ? "and was cancelled on the warehouse" + : "and may still be running on the warehouse" + throw new Error(`The ${what} query ran past ${limitName} ${outcome}.`) + } + } + const warehouses = await read("warehouses", SNOWFLAKE_WAREHOUSES_SQL, SHOW_ROW_LIMIT) + if (warehouses.length === 0) return failed("SHOW WAREHOUSES lists no warehouse this connection's role can see.") + + // A view the role may not read stops only the checks that need it; the rest still report. + const skipped: { check: string; reason: string }[] = [] + let checksMissing = 0 + const reasonOf = (e: unknown) => (e instanceof Error ? e.message : String(e)).split("\n")[0]!.slice(0, 300) + const optional = async (check: string, what: string, sql: string) => { + try { + return await read(what, sql) + } catch (e) { + if (signal?.aborted) throw e + checksMissing++ + skipped.push({ check, reason: reasonOf(e) }) + return [] + } + } + // Most accounts cannot read the rate sheet (it needs organization access); list price stands in. + const rate = await read("credit rate", SNOWFLAKE_RATE_SQL).catch((e) => { + if (signal?.aborted) throw e + // An unreadable rate sheet is ordinary; a query that may still be running is worth naming. + if (reasonOf(e).includes("may still be running")) skipped.push({ check: "The account's credit rate", reason: reasonOf(e) }) + return [] as Row[] + }) + const metering = await read("metering", SNOWFLAKE_METERING_FACTS_SQL) + const periods = await optional( + "High Idle Time, Underutilized Cluster and the short-period signal of Downsize Warehouse", + "active periods", + SNOWFLAKE_PERIOD_FACTS_SQL, + ) + const restarts = await optional("Frequent Restart Overcharge", "restart events", SNOWFLAKE_RESTART_FACTS_SQL) + const queries = await optional("The short-query signal of Downsize Warehouse", "query history", SNOWFLAKE_QUERY_FACTS_SQL) + + // All four checks rest on these three queries: with none of them, nothing was assessed. + if (checksMissing === 3) { + // A query that may still be running and billing matters more than the ones after it. + const reason = skipped.find((one) => one.reason.includes("may still be running")) ?? skipped.at(-1)! + return failed(`No check could run. ${reason.reason}`) + } + + const price = creditPrice(rate[0]) + const { facts, unlisted } = buildFacts({ warehouses, metering, periods, restarts, queries }) + if (unlisted.length > 0) { + skipped.push({ + check: `${unlisted.length} warehouse${unlisted.length === 1 ? "" : "s"} that billed credits`, + reason: `SHOW WAREHOUSES does not list ${unlisted.slice(0, 5).join(", ")}${unlisted.length > 5 ? ", …" : ""}: dropped, renamed, or not visible to the connection's role`, + }) + } + const report = rules(JSON.stringify({ lookback_days: LOOKBACK_DAYS, price_per_credit: price.per_credit, warehouses: facts })) + return { + success: true, + window, + warehouse: resolved.warehouse, + ran_on: config?.warehouse == null ? undefined : String(config.warehouse), + warehouses_checked: facts.length, + price, + skipped, + limited, + // What each warehouse is and costs now, so a later run can tell what changed. + warehouses: facts.map((one) => ({ + id: one.warehouse_id, + name: one.warehouse_name, + type: one.warehouse_type ?? "", + size: one.warehouse_size, + window_cost: one.usage_days.reduce((sum, day) => sum + (day.credits_used ?? 0), 0) * price.per_credit, + })), + report, + } + } catch (e) { + return failed(e instanceof Error ? e.message : String(e)) + } +} diff --git a/packages/opencode/src/altimate/native/types.ts b/packages/opencode/src/altimate/native/types.ts index c221a4cba9..4d78d45830 100644 --- a/packages/opencode/src/altimate/native/types.ts +++ b/packages/opencode/src/altimate/native/types.ts @@ -686,6 +686,8 @@ export interface DatabricksSavingsResult { /** The connection that ran the check, and the SQL warehouse it runs on. */ warehouse?: string ran_on?: string + /** Distinct workspaces across every warehouse, cluster and job row checked, with or without a finding. */ + workspaces_checked?: number warehouses_checked?: number clusters_checked?: number jobs_checked?: number @@ -699,6 +701,64 @@ export interface DatabricksSavingsResult { error?: string } +// --- FinOps: Snowflake warehouse savings --- + +export interface SnowflakeSavingsParams { + /** Snowflake connection name. If omitted, the first Snowflake connection is used. */ + warehouse?: string + /** Clock override for tests; the window ends on the UTC date six hours before this instant. */ + now_ms?: number +} + +/** One saving on one warehouse, as altimate-core reports it. Monthly figures are 0 when it has no dollar estimate. */ +export interface SnowflakeSavingsFinding { + check_id: "high_idle_time" | "downsize_warehouse" | "underutilized_cluster" | "frequent_restart_overcharge" + title: string + resource_type: "sql_warehouse" + resource_id: string + resource_name: string + workspace_id?: null + severity: "critical" | "high" | "medium" | "low" + daily_min: number + daily_max: number + monthly_min: number + monthly_max: number + evidence: Record & { + kind: "high_idle_time" | "downsize_warehouse" | "underutilized_cluster" | "frequent_restart" + } +} + +export interface SnowflakeSavingsReport { + lookback_days: number + price_per_credit: number + /** Largest monthly saving first; findings with no dollar estimate last. */ + findings: SnowflakeSavingsFinding[] + /** Warehouses with a dollar saving, each counted once at its largest low and high end. */ + total: { monthly_min: number; monthly_max: number; resources: number } + /** Always empty: no Snowflake rule prices alternatives. */ + what_if: DatabricksWhatIf[] +} + +export interface SnowflakeSavingsResult { + success: boolean + /** The days the facts cover: `start` inclusive, `end` exclusive (YYYY-MM-DD, UTC). */ + window: { start: string; end: string } + /** The connection that ran the check, and the warehouse its configuration names. */ + warehouse?: string + ran_on?: string + warehouses_checked?: number + /** The dollars one credit is priced at, and where that came from. */ + price?: { per_credit: number; source: "rate_sheet" | "list_price"; as_of?: string } + /** Checks whose views the connection could not read, and billed warehouses it could not list. */ + skipped?: { check: string; reason: string }[] + /** What a query read only the first rows of, e.g. `active periods`. */ + limited?: string[] + /** Each warehouse as it is now, and its cost over the window. */ + warehouses?: { id: string; workspace_id?: string | null; name: string; type: string; size: string | null; window_cost: number }[] + report?: SnowflakeSavingsReport + error?: string +} + // --- FinOps: Warehouse Advisor --- export interface WarehouseAdvisorParams { @@ -1319,6 +1379,7 @@ export const BridgeMethods = { "finops.expensive_queries": {} as { params: ExpensiveQueriesParams; result: ExpensiveQueriesResult }, "finops.warehouse_advice": {} as { params: WarehouseAdvisorParams; result: WarehouseAdvisorResult }, "finops.databricks_savings": {} as { params: DatabricksSavingsParams; result: DatabricksSavingsResult }, + "finops.snowflake_savings": {} as { params: SnowflakeSavingsParams; result: SnowflakeSavingsResult }, "finops.unused_resources": {} as { params: UnusedResourcesParams; result: UnusedResourcesResult }, "finops.role_grants": {} as { params: RoleGrantsParams; result: RoleGrantsResult }, "finops.role_hierarchy": {} as { params: RoleHierarchyParams; result: RoleHierarchyResult }, diff --git a/packages/opencode/src/altimate/tool-cli/allow-list.ts b/packages/opencode/src/altimate/tool-cli/allow-list.ts index f62a6b2d08..8e0e6b56fb 100644 --- a/packages/opencode/src/altimate/tool-cli/allow-list.ts +++ b/packages/opencode/src/altimate/tool-cli/allow-list.ts @@ -50,6 +50,7 @@ import { DbtUnitTestGenTool } from "../tools/dbt-unit-test-gen" import { DocsShareCommentsTool } from "../tools/docs-share-comments" import { FinopsAnalyzeCreditsTool } from "../tools/finops-analyze-credits" import { FinopsDatabricksSavingsTool } from "../tools/finops-databricks-savings" +import { FinopsSnowflakeSavingsTool } from "../tools/finops-snowflake-savings" import { FinopsExpensiveQueriesTool } from "../tools/finops-expensive-queries" import { FinopsQueryHistoryTool } from "../tools/finops-query-history" import { FinopsRoleGrantsTool, FinopsRoleHierarchyTool, FinopsUserRolesTool } from "../tools/finops-role-access" @@ -131,6 +132,7 @@ export const EXPOSED: Record = { docs_share_comments: DocsShareCommentsTool, finops_analyze_credits: FinopsAnalyzeCreditsTool, finops_databricks_savings: FinopsDatabricksSavingsTool, + finops_snowflake_savings: FinopsSnowflakeSavingsTool, finops_expensive_queries: FinopsExpensiveQueriesTool, finops_query_history: FinopsQueryHistoryTool, finops_role_grants: FinopsRoleGrantsTool, diff --git a/packages/opencode/src/altimate/tools/finops-databricks-savings.ts b/packages/opencode/src/altimate/tools/finops-databricks-savings.ts index 4b8cdf539d..867217f35c 100644 --- a/packages/opencode/src/altimate/tools/finops-databricks-savings.ts +++ b/packages/opencode/src/altimate/tools/finops-databricks-savings.ts @@ -10,6 +10,7 @@ type SavingsMetadata = { window?: { start: string; end: string } warehouse?: string ran_on?: string + workspaces_checked?: number warehouses_checked?: number clusters_checked?: number jobs_checked?: number @@ -22,13 +23,13 @@ type SavingsMetadata = { needs_confirmation?: boolean } -function dollars(value: number): string { +export function dollars(value: number): string { return value >= 100 ? `$${Math.round(value).toLocaleString("en-US")}` : `$${value.toFixed(2)}` } -function range(low: number, high: number): string { +export function range(low: number, high: number): string { return dollars(low) === dollars(high) ? dollars(high) : `${dollars(low)}–${dollars(high)}` } @@ -47,7 +48,7 @@ const tenths = (value: unknown) => Number(value) < 0.05 ? "under 0.1" : Number(value).toLocaleString("en-US", { minimumFractionDigits: 1, maximumFractionDigits: 1 }) /** Free text from the workspace on one line, with control and invisible characters gone. */ -const oneLine = (value: unknown) => +export const oneLine = (value: unknown) => String(value ?? "") .replace(/\p{Cf}/gu, "") .replace(/[\s\p{Cc}]+/gu, " ") @@ -167,6 +168,7 @@ export const FinopsDatabricksSavingsTool = Tool.define("finops_databricks_saving window: result.window, warehouse: result.warehouse, ran_on: result.ran_on, + workspaces_checked: result.workspaces_checked, warehouses_checked: result.warehouses_checked, clusters_checked: result.clusters_checked, jobs_checked: result.jobs_checked, diff --git a/packages/opencode/src/altimate/tools/finops-snowflake-savings.ts b/packages/opencode/src/altimate/tools/finops-snowflake-savings.ts new file mode 100644 index 0000000000..a748cd6c38 --- /dev/null +++ b/packages/opencode/src/altimate/tools/finops-snowflake-savings.ts @@ -0,0 +1,173 @@ +// altimate_change - new file +import z from "zod" +import { Tool } from "../../tool/tool" +import { Dispatcher } from "../native" +import type { SnowflakeSavingsFinding, SnowflakeSavingsReport, SnowflakeSavingsResult } from "../native/types" +import { dollars, oneLine, range } from "./finops-databricks-savings" + +/** One shape for every outcome, so a reader of the metadata never guesses which fields exist. */ +type SavingsMetadata = { + success: boolean + window?: { start: string; end: string } + warehouse?: string + ran_on?: string + warehouses_checked?: number + price?: SnowflakeSavingsResult["price"] + skipped?: { check: string; reason: string }[] + limited?: string[] + warehouses?: SnowflakeSavingsResult["warehouses"] + report?: SnowflakeSavingsReport + error?: string + /** True when the check did not run because `confirm` was not set. */ + needs_confirmation?: boolean +} + +const count = (value: unknown) => Number(value).toLocaleString("en-US") +const days = (value: unknown) => { + const n = Array.isArray(value) ? value.length : 0 + return `${n} day${n === 1 ? "" : "s"}` +} +const hours = (seconds: unknown) => (Number(seconds) / 3_600).toFixed(1) + +/** What a finding rests on, one short line each. */ +export function evidenceLines(finding: SnowflakeSavingsFinding): string[] { + const e = finding.evidence as Record & { kind: string } + switch (e.kind) { + case "high_idle_time": + return [ + `${Math.round(Number(e.idle_ratio) * 100)}% idle on ${days(e.flagged_days)} above 40%: ${hours(e.idle_seconds)} of ${hours(e.active_seconds)} active hours`, + `${Number(e.credits_used).toFixed(2)} credits on those days, at $${Number(e.price_per_credit).toFixed(2)} a credit`, + ] + case "downsize_warehouse": { + const signals = [ + e.short_period_percent == null ? null : `${Number(e.short_period_percent).toFixed(1)}% of its active periods lasted 80 seconds or less`, + e.sub_five_second_percent == null + ? null + : `${Number(e.sub_five_second_percent).toFixed(1)}% of ${count(e.query_count)} queries ran in 5 seconds or less`, + ].filter(Boolean) + return [ + `${e.current_size} could be ${e.recommended_size}: ${signals.join("; ")}`, + `The saving is a share of $${Number(e.cost_basis).toFixed(2)} over the window`, + ] + } + case "underutilized_cluster": + return [ + `Cluster ${e.cluster_number} of ${e.max_cluster_count} was barely needed on ${days(e.flagged_days)} (up to ${Number(e.highest_idle_percent).toFixed(0)}% idle)`, + `Lowering the maximum to ${e.recommended_max_cluster_count} cluster${e.recommended_max_cluster_count === 1 ? "" : "s"} may add some queueing; no dollar estimate`, + ] + case "frequent_restart": + return [ + `${count(e.bounces)} suspend-and-resume bounces inside a billed minute on ${days(e.flagged_days)}`, + `${count(e.double_billed_seconds)} seconds billed twice at ${e.credits_per_hour} credits an hour`, + ] + default: + return [] + } +} + +function priceLine(result: SnowflakeSavingsResult): string { + const price = result.price + if (!price) return "" + return price.source === "rate_sheet" + ? `$${price.per_credit.toFixed(2)} a credit from the account's rate sheet` + : `$${price.per_credit.toFixed(2)} a credit, list price (no dollar rate could be read from the account's rate sheet)` +} + +function headline(report: SnowflakeSavingsReport, partial = false): string { + const { monthly_min, monthly_max, resources } = report.total + if (resources > 0) return `${range(monthly_min, monthly_max)} a month on ${resources} warehouse${resources === 1 ? "" : "s"}` + if (report.findings.length === 0) return partial ? "no savings found in what could be checked" : "no savings found" + return `${report.findings.length} finding${report.findings.length === 1 ? "" : "s"}, no dollar estimate` +} + +export function formatSavings(result: SnowflakeSavingsResult): string { + const report = result.report + if (!report) return "No report." + const checked = result.warehouses_checked ?? 0 + const lines: string[] = [ + `Snowflake savings: ${checked} warehouse${checked === 1 ? "" : "s"} checked, the ${report.lookback_days} days to ${result.window.end}, at ${priceLine(result)}.`, + ] + // Warehouse names come from the account and can hold line breaks. + for (const one of result.skipped ?? []) lines.push(`Not checked: ${oneLine(one.check)} (${oneLine(one.reason)})`) + for (const what of result.limited ?? []) lines.push(`Only part of the ${what} could be read; the rest was not checked.`) + if (report.findings.length === 0) { + lines.push(result.skipped?.length || result.limited?.length ? "No savings found in what could be checked." : "No savings found.") + return lines.join("\n") + } + if (report.total.resources > 0) lines.push(`Total: ${headline(report)}, each counted once at its largest saving.`) + lines.push("") + for (const finding of report.findings) { + const amount = finding.monthly_max > 0 ? `${range(finding.monthly_min, finding.monthly_max)} a month` : "no dollar estimate" + lines.push(`${oneLine(finding.resource_name) || finding.resource_id} (warehouse) · ${finding.title} · ${amount} (${finding.severity})`) + for (const line of evidenceLines(finding)) lines.push(` ${line}`) + } + return lines.join("\n") +} + +export const FinopsSnowflakeSavingsTool = Tool.define("finops_snowflake_savings", { + description: + "Check a Snowflake account for savings with Altimate's rules: warehouses that sit idle, could run one size smaller, keep an unneeded extra cluster, or restart so often they are billed twice. Read-only: SHOW WAREHOUSES plus five queries on the connection's warehouse: four read up to eleven days of SNOWFLAKE.ACCOUNT_USAGE, to report on seven, and one reads the rate sheet. Snowflake resumes that warehouse if it is suspended and auto-resume is on. Returns each finding with its monthly dollar range and evidence. Ask the user before running it, and pass confirm: true once they agree; without it nothing runs.", + parameters: z.object({ + warehouse: z + .string() + .optional() + .describe("Snowflake connection name. Optional — omit to use the first configured Snowflake connection."), + confirm: z + .boolean() + .optional() + .describe( + "Set to true only after the user has agreed to run the check. It runs six queries on their Snowflake account; five use the connection's warehouse, which Snowflake resumes if it is suspended and auto-resume is on, and which bills while it runs; the check stops after 9 minutes, cancelling any query still running.", + ), + }), + async execute(args, ctx) { + if (args.confirm !== true) { + // Asking first is the expected first step, not a failed run. + return { + title: "Snowflake Savings: not run, needs the user's go-ahead", + metadata: { success: true, needs_confirmation: true } as SavingsMetadata, + output: + "Not run. The Snowflake savings check runs six queries that read up to eleven days of account usage, to report on seven; five use the connection's warehouse, " + + "which Snowflake resumes if it is suspended and auto-resume is on, and which bills while it runs. It stops after 9 minutes, cancelling any query still running. " + + "Ask the user whether to run it; if they agree, call this tool again with confirm: true.", + } + } + try { + const result = await Dispatcher.call( + "finops.snowflake_savings", + { warehouse: args.warehouse }, + { signal: ctx.abort, sessionID: ctx.sessionID }, + ) + if (!result.success || !result.report) { + const error = result.error ?? "Unknown error" + return { + title: "Snowflake Savings: FAILED", + metadata: { success: false, window: result.window, error } as SavingsMetadata, + output: `The Snowflake savings check failed: ${error}`, + } + } + return { + title: `Snowflake Savings: ${headline(result.report, Boolean(result.skipped?.length || result.limited?.length))}`, + metadata: { + success: true, + window: result.window, + warehouse: result.warehouse, + ran_on: result.ran_on, + warehouses_checked: result.warehouses_checked, + price: result.price, + skipped: result.skipped, + limited: result.limited, + warehouses: result.warehouses, + report: result.report, + } as SavingsMetadata, + output: formatSavings(result), + } + } catch (e) { + const msg = e instanceof Error ? e.message : String(e) + return { + title: "Snowflake Savings: ERROR", + metadata: { success: false, error: msg } as SavingsMetadata, + output: `The Snowflake savings check failed: ${msg}`, + } + } + }, +}) diff --git a/packages/opencode/src/tool/registry.ts b/packages/opencode/src/tool/registry.ts index db2e3b238e..40842ec265 100644 --- a/packages/opencode/src/tool/registry.ts +++ b/packages/opencode/src/tool/registry.ts @@ -107,6 +107,7 @@ import { FinopsAnalyzeCreditsTool } from "../altimate/tools/finops-analyze-credi import { FinopsExpensiveQueriesTool } from "../altimate/tools/finops-expensive-queries" import { FinopsWarehouseAdviceTool } from "../altimate/tools/finops-warehouse-advice" import { FinopsDatabricksSavingsTool } from "../altimate/tools/finops-databricks-savings" +import { FinopsSnowflakeSavingsTool } from "../altimate/tools/finops-snowflake-savings" import { FinopsUnusedResourcesTool } from "../altimate/tools/finops-unused-resources" import { FinopsRoleGrantsTool, @@ -492,6 +493,7 @@ export namespace ToolRegistry { FinopsExpensiveQueriesTool, FinopsWarehouseAdviceTool, FinopsDatabricksSavingsTool, + FinopsSnowflakeSavingsTool, FinopsUnusedResourcesTool, FinopsRoleGrantsTool, FinopsRoleHierarchyTool, diff --git a/packages/opencode/test/agent/analyst-data-explorer.test.ts b/packages/opencode/test/agent/analyst-data-explorer.test.ts index 8943948662..4d4a43f56c 100644 --- a/packages/opencode/test/agent/analyst-data-explorer.test.ts +++ b/packages/opencode/test/agent/analyst-data-explorer.test.ts @@ -424,6 +424,7 @@ const analystConfigEnabled = new Set([ "finops_query_history", "finops_role_grants", "finops_role_hierarchy", + "finops_snowflake_savings", "finops_unused_resources", "finops_user_roles", "finops_warehouse_advice", diff --git a/packages/opencode/test/altimate/assessment-zero-egress.test.ts b/packages/opencode/test/altimate/assessment-zero-egress.test.ts index ff9e0ac83f..f9ad659737 100644 --- a/packages/opencode/test/altimate/assessment-zero-egress.test.ts +++ b/packages/opencode/test/altimate/assessment-zero-egress.test.ts @@ -4,7 +4,8 @@ import { afterAll, beforeAll, describe, expect, test } from "bun:test" import http from "node:http" import type { AddressInfo } from "node:net" -import { mkdir, mkdtemp, readFile, readdir, rm, stat, writeFile } from "node:fs/promises" +import { createRequire } from "node:module" +import { mkdir, mkdtemp, readFile, readdir, realpath, rm, stat, symlink, writeFile } from "node:fs/promises" import { tmpdir } from "node:os" import path from "node:path" @@ -137,6 +138,57 @@ describe("assessment mode on the packaged CLI", () => { expect(JSON.parse(result.stdout).output).toContain("call this tool again with confirm: true") }, 120_000) + posixOnly("the Snowflake check: unconfirmed, with no warehouse, and with an unreachable one, no other host is contacted", async () => { + const snowflake = (args: string) => ["tool", "run", "finops_snowflake_savings", "--args", args, "--json", "--no-daemon"] + const unconfirmed = await run(snowflake("{}"), assessment) + expect(unconfirmed.hosts).toEqual([]) + expect(JSON.parse(unconfirmed.stdout).output).toContain("call this tool again with confirm: true") + + const noWarehouse = await run(snowflake('{"confirm":true}'), assessment) + expect(noWarehouse.hosts).toEqual([]) + expect(JSON.parse(noWarehouse.stdout).status).toBe("error") + + const account = "egress-test.invalid" + const unreachable = await run(snowflake('{"confirm":true}'), assessment, async (_home, project) => { + await mkdir(path.join(project, ".altimate-code"), { recursive: true }) + await writeFile( + path.join(project, ".altimate-code", "connections.json"), + JSON.stringify({ sf: { type: "snowflake", account, user: "u", password: "unused", warehouse: "W" } }), + ) + }) + expect(unreachable.hosts.filter((host) => !host.includes(account))).toEqual([]) + expect(JSON.parse(unreachable.stdout).output).not.toContain("none are set up") + }, 360_000) + + posixOnly("with the Snowflake driver installed, assessment mode keeps it off the cloud metadata services", async () => { + const account = "egress-test" + // The connection test reaches the driver whether or not the installed altimate-core has the savings rules. + const args = ["tool", "run", "warehouse_test", "--args", '{"name":"sf"}', "--json", "--no-daemon"] + // The driver the packaged CLI would install, linked in with its dependencies. + const sdk = createRequire(path.join(root, "../drivers/package.json")).resolve("snowflake-sdk/package.json") + const dependencies = path.dirname(path.dirname(await realpath(sdk))) + const setup = async (home: string, project: string) => { + const drivers = path.join(home, ".local/share/altimate-code/drivers") + await mkdir(drivers, { recursive: true }) + await writeFile(path.join(drivers, "package.json"), JSON.stringify({ name: "drivers", private: true, dependencies: { "snowflake-sdk": "*" } })) + await symlink(dependencies, path.join(drivers, "node_modules")) + await mkdir(path.join(project, ".altimate-code"), { recursive: true }) + await writeFile( + path.join(project, ".altimate-code", "connections.json"), + JSON.stringify({ sf: { type: "snowflake", account, user: "u", password: "unused", warehouse: "W" } }), + ) + } + const metadata = (hosts: string[]) => hosts.filter((host) => /169\.254\.169\.254|metadata\.google\.internal|sts\.|login\.microsoftonline/.test(host)) + + // Control: without the mode the SDK does probe, so the proxy would see it. + const control = await run(args, { ALTIMATE_TELEMETRY_DISABLED: "true" }, setup) + expect(metadata(control.hosts).length).toBeGreaterThan(0) + + const result = await run(args, assessment, setup) + expect(result.hosts.some((host) => host.includes(account))).toBe(true) + expect(result.hosts.filter((host) => !host.includes(account))).toEqual([]) + }, 360_000) + posixOnly("startup path (`tool list`): no host is contacted", async () => { const result = await run(["tool", "list"], assessment) expect(result.code).toBe(0) diff --git a/packages/opencode/test/altimate/finops-databricks-savings.test.ts b/packages/opencode/test/altimate/finops-databricks-savings.test.ts index 493672f1fe..bb39f365fa 100644 --- a/packages/opencode/test/altimate/finops-databricks-savings.test.ts +++ b/packages/opencode/test/altimate/finops-databricks-savings.test.ts @@ -122,6 +122,13 @@ describe("finops_databricks_savings tool", () => { expect(result.metadata.window).toEqual({ start: "2026-11-05", end: "2026-11-12" }) }) + test("the number of workspaces checked reaches the metadata", async () => { + Dispatcher.register("finops.databricks_savings" as any, async () => ({ ...REPORT, workspaces_checked: 3 })) + const tool = await initTool(FinopsDatabricksSavingsTool) + const result = await tool.execute({ confirm: true }, stubCtx()) + expect(result.metadata.workspaces_checked).toBe(3) + }) + test("without the user's go-ahead nothing runs: the agent is told to ask first", async () => { let ran = 0 Dispatcher.register("finops.databricks_savings" as any, async () => { @@ -564,6 +571,24 @@ describe("databricks savings handler over a stubbed connection", () => { ]) }) + test("every workspace a checked row came from is counted, with or without a finding or a SQL warehouse", async () => { + // Workspace 111 has the SQL warehouse; 333 has only a cluster and 222 only a job, and nothing is flagged. + const CLUSTER_ROW = { workspace_id: "333", cluster_id: "c9", cluster_name: "adhoc", auto_termination_minutes: 30, total_node_minutes: 600, active_util_days: 3, total_worker_minutes: 600, idle_node_minutes: 60, total_cost: 12 } + const rows = (...all: Record[]) => ({ columns: Object.keys(all[0]!), rows: all.map((one) => Object.values(one)), truncated: false }) + const spy = connect((sql) => { + if (sql === DATABRICKS_WAREHOUSE_FACTS_SQL) return table(WAREHOUSE_ROW) + if (sql === DATABRICKS_CLUSTER_FACTS_SQL) return rows(CLUSTER_ROW, { ...CLUSTER_ROW, cluster_id: "c10", workspace_id: 333n }) + // A second task of the same workspace, and a row with no workspace, add nothing. + return rows(JOB_ROW, { ...JOB_ROW, task_key: "other" }, { ...JOB_ROW, job_id: "j2", workspace_id: null }) + }) + const result = await databricksSavings({ warehouse: "dbx", now_ms: Date.parse("2026-11-18T10:00:00Z") }, rules) + spy.mockRestore() + expect(result.success).toBe(true) + expect(result.report?.findings).toEqual([]) + expect([result.warehouses_checked, result.clusters_checked, result.jobs_checked]).toEqual([1, 2, 2]) + expect(result.workspaces_checked).toBe(3) + }) + test("connecting counts toward the limit: a connection that never comes up ends the check in time", async () => { // A 30 ms session budget stands in for the last moments of the 9 minutes. const budget = spyOn(RunBudget, "queryDeadline").mockReturnValue(Date.now() + 30) diff --git a/packages/opencode/test/altimate/finops-snowflake-savings.test.ts b/packages/opencode/test/altimate/finops-snowflake-savings.test.ts new file mode 100644 index 0000000000..78ef11bf21 --- /dev/null +++ b/packages/opencode/test/altimate/finops-snowflake-savings.test.ts @@ -0,0 +1,781 @@ +import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test" +import { initTool } from "./tool-fixture" +import * as Dispatcher from "../../src/altimate/native/dispatcher" +import * as Registry from "../../src/altimate/native/connections/registry" +import { + LIST_PRICE_PER_CREDIT, + ROW_LIMIT, + SAVINGS_RUN_LIMIT_MS, + SHOW_ROW_LIMIT, + SNOWFLAKE_METERING_FACTS_SQL, + SNOWFLAKE_PERIOD_FACTS_SQL, + SNOWFLAKE_QUERY_FACTS_SQL, + SNOWFLAKE_RATE_SQL, + SNOWFLAKE_RESTART_FACTS_SQL, + SNOWFLAKE_WAREHOUSES_SQL, + buildFacts, + creditPrice, + rulesEngine, + snowflakeSavings, +} from "../../src/altimate/native/finops/snowflake-savings" +import { FinopsSnowflakeSavingsTool, formatSavings } from "../../src/altimate/tools/finops-snowflake-savings" +import type { SnowflakeSavingsResult } from "../../src/altimate/native/types" +import { RunBudget } from "../../src/altimate/run-budget" + +function stubCtx(): any { + return { sessionID: "test", messageID: "test", agent: "test", abort: new AbortController().signal, metadata: () => {}, ask: async () => {} } +} + +const finding = (check_id: string, evidence: Record, monthly_min: number, monthly_max: number) => ({ + check_id, + title: { high_idle_time: "High Idle Time", downsize_warehouse: "Downsize Warehouse", underutilized_cluster: "Underutilized Cluster", frequent_restart_overcharge: "Frequent Restart Overcharge" }[check_id], + resource_type: "sql_warehouse", + resource_id: "7", + resource_name: "BI_WH", + workspace_id: null, + severity: "medium", + daily_min: monthly_min / 30, + daily_max: monthly_max / 30, + monthly_min, + monthly_max, + evidence, +}) + +const REPORT = { + success: true, + window: { start: "2026-11-11", end: "2026-11-18" }, + warehouse: "sf", + ran_on: "BI_WH", + warehouses_checked: 3, + price: { per_credit: 3, source: "rate_sheet", as_of: "2026-11-17" }, + skipped: [], + limited: [], + warehouses: [{ id: "7", name: "BI_WH", type: "STANDARD", size: "Large", window_cost: 42 }], + report: { + lookback_days: 7, + price_per_credit: 3, + findings: [ + finding("downsize_warehouse", { kind: "downsize_warehouse", current_size: "Large", recommended_size: "Medium", short_period_percent: 95, sub_five_second_percent: 91.5, query_count: 1200, cost_basis: 42, price_per_credit: 3 }, 54, 90), + finding("high_idle_time", { kind: "high_idle_time", idle_ratio: 0.6, idle_seconds: 7200, active_seconds: 12000, flagged_days: ["2026-11-11", "2026-11-12"], credits_used: 10, price_per_credit: 3, wasted_cost: 18 }, 54, 77.14), + finding("frequent_restart_overcharge", { kind: "frequent_restart", bounces: 12, double_billed_seconds: 300, credits_per_hour: 4, flagged_days: ["2026-11-11"], price_per_credit: 3 }, 4.29, 6.43), + finding("underutilized_cluster", { kind: "underutilized_cluster", cluster_number: 2, max_cluster_count: 2, recommended_max_cluster_count: 1, highest_idle_percent: 70, flagged_days: ["2026-11-13"] }, 0, 0), + ], + total: { monthly_min: 54, monthly_max: 90, resources: 1 }, + what_if: [], + }, +} as unknown as SnowflakeSavingsResult + +describe("finops_snowflake_savings tool", () => { + beforeEach(() => Dispatcher.reset()) + + test("gives Claude the total, each finding with its evidence, and the whole report as metadata", async () => { + Dispatcher.register("finops.snowflake_savings" as any, async () => REPORT) + const tool = await initTool(FinopsSnowflakeSavingsTool) + const result = await tool.execute({ warehouse: "sf", confirm: true }, stubCtx()) + expect(result.title).toBe("Snowflake Savings: $54.00–$90.00 a month on 1 warehouse") + expect(result.metadata.report).toEqual(REPORT.report) + expect(result.metadata.price).toEqual(REPORT.price) + expect(result.output).toContain("3 warehouses checked, the 7 days to 2026-11-18, at $3.00 a credit from the account's rate sheet") + expect(result.output).toContain("BI_WH (warehouse) · Downsize Warehouse · $54.00–$90.00 a month (medium)") + expect(result.output).toContain("Large could be Medium: 95.0% of its active periods lasted 80 seconds or less; 91.5% of 1,200 queries ran in 5 seconds or less") + expect(result.output).toContain("60% idle on 2 days above 40%: 2.0 of 3.3 active hours") + expect(result.output).toContain("12 suspend-and-resume bounces inside a billed minute on 1 day") + expect(result.output).toContain("Underutilized Cluster · no dollar estimate") + expect(result.output).toContain("Lowering the maximum to 1 cluster may add some queueing") + }) + + test("without the user's go-ahead nothing runs: the agent is told to ask first", async () => { + let ran = false + Dispatcher.register("finops.snowflake_savings" as any, async () => { + ran = true + return REPORT + }) + const tool = await initTool(FinopsSnowflakeSavingsTool) + for (const args of [{}, { confirm: false }, { warehouse: "sf" }]) { + const result = await tool.execute(args, stubCtx()) + expect(result.title).toBe("Snowflake Savings: not run, needs the user's go-ahead") + expect(result.metadata).toMatchObject({ success: true, needs_confirmation: true }) + expect(result.metadata.report).toBeUndefined() + expect(result.output).toContain("Snowflake resumes if it is suspended") + expect(result.output).toContain("It stops after 9 minutes, cancelling any query still running.") + expect(result.output).toContain("Ask the user whether to run it; if they agree, call this tool again with confirm: true.") + } + expect(ran).toBe(false) + expect(tool.description).toEndWith("Ask the user before running it, and pass confirm: true once they agree; without it nothing runs.") + }) + + test("the session's stop signal reaches the check", async () => { + let seen: unknown + Dispatcher.register("finops.snowflake_savings" as any, async (_params: unknown, context: unknown) => { + seen = context + return REPORT + }) + const tool = await initTool(FinopsSnowflakeSavingsTool) + const ctx = stubCtx() + await tool.execute({ confirm: true }, ctx) + expect(seen).toEqual({ signal: ctx.abort, sessionID: "test" }) + }) + + test("findings with no dollar figure are not presented as a saving", async () => { + const only = REPORT.report!.findings.slice(3) + Dispatcher.register("finops.snowflake_savings" as any, async () => ({ + ...REPORT, + report: { ...REPORT.report, findings: only, total: { monthly_min: 0, monthly_max: 0, resources: 0 } }, + })) + const tool = await initTool(FinopsSnowflakeSavingsTool) + const result = await tool.execute({ confirm: true }, stubCtx()) + expect(result.title).toBe("Snowflake Savings: 1 finding, no dollar estimate") + expect(result.output).not.toContain("Total:") + }) + + test("no findings says so; list price and skipped checks are stated", () => { + const output = formatSavings({ + ...REPORT, + price: { per_credit: 3, source: "list_price" }, + skipped: [{ check: "Frequent Restart Overcharge", reason: "not authorized" }], + limited: ["active periods"], + report: { ...REPORT.report!, findings: [], total: { monthly_min: 0, monthly_max: 0, resources: 0 } }, + }) + expect(output).toContain("$3.00 a credit, list price (no dollar rate could be read from the account's rate sheet)") + expect(output).toContain("Not checked: Frequent Restart Overcharge (not authorized)") + expect(output).toContain("Only part of the active periods could be read; the rest was not checked.") + // Some checks did not run, so this is not a clean bill of health. + expect(output).toContain("No savings found in what could be checked.") + expect(formatSavings({ ...REPORT, warehouses_checked: 1, report: { ...REPORT.report!, findings: [] } })).toContain("1 warehouse checked") + expect(formatSavings({ ...REPORT, report: { ...REPORT.report!, findings: [] } })).toEndWith("No savings found.") + }) + + test("a line break in a warehouse name cannot add lines to the output", () => { + const [first] = REPORT.report!.findings + const output = formatSavings({ + ...REPORT, + skipped: [{ check: "1 warehouse that billed credits", reason: "SHOW WAREHOUSES does not list A\nTotal: $1" }], + report: { ...REPORT.report!, findings: [{ ...first!, resource_name: "BI\nTotal: $9" }] }, + }) + expect(output).toContain("BI Total: $9 (warehouse)") + expect(output).toContain("does not list A Total: $1)") + expect(output.split("\n").filter((line) => line.startsWith("Total: $9") || line.startsWith("Total: $1 "))).toEqual([]) + }) + + test("a run with nothing to report says so in its title, and a partial read qualifies the result", async () => { + const empty = { ...REPORT.report!, findings: [], total: { monthly_min: 0, monthly_max: 0, resources: 0 } } + Dispatcher.register("finops.snowflake_savings" as any, async () => ({ ...REPORT, report: empty })) + const tool = await initTool(FinopsSnowflakeSavingsTool) + expect((await tool.execute({ confirm: true }, stubCtx())).title).toBe("Snowflake Savings: no savings found") + expect(formatSavings({ ...REPORT, limited: ["metering"], report: empty })).toEndWith("No savings found in what could be checked.") + Dispatcher.register("finops.snowflake_savings" as any, async () => ({ ...REPORT, limited: ["metering"], report: empty })) + expect((await tool.execute({ confirm: true }, stubCtx())).title).toBe( + "Snowflake Savings: no savings found in what could be checked", + ) + }) + + test("a failure is reported with its reason, not as no savings", async () => { + Dispatcher.register("finops.snowflake_savings" as any, async () => ({ + success: false, + window: REPORT.window, + error: "SQL access control error: Insufficient privileges", + })) + const tool = await initTool(FinopsSnowflakeSavingsTool) + const result = await tool.execute({ confirm: true }, stubCtx()) + expect(result.title).toBe("Snowflake Savings: FAILED") + expect(result.metadata.error).toContain("Insufficient privileges") + expect(result.output).not.toContain("No savings found") + }) +}) + +describe("the fact queries", () => { + const all = [SNOWFLAKE_METERING_FACTS_SQL, SNOWFLAKE_PERIOD_FACTS_SQL, SNOWFLAKE_RESTART_FACTS_SQL, SNOWFLAKE_QUERY_FACTS_SQL, SNOWFLAKE_RATE_SQL] + + test("each is one read-only statement whose only parameter is the window end", () => { + for (const sql of all) { + expect(sql.trim()).toMatch(/^(WITH|SELECT)\b/) + expect(sql).not.toMatch(/;\s*\S/) + expect(sql).not.toMatch(/\b(INSERT|UPDATE|DELETE|MERGE|CREATE|ALTER|DROP|CALL|GRANT)\b/) + expect(sql).toContain("?") + } + expect(SNOWFLAKE_WAREHOUSES_SQL).toBe("SHOW WAREHOUSES") + }) + + test("every timestamp is read as UTC, so a session time zone cannot move a day", () => { + for (const sql of all.slice(0, 4)) { + expect(sql).toContain("CONVERT_TIMEZONE('UTC'") + expect(sql).toContain("+0000") + } + }) + + test("dates are parsed and printed in a fixed format, whatever the session's settings", () => { + for (const sql of all) expect(sql).not.toMatch(/TO_TIMESTAMP_NTZ\(\?\)/) + for (const sql of all) { + for (const call of sql.match(/TO_VARCHAR\([^\n]*/g) ?? []) expect(call).toContain("'YYYY-MM-DD'") + } + }) + + test("the measured thresholds are the hosted rules'", () => { + expect(SNOWFLAKE_PERIOD_FACTS_SQL).toContain("COUNT_IF(active_seconds <= 80)") + expect(SNOWFLAKE_PERIOD_FACTS_SQL).toContain("DATEDIFF('millisecond', valid_from, valid_to) >= 1000") + expect(SNOWFLAKE_PERIOD_FACTS_SQL).toContain("DATEDIFF('second', valid_from, valid_to)") + expect(SNOWFLAKE_QUERY_FACTS_SQL).toContain("COUNT_IF(q.execution_ms <= 5000)") + expect(SNOWFLAKE_QUERY_FACTS_SQL).toContain("SUM(compute_credits) * 0.1") + }) + + test("the windows and filters are the ones the rules are defined on", () => { + // Seven days plus the hour the first idle window starts early; system warehouse 0 is left out. + expect(SNOWFLAKE_METERING_FACTS_SQL).toContain("DATEADD(hour, -169,") + expect(SNOWFLAKE_METERING_FACTS_SQL).toContain("WAREHOUSE_ID > 0") + expect(SNOWFLAKE_METERING_FACTS_SQL).toContain("TO_VARCHAR(DATEADD(hour, 1, hour_start)::DATE, 'YYYY-MM-DD'), NULL") + expect(SNOWFLAKE_PERIOD_FACTS_SQL).toContain("TO_VARCHAR(DATEADD(hour, 1, valid_from)::DATE, 'YYYY-MM-DD'), NULL") + expect(SNOWFLAKE_PERIOD_FACTS_SQL).toContain("EVENT_NAME IN ('RESUME_CLUSTER', 'SUSPEND_CLUSTER') AND EVENT_STATE = 'STARTED'") + expect(SNOWFLAKE_PERIOD_FACTS_SQL).toContain("q.cluster_number = p.cluster_number") + expect(SNOWFLAKE_QUERY_FACTS_SQL).toContain("q.et >= DATEADD(day, -7,") + expect(SNOWFLAKE_RATE_SQL).toContain("ACCOUNT_NAME = CURRENT_ACCOUNT_NAME()") + expect(SNOWFLAKE_RATE_SQL).toContain("SERVICE_TYPE = 'WAREHOUSE_METERING' AND USAGE_TYPE = 'compute'") + expect(SNOWFLAKE_RATE_SQL).toContain("AND EFFECTIVE_RATE > 0\nORDER BY DATE DESC, EFFECTIVE_RATE LIMIT 1") + }) + + test("a bounce is the full resume, suspend, resume sequence on distinct events", () => { + expect(SNOWFLAKE_RESTART_FACTS_SQL).toContain("SELECT DISTINCT") + expect(SNOWFLAKE_RESTART_FACTS_SQL).toContain( + "event_name = 'RESUME_WAREHOUSE' AND n1 = 'WAREHOUSE_CONSISTENT' AND n2 = 'SUSPEND_WAREHOUSE'", + ) + expect(SNOWFLAKE_RESTART_FACTS_SQL).toContain("n3 = 'WAREHOUSE_CONSISTENT' AND n4 = 'RESUME_WAREHOUSE'") + expect(SNOWFLAKE_RESTART_FACTS_SQL).toContain("TRUNC(DATEDIFF('millisecond', ts, t4) / 1000) AS gap_seconds") + expect(SNOWFLAKE_RESTART_FACTS_SQL).toContain("DATEDIFF('millisecond', ts, t4) < 60000") + expect(SNOWFLAKE_RESTART_FACTS_SQL).not.toContain("DATEDIFF('second'") + }) +}) + +describe("building the facts", () => { + const show = (over: Record = {}) => ({ name: "BI_WH", size: "Large", type: "STANDARD", min_cluster_count: 1, max_cluster_count: "2", ...over }) + + test("rows become one entry per listed warehouse, with database values coerced", () => { + const { facts, unlisted } = buildFacts({ + warehouses: [show(), show({ name: "IDLE_WH", size: "X-Small", max_cluster_count: 1 })], + metering: [ + { warehouse_id: 7, warehouse_name: "BI_WH", usage_day: "2026-11-11", idle_day: "2026-11-11", credits_used: "1.5" }, + { warehouse_id: 7, warehouse_name: "BI_WH", usage_day: "2026-11-11", idle_day: "2026-11-12", credits_used: 0.5 }, + { warehouse_id: 7, warehouse_name: "BI_WH", usage_day: null, idle_day: "2026-11-11", credits_used: 2 }, + ], + periods: [ + { warehouse_id: 7, warehouse_name: "BI_WH", cluster_number: 1, usage_day: "2026-11-11", idle_day: "2026-11-11", periods: 3, short_periods: "2", active_seconds: "100", idle_seconds: 40 }, + { warehouse_id: 7, warehouse_name: "BI_WH", cluster_number: 2, usage_day: "2026-11-11", idle_day: "2026-11-11", periods: 1, short_periods: 1, active_seconds: 50, idle_seconds: 45 }, + ], + restarts: [{ warehouse_id: 7, warehouse_name: "BI_WH", day: "2026-11-11", gap_seconds: 30, bounces: 4n }], + queries: [{ warehouse_id: 7, warehouse_name: "BI_WH", query_count: "10", sub_five_second_queries: 9, sub_five_second_credits: "0.25" }], + }) + expect(unlisted).toEqual([]) + expect(facts[0]).toEqual({ + warehouse_id: "7", + warehouse_name: "BI_WH", + warehouse_size: "Large", + warehouse_type: "STANDARD", + min_cluster_count: 1, + max_cluster_count: 2, + query_count: 10, + sub_five_second_queries: 9, + sub_five_second_credits: 0.25, + usage_days: [{ day: "2026-11-11", credits_used: 2, active_periods: 4, short_periods: 3 }], + idle_days: [ + { day: "2026-11-11", active_seconds: 150, idle_seconds: 85, credits_used: 3.5 }, + { day: "2026-11-12", active_seconds: 0, idle_seconds: 0, credits_used: 0.5 }, + ], + cluster_days: [ + { + day: "2026-11-11", + clusters: [ + { cluster_number: 1, idle_seconds: 40, busy_seconds: 60 }, + { cluster_number: 2, idle_seconds: 45, busy_seconds: 5 }, + ], + }, + ], + restart_days: [{ day: "2026-11-11", gaps: [{ gap_seconds: 30, bounces: 4 }] }], + }) + // A listed warehouse with no usage is still an entry, under its name. + expect(facts[1]).toMatchObject({ warehouse_id: "unused:IDLE_WH", warehouse_name: "IDLE_WH", usage_days: [], query_count: 0 }) + }) + + test("a billed day with no periods keeps its credits, and a day with periods but no billing has none", () => { + const { facts } = buildFacts({ + warehouses: [show()], + metering: [{ warehouse_id: 7, warehouse_name: "BI_WH", usage_day: "2026-11-11", idle_day: null, credits_used: 1 }], + periods: [{ warehouse_id: 7, warehouse_name: "BI_WH", cluster_number: 1, usage_day: "2026-11-12", idle_day: null, periods: 1, short_periods: 0, active_seconds: 9, idle_seconds: 1 }], + restarts: [], + queries: [], + }) + expect(facts[0]!.usage_days).toEqual([ + { day: "2026-11-11", credits_used: 1, active_periods: 0, short_periods: 0 }, + { day: "2026-11-12", credits_used: null, active_periods: 1, short_periods: 0 }, + ]) + }) + + test("a recreated warehouse keeps only its newest id's usage", () => { + const { facts } = buildFacts({ + warehouses: [show()], + metering: [ + { warehouse_id: 7, warehouse_name: "BI_WH", usage_day: "2026-11-11", idle_day: null, credits_used: 100 }, + { warehouse_id: 9, warehouse_name: "BI_WH", usage_day: "2026-11-12", idle_day: null, credits_used: 1 }, + ], + periods: [], + restarts: [], + queries: [], + }) + expect(facts[0]!.warehouse_id).toBe("9") + expect(facts[0]!.usage_days).toEqual([{ day: "2026-11-12", credits_used: 1, active_periods: 0, short_periods: 0 }]) + }) + + const meter = (id: number, name: string, day: string, credits = 1) => ({ warehouse_id: id, warehouse_name: name, usage_day: day, idle_day: null, credits_used: credits }) + const none = { periods: [], restarts: [], queries: [] } + + test("a renamed warehouse keeps its usage under its new name", () => { + const { facts, unlisted } = buildFacts({ + warehouses: [show({ name: "ETL_NEW" })], + metering: [meter(7, "ETL", "2026-11-11", 1), meter(7, "ETL_NEW", "2026-11-12", 2)], + ...none, + }) + expect(unlisted).toEqual([]) + expect(facts[0]).toMatchObject({ warehouse_id: "7", warehouse_name: "ETL_NEW" }) + expect(facts[0]!.usage_days.map((day) => day.credits_used)).toEqual([1, 2]) + }) + + test.each([ + ["the old name first", ["ETL", "ETL_NEW"]], + ["the new name first", ["ETL_NEW", "ETL"]], + ])("a warehouse renamed on its last day of usage stays under its listed name, %s", (_, names) => { + const { facts, unlisted } = buildFacts({ + warehouses: [show({ name: "ETL_NEW" })], + metering: names.map((name, at) => meter(7, name, "2026-11-12", at + 1)), + ...none, + }) + expect(unlisted).toEqual([]) + expect(facts[0]).toMatchObject({ warehouse_id: "7", warehouse_name: "ETL_NEW" }) + expect(facts[0]!.usage_days.map((day) => day.credits_used)).toEqual([3]) + }) + + test("a rename's query and restart rows are added, not read from one name or repeated", () => { + const { facts } = buildFacts({ + warehouses: [show({ name: "NEW" })], + metering: [meter(7, "AOLD", "2026-11-11"), meter(7, "NEW", "2026-11-12")], + periods: [], + restarts: [ + { warehouse_id: 7, warehouse_name: "AOLD", day: "2026-11-12", gap_seconds: 30, bounces: 1 }, + { warehouse_id: 7, warehouse_name: "NEW", day: "2026-11-12", gap_seconds: 30, bounces: 2 }, + ], + queries: [ + { warehouse_id: 7, warehouse_name: "AOLD", query_count: 10, sub_five_second_queries: 10, sub_five_second_credits: 1 }, + { warehouse_id: 7, warehouse_name: "NEW", query_count: 990, sub_five_second_queries: 0, sub_five_second_credits: 0.5 }, + ], + }) + expect(facts[0]).toMatchObject({ query_count: 1000, sub_five_second_queries: 10, sub_five_second_credits: 1.5 }) + expect(facts[0]!.restart_days).toEqual([{ day: "2026-11-12", gaps: [{ gap_seconds: 30, bounces: 3 }] }]) + }) + + test("two warehouses that swapped names keep their own usage and distinct ids", () => { + const { facts } = buildFacts({ + warehouses: [show({ name: "BLUE" }), show({ name: "GREEN" })], + metering: [meter(7, "BLUE", "2026-11-11", 1), meter(8, "GREEN", "2026-11-11", 2), meter(7, "GREEN", "2026-11-12", 3), meter(8, "BLUE", "2026-11-12", 4)], + ...none, + }) + expect(facts.map((one) => one.warehouse_id)).toEqual(["8", "7"]) + expect(facts[0]!.usage_days.map((day) => day.credits_used)).toEqual([2, 4]) + expect(facts[1]!.usage_days.map((day) => day.credits_used)).toEqual([1, 3]) + }) + + test("an unused warehouse named like another's id keeps a distinct id", () => { + const { facts } = buildFacts({ + warehouses: [show({ name: "7" }), show({ name: "ETL" })], + metering: [meter(7, "ETL", "2026-11-11")], + ...none, + }) + expect(facts.map((one) => one.warehouse_id)).toEqual(["unused:7", "7"]) + }) + + test("a new unused warehouse on a renamed one's old name gets none of its usage", () => { + const { facts } = buildFacts({ + warehouses: [show({ name: "ETL" }), show({ name: "ETL_OLD" })], + metering: [meter(7, "ETL", "2026-11-11"), meter(7, "ETL_OLD", "2026-11-12")], + ...none, + }) + expect(facts.map((one) => one.warehouse_id)).toEqual(["unused:ETL", "7"]) + expect(facts[0]!.usage_days).toEqual([]) + expect(facts[1]!.usage_days.length).toBe(2) + }) + + test("usage that ended before the listed warehouse was created belongs to its predecessor", () => { + const rows = { metering: [meter(100, "BI_WH", "2026-11-12")], ...none } + const recreated = buildFacts({ warehouses: [show({ created_on: new Date("2026-11-15T08:00:00Z") })], ...rows }) + expect(recreated.facts[0]).toMatchObject({ warehouse_id: "unused:BI_WH", usage_days: [] }) + expect(recreated.unlisted).toEqual([]) + const older = buildFacts({ warehouses: [show({ created_on: "2026-01-01T00:00:00Z" })], ...rows }) + expect(older.facts[0]!.warehouse_id).toBe("100") + // A creation time that cannot be read changes nothing. + expect(buildFacts({ warehouses: [show({ created_on: "not a time" })], ...rows }).facts[0]!.warehouse_id).toBe("100") + }) + + test("a warehouse that billed credits but cannot be listed is named", () => { + const { facts, unlisted } = buildFacts({ + warehouses: [show()], + metering: [{ warehouse_id: 3, warehouse_name: "SECRET_WH", usage_day: "2026-11-11", idle_day: null, credits_used: 1 }], + periods: [], + restarts: [], + queries: [], + }) + expect(unlisted).toEqual(["SECRET_WH"]) + expect(facts.map((one) => one.warehouse_name)).toEqual(["BI_WH"]) + }) +}) + +describe("the price of a credit", () => { + test("is the rate sheet's in dollars, otherwise list price", () => { + expect(creditPrice({ effective_rate: "2.40", currency: "USD", rate_date: "2026-11-17" })).toEqual({ per_credit: 2.4, source: "rate_sheet", as_of: "2026-11-17" }) + for (const rate of [undefined, { effective_rate: 2.4, currency: "EUR" }, { effective_rate: 0, currency: "USD" }]) { + expect(creditPrice(rate)).toEqual({ per_credit: LIST_PRICE_PER_CREDIT, source: "list_price" }) + } + }) +}) + +describe("snowflake savings handler over a stubbed connection", () => { + const NOW = Date.parse("2026-11-18T10:00:00Z") + const table = (rows: Record[], truncated = false) => ({ + columns: Object.keys(rows[0] ?? {}), + rows: rows.map((row) => Object.values(row)), + truncated, + }) + const SHOW = [{ name: "BI_WH", size: "Large", type: "STANDARD", min_cluster_count: 1, max_cluster_count: 1 }] + const METERING = [{ warehouse_id: 7, warehouse_name: "BI_WH", usage_day: "2026-11-11", idle_day: "2026-11-11", credits_used: 14 }] + const RATE = [{ effective_rate: 2.5, currency: "USD", rate_date: "2026-11-17" }] + const answers = (sql: string) => + sql === SNOWFLAKE_WAREHOUSES_SQL ? table(SHOW) : sql === SNOWFLAKE_METERING_FACTS_SQL ? table(METERING) : sql === SNOWFLAKE_RATE_SQL ? table(RATE) : table([]) + let calls: { sql: string; limit: number; binds: unknown[]; timeout?: number; clientTimeout?: number; signal?: AbortSignal }[] + let sent: any + + beforeEach(() => { + calls = [] + sent = undefined + Registry.setConfigs({ sf: { type: "snowflake", account: "a", user: "u", password: "p", warehouse: "BI_WH" } } as any) + }) + afterEach(() => Registry.reset()) + + const rules = (input: string) => { + sent = JSON.parse(input) + return { lookback_days: 7, price_per_credit: sent.price_per_credit, findings: [], total: { monthly_min: 0, monthly_max: 0, resources: 0 }, what_if: [] } + } + const connect = (answer: (sql: string) => unknown) => + spyOn(Registry, "get").mockResolvedValue({ + execute: async (sql: string, limit: number, binds: unknown[], options?: { cancelAfterMs?: number; queryTimeoutMs?: number; signal?: AbortSignal }) => { + calls.push({ sql, limit, binds, timeout: options?.cancelAfterMs, clientTimeout: options?.queryTimeoutMs, signal: options?.signal }) + return answer(sql) + }, + } as any) + + test("every query is bound to the window end and gets what is left of the 9 minutes; the facts and rate reach core", async () => { + const spy = connect(answers) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + expect(result.success).toBe(true) + expect(result.window).toEqual({ start: "2026-11-11", end: "2026-11-18" }) + expect(calls.map((one) => one.sql)).toEqual([ + SNOWFLAKE_WAREHOUSES_SQL, + SNOWFLAKE_RATE_SQL, + SNOWFLAKE_METERING_FACTS_SQL, + SNOWFLAKE_PERIOD_FACTS_SQL, + SNOWFLAKE_RESTART_FACTS_SQL, + SNOWFLAKE_QUERY_FACTS_SQL, + ]) + for (const one of calls) { + expect(one.binds).toEqual(Array(one.sql.split("?").length - 1).fill("2026-11-18")) + expect(one.timeout).toBeGreaterThan(0) + expect(one.timeout).toBeLessThanOrEqual(SAVINGS_RUN_LIMIT_MS) + expect(one.clientTimeout).toBeUndefined() + // SHOW takes a LIMIT of at most 10,000, and the driver asks for one row more than this. + expect(one.limit).toBe(one.sql === SNOWFLAKE_WAREHOUSES_SQL ? SHOW_ROW_LIMIT : ROW_LIMIT) + } + expect(SHOW_ROW_LIMIT + 1).toBeLessThanOrEqual(10_000) + expect(SAVINGS_RUN_LIMIT_MS).toBe(540_000) + expect(sent).toMatchObject({ lookback_days: 7, price_per_credit: 2.5 }) + expect(sent.warehouses[0]).toMatchObject({ warehouse_id: "7", warehouse_name: "BI_WH", warehouse_size: "Large" }) + expect(result.price).toEqual({ per_credit: 2.5, source: "rate_sheet", as_of: "2026-11-17" }) + expect(result.ran_on).toBe("BI_WH") + expect(result.warehouses).toEqual([{ id: "7", name: "BI_WH", type: "STANDARD", size: "Large", window_cost: 35 }]) + }) + + test("a rate sheet the role cannot read falls back to the stated list price and skips nothing", async () => { + const spy = connect((sql) => { + if (sql === SNOWFLAKE_RATE_SQL) throw new Error("Object does not exist or not authorized") + return answers(sql) + }) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + expect(result.success).toBe(true) + expect(sent.price_per_credit).toBe(LIST_PRICE_PER_CREDIT) + expect(result.price).toEqual({ per_credit: LIST_PRICE_PER_CREDIT, source: "list_price" }) + expect(result.skipped).toEqual([]) + }) + + test("a view the role cannot read skips only the checks that need it", async () => { + const spy = connect((sql) => { + if (sql === SNOWFLAKE_RESTART_FACTS_SQL) throw new Error("not authorized\nstack") + return answers(sql) + }) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + expect(result.success).toBe(true) + expect(result.skipped).toEqual([{ check: "Frequent Restart Overcharge", reason: "not authorized" }]) + }) + + test("when no usage query could run, one that may still be running is the one named", async () => { + const timeout = Object.assign(new Error("QUERY_TIMEOUT"), { code: "QUERY_TIMEOUT", cancelled: false }) + const spy = connect((sql) => { + if (sql === SNOWFLAKE_PERIOD_FACTS_SQL) throw timeout + if ([SNOWFLAKE_RESTART_FACTS_SQL, SNOWFLAKE_QUERY_FACTS_SQL].includes(sql)) throw new Error("not authorized") + return answers(sql) + }) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + expect(result.success).toBe(false) + expect(result.error).toContain("The active periods query ran past") + expect(result.error).toContain("may still be running on the warehouse") + }) + + test("when none of the three usage queries can run, the check fails instead of reporting no savings", async () => { + const spy = connect((sql) => { + if ([SNOWFLAKE_PERIOD_FACTS_SQL, SNOWFLAKE_RESTART_FACTS_SQL, SNOWFLAKE_QUERY_FACTS_SQL].includes(sql)) throw new Error("not authorized") + return answers(sql) + }) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + expect(result).toMatchObject({ success: false, error: "No check could run. not authorized" }) + expect(sent).toBeUndefined() + }) + + test("billing that cannot be read fails the check: nothing could be priced", async () => { + const spy = connect((sql) => { + if (sql === SNOWFLAKE_METERING_FACTS_SQL) throw new Error("Insufficient privileges") + return answers(sql) + }) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + expect(result).toMatchObject({ success: false, error: "Insufficient privileges" }) + expect(sent).toBeUndefined() + }) + + test("a role that can list no warehouse gets a plain failure before any usage query", async () => { + const spy = connect(() => table([])) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + expect(result.success).toBe(false) + expect(result.error).toContain("SHOW WAREHOUSES lists no warehouse") + expect(calls.length).toBe(1) + }) + + test("billed warehouses the role cannot list are named as not checked", async () => { + const spy = connect((sql) => + sql === SNOWFLAKE_METERING_FACTS_SQL + ? table([...METERING, { warehouse_id: 3, warehouse_name: "SECRET_WH", usage_day: "2026-11-11", idle_day: null, credits_used: 1 }]) + : answers(sql), + ) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + expect(result.skipped).toEqual([ + { check: "1 warehouse that billed credits", reason: "SHOW WAREHOUSES does not list SECRET_WH: dropped, renamed, or not visible to the connection's role" }, + ]) + }) + + test("a query past its limit fails with a plain reason, or skips an optional check", async () => { + const timeout = (cancelled: boolean) => Object.assign(new Error("QUERY_TIMEOUT: snowflake query exceeded"), { code: "QUERY_TIMEOUT", cancelled }) + let spy = connect((sql) => { + if (sql === SNOWFLAKE_METERING_FACTS_SQL) throw timeout(true) + return answers(sql) + }) + let result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + expect(result.error).toBe("The metering query ran past the check's 9-minute limit and was cancelled on the warehouse.") + + spy = connect((sql) => { + if (sql === SNOWFLAKE_QUERY_FACTS_SQL) throw timeout(false) + return answers(sql) + }) + result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + expect(result.success).toBe(true) + expect(result.skipped).toEqual([ + { check: "The short-query signal of Downsize Warehouse", reason: "The query history query ran past the check's 9-minute limit and may still be running on the warehouse." }, + ]) + }) + + test("each query gets only what the earlier ones left; with nothing left it is not started", async () => { + let now = 1_000_000 + const clock = spyOn(Date, "now").mockImplementation(() => now) + const spy = connect((sql) => { + // The metering query uses up two minutes, and the period query all the rest. + if (sql === SNOWFLAKE_METERING_FACTS_SQL) now += 120_000 + if (sql === SNOWFLAKE_PERIOD_FACTS_SQL) now += SAVINGS_RUN_LIMIT_MS + return answers(sql) + }) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + clock.mockRestore() + expect(calls.map((one) => one.timeout)).toEqual([ + SAVINGS_RUN_LIMIT_MS, + SAVINGS_RUN_LIMIT_MS, + SAVINGS_RUN_LIMIT_MS, + SAVINGS_RUN_LIMIT_MS - 120_000, + ]) + expect(result.success).toBe(true) + expect(result.skipped).toEqual([ + { check: "Frequent Restart Overcharge", reason: "The restart events query was not started: it would run past the check's 9-minute limit." }, + { check: "The short-query signal of Downsize Warehouse", reason: "The query history query was not started: it would run past the check's 9-minute limit." }, + ]) + }) + + test("reaching the limit before the required billing query fails the check", async () => { + let now = 1_000_000 + const clock = spyOn(Date, "now").mockImplementation(() => now) + const spy = connect((sql) => { + if (sql === SNOWFLAKE_RATE_SQL) now += SAVINGS_RUN_LIMIT_MS + return answers(sql) + }) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + clock.mockRestore() + expect(result).toMatchObject({ success: false, error: "The metering query was not started: it would run past the check's 9-minute limit." }) + }) + + test("stopping the session cancels the running query and starts no other", async () => { + const controller = new AbortController() + const spy = connect((sql) => { + if (sql === SNOWFLAKE_PERIOD_FACTS_SQL) { + controller.abort(new Error("session stopped")) + throw new Error("session stopped") + } + return answers(sql) + }) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules, { signal: controller.signal }) + spy.mockRestore() + expect(result).toMatchObject({ success: false, error: "session stopped" }) + expect(result.skipped).toBeUndefined() + expect(calls.at(-1)!.sql).toBe(SNOWFLAKE_PERIOD_FACTS_SQL) + expect(sent).toBeUndefined() + // Each query got the session's signal, so the driver cancels it on Snowflake. + for (const one of calls) expect(one.signal).toBe(controller.signal) + }) + + test("connecting counts toward the limit: a connection that never comes up ends the check in time", async () => { + // A 30 ms session budget stands in for the last moments of the 9 minutes. + const budget = spyOn(RunBudget, "queryDeadline").mockReturnValue(Date.now() + 30) + const get = spyOn(Registry, "get").mockReturnValue(new Promise(() => {}) as any) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules, { sessionID: "s1" }) + get.mockRestore() + budget.mockRestore() + expect(result).toMatchObject({ success: false, error: "Connecting to the warehouse took longer than the run's time budget." }) + }) + + test("stopping the session while connecting ends the check at once", async () => { + const controller = new AbortController() + const get = spyOn(Registry, "get").mockReturnValue(new Promise(() => {}) as any) + const pending = snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules, { signal: controller.signal }) + controller.abort(new Error("session stopped")) + const result = await pending + get.mockRestore() + expect(result).toMatchObject({ success: false, error: "session stopped" }) + }) + + test("a connection that fails after the session already stopped is not left unhandled", async () => { + const controller = new AbortController() + controller.abort(new Error("session stopped")) + const unhandled: unknown[] = [] + const note = (reason: unknown) => unhandled.push(reason) + process.on("unhandledRejection", note) + let fail!: (reason: Error) => void + const connecting = new Promise((_, reject) => (fail = reject)) + const get = spyOn(Registry, "get").mockReturnValue(connecting as any) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules, { signal: controller.signal }) + fail(new Error("connection refused")) + await new Promise((resolve) => setTimeout(resolve, 0)) + get.mockRestore() + process.off("unhandledRejection", note) + expect(result).toMatchObject({ success: false, error: "session stopped" }) + expect(unhandled).toEqual([]) + }) + + test("a rate query that may still be running is named, and list price is used", async () => { + const timeout = Object.assign(new Error("QUERY_TIMEOUT"), { code: "QUERY_TIMEOUT", cancelled: false }) + const spy = connect((sql) => { + if (sql === SNOWFLAKE_RATE_SQL) throw timeout + return answers(sql) + }) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + expect(result.price?.source).toBe("list_price") + expect(result.skipped).toEqual([ + { check: "The account's credit rate", reason: "The credit rate query ran past the check's 9-minute limit and may still be running on the warehouse." }, + ]) + }) + + test("a session time budget shorter than 9 minutes is the limit, and is named when it stops a query", async () => { + const budget = spyOn(RunBudget, "queryDeadline").mockReturnValue(Date.now() + 60_000) + const timeout = Object.assign(new Error("QUERY_TIMEOUT"), { code: "QUERY_TIMEOUT", cancelled: true }) + const spy = connect((sql) => { + if (sql === SNOWFLAKE_METERING_FACTS_SQL) throw timeout + return answers(sql) + }) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules, { sessionID: "s1" }) + spy.mockRestore() + budget.mockRestore() + for (const one of calls) expect(one.timeout).toBeLessThanOrEqual(60_000) + expect(result.error).toBe("The metering query ran past the run's time budget and was cancelled on the warehouse.") + }) + + test("a stop during the rate lookup is not mistaken for an unreadable rate sheet", async () => { + const controller = new AbortController() + const spy = connect((sql) => { + if (sql === SNOWFLAKE_RATE_SQL) { + controller.abort(new Error("session stopped")) + throw new Error("session stopped") + } + return answers(sql) + }) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules, { signal: controller.signal }) + spy.mockRestore() + expect(result).toMatchObject({ success: false, error: "session stopped" }) + expect(calls.at(-1)!.sql).toBe(SNOWFLAKE_RATE_SQL) + }) + + test("a query that fills the row limit is named", async () => { + const spy = connect((sql) => (sql === SNOWFLAKE_PERIOD_FACTS_SQL ? table([], true) : answers(sql))) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, rules) + spy.mockRestore() + expect(result.limited).toEqual(["active periods"]) + }) + + test("with no Snowflake connection it says how to add one", async () => { + Registry.setConfigs({}) + const result = await snowflakeSavings({}, rules) + expect(result.success).toBe(false) + expect(result.error).toContain("warehouse_add") + }) + + test("a core without the rules is refused before any query", async () => { + const spy = connect(answers) + const result = await snowflakeSavings({ warehouse: "sf", now_ms: NOW }, null) + spy.mockRestore() + expect(result.error).toContain("no Snowflake savings rules") + expect(calls).toEqual([]) + }) + + test("in assessment mode the rules come with core's own telemetry switched off", () => { + const saved = process.env["ALTIMATE_ASSESSMENT_MODE"] + const calls: unknown[][] = [] + const lib = { snowflakeSavings: () => ({}), initSdk: (...args: unknown[]) => void calls.push(args) } + try { + process.env["ALTIMATE_ASSESSMENT_MODE"] = "true" + expect(rulesEngine(lib)).toBe(lib.snowflakeSavings as never) + expect(calls).toEqual([["", "", "", false, false]]) + expect(rulesEngine({ initSdk: lib.initSdk })).toBeNull() + delete process.env["ALTIMATE_ASSESSMENT_MODE"] + rulesEngine(lib) + expect(calls.length).toBe(1) + } finally { + if (saved === undefined) delete process.env["ALTIMATE_ASSESSMENT_MODE"] + else process.env["ALTIMATE_ASSESSMENT_MODE"] = saved + } + }) +})