Skip to content

Commit b915703

Browse files
anandgupta42claude
andcommitted
fix: [#827] address re-review — finops_warehouse_advice formatter + last 2 String(e)
Fourth pass on PR #828. Re-review found one MAJOR formatter bug missed by the previous formatter audit, plus the last two String(e) instances that the third pass left behind. 1. MAJOR — formatter field-name mismatch in finops-warehouse-advice Same class of bug as the one commit ed6f247 fixed for finops-query-history.ts and finops-expensive-queries.ts — the formatter read keys the handler never emits: - Performance summary: read `avg_execution_time`, `avg_queue_time`, `status`, `health`; handler emits `query_count`, `avg_time_sec`, `p95_time_sec`, `avg_bytes_scanned`, `total_credits`. - Load metrics: read `warehouse_size`, `avg_load`, `peak_load`, `utilization`; handler emits `avg_concurrency`, `avg_queue_load`, `peak_queue_load`, `sample_count`. Every value rendered as "-" regardless of input. Auto-pick from the prior commits expanded the user population hitting this formatter. Fix: rewrote both table headers and row loops to read the actual handler keys. Added inline comments pointing back at the previous instance of this bug pattern so the next reviewer doesn't have to discover it independently. 2. MINOR — null-guard on result.recommendations `result.recommendations.length` was read directly. If a future handler variance or partial success ever dropped the field, the tool throws a TypeError instead of returning a structured error. Coalesced to `[]`. Also defensive-defaulted warehouse_load and warehouse_performance for the same reason. 3. MINOR — last two String(e) call sites The third pass normalized role-access.ts but missed: - unused-resources.ts:234 - warehouse-advisor.ts:301 Both fixed in this commit. All five finops handlers now use the same `e instanceof Error ? e.message : String(e)` pattern, so driver errors no longer carry "Error:" prefixes inconsistently. Validation - `tsgo --noEmit`: pass. - `bun test test/altimate/finops-*.test.ts test/altimate/schema-finops-dbt.test.ts`: 95 pass / 0 fail / 67 skip (credential-gated). - Marker check: pass. Reviewer note - GPT-5.4 and Kimi K2.5 second-opinions did not produce output this round either — kilo CLI was preempted by other concurrent jobs on the same OpenRouter quota. Single-reviewer Claude analysis. Deferred to follow-up - Tool-level integration test for each formatter (would have caught all three formatter bugs at once — tracked as a separate quality bar enhancement). - warehouse_filter param silently ignored for BigQuery/Databricks (description already documents Snowflake-only; tracking as NIT). Closes review feedback for PR #828 (fourth pass). Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 76ce370 commit b915703

3 files changed

Lines changed: 41 additions & 23 deletions

File tree

‎packages/opencode/src/altimate/native/finops/unused-resources.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ export async function findUnusedResources(params: UnusedResourcesParams): Promis
231231
idle_warehouses: [],
232232
summary: {},
233233
days_analyzed: days,
234-
error: String(e),
234+
error: e instanceof Error ? e.message : String(e),
235235
}
236236
}
237237
}

‎packages/opencode/src/altimate/native/finops/warehouse-advisor.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ export async function adviseWarehouse(params: WarehouseAdvisorParams): Promise<W
298298
warehouse_performance: [],
299299
recommendations: [],
300300
days_analyzed: days,
301-
error: String(e),
301+
error: e instanceof Error ? e.message : String(e),
302302
}
303303
}
304304
}

‎packages/opencode/src/altimate/tools/finops-warehouse-advice.ts‎

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,46 @@ function formatWarehouseAdvice(
1818
lines.push("".padEnd(50, "="))
1919

2020
if (perf.length > 0) {
21-
lines.push("Warehouse | Avg Exec Time | Avg Queue Time | Queries | Status")
22-
lines.push("----------|---------------|----------------|---------|-------")
21+
// Field names match what `warehouse-advisor.ts` actually returns from
22+
// the SIZING SQL: warehouse_name, query_count, avg_time_sec,
23+
// p95_time_sec, avg_bytes_scanned, total_credits. The previous version
24+
// of this formatter read `avg_execution_time` / `avg_queue_time` /
25+
// `status` / `health` — none of which exist in the result rows, so
26+
// every value rendered as `-` regardless of input. (Same class of
27+
// bug previously fixed for finops-query-history.ts and
28+
// finops-expensive-queries.ts.)
29+
lines.push("Warehouse | Queries | Avg Time | p95 Time | Credits")
30+
lines.push("----------|---------|----------|----------|--------")
2331
for (const p of perf) {
2432
const r = p as Record<string, unknown>
2533
const name = String(r.warehouse_name ?? r.name ?? "unknown")
26-
const avgExec = r.avg_execution_time !== undefined ? `${Number(r.avg_execution_time).toFixed(2)}s` : "-"
27-
const avgQueue = r.avg_queue_time !== undefined ? `${Number(r.avg_queue_time).toFixed(2)}s` : "-"
28-
const queries = r.query_count ?? r.total_queries ?? "-"
29-
const statusVal = r.status ?? r.health ?? "-"
30-
lines.push(`${name} | ${avgExec} | ${avgQueue} | ${queries} | ${statusVal}`)
34+
const queries = r.query_count ?? "-"
35+
const avgTime = r.avg_time_sec !== undefined ? `${Number(r.avg_time_sec).toFixed(2)}s` : "-"
36+
const p95Time = r.p95_time_sec !== undefined ? `${Number(r.p95_time_sec).toFixed(2)}s` : "-"
37+
const credits = r.total_credits !== undefined ? Number(r.total_credits).toFixed(2) : "-"
38+
lines.push(`${name} | ${queries} | ${avgTime} | ${p95Time} | ${credits}`)
3139
}
3240
lines.push("")
3341
}
3442

3543
if (load.length > 0) {
36-
lines.push("Warehouse Load Metrics")
37-
lines.push("".padEnd(50, "-"))
38-
lines.push("Warehouse | Size | Avg Load | Peak Load | Utilization")
39-
lines.push("----------|------|----------|-----------|------------")
44+
// LOAD SQL returns: warehouse_name, avg_concurrency, avg_queue_load,
45+
// peak_queue_load, sample_count. The previous version read
46+
// `warehouse_size` / `avg_load` / `peak_load` / `utilization` — none of
47+
// which are in the result rows. (Same bug pattern.) `warehouse_size` is
48+
// populated separately in `recommendations[*].current_size` from the
49+
// SHOW WAREHOUSES probe; surface that in the Recommendations section
50+
// below, not here.
51+
lines.push("Warehouse | Avg Concurrency | Avg Queue | Peak Queue | Samples")
52+
lines.push("----------|-----------------|-----------|------------|--------")
4053
for (const l of load) {
4154
const r = l as Record<string, unknown>
4255
const name = String(r.warehouse_name ?? r.name ?? "unknown")
43-
const size = String(r.warehouse_size ?? r.size ?? "-")
44-
const avgLoad = r.avg_load !== undefined ? Number(r.avg_load).toFixed(1) : "-"
45-
const peakLoad = r.peak_load !== undefined ? Number(r.peak_load).toFixed(1) : "-"
46-
const util = r.utilization !== undefined ? `${Number(r.utilization).toFixed(1)}%` : "-"
47-
lines.push(`${name} | ${size} | ${avgLoad} | ${peakLoad} | ${util}`)
56+
const avgConc = r.avg_concurrency !== undefined ? Number(r.avg_concurrency).toFixed(2) : "-"
57+
const avgQ = r.avg_queue_load !== undefined ? Number(r.avg_queue_load).toFixed(2) : "-"
58+
const peakQ = r.peak_queue_load !== undefined ? Number(r.peak_queue_load).toFixed(2) : "-"
59+
const samples = r.sample_count ?? "-"
60+
lines.push(`${name} | ${avgConc} | ${avgQ} | ${peakQ} | ${samples}`)
4861
}
4962
lines.push("")
5063
}
@@ -96,13 +109,18 @@ export const FinopsWarehouseAdviceTool = Tool.define("finops_warehouse_advice",
96109
}
97110
}
98111

112+
// Defensive null-coalesce in case the handler ever returns a partial
113+
// shape (transient dispatcher variance, future schema change). Without
114+
// this, `.length` on an undefined would throw a TypeError and the user
115+
// would see a JS stack instead of a structured tool error.
116+
const recs = (result.recommendations as unknown[] | undefined) ?? []
99117
return {
100-
title: `Warehouse Advice: ${result.recommendations.length} recommendation${result.recommendations.length !== 1 ? "s" : ""}`,
101-
metadata: { success: true, recommendation_count: result.recommendations.length },
118+
title: `Warehouse Advice: ${recs.length} recommendation${recs.length !== 1 ? "s" : ""}`,
119+
metadata: { success: true, recommendation_count: recs.length },
102120
output: formatWarehouseAdvice(
103-
result.recommendations as unknown[],
104-
result.warehouse_load as unknown[],
105-
result.warehouse_performance as unknown[],
121+
recs,
122+
(result.warehouse_load as unknown[] | undefined) ?? [],
123+
(result.warehouse_performance as unknown[] | undefined) ?? [],
106124
),
107125
}
108126
} catch (e) {

0 commit comments

Comments
 (0)