Description
The finops_query_history tool errors 100% of the time against BigQuery warehouses. The BQ SQL template in packages/opencode/src/altimate/native/finops/query-history.ts selects columns that do not exist in INFORMATION_SCHEMA.JOBS, so the query is rejected by BQ before any rows are returned.
Evidence from telemetry
Today (2026-04-23 PDT), finops_query_history had 75/75 (100%) failures concentrated in a single session (ses_24619658…) over a 2.5-hour retry loop. All failures share the same BQ parse error:
Error: Unrecognized name: error_message at [11:5]
The [11:5] position maps exactly to line 11, column 5 of the BIGQUERY_HISTORY_SQL template.
Root cause
Three broken references in BIGQUERY_HISTORY_SQL:
error_message (line 72) — does not exist at the top level. BQ uses error_result STRUCT with {reason, location, message, debug_info} sub-fields.
total_rows (line 77) — does not exist in INFORMATION_SCHEMA.JOBS. It's a column in the PARTITIONS view. Fixing error_message alone would expose this as the next parse error.
state → execution_status (line 70) — BQ's state returns 'DONE', not 'SUCCESS', so execution_status !== 'SUCCESS' in the summary loop (query-history.ts:228) counts every completed job as an error.
Fix
In BIGQUERY_HISTORY_SQL:
- Replace
NULL as error_code + error_message, with error_result.reason AS error_code, + error_result.message AS error_message,
- Replace
total_rows as rows_produced with NULL AS rows_produced
- Replace
state as execution_status with a CASE WHEN error_result IS NULL THEN 'SUCCESS' ELSE 'FAILED' END AS execution_status so the summary error-count logic works consistently across warehouses.
Test gap
packages/opencode/test/altimate/ has finops-snowflake-e2e.test.ts and finops-databricks-e2e.test.ts but no BigQuery E2E test — that's why the typo shipped. The unit-level test in schema-finops-dbt.test.ts only asserts INFORMATION_SCHEMA.JOBS appears in the SQL, not that the referenced columns exist. Need column-level assertions.
Out of scope for this fix (follow-ups)
`region-US.INFORMATION_SCHEMA.JOBS` is hardcoded — non-US projects can't use this tool at all. Should thread a region from the connection config.
- Retry-loop guard: the agent re-ran the failing tool 75 times over 2.5 hours without backing off. Worth a separate issue.
Description
The
finops_query_historytool errors 100% of the time against BigQuery warehouses. The BQ SQL template inpackages/opencode/src/altimate/native/finops/query-history.tsselects columns that do not exist inINFORMATION_SCHEMA.JOBS, so the query is rejected by BQ before any rows are returned.Evidence from telemetry
Today (2026-04-23 PDT),
finops_query_historyhad 75/75 (100%) failures concentrated in a single session (ses_24619658…) over a 2.5-hour retry loop. All failures share the same BQ parse error:Error: Unrecognized name: error_message at [11:5]The
[11:5]position maps exactly to line 11, column 5 of theBIGQUERY_HISTORY_SQLtemplate.Root cause
Three broken references in
BIGQUERY_HISTORY_SQL:error_message(line 72) — does not exist at the top level. BQ useserror_resultSTRUCT with{reason, location, message, debug_info}sub-fields.total_rows(line 77) — does not exist inINFORMATION_SCHEMA.JOBS. It's a column in thePARTITIONSview. Fixingerror_messagealone would expose this as the next parse error.state→execution_status(line 70) — BQ'sstatereturns'DONE', not'SUCCESS', soexecution_status !== 'SUCCESS'in the summary loop (query-history.ts:228) counts every completed job as an error.Fix
In
BIGQUERY_HISTORY_SQL:NULL as error_code+error_message,witherror_result.reason AS error_code,+error_result.message AS error_message,total_rows as rows_producedwithNULL AS rows_producedstate as execution_statuswith aCASE WHEN error_result IS NULL THEN 'SUCCESS' ELSE 'FAILED' END AS execution_statusso the summary error-count logic works consistently across warehouses.Test gap
packages/opencode/test/altimate/hasfinops-snowflake-e2e.test.tsandfinops-databricks-e2e.test.tsbut no BigQuery E2E test — that's why the typo shipped. The unit-level test inschema-finops-dbt.test.tsonly assertsINFORMATION_SCHEMA.JOBSappears in the SQL, not that the referenced columns exist. Need column-level assertions.Out of scope for this fix (follow-ups)
`region-US.INFORMATION_SCHEMA.JOBS`is hardcoded — non-US projects can't use this tool at all. Should thread a region from the connection config.