Skip to content

finops_query_history fails on BigQuery — invalid column names in SQL template #738

@anandgupta42

Description

@anandgupta42

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:

  1. error_message (line 72) — does not exist at the top level. BQ uses error_result STRUCT with {reason, location, message, debug_info} sub-fields.
  2. 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.
  3. stateexecution_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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions