@@ -31,37 +31,40 @@ export type FinopsOperation =
3131 * handler itself reads (`native/finops/*.ts`), so the model writes the query the
3232 * tool would have run. Keyed by canonical local driver type, the key
3333 * `servedInventory` reports; an operation/type pair with no entry gets the tool
34- * name alone rather than a table that holds something else. */
34+ * name alone rather than a table that holds something else. BigQuery's
35+ * INFORMATION_SCHEMA views are only reachable region-qualified (`bq-utils.ts`), and
36+ * the snapshot does not carry the integration's region, so the placeholder is
37+ * spelled out rather than a bare name that would fail again. */
3538const SOURCE : Readonly < Record < FinopsOperation , Readonly < Record < string , string > > > > = {
3639 query_history : {
3740 snowflake : "`SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY`" ,
38- bigquery : "`INFORMATION_SCHEMA.JOBS`" ,
41+ bigquery : "`region-<location>. INFORMATION_SCHEMA.JOBS`" ,
3942 databricks : "`system.query.history`" ,
4043 postgres : "`pg_stat_statements`" ,
4144 } ,
4245 analyze_credits : {
4346 snowflake : "`SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_METERING_HISTORY` and `QUERY_HISTORY`" ,
44- bigquery : "`INFORMATION_SCHEMA.JOBS`" ,
47+ bigquery : "`region-<location>. INFORMATION_SCHEMA.JOBS`" ,
4548 databricks : "`system.billing.usage` and `system.query.history`" ,
4649 } ,
4750 expensive_queries : {
4851 snowflake : "`SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY`" ,
49- bigquery : "`INFORMATION_SCHEMA.JOBS`" ,
52+ bigquery : "`region-<location>. INFORMATION_SCHEMA.JOBS`" ,
5053 databricks : "`system.query.history`" ,
5154 } ,
5255 warehouse_advice : {
53- snowflake : "`SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_LOAD_HISTORY` and `QUERY_HISTORY`" ,
54- bigquery : "`INFORMATION_SCHEMA.JOBS` and `JOBS_TIMELINE`" ,
56+ snowflake : "`SNOWFLAKE.ACCOUNT_USAGE.WAREHOUSE_LOAD_HISTORY` and `QUERY_HISTORY`, plus `SHOW WAREHOUSES` " ,
57+ bigquery : "`region-<location>. INFORMATION_SCHEMA.JOBS` and `JOBS_TIMELINE`" ,
5558 databricks : "`system.compute.warehouse_events` and `system.query.history`" ,
5659 } ,
5760 unused_resources : {
58- snowflake : "`SNOWFLAKE.ACCOUNT_USAGE.TABLE_STORAGE_METRICS`, `ACCESS_HISTORY` and `WAREHOUSES `" ,
59- bigquery : "`INFORMATION_SCHEMA.TABLE_STORAGE`" ,
61+ snowflake : "`SNOWFLAKE.ACCOUNT_USAGE.TABLE_STORAGE_METRICS`, `ACCESS_HISTORY`, `WAREHOUSES` and `QUERY_HISTORY `" ,
62+ bigquery : "`region-<location>. INFORMATION_SCHEMA.TABLE_STORAGE`" ,
6063 databricks : "`system.information_schema.tables`" ,
6164 } ,
6265 role_grants : {
6366 snowflake : "`SNOWFLAKE.ACCOUNT_USAGE.GRANTS_TO_ROLES`" ,
64- bigquery : "`INFORMATION_SCHEMA.OBJECT_PRIVILEGES`" ,
67+ bigquery : "`region-<location>. INFORMATION_SCHEMA.OBJECT_PRIVILEGES`" ,
6568 databricks : "`system.information_schema.table_privileges`" ,
6669 } ,
6770 role_hierarchy : { snowflake : "`SNOWFLAKE.ACCOUNT_USAGE.GRANTS_TO_ROLES` (granted_on = 'ROLE')" } ,
@@ -85,16 +88,20 @@ export interface WorkspaceFallback {
8588 * snapshot only — the same reachability-filtered projection the awareness section
8689 * uses, so this never names a tool the caller's agent cannot call.
8790 */
88- export function workspaceFallbacks ( sessionID : string , supportedTypes : readonly string [ ] ) : WorkspaceFallback [ ] {
91+ export async function workspaceFallbacks ( sessionID : string , supportedTypes : readonly string [ ] ) : Promise < WorkspaceFallback [ ] > {
8992 const precedence = Precedence . forSession ( sessionID )
9093 if ( ! precedence ?. enabled ) return [ ]
91- return Precedence . servedInventory ( precedence ) . flatMap ( ( { type, served } ) => {
94+ const served = Precedence . servedInventory ( precedence ) . flatMap ( ( { type, served } ) => {
9295 if ( ! supportedTypes . includes ( type ) ) return [ ]
9396 const execute = served . find ( ( row ) => row . capability === "sql_execute" )
9497 return execute
9598 ? [ { workspaceName : precedence . workspaceName , workspaceId : precedence . workspaceId , type, modelKey : execute . modelKey } ]
9699 : [ ]
97100 } )
101+ if ( served . length === 0 ) return [ ]
102+ // Same re-validation `check()` does before a redirect: a note naming a workspace the
103+ // project has since left would send the model to that workspace's engine.
104+ return ( await Precedence . snapshotState ( precedence ) ) === "current" ? served : [ ]
98105}
99106
100107/** The sentence appended to a FinOps failure, or nothing when the workspace serves
@@ -117,18 +124,32 @@ export function workspaceFallbackNote(operation: FinopsOperation, fallbacks: Wor
117124 )
118125}
119126
120- /** Why the fallback could not be decided, when routing is disabled for a reason that
121- * is uncertainty rather than a choice. Deliberate disablement (unbound, pilot off,
122- * `--integrations=local`, nothing materialised) says nothing: that is the plain local
123- * failure. Uncertainty must say so (the precedence module's first claim), so the
124- * failure is marked `undetermined` and says the workspace could not be consulted. */
125- function undeterminedNote ( sessionID : string ) : string | undefined {
126- const reason = Precedence . forSession ( sessionID ) ?. disabledReason
127- if ( reason !== "unattributed" && reason !== "binding-unreadable" && reason !== "derive-failed" ) return undefined
128- return (
129- "Whether the linked workspace serves this connection type could not be determined this turn " +
130- "(no routing decision was available), so no workspace alternative is offered here."
131- )
127+ /** Why the fallback could not be decided, when that is uncertainty rather than a
128+ * choice. Deliberate disablement (unbound, pilot off, `--integrations=local`, nothing
129+ * materialised) says nothing: that is the plain local failure. Uncertainty must say so
130+ * (the precedence module's first claim), so the failure is marked `undetermined` and
131+ * says the workspace could not be consulted — mirroring `check()`'s own cases. */
132+ async function undeterminedNote ( sessionID : string ) : Promise < string | undefined > {
133+ const precedence = Precedence . forSession ( sessionID )
134+ // No snapshot at all is the same unknown `check()` reports: a caller that never
135+ // resolved tools, or an entry evicted between resolution and this call.
136+ if ( ! precedence ) {
137+ return "No routing decision was available for this call, so whether the linked workspace serves this connection type is unknown."
138+ }
139+ const reason = precedence . disabledReason
140+ if ( reason === "unattributed" || reason === "binding-unreadable" || reason === "derive-failed" ) {
141+ return (
142+ "Whether the linked workspace serves this connection type could not be determined this turn " +
143+ "(no routing decision was available), so no workspace alternative is offered here."
144+ )
145+ }
146+ if ( ! precedence . enabled ) return undefined
147+ // Enabled, but the link changed or could not be read while this call ran.
148+ const state = await Precedence . snapshotState ( precedence )
149+ if ( state === "current" ) return undefined
150+ return state === "unreadable"
151+ ? "The workspace link could not be read while this call ran, so whether the workspace serves this connection type is unknown."
152+ : "The project was re-linked while this call ran, so the previous routing decision no longer applies."
132153}
133154
134155/**
@@ -137,13 +158,13 @@ function undeterminedNote(sessionID: string): string | undefined {
137158 * metadata so telemetry can tell a failure the model could route around from a dead
138159 * end.
139160 */
140- export function withWorkspaceFallback < T extends { metadata : Record < string , unknown > ; output : string } > (
161+ export async function withWorkspaceFallback < T extends { metadata : Record < string , unknown > ; output : string } > (
141162 sessionID : string ,
142163 operation : FinopsOperation ,
143164 supportedTypes : readonly string [ ] ,
144165 result : T ,
145- ) : T {
146- const fallbacks = workspaceFallbacks ( sessionID , supportedTypes )
166+ ) : Promise < T > {
167+ const fallbacks = await workspaceFallbacks ( sessionID , supportedTypes )
147168 const note = workspaceFallbackNote ( operation , fallbacks )
148169 if ( note ) {
149170 return {
@@ -152,7 +173,7 @@ export function withWorkspaceFallback<T extends { metadata: Record<string, unkno
152173 output : `${ result . output } \n\n${ note } ` ,
153174 }
154175 }
155- const undetermined = undeterminedNote ( sessionID )
176+ const undetermined = await undeterminedNote ( sessionID )
156177 if ( ! undetermined ) return result
157178 return {
158179 ...result ,
0 commit comments