@fabric-harness/databricks 5.0.1 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/agent.js CHANGED
@@ -1803,6 +1803,295 @@ import {
1803
1803
  mergeCapabilityPolicies
1804
1804
  } from "@fabric-harness/sdk/strict";
1805
1805
 
1806
+ // src/cost-budget-actuals.ts
1807
+ import { parseScopeKey, tenantCostLimit } from "@fabric-harness/sdk";
1808
+
1809
+ // src/sql-exec.ts
1810
+ var TERMINAL_STATES = /* @__PURE__ */ new Set(["SUCCEEDED", "FAILED", "CANCELED", "CLOSED"]);
1811
+ async function runStatement(client, warehouseId, statement, parameters, poll) {
1812
+ const submit = await client.executeStatement({
1813
+ warehouseId,
1814
+ statement,
1815
+ waitTimeout: "30s",
1816
+ disposition: "INLINE",
1817
+ ...parameters.length > 0 ? { parameters } : {}
1818
+ });
1819
+ if (TERMINAL_STATES.has(submit.status?.state ?? "")) return submit;
1820
+ const id = submit.statementId;
1821
+ if (!id) return submit;
1822
+ const deadline = Date.now() + poll.timeoutMs;
1823
+ let current = submit;
1824
+ while (!TERMINAL_STATES.has(current.status?.state ?? "") && Date.now() < deadline) {
1825
+ await new Promise((resolve2) => setTimeout(resolve2, poll.intervalMs));
1826
+ current = await client.getStatementResult({ statementId: id });
1827
+ }
1828
+ return current;
1829
+ }
1830
+ function parseStatementRows(response) {
1831
+ const res = response;
1832
+ const columns = res.manifest?.schema?.columns ?? [];
1833
+ const names = columns.map((c2) => c2.name ?? "");
1834
+ const rows = res.result?.dataArray ?? [];
1835
+ return rows.map((row) => {
1836
+ const record2 = {};
1837
+ names.forEach((name, i2) => {
1838
+ record2[name] = row[i2];
1839
+ });
1840
+ return record2;
1841
+ });
1842
+ }
1843
+
1844
+ // src/cost-budget-actuals.ts
1845
+ function toNumber(value) {
1846
+ const n2 = typeof value === "number" ? value : Number(value);
1847
+ return Number.isFinite(n2) ? n2 : 0;
1848
+ }
1849
+ function deriveDateWindow(parsed) {
1850
+ const period = parsed.period;
1851
+ if (!period) return void 0;
1852
+ const parts = period.split(":");
1853
+ const periodType = parts[0];
1854
+ const periodValue = parts.slice(1).join(":");
1855
+ if (!periodValue) return void 0;
1856
+ switch (periodType) {
1857
+ case "day": {
1858
+ return { startDate: periodValue, endDate: periodValue };
1859
+ }
1860
+ case "hour": {
1861
+ const date5 = periodValue.match(/^(\d{4}-\d{2}-\d{2})/)?.[1];
1862
+ return date5 ? { startDate: date5, endDate: date5 } : void 0;
1863
+ }
1864
+ case "month": {
1865
+ const [year, month] = periodValue.split("-");
1866
+ if (!year || !month) return void 0;
1867
+ const lastDay = new Date(Number(year), Number(month), 0).getDate();
1868
+ return {
1869
+ startDate: `${year}-${month}-01`,
1870
+ endDate: `${year}-${month}-${String(lastDay).padStart(2, "0")}`
1871
+ };
1872
+ }
1873
+ default:
1874
+ return void 0;
1875
+ }
1876
+ }
1877
+ function buildCostQuery(tagPrefix, attribution) {
1878
+ const tagAgent = `${tagPrefix}.agent`;
1879
+ const tagTenant = `${tagPrefix}.tenant`;
1880
+ const tagUser = `${tagPrefix}.user`;
1881
+ const conditions = [
1882
+ "u.usage_date >= :start_date AND u.usage_date <= :end_date"
1883
+ ];
1884
+ if (attribution?.agentId) {
1885
+ conditions.push(`u.tags['${tagAgent}'] = :agent_id`);
1886
+ }
1887
+ if (attribution?.tenantId) {
1888
+ conditions.push(`u.tags['${tagTenant}'] = :tenant_id`);
1889
+ }
1890
+ if (attribution?.userId) {
1891
+ conditions.push(`u.tags['${tagUser}'] = :user_id`);
1892
+ }
1893
+ return [
1894
+ "SELECT",
1895
+ " SUM(u.usage_quantity * lp.pricing.default) AS list_cost_usd,",
1896
+ " SUM(u.usage_quantity) AS quantity",
1897
+ "FROM system.billing.usage u",
1898
+ "LEFT JOIN system.billing.list_prices lp",
1899
+ " ON u.cloud = lp.cloud AND u.sku_name = lp.sku_name AND u.usage_unit = lp.usage_unit",
1900
+ " AND u.usage_end_time >= lp.price_start_time",
1901
+ " AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)",
1902
+ `WHERE ${conditions.join(" AND ")}`
1903
+ ].join("\n");
1904
+ }
1905
+ function buildAttributionQuery(tagPrefix, filters) {
1906
+ const tagAgent = `${tagPrefix}.agent`;
1907
+ const tagTenant = `${tagPrefix}.tenant`;
1908
+ const tagUser = `${tagPrefix}.user`;
1909
+ const conditions = [
1910
+ "u.usage_date >= :start_date AND u.usage_date <= :end_date"
1911
+ ];
1912
+ const parameters = [
1913
+ { name: "start_date", value: filters.startDate ?? "", type: "DATE" },
1914
+ { name: "end_date", value: filters.endDate ?? "", type: "DATE" }
1915
+ ];
1916
+ if (filters.agentId) {
1917
+ conditions.push(`u.tags['${tagAgent}'] = :agent_id`);
1918
+ parameters.push({ name: "agent_id", value: filters.agentId });
1919
+ }
1920
+ if (filters.tenantId) {
1921
+ conditions.push(`u.tags['${tagTenant}'] = :tenant_id`);
1922
+ parameters.push({ name: "tenant_id", value: filters.tenantId });
1923
+ }
1924
+ if (filters.userId) {
1925
+ conditions.push(`u.tags['${tagUser}'] = :user_id`);
1926
+ parameters.push({ name: "user_id", value: filters.userId });
1927
+ }
1928
+ if (filters.model) {
1929
+ conditions.push("u.sku_name = :model");
1930
+ parameters.push({ name: "model", value: filters.model });
1931
+ }
1932
+ if (filters.provider) {
1933
+ conditions.push("u.cloud = :provider");
1934
+ parameters.push({ name: "provider", value: filters.provider });
1935
+ }
1936
+ const groupBy = filters.groupBy ?? ["agentId", "tenantId", "userId", "model", "provider"];
1937
+ const groupColumns = [];
1938
+ const selectColumns = [];
1939
+ for (const dim of groupBy) {
1940
+ switch (dim) {
1941
+ case "agentId":
1942
+ selectColumns.push(`u.tags['${tagAgent}'] AS agentId`);
1943
+ groupColumns.push(`u.tags['${tagAgent}']`);
1944
+ break;
1945
+ case "tenantId":
1946
+ selectColumns.push(`u.tags['${tagTenant}'] AS tenantId`);
1947
+ groupColumns.push(`u.tags['${tagTenant}']`);
1948
+ break;
1949
+ case "userId":
1950
+ selectColumns.push(`u.tags['${tagUser}'] AS userId`);
1951
+ groupColumns.push(`u.tags['${tagUser}']`);
1952
+ break;
1953
+ case "model":
1954
+ selectColumns.push("u.sku_name AS model");
1955
+ groupColumns.push("u.sku_name");
1956
+ break;
1957
+ case "provider":
1958
+ selectColumns.push("u.cloud AS provider");
1959
+ groupColumns.push("u.cloud");
1960
+ break;
1961
+ case "sessionId":
1962
+ break;
1963
+ case "turnId":
1964
+ break;
1965
+ }
1966
+ }
1967
+ selectColumns.push(
1968
+ "CONCAT('day:', u.usage_date) AS period",
1969
+ "SUM(u.usage_quantity * lp.pricing.default) AS actualUsd",
1970
+ "SUM(u.usage_quantity) AS actualDbus",
1971
+ "COUNT(*) AS calls"
1972
+ );
1973
+ groupColumns.push("CONCAT('day:', u.usage_date)");
1974
+ const query = [
1975
+ "SELECT",
1976
+ ` ${selectColumns.join(",\n ")}`,
1977
+ "FROM system.billing.usage u",
1978
+ "LEFT JOIN system.billing.list_prices lp",
1979
+ " ON u.cloud = lp.cloud AND u.sku_name = lp.sku_name AND u.usage_unit = lp.usage_unit",
1980
+ " AND u.usage_end_time >= lp.price_start_time",
1981
+ " AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)",
1982
+ `WHERE ${conditions.join(" AND ")}`,
1983
+ `GROUP BY ${groupColumns.join(", ")}`,
1984
+ "ORDER BY actualUsd DESC"
1985
+ ].join("\n");
1986
+ return { query, parameters };
1987
+ }
1988
+ function databricksActualCostSource(client, warehouseId, options = {}) {
1989
+ const cacheTtlMs = options.cacheTtlMs ?? 6e4;
1990
+ const tagPrefix = options.tagPrefix ?? "fabric";
1991
+ const poll = options.poll ?? { intervalMs: 2e3, timeoutMs: 12e4 };
1992
+ const cache = /* @__PURE__ */ new Map();
1993
+ return {
1994
+ async getUsd(scopeKey, attribution, queryOptions) {
1995
+ const ttl = queryOptions?.cacheTtlMs ?? cacheTtlMs;
1996
+ const now = Date.now();
1997
+ const cached4 = cache.get(scopeKey);
1998
+ if (cached4 && cached4.expiresAt > now) {
1999
+ const result2 = {
2000
+ usd: cached4.usd
2001
+ };
2002
+ if (cached4.dbus !== void 0) result2.dbus = cached4.dbus;
2003
+ result2.actualsFetchedAt = cached4.fetchedAt;
2004
+ result2.cacheTtlMs = ttl;
2005
+ return result2;
2006
+ }
2007
+ const parsed = parseScopeKey(scopeKey);
2008
+ if (!parsed) {
2009
+ return { usd: 0, cacheTtlMs: ttl };
2010
+ }
2011
+ const dateWindow = deriveDateWindow(parsed);
2012
+ if (!dateWindow) {
2013
+ return { usd: 0, cacheTtlMs: ttl };
2014
+ }
2015
+ const parameters = [
2016
+ { name: "start_date", value: dateWindow.startDate, type: "DATE" },
2017
+ { name: "end_date", value: dateWindow.endDate, type: "DATE" }
2018
+ ];
2019
+ if (attribution?.agentId) {
2020
+ parameters.push({ name: "agent_id", value: attribution.agentId });
2021
+ }
2022
+ if (attribution?.tenantId) {
2023
+ parameters.push({ name: "tenant_id", value: attribution.tenantId });
2024
+ }
2025
+ if (attribution?.userId) {
2026
+ parameters.push({ name: "user_id", value: attribution.userId });
2027
+ }
2028
+ const response = await runStatement(
2029
+ client,
2030
+ warehouseId,
2031
+ buildCostQuery(tagPrefix, attribution),
2032
+ parameters,
2033
+ poll
2034
+ );
2035
+ const rows = parseStatementRows(response);
2036
+ const usd = rows.reduce((sum, r2) => sum + toNumber(r2.list_cost_usd), 0);
2037
+ const dbus = rows.reduce((sum, r2) => sum + toNumber(r2.quantity), 0);
2038
+ const fetchedAt = (/* @__PURE__ */ new Date()).toISOString();
2039
+ cache.set(scopeKey, {
2040
+ usd,
2041
+ dbus: dbus || void 0,
2042
+ fetchedAt,
2043
+ expiresAt: now + ttl
2044
+ });
2045
+ const result = {
2046
+ usd
2047
+ };
2048
+ if (dbus) result.dbus = dbus;
2049
+ result.actualsFetchedAt = fetchedAt;
2050
+ result.cacheTtlMs = ttl;
2051
+ return result;
2052
+ },
2053
+ async queryAttribution(filters) {
2054
+ if (!filters.startDate || !filters.endDate) {
2055
+ throw new Error("queryAttribution requires startDate and endDate");
2056
+ }
2057
+ const { query, parameters } = buildAttributionQuery(tagPrefix, filters);
2058
+ const response = await runStatement(client, warehouseId, query, parameters, poll);
2059
+ const rows = parseStatementRows(response);
2060
+ return rows.map((r2) => {
2061
+ const row = {
2062
+ period: String(r2.period ?? ""),
2063
+ estimatedUsd: 0,
2064
+ actualUsd: toNumber(r2.actualUsd),
2065
+ actualDbus: toNumber(r2.actualDbus),
2066
+ deltaUsd: toNumber(r2.actualUsd),
2067
+ calls: toNumber(r2.calls)
2068
+ };
2069
+ if (r2.agentId) row.agentId = String(r2.agentId);
2070
+ if (r2.userId) row.userId = String(r2.userId);
2071
+ if (r2.tenantId) row.tenantId = String(r2.tenantId);
2072
+ if (r2.model) row.model = String(r2.model);
2073
+ if (r2.provider) row.provider = String(r2.provider);
2074
+ return row;
2075
+ });
2076
+ }
2077
+ };
2078
+ }
2079
+ function databricksTenantCostLimit(client, warehouseId, tenantId, options) {
2080
+ const base2 = tenantCostLimit(tenantId, {
2081
+ ...options.perHourUsd !== void 0 ? { perHourUsd: options.perHourUsd } : {},
2082
+ ...options.perDayUsd !== void 0 ? { perDayUsd: options.perDayUsd } : {},
2083
+ ...options.perMonthUsd !== void 0 ? { perMonthUsd: options.perMonthUsd } : {},
2084
+ ...options.onExceed ? { onExceed: options.onExceed } : {}
2085
+ });
2086
+ const opts = {};
2087
+ if (options.cacheTtlMs !== void 0) opts.cacheTtlMs = options.cacheTtlMs;
2088
+ return {
2089
+ ...base2,
2090
+ scopeSource: "external",
2091
+ actualSource: databricksActualCostSource(client, warehouseId, opts)
2092
+ };
2093
+ }
2094
+
1806
2095
  // src/governance.ts
1807
2096
  import {
1808
2097
  approvalInputDigest,
@@ -17633,295 +17922,6 @@ function fabricPrincipalFor(principal, overrides = {}) {
17633
17922
  };
17634
17923
  }
17635
17924
 
17636
- // src/cost-budget-actuals.ts
17637
- import { parseScopeKey, tenantCostLimit } from "@fabric-harness/sdk";
17638
-
17639
- // src/sql-exec.ts
17640
- var TERMINAL_STATES = /* @__PURE__ */ new Set(["SUCCEEDED", "FAILED", "CANCELED", "CLOSED"]);
17641
- async function runStatement(client, warehouseId, statement, parameters, poll) {
17642
- const submit = await client.executeStatement({
17643
- warehouseId,
17644
- statement,
17645
- waitTimeout: "30s",
17646
- disposition: "INLINE",
17647
- ...parameters.length > 0 ? { parameters } : {}
17648
- });
17649
- if (TERMINAL_STATES.has(submit.status?.state ?? "")) return submit;
17650
- const id = submit.statementId;
17651
- if (!id) return submit;
17652
- const deadline = Date.now() + poll.timeoutMs;
17653
- let current = submit;
17654
- while (!TERMINAL_STATES.has(current.status?.state ?? "") && Date.now() < deadline) {
17655
- await new Promise((resolve2) => setTimeout(resolve2, poll.intervalMs));
17656
- current = await client.getStatementResult({ statementId: id });
17657
- }
17658
- return current;
17659
- }
17660
- function parseStatementRows(response) {
17661
- const res = response;
17662
- const columns = res.manifest?.schema?.columns ?? [];
17663
- const names = columns.map((c2) => c2.name ?? "");
17664
- const rows = res.result?.dataArray ?? [];
17665
- return rows.map((row) => {
17666
- const record2 = {};
17667
- names.forEach((name, i2) => {
17668
- record2[name] = row[i2];
17669
- });
17670
- return record2;
17671
- });
17672
- }
17673
-
17674
- // src/cost-budget-actuals.ts
17675
- function toNumber(value) {
17676
- const n2 = typeof value === "number" ? value : Number(value);
17677
- return Number.isFinite(n2) ? n2 : 0;
17678
- }
17679
- function deriveDateWindow(parsed) {
17680
- const period = parsed.period;
17681
- if (!period) return void 0;
17682
- const parts = period.split(":");
17683
- const periodType = parts[0];
17684
- const periodValue = parts.slice(1).join(":");
17685
- if (!periodValue) return void 0;
17686
- switch (periodType) {
17687
- case "day": {
17688
- return { startDate: periodValue, endDate: periodValue };
17689
- }
17690
- case "hour": {
17691
- const date5 = periodValue.match(/^(\d{4}-\d{2}-\d{2})/)?.[1];
17692
- return date5 ? { startDate: date5, endDate: date5 } : void 0;
17693
- }
17694
- case "month": {
17695
- const [year, month] = periodValue.split("-");
17696
- if (!year || !month) return void 0;
17697
- const lastDay = new Date(Number(year), Number(month), 0).getDate();
17698
- return {
17699
- startDate: `${year}-${month}-01`,
17700
- endDate: `${year}-${month}-${String(lastDay).padStart(2, "0")}`
17701
- };
17702
- }
17703
- default:
17704
- return void 0;
17705
- }
17706
- }
17707
- function buildCostQuery(tagPrefix, attribution) {
17708
- const tagAgent = `${tagPrefix}.agent`;
17709
- const tagTenant = `${tagPrefix}.tenant`;
17710
- const tagUser = `${tagPrefix}.user`;
17711
- const conditions = [
17712
- "u.usage_date >= :start_date AND u.usage_date <= :end_date"
17713
- ];
17714
- if (attribution?.agentId) {
17715
- conditions.push(`u.tags['${tagAgent}'] = :agent_id`);
17716
- }
17717
- if (attribution?.tenantId) {
17718
- conditions.push(`u.tags['${tagTenant}'] = :tenant_id`);
17719
- }
17720
- if (attribution?.userId) {
17721
- conditions.push(`u.tags['${tagUser}'] = :user_id`);
17722
- }
17723
- return [
17724
- "SELECT",
17725
- " SUM(u.usage_quantity * lp.pricing.default) AS list_cost_usd,",
17726
- " SUM(u.usage_quantity) AS quantity",
17727
- "FROM system.billing.usage u",
17728
- "LEFT JOIN system.billing.list_prices lp",
17729
- " ON u.cloud = lp.cloud AND u.sku_name = lp.sku_name AND u.usage_unit = lp.usage_unit",
17730
- " AND u.usage_end_time >= lp.price_start_time",
17731
- " AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)",
17732
- `WHERE ${conditions.join(" AND ")}`
17733
- ].join("\n");
17734
- }
17735
- function buildAttributionQuery(tagPrefix, filters) {
17736
- const tagAgent = `${tagPrefix}.agent`;
17737
- const tagTenant = `${tagPrefix}.tenant`;
17738
- const tagUser = `${tagPrefix}.user`;
17739
- const conditions = [
17740
- "u.usage_date >= :start_date AND u.usage_date <= :end_date"
17741
- ];
17742
- const parameters = [
17743
- { name: "start_date", value: filters.startDate ?? "", type: "DATE" },
17744
- { name: "end_date", value: filters.endDate ?? "", type: "DATE" }
17745
- ];
17746
- if (filters.agentId) {
17747
- conditions.push(`u.tags['${tagAgent}'] = :agent_id`);
17748
- parameters.push({ name: "agent_id", value: filters.agentId });
17749
- }
17750
- if (filters.tenantId) {
17751
- conditions.push(`u.tags['${tagTenant}'] = :tenant_id`);
17752
- parameters.push({ name: "tenant_id", value: filters.tenantId });
17753
- }
17754
- if (filters.userId) {
17755
- conditions.push(`u.tags['${tagUser}'] = :user_id`);
17756
- parameters.push({ name: "user_id", value: filters.userId });
17757
- }
17758
- if (filters.model) {
17759
- conditions.push("u.sku_name = :model");
17760
- parameters.push({ name: "model", value: filters.model });
17761
- }
17762
- if (filters.provider) {
17763
- conditions.push("u.cloud = :provider");
17764
- parameters.push({ name: "provider", value: filters.provider });
17765
- }
17766
- const groupBy = filters.groupBy ?? ["agentId", "tenantId", "userId", "model", "provider"];
17767
- const groupColumns = [];
17768
- const selectColumns = [];
17769
- for (const dim of groupBy) {
17770
- switch (dim) {
17771
- case "agentId":
17772
- selectColumns.push(`u.tags['${tagAgent}'] AS agentId`);
17773
- groupColumns.push(`u.tags['${tagAgent}']`);
17774
- break;
17775
- case "tenantId":
17776
- selectColumns.push(`u.tags['${tagTenant}'] AS tenantId`);
17777
- groupColumns.push(`u.tags['${tagTenant}']`);
17778
- break;
17779
- case "userId":
17780
- selectColumns.push(`u.tags['${tagUser}'] AS userId`);
17781
- groupColumns.push(`u.tags['${tagUser}']`);
17782
- break;
17783
- case "model":
17784
- selectColumns.push("u.sku_name AS model");
17785
- groupColumns.push("u.sku_name");
17786
- break;
17787
- case "provider":
17788
- selectColumns.push("u.cloud AS provider");
17789
- groupColumns.push("u.cloud");
17790
- break;
17791
- case "sessionId":
17792
- break;
17793
- case "turnId":
17794
- break;
17795
- }
17796
- }
17797
- selectColumns.push(
17798
- "CONCAT('day:', u.usage_date) AS period",
17799
- "SUM(u.usage_quantity * lp.pricing.default) AS actualUsd",
17800
- "SUM(u.usage_quantity) AS actualDbus",
17801
- "COUNT(*) AS calls"
17802
- );
17803
- groupColumns.push("CONCAT('day:', u.usage_date)");
17804
- const query = [
17805
- "SELECT",
17806
- ` ${selectColumns.join(",\n ")}`,
17807
- "FROM system.billing.usage u",
17808
- "LEFT JOIN system.billing.list_prices lp",
17809
- " ON u.cloud = lp.cloud AND u.sku_name = lp.sku_name AND u.usage_unit = lp.usage_unit",
17810
- " AND u.usage_end_time >= lp.price_start_time",
17811
- " AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)",
17812
- `WHERE ${conditions.join(" AND ")}`,
17813
- `GROUP BY ${groupColumns.join(", ")}`,
17814
- "ORDER BY actualUsd DESC"
17815
- ].join("\n");
17816
- return { query, parameters };
17817
- }
17818
- function databricksActualCostSource(client, warehouseId, options = {}) {
17819
- const cacheTtlMs = options.cacheTtlMs ?? 6e4;
17820
- const tagPrefix = options.tagPrefix ?? "fabric";
17821
- const poll = options.poll ?? { intervalMs: 2e3, timeoutMs: 12e4 };
17822
- const cache = /* @__PURE__ */ new Map();
17823
- return {
17824
- async getUsd(scopeKey, attribution, queryOptions) {
17825
- const ttl = queryOptions?.cacheTtlMs ?? cacheTtlMs;
17826
- const now = Date.now();
17827
- const cached4 = cache.get(scopeKey);
17828
- if (cached4 && cached4.expiresAt > now) {
17829
- const result2 = {
17830
- usd: cached4.usd
17831
- };
17832
- if (cached4.dbus !== void 0) result2.dbus = cached4.dbus;
17833
- result2.actualsFetchedAt = cached4.fetchedAt;
17834
- result2.cacheTtlMs = ttl;
17835
- return result2;
17836
- }
17837
- const parsed = parseScopeKey(scopeKey);
17838
- if (!parsed) {
17839
- return { usd: 0, cacheTtlMs: ttl };
17840
- }
17841
- const dateWindow = deriveDateWindow(parsed);
17842
- if (!dateWindow) {
17843
- return { usd: 0, cacheTtlMs: ttl };
17844
- }
17845
- const parameters = [
17846
- { name: "start_date", value: dateWindow.startDate, type: "DATE" },
17847
- { name: "end_date", value: dateWindow.endDate, type: "DATE" }
17848
- ];
17849
- if (attribution?.agentId) {
17850
- parameters.push({ name: "agent_id", value: attribution.agentId });
17851
- }
17852
- if (attribution?.tenantId) {
17853
- parameters.push({ name: "tenant_id", value: attribution.tenantId });
17854
- }
17855
- if (attribution?.userId) {
17856
- parameters.push({ name: "user_id", value: attribution.userId });
17857
- }
17858
- const response = await runStatement(
17859
- client,
17860
- warehouseId,
17861
- buildCostQuery(tagPrefix, attribution),
17862
- parameters,
17863
- poll
17864
- );
17865
- const rows = parseStatementRows(response);
17866
- const usd = rows.reduce((sum, r2) => sum + toNumber(r2.list_cost_usd), 0);
17867
- const dbus = rows.reduce((sum, r2) => sum + toNumber(r2.quantity), 0);
17868
- const fetchedAt = (/* @__PURE__ */ new Date()).toISOString();
17869
- cache.set(scopeKey, {
17870
- usd,
17871
- dbus: dbus || void 0,
17872
- fetchedAt,
17873
- expiresAt: now + ttl
17874
- });
17875
- const result = {
17876
- usd
17877
- };
17878
- if (dbus) result.dbus = dbus;
17879
- result.actualsFetchedAt = fetchedAt;
17880
- result.cacheTtlMs = ttl;
17881
- return result;
17882
- },
17883
- async queryAttribution(filters) {
17884
- if (!filters.startDate || !filters.endDate) {
17885
- throw new Error("queryAttribution requires startDate and endDate");
17886
- }
17887
- const { query, parameters } = buildAttributionQuery(tagPrefix, filters);
17888
- const response = await runStatement(client, warehouseId, query, parameters, poll);
17889
- const rows = parseStatementRows(response);
17890
- return rows.map((r2) => {
17891
- const row = {
17892
- period: String(r2.period ?? ""),
17893
- estimatedUsd: 0,
17894
- actualUsd: toNumber(r2.actualUsd),
17895
- actualDbus: toNumber(r2.actualDbus),
17896
- deltaUsd: toNumber(r2.actualUsd),
17897
- calls: toNumber(r2.calls)
17898
- };
17899
- if (r2.agentId) row.agentId = String(r2.agentId);
17900
- if (r2.userId) row.userId = String(r2.userId);
17901
- if (r2.tenantId) row.tenantId = String(r2.tenantId);
17902
- if (r2.model) row.model = String(r2.model);
17903
- if (r2.provider) row.provider = String(r2.provider);
17904
- return row;
17905
- });
17906
- }
17907
- };
17908
- }
17909
- function databricksTenantCostLimit(client, warehouseId, tenantId, options) {
17910
- const base2 = tenantCostLimit(tenantId, {
17911
- ...options.perHourUsd !== void 0 ? { perHourUsd: options.perHourUsd } : {},
17912
- ...options.perDayUsd !== void 0 ? { perDayUsd: options.perDayUsd } : {},
17913
- ...options.perMonthUsd !== void 0 ? { perMonthUsd: options.perMonthUsd } : {},
17914
- ...options.onExceed ? { onExceed: options.onExceed } : {}
17915
- });
17916
- const opts = {};
17917
- if (options.cacheTtlMs !== void 0) opts.cacheTtlMs = options.cacheTtlMs;
17918
- return {
17919
- ...base2,
17920
- scopeSource: "external",
17921
- actualSource: databricksActualCostSource(client, warehouseId, opts)
17922
- };
17923
- }
17924
-
17925
17925
  // src/index.ts
17926
17926
  import { createSearchTool } from "@fabric-harness/sdk";
17927
17927
 
@@ -18390,26 +18390,87 @@ function jitter(ms) {
18390
18390
  return Math.max(1, Math.round(ms * (0.9 + Math.random() * 0.2)));
18391
18391
  }
18392
18392
 
18393
+ // src/resource-policy.ts
18394
+ var DatabricksResourceNotAllowedError = class extends Error {
18395
+ code = "DATABRICKS_RESOURCE_NOT_ALLOWED";
18396
+ resourceKind;
18397
+ constructor(resourceKind) {
18398
+ super(`Databricks ${resourceKind} is not allowed by the configured policy.`);
18399
+ this.name = "DatabricksResourceNotAllowedError";
18400
+ this.resourceKind = resourceKind;
18401
+ }
18402
+ };
18403
+ function resolveExactResourcePolicy(policy, allowedKey, allowAnyKey, resourceKind) {
18404
+ if (!policy || typeof policy !== "object" || Array.isArray(policy))
18405
+ throw new TypeError(`Databricks ${resourceKind} policy must be an object.`);
18406
+ const value = policy;
18407
+ const allowed = value[allowedKey];
18408
+ const allowAny = value[allowAnyKey];
18409
+ if (allowAny === true && allowed === void 0) return void 0;
18410
+ if (Array.isArray(allowed) && allowed.length > 0 && allowed.every((entry) => typeof entry === "string" && entry.trim().length > 0) && allowAny === void 0)
18411
+ return [...new Set(allowed.map((entry) => entry.trim()))];
18412
+ throw new TypeError(
18413
+ `Databricks ${resourceKind} policy must set either non-empty ${allowedKey} or ${allowAnyKey}: true.`
18414
+ );
18415
+ }
18416
+ function assertExactResourceAllowed(resourceKind, value, allowed) {
18417
+ if (allowed && !allowed.includes(value.trim()))
18418
+ throw new DatabricksResourceNotAllowedError(resourceKind);
18419
+ }
18420
+ function pinnedStringResourceSchema(allowed) {
18421
+ if (!allowed) return { type: "string" };
18422
+ if (allowed.length === 1) return { type: "string", const: allowed[0] };
18423
+ return { type: "string", enum: [...allowed] };
18424
+ }
18425
+
18393
18426
  // src/ai.ts
18394
- function databricksAiQueryTool(client, options) {
18395
- const poll = { intervalMs: options.pollIntervalMs ?? 2e3, timeoutMs: options.timeoutMs ?? 12e4 };
18427
+ function databricksAiQueryTool(client, endpointPolicy, options) {
18428
+ const poll = {
18429
+ intervalMs: options.pollIntervalMs ?? 2e3,
18430
+ timeoutMs: options.timeoutMs ?? 12e4
18431
+ };
18432
+ const allowedEndpoints = resolveExactResourcePolicy(
18433
+ endpointPolicy,
18434
+ "allowedEndpoints",
18435
+ "allowAnyEndpoint",
18436
+ "AI Functions endpoint"
18437
+ );
18396
18438
  return {
18397
18439
  name: options.name ?? "databricks_ai_query",
18398
18440
  description: options.description ?? "Run model inference inside the SQL warehouse via ai_query() against a Databricks serving endpoint.",
18399
18441
  inputSchema: {
18400
18442
  type: "object",
18401
18443
  additionalProperties: false,
18402
- properties: { endpoint: { type: "string" }, request: { type: "string" } },
18444
+ properties: {
18445
+ endpoint: pinnedStringResourceSchema(allowedEndpoints),
18446
+ request: { type: "string" }
18447
+ },
18403
18448
  required: ["endpoint", "request"]
18404
18449
  },
18405
- metadata: { effect: "execute", provider: "databricks", service: "ai-functions" },
18406
- execute: (input) => runStatement(
18407
- client,
18408
- options.warehouseId,
18409
- "SELECT ai_query(:endpoint, :request) AS result",
18410
- [{ name: "endpoint", value: input.endpoint }, { name: "request", value: input.request }],
18411
- poll
18412
- )
18450
+ metadata: {
18451
+ effect: "execute",
18452
+ provider: "databricks",
18453
+ service: "ai-functions",
18454
+ governance: {
18455
+ resources: [
18456
+ { kind: "serving-endpoint", path: "/endpoint" },
18457
+ { kind: "sql-warehouse", value: options.warehouseId }
18458
+ ]
18459
+ }
18460
+ },
18461
+ async execute(input) {
18462
+ assertExactResourceAllowed("AI Functions endpoint", input.endpoint, allowedEndpoints);
18463
+ return await runStatement(
18464
+ client,
18465
+ options.warehouseId,
18466
+ "SELECT ai_query(:endpoint, :request) AS result",
18467
+ [
18468
+ { name: "endpoint", value: input.endpoint },
18469
+ { name: "request", value: input.request }
18470
+ ],
18471
+ poll
18472
+ );
18473
+ }
18413
18474
  };
18414
18475
  }
18415
18476
 
@@ -18543,9 +18604,12 @@ function canonicalJson(value) {
18543
18604
  // src/asset-bundles.ts
18544
18605
  var BUNDLE_RESOURCE_TYPE = "databricks-bundle";
18545
18606
  var FINGERPRINT_EXCLUDED_DIRS = /* @__PURE__ */ new Set([".databricks", "node_modules", "dist"]);
18607
+ var BUNDLE_NAME_PATTERN = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;
18546
18608
  var DatabricksAssetBundleLifecycle = class {
18547
18609
  /** Resolved bundle directory; also the governed resource identity on the tools. */
18548
18610
  bundleDir;
18611
+ /** Logical name of this bundle within a named `assetBundles` map; unset in the single-bundle form. */
18612
+ instanceName;
18549
18613
  target;
18550
18614
  profile;
18551
18615
  executable;
@@ -18565,6 +18629,7 @@ var DatabricksAssetBundleLifecycle = class {
18565
18629
  this.principalLabel = options.principalLabel ?? "unknown";
18566
18630
  if (options.target !== void 0) this.target = options.target;
18567
18631
  if (options.profile !== void 0) this.profile = options.profile;
18632
+ if (options.instanceName !== void 0) this.instanceName = options.instanceName;
18568
18633
  }
18569
18634
  /** Run `databricks bundle validate` without mutating the workspace. */
18570
18635
  async validate() {
@@ -18642,6 +18707,10 @@ var DatabricksAssetBundleLifecycle = class {
18642
18707
  key,
18643
18708
  "--no-wait",
18644
18709
  ...options.restart === true ? ["--restart"] : [],
18710
+ "--output",
18711
+ "json",
18712
+ "--progress-format",
18713
+ "json",
18645
18714
  ...this.targetArgs(),
18646
18715
  ...this.profileArgs()
18647
18716
  ];
@@ -18650,7 +18719,7 @@ var DatabricksAssetBundleLifecycle = class {
18650
18719
  env: this.env
18651
18720
  });
18652
18721
  requireSuccess("Databricks bundle run submission", result);
18653
- return { submitted: true, output: lastNonEmptyLine(result.stdout) };
18722
+ return parseBundleRunResult(key, result);
18654
18723
  }
18655
18724
  /** Destroy a Harness-recorded bundle deployment and delete its managed record. */
18656
18725
  async destroy(options = {}) {
@@ -18686,14 +18755,20 @@ var DatabricksAssetBundleLifecycle = class {
18686
18755
  await this.store.delete(BUNDLE_RESOURCE_TYPE, resourceId);
18687
18756
  return { destroyed: true, output: lastNonEmptyLine(result.stdout) };
18688
18757
  }
18689
- /** Bundle `name:` parsed from `databricks.yml`, falling back to the directory basename. */
18758
+ /**
18759
+ * Bundle `name:` parsed from `databricks.yml`, falling back to the directory basename. Named
18760
+ * bundles append `#instanceName` (and `#target` when set) so two lifecycles over the same manifest
18761
+ * cannot collide in a shared managed-resource store; the single-bundle form keeps the bare name so
18762
+ * existing records stay addressable.
18763
+ */
18690
18764
  async resourceId() {
18691
18765
  if (this.bundleName === void 0) {
18692
18766
  const manifest = await readFile2(path2.join(this.bundleDir, "databricks.yml"), "utf8");
18693
18767
  const match = /^\s*name:\s*["']?([^\s"']+)["']?\s*$/m.exec(manifest);
18694
18768
  this.bundleName = match?.[1] ?? path2.basename(this.bundleDir);
18695
18769
  }
18696
- return this.bundleName;
18770
+ if (this.instanceName === void 0) return this.bundleName;
18771
+ return `${this.bundleName}#${this.instanceName}${this.target ? `#${this.target}` : ""}`;
18697
18772
  }
18698
18773
  async requireManifest() {
18699
18774
  const manifest = path2.join(this.bundleDir, "databricks.yml");
@@ -18713,7 +18788,65 @@ var DatabricksAssetBundleLifecycle = class {
18713
18788
  function databricksAssetBundleLifecycle(options) {
18714
18789
  return new DatabricksAssetBundleLifecycle(options);
18715
18790
  }
18791
+ function isDatabricksAssetBundleEntry(value) {
18792
+ if (value instanceof DatabricksAssetBundleLifecycle) return true;
18793
+ return isPlainObject2(value) && Object.hasOwn(value, "bundleDir") && typeof value.bundleDir === "string";
18794
+ }
18795
+ function requireDatabricksAssetBundleMap(value) {
18796
+ if (!isPlainObject2(value))
18797
+ throw new Error(
18798
+ "databricks asset bundles: named bundles must be an object keyed by bundle name."
18799
+ );
18800
+ }
18801
+ function databricksAssetBundleLifecycles(options, defaults = {}) {
18802
+ requireDatabricksAssetBundleMap(options);
18803
+ const lifecycles = /* @__PURE__ */ new Map();
18804
+ for (const name of Object.keys(options).sort()) {
18805
+ requireBundleName(name);
18806
+ const entry = options[name];
18807
+ if (!isPlainObject2(entry) || typeof entry.bundleDir !== "string" || !entry.bundleDir.trim())
18808
+ throw new Error(
18809
+ `databricks asset bundles: bundle "${name}" requires a non-empty bundleDir string.`
18810
+ );
18811
+ lifecycles.set(
18812
+ name,
18813
+ new DatabricksAssetBundleLifecycle({ ...defaults, ...entry, instanceName: name })
18814
+ );
18815
+ }
18816
+ requireNamedBundles(lifecycles);
18817
+ return lifecycles;
18818
+ }
18716
18819
  function databricksAssetBundleTools(options) {
18820
+ if (isDatabricksAssetBundleEntry(options)) return singleBundleTools(options);
18821
+ return namedBundleTools(
18822
+ options instanceof Map ? checkedLifecycles(options) : databricksAssetBundleLifecycles(options)
18823
+ );
18824
+ }
18825
+ function checkedLifecycles(lifecycles) {
18826
+ const checked = /* @__PURE__ */ new Map();
18827
+ for (const [name, lifecycle] of lifecycles) {
18828
+ requireBundleName(name);
18829
+ if (!(lifecycle instanceof DatabricksAssetBundleLifecycle))
18830
+ throw new Error(
18831
+ `databricks asset bundles: bundle "${name}" must be a DatabricksAssetBundleLifecycle.`
18832
+ );
18833
+ if (lifecycle.instanceName !== name)
18834
+ throw new Error(
18835
+ `databricks asset bundles: bundle "${name}" must be built with instanceName "${name}"${lifecycle.instanceName === void 0 ? "" : `, not "${lifecycle.instanceName}"`}.`
18836
+ );
18837
+ checked.set(name, lifecycle);
18838
+ }
18839
+ return checked;
18840
+ }
18841
+ function requireBundleName(name) {
18842
+ if (typeof name !== "string")
18843
+ throw new Error(`databricks asset bundles: bundle names must be strings, not ${typeof name}.`);
18844
+ if (!BUNDLE_NAME_PATTERN.test(name))
18845
+ throw new Error(
18846
+ `databricks asset bundles: bundle name "${name}" must match ${BUNDLE_NAME_PATTERN.source}.`
18847
+ );
18848
+ }
18849
+ function singleBundleTools(options) {
18717
18850
  const lifecycle = options instanceof DatabricksAssetBundleLifecycle ? options : new DatabricksAssetBundleLifecycle(options);
18718
18851
  const resources = [{ kind: "bundle", value: lifecycle.bundleDir }];
18719
18852
  return [
@@ -18770,6 +18903,94 @@ function databricksAssetBundleTools(options) {
18770
18903
  )
18771
18904
  ];
18772
18905
  }
18906
+ function namedBundleTools(lifecycles) {
18907
+ requireNamedBundles(lifecycles);
18908
+ const names = [...lifecycles.keys()].sort();
18909
+ const selector = { bundle: { type: "string", enum: names } };
18910
+ const resources = [{ kind: "bundle", path: "/bundle" }];
18911
+ const select = (input) => selectBundle(lifecycles, names, input);
18912
+ return [
18913
+ bundleTool(
18914
+ "databricks_bundle_validate",
18915
+ "Validate one checked-in Databricks Asset Bundle, selected by name with `bundle`, without deploying it.",
18916
+ "read",
18917
+ {
18918
+ type: "object",
18919
+ additionalProperties: false,
18920
+ properties: { ...selector },
18921
+ required: ["bundle"]
18922
+ },
18923
+ [],
18924
+ async (input) => select(input).validate()
18925
+ ),
18926
+ bundleTool(
18927
+ "databricks_bundle_deploy",
18928
+ "Validate and deploy one checked-in Databricks Asset Bundle, selected by name with `bundle`, recording its source fingerprint.",
18929
+ "write",
18930
+ {
18931
+ type: "object",
18932
+ additionalProperties: false,
18933
+ properties: { ...selector, expectedFingerprint: { type: "string" } },
18934
+ required: ["bundle"]
18935
+ },
18936
+ resources,
18937
+ async (input) => {
18938
+ const lifecycle = select(input);
18939
+ const expectedFingerprint = input.expectedFingerprint;
18940
+ return lifecycle.deploy(expectedFingerprint !== void 0 ? { expectedFingerprint } : {});
18941
+ }
18942
+ ),
18943
+ bundleTool(
18944
+ "databricks_bundle_run",
18945
+ "Submit (without waiting) a run of a job or pipeline defined by one Databricks Asset Bundle, selected by name with `bundle`, using its databricks.yml resource key.",
18946
+ "execute",
18947
+ {
18948
+ type: "object",
18949
+ additionalProperties: false,
18950
+ properties: { ...selector, key: { type: "string" }, restart: { type: "boolean" } },
18951
+ required: ["bundle", "key"]
18952
+ },
18953
+ resources,
18954
+ async (input) => select(input).run({
18955
+ key: input.key,
18956
+ restart: input.restart === true
18957
+ })
18958
+ ),
18959
+ bundleTool(
18960
+ "databricks_bundle_destroy",
18961
+ "Destroy a Harness-recorded deployment of one Databricks Asset Bundle, selected by name with `bundle`.",
18962
+ "write",
18963
+ {
18964
+ type: "object",
18965
+ additionalProperties: false,
18966
+ properties: { ...selector, force: { type: "boolean" } },
18967
+ required: ["bundle"]
18968
+ },
18969
+ resources,
18970
+ async (input) => select(input).destroy({ force: input.force === true })
18971
+ )
18972
+ ];
18973
+ }
18974
+ function selectBundle(lifecycles, names, input) {
18975
+ const bundle = input.bundle;
18976
+ if (typeof bundle !== "string" || !bundle)
18977
+ throw new Error(
18978
+ `databricks asset bundles: bundle is required; configured bundles: ${names.join(", ")}.`
18979
+ );
18980
+ const lifecycle = lifecycles.get(bundle);
18981
+ if (!lifecycle)
18982
+ throw new Error(
18983
+ `databricks asset bundles: unknown bundle "${bundle}"; configured bundles: ${names.join(", ")}.`
18984
+ );
18985
+ return lifecycle;
18986
+ }
18987
+ function requireNamedBundles(lifecycles) {
18988
+ if (lifecycles.size === 0)
18989
+ throw new Error("databricks asset bundles: at least one named bundle is required.");
18990
+ }
18991
+ function isPlainObject2(value) {
18992
+ return typeof value === "object" && value !== null && !Array.isArray(value);
18993
+ }
18773
18994
  function bundleTool(name, description, effect, inputSchema, resources, execute2) {
18774
18995
  return {
18775
18996
  name,
@@ -18808,6 +19029,79 @@ function requireSuccess(label, result) {
18808
19029
  function lastNonEmptyLine(value) {
18809
19030
  return value.split("\n").map((line) => line.trim()).filter(Boolean).at(-1) ?? "";
18810
19031
  }
19032
+ function parseBundleRunResult(resourceKey2, result) {
19033
+ const records = [...jsonRecords(result.stdout), ...jsonRecords(result.stderr)];
19034
+ const jobEvent = records.find((record2) => record2.type === "job_run_url");
19035
+ const pipelineEvent = records.find((record2) => record2.type === "pipeline_update_url");
19036
+ const runUrl = stringField(jobEvent, "url") ?? stringField(pipelineEvent, "url") ?? firstUrl(result.stdout) ?? firstUrl(result.stderr);
19037
+ const explicitRunId = numberField(jobEvent, "run_id") ?? numberField(jobEvent, "runId") ?? records.map((record2) => numberField(record2, "run_id") ?? numberField(record2, "runId")).find(
19038
+ (value) => value !== void 0
19039
+ );
19040
+ const runId = explicitRunId ?? runIdFromUrl(runUrl);
19041
+ const pipelineId = stringField(pipelineEvent, "pipeline_id") ?? stringField(pipelineEvent, "pipelineId") ?? pipelineIdentityFromUrl(runUrl)?.pipelineId;
19042
+ const updateId = stringField(pipelineEvent, "update_id") ?? stringField(pipelineEvent, "updateId") ?? records.map((record2) => stringField(record2, "update_id") ?? stringField(record2, "updateId")).find((value) => value !== void 0) ?? pipelineIdentityFromUrl(runUrl)?.updateId;
19043
+ const output = lastNonEmptyLine(result.stdout) || lastNonEmptyLine(result.stderr);
19044
+ return {
19045
+ submitted: true,
19046
+ resourceKey: resourceKey2,
19047
+ ...runId !== void 0 ? { runKind: "job", runId } : {},
19048
+ ...pipelineId !== void 0 || updateId !== void 0 ? {
19049
+ runKind: "pipeline",
19050
+ ...pipelineId !== void 0 ? { pipelineId } : {},
19051
+ ...updateId !== void 0 ? { updateId } : {}
19052
+ } : {},
19053
+ ...runUrl !== void 0 ? { runUrl } : {},
19054
+ output
19055
+ };
19056
+ }
19057
+ function jsonRecords(value) {
19058
+ const records = [];
19059
+ for (const candidate of [value.trim(), ...value.split("\n").map((line) => line.trim())]) {
19060
+ if (!(candidate.startsWith("{") && candidate.endsWith("}"))) continue;
19061
+ try {
19062
+ const parsed = JSON.parse(candidate);
19063
+ if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed))
19064
+ records.push(parsed);
19065
+ } catch {
19066
+ }
19067
+ }
19068
+ return records;
19069
+ }
19070
+ function stringField(record2, key) {
19071
+ const value = record2?.[key];
19072
+ return typeof value === "string" && value.trim() ? value : void 0;
19073
+ }
19074
+ function numberField(record2, key) {
19075
+ const value = record2?.[key];
19076
+ const parsed = typeof value === "number" ? value : typeof value === "string" && /^\d+$/.test(value) ? Number(value) : void 0;
19077
+ return parsed !== void 0 && Number.isSafeInteger(parsed) && parsed > 0 ? parsed : void 0;
19078
+ }
19079
+ function firstUrl(value) {
19080
+ const match = /https?:\/\/[^\s"']+/u.exec(value);
19081
+ return match?.[0]?.replace(/[),.;]+$/u, "");
19082
+ }
19083
+ function runIdFromUrl(value) {
19084
+ if (!value) return void 0;
19085
+ try {
19086
+ const url2 = new URL(value);
19087
+ const raw = /\/jobs\/\d+\/runs\/(\d+)/u.exec(url2.pathname)?.[1] ?? /(?:^|\/)job\/\d+\/run\/(\d+)(?:\/|$)/u.exec(url2.hash.replace(/^#/, ""))?.[1];
19088
+ return raw ? numberField({ value: raw }, "value") : void 0;
19089
+ } catch {
19090
+ return void 0;
19091
+ }
19092
+ }
19093
+ function pipelineIdentityFromUrl(value) {
19094
+ if (!value) return void 0;
19095
+ try {
19096
+ const url2 = new URL(value);
19097
+ const match = /(?:^|\/)pipelines\/([^/]+)\/updates\/([^/?#]+)/u.exec(
19098
+ `${url2.pathname}/${url2.hash.replace(/^#/, "")}`
19099
+ );
19100
+ return match?.[1] && match[2] ? { pipelineId: match[1], updateId: match[2] } : void 0;
19101
+ } catch {
19102
+ return void 0;
19103
+ }
19104
+ }
18811
19105
 
18812
19106
  // src/consumption.ts
18813
19107
  var GROUP_COLUMNS = {
@@ -21868,43 +22162,76 @@ function databricksPipelineStatusTool(client, options = {}) {
21868
22162
  execute: (input) => client.get({ pipelineId: input.pipelineId })
21869
22163
  };
21870
22164
  }
21871
- function databricksPipelineStartTool(client, options = {}) {
22165
+ function databricksPipelineStartTool(client, runPolicy, options = {}) {
22166
+ const allowedPipelineIds = resolveExactResourcePolicy(
22167
+ runPolicy,
22168
+ "allowedPipelineIds",
22169
+ "allowAnyPipelineId",
22170
+ "Lakeflow pipeline"
22171
+ );
21872
22172
  return {
21873
22173
  name: options.name ?? "databricks_pipeline_start",
21874
22174
  description: options.description ?? "Trigger a Lakeflow pipeline update (run). Set fullRefresh to reprocess all data.",
21875
22175
  inputSchema: {
21876
22176
  type: "object",
21877
22177
  additionalProperties: false,
21878
- properties: { pipelineId: { type: "string" }, fullRefresh: { type: "boolean" } },
22178
+ properties: {
22179
+ pipelineId: pinnedStringResourceSchema(allowedPipelineIds),
22180
+ fullRefresh: { type: "boolean" }
22181
+ },
21879
22182
  required: ["pipelineId"]
21880
22183
  },
21881
- metadata: { effect: "execute", provider: "databricks", service: "lakeflow" },
21882
- execute: (input) => client.start({
21883
- pipelineId: input.pipelineId,
21884
- ...input.fullRefresh !== void 0 ? { fullRefresh: input.fullRefresh } : {}
21885
- })
22184
+ metadata: {
22185
+ effect: "execute",
22186
+ provider: "databricks",
22187
+ service: "lakeflow",
22188
+ governance: { resources: [{ kind: "pipeline", path: "/pipelineId" }] }
22189
+ },
22190
+ async execute(input) {
22191
+ assertExactResourceAllowed("Lakeflow pipeline", input.pipelineId, allowedPipelineIds);
22192
+ return await client.start({
22193
+ pipelineId: input.pipelineId,
22194
+ ...input.fullRefresh !== void 0 ? { fullRefresh: input.fullRefresh } : {}
22195
+ });
22196
+ }
21886
22197
  };
21887
22198
  }
21888
- function databricksPipelineStopTool(client, options = {}) {
22199
+ function databricksPipelineStopTool(client, runPolicy, options = {}) {
22200
+ const allowedPipelineIds = resolveExactResourcePolicy(
22201
+ runPolicy,
22202
+ "allowedPipelineIds",
22203
+ "allowAnyPipelineId",
22204
+ "Lakeflow pipeline"
22205
+ );
21889
22206
  return {
21890
22207
  name: options.name ?? "databricks_pipeline_stop",
21891
22208
  description: options.description ?? "Stop the active update of a Lakeflow pipeline.",
21892
22209
  inputSchema: {
21893
22210
  type: "object",
21894
22211
  additionalProperties: false,
21895
- properties: { pipelineId: { type: "string" } },
22212
+ properties: { pipelineId: pinnedStringResourceSchema(allowedPipelineIds) },
21896
22213
  required: ["pipelineId"]
21897
22214
  },
21898
- metadata: { effect: "execute", provider: "databricks", service: "lakeflow" },
21899
- execute: (input) => client.stop({ pipelineId: input.pipelineId })
22215
+ metadata: {
22216
+ effect: "execute",
22217
+ provider: "databricks",
22218
+ service: "lakeflow",
22219
+ governance: { resources: [{ kind: "pipeline", path: "/pipelineId" }] }
22220
+ },
22221
+ async execute(input) {
22222
+ assertExactResourceAllowed("Lakeflow pipeline", input.pipelineId, allowedPipelineIds);
22223
+ return await client.stop({ pipelineId: input.pipelineId });
22224
+ }
21900
22225
  };
21901
22226
  }
21902
- function databricksLakeflowTools(client) {
22227
+ function databricksLakeflowTools(client, runPolicy) {
21903
22228
  return [
21904
22229
  databricksPipelineListTool(client),
21905
22230
  databricksPipelineStatusTool(client),
21906
- databricksPipelineStartTool(client),
21907
- databricksPipelineStopTool(client)
22231
+ ...runPolicy ? [
22232
+ databricksPipelineStartTool(client, runPolicy),
22233
+ databricksPipelineStopTool(client, runPolicy)
22234
+ ] : []
21908
22235
  ];
21909
22236
  }
21910
22237
 
@@ -58522,7 +58849,8 @@ function lexSqlReadStatement(statement) {
58522
58849
  function assertDatabricksSqlReadStatement(statement) {
58523
58850
  if (!statement.trim()) throw new Error("Databricks SQL read denied: statement cannot be empty.");
58524
58851
  const { tokens, semicolons } = lexSqlReadStatement(statement);
58525
- if (tokens.length === 0) throw new Error("Databricks SQL read denied: expected a SELECT statement.");
58852
+ if (tokens.length === 0)
58853
+ throw new Error("Databricks SQL read denied: expected a SELECT statement.");
58526
58854
  if (semicolons.length > 1)
58527
58855
  throw new Error("Databricks SQL read denied: multiple statements are not allowed.");
58528
58856
  if (semicolons.length === 1) {
@@ -58540,7 +58868,8 @@ function assertDatabricksSqlReadStatement(statement) {
58540
58868
  if (unsafe)
58541
58869
  throw new Error(`Databricks SQL read denied: unsafe keyword ${unsafe.value} is not allowed.`);
58542
58870
  }
58543
- function databricksSqlTool(client, options) {
58871
+ function databricksSqlTool(client, policy, options) {
58872
+ const executionPolicy = resolveSqlExecutionPolicy(policy);
58544
58873
  return {
58545
58874
  name: options.name ?? "databricks_sql",
58546
58875
  description: options.description ?? "Run a SQL statement on a Databricks SQL Warehouse.",
@@ -58548,23 +58877,55 @@ function databricksSqlTool(client, options) {
58548
58877
  type: "object",
58549
58878
  additionalProperties: false,
58550
58879
  properties: {
58551
- statement: { type: "string" },
58880
+ statement: executionPolicy.kind === "allowlist" ? pinnedStringResourceSchema(executionPolicy.statements) : { type: "string" },
58552
58881
  catalog: { type: "string" },
58553
58882
  schema: { type: "string" }
58554
58883
  },
58555
58884
  required: ["statement"]
58556
58885
  },
58557
- metadata: { effect: "execute", provider: "databricks", service: "sql" },
58558
- execute: (input) => client.executeStatement({
58559
- warehouseId: options.warehouseId,
58560
- statement: input.statement,
58561
- waitTimeout: options.waitTimeout ?? "30s",
58562
- disposition: options.disposition ?? "INLINE",
58563
- ...input.catalog ? { catalog: input.catalog } : {},
58564
- ...input.schema ? { schema: input.schema } : {}
58565
- })
58886
+ metadata: {
58887
+ effect: "execute",
58888
+ provider: "databricks",
58889
+ service: "sql",
58890
+ governance: {
58891
+ resources: [{ kind: "sql-warehouse", value: options.warehouseId }]
58892
+ }
58893
+ },
58894
+ async execute(input) {
58895
+ await assertSqlExecutionAllowed(input, executionPolicy);
58896
+ return await client.executeStatement({
58897
+ warehouseId: options.warehouseId,
58898
+ statement: input.statement,
58899
+ waitTimeout: options.waitTimeout ?? "30s",
58900
+ disposition: options.disposition ?? "INLINE",
58901
+ ...input.catalog ? { catalog: input.catalog } : {},
58902
+ ...input.schema ? { schema: input.schema } : {}
58903
+ });
58904
+ }
58566
58905
  };
58567
58906
  }
58907
+ function resolveSqlExecutionPolicy(policy) {
58908
+ if (!policy || typeof policy !== "object" || Array.isArray(policy))
58909
+ throw new TypeError("Databricks SQL execution policy must be an object.");
58910
+ if ("validateStatement" in policy && typeof policy.validateStatement === "function")
58911
+ return { kind: "validator", validate: policy.validateStatement };
58912
+ const statements = resolveExactResourcePolicy(
58913
+ policy,
58914
+ "allowedStatements",
58915
+ "allowAnyStatement",
58916
+ "SQL statement"
58917
+ );
58918
+ return statements ? { kind: "allowlist", statements } : { kind: "allow-any" };
58919
+ }
58920
+ async function assertSqlExecutionAllowed(input, policy) {
58921
+ if (policy.kind === "allow-any") return;
58922
+ if (policy.kind === "allowlist") {
58923
+ assertExactResourceAllowed("SQL statement", input.statement, policy.statements);
58924
+ return;
58925
+ }
58926
+ if (await policy.validate(input) !== true)
58927
+ throw new DatabricksResourceNotAllowedError("SQL statement");
58928
+ }
58568
58929
  function databricksSqlReadTool(client, options) {
58569
58930
  return {
58570
58931
  name: options.name ?? "sql_read",
@@ -58584,9 +58945,7 @@ function databricksSqlReadTool(client, options) {
58584
58945
  provider: "databricks",
58585
58946
  service: "sql",
58586
58947
  governance: {
58587
- resources: [
58588
- { kind: "catalog", path: "/catalog", catalogQualified: true, optional: true }
58589
- ]
58948
+ resources: [{ kind: "catalog", path: "/catalog", catalogQualified: true, optional: true }]
58590
58949
  }
58591
58950
  },
58592
58951
  async execute(input) {
@@ -59404,6 +59763,7 @@ async function databricksWithManagedMcp(config2, scope) {
59404
59763
  }
59405
59764
  function buildDatabricksBundle(config2, scope) {
59406
59765
  registerDatabricksModelProvider();
59766
+ const assetBundles = buildAssetBundleLifecycles(config2);
59407
59767
  const genie = normalizeGenieConfig(config2);
59408
59768
  validateAuthoringConfig(config2, genie);
59409
59769
  const identity = scope?.identity ?? databricksIdentity(config2.principal);
@@ -59417,12 +59777,8 @@ function buildDatabricksBundle(config2, scope) {
59417
59777
  const client = native.rawProtocol;
59418
59778
  const genieAgentModeClient = genie.agentMode ? databricksGenieAgentMode(client, genie.agentMode) : void 0;
59419
59779
  const agentServicesClient = config2.agentServices ? databricksAgentServices(client, config2.agentServices) : void 0;
59420
- const assetBundle = config2.assetBundles ? databricksAssetBundleLifecycle({
59421
- bundleDir: config2.assetBundles.bundleDir,
59422
- ...config2.assetBundles.target ? { target: config2.assetBundles.target } : {},
59423
- ...config2.assetBundles.store ? { store: config2.assetBundles.store } : {},
59424
- principalLabel: config2.governance?.principalLabel ?? principalLabel(config2.principal)
59425
- }) : void 0;
59780
+ const namedAssetBundles = assetBundles instanceof Map ? Object.freeze(Object.fromEntries(assetBundles)) : void 0;
59781
+ const assetBundle = assetBundles instanceof Map ? assetBundles.size === 1 ? [...assetBundles.values()][0] : void 0 : assetBundles;
59426
59782
  let embeddings;
59427
59783
  let retriever;
59428
59784
  let searchTool;
@@ -59457,11 +59813,19 @@ function buildDatabricksBundle(config2, scope) {
59457
59813
  }
59458
59814
  const rawTools = [
59459
59815
  ...config2.sqlRead && config2.warehouseId ? [databricksSqlReadTool(native.statements, { warehouseId: config2.warehouseId })] : [],
59460
- ...config2.warehouseId ? [databricksSqlTool(native.statements, { warehouseId: config2.warehouseId })] : [],
59816
+ ...config2.sqlExecute && config2.warehouseId ? [
59817
+ databricksSqlTool(native.statements, config2.sqlExecute, {
59818
+ warehouseId: config2.warehouseId
59819
+ })
59820
+ ] : [],
59461
59821
  unityCatalogTablesTool(native.tables),
59462
59822
  databricksTableInfoTool(native.tables),
59463
59823
  ...searchTool ? [searchTool] : [],
59464
- ...config2.aiFunctions && config2.warehouseId ? [databricksAiQueryTool(native.statements, { warehouseId: config2.warehouseId })] : [],
59824
+ ...config2.aiFunctions && config2.warehouseId ? [
59825
+ databricksAiQueryTool(native.statements, config2.aiFunctions, {
59826
+ warehouseId: config2.warehouseId
59827
+ })
59828
+ ] : [],
59465
59829
  ...genie.conversations ? [
59466
59830
  databricksGenieTool(native.genie, {
59467
59831
  ...genie.conversations,
@@ -59487,7 +59851,10 @@ function buildDatabricksBundle(config2, scope) {
59487
59851
  ...genie.manage.sqlPolicy ? { sqlPolicy: genie.manage.sqlPolicy } : {}
59488
59852
  }
59489
59853
  ) : [],
59490
- ...config2.lakeflow ? databricksLakeflowTools(native.pipelines) : [],
59854
+ ...config2.lakeflow ? databricksLakeflowTools(
59855
+ native.pipelines,
59856
+ typeof config2.lakeflow === "object" ? config2.lakeflow.runPolicy : void 0
59857
+ ) : [],
59491
59858
  ...config2.consumption && config2.warehouseId ? [
59492
59859
  databricksConsumptionTool(native.statements, {
59493
59860
  warehouseId: config2.warehouseId
@@ -59504,7 +59871,7 @@ function buildDatabricksBundle(config2, scope) {
59504
59871
  ...config2.lakeflowAuthoring ? databricksLakeflowAuthoringTools(native.pipelines, {
59505
59872
  ...config2.lakeflowEvents && config2.warehouseId ? { warehouseId: config2.warehouseId, statements: native.statements } : {}
59506
59873
  }) : [],
59507
- ...assetBundle ? databricksAssetBundleTools(assetBundle) : [],
59874
+ ...assetBundles ? databricksAssetBundleTools(assetBundles) : [],
59508
59875
  ...config2.aiSearchAdmin ? databricksAiSearchAdminTools(native.vectorSearch) : [],
59509
59876
  ...config2.servingAdmin ? databricksServingAdminTools(native.modelServing) : [],
59510
59877
  ...config2.ucAdmin ? databricksUnityCatalogAdminTools(
@@ -59524,16 +59891,19 @@ function buildDatabricksBundle(config2, scope) {
59524
59891
  ...config2.tools ?? []
59525
59892
  ];
59526
59893
  const authoringServices = enabledAuthoringServices(config2);
59527
- const customTools = new Set(config2.tools ?? []);
59528
- const authoringToolNames = new Set(
59894
+ const approvalServices = config2.governance?.approvalServices;
59895
+ const approvalRequiredToolNames = new Set(
59529
59896
  rawTools.filter((tool7) => {
59530
59897
  const effect = tool7.metadata?.effect;
59531
- return (effect === "write" || effect === "execute") && Boolean(governanceMetadata(tool7));
59898
+ if (effect !== "write" && effect !== "execute") return false;
59899
+ if (!approvalServices) return true;
59900
+ const service = tool7.metadata?.service;
59901
+ return typeof service === "string" && approvalServices.includes(service);
59532
59902
  }).map((tool7) => tool7.name)
59533
59903
  );
59534
59904
  for (const tool7 of rawTools) {
59535
59905
  const effect = tool7.metadata?.effect;
59536
- if ((authoringToolNames.has(tool7.name) || customTools.has(tool7)) && (effect === "write" || effect === "execute"))
59906
+ if (effect === "write" || effect === "execute")
59537
59907
  validateDatabricksGovernanceDescriptor(tool7, true);
59538
59908
  }
59539
59909
  const tools = withGovernanceTools(rawTools, {
@@ -59541,7 +59911,7 @@ function buildDatabricksBundle(config2, scope) {
59541
59911
  ...config2.governance?.onLineage ? { onLineage: config2.governance.onLineage } : {},
59542
59912
  ...config2.governance?.catalogs ? { catalogs: config2.governance.catalogs } : {},
59543
59913
  executingPrincipal,
59544
- ...config2.governance?.stewardAudience && !config2.allowUnapprovedAuthoring ? { requireApprovalGrantForTools: [...authoringToolNames] } : {}
59914
+ ...config2.governance?.stewardAudience && !config2.allowUnapprovedAuthoring ? { requireApprovalGrantForTools: [...approvalRequiredToolNames] } : {}
59545
59915
  });
59546
59916
  const policy = databricksGovernancePolicy({
59547
59917
  tools,
@@ -59584,6 +59954,7 @@ function buildDatabricksBundle(config2, scope) {
59584
59954
  ...genieAgentModeClient ? { genieAgentMode: genieAgentModeClient } : {},
59585
59955
  ...agentServicesClient ? { agentServices: agentServicesClient } : {},
59586
59956
  ...assetBundle ? { assetBundle } : {},
59957
+ ...namedAssetBundles ? { assetBundles: namedAssetBundles } : {},
59587
59958
  aiGateway: {
59588
59959
  ensure: () => ensureDatabricksAiGateway(client),
59589
59960
  listModelServices: (options) => listDatabricksModelServices(client, options),
@@ -59612,6 +59983,36 @@ function buildDatabricksBundle(config2, scope) {
59612
59983
  }
59613
59984
  return bundle;
59614
59985
  }
59986
+ function buildAssetBundleLifecycles(config2) {
59987
+ const assetBundles = config2.assetBundles;
59988
+ if (assetBundles === void 0) return void 0;
59989
+ const label = config2.governance?.principalLabel ?? principalLabel(config2.principal);
59990
+ if (isDatabricksAssetBundleEntry(assetBundles))
59991
+ return databricksAssetBundleLifecycle({
59992
+ bundleDir: assetBundles.bundleDir,
59993
+ ...assetBundles.target !== void 0 ? { target: assetBundles.target } : {},
59994
+ ...assetBundles.store !== void 0 ? { store: assetBundles.store } : {},
59995
+ principalLabel: label
59996
+ });
59997
+ requireDatabricksAssetBundleMap(assetBundles);
59998
+ return databricksAssetBundleLifecycles(
59999
+ // Cast at the boundary into the validating helper: these are unvalidated config candidates, and
60000
+ // each entry's `bundleDir` is enforced there rather than trusted here.
60001
+ Object.fromEntries(
60002
+ Object.entries(assetBundles).map(([name, entry]) => [name, assetBundleEntryOptions(entry)])
60003
+ ),
60004
+ { principalLabel: label }
60005
+ );
60006
+ }
60007
+ function assetBundleEntryOptions(entry) {
60008
+ if (entry === null || typeof entry !== "object") return {};
60009
+ const { bundleDir, target, store } = entry;
60010
+ return {
60011
+ ...bundleDir !== void 0 ? { bundleDir } : {},
60012
+ ...target !== void 0 ? { target } : {},
60013
+ ...store !== void 0 ? { store } : {}
60014
+ };
60015
+ }
59615
60016
  function enabledAuthoringServices(config2, genie = normalizeGenieConfig(config2)) {
59616
60017
  return [
59617
60018
  .../* @__PURE__ */ new Set([
@@ -59641,11 +60042,9 @@ function validateAuthoringConfig(config2, genie = normalizeGenieConfig(config2))
59641
60042
  if (config2.jobAuthoring || config2.oneOffCompute)
59642
60043
  validateDatabricksComputePolicy(config2.computePolicy ?? {});
59643
60044
  const audience = config2.governance?.stewardAudience;
59644
- const excluded = config2.governance?.approvalServices ? services.filter((service) => !config2.governance?.approvalServices?.includes(service)) : [];
59645
- if (!config2.allowUnapprovedAuthoring && (!audience || excluded.length > 0)) {
59646
- const ungated = !audience ? services : excluded;
60045
+ if (!config2.allowUnapprovedAuthoring && !audience) {
59647
60046
  throw new Error(
59648
- `databricks: authoring services are not approval-gated: ${ungated.join(", ")}. Configure governance.stewardAudience${excluded.length ? " and include every service in governance.approvalServices" : ""}, or explicitly set allowUnapprovedAuthoring: true.`
60047
+ `databricks: authoring services are not approval-gated: ${services.join(", ")}. Configure governance.stewardAudience, or explicitly set allowUnapprovedAuthoring: true.`
59649
60048
  );
59650
60049
  }
59651
60050
  }
@@ -59739,7 +60138,8 @@ function defineDatabricksAgent(options = {}) {
59739
60138
  ...directTools.length > 0 ? { tools: directTools } : {},
59740
60139
  run: async (context) => {
59741
60140
  const env2 = context.env ?? {};
59742
- const config2 = explicitConfig ?? databricksConfigFromEnv(env2, { analyticsCopilot });
60141
+ const baseConfig = explicitConfig ?? databricksConfigFromEnv(env2, { analyticsCopilot });
60142
+ const config2 = baseConfig.warehouseId && !baseConfig.sqlRead && toolRefs.some((ref) => ref === "sql" || ref === "sql-read" || ref === "sql_read") ? { ...baseConfig, sqlRead: true } : baseConfig;
59743
60143
  const bundle = config2.mcp?.length ? await databricksWithManagedMcp(config2) : databricks(config2);
59744
60144
  try {
59745
60145
  const analyticsCostLimit = analyticsCopilot ? databricksAnalyticsCostLimitFromEnv(env2, config2, bundle) : void 0;
@@ -59840,7 +60240,7 @@ function resolveToolRefs(bundle, refs) {
59840
60240
  toolMap.set(tool7.name, tool7);
59841
60241
  }
59842
60242
  const shortNameMap = {
59843
- sql: "databricks_sql",
60243
+ sql: "sql_read",
59844
60244
  "sql-read": "sql_read",
59845
60245
  tables: "databricks_unity_catalog_tables",
59846
60246
  "table-info": "databricks_table_info",