@fabric-harness/databricks 4.4.1 → 4.6.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
@@ -1799,6 +1799,340 @@ import {
1799
1799
  mergeCapabilityPolicies
1800
1800
  } from "@fabric-harness/sdk/strict";
1801
1801
 
1802
+ // src/governance.ts
1803
+ import { currentSubmissionContext, redactJson, sameApprovalOperation } from "@fabric-harness/sdk";
1804
+ function metaString(tool7, key) {
1805
+ const value = tool7.metadata?.[key];
1806
+ return typeof value === "string" ? value : void 0;
1807
+ }
1808
+ function inputString(input, key) {
1809
+ if (input && typeof input === "object" && key in input) {
1810
+ const value = input[key];
1811
+ if (typeof value === "string") return value;
1812
+ }
1813
+ return void 0;
1814
+ }
1815
+ function governedObject(input) {
1816
+ for (const key of [
1817
+ "fullName",
1818
+ "table",
1819
+ "index",
1820
+ "pipelineId",
1821
+ "jobId",
1822
+ "endpoint",
1823
+ "notebookPath"
1824
+ ]) {
1825
+ const value = inputString(input, key);
1826
+ if (value) return value;
1827
+ }
1828
+ const catalog = inputString(input, "catalog");
1829
+ const schema = inputString(input, "schema");
1830
+ return catalog ? [catalog, schema].filter(Boolean).join(".") : void 0;
1831
+ }
1832
+ function catalogFromInput(input) {
1833
+ const explicit = inputString(input, "catalog");
1834
+ if (explicit) return explicit;
1835
+ const object2 = governedObject(input);
1836
+ return object2?.includes(".") ? object2.split(".")[0] : void 0;
1837
+ }
1838
+ function governanceMetadata(tool7) {
1839
+ const value = tool7.metadata?.governance;
1840
+ if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
1841
+ const resources = value.resources;
1842
+ if (!Array.isArray(resources)) return void 0;
1843
+ return {
1844
+ resources: resources.map(
1845
+ (resource, index) => parseResourceDescriptor(resource, tool7.name, index)
1846
+ ),
1847
+ ...value.secretFields !== void 0 ? {
1848
+ secretFields: parseStringArray(
1849
+ value.secretFields,
1850
+ `${tool7.name} governance.secretFields`
1851
+ )
1852
+ } : {}
1853
+ };
1854
+ }
1855
+ function validateDatabricksGovernanceDescriptor(tool7, required2 = false) {
1856
+ const metadata = governanceMetadata(tool7);
1857
+ if (!metadata) {
1858
+ if (required2)
1859
+ throw new Error(
1860
+ `databricks governance: authoring tool "${tool7.name}" must declare metadata.governance.resources.`
1861
+ );
1862
+ return;
1863
+ }
1864
+ if (metadata.resources.length === 0)
1865
+ throw new Error(`databricks governance: tool "${tool7.name}" declares no governed resources.`);
1866
+ if (!tool7.inputSchema || typeof tool7.inputSchema !== "object")
1867
+ throw new Error(
1868
+ `databricks governance: tool "${tool7.name}" needs an inputSchema for resource validation.`
1869
+ );
1870
+ for (const descriptor of metadata.resources)
1871
+ validateSchemaPath(tool7.name, tool7.inputSchema, descriptor);
1872
+ }
1873
+ function extractDatabricksGovernedResources(tool7, input) {
1874
+ const metadata = governanceMetadata(tool7);
1875
+ if (!metadata) return legacyResources(input);
1876
+ const resources = [];
1877
+ for (const descriptor of metadata.resources) {
1878
+ const values = pointerValues(input, descriptor.path);
1879
+ if (values.length === 0 && !descriptor.optional) {
1880
+ throw new Error(
1881
+ `databricks governance: required resource path "${descriptor.path}" matched no values for tool "${tool7.name}".`
1882
+ );
1883
+ }
1884
+ for (const value of values) {
1885
+ if (typeof value !== "string" && typeof value !== "number") {
1886
+ throw new Error(
1887
+ `databricks governance: resource path "${descriptor.path}" on tool "${tool7.name}" must resolve to strings or numbers.`
1888
+ );
1889
+ }
1890
+ const text = String(value);
1891
+ if (!text)
1892
+ throw new Error(
1893
+ `databricks governance: resource path "${descriptor.path}" on tool "${tool7.name}" resolved to an empty value.`
1894
+ );
1895
+ resources.push({
1896
+ kind: descriptor.kind,
1897
+ value: text,
1898
+ path: descriptor.path,
1899
+ ...descriptor.catalogQualified && (descriptor.kind === "catalog" || text.includes(".")) ? { catalog: descriptor.kind === "catalog" ? text : text.split(".")[0] } : {}
1900
+ });
1901
+ }
1902
+ }
1903
+ return resources;
1904
+ }
1905
+ function withGovernance(tool7, options = {}) {
1906
+ const inner = tool7.execute;
1907
+ if (!inner) return tool7;
1908
+ validateDatabricksGovernanceDescriptor(tool7);
1909
+ const now = options.now ?? (() => /* @__PURE__ */ new Date());
1910
+ return {
1911
+ ...tool7,
1912
+ metadata: { ...tool7.metadata ?? {}, governed: true },
1913
+ async execute(input, context) {
1914
+ const resources = extractDatabricksGovernedResources(tool7, input);
1915
+ const catalogs = [
1916
+ ...new Set(resources.flatMap((resource) => resource.catalog ? [resource.catalog] : []))
1917
+ ];
1918
+ const catalog = catalogs[0] ?? catalogFromInput(input);
1919
+ const schema = inputString(input, "schema");
1920
+ const object2 = governedObject(input);
1921
+ const startedAt = now().toISOString();
1922
+ const service = metaString(tool7, "service");
1923
+ const provider = metaString(tool7, "provider");
1924
+ const operation = metaString(tool7, "effect");
1925
+ const submission = currentSubmissionContext();
1926
+ const principal = options.principal ?? options.executingPrincipal?.ucPrincipal ?? options.executingPrincipal?.id ?? submission?.actor?.principal?.id;
1927
+ const base2 = {
1928
+ tool: tool7.name,
1929
+ ...service !== void 0 ? { service } : {},
1930
+ ...provider !== void 0 ? { provider } : {},
1931
+ ...principal !== void 0 ? { principal } : {},
1932
+ ...submission ? {
1933
+ submissionId: submission.submissionId,
1934
+ attemptId: submission.attemptId,
1935
+ ...submission.tenantId !== void 0 ? { tenantId: submission.tenantId } : {}
1936
+ } : {},
1937
+ ...catalog !== void 0 ? { catalog } : {},
1938
+ ...schema !== void 0 ? { schema } : {},
1939
+ ...object2 !== void 0 ? { object: object2 } : {},
1940
+ ...resources.length > 0 ? { resources } : {},
1941
+ ...context?.approval?.approvers.length ? { approvers: context.approval.approvers } : {},
1942
+ ...operation !== void 0 ? { operation } : {},
1943
+ // Redact secret-ish keys before the input enters the audit/lineage record.
1944
+ input: redactToolInput(input, governanceMetadata(tool7)?.secretFields),
1945
+ startedAt
1946
+ };
1947
+ const deniedCatalog = options.catalogs ? catalogs.find((candidate) => !options.catalogs?.includes(candidate)) : void 0;
1948
+ if (options.catalogs && deniedCatalog) {
1949
+ const error51 = `databricks governance: catalog "${deniedCatalog}" is not in the allowed list (${options.catalogs.join(", ")}).`;
1950
+ options.onLineage?.({ ...base2, finishedAt: now().toISOString(), ok: false, error: error51 });
1951
+ throw new Error(error51);
1952
+ }
1953
+ try {
1954
+ if (service && (operation === "write" || operation === "execute") && (options.requireApprovalGrantFor?.includes(service) || options.requireApprovalGrantForTools?.includes(tool7.name))) {
1955
+ if (!options.executingPrincipal)
1956
+ throw new Error(
1957
+ `databricks governance: service "${service}" requires a verified executing principal.`
1958
+ );
1959
+ if (!context?.approval || !context.toolCallId || !sameApprovalOperation(context.approval, {
1960
+ toolCallId: context.toolCallId,
1961
+ toolInput: input,
1962
+ principal: options.executingPrincipal
1963
+ })) {
1964
+ throw new Error(
1965
+ `databricks governance: tool "${tool7.name}" requires an ApprovalGrant bound to this call, input, and executing principal.`
1966
+ );
1967
+ }
1968
+ }
1969
+ const result = await inner(input, context);
1970
+ options.onLineage?.({ ...base2, finishedAt: now().toISOString(), ok: true });
1971
+ return result;
1972
+ } catch (error51) {
1973
+ options.onLineage?.({
1974
+ ...base2,
1975
+ finishedAt: now().toISOString(),
1976
+ ok: false,
1977
+ error: error51 instanceof Error ? error51.message : String(error51)
1978
+ });
1979
+ throw error51;
1980
+ }
1981
+ }
1982
+ };
1983
+ }
1984
+ function parseResourceDescriptor(value, toolName, index) {
1985
+ if (!value || typeof value !== "object" || Array.isArray(value))
1986
+ throw new Error(
1987
+ `databricks governance: invalid resource descriptor ${index} on tool "${toolName}".`
1988
+ );
1989
+ const row = value;
1990
+ if (typeof row.kind !== "string" || !row.kind || typeof row.path !== "string" || !row.path.startsWith("/")) {
1991
+ throw new Error(
1992
+ `databricks governance: descriptor ${index} on tool "${toolName}" requires kind and an absolute path.`
1993
+ );
1994
+ }
1995
+ return {
1996
+ kind: row.kind,
1997
+ path: row.path,
1998
+ ...row.catalogQualified === true ? { catalogQualified: true } : {},
1999
+ ...row.optional === true ? { optional: true } : {}
2000
+ };
2001
+ }
2002
+ function parseStringArray(value, label) {
2003
+ if (!Array.isArray(value) || value.some((item) => typeof item !== "string"))
2004
+ throw new Error(`databricks governance: ${label} must be a string array.`);
2005
+ return value;
2006
+ }
2007
+ function pointerSegments(path) {
2008
+ return path.slice(1).split("/").map((segment) => segment.replace(/~1/g, "/").replace(/~0/g, "~"));
2009
+ }
2010
+ function pointerValues(input, path) {
2011
+ let current = [input];
2012
+ for (const segment of pointerSegments(path)) {
2013
+ current = current.flatMap((value) => {
2014
+ if (segment === "*") return Array.isArray(value) ? value : [];
2015
+ if (!value || typeof value !== "object" || Array.isArray(value)) return [];
2016
+ return segment in value ? [value[segment]] : [];
2017
+ }).filter((value) => value !== void 0 && value !== null);
2018
+ }
2019
+ return current;
2020
+ }
2021
+ function validateSchemaPath(toolName, schema, descriptor) {
2022
+ let current = [schema];
2023
+ for (const segment of pointerSegments(descriptor.path)) {
2024
+ const variants = current.flatMap(schemaVariants);
2025
+ const next = [];
2026
+ let missing = 0;
2027
+ for (const variant of variants) {
2028
+ if (segment === "*") {
2029
+ const items = variant.items;
2030
+ if (variant.type === "array" && items && typeof items === "object" && !Array.isArray(items))
2031
+ next.push(items);
2032
+ else missing += 1;
2033
+ continue;
2034
+ }
2035
+ const properties = variant.properties;
2036
+ const child = properties && typeof properties === "object" && !Array.isArray(properties) ? properties[segment] : void 0;
2037
+ if (child && typeof child === "object" && !Array.isArray(child))
2038
+ next.push(child);
2039
+ else missing += 1;
2040
+ }
2041
+ if (next.length === 0)
2042
+ throw new Error(
2043
+ `databricks governance: path "${descriptor.path}" does not exist in inputSchema for tool "${toolName}".`
2044
+ );
2045
+ if (!descriptor.optional && missing > 0)
2046
+ throw new Error(
2047
+ `databricks governance: required path "${descriptor.path}" is not present in every inputSchema branch for tool "${toolName}". Mark the descriptor optional or make the schema branch mandatory.`
2048
+ );
2049
+ current = next;
2050
+ }
2051
+ }
2052
+ function schemaVariants(schema) {
2053
+ const variants = Array.isArray(schema.oneOf) ? schema.oneOf : Array.isArray(schema.anyOf) ? schema.anyOf : void 0;
2054
+ if (!variants) return [schema];
2055
+ return variants.filter(
2056
+ (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value)
2057
+ );
2058
+ }
2059
+ function legacyResources(input) {
2060
+ const value = governedObject(input);
2061
+ if (!value) return [];
2062
+ const catalog = catalogFromInput(input);
2063
+ return [{ kind: "object", value, path: "legacy-heuristic", ...catalog ? { catalog } : {} }];
2064
+ }
2065
+ function redactToolInput(input, secretFields) {
2066
+ const clone2 = redactNamedFields(input ?? {}, new Set(secretFields ?? []));
2067
+ return redactJson(clone2);
2068
+ }
2069
+ function redactNamedFields(value, names) {
2070
+ if (Array.isArray(value)) return value.map((item) => redactNamedFields(item, names));
2071
+ if (!value || typeof value !== "object") return value;
2072
+ return Object.fromEntries(
2073
+ Object.entries(value).map(([key, child]) => [
2074
+ key,
2075
+ names.has(key) ? "[REDACTED]" : redactNamedFields(child, names)
2076
+ ])
2077
+ );
2078
+ }
2079
+ function withGovernanceTools(tools, options = {}) {
2080
+ return tools.map((tool7) => withGovernance(tool7, options));
2081
+ }
2082
+ function shouldGate(tool7, services) {
2083
+ const effect = tool7.metadata?.effect;
2084
+ if (effect !== "write" && effect !== "execute") return false;
2085
+ return !services || services.includes(metaString(tool7, "service") ?? "");
2086
+ }
2087
+ function databricksApprovalRules(tools, options) {
2088
+ return tools.filter((tool7) => shouldGate(tool7, options.services)).map((tool7) => ({
2089
+ pattern: tool7.name,
2090
+ audience: options.audience,
2091
+ // ${name} is templated by the policy layer at request time — keep it literal here.
2092
+ reason: options.reason ?? "Databricks ${name} requires data-steward approval.",
2093
+ ...options.ttlSeconds !== void 0 ? { ttlSeconds: options.ttlSeconds } : {}
2094
+ }));
2095
+ }
2096
+ function analyticsCopilotGovernance(options) {
2097
+ const stewardAudience = options.stewardAudience.trim();
2098
+ if (!stewardAudience)
2099
+ throw new Error("databricks analytics copilot governance requires a steward audience.");
2100
+ return {
2101
+ stewardAudience,
2102
+ approvalServices: ["sql", "genie"],
2103
+ ...options.catalogs ? { catalogs: [...options.catalogs] } : {},
2104
+ ...options.approvalTtlSeconds !== void 0 ? { approvalTtlSeconds: options.approvalTtlSeconds } : {},
2105
+ ...options.principalLabel ? { principalLabel: options.principalLabel } : {},
2106
+ ...options.onLineage ? { onLineage: options.onLineage } : {}
2107
+ };
2108
+ }
2109
+ function toHostPatterns(host) {
2110
+ if (!host) return [];
2111
+ const list = Array.isArray(host) ? host : [host];
2112
+ return list.map((entry) => {
2113
+ try {
2114
+ return new URL(entry.includes("://") ? entry : `https://${entry}`).host;
2115
+ } catch {
2116
+ return entry;
2117
+ }
2118
+ });
2119
+ }
2120
+ function databricksGovernancePolicy(options) {
2121
+ const policy = {};
2122
+ if (options.stewardAudience) {
2123
+ policy.toolPolicy = {
2124
+ approvalRules: databricksApprovalRules(options.tools, {
2125
+ audience: options.stewardAudience,
2126
+ ...options.approvalServices ? { services: options.approvalServices } : {},
2127
+ ...options.ttlSeconds !== void 0 ? { ttlSeconds: options.ttlSeconds } : {}
2128
+ })
2129
+ };
2130
+ }
2131
+ const hosts = toHostPatterns(options.host);
2132
+ if (hosts.length > 0) policy.network = { mode: "allowlist", hosts };
2133
+ return policy;
2134
+ }
2135
+
1802
2136
  // ../../node_modules/.pnpm/@databricks+sdk-auth@0.21.0/node_modules/@databricks/sdk-auth/package.json
1803
2137
  var package_default = {
1804
2138
  name: "@databricks/sdk-auth",
@@ -17263,6 +17597,295 @@ function fabricPrincipalFor(principal, overrides = {}) {
17263
17597
  };
17264
17598
  }
17265
17599
 
17600
+ // src/cost-budget-actuals.ts
17601
+ import { parseScopeKey, tenantCostLimit } from "@fabric-harness/sdk";
17602
+
17603
+ // src/sql-exec.ts
17604
+ var TERMINAL_STATES = /* @__PURE__ */ new Set(["SUCCEEDED", "FAILED", "CANCELED", "CLOSED"]);
17605
+ async function runStatement(client, warehouseId, statement, parameters, poll) {
17606
+ const submit = await client.executeStatement({
17607
+ warehouseId,
17608
+ statement,
17609
+ waitTimeout: "30s",
17610
+ disposition: "INLINE",
17611
+ ...parameters.length > 0 ? { parameters } : {}
17612
+ });
17613
+ if (TERMINAL_STATES.has(submit.status?.state ?? "")) return submit;
17614
+ const id = submit.statementId;
17615
+ if (!id) return submit;
17616
+ const deadline = Date.now() + poll.timeoutMs;
17617
+ let current = submit;
17618
+ while (!TERMINAL_STATES.has(current.status?.state ?? "") && Date.now() < deadline) {
17619
+ await new Promise((resolve2) => setTimeout(resolve2, poll.intervalMs));
17620
+ current = await client.getStatementResult({ statementId: id });
17621
+ }
17622
+ return current;
17623
+ }
17624
+ function parseStatementRows(response) {
17625
+ const res = response;
17626
+ const columns = res.manifest?.schema?.columns ?? [];
17627
+ const names = columns.map((c2) => c2.name ?? "");
17628
+ const rows = res.result?.dataArray ?? [];
17629
+ return rows.map((row) => {
17630
+ const record2 = {};
17631
+ names.forEach((name, i2) => {
17632
+ record2[name] = row[i2];
17633
+ });
17634
+ return record2;
17635
+ });
17636
+ }
17637
+
17638
+ // src/cost-budget-actuals.ts
17639
+ function toNumber(value) {
17640
+ const n2 = typeof value === "number" ? value : Number(value);
17641
+ return Number.isFinite(n2) ? n2 : 0;
17642
+ }
17643
+ function deriveDateWindow(parsed) {
17644
+ const period = parsed.period;
17645
+ if (!period) return void 0;
17646
+ const parts = period.split(":");
17647
+ const periodType = parts[0];
17648
+ const periodValue = parts.slice(1).join(":");
17649
+ if (!periodValue) return void 0;
17650
+ switch (periodType) {
17651
+ case "day": {
17652
+ return { startDate: periodValue, endDate: periodValue };
17653
+ }
17654
+ case "hour": {
17655
+ const date5 = periodValue.match(/^(\d{4}-\d{2}-\d{2})/)?.[1];
17656
+ return date5 ? { startDate: date5, endDate: date5 } : void 0;
17657
+ }
17658
+ case "month": {
17659
+ const [year, month] = periodValue.split("-");
17660
+ if (!year || !month) return void 0;
17661
+ const lastDay = new Date(Number(year), Number(month), 0).getDate();
17662
+ return {
17663
+ startDate: `${year}-${month}-01`,
17664
+ endDate: `${year}-${month}-${String(lastDay).padStart(2, "0")}`
17665
+ };
17666
+ }
17667
+ default:
17668
+ return void 0;
17669
+ }
17670
+ }
17671
+ function buildCostQuery(tagPrefix, attribution) {
17672
+ const tagAgent = `${tagPrefix}.agent`;
17673
+ const tagTenant = `${tagPrefix}.tenant`;
17674
+ const tagUser = `${tagPrefix}.user`;
17675
+ const conditions = [
17676
+ "u.usage_date >= :start_date AND u.usage_date <= :end_date"
17677
+ ];
17678
+ if (attribution?.agentId) {
17679
+ conditions.push(`u.tags['${tagAgent}'] = :agent_id`);
17680
+ }
17681
+ if (attribution?.tenantId) {
17682
+ conditions.push(`u.tags['${tagTenant}'] = :tenant_id`);
17683
+ }
17684
+ if (attribution?.userId) {
17685
+ conditions.push(`u.tags['${tagUser}'] = :user_id`);
17686
+ }
17687
+ return [
17688
+ "SELECT",
17689
+ " SUM(u.usage_quantity * lp.pricing.default) AS list_cost_usd,",
17690
+ " SUM(u.usage_quantity) AS quantity",
17691
+ "FROM system.billing.usage u",
17692
+ "LEFT JOIN system.billing.list_prices lp",
17693
+ " ON u.cloud = lp.cloud AND u.sku_name = lp.sku_name AND u.usage_unit = lp.usage_unit",
17694
+ " AND u.usage_end_time >= lp.price_start_time",
17695
+ " AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)",
17696
+ `WHERE ${conditions.join(" AND ")}`
17697
+ ].join("\n");
17698
+ }
17699
+ function buildAttributionQuery(tagPrefix, filters) {
17700
+ const tagAgent = `${tagPrefix}.agent`;
17701
+ const tagTenant = `${tagPrefix}.tenant`;
17702
+ const tagUser = `${tagPrefix}.user`;
17703
+ const conditions = [
17704
+ "u.usage_date >= :start_date AND u.usage_date <= :end_date"
17705
+ ];
17706
+ const parameters = [
17707
+ { name: "start_date", value: filters.startDate ?? "", type: "DATE" },
17708
+ { name: "end_date", value: filters.endDate ?? "", type: "DATE" }
17709
+ ];
17710
+ if (filters.agentId) {
17711
+ conditions.push(`u.tags['${tagAgent}'] = :agent_id`);
17712
+ parameters.push({ name: "agent_id", value: filters.agentId });
17713
+ }
17714
+ if (filters.tenantId) {
17715
+ conditions.push(`u.tags['${tagTenant}'] = :tenant_id`);
17716
+ parameters.push({ name: "tenant_id", value: filters.tenantId });
17717
+ }
17718
+ if (filters.userId) {
17719
+ conditions.push(`u.tags['${tagUser}'] = :user_id`);
17720
+ parameters.push({ name: "user_id", value: filters.userId });
17721
+ }
17722
+ if (filters.model) {
17723
+ conditions.push("u.sku_name = :model");
17724
+ parameters.push({ name: "model", value: filters.model });
17725
+ }
17726
+ if (filters.provider) {
17727
+ conditions.push("u.cloud = :provider");
17728
+ parameters.push({ name: "provider", value: filters.provider });
17729
+ }
17730
+ const groupBy = filters.groupBy ?? ["agentId", "tenantId", "userId", "model", "provider"];
17731
+ const groupColumns = [];
17732
+ const selectColumns = [];
17733
+ for (const dim of groupBy) {
17734
+ switch (dim) {
17735
+ case "agentId":
17736
+ selectColumns.push(`u.tags['${tagAgent}'] AS agentId`);
17737
+ groupColumns.push(`u.tags['${tagAgent}']`);
17738
+ break;
17739
+ case "tenantId":
17740
+ selectColumns.push(`u.tags['${tagTenant}'] AS tenantId`);
17741
+ groupColumns.push(`u.tags['${tagTenant}']`);
17742
+ break;
17743
+ case "userId":
17744
+ selectColumns.push(`u.tags['${tagUser}'] AS userId`);
17745
+ groupColumns.push(`u.tags['${tagUser}']`);
17746
+ break;
17747
+ case "model":
17748
+ selectColumns.push("u.sku_name AS model");
17749
+ groupColumns.push("u.sku_name");
17750
+ break;
17751
+ case "provider":
17752
+ selectColumns.push("u.cloud AS provider");
17753
+ groupColumns.push("u.cloud");
17754
+ break;
17755
+ case "sessionId":
17756
+ break;
17757
+ case "turnId":
17758
+ break;
17759
+ }
17760
+ }
17761
+ selectColumns.push(
17762
+ "CONCAT('day:', u.usage_date) AS period",
17763
+ "SUM(u.usage_quantity * lp.pricing.default) AS actualUsd",
17764
+ "SUM(u.usage_quantity) AS actualDbus",
17765
+ "COUNT(*) AS calls"
17766
+ );
17767
+ groupColumns.push("CONCAT('day:', u.usage_date)");
17768
+ const query = [
17769
+ "SELECT",
17770
+ ` ${selectColumns.join(",\n ")}`,
17771
+ "FROM system.billing.usage u",
17772
+ "LEFT JOIN system.billing.list_prices lp",
17773
+ " ON u.cloud = lp.cloud AND u.sku_name = lp.sku_name AND u.usage_unit = lp.usage_unit",
17774
+ " AND u.usage_end_time >= lp.price_start_time",
17775
+ " AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)",
17776
+ `WHERE ${conditions.join(" AND ")}`,
17777
+ `GROUP BY ${groupColumns.join(", ")}`,
17778
+ "ORDER BY actualUsd DESC"
17779
+ ].join("\n");
17780
+ return { query, parameters };
17781
+ }
17782
+ function databricksActualCostSource(client, warehouseId, options = {}) {
17783
+ const cacheTtlMs = options.cacheTtlMs ?? 6e4;
17784
+ const tagPrefix = options.tagPrefix ?? "fabric";
17785
+ const poll = options.poll ?? { intervalMs: 2e3, timeoutMs: 12e4 };
17786
+ const cache = /* @__PURE__ */ new Map();
17787
+ return {
17788
+ async getUsd(scopeKey, attribution, queryOptions) {
17789
+ const ttl = queryOptions?.cacheTtlMs ?? cacheTtlMs;
17790
+ const now = Date.now();
17791
+ const cached4 = cache.get(scopeKey);
17792
+ if (cached4 && cached4.expiresAt > now) {
17793
+ const result2 = {
17794
+ usd: cached4.usd
17795
+ };
17796
+ if (cached4.dbus !== void 0) result2.dbus = cached4.dbus;
17797
+ result2.actualsFetchedAt = cached4.fetchedAt;
17798
+ result2.cacheTtlMs = ttl;
17799
+ return result2;
17800
+ }
17801
+ const parsed = parseScopeKey(scopeKey);
17802
+ if (!parsed) {
17803
+ return { usd: 0, cacheTtlMs: ttl };
17804
+ }
17805
+ const dateWindow = deriveDateWindow(parsed);
17806
+ if (!dateWindow) {
17807
+ return { usd: 0, cacheTtlMs: ttl };
17808
+ }
17809
+ const parameters = [
17810
+ { name: "start_date", value: dateWindow.startDate, type: "DATE" },
17811
+ { name: "end_date", value: dateWindow.endDate, type: "DATE" }
17812
+ ];
17813
+ if (attribution?.agentId) {
17814
+ parameters.push({ name: "agent_id", value: attribution.agentId });
17815
+ }
17816
+ if (attribution?.tenantId) {
17817
+ parameters.push({ name: "tenant_id", value: attribution.tenantId });
17818
+ }
17819
+ if (attribution?.userId) {
17820
+ parameters.push({ name: "user_id", value: attribution.userId });
17821
+ }
17822
+ const response = await runStatement(
17823
+ client,
17824
+ warehouseId,
17825
+ buildCostQuery(tagPrefix, attribution),
17826
+ parameters,
17827
+ poll
17828
+ );
17829
+ const rows = parseStatementRows(response);
17830
+ const usd = rows.reduce((sum, r2) => sum + toNumber(r2.list_cost_usd), 0);
17831
+ const dbus = rows.reduce((sum, r2) => sum + toNumber(r2.quantity), 0);
17832
+ const fetchedAt = (/* @__PURE__ */ new Date()).toISOString();
17833
+ cache.set(scopeKey, {
17834
+ usd,
17835
+ dbus: dbus || void 0,
17836
+ fetchedAt,
17837
+ expiresAt: now + ttl
17838
+ });
17839
+ const result = {
17840
+ usd
17841
+ };
17842
+ if (dbus) result.dbus = dbus;
17843
+ result.actualsFetchedAt = fetchedAt;
17844
+ result.cacheTtlMs = ttl;
17845
+ return result;
17846
+ },
17847
+ async queryAttribution(filters) {
17848
+ if (!filters.startDate || !filters.endDate) {
17849
+ throw new Error("queryAttribution requires startDate and endDate");
17850
+ }
17851
+ const { query, parameters } = buildAttributionQuery(tagPrefix, filters);
17852
+ const response = await runStatement(client, warehouseId, query, parameters, poll);
17853
+ const rows = parseStatementRows(response);
17854
+ return rows.map((r2) => {
17855
+ const row = {
17856
+ period: String(r2.period ?? ""),
17857
+ estimatedUsd: 0,
17858
+ actualUsd: toNumber(r2.actualUsd),
17859
+ actualDbus: toNumber(r2.actualDbus),
17860
+ deltaUsd: toNumber(r2.actualUsd),
17861
+ calls: toNumber(r2.calls)
17862
+ };
17863
+ if (r2.agentId) row.agentId = String(r2.agentId);
17864
+ if (r2.userId) row.userId = String(r2.userId);
17865
+ if (r2.tenantId) row.tenantId = String(r2.tenantId);
17866
+ if (r2.model) row.model = String(r2.model);
17867
+ if (r2.provider) row.provider = String(r2.provider);
17868
+ return row;
17869
+ });
17870
+ }
17871
+ };
17872
+ }
17873
+ function databricksTenantCostLimit(client, warehouseId, tenantId, options) {
17874
+ const base2 = tenantCostLimit(tenantId, {
17875
+ ...options.perHourUsd !== void 0 ? { perHourUsd: options.perHourUsd } : {},
17876
+ ...options.perDayUsd !== void 0 ? { perDayUsd: options.perDayUsd } : {},
17877
+ ...options.perMonthUsd !== void 0 ? { perMonthUsd: options.perMonthUsd } : {},
17878
+ ...options.onExceed ? { onExceed: options.onExceed } : {}
17879
+ });
17880
+ const opts = {};
17881
+ if (options.cacheTtlMs !== void 0) opts.cacheTtlMs = options.cacheTtlMs;
17882
+ return {
17883
+ ...base2,
17884
+ scopeSource: "external",
17885
+ actualSource: databricksActualCostSource(client, warehouseId, opts)
17886
+ };
17887
+ }
17888
+
17266
17889
  // src/index.ts
17267
17890
  import { createSearchTool } from "@fabric-harness/sdk";
17268
17891
 
@@ -17713,41 +18336,6 @@ function jitter(ms) {
17713
18336
  return Math.max(1, Math.round(ms * (0.9 + Math.random() * 0.2)));
17714
18337
  }
17715
18338
 
17716
- // src/sql-exec.ts
17717
- var TERMINAL_STATES = /* @__PURE__ */ new Set(["SUCCEEDED", "FAILED", "CANCELED", "CLOSED"]);
17718
- async function runStatement(client, warehouseId, statement, parameters, poll) {
17719
- const submit = await client.executeStatement({
17720
- warehouseId,
17721
- statement,
17722
- waitTimeout: "30s",
17723
- disposition: "INLINE",
17724
- ...parameters.length > 0 ? { parameters } : {}
17725
- });
17726
- if (TERMINAL_STATES.has(submit.status?.state ?? "")) return submit;
17727
- const id = submit.statementId;
17728
- if (!id) return submit;
17729
- const deadline = Date.now() + poll.timeoutMs;
17730
- let current = submit;
17731
- while (!TERMINAL_STATES.has(current.status?.state ?? "") && Date.now() < deadline) {
17732
- await new Promise((resolve2) => setTimeout(resolve2, poll.intervalMs));
17733
- current = await client.getStatementResult({ statementId: id });
17734
- }
17735
- return current;
17736
- }
17737
- function parseStatementRows(response) {
17738
- const res = response;
17739
- const columns = res.manifest?.schema?.columns ?? [];
17740
- const names = columns.map((c2) => c2.name ?? "");
17741
- const rows = res.result?.dataArray ?? [];
17742
- return rows.map((row) => {
17743
- const record2 = {};
17744
- names.forEach((name, i2) => {
17745
- record2[name] = row[i2];
17746
- });
17747
- return record2;
17748
- });
17749
- }
17750
-
17751
18339
  // src/ai.ts
17752
18340
  function databricksAiQueryTool(client, options) {
17753
18341
  const poll = { intervalMs: options.pollIntervalMs ?? 2e3, timeoutMs: options.timeoutMs ?? 12e4 };
@@ -17792,7 +18380,7 @@ function buildQuery(groupBy) {
17792
18380
  "ORDER BY list_cost_usd DESC"
17793
18381
  ].join("\n");
17794
18382
  }
17795
- function toNumber(value) {
18383
+ function toNumber2(value) {
17796
18384
  const n2 = typeof value === "number" ? value : Number(value);
17797
18385
  return Number.isFinite(n2) ? n2 : 0;
17798
18386
  }
@@ -17817,8 +18405,8 @@ function databricksConsumption(client, options) {
17817
18405
  endDate: opts.endDate,
17818
18406
  groupBy,
17819
18407
  rows,
17820
- totalDbus: rows.reduce((sum, r2) => sum + toNumber(r2.quantity), 0),
17821
- totalListCostUsd: rows.reduce((sum, r2) => sum + toNumber(r2.list_cost_usd), 0)
18408
+ totalDbus: rows.reduce((sum, r2) => sum + toNumber2(r2.quantity), 0),
18409
+ totalListCostUsd: rows.reduce((sum, r2) => sum + toNumber2(r2.list_cost_usd), 0)
17822
18410
  };
17823
18411
  }
17824
18412
  };
@@ -19956,327 +20544,6 @@ function snakeValue2(value) {
19956
20544
  return value;
19957
20545
  }
19958
20546
 
19959
- // src/governance.ts
19960
- import { currentSubmissionContext, redactJson, sameApprovalOperation } from "@fabric-harness/sdk";
19961
- function metaString(tool7, key) {
19962
- const value = tool7.metadata?.[key];
19963
- return typeof value === "string" ? value : void 0;
19964
- }
19965
- function inputString(input, key) {
19966
- if (input && typeof input === "object" && key in input) {
19967
- const value = input[key];
19968
- if (typeof value === "string") return value;
19969
- }
19970
- return void 0;
19971
- }
19972
- function governedObject(input) {
19973
- for (const key of [
19974
- "fullName",
19975
- "table",
19976
- "index",
19977
- "pipelineId",
19978
- "jobId",
19979
- "endpoint",
19980
- "notebookPath"
19981
- ]) {
19982
- const value = inputString(input, key);
19983
- if (value) return value;
19984
- }
19985
- const catalog = inputString(input, "catalog");
19986
- const schema = inputString(input, "schema");
19987
- return catalog ? [catalog, schema].filter(Boolean).join(".") : void 0;
19988
- }
19989
- function catalogFromInput(input) {
19990
- const explicit = inputString(input, "catalog");
19991
- if (explicit) return explicit;
19992
- const object2 = governedObject(input);
19993
- return object2?.includes(".") ? object2.split(".")[0] : void 0;
19994
- }
19995
- function governanceMetadata(tool7) {
19996
- const value = tool7.metadata?.governance;
19997
- if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
19998
- const resources = value.resources;
19999
- if (!Array.isArray(resources)) return void 0;
20000
- return {
20001
- resources: resources.map(
20002
- (resource, index) => parseResourceDescriptor(resource, tool7.name, index)
20003
- ),
20004
- ...value.secretFields !== void 0 ? {
20005
- secretFields: parseStringArray(
20006
- value.secretFields,
20007
- `${tool7.name} governance.secretFields`
20008
- )
20009
- } : {}
20010
- };
20011
- }
20012
- function validateDatabricksGovernanceDescriptor(tool7, required2 = false) {
20013
- const metadata = governanceMetadata(tool7);
20014
- if (!metadata) {
20015
- if (required2)
20016
- throw new Error(
20017
- `databricks governance: authoring tool "${tool7.name}" must declare metadata.governance.resources.`
20018
- );
20019
- return;
20020
- }
20021
- if (metadata.resources.length === 0)
20022
- throw new Error(`databricks governance: tool "${tool7.name}" declares no governed resources.`);
20023
- if (!tool7.inputSchema || typeof tool7.inputSchema !== "object")
20024
- throw new Error(
20025
- `databricks governance: tool "${tool7.name}" needs an inputSchema for resource validation.`
20026
- );
20027
- for (const descriptor of metadata.resources)
20028
- validateSchemaPath(tool7.name, tool7.inputSchema, descriptor);
20029
- }
20030
- function extractDatabricksGovernedResources(tool7, input) {
20031
- const metadata = governanceMetadata(tool7);
20032
- if (!metadata) return legacyResources(input);
20033
- const resources = [];
20034
- for (const descriptor of metadata.resources) {
20035
- const values = pointerValues(input, descriptor.path);
20036
- if (values.length === 0 && !descriptor.optional) {
20037
- throw new Error(
20038
- `databricks governance: required resource path "${descriptor.path}" matched no values for tool "${tool7.name}".`
20039
- );
20040
- }
20041
- for (const value of values) {
20042
- if (typeof value !== "string" && typeof value !== "number") {
20043
- throw new Error(
20044
- `databricks governance: resource path "${descriptor.path}" on tool "${tool7.name}" must resolve to strings or numbers.`
20045
- );
20046
- }
20047
- const text = String(value);
20048
- if (!text)
20049
- throw new Error(
20050
- `databricks governance: resource path "${descriptor.path}" on tool "${tool7.name}" resolved to an empty value.`
20051
- );
20052
- resources.push({
20053
- kind: descriptor.kind,
20054
- value: text,
20055
- path: descriptor.path,
20056
- ...descriptor.catalogQualified && (descriptor.kind === "catalog" || text.includes(".")) ? { catalog: descriptor.kind === "catalog" ? text : text.split(".")[0] } : {}
20057
- });
20058
- }
20059
- }
20060
- return resources;
20061
- }
20062
- function withGovernance(tool7, options = {}) {
20063
- const inner = tool7.execute;
20064
- if (!inner) return tool7;
20065
- validateDatabricksGovernanceDescriptor(tool7);
20066
- const now = options.now ?? (() => /* @__PURE__ */ new Date());
20067
- return {
20068
- ...tool7,
20069
- metadata: { ...tool7.metadata ?? {}, governed: true },
20070
- async execute(input, context) {
20071
- const resources = extractDatabricksGovernedResources(tool7, input);
20072
- const catalogs = [
20073
- ...new Set(resources.flatMap((resource) => resource.catalog ? [resource.catalog] : []))
20074
- ];
20075
- const catalog = catalogs[0] ?? catalogFromInput(input);
20076
- const schema = inputString(input, "schema");
20077
- const object2 = governedObject(input);
20078
- const startedAt = now().toISOString();
20079
- const service = metaString(tool7, "service");
20080
- const provider = metaString(tool7, "provider");
20081
- const operation = metaString(tool7, "effect");
20082
- const submission = currentSubmissionContext();
20083
- const principal = options.principal ?? options.executingPrincipal?.ucPrincipal ?? options.executingPrincipal?.id ?? submission?.actor?.principal?.id;
20084
- const base2 = {
20085
- tool: tool7.name,
20086
- ...service !== void 0 ? { service } : {},
20087
- ...provider !== void 0 ? { provider } : {},
20088
- ...principal !== void 0 ? { principal } : {},
20089
- ...submission ? {
20090
- submissionId: submission.submissionId,
20091
- attemptId: submission.attemptId,
20092
- ...submission.tenantId !== void 0 ? { tenantId: submission.tenantId } : {}
20093
- } : {},
20094
- ...catalog !== void 0 ? { catalog } : {},
20095
- ...schema !== void 0 ? { schema } : {},
20096
- ...object2 !== void 0 ? { object: object2 } : {},
20097
- ...resources.length > 0 ? { resources } : {},
20098
- ...context?.approval?.approvers.length ? { approvers: context.approval.approvers } : {},
20099
- ...operation !== void 0 ? { operation } : {},
20100
- // Redact secret-ish keys before the input enters the audit/lineage record.
20101
- input: redactToolInput(input, governanceMetadata(tool7)?.secretFields),
20102
- startedAt
20103
- };
20104
- const deniedCatalog = options.catalogs ? catalogs.find((candidate) => !options.catalogs?.includes(candidate)) : void 0;
20105
- if (options.catalogs && deniedCatalog) {
20106
- const error51 = `databricks governance: catalog "${deniedCatalog}" is not in the allowed list (${options.catalogs.join(", ")}).`;
20107
- options.onLineage?.({ ...base2, finishedAt: now().toISOString(), ok: false, error: error51 });
20108
- throw new Error(error51);
20109
- }
20110
- try {
20111
- if (service && (operation === "write" || operation === "execute") && (options.requireApprovalGrantFor?.includes(service) || options.requireApprovalGrantForTools?.includes(tool7.name))) {
20112
- if (!options.executingPrincipal)
20113
- throw new Error(
20114
- `databricks governance: service "${service}" requires a verified executing principal.`
20115
- );
20116
- if (!context?.approval || !context.toolCallId || !sameApprovalOperation(context.approval, {
20117
- toolCallId: context.toolCallId,
20118
- toolInput: input,
20119
- principal: options.executingPrincipal
20120
- })) {
20121
- throw new Error(
20122
- `databricks governance: tool "${tool7.name}" requires an ApprovalGrant bound to this call, input, and executing principal.`
20123
- );
20124
- }
20125
- }
20126
- const result = await inner(input, context);
20127
- options.onLineage?.({ ...base2, finishedAt: now().toISOString(), ok: true });
20128
- return result;
20129
- } catch (error51) {
20130
- options.onLineage?.({
20131
- ...base2,
20132
- finishedAt: now().toISOString(),
20133
- ok: false,
20134
- error: error51 instanceof Error ? error51.message : String(error51)
20135
- });
20136
- throw error51;
20137
- }
20138
- }
20139
- };
20140
- }
20141
- function parseResourceDescriptor(value, toolName, index) {
20142
- if (!value || typeof value !== "object" || Array.isArray(value))
20143
- throw new Error(
20144
- `databricks governance: invalid resource descriptor ${index} on tool "${toolName}".`
20145
- );
20146
- const row = value;
20147
- if (typeof row.kind !== "string" || !row.kind || typeof row.path !== "string" || !row.path.startsWith("/")) {
20148
- throw new Error(
20149
- `databricks governance: descriptor ${index} on tool "${toolName}" requires kind and an absolute path.`
20150
- );
20151
- }
20152
- return {
20153
- kind: row.kind,
20154
- path: row.path,
20155
- ...row.catalogQualified === true ? { catalogQualified: true } : {},
20156
- ...row.optional === true ? { optional: true } : {}
20157
- };
20158
- }
20159
- function parseStringArray(value, label) {
20160
- if (!Array.isArray(value) || value.some((item) => typeof item !== "string"))
20161
- throw new Error(`databricks governance: ${label} must be a string array.`);
20162
- return value;
20163
- }
20164
- function pointerSegments(path) {
20165
- return path.slice(1).split("/").map((segment) => segment.replace(/~1/g, "/").replace(/~0/g, "~"));
20166
- }
20167
- function pointerValues(input, path) {
20168
- let current = [input];
20169
- for (const segment of pointerSegments(path)) {
20170
- current = current.flatMap((value) => {
20171
- if (segment === "*") return Array.isArray(value) ? value : [];
20172
- if (!value || typeof value !== "object" || Array.isArray(value)) return [];
20173
- return segment in value ? [value[segment]] : [];
20174
- }).filter((value) => value !== void 0 && value !== null);
20175
- }
20176
- return current;
20177
- }
20178
- function validateSchemaPath(toolName, schema, descriptor) {
20179
- let current = [schema];
20180
- for (const segment of pointerSegments(descriptor.path)) {
20181
- const variants = current.flatMap(schemaVariants);
20182
- const next = [];
20183
- let missing = 0;
20184
- for (const variant of variants) {
20185
- if (segment === "*") {
20186
- const items = variant.items;
20187
- if (variant.type === "array" && items && typeof items === "object" && !Array.isArray(items))
20188
- next.push(items);
20189
- else missing += 1;
20190
- continue;
20191
- }
20192
- const properties = variant.properties;
20193
- const child = properties && typeof properties === "object" && !Array.isArray(properties) ? properties[segment] : void 0;
20194
- if (child && typeof child === "object" && !Array.isArray(child))
20195
- next.push(child);
20196
- else missing += 1;
20197
- }
20198
- if (next.length === 0)
20199
- throw new Error(
20200
- `databricks governance: path "${descriptor.path}" does not exist in inputSchema for tool "${toolName}".`
20201
- );
20202
- if (!descriptor.optional && missing > 0)
20203
- throw new Error(
20204
- `databricks governance: required path "${descriptor.path}" is not present in every inputSchema branch for tool "${toolName}". Mark the descriptor optional or make the schema branch mandatory.`
20205
- );
20206
- current = next;
20207
- }
20208
- }
20209
- function schemaVariants(schema) {
20210
- const variants = Array.isArray(schema.oneOf) ? schema.oneOf : Array.isArray(schema.anyOf) ? schema.anyOf : void 0;
20211
- if (!variants) return [schema];
20212
- return variants.filter(
20213
- (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value)
20214
- );
20215
- }
20216
- function legacyResources(input) {
20217
- const value = governedObject(input);
20218
- if (!value) return [];
20219
- const catalog = catalogFromInput(input);
20220
- return [{ kind: "object", value, path: "legacy-heuristic", ...catalog ? { catalog } : {} }];
20221
- }
20222
- function redactToolInput(input, secretFields) {
20223
- const clone2 = redactNamedFields(input ?? {}, new Set(secretFields ?? []));
20224
- return redactJson(clone2);
20225
- }
20226
- function redactNamedFields(value, names) {
20227
- if (Array.isArray(value)) return value.map((item) => redactNamedFields(item, names));
20228
- if (!value || typeof value !== "object") return value;
20229
- return Object.fromEntries(
20230
- Object.entries(value).map(([key, child]) => [
20231
- key,
20232
- names.has(key) ? "[REDACTED]" : redactNamedFields(child, names)
20233
- ])
20234
- );
20235
- }
20236
- function withGovernanceTools(tools, options = {}) {
20237
- return tools.map((tool7) => withGovernance(tool7, options));
20238
- }
20239
- function shouldGate(tool7, services) {
20240
- const effect = tool7.metadata?.effect;
20241
- if (effect !== "write" && effect !== "execute") return false;
20242
- return !services || services.includes(metaString(tool7, "service") ?? "");
20243
- }
20244
- function databricksApprovalRules(tools, options) {
20245
- return tools.filter((tool7) => shouldGate(tool7, options.services)).map((tool7) => ({
20246
- pattern: tool7.name,
20247
- audience: options.audience,
20248
- // ${name} is templated by the policy layer at request time — keep it literal here.
20249
- reason: options.reason ?? "Databricks ${name} requires data-steward approval.",
20250
- ...options.ttlSeconds !== void 0 ? { ttlSeconds: options.ttlSeconds } : {}
20251
- }));
20252
- }
20253
- function toHostPatterns(host) {
20254
- if (!host) return [];
20255
- const list = Array.isArray(host) ? host : [host];
20256
- return list.map((entry) => {
20257
- try {
20258
- return new URL(entry.includes("://") ? entry : `https://${entry}`).host;
20259
- } catch {
20260
- return entry;
20261
- }
20262
- });
20263
- }
20264
- function databricksGovernancePolicy(options) {
20265
- const policy = {};
20266
- if (options.stewardAudience) {
20267
- policy.toolPolicy = {
20268
- approvalRules: databricksApprovalRules(options.tools, {
20269
- audience: options.stewardAudience,
20270
- ...options.approvalServices ? { services: options.approvalServices } : {},
20271
- ...options.ttlSeconds !== void 0 ? { ttlSeconds: options.ttlSeconds } : {}
20272
- })
20273
- };
20274
- }
20275
- const hosts = toHostPatterns(options.host);
20276
- if (hosts.length > 0) policy.network = { mode: "allowlist", hosts };
20277
- return policy;
20278
- }
20279
-
20280
20547
  // src/jobs-authoring.ts
20281
20548
  function validateDatabricksComputePolicy(policy) {
20282
20549
  for (const [name, value] of [
@@ -57544,6 +57811,133 @@ function jitter3(ms) {
57544
57811
  }
57545
57812
 
57546
57813
  // src/tools.ts
57814
+ var UNSAFE_SQL_KEYWORDS = /* @__PURE__ */ new Set([
57815
+ "ADD",
57816
+ "ALTER",
57817
+ "ANALYZE",
57818
+ "CACHE",
57819
+ "CALL",
57820
+ "COMMENT",
57821
+ "COPY",
57822
+ "CREATE",
57823
+ "DELETE",
57824
+ "DROP",
57825
+ "EXECUTE",
57826
+ "GRANT",
57827
+ "INSERT",
57828
+ "INTO",
57829
+ "LIST",
57830
+ "MERGE",
57831
+ "MSCK",
57832
+ "OPTIMIZE",
57833
+ "PUT",
57834
+ "REFRESH",
57835
+ "REMOVE",
57836
+ "RESET",
57837
+ "RESTORE",
57838
+ "REVOKE",
57839
+ "SET",
57840
+ "TRANSFORM",
57841
+ "TRUNCATE",
57842
+ "UNCACHE",
57843
+ "UPDATE",
57844
+ "USE",
57845
+ "VACUUM"
57846
+ ]);
57847
+ function lexSqlReadStatement(statement) {
57848
+ const tokens = [];
57849
+ const semicolons = [];
57850
+ let depth = 0;
57851
+ for (let index = 0; index < statement.length; ) {
57852
+ const char = statement.charAt(index);
57853
+ const next = statement.charAt(index + 1);
57854
+ if (/\s/u.test(char)) {
57855
+ index += 1;
57856
+ continue;
57857
+ }
57858
+ if (char === "-" && next === "-") {
57859
+ index += 2;
57860
+ while (index < statement.length && statement[index] !== "\n") index += 1;
57861
+ continue;
57862
+ }
57863
+ if (char === "/" && next === "*") {
57864
+ const end = statement.indexOf("*/", index + 2);
57865
+ if (end < 0) throw new Error("Databricks SQL read denied: unterminated block comment.");
57866
+ index = end + 2;
57867
+ continue;
57868
+ }
57869
+ if (char === "'" || char === '"' || char === "`") {
57870
+ const quote = char;
57871
+ index += 1;
57872
+ let closed = false;
57873
+ while (index < statement.length) {
57874
+ if (statement[index] === quote) {
57875
+ if (statement[index + 1] === quote) {
57876
+ index += 2;
57877
+ continue;
57878
+ }
57879
+ index += 1;
57880
+ closed = true;
57881
+ break;
57882
+ }
57883
+ if (statement[index] === "\\" && quote === "'" && index + 1 < statement.length) {
57884
+ index += 2;
57885
+ continue;
57886
+ }
57887
+ index += 1;
57888
+ }
57889
+ if (!closed) throw new Error("Databricks SQL read denied: unterminated quoted value.");
57890
+ continue;
57891
+ }
57892
+ if (char === ";") {
57893
+ semicolons.push(index);
57894
+ index += 1;
57895
+ continue;
57896
+ }
57897
+ if (char === "(") {
57898
+ depth += 1;
57899
+ index += 1;
57900
+ continue;
57901
+ }
57902
+ if (char === ")") {
57903
+ depth -= 1;
57904
+ if (depth < 0) throw new Error("Databricks SQL read denied: unbalanced parentheses.");
57905
+ index += 1;
57906
+ continue;
57907
+ }
57908
+ if (/[A-Za-z_]/u.test(char)) {
57909
+ const start = index;
57910
+ index += 1;
57911
+ while (index < statement.length && /[A-Za-z0-9_$]/u.test(statement.charAt(index))) index += 1;
57912
+ tokens.push({ value: statement.slice(start, index).toUpperCase(), depth });
57913
+ continue;
57914
+ }
57915
+ index += 1;
57916
+ }
57917
+ if (depth !== 0) throw new Error("Databricks SQL read denied: unbalanced parentheses.");
57918
+ return { tokens, semicolons };
57919
+ }
57920
+ function assertDatabricksSqlReadStatement(statement) {
57921
+ if (!statement.trim()) throw new Error("Databricks SQL read denied: statement cannot be empty.");
57922
+ const { tokens, semicolons } = lexSqlReadStatement(statement);
57923
+ if (tokens.length === 0) throw new Error("Databricks SQL read denied: expected a SELECT statement.");
57924
+ if (semicolons.length > 1)
57925
+ throw new Error("Databricks SQL read denied: multiple statements are not allowed.");
57926
+ if (semicolons.length === 1) {
57927
+ const tail = statement.slice((semicolons[0] ?? -1) + 1);
57928
+ const trailing = lexSqlReadStatement(tail);
57929
+ if (trailing.tokens.length > 0 || trailing.semicolons.length > 0)
57930
+ throw new Error("Databricks SQL read denied: multiple statements are not allowed.");
57931
+ }
57932
+ const first = tokens[0]?.value;
57933
+ const topLevel = tokens.filter((token) => token.depth === 0).map((token) => token.value);
57934
+ const mainKeyword = first === "WITH" ? topLevel.find((token) => token === "SELECT") : first;
57935
+ if (mainKeyword !== "SELECT")
57936
+ throw new Error("Databricks SQL read denied: only SELECT queries are allowed.");
57937
+ const unsafe = tokens.find((token) => UNSAFE_SQL_KEYWORDS.has(token.value));
57938
+ if (unsafe)
57939
+ throw new Error(`Databricks SQL read denied: unsafe keyword ${unsafe.value} is not allowed.`);
57940
+ }
57547
57941
  function databricksSqlTool(client, options) {
57548
57942
  return {
57549
57943
  name: options.name ?? "databricks_sql",
@@ -57569,6 +57963,43 @@ function databricksSqlTool(client, options) {
57569
57963
  })
57570
57964
  };
57571
57965
  }
57966
+ function databricksSqlReadTool(client, options) {
57967
+ return {
57968
+ name: options.name ?? "sql_read",
57969
+ description: options.description ?? "Run one SELECT-only query on a Databricks SQL Warehouse. Mutations and multiple statements are denied.",
57970
+ inputSchema: {
57971
+ type: "object",
57972
+ additionalProperties: false,
57973
+ properties: {
57974
+ statement: { type: "string" },
57975
+ catalog: { type: "string" },
57976
+ schema: { type: "string" }
57977
+ },
57978
+ required: ["statement"]
57979
+ },
57980
+ metadata: {
57981
+ effect: "read",
57982
+ provider: "databricks",
57983
+ service: "sql",
57984
+ governance: {
57985
+ resources: [
57986
+ { kind: "catalog", path: "/catalog", catalogQualified: true, optional: true }
57987
+ ]
57988
+ }
57989
+ },
57990
+ async execute(input) {
57991
+ assertDatabricksSqlReadStatement(input.statement);
57992
+ return await client.executeStatement({
57993
+ warehouseId: options.warehouseId,
57994
+ statement: input.statement,
57995
+ waitTimeout: options.waitTimeout ?? "30s",
57996
+ disposition: options.disposition ?? "INLINE",
57997
+ ...input.catalog ? { catalog: input.catalog } : {},
57998
+ ...input.schema ? { schema: input.schema } : {}
57999
+ });
58000
+ }
58001
+ };
58002
+ }
57572
58003
  function unityCatalogTablesTool(client, options = {}) {
57573
58004
  return {
57574
58005
  name: options.name ?? "databricks_unity_catalog_tables",
@@ -58281,6 +58712,7 @@ function buildDatabricksBundle(config2, scope) {
58281
58712
  });
58282
58713
  }
58283
58714
  const rawTools = [
58715
+ ...config2.sqlRead && config2.warehouseId ? [databricksSqlReadTool(native.statements, { warehouseId: config2.warehouseId })] : [],
58284
58716
  ...config2.warehouseId ? [databricksSqlTool(native.statements, { warehouseId: config2.warehouseId })] : [],
58285
58717
  unityCatalogTablesTool(native.tables),
58286
58718
  databricksTableInfoTool(native.tables),
@@ -58539,6 +58971,7 @@ function defineDatabricksAgent(options = {}) {
58539
58971
  const merged = options ? mergeOptions(options) : {};
58540
58972
  const {
58541
58973
  databricks: explicitConfig,
58974
+ analyticsCopilot = false,
58542
58975
  modelProvider,
58543
58976
  tools = [],
58544
58977
  sandbox,
@@ -58556,8 +58989,9 @@ function defineDatabricksAgent(options = {}) {
58556
58989
  ...directTools.length > 0 ? { tools: directTools } : {},
58557
58990
  run: async (context) => {
58558
58991
  const env2 = context.env ?? {};
58559
- const config2 = explicitConfig ?? databricksConfigFromEnv(env2);
58992
+ const config2 = explicitConfig ?? databricksConfigFromEnv(env2, { analyticsCopilot });
58560
58993
  const bundle = databricks(config2);
58994
+ const analyticsCostLimit = analyticsCopilot ? databricksAnalyticsCostLimitFromEnv(env2, config2, bundle) : void 0;
58561
58995
  if (toolRefs.length > 0) resolveToolRefs(bundle, toolRefs);
58562
58996
  const directNames = new Set(directTools.map((tool7) => tool7.name));
58563
58997
  const bundleTools = bundle.tools.filter((tool7) => !directNames.has(tool7.name));
@@ -58567,6 +59001,7 @@ function defineDatabricksAgent(options = {}) {
58567
59001
  ...initOptions,
58568
59002
  modelProvider: initOptions.modelProvider ?? modelProvider ?? bundle.modelProvider,
58569
59003
  tools: [...bundleTools, ...initOptions.tools ?? []],
59004
+ ...analyticsCostLimit && initOptions.costLimit === void 0 ? { costLimit: analyticsCostLimit } : {},
58570
59005
  ...policy ? { policy } : {},
58571
59006
  ...bundle.store && initOptions.store === void 0 ? { store: bundle.store } : {},
58572
59007
  ...sandbox && initOptions.sandbox === void 0 ? { sandbox } : {}
@@ -58580,7 +59015,22 @@ function defineDatabricksAgent(options = {}) {
58580
59015
  };
58581
59016
  return defineAgent(def);
58582
59017
  }
58583
- function databricksConfigFromEnv(env2) {
59018
+ function databricksAnalyticsCostLimitFromEnv(env2, config2, bundle) {
59019
+ if (env2.FABRIC_MOCK === "1") return void 0;
59020
+ const warehouseId = config2.warehouseId;
59021
+ const tenantId = env2.DATABRICKS_COST_TENANT_ID?.trim();
59022
+ const perDayUsd = Number(env2.DATABRICKS_COST_PER_DAY_USD);
59023
+ if (!warehouseId) throw new Error("Analytics copilot cost enforcement requires a SQL Warehouse.");
59024
+ if (!tenantId) throw new Error("Analytics copilot requires DATABRICKS_COST_TENANT_ID.");
59025
+ if (!Number.isFinite(perDayUsd) || perDayUsd <= 0) {
59026
+ throw new Error("Analytics copilot requires a positive DATABRICKS_COST_PER_DAY_USD.");
59027
+ }
59028
+ return databricksTenantCostLimit(bundle.sdk.statements, warehouseId, tenantId, {
59029
+ perDayUsd,
59030
+ onExceed: "throw"
59031
+ });
59032
+ }
59033
+ function databricksConfigFromEnv(env2, options = {}) {
58584
59034
  const rawHost = env2.DATABRICKS_HOST;
58585
59035
  const host = rawHost ? rawHost.includes("://") ? rawHost : `https://${rawHost}` : void 0;
58586
59036
  if (!host && env2.FABRIC_MOCK !== "1") {
@@ -58589,12 +59039,38 @@ function databricksConfigFromEnv(env2) {
58589
59039
  );
58590
59040
  }
58591
59041
  const principal = env2.FABRIC_MOCK === "1" ? { kind: "pat", token: "mock-token" } : databricksPrincipalFromEnv(env2);
59042
+ const analyticsCopilot = options.analyticsCopilot === true;
59043
+ const genieAgentId = env2.DATABRICKS_GENIE_SPACE_ID;
59044
+ const stewardAudience = env2.DATABRICKS_ANALYTICS_STEWARD_AUDIENCE;
59045
+ if (analyticsCopilot && env2.FABRIC_MOCK !== "1" && !env2.DATABRICKS_WAREHOUSE_ID) {
59046
+ throw new Error("Analytics copilot requires DATABRICKS_WAREHOUSE_ID.");
59047
+ }
59048
+ if (analyticsCopilot && env2.FABRIC_MOCK !== "1" && !genieAgentId) {
59049
+ throw new Error("Analytics copilot requires DATABRICKS_GENIE_SPACE_ID.");
59050
+ }
59051
+ if (analyticsCopilot && env2.FABRIC_MOCK !== "1" && !stewardAudience) {
59052
+ throw new Error("Analytics copilot requires DATABRICKS_ANALYTICS_STEWARD_AUDIENCE.");
59053
+ }
59054
+ const catalog = env2.DATABRICKS_CATALOG?.trim();
58592
59055
  return {
58593
59056
  host: host ?? "https://mock.databricks.com",
58594
59057
  principal,
58595
59058
  ...env2.DATABRICKS_MODEL ? { model: env2.DATABRICKS_MODEL } : {},
58596
59059
  ...env2.DATABRICKS_WAREHOUSE_ID ? { warehouseId: env2.DATABRICKS_WAREHOUSE_ID } : {},
58597
- ...env2.FABRIC_MOCK === "1" ? { warehouseId: "mock-warehouse-id" } : {}
59060
+ ...env2.FABRIC_MOCK === "1" ? { warehouseId: "mock-warehouse-id" } : {},
59061
+ ...analyticsCopilot ? {
59062
+ sqlRead: true,
59063
+ consumption: true,
59064
+ genie: {
59065
+ conversations: {
59066
+ agentId: genieAgentId ?? "mock-genie-agent"
59067
+ }
59068
+ },
59069
+ governance: analyticsCopilotGovernance({
59070
+ stewardAudience: stewardAudience ?? "analytics-stewards",
59071
+ ...catalog ? { catalogs: [catalog] } : {}
59072
+ })
59073
+ } : {}
58598
59074
  };
58599
59075
  }
58600
59076
  function resolveToolRefs(bundle, refs) {
@@ -58604,6 +59080,7 @@ function resolveToolRefs(bundle, refs) {
58604
59080
  }
58605
59081
  const shortNameMap = {
58606
59082
  sql: "databricks_sql",
59083
+ "sql-read": "sql_read",
58607
59084
  tables: "databricks_unity_catalog_tables",
58608
59085
  "table-info": "databricks_table_info",
58609
59086
  search: "search",