@fabric-harness/databricks 7.0.1 → 7.1.1

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/index.js CHANGED
@@ -4849,6 +4849,17 @@ async function validateDatabricksGenieSqlReferences(spec, policy) {
4849
4849
  async function serializeDatabricksGenieAgentSpec(spec) {
4850
4850
  return JSON.stringify(serializedSpec(await normalizeDatabricksGenieAgentSpec(spec)));
4851
4851
  }
4852
+ async function databricksGenieAgentSpecFingerprint(spec, agentId) {
4853
+ const normalized = await normalizeDatabricksGenieAgentSpec(spec);
4854
+ return databricksResourceFingerprint({
4855
+ agentId: agentId ?? "",
4856
+ title: normalized.title,
4857
+ description: normalized.description,
4858
+ parentPath: fingerprintWorkspacePath(normalized.parentPath),
4859
+ warehouseId: normalized.warehouseId,
4860
+ serializedSpace: fingerprintSerializedSpace(JSON.stringify(serializedSpec(normalized)))
4861
+ });
4862
+ }
4852
4863
  function serializedSpec(spec) {
4853
4864
  const tables = spec.dataSources.filter((source) => (source.kind ?? "table") === "table");
4854
4865
  const metricViews = spec.dataSources.filter((source) => source.kind === "metric-view");
@@ -23296,6 +23307,11 @@ function databricksModelBaseUrl(host, mode) {
23296
23307
  function isDatabricksModelService(model) {
23297
23308
  return model?.startsWith("system.ai.") ?? false;
23298
23309
  }
23310
+ function databricksNativeModelId(model) {
23311
+ if (model === void 0) return void 0;
23312
+ const prefix = "databricks/";
23313
+ return model.startsWith(prefix) ? model.slice(prefix.length) : model;
23314
+ }
23299
23315
  var DatabricksModelProvider = class {
23300
23316
  name = "databricks";
23301
23317
  options;
@@ -23328,26 +23344,29 @@ var DatabricksModelProvider = class {
23328
23344
  }) : void 0;
23329
23345
  }
23330
23346
  generate(request) {
23331
- return this.providerFor(request.model).generate(request);
23347
+ const model = databricksNativeModelId(request.model);
23348
+ return this.providerFor(model).generate({ ...request, ...model !== void 0 ? { model } : {} });
23332
23349
  }
23333
23350
  stream(request) {
23334
- const provider = this.providerFor(request.model);
23351
+ const model = databricksNativeModelId(request.model);
23352
+ const provider = this.providerFor(model);
23335
23353
  if (!provider.stream)
23336
23354
  throw new Error("databricks: selected provider does not support streaming.");
23337
- return provider.stream(request);
23355
+ return provider.stream({ ...request, ...model !== void 0 ? { model } : {} });
23338
23356
  }
23339
23357
  getModelMetadata(model = this.options.defaultModel) {
23340
- const mode = this.modeFor(model);
23358
+ const native = databricksNativeModelId(model);
23359
+ const mode = this.modeFor(native);
23341
23360
  return {
23342
23361
  provider: this.name,
23343
- ...model ? { model } : {},
23362
+ ...native ? { model: native } : {},
23344
23363
  gateway: this.options.baseUrl ?? databricksModelBaseUrl(this.options.host, mode),
23345
- ...model && this.options.modelMetadata?.[model] ? this.options.modelMetadata[model] : {}
23364
+ ...native && this.options.modelMetadata?.[native] ? this.options.modelMetadata[native] : {}
23346
23365
  };
23347
23366
  }
23348
23367
  providerFor(model = this.options.defaultModel) {
23349
23368
  if (this.explicit) return this.explicit;
23350
- return this.modeFor(model) === "ai-gateway" ? this.gateway : this.serving;
23369
+ return this.modeFor(databricksNativeModelId(model)) === "ai-gateway" ? this.gateway : this.serving;
23351
23370
  }
23352
23371
  modeFor(model) {
23353
23372
  const configured = this.options.mode ?? "auto";
@@ -61738,6 +61757,1716 @@ function rejectUnsupportedEntry(entry, info) {
61738
61757
  }
61739
61758
  }
61740
61759
 
61760
+ // src/ontology-metric-view.ts
61761
+ var MANAGED_MARKER = "fabric-harness.managed";
61762
+ var NUMERIC_THRESHOLD_OPERATORS = /* @__PURE__ */ new Set([
61763
+ "gt",
61764
+ "gte",
61765
+ "lt",
61766
+ "lte"
61767
+ ]);
61768
+ var OPERATOR_SYMBOLS = {
61769
+ gt: ">",
61770
+ gte: ">=",
61771
+ lt: "<",
61772
+ lte: "<="
61773
+ };
61774
+ function collectLeafConditions(conditions, into) {
61775
+ for (const condition of conditions) {
61776
+ if (condition.type === "all" || condition.type === "any") {
61777
+ collectLeafConditions(condition.conditions, into);
61778
+ } else if (condition.type === "not") {
61779
+ collectLeafConditions([condition.condition], into);
61780
+ } else {
61781
+ into.push(condition);
61782
+ }
61783
+ }
61784
+ }
61785
+ function asNumericThresholdCondition(condition) {
61786
+ if (condition.type !== "comparison") return void 0;
61787
+ if (!NUMERIC_THRESHOLD_OPERATORS.has(condition.operator)) return void 0;
61788
+ if (typeof condition.value !== "number" || !Number.isFinite(condition.value)) return void 0;
61789
+ return {
61790
+ conditionId: condition.conditionId,
61791
+ source: condition.left.source,
61792
+ path: condition.left.path,
61793
+ operator: condition.operator,
61794
+ value: condition.value
61795
+ };
61796
+ }
61797
+ function describeCondition(condition) {
61798
+ const operator = "operator" in condition ? `, operator "${condition.operator}"` : "";
61799
+ return `condition "${condition.conditionId}" (type "${condition.type}"${operator})`;
61800
+ }
61801
+ function classifyDataPolicy(policy) {
61802
+ const businessRule = (evidence) => ({ class: "gate-compliance-rule", target: "glossary-business-rule", evidence });
61803
+ if (policy.kind !== "data") {
61804
+ return businessRule(
61805
+ `data policy kind "${policy.kind}" is not purely declarative; ambiguous policies default to a business rule`
61806
+ );
61807
+ }
61808
+ const leaves = [];
61809
+ collectLeafConditions(policy.dataDefinition?.conditions ?? [], leaves);
61810
+ if (leaves.length === 0) {
61811
+ return businessRule(
61812
+ "data definition declares no conditions; ambiguous policies default to a business rule"
61813
+ );
61814
+ }
61815
+ const thresholds = leaves.map(asNumericThresholdCondition);
61816
+ if (thresholds.every((threshold) => threshold !== void 0)) {
61817
+ const first = thresholds[0];
61818
+ const symbol2 = OPERATOR_SYMBOLS[first.operator] ?? first.operator;
61819
+ return {
61820
+ class: "threshold-ratio-metric",
61821
+ target: "metric-view-measure",
61822
+ evidence: `all ${leaves.length} leaf condition(s) are numeric threshold comparisons (e.g. "${first.conditionId}": ${first.source}.${first.path} ${symbol2} ${first.value})`
61823
+ };
61824
+ }
61825
+ const firstNonThreshold = leaves[thresholds.findIndex((threshold) => threshold === void 0)];
61826
+ return businessRule(
61827
+ `${describeCondition(firstNonThreshold)} is not a numeric threshold comparison; admissibility gates stay business rules and never become measures`
61828
+ );
61829
+ }
61830
+ function classifyModuleOntologyPolicies(manifest, options = {}) {
61831
+ const seeds = new Set(options.metricSeedPolicyIds ?? []);
61832
+ return manifest.policies.map((policy) => {
61833
+ if (policy.type === "code") {
61834
+ const seeded = seeds.has(policy.policyId);
61835
+ return {
61836
+ namespace: manifest.namespace,
61837
+ policyId: policy.policyId,
61838
+ policyVersion: policy.version,
61839
+ policyType: policy.type,
61840
+ class: seeded ? "threshold-ratio-metric" : "code-evaluator",
61841
+ target: seeded ? "metric-view-measure" : "glossary-business-rule",
61842
+ evidence: seeded ? "explicit metricSeedPolicyIds annotation promotes this code evaluator to a metric seed" : `code evaluator logic is not serializable; exported as a business rule referencing ${policy.policyId}@v${policy.version}`
61843
+ };
61844
+ }
61845
+ return {
61846
+ namespace: manifest.namespace,
61847
+ policyId: policy.policyId,
61848
+ policyVersion: policy.policyVersion,
61849
+ policyType: policy.type,
61850
+ ...classifyDataPolicy(policy)
61851
+ };
61852
+ });
61853
+ }
61854
+ function sqlIdentifier2(value) {
61855
+ return value.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[^A-Za-z0-9]+/g, "_").replace(/_+/g, "_").replace(/^_+|_+$/g, "").toLowerCase();
61856
+ }
61857
+ function yamlString(value) {
61858
+ return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/\s*\n\s*/g, " ")}"`;
61859
+ }
61860
+ function qualifiedName2(...parts) {
61861
+ return parts.map((part) => `\`${part}\``).join(".");
61862
+ }
61863
+ function deriveModuleMetricViewMeasures(manifest, classifications) {
61864
+ const metricPolicyIds = new Set(
61865
+ classifications.filter(
61866
+ (classification) => classification.namespace === manifest.namespace && classification.target === "metric-view-measure"
61867
+ ).map((classification) => classification.policyId)
61868
+ );
61869
+ const measures = [];
61870
+ const usedNames = /* @__PURE__ */ new Set();
61871
+ for (const policy of manifest.policies) {
61872
+ if (policy.type !== "data" || !metricPolicyIds.has(policy.policyId)) continue;
61873
+ const leaves = [];
61874
+ collectLeafConditions(policy.dataDefinition?.conditions ?? [], leaves);
61875
+ const thresholds = leaves.map(asNumericThresholdCondition).filter(
61876
+ (threshold) => threshold !== void 0
61877
+ );
61878
+ const base2 = sqlIdentifier2(policy.policyId.replace(/\.v\d+$/, ""));
61879
+ thresholds.forEach((threshold, index) => {
61880
+ const column = sqlIdentifier2(threshold.path);
61881
+ if (!column) return;
61882
+ let name = thresholds.length === 1 ? base2 : `${base2}_${sqlIdentifier2(threshold.conditionId)}`;
61883
+ for (let suffix = 2; usedNames.has(name); suffix += 1) name = `${base2}_${suffix}`;
61884
+ usedNames.add(name);
61885
+ const symbol2 = OPERATOR_SYMBOLS[threshold.operator] ?? threshold.operator;
61886
+ measures.push({
61887
+ name,
61888
+ expr: `AVG(\`${column}\`)`,
61889
+ comment: `Policy ${policy.policyId} numeric threshold: ${threshold.source}.${threshold.path} ${symbol2} ${threshold.value}.${index === 0 ? "" : ` Condition "${threshold.conditionId}".`}`
61890
+ });
61891
+ });
61892
+ }
61893
+ return measures;
61894
+ }
61895
+ async function generateModuleMetricViewDdl(input) {
61896
+ const { manifest, catalog, schema } = input;
61897
+ const namespace = sqlIdentifier2(manifest.namespace);
61898
+ const viewName = `${namespace}_metrics`;
61899
+ const fingerprint = await databricksResourceFingerprint({
61900
+ scope: "databricks-ontology-metric-view",
61901
+ manifest,
61902
+ catalog,
61903
+ schema,
61904
+ policyClassifications: input.classifications
61905
+ });
61906
+ const dimensions = [
61907
+ {
61908
+ name: "object_type",
61909
+ expr: "object_type",
61910
+ comment: `Declared object types: ${manifest.objectTypes.map((objectType) => objectType.type).join(", ")}.`
61911
+ }
61912
+ ];
61913
+ if (manifest.stateMachines.length > 0) {
61914
+ const stateSummary = manifest.stateMachines.map(
61915
+ (machine) => `${machine.entityType}: ${machine.states.map((state) => state.id).join(", ")}`
61916
+ ).join("; ");
61917
+ dimensions.push({
61918
+ name: "state",
61919
+ expr: "state",
61920
+ comment: `State-machine states \u2014 ${stateSummary}.`
61921
+ });
61922
+ }
61923
+ const measures = deriveModuleMetricViewMeasures(manifest, input.classifications);
61924
+ const mutatingActions = manifest.actions.filter((action) => action.mutatesDomain);
61925
+ const seededCodePolicies = input.classifications.filter(
61926
+ (classification) => classification.namespace === manifest.namespace && classification.policyType === "code" && classification.target === "metric-view-measure"
61927
+ );
61928
+ const lines = [
61929
+ `-- ${MANAGED_MARKER}: true`,
61930
+ `-- fabric-harness.manifest-fingerprint: ${fingerprint}`,
61931
+ "-- Generated by planModuleOntologyExport (@fabric-harness/databricks); do not edit by hand.",
61932
+ `CREATE OR REPLACE VIEW ${qualifiedName2(catalog, schema, viewName)}`,
61933
+ "WITH METRICS LANGUAGE YAML",
61934
+ "AS $$",
61935
+ "version: 1.1",
61936
+ `comment: ${yamlString(
61937
+ manifest.description ?? `Fabric module ${manifest.namespace} ontology.`
61938
+ )}`,
61939
+ `source: ${qualifiedName2(catalog, schema, `${namespace}_asset_events`)}`
61940
+ ];
61941
+ if (mutatingActions.length > 0) {
61942
+ lines.push(
61943
+ `# Count measures over countable action volumes (${mutatingActions.map((action) => action.actionId).join(", ")}) arrive with the P1 operational-truth feed tables.`
61944
+ );
61945
+ }
61946
+ for (const seed of seededCodePolicies) {
61947
+ lines.push(
61948
+ `# Metric seed ${seed.policyId}: code evaluator promoted via metricSeedPolicyIds; its measure expression is authored at apply time (policy logic never leaves Platform).`
61949
+ );
61950
+ }
61951
+ lines.push("dimensions:");
61952
+ for (const dimension of dimensions) {
61953
+ lines.push(
61954
+ ` - name: ${dimension.name}`,
61955
+ ` expr: ${dimension.expr}`,
61956
+ ` comment: ${yamlString(dimension.comment)}`
61957
+ );
61958
+ }
61959
+ if (measures.length > 0) {
61960
+ lines.push("measures:");
61961
+ for (const measure of measures) {
61962
+ lines.push(
61963
+ ` - name: ${measure.name}`,
61964
+ ` expr: ${yamlString(measure.expr)}`,
61965
+ ` comment: ${yamlString(measure.comment)}`
61966
+ );
61967
+ }
61968
+ }
61969
+ lines.push("$$;");
61970
+ return `${lines.join("\n")}
61971
+ `;
61972
+ }
61973
+
61974
+ // src/ontology-feed.ts
61975
+ var MANAGED_MARKER2 = "fabric-harness.managed";
61976
+ var ASSET_EVENTS_COLUMNS = [
61977
+ {
61978
+ name: "event_id",
61979
+ type: "STRING",
61980
+ comment: "Platform AssetEvent id. Replay key: hydration merges on this column so re-runs are idempotent."
61981
+ },
61982
+ {
61983
+ name: "event_type",
61984
+ type: "STRING",
61985
+ comment: "Declared event type from the module manifest (e.g. loan_application.approved)."
61986
+ },
61987
+ {
61988
+ name: "schema_version",
61989
+ type: "INT",
61990
+ comment: "Declared event schemaVersion. Events land at their declared version; no upcasting at append time (design Q3, conservative)."
61991
+ },
61992
+ {
61993
+ name: "namespace",
61994
+ type: "STRING",
61995
+ comment: "Fabric module namespace that emitted the event."
61996
+ },
61997
+ {
61998
+ name: "tenant_id",
61999
+ type: "STRING",
62000
+ comment: "Tenant scope, carried as a column and enforced by UC grants, not filtered at export."
62001
+ },
62002
+ {
62003
+ name: "object_type",
62004
+ type: "STRING",
62005
+ comment: "Declared object type the event concerns."
62006
+ },
62007
+ {
62008
+ name: "object_id",
62009
+ type: "STRING",
62010
+ comment: "Governed object identifier (module idPrefix vocabulary)."
62011
+ },
62012
+ {
62013
+ name: "state",
62014
+ type: "STRING",
62015
+ comment: "State-machine state after the event, when the module binds one; null otherwise."
62016
+ },
62017
+ {
62018
+ name: "occurred_at",
62019
+ type: "TIMESTAMP",
62020
+ comment: "Domain time the event occurred."
62021
+ },
62022
+ {
62023
+ name: "correlation_id",
62024
+ type: "STRING",
62025
+ comment: "Correlation id linking the event to its originating invocation."
62026
+ },
62027
+ {
62028
+ name: "causation_id",
62029
+ type: "STRING",
62030
+ comment: "Causation id linking the event to the record that caused it."
62031
+ },
62032
+ {
62033
+ name: "recorded_at",
62034
+ type: "TIMESTAMP",
62035
+ comment: "Time the event was recorded into durable history."
62036
+ }
62037
+ ];
62038
+ var ACTION_INVOCATIONS_COLUMNS = [
62039
+ {
62040
+ name: "invocation_id",
62041
+ type: "STRING",
62042
+ comment: "Platform ActionInvocation id. Replay key: hydration merges on this column so re-runs are idempotent."
62043
+ },
62044
+ {
62045
+ name: "action_id",
62046
+ type: "STRING",
62047
+ comment: "Declared action id from the module manifest."
62048
+ },
62049
+ {
62050
+ name: "action_version",
62051
+ type: "INT",
62052
+ comment: "Declared action version."
62053
+ },
62054
+ {
62055
+ name: "namespace",
62056
+ type: "STRING",
62057
+ comment: "Fabric module namespace that executed the action."
62058
+ },
62059
+ {
62060
+ name: "tenant_id",
62061
+ type: "STRING",
62062
+ comment: "Tenant scope, carried as a column and enforced by UC grants, not filtered at export."
62063
+ },
62064
+ {
62065
+ name: "terminal_status",
62066
+ type: "STRING",
62067
+ comment: "Terminal status: succeeded, failed, or cancelled."
62068
+ },
62069
+ {
62070
+ name: "failure_classification",
62071
+ type: "STRING",
62072
+ comment: "Failure classification (retryable, permanent, exhausted, cancelled); null on success."
62073
+ },
62074
+ {
62075
+ name: "started_at",
62076
+ type: "TIMESTAMP",
62077
+ comment: "Invocation start time."
62078
+ },
62079
+ {
62080
+ name: "completed_at",
62081
+ type: "TIMESTAMP",
62082
+ comment: "Invocation terminal time."
62083
+ },
62084
+ {
62085
+ name: "duration_ms",
62086
+ type: "BIGINT",
62087
+ comment: "Wall-clock duration of the invocation in milliseconds."
62088
+ },
62089
+ {
62090
+ name: "correlation_id",
62091
+ type: "STRING",
62092
+ comment: "Correlation id linking the invocation to related events and evaluations."
62093
+ },
62094
+ {
62095
+ name: "redacted_parameters",
62096
+ type: "STRING",
62097
+ comment: "JSON of action parameters AFTER the vertical's redaction producer. Raw parameters are never transported (design \xA73.7, KD-10)."
62098
+ }
62099
+ ];
62100
+ var POLICY_EVALUATIONS_COLUMNS = [
62101
+ {
62102
+ name: "evaluation_id",
62103
+ type: "STRING",
62104
+ comment: "Platform PolicyEvaluation id. Replay key: hydration merges on this column so re-runs are idempotent."
62105
+ },
62106
+ {
62107
+ name: "policy_id",
62108
+ type: "STRING",
62109
+ comment: "Declared policy id from the module manifest."
62110
+ },
62111
+ {
62112
+ name: "policy_version",
62113
+ type: "INT",
62114
+ comment: "Declared policy version."
62115
+ },
62116
+ {
62117
+ name: "namespace",
62118
+ type: "STRING",
62119
+ comment: "Fabric module namespace whose policy was evaluated."
62120
+ },
62121
+ {
62122
+ name: "tenant_id",
62123
+ type: "STRING",
62124
+ comment: "Tenant scope, carried as a column and enforced by UC grants, not filtered at export."
62125
+ },
62126
+ {
62127
+ name: "decision",
62128
+ type: "STRING",
62129
+ comment: "Evaluation decision (allow, deny, ...)."
62130
+ },
62131
+ {
62132
+ name: "reasons",
62133
+ type: "ARRAY<STRING>",
62134
+ comment: "Declared-safe reason strings; no facts payload beyond them."
62135
+ },
62136
+ {
62137
+ name: "evaluator_identity",
62138
+ type: "STRING",
62139
+ comment: "Identity of the evaluator (policy engine or code evaluator) that produced the decision."
62140
+ },
62141
+ {
62142
+ name: "evaluator_version",
62143
+ type: "STRING",
62144
+ comment: "Version of the evaluator that produced the decision."
62145
+ },
62146
+ {
62147
+ name: "failure_mode",
62148
+ type: "STRING",
62149
+ comment: "Evaluator failure mode when the evaluation itself failed; null otherwise."
62150
+ },
62151
+ {
62152
+ name: "evaluated_at",
62153
+ type: "TIMESTAMP",
62154
+ comment: "Time the evaluation was recorded."
62155
+ },
62156
+ {
62157
+ name: "invocation_id",
62158
+ type: "STRING",
62159
+ comment: "Action invocation this evaluation gated, when applicable; joins to action_invocations."
62160
+ }
62161
+ ];
62162
+ var FEED_TABLE_SCHEMAS = {
62163
+ asset_events: ASSET_EVENTS_COLUMNS,
62164
+ action_invocations: ACTION_INVOCATIONS_COLUMNS,
62165
+ policy_evaluations: POLICY_EVALUATIONS_COLUMNS
62166
+ };
62167
+ var FEED_TABLE_CONTENT = {
62168
+ asset_events: "append-only domain events with schemaVersion, tenant, correlation, and causation",
62169
+ action_invocations: "action id, version, terminal status, failure classification, duration, and redacted parameters only",
62170
+ policy_evaluations: "decision, reasons, evaluator identity/version, and failure mode \u2014 no facts payload beyond declared-safe fields"
62171
+ };
62172
+ var DEFAULT_FEED_TABLE_KINDS = [
62173
+ "asset_events",
62174
+ "action_invocations",
62175
+ "policy_evaluations"
62176
+ ];
62177
+ function qualifiedName3(...parts) {
62178
+ return parts.map((part) => `\`${part}\``).join(".");
62179
+ }
62180
+ function sqlString(value) {
62181
+ return `'${value.replace(/'/g, "''")}'`;
62182
+ }
62183
+ async function generateModuleFeedTableSpecs(input) {
62184
+ const kinds = input.kinds ?? DEFAULT_FEED_TABLE_KINDS;
62185
+ const specs = [];
62186
+ for (const kind of kinds) {
62187
+ const columns = FEED_TABLE_SCHEMAS[kind];
62188
+ const table = `${input.catalog}.${input.schema}.${sqlIdentifier2(input.namespace)}_${kind}`;
62189
+ const replayKey = columns[0].name;
62190
+ const publishableFields = columns.map((column) => column.name);
62191
+ const fingerprint = await databricksResourceFingerprint({
62192
+ scope: "databricks-ontology-feed-table",
62193
+ kind,
62194
+ namespace: input.namespace,
62195
+ catalog: input.catalog,
62196
+ schema: input.schema,
62197
+ columns
62198
+ });
62199
+ const ddl = renderFeedTableDdl({
62200
+ kind,
62201
+ table,
62202
+ replayKey,
62203
+ columns,
62204
+ fingerprint,
62205
+ namespace: input.namespace
62206
+ });
62207
+ specs.push({ kind, table, ddl, replayKey, publishableFields });
62208
+ }
62209
+ return specs;
62210
+ }
62211
+ function renderFeedTableDdl(input) {
62212
+ const [catalog, schema, name] = input.table.split(".");
62213
+ const lines = [
62214
+ `-- ${MANAGED_MARKER2}: true`,
62215
+ `-- fabric-harness.feed-fingerprint: ${input.fingerprint}`,
62216
+ "-- Generated by planModuleOntologyExport (@fabric-harness/databricks); do not edit by hand.",
62217
+ `-- Append-only, replay-safe operational-truth feed (design \xA73.7). Re-hydration merges on \`${input.replayKey}\` (MERGE ... WHEN NOT MATCHED THEN INSERT), so replays are idempotent.`,
62218
+ `CREATE TABLE IF NOT EXISTS ${qualifiedName3(catalog, schema, name)} (`,
62219
+ ...input.columns.map(
62220
+ (column, index) => ` \`${column.name}\` ${column.type} COMMENT ${sqlString(column.comment)}${index < input.columns.length - 1 ? "," : ""}`
62221
+ ),
62222
+ ")",
62223
+ "USING DELTA",
62224
+ `COMMENT ${sqlString(
62225
+ `Fabric module ${input.namespace} ${input.kind} feed: ${FEED_TABLE_CONTENT[input.kind]} (design \xA73.7).`
62226
+ )}`,
62227
+ "TBLPROPERTIES (",
62228
+ ` 'delta.appendOnly' = 'true',`,
62229
+ ` ${sqlString(MANAGED_MARKER2)} = 'true',`,
62230
+ ` 'fabric-harness.feed-kind' = '${input.kind}'`,
62231
+ ");"
62232
+ ];
62233
+ return `${lines.join("\n")}
62234
+ `;
62235
+ }
62236
+ async function materializeOntologyFeedRows(input) {
62237
+ if (typeof input.producer !== "function") {
62238
+ throw new Error(
62239
+ `Databricks ontology feed ${input.spec.table} requires the vertical's redacted-record producer; raw records cannot be materialized without it (design \xA73.7, KD-10).`
62240
+ );
62241
+ }
62242
+ const rows = [];
62243
+ for (const [index, record2] of input.records.entries()) {
62244
+ const redacted = await input.producer(record2);
62245
+ if (!redacted || typeof redacted !== "object" || Array.isArray(redacted)) {
62246
+ throw new Error(
62247
+ `Databricks ontology feed ${input.spec.table} producer must return a redacted record object (record ${index}).`
62248
+ );
62249
+ }
62250
+ const row = {};
62251
+ for (const field of input.spec.publishableFields) {
62252
+ if (redacted[field] !== void 0) row[field] = redacted[field];
62253
+ }
62254
+ const replayKey = row[input.spec.replayKey];
62255
+ if (typeof replayKey !== "string" || !replayKey) {
62256
+ throw new Error(
62257
+ `Databricks ontology feed ${input.spec.table} row ${index} is missing replay key "${input.spec.replayKey}" after redaction and allowlist projection; the row could not merge idempotently.`
62258
+ );
62259
+ }
62260
+ rows.push(row);
62261
+ }
62262
+ return rows;
62263
+ }
62264
+
62265
+ // src/ontology-genie-spec.ts
62266
+ var SENSITIVE_FIELD_FRAGMENTS = [
62267
+ "password",
62268
+ "passwd",
62269
+ "passphrase",
62270
+ "secret",
62271
+ "token",
62272
+ "ssn",
62273
+ "credential",
62274
+ "apikey",
62275
+ "privatekey",
62276
+ "bearer",
62277
+ "cookie"
62278
+ ];
62279
+ var FEED_TABLE_KINDS = [
62280
+ "asset_events",
62281
+ "action_invocations",
62282
+ "policy_evaluations"
62283
+ ];
62284
+ var FEED_TABLE_DESCRIPTIONS = {
62285
+ asset_events: "Append-only domain event history with schema versions (design \xA73.7).",
62286
+ action_invocations: "Action invocation history with terminal status and redacted parameters (design \xA73.7).",
62287
+ policy_evaluations: "Policy evaluation evidence with decisions and evaluator identity (design \xA73.7)."
62288
+ };
62289
+ function normalizeFieldFragment(value) {
62290
+ return value.toLowerCase().replace(/[^a-z0-9]+/g, "");
62291
+ }
62292
+ function isSensitiveFieldName(name, sensitiveFieldPatterns) {
62293
+ const normalized = normalizeFieldFragment(name);
62294
+ if (!normalized) return true;
62295
+ const fragments = [
62296
+ ...SENSITIVE_FIELD_FRAGMENTS,
62297
+ ...(sensitiveFieldPatterns ?? []).map(normalizeFieldFragment)
62298
+ ].filter(Boolean);
62299
+ return fragments.some((fragment) => normalized.includes(fragment));
62300
+ }
62301
+ function actionParameterNames(action) {
62302
+ if (action.parameterSchemaFormat !== "json-schema") return [];
62303
+ const schema = action.parameterSchema;
62304
+ if (!schema || typeof schema !== "object" || !schema.properties) return [];
62305
+ return Object.keys(schema.properties);
62306
+ }
62307
+ function qualifiedTable(catalog, schema, name) {
62308
+ return `${catalog}.${schema}.${name}`;
62309
+ }
62310
+ function metricViewName(namespace) {
62311
+ return `${sqlIdentifier2(namespace)}_metrics`;
62312
+ }
62313
+ function feedTableNames(namespace, catalog, schema, feed) {
62314
+ if (!feed) return [];
62315
+ const kinds = feed.tables ?? FEED_TABLE_KINDS;
62316
+ return kinds.map((kind) => ({
62317
+ table: qualifiedTable(catalog, schema, `${sqlIdentifier2(namespace)}_${kind}`),
62318
+ description: FEED_TABLE_DESCRIPTIONS[kind]
62319
+ }));
62320
+ }
62321
+ function unique(values) {
62322
+ return [...new Set(values)];
62323
+ }
62324
+ function metricViewColumns(manifest, glossaryTerms, classifications) {
62325
+ const terms = glossaryTerms.filter((term) => term.metadata?.namespace === manifest.namespace);
62326
+ const columns = [
62327
+ {
62328
+ name: "object_type",
62329
+ description: `Declared object types of the ${manifest.namespace} module.`,
62330
+ synonyms: unique(
62331
+ terms.filter((term) => term.kind === "object-type").map((term) => term.name)
62332
+ )
62333
+ }
62334
+ ];
62335
+ if (manifest.stateMachines.length > 0) {
62336
+ columns.push({
62337
+ name: "state",
62338
+ description: `Lifecycle state of the entity (${manifest.stateMachines.map((machine) => machine.entityType).join(", ")}).`,
62339
+ synonyms: unique(terms.filter((term) => term.kind === "state").map((term) => term.name))
62340
+ });
62341
+ }
62342
+ for (const measure of deriveModuleMetricViewMeasures(manifest, classifications)) {
62343
+ columns.push({ name: measure.name, description: measure.comment });
62344
+ }
62345
+ return columns;
62346
+ }
62347
+ function measureSnippets(manifests, classifications) {
62348
+ return manifests.flatMap(
62349
+ (manifest) => deriveModuleMetricViewMeasures(manifest, classifications).map((measure) => ({
62350
+ sql: measure.expr,
62351
+ alias: measure.name,
62352
+ displayName: measure.name
62353
+ }))
62354
+ );
62355
+ }
62356
+ function deriveJoins(manifests, catalog, schema) {
62357
+ const bundled = new Set(manifests.map((manifest) => manifest.namespace));
62358
+ const seen = /* @__PURE__ */ new Set();
62359
+ const joins = [];
62360
+ for (const manifest of manifests) {
62361
+ for (const dependency of manifest.dependencies) {
62362
+ if (!bundled.has(dependency.namespace)) continue;
62363
+ const pairKey = [dependency.namespace, manifest.namespace].sort().join("|");
62364
+ if (seen.has(pairKey)) continue;
62365
+ seen.add(pairKey);
62366
+ const leftAlias = sqlIdentifier2(dependency.namespace);
62367
+ const rightAlias = sqlIdentifier2(manifest.namespace);
62368
+ joins.push({
62369
+ left: {
62370
+ identifier: qualifiedTable(catalog, schema, metricViewName(dependency.namespace)),
62371
+ alias: leftAlias
62372
+ },
62373
+ right: {
62374
+ identifier: qualifiedTable(catalog, schema, metricViewName(manifest.namespace)),
62375
+ alias: rightAlias
62376
+ },
62377
+ sql: `\`${leftAlias}\`.object_type = \`${rightAlias}\`.object_type`
62378
+ });
62379
+ }
62380
+ }
62381
+ return joins;
62382
+ }
62383
+ function displayNameFor(manifest, type) {
62384
+ return manifest.objectTypes.find((objectType) => objectType.type === type)?.displayName ?? type;
62385
+ }
62386
+ function deriveSampleQuestions(manifests, sensitiveFieldPatterns) {
62387
+ const questions = [];
62388
+ for (const manifest of manifests) {
62389
+ for (const machine of manifest.stateMachines) {
62390
+ const entity = displayNameFor(manifest, machine.entityType);
62391
+ for (const state of machine.states) {
62392
+ questions.push({
62393
+ question: `Which ${entity} records are currently in the "${state.id}" state?`
62394
+ });
62395
+ }
62396
+ }
62397
+ for (const action of manifest.actions) {
62398
+ if (action.mutatesDomain) {
62399
+ const [event] = action.emitsEvents;
62400
+ questions.push({
62401
+ question: event ? `How many "${event}" events has the ${manifest.namespace} module recorded?` : `How many times has the "${action.actionId}" action been recorded in the ${manifest.namespace} module?`
62402
+ });
62403
+ }
62404
+ const slotHints = actionParameterNames(action).filter((name) => !isSensitiveFieldName(name, sensitiveFieldPatterns)).slice(0, 3);
62405
+ if (slotHints.length > 0) {
62406
+ questions.push({
62407
+ question: `Show ${manifest.namespace} records by ${slotHints.join(" and ")}.`
62408
+ });
62409
+ }
62410
+ }
62411
+ }
62412
+ return questions;
62413
+ }
62414
+ function deriveInstruction(manifests) {
62415
+ const namespaces = manifests.map((manifest) => manifest.namespace);
62416
+ const descriptions = manifests.map(
62417
+ (manifest) => manifest.description ?? `Fabric module ${manifest.namespace} ontology.`
62418
+ );
62419
+ const lifecycle = manifests.flatMap(
62420
+ (manifest) => manifest.eventTypes.map(
62421
+ (eventType) => `${eventType.eventType} (schema v${eventType.schemaVersion})`
62422
+ )
62423
+ ).join(", ");
62424
+ return [
62425
+ `You are a read-only analytics assistant over the Fabric-governed ${namespaces.join(" and ")} ontology.`,
62426
+ ...descriptions,
62427
+ ...lifecycle ? [`Lifecycle vocabulary: ${lifecycle}.`] : [],
62428
+ "Domain mutations are governed by Fabric Platform actions and never flow through this Genie Agent: answer questions only by reading the declared metric views and feed tables, and never attempt to create, update, invoke, approve, or delete records."
62429
+ ].join(" ");
62430
+ }
62431
+ async function deriveOntologyGenieSpec(input) {
62432
+ const { manifests, catalog, schema, genie } = input;
62433
+ const dataSources = manifests.flatMap((manifest) => {
62434
+ const view = {
62435
+ kind: "metric-view",
62436
+ table: qualifiedTable(catalog, schema, metricViewName(manifest.namespace)),
62437
+ description: manifest.description ?? `Fabric module ${manifest.namespace} ontology metric view.`,
62438
+ columns: metricViewColumns(manifest, input.glossaryTerms, input.classifications)
62439
+ };
62440
+ const feeds = feedTableNames(manifest.namespace, catalog, schema, input.feed).map((feed) => ({
62441
+ kind: "table",
62442
+ table: feed.table,
62443
+ description: `${manifest.namespace} module feed \u2014 ${feed.description}`
62444
+ }));
62445
+ return [view, ...feeds];
62446
+ });
62447
+ const measures = measureSnippets(manifests, input.classifications);
62448
+ const joins = deriveJoins(manifests, catalog, schema);
62449
+ const sampleQuestions = deriveSampleQuestions(manifests, genie.sensitiveFieldPatterns);
62450
+ const titles = manifests.map((manifest) => manifest.displayName ?? manifest.namespace);
62451
+ const descriptions = manifests.map((manifest) => manifest.description).filter((description) => Boolean(description));
62452
+ const spec = await normalizeDatabricksGenieAgentSpec({
62453
+ version: 2,
62454
+ title: `${titles.join(" + ")} Ontology`,
62455
+ ...descriptions.length ? { description: descriptions.join(" ") } : {},
62456
+ parentPath: genie.parentPath,
62457
+ warehouseId: genie.warehouseId,
62458
+ ...sampleQuestions.length ? { sampleQuestions } : {},
62459
+ dataSources,
62460
+ instructions: [deriveInstruction(manifests)],
62461
+ ...joins.length ? { joins } : {},
62462
+ ...measures.length ? { sqlSnippets: { measures } } : {}
62463
+ });
62464
+ return {
62465
+ spec,
62466
+ fingerprint: await databricksGenieAgentSpecFingerprint(spec, genie.agentId)
62467
+ };
62468
+ }
62469
+
62470
+ // src/ontology-glossary.ts
62471
+ function formatLiteral(value) {
62472
+ return value === void 0 ? "<unspecified>" : JSON.stringify(value);
62473
+ }
62474
+ function summarizeCondition(condition) {
62475
+ switch (condition.type) {
62476
+ case "comparison":
62477
+ return `${condition.conditionId}: ${condition.left.source}.${condition.left.path} ${condition.operator} ${formatLiteral(condition.value)}`;
62478
+ case "parameter":
62479
+ return `${condition.conditionId}: parameters.${condition.path} ${condition.operator} ${formatLiteral(condition.value)}`;
62480
+ case "always":
62481
+ return `${condition.conditionId}: always ${condition.result}`;
62482
+ case "all":
62483
+ case "any":
62484
+ return `${condition.conditionId}: ${condition.type}(${condition.conditions.map(summarizeCondition).join("; ")})`;
62485
+ case "not":
62486
+ return `${condition.conditionId}: not(${summarizeCondition(condition.condition)})`;
62487
+ }
62488
+ }
62489
+ function uniqueTermName(base2, used) {
62490
+ if (!used.has(base2)) {
62491
+ used.add(base2);
62492
+ return base2;
62493
+ }
62494
+ for (let suffix = 2; ; suffix += 1) {
62495
+ const candidate = `${base2} (${suffix})`;
62496
+ if (!used.has(candidate)) {
62497
+ used.add(candidate);
62498
+ return candidate;
62499
+ }
62500
+ }
62501
+ }
62502
+ function generateModuleGlossary(input) {
62503
+ const { manifest, domain: domain2 } = input;
62504
+ const terms = [];
62505
+ const usedNames = /* @__PURE__ */ new Set();
62506
+ const baseMetadata = {
62507
+ namespace: manifest.namespace,
62508
+ manifestVersion: String(manifest.manifestVersion)
62509
+ };
62510
+ const push = (term) => {
62511
+ terms.push({ ...term, name: uniqueTermName(term.name, usedNames) });
62512
+ };
62513
+ for (const objectType of manifest.objectTypes) {
62514
+ const registryPrefix = manifest.idPrefixes.find((entry) => entry.name === objectType.type);
62515
+ const idPrefix = objectType.idPrefix ?? registryPrefix?.prefix;
62516
+ push({
62517
+ kind: "object-type",
62518
+ name: objectType.displayName ?? objectType.type,
62519
+ domain: domain2,
62520
+ definition: `Object type "${objectType.type}" declared by the ${manifest.namespace} module.`,
62521
+ metadata: {
62522
+ ...baseMetadata,
62523
+ objectType: objectType.type,
62524
+ ...idPrefix ? { idPrefix } : {}
62525
+ }
62526
+ });
62527
+ }
62528
+ for (const subjectType of manifest.subjectTypes) {
62529
+ push({
62530
+ kind: "subject-type",
62531
+ name: subjectType,
62532
+ domain: domain2,
62533
+ definition: `Subject type "${subjectType}" \u2014 an actor in the ${manifest.namespace} module ontology, not a measure.`,
62534
+ metadata: { ...baseMetadata, subjectType }
62535
+ });
62536
+ }
62537
+ for (const eventType of manifest.eventTypes) {
62538
+ push({
62539
+ kind: "event-type",
62540
+ name: eventType.eventType,
62541
+ domain: domain2,
62542
+ definition: eventType.description ?? `Domain event "${eventType.eventType}" declared by the ${manifest.namespace} module.`,
62543
+ metadata: {
62544
+ ...baseMetadata,
62545
+ eventType: eventType.eventType,
62546
+ schemaVersion: String(eventType.schemaVersion)
62547
+ }
62548
+ });
62549
+ }
62550
+ for (const action of manifest.actions) {
62551
+ if (!action.mutatesDomain) continue;
62552
+ push({
62553
+ kind: "lifecycle-action",
62554
+ name: action.actionId,
62555
+ domain: domain2,
62556
+ definition: `Lifecycle action "${action.actionId}" (v${action.version}) of the ${manifest.namespace} module. Domain mutations are governed by Platform; analytics observe the recorded lifecycle events, never invoke the action.`,
62557
+ metadata: {
62558
+ ...baseMetadata,
62559
+ actionId: action.actionId,
62560
+ actionVersion: String(action.version),
62561
+ ...action.emitsEvents.length > 0 ? { emitsEvents: action.emitsEvents.join(",") } : {}
62562
+ }
62563
+ });
62564
+ }
62565
+ const classificationsByPolicyId = new Map(
62566
+ input.classifications.filter((classification) => classification.namespace === manifest.namespace).map((classification) => [classification.policyId, classification])
62567
+ );
62568
+ for (const policy of manifest.policies) {
62569
+ const classification = classificationsByPolicyId.get(policy.policyId);
62570
+ if (classification?.target !== "glossary-business-rule") continue;
62571
+ const reference = policy.type === "code" ? `${policy.policyId}@v${policy.version}` : `${policy.policyId}@${policy.policyVersion}`;
62572
+ const definition = policy.type === "code" ? `Governed business rule ${reference}. The evaluator logic is Platform-internal and never exported; this term records the rule reference only.` : `Governed business rule ${reference}. Data definition: ${(policy.dataDefinition?.conditions ?? []).map(summarizeCondition).join("; ") || "<none declared>"}.`;
62573
+ push({
62574
+ kind: "business-rule",
62575
+ name: policy.policyId,
62576
+ domain: domain2,
62577
+ definition,
62578
+ metadata: {
62579
+ ...baseMetadata,
62580
+ policy: reference,
62581
+ policyClass: classification.class,
62582
+ policyType: policy.type
62583
+ }
62584
+ });
62585
+ }
62586
+ for (const stateMachine of manifest.stateMachines) {
62587
+ for (const state of stateMachine.states) {
62588
+ push({
62589
+ kind: "state",
62590
+ name: state.label,
62591
+ domain: domain2,
62592
+ definition: `State "${state.id}" (${state.stateClass}) of the ${stateMachine.entityType} lifecycle in the ${manifest.namespace} module.`,
62593
+ metadata: {
62594
+ ...baseMetadata,
62595
+ entityType: stateMachine.entityType,
62596
+ stateId: state.id,
62597
+ stateClass: state.stateClass
62598
+ }
62599
+ });
62600
+ }
62601
+ }
62602
+ return {
62603
+ terms,
62604
+ domainAssignment: {
62605
+ namespace: manifest.namespace,
62606
+ domain: domain2,
62607
+ ...manifest.description ? { comment: manifest.description } : {}
62608
+ }
62609
+ };
62610
+ }
62611
+
62612
+ // src/ontology-export.ts
62613
+ var DATABRICKS_ONTOLOGY_EXPORT_PLAN_VERSION = "1";
62614
+ function normalizeModuleManifests(manifest) {
62615
+ return isModuleManifestBundle(manifest) ? manifest.modules : [manifest];
62616
+ }
62617
+ function isModuleManifestBundle(manifest) {
62618
+ return Array.isArray(manifest.modules);
62619
+ }
62620
+ async function planModuleOntologyExport(options) {
62621
+ if (!options.catalog.trim()) {
62622
+ throw new Error("Databricks ontology export requires a non-empty target catalog.");
62623
+ }
62624
+ if (!options.schema.trim()) {
62625
+ throw new Error("Databricks ontology export requires a non-empty target schema.");
62626
+ }
62627
+ const manifests = normalizeModuleManifests(options.manifest);
62628
+ if (manifests.length === 0) {
62629
+ throw new Error("Databricks ontology export requires at least one module manifest.");
62630
+ }
62631
+ const targets = manifests.map((manifest) => ({
62632
+ namespace: manifest.namespace,
62633
+ manifestVersion: manifest.manifestVersion,
62634
+ catalog: options.catalog,
62635
+ schema: options.schema,
62636
+ domain: options.domain ?? manifest.namespace
62637
+ }));
62638
+ const policyClassifications = [];
62639
+ const metricViewDdl = [];
62640
+ const glossaryTerms = [];
62641
+ const domainAssignments = [];
62642
+ const feedTables = [];
62643
+ for (const manifest of manifests) {
62644
+ const classifications = classifyModuleOntologyPolicies(manifest, {
62645
+ metricSeedPolicyIds: options.metricSeedPolicyIds
62646
+ });
62647
+ policyClassifications.push(...classifications);
62648
+ metricViewDdl.push(
62649
+ await generateModuleMetricViewDdl({
62650
+ manifest,
62651
+ catalog: options.catalog,
62652
+ schema: options.schema,
62653
+ classifications
62654
+ })
62655
+ );
62656
+ const glossary = generateModuleGlossary({
62657
+ manifest,
62658
+ domain: options.domain ?? manifest.namespace,
62659
+ classifications
62660
+ });
62661
+ glossaryTerms.push(...glossary.terms);
62662
+ domainAssignments.push(glossary.domainAssignment);
62663
+ if (options.feed) {
62664
+ feedTables.push(
62665
+ ...await generateModuleFeedTableSpecs({
62666
+ namespace: manifest.namespace,
62667
+ catalog: options.catalog,
62668
+ schema: options.schema,
62669
+ kinds: options.feed.tables
62670
+ })
62671
+ );
62672
+ }
62673
+ }
62674
+ const fingerprint = await databricksResourceFingerprint({
62675
+ plan: "databricks-ontology-export",
62676
+ planVersion: DATABRICKS_ONTOLOGY_EXPORT_PLAN_VERSION,
62677
+ manifest: options.manifest,
62678
+ catalog: options.catalog,
62679
+ schema: options.schema,
62680
+ domain: options.domain ?? null,
62681
+ metricSeedPolicyIds: options.metricSeedPolicyIds ?? [],
62682
+ genie: options.genie ? {
62683
+ agentId: options.genie.agentId ?? null,
62684
+ parentPath: options.genie.parentPath,
62685
+ warehouseId: options.genie.warehouseId,
62686
+ sensitiveFieldPatterns: options.genie.sensitiveFieldPatterns ?? []
62687
+ } : null,
62688
+ feed: options.feed ?? null
62689
+ });
62690
+ const derivedGenie = options.genie ? await deriveOntologyGenieSpec({
62691
+ manifests,
62692
+ catalog: options.catalog,
62693
+ schema: options.schema,
62694
+ classifications: policyClassifications,
62695
+ glossaryTerms,
62696
+ genie: options.genie,
62697
+ feed: options.feed
62698
+ }) : void 0;
62699
+ return {
62700
+ planVersion: DATABRICKS_ONTOLOGY_EXPORT_PLAN_VERSION,
62701
+ targets,
62702
+ metricViewDdl,
62703
+ glossaryTerms,
62704
+ domainAssignments,
62705
+ policyClassifications,
62706
+ ...derivedGenie ? { genieSpec: derivedGenie.spec, genieSpecFingerprint: derivedGenie.fingerprint } : {},
62707
+ ...options.feed ? { feedTables } : {},
62708
+ fingerprint
62709
+ };
62710
+ }
62711
+
62712
+ // src/ontology-apply.ts
62713
+ var GENIE_RESOURCE_TYPE2 = "genie-agent";
62714
+ var METRIC_VIEW_RESOURCE_TYPE = "ontology-metric-view";
62715
+ var FEED_TABLE_RESOURCE_TYPE = "ontology-feed-table";
62716
+ var GLOSSARY_DOMAIN_RESOURCE_TYPE = "ontology-glossary-domain";
62717
+ var EXPORT_RESOURCE_TYPE = "ontology-export";
62718
+ var MANAGED_TAG2 = "fabric-harness.managed";
62719
+ var DEFAULT_POLL = { intervalMs: 2e3, timeoutMs: 12e4 };
62720
+ async function applyModuleOntologyExport(plan, options) {
62721
+ const principal = nonEmpty6(options.principal, "principal");
62722
+ const toolCallId = nonEmpty6(options.toolCallId, "toolCallId");
62723
+ nonEmpty6(options.warehouseId, "warehouseId");
62724
+ if (plan.metricViewDdl.length !== plan.targets.length) {
62725
+ throw new Error(
62726
+ `Databricks ontology export plan is inconsistent: ${plan.metricViewDdl.length} DDL statements for ${plan.targets.length} targets.`
62727
+ );
62728
+ }
62729
+ const now = options.now ?? (() => /* @__PURE__ */ new Date());
62730
+ const store = options.resourceStore;
62731
+ const approve = (operation, resource, contentFingerprint, summary) => options.approval({
62732
+ effect: "write",
62733
+ operation,
62734
+ resource,
62735
+ principal,
62736
+ toolCallId,
62737
+ planFingerprint: plan.fingerprint,
62738
+ contentFingerprint,
62739
+ summary
62740
+ });
62741
+ if (plan.genieSpec) {
62742
+ if (!plan.genieSpecFingerprint) {
62743
+ throw new Error(
62744
+ "Databricks ontology export plan carries a Genie spec without genieSpecFingerprint."
62745
+ );
62746
+ }
62747
+ if (!options.genie?.admin) {
62748
+ throw new Error(
62749
+ "Databricks ontology export apply requires genie.admin when the plan carries a Genie spec."
62750
+ );
62751
+ }
62752
+ if (specHasSqlFragments(plan.genieSpec)) {
62753
+ if (!options.sqlPolicy) {
62754
+ throw new Error(
62755
+ "Databricks ontology export apply requires a DatabricksGenieSqlPolicy before admitting plan SQL fragments into the Genie Agent spec."
62756
+ );
62757
+ }
62758
+ await validateDatabricksGenieSqlReferences(plan.genieSpec, options.sqlPolicy);
62759
+ }
62760
+ }
62761
+ let changed = false;
62762
+ const feedTables = [];
62763
+ for (const spec of plan.feedTables ?? []) {
62764
+ const fingerprint = await databricksResourceFingerprint({
62765
+ scope: "databricks-ontology-feed-table-ddl",
62766
+ ddl: spec.ddl
62767
+ });
62768
+ const provenanceRecordId = `${FEED_TABLE_RESOURCE_TYPE}/${spec.table}`;
62769
+ const existing = await store.get(FEED_TABLE_RESOURCE_TYPE, spec.table);
62770
+ if (existing?.fingerprint === fingerprint) {
62771
+ feedTables.push({
62772
+ kind: spec.kind,
62773
+ table: spec.table,
62774
+ fingerprint,
62775
+ applied: false,
62776
+ provenanceRecordId
62777
+ });
62778
+ continue;
62779
+ }
62780
+ const [catalog] = spec.table.split(".");
62781
+ await approve(
62782
+ "feed-table-ddl",
62783
+ { kind: "feed-table", id: spec.table, ...catalog ? { catalog } : {} },
62784
+ fingerprint,
62785
+ `Create operational-truth feed table ${spec.table} (${spec.kind}) from ontology export.`
62786
+ );
62787
+ const response = await runStatement(
62788
+ options.statements,
62789
+ options.warehouseId,
62790
+ spec.ddl,
62791
+ [],
62792
+ options.poll ?? DEFAULT_POLL
62793
+ );
62794
+ assertStatementSucceeded(response, `feed table ${spec.table}`);
62795
+ const appliedAt2 = now().toISOString();
62796
+ await store.put({
62797
+ resourceType: FEED_TABLE_RESOURCE_TYPE,
62798
+ resourceId: spec.table,
62799
+ fingerprint,
62800
+ createdBy: existing?.createdBy ?? principal,
62801
+ toolCallId,
62802
+ createdAt: existing?.createdAt ?? appliedAt2,
62803
+ updatedAt: appliedAt2,
62804
+ metadata: {
62805
+ managed: MANAGED_TAG2,
62806
+ feedKind: spec.kind,
62807
+ replayKey: spec.replayKey,
62808
+ publishableFields: spec.publishableFields,
62809
+ planFingerprint: plan.fingerprint
62810
+ }
62811
+ });
62812
+ changed = true;
62813
+ feedTables.push({
62814
+ kind: spec.kind,
62815
+ table: spec.table,
62816
+ fingerprint,
62817
+ applied: true,
62818
+ provenanceRecordId
62819
+ });
62820
+ }
62821
+ const metricViews = [];
62822
+ for (const [index, ddl] of plan.metricViewDdl.entries()) {
62823
+ const target = plan.targets[index];
62824
+ const view = `${target.catalog}.${target.schema}.${sqlIdentifier2(target.namespace)}_metrics`;
62825
+ const fingerprint = await databricksResourceFingerprint({
62826
+ scope: "databricks-ontology-metric-view-ddl",
62827
+ ddl
62828
+ });
62829
+ const provenanceRecordId = `${METRIC_VIEW_RESOURCE_TYPE}/${view}`;
62830
+ const existing = await store.get(METRIC_VIEW_RESOURCE_TYPE, view);
62831
+ if (existing?.fingerprint === fingerprint) {
62832
+ metricViews.push({ view, fingerprint, applied: false, provenanceRecordId });
62833
+ continue;
62834
+ }
62835
+ await approve(
62836
+ "metric-view-ddl",
62837
+ { kind: "metric-view", id: view, catalog: target.catalog },
62838
+ fingerprint,
62839
+ `Apply UC metric view ${view} from ontology export.`
62840
+ );
62841
+ const response = await runStatement(
62842
+ options.statements,
62843
+ options.warehouseId,
62844
+ ddl,
62845
+ [],
62846
+ options.poll ?? DEFAULT_POLL
62847
+ );
62848
+ assertStatementSucceeded(response, `metric view ${view}`);
62849
+ const appliedAt2 = now().toISOString();
62850
+ await store.put({
62851
+ resourceType: METRIC_VIEW_RESOURCE_TYPE,
62852
+ resourceId: view,
62853
+ fingerprint,
62854
+ createdBy: existing?.createdBy ?? principal,
62855
+ toolCallId,
62856
+ createdAt: existing?.createdAt ?? appliedAt2,
62857
+ updatedAt: appliedAt2,
62858
+ metadata: {
62859
+ managed: MANAGED_TAG2,
62860
+ namespace: target.namespace,
62861
+ catalog: target.catalog,
62862
+ schema: target.schema,
62863
+ planFingerprint: plan.fingerprint
62864
+ }
62865
+ });
62866
+ changed = true;
62867
+ metricViews.push({ view, fingerprint, applied: true, provenanceRecordId });
62868
+ }
62869
+ const glossaryDomains = [];
62870
+ let glossaryTermCount = 0;
62871
+ for (const assignment of plan.domainAssignments) {
62872
+ const terms = plan.glossaryTerms.filter((term) => term.domain === assignment.domain);
62873
+ const fingerprint = await databricksResourceFingerprint({
62874
+ scope: "databricks-ontology-glossary-domain",
62875
+ assignment,
62876
+ terms
62877
+ });
62878
+ const provenanceRecordId = `${GLOSSARY_DOMAIN_RESOURCE_TYPE}/${assignment.domain}`;
62879
+ if (!options.glossary) {
62880
+ glossaryDomains.push({
62881
+ domain: assignment.domain,
62882
+ namespace: assignment.namespace,
62883
+ termCount: terms.length,
62884
+ fingerprint,
62885
+ applied: false,
62886
+ published: false
62887
+ });
62888
+ continue;
62889
+ }
62890
+ const existing = await store.get(GLOSSARY_DOMAIN_RESOURCE_TYPE, assignment.domain);
62891
+ if (existing?.fingerprint === fingerprint) {
62892
+ glossaryDomains.push({
62893
+ domain: assignment.domain,
62894
+ namespace: assignment.namespace,
62895
+ termCount: terms.length,
62896
+ fingerprint,
62897
+ applied: false,
62898
+ published: true,
62899
+ provenanceRecordId
62900
+ });
62901
+ continue;
62902
+ }
62903
+ const catalog = plan.targets.find(
62904
+ (target) => target.namespace === assignment.namespace
62905
+ )?.catalog;
62906
+ const resource = {
62907
+ kind: "glossary-domain",
62908
+ id: assignment.domain,
62909
+ ...catalog ? { catalog } : {}
62910
+ };
62911
+ await approve(
62912
+ "domain-assignment",
62913
+ resource,
62914
+ fingerprint,
62915
+ `Assign module namespace ${assignment.namespace} to UC domain ${assignment.domain}.`
62916
+ );
62917
+ await options.glossary.publishDomain(assignment);
62918
+ await approve(
62919
+ "glossary-publish",
62920
+ resource,
62921
+ fingerprint,
62922
+ `Publish ${terms.length} glossary terms to UC domain ${assignment.domain}.`
62923
+ );
62924
+ await options.glossary.upsertTerms(assignment.domain, terms);
62925
+ const appliedAt2 = now().toISOString();
62926
+ await store.put({
62927
+ resourceType: GLOSSARY_DOMAIN_RESOURCE_TYPE,
62928
+ resourceId: assignment.domain,
62929
+ fingerprint,
62930
+ createdBy: existing?.createdBy ?? principal,
62931
+ toolCallId,
62932
+ createdAt: existing?.createdAt ?? appliedAt2,
62933
+ updatedAt: appliedAt2,
62934
+ metadata: {
62935
+ managed: MANAGED_TAG2,
62936
+ namespace: assignment.namespace,
62937
+ termCount: terms.length,
62938
+ planFingerprint: plan.fingerprint
62939
+ }
62940
+ });
62941
+ changed = true;
62942
+ glossaryTermCount += terms.length;
62943
+ glossaryDomains.push({
62944
+ domain: assignment.domain,
62945
+ namespace: assignment.namespace,
62946
+ termCount: terms.length,
62947
+ fingerprint,
62948
+ applied: true,
62949
+ published: true,
62950
+ provenanceRecordId
62951
+ });
62952
+ }
62953
+ let genieAgent;
62954
+ if (plan.genieSpec && plan.genieSpecFingerprint && options.genie?.admin) {
62955
+ const spec = plan.genieSpec;
62956
+ const admin = options.genie.admin;
62957
+ const ownership2 = { principal, toolCallId };
62958
+ const agentId = options.genie.agentId;
62959
+ if (agentId) {
62960
+ genieAgent = await updateGenieAgent({
62961
+ plan,
62962
+ spec,
62963
+ admin,
62964
+ agentId,
62965
+ store,
62966
+ ownership: ownership2,
62967
+ approve,
62968
+ signal: options.signal,
62969
+ onWrite: () => {
62970
+ changed = true;
62971
+ }
62972
+ });
62973
+ } else {
62974
+ genieAgent = await createOrReuseGenieAgent({
62975
+ plan,
62976
+ spec,
62977
+ admin,
62978
+ store,
62979
+ ownership: ownership2,
62980
+ approve,
62981
+ signal: options.signal,
62982
+ onWrite: () => {
62983
+ changed = true;
62984
+ }
62985
+ });
62986
+ }
62987
+ }
62988
+ let appliedAt;
62989
+ let exportRecordId;
62990
+ if (changed) {
62991
+ appliedAt = now().toISOString();
62992
+ exportRecordId = `${EXPORT_RESOURCE_TYPE}/${plan.fingerprint}`;
62993
+ const existing = await store.get(EXPORT_RESOURCE_TYPE, plan.fingerprint);
62994
+ await store.put({
62995
+ resourceType: EXPORT_RESOURCE_TYPE,
62996
+ resourceId: plan.fingerprint,
62997
+ fingerprint: plan.fingerprint,
62998
+ createdBy: existing?.createdBy ?? principal,
62999
+ toolCallId,
63000
+ createdAt: existing?.createdAt ?? appliedAt,
63001
+ updatedAt: appliedAt,
63002
+ metadata: {
63003
+ managed: MANAGED_TAG2,
63004
+ planVersion: plan.planVersion,
63005
+ targets: plan.targets,
63006
+ metricViews: metricViews.map((metricView) => metricView.view),
63007
+ feedTables: feedTables.map((feedTable) => feedTable.table),
63008
+ glossaryDomains: glossaryDomains.filter((domain2) => domain2.published).map((domain2) => domain2.domain),
63009
+ ...plan.genieSpecFingerprint ? { genieSpecFingerprint: plan.genieSpecFingerprint } : {},
63010
+ ...genieAgent ? { genieAgentId: genieAgent.agentId, genieAgentFingerprint: genieAgent.fingerprint } : {}
63011
+ }
63012
+ });
63013
+ }
63014
+ return {
63015
+ planFingerprint: plan.fingerprint,
63016
+ noChanges: !changed,
63017
+ ...appliedAt ? { appliedAt } : {},
63018
+ metricViews,
63019
+ feedTables,
63020
+ glossaryDomains,
63021
+ glossaryTermCount,
63022
+ ...genieAgent ? { genieAgent } : {},
63023
+ ...exportRecordId ? { exportRecordId } : {}
63024
+ };
63025
+ }
63026
+ async function updateGenieAgent(context) {
63027
+ const { plan, spec, admin, agentId, store, ownership: ownership2, approve, signal } = context;
63028
+ const planFingerprint = plan.genieSpecFingerprint;
63029
+ const provenanceRecordId = `${GENIE_RESOURCE_TYPE2}/${agentId}`;
63030
+ const record2 = await requireDatabricksManagedResource({
63031
+ store,
63032
+ resourceType: GENIE_RESOURCE_TYPE2,
63033
+ resourceId: agentId
63034
+ });
63035
+ if (record2.fingerprint === planFingerprint) {
63036
+ const live = await admin.get(agentId, { includeConfiguration: true, ...signalOption4(signal) });
63037
+ if (live.fingerprint === record2.fingerprint) {
63038
+ return {
63039
+ agentId,
63040
+ fingerprint: record2.fingerprint,
63041
+ created: false,
63042
+ updated: false,
63043
+ provenanceRecordId
63044
+ };
63045
+ }
63046
+ throw new DatabricksManagedResourceConflictError({
63047
+ resourceType: GENIE_RESOURCE_TYPE2,
63048
+ resourceId: agentId,
63049
+ expectedFingerprint: record2.fingerprint,
63050
+ actualFingerprint: live.fingerprint
63051
+ });
63052
+ }
63053
+ await approve(
63054
+ "genie-agent-update",
63055
+ { kind: "genie-agent", id: agentId },
63056
+ planFingerprint,
63057
+ `Update Genie Agent ${agentId} to the exported ontology spec.`
63058
+ );
63059
+ const updated = await admin.update(
63060
+ agentId,
63061
+ { expectedFingerprint: record2.fingerprint, set: { spec } },
63062
+ { ownership: ownership2, ...signalOption4(signal) }
63063
+ );
63064
+ context.onWrite();
63065
+ return {
63066
+ agentId,
63067
+ fingerprint: updated.fingerprint,
63068
+ created: false,
63069
+ updated: true,
63070
+ provenanceRecordId
63071
+ };
63072
+ }
63073
+ async function createOrReuseGenieAgent(context) {
63074
+ const { plan, spec, admin, store, ownership: ownership2, approve, signal } = context;
63075
+ const exportRecord = await store.get(EXPORT_RESOURCE_TYPE, plan.fingerprint);
63076
+ const recordedAgentId = typeof exportRecord?.metadata?.genieAgentId === "string" ? exportRecord.metadata.genieAgentId : void 0;
63077
+ if (recordedAgentId) {
63078
+ const expectedFingerprint = await databricksGenieAgentSpecFingerprint(spec, recordedAgentId);
63079
+ const record2 = await store.get(GENIE_RESOURCE_TYPE2, recordedAgentId);
63080
+ if (record2) {
63081
+ const provenanceRecordId = `${GENIE_RESOURCE_TYPE2}/${recordedAgentId}`;
63082
+ if (record2.fingerprint !== expectedFingerprint) {
63083
+ throw new DatabricksManagedResourceConflictError({
63084
+ resourceType: GENIE_RESOURCE_TYPE2,
63085
+ resourceId: recordedAgentId,
63086
+ expectedFingerprint,
63087
+ actualFingerprint: record2.fingerprint
63088
+ });
63089
+ }
63090
+ const live = await admin.get(recordedAgentId, {
63091
+ includeConfiguration: true,
63092
+ ...signalOption4(signal)
63093
+ });
63094
+ if (live.fingerprint !== expectedFingerprint) {
63095
+ throw new DatabricksManagedResourceConflictError({
63096
+ resourceType: GENIE_RESOURCE_TYPE2,
63097
+ resourceId: recordedAgentId,
63098
+ expectedFingerprint,
63099
+ actualFingerprint: live.fingerprint
63100
+ });
63101
+ }
63102
+ return {
63103
+ agentId: recordedAgentId,
63104
+ fingerprint: expectedFingerprint,
63105
+ created: false,
63106
+ updated: false,
63107
+ provenanceRecordId
63108
+ };
63109
+ }
63110
+ }
63111
+ await approve(
63112
+ "genie-agent-create",
63113
+ { kind: "genie-agent", id: spec.title },
63114
+ plan.genieSpecFingerprint,
63115
+ `Create Genie Agent "${spec.title}" from the exported ontology spec.`
63116
+ );
63117
+ const created = await admin.create(spec, { ownership: ownership2, ...signalOption4(signal) });
63118
+ context.onWrite();
63119
+ return {
63120
+ agentId: created.agentId,
63121
+ fingerprint: created.fingerprint,
63122
+ created: true,
63123
+ updated: false,
63124
+ provenanceRecordId: `${GENIE_RESOURCE_TYPE2}/${created.agentId}`
63125
+ };
63126
+ }
63127
+ function specHasSqlFragments(spec) {
63128
+ return Boolean(
63129
+ spec.examples?.length || spec.joins?.length || spec.benchmarks?.length || spec.sqlSnippets?.filters?.length || spec.sqlSnippets?.expressions?.length || spec.sqlSnippets?.measures?.length
63130
+ );
63131
+ }
63132
+ function assertStatementSucceeded(response, label) {
63133
+ const status = response.status;
63134
+ const state = status?.state ?? "UNKNOWN";
63135
+ if (state !== "SUCCEEDED") {
63136
+ const detail = status?.error?.message ? `: ${status.error.message}` : "";
63137
+ throw new Error(
63138
+ `Databricks ontology export ${label} DDL did not succeed (state ${state})${detail}.`
63139
+ );
63140
+ }
63141
+ }
63142
+ function nonEmpty6(value, name) {
63143
+ const normalized = value.trim();
63144
+ if (!normalized)
63145
+ throw new Error(`Databricks ontology export apply requires a non-empty ${name}.`);
63146
+ return normalized;
63147
+ }
63148
+ function signalOption4(signal) {
63149
+ return signal ? { signal } : {};
63150
+ }
63151
+
63152
+ // src/platform.ts
63153
+ function databricksPlatformResourceRef(workspaceHost, resource) {
63154
+ return {
63155
+ provider: "databricks",
63156
+ resourceType: resource.resourceType,
63157
+ resourceId: resource.resourceId,
63158
+ ...resource.displayName ? { displayName: resource.displayName } : {},
63159
+ metadata: {
63160
+ workspaceHost: databricksWorkspaceOrigin(workspaceHost),
63161
+ ...resource.metadata ?? {}
63162
+ }
63163
+ };
63164
+ }
63165
+ function databricksExecutionAttestation(input) {
63166
+ return {
63167
+ provider: "databricks",
63168
+ operation: input.operation,
63169
+ externalOperationId: input.externalOperationId,
63170
+ ...input.resource && input.workspaceHost ? { resource: databricksPlatformResourceRef(input.workspaceHost, input.resource) } : {},
63171
+ evidenceReferences: [{
63172
+ referenceType: "external_observation",
63173
+ referenceId: input.externalOperationId,
63174
+ ...input.evidenceUri ? { uri: input.evidenceUri } : {},
63175
+ metadata: { provider: "databricks", operation: input.operation }
63176
+ }],
63177
+ ...input.satisfiesObligations ? { satisfiesObligations: input.satisfiesObligations } : {},
63178
+ startedAt: input.startedAt,
63179
+ ...input.completedAt ? { completedAt: input.completedAt } : {},
63180
+ ...input.metadata ? { metadata: input.metadata } : {}
63181
+ };
63182
+ }
63183
+
63184
+ // src/ontology-drift.ts
63185
+ var FINDING_KIND_ORDER = [
63186
+ "inferred-synonym-vs-declared-term",
63187
+ "inferred-expression-vs-declared-measure",
63188
+ "inferred-concept-without-declared-counterpart",
63189
+ "declared-definition-without-inferred-counterpart"
63190
+ ];
63191
+ function normalizeVocabulary(value) {
63192
+ return value.toLowerCase().replace(/[^a-z0-9]+/g, "");
63193
+ }
63194
+ function declaredEntries(plan) {
63195
+ const entries = [];
63196
+ for (const term of plan.glossaryTerms) {
63197
+ entries.push({
63198
+ reference: { kind: "term", id: term.name, domain: term.domain },
63199
+ normalized: normalizeVocabulary(term.name),
63200
+ referenced: false
63201
+ });
63202
+ }
63203
+ for (const classification of plan.policyClassifications) {
63204
+ if (classification.target !== "metric-view-measure") continue;
63205
+ entries.push({
63206
+ reference: { kind: "measure", id: classification.policyId, domain: classification.namespace },
63207
+ normalized: normalizeVocabulary(classification.policyId),
63208
+ referenced: false
63209
+ });
63210
+ }
63211
+ return entries;
63212
+ }
63213
+ function inferredReference(concept) {
63214
+ return { kind: concept.kind, id: concept.id, name: concept.name };
63215
+ }
63216
+ function emptyCounts() {
63217
+ return {
63218
+ "inferred-synonym-vs-declared-term": 0,
63219
+ "inferred-expression-vs-declared-measure": 0,
63220
+ "inferred-concept-without-declared-counterpart": 0,
63221
+ "declared-definition-without-inferred-counterpart": 0
63222
+ };
63223
+ }
63224
+ function compareOntologyDrift(input) {
63225
+ const declared = declaredEntries(input.plan);
63226
+ const terms = declared.filter((entry) => entry.reference.kind === "term");
63227
+ const measures = declared.filter((entry) => entry.reference.kind === "measure");
63228
+ const findings = [];
63229
+ const findByReference = (pool, reference) => {
63230
+ if (!reference) return void 0;
63231
+ const normalized = normalizeVocabulary(reference);
63232
+ return pool.find((entry) => entry.normalized === normalized);
63233
+ };
63234
+ for (const concept of input.inferred.concepts) {
63235
+ const pool = concept.kind === "expression" ? measures : terms;
63236
+ const byTarget = findByReference(pool, concept.target);
63237
+ const byName = findByReference(pool, concept.name);
63238
+ const matched = byTarget ?? byName;
63239
+ if (matched) matched.referenced = true;
63240
+ if (concept.kind === "synonym" || concept.kind === "expression") {
63241
+ if (!matched) {
63242
+ findings.push({
63243
+ kind: "inferred-concept-without-declared-counterpart",
63244
+ severity: "informational",
63245
+ inferred: inferredReference(concept),
63246
+ evidence: `Inferred ${concept.kind} "${concept.name}" maps onto no declared ${concept.kind === "expression" ? "measure" : "term"}; inference is analytics-only observation until a steward acts.`
63247
+ });
63248
+ continue;
63249
+ }
63250
+ if (byTarget && byName && byTarget !== byName) {
63251
+ findings.push({
63252
+ kind: concept.kind === "synonym" ? "inferred-synonym-vs-declared-term" : "inferred-expression-vs-declared-measure",
63253
+ severity: "conflict",
63254
+ declared: byTarget.reference,
63255
+ inferred: inferredReference(concept),
63256
+ evidence: `Inferred ${concept.kind} "${concept.name}" targets declared "${byTarget.reference.id}" but its label collides with declared "${byName.reference.id}"; the manifest stays authoritative and the contradiction is routed to steward review.`
63257
+ });
63258
+ continue;
63259
+ }
63260
+ if (byTarget && byName === byTarget) continue;
63261
+ findings.push({
63262
+ kind: concept.kind === "synonym" ? "inferred-synonym-vs-declared-term" : "inferred-expression-vs-declared-measure",
63263
+ severity: "analytics-only",
63264
+ declared: matched.reference,
63265
+ inferred: inferredReference(concept),
63266
+ evidence: `Inferred ${concept.kind} "${concept.name}" maps onto declared "${matched.reference.id}"; tolerated for analytics only \u2014 the declared definition remains authoritative and nothing is applied back.`
63267
+ });
63268
+ continue;
63269
+ }
63270
+ if (concept.kind === "concept" && matched) continue;
63271
+ findings.push({
63272
+ kind: "inferred-concept-without-declared-counterpart",
63273
+ severity: "informational",
63274
+ inferred: inferredReference(concept),
63275
+ evidence: `Inferred ${concept.kind} "${concept.name}" has no declared counterpart in the export plan; inference is analytics-only observation until a steward acts.`
63276
+ });
63277
+ }
63278
+ for (const entry of declared) {
63279
+ if (entry.referenced) continue;
63280
+ findings.push({
63281
+ kind: "declared-definition-without-inferred-counterpart",
63282
+ severity: "informational",
63283
+ declared: entry.reference,
63284
+ evidence: `Declared ${entry.reference.kind} "${entry.reference.id}" has no inferred counterpart in the knowledge store; informational only.`
63285
+ });
63286
+ }
63287
+ findings.sort((left, right) => {
63288
+ const kindDelta = FINDING_KIND_ORDER.indexOf(left.kind) - FINDING_KIND_ORDER.indexOf(right.kind);
63289
+ if (kindDelta !== 0) return kindDelta;
63290
+ const declaredDelta = (left.declared?.id ?? "").localeCompare(right.declared?.id ?? "");
63291
+ if (declaredDelta !== 0) return declaredDelta;
63292
+ return (left.inferred?.id ?? "").localeCompare(right.inferred?.id ?? "");
63293
+ });
63294
+ const counts = emptyCounts();
63295
+ for (const finding of findings) counts[finding.kind] += 1;
63296
+ return { planFingerprint: input.plan.fingerprint, findings, counts };
63297
+ }
63298
+ async function attestOntologyDrift(plan, options) {
63299
+ const stewardAudience = options.stewardAudience.trim();
63300
+ if (!stewardAudience) {
63301
+ throw new Error("Databricks ontology drift attestation requires a steward audience.");
63302
+ }
63303
+ const inferred = await options.knowledgeStore.readInferredConcepts({
63304
+ ...options.agentId ? { agentId: options.agentId } : {}
63305
+ });
63306
+ const comparison = compareOntologyDrift({ plan, inferred });
63307
+ const runId = await databricksResourceFingerprint({
63308
+ scope: "databricks-ontology-drift",
63309
+ planFingerprint: plan.fingerprint,
63310
+ stewardAudience,
63311
+ findings: comparison.findings
63312
+ });
63313
+ const now = (options.now ?? (() => /* @__PURE__ */ new Date()))().toISOString();
63314
+ const attestation = databricksExecutionAttestation({
63315
+ operation: "genie-ontology-drift",
63316
+ externalOperationId: runId,
63317
+ startedAt: now,
63318
+ completedAt: now,
63319
+ ...options.workspaceHost ? { workspaceHost: options.workspaceHost } : {},
63320
+ resource: {
63321
+ resourceType: "genie-knowledge-store",
63322
+ resourceId: options.agentId ?? "knowledge-store",
63323
+ displayName: "Genie Ontology declared-vs-inferred drift comparison"
63324
+ },
63325
+ metadata: {
63326
+ stewardAudience,
63327
+ planFingerprint: plan.fingerprint,
63328
+ findingCount: comparison.findings.length,
63329
+ findingsByKind: comparison.counts,
63330
+ conflictCount: comparison.findings.filter((finding) => finding.severity === "conflict").length
63331
+ }
63332
+ });
63333
+ const record2 = {
63334
+ runId,
63335
+ stewardAudience,
63336
+ planFingerprint: plan.fingerprint,
63337
+ comparison,
63338
+ attestation,
63339
+ recordedAt: now
63340
+ };
63341
+ await options.evidence.append(record2);
63342
+ return record2;
63343
+ }
63344
+
63345
+ // src/ontology-feed-pipeline.ts
63346
+ var HISTORY_KEYS = {
63347
+ asset_events: "assetEvents",
63348
+ action_invocations: "actionInvocations",
63349
+ policy_evaluations: "policyEvaluations"
63350
+ };
63351
+ function assetEventRecordFromEnvelope(envelope, namespace) {
63352
+ return {
63353
+ event_id: envelope.id,
63354
+ event_type: envelope.eventType,
63355
+ schema_version: envelope.eventSchemaVersion,
63356
+ ...namespace !== void 0 ? { namespace } : {},
63357
+ tenant_id: envelope.tenantId,
63358
+ object_type: envelope.subjectType,
63359
+ object_id: envelope.subjectId,
63360
+ occurred_at: isoTimestamp(envelope.occurredAt),
63361
+ correlation_id: envelope.correlationId,
63362
+ ...envelope.causationId !== void 0 ? { causation_id: envelope.causationId } : {},
63363
+ recorded_at: isoTimestamp(envelope.recordedAt),
63364
+ payload: envelope.payload
63365
+ };
63366
+ }
63367
+ async function planOntologyFeedHydration(input) {
63368
+ if (input.feedTables.length === 0) {
63369
+ throw new Error("Databricks ontology feed hydration requires at least one feed table spec.");
63370
+ }
63371
+ if (typeof input.producer !== "function") {
63372
+ throw new Error(
63373
+ "Databricks ontology feed hydration requires the vertical's redacted-record producer; raw records cannot be hydrated without it (design \xA73.7, KD-10)."
63374
+ );
63375
+ }
63376
+ const tables = [];
63377
+ for (const spec of input.feedTables) {
63378
+ const records = input.history[HISTORY_KEYS[spec.kind]] ?? [];
63379
+ const materialized = await materializeOntologyFeedRows({
63380
+ spec,
63381
+ records,
63382
+ producer: input.producer
63383
+ });
63384
+ const seen = /* @__PURE__ */ new Set();
63385
+ const rows = [];
63386
+ let droppedDuplicates = 0;
63387
+ for (const row of materialized) {
63388
+ const replayKey = row[spec.replayKey];
63389
+ if (seen.has(replayKey)) {
63390
+ droppedDuplicates += 1;
63391
+ continue;
63392
+ }
63393
+ seen.add(replayKey);
63394
+ rows.push(row);
63395
+ }
63396
+ rows.sort(
63397
+ (left, right) => String(left[spec.replayKey]).localeCompare(String(right[spec.replayKey]))
63398
+ );
63399
+ if (rows.length === 0) {
63400
+ tables.push({ spec, rows, droppedDuplicates, statement: null, statementFingerprint: null });
63401
+ continue;
63402
+ }
63403
+ const statementFingerprint = await databricksResourceFingerprint({
63404
+ scope: "databricks-ontology-feed-hydration",
63405
+ table: spec.table,
63406
+ replayKey: spec.replayKey,
63407
+ rows
63408
+ });
63409
+ tables.push({
63410
+ spec,
63411
+ rows,
63412
+ droppedDuplicates,
63413
+ statement: renderMergeStatement(spec, rows, statementFingerprint),
63414
+ statementFingerprint
63415
+ });
63416
+ }
63417
+ const fingerprint = await databricksResourceFingerprint({
63418
+ scope: "databricks-ontology-feed-hydration-plan",
63419
+ tables: tables.map((table) => ({
63420
+ table: table.spec.table,
63421
+ replayKey: table.spec.replayKey,
63422
+ rowCount: table.rows.length,
63423
+ statementFingerprint: table.statementFingerprint
63424
+ }))
63425
+ });
63426
+ return { fingerprint, tables };
63427
+ }
63428
+ function isoTimestamp(value) {
63429
+ return value instanceof Date ? value.toISOString() : String(value);
63430
+ }
63431
+ function sqlString2(value) {
63432
+ return `'${value.replace(/'/g, "''")}'`;
63433
+ }
63434
+ function sqlValue(value) {
63435
+ if (value === null || value === void 0) return "NULL";
63436
+ if (typeof value === "string") return sqlString2(value);
63437
+ if (typeof value === "number") {
63438
+ if (!Number.isFinite(value)) {
63439
+ throw new Error("Databricks ontology feed hydration cannot render a non-finite number.");
63440
+ }
63441
+ return String(value);
63442
+ }
63443
+ if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
63444
+ if (value instanceof Date) return sqlString2(value.toISOString());
63445
+ if (Array.isArray(value)) return `ARRAY(${value.map((item) => sqlValue(item)).join(", ")})`;
63446
+ return sqlString2(JSON.stringify(value));
63447
+ }
63448
+ function renderMergeStatement(spec, rows, fingerprint) {
63449
+ const [catalog, schema, name] = spec.table.split(".");
63450
+ const qualifiedTable2 = [catalog, schema, name].map((part) => `\`${part}\``).join(".");
63451
+ const columns = spec.publishableFields;
63452
+ const columnList = columns.map((column) => `\`${column}\``).join(", ");
63453
+ const stagingColumns = columns.map((column) => `staging.\`${column}\``).join(", ");
63454
+ const values = rows.map((row) => ` (${columns.map((column) => sqlValue(row[column])).join(", ")})`).join(",\n");
63455
+ return `-- fabric-harness.managed: true
63456
+ -- fabric-harness.feed-hydration-fingerprint: ${fingerprint}
63457
+ -- Replay-safe hydration (design \xA73.7): merges on \`${spec.replayKey}\`; re-applying inserts no duplicate rows.
63458
+ MERGE INTO ${qualifiedTable2} AS target
63459
+ USING (
63460
+ VALUES
63461
+ ${values}
63462
+ ) AS staging (${columnList})
63463
+ ON target.\`${spec.replayKey}\` = staging.\`${spec.replayKey}\`
63464
+ WHEN NOT MATCHED THEN
63465
+ INSERT (${columnList})
63466
+ VALUES (${stagingColumns});
63467
+ `;
63468
+ }
63469
+
61741
63470
  // src/authenticated-fetch.ts
61742
63471
  function createDatabricksAuthenticatedFetch(options) {
61743
63472
  let token = databricksIdentity(options.principal);
@@ -63405,17 +65134,23 @@ function resolveDatabricksRequiredChecks(requiredTiers, explicitlyRequired = [])
63405
65134
  }
63406
65135
  function validateDatabricksCertificationEnvironment(environment, requiredChecks) {
63407
65136
  const issues = [];
63408
- if (!present(environment, "DATABRICKS_HOST")) {
65137
+ const hasCliProfile = environment.DATABRICKS_AUTH_MODE?.trim() === "cli" || present(environment, "DATABRICKS_CONFIG_PROFILE") || present(environment, "DATABRICKS_PROFILE");
65138
+ if (!present(environment, "DATABRICKS_HOST") && !hasCliProfile) {
63409
65139
  issues.push(issue2("identity", ["DATABRICKS_HOST"], "workspace host is missing"));
63410
65140
  }
63411
65141
  const hasPat = present(environment, "DATABRICKS_TOKEN");
63412
65142
  const hasM2m = present(environment, "DATABRICKS_CLIENT_ID") && present(environment, "DATABRICKS_CLIENT_SECRET");
63413
- if (!hasPat && !hasM2m) {
65143
+ if (!hasPat && !hasM2m && !hasCliProfile) {
63414
65144
  issues.push(
63415
65145
  issue2(
63416
65146
  "identity",
63417
- ["DATABRICKS_TOKEN", "DATABRICKS_CLIENT_ID", "DATABRICKS_CLIENT_SECRET"],
63418
- "configure a PAT or both OAuth M2M credentials"
65147
+ [
65148
+ "DATABRICKS_TOKEN",
65149
+ "DATABRICKS_CLIENT_ID",
65150
+ "DATABRICKS_CLIENT_SECRET",
65151
+ "DATABRICKS_AUTH_MODE"
65152
+ ],
65153
+ "configure a PAT, both OAuth M2M credentials, or CLI-profile auth (DATABRICKS_AUTH_MODE=cli with a profile from `databricks auth login`)"
63419
65154
  )
63420
65155
  );
63421
65156
  }
@@ -64234,7 +65969,7 @@ var DATABRICKS_API_VERSIONS = {
64234
65969
  var DATABRICKS_PACKAGE_COMPATIBILITY = {
64235
65970
  node: ">=22.0.0",
64236
65971
  package: "@fabric-harness/databricks",
64237
- packageRange: ">=6.0.0 <7",
65972
+ packageRange: ">=7.0.0 <8",
64238
65973
  pg: "^8.11.0 (only for Lakebase)"
64239
65974
  };
64240
65975
  var DATABRICKS_AUTH_MODES = [
@@ -64363,10 +66098,14 @@ var DATABRICKS_PLATFORM_DOMAINS = [
64363
66098
  {
64364
66099
  id: "sql-and-bi",
64365
66100
  name: "SQL, dashboards, and AI/BI",
64366
- capabilityIds: ["sql-warehouse", "genie-query", "genie-authoring", "genie-agent-mode"],
64367
- knownGaps: [
64368
- "Dashboard, query, alert, warehouse, and metric-view authoring are not yet complete."
64369
- ]
66101
+ capabilityIds: [
66102
+ "sql-warehouse",
66103
+ "genie-query",
66104
+ "genie-authoring",
66105
+ "genie-agent-mode",
66106
+ "genie-ontology-export"
66107
+ ],
66108
+ knownGaps: ["Dashboard, query, alert, and warehouse authoring are not yet complete."]
64370
66109
  },
64371
66110
  {
64372
66111
  id: "apps-and-operational-data",
@@ -64910,6 +66649,34 @@ var DATABRICKS_CAPABILITY_DEFINITIONS = [
64910
66649
  requiredEvidence: ["genie-agent-mode.stream", "genie-agent-mode.terminal"],
64911
66650
  notes: "The upstream API is Beta and requires explicit workspace preview enrollment. Credential-free SSE contracts pass; no stable promotion is implied."
64912
66651
  },
66652
+ {
66653
+ id: "genie-ontology-export",
66654
+ name: "Genie Ontology export from Fabric module manifests",
66655
+ category: "data",
66656
+ status: "beta",
66657
+ tier: "first-party",
66658
+ certification: "contract",
66659
+ auth: ["oauth-m2m", "app-service-principal", "on-behalf-of", "pat"],
66660
+ contractClouds: ALL_CLOUDS,
66661
+ surfaces: [
66662
+ {
66663
+ package: "@fabric-harness/databricks",
66664
+ exports: ["planModuleOntologyExport", "applyModuleOntologyExport"]
66665
+ }
66666
+ ],
66667
+ docs: "/docs/databricks/authoring",
66668
+ requiredEvidence: [
66669
+ "genie-ontology.plan",
66670
+ "genie-ontology.apply",
66671
+ "genie-ontology.fingerprint-round-trip"
66672
+ ],
66673
+ upstreamMaturity: "preview",
66674
+ lifecycle: ["read", "create", "update"],
66675
+ knownGaps: [
66676
+ "Metric-view DDL, feed-table DDL, glossary publish, and Genie spec apply carry credential-free contract coverage only; retained protected-workspace apply evidence is pending against the preview Genie Ontology APIs. Feed table retention/compaction policy is a pre-dogfood gate."
66677
+ ],
66678
+ notes: "Remains Beta while Genie Ontology is in public preview; stable promotion requires retained protected-workspace evidence on non-preview APIs, the same rule as genie-authoring."
66679
+ },
64913
66680
  {
64914
66681
  id: "jobs-authoring",
64915
66682
  name: "Jobs authoring",
@@ -65780,27 +67547,52 @@ function databricksAnalyticsCostLimitFromEnv(env2, config2, bundle) {
65780
67547
  onExceed: "throw"
65781
67548
  });
65782
67549
  }
67550
+ function collectDatabricksEnvPrerequisites(env2, options = {}) {
67551
+ if (env2.FABRIC_MOCK === "1") return [];
67552
+ const missing = [];
67553
+ if (!env2.DATABRICKS_HOST?.trim()) {
67554
+ missing.push(
67555
+ "DATABRICKS_HOST \u2014 workspace URL (e.g. https://adb-1234567890.0.azuredatabricks.net)"
67556
+ );
67557
+ }
67558
+ if (options.analyticsCopilot === true) {
67559
+ if (!env2.DATABRICKS_WAREHOUSE_ID?.trim()) {
67560
+ missing.push(
67561
+ "DATABRICKS_WAREHOUSE_ID \u2014 SQL warehouse id (SELECT-only tools and cost enforcement; a stopped warehouse id is enough to start)"
67562
+ );
67563
+ }
67564
+ if (!env2.DATABRICKS_GENIE_SPACE_ID?.trim()) {
67565
+ missing.push("DATABRICKS_GENIE_SPACE_ID \u2014 Genie space id for the analytics copilot");
67566
+ }
67567
+ if (!env2.DATABRICKS_ANALYTICS_STEWARD_AUDIENCE?.trim()) {
67568
+ missing.push("DATABRICKS_ANALYTICS_STEWARD_AUDIENCE \u2014 governance steward audience");
67569
+ }
67570
+ if (!env2.DATABRICKS_COST_TENANT_ID?.trim()) {
67571
+ missing.push("DATABRICKS_COST_TENANT_ID \u2014 tenant id for per-day cost enforcement");
67572
+ }
67573
+ const perDayUsd = Number(env2.DATABRICKS_COST_PER_DAY_USD);
67574
+ if (!Number.isFinite(perDayUsd) || perDayUsd <= 0) {
67575
+ missing.push("DATABRICKS_COST_PER_DAY_USD \u2014 positive number for per-day cost enforcement");
67576
+ }
67577
+ }
67578
+ return missing;
67579
+ }
65783
67580
  function databricksConfigFromEnv(env2, options = {}) {
65784
- const rawHost = env2.DATABRICKS_HOST;
65785
- const host = rawHost ? rawHost.includes("://") ? rawHost : `https://${rawHost}` : void 0;
65786
- if (!host && env2.FABRIC_MOCK !== "1") {
67581
+ const analyticsCopilot = options.analyticsCopilot === true;
67582
+ const missing = collectDatabricksEnvPrerequisites(env2, { analyticsCopilot });
67583
+ if (missing.length > 0) {
67584
+ const rows = missing.map((row) => `- ${row}`).join("\n");
65787
67585
  throw new Error(
65788
- "Missing DATABRICKS_HOST environment variable. Set it or pass an explicit databricks config."
67586
+ `Databricks agent is missing required configuration (${missing.length} ${missing.length === 1 ? "issue" : "issues"}):
67587
+ ${rows}
67588
+ Set every variable in .env (see .env.example) or pass an explicit databricks config to defineDatabricksAgent.`
65789
67589
  );
65790
67590
  }
67591
+ const rawHost = env2.DATABRICKS_HOST;
67592
+ const host = rawHost ? rawHost.includes("://") ? rawHost : `https://${rawHost}` : void 0;
65791
67593
  const principal = env2.FABRIC_MOCK === "1" ? { kind: "pat", token: "mock-token" } : databricksPrincipalFromEnv(env2);
65792
- const analyticsCopilot = options.analyticsCopilot === true;
65793
67594
  const genieAgentId = env2.DATABRICKS_GENIE_SPACE_ID;
65794
67595
  const stewardAudience = env2.DATABRICKS_ANALYTICS_STEWARD_AUDIENCE;
65795
- if (analyticsCopilot && env2.FABRIC_MOCK !== "1" && !env2.DATABRICKS_WAREHOUSE_ID) {
65796
- throw new Error("Analytics copilot requires DATABRICKS_WAREHOUSE_ID.");
65797
- }
65798
- if (analyticsCopilot && env2.FABRIC_MOCK !== "1" && !genieAgentId) {
65799
- throw new Error("Analytics copilot requires DATABRICKS_GENIE_SPACE_ID.");
65800
- }
65801
- if (analyticsCopilot && env2.FABRIC_MOCK !== "1" && !stewardAudience) {
65802
- throw new Error("Analytics copilot requires DATABRICKS_ANALYTICS_STEWARD_AUDIENCE.");
65803
- }
65804
67596
  const catalog = env2.DATABRICKS_CATALOG?.trim();
65805
67597
  return {
65806
67598
  host: host ?? "https://mock.databricks.com",
@@ -65861,6 +67653,14 @@ function estimateUsage(messages) {
65861
67653
  const chars = messages.reduce((sum, m2) => sum + m2.content.length, 0);
65862
67654
  return { totalTokens: Math.ceil(chars / 4) };
65863
67655
  }
67656
+ function throwIfAborted2(signal) {
67657
+ if (!signal?.aborted) return;
67658
+ const reason = signal.reason;
67659
+ const message = reason instanceof Error ? reason.message : typeof reason === "string" ? reason : "This operation was aborted";
67660
+ const error51 = new Error(message);
67661
+ error51.name = "AbortError";
67662
+ throw error51;
67663
+ }
65864
67664
  var MockDatabricksModelProvider = class {
65865
67665
  name = "mock-databricks";
65866
67666
  options;
@@ -65868,6 +67668,13 @@ var MockDatabricksModelProvider = class {
65868
67668
  this.options = options;
65869
67669
  }
65870
67670
  async generate(request) {
67671
+ throwIfAborted2(request.signal);
67672
+ const response = this.buildResponse(request);
67673
+ await Promise.resolve();
67674
+ throwIfAborted2(request.signal);
67675
+ return response;
67676
+ }
67677
+ buildResponse(request) {
65871
67678
  const latest = [...request.messages].reverse().find((m2) => m2.role === "user");
65872
67679
  const text = latest?.content ?? "";
65873
67680
  if (this.options.responses) {
@@ -65897,9 +67704,11 @@ var MockDatabricksModelProvider = class {
65897
67704
  }
65898
67705
  async *stream(request) {
65899
67706
  const response = await this.generate(request);
67707
+ throwIfAborted2(request.signal);
65900
67708
  if (response.message?.content) {
65901
67709
  yield { textDelta: response.message.content };
65902
67710
  }
67711
+ throwIfAborted2(request.signal);
65903
67712
  if (response.toolCalls) {
65904
67713
  for (const toolCall of response.toolCalls) {
65905
67714
  yield {
@@ -66773,6 +68582,7 @@ export {
66773
68582
  DATABRICKS_CAPABILITIES,
66774
68583
  DATABRICKS_CAPABILITY_EVIDENCE,
66775
68584
  DATABRICKS_CERTIFICATION_RESOURCE_PREFIX,
68585
+ DATABRICKS_ONTOLOGY_EXPORT_PLAN_VERSION,
66776
68586
  DATABRICKS_OPTIONAL_CERTIFICATION_CHECKS,
66777
68587
  DATABRICKS_PACKAGE_COMPATIBILITY,
66778
68588
  DATABRICKS_PLATFORM_DOMAINS,
@@ -66809,8 +68619,12 @@ export {
66809
68619
  UcVolumesAttachmentStore,
66810
68620
  analyticsCopilotGovernance,
66811
68621
  appServicePrincipalFromEnv,
68622
+ applyModuleOntologyExport,
68623
+ assetEventRecordFromEnvelope,
68624
+ attestOntologyDrift,
66812
68625
  buildMlflowTrace,
66813
68626
  chooseDatabricksSqlWarehouse,
68627
+ compareOntologyDrift,
66814
68628
  connectDatabricksManagedMcpServer,
66815
68629
  createDatabricksAppUserAuthenticator,
66816
68630
  createDatabricksAuthenticatedFetch,
@@ -66852,6 +68666,7 @@ export {
66852
68666
  databricksMlflowLogParamTool,
66853
68667
  databricksModelBaseUrl,
66854
68668
  databricksModelProviderSupportsAnthropic,
68669
+ databricksNativeModelId,
66855
68670
  databricksNotebookTool,
66856
68671
  databricksPersistence,
66857
68672
  databricksPipelineListTool,
@@ -66898,8 +68713,11 @@ export {
66898
68713
  listDatabricksCapabilities,
66899
68714
  missingDatabricksCapabilityEvidence,
66900
68715
  normalizeDatabricksGenieAgentSpec,
68716
+ normalizeModuleManifests,
66901
68717
  onBehalfOfFromHeaders,
66902
68718
  parseStatementRows,
68719
+ planModuleOntologyExport,
68720
+ planOntologyFeedHydration,
66903
68721
  queryDatabricksLineageEvidence,
66904
68722
  ragTurnCitationsScorer,
66905
68723
  ragTurnContainsScorer,