The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Consolidate "curation" filtering for Metabot (#75510)
Chris Truteredf5b0ae2e24
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 6d6de8f66bd..cf1b48e98a0 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -573,6 +573,7 @@collections{:team "UX West":api #{metabase.collections.core+ metabase.collections.curationmetabase.collections.models.collectionmetabase.collections.models.collection.rootmetabase.collections.schema}@@ -1192,7 +1193,10 @@syncutilwarehouse-schema}- :model-imports #{:model/Card :model/Database :model/Field :model/Table}}+ :model-imports #{:model/Card+ :model/Database+ :model/Field+ :model/Table}}logger{:team "DevEx"@@ -3451,6 +3455,7 @@analytics-interfaceapiapp-db+ collectionsconfigconnection-poolllm@@ -3463,7 +3468,9 @@tasktracingutil}- :model-imports #{:model/Collection}}+ :model-imports #{:model/Collection+ :model/Dashboard+ :model/Table}}enterprise/serialization{:team "Graphy"diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/db/migration/impl.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/db/migration/impl.cljindex a1bdbe7b85b..ca535fb0c50 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/db/migration/impl.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/db/migration/impl.clj@@ -2,6 +2,8 @@(:require[honey.sql :as sql][metabase-enterprise.semantic-search.index-metadata :as semantic.index-metadata]+ [metabase.collections.curation :as collections.curation]+ [metabase.config.core :as config][metabase.util :as u][metabase.util.log :as log][next.jdbc :as jdbc]@@ -40,7 +42,7 @@(def dynamic-schema-version"Code version of dynamic schema (index_table_xyzs). If higher than what's found in db dynamic schema migration willbe attempted."- 4)+ 5)(defn- alter-index-tables!"Run `alter-fn` against each existing index table whose `index_version` is below `target-version`, then bump those@@ -143,6 +145,112 @@[:= :root_collection_type nil][:in :collection_id (mapv key entries)]]})))))))+(defn- candidate-table-ids+ "The table id lists Migration 5 backfills, as strings matching `model_id`.+ `:authoritative` are authoritative tables; `:published` are published-final tables. Together these are+ exactly the curated tables: fresh ingestion only sets a table's `root_collection_type` when it is+ published, so table curation reduces to published-final-or-authoritative. The index's `root_collection_type`+ is not a reliable table signal (migration 4 set it from collection_id without an is_published check), so it+ is never used for table curation.+ Scans only active published/authoritative tables via a streamed reduce, so it stays bounded.+ Throws outside tests if the appdb lookup fails; a silent skip would corrupt curation in prod."+ []+ (try+ (reduce+ (fn [acc {:keys [id is_published data_layer data_authority]}]+ (let [id (str id)]+ (cond-> acc+ (= :authoritative data_authority) (update :authoritative conj id)+ (and is_published (= :final data_layer)) (update :published conj id))))+ {:authoritative [] :published []}+ (t2/reducible-select [:model/Table :id :is_published :data_layer :data_authority]+ {:where [:and+ [:= :active true]+ [:or [:= :is_published true]+ [:= :data_authority [:inline "authoritative"]]]]}))+ (catch Exception e+ (when-not config/is-test?+ (throw e))+ (log/warn e "Skipping semantic table curation backfill — appdb unavailable (test)")+ nil)))++(defn- official-collection-dashboard-ids+ "Ids (as strings) of non-archived dashboards in an official collection.+ official_collection is new on the dashboard search spec, so existing index dashboard rows have it unset+ and curated-honeysql would miss official-only dashboards; this backfills them from the appdb.+ Throws outside tests if the appdb lookup fails."+ []+ (try+ (let [official-coll-ids (t2/select-pks-set :model/Collection :authority_level :official)]+ (if (empty? official-coll-ids)+ []+ (into [] (map (comp str :id))+ (t2/reducible-select [:model/Dashboard :id]+ {:where [:and [:= :archived false]+ [:in :collection_id (vec official-coll-ids)]]}))))+ (catch Exception e+ (when-not config/is-test?+ (throw e))+ (log/warn e "Skipping semantic dashboard curation backfill — appdb unavailable (test)")+ nil)))++(defn- index-empty?+ "True when the index table has no rows yet.+ A fresh index migrated up before its first population has nothing to backfill (ingestion computes+ `curated` directly at the current version), so the appdb sweep is skipped for it."+ [execute! kw-tbl]+ (empty? (execute! {:select [:id] :from [kw-tbl] :limit 1})))++(defn- update-model-rows-in-batches!+ "Apply `set-map` to the index table's `model` rows for `model-ids`, chunked so a large id list never+ becomes one oversized statement. `model-ids` are already strings."+ [execute! kw-tbl model model-ids set-map]+ (doseq [chunk (partition-all 5000 model-ids)]+ (execute! {:update kw-tbl+ :set set-map+ :where [:and [:= :model [:inline model]] [:in :model_id (vec chunk)]]})))++(defn- add-data-authority-and-curated-columns!+ "Migration 5: add `data_authority` and the precomputed `curated` flag to index tables.+ `curated` backs Metabot's \"verified or curated content\" filter.+ Non-table rows compute `curated` from index columns via [[metabase.collections.curation/curated-honeysql]]+ (is_published forced false — the index lacks it), covering verified cards, official cards, and library+ content via `root_collection_type`. Two bounded appdb sweeps fix what the index columns can't express for+ existing rows: tables are curated only when published-final or authoritative (the index's+ `root_collection_type` isn't is_published-gated for tables, so it's never used for table curation), and+ official-only dashboards (whose official_collection column is new and unset on existing rows).+ Empty (freshly migrated, not-yet-populated) index tables are skipped, since ingestion populates them.+ Known limitation: the backfill writes the `curated`/`data_authority` columns (used for filtering) but not+ `legacy_input`, from which results are reconstructed — so migrated rows surface the new is_curated/+ data_layer/data_authority LLM annotations only after their next reindex; the curated *filter* is correct+ immediately. Changing the rule needs a new migration to recompute this."+ [tx index-metadata]+ (let [curated-expr (collections.curation/curated-honeysql+ (fn [signal] (if (= signal :is_published) [:inline false] signal)))+ ;; resolved lazily so the appdb sweeps only run once a non-empty index table is found+ tables (delay (candidate-table-ids))+ dashboards (delay (official-collection-dashboard-ids))]+ (alter-index-tables!+ tx index-metadata 5+ (fn [execute! table-name]+ (let [kw-tbl (keyword table-name)]+ (execute! {:alter-table [kw-tbl] :add-column [[:data_authority :text :if-not-exists]]})+ (execute! {:alter-table [kw-tbl] :add-column [[:curated :boolean :if-not-exists]]})+ (when-not (index-empty? execute! kw-tbl)+ (let [{:keys [authoritative published]} @tables]+ ;; Non-table rows from index columns (cards accurate; official-only dashboards fixed below).+ (execute! {:update kw-tbl+ :set {:curated curated-expr}+ :where [:and [:= :curated nil] [:!= :model [:inline "table"]]]})+ ;; Tables: curated only from the appdb sweep. Authoritative rows also get data_authority.+ (update-model-rows-in-batches! execute! kw-tbl "table" authoritative+ {:data_authority [:inline "authoritative"] :curated true})+ (update-model-rows-in-batches! execute! kw-tbl "table" published {:curated true})+ ;; Dashboards: official_collection is new on the spec, so backfill both it and curated for+ ;; official dashboards (curated alone would leave them scoring as non-official until reindex).+ (update-model-rows-in-batches! execute! kw-tbl "dashboard" @dashboards+ {:curated true :official_collection true}))))))))+(defn migrate-dynamic-schema!"Migrate runtime-managed schema, ie. schema of `index_table_...` tables. Migration author is responsible for removingleftovers if necessary."@@ -151,6 +259,8 @@;; migration 2: add personal_owner_id column to index tables;; migration 3: add collection_type and data_layer columns to index tables;; migration 4: add root_collection_type column to index tables+ ;; migration 5: add data_authority and precomputed curated columns to index tables(add-personal-owner-id-column! tx index-metadata)(add-collection-type-and-data-layer-columns! tx index-metadata)- (add-root-collection-type-column! tx index-metadata))+ (add-root-collection-type-column! tx index-metadata)+ (add-data-authority-and-curated-columns! tx index-metadata))diff --git a/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj b/enterprise/backend/src/metabase_enterprise/semantic_search/index.cljindex 7069f0528b5..7a6c7bda3f2 100644--- a/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj+++ b/enterprise/backend/src/metabase_enterprise/semantic_search/index.clj@@ -70,6 +70,9 @@[:collection_type :text][:root_collection_type :text][:data_layer :text]+ [:data_authority :text]+ ;; Precomputed "verified or curated content" flag — see metabase.collections.curation/curated?+ [:curated :boolean][:dashboardcard_count :int][:view_count :int][:created_at :timestamp-with-time-zone [:default [:raw "CURRENT_TIMESTAMP"]] :not-null]@@ -155,7 +158,7 @@[owner-ids {:keys [model id embedding searchable_text embeddable_text native_query created_at creator_id updated_atlast_editor_id archived verified official_collection database_id collection_id display_type legacy_inputpinned dashboardcard_count view_count last_viewed_at collection_type root_collection_type- data_layer] :as doc}]+ data_layer data_authority curated] :as doc}]{:model model:model_id id:collection_id collection_id@@ -173,6 +176,8 @@:collection_type collection_type:root_collection_type root_collection_type:data_layer data_layer+ :data_authority data_authority+ :curated (some-> curated to-boolean):dashboardcard_count dashboardcard_count:view_count view_count:model_created_at (some-> created_at to-instant)@@ -288,7 +293,10 @@(let [table-name (or table-name (model-table-name embedding-model))]{:embedding-model embedding-model:table-name table-name- :version 4}))+ ;; Must equal db.migration.impl/dynamic-schema-version — a freshly created index already has the+ ;; latest schema, so recording an older version would mark it stale on creation. Kept as a literal+ ;; (not a require) to avoid an index → impl → index-metadata → index cycle; bump both together.+ :version 5}))(defn- upsert-embedding!-fn [connectable index text->docs](fn [text->embedding]@@ -543,14 +551,21 @@"Ordered `[filter-key where-condition]` pairs for the structural filters implied by `search-context`. Thesearch query ANDs these together (see [[search-filters]]); the search debug API uses the keys to attributewhich specific filter excludes a given row."- [{:keys [archived? verified models created-at created-by last-edited-at last-edited-by+ [{:keys [archived? verified curated? models created-at created-by last-edited-at last-edited-bytable-db-id ids display-type] :as search-context}](keep(fn [[k clause]] (when clause [k clause]))[[:personal-collection (personal-collection-filter search-context)][:archived? (when (some? archived?) [:= :archived archived?])][:verified (when (some? verified) [:= :verified verified])]- [:models (when (seq models) [:in :model models])]+ ;; "Verified or curated content" — precomputed flag (collections.curation/curated?)+ [:curated (when (some? curated?) [:= :curated curated?])]+ ;; search.impl sets :models to the applicable models; an empty (but present) set means filters left no+ ;; applicable model (e.g. curated + non-curatable entity-types), so match nothing rather than omitting the+ ;; predicate (= all models)+ [:models (cond+ (seq models) [:in :model models]+ (some? models) [:= [:inline 1] [:inline 0]])][:created-by (when (seq created-by) [:in :creator_id created-by])][:last-edited-by (when (seq last-edited-by) [:in :last_editor_id last-edited-by])][:table-db-id (when table-db-id [:= :database_id table-db-id])]@@ -590,6 +605,8 @@[:collection_type :collection_type][:root_collection_type :root_collection_type][:data_layer :data_layer]+ [:data_authority :data_authority]+ [:curated :curated][:dashboardcard_count :dashboardcard_count][:view_count :view_count][:model_created_at :model_created_at]diff --git a/enterprise/backend/test/metabase_enterprise/metabot/tools/semantic_search_test.clj b/enterprise/backend/test/metabase_enterprise/metabot/tools/semantic_search_test.cljindex dc6d85aba49..572786b9adb 100644--- a/enterprise/backend/test/metabase_enterprise/metabot/tools/semantic_search_test.clj+++ b/enterprise/backend/test/metabase_enterprise/metabot/tools/semantic_search_test.clj@@ -88,15 +88,15 @@:entity-types ["table"]}results (search/search args)](is (empty? results)))))- (testing "search with metabot verified content flag"+ (testing "search with metabot verified-or-curated content flag"(let [metabot {:entity_id "test-bot":use_verified_content true}](with-redefs [t2/select-one (fn [model & _](is (= :model/Metabot model) "Should query for Metabot model")metabot)search-core/search (fn [context]- ;; Verify that verified flag is set when metabot has use_verified_content- (is (true? (:verified context)))+ ;; use_verified_content now drives the curated filter, not :verified+ (is (true? (:curated? context))){:data [dashboard]})](let [results (search/search {:term-queries ["test"]:metabot-id "test-bot"diff --git a/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.clj b/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.cljindex 455caeebce6..3c8d40a412b 100644--- a/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.clj+++ b/enterprise/backend/test/metabase_enterprise/semantic_search/index_test.clj@@ -742,3 +742,15 @@(-> (semantic.tu/query-index {:search-string "Antarctic wildlife"})first(select-keys [:id :name :model])))))))))))++(deftest search-filters-models-test+ (testing "the models predicate distinguishes an empty applicable-model set from an absent one"+ (let [conds (fn [ctx] (tree-seq coll? seq (#'semantic.index/search-filters ctx)))]+ (testing "a non-empty set filters to those models"+ (is (some #{[:in :model #{"card"}]} (conds {:models #{"card"}}))))+ (testing "an empty (but present) set matches nothing, rather than omitting the predicate (all models)"+ (is (some #{[:= [:inline 1] [:inline 0]]} (conds {:models #{} :curated? true}))))+ (testing "an absent :models adds no model predicate"+ (let [flat (conds {:archived? false})]+ (is (not-any? #(and (vector? %) (= [:in :model] (subvec % 0 (min 2 (count %))))) flat))+ (is (not-any? #{[:= [:inline 1] [:inline 0]]} flat)))))))diff --git a/enterprise/backend/test/metabase_enterprise/semantic_search/migration_test.clj b/enterprise/backend/test/metabase_enterprise/semantic_search/migration_test.cljindex 58532ede896..554ae6906af 100644--- a/enterprise/backend/test/metabase_enterprise/semantic_search/migration_test.clj+++ b/enterprise/backend/test/metabase_enterprise/semantic_search/migration_test.clj@@ -176,8 +176,10 @@"content""created_at""creator_id"+ "curated""dashboardcard_count""database_id"+ "data_authority""data_layer""display_type""embedding"@@ -377,3 +379,33 @@"6" nil ; non-library collection, no gate doc"7" "library-data"} ; pre-existing value preservedrows-by-id)))))))))++(deftest candidate-table-ids-test+ (testing "Migration 5 sweeps only active published/authoritative tables, sorted into the two id lists it+ backfills — others are implicitly not curated (table curation never uses root_collection_type)"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {pub :id} {:db_id db-id :active true :is_published true :data_layer :final}+ :model/Table {auth :id} {:db_id db-id :active true :data_authority :authoritative}+ :model/Table {inactive :id} {:db_id db-id :active false :is_published true :data_layer :final}+ :model/Table {plain :id} {:db_id db-id :active true}]+ (let [{:keys [authoritative published]} (#'semantic.db.migration.impl/candidate-table-ids)+ authoritative (set authoritative)+ published (set published)]+ (is (contains? published (str pub)) "published-final tables go in :published")+ (is (contains? authoritative (str auth)) "authoritative tables go in :authoritative")+ (is (not (contains? published (str inactive))) "inactive tables are skipped")+ (is (not (contains? published (str plain))) "plain tables aren't candidates")+ (is (not (contains? authoritative (str plain))))))))++(deftest official-collection-dashboard-ids-test+ (testing "Migration 5 backfills official-only dashboards (official_collection is new on the spec, so+ existing index rows lack it)"+ (mt/with-temp [:model/Collection {official :id} {:authority_level :official}+ :model/Collection {normal :id} {}+ :model/Dashboard {od :id} {:collection_id official}+ :model/Dashboard {nd :id} {:collection_id normal}+ :model/Dashboard {ad :id} {:collection_id official :archived true}]+ (let [ids (set (#'semantic.db.migration.impl/official-collection-dashboard-ids))]+ (is (contains? ids (str od)) "dashboards in official collections are backfilled")+ (is (not (contains? ids (str nd))) "dashboards in normal collections are not")+ (is (not (contains? ids (str ad))) "archived dashboards are skipped")))))diff --git a/enterprise/frontend/src/metabase-enterprise/moderation/components/MetabotVerifiedContentConfigurationPane.tsx b/enterprise/frontend/src/metabase-enterprise/moderation/components/MetabotVerifiedContentConfigurationPane.tsxindex 67366f45327..f7264ff70d6 100644--- a/enterprise/frontend/src/metabase-enterprise/moderation/components/MetabotVerifiedContentConfigurationPane.tsx+++ b/enterprise/frontend/src/metabase-enterprise/moderation/components/MetabotVerifiedContentConfigurationPane.tsx@@ -1,4 +1,4 @@-import { jt, t } from "ttag";+import { t } from "ttag";import { SettingHeader } from "metabase/admin/settings/components/SettingHeader";import { useUpdateMetabotMutation } from "metabase/api";@@ -32,11 +32,11 @@ export function MetabotVerifiedContentConfigurationPane({<Stack gap="sm"><SettingHeaderid="verified-content"- title={t`Verified content`}- description={jt`When enabled, Metabot will only use models and metrics marked as Verified.`}+ title={t`Verified or curated content`}+ description={t`When enabled, Metabot will only use content that's verified, in an official collection, or published to the library.`}/><Switch- label={t`Only use Verified content`}+ label={t`Only use verified or curated content`}checked={!!metabot.use_verified_content}onChange={(e) => handleVerifiedContentToggle(e.target.checked)}disabled={isUpdating}diff --git a/frontend/src/metabase/admin/ai/AISettingsPage.unit.spec.tsx b/frontend/src/metabase/admin/ai/AISettingsPage.unit.spec.tsxindex d954c1ce33c..91be5dca219 100644--- a/frontend/src/metabase/admin/ai/AISettingsPage.unit.spec.tsx+++ b/frontend/src/metabase/admin/ai/AISettingsPage.unit.spec.tsx@@ -237,7 +237,7 @@ describe("AISettingsPage", () => {expect(await screen.findByRole("switch", {- name: "Only use Verified content",+ name: "Only use verified or curated content",}),).toBeChecked();});diff --git a/frontend/src/metabase/admin/ai/MetabotSettingsPanel.unit.spec.tsx b/frontend/src/metabase/admin/ai/MetabotSettingsPanel.unit.spec.tsxindex a01adfdc3fa..aa50b559e97 100644--- a/frontend/src/metabase/admin/ai/MetabotSettingsPanel.unit.spec.tsx+++ b/frontend/src/metabase/admin/ai/MetabotSettingsPanel.unit.spec.tsx@@ -275,9 +275,11 @@ describe("MetabotSettingsPanel", () => {it("should not show verification switch without content_verification feature", async () => {await setup();- expect(screen.queryByText("Verified content")).not.toBeInTheDocument();expect(- screen.queryByText("Only use Verified content"),+ screen.queryByText("Verified or curated content"),+ ).not.toBeInTheDocument();+ expect(+ screen.queryByText("Only use verified or curated content"),).not.toBeInTheDocument();});@@ -286,13 +288,15 @@ describe("MetabotSettingsPanel", () => {await setup();- expect(await screen.findByText("Verified content")).toBeInTheDocument();expect(- await screen.findByText("Only use Verified content"),+ await screen.findByText("Verified or curated content"),+ ).toBeInTheDocument();+ expect(+ await screen.findByText("Only use verified or curated content"),).toBeInTheDocument();expect(await screen.findByRole("switch", {- name: "Only use Verified content",+ name: "Only use verified or curated content",}),).toBeInTheDocument();});@@ -303,7 +307,7 @@ describe("MetabotSettingsPanel", () => {await setup();const verifiedSwitch = await screen.findByRole("switch", {- name: "Only use Verified content",+ name: "Only use verified or curated content",});// Verify switch is initially unchecked (default metabot has use_verified_content: false)diff --git a/src/metabase/collections/curation.clj b/src/metabase/collections/curation.cljnew file mode 100644index 00000000000..32037f801b7--- /dev/null+++ b/src/metabase/collections/curation.clj@@ -0,0 +1,74 @@+(ns metabase.collections.curation+ "Canonical definition of \"curated\" content for Metabot's \"verified or curated content\" setting.+ Curated content is verified cards/dashboards, official-collection content, library/published content,+ or authoritative tables.+ [[curated?]] is the predicate; [[curated-honeysql]] is its SQL mirror for migrations.+ (The source-of-truth variant that reads signals from cards/dashboards/tables lives in+ `metabase.metabot.curation`, since this foundational module shouldn't reference those higher-level models.)"+ (:require+ [metabase.collections.models.collection :as collection]))++(def library-root-collection-types+ "Collection `type`s (as strings) whose members count as library content."+ (into #{} (map name) collection/library-collection-types))++(defn- as-name+ "Normalize a text signal that may arrive as a keyword or string."+ [v]+ (some-> v name))++(defn- as-bool+ "Coerce a raw boolean signal: only `true`/`1` count as true.+ Guards against DB drivers that surface a boolean column/expression as numeric `0`/`1`, where a plain+ Clojure truthiness check would treat `0` as true."+ [v]+ (or (true? v) (= 1 v)))++(defn curated?+ "Whether the given curation-signal map is curated. THIS IS THE AUTHORITATIVE RULE.+ Reads `:model` `:verified` `:official_collection` `:is_published` `:root_collection_type`+ `:data_layer` `:data_authority`; text signals may be strings or keywords.++ Several places re-encode or apply this rule and MUST be kept in sync when it changes:+ - [[curated-honeysql]] — SQL mirror of this predicate (used by the semantic backfill)+ - metabase.search.in-place.filter/build-optional-filter-query (the `[:curated …]` methods) — in-place+ engine SQL filter+ - metabase.metabot.tools.util/metabot-metrics-and-models-query — suggested-prompt source filter+ - metabase.metabot.curation/curated-ids — source-of-truth check for recent views+ - metabase-enterprise.semantic-search.db.migration.impl/add-data-authority-and-curated-columns! —+ recomputes the precomputed `curated` column for existing index rows++ The signals above are populated at ingestion by the search specs, so a NEW signal must also be added to+ metabase.queries.models.card, metabase.dashboards.models.dashboard, metabase.warehouse-schema.models.table+ (and metabase.search.spec attr-types), and reaches existing rows only on reindex / via the migration above."+ [{:keys [model verified official_collection is_published root_collection_type data_layer data_authority]}]+ ;; No feature gate: a signal can only be set while its feature is present, so the flag is already+ ;; feature-correct and self-heals on the next reindex after any feature change.+ (boolean+ (or (as-bool verified)+ (as-bool official_collection)+ (and (or (as-bool is_published)+ (contains? library-root-collection-types (as-name root_collection_type)))+ ;; published tables count only at the `final` layer; non-table library content has none+ (or (not= (as-name model) "table")+ (= "final" (as-name data_layer))))+ (= "authoritative" (as-name data_authority)))))++(defn curated-honeysql+ "HoneySQL mirror of [[curated?]] for SQL contexts, e.g. backfilling the precomputed `curated` column.+ Keep in sync with [[curated?]] (the authoritative rule).+ `col` resolves a signal key to a HoneySQL fragment for the target table; pass a constant for any+ column the table lacks (the semantic index has no `is_published`, so it leans on+ `root_collection_type`)."+ [col]+ [:coalesce+ [:or+ [:is (col :verified) true]+ [:is (col :official_collection) true]+ [:and+ [:or [:is (col :is_published) true]+ [:in (col :root_collection_type) (vec library-root-collection-types)]]+ [:or [:not= (col :model) [:inline "table"]]+ [:= (col :data_layer) [:inline "final"]]]]+ [:= (col :data_authority) [:inline "authoritative"]]]+ false])diff --git a/src/metabase/dashboards/models/dashboard.clj b/src/metabase/dashboards/models/dashboard.cljindex 428282b6ae1..347c3cfaf8b 100644--- a/src/metabase/dashboards/models/dashboard.clj+++ b/src/metabase/dashboards/models/dashboard.clj@@ -503,6 +503,7 @@:last-viewed-at true:pinned [:> [:coalesce :collection_position [:inline 0]] [:inline 0]]:verified [:= "verified" :mr.status]+ :official-collection [:= "official" :collection.authority_level]:view-count true:created-at true:updated-at truediff --git a/src/metabase/metabot/api/metabot.clj b/src/metabase/metabot/api/metabot.cljindex 2fc6b1a665d..35312d3be62 100644--- a/src/metabase/metabot/api/metabot.clj+++ b/src/metabase/metabot/api/metabot.clj@@ -6,6 +6,7 @@[metabase.api.routes.common :refer [+auth]][metabase.app-db.core :as mdb][metabase.metabot.suggested-prompts :as metabot.suggested-prompts]+ [metabase.metabot.task.suggested-prompts-refresh :as metabot.suggested-prompts-refresh][metabase.metabot.tools.util :as metabot.tools.u][metabase.metabot.usage :as metabot.usage][metabase.premium-features.core :as premium-features]@@ -56,18 +57,9 @@(let [old-vals (select-keys old-metabot (keys metabot-updates))](when (not= old-vals metabot-updates)(t2/update! :model/Metabot id metabot-updates)- (when-not (metabot.usage/managed-free-limit-reached?)- ;; Delete + regenerate atomically so a raced managed-AI lock (the 402 swallowed below) or- ;; any other failure rolls back the delete instead of leaving the Metabot with zero- ;; prompts. The 402 still triggers the rollback because it escapes the transaction body- ;; before we catch it. Swallowing it keeps a settings-only PUT from failing on a race.- (try- (t2/with-transaction [_conn]- (metabot.suggested-prompts/delete-all-metabot-prompts id)- (metabot.suggested-prompts/generate-sample-prompts id))- (catch clojure.lang.ExceptionInfo e- (when-not (= "metabase_ai_managed_locked" (:error-code (ex-data e)))- (throw e))))))+ ;; Content scope changed, so the suggested prompts are stale. Regenerate in the background so+ ;; the toggle returns instantly; the job re-reads the saved scope and debounces rapid toggles.+ (metabot.suggested-prompts-refresh/schedule-refresh! id))(t2/select-one :model/Metabot :id id))))(api.macros/defendpoint :post "/:id/prompt-suggestions/regenerate"diff --git a/src/metabase/metabot/context.clj b/src/metabase/metabot/context.cljindex 7f425bba709..83e44bf048b 100644--- a/src/metabase/metabot/context.clj+++ b/src/metabase/metabot/context.clj@@ -11,9 +11,9 @@[metabase.lib.core :as lib][metabase.lib.schema :as lib.schema][metabase.metabot.config :as metabot.config]+ [metabase.metabot.curation :as curation][metabase.metabot.settings :as metabot.settings][metabase.metabot.table-utils :as table-utils]- [metabase.premium-features.core :as premium-features][metabase.transforms-base.util :as transforms-base.u][metabase.util.json :as json][metabase.util.log :as log]@@ -255,49 +255,22 @@[metabot-id :entity-id]metabot-id))))-(defn- verified-only?- [metabot]- (and (:use_verified_content metabot)- (premium-features/has-feature? :content-verification)))--(defn- batch-verified-ids- "Of the given `ids`, return the set whose most-recent ModerationReview for `item-type` has status \"verified\"."- [ids item-type]- (if (empty? ids)- #{}- (t2/select-fn-set :moderated_item_id :model/ModerationReview- :moderated_item_id [:in ids]- :moderated_item_type item-type- :most_recent true- :status "verified")))--(defn- filter-recents-to-verified- "Drop card/dataset/metric/dashboard recents that aren't verified. Tables (and any other- non-moderatable model) pass through unchanged."+(defn- filter-recents-to-curated+ "Keep only recents that are curated (verified, official-collection, library/published, or authoritative).+ Delegates to metabot.curation/curated-ids, the source-of-truth check, so recent-view filtering can't drift+ from the canonical rule and doesn't depend on the search index."[recents]- (let [card-like-ids (->> recents- (filter #(#{:card :dataset :metric} (:model %)))- (map :id))- dashboard-ids (->> recents- (filter #(= :dashboard (:model %)))- (map :id))- verified-cards (batch-verified-ids card-like-ids "card")- verified-dashboards (batch-verified-ids dashboard-ids "dashboard")]- (filter (fn [{:keys [id model]}]- (case model- (:card :dataset :metric) (contains? verified-cards id)- :dashboard (contains? verified-dashboards id)- true))- recents)))+ (let [curated (curation/curated-ids (map (juxt (comp name :model) :id) recents))]+ (filter (fn [{:keys [model id]}] (contains? curated [(name model) id])) recents)))(defn- add-recent-views"Add user's recent views to the context since these have a higher likelihood of being relevant to a user's query.Includes the 5 most recent items across cards, datasets, metrics, dashboards, and tables.(Excludes collections and documents for now, which aren't searchable by Metabot.)- When `metabot-id` is provided and the metabot has `use_verified_content` enabled (and the- `:content-verification` premium feature is active), filters out unverified cards/datasets/metrics- /dashboards before taking the top 5. Tables are not moderatable and always pass through."+ When `metabot-id` is provided and the metabot has `use_verified_content` enabled, filters recents down+ to curated content (verified, official-collection, library/published, or authoritative) before taking+ the top 5, matching how search filters answer sources."[context {:keys [metabot-id] :as _opts}](try;; When disabled, strip any preexisting :user_recently_viewed so the setting guarantees recent@@ -309,8 +282,8 @@[:views :selections]{:models [:card :dataset :metric :dashboard :table]}))recents (cond->> recents- (verified-only? (get-metabot metabot-id))- filter-recents-to-verified)]+ (:use_verified_content (get-metabot metabot-id))+ filter-recents-to-curated)](mapv (fn [item](let [item-type(case (:model item)diff --git a/src/metabase/metabot/curation.clj b/src/metabase/metabot/curation.cljnew file mode 100644index 00000000000..6f4fbf91289--- /dev/null+++ b/src/metabase/metabot/curation.clj@@ -0,0 +1,88 @@+(ns metabase.metabot.curation+ "Source-of-truth curation check for Metabot's recent-view context.+ Reads each item's curation signals straight from the source tables and applies the canonical+ [[metabase.collections.curation/curated?]] predicate, so it has no dependency on the search index being+ present, fresh, or complete, and can't drift from the rule.+ Lives in metabot rather than collections.curation because it reads cards/dashboards/tables/reviews, which+ are higher-level models a foundational module shouldn't reference; a consistency test pins it to the+ index's precomputed `curated` column."+ (:require+ [metabase.collections.curation :as curation]+ [metabase.collections.models.collection :as collection]+ [toucan2.core :as t2]))++(def ^:private report-card-models+ "Search-model strings backed by `report_card`, moderated as \"card\"."+ #{"card" "dataset" "metric"})++(defn- verified-item-ids+ "Subset of `ids` whose most-recent moderation review for `item-type` is \"verified\"."+ [ids item-type]+ (if (empty? ids)+ #{}+ (t2/select-fn-set :moderated_item_id :model/ModerationReview+ :moderated_item_id [:in ids]+ :moderated_item_type item-type+ :most_recent true+ :status "verified")))++(defn- collection-info+ "`collection-id → {:authority_level :location :type}` for the given ids."+ [coll-ids]+ (when (seq coll-ids)+ (t2/select-pk->fn #(select-keys % [:authority_level :location :type])+ :model/Collection :id [:in coll-ids])))++(defn- root-collection-type-of+ [coll-id->info coll-id]+ (let [{:keys [location type]} (get coll-id->info coll-id)]+ (collection/root-collection-type {:collection_id coll-id+ :collection_location location+ :collection_type type})))++(defn- moderatable-curation-signals+ "`[id signal-map]` pairs for collection-housed, moderatable models (report_card-backed and dashboards)."+ [search-model t2-model item-type ids]+ (let [rows (t2/select [t2-model :id :collection_id] :id [:in ids])+ coll (collection-info (into #{} (keep :collection_id) rows))+ verified (verified-item-ids ids item-type)]+ (for [{:keys [id collection_id]} rows]+ [id {:model search-model+ :verified (contains? verified id)+ ;; authority_level is keyword-transformed on read (collection :type is not — it stays a string,+ ;; which is what curated? and library-root-collection-types expect)+ :official_collection (= :official (:authority_level (get coll collection_id)))+ :root_collection_type (root-collection-type-of coll collection_id)}])))++(defn- table-curation-signals+ "`[id signal-map]` pairs for tables, whose curation comes from is_published, data_layer, and data_authority.+ root_collection_type is intentionally omitted: the table search spec joins the collection only for+ published tables, and for a published table is_published already decides curation, so the root type never+ changes a table's verdict. Omitting it avoids re-deriving from a possibly-stale collection_id, and lets+ curated? coerce is_published consistently (only true/1 count)."+ [ids]+ (for [{:keys [id is_published data_layer data_authority]}+ (t2/select [:model/Table :id :is_published :data_layer :data_authority] :id [:in ids])]+ [id {:model "table"+ :is_published is_published+ :data_layer data_layer+ :data_authority data_authority}]))++(defn- curation-signals+ "`[id signal-map]` pairs for the given search-model string and ids, read from source-of-truth tables."+ [model ids]+ (cond+ (report-card-models model) (moderatable-curation-signals model :model/Card "card" ids)+ (= "dashboard" model) (moderatable-curation-signals model :model/Dashboard "dashboard" ids)+ (= "table" model) (table-curation-signals ids)))++(defn curated-ids+ "Of `model+ids` (`[search-model-string id]` pairs), return the subset that are curated.+ Reads each item's signals from the source tables and applies [[curation/curated?]], with no search-index+ dependency. Recognizes the Metabot recent-view models: card/dataset/metric, dashboard, table."+ [model+ids]+ (into #{}+ (for [[model ids] (update-vals (group-by first model+ids) #(mapv second %))+ [id signals] (curation-signals model ids)+ :when (curation/curated? signals)]+ [model id])))diff --git a/src/metabase/metabot/example_question_generator.clj b/src/metabase/metabot/example_question_generator.cljindex 4079b61a119..bdcbe75bd77 100644--- a/src/metabase/metabot/example_question_generator.clj+++ b/src/metabase/metabot/example_question_generator.clj@@ -152,12 +152,22 @@(def ^:private max-batch-size 10)(defn- process-batch-parallel- "Process items in parallel batches, matching Python's asyncio.gather behavior."+ "Process items in parallel batches, matching Python's asyncio.gather behavior.+ Returns one result per item, in order (callers zip positionally): `{:ok <result>}` on success, or+ `{:error <throwable>}` when generation threw. [[generate-example-questions]] decides what to do with+ failures — drop them when there are partial successes, or rethrow when nothing was produced."[items generate-fn](vec(mapcat(fn [batch]- (let [futures (mapv #(future (generate-fn %)) batch)]+ (let [futures (mapv (fn [item]+ (future+ (try+ {:ok (generate-fn item)}+ (catch Throwable e+ (log/warn e "Example question generation failed for one item")+ {:error e}))))+ batch)](mapv deref futures)))(partition-all max-batch-size items))))@@ -175,14 +185,25 @@(log/info "Generating example questions natively"{:table-count (count (:tables payload)):metric-count (count (:metrics payload))})- (let [table-questions (if (seq (:tables payload))- (process-batch-parallel (:tables payload) generate-table-questions)- [])- metric-questions (if (seq (:metrics payload))- (process-batch-parallel (:metrics payload) generate-metric-questions)- [])]+ (let [table-results (if (seq (:tables payload))+ (process-batch-parallel (:tables payload) generate-table-questions)+ [])+ metric-results (if (seq (:metrics payload))+ (process-batch-parallel (:metrics payload) generate-metric-questions)+ [])+ all-results (concat table-results metric-results)+ errors (keep :error all-results)+ produced (transduce (comp (keep :ok) (map (comp count :questions)))+ + 0 all-results)+ ->questions (fn [r] (or (:ok r) {:questions []}))]+ ;; If failures left us with nothing usable, surface the original error instead of an empty result —+ ;; otherwise the caller's delete+generate transaction commits and wipes existing prompts. Rethrow+ ;; the first failure so its message/cause is preserved. Partial successes (produced > 0) go through.+ (when (and (seq errors) (zero? produced))+ (throw (first errors)))(log/info "Native example question generation complete"- {:table-results (count table-questions)- :metric-results (count metric-questions)})- {:table_questions table-questions- :metric_questions metric-questions}))+ {:table-results (count table-results)+ :metric-results (count metric-results)+ :failures (count errors)})+ {:table_questions (mapv ->questions table-results)+ :metric_questions (mapv ->questions metric-results)}))diff --git a/src/metabase/metabot/init.clj b/src/metabase/metabot/init.cljindex c2e7042e14d..3e3ac4970a3 100644--- a/src/metabase/metabot/init.clj+++ b/src/metabase/metabot/init.clj@@ -3,4 +3,5 @@[metabase.metabot.scope][metabase.metabot.settings][metabase.metabot.task.metabot-conversation-trimmer]- [metabase.metabot.task.suggested-prompts-generator]))+ [metabase.metabot.task.suggested-prompts-generator]+ [metabase.metabot.task.suggested-prompts-refresh]))diff --git a/src/metabase/metabot/models/metabot.clj b/src/metabase/metabot/models/metabot.cljindex b79e1af444e..49369214244 100644--- a/src/metabase/metabot/models/metabot.clj+++ b/src/metabase/metabot/models/metabot.clj@@ -16,6 +16,13 @@(derive :hook/entity-id)(derive :hook/timestamped?))+;; NOTE: the `use_verified_content` column now means "use only verified OR curated content" — when on,+;; Metabot search is restricted via the precomputed `curated` flag (see metabase.collections.curation),+;; which covers verified items, official collections, and library/published content, not just verified+;; cards. The column keeps its original name on purpose: renaming it would need an appdb migration and+;; would break the serdes `:copy`/`:defaults` contract and the API/FE field, for no behavioural gain.+;; The user-facing label ("Verified or curated content") carries the broadened meaning instead.+;;; --------------------------------------------------- Hydration ----------------------------------------------------(methodical/defmethod t2/batched-hydrate [:model/Metabot :prompts]diff --git a/src/metabase/metabot/suggested_prompts.clj b/src/metabase/metabot/suggested_prompts.cljindex a3f2d1554ec..bb99d9a655a 100644--- a/src/metabase/metabot/suggested_prompts.clj+++ b/src/metabase/metabot/suggested_prompts.clj@@ -54,8 +54,14 @@{:status :ai-produced-no-prompts} ; LLM returned 0 questions for the inputsThrows a 402 via [[metabot.usage/check-metabase-managed-free-limit!]] when the managed AI cap is hit.- Best-effort callers (e.g. PUT /:id) pre-guard with [[metabot.usage/managed-free-limit-reached?]]- and catch the 402 to handle the rare TOCTOU race where the limit flips between the two checks."+ Best-effort callers (e.g. the suggested-prompts-refresh job) pre-guard with+ [[metabot.usage/managed-free-limit-reached?]] and catch the 402 to handle the rare TOCTOU race where+ the limit flips between the two checks."+ ;; TODO (Chris 2026-06-09) -- include warehouse tables (esp. library/published) in prompt inputs, not+ ;; just metric/model cards. Needs a `table_id` column on metabot_prompt (+ serdes + API + FE rendering+ ;; of table-backed prompts), then fetch curated tables in scope and feed them to the table generator.+ ;; TODO (Chris 2026-06-09) -- @mentioned entities should seed prompts and bypass the curated filter+ ;; (the user picked them deliberately); wire once the agent's @mention context assembly is settled.[metabot-id & {:as opts}](metabot.usage/check-metabase-managed-free-limit!)(let [opts (merge default-opts opts)]diff --git a/src/metabase/metabot/task/suggested_prompts_refresh.clj b/src/metabase/metabot/task/suggested_prompts_refresh.cljnew file mode 100644index 00000000000..ba4f14c0e51--- /dev/null+++ b/src/metabase/metabot/task/suggested_prompts_refresh.clj@@ -0,0 +1,97 @@+(ns metabase.metabot.task.suggested-prompts-refresh+ "Background regeneration of a Metabot's suggested prompts when its content scope changes — e.g. the+ \"verified or curated content\" setting or the configured collection is toggled.+ Scheduling through Quartz keeps the toggle request instant and makes rapid or multi-instance toggles+ converge: a single durable job with `DisallowConcurrentExecution` runs at most one refresh per+ Metabot at a time, a per-Metabot one-shot trigger debounces a burst into one run, and the job+ re-reads the Metabot's current scope so the final run always reflects the committed state."+ (:require+ [clojurewerkz.quartzite.conversion :as qc]+ [clojurewerkz.quartzite.jobs :as jobs]+ [clojurewerkz.quartzite.schedule.simple :as simple]+ [clojurewerkz.quartzite.triggers :as triggers]+ [metabase.metabot.suggested-prompts :as metabot.suggested-prompts]+ [metabase.metabot.usage :as metabot.usage]+ [metabase.request.core :as request]+ [metabase.task.core :as task]+ [metabase.util.log :as log]+ [toucan2.core :as t2])+ (:import+ (java.time Instant)+ (java.util Date)+ (org.quartz DisallowConcurrentExecution ObjectAlreadyExistsException)))++(set! *warn-on-reflection* true)++(def ^:private job-key (jobs/key "metabase.task.metabot.suggested-prompts-refresh.job"))++(defn- trigger-key [metabot-id]+ (triggers/key (str "metabase.task.metabot.suggested-prompts-refresh.trigger." metabot-id)))++;; Delay the run so a burst of toggles debounces into one, and so the setting write settles first.+(def ^:private debounce-seconds 5)++(defn- regenerate!+ "Rebuild `metabot-id`'s suggested prompts from its current scope, atomically.+ Skips when the managed-AI limit is reached. Runs delete+generate in one transaction and rolls it back+ unless new prompts were actually generated — so a Metabot's existing prompts are never wiped to empty+ (e.g. when the curated filter currently excludes its only content). Being a background job, it logs+ and swallows everything."+ [metabot-id]+ ;; System task: run as admin so all in-scope content is considered (per-user reads stay filtered).+ (request/as-admin+ (try+ (when-not (metabot.usage/managed-free-limit-reached?)+ (t2/with-transaction [_conn]+ (metabot.suggested-prompts/delete-all-metabot-prompts metabot-id)+ (let [{:keys [status]} (metabot.suggested-prompts/generate-sample-prompts metabot-id)]+ (when (not= status :generated)+ ;; Better to keep stale prompts than to leave the Metabot with none.+ (throw (ex-info "no new prompts generated" {::preserve true :status status}))))))+ (catch clojure.lang.ExceptionInfo e+ (if (::preserve (ex-data e))+ (log/infof "Kept existing suggested prompts for Metabot %s (regeneration produced %s)"+ metabot-id (:status (ex-data e)))+ (log/warnf e "Failed to regenerate suggested prompts for Metabot %s" metabot-id)))+ (catch Throwable e+ (log/warnf e "Failed to regenerate suggested prompts for Metabot %s" metabot-id)))))++(task/defjob ^{DisallowConcurrentExecution true+ :doc "Regenerate a Metabot's suggested prompts after its content scope changes."}+ SuggestedPromptsRefresh [ctx]+ (when-let [metabot-id (get (qc/from-job-data ctx) "metabot-id")]+ (regenerate! metabot-id)))++(def ^:private refresh-job+ (jobs/build+ (jobs/with-description "Regenerate Metabot suggested prompts after a scope change")+ (jobs/of-type SuggestedPromptsRefresh)+ (jobs/with-identity job-key)+ (jobs/store-durably)))++(defn- refresh-trigger [metabot-id]+ (triggers/build+ (triggers/with-identity (trigger-key metabot-id))+ (triggers/for-job job-key)+ (triggers/using-job-data {"metabot-id" metabot-id})+ (triggers/start-at (Date/from (.plusSeconds (Instant/now) debounce-seconds)))+ (triggers/with-schedule+ ;; Fire once; if the run is missed (no node free in time), fire as soon as possible — never drop it.+ (simple/schedule (simple/with-misfire-handling-instruction-fire-now)))))++(defn schedule-refresh!+ "(Re)schedule a background regeneration of `metabot-id`'s suggested prompts.+ Replacing any pending trigger debounces rapid toggles; clustered Quartz plus+ `DisallowConcurrentExecution` serialize execution across instances."+ [metabot-id]+ (let [trigger (refresh-trigger metabot-id)]+ ;; reschedule replaces a pending trigger (debounce); nil means none exists, so add one+ (or (task/reschedule-trigger! trigger)+ (try (task/add-trigger! trigger)+ ;; lost a race with another instance adding the same trigger — fall back to reschedule+ (catch ObjectAlreadyExistsException _+ (task/reschedule-trigger! trigger))))))++(defmethod task/init! ::SuggestedPromptsRefresh+ [_]+ (task/add-job! refresh-job))diff --git a/src/metabase/metabot/tools/search.clj b/src/metabase/metabot/tools/search.cljindex 51feec73c2d..bae780b1f91 100644--- a/src/metabase/metabot/tools/search.clj+++ b/src/metabase/metabot/tools/search.clj@@ -269,7 +269,7 @@search-native-query(assoc :search-native-query (boolean search-native-query))use-verified?- (assoc :verified true)+ (assoc :curated true)weights(assoc :weights weights)search-enginediff --git a/src/metabase/metabot/tools/shared/llm_shape.clj b/src/metabase/metabot/tools/shared/llm_shape.cljindex dce8ed5bd80..6f9469edd2c 100644--- a/src/metabase/metabot/tools/shared/llm_shape.clj+++ b/src/metabase/metabot/tools/shared/llm_shape.clj@@ -652,6 +652,10 @@(if (keyword? database_engine)(clojure.core/name database_engine)database_engine)))]+ ;; TODO (Chris 2026-06-09) -- surface the other curation signals to the LLM (is_curated / official /+ ;; in_library / data_layer), not just is_verified, so it can reason about why content is curated.+ ;; The signals are on the search-index row; thread them through postprocess-search-result and add+ ;; attributes here + in llm_shape.selmer. Run ai-benchmarks before landing — it changes model input.(render-llm-template:search_result{:search_tag_name (search-result-tag-name type)diff --git a/src/metabase/metabot/tools/util.clj b/src/metabase/metabot/tools/util.cljindex 5546c6380b8..39a2fa0237b 100644--- a/src/metabase/metabot/tools/util.clj+++ b/src/metabase/metabot/tools/util.clj@@ -198,44 +198,68 @@"Return the metric and model cards in metabot scope visible to the current user.Takes a metabot-id and returns all metric and model cards in that metabot's collection- and its subcollections. If the metabot has use_verified_content enabled, only verified- content is returned.+ and its subcollections. If the metabot has use_verified_content enabled, only verified-or-curated+ content is returned — verified, official-collection, or library-published cards.Ignores analytics content."[metabot-id & {:keys [limit] :as _opts}](let [metabot (t2/select-one :model/Metabot :id metabot-id)metabot-collection-id (:collection_id metabot)use-verified-content? (:use_verified_content metabot)+ verified? (premium-features/has-feature? :content-verification)+ official? (premium-features/has-feature? :official-collections)+ library? (premium-features/has-feature? :library)+ ;; ids of collections under a Library-type root; their metrics/models are library-published content+ library-coll-ids (when library?+ (let [roots (t2/select :model/Collection+ :type [:in (mapv name collection/library-collection-types)]+ :location "/")]+ (into (set (map :id roots)) (mapcat collection/descendant-ids roots))))+ ;; Mirror collections.curation/curated? for card scope: verified, official-collection, or+ ;; library-published (under a Library root). Each disjunct is gated on its feature.+ curated-conds (cond-> []+ verified? (conj [:= :mr.status [:inline "verified"]])+ official? (conj [:= :collection.authority_level [:inline "official"]])+ (seq library-coll-ids) (conj [:in :report_card.collection_id (vec library-coll-ids)]))+ ;; Columns are qualified with report_card because the official-collections branch joins+ ;; `collection`, which shares column names (type, archived, id) — unqualified refs would be ambiguous.collection-filter (if metabot-collection-id(let [collection (t2/select-one :model/Collection :id metabot-collection-id)collection-ids (conj (collection/descendant-ids collection) metabot-collection-id)]- [:in :collection_id collection-ids])+ [:in :report_card.collection_id collection-ids])[:and true])base-query {:select [:report_card.*]:from [[:report_card]]:where [:and[:!= :report_card.database_id audit-app/audit-db-id]collection-filter- [:in :type [:inline ["metric" "model"]]]- [:= :archived false]+ [:in :report_card.type [:inline ["metric" "model"]]]+ [:= :report_card.archived false](when api/*current-user-id*- (collection/visible-collection-filter-clause :collection_id))]}]+ (collection/visible-collection-filter-clause :report_card.collection_id))]}](cond-> base-query- ;; Prioritize verified content.- (premium-features/has-feature? :content-verification)- (assoc- :left-join [[:moderation_review :mr] [:and- [:= :mr.moderated_item_id :report_card.id]- [:= :mr.moderated_item_type [:inline "card"]]- [:= :mr.most_recent true]]]- :order-by [[[:case [:= :mr.status [:inline "verified"]] [:inline 0]- :else [:inline 1]]- :asc]])-- ;; Filter verified items only when that's desired.- (and (premium-features/has-feature? :content-verification)- use-verified-content?)- (update :where conj [:= :mr.status [:inline "verified"]])+ verified?+ (update :left-join (fnil into []) [[:moderation_review :mr] [:and+ [:= :mr.moderated_item_id :report_card.id]+ [:= :mr.moderated_item_type [:inline "card"]]+ [:= :mr.most_recent true]]])++ official?+ (update :left-join (fnil into []) [[:collection :collection]+ [:= :collection.id :report_card.collection_id]])++ ;; Prioritize curated content.+ (seq curated-conds)+ (assoc :order-by [[[:case (into [:or] curated-conds) [:inline 0] :else [:inline 1]] :asc]])++ ;; Restrict to curated content only when that's desired.+ (and use-verified-content? (seq curated-conds))+ (update :where conj (into [:or] curated-conds))++ ;; Setting on but no curation features active → nothing is curated, so return nothing rather than+ ;; falling through unfiltered to uncurated cards.+ (and use-verified-content? (empty? curated-conds))+ (update :where conj [:= [:inline 1] [:inline 0]])(integer? limit)(assoc :limit limit))))@@ -246,4 +270,5 @@Only cards visible to the current user are returned."[metabot-id & {:as opts}](t2/select :model/Card (-> (metabot-metrics-and-models-query metabot-id opts)- (update :order-by (fnil conj []) [:id]))))+ ;; qualified: the official-collections branch joins `collection`, which also has `id`+ (update :order-by (fnil conj []) [:report_card.id]))))diff --git a/src/metabase/search/config.clj b/src/metabase/search/config.cljindex e2f176520ad..8af1d7d5766 100644--- a/src/metabase/search/config.clj+++ b/src/metabase/search/config.clj@@ -69,6 +69,12 @@"Set of all valid models to search for. "(set (keys model-to-db-model)))+(def curated-search-models+ "Search models that can carry a curation signal, so the `:curated?` filter restricts to these (and+ keeps them consistent across the appdb and in-place engines). Includes `table`, which is exactly why+ curated content stays visible where the older verified-only filter dropped it (BOT-1536)."+ #{"card" "dataset" "metric" "dashboard" "table"})+(def models-search-order"The order of this list influences the order of the results: items earlier in thelist will be ranked higher."@@ -213,6 +219,11 @@:last-editor-id {:type :list, :context-key :last-edited-by}:native-query {:type :native-query, :context-key :search-native-query}:verified {:type :single-value, :supported-value? #{true}, :required-feature :content-verification}+ ;; "Verified or curated content" (Metabot). Backed by the precomputed `curated` index column+ ;; (an OR over verified + official + library/published + authoritative, computed at ingestion),+ ;; so the filter is a single indexed boolean. No :required-feature: a signal can only be set while+ ;; its feature is present, so the precomputed flag is already feature-correct.+ :curated {:type :single-value, :context-key :curated?, :supported-value? #{true}}:non-temporal-dim-ids {:type :single-value :engine :appdb}:has-temporal-dim {:type :single-value :engine :appdb}:display-type {:type :list, :field "display_type"}}))@@ -421,6 +432,8 @@[:table-db-id {:optional true} ms/PositiveInt];; true to search for verified items only, nil will return all items[:verified {:optional true} true?]+ ;; true to restrict to verified-or-curated content (precomputed `curated` index column)+ [:curated? {:optional true} true?][:ids {:optional true} [:set {:min 1} ms/PositiveInt]][:include-dashboard-questions? {:optional true} :boolean][:include-metadata? {:optional true} :boolean]diff --git a/src/metabase/search/filter.clj b/src/metabase/search/filter.cljindex 39d6f9668f6..3c23b77410f 100644--- a/src/metabase/search/filter.clj+++ b/src/metabase/search/filter.clj@@ -1,5 +1,6 @@(ns metabase.search.filter(:require+ [clojure.set :as set][honey.sql.helpers :as sql.helpers][metabase.collections.models.collection :as collection][metabase.premium-features.core :as premium-features]@@ -50,14 +51,23 @@[search-ctx];; Archived is an eccentric one - we treat it as false for models that don't map it, rather than removing them.;; TODO move this behavior to the spec somehow- (let [required (->> (remove-if-falsey search-ctx :archived?) keys (keep context-key->filter))]- (into #{}- (remove nil?)- (for [search-model (:models search-ctx)- :let [spec (search.spec/spec search-model)]]- (when (and (visible-to? search-ctx spec)- (every? (spec-supported-attr-keys spec) required))- (:name spec))))))+ ;; :curated? is a precomputed flag every index row carries, so it isn't a per-spec attr; handle it+ ;; explicitly below rather than through the spec-attr gate (which would drop every model). It restricts+ ;; to the curatable models — which include `table`, so curated content stays visible where the+ ;; verified-only filter dropped it (BOT-1536) — and matches the in-place engine for consistency.+ (let [required (->> (remove-if-falsey search-ctx :archived?)+ (#(dissoc % :curated?))+ keys+ (keep context-key->filter))+ models (into #{}+ (remove nil?)+ (for [search-model (:models search-ctx)+ :let [spec (search.spec/spec search-model)]]+ (when (and (visible-to? search-ctx spec)+ (every? (spec-supported-attr-keys spec) required))+ (:name spec))))]+ (cond-> models+ (:curated? search-ctx) (set/intersection search.config/curated-search-models))))(defn models-without-collection"A list of the search models which are not associated with collections, even indirectly."diff --git a/src/metabase/search/impl.clj b/src/metabase/search/impl.cljindex d409dec8d66..1d9abb13332 100644--- a/src/metabase/search/impl.clj+++ b/src/metabase/search/impl.clj@@ -275,6 +275,7 @@[:search-native-query {:optional true} [:maybe boolean?]][:model-ancestors? {:optional true} [:maybe boolean?]][:verified {:optional true} [:maybe true?]]+ [:curated {:optional true} [:maybe true?]][:ids {:optional true} [:maybe [:set ms/PositiveInt]]][:calculate-available-models? {:optional true} [:maybe :boolean]][:include-dashboard-questions? {:optional true} [:maybe boolean?]]@@ -315,6 +316,7 @@search-stringtable-db-idverified+ curatednon-temporal-dim-idshas-temporal-dimweights]} :- ::search-context.input]@@ -357,6 +359,7 @@(not (str/blank? vector-search-strategy)) (assoc :vector-search-strategy (keyword vector-search-strategy))(some? search-native-query) (assoc :search-native-query search-native-query)(some? verified) (assoc :verified verified)+ (some? curated) (assoc :curated? curated)(some? include-dashboard-questions?) (assoc :include-dashboard-questions? include-dashboard-questions?)(some? include-metadata?) (assoc :include-metadata? include-metadata?)(seq ids) (assoc :ids ids)diff --git a/src/metabase/search/in_place/filter.clj b/src/metabase/search/in_place/filter.cljindex 7127c43fb60..5f45d441eee 100644--- a/src/metabase/search/in_place/filter.clj+++ b/src/metabase/search/in_place/filter.clj@@ -157,6 +157,54 @@[:= :moderation_review.most_recent true]))(sql.helpers/where query false-clause)))+;; Curated filters — the "verified or curated content" setting for Metabot. The canonical rule is+;; [[metabase.collections.curation/curated?]], which the appdb/semantic engines apply via a precomputed+;; `curated` index column. The legacy in-place engine has no such column, so it approximates the rule+;; in SQL over the signals that are tractable here: verified + official-collection for card-like models,+;; and published-at-final-layer or authoritative for tables. Library-membership-by-root-collection-type is+;; not enforced here; the legacy engine is being retired. Keep in sync with [[curated?]].++(defn- curated-card-like-query+ "Build the curated OR clause for a card-like `model` (`item-type` is the moderation_review type)."+ [model item-type query]+ (let [or-clauses (cond-> []+ (premium-features/has-feature? :content-verification)+ (conj [:= :cur_mr.status "verified"])+ (premium-features/has-feature? :official-collections)+ (conj [:= :cur_coll.authority_level "official"]))]+ (-> query+ (cond-> (premium-features/has-feature? :content-verification)+ (sql.helpers/left-join [:moderation_review :cur_mr]+ [:and+ [:= :cur_mr.moderated_item_id (search.config/column-with-model-alias model :id)]+ [:= :cur_mr.moderated_item_type item-type]+ [:= :cur_mr.most_recent true]]))+ (cond-> (premium-features/has-feature? :official-collections)+ (sql.helpers/left-join [:collection :cur_coll]+ [:= :cur_coll.id (search.config/column-with-model-alias model :collection_id)]))+ (sql.helpers/where (if (seq or-clauses) (into [:or] or-clauses) false-clause)))))++(doseq [[model item-type] [["card" "card"] ["dataset" "card"] ["metric" "card"] ["dashboard" "dashboard"]]]+ (defmethod build-optional-filter-query [:curated model]+ [_filter model query curated?]+ (assert (true? curated?) "filter for non-curated content is not supported")+ (curated-card-like-query model item-type query)))++(defmethod build-optional-filter-query [:curated "table"]+ [_filter model query curated?]+ (assert (true? curated?) "filter for non-curated content is not supported")+ ;; Approximate collections.curation/curated? for tables: authoritative tables count unconditionally;+ ;; published tables count only at the `final` layer. Unlike curated? (feature-ungated, since the precomputed+ ;; column is only written while a feature is active), this live in-place filter gates the published branch+ ;; on the current :library feature, matching the card-like in-place filters above.+ (let [authoritative [:= (search.config/column-with-model-alias model :data_authority) "authoritative"]+ published-final (when (premium-features/has-feature? :library)+ [:and+ [:= (search.config/column-with-model-alias model :is_published) true]+ [:= (search.config/column-with-model-alias model :data_layer) "final"]])]+ (sql.helpers/where query (cond-> [:or authoritative]+ published-final (conj published-final)))))+;; Created at filters(defn- date-range-filter-clause@@ -346,6 +394,7 @@modelssearch-native-queryverified+ curated?non-temporal-dim-idshas-temporal-dimdisplay-type@@ -362,6 +411,7 @@(some? last-edited-by) (set/intersection (:last-edited-by feature->supported-models))(true? search-native-query) (set/intersection (:search-native-query feature->supported-models))(true? verified) (set/intersection (:verified feature->supported-models))+ (true? curated?) (set/intersection (:curated feature->supported-models))(some? non-temporal-dim-ids) (set/intersection (:non-temporal-dim-ids feature->supported-models))(some? has-temporal-dim) (set/intersection (:has-temporal-dim feature->supported-models))(seq display-type) (set/intersection (:display-type feature->supported-models)))))@@ -381,6 +431,7 @@search-stringsearch-native-queryverified+ curated?idsnon-temporal-dim-idshas-temporal-dim@@ -413,6 +464,9 @@(some? verified)(#(build-optional-filter-query :verified model % verified))+ (true? curated?)+ (#(build-optional-filter-query :curated model % curated?))+(and (some? ids)(contains? models model))(#(build-optional-filter-query :id model % ids))diff --git a/src/metabase/search/ingestion.clj b/src/metabase/search/ingestion.cljindex 25ddde12112..16a53fe1b48 100644--- a/src/metabase/search/ingestion.clj+++ b/src/metabase/search/ingestion.clj@@ -5,6 +5,7 @@[medley.core :as m][metabase.analytics-interface.core :as analytics][metabase.app-db.core :as mdb]+ [metabase.collections.curation :as collections.curation][metabase.lib-be.core :as lib-be][metabase.search.engine :as search.engine][metabase.search.spec :as search.spec]@@ -140,10 +141,17 @@(update :archived boolean)(assoc:display_data (display-data m)- :legacy_input (json/encode (apply dissoc m search.spec/legacy-input-excluded-keys)):searchable_text (searchable-text m)- :embeddable_text (embeddable-text m)))]- (merge fn-results sql-results)))+ :embeddable_text (embeddable-text m)))+ document (merge fn-results sql-results)+ curated (collections.curation/curated? document)]+ ;; Both production engines reconstruct results from legacy_input (appdb rehydrate, semantic+ ;; legacy-input-with-score), so curated must live there to reach the search response, not just in the+ ;; search_index column used for filtering. data_layer rides along once it leaves the excluded set.+ (assoc document+ :curated curated+ :legacy_input (json/encode (assoc (apply dissoc m search.spec/legacy-input-excluded-keys)+ :curated curated)))))(defn- attrs->select-items [attrs](for [[k v] attrsdiff --git a/src/metabase/search/spec.clj b/src/metabase/search/spec.cljindex 9ac04cc480a..56fd9974f43 100644--- a/src/metabase/search/spec.clj+++ b/src/metabase/search/spec.clj@@ -92,7 +92,8 @@;; - `metabase.search.impl/add-dataset-collection-hierarchy` reads `:collection_location` to hydrate;; `:collection_effective_ancestors`, and the collection-result hydration path reads `:location` from;; the toucan instance (which the render-term keeps populated).- #{:pinned :view_count :last_viewed_at :native_query :dataset_query :data_layer})+ ;; `:data_layer` also stays IN: Metabot surfaces it on table results so the LLM sees a table's data layer.+ #{:pinned :view_count :last_viewed_at :native_query :dataset_query})(def attr-types"The abstract types of each attribute."@@ -123,7 +124,11 @@:collection-type :text:collection-location :text:root-collection-type :text- :data-layer :text})+ :data-layer :text+ :data-authority :text+ ;; Precomputed at ingestion (see metabase.search.ingestion) from the curation signals above, so the+ ;; "verified or curated content" filter is a single indexed boolean rather than a composite OR.+ :curated :boolean})(def ^:private explicit-attrs"These attributes must be explicitly defined, omitting them could be a source of bugs."@@ -151,7 +156,8 @@:collection-type ;; surfaced for downstream consumers (metabase.search.impl/serialize):collection-location ;; surfaced for downstream consumers (add-dataset-collection-hierarchy):root-collection-type ;; indexed for :library scorer — type of the top-level ancestor collection- :data-layer]) ;; indexed for the :data-layer scorer (table.data_layer; per-tier weights under :data-layer/*)+ :data-layer ;; indexed for the :data-layer scorer (table.data_layer; per-tier weights under :data-layer/*)+ :data-authority]) ;; input to the precomputed :curated flag (authoritative tables)distinctvec))diff --git a/src/metabase/warehouse_schema/models/table.clj b/src/metabase/warehouse_schema/models/table.cljindex 09a6acad3fc..a264ea6d46a 100644--- a/src/metabase/warehouse_schema/models/table.clj+++ b/src/metabase/warehouse_schema/models/table.clj@@ -692,7 +692,8 @@:collection-type :collection.type:collection-location :collection.location:root-collection-type {:fn collection/root-collection-type}- :data-layer :data_layer}+ :data-layer :data_layer+ :data-authority :data_authority}:search-terms {:name search.spec/explode-camel-case:display_name true:description true}diff --git a/test/metabase/collections/curation_test.clj b/test/metabase/collections/curation_test.cljnew file mode 100644index 00000000000..f31cb89a3a5--- /dev/null+++ b/test/metabase/collections/curation_test.clj@@ -0,0 +1,34 @@+(ns metabase.collections.curation-test+ (:require+ [clojure.test :refer :all]+ [metabase.collections.curation :as collections.curation]))++(deftest curated?-test+ (testing "verified card/dashboard is curated"+ (is (collections.curation/curated? {:model "card" :verified true}))+ (is (collections.curation/curated? {:model "dashboard" :verified true})))+ (testing "official-collection content is curated"+ (is (collections.curation/curated? {:model "card" :official_collection true})))+ (testing "plain content is not curated"+ (is (not (collections.curation/curated? {:model "card"})))+ (is (not (collections.curation/curated? {:model "table"}))))+ (testing "non-table library content is always curated"+ (is (collections.curation/curated? {:model "metric" :root_collection_type "library-metrics"}))+ (is (collections.curation/curated? {:model "segment" :root_collection_type "library"})))+ (testing "published tables count only when at the final data layer (BOT-1536)"+ (is (collections.curation/curated? {:model "table" :is_published true :data_layer "final"}))+ (is (not (collections.curation/curated? {:model "table" :is_published true :data_layer "internal"})))+ (is (not (collections.curation/curated? {:model "table" :is_published true :data_layer "hidden"}))))+ (testing "authoritative tables are curated regardless of data layer"+ (is (collections.curation/curated? {:model "table" :data_authority "authoritative"}))+ (is (collections.curation/curated? {:model "table" :data_authority "authoritative" :data_layer "internal"})))+ (testing "text signals are normalized whether keyword or string"+ (is (collections.curation/curated? {:model "table" :is_published true :data_layer :final}))+ (is (collections.curation/curated? {:model :metric :root_collection_type :library-metrics}))+ (is (collections.curation/curated? {:model "table" :data_authority :authoritative})))+ (testing "boolean signals are coerced — only true/1 count, not numeric 0 (DB driver quirk)"+ (is (collections.curation/curated? {:model "card" :verified 1}))+ (is (collections.curation/curated? {:model "card" :official_collection 1}))+ (is (not (collections.curation/curated? {:model "card" :verified 0})))+ (is (not (collections.curation/curated? {:model "card" :official_collection 0})))+ (is (not (collections.curation/curated? {:model "table" :is_published 0 :data_layer "final"})))))diff --git a/test/metabase/metabot/api/metabot_test.clj b/test/metabase/metabot/api/metabot_test.cljindex adcaaa99ce1..83b1c6d406d 100644--- a/test/metabase/metabot/api/metabot_test.clj+++ b/test/metabase/metabot/api/metabot_test.clj@@ -8,7 +8,7 @@[metabase.metabot.example-question-generator :as metabot.example-question-generator][metabase.metabot.settings :as metabot.settings][metabase.metabot.suggested-prompts :as metabot.suggested-prompts]- [metabase.metabot.usage :as metabot.usage]+ [metabase.metabot.task.suggested-prompts-refresh :as metabot.suggested-prompts-refresh][metabase.permissions.core :as perms][metabase.permissions.models.permissions-group :as perms-group][metabase.premium-features.core :as premium-features]@@ -269,79 +269,9 @@(format "metabot/metabot/%d" metabot-id){:use_verified_content true}))))))))))-(deftest metabot-put-skips-prompt-regeneration-when-managed-provider-is-locked-test- (mt/dataset test-data- (let [model-query {:type :query, :database (mt/id), :query {:source-table (mt/id :products)}}]- (mt/with-temporary-setting-values [metabot.settings/llm-metabot-provider- "metabase/anthropic/claude-sonnet-4-6"]- (mt/with-temp [:model/Collection {collection-id :id} {:name "Collection"}- :model/Metabot {metabot-id :id} {:name "Test Metabot"- :collection_id collection-id}- :model/Card {card-id :id} {:name "Test Model Card"- :type :model- :dataset_query model-query}- :model/MetabotPrompt {prompt-id :id} {:metabot_id metabot-id- :prompt "existing prompt"- :model :model- :card_id card-id}]- (mt/with-dynamic-fn-redefs [premium-features/token-status- (constantly {:meters {:anthropic:claude-sonnet-4-6:tokens {:meter-value 1000000- :is-locked true}}})- metabot.suggested-prompts/delete-all-metabot-prompts- (fn [& _]- (throw (ex-info "should not delete prompts" {})))- metabot.suggested-prompts/generate-sample-prompts- (fn [& _]- (throw (ex-info "should not generate prompts" {})))]- (let [response (mt/user-http-request :crowberto :put 200- (format "metabot/metabot/%d" metabot-id)- {:collection_id nil})]- (is (nil? (:collection_id response)))- (is (= #{prompt-id}- (t2/select-pks-set :model/MetabotPrompt :metabot_id metabot-id))))))))))--(deftest metabot-put-swallows-managed-locked-toctou-from-generate-sample-prompts-test- (testing "if the managed-AI lock flips between the pre-check and `generate-sample-prompts`,- the PUT still succeeds (best-effort regeneration) — the 402 is swallowed, and the- delete is rolled back with it so existing prompts are preserved."- (mt/with-temp [:model/Collection {collection-id :id} {:name "Collection"}- :model/Metabot {metabot-id :id} {:name "Test Metabot"- :collection_id collection-id}- :model/Card {card-id :id} {:name "Test Model Card" :type :model}- :model/MetabotPrompt {prompt-id :id} {:metabot_id metabot-id- :prompt "existing prompt"- :model :model- :card_id card-id}]- (mt/with-dynamic-fn-redefs [metabot.usage/managed-free-limit-reached? (constantly false)- metabot.suggested-prompts/generate-sample-prompts- (fn [& _]- (throw (ex-info "locked" {:status-code 402- :error-code "metabase_ai_managed_locked"})))]- (let [response (mt/user-http-request :crowberto :put 200- (format "metabot/metabot/%d" metabot-id)- {:collection_id nil})]- (is (nil? (:collection_id response)))- (is (= #{prompt-id}- (t2/select-pks-set :model/MetabotPrompt :metabot_id metabot-id)))))))- (testing "non-locked ex-info from generate-sample-prompts still propagates, and the delete is- rolled back so existing prompts survive"- (mt/with-temp [:model/Collection {collection-id :id} {:name "Collection"}- :model/Metabot {metabot-id :id} {:name "Test Metabot"- :collection_id collection-id}- :model/Card {card-id :id} {:name "Test Model Card" :type :model}- :model/MetabotPrompt {prompt-id :id} {:metabot_id metabot-id- :prompt "existing prompt"- :model :model- :card_id card-id}]- (mt/with-dynamic-fn-redefs [metabot.usage/managed-free-limit-reached? (constantly false)- metabot.suggested-prompts/generate-sample-prompts- (fn [& _]- (throw (ex-info "boom" {:status-code 500})))]- (mt/user-http-request :crowberto :put 500- (format "metabot/metabot/%d" metabot-id)- {:collection_id nil})- (is (= #{prompt-id}- (t2/select-pks-set :model/MetabotPrompt :metabot_id metabot-id)))))))+;; PUT now schedules a background refresh (see suggested-prompts-refresh) instead of regenerating+;; inline, so the managed-AI lock / TOCTOU / rollback behavior is covered in+;; metabase.metabot.task.suggested-prompts-refresh-test rather than here.(deftest metabot-prompt-regenerate-returns-free-trial-limit-error-when-managed-provider-is-locked-test(mt/dataset test-data@@ -406,93 +336,35 @@(is (pos-int? card-id)))))))))(deftest metabot-prompt-regeneration-on-config-change-test- (mt/dataset test-data- (testing "PUT /api/metabot/metabot/:id should regenerate prompts when config changes"- (mt/with-premium-features #{:content-verification}- (let [metric-query {:type :query, :database (mt/id), :query {:source-table (mt/id :products)}}- model-query {:type :query, :database (mt/id), :query {:source-table (mt/id :products)}}]- (mt/with-temp [:model/Collection {collection-id-1 :id} {:name "Collection 1"}- :model/Collection {collection-id-2 :id} {:name "Collection 2"}- :model/Card {card-id-1 :id} {:name "Test Metric Card"- :type :metric- :dataset_query metric-query}- :model/Card {card-id-2 :id} {:name "Test Model Card"- :type :model- :dataset_query model-query}- :model/Card {card-id-3 :id} {:name "Test Card for New Prompt"- :type :metric- :dataset_query metric-query}- :model/Card {card-id-4 :id} {:name "Test Card for Collection Change"- :type :model- :dataset_query model-query}- :model/Card {card-id-5 :id} {:name "Test Card for Baseline"- :type :metric- :dataset_query metric-query}- :model/Metabot {metabot-id :id} {:name "Test Metabot"- :use_verified_content false- :collection_id collection-id-1}- :model/MetabotPrompt {prompt-id-1 :id} {:metabot_id metabot-id- :prompt "old prompt 1"- :model :metric- :card_id card-id-1}- :model/MetabotPrompt {prompt-id-2 :id} {:metabot_id metabot-id- :prompt "old prompt 2"- :model :model- :card_id card-id-2}]- (let [original-prompt-ids #{prompt-id-1 prompt-id-2}]- (testing "should regenerate prompts when use_verified_content changes"- (mt/with-dynamic-fn-redefs [metabot.suggested-prompts/generate-sample-prompts- (fn [metabot-id]- (t2/insert! :model/MetabotPrompt {:metabot_id metabot-id- :prompt "new prompt after verified change"- :model :metric- :card_id card-id-3}))]- (mt/user-http-request :crowberto :put 200- (format "metabot/metabot/%d" metabot-id)- {:use_verified_content true})- ;; Verify old prompts were deleted and new ones created- (let [current-prompts (t2/select :model/MetabotPrompt :metabot_id metabot-id)- current-prompt-ids (set (map :id current-prompts))]- (is (= 1 (count current-prompts)))- (is (empty? (set/intersection original-prompt-ids current-prompt-ids)))- (is (= "new prompt after verified change" (:prompt (first current-prompts)))))))- (testing "should regenerate prompts when collection_id changes"- (mt/with-dynamic-fn-redefs [metabot.suggested-prompts/generate-sample-prompts- (fn [metabot-id]- (t2/insert! :model/MetabotPrompt {:metabot_id metabot-id- :prompt "new prompt after collection change"- :model :model- :card_id card-id-4}))]- (mt/user-http-request :crowberto :put 200- (format "metabot/metabot/%d" metabot-id)- {:collection_id collection-id-2})- ;; Verify prompts were regenerated again- (let [current-prompts (t2/select :model/MetabotPrompt :metabot_id metabot-id)]- (is (= 1 (count current-prompts)))- (is (= "new prompt after collection change" (:prompt (first current-prompts)))))))- (testing "should NOT regenerate prompts when no relevant fields change"- ;; First, establish a baseline- (t2/delete! :model/MetabotPrompt :metabot_id metabot-id)- (t2/insert! :model/MetabotPrompt {:metabot_id metabot-id- :prompt "baseline prompt"- :model :metric- :card_id card-id-5})- (let [baseline-prompts (t2/select :model/MetabotPrompt :metabot_id metabot-id)- baseline-ids (set (map :id baseline-prompts))]- ;; Make a PUT request that doesn't change verified content or collection_id- ;; (This would be if we add other fields to update in the future)- (mt/with-dynamic-fn-redefs [metabot.suggested-prompts/generate-sample-prompts- (fn [_] (throw (Exception. "Should not be called")))]- (mt/user-http-request :crowberto :put 200- (format "metabot/metabot/%d" metabot-id)- {:use_verified_content true ; Same as current value- :collection_id collection-id-2})) ; Same as current value-- ;; Verify prompts were NOT changed- (let [current-prompts (t2/select :model/MetabotPrompt :metabot_id metabot-id)- current-ids (set (map :id current-prompts))]- (is (= baseline-ids current-ids))- (is (= "baseline prompt" (:prompt (first current-prompts))))))))))))))+ (testing "PUT /api/metabot/metabot/:id schedules a background prompt refresh when the content scope changes"+ (mt/with-premium-features #{:content-verification}+ (mt/with-temp [:model/Collection {collection-id-1 :id} {:name "Collection 1"}+ :model/Collection {collection-id-2 :id} {:name "Collection 2"}+ :model/Metabot {metabot-id :id} {:name "Test Metabot"+ :use_verified_content false+ :collection_id collection-id-1}]+ (let [scheduled (atom [])]+ (mt/with-dynamic-fn-redefs [metabot.suggested-prompts-refresh/schedule-refresh!+ (fn [id] (swap! scheduled conj id))]+ (testing "schedules a refresh when use_verified_content changes"+ (reset! scheduled [])+ (mt/user-http-request :crowberto :put 200+ (format "metabot/metabot/%d" metabot-id)+ {:use_verified_content true})+ (is (= [metabot-id] @scheduled)))+ (testing "schedules a refresh when collection_id changes"+ (reset! scheduled [])+ (mt/user-http-request :crowberto :put 200+ (format "metabot/metabot/%d" metabot-id)+ {:collection_id collection-id-2})+ (is (= [metabot-id] @scheduled)))+ (testing "does NOT schedule a refresh when no relevant field changes"+ (reset! scheduled [])+ (mt/user-http-request :crowberto :put 200+ (format "metabot/metabot/%d" metabot-id)+ {:use_verified_content true ; same as current+ :collection_id collection-id-2}) ; same as current+ (is (= [] @scheduled)))))))))(deftest prompt-suggestions-collection-permissions-test(testing "GET /api/metabot/metabot/:id/prompt-suggestions respects collection permissions"diff --git a/test/metabase/metabot/context_test.clj b/test/metabase/metabot/context_test.cljindex f7a91f96ab8..833a496ad1a 100644--- a/test/metabase/metabot/context_test.clj+++ b/test/metabase/metabot/context_test.clj@@ -8,6 +8,7 @@[metabase.lib.test-metadata :as meta][metabase.metabot.agent.user-context :as user-context][metabase.metabot.context :as context]+ [metabase.metabot.curation :as curation][metabase.metabot.table-utils :as table-utils][metabase.test :as mt][toucan2.core :as t2]))@@ -261,7 +262,7 @@:user_recently_viewed))))))(deftest recent-views-verified-content-filter-test- (testing "Recent views filtering by verified content"+ (testing "Recent views filtering by curated content (verified is one curation signal)"(mt/with-test-user :rasta(mt/with-temp [:model/Card {vq1 :id} {:type "question" :name "verified q1"}:model/Card {vq2 :id} {:type "question" :name "verified q2"}@@ -313,12 +314,12 @@(is (contains? ks um*))(is (contains? ks ud*))(is (contains? ks table*)))))- (testing "use_verified_content=true with :content-verification feature -> filters"+ (testing "use_verified_content=true -> filters recents to curated content"(mt/with-premium-features #{:content-verification}(let [items (-> (context/create-context {} {:metabot-id metabot-eid}):user_recently_viewed)ks (keys-of items)]- (is (contains? ks table*) "tables are not moderatable and pass through")+ (is (not (contains? ks table*)) "a plain table is not curated, so it is filtered out")(is (contains? ks vd*))(is (not (contains? ks uq*)))(is (not (contains? ks um*)))@@ -328,14 +329,15 @@"Should keep 5 items even though 3 unverified items were ahead of older verified items")(is (contains? ks vm2*))(is (contains? ks vm1*))))))- (testing "use_verified_content=true but premium feature absent -> no filtering"+ (testing "filtering is gated on the setting, not the :content-verification feature — curated is+ precomputed at ingestion, so recents still filter without the feature (matches search)"(mt/with-premium-features #{}(let [items (-> (context/create-context {} {:metabot-id metabot-eid}):user_recently_viewed)ks (keys-of items)]- (is (contains? ks uq*))- (is (contains? ks um*))- (is (contains? ks ud*)))))+ (is (not (contains? ks uq*)))+ (is (not (contains? ks um*)))+ (is (not (contains? ks ud*))))))(testing "metabot-id that does not resolve -> no filtering"(mt/with-premium-features #{:content-verification}(let [items (-> (context/create-context {} {:metabot-id "nonexistent-entity-id"})@@ -415,3 +417,10 @@_ (#'context/enhance-context-with-schema input)](is (true? @called-native?) "Should use native SQL parsing path")(is (false? @called-mbql?) "Should NOT use MBQL path for native queries"))))))++(deftest filter-recents-to-curated-test+ (testing "keeps only recents that collections.curation marks curated, keyed by [model id] (BOT-1570)"+ (let [recents [{:model :card :id 1} {:model :table :id 2} {:model :dashboard :id 3}]]+ (mt/with-dynamic-fn-redefs [curation/curated-ids (constantly #{["card" 1] ["table" 2]})]+ (is (= [{:model :card :id 1} {:model :table :id 2}]+ (#'context/filter-recents-to-curated recents)))))))diff --git a/test/metabase/metabot/curation_test.clj b/test/metabase/metabot/curation_test.cljnew file mode 100644index 00000000000..aa3e8649fee--- /dev/null+++ b/test/metabase/metabot/curation_test.clj@@ -0,0 +1,48 @@+(ns metabase.metabot.curation-test+ (:require+ [clojure.test :refer :all]+ [metabase.content-verification.core :as moderation]+ [metabase.metabot.curation :as metabot.curation]+ [metabase.search.core :as search]+ [metabase.search.test-util :as search.tu]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(deftest curated-ids-consistent-with-index-test+ (testing "curated-ids reads the same verdict from source-of-truth tables that the search index+ precomputes from the specs, so the two definitions stay consistent (BOT-1570)"+ (search.tu/with-new-search-if-available-without-fallback+ (mt/with-temp [:model/Database {db :id} {}+ :model/Collection {off :id} {:authority_level "official"}+ ;; name avoids the "curationfixture" search token so the collection itself isn't a+ ;; result (curated-ids only handles recent-view models, not collections)+ :model/Collection {lib :id} {:type "library-data" :name "fixturelibcoll"}+ :model/Card {vq :id} {:name "curationfixture verified"}+ :model/Card {oq :id} {:name "curationfixture official" :collection_id off}+ :model/Card {pq :id} {:name "curationfixture plain"}+ :model/Table {pt :id} {:db_id db :name "curationfixture published" :active true+ :is_published true :data_layer :final}+ :model/Table {at :id} {:db_id db :name "curationfixture authoritative" :active true+ :data_authority :authoritative}+ :model/Table {xt :id} {:db_id db :name "curationfixture plaintable" :active true}+ ;; final-layer table under a library collection but NOT published: the spec joins the+ ;; collection only for published tables, so neither source nor index marks it curated+ :model/Table {ult :id} {:db_id db :name "curationfixture unpublishedlib" :active true+ :is_published false :data_layer :final :collection_id lib}]+ (moderation/create-review! {:moderated_item_id vq+ :moderated_item_type "card"+ :moderator_id (mt/user->id :crowberto)+ :status "verified"})+ ;; re-ingest the now-verified card so the index reflects it+ (search/update! (t2/select-one :model/Card :id vq) true)+ (let [items [["card" vq] ["card" oq] ["card" pq]+ ["table" pt] ["table" at] ["table" xt] ["table" ult]]+ source (metabot.curation/curated-ids items)+ ;; the index-derived verdict, read back off search results (which now carry :curated)+ indexed (into #{} (comp (filter :curated) (map (juxt :model :id)))+ (search.tu/search-results "curationfixture"))+ ->strs #(into #{} (map (fn [[m id]] [m (str id)])) %)]+ (is (= #{["card" vq] ["card" oq] ["table" pt] ["table" at]} source)+ "source-of-truth marks verified/official cards and published-final/authoritative tables curated")+ (is (= (->strs source) (->strs indexed))+ "source-of-truth agrees with the index's precomputed curated column"))))))diff --git a/test/metabase/metabot/task/suggested_prompts_refresh_test.clj b/test/metabase/metabot/task/suggested_prompts_refresh_test.cljnew file mode 100644index 00000000000..f197e04fb87--- /dev/null+++ b/test/metabase/metabot/task/suggested_prompts_refresh_test.clj@@ -0,0 +1,63 @@+(ns metabase.metabot.task.suggested-prompts-refresh-test+ (:require+ [clojure.test :refer :all]+ [metabase.metabot.suggested-prompts :as metabot.suggested-prompts]+ [metabase.metabot.task.suggested-prompts-refresh :as metabot.suggested-prompts-refresh]+ [metabase.metabot.usage :as metabot.usage]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(def ^:private regenerate! @#'metabot.suggested-prompts-refresh/regenerate!)++(deftest regenerate!-test+ (testing "skips generation when the managed-AI limit is reached — existing prompts are left intact"+ (mt/with-temp [:model/Metabot {metabot-id :id} {:name "mb"}+ :model/Card {card-id :id} {:name "c" :type :model}+ :model/MetabotPrompt {prompt-id :id} {:metabot_id metabot-id :prompt "keep"+ :model :model :card_id card-id}]+ (mt/with-dynamic-fn-redefs [metabot.usage/managed-free-limit-reached? (constantly true)+ metabot.suggested-prompts/delete-all-metabot-prompts+ (fn [& _] (throw (ex-info "should not delete" {})))+ metabot.suggested-prompts/generate-sample-prompts+ (fn [& _] (throw (ex-info "should not generate" {})))]+ (regenerate! metabot-id)+ (is (= #{prompt-id} (t2/select-pks-set :model/MetabotPrompt :metabot_id metabot-id))))))+ (testing "a generation failure is swallowed and rolls back the delete, so existing prompts survive"+ (mt/with-temp [:model/Metabot {metabot-id :id} {:name "mb"}+ :model/Card {card-id :id} {:name "c" :type :model}+ :model/MetabotPrompt {prompt-id :id} {:metabot_id metabot-id :prompt "keep"+ :model :model :card_id card-id}]+ (mt/with-dynamic-fn-redefs [metabot.usage/managed-free-limit-reached? (constantly false)+ metabot.suggested-prompts/generate-sample-prompts+ (fn [& _] (throw (ex-info "boom" {})))]+ (is (nil? (regenerate! metabot-id)))+ (is (= #{prompt-id} (t2/select-pks-set :model/MetabotPrompt :metabot_id metabot-id))))))+ (testing "happy path replaces existing prompts with freshly generated ones"+ (mt/with-temp [:model/Metabot {metabot-id :id} {:name "mb"}+ :model/Card {card-id :id} {:name "c" :type :model}+ :model/MetabotPrompt {old-id :id} {:metabot_id metabot-id :prompt "old"+ :model :model :card_id card-id}]+ (mt/with-dynamic-fn-redefs [metabot.usage/managed-free-limit-reached? (constantly false)+ metabot.suggested-prompts/generate-sample-prompts+ (fn [mid]+ (t2/insert! :model/MetabotPrompt {:metabot_id mid :prompt "new"+ :model :model :card_id card-id})+ {:status :generated :prompt_count 1})]+ (regenerate! metabot-id)+ (let [prompts (t2/select :model/MetabotPrompt :metabot_id metabot-id)]+ (is (= 1 (count prompts)))+ (is (= "new" (:prompt (first prompts))))+ (is (not= old-id (:id (first prompts))))))))+ (testing "when regeneration produces nothing, existing prompts are preserved (not wiped to empty)"+ (doseq [status [:no-library-content :ai-produced-no-prompts]]+ (mt/with-temp [:model/Metabot {metabot-id :id} {:name "mb"}+ :model/Card {card-id :id} {:name "c" :type :model}+ :model/MetabotPrompt {prompt-id :id} {:metabot_id metabot-id :prompt "keep"+ :model :model :card_id card-id}]+ (mt/with-dynamic-fn-redefs [metabot.usage/managed-free-limit-reached? (constantly false)+ metabot.suggested-prompts/generate-sample-prompts (constantly {:status status})]+ (regenerate! metabot-id)+ (is (= #{prompt-id} (t2/select-pks-set :model/MetabotPrompt :metabot_id metabot-id))+ (str "prompts preserved when generation returns " status)))))))diff --git a/test/metabase/metabot/tools/util_test.clj b/test/metabase/metabot/tools/util_test.cljindex 6e6eb3ca40b..bbd9bfa6780 100644--- a/test/metabase/metabot/tools/util_test.clj+++ b/test/metabase/metabot/tools/util_test.clj@@ -223,6 +223,33 @@(is (every? verified-ids (take 2 ordered-ids)))(is (every? unverified-ids (drop 2 ordered-ids))))))))))))+(deftest metabot-verified-content-library-test+ (testing "with the :library feature, use_verified_content=true includes library-published metrics/models+ (BOT-1570), matching collections.curation/curated?"+ (mt/with-premium-features #{:library}+ (mt/with-temp [:model/Collection lib {:type "library-metrics" :name "libcoll"}+ :model/Collection norm {:name "normcoll"}+ :model/Card lib-metric {:type :metric :collection_id (:id lib)}+ :model/Card plain-metric {:type :metric :collection_id (:id norm)}+ :model/Metabot metabot {:name "mb" :use_verified_content true}]+ (let [ids (set (map :id (mt/with-test-user :crowberto+ (metabot.tools.util/get-metrics-and-models (:id metabot)))))]+ (is (contains? ids (:id lib-metric)) "library-published metric is curated")+ (is (not (contains? ids (:id plain-metric))) "plain metric is not curated"))))))++(deftest metabot-verified-content-no-feature-test+ (testing "use_verified_content=true with no curation features active returns nothing, rather than falling+ through unfiltered to uncurated content"+ (mt/with-premium-features #{}+ (mt/with-temp [:model/Collection metabot-coll {:name "mb coll"}+ :model/Card _model {:type :model :collection_id (:id metabot-coll)}+ :model/Card _metric {:type :metric :collection_id (:id metabot-coll)}+ :model/Metabot metabot {:name "verified metabot"+ :collection_id (:id metabot-coll)+ :use_verified_content true}]+ (is (empty? (mt/with-test-user :crowberto+ (metabot.tools.util/get-metrics-and-models (:id metabot)))))))))+(deftest get-table-filters-inactive-test(testing "get-table only returns active tables"(mt/with-temp [:model/Database {db-id :id} {}diff --git a/test/metabase/search/api_test.clj b/test/metabase/search/api_test.cljindex 2276357f515..93d31ace555 100644--- a/test/metabase/search/api_test.clj+++ b/test/metabase/search/api_test.clj@@ -87,6 +87,13 @@(defn- clean-result [result](cond-> (dissoc (u/remove-nils result) :database_id :table_id :last_editor_id :last_edited_at:creator_common_name :creator_id+ ;; Metabot curation signals now ride in search results: curated on every model, and+ ;; table data_authority/data_layer (DB-specific defaults, e.g. data_authority+ ;; "unconfigured" on H2/Postgres vs NULL on MySQL; data_layer nil on H2 vs "internal" on+ ;; MySQL/MariaDB) — all incidental to these assertions.+ :curated+ :data_authority+ :data_layer;; false for new search segments... not sure why:created_at)(false? (:model_id result))diff --git a/test/metabase/search/filter_test.clj b/test/metabase/search/filter_test.cljindex 9253078d2ac..8d529cd4feb 100644--- a/test/metabase/search/filter_test.clj+++ b/test/metabase/search/filter_test.clj@@ -92,6 +92,7 @@:last-edited-at "2024-10-02":search-native-query true:verified true+ :curated? true:ids [1 2 3 4]:non-temporal-dim-ids "[1]":has-temporal-dim true@@ -127,6 +128,7 @@[:or [:= :search_index.collection_id 5] [:like :collection.location "%/5/%"]][:not= :search_index.model [:inline "table"]][:= :search_index.archived true]+ [:= :search_index.curated true][:in :search_index.model ["card" "dataset" "metric" "dashboard" "action"]][:or[:= nil :search_index.dashboard_id]@@ -173,3 +175,9 @@:search_index.collection_id)](is (=? [:or any? [:exists any?]]clause)))))++(deftest in-place-curated-table-filter-counts-authoritative-test+ (testing "the in-place curated table filter counts authoritative tables regardless of publish state,+ mirroring collections.curation/curated? (BOT-1570)"+ (let [where (:where (#'search.in-place.filter/build-optional-filter-query :curated "table" {} true))]+ (is (some #{"authoritative"} (tree-seq coll? seq where))))))diff --git a/test/metabase/search/ingestion_test.clj b/test/metabase/search/ingestion_test.cljindex 74b951059b7..6d7519ae8c0 100644--- a/test/metabase/search/ingestion_test.clj+++ b/test/metabase/search/ingestion_test.clj@@ -8,6 +8,7 @@[metabase.search.engine :as search.engine][metabase.search.ingestion :as search.ingestion][metabase.search.spec :as search.spec]+ [metabase.search.test-util :as search.tu][metabase.test :as mt]))(deftest extract-model-and-id@@ -188,3 +189,19 @@(search/init-index!)(is (= 3 @factory-calls)"Factory should be called once per unique database-id, not once per lookup")))))++(deftest curation-signals-surfaced-in-results-test+ (testing "curated (all models) and table data_layer ride through legacy_input to appdb search results,+ so Metabot can render them (BOT-1570) — not just stored in the filtering column"+ (search.tu/with-new-search-if-available-without-fallback+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table _ {:db_id db-id+ :name "Curatedgoldtbl"+ :active true+ :data_authority :authoritative+ :data_layer :final}]+ (let [result (->> (search.tu/search-results "Curatedgoldtbl")+ (filter (comp #{"table"} :model))+ first)]+ (is (=? {:model "table" :curated true :data_authority "authoritative" :data_layer "final"}+ result)))))))diff --git a/test/metabase/search/spec_test.clj b/test/metabase/search/spec_test.cljindex cf298765df0..e3eb9a217e6 100644--- a/test/metabase/search/spec_test.clj+++ b/test/metabase/search/spec_test.clj@@ -102,7 +102,7 @@:fields#{:active :description :schema :name :id :db_id :initial_sync_status :display_name:visibility_type :view_count :created_at :updated_at :collection_id :is_published- :data_layer}+ :data_layer :data_authority}:where [:= :updated.id :this.id]}},:Database #{{:search-model "table":fields #{:name :router_database_id}