Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Check source permissions before Metabot returns X-ray links (#84624)

Andrei Simionescu67c90908a87a
diff --git a/src/metabase/metabot/tools/autogen_dashboard.clj b/src/metabase/metabot/tools/autogen_dashboard.clj
index 7d4461d1caa..98be94511c6 100644
--- a/src/metabase/metabot/tools/autogen_dashboard.clj
+++ b/src/metabase/metabot/tools/autogen_dashboard.clj
@@ -2,11 +2,12 @@
"Autogenerated dashboard tool wrappers."
(:require
[clojure.string :as str]
+ [metabase.api.common :as api]
[metabase.metabot.agent.streaming :as streaming]
[metabase.metabot.db :as metabot.db]
[metabase.metabot.scope :as scope]
+ [metabase.metabot.tools.core :as tools]
[metabase.metabot.tools.shared :as shared]
- [metabase.metabot.tools.util :as metabot.tools.u]
[metabase.util.log :as log]
[metabase.util.malli :as mu]))
@@ -14,42 +15,49 @@
;;; Source Resolution
+(tools/defrecoverable invalid-source!
+ "The call set no source, or more than one."
+ {:payload [:map {:closed true}]}
+ [_]
+ {:message "Set exactly one of `table_id`, `model_id`, `metric_id`, `query_id` or `report_id` in `source`."
+ :recovery []})
+
+(tools/defrecoverable unavailable-source!
+ "The user can read the source, but it is in the trash, or it is a table that is gone from its database."
+ {:payload [:map {:closed true}
+ [:kind [:enum :table :model :metric :question]]
+ [:id :int]]}
+ [{:keys [kind id]}]
+ {:message (str (str/capitalize (name kind)) " " id
+ (if (= :table kind)
+ " is no longer in its database."
+ " is in the trash."))
+ :recovery [{:uses #{"search"}
+ :text "Call `search` to find another source and use an id from the results."}]})
+
+(defn- readable-source
+ "The `kind` entity with `id` and its X-ray path, if the current user can read it and it is still in use."
+ [kind id]
+ (let [entity (tools/with-entity {:kind kind :id id}
+ (api/read-check (if (= :table kind)
+ (metabot.db/table id)
+ (metabot.db/card-of-type id kind))))]
+ (when (or (:archived entity) (false? (:active entity)))
+ (unavailable-source! {:kind kind :id id}))
+ {:entity entity
+ :path (str "/auto/dashboard/" (name kind) "/" id)}))
+
(defn- resolve-source
"Resolve the source entity from the provided source parameters.
Returns the entity to analyze and the path for navigation."
- [{:keys [table_id model_id metric_id query_id report_id]} memory]
+ [{:keys [table_id model_id metric_id query_id report_id] :as source} memory]
+ (when-not (= 1 (count (remove nil? (vals source))))
+ (invalid-source! {}))
(cond
- table_id
- (let [table (metabot.db/table table_id)]
- (when-not table
- (throw (ex-info (str "Table not found with ID: " table_id)
- {:agent-error? true :table-id table_id})))
- {:entity table
- :path (str "/auto/dashboard/table/" table_id)})
-
- model_id
- (let [model (metabot.db/card-of-type model_id :model)]
- (when-not model
- (throw (ex-info (str "Model not found with ID: " model_id)
- {:agent-error? true :model-id model_id})))
- {:entity model
- :path (str "/auto/dashboard/model/" model_id)})
-
- metric_id
- (let [metric (metabot.db/card-of-type metric_id :metric)]
- (when-not metric
- (throw (ex-info (str "Metric not found with ID: " metric_id)
- {:agent-error? true :metric-id metric_id})))
- {:entity metric
- :path (str "/auto/dashboard/metric/" metric_id)})
-
- report_id
- (let [report (metabot.db/card-of-type report_id :question)]
- (when-not report
- (throw (ex-info (str "Report/Question not found with ID: " report_id)
- {:agent-error? true :report-id report_id})))
- {:entity report
- :path (str "/auto/dashboard/question/" report_id)})
+ table_id (readable-source :table table_id)
+ model_id (readable-source :model model_id)
+ metric_id (readable-source :metric metric_id)
+ report_id (readable-source :question report_id)
query_id
;; For query_id, we need to resolve from memory
@@ -63,11 +71,7 @@
:available-queries (keys queries-state)})))
;; For queries, we create a transient card-like structure
{:entity {:dataset_query query}
- :path (str "/auto/dashboard/adhoc/" query_id)})
-
- :else
- (throw (ex-info "Invalid source: must specify one of table_id, model_id, metric_id, query_id, or report_id"
- {:agent-error? true}))))
+ :path (str "/auto/dashboard/adhoc/" query_id)})))
;;; Response Messages
@@ -143,15 +147,12 @@
- query_id: Create dashboard for query results from this conversation
- report_id: Create dashboard for an existing report/question"
[{:keys [source]} :- autogen-dashboard-schema]
- (try
- (let [result (create-autogenerated-dashboard
- {:source source
- :memory-atom shared/*memory-atom*})
- structured (:structured-output result)
- data-parts (:data-parts result)]
- (cond-> {:output (:message structured)
- :structured-output structured
- :instructions (:instructions result)}
- (seq data-parts) (assoc :data-parts data-parts)))
- (catch Exception e
- (metabot.tools.u/handle-agent-error e))))
+ (let [result (create-autogenerated-dashboard
+ {:source source
+ :memory-atom shared/*memory-atom*})
+ structured (:structured-output result)
+ data-parts (:data-parts result)]
+ (cond-> {:output (:message structured)
+ :structured-output structured
+ :instructions (:instructions result)}
+ (seq data-parts) (assoc :data-parts data-parts))))
diff --git a/test/metabase/metabot/tools/autogen_dashboard_test.clj b/test/metabase/metabot/tools/autogen_dashboard_test.clj
index b82803a7233..e0763693593 100644
--- a/test/metabase/metabot/tools/autogen_dashboard_test.clj
+++ b/test/metabase/metabot/tools/autogen_dashboard_test.clj
@@ -2,7 +2,9 @@
(:require
[clojure.string :as str]
[clojure.test :refer :all]
+ [metabase.metabot.test-util :as test-util]
[metabase.metabot.tools.autogen-dashboard :as autogen-dashboard]
+ [metabase.metabot.tools.legacy :as tools.legacy]
[metabase.test :as mt]))
;;; Source Resolution Tests (without database)
@@ -12,7 +14,7 @@
(let [memory-atom (atom {:state {:queries {} :charts {}}})]
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
- #"Invalid source"
+ #"Set exactly one of"
(autogen-dashboard/create-autogenerated-dashboard
{:source {}
:memory-atom memory-atom})))))
@@ -28,78 +30,80 @@
;;; Response Structure Tests
(deftest response-structure-test
- (testing "returns instructions for LLM"
- ;; We need to test the structure without actually hitting the database
- ;; Using mt/with-temp for table creation
- (mt/with-temp [:model/Table table {:name (mt/random-name) :db_id (mt/id)}]
- (let [result (autogen-dashboard/create-autogenerated-dashboard
- {:source {:table_id (:id table)}
- :memory-atom (atom {:state {}})})]
- (is (string? (:instructions result))))))
- (testing "returns a dashboard generated_entity data part"
- (mt/with-temp [:model/Table table {:name (mt/random-name) :db_id (mt/id)}]
- (let [result (autogen-dashboard/create-autogenerated-dashboard
- {:source {:table_id (:id table)}
- :memory-atom (atom {:state {}})})
- part (first (:data-parts result))
- entity (:data part)]
- (is (contains? result :data-parts))
- (is (= "generated_entity" (:data-type part)))
- (is (= "dashboard" (:type entity)))
- (is (str/starts-with? (:url entity) "/auto/dashboard/"))
- (is (= (or (:display_name table) (:name table)) (:title entity))))))
- (testing "returns user-friendly message"
- (mt/with-temp [:model/Table table {:name (mt/random-name) :db_id (mt/id)}]
- (let [result (autogen-dashboard/create-autogenerated-dashboard
- {:source {:table_id (:id table)}
- :memory-atom (atom {:state {}})})]
- (is (string? (get-in result [:structured-output :message])))
- (is (str/includes? (get-in result [:structured-output :message]) "dashboard")))))
- (testing "returns path in structured output"
- (mt/with-temp [:model/Table table {:name (mt/random-name) :db_id (mt/id)}]
- (let [result (autogen-dashboard/create-autogenerated-dashboard
- {:source {:table_id (:id table)}
- :memory-atom (atom {:state {}})})]
- (is (string? (get-in result [:structured-output :path])))))))
+ (mt/with-current-user (mt/user->id :rasta)
+ (testing "returns instructions for LLM"
+ ;; We need to test the structure without actually hitting the database
+ ;; Using mt/with-temp for table creation
+ (mt/with-temp [:model/Table table {:name (mt/random-name) :db_id (mt/id)}]
+ (let [result (autogen-dashboard/create-autogenerated-dashboard
+ {:source {:table_id (:id table)}
+ :memory-atom (atom {:state {}})})]
+ (is (string? (:instructions result))))))
+ (testing "returns a dashboard generated_entity data part"
+ (mt/with-temp [:model/Table table {:name (mt/random-name) :db_id (mt/id)}]
+ (let [result (autogen-dashboard/create-autogenerated-dashboard
+ {:source {:table_id (:id table)}
+ :memory-atom (atom {:state {}})})
+ part (first (:data-parts result))
+ entity (:data part)]
+ (is (contains? result :data-parts))
+ (is (= "generated_entity" (:data-type part)))
+ (is (= "dashboard" (:type entity)))
+ (is (str/starts-with? (:url entity) "/auto/dashboard/"))
+ (is (= (or (:display_name table) (:name table)) (:title entity))))))
+ (testing "returns user-friendly message"
+ (mt/with-temp [:model/Table table {:name (mt/random-name) :db_id (mt/id)}]
+ (let [result (autogen-dashboard/create-autogenerated-dashboard
+ {:source {:table_id (:id table)}
+ :memory-atom (atom {:state {}})})]
+ (is (string? (get-in result [:structured-output :message])))
+ (is (str/includes? (get-in result [:structured-output :message]) "dashboard")))))
+ (testing "returns path in structured output"
+ (mt/with-temp [:model/Table table {:name (mt/random-name) :db_id (mt/id)}]
+ (let [result (autogen-dashboard/create-autogenerated-dashboard
+ {:source {:table_id (:id table)}
+ :memory-atom (atom {:state {}})})]
+ (is (string? (get-in result [:structured-output :path]))))))))
;;; Path Generation Tests
(deftest path-generation-test
- (testing "table source generates correct path"
- (mt/with-temp [:model/Table table {:name (mt/random-name) :db_id (mt/id)}]
- (let [result (autogen-dashboard/create-autogenerated-dashboard
- {:source {:table_id (:id table)}
- :memory-atom (atom {:state {}})})]
- (is (= (str "/auto/dashboard/table/" (:id table))
- (get-in result [:structured-output :path]))))))
- (testing "model source generates correct path"
- (mt/with-temp [:model/Card model {:type :model :name "test_model"}]
- (let [result (autogen-dashboard/create-autogenerated-dashboard
- {:source {:model_id (:id model)}
- :memory-atom (atom {:state {}})})]
- (is (= (str "/auto/dashboard/model/" (:id model))
- (get-in result [:structured-output :path]))))))
- (testing "metric source generates correct path"
- (mt/with-temp [:model/Card metric {:type :metric :name "test_metric"}]
- (let [result (autogen-dashboard/create-autogenerated-dashboard
- {:source {:metric_id (:id metric)}
- :memory-atom (atom {:state {}})})]
- (is (= (str "/auto/dashboard/metric/" (:id metric))
- (get-in result [:structured-output :path]))))))
- (testing "report source generates correct path"
- (mt/with-temp [:model/Card report {:type :question :name "test_report"}]
- (let [result (autogen-dashboard/create-autogenerated-dashboard
- {:source {:report_id (:id report)}
- :memory-atom (atom {:state {}})})]
- (is (= (str "/auto/dashboard/question/" (:id report))
- (get-in result [:structured-output :path]))))))
- (testing "query source generates correct path"
- (let [query {:lib/type :mbql/query :database 1 :stages []}
- memory-atom (atom {:state {:queries {"q123" query}}})]
- (is (=? {:structured-output {:path "/auto/dashboard/adhoc/q123"}}
- (autogen-dashboard/create-autogenerated-dashboard
- {:source {:query_id "q123"}
- :memory-atom memory-atom}))))))
+ (mt/with-current-user (mt/user->id :rasta)
+ (testing "table source generates correct path"
+ (mt/with-temp [:model/Table table {:name (mt/random-name) :db_id (mt/id)}]
+ (let [result (autogen-dashboard/create-autogenerated-dashboard
+ {:source {:table_id (:id table)}
+ :memory-atom (atom {:state {}})})]
+ (is (= (str "/auto/dashboard/table/" (:id table))
+ (get-in result [:structured-output :path]))))))
+ (testing "model source generates correct path"
+ (mt/with-temp [:model/Card model {:type :model :name "test_model"}]
+ (let [result (autogen-dashboard/create-autogenerated-dashboard
+ {:source {:model_id (:id model)}
+ :memory-atom (atom {:state {}})})]
+ (is (= (str "/auto/dashboard/model/" (:id model))
+ (get-in result [:structured-output :path]))))))
+ (testing "metric source generates correct path"
+ (mt/with-temp [:model/Card metric {:type :metric :name "test_metric"}]
+ (let [result (autogen-dashboard/create-autogenerated-dashboard
+ {:source {:metric_id (:id metric)}
+ :memory-atom (atom {:state {}})})]
+ (is (= (str "/auto/dashboard/metric/" (:id metric))
+ (get-in result [:structured-output :path]))))))
+ (testing "report source generates correct path"
+ (mt/with-temp [:model/Card report {:type :question :name "test_report"}]
+ (let [result (autogen-dashboard/create-autogenerated-dashboard
+ {:source {:report_id (:id report)}
+ :memory-atom (atom {:state {}})})]
+ (is (= (str "/auto/dashboard/question/" (:id report))
+ (get-in result [:structured-output :path]))))))
+ (testing "query source generates correct path"
+ (let [query {:lib/type :mbql/query :database 1 :stages []}
+ memory-atom (atom {:state {:queries {"q123" query}}})]
+ (is (=? {:structured-output {:path "/auto/dashboard/adhoc/q123"}}
+ (autogen-dashboard/create-autogenerated-dashboard
+ {:source {:query_id "q123"}
+ :memory-atom memory-atom})))))))
;;; Entity Not Found Tests
@@ -107,37 +111,99 @@
(testing "fails for nonexistent table"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
- #"Table not found"
+ #"Table 999999 was not found"
(autogen-dashboard/create-autogenerated-dashboard
{:source {:table_id 999999}
:memory-atom (atom {:state {}})}))))
(testing "fails for nonexistent model"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
- #"Model not found"
+ #"Model 999999 was not found"
(autogen-dashboard/create-autogenerated-dashboard
{:source {:model_id 999999}
:memory-atom (atom {:state {}})}))))
(testing "fails for nonexistent metric"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
- #"Metric not found"
+ #"Metric 999999 was not found"
(autogen-dashboard/create-autogenerated-dashboard
{:source {:metric_id 999999}
:memory-atom (atom {:state {}})}))))
(testing "fails for nonexistent report"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
- #"Report/Question not found"
+ #"Question 999999 was not found"
(autogen-dashboard/create-autogenerated-dashboard
{:source {:report_id 999999}
:memory-atom (atom {:state {}})})))))
+(defn- failure
+ "How a `create_autogenerated_dashboard` call with `source` fails, as the agent sees it."
+ [source]
+ (test-util/tool-failure (tools.legacy/adapt #'autogen-dashboard/create-autogenerated-dashboard-tool)
+ {:source source}))
+
(deftest create-autogenerated-dashboard-tool-errors-test
- (testing "a query missing from memory goes back to the agent as output"
- (is (= {:output "Query not found in memory with ID: q-9. Available queries: []"}
- (autogen-dashboard/create-autogenerated-dashboard-tool {:source {:query_id "q-9"}}))))
+ (testing "a query missing from memory goes back to the agent as a failed call"
+ (is (= {:class :recoverable
+ :code :metabase.metabot.tools.recoverable.legacy/agent-error
+ :text "Query not found in memory with ID: q-9. Available queries: []"}
+ (failure {:query_id "q-9"}))))
(testing "an unexpected error propagates to the agent loop"
(mt/with-dynamic-fn-redefs [autogen-dashboard/create-autogenerated-dashboard (fn [_] (throw (ex-info "boom" {})))]
(is (thrown-with-msg? clojure.lang.ExceptionInfo #"boom"
(autogen-dashboard/create-autogenerated-dashboard-tool {:source {:table_id 1}}))))))
+
+(deftest unreadable-source-test
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-temp [:model/Collection {coll-id :id} {}
+ :model/Card {model-id :id} {:name "Secret model" :type :model :collection_id coll-id}
+ :model/Card {metric-id :id} {:name "Secret metric" :type :metric :collection_id coll-id}
+ :model/Card {question-id :id} {:name "Secret question" :type :question :collection_id coll-id}
+ :model/Table {table-id :id} {:name "Secret table" :db_id (mt/id)}]
+ (mt/with-no-data-perms-for-all-users!
+ (doseq [[source-key id noun] [[:model_id model-id "Model"]
+ [:metric_id metric-id "Metric"]
+ [:report_id question-id "Question"]
+ [:table_id table-id "Table"]]]
+ (testing noun
+ (testing "fails for a user who cannot read it, the same way a missing one does"
+ (is (=? {:class :recoverable
+ :code :metabase.metabot.tools.recoverable.common/not-found
+ :text (re-pattern (str "(?s)" noun " " id " was not found\\..*"))}
+ (mt/with-current-user (mt/user->id :rasta)
+ (failure {source-key id})))))
+ (testing "still gets an admin a dashboard"
+ (is (=? {:data-parts [{:data {:title #"Secret .*"}}]}
+ (mt/with-current-user (mt/user->id :crowberto)
+ (autogen-dashboard/create-autogenerated-dashboard-tool {:source {source-key id}})))))))))))
+
+(deftest unavailable-source-test
+ (mt/with-temp [:model/Card {model-id :id} {:type :model :archived true}
+ :model/Table {table-id :id} {:name (mt/random-name) :db_id (mt/id) :active false}]
+ (mt/with-current-user (mt/user->id :rasta)
+ (testing "an archived source the user can read is in the trash"
+ (is (= {:class :recoverable
+ :code ::autogen-dashboard/unavailable-source
+ :text (str "Model " model-id " is in the trash.\n"
+ "Call `search` to find another source and use an id from the results.")}
+ (failure {:model_id model-id}))))
+ (testing "an inactive table is gone from its database"
+ (is (= {:class :recoverable
+ :code ::autogen-dashboard/unavailable-source
+ :text (str "Table " table-id " is no longer in its database.\n"
+ "Call `search` to find another source and use an id from the results.")}
+ (failure {:table_id table-id})))))))
+
+(deftest one-source-test
+ (testing "a call that sets two sources is rejected before either is read"
+ (is (= {:class :recoverable
+ :code ::autogen-dashboard/invalid-source
+ :text "Set exactly one of `table_id`, `model_id`, `metric_id`, `query_id` or `report_id` in `source`."}
+ (failure {:table_id 1 :model_id 2}))))
+ (testing "a null for an unused source is not a second source"
+ (mt/with-temp [:model/Card {model-id :id} {:type :model}]
+ (mt/with-current-user (mt/user->id :rasta)
+ (is (=? {:structured-output {:path (str "/auto/dashboard/model/" model-id)}}
+ (autogen-dashboard/create-autogenerated-dashboard-tool
+ {:source {:table_id nil :model_id model-id}})))))))