Skip to content

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

Let Metabot run any saved question it can read by its ID (#84582)

Chris Truterc5d14c57671e
diff --git a/resources/metabot/prompts/shared/prompt_snippets/results_saved_question_notebook.selmer b/resources/metabot/prompts/shared/prompt_snippets/results_saved_question_notebook.selmer
index 8dc4eeff9b8..9b365babea4 100644
--- a/resources/metabot/prompts/shared/prompt_snippets/results_saved_question_notebook.selmer
+++ b/resources/metabot/prompts/shared/prompt_snippets/results_saved_question_notebook.selmer
@@ -1 +1 @@
-To read a saved question or model the user is viewing, run it with `run_query`, with its id as the `query_id`. To read any other saved question or model, build a notebook query with it as the `source-card` and run that.
+To read a saved question or model, run it with `run_query`, with its id as the `query_id`.
diff --git a/src/metabase/metabot/tools/run_query.clj b/src/metabase/metabot/tools/run_query.clj
index e9b73fbcdfe..bbbb7e3c896 100644
--- a/src/metabase/metabot/tools/run_query.clj
+++ b/src/metabase/metabot/tools/run_query.clj
@@ -126,9 +126,9 @@
:else no-values-hint))
(defn- metric-refusal
- "The refusal for `query-id`, the id of `metric`, a metric the user is viewing and can read.
+ "The refusal for `query-id`, the id of `metric`, a metric the user can read.
It says what to aggregate the metric over: the model or question it is built on, or else its base table.
- Where the user can't read the card it is built on, it offers no way to its values."
+ Where the user can't read the card it is built on, or query the table, it offers no way to its values."
[query-id metric]
;; A metric is an aggregation, not a source, so no query with it as the source would run.
(let [source-card (some-> (:source_card_id metric) metabot.db/card)
@@ -137,36 +137,32 @@
(format "the %s it is built on as the `source-card`, whose portable entity id is %s,"
(name (:type source-card)) (:entity_id source-card))
- (and (nil? (:source_card_id metric)) (:table_id metric))
+ ;; Reading the metric is collection-based and does not imply querying its table.
+ (and (nil? (:source_card_id metric))
+ (some->> (:table_id metric) (mi/can-query? :model/Table)))
"the metric's base table as its source")]
- (refusal (str "Query id " query-id " is a metric the user is viewing, and a metric does not run by its id. "
+ (refusal (str "Query id " query-id " is a metric, and a metric does not run by its id. "
(if source
(str "To get its values, build a notebook query with construct_notebook_query that has " source
" and the metric in `aggregation`, then run that query with run_query.")
no-values-hint)))))
-(defn- viewed-saved-question
- "`{:query :saved-card}` for the saved question or model the user is viewing whose id is `query-id`, or nil:
- a notebook query over the saved version, and the question itself.
+(defn- saved-question
+ "`{:query :saved-card}` for the saved question or model whose id is `query-id`, or nil when the user
+ can't read it: a notebook query over the saved version, and the question itself.
Throws a refusal for a metric the user can read.
- Only run_query reads a viewed question this way: the chart, link and save tools still take a query the
+ Only run_query reads a saved question this way: the chart, link and save tools still take a query the
conversation holds."
[query-id]
;; The conversation holds no query under a saved question's id.
- ;; The client sends the query on screen with the item, but that is not what runs.
- ;; The saved question runs instead, as a source-card query.
+ ;; The saved question runs as a source-card query, the same query the model could build itself.
;; That keeps its own permissions, and the checks on SQL that Metabot saved.
- (when-let [card-id (some (fn [{:keys [id type]}]
- (when (and (= query-id (str id))
- (contains? #{"question" "model" "metric"} (some-> type name)))
- (let [parsed (parse-long (str id))]
- (when (pos-int? parsed)
- parsed))))
- (get-in (shared/current-memory) [:context :user_is_viewing]))]
+ (when-let [card-id (let [parsed (parse-long query-id)]
+ (when (and (pos-int? parsed) (= query-id (str parsed)))
+ parsed))]
;; A question the user can't read is treated like one that doesn't exist, so its id tells them nothing.
(let [card (metabot.db/card card-id)]
(when (some-> card mi/can-read?)
- ;; The client sets the item's type, so the card decides whether this is a metric.
(when (= :metric (:type card))
(throw (metric-refusal query-id card)))
{:saved-card card
@@ -177,19 +173,17 @@
(defn- stored-query
"`{:query :saved-card}` for the query `query-id` names: one the conversation holds, or with `allow-non-sql?` a
- saved question the user is viewing, which is then `:saved-card`."
+ saved question the user can read, which is then `:saved-card`."
[query-id allow-non-sql?]
(let [queries (shared/current-queries-state)]
(or (when-let [query (get queries query-id)]
{:query query})
(when allow-non-sql?
- (viewed-saved-question query-id))
+ (saved-question query-id))
(throw (refusal (str "No query with id " query-id ". Known query ids: [" (str/join ", " (keys queries)) "]. "
(cond
allow-non-sql?
- (str "Only a saved question or model the user is viewing runs by its own id: to run "
- "another, build a notebook query with it as the source-card using "
- "construct_notebook_query, then run that query.")
+ "Only a query you built, or a saved question or model the user can read, runs by its id."
(scope/sql-execution-allowed?)
(str "A saved question or model has no query id: to run one, write SQL that reads it "
@@ -654,8 +648,7 @@
"Run a query you already have and read its first rows (default 20, max 200).
Use it when the answer needs actual values: a number, the top item, whether a filter matches anything.
`query_id` is the id of a query you built, or of a query the user is viewing.
- For a saved question or model the user is viewing, `query_id` is its own id. Any other has no query id: build a
- notebook query with it as the source-card and run that.
+ For a saved question or model, `query_id` is its own id.
A SQL query, whether built with create_sql_query or viewed by the user, runs only where SQL execution is
on, and only when it is a single read-only SELECT statement; otherwise it is refused, and you get values by
building the question with construct_notebook_query.
diff --git a/test/metabase/metabot/agent/prompt_gating_test.clj b/test/metabase/metabot/agent/prompt_gating_test.clj
index ee5a19522be..2be37ffe194 100644
--- a/test/metabase/metabot/agent/prompt_gating_test.clj
+++ b/test/metabase/metabot/agent/prompt_gating_test.clj
@@ -322,7 +322,7 @@
(testing "with run_query the model is told how to read results"
(let [rendered (render-internal-template all-yes-perms ["construct_notebook_query" "run_query"])]
(is (re-find #"# You can see results only by running a query" rendered))
- (is (re-find #"To read a saved question or model.*`source-card`" rendered))
+ (is (re-find #"To read a saved question or model, run it with `run_query`" rendered))
(is (re-find #"pass `show_chart: false` to `construct_notebook_query`" rendered))
(is (not (re-find #"you cannot see query results" rendered))))))
diff --git a/test/metabase/metabot/tools/run_query_test.clj b/test/metabase/metabot/tools/run_query_test.clj
index 1bd032cafdd..5a2d5a31007 100644
--- a/test/metabase/metabot/tools/run_query_test.clj
+++ b/test/metabase/metabot/tools/run_query_test.clj
@@ -129,9 +129,8 @@
(is (= {:output "Query execution is turned off for Metabot."}
(call-tool! {:state {:queries {"q1" (venues-by-id)}}} {:query_id "q1"})))))
(testing "an unknown id lists the ids the model can use"
- (is (= {:output (str "No query with id nope. Known query ids: [q1]. Only a saved question or model the user is "
- "viewing runs by its own id: to run another, build a notebook query with it as the "
- "source-card using construct_notebook_query, then run that query.")}
+ (is (= {:output (str "No query with id nope. Known query ids: [q1]. Only a query you built, or a saved question "
+ "or model the user can read, runs by its id.")}
(run-tool! {"q1" (venues-by-id)} {:query_id "nope"}))))
(testing "a SQL query is refused with a pointer to construct_notebook_query while SQL execution is off"
(mt/with-temporary-setting-values [metabot-sql-execution-enabled? false]
@@ -422,9 +421,13 @@
item-type card-id item-type card-id)))))))
(testing "a query the conversation holds says nothing about a saved version"
(is (not (str/includes? (:output (run-tool! {"q1" (venues-count)} {:query_id "q1"})) "saved version"))))
- (testing "a saved question the user is not viewing does not run by its id"
- (is (=? {:output #"No query with id \d+\. Known query ids: \[\]\. .*"}
- (run-viewing! [] (str notebook-card)))))
+ (testing "a saved question the user is not viewing runs by its id too"
+ (is (= ["| Count |" "| --- |" "| 100 |"]
+ (data-lines (:output (run-viewing! [] (str notebook-card)))))))
+ (testing "an id with anything but digits names no saved question"
+ (doseq [id [(str "+" notebook-card) (str notebook-card " ") (str "0" notebook-card)]]
+ (is (=? {:output #"No query with id .*\. Known query ids: \[\]\. .*"}
+ (run-viewing! [] id)))))
(testing "a viewed question the user can't read is refused like an id that names nothing"
(mt/with-non-admin-groups-no-root-collection-perms
(mt/with-temp [:model/Collection {hidden :id} {}
@@ -726,11 +729,16 @@
(run-viewing! [{:type "question", :id archived-card}] (str archived-card)))))
(testing "a viewed metric is refused with how to query it, whatever type the client gives the item"
(doseq [item-type ["metric" "question"]]
- (is (= {:output (str "Query id " metric-card " is a metric the user is viewing, and a metric does not run by its "
+ (is (= {:output (str "Query id " metric-card " is a metric, and a metric does not run by its "
"id. To get its values, build a notebook query with construct_notebook_query that has "
"the metric's base table as its source and the metric in `aggregation`, then run that "
"query with run_query.")}
(run-viewing! [{:type item-type, :id metric-card}] (str metric-card))))))
+ (testing "a metric over a table the user can't query offers no way to its values"
+ (mt/with-no-data-perms-for-all-users!
+ (is (= {:output (str "Query id " metric-card " is a metric, and a metric does not run by its id. "
+ "Tell the user you can't read its results.")}
+ (run-viewing! [] (str metric-card))))))
(testing "a viewed metric built on a model names that model as the source, when the user can read it"
(mt/with-non-admin-groups-no-root-collection-perms
(mt/with-temp [:model/Collection {open :id} {}
@@ -751,13 +759,13 @@
[:query :aggregation] [[:count]])
:type :metric}]
(perms/grant-collection-read-permissions! (perms-group/all-users) open)
- (is (= {:output (str "Query id " on-open " is a metric the user is viewing, and a metric does not run by its id. "
+ (is (= {:output (str "Query id " on-open " is a metric, and a metric does not run by its id. "
"To get its values, build a notebook query with construct_notebook_query that has the "
"model it is built on as the `source-card`, whose portable entity id is " entity-id
", and the metric in `aggregation`, then run that query with run_query.")}
(run-viewing! [{:type "metric", :id on-open}] (str on-open))))
(testing "and offers no way to its values when they can't"
- (is (= {:output (str "Query id " on-hidden " is a metric the user is viewing, and a metric does not run by "
+ (is (= {:output (str "Query id " on-hidden " is a metric, and a metric does not run by "
"its id. Tell the user you can't read its results.")}
(run-viewing! [{:type "metric", :id on-hidden}] (str on-hidden))))))))
(testing "a viewed metric the user can't read is refused like an id that names nothing"