Skip to content

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

Fix Metabot's recent item links and missing-field errors (#84921)

Andrei Simionescu580844ef652d
diff --git a/src/metabase/metabot/tools/core.clj b/src/metabase/metabot/tools/core.clj
index 626544b83f9..f50d8cb820e 100644
--- a/src/metabase/metabot/tools/core.clj
+++ b/src/metabase/metabot/tools/core.clj
@@ -441,7 +441,8 @@
(try
(thunk)
(catch Throwable e
- (if (contains? refusal-status-codes (:status-code (ex-data e)))
+ (if (and (not (contains? (ex-data e) tools.error/error-key))
+ (contains? refusal-status-codes (:status-code (ex-data e))))
(tools.error/throw-recoverable! ::recoverable.common/not-found
(cond-> {} kind (assoc :kind kind) (some? id) (assoc :id id))
{:cause e})
@@ -453,10 +454,10 @@
(with-entity {:kind :timeline :id timeline-id}
(timeline/include-events-singular (timeline/get-timeline timeline-id)))
- `entity` is `{:kind <keyword> :id <id>}`. A 403 and a 404 — the two things `api/read-check` raises
- — both become the same recoverable error; see `recoverable.common/not-found!` for why they are not
- distinguished. Every other exception passes through, so a genuine failure inside `body` stays
- unrecoverable instead of being reported to the model as a missing entity."
+ `entity` is `{:kind <keyword> :id <id>}`. Both 403 and 404 errors from `api/read-check` become
+ the same recoverable error; see `recoverable.common/not-found!` for why they are not distinguished.
+ Declared tool errors and other exceptions pass through unchanged, so other failures remain
+ unrecoverable."
{:style/indent [:defn]}
[entity & body]
`(do-with-entity ~entity (fn [] ~@body)))
diff --git a/src/metabase/metabot/tools/resources.clj b/src/metabase/metabot/tools/resources.clj
index 1d7274802af..4289a43990c 100644
--- a/src/metabase/metabot/tools/resources.clj
+++ b/src/metabase/metabot/tools/resources.clj
@@ -493,9 +493,9 @@
[])
items (mapv (fn [{:keys [id name model timestamp]}]
(let [type (case model
- "card" "question"
- "dataset" "model"
- (or model "item"))]
+ :card "question"
+ :dataset "model"
+ (clojure.core/name (or model :item)))]
{:type type
:id id
:name name
@@ -636,13 +636,24 @@
(defn- fetch-table-fields [id-str]
(table-details :table (parse-long id-str) true))
+(defn- field-values
+ [{:keys [field-id] :as args}]
+ (let [entity {:kind :field :id field-id}]
+ (tools/with-entity entity
+ (let [result (field-stats/field-values args)]
+ (when-not (:structured-output result)
+ (recoverable.common/not-found! entity))
+ result))))
+
(defn- fetch-table-field [id-str field-id]
- (let [table-id (parse-long id-str)]
- (check-table-resource-database table-id)
- (field-stats/field-values {:entity-type "table"
- :entity-id table-id
- :field-id field-id
- :limit 30})))
+ (let [table-id (parse-long id-str)
+ table (api/read-check :model/Table table-id)]
+ (api/check-404 (:active table))
+ (check-resource-database (:db_id table))
+ (field-values {:entity-type "table"
+ :entity-id table-id
+ :field-id field-id
+ :limit 30})))
(defn- fetch-table-derived [id-str query-params]
(let [table-id (parse-long id-str)
@@ -676,10 +687,10 @@
(defn- fetch-card-field [type-str id-str field-id]
(let [card-id (parse-long id-str)]
(check-card-resource-database card-id)
- (field-stats/field-values {:entity-type type-str
- :entity-id card-id
- :field-id field-id
- :limit 30})))
+ (field-values {:entity-type type-str
+ :entity-id card-id
+ :field-id field-id
+ :limit 30})))
(defn- fetch-card-sources [id-str query-params]
(let [card (api/read-check :model/Card (parse-long id-str))]
@@ -716,10 +727,10 @@
(defn- fetch-metric-dimension [id-str dim-id]
(let [metric-id (parse-long id-str)]
(check-metric-resource metric-id "fields" dim-id)
- (field-stats/field-values {:entity-type "metric"
- :entity-id metric-id
- :field-id dim-id
- :limit 30})))
+ (field-values {:entity-type "metric"
+ :entity-id metric-id
+ :field-id dim-id
+ :limit 30})))
;; ----- Measure / Segment -----
@@ -1144,10 +1155,7 @@
"<instructions>\n" instruction-text "\n</instructions>"))
(defn- format-content
- "One read's structured output as the LLM-ready string, by `:result-type`.
-
- Every handler returns a `:structured-output`; a miss or a refusal is thrown rather than returned,
- so there is no error branch here and no result shape that renders as an empty `<resource>`."
+ "Render a successful read's structured output as text by `:result-type`."
[structured]
(case (:result-type structured)
;; NOTE: keep in sync with agent/tools/metadata.clj/format-field-metadata-output
@@ -1161,13 +1169,9 @@
(llm-shape/entity->xml structured)))
(defn- resource-element
- "One `<resource>` element, for either outcome.
-
- `uri` goes through `pr-str` because it reaches the model inside an attribute and is the agent's
- own text — an unescaped `\"` would close the attribute early and the rest of the URI would read as
- markup."
+ "Wrap a resource body in XML, escaping its URI attribute."
[uri body]
- (str "<resource uri=" (pr-str uri) ">\n" body "\n</resource>"))
+ (str "<resource uri=\"" (llm-shape/escape-xml uri) "\">\n" body "\n</resource>"))
(def ^:private uri-arg
"The one thing a read takes. Both declarations below build their `:args` from this, so the single
@@ -1239,12 +1243,7 @@
(handle [_ {:keys [uri]} _ctx]
(let [structured (:structured-output (dispatch uri))]
(when-not structured
- ;; Two handlers still report a miss by returning `{:output … :status-code 404}` rather than
- ;; throwing — `entity-details/get-measure-details` and `get-segment-details`, whose other
- ;; callers depend on that shape. Their text is dropped rather than forwarded: it was not
- ;; written for a model, and `not-found!` already names the entity and says what to do next.
- ;; This also closes the case where a result carrying only `:output` rendered as an empty
- ;; `<resource>`, because the formatter read a `:formatted` key nothing ever set.
+ ;; Table, card, metric, measure and segment helpers return failures as text.
(recoverable.common/not-found! (uri-entity (:segments (parse-uri uri)))))
{:output (format-content structured)
:structured-output structured}))
@@ -1269,7 +1268,7 @@
;; so there is no shared query to issue up front — what they do share is the permission cache,
;; which `tools/call` holds across the whole call. `pmap` over the fetches is worth measuring,
;; but it needs the request bindings carried onto the threads.
- (mapv (fn [uri] {:uri uri}) uris))
+ (mapv (fn [uri] {:uri (str/replace uri "&amp;" "&")}) uris))
(compose [_ entries _ctx]
;; Every entry is wrapped here rather than in `handle`, which is what puts a failed URI inside
diff --git a/src/metabase/metabot/tools/shared/llm_shape.clj b/src/metabase/metabot/tools/shared/llm_shape.clj
index 735f8daf150..578ae9a0ef0 100644
--- a/src/metabase/metabot/tools/shared/llm_shape.clj
+++ b/src/metabase/metabot/tools/shared/llm_shape.clj
@@ -1084,7 +1084,7 @@
truncated (< page pages)
note (when truncated
(str "<truncation-note>Page " page " of " pages " (" showing " of " total " items). "
- "Fetch " next-page-uri " for the next page — copy it exactly, do not "
+ "Fetch " (escape-xml next-page-uri) " for the next page. Copy it exactly; do not "
"modify its query string.</truncation-note>"))]
(str "<list type=\"" type-attr "\" total=\"" total
"\" page=\"" (or page 1)
diff --git a/test/metabase/metabot/tools/core_test.clj b/test/metabase/metabot/tools/core_test.clj
index 250e9aa25f6..5cd4547889f 100644
--- a/test/metabase/metabot/tools/core_test.clj
+++ b/test/metabase/metabot/tools/core_test.clj
@@ -301,6 +301,18 @@
(tools/with-entity {:kind :timeline :id 5}
(throw (ex-info "nope" {:status-code status})))))))))))
+(deftest ^:parallel with-entity-keeps-inner-entity-test
+ (doseq [status [403 404]]
+ (is (=? {:class :recoverable
+ :code :metabase.metabot.tools.recoverable.common/not-found
+ :message "Field 2 was not found. It may not exist, or you may not have access to it."
+ :data {:kind :field :id 2}}
+ (tools.error/classify
+ (is (thrown? clojure.lang.ExceptionInfo
+ (tools/with-entity {:kind :table :id 1}
+ (tools/with-entity {:kind :field :id 2}
+ (throw (ex-info "Field lookup refused" {:status-code status})))))))))))
+
(deftest ^:parallel with-entity-passes-everything-else-through-test
(testing "a real failure inside the body stays unrecoverable instead of being reported as a miss"
(doseq [data [{:status-code 500} {} {:status-code 400}]]
diff --git a/test/metabase/metabot/tools/resources_test.clj b/test/metabase/metabot/tools/resources_test.clj
index 702ef599cb5..7d61f9c0f20 100644
--- a/test/metabase/metabot/tools/resources_test.clj
+++ b/test/metabase/metabot/tools/resources_test.clj
@@ -6,12 +6,15 @@
[clojure.string :as str]
[clojure.test :refer :all]
[medley.core :as m]
+ [metabase.activity-feed.core :as activity-feed]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.metabot.test-util :as test-util]
[metabase.metabot.tools :as metabot.tools]
[metabase.metabot.tools.core :as tools]
+ [metabase.metabot.tools.entity-details :as entity-details]
+ [metabase.metabot.tools.field-stats :as field-stats]
[metabase.metabot.tools.resources :as read-resource]
[metabase.metabot.tools.shared :as tools.shared]
[metabase.metabot.tools.shared.content-store :as shared.content-store]
@@ -1193,6 +1196,26 @@
(with-redefs [mi/can-read? (constantly false)]
(is (error? (read-uris {:uris [(str "metabase://segment/" segment-id)]}))))))))))
+(deftest read-measure-and-segment-on-inactive-table-test
+ (mt/test-drivers #{:h2}
+ (mt/with-current-user (mt/user->id :crowberto)
+ (let [orders (mt/id :orders)
+ total (mt/id :orders :total)]
+ (mt/with-temp [:model/Measure {measure-id :id}
+ {:name "Order Revenue"
+ :table_id orders
+ :creator_id (mt/user->id :crowberto)
+ :definition (measure-definition orders total)}
+ :model/Segment {segment-id :id}
+ {:name "Big Orders"
+ :table_id orders
+ :definition (segment-definition orders total 100)}]
+ (mt/with-temp-vals-in-db :model/Table orders {:active false}
+ (is (=? {:resources [{:content {:structured-output {:type :measure :name "Order Revenue"}}}
+ {:content {:structured-output {:type :segment :name "Big Orders"}}}]}
+ (read-uris {:uris [(str "metabase://measure/" measure-id)
+ (str "metabase://segment/" segment-id)]})))))))))
+
(deftest read-dashboard-items-test
(mt/with-current-user (mt/user->id :crowberto)
(mt/with-temp [:model/Dashboard {dash-id :id} {}
@@ -1326,6 +1349,63 @@
(let [{:keys [output]} (read-uris {:uris ["metabase://user/recent-items"]})]
(is (str/includes? output "<list type=\"recent-items\""))))))
+(deftest read-user-recents-links-test
+ (let [mp (mt/metadata-provider)
+ query (lib/query mp (lib.metadata/table mp (mt/id :products)))]
+ (doseq [[entity-model attributes type]
+ [[:model/Card {:type :question :dataset_query query} "question"]
+ [:model/Card {:type :model :dataset_query query} "model"]
+ [:model/Card {:type :metric :dataset_query (lib/aggregate query (lib/count))} "metric"]
+ [:model/Table {:db_id (mt/id)} "table"]
+ [:model/Dashboard {} "dashboard"]
+ [:model/Collection {} "collection"]
+ [:model/Document {:document {:type "doc" :content []}} "document"]]]
+ (testing type
+ (mt/with-temp [:model/User {user-id :id} {:is_superuser true}
+ entity-model {id :id} (assoc attributes :name "Recent item")
+ :model/RecentViews _ {:user_id user-id
+ :model (if (= entity-model :model/Card) "card" type)
+ :model_id id
+ :context "view"}]
+ (mt/with-current-user user-id
+ (let [{:keys [output resources]} (read-uris {:uris ["metabase://user/recent-items"]})
+ items (get-in resources [0 :content :structured-output :items])
+ uri (str "metabase://" type "/" id)]
+ (is (=? [{:type type :id id :name "Recent item" :timestamp string? :uri uri}] items))
+ (is (str/includes? output (str "type=\"" type "\"")))
+ (is (str/includes? output (str "uri=\"" uri "\"")))
+ (is (=? {:resources [{:content {:structured-output map?}}]}
+ (read-uris {:uris (mapv :uri items)}))))))))))
+
+(deftest ^:parallel read-user-recents-pagination-test
+ (let [recents (mapv (fn [id]
+ {:id id
+ :name (str "Recent item " id)
+ :model (if (odd? id) :card :dashboard)
+ :timestamp "2026-10-20T12:00:00Z"})
+ (range 1 31))]
+ (mt/with-dynamic-fn-redefs [activity-feed/get-recents (constantly {:recents recents})]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (let [first-result (read-uris {:uris ["metabase://user/recent-items"]})
+ first-page (get-in first-result [:resources 0 :content :structured-output])
+ next-uri (:next-page-uri first-page)]
+ (testing "the first page gives the model 25 items and a link to the rest"
+ (is (=? {:page 1 :pages 2 :total 30
+ :next-page-uri "metabase://user/recent-items?page=2"}
+ first-page))
+ (is (= (range 1 26) (map :id (:items first-page))))
+ (is (= 25 (count (re-seq #"<item " (:output first-result)))))
+ (is (str/includes? (:output first-result) "truncated=\"true\""))
+ (is (str/includes? (:output first-result) next-uri)))
+ (testing "following that link returns the remaining five items"
+ (let [{:keys [output resources]} (read-uris {:uris [next-uri]})
+ second-page (get-in resources [0 :content :structured-output])]
+ (is (=? {:page 2 :pages 2 :total 30} second-page))
+ (is (= (range 26 31) (map :id (:items second-page))))
+ (is (= 5 (count (re-seq #"<item " output))))
+ (is (str/includes? output "truncated=\"false\""))
+ (is (nil? (:next-page-uri second-page))))))))))
+
(deftest read-list-shape-test
(testing "list responses carry total/page/pages/showing/truncated attrs in the rendered XML"
(mt/with-current-user (mt/user->id :crowberto)
@@ -1359,10 +1439,100 @@
(deftest ^:parallel a-uri-attribute-is-escaped-test
(mt/with-current-user (mt/user->id :crowberto)
- (testing "the URI is the agent's own text and reaches the model inside an attribute, so a quote
- in it must not be able to close that attribute early"
- (let [{:keys [output]} (read-uris {:uris ["metabase://table/\"><script>"]})]
- (is (str/includes? output "<resource uri=\"metabase://table/\\\"><script>\">"))))))
+ (let [{:keys [output]} (read-uris {:uris ["metabase://table/<&\""]})]
+ (is (str/includes? output "<resource uri=\"metabase://table/&lt;&amp;&quot;\">")))))
+
+(deftest read-missing-fields-test
+ (let [mp (mt/metadata-provider)
+ query (lib/query mp (lib.metadata/table mp (mt/id :products)))
+ metadata (-> query qp/process-query :data :results_metadata :columns)]
+ (mt/with-temp [:model/Card {question-id :id} {:type :question :dataset_query query :result_metadata metadata}
+ :model/Card {model-id :id} {:type :model :dataset_query query :result_metadata metadata}
+ :model/Card {metric-id :id} {:type :metric :dataset_query (lib/aggregate query (lib/count))}]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (doseq [[kind id child] [["table" (mt/id :products) "fields"]
+ ["question" question-id "fields"]
+ ["model" model-id "fields"]
+ ["metric" metric-id "dimensions"]]
+ field-id ["999999" "CATEGORY"]]
+ (let [uri (str "metabase://" kind "/" id "/" child "/" field-id)]
+ (testing uri
+ (let [{:keys [class text]} (test-util/tool-failure read-resource/read-resource-tool {:uris [uri]})]
+ (is (= :recoverable class))
+ (is (str/includes? text (str "**Error:** Field " field-id
+ " was not found. It may not exist, or you may not have access to it.")))
+ (is (str/includes? text "Call `search`"))))))))))
+
+(deftest read-missing-field-parent-test
+ (mt/with-current-user (mt/user->id :crowberto)
+ (doseq [[kind child] [["table" "fields"] ["question" "fields"]
+ ["model" "fields"] ["metric" "dimensions"]]]
+ (let [uri (str "metabase://" kind "/999999/" child "/123")
+ {:keys [class text]} (test-util/tool-failure read-resource/read-resource-tool {:uris [uri]})]
+ (is (= :recoverable class))
+ (is (str/includes? text (str "**Error:** " (str/capitalize kind) " 999999 was not found.")))))))
+
+(deftest read-unreadable-field-parent-test
+ (mt/with-non-admin-groups-no-root-collection-perms
+ (mt/with-temp [:model/Collection {collection-id :id} {}
+ :model/Card {card-id :id} {:collection_id collection-id}]
+ (mt/with-current-user (mt/user->id :rasta)
+ (doseq [[kind child] [["question" "fields"] ["model" "fields"] ["metric" "dimensions"]]]
+ (let [uri (str "metabase://" kind "/" card-id "/" child "/123")
+ {:keys [class text]} (test-util/tool-failure read-resource/read-resource-tool {:uris [uri]})]
+ (is (= :recoverable class))
+ (is (str/includes? text (str "**Error:** " (str/capitalize kind) " " card-id " was not found."))))))))
+ (mt/with-no-data-perms-for-all-users!
+ (mt/with-current-user (mt/user->id :rasta)
+ (let [uri (str "metabase://table/" (mt/id :products) "/fields/123")
+ {:keys [class text]} (test-util/tool-failure read-resource/read-resource-tool {:uris [uri]})]
+ (is (= :recoverable class))
+ (is (str/includes? text (str "**Error:** Table " (mt/id :products) " was not found.")))))))
+
+(deftest read-inactive-field-parent-test
+ (mt/with-temp [:model/Table {table-id :id} {:db_id (mt/id) :active false}]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (let [uri (str "metabase://table/" table-id "/fields/123")
+ {:keys [class text]} (test-util/tool-failure read-resource/read-resource-tool {:uris [uri]})]
+ (is (= :recoverable class))
+ (is (str/includes? text (str "**Error:** Table " table-id " was not found.")))))))
+
+(deftest read-field-refusal-test
+ (mt/with-current-user (mt/user->id :crowberto)
+ (doseq [status [403 404]]
+ (mt/with-dynamic-fn-redefs [field-stats/field-values
+ (fn [_] (throw (ex-info "Field lookup refused" {:status-code status})))]
+ (let [uri (str "metabase://table/" (mt/id :products) "/fields/999999")
+ {:keys [class text]} (test-util/tool-failure read-resource/read-resource-tool {:uris [uri]})]
+ (is (= :recoverable class))
+ (is (str/includes? text "**Error:** Field 999999 was not found.")))))))
+
+(deftest read-legacy-detail-failures-test
+ (mt/with-temp [:model/Card {question-id :id} {:type :question}
+ :model/Card {model-id :id} {:type :model}
+ :model/Card {metric-id :id} {:type :metric}]
+ (mt/with-current-user (mt/user->id :crowberto)
+ (let [failure (constantly {:output "Legacy detail not found" :status-code 404})]
+ (mt/with-dynamic-fn-redefs [entity-details/get-table-details failure
+ entity-details/get-metric-details failure
+ entity-details/get-measure-details failure
+ entity-details/get-segment-details failure]
+ (doseq [[kind id suffix] [["table" (mt/id :products) ""]
+ ["table" (mt/id :products) "/fields"]
+ ["question" question-id ""]
+ ["question" question-id "/fields"]
+ ["model" model-id ""]
+ ["model" model-id "/fields"]
+ ["metric" metric-id ""]
+ ["metric" metric-id "/dimensions"]
+ ["measure" 999999 ""]
+ ["segment" 999999 ""]]]
+ (let [uri (str "metabase://" kind "/" id suffix)]
+ (testing uri
+ (let [{:keys [class text]} (test-util/tool-failure read-resource/read-resource-tool {:uris [uri]})]
+ (is (= :recoverable class))
+ (is (str/includes? text (str "**Error:** " (str/capitalize kind) " " id " was not found.")))
+ (is (str/includes? text "Call `search`")))))))))))
;; ===== Behavioral tests for patterns where the dispatch contract isn't enough =====
@@ -1666,6 +1836,35 @@
{:uris [(str "metabase://database/" db-id "/tables?page=999")]})]
(is (=? {:resources [{:error string?}]} result)))))))
+(deftest pagination-copied-xml-uri-test
+ (mt/with-current-user (mt/user->id :crowberto)
+ (mt/with-temp [:model/Collection {parent-id :id} {:name "Paging" :location "/"}]
+ (t2/insert! :model/Collection
+ (for [i (range 60)]
+ {:name (format "Child-%02d" i)
+ :location (str "/" parent-id "/")}))
+ (let [read-page (fn [uri] (test-util/call-tool read-resource/read-resource-tool {:uris [uri]}))
+ page-1 (read-page "metabase://collections?tree=true")
+ next-page-uri (second (re-find #"Fetch (\S+) for the next page" (:output page-1)))
+ page-2 (read-page next-page-uri)
+ resource-uri (second (re-find #"<resource uri=\"([^\"]+)\">" (:output page-2)))
+ copied-page-2 (read-page resource-uri)
+ structured-1 (first (:structured-output page-1))
+ structured-2 (first (:structured-output page-2))]
+ (is (> (:total structured-1) 25))
+ (is (= 25 (count (:items structured-1))))
+ (is (= "metabase://collections?tree=true&amp;page=2" next-page-uri))
+ (testing "a URI copied from the note opens the next page"
+ (is (= 2 (:page structured-2)))
+ (is (= 25 (count (:items structured-2))))
+ (is (= "metabase://collections?tree=true&page=3" (:next-page-uri structured-2)))
+ (is (str/includes? (:output page-2) "Fetch metabase://collections?tree=true&amp;page=3"))
+ (is (empty? (set/intersection (set (map :id (:items structured-1)))
+ (set (map :id (:items structured-2)))))))
+ (testing "the escaped resource attribute can also be copied as-is"
+ (is (= "metabase://collections?tree=true&amp;page=2" resource-uri))
+ (is (= structured-2 (first (:structured-output copied-page-2)))))))))
+
(deftest pagination-xml-output-test
(testing "truncated list XML includes page/pages attrs and a truncation note with next-page URI hint"
(mt/with-current-user (mt/user->id :crowberto)
diff --git a/test/metabase/metabot/tools/shared/llm_shape_test.clj b/test/metabase/metabot/tools/shared/llm_shape_test.clj
index 4622b028cbe..194fc679a2c 100644
--- a/test/metabase/metabot/tools/shared/llm_shape_test.clj
+++ b/test/metabase/metabot/tools/shared/llm_shape_test.clj
@@ -19,6 +19,18 @@
(testing "escape-xml handles nil"
(is (nil? (#'llm-shape/escape-xml nil)))))
+(deftest ^:parallel next-page-uri-xml-escaping-test
+ (let [output (llm-shape/metabot-list->xml
+ {:list-type :recent-items
+ :items []
+ :total 30
+ :page 1
+ :pages 2
+ :next-page-uri "metabase://user/recent-items?filter=<&\"&page=2"})]
+ (is (str/includes? output
+ (str "Fetch metabase://user/recent-items?filter=&lt;&amp;&quot;&amp;page=2 for the next page. "
+ "Copy it exactly; do not modify its query string.")))))
+
(deftest ^:parallel field->xml-test
(testing "formats field with all attributes matching Python format"
;; for field data format see `metabase.metabot.tools.util/->result-column`