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
Delete unused Metabot tools
Andrei Simionescu829cac87a612
diff --git a/frontend/src/metabase/metabot/constants.ts b/frontend/src/metabase/metabot/constants.tsindex d784fbe6e84..7574eddf60f 100644--- a/frontend/src/metabase/metabot/constants.ts+++ b/frontend/src/metabase/metabot/constants.ts@@ -165,12 +165,6 @@ export const TOOL_MESSAGES = {active: () => t`Searching database tables`,done: () => t`Searched database tables`,},- search_transforms: {- active: () => t`Searching transforms`,- done: () => t`Searched transforms`,- },- todo_read: { active: () => t`Planning`, done: () => t`Planned` },- todo_write: { active: () => t`Planning`, done: () => t`Planned` },write_transform_python: {active: () => t`Writing Python`,done: () => t`Wrote Python`,@@ -179,6 +173,18 @@ export const TOOL_MESSAGES = {active: () => t`Writing SQL`,done: () => t`Wrote SQL`,},+ // Removed tools. The model can still call one it saw in an old+ // conversation, and these labels name that step.+ get_transform_details: {+ active: () => t`Getting transform details`,+ done: () => t`Got transform details`,+ },+ search_transforms: {+ active: () => t`Searching transforms`,+ done: () => t`Searched transforms`,+ },+ todo_read: { active: () => t`Planning`, done: () => t`Planned` },+ todo_write: { active: () => t`Planning`, done: () => t`Planned` },} satisfies Record<string, ToolMessage>;// widened view for lookups by dynamic (server-supplied) tool namesdiff --git a/src/metabase/metabot/agent/memory.clj b/src/metabase/metabot/agent/memory.cljindex f731abd7b2b..8c9d4385ea0 100644--- a/src/metabase/metabot/agent/memory.clj+++ b/src/metabase/metabot/agent/memory.clj@@ -13,7 +13,7 @@(defn merge-states"Merge two partial states. Map entries (e.g. :queries) merge entry-wise;- scalar/vector entries (e.g. :todos) take the later value."+ other entries (e.g. :client-ids) take the later value."[a b](merge-with (fn [x y] (if (every? map? [x y]) (merge x y) y)) a b))@@ -27,7 +27,7 @@:steps-taken []:context context:state (metabot.schema/normalize-state- (or state {:queries {} :charts {} :todos [] :link-registry {}}))+ (or state {:queries {} :charts {} :link-registry {}})):turn-state {}}))(defn add-step@@ -124,11 +124,6 @@:chart-id chart-id:available-charts (keys charts)})))))-(defn set-todos- "Set the todo list (a vector of todo item maps)."- [memory todos]- (record memory [:todos] (vec todos)))-(defn add-client-ids"Mark `ids` as the queries and charts the client supplied this turn.Each is persisted, with the ids earlier turns recorded, once a query or chart is stored under it."diff --git a/src/metabase/metabot/agent/streaming.clj b/src/metabase/metabot/agent/streaming.cljindex e01c49408c7..07eb692e9fa 100644--- a/src/metabase/metabot/agent/streaming.clj+++ b/src/metabase/metabot/agent/streaming.clj@@ -14,7 +14,6 @@;; These match the Python AI Service's AISDKDataTypes enum(def state-type "AI-SDK data type for state updates." "state")-(def todo-list-type "AI-SDK data type for todo lists." "todo_list")(def code-edit-type "AI-SDK data type for code edits." "code_edit")(def generated-entity-type "AI-SDK data type for generated entities." "generated_entity")(def entity-saved-type "AI-SDK data type for saved-entity annotations." "entity_saved")@@ -75,17 +74,6 @@:data-type state-type:data state-map})-(defn todo-list-part- "Create a TODO_LIST data part for streaming.- Todos should be a vector of todo item maps with :id, :content, :status, :priority keys.-- This matches Python AI Service's:- ai_sdk.create_data_part(data_type=AISDKDataTypes.TODO_LIST, version=1, value=todos)"- [todos]- {:type :data- :data-type todo-list-type- :data todos})-(defn code-edit-part"Create a CODE_EDIT data part for streaming.Edit-data should be a map describing the code edit operation.diff --git a/src/metabase/metabot/capabilities.clj b/src/metabase/metabot/capabilities.cljindex 14077ffe7d2..d425dae5063 100644--- a/src/metabase/metabot/capabilities.clj+++ b/src/metabase/metabot/capabilities.clj@@ -69,10 +69,7 @@;; second probe here could disagree with that one and leave the agent with no discovery tool.(into #{}(remove nil?)- [(when (or (not (premium-features/is-hosted?))- (premium-features/has-feature? :transforms-basic))- :feature-transforms)- (when (premium-features/has-feature? :semantic-search)+ [(when (premium-features/has-feature? :semantic-search):feature-semantic-search)(when (metabot.settings/metabot-query-execution-enabled?):feature-query-execution)]))diff --git a/src/metabase/metabot/schema.clj b/src/metabase/metabot/schema.cljindex 1222087ec33..b7494a19535 100644--- a/src/metabase/metabot/schema.clj+++ b/src/metabase/metabot/schema.clj@@ -42,8 +42,8 @@::lib.util/query-like])(mr/def ::todo- "One todo item of persisted turn state; only `:id` is guaranteed, since state can hold a partial item- between the turn that creates it and the turn that fills in its details."+ "A todo item in conversation state persisted while Metabot had todo tools. `:todos` stays in [[::state]], a+ closed map, so those conversations still validate."[:map {:closed true}[:id :string][:content {:optional true} [:maybe :string]]diff --git a/src/metabase/metabot/scope.clj b/src/metabase/metabot/scope.cljindex ad5714aa01b..77c846fc31b 100644--- a/src/metabase/metabot/scope.clj+++ b/src/metabase/metabot/scope.clj@@ -75,10 +75,6 @@(api-scope/defscope agent-metric-update "agent:metric:update"(deferred-tru "Update metrics"))-;; Transforms-(api-scope/defscope agent-transforms-read "agent:transforms:read"- (deferred-tru "View transforms"))-;; Snippets(api-scope/defscope agent-snippets-read "agent:snippets:read"(deferred-tru "View SQL snippets"))@@ -161,12 +157,6 @@(api-scope/defscope agent-resource-read "agent:resource:read"(deferred-tru "Read MCP resources"))-;; Todo-(api-scope/defscope agent-todo-read "agent:todo:read"- (deferred-tru "View todos"))-(api-scope/defscope agent-todo-write "agent:todo:write"- (deferred-tru "Create and edit todos"))-;;; ──────────────────────────────────────────────────────────────────;;; Metabot permission type definitions;;; ──────────────────────────────────────────────────────────────────@@ -229,7 +219,7 @@(def ^:private perm-type->scopes"Map from metabot permission type to the wildcard scope strings granted whenthat permission is `:yes`."- {:permission/metabot-sql-generation #{"agent:sql:*" "agent:transforms:*" "agent:snippets:*"}+ {:permission/metabot-sql-generation #{"agent:sql:*" "agent:snippets:*"};; NLQ grants `agent:content:read` (not the `agent:content:*` wildcard): an NLQ-only user reads;; content and data structure, but content *writes* are an other-tools capability. Granting the;; wildcard here would satisfy `agent:content:write` too, over-granting NLQ-only users.@@ -259,7 +249,7 @@(def always-granted-scopes"Scopes granted to every user regardless of permissions."- #{"agent:search" "agent:resource:*" "agent:todo:*" "agent:metadata:*"})+ #{"agent:search" "agent:resource:*" "agent:metadata:*"})(def all-yes-permissions"Permissions map granting all permissions. Used for superuser context."diff --git a/src/metabase/metabot/tools.clj b/src/metabase/metabot/tools.cljindex 0fba7d6d0d4..dd2ac02adc9 100644--- a/src/metabase/metabot/tools.clj+++ b/src/metabase/metabot/tools.clj@@ -31,7 +31,6 @@[metabase.metabot.tools.static-viz :as tools.static-viz][metabase.metabot.tools.subscriptions :as tools.subscriptions][metabase.metabot.tools.timelines :as tools.timelines]- [metabase.metabot.tools.todo :as tools.todo][potemkin :as p]))(set! *warn-on-reflection* true)@@ -81,9 +80,6 @@[tools.snippetslist-snippets-toolget-snippet-details-tool]- [tools.todo- todo-write-tool- todo-read-tool][tools.timelineslist-timelines-toolget-timeline-details-tool]diff --git a/src/metabase/metabot/tools/core.clj b/src/metabase/metabot/tools/core.cljindex 744edd1ffd0..9e02b6e1edc 100644--- a/src/metabase/metabot/tools/core.clj+++ b/src/metabase/metabot/tools/core.clj@@ -7,10 +7,9 @@or what text each audience sees — that is `metabase.metabot.tools.runtime`'s job, so a tool sayswhat happened exactly once.- A record rather than a function because several of our tools are the *same* tool under different- configuration, and a record says so. The four `search` tools share a scope, a title function and a- body, and differ only in their name, description, argument schema, allowed entity types and an- options map — four instances of one type, not four functions that happen to resemble each other.+ Several tools use the same implementation with different settings. The three `search` tools share+ a name, scope, title function and handler, but differ in their descriptions, argument schemas,+ allowed entity types and options.Handlers return success only. There is no error-shaped success value and no `{:output \"Failedto …\"}` convention: a failure is thrown, so forgetting to handle one cannot silently hand the@@ -95,9 +94,8 @@Closed on purpose: a misspelled key (`:capability`, `:scopes`) would otherwise read as \"this toolneeds nothing\" and quietly drop a gate.- `:description` is the model-facing text. It is a field rather than a docstring because a tool that- exists in several configurations should be able to build it — the four `search` tools each hardcode- theirs today precisely because a docstring cannot be computed.+ `:description` is the model-facing text. It is a field so that tools with several configurations+ can compute it. The three `search` tools each hardcode theirs because docstrings cannot be computed.`:name`, `:description`, `:args` and `:scope` are the neutral core: every consumer needs all four,and `:scope` is shared because both surfaces check strings from the same `api-scope` registry (MCP@@ -400,10 +398,9 @@The declaration is the published one, so a batched tool's entry carries its batched `:args` and theruntime validates the model's arguments against the shape it was actually offered.- Rejects two tools claiming the same name: within a profile the name is how the model and the- runtime address a tool, so a collision means one of them is unreachable and which one depends on- the order of the seq. Across profiles a name legitimately belongs to several tools — `search` has- four — which is why there is no global registry."+ Rejects duplicate names within a profile, where both the model and the runtime use the name to+ identify a tool. A collision would make one tool unreachable, depending on their order. Tools in+ different profiles may share a name, as the three `search` tools do, so there is no global registry."[tools](reduce (fn [acc tool](let [{tool-name :name :as declared} (validate-tool! tool)]diff --git a/src/metabase/metabot/tools/generate_insights.clj b/src/metabase/metabot/tools/generate_insights.cljdeleted file mode 100644index 5fcd2a6c330..00000000000--- a/src/metabase/metabot/tools/generate_insights.clj+++ /dev/null@@ -1,37 +0,0 @@-(ns metabase.metabot.tools.generate-insights- (:require- [buddy.core.codecs :as codecs]- [metabase.lib.util :as lib.util]- [metabase.metabot.agent.streaming :as streaming]- [metabase.metabot.tools.util :as metabot.tools.u]- [metabase.util.json :as json]))--(set! *warn-on-reflection* true)--(defn generate-insights- "Generate insights."- [{what-for :for}]- (try- (let [[k id] (some #(find what-for %) [:metric_id :table_id :report_id :query])- model-id (when (= k :table_id)- (lib.util/legacy-string-table-id->card-id id))- entity-type (case k- (:metric_id :report_id) "question"- :table_id (if model-id "model" "table")- (:query :query_id) "adhoc"- (throw (ex-info (str "Cannot generate insights for " what-for) {:agent-error? true})))- entity-id (cond- model-id model-id- (= k :query) (-> id json/encode .getBytes codecs/bytes->b64-str)- :else id)- results-url (str "/auto/dashboard/" entity-type "/" entity-id)]- (when (and (= k :table_id)- (not (int? id))- (not (and (string? id)- (re-matches #"(?:card__)?\d+" id))))- (throw (ex-info "Invalid table_id" {:agent-error? true- :table_id id})))- {:output results-url- :data-parts [(streaming/dashboard-entity-part {:url results-url})]})- (catch Exception e- (metabot.tools.u/handle-agent-error e))))diff --git a/src/metabase/metabot/tools/search.clj b/src/metabase/metabot/tools/search.cljindex 5fe97709823..08636a883cf 100644--- a/src/metabase/metabot/tools/search.clj+++ b/src/metabase/metabot/tools/search.clj@@ -344,7 +344,7 @@multi-query search is coherent. The result carries the size of the fused, deduped match set as`:total` metadata."[{:keys [term-queries semantic-queries database-id created-at last-edited-at- entity-types limit metabot profile-id search-native-query weights+ entity-types limit metabot profile-id weightscreated-by archived collection-id offset filters-only?]}](log/infof "[METABOT-SEARCH] Starting search with params: %s"{:term-query-count (count term-queries)@@ -354,7 +354,6 @@:limit limit:metabot-id (:entity_id metabot):profile-id profile-id- :search-native-query search-native-query:weights weights:created-by created-by:archived archived@@ -393,10 +392,6 @@:filter-items-in-personal-collection "exclude-others":context :metabot:archived (boolean archived)})- ;; Don't include search-native-query key if nil so that we don't- ;; inadvertently filter out search models that don't support it- search-native-query- (assoc :search-native-query (boolean search-native-query))use-verified?(assoc :curated true)weightsdiff --git a/src/metabase/metabot/tools/todo.clj b/src/metabase/metabot/tools/todo.cljdeleted file mode 100644index 14728244d73..00000000000--- a/src/metabase/metabot/tools/todo.clj+++ /dev/null@@ -1,142 +0,0 @@-(ns metabase.metabot.tools.todo- "Tools for managing todo lists during agent conversations.- Todos are stored in agent memory and streamed to the frontend via todo_list data parts."- (:require- [clojure.string :as str]- [metabase.metabot.agent.memory :as memory]- [metabase.metabot.agent.streaming :as streaming]- [metabase.metabot.scope :as scope]- [metabase.metabot.tools.shared :as shared]- [metabase.metabot.tools.util :as metabot.tools.u]- [metabase.util.log :as log]- [metabase.util.malli :as mu]))--(set! *warn-on-reflection* true)--(def ^:private valid-statuses- "Valid status values for todo items."- #{"pending" "in_progress" "completed" "cancelled"})--(def ^:private valid-priorities- "Valid priority values for todo items."- #{"high" "medium" "low"})--(defn- validate-todo- "Validate a single todo item. Throws if invalid."- [{:keys [id content status priority] :as todo}]- (when-not (and id (string? id) (seq id))- (throw (ex-info "Todo item missing required 'id' field"- {:agent-error? true :todo todo})))- (when-not (and content (string? content) (seq content))- (throw (ex-info "Todo item missing required 'content' field"- {:agent-error? true :todo todo})))- (when-not (contains? valid-statuses status)- (throw (ex-info (str "Invalid todo status: " status ". Valid statuses: " (pr-str valid-statuses))- {:agent-error? true :status status})))- (when-not (contains? valid-priorities priority)- (throw (ex-info (str "Invalid todo priority: " priority ". Valid priorities: " (pr-str valid-priorities))- {:agent-error? true :priority priority})))- todo)--(defn- validate-todos- "Validate all todos. Returns the validated todos vector or throws."- [todos]- (when-not (sequential? todos)- (throw (ex-info "Todos must be an array" {:agent-error? true})))- (mapv validate-todo todos))--(defn todo-write- "Write/update the todo list.- Stores todos in memory and returns a todo_list data part for streaming.-- Parameters:- - todos: Vector of todo item maps with :id, :content, :status, :priority- - memory-atom: Atom containing agent memory (injected by wrapper)-- Returns map with:- - :structured-output - Message for the LLM- - :data-parts - Vector containing todo_list data part for streaming- - :instructions - Instructions for the LLM on how to handle the result"- [{:keys [todos memory-atom]}]- (log/info "Writing todo list" {:todo-count (count todos)})- (let [validated-todos (validate-todos todos)]- ;; Store in memory- (swap! memory-atom memory/set-todos validated-todos)- (log/debug "Stored todos in memory" {:count (count validated-todos)})- {:structured-output {:message "Todo list updated successfully."- :todo_count (count validated-todos)}- :data-parts [(streaming/todo-list-part validated-todos)]- :instructions "The todo list has been updated and is automatically displayed to the user. NEVER repeat or summarize the todo list contents in your response."}))--(defn todo-read- "Read the current todo list from memory.-- Parameters:- - memory-atom: Atom containing agent memory (injected by wrapper)-- Returns map with:- - :structured-output - The current todos- - :instructions - Instructions for the LLM"- [{:keys [memory-atom]}]- (let [todos (get-in @memory-atom [:state :todos] [])]- (log/debug "Reading todos from memory" {:count (count todos)})- {:structured-output {:todos todos- :todo_count (count todos)}- :instructions "You should call todo_write after reading this todo_list to make any updates."}))--(defn- format-todo-write-output- [{:keys [todo_count]}]- (str "Todo list updated successfully. " todo_count " item(s)."))--(defn- format-todo-read-output- [{:keys [todos todo_count]}]- (if (seq todos)- (str "Current todo list (" todo_count " items):\n"- (str/join "\n" (map (fn [{:keys [id content status priority]}]- (str "- [" status "] " content " (id=" id ", priority=" priority ")"))- todos)))- "No todos found."))--(defn- add-output- [result format-fn]- (if-let [structured (:structured-output result)]- (assoc result :output (format-fn structured))- result))--(def ^:private todo-write-schema- [:map {:closed true}- [:todos [:sequential [:map {:closed true}- [:id :string]- [:content :string]- [:status [:enum "pending" "in_progress" "completed" "cancelled"]]- [:priority [:enum "high" "medium" "low"]]]]]])--(mu/defn ^{:tool-name "todo_write"- :scope scope/agent-todo-write}- todo-write-tool- "Create and manage a structured task list.- Write or update the todo list with tasks for tracking progress.-- Each todo item must have:- - id: Unique identifier for the task- - content: Description of what needs to be done- - status: One of 'pending', 'in_progress', 'completed', 'cancelled'- - priority: One of 'high', 'medium', 'low'"- [{:keys [todos]} :- todo-write-schema]- (try- (add-output- (todo-write {:todos todos- :memory-atom shared/*memory-atom*})- format-todo-write-output)- (catch Exception e- (metabot.tools.u/handle-agent-error e))))--(mu/defn ^{:tool-name "todo_read"- :scope scope/agent-todo-read}- todo-read-tool- "Read the current todo list from memory.- Returns the list of todos that have been created during this conversation."- [_args :- [:map {:closed true}]]- (add-output- (todo-read {:memory-atom shared/*memory-atom*})- format-todo-read-output))diff --git a/test/metabase/metabot/agent/memory_test.clj b/test/metabase/metabot/agent/memory_test.cljindex 623d2143a32..637476d6e4b 100644--- a/test/metabase/metabot/agent/memory_test.clj+++ b/test/metabase/metabot/agent/memory_test.clj@@ -22,7 +22,7 @@(is (= [] (:steps-taken mem)))))(testing "defaults the working state when nil"(let [mem (memory/initialize [{:role :user :content "Hello"}] nil)]- (is (= {:queries {} :charts {} :todos [] :link-registry {}}+ (is (= {:queries {} :charts {} :link-registry {}}(memory/get-state mem))))))(deftest ^:parallel initialize-repairs-stripped-queries-test@@ -104,12 +104,12 @@(let [mem (memory/initialize [] {:queries {"q1" {:id "q1"}}})mem' (-> mem(memory/set-query "q2" {:id "q2"})- (memory/set-todos [{:id "b"}]))]+ (memory/set-chart "c1" {:id "c1"}))](testing "working state carries the seeded baseline plus this turn's writes"- (is (= {:queries {"q1" {:id "q1"} "q2" {:id "q2"}} :todos [{:id "b"}]}+ (is (= {:queries {"q1" {:id "q1"} "q2" {:id "q2"}} :charts {"c1" {:id "c1"}}}(memory/get-state mem'))))(testing "turn delta carries only this turn's writes — not the seeded baseline"- (is (= {:queries {"q2" {:id "q2"}} :todos [{:id "b"}]}+ (is (= {:queries {"q2" {:id "q2"}} :charts {"c1" {:id "c1"}}}(memory/turn-state mem'))))))(testing "turn-state is nil until this turn writes something"(let [mem (memory/initialize [] {:queries {"q1" {:id "q1"}}})]@@ -124,6 +124,11 @@(is (= #{"seeded-q"}(get-in (memory/initialize [] persisted) [:state :client-ids]))))))+(deftest ^:parallel persisted-todo-lists-validate-test+ (testing "state persisted while Metabot had todo tools still validates, since the agent entry point checks it"+ (is (mr/validate ::metabot.schema/state+ {:todos [{:id "1" :content "Find the orders table" :status "pending" :priority "high"}]}))))+(deftest ^:parallel client-ids-persist-once-stored-test(let [query {:database 1}earlier (memory/initialize [] {:client-ids #{"from-an-earlier-turn"}})@@ -162,10 +167,10 @@(memory/turn-state mem'))))))(deftest ^:parallel merge-states-test- (testing "map entries merge; scalar/vector entries take the later value"- (is (= {:queries {:q1 1 :q2 2} :todos [{:id "b"}]}- (memory/merge-states {:queries {:q1 1} :todos [{:id "a"}]}- {:queries {:q2 2} :todos [{:id "b"}]})))))+ (testing "map entries merge; other entries take the later value"+ (is (= {:queries {:q1 1 :q2 2} :client-ids #{"b"}}+ (memory/merge-states {:queries {:q1 1} :client-ids #{"a"}}+ {:queries {:q2 2} :client-ids #{"b"}})))))(deftest ^:parallel get-steps-test(testing "retrieves all steps"diff --git a/test/metabase/metabot/agent/profiles_test.clj b/test/metabase/metabot/agent/profiles_test.cljindex b79242ca983..4863e751078 100644--- a/test/metabase/metabot/agent/profiles_test.clj+++ b/test/metabase/metabot/agent/profiles_test.clj@@ -7,7 +7,8 @@[metabase.metabot.tools :as tools][metabase.metabot.tools.legacy :as tools.legacy][metabase.search.entity-retrieval.core :as entity-retrieval]- [metabase.test :as mt]))+ [metabase.test :as mt]+ [metabase.util.malli.registry :as mr]))(deftest get-profile-test;; The declaration, not the var's metadata: a converted tool is a record and carries none.@@ -267,3 +268,13 @@(is (contains? tools "read_resource")"precondition: read_resource is active (would otherwise match a skill)")(is (not (contains? tools "load_skill")))))))++(deftest ^:parallel every-tool-is-in-a-profile-test+ (testing "every tool is in a profile, except the snippet tools, which no profile offers yet"+ ;; `profile->tools` adds load_skill to every profile that has skills+ (let [offered (into #{#'tools/load-skill-tool} (mapcat :tools) (vals @@#'profiles/*profiles))]+ (is (= #{#'tools/list-snippets-tool #'tools/get-snippet-details-tool}+ (into #{}+ (comp (filter #(mr/validate @#'profiles/tool-var-schema %))+ (remove offered))+ (vals (ns-publics 'metabase.metabot.tools))))))))diff --git a/test/metabase/metabot/agent/streaming_test.clj b/test/metabase/metabot/agent/streaming_test.cljindex 0c4f1610b5a..307c71b0e20 100644--- a/test/metabase/metabot/agent/streaming_test.clj+++ b/test/metabase/metabot/agent/streaming_test.clj@@ -70,20 +70,6 @@;;; New Data Part Types (AI Service Parity)-(deftest todo-list-part-test- (testing "creates todo_list data part with correct structure"- (let [todos [{:id "1" :content "Task 1" :status "pending" :priority "high"}- {:id "2" :content "Task 2" :status "completed" :priority "low"}]- part (streaming/todo-list-part todos)]- (is (= :data (:type part)))- (is (= "todo_list" (:data-type part)))- (is (= todos (:data part)))))- (testing "handles empty todo list"- (let [part (streaming/todo-list-part [])]- (is (= :data (:type part)))- (is (= "todo_list" (:data-type part)))- (is (= [] (:data part))))))-(deftest code-edit-part-test(testing "creates code_edit data part with correct structure"(let [edit-data {:buffer_id "buffer-123"@@ -129,7 +115,6 @@(deftest data-type-constants-test(testing "data type constants are defined correctly"(is (= "state" streaming/state-type))- (is (= "todo_list" streaming/todo-list-type))(is (= "code_edit" streaming/code-edit-type))(is (= "generated_entity" streaming/generated-entity-type))(is (= "adhoc_viz" streaming/adhoc-viz-type))@@ -143,7 +128,7 @@(testing "tool-title parts are not persisted (they render under the ephemeral chain of thought)"(is (false? (streaming/persistable-data-part? (streaming/tool-title-part "[Orders](metabase://table/1)")))))(testing "other parts are persisted"- (is (true? (streaming/persistable-data-part? (streaming/todo-list-part []))))+ (is (true? (streaming/persistable-data-part? {:type :data :data-type "todo_list" :data []})))(is (true? (streaming/persistable-data-part? {:type :text :text "hi"})))));;; Transducer Testsdiff --git a/test/metabase/metabot/query_export_test.clj b/test/metabase/metabot/query_export_test.cljnew file mode 100644index 00000000000..d67f9de8e94--- /dev/null+++ b/test/metabase/metabot/query_export_test.clj@@ -0,0 +1,85 @@+(ns metabase.metabot.query-export-test+ (:require+ [clojure.string :as str]+ [clojure.test :refer :all]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.metabot.metadata-perms :as metabot.perms]+ [metabase.metabot.query-export :as query-export]+ [metabase.metabot.tools.shared.content-store :as shared.content-store]+ [metabase.test :as mt]+ [metabase.util.json :as json]+ [toucan2.core :as t2]))++(defn- rendered-source+ "Return the exported query for a transform, or nil if export is denied."+ [query]+ (get-in (query-export/transform-with-exportable-source {:source {:type "query" :query query}})+ [:source :query]))++(deftest transform-source-withholds-native-sql-behind-a-later-stage-test+ (testing "native SQL is withheld if its permissions cannot be checked, even when an MBQL stage follows it"+ (let [native-stage {:lib/type :mbql.stage/native+ :native "SELECT * FROM {{snip}}"+ :template-tags {"snip" {:type :snippet+ :name "snip"+ :display-name "snip"+ :snippet-id Integer/MAX_VALUE}}}]+ (mt/with-current-user (mt/user->id :rasta)+ (doseq [stages [[native-stage] [native-stage {:lib/type :mbql.stage/mbql}]]]+ (is (nil? (rendered-source {:lib/type :mbql/query :database (mt/id) :stages stages}))+ (str (count stages) " stage(s)")))))))++(deftest transform-source-survives-a-deleted-source-database-test+ (testing "a transform keeps its source after its database is deleted so that an admin can repair it"+ (mt/with-temp [:model/Database {db-id :id} {}]+ (t2/delete! :model/Database :id db-id)+ (mt/with-current-user (mt/user->id :crowberto)+ (is (str/includes? (str (rendered-source {:database db-id+ :type :native+ :native {:query "SELECT 1"}}))+ "SELECT 1"))))))++(deftest transform-source-withholds-a-sandbox-hidden-field-test+ (testing "a source query is withheld if it uses a field hidden by the user's sandbox"+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :venues)))+ (lib/filter (lib/> (lib.metadata/field mp (mt/id :venues :price)) 1)))]+ (mt/with-current-user (mt/user->id :rasta)+ (is (string? (rendered-source query)))+ (mt/with-dynamic-fn-redefs [metabot.perms/sandbox-restricted-fields+ (fn [_table-ids] {(mt/id :venues) #{}})]+ (is (nil? (rendered-source query))))))))++(deftest transform-source-renders-portable-mbql-test+ (mt/with-current-user (mt/user->id :crowberto)+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :products)))]+ (is (:mp (shared.content-store/query-for-export query false)))+ (let [exported (json/decode (second (re-find #"(?s)```json\n(.*)\n```" (rendered-source query))))]+ (is (= [(:name (lib.metadata/database mp)) "PUBLIC" "PRODUCTS"]+ (get-in exported ["stages" 0 "source-table"])))+ (is (not-any? #(and (map? %) (contains? % "lib/metadata"))+ (tree-seq coll? seq exported)))))))++(deftest transform-source-renders-an-unchecked-source-unresolved-test+ (testing "a source query keeps its IDs unresolved if its permissions cannot be checked"+ (mt/with-current-user (mt/user->id :rasta)+ (let [query {:lib/type :mbql/query+ :database (mt/id)+ :stages [{:lib/type :mbql.stage/mbql :source-card 13371337}]}]+ (is (:unchecked? (shared.content-store/query-for-export query false)))+ (is (str/includes? (str (rendered-source query)) ":source-card 13371337"))))))++(deftest transform-source-withholds-unchecked-legacy-native-sql-test+ (testing "native SQL in the legacy format is withheld if its permissions cannot be checked"+ (let [query {:database (mt/id)+ :type :native+ :native {:query "SELECT * FROM {{snip}}"+ :template-tags {"snip" {:type :snippet+ :name "snip"+ :display-name "snip"+ :snippet-id Integer/MAX_VALUE}}}}]+ (mt/with-current-user (mt/user->id :rasta)+ (is (:unchecked? (shared.content-store/query-for-export query false)))+ (is (nil? (rendered-source query)))))))diff --git a/test/metabase/metabot/scope_resolution_test.clj b/test/metabase/metabot/scope_resolution_test.cljindex 4b493909b5d..953c0751e98 100644--- a/test/metabase/metabot/scope_resolution_test.clj+++ b/test/metabase/metabot/scope_resolution_test.clj@@ -5,13 +5,12 @@[metabase.test :as mt]))(deftest user-metabot-perms-sql-generation-scopes-test- (testing "sql-generation :yes grants sql, transforms, snippets scopes"+ (testing "enabling SQL generation grants access to SQL tools and snippets"(let [scopes (scope/user-metabot-perms->scopes{:permission/metabot-sql-generation :yes:permission/metabot-nlq :no:permission/metabot-other-tools :no})](is (contains? scopes "agent:sql:*"))- (is (contains? scopes "agent:transforms:*"))(is (contains? scopes "agent:snippets:*"))(is (not (contains? scopes "agent:notebook:*")))(is (not (contains? scopes "agent:viz:*"))))))@@ -47,7 +46,6 @@:permission/metabot-other-tools :no})](is (contains? scopes "agent:search"))(is (contains? scopes "agent:resource:*"))- (is (contains? scopes "agent:todo:*"))(is (contains? scopes "agent:metadata:*")))))(deftest user-metabot-perms-all-yes-scopes-testdiff --git a/test/metabase/metabot/scope_test.clj b/test/metabase/metabot/scope_test.cljindex dfb2537d588..19ce4c97d72 100644--- a/test/metabase/metabot/scope_test.clj+++ b/test/metabase/metabot/scope_test.clj@@ -44,7 +44,6 @@(deftest ^:parallel perms->scopes-sql-generation-test(let [scopes (scope/user-metabot-perms->scopes {:permission/metabot-sql-generation :yes})](is (contains? scopes "agent:sql:*"))- (is (contains? scopes "agent:transforms:*"))(is (contains? scopes "agent:snippets:*"))(is (contains? scopes "agent:search"))(testing "metric is an MBQL macro gated by NLQ, not SQL generation"diff --git a/test/metabase/metabot/tools/resources_test.clj b/test/metabase/metabot/tools/resources_test.cljindex 7ee351819e8..294f6f99cc0 100644--- a/test/metabase/metabot/tools/resources_test.clj+++ b/test/metabase/metabot/tools/resources_test.clj@@ -797,6 +797,33 @@(let [result (read-uris {:uris [(str "metabase://transform/" transform-id)]})](is (some? (get-in result [:resources 0 :content :structured-output :source :query])))))))))))))+(mt/when-ee-evailable+ (deftest read-transform-resource-source-card-permission-test+ (mt/with-premium-features #{:transforms-basic :hosting :advanced-permissions}+ (mt/with-non-admin-groups-no-root-collection-perms+ (mt/with-full-data-perms-for-all-users!+ (mt/with-data-analyst-role! (mt/user->id :rasta)+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Collection {collection-id :id} {}+ :model/Card {card-id :id}+ {:collection_id collection-id+ :database_id (mt/id)+ :dataset_query (lib/query mp (lib.metadata/table mp (mt/id :orders)))}+ :model/Transform {transform-id :id}+ {:name "Private source card"+ :source {:type "query"+ :query (lib/query mp (lib.metadata/card mp card-id))}}]+ (let [uri (str "metabase://transform/" transform-id)]+ (testing "a source card in an unreadable collection prevents reading the transform"+ (mt/with-current-user (mt/user->id :rasta)+ (is (=? {:resources [{:error #"(?s)^Transform \d+ was not found\..*"}]}+ (read-uris {:uris [uri]})))))+ (testing "the source query renders after granting read on the collection"+ (perms/grant-collection-read-permissions! (perms-group/all-users) collection-id)+ (mt/with-current-user (mt/user->id :rasta)+ (is (string? (get-in (read-uris {:uris [uri]})+ [:resources 0 :content :structured-output :source :query]))))))))))))))+(defn- read-title"The chain-of-thought title `read-resource` derives from what it read."[& uris]diff --git a/test/metabase/metabot/tools/search_test.clj b/test/metabase/metabot/tools/search_test.cljindex 772ec12fca4..4cb5ed3632e 100644--- a/test/metabase/metabot/tools/search_test.clj+++ b/test/metabase/metabot/tools/search_test.clj@@ -523,29 +523,6 @@:table_name "ORDERS":table_schema "PUBLIC"}))))))-(deftest search-native-query-test- (mt/with-test-user :rasta- (with-redefs [perms/impersonated-user? (fn [] false)- perms/sandboxed-user? (fn [] false)- api/*current-user-id* 1]- (testing ":search-native-query is included in context when true"- (mt/with-dynamic-fn-redefs [search-core/ranked-results (fn [context]- (is (true? (:search-native-query context)))- [])]- (search/search {:term-queries ["test"]- :entity-types ["card"]- :search-native-query true})))- (testing ":search-native-query is not included in context when nil or false"- (mt/with-dynamic-fn-redefs [search-core/ranked-results (fn [context]- (is (not (contains? context :search-native-query)))- [])]- (search/search {:term-queries ["test"]- :entity-types ["card"]- :search-native-query false})- (search/search {:term-queries ["test"]- :entity-types ["card"]- :search-native-query nil}))))))-(deftest tool-default-entity-types-test(testing "tool variants restrict default entity types to their allowed set"(mt/with-test-user :rastadiff --git a/test/metabase/metabot/tools/todo_test.clj b/test/metabase/metabot/tools/todo_test.cljdeleted file mode 100644index 4ea3547271c..00000000000--- a/test/metabase/metabot/tools/todo_test.clj+++ /dev/null@@ -1,113 +0,0 @@-(ns metabase.metabot.tools.todo-test- (:require- [clojure.test :refer :all]- [metabase.metabot.tools.shared :as shared]- [metabase.metabot.tools.todo :as todo]- [metabase.test :as mt]))--(deftest ^:parallel todo-write-test- (testing "todo-write validates and stores todos in memory"- (let [memory-atom (atom {:state {}})- todos [{:id "1" :content "First task" :status "pending" :priority "high"}- {:id "2" :content "Second task" :status "in_progress" :priority "medium"}]- result (todo/todo-write {:todos todos :memory-atom memory-atom})]- (is (map? result))- (is (contains? result :structured-output))- (is (contains? result :data-parts))- (is (contains? result :instructions))- ;; Check memory was updated- (is (= todos (get-in @memory-atom [:state :todos]))))))--(deftest ^:parallel todo-write-test-2- (testing "todo-write returns data-parts with todo_list type"- (let [memory-atom (atom {:state {}})- todos [{:id "1" :content "Task" :status "pending" :priority "low"}]- result (todo/todo-write {:todos todos :memory-atom memory-atom})- data-part (first (:data-parts result))]- (is (= :data (:type data-part)))- (is (= "todo_list" (:data-type data-part)))- (is (= todos (:data data-part))))))--(deftest ^:parallel todo-write-test-3- (testing "todo-write rejects invalid status"- (let [memory-atom (atom {:state {}})- todos [{:id "1" :content "Task" :status "invalid_status" :priority "high"}]]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Invalid todo status"- (todo/todo-write {:todos todos :memory-atom memory-atom}))))))--(deftest ^:parallel todo-write-test-4- (testing "todo-write rejects invalid priority"- (let [memory-atom (atom {:state {}})- todos [{:id "1" :content "Task" :status "pending" :priority "critical"}]]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"Invalid todo priority"- (todo/todo-write {:todos todos :memory-atom memory-atom}))))))--(deftest ^:parallel todo-write-test-5- (testing "todo-write rejects missing id"- (let [memory-atom (atom {:state {}})- todos [{:content "Task" :status "pending" :priority "high"}]]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"missing required 'id' field"- (todo/todo-write {:todos todos :memory-atom memory-atom}))))))--(deftest ^:parallel todo-write-test-6- (testing "todo-write rejects missing content"- (let [memory-atom (atom {:state {}})- todos [{:id "1" :status "pending" :priority "high"}]]- (is (thrown-with-msg?- clojure.lang.ExceptionInfo- #"missing required 'content' field"- (todo/todo-write {:todos todos :memory-atom memory-atom}))))))--(deftest ^:parallel todo-write-test-7- (testing "todo-write accepts all valid statuses"- (let [memory-atom (atom {:state {}})]- (doseq [status ["pending" "in_progress" "completed" "cancelled"]]- (let [todos [{:id "1" :content "Task" :status status :priority "medium"}]- result (todo/todo-write {:todos todos :memory-atom memory-atom})]- (is (some? (:structured-output result))))))))--(deftest ^:parallel todo-write-test-8- (testing "todo-write accepts all valid priorities"- (let [memory-atom (atom {:state {}})]- (doseq [priority ["high" "medium" "low"]]- (let [todos [{:id "1" :content "Task" :status "pending" :priority priority}]- result (todo/todo-write {:todos todos :memory-atom memory-atom})]- (is (some? (:structured-output result))))))))--(deftest ^:parallel todo-read-test- (testing "todo-read returns empty list when no todos in memory"- (let [memory-atom (atom {:state {}})- result (todo/todo-read {:memory-atom memory-atom})]- (is (map? result))- (is (contains? result :structured-output))- (is (= [] (get-in result [:structured-output :todos])))- (is (= 0 (get-in result [:structured-output :todo_count])))))- (testing "todo-read returns stored todos"- (let [todos [{:id "1" :content "Task 1" :status "pending" :priority "high"}- {:id "2" :content "Task 2" :status "completed" :priority "low"}]- memory-atom (atom {:state {:todos todos}})- result (todo/todo-read {:memory-atom memory-atom})]- (is (= todos (get-in result [:structured-output :todos])))- (is (= 2 (get-in result [:structured-output :todo_count])))))- (testing "todo-read includes instructions for LLM"- (let [memory-atom (atom {:state {:todos [{:id "1" :content "Task" :status "pending" :priority "medium"}]}})- result (todo/todo-read {:memory-atom memory-atom})]- (is (contains? result :instructions))- (is (string? (:instructions result))))))--(deftest ^:parallel todo-tools-errors-test- (testing "an invalid todo goes back to the agent as output"- (binding [shared/*memory-atom* (atom {:state {}})]- (is (= {:output "Todo item missing required 'id' field"}- (todo/todo-write-tool {:todos [{:id "" :content "Task" :status "pending" :priority "high"}]})))))- (testing "an unexpected error propagates to the agent loop"- (mt/with-dynamic-fn-redefs [todo/todo-write (fn [_] (throw (ex-info "boom" {})))- todo/todo-read (fn [_] (throw (ex-info "boom" {})))]- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"boom" (todo/todo-write-tool {:todos []})))- (is (thrown-with-msg? clojure.lang.ExceptionInfo #"boom" (todo/todo-read-tool {}))))))