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
Save viewing context item IDs only once stored (#82748)
Andrei Simionescuaf594ebafe7c
diff --git a/src/metabase/metabot/agent/core.clj b/src/metabase/metabot/agent/core.cljindex ff194f23743..e7212968317 100644--- a/src/metabase/metabot/agent/core.clj+++ b/src/metabase/metabot/agent/core.clj@@ -435,11 +435,10 @@(:user_is_viewing context)))(defn- client-content-ids- "Ids of the queries and charts this request's viewing context seeds, as opposed to ones the- agent's own tools wrote. A refusal to present one of these is a real access attempt and gets- the audited treatment; see [[metabase.metabot.tools.shared.content-store]]. Seeding a fresh map- keeps this to what the client sent this turn; [[metabase.metabot.agent.memory/add-client-ids]]- adds it to what earlier turns recorded."+ "Ids of the queries and charts this request's viewing context seeds, as opposed to ones the agent's own tools wrote.+ A refusal to present one of these is a real access attempt and gets the audited treatment; see+ [[metabase.metabot.tools.shared.content-store]]. Seeding a fresh map keeps this to what the client+ sent this turn."[context](let [seeded (-> {} (seed-state context) (seed-charts context))](into (set (keys (:queries seeded)))diff --git a/src/metabase/metabot/agent/memory.clj b/src/metabase/metabot/agent/memory.cljindex 7f4985c48a7..f731abd7b2b 100644--- a/src/metabase/metabot/agent/memory.clj+++ b/src/metabase/metabot/agent/memory.clj@@ -44,6 +44,15 @@(assoc-in (into [:state] path) v)(assoc-in (into [:turn-state] path) v)))+(defn- record-client-id+ "Persist `id` as client-supplied when [[add-client-ids]] marked it and it isn't recorded yet."+ [memory id]+ (let [recorded (set (get-in memory [:state :client-ids]))]+ (if (and (contains? (:client-ids memory) id)+ (not (contains? recorded id)))+ (record memory [:client-ids] (conj recorded id))+ memory)))+(defn get-state"The agent's current full working state."[memory]@@ -77,7 +86,9 @@"Store a query in state by its query-id.Query should be an MBQL 4 (legacy) or MBQL 5 query map."[memory query-id query]- (record memory [:queries query-id] query))+ (-> memory+ (record [:queries query-id] query)+ (record-client-id query-id)))(defn find-query"Retrieve a query by its query-id. Throws if not found."@@ -97,7 +108,9 @@(defn set-chart"Store a chart configuration in state by its chart-id."[memory chart-id chart]- (record memory [:charts chart-id] chart))+ (-> memory+ (record [:charts chart-id] chart)+ (record-client-id chart-id)))(defn find-chart"Retrieve a chart by its chart-id. Throws if not found."@@ -117,12 +130,10 @@(record memory [:todos] (vec todos)))(defn add-client-ids- "Add `ids` to the queries and charts this conversation knows the client supplied.- Earlier turns' ids are kept. A tool can persist a client-supplied query and a later turn read- it back long after it left the viewing context, so the provenance has to live as long as the- query does."+ "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."[memory ids]- (record memory [:client-ids] (into (set (get-in memory [:state :client-ids])) ids)))+ (update memory :client-ids (fnil into #{}) ids))(defn set-link-registry"Set the link registry (url → stable id mappings)."diff --git a/src/metabase/metabot/tools/shared.clj b/src/metabase/metabot/tools/shared.cljindex 5861be4478e..a5947cc18e3 100644--- a/src/metabase/metabot/tools/shared.clj+++ b/src/metabase/metabot/tools/shared.clj@@ -51,13 +51,13 @@(get-in (current-memory) [:state :charts] {}))(defn current-client-ids- "Ids of the queries and charts seeded from this request's viewing context, as opposed to- written by the agent's own tools. A refusal to present one of these is a real access attempt- and gets the audited treatment; see `metabase.metabot.tools.shared.content-store`. Kept in state- rather than per request, since a tool can persist one of these queries and a later turn read it- back after it has left the viewing context."+ "Ids of the queries and charts the client supplied, as opposed to ones the agent's tools wrote.+ They come from this request's viewing context, plus the ones earlier turns stored. A refusal to+ present one of these is a real access attempt and gets the audited treatment; see+ `metabase.metabot.tools.shared.content-store`."[]- (get-in (current-memory) [:state :client-ids] #{}))+ (let [memory (current-memory)]+ (into (set (get-in memory [:state :client-ids])) (:client-ids memory))))(defn current-chart-configs-state"Returns the current chart-configs state map from agent memory.diff --git a/test/metabase/metabot/agent/core_test.clj b/test/metabase/metabot/agent/core_test.cljindex 88b5277565d..0c3b050c13e 100644--- a/test/metabase/metabot/agent/core_test.clj+++ b/test/metabase/metabot/agent/core_test.clj@@ -1123,6 +1123,24 @@(is (=? {chart-configs-key chart-config}chart-configs)))))+(deftest viewing-context-item-id-persists-once-stored-test+ (let [query {:database 1 :type :query :query {:source-table 1}}+ agent (#'agent/init-agent {:profile-id :internal+ :context {:user_is_viewing [{:type "adhoc" :query query}]}})+ memory @(:memory-atom agent)+ id (get-in agent [:context :user_is_viewing 0 :id])]+ (testing "a turn that stores nothing under an ad hoc item's minted id persists no state"+ (is (nil? (memory/turn-state memory))))+ (testing "a chart created from the item's query persists the id with the query"+ (is (= #{id}+ (-> (#'agent/update-memory memory [{:type :tool-output+ :result {:structured-output {:chart-id "chart-1"+ :query-id id+ :query query+ :chart-type :bar}}}])+ memory/turn-state+ :client-ids))))))+;;; ──────────────────────────────────────────────────────────────────;;; Profile permission checks;;; ──────────────────────────────────────────────────────────────────diff --git a/test/metabase/metabot/agent/memory_test.clj b/test/metabase/metabot/agent/memory_test.cljindex 9b17bf6a15e..10cfe92a06c 100644--- a/test/metabase/metabot/agent/memory_test.clj+++ b/test/metabase/metabot/agent/memory_test.clj@@ -103,13 +103,23 @@(is (= #{"seeded-q"}(get-in (memory/initialize [] persisted) [:state :client-ids]))))))-(deftest ^:parallel add-client-ids-test- (testing "ids accumulate across turns and are written to the persisted delta, so a query a tool- stored still audits its refusals once it has left the viewing context"- (let [memory (-> (memory/initialize [] {:client-ids #{"from-an-earlier-turn"}})- (memory/add-client-ids #{"seeded-now"}))]- (is (= #{"from-an-earlier-turn" "seeded-now"} (get-in memory [:state :client-ids])))- (is (= #{"from-an-earlier-turn" "seeded-now"} (get-in memory [:turn-state :client-ids]))))))+(deftest ^:parallel client-ids-persist-once-stored-test+ (let [query {:database 1}+ earlier (memory/initialize [] {:client-ids #{"from-an-earlier-turn"}})+ memory (memory/add-client-ids earlier #{"from-an-earlier-turn" "stored-now" "only-viewed"})]+ (testing "tools see this turn's ids along with the ones earlier turns persisted"+ (binding [shared/*memory-atom* (atom (memory/add-client-ids earlier #{"stored-now" "only-viewed"}))]+ (is (= #{"from-an-earlier-turn" "stored-now" "only-viewed"} (shared/current-client-ids)))))+ (testing "a turn that stores nothing persists no ids"+ (is (nil? (memory/turn-state memory))))+ (testing "storing a query or chart under an id persists it with earlier turns' ids"+ (doseq [store [#(memory/set-query % "stored-now" query)+ #(memory/set-chart % "stored-now" {:chart_id "stored-now"})]]+ (is (= #{"from-an-earlier-turn" "stored-now"}+ (:client-ids (memory/turn-state (store memory)))))))+ (testing "storing under an id an earlier turn persisted leaves the client ids unwritten"+ (is (= {:queries {"from-an-earlier-turn" query}}+ (memory/turn-state (memory/set-query memory "from-an-earlier-turn" query)))))))(deftest ^:parallel set-link-registry-test(testing "an empty or unchanged registry is not written into the turn delta"