Skip to content

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

Strip surrounding double-quotes from source-card/cross-stage column names (#74731)

Mihael Konjevićd59dc0499a12
diff --git a/src/metabase/agent_lib/representations/repair.clj b/src/metabase/agent_lib/representations/repair.clj
index baad284806c..2a87e3a32d1 100644
--- a/src/metabase/agent_lib/representations/repair.clj
+++ b/src/metabase/agent_lib/representations/repair.clj
@@ -1972,17 +1972,60 @@
(dec stage-idx) stage-idx)
nil)))
+(defn- strip-surrounding-double-quotes
+ "If `s` is wrapped in a matched pair of leading/trailing double-quote characters, return the
+ inner string; otherwise return `s` unchanged.
+
+ LLMs sometimes quote a cross-stage / source-card column name the way they'd quote a SQL
+ identifier - e.g. `\"app_id\"` - but those quotes are not part of the column's actual name.
+ Left in place they make the name match no source column, so the [[maybe-fill-cross-stage-types]]
+ pass can't infer `base-type`, the resolver passes the typeless string-named ref through
+ unvalidated (production has Malli instrumentation off), and the FE later crashes computing
+ display info for it."
+ [s]
+ (or (when (string? s)
+ (second (re-matches #"\"(.*)\"" s)))
+ s))
+
+(defn- match-cross-stage-column
+ "Find the canonical column entry in `name->types` for `col-name`. Returns `[canonical-name
+ types]` or `nil` when no column matches.
+
+ Prefers an exact match. Falls back to the double-quote-stripped name, but only when stripping
+ actually resolves to a real column - so a column whose name legitimately contains surrounding
+ quotes (vanishingly rare) is never clobbered, and unmatched names are left for the resolver to
+ report with a better message."
+ [name->types col-name]
+ (cond
+ (contains? name->types col-name)
+ [col-name (get name->types col-name)]
+
+ :else
+ (let [stripped (strip-surrounding-double-quotes col-name)]
+ (when (and (not= stripped col-name)
+ (contains? name->types stripped))
+ [stripped (get name->types stripped)]))))
+
(defn- maybe-fill-cross-stage-types
- "Given a cross-stage field clause and a name→types map, return the clause with `base-type`
- / `effective-type` filled in (when missing and known)."
+ "Given a cross-stage field clause and a name→types map, return the clause with its column name
+ canonicalised (surrounding double-quotes stripped when that is what makes it match a real
+ column) and `base-type` / `effective-type` filled in (when missing and known).
+
+ Idempotent: once the name is canonical it matches exactly on the next pass (no rename), and
+ once `base-type` is present it is left alone."
[clause name->types]
- (let [opts (nth clause 1)
+ (let [opts (nth clause 1)
col-name (nth clause 2)]
- (if (contains? opts "base-type")
- clause
- (if-let [types (get name->types col-name)]
- (assoc clause 1 (merge opts types))
- clause))))
+ (if-let [[canonical-name types] (match-cross-stage-column name->types col-name)]
+ (cond-> clause
+ ;; Canonicalise the name when quote-stripping was needed to match. A bare `base-type`
+ ;; stamp without this would leave the ref pointing at a non-existent column.
+ (not= canonical-name col-name)
+ (assoc 2 canonical-name)
+ ;; Stamp inferred types only when the LLM didn't author a `base-type` already.
+ (not (contains? opts "base-type"))
+ (assoc 1 (merge opts types)))
+ clause)))
(defn- infer-cross-stage-field-types-in-stage
"Walk one stage and stamp inferred types into every string-named field reference that lacks
diff --git a/test/metabase/agent_lib/representations/repair_test.clj b/test/metabase/agent_lib/representations/repair_test.clj
index 74e74643f24..162d7b9f089 100644
--- a/test/metabase/agent_lib/representations/repair_test.clj
+++ b/test/metabase/agent_lib/representations/repair_test.clj
@@ -1746,12 +1746,49 @@
opts (get-in out ["stages" 1 "filters" 0 2 1])]
(is (not (contains? opts "base-type"))))))
+(deftest cross-stage-field-type-strips-surrounding-double-quotes-test
+ (testing (str "BOT-1587: when the LLM quotes a cross-stage column name like a SQL identifier\n"
+ "(`\"count\"` instead of `count`), repair strips the surrounding double-quotes so\n"
+ "the name matches the previous stage's output, rewrites the ref to the canonical\n"
+ "name, and stamps `base-type`. Without this the typeless, mis-named ref reaches\n"
+ "the FE and crashes display-info calculation.")
+ (let [q (assoc-in multi-stage-base-query
+ ["stages" 1 "filters" 0 2 2] "\"count\"")
+ out (repair/repair mp-fks q)
+ field-clause (get-in out ["stages" 1 "filters" 0 2])
+ opts (nth field-clause 1)]
+ (testing "name canonicalised (quotes stripped)"
+ (is (= "count" (nth field-clause 2))))
+ (testing "base-type / effective-type stamped from the previous stage"
+ (is (= "type/Integer" (get opts "base-type")))
+ (is (= "type/Integer" (get opts "effective-type")))))))
+
+(deftest cross-stage-field-type-unmatched-quoted-name-left-alone-test
+ (testing (str "Quote-stripping only rewrites when the stripped name matches a real column.\n"
+ "A quoted name whose stripped form still isn't produced by the previous stage is\n"
+ "left verbatim (the resolver surfaces the real error with a better message).")
+ (let [q (assoc-in multi-stage-base-query
+ ["stages" 1 "filters" 0 2 2] "\"no_such_column\"")
+ out (repair/repair mp-fks q)
+ field-clause (get-in out ["stages" 1 "filters" 0 2])
+ opts (nth field-clause 1)]
+ (is (= "\"no_such_column\"" (nth field-clause 2)) "name left untouched")
+ (is (not (contains? opts "base-type"))))))
+
(deftest cross-stage-field-type-idempotent-test
(testing "cross-stage field-type inference is idempotent"
(let [once (repair/repair mp-fks multi-stage-base-query)
twice (repair/repair mp-fks once)]
(is (= once twice)))))
+(deftest cross-stage-field-type-quoted-name-idempotent-test
+ (testing "the quote-stripping rewrite is also idempotent"
+ (let [q (assoc-in multi-stage-base-query
+ ["stages" 1 "filters" 0 2 2] "\"count\"")
+ once (repair/repair mp-fks q)
+ twice (repair/repair mp-fks once)]
+ (is (= once twice)))))
+
(deftest cross-stage-field-type-end-to-end-resolve-test
(testing (str "End-to-end: a multi-stage YAML with a stage-1 cross-stage ref lacking\n"
"base-type is repaired and then `resolve-query` + `lib/query` accept the\n"
diff --git a/test/metabase/metabot/tools/construct_representations_test.clj b/test/metabase/metabot/tools/construct_representations_test.clj
index 34370dc6e35..6138c96fefc 100644
--- a/test/metabase/metabot/tools/construct_representations_test.clj
+++ b/test/metabase/metabot/tools/construct_representations_test.clj
@@ -741,3 +741,71 @@
(is false "expected throw")
(catch clojure.lang.ExceptionInfo e
(is (true? (:agent-error? (ex-data e))))))))))
+
+;;; ============================================================
+;;; Quoted source-card column names are canonicalised before type inference (BOT-1587)
+;;; ============================================================
+
+(deftest source-card-quoted-column-name-canonicalized-test
+ (testing
+ (str "Regression (BOT-1587): the LLM sometimes references a `source-card:` column by a name\n"
+ "that doesn't EXACTLY match a column the card returns - it quotes the name the way it\n"
+ "would a SQL identifier, e.g. `[\"field\" {} \"\\\"TOTAL\\\"\"]` instead of\n"
+ "`[\"field\" {} \"TOTAL\"]`. Without canonicalisation the `infer-source-card-field-types*`\n"
+ "repair pass can't match a column, leaves `base-type` off, and (since `resolve-query`\n"
+ "only transforms and production runs with Malli instrumentation off) the typeless\n"
+ "string-named ref is stored on the card - then the FE crashes computing display info\n"
+ "(`unable to determine the type of [\"field\" {} \"\\\"TOTAL\\\"\"]`).\n\n"
+ "The repair pass now strips surrounding double-quotes when that makes the name match a\n"
+ "real column, rewrites the ref to the canonical name, and stamps `base-type`.")
+ (let [quoted-name "\"TOTAL\"" ; the literal value the LLM emitted: TOTAL wrapped in quotes
+ query (query-data
+ {"lib/type" "mbql/query"
+ "database" "Sample"
+ "stages" [{"lib/type" "mbql.stage/mbql"
+ "source-card" card-entity-id
+ ;; quoted name in both a filter and the field selection
+ "filters" [["=" {} ["field" {} quoted-name] 100]]
+ "fields" [["field" {} quoted-name]]}]})]
+ (with-card-mp-and-stubs!
+ (fn []
+ ;; No throw under default instrumentation - the canonicalised ref is now schema-valid.
+ (let [result (construct/execute-representations-query query)
+ q (get-in result [:structured-output :query])
+ fields-ref (get-in q [:stages 0 :fields 0])
+ filter-clause (get-in q [:stages 0 :filters 0])
+ filter-ref (nth filter-clause 2)
+ exported (get-in result [:structured-output :query-json])
+ exported-ref (get-in exported ["stages" 0 "fields" 0])]
+ (testing "field-selection ref: quotes stripped + base-type stamped"
+ (is (= :field (first fields-ref)))
+ (is (= "TOTAL" (nth fields-ref 2)) "surrounding double-quotes stripped to the canonical name")
+ (is (= :type/Float (get-in fields-ref [1 :base-type]))))
+ (testing "filter ref: same canonicalisation applies to nested clauses"
+ (is (= "TOTAL" (nth filter-ref 2)))
+ (is (= :type/Float (get-in filter-ref [1 :base-type]))))
+ (testing "exported query-json carries the canonical name and base-type"
+ (is (= "TOTAL" (nth exported-ref 2)))
+ (is (= "type/Float" (get-in exported-ref [1 "base-type"]))))))))))
+
+(deftest source-card-correct-column-name-stamps-base-type-test
+ (testing
+ (str "Control for `source-card-quoted-column-name-canonicalized-test`: the identical query\n"
+ "with the *exact* column name `TOTAL` (no surrounding quotes) resolves cleanly and the\n"
+ "exported field ref carries `base-type` from the card's result-metadata. Confirms the\n"
+ "canonicalisation pass leaves already-correct names untouched.")
+ (with-card-mp-and-stubs!
+ (fn []
+ (let [result (construct/execute-representations-query
+ (query-data
+ {"lib/type" "mbql/query"
+ "database" "Sample"
+ "stages" [{"lib/type" "mbql.stage/mbql"
+ "source-card" card-entity-id
+ "filters" [["=" {} ["field" {} "TOTAL"] 100]]
+ "fields" [["field" {} "TOTAL"]]}]}))
+ exported (get-in result [:structured-output :query-json])
+ field-ref (get-in exported ["stages" 0 "fields" 0])
+ opts (nth field-ref 1)]
+ (is (= "TOTAL" (nth field-ref 2)))
+ (is (= "type/Float" (get opts "base-type"))))))))