Skip to content

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

Make representations repair idempotent for unwrapped boolean wrappers (#75304)

Nemanja Glumacc33fe8afee73
diff --git a/src/metabase/agent_lib/representations/repair.clj b/src/metabase/agent_lib/representations/repair.clj
index e76c7d8d8dc..45f71f21131 100644
--- a/src/metabase/agent_lib/representations/repair.clj
+++ b/src/metabase/agent_lib/representations/repair.clj
@@ -716,7 +716,11 @@
(let [head (u/lower-case-en (nth node 0))
x (nth node 2)]
(case head
- "true" x
+ ;; Always emit a vector so we don't expose a bare scalar (`["true" {} x]` -> `x`) that a
+ ;; sole-element parent would turn into an un-optioned clause `[x]`, which a later `repair`
+ ;; pass would then "fix" to `[x {}]` - breaking idempotency. A wrapped clause is returned
+ ;; as-is; a wrapped scalar becomes a clause with an empty options map.
+ "true" (if (vector? x) x [x {}])
"false" ["not" {} x])))
(defn- unwrap-boolean-wrappers*
diff --git a/test/metabase/agent_lib/representations/repair_test.clj b/test/metabase/agent_lib/representations/repair_test.clj
index b7e64dbeaa9..36c8a835299 100644
--- a/test/metabase/agent_lib/representations/repair_test.clj
+++ b/test/metabase/agent_lib/representations/repair_test.clj
@@ -1172,6 +1172,20 @@
twice (repair/repair trivial-mp once)]
(is (= once twice)))))
+(deftest ^:parallel idempotency-unwrapped-boolean-wrapper-test
+ (testing "unwrapping a boolean wrapper that is the sole element of a parent vector stays idempotent"
+ ;; Regression for a non-idempotency the generative idempotency-property-test surfaced:
+ ;; `["true" {} x]` unwraps to `x`. For a scalar `x`, `unwrap-boolean-wrapper` emits a clause
+ ;; `[x {}]` rather than the bare scalar, so a sole-element parent stays `[[x {}]]` instead of
+ ;; collapsing to `[x]` - which a later `repair` pass would otherwise "fix" to `[x {}]`, breaking
+ ;; the fixed point. Any non-blank scalar triggered it (the shrunk counterexample minimised `x`
+ ;; to a single NUL char).
+ (let [once (repair/repair trivial-mp [["true" {} "x"]])]
+ (is (= [["x" {}]] once)
+ "the wrapped scalar is emitted as a clause, leaving the parent vector intact")
+ (is (= once (repair/repair trivial-mp once))
+ "and the result is a fixed point"))))
+
;;; Property-based fuzz: randomly-shaped inputs go through repair twice and must equal on pass 2.
(def ^:private gen-scalar