codeant-ai-for-open-source[bot] commented on code in PR #44303:
URL: https://github.com/apache/superset/pull/44303#discussion_r4013130237


##########
superset/dashboards/layout.py:
##########
@@ -0,0 +1,101 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""Structural repair for a dashboard's ``position_json``."""
+
+from __future__ import annotations
+
+import logging
+from typing import Any
+
+logger = logging.getLogger(__name__)
+
+ROOT_ID = "ROOT_ID"
+# ``HEADER_ID`` is dashboard metadata rather than a rendered child, and a
+# dashboard with top-level tabs keeps an empty, detached ``GRID_ID``. Both are
+# unreachable by design. Mirrors the reserved ids in the frontend's
+# ``removeUnreachableComponents``.
+RESERVED_IDS = frozenset({ROOT_ID, "GRID_ID", "HEADER_ID"})
+
+
+def remove_unreachable_components(
+    position: dict[str, Any],
+) -> tuple[dict[str, Any], list[str]]:
+    """Drop layout components that cannot be reached from ``ROOT_ID``.
+
+    Superset renders only what hangs off ``ROOT_ID``, so a detached component 
is
+    already invisible. It still survives in ``position_json``, where code that
+    walks every entry or trusts a node's stale ``parents`` can find it โ€” a
+    detached subtree holding a cycle is what crashes the filter scope modal 
with
+    "Maximum call stack size exceeded". Dropping such a subtree also releases 
any
+    chart trapped inside: the dashboard keeps the chart in ``slices``, so the
+    frontend places it back into the layout on the next load.
+
+    Returns the (possibly unchanged) position and the ids that were removed.
+    Non-dict entries such as ``DASHBOARD_VERSION_KEY`` are never removed.
+    """
+    if not isinstance(position, dict) or not isinstance(position.get(ROOT_ID), 
dict):
+        return position, []
+
+    reachable: set[str] = set()
+    stack: list[str] = [ROOT_ID]
+    while stack:
+        component_id = stack.pop()
+        # doubles as the cycle guard: an id already seen is never expanded 
twice
+        if component_id in reachable:
+            continue
+        reachable.add(component_id)
+        component = position.get(component_id)
+        if isinstance(component, dict):
+            for child_id in component.get("children") or []:
+                if child_id in position:

Review Comment:
   **Suggestion:** A valid JSON layout can contain an object or array in 
`children`; membership testing that value in `position` raises `TypeError` and 
aborts dashboard writes. [type error]
   
   **Assessment:** ๐ŸŸ  `Major` ยท ๐Ÿ” `Occurrence: Rarely`
   
   [![Use CodeAnt 
Skill](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/use-codeant-skill-flat-v2.svg)](https://docs.codeant.ai/cli/resolve-pr-comments-skill)
 [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=0ac31d9f830448b4aedd962eda4bef2a&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=0ac31d9f830448b4aedd962eda4bef2a&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   <details>
   <summary><b>Prompt for AI Agent ๐Ÿค– </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/dashboards/layout.py
   **Line:** 64:65
   **Comment:**
        *Type Error: A valid JSON layout can contain an object or array in 
`children`; membership testing that value in `position` raises `TypeError` and 
aborts dashboard writes.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F44303&comment_hash=3640dcbac84b3c357a8537a91154a1522d254af40e86f67fd956aa156ec43682&reaction=like'>๐Ÿ‘</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F44303&comment_hash=3640dcbac84b3c357a8537a91154a1522d254af40e86f67fd956aa156ec43682&reaction=dislike'>๐Ÿ‘Ž</a>



##########
superset-frontend/src/dashboard/actions/hydrate.ts:
##########
@@ -133,11 +134,14 @@ export const hydrateDashboard =
     // new dash: position_json could be {} or null
     // getEmptyLayout() includes a version string entry plus BasicLayoutItem 
entries
     // which lack the `meta` field; layout is mutated below to add full 
LayoutItem entries
-    const layout = (
-      positionData && Object.keys(positionData).length > 0
+    // Detached components are dropped before anything indexes the layout: they
+    // never render, but a detached cycle crashes the filter scope modal, and a
+    // chart trapped in one is neither visible nor eligible for re-adding 
below.
+    const layout = removeUnreachableComponents(
+      (positionData && Object.keys(positionData).length > 0
         ? positionData
-        : getEmptyLayout()
-    ) as Record<string, LayoutItem | DashboardEntity>;
+        : getEmptyLayout()) as Record<string, LayoutItem | DashboardEntity>,
+    );

Review Comment:
   **Suggestion:** `removeUnreachableComponents` assumes `ROOT_ID.children` is 
an array; malformed position data makes hydration throw before it can load the 
dashboard. [null pointer]
   
   **Assessment:** ๐ŸŸ  `Major` ยท ๐Ÿ” `Occurrence: Sometimes`
   
   [![Use CodeAnt 
Skill](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/use-codeant-skill-flat-v2.svg)](https://docs.codeant.ai/cli/resolve-pr-comments-skill)
 [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=4d8a5441c5d34a1387a35cbc88a9521c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=4d8a5441c5d34a1387a35cbc88a9521c&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   <details>
   <summary><b>Prompt for AI Agent ๐Ÿค– </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset-frontend/src/dashboard/actions/hydrate.ts
   **Line:** 140:144
   **Comment:**
        *Null Pointer: `removeUnreachableComponents` assumes `ROOT_ID.children` 
is an array; malformed position data makes hydration throw before it can load 
the dashboard.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F44303&comment_hash=15be747ac5eee1d2af8cc2d01a89dd1b0959258cdee79e47ba489a3f8d372797&reaction=like'>๐Ÿ‘</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F44303&comment_hash=15be747ac5eee1d2af8cc2d01a89dd1b0959258cdee79e47ba489a3f8d372797&reaction=dislike'>๐Ÿ‘Ž</a>



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to