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


##########
superset/mcp_service/dashboard/tool/duplicate_dashboard.py:
##########
@@ -0,0 +1,376 @@
+# 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.
+
+"""
+MCP tool: duplicate_dashboard
+
+Duplicates an existing dashboard, optionally deep-copying its charts.
+Canonical workflow: clone a template dashboard, then edit the copy
+(e.g. to create a regional or staging variant).
+"""
+
+import logging
+from typing import Any
+
+from fastmcp import Context
+from sqlalchemy.exc import SQLAlchemyError
+from superset_core.mcp.decorators import tool, ToolAnnotations
+
+from superset.extensions import event_logger
+from superset.mcp_service.dashboard.schemas import (
+    _sanitize_dashboard_info_for_llm_context,
+    DashboardInfo,
+    DuplicateDashboardRequest,
+    DuplicateDashboardResponse,
+    serialize_chart_summary,
+)
+from superset.mcp_service.privacy import user_can_view_data_model_metadata
+from superset.mcp_service.utils.url_utils import get_superset_base_url
+from superset.utils import json
+
+logger = logging.getLogger(__name__)
+
+
+def _positions_reference_charts(positions: dict[str, Any]) -> bool:
+    """Return whether a layout maps any chart into the dashboard.
+
+    ``DashboardDAO.set_dash_metadata`` rebuilds the new dashboard's slice
+    list solely from the chart IDs found in ``positions``, so a layout
+    with no ``CHART`` entries yields an empty dashboard regardless of the
+    source's ``slices`` relationship.
+    """
+    return any(
+        isinstance(value, dict)
+        and value.get("type") == "CHART"
+        and value.get("meta", {}).get("chartId")
+        for value in positions.values()
+    )
+
+
+def _build_copy_payload(
+    source: Any, dashboard_title: str, duplicate_slices: bool
+) -> tuple[dict[str, Any], bool]:
+    """Build the data payload expected by ``CopyDashboardCommand``.
+
+    Mirrors what the frontend "Save as" flow sends to the
+    ``/api/v1/dashboard/<id>/copy/`` endpoint: the source dashboard's
+    current ``json_metadata`` with a ``positions`` key holding the current
+    layout (``position_json``). ``DashboardCopySchema`` requires
+    ``json_metadata``, and ``DashboardDAO.copy_dashboard`` reads
+    ``positions`` from it to remap chart IDs when ``duplicate_slices``
+    is enabled.
+
+    Returns the payload and a flag indicating whether the layout maps any
+    chart, so the caller can refuse to produce a silently empty copy.
+    """
+    try:
+        metadata = json.loads(source.json_metadata or "{}")
+    except (json.JSONDecodeError, TypeError):
+        metadata = {}
+    if not isinstance(metadata, dict):
+        metadata = {}
+
+    try:
+        positions = json.loads(source.position_json or "{}")
+    except (json.JSONDecodeError, TypeError):
+        positions = {}
+    if not isinstance(positions, dict):
+        positions = {}
+
+    metadata["positions"] = positions
+
+    payload = {
+        "dashboard_title": dashboard_title,
+        "css": source.css,
+        "duplicate_slices": duplicate_slices,
+        "json_metadata": json.dumps(metadata),
+    }
+    return payload, _positions_reference_charts(positions)
+
+
+def _serialize_new_dashboard(dashboard: Any) -> tuple[DashboardInfo, str]:
+    """Build the response ``DashboardInfo`` and URL for the new dashboard."""
+    from superset.mcp_service.dashboard.schemas import serialize_tag_object
+
+    dashboard_url = 
f"{get_superset_base_url()}/superset/dashboard/{dashboard.id}/"
+    include_data_model_metadata = user_can_view_data_model_metadata()
+    info = DashboardInfo(
+        id=dashboard.id,
+        dashboard_title=dashboard.dashboard_title,
+        slug=dashboard.slug,
+        description=dashboard.description,
+        published=dashboard.published,
+        created_on=dashboard.created_on,
+        changed_on=dashboard.changed_on,
+        uuid=str(dashboard.uuid) if dashboard.uuid else None,
+        url=dashboard_url,
+        chart_count=len(dashboard.slices),
+        tags=[
+            obj
+            for tag in getattr(dashboard, "tags", [])
+            if (obj := serialize_tag_object(tag)) is not None
+        ],
+        charts=[
+            obj
+            for chart in getattr(dashboard, "slices", [])
+            if (
+                obj := serialize_chart_summary(
+                    chart,
+                    include_data_model_metadata=include_data_model_metadata,
+                )
+            )
+            is not None
+        ],
+    )
+    return _sanitize_dashboard_info_for_llm_context(info), dashboard_url
+
+
+def _safe_rollback(context_label: str) -> None:
+    """Roll back the current DB session, swallowing rollback failures.
+
+    A failed operation can leave the shared session in an invalid
+    transaction state; rolling back keeps later ORM use in the same request
+    lifecycle from inheriting the broken transaction.
+    """
+    from superset import db
+
+    try:
+        db.session.rollback()  # pylint: disable=consider-using-transaction
+    except SQLAlchemyError:
+        logger.warning(
+            "Database rollback failed during %s error handling",
+            context_label,
+            exc_info=True,
+        )
+
+
+def _refetch_and_serialize(
+    new_dashboard: Any, dashboard_title: str
+) -> tuple[DashboardInfo, str]:
+    """Re-fetch the new dashboard with eager-loaded relationships.
+
+    The eager load avoids lazy-loading on a session the command's commit may
+    have invalidated. If the re-fetch fails, the failed transaction is rolled
+    back and a minimal response is returned instead.
+    """
+    from sqlalchemy.orm import subqueryload
+
+    from superset.daos.dashboard import DashboardDAO
+    from superset.models.dashboard import Dashboard
+    from superset.models.slice import Slice
+
+    try:
+        dashboard = (
+            DashboardDAO.find_by_id(
+                new_dashboard.id,
+                query_options=[
+                    subqueryload(Dashboard.slices).subqueryload(Slice.tags),
+                    subqueryload(Dashboard.tags),
+                ],
+            )
+            or new_dashboard
+        )
+        return _serialize_new_dashboard(dashboard)
+    except SQLAlchemyError:
+        logger.warning(
+            "Re-fetch of dashboard %s failed; returning minimal response",
+            new_dashboard.id,
+            exc_info=True,
+        )
+        _safe_rollback("dashboard re-fetch")
+        dashboard_url = (
+            f"{get_superset_base_url()}/superset/dashboard/{new_dashboard.id}/"
+        )
+        info = _sanitize_dashboard_info_for_llm_context(
+            DashboardInfo(
+                id=new_dashboard.id,
+                dashboard_title=dashboard_title,
+                url=dashboard_url,
+            )
+        )
+        return info, dashboard_url
+
+
+async def _resolve_source(
+    request: DuplicateDashboardRequest, ctx: Context
+) -> tuple[Any, DuplicateDashboardResponse | None]:
+    """Resolve and authorize the source dashboard.
+
+    Returns ``(source, None)`` on success, or ``(None, error_response)`` when
+    the dashboard is missing or inaccessible.
+    """
+    from superset.commands.dashboard.exceptions import (
+        DashboardAccessDeniedError,
+        DashboardNotFoundError,
+    )
+    from superset.daos.dashboard import DashboardDAO
+
+    with event_logger.log_context(action="mcp.duplicate_dashboard.lookup"):
+        try:
+            return DashboardDAO.get_by_id_or_slug(str(request.dashboard_id)), 
None
+        except DashboardNotFoundError:
+            await ctx.warning(
+                "Dashboard not found for duplication: dashboard_id=%s"
+                % (request.dashboard_id,)
+            )
+            return None, DuplicateDashboardResponse(
+                error=(
+                    f"Dashboard '{request.dashboard_id}' not found. "
+                    "Use list_dashboards to get valid dashboard IDs."
+                ),
+            )
+        except DashboardAccessDeniedError:
+            await ctx.warning(
+                "Dashboard access denied for duplication: dashboard_id=%s"
+                % (request.dashboard_id,)
+            )
+            return None, DuplicateDashboardResponse(
+                error=(
+                    f"You don't have access to dashboard "
+                    f"'{request.dashboard_id}', so it cannot be duplicated."
+                ),
+            )

Review Comment:
   **Suggestion:** Database lookup failures while resolving the source 
dashboard are not handled as structured tool errors. If 
`DashboardDAO.get_by_id_or_slug` raises `SQLAlchemyError` (for example, 
transient DB/session failures), the exception falls through to the outer 
`except Exception` and is re-raised, causing a hard MCP tool failure instead of 
a normal `DuplicateDashboardResponse`. Catch operational DB errors in source 
resolution (or convert the outer catch to a structured response) so callers 
always get a contract-consistent error payload. [api mismatch]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ `duplicate_dashboard` MCP tool crashes on DB lookup failures.
   - ⚠️ MCP clients receive internal errors, not structured error payloads.
   - ⚠️ LLM workflows must special-case unexpected tool failures.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Start Superset with the MCP service enabled and ensure the MCP dashboard 
tools module
   is loaded; the `duplicate_dashboard` tool is exported in
   `superset/mcp_service/dashboard/tool/__init__.py:18-32` and implemented in
   `superset/mcp_service/dashboard/tool/duplicate_dashboard.py:259-372` (per PR 
diff).
   
   2. From an MCP client, invoke the `duplicate_dashboard` tool with any 
valid-looking
   payload (for example, `{"dashboard_id": 1, "dashboard_title": "Copy"}`), 
which routes into
   `duplicate_dashboard()` at
   `superset/mcp_service/dashboard/tool/duplicate_dashboard.py:259-272`, where 
the first
   operation inside the `try` block is `source, error_response = await
   _resolve_source(request, ctx)` at lines 276-279.
   
   3. Inside `_resolve_source()` at
   `superset/mcp_service/dashboard/tool/duplicate_dashboard.py:208-246`, the 
code enters the
   `with event_logger.log_context(...):` block and executes
   `DashboardDAO.get_by_id_or_slug(str(request.dashboard_id))` at line 224. 
This DAO method,
   implemented in `superset/daos/dashboard.py:17-33`, issues a
   `db.session.query(Dashboard)...one_or_none()`. If the database is 
temporarily unavailable
   or the session is broken (e.g., connection loss, transaction error), 
SQLAlchemy can raise
   an `SQLAlchemyError`/`OperationalError` from this call.
   
   4. Because `_resolve_source()` only catches `DashboardNotFoundError` and
   `DashboardAccessDeniedError` (lines 225-246) and does not catch 
`SQLAlchemyError`, the
   database error propagates back into `duplicate_dashboard()`, where it is 
caught by the
   broad `except Exception as exc:` block at
   `superset/mcp_service/dashboard/tool/duplicate_dashboard.py:371-376`. That 
block logs via
   `await ctx.error(...)` but then `raise`s the exception instead of returning a
   `DuplicateDashboardResponse`, causing the MCP tool invocation to fail with 
an unstructured
   internal error rather than the contract-consistent 
`DuplicateDashboardResponse(error=...)`
   that all other failure paths in this tool return.
   ```
   </details>
   
   [![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=ef7debd271b7480f81913379469af06e&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=ef7debd271b7480f81913379469af06e&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/mcp_service/dashboard/tool/duplicate_dashboard.py
   **Line:** 223:246
   **Comment:**
        *Api Mismatch: Database lookup failures while resolving the source 
dashboard are not handled as structured tool errors. If 
`DashboardDAO.get_by_id_or_slug` raises `SQLAlchemyError` (for example, 
transient DB/session failures), the exception falls through to the outer 
`except Exception` and is re-raised, causing a hard MCP tool failure instead of 
a normal `DuplicateDashboardResponse`. Catch operational DB errors in source 
resolution (or convert the outer catch to a structured response) so callers 
always get a contract-consistent error payload.
   
   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%2F40959&comment_hash=64ffb573c273883357e41947d62d9fdf51ffe5f717cfb0fa6ed1f22c2ab48907&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40959&comment_hash=64ffb573c273883357e41947d62d9fdf51ffe5f717cfb0fa6ed1f22c2ab48907&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