Skip to main content

msh ai fix

Use AI to suggest fixes for broken or failing assets.

Usage

msh ai fix <asset_path> [--apply] [--json] [--error <error_message>]

Description

Analyzes a broken asset and suggests fixes:

  • Parses error messages (if provided)
  • Analyzes asset structure
  • Generates JSON patch with suggested fixes
  • Optionally applies fixes automatically

Options

  • --apply: Apply the suggested patch after user confirmation
  • --json: Return patch as JSON structure
  • --error <error_message>: Provide error message from failed run

Examples

Suggest Fixes (Preview Only)

msh ai fix assets/revenue.msh

Example Output:

Asset: revenue

Suggested Fixes:
1. Column 'customer_id' not found
→ Fix: Change 'customer_id' to 'customerId' in transform SQL

2. Missing WHERE clause causing performance issue
→ Fix: Add WHERE clause to filter recent data

Patch Preview:
--- assets/revenue.msh
+++ assets/revenue.msh
@@ -10,7 +10,7 @@
transform: |
SELECT
- customer_id,
+ customerId as customer_id,
DATE_TRUNC('month', order_date) as month,
SUM(amount) as monthly_revenue
FROM {{ source }}
+ WHERE order_date >= CURRENT_DATE - INTERVAL '1 year'
GROUP BY customer_id, DATE_TRUNC('month', order_date)

Apply these fixes? (y/n):

Suggest Fixes with Error Context

msh ai fix assets/revenue.msh --error "Column 'customer_id' not found"

Provides error context to AI for more accurate fixes.

Apply Fixes Automatically

msh ai fix assets/revenue.msh --apply

Applies fixes automatically after showing preview.

Get Patch as JSON

msh ai fix assets/revenue.msh --json

Example JSON Output:

{
"patches": [
{
"file_path": "assets/revenue.msh",
"diff": "--- assets/revenue.msh\n+++ assets/revenue.msh\n...",
"operations": [
{
"op": "replace",
"path": "/transform",
"value": "SELECT customerId as customer_id, ..."
},
{
"op": "add",
"path": "/transform",
"value": "WHERE order_date >= CURRENT_DATE - INTERVAL '1 year'"
}
]
}
]
}

Patch Format

Fixes are provided as JSON Patch (RFC 6902) with file-level operations:

{
"patches": [{
"file_path": "assets/revenue.msh",
"diff": "unified diff string",
"operations": [
{"op": "replace", "path": "/transform", "value": "fixed SQL"},
{"op": "add", "path": "/tests", "value": [{"assert": "..."}]}
]
}]
}

Operations:

  • add - Add value at path
  • remove - Remove value at path
  • replace - Replace value at path

Safety Validation

The safety layer validates patches before applying:

  • Blocks dangerous operations (DROP TABLE, TRUNCATE, etc.)
  • Validates SQL syntax
  • Checks for policy violations

If a patch contains dangerous operations, it's blocked.

Prerequisites

  1. AI Configuration: Must configure AI provider first

    msh config ai --provider openai --model gpt-4 --api-key env:OPENAI_API_KEY
  2. Manifest: Generate manifest before using AI commands

    msh manifest
  3. Error Context (optional): Provide error message for better fixes

Use Cases

  • Fix Broken Assets: Quickly fix assets that fail to run
  • Address Review Feedback: Apply suggestions from msh ai review
  • Refactor Code: Improve asset structure based on errors

Reviewing Before Applying

Always review suggested fixes before applying:

  1. Preview changes: Review the diff
  2. Understand fixes: Make sure fixes make sense
  3. Test after applying: Run msh run to verify fixes work

Applying Patches

You can apply patches in two ways:

  1. Interactive: Use --apply flag for interactive confirmation

    msh ai fix assets/revenue.msh --apply
  2. Manual: Save patch to file and apply manually

    msh ai fix assets/revenue.msh --json > patch.json
    msh ai apply patch.json --dry-run # Preview
    msh ai apply patch.json # Apply