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 pathremove- Remove value at pathreplace- 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
-
AI Configuration: Must configure AI provider first
msh config ai --provider openai --model gpt-4 --api-key env:OPENAI_API_KEY -
Manifest: Generate manifest before using AI commands
msh manifest -
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:
- Preview changes: Review the diff
- Understand fixes: Make sure fixes make sense
- Test after applying: Run
msh runto verify fixes work
Applying Patches
You can apply patches in two ways:
-
Interactive: Use
--applyflag for interactive confirmationmsh ai fix assets/revenue.msh --apply -
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
Related Commands
msh ai review- Review asset for issuesmsh ai apply- Apply patch filemsh ai tests- Generate tests