Skip to main content

msh ai apply

Apply an AI-generated patch file to one or more .msh assets.

Usage

msh ai apply <patch_file> [--dry-run]

Description

Applies a JSON patch file (generated by AI commands) to .msh files:

  • Validates patch using safety layer
  • Shows diff in dry-run mode
  • Applies changes atomically
  • Reports success/failure for each file

Options

  • --dry-run: Show diff without modifying any files

Examples

Preview Changes

msh ai apply patch.json --dry-run

Example Output:

Previewing patch: patch.json

Files to modify:
- assets/revenue.msh

Changes:
--- 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)

No files modified (dry-run mode)

Apply Patch

msh ai apply patch.json

Example Output:

Applying patch: patch.json

✓ assets/revenue.msh - Applied successfully

Patch applied to 1 file(s)

Patch Format

Patches use JSON Patch (RFC 6902) format:

{
"patches": [{
"file_path": "assets/revenue.msh",
"diff": "unified diff string",
"operations": [
{
"op": "replace",
"path": "/transform",
"value": "SELECT customerId as customer_id, ..."
},
{
"op": "add",
"path": "/tests",
"value": [
{"assert": "monthly_revenue > 0"}
]
}
]
}]
}

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:

✗ assets/revenue.msh - Blocked: Contains dangerous operation 'DROP TABLE'

Generating Patches

Patches are generated by AI commands:

# Generate patch from fix command
msh ai fix assets/revenue.msh --json > patch.json

# Generate patch from tests command
msh ai tests assets/revenue.msh --json > patch.json

Workflow

  1. Generate patch: Use AI command to generate patch

    msh ai fix assets/revenue.msh --json > patch.json
  2. Preview changes: Review patch before applying

    msh ai apply patch.json --dry-run
  3. Apply patch: Apply patch to files

    msh ai apply patch.json
  4. Verify changes: Test that changes work

    msh run revenue

Error Handling

If a patch fails to apply:

✗ assets/revenue.msh - Failed: File not found

Common errors:

  • File not found: Asset file doesn't exist
  • Invalid patch: Patch format is incorrect
  • Safety violation: Patch contains blocked operations
  • Syntax error: Generated code has syntax errors