{
  "version": "3.16",
  "name": "CompileFix",
  "description": "Apply targeted VL fixes based on the latest .vl-code/last-compile.json report. Precondition: run VLCompile first so the compile report is fresh. This workflow fixes files from the current report, then stops for an external recompile/verification pass.",
  "sectionMerge": true,
  "registry": {
    "params": [
      "projectPath(STRING)"
    ],
    "services": [],
    "apis": [],
    "components": [],
    "vars": [
      "$compileReport(OBJECT)",
      "$errors(ARRAY)",
      "$fixTargets(ARRAY)"
    ],
    "files": {
      "inputs": [
        ".vl-code/last-compile.json"
      ],
      "artifacts": []
    },
    "docs": {
      "1": "VL Syntax Rules"
    }
  },
  "steps": [
    {
      "id": "LLM_010_LoadCompileReport",
      "meta": {
        "title": "1. Load latest compile report"
      },
      "in": {
        "docs": [
          "1"
        ],
        "max_tokens": 16384,
        "readFiles": [
          ".vl-code/last-compile.json"
        ],
        "messages": [
          {
            "role": "user",
            "content": "Read .vl-code/last-compile.json and return a JSON summary with EXACTLY these top-level keys: success(BOOL), errors(ARRAY of {file, line, message}), warningCount(INT), summary(STRING).\n\nRules:\n1. Only include hard compile errors in errors. Exclude warnings.\n2. If the file is missing, stale, malformed, or contains no hard errors, return success:true and errors:[] with an explanatory summary.\n3. Preserve file paths exactly as reported."
          }
        ],
        "output_config": {
          "format": {
            "type": "json_object"
          }
        }
      },
      "out": {
        "$compileReport": "=_result",
        "$errors": "=_result.errors"
      },
      "next": "Branch_015_CheckErrors"
    },
    {
      "id": "Branch_015_CheckErrors",
      "meta": {
        "title": "2. Check whether fixes are needed"
      },
      "cases": [
        {
          "condition": "=$errors.length > 0",
          "next": "LLM_020_PlanFixes"
        }
      ],
      "default": "Stop_NoErrors"
    },
    {
      "id": "LLM_020_PlanFixes",
      "meta": {
        "title": "3. Plan file-level fixes from compile errors"
      },
      "in": {
        "docs": [
          "1"
        ],
        "max_tokens": 32768,
        "readFiles": [
          "=$errors.map(e => e.file)"
        ],
        "messages": [
          {
            "role": "user",
            "content": "Latest compile report summary:"
          },
          {
            "role": "user",
            "content": "=$compileReport"
          },
          {
            "role": "user",
            "content": "Plan the minimum file-level fixes needed to resolve these compile errors. Return JSON with EXACTLY one top-level key: fixes(ARRAY of {file, line, errors, reason}).\n\nRules:\n1. Group multiple errors for the same file into a single fixes item.\n2. Keep file paths exactly as given.\n3. Do not invent new files.\n4. Focus only on the reported compiler errors."
          }
        ],
        "output_config": {
          "format": {
            "type": "json_object"
          }
        }
      },
      "out": {
        "$fixTargets": "=_result.fixes"
      },
      "next": "Loop_030_ApplyFixes"
    },
    {
      "id": "Loop_030_ApplyFixes",
      "meta": {
        "title": "4. Apply fixes to affected files"
      },
      "source": "=$fixTargets",
      "mode": "serial",
      "children": [
        "LLM_031_ApplyFix"
      ],
      "next": "Stop_NeedsRecompile",
      "onError": "skip"
    },
    {
      "id": "LLM_031_ApplyFix",
      "meta": {
        "title": "Fix file: =_item.file"
      },
      "in": {
        "docs": [
          "1"
        ],
        "max_tokens": 32768,
        "stream": true,
        "readFiles": [
          "=_item.file"
        ],
        "messages": [
          {
            "role": "user",
            "content": "Compiler-driven fix target:"
          },
          {
            "role": "user",
            "content": "=_item"
          },
          {
            "role": "user",
            "content": "Latest compile report summary:"
          },
          {
            "role": "user",
            "content": "=$compileReport"
          },
          {
            "role": "user",
            "content": "Update the file to resolve the reported compiler errors only. Output the COMPLETE updated file, preserve unaffected logic/layout, and keep the VL version header on the first non-empty line."
          }
        ]
      },
      "out": {
        "/{_item.file}": "=_result"
      },
      "next": "RETURN"
    },
    {
      "id": "Stop_NoErrors",
      "meta": {
        "title": "No hard compile errors in latest report"
      }
    },
    {
      "id": "Stop_NeedsRecompile",
      "meta": {
        "title": "Fixes applied — recompile externally to verify"
      }
    }
  ]
}
