{
  "version": "3.16",
  "name": "DataAdjust",
  "description": "Add or modify database fields/tables with automatic cascade through services and sections. Handles: add field, remove field, add table, change field type, add enum value.",
  "sectionMerge": true,
  "registry": {
    "params": [
      "dataRequest(STRING)",
      "currentMeta(OBJECT)"
    ],
    "services": [],
    "apis": [],
    "components": [],
    "vars": [
      "$dataPlan(OBJECT)",
      "$dbChanges(ARRAY)",
      "$cascadeServices(ARRAY)",
      "$cascadeSections(ARRAY)",
      "$updatedMeta(OBJECT)"
    ],
    "files": {
      "inputs": [
        ".vl-code/ProjectMeta.json",
        "Database/*",
        "Services/*",
        "Sections/*"
      ],
      "artifacts": [
        ".vl-code/ProjectMeta.json",
        "Database/*",
        "Services/*",
        "Sections/*"
      ]
    },
    "docs": {
      "1": "VL 3.7 Syntax Rules"
    }
  },
  "steps": [
    {
      "id": "LLM_010_PlanDataChange",
      "meta": {
        "title": "1. Analyze data model change and plan cascade"
      },
      "in": {
        "docs": [
          "1"
        ],
        "max_tokens": 32768,
        "messages": [
          {
            "role": "user",
            "content": "You are a VL data architect. Analyze the requested data model change and determine the full cascade impact.\n\nCurrent ProjectMeta:"
          },
          {
            "role": "user",
            "content": "=currentMeta"
          },
          {
            "role": "user",
            "content": "Data change request:"
          },
          {
            "role": "user",
            "content": "=dataRequest"
          },
          {
            "role": "user",
            "content": "Return JSON with:\n- summary: one-line description\n- dbChanges: [{table, action(\"addField\"|\"removeField\"|\"addTable\"|\"changeType\"|\"addEnum\"), details: object}]\n- cascadeServices: [{name, file, changes: string}] — services affected by the data change\n- cascadeSections: [{name, file, changes: string}] — sections that use affected data and need UI updates\n- updatedMeta: FULL updated ProjectMeta\n\nTrace the cascade: DB change → affected SERVICE queries/methods → affected SECTION bindings/events/layout"
          }
        ],
        "output_config": {
          "format": {
            "type": "json_object"
          }
        },
        "stream": true
      },
      "out": {
        "$dataPlan": "=_result",
        "$dbChanges": "=_result.dbChanges",
        "$cascadeServices": "=_result.cascadeServices",
        "$cascadeSections": "=_result.cascadeSections",
        "$updatedMeta": "=_result.updatedMeta"
      },
      "next": "Pause_020_ReviewPlan"
    },
    {
      "id": "Pause_020_ReviewPlan",
      "meta": {
        "title": "2. Review data change plan (developer checkpoint)"
      },
      "resumeResultTarget": "$approved",
      "next": "Branch_025_CheckApproval"
    },
    {
      "id": "Branch_025_CheckApproval",
      "meta": {
        "title": "Check approval"
      },
      "cases": [
        {
          "condition": "=$approved === false",
          "next": "Stop_Cancelled"
        }
      ],
      "default": "LLM_030_EditDatabase"
    },
    {
      "id": "LLM_030_EditDatabase",
      "meta": {
        "title": "3. Apply database schema changes"
      },
      "in": {
        "docs": [
          "1"
        ],
        "max_tokens": 32768,
        "stream": true,
        "messages": [
          {
            "role": "user",
            "content": "Apply the following database changes to the VL .vdb file.\n\nCurrent database from ProjectMeta:"
          },
          {
            "role": "user",
            "content": "=currentMeta.database"
          },
          {
            "role": "user",
            "content": "Changes to apply:"
          },
          {
            "role": "user",
            "content": "=$dbChanges"
          },
          {
            "role": "user",
            "content": "Output the COMPLETE updated .vdb file in VL 3.7 syntax. Preserve all existing tables/fields not being changed.\n\nIMPORTANT: The file MUST start with '// VL_VERSION:3.7' as the first non-empty line."
          }
        ]
      },
      "out": {
        "/{currentMeta.database.file || ('Database/' + currentMeta.projectName + '.vdb')}": "=_result"
      },
      "next": "Fork_040_Cascade"
    },
    {
      "id": "Fork_040_Cascade",
      "meta": {
        "title": "4. Cascade changes to services and sections (parallel)"
      },
      "children": [
        "Loop_050_CascadeServices",
        "Loop_060_CascadeSections"
      ],
      "next": "Set_070_UpdateMeta"
    },
    {
      "id": "Loop_050_CascadeServices",
      "meta": {
        "title": "4a. Update affected services"
      },
      "source": "=$cascadeServices",
      "mode": "serial",
      "children": [
        "LLM_051_EditService"
      ],
      "next": "RETURN"
    },
    {
      "id": "LLM_051_EditService",
      "meta": {
        "title": "Update service: =_item.name"
      },
      "in": {
        "docs": [
          "1"
        ],
        "max_tokens": 32768,
        "stream": true,
        "readFiles": [
          "=_item.file"
        ],
        "messages": [
          {
            "role": "user",
            "content": "Update this VL service file to reflect database changes.\n\nChanges needed:"
          },
          {
            "role": "user",
            "content": "=_item.changes"
          },
          {
            "role": "user",
            "content": "Output the COMPLETE updated .vs file. Preserve all existing methods. Add/modify only what's needed for the data change.\n\nIMPORTANT: The file MUST start with '// VL_VERSION:3.7' as the first non-empty line."
          }
        ]
      },
      "out": {
        "/{_item.file}": "=_result"
      },
      "onError": "retry",
      "retryCount": 1
    },
    {
      "id": "Loop_060_CascadeSections",
      "meta": {
        "title": "4b. Update affected sections"
      },
      "source": "=$cascadeSections",
      "mode": "serial",
      "children": [
        "LLM_061_EditSection"
      ],
      "next": "RETURN"
    },
    {
      "id": "LLM_061_EditSection",
      "meta": {
        "title": "Update section: =_item.name"
      },
      "in": {
        "docs": [
          "1"
        ],
        "max_tokens": 32768,
        "stream": true,
        "readFiles": [
          "=_item.file"
        ],
        "messages": [
          {
            "role": "user",
            "content": "Update this VL section file to reflect data model changes.\n\nChanges needed:"
          },
          {
            "role": "user",
            "content": "=_item.changes"
          },
          {
            "role": "user",
            "content": "Output the COMPLETE updated .sc file. Preserve ALL existing elements. Only modify what's needed for the data change.\n\nIMPORTANT: The file MUST start with '// VL_VERSION:3.7' as the first non-empty line."
          }
        ]
      },
      "out": {
        "/{_item.file}": "=_result"
      },
      "onError": "retry",
      "retryCount": 1
    },
    {
      "id": "Set_070_UpdateMeta",
      "meta": {
        "title": "5. Update ProjectMeta"
      },
      "in": {},
      "out": {
        "/.vl-code/ProjectMeta.json": "=$updatedMeta"
      },
      "next": "Stop_Done"
    },
    {
      "id": "Stop_Done",
      "meta": {
        "title": "Data model adjustment complete — cascade applied"
      }
    },
    {
      "id": "Stop_Cancelled",
      "meta": {
        "title": "Data adjustment cancelled by developer"
      }
    }
  ]
}
