{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://vgpu.sh/schemas/native/tint-compiler/v1/response.schema.json",
    "title": "vGPU Native Tint Compiler Response v1",
    "description": "The structured result of one resolved WGSL entry-point translation. Expected compiler failures are responses, not process-transport failures.",
    "oneOf": [
        {
            "$ref": "#/$defs/successResponse"
        },
        {
            "$ref": "#/$defs/errorResponse"
        }
    ],
    "$defs": {
        "nonEmptyString": {
            "type": "string",
            "minLength": 1,
            "maxLength": 16384
        },
        "identifier": {
            "type": "string",
            "pattern": "^[A-Za-z_][A-Za-z0-9_]*$",
            "maxLength": 256
        },
        "emittedIdentifier": {
            "description": "A vgpu-owned Metal identifier validated before invoking Tint.",
            "type": "string",
            "pattern": "^vgpu_[A-Za-z0-9_]+$",
            "maxLength": 256
        },
        "semanticVersion": {
            "type": "string",
            "pattern": "^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(?:-[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?(?:\\+[0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*)?$"
        },
        "relativePath": {
            "type": "string",
            "minLength": 1,
            "maxLength": 4096,
            "pattern": "^(?!/)(?![A-Za-z]:)(?![A-Za-z][A-Za-z0-9+.-]*:)(?!.*\\\\)(?!.*[\\u0000-\\u001f\\u007f])(?!.*(?:^|/)\\.\\.?(?:/|$))(?!.*//).+$"
        },
        "uint32": {
            "type": "integer",
            "minimum": 0,
            "maximum": 4294967295
        },
        "stage": {
            "enum": ["vertex", "fragment", "compute"]
        },
        "sourcePosition": {
            "type": "object",
            "additionalProperties": false,
            "required": ["line", "column"],
            "properties": {
                "line": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 4294967295
                },
                "column": {
                    "description": "One-based UTF-8 byte column, matching Tint source locations.",
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 4294967295
                }
            }
        },
        "moduleOrigin": {
            "description": "The resolver can prove only the authored input module, not an authored span.",
            "type": "object",
            "additionalProperties": false,
            "required": ["input", "precision"],
            "properties": {
                "input": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 1024
                },
                "precision": {
                    "const": "module"
                }
            }
        },
        "generatedLocation": {
            "description": "A half-open range in the resolved WGSL passed to Tint. origin, when present, is module attribution only.",
            "type": "object",
            "additionalProperties": false,
            "required": ["kind", "virtualPath", "start", "end"],
            "properties": {
                "kind": {
                    "const": "generated-wgsl"
                },
                "virtualPath": {
                    "$ref": "#/$defs/relativePath"
                },
                "start": {
                    "$ref": "#/$defs/sourcePosition"
                },
                "end": {
                    "$ref": "#/$defs/sourcePosition"
                },
                "origin": {
                    "$ref": "#/$defs/moduleOrigin"
                }
            }
        },
        "diagnostic": {
            "type": "object",
            "additionalProperties": false,
            "required": ["code", "severity", "phase", "message"],
            "dependentSchemas": {
                "location": {
                    "properties": {
                        "phase": {
                            "const": "wgsl"
                        }
                    }
                }
            },
            "properties": {
                "code": {
                    "type": "string",
                    "pattern": "^VGPU-[A-Z0-9-]+$",
                    "maxLength": 256
                },
                "severity": {
                    "enum": ["note", "warning", "error"]
                },
                "phase": {
                    "enum": [
                        "protocol",
                        "wgsl",
                        "inspect",
                        "lower",
                        "generate",
                        "internal"
                    ]
                },
                "message": {
                    "$ref": "#/$defs/nonEmptyString"
                },
                "fix": {
                    "$ref": "#/$defs/nonEmptyString"
                },
                "location": {
                    "$ref": "#/$defs/generatedLocation"
                }
            }
        },
        "nonErrorDiagnostic": {
            "allOf": [
                {
                    "$ref": "#/$defs/diagnostic"
                },
                {
                    "type": "object",
                    "properties": {
                        "severity": {
                            "enum": ["note", "warning"]
                        }
                    }
                }
            ]
        },
        "compiler": {
            "type": "object",
            "additionalProperties": false,
            "required": ["name", "version", "protocol", "upstream"],
            "properties": {
                "name": {
                    "const": "vgpu-tint-compiler"
                },
                "version": {
                    "$ref": "#/$defs/semanticVersion"
                },
                "protocol": {
                    "const": 1
                },
                "upstream": {
                    "type": "object",
                    "additionalProperties": false,
                    "required": ["name", "revision"],
                    "properties": {
                        "name": {
                            "const": "dawn/tint"
                        },
                        "revision": {
                            "type": "string",
                            "pattern": "^[a-f0-9]{40}$"
                        }
                    }
                }
            }
        },
        "entryPoint": {
            "type": "object",
            "additionalProperties": false,
            "required": ["stage", "wgsl", "metal"],
            "properties": {
                "stage": {
                    "$ref": "#/$defs/stage"
                },
                "wgsl": {
                    "$ref": "#/$defs/identifier"
                },
                "metal": {
                    "$ref": "#/$defs/emittedIdentifier"
                }
            }
        },
        "directSlot": {
            "type": "object",
            "additionalProperties": false,
            "required": ["mode", "resourceClass", "component", "index", "count"],
            "properties": {
                "mode": {
                    "const": "direct"
                },
                "resourceClass": {
                    "enum": ["buffer", "texture", "sampler"]
                },
                "component": {
                    "type": "string",
                    "pattern": "^[a-z][a-z0-9-]*$",
                    "maxLength": 256
                },
                "index": {
                    "$ref": "#/$defs/uint32"
                },
                "count": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 4294967295
                }
            },
            "allOf": [
                {
                    "if": {
                        "properties": { "resourceClass": { "const": "buffer" } },
                        "required": ["resourceClass"]
                    },
                    "then": { "properties": { "component": { "const": "buffer" } } }
                },
                {
                    "if": {
                        "properties": { "resourceClass": { "const": "texture" } },
                        "required": ["resourceClass"]
                    },
                    "then": { "properties": { "component": { "const": "texture" } } }
                },
                {
                    "if": {
                        "properties": { "resourceClass": { "const": "sampler" } },
                        "required": ["resourceClass"]
                    },
                    "then": { "properties": { "component": { "const": "sampler" } } }
                }
            ]
        },
        "binding": {
            "type": "object",
            "additionalProperties": false,
            "required": ["group", "binding", "slots"],
            "properties": {
                "group": {
                    "$ref": "#/$defs/uint32"
                },
                "binding": {
                    "$ref": "#/$defs/uint32"
                },
                "slots": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 1,
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/$defs/directSlot"
                    }
                }
            }
        },
        "internalBinding": {
            "description": "The shared immediate-data reservation appears here only when the generated entry point actually uses it.",
            "type": "object",
            "additionalProperties": false,
            "required": ["role", "slots"],
            "properties": {
                "role": {
                    "const": "immediate-data"
                },
                "slots": {
                    "type": "array",
                    "minItems": 1,
                    "maxItems": 1,
                    "items": {
                        "$ref": "#/$defs/immediateDataSlot"
                    }
                }
            }
        },
        "immediateDataSlot": {
            "type": "object",
            "additionalProperties": false,
            "required": ["mode", "resourceClass", "component", "index", "count"],
            "properties": {
                "mode": {
                    "const": "direct"
                },
                "resourceClass": {
                    "const": "buffer"
                },
                "component": {
                    "const": "buffer"
                },
                "index": {
                    "const": 30
                },
                "count": {
                    "const": 1
                }
            }
        },
        "storageBufferSizeRegion": {
            "type": "object",
            "additionalProperties": false,
            "required": ["stage", "immediateDataByteOffset"],
            "properties": {
                "stage": {
                    "$ref": "#/$defs/stage"
                },
                "immediateDataByteOffset": {
                    "enum": [4, 12]
                }
            },
            "allOf": [
                {
                    "if": {
                        "properties": {
                            "stage": {
                                "const": "fragment"
                            }
                        },
                        "required": ["stage"]
                    },
                    "then": {
                        "properties": {
                            "immediateDataByteOffset": {
                                "const": 12
                            }
                        }
                    },
                    "else": {
                        "properties": {
                            "immediateDataByteOffset": {
                                "const": 4
                            }
                        }
                    }
                }
            ]
        },
        "resolvedWorkgroupSize": {
            "type": "object",
            "additionalProperties": false,
            "required": ["x", "y", "z"],
            "properties": {
                "x": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 4294967295
                },
                "y": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 4294967295
                },
                "z": {
                    "type": "integer",
                    "minimum": 1,
                    "maximum": 4294967295
                }
            }
        },
        "vertexAttributeSemantic": {
            "type": "object",
            "additionalProperties": false,
            "required": ["location"],
            "properties": {
                "location": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 30
                }
            }
        },
        "vertexAttributeMetal": {
            "type": "object",
            "additionalProperties": false,
            "required": ["attribute"],
            "properties": {
                "attribute": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 30
                }
            }
        },
        "vertexAttribute": {
            "type": "object",
            "additionalProperties": false,
            "required": ["semantic", "metal"],
            "properties": {
                "semantic": {
                    "$ref": "#/$defs/vertexAttributeSemantic"
                },
                "metal": {
                    "$ref": "#/$defs/vertexAttributeMetal"
                }
            }
        },
        "fragmentColorSemantic": {
            "type": "object",
            "additionalProperties": false,
            "required": ["location"],
            "properties": {
                "location": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 7
                },
                "blendSource": {
                    "enum": [0, 1]
                }
            }
        },
        "fragmentColorMetal": {
            "type": "object",
            "additionalProperties": false,
            "required": ["color"],
            "properties": {
                "color": {
                    "type": "integer",
                    "minimum": 0,
                    "maximum": 7
                },
                "index": {
                    "enum": [0, 1]
                }
            }
        },
        "fragmentColorOutput": {
            "type": "object",
            "additionalProperties": false,
            "required": ["semantic", "metal"],
            "properties": {
                "semantic": {
                    "$ref": "#/$defs/fragmentColorSemantic"
                },
                "metal": {
                    "$ref": "#/$defs/fragmentColorMetal"
                }
            },
            "oneOf": [
                {
                    "type": "object",
                    "properties": {
                        "semantic": {
                            "type": "object",
                            "properties": {
                                "blendSource": false
                            }
                        },
                        "metal": {
                            "type": "object",
                            "properties": {
                                "index": false
                            }
                        }
                    }
                },
                {
                    "type": "object",
                    "properties": {
                        "semantic": {
                            "type": "object",
                            "required": ["blendSource"],
                            "properties": {
                                "blendSource": true
                            }
                        },
                        "metal": {
                            "type": "object",
                            "required": ["index"],
                            "properties": {
                                "index": true
                            }
                        }
                    }
                }
            ]
        },
        "vertexInterface": {
            "type": "object",
            "additionalProperties": false,
            "required": ["kind", "attributes"],
            "properties": {
                "kind": {
                    "const": "vertex"
                },
                "attributes": {
                    "type": "array",
                    "maxItems": 31,
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/$defs/vertexAttribute"
                    }
                }
            }
        },
        "fragmentInterface": {
            "type": "object",
            "additionalProperties": false,
            "required": ["kind", "colorOutputs"],
            "properties": {
                "kind": {
                    "const": "fragment"
                },
                "colorOutputs": {
                    "type": "array",
                    "maxItems": 16,
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/$defs/fragmentColorOutput"
                    }
                }
            }
        },
        "computeInterface": {
            "type": "object",
            "additionalProperties": false,
            "required": ["kind"],
            "properties": {
                "kind": {
                    "const": "compute"
                }
            }
        },
        "shaderInterface": {
            "oneOf": [
                {
                    "$ref": "#/$defs/vertexInterface"
                },
                {
                    "$ref": "#/$defs/fragmentInterface"
                },
                {
                    "$ref": "#/$defs/computeInterface"
                }
            ]
        },
        "result": {
            "type": "object",
            "additionalProperties": false,
            "required": [
                "msl",
                "entryPoint",
                "interface",
                "bindings",
                "internalBindings",
                "storageBufferSizeRegions"
            ],
            "properties": {
                "msl": {
                    "type": "string",
                    "minLength": 1,
                    "maxLength": 67108864
                },
                "entryPoint": {
                    "$ref": "#/$defs/entryPoint"
                },
                "interface": {
                    "$ref": "#/$defs/shaderInterface"
                },
                "resolvedWorkgroupSize": {
                    "$ref": "#/$defs/resolvedWorkgroupSize"
                },
                "bindings": {
                    "type": "array",
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/$defs/binding"
                    }
                },
                "internalBindings": {
                    "type": "array",
                    "maxItems": 1,
                    "items": {
                        "$ref": "#/$defs/internalBinding"
                    }
                },
                "storageBufferSizeRegions": {
                    "type": "array",
                    "maxItems": 1,
                    "uniqueItems": true,
                    "items": {
                        "$ref": "#/$defs/storageBufferSizeRegion"
                    }
                }
            },
            "allOf": [
                {
                    "if": {
                        "properties": {
                            "entryPoint": {
                                "type": "object",
                                "properties": {
                                    "stage": {
                                        "const": "compute"
                                    }
                                },
                                "required": ["stage"]
                            }
                        },
                        "required": ["entryPoint"]
                    },
                    "then": {
                        "required": ["resolvedWorkgroupSize"],
                        "properties": {
                            "resolvedWorkgroupSize": {
                                "$ref": "#/$defs/resolvedWorkgroupSize"
                            }
                        }
                    },
                    "else": {
                        "properties": {
                            "resolvedWorkgroupSize": false
                        }
                    }
                },
                {
                    "if": {
                        "properties": {
                            "storageBufferSizeRegions": {
                                "type": "array",
                                "minItems": 1
                            }
                        },
                        "required": ["storageBufferSizeRegions"]
                    },
                    "then": {
                        "properties": {
                            "internalBindings": {
                                "type": "array",
                                "minItems": 1
                            }
                        }
                    }
                }
            ]
        },
        "successResponse": {
            "type": "object",
            "additionalProperties": false,
            "required": [
                "schemaVersion",
                "contractId",
                "ok",
                "compiler",
                "diagnostics",
                "result"
            ],
            "properties": {
                "schemaVersion": {
                    "const": 1
                },
                "contractId": {
                    "const": "vgpu-native-tint-compiler/v1"
                },
                "ok": {
                    "const": true
                },
                "compiler": {
                    "$ref": "#/$defs/compiler"
                },
                "diagnostics": {
                    "type": "array",
                    "items": {
                        "$ref": "#/$defs/nonErrorDiagnostic"
                    }
                },
                "result": {
                    "$ref": "#/$defs/result"
                }
            }
        },
        "errorResponse": {
            "type": "object",
            "additionalProperties": false,
            "required": [
                "schemaVersion",
                "contractId",
                "ok",
                "compiler",
                "diagnostics"
            ],
            "properties": {
                "schemaVersion": {
                    "const": 1
                },
                "contractId": {
                    "const": "vgpu-native-tint-compiler/v1"
                },
                "ok": {
                    "const": false
                },
                "compiler": {
                    "$ref": "#/$defs/compiler"
                },
                "diagnostics": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                        "$ref": "#/$defs/diagnostic"
                    },
                    "contains": {
                        "type": "object",
                        "required": ["severity"],
                        "properties": {
                            "severity": {
                                "const": "error"
                            }
                        }
                    },
                    "minContains": 1
                }
            }
        }
    }
}
