{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://hwlab.pikastech.local/deploy/deploy.schema.json",
  "title": "HWLAB Deploy Manifest",
  "type": "object",
  "required": ["manifestVersion", "environment", "health", "publicEndpoints", "frp", "k3s", "profiles", "services"],
  "properties": {
    "manifestVersion": {
      "type": "string",
      "pattern": "^v[0-9]+$"
    },
    "environment": {
      "type": "string",
      "enum": ["dev"]
    },
    "namespace": {
      "type": "string",
      "minLength": 1,
      "maxLength": 63,
      "pattern": "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"
    },
    "endpoint": {
      "type": "string",
      "const": "http://74.48.78.17:16667"
    },
    "health": {
      "$ref": "#/$defs/health"
    },
    "publicEndpoints": {
      "$ref": "#/$defs/publicEndpoints"
    },
    "frp": {
      "$ref": "#/$defs/frp"
    },
    "k3s": {
      "$ref": "#/$defs/k3s"
    },
    "profiles": {
      "type": "object",
      "required": ["dev"],
      "properties": {
        "dev": {
          "$ref": "#/$defs/profile"
        },
        "prod": {
          "$ref": "#/$defs/profile"
        }
      },
      "additionalProperties": false
    },
    "lanes": {
      "$ref": "#/$defs/lanes"
    },
    "services": {
      "type": "array",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/service"
      }
    },
    "createdAt": {
      "type": "string",
      "format": "date-time"
    }
  },
  "additionalProperties": false,
  "$defs": {
    "serviceId": {
      "type": "string",
      "enum": [
        "hwlab-cloud-api",
        "hwlab-cloud-web",
        "hwlab-gateway",
        "hwlab-edge-proxy",
        "hwlab-agent-skills"
      ]
    },
    "health": {
      "type": "object",
      "required": ["method", "path", "responseFormat"],
      "properties": {
        "method": {
          "type": "string",
          "const": "GET"
        },
        "path": {
          "type": "string",
          "const": "/health/live"
        },
        "responseFormat": {
          "type": "string",
          "const": "json"
        }
      },
      "additionalProperties": false
    },
    "publicEndpoints": {
      "type": "object",
      "required": ["frontend", "api"],
      "properties": {
        "frontend": {
          "allOf": [
            {
              "$ref": "#/$defs/publicEndpoint"
            },
            {
              "properties": {
                "url": {
                  "const": "http://74.48.78.17:16666"
                },
                "port": {
                  "const": 16666
                },
                "serviceId": {
                  "const": "hwlab-cloud-web"
                },
                "frpProxy": {
                  "const": "hwlab-dev-cloud-web"
                }
              }
            }
          ]
        },
        "api": {
          "allOf": [
            {
              "$ref": "#/$defs/publicEndpoint"
            },
            {
              "properties": {
                "url": {
                  "const": "http://74.48.78.17:16667"
                },
                "port": {
                  "const": 16667
                },
                "serviceId": {
                  "const": "hwlab-edge-proxy"
                },
                "frpProxy": {
                  "const": "hwlab-dev-edge-proxy"
                }
              }
            }
          ]
        }
      },
      "additionalProperties": false
    },
    "publicEndpoint": {
      "type": "object",
      "required": ["url", "protocol", "host", "port", "serviceId", "frpProxy"],
      "properties": {
        "url": {
          "type": "string",
          "pattern": "^http://74\\.48\\.78\\.17:1666[67]$"
        },
        "protocol": {
          "type": "string",
          "const": "http"
        },
        "host": {
          "type": "string",
          "const": "74.48.78.17"
        },
        "port": {
          "type": "integer",
          "enum": [16666, 16667]
        },
        "serviceId": {
          "$ref": "#/$defs/serviceId"
        },
        "frpProxy": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": false
    },
    "frp": {
      "type": "object",
      "required": ["server", "proxies"],
      "properties": {
        "server": {
          "type": "object",
          "required": ["address", "bindPort"],
          "properties": {
            "address": {
              "type": "string",
              "const": "74.48.78.17"
            },
            "bindPort": {
              "type": "integer",
              "const": 7000
            }
          },
          "additionalProperties": false
        },
        "proxies": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/frpProxy"
          }
        }
      },
      "additionalProperties": false
    },
    "frpProxy": {
      "type": "object",
      "required": ["name", "type", "localServiceId", "localHost", "localPort", "remotePort"],
      "properties": {
        "name": {
          "type": "string",
          "minLength": 1
        },
        "endpointId": {
          "type": "string",
          "enum": ["frontend", "api"]
        },
        "type": {
          "type": "string",
          "const": "tcp"
        },
        "localServiceId": {
          "$ref": "#/$defs/serviceId"
        },
        "localHost": {
          "type": "string",
          "pattern": "^[-a-z0-9]+\\.hwlab-dev\\.svc\\.cluster\\.local$"
        },
        "localPort": {
          "type": "integer",
          "minimum": 1,
          "maximum": 65535
        },
        "remotePort": {
          "type": "integer",
          "minimum": 1,
          "maximum": 65535
        }
      },
      "additionalProperties": false
    },
    "k3s": {
      "type": "object",
      "required": ["serviceMappings"],
      "properties": {
        "serviceMappings": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/k3sServiceMapping"
          }
        }
      },
      "additionalProperties": false
    },
    "k3sServiceMapping": {
      "type": "object",
      "required": ["serviceId", "name", "namespace", "port", "targetPort"],
      "properties": {
        "serviceId": {
          "$ref": "#/$defs/serviceId"
        },
        "name": {
          "type": "string",
          "minLength": 1
        },
        "namespace": {
          "type": "string",
          "const": "hwlab-dev"
        },
        "port": {
          "type": "integer",
          "minimum": 1,
          "maximum": 65535
        },
        "targetPort": {
          "type": "string",
          "const": "http"
        }
      },
      "additionalProperties": false
    },
    "profile": {
      "type": "object",
      "required": ["name", "enabled"],
      "properties": {
        "name": {
          "type": "string",
          "enum": ["dev", "prod"]
        },
        "enabled": {
          "type": "boolean"
        },
        "namespace": {
          "type": "string"
        },
        "endpoint": {
          "type": "string"
        },
        "notes": {
          "type": "string"
        }
      },
      "additionalProperties": false
    },
    "lanes": {
      "type": "object",
      "patternProperties": {
        "^v[0-9]{2,}$": {
          "$ref": "#/$defs/runtimeLane"
        }
      },
      "additionalProperties": false
    },
    "runtimeLane": {
      "type": "object",
      "required": ["name", "sourceBranch", "gitopsBranch", "namespace", "envReuseServices", "bootScripts", "serviceDeclarations", "envRecipe", "bootConfig"],
      "properties": {
        "name": { "type": "string", "pattern": "^v0\\.[0-9]+$" },
        "sourceBranch": { "type": "string", "pattern": "^v0\\.[0-9]+$" },
        "gitopsBranch": { "type": "string", "pattern": "^v0\\.[0-9]+-gitops$" },
        "namespace": { "type": "string", "pattern": "^hwlab-v[0-9]{2,}$" },
        "endpoint": { "type": "string" },
        "publicEndpoints": {
          "type": "object",
          "additionalProperties": { "type": "string" }
        },
        "artifactCatalog": { "type": "string" },
        "runtimePath": { "type": "string" },
        "imageTagMode": { "type": "string", "enum": ["short", "full"] },
        "sourceRepo": { "type": "string" },
        "envReuseServices": {
          "type": "array",
          "items": { "$ref": "#/$defs/serviceId" },
          "uniqueItems": true
        },
        "bootScripts": {
          "type": "object",
          "minProperties": 1,
          "additionalProperties": { "$ref": "#/$defs/repoPath" }
        },
        "serviceDeclarations": {
          "type": "object",
          "minProperties": 1,
          "additionalProperties": { "$ref": "#/$defs/serviceDeclaration" }
        },
        "envRecipe": { "$ref": "#/$defs/envRecipe" },
        "bootConfig": { "$ref": "#/$defs/bootConfig" },
        "services": {
          "type": "array",
          "items": { "$ref": "#/$defs/laneService" }
        }
      },
      "additionalProperties": false
    },
    "serviceDeclaration": {
      "type": "object",
      "required": ["runtimeKind", "entrypoint", "componentPaths"],
      "properties": {
        "runtimeKind": { "type": "string", "enum": ["bun-command", "cloud-web", "skills-bundle", "node-command"] },
        "entrypoint": { "$ref": "#/$defs/repoPath" },
        "artifactKind": { "type": "string", "enum": ["bun-command", "cloud-web", "skills-bundle", "node-command"] },
        "healthPath": { "type": "string", "pattern": "^/" },
        "healthPort": { "type": "integer", "minimum": 1, "maximum": 65535 },
        "componentPaths": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/repoPath" }
        },
        "env": {
          "type": "object",
          "additionalProperties": { "type": "string" }
        },
        "observable": { "type": "boolean" }
      },
      "additionalProperties": false
    },
    "envRecipe": {
      "type": "object",
      "required": ["osPackages", "bunVersion", "launcherPath", "runtimeNodeModulesPath", "additionalEnvPaths", "hwpodAliases"],
      "properties": {
        "osPackages": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "minLength": 1 },
          "uniqueItems": true
        },
        "bunVersion": { "type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+" },
        "launcherPath": { "$ref": "#/$defs/repoPath" },
        "runtimeNodeModulesPath": { "type": "string", "pattern": "^/" },
        "additionalEnvPaths": {
          "type": "array",
          "items": { "$ref": "#/$defs/repoPath" },
          "uniqueItems": true
        },
        "hwpodAliases": {
          "type": "array",
          "items": { "$ref": "#/$defs/hwpodAlias" }
        }
      },
      "additionalProperties": false
    },
    "hwpodAlias": {
      "type": "object",
      "required": ["command", "script"],
      "properties": {
        "command": { "type": "string", "minLength": 1 },
        "script": { "$ref": "#/$defs/repoPath" }
      },
      "additionalProperties": false
    },
    "bootConfig": {
      "type": "object",
      "required": ["template"],
      "properties": {
        "template": { "type": "string", "enum": ["default"] },
        "overrides": { "type": "object" }
      },
      "additionalProperties": false
    },
    "laneService": {
      "type": "object",
      "required": ["serviceId"],
      "properties": {
        "serviceId": { "$ref": "#/$defs/serviceId" },
        "env": {
          "type": "object",
          "additionalProperties": { "type": "string" }
        }
      },
      "additionalProperties": false
    },
    "repoPath": {
      "type": "string",
      "minLength": 1,
      "not": { "pattern": "(^/|(^|/)\\.\\.(/|$))" }
    },
    "service": {
      "type": "object",
      "required": ["serviceId", "namespace", "healthPath", "profile"],
      "properties": {
        "serviceId": {
          "$ref": "#/$defs/serviceId"
        },
        "namespace": {
          "type": "string",
          "minLength": 1
        },
        "healthPath": {
          "type": "string",
          "pattern": "^/"
        },
        "profile": {
          "type": "string",
          "enum": ["dev", "prod"]
        },
        "replicas": {
          "type": "integer",
          "minimum": 0
        },
        "instanceNames": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "identityStrategy": {
          "type": "string",
          "enum": ["indexed-workloads"]
        },
        "env": {
          "type": "object",
          "additionalProperties": {
            "type": "string"
          },
          "properties": {
            "HWLAB_CLOUD_DB_URL": {
              "type": "string",
              "const": "secretRef:hwlab-cloud-api-dev-db/database-url"
            },
            "HWLAB_CLOUD_DB_SSL_MODE": {
              "type": "string",
              "const": "disable"
            },
            "HWLAB_CLOUD_DB_CONTRACT": {
              "type": "string",
              "const": "dev-redacted-presence-only"
            },
            "HWLAB_CLOUD_RUNTIME_ADAPTER": {
              "type": "string",
              "const": "postgres"
            },
            "HWLAB_CLOUD_RUNTIME_DURABLE": {
              "type": "string",
              "const": "true"
            },
            "HWLAB_CODE_AGENT_PROVIDER": {
              "type": "string",
              "const": "codex-stdio"
            },
            "HWLAB_CODE_AGENT_MODEL": {
              "type": "string",
              "const": "gpt-5.5"
            },
            "HWLAB_CODE_AGENT_DEFAULT_PROVIDER_PROFILE": {
              "type": "string",
              "const": "deepseek"
            },
            "HWLAB_CODE_AGENT_DEEPSEEK_MODEL": {
              "type": "string",
              "const": "deepseek-chat"
            },
            "HWLAB_CODE_AGENT_DEEPSEEK_BASE_URL": {
              "type": "string",
              "const": "http://hwlab-deepseek-proxy.hwlab-dev.svc.cluster.local:4000/v1/responses"
            },
            "HWLAB_CODE_AGENT_CODEX_API_MODEL": {
              "type": "string",
              "const": "gpt-5.5"
            },
            "HWLAB_CODE_AGENT_CODEX_API_BASE_URL": {
              "type": "string",
              "const": "http://127.0.0.1:49280/responses"
            },
            "HWLAB_CODE_AGENT_CODEX_API_UPSTREAM_BASE_URL": {
              "type": "string",
              "const": "https://hyueapi.com"
            },
            "HWLAB_CODE_AGENT_CODEX_API_FORWARDER_PORT": {
              "type": "string",
              "const": "49280"
            },
            "HWLAB_CODE_AGENT_OPENAI_BASE_URL": {
              "type": "string",
              "const": "http://127.0.0.1:49280/responses"
            },
            "HWLAB_CODE_AGENT_CODEX_STDIO_ENABLED": {
              "type": "string",
              "const": "1"
            },
            "HWLAB_CODE_AGENT_CODEX_STDIO_SUPERVISOR": {
              "type": "string",
              "const": "repo-owned"
            },
            "HWLAB_CODE_AGENT_WORKSPACE": {
              "type": "string",
              "const": "/workspace/hwlab"
            },
            "HWLAB_CODE_AGENT_CODEX_WORKSPACE": {
              "type": "string",
              "const": "/workspace/hwlab"
            },
            "HWLAB_CODE_AGENT_CODEX_SANDBOX": {
              "type": "string",
              "const": "danger-full-access"
            },
            "HWLAB_CODE_AGENT_CODEX_COMMAND": {
              "type": "string",
              "const": "/app/node_modules/.bin/codex"
            },
            "CODEX_HOME": {
              "type": "string",
              "const": "/codex-home"
            },
            "NO_PROXY": {
              "type": "string"
            },
            "no_proxy": {
              "type": "string"
            },
            "OPENAI_API_KEY": {
              "type": "string",
              "const": "secretRef:hwlab-code-agent-provider/openai-api-key"
            }
          }
        },
        "publishEnabled": {
          "type": "boolean"
        },
        "artifactRequired": {
          "type": "boolean"
        },
        "artifactScope": {
          "type": "string",
          "enum": ["required", "disabled"]
        },
        "notPublishedReason": {
          "type": ["string", "null"]
        }
      },
      "additionalProperties": false
    }
  }
}
