{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://desirecore.com/schemas/market/catalog-metadata.v1.schema.json",
  "title": "DesireCore Market catalog metadata sidecar v1",
  "description": "Strict source-owned metadata stored next to one legacy Market Agent or Skill. The trusted catalog source ID, catalog commit, catalog path and catalog trust are injected by DesireCore and are intentionally prohibited here.",
  "type": "object",
  "required": [
    "schemaVersion",
    "identity",
    "presentation",
    "release",
    "timestamps",
    "provenance",
    "governance",
    "compatibility",
    "spec"
  ],
  "properties": {
    "$schema": {
      "type": "string",
      "const": "../../schemas/catalog-metadata.v1.schema.json",
      "description": "Relative schema reference from agents/<id>/ or skills/<id>/."
    },
    "schemaVersion": {
      "type": "integer",
      "const": 1,
      "description": "Sidecar contract version. This is not the legacy Market manifest version."
    },
    "identity": {
      "$ref": "#/$defs/identity"
    },
    "presentation": {
      "$ref": "#/$defs/presentation"
    },
    "release": {
      "$ref": "#/$defs/release"
    },
    "timestamps": {
      "$ref": "#/$defs/timestamps"
    },
    "provenance": {
      "$ref": "#/$defs/provenance"
    },
    "governance": {
      "$ref": "#/$defs/governance"
    },
    "compatibility": {
      "$ref": "#/$defs/compatibility"
    },
    "spec": {
      "oneOf": [
        { "$ref": "#/$defs/agentSpec" },
        { "$ref": "#/$defs/skillSpec" }
      ]
    }
  },
  "additionalProperties": false,
  "allOf": [
    {
      "if": {
        "properties": {
          "identity": {
            "properties": { "kind": { "const": "agent" } },
            "required": ["kind"]
          }
        }
      },
      "then": {
        "properties": {
          "spec": { "$ref": "#/$defs/agentSpec" }
        }
      }
    },
    {
      "if": {
        "properties": {
          "identity": {
            "properties": { "kind": { "const": "skill" } },
            "required": ["kind"]
          }
        }
      },
      "then": {
        "properties": {
          "spec": { "$ref": "#/$defs/skillSpec" }
        }
      }
    },
    {
      "if": {
        "properties": {
          "spec": {
            "properties": {
              "kind": { "const": "agent" },
              "installPolicy": { "const": "system" }
            },
            "required": ["kind", "installPolicy"]
          }
        },
        "required": ["spec"]
      },
      "then": {
        "properties": {
          "spec": {
            "properties": { "updatePolicy": { "const": "repository" } },
            "required": ["updatePolicy"]
          },
          "governance": {
            "properties": { "availability": { "const": "listing-only" } },
            "required": ["availability"]
          },
          "release": {
            "properties": { "state": { "const": "unknown" } },
            "required": ["state"]
          }
        }
      }
    },
    {
      "if": {
        "properties": {
          "spec": {
            "properties": {
              "kind": { "const": "agent" },
              "installPolicy": { "const": "market" }
            },
            "required": ["kind", "installPolicy"]
          }
        },
        "required": ["spec"]
      },
      "then": {
        "properties": {
          "spec": {
            "properties": { "updatePolicy": { "const": "market" } },
            "required": ["updatePolicy"]
          }
        }
      }
    }
  ],
  "$defs": {
    "slug": {
      "type": "string",
      "minLength": 1,
      "maxLength": 160,
      "pattern": "^(?!-)(?!.*--)[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$"
    },
    "safeRelativePath": {
      "type": "string",
      "minLength": 1,
      "pattern": "^(?!/)(?!.*(?:^|/)\\.\\.(?:/|$))(?!.*\\\\).+$"
    },
    "identity": {
      "type": "object",
      "description": "Source-local identity. catalogSourceId is injected by the trusted synchronizer and must not appear in the sidecar.",
      "required": ["kind", "id"],
      "properties": {
        "kind": {
          "type": "string",
          "enum": ["agent", "skill"]
        },
        "id": {
          "$ref": "#/$defs/slug"
        },
        "parentId": {
          "$ref": "#/$defs/slug"
        }
      },
      "additionalProperties": false
    },
    "localizedPresentation": {
      "type": "object",
      "required": ["name", "summary"],
      "properties": {
        "name": { "type": "string", "minLength": 1, "maxLength": 200 },
        "summary": { "type": "string", "minLength": 1, "maxLength": 1024 },
        "description": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },
    "presentation": {
      "type": "object",
      "required": ["defaultLocale", "i18n", "tags"],
      "properties": {
        "defaultLocale": {
          "type": "string",
          "pattern": "^[a-z]{2,3}(?:-[A-Z]{2})?$"
        },
        "i18n": {
          "type": "object",
          "minProperties": 1,
          "patternProperties": {
            "^[a-z]{2,3}(?:-[A-Z]{2})?$": {
              "$ref": "#/$defs/localizedPresentation"
            }
          },
          "additionalProperties": false
        },
        "icon": {
          "type": "object",
          "required": ["kind", "value"],
          "properties": {
            "kind": { "enum": ["emoji", "letter", "asset", "url"] },
            "value": { "type": "string", "minLength": 1, "maxLength": 2048 }
          },
          "additionalProperties": false
        },
        "category": { "$ref": "#/$defs/slug" },
        "tags": {
          "type": "array",
          "items": { "type": "string", "minLength": 1, "maxLength": 120 },
          "uniqueItems": true
        }
      },
      "additionalProperties": false
    },
    "unknownRelease": {
      "type": "object",
      "required": ["state"],
      "properties": { "state": { "const": "unknown" } },
      "additionalProperties": false
    },
    "knownRelease": {
      "type": "object",
      "required": ["state", "version", "versionScheme"],
      "properties": {
        "state": { "const": "known" },
        "version": { "type": "string", "minLength": 1, "maxLength": 160 },
        "versionScheme": {
          "type": "string",
          "enum": ["semver", "calver", "opaque"]
        },
        "changelog": {
          "type": "array",
          "maxItems": 100,
          "items": { "type": "string", "minLength": 1, "maxLength": 2000 }
        }
      },
      "additionalProperties": false,
      "allOf": [
        {
          "if": { "properties": { "versionScheme": { "const": "semver" } } },
          "then": {
            "properties": {
              "version": {
                "pattern": "^(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)(?:-[0-9A-Za-z.-]+)?(?:\\+[0-9A-Za-z.-]+)?$"
              }
            }
          }
        },
        {
          "if": { "properties": { "versionScheme": { "const": "calver" } } },
          "then": {
            "properties": {
              "version": { "pattern": "^\\d{4}(?:[.-]\\d{1,2}){1,2}(?:[.-][0-9A-Za-z]+)*$" }
            }
          }
        }
      ]
    },
    "release": {
      "description": "A collection parent may explicitly use unknown; versions are never synthesized from children.",
      "oneOf": [
        { "$ref": "#/$defs/unknownRelease" },
        { "$ref": "#/$defs/knownRelease" }
      ]
    },
    "unknownTimestamp": {
      "type": "object",
      "required": ["state"],
      "properties": { "state": { "const": "unknown" } },
      "additionalProperties": false
    },
    "knownDayTimestamp": {
      "type": "object",
      "required": ["state", "value", "precision"],
      "properties": {
        "state": { "const": "known" },
        "value": {
          "type": "string",
          "format": "date",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}$"
        },
        "precision": { "const": "day" }
      },
      "additionalProperties": false
    },
    "knownSecondTimestamp": {
      "type": "object",
      "required": ["state", "value", "precision"],
      "properties": {
        "state": { "const": "known" },
        "value": {
          "type": "string",
          "format": "date-time",
          "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?Z$"
        },
        "precision": { "const": "second" }
      },
      "additionalProperties": false
    },
    "timestamp": {
      "oneOf": [
        { "$ref": "#/$defs/unknownTimestamp" },
        { "$ref": "#/$defs/knownDayTimestamp" },
        { "$ref": "#/$defs/knownSecondTimestamp" }
      ]
    },
    "timestamps": {
      "type": "object",
      "description": "Catalog modification, content publication, governance review and upstream observation are separate facts. syncedAt is runtime-only and prohibited.",
      "required": [
        "catalogUpdatedAt",
        "releasePublishedAt",
        "reviewedAt",
        "upstreamObservedAt"
      ],
      "properties": {
        "catalogUpdatedAt": { "$ref": "#/$defs/timestamp" },
        "releasePublishedAt": { "$ref": "#/$defs/timestamp" },
        "reviewedAt": { "$ref": "#/$defs/timestamp" },
        "upstreamObservedAt": { "$ref": "#/$defs/timestamp" }
      },
      "additionalProperties": false
    },
    "contentProvenance": {
      "type": "object",
      "required": ["kind", "url"],
      "properties": {
        "kind": {
          "type": "string",
          "enum": ["git", "web", "zip", "release", "package", "container"]
        },
        "url": { "type": "string", "format": "uri", "pattern": "^https://" },
        "path": { "$ref": "#/$defs/safeRelativePath" },
        "ref": { "type": "string", "minLength": 1, "maxLength": 256 },
        "sha256": { "type": "string", "pattern": "^[a-fA-F0-9]{64}$" }
      },
      "additionalProperties": false
    },
    "provenance": {
      "type": "object",
      "description": "Only content provenance is source-owned. Catalog provenance and trust are injected by the synchronizer.",
      "properties": {
        "content": { "$ref": "#/$defs/contentProvenance" }
      },
      "additionalProperties": false
    },
    "maintainer": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "url": { "type": "string", "format": "uri" },
        "verified": { "type": "boolean" }
      },
      "additionalProperties": false
    },
    "unknownLicense": {
      "type": "object",
      "required": ["state"],
      "properties": { "state": { "const": "unknown" } },
      "additionalProperties": false
    },
    "knownLicense": {
      "type": "object",
      "required": ["state", "value"],
      "properties": {
        "state": { "const": "known" },
        "value": { "type": "string", "minLength": 1 },
        "evidencePath": { "$ref": "#/$defs/safeRelativePath" }
      },
      "additionalProperties": false
    },
    "license": {
      "oneOf": [
        { "$ref": "#/$defs/unknownLicense" },
        { "$ref": "#/$defs/knownLicense" }
      ]
    },
    "branding": {
      "type": "object",
      "required": ["relationship", "nameUsage", "logoStatus"],
      "properties": {
        "relationship": { "enum": ["official", "partner", "independent-listing"] },
        "nameUsage": { "enum": ["owned", "licensed", "nominative"] },
        "logoStatus": {
          "enum": ["not-used", "license-covered", "trademark-policy-allowed", "permission-obtained"]
        }
      },
      "additionalProperties": false
    },
    "compliance": {
      "type": "object",
      "required": ["licenseEvidencePath", "reviewedRef", "reviewedAt", "reviewedBy", "upstreamEndorsed"],
      "properties": {
        "licenseEvidencePath": { "$ref": "#/$defs/safeRelativePath" },
        "noticePath": { "$ref": "#/$defs/safeRelativePath" },
        "reviewedRef": { "type": "string", "minLength": 1 },
        "reviewedAt": {
          "type": "string",
          "oneOf": [
            { "format": "date", "pattern": "^\\d{4}-\\d{2}-\\d{2}$" },
            {
              "format": "date-time",
              "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d+)?Z$"
            }
          ]
        },
        "reviewedBy": { "type": "string", "minLength": 1 },
        "upstreamEndorsed": { "type": "boolean" }
      },
      "additionalProperties": false
    },
    "governance": {
      "type": "object",
      "required": ["availability", "license", "redistribution"],
      "properties": {
        "stewardship": {
          "enum": ["official", "partner", "community", "pointer"]
        },
        "availability": {
          "enum": ["listing-only", "installable"]
        },
        "license": { "$ref": "#/$defs/license" },
        "redistribution": {
          "enum": ["allowed", "source-pointer-only", "verify-package-terms"]
        },
        "branding": { "$ref": "#/$defs/branding" },
        "compliance": { "$ref": "#/$defs/compliance" },
        "listingMaintainer": { "$ref": "#/$defs/maintainer" },
        "upstreamMaintainer": { "$ref": "#/$defs/maintainer" }
      },
      "additionalProperties": false
    },
    "platforms": {
      "oneOf": [
        {
          "type": "object",
          "required": ["state"],
          "properties": { "state": { "const": "unknown" } },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["state"],
          "properties": { "state": { "const": "all" } },
          "additionalProperties": false
        },
        {
          "type": "object",
          "required": ["state", "values"],
          "properties": {
            "state": { "const": "known" },
            "values": {
              "type": "array",
              "minItems": 1,
              "uniqueItems": true,
              "items": { "enum": ["macos", "windows", "linux", "web", "ios", "android"] }
            }
          },
          "additionalProperties": false
        }
      ]
    },
    "requirement": {
      "type": "object",
      "required": ["kind", "value"],
      "properties": {
        "kind": { "enum": ["runtime", "hardware", "permission", "connection"] },
        "value": { "type": "string", "minLength": 1, "maxLength": 500 }
      },
      "additionalProperties": false
    },
    "compatibility": {
      "type": "object",
      "required": ["platforms"],
      "properties": {
        "platforms": { "$ref": "#/$defs/platforms" },
        "requiredClientVersion": {
          "type": "string",
          "pattern": "^(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)\\.(?:0|[1-9]\\d*)(?:-[0-9A-Za-z.-]+)?(?:\\+[0-9A-Za-z.-]+)?$"
        },
        "compatibleAgents": {
          "type": "array",
          "items": { "$ref": "#/$defs/slug" },
          "uniqueItems": true
        },
        "requirements": {
          "type": "array",
          "items": { "$ref": "#/$defs/requirement" },
          "uniqueItems": true
        }
      },
      "additionalProperties": false
    },
    "agentSpec": {
      "type": "object",
      "required": ["kind"],
      "properties": {
        "kind": { "const": "agent" },
        "installPolicy": { "enum": ["market", "system"] },
        "updatePolicy": { "enum": ["market", "repository"] },
        "persona": {
          "type": "object",
          "required": ["role", "traits"],
          "properties": {
            "role": { "type": "string", "minLength": 1 },
            "traits": {
              "type": "array",
              "items": { "type": "string", "minLength": 1 }
            }
          },
          "additionalProperties": false
        }
      },
      "dependencies": {
        "installPolicy": ["updatePolicy"],
        "updatePolicy": ["installPolicy"]
      },
      "additionalProperties": false
    },
    "collectionChild": {
      "type": "object",
      "required": ["identity", "path", "presentation", "release"],
      "properties": {
        "identity": {
          "type": "object",
          "required": ["kind", "id", "parentId"],
          "properties": {
            "kind": { "const": "skill" },
            "id": { "$ref": "#/$defs/slug" },
            "parentId": { "$ref": "#/$defs/slug" }
          },
          "additionalProperties": false
        },
        "path": { "$ref": "#/$defs/safeRelativePath" },
        "presentation": { "$ref": "#/$defs/presentation" },
        "release": { "$ref": "#/$defs/release" }
      },
      "additionalProperties": false
    },
    "collection": {
      "type": "object",
      "required": ["role", "childCount", "children"],
      "properties": {
        "role": { "const": "parent" },
        "childCount": { "type": "integer", "minimum": 1 },
        "children": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/collectionChild" }
        }
      },
      "additionalProperties": false
    },
    "skillSpec": {
      "type": "object",
      "required": ["kind"],
      "properties": {
        "kind": { "const": "skill" },
        "riskLevel": { "enum": ["low", "medium", "high", "critical"] },
        "skillType": { "enum": ["procedural", "declarative", "hybrid"] },
        "requires": {
          "type": "object",
          "required": ["tools", "connections"],
          "properties": {
            "tools": {
              "type": "array",
              "items": { "type": "string", "minLength": 1 }
            },
            "connections": {
              "type": "array",
              "items": { "type": "string", "minLength": 1 }
            }
          },
          "additionalProperties": false
        },
        "collection": { "$ref": "#/$defs/collection" }
      },
      "additionalProperties": false
    }
  }
}
