{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://alectrona.com/schemas/patch-extend-feed.schema.json",
  "title": "Patch Extend Feed",
  "description": "Wire format for a Patch Extend feed envelope. Mirrors FeedEnvelope (PatchExtend) with payload.apps as FeedAppDTO / InstallPackage (PatchWire). This is not the catalog CatalogAppDTO shape.",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "feedId", "payload"],
  "properties": {
    "schemaVersion": {
      "type": "integer",
      "const": 1,
      "description": "Feed schema revision. Currently only version 1 is published."
    },
    "feedId": {
      "type": "string",
      "minLength": 1,
      "description": "Stable identifier for this feed."
    },
    "label": {
      "type": "string",
      "description": "Optional human-readable feed label."
    },
    "generatedAt": {
      "type": "string",
      "format": "date-time",
      "description": "Optional RFC 3339 timestamp when the feed was generated."
    },
    "priority": {
      "type": "integer",
      "description": "Optional publisher-declared feed priority (lower wins when coalescing)."
    },
    "scope": {
      "$ref": "#/$defs/feedScope"
    },
    "payload": {
      "$ref": "#/$defs/feedPayload"
    },
    "signature": {
      "$ref": "#/$defs/signatureBlock"
    }
  },
  "$defs": {
    "feedScope": {
      "type": "object",
      "additionalProperties": false,
      "description": "Publisher-declared inclusion/exclusion patterns for this feed. Exclusions win. Patterns may be \"*\", exact ids, or prefixes ending in \".*\".",
      "properties": {
        "apps": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        },
        "excludeApps": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        }
      }
    },
    "feedPayload": {
      "type": "object",
      "additionalProperties": false,
      "required": ["apps"],
      "properties": {
        "apps": {
          "type": "array",
          "items": { "$ref": "#/$defs/feedApp" }
        }
      }
    },
    "signatureBlock": {
      "type": "object",
      "additionalProperties": false,
      "required": ["algorithm", "signature"],
      "description": "Optional Ed25519 (or other) signature metadata for feed integrity verification.",
      "properties": {
        "algorithm": {
          "type": "string",
          "minLength": 1,
          "description": "Signature algorithm identifier (for example ed25519)."
        },
        "keyId": {
          "type": "string",
          "description": "Optional key identifier for key rotation."
        },
        "signature": {
          "type": "string",
          "minLength": 1,
          "description": "Signature value (typically base64)."
        },
        "signedContentHash": {
          "type": "string",
          "description": "Optional content hash of the signed payload (for example sha256:…)."
        },
        "canonicalization": {
          "type": "string",
          "description": "Optional canonicalization scheme (for example JCS@RFC8785)."
        },
        "signedPayloadB64": {
          "type": "string",
          "description": "Optional base64-encoded signed payload."
        },
        "signingTool": {
          "type": "string",
          "description": "Optional identifier for the tool/version that produced the signature."
        }
      }
    },
    "architecture": {
      "type": "string",
      "enum": ["arm64", "x86_64"]
    },
    "installMethod": {
      "type": "string",
      "enum": [
        "appInDmg",
        "appInZip",
        "appInPkg",
        "appInTarBz2",
        "pkg",
        "pkgInDmg",
        "pkgInZip",
        "exeInDmg",
        "exeInZip",
        "exeInTarBz2",
        "payloadFree"
      ],
      "description": "Install container / delivery method understood by the Patch client."
    },
    "installActions": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "preInstall": { "type": "string" },
        "postInstall": { "type": "string" },
        "preKillCommand": { "type": "string" },
        "alternateOpenCommand": { "type": "string" },
        "installationCommand": { "type": "string" },
        "environment": {
          "type": "object",
          "additionalProperties": { "type": "string" }
        }
      }
    },
    "integrity": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "sha256": {
          "type": "string",
          "pattern": "^[a-fA-F0-9]{64}$",
          "description": "SHA-256 digest of the downloadable package bytes."
        },
        "fileSize": {
          "type": "integer",
          "minimum": 0,
          "description": "Package size in bytes."
        }
      }
    },
    "installPackage": {
      "type": "object",
      "additionalProperties": false,
      "required": ["url", "version"],
      "description": "Downloadable installer or updater package. App-level fields are defaults; package-level values override when set.",
      "properties": {
        "kind": {
          "type": "string",
          "enum": ["installer", "updater"],
          "description": "Defaults to installer when omitted."
        },
        "architectures": {
          "type": "array",
          "items": { "$ref": "#/$defs/architecture" },
          "uniqueItems": true
        },
        "priority": {
          "type": "integer",
          "description": "Tie-break among package candidates (lower wins). Client default is 100 when omitted."
        },
        "minOSVersion": { "type": "string" },
        "maxOSVersion": { "type": "string" },
        "appliesFromVersion": {
          "type": "string",
          "description": "Minimum installed version (inclusive) this updater applies to."
        },
        "appliesToVersion": {
          "type": "string",
          "description": "Maximum installed version (inclusive) this updater applies to."
        },
        "packageFileName": { "type": "string" },
        "sourceItemName": {
          "type": "string",
          "description": "Name of the app/binary inside the container when it differs from fileName."
        },
        "url": {
          "type": "string",
          "minLength": 1,
          "description": "Download URL for this package."
        },
        "version": {
          "type": "string",
          "minLength": 1
        },
        "downloadHeaders": {
          "type": "object",
          "additionalProperties": { "type": "string" },
          "description": "Optional HTTP request headers applied when downloading this package."
        },
        "installActions": { "$ref": "#/$defs/installActions" },
        "integrity": { "$ref": "#/$defs/integrity" }
      }
    },
    "feedAppMetadata": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "description": { "type": "string" },
        "icon": { "type": "string" },
        "thumbnail": { "type": "string" },
        "homepage": { "type": "string" },
        "category": {
          "type": "string",
          "description": "Primary controlled-vocabulary classification."
        },
        "subcategory": {
          "type": "string",
          "description": "Optional controlled-vocabulary nesting under category."
        },
        "tags": {
          "type": "array",
          "items": { "type": "string" },
          "description": "Free-form cross-cutting attributes."
        },
        "releaseNotesURL": { "type": "string" }
      }
    },
    "feedAppFlags": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "skipTermination": { "type": "boolean" },
        "ignoredRunningExecutables": {
          "type": "array",
          "items": { "type": "string" }
        },
        "isConflictPriority": { "type": "boolean" },
        "packageIntegrityPolicy": { "type": "string" },
        "permissionFixModes": {
          "type": "array",
          "items": { "type": "string" }
        },
        "enforceBundleIdentifierOnReplace": { "type": "boolean" },
        "replacesAppsAtPaths": {
          "type": "array",
          "items": { "type": "string" }
        },
        "restrictUpdatesToSameMajorVersion": { "type": "boolean" }
      }
    },
    "feedAppChecks": {
      "type": "object",
      "additionalProperties": false,
      "description": "Runtime checks for an app. At least one of versionComparisonKey or commandToGetLocalVersion must be present as a non-whitespace string so Patch can read the installed version.",
      "properties": {
        "installEligibilityCheck": { "type": "string" },
        "updateEligibilityCheck": { "type": "string" },
        "versionComparisonKey": {
          "type": "string",
          "minLength": 1,
          "pattern": "\\S",
          "description": "Info.plist key used to compare the installed version (for example CFBundleShortVersionString). Must contain at least one non-whitespace character."
        },
        "commandToGetLocalVersion": {
          "type": "string",
          "minLength": 1,
          "pattern": "\\S",
          "description": "Shell command whose stdout is the installed version when no plist key applies. Must contain at least one non-whitespace character."
        },
        "conflictsWith": { "type": "string" },
        "commandToGetURLOnClient": { "type": "string" }
      },
      "anyOf": [
        { "required": ["versionComparisonKey"] },
        { "required": ["commandToGetLocalVersion"] }
      ]
    },
    "feedAppVerification": {
      "type": "object",
      "additionalProperties": false,
      "description": "Code-signing verification for the installed binary and installer package. bundleIdentifier lives at the app root, not here.",
      "properties": {
        "teamIdentifier": { "type": "string" },
        "designatedRequirement": { "type": "string" },
        "packageTeamIdentifier": { "type": "string" }
      }
    },
    "feedApp": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "friendlyName",
        "fileName",
        "version",
        "installMethod",
        "checks",
        "packages"
      ],
      "description": "One application entry in a Patch Extend feed (FeedAppDTO). installPath and architectures may be omitted; consumers derive them from installDirectory+fileName and the union of package architectures. checks is required and must include a non-empty versionComparisonKey and/or commandToGetLocalVersion. packages must contain at least one package.",
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1,
          "description": "Stable app id (typically metaSlug)."
        },
        "friendlyName": { "type": "string", "minLength": 1 },
        "fileName": {
          "type": "string",
          "minLength": 1,
          "description": "Final installed name (App.app or binary name)."
        },
        "bundleIdentifier": {
          "type": "string",
          "description": "CFBundleIdentifier; app-level identity shared across packages."
        },
        "version": { "type": "string", "minLength": 1 },
        "installMethod": { "$ref": "#/$defs/installMethod" },
        "sourceItemName": {
          "type": "string",
          "description": "App-level default; per-package sourceItemName overrides."
        },
        "installDirectory": {
          "type": "string",
          "description": "Install directory component (for example /Applications). Nil defaults to /Applications on consumers."
        },
        "installPath": {
          "type": "string",
          "description": "Optional derived absolute install path. May be omitted by producers."
        },
        "minOSVersion": { "type": "string" },
        "maxOSVersion": { "type": "string" },
        "metadata": { "$ref": "#/$defs/feedAppMetadata" },
        "flags": { "$ref": "#/$defs/feedAppFlags" },
        "checks": { "$ref": "#/$defs/feedAppChecks" },
        "installActions": { "$ref": "#/$defs/installActions" },
        "verification": { "$ref": "#/$defs/feedAppVerification" },
        "packages": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/installPackage" }
        },
        "architectures": {
          "type": "array",
          "items": { "$ref": "#/$defs/architecture" },
          "uniqueItems": true,
          "description": "Optional union of architectures across packages. May be omitted by producers."
        }
      }
    }
  }
}
