{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://cernly.dev/schema/plugin.v1.schema.json",
  "title": "Cernly WebAssembly plugin protocol v1",
  "description": "What a WebAssembly plugin (https://cernly.dev/how/plugins/) is given and returns. The module runs under WASI preview 1 with no files, no network, an empty environment, a fixed clock and a fixed random seed. It reads one input object on stdin and writes one output object on stdout. Facts use the vocabulary of scan.v1.schema.json. Built with plugin/sdk.",
  "$defs": {
    "manifest": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "version", "plugin_api", "watches", "emits", "sha256"],
      "properties": {
        "name": { "type": "string", "pattern": "^[a-z][a-z0-9-]{1,40}$", "description": "Unique among the built-in and loaded plugins" },
        "version": { "type": "string", "minLength": 1, "description": "Semver; recorded in the scan" },
        "plugin_api": { "const": 1 },
        "watches": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string" },
          "description": "Repo-relative globs, the.cernly.yml syntax: a pattern covers a file or any parent folder, ** spans folders, other segments use path.Match. Only tracked, non-excluded files they cover are sent"
        },
        "emits": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string" },
          "description": "Fact kinds of scan.v1 the plugin may return; any other kind is dropped and recorded"
        },
        "consumes": {
          "type": "array",
          "items": { "type": "string" },
          "description": "Phase-1 fact kinds the plugin is given; present = a phase-2 plugin (https://cernly.dev/how/plugins/)"
        },
        "tools": {
          "type": "array",
          "items": { "type": "string" },
          "description": "Narrows consumed stack facts to these tools"
        },
        "sha256": { "type": "string", "pattern": "^[0-9a-f]{64}$", "description": "Hex SHA-256 of the.wasm module" }
      }
    },
    "bundle": {
      "type": "object",
      "additionalProperties": false,
      "required": ["manifest", "signature"],
      "description": "<name>.plugin.json beside <name>.wasm",
      "properties": {
        "manifest": { "$ref": "#/$defs/manifest" },
        "signature": {
          "type": "object",
          "additionalProperties": false,
          "required": ["key", "sig"],
          "properties": {
            "key": { "type": "string", "pattern": "^ed25519:[0-9a-f]{16}$", "description": "ed25519: and the first 16 hex digits of the SHA-256 of the raw public key" },
            "sig": { "type": "string", "description": "Base64 ed25519 signature over the bytes \"cernly plugin manifest v1\\u0000\" followed by the canonical JSON (sorted keys, no whitespace) of the manifest" }
          }
        }
      }
    },
    "input": {
      "type": "object",
      "additionalProperties": false,
      "required": ["plugin_api", "repo", "facts"],
      "properties": {
        "plugin_api": { "const": 1 },
        "repo": {
          "type": "object",
          "additionalProperties": false,
          "required": ["files"],
          "properties": {
            "files": {
              "type": "array",
              "description": "Sorted by path",
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": ["path", "content_b64"],
                "properties": {
                  "path": { "type": "string", "description": "Repo-relative, forward slashes" },
                  "content_b64": { "type": "string", "contentEncoding": "base64" }
                }
              }
            }
          }
        },
        "facts": {
          "type": "array",
          "description": "Phase-1 facts of the consumed kinds, sorted canonically; empty for a phase-1 plugin",
          "items": { "$ref": "https://cernly.dev/schema/scan.v1.schema.json#/$defs/fact" }
        }
      }
    },
    "output": {
      "type": "object",
      "additionalProperties": false,
      "required": ["facts", "errors"],
      "properties": {
        "facts": {
          "type": "array",
          "description": "Each fact is validated against scan.v1; its evidence may only name files sent or the evidence paths of facts sent; plugin, when set, must be the manifest name. Invalid facts are dropped and recorded as errors",
          "items": { "$ref": "https://cernly.dev/schema/scan.v1.schema.json#/$defs/fact" }
        },
        "errors": {
          "type": "array",
          "description": "Files the plugin could not read; any error makes the run partial",
          "items": { "$ref": "https://cernly.dev/schema/scan.v1.schema.json#/$defs/location_error" }
        }
      }
    }
  }
}
