{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "gno://schemas/process-status@1.0",
  "title": "GNO Process Status",
  "description": "Status of a detached gno serve or daemon process, reported by --status --json",
  "type": "object",
  "required": [
    "running",
    "pid",
    "port",
    "cmd",
    "version",
    "started_at",
    "uptime_seconds",
    "resident",
    "pid_file",
    "log_file",
    "log_size_bytes"
  ],
  "properties": {
    "running": {
      "type": "boolean",
      "description": "Whether a live matching process was found via pid-file + liveness probe"
    },
    "pid": {
      "type": ["integer", "null"],
      "minimum": 1,
      "description": "Process ID from the pid-file, or null when no pid-file exists"
    },
    "port": {
      "type": ["integer", "null"],
      "minimum": 1,
      "maximum": 65535,
      "description": "Resident HTTP listener port, or null when no usable live process was found."
    },
    "cmd": {
      "type": "string",
      "enum": ["serve", "daemon"],
      "description": "Which command the status is reporting on"
    },
    "version": {
      "type": ["string", "null"],
      "description": "GNO version recorded when the process was started; null when no pid-file exists"
    },
    "started_at": {
      "type": ["string", "null"],
      "format": "date-time",
      "description": "ISO 8601 timestamp of when the detached process was started; null when no pid-file exists"
    },
    "uptime_seconds": {
      "type": ["integer", "null"],
      "minimum": 0,
      "description": "Seconds since started_at, computed at status-read time; null when not running"
    },
    "resident": {
      "anyOf": [
        { "$ref": "gno://schemas/resident-status@1.0" },
        { "type": "null" }
      ],
      "description": "Best-effort live redacted resident lifecycle snapshot, or null when unavailable."
    },
    "pid_file": {
      "type": "string",
      "description": "Absolute path to the pid-file that was consulted"
    },
    "log_file": {
      "type": "string",
      "description": "Absolute path to the log-file the detached process writes to"
    },
    "log_size_bytes": {
      "type": ["integer", "null"],
      "minimum": 0,
      "description": "Current size of the log-file in bytes, or null when the file does not exist"
    },
    "findings": {
      "description": "Daemon only: persisted last-run state of the scheduled findings pass, or null when findings.enabled is false / never configured. Read from the data-dir state file, so it survives daemon restarts.",
      "anyOf": [
        { "type": "null" },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "schemaVersion",
            "state",
            "collection",
            "cadence",
            "lastOutcome",
            "lastRunAt",
            "lastSuccessAt",
            "nextDueAt",
            "durationMs",
            "counts",
            "error"
          ],
          "properties": {
            "schemaVersion": { "const": "1.0" },
            "state": {
              "type": "string",
              "enum": [
                "pending",
                "success",
                "failed",
                "skipped_lease",
                "overdue"
              ],
              "description": "lastOutcome, or overdue once nextDueAt has slipped by a full cadence"
            },
            "collection": { "type": "string" },
            "cadence": { "type": "string" },
            "lastOutcome": {
              "type": "string",
              "enum": ["pending", "success", "failed", "skipped_lease"]
            },
            "lastRunAt": { "type": ["string", "null"], "format": "date-time" },
            "lastSuccessAt": {
              "type": ["string", "null"],
              "format": "date-time"
            },
            "nextDueAt": { "type": "string", "format": "date-time" },
            "durationMs": { "type": ["integer", "null"], "minimum": 0 },
            "counts": {
              "anyOf": [
                { "type": "null" },
                {
                  "type": "object",
                  "additionalProperties": false,
                  "required": [
                    "findings",
                    "written",
                    "reopened",
                    "resolved",
                    "deleted",
                    "open"
                  ],
                  "properties": {
                    "findings": { "type": "integer", "minimum": 0 },
                    "written": { "type": "integer", "minimum": 0 },
                    "reopened": { "type": "integer", "minimum": 0 },
                    "resolved": { "type": "integer", "minimum": 0 },
                    "deleted": { "type": "integer", "minimum": 0 },
                    "open": { "type": "integer", "minimum": 0 }
                  }
                }
              ]
            },
            "error": { "type": ["string", "null"] }
          }
        }
      ]
    }
  },
  "allOf": [
    {
      "description": "when running, pid/version/started_at/uptime_seconds must all be populated",
      "if": {
        "properties": { "running": { "const": true } },
        "required": ["running"]
      },
      "then": {
        "properties": {
          "pid": { "type": "integer", "minimum": 1 },
          "version": { "type": "string" },
          "started_at": { "type": "string", "format": "date-time" },
          "uptime_seconds": { "type": "integer", "minimum": 0 }
        },
        "required": ["pid", "version", "started_at", "uptime_seconds"]
      }
    },
    {
      "description": "when not running, uptime_seconds must be null (the process has no live uptime to report)",
      "if": {
        "properties": { "running": { "const": false } },
        "required": ["running"]
      },
      "then": {
        "properties": { "uptime_seconds": { "type": "null" } }
      }
    },
    {
      "description": "a live resident process must report its HTTP listening port as an integer",
      "if": {
        "properties": {
          "running": { "const": true }
        },
        "required": ["running"]
      },
      "then": {
        "properties": {
          "port": { "type": "integer", "minimum": 1, "maximum": 65535 }
        },
        "required": ["port"]
      }
    }
  ]
}
