{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "ecc.state-store.v1",
  "title": "ECC State Store Schema",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "sessions": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/session"
      }
    },
    "skillRuns": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/skillRun"
      }
    },
    "skillVersions": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/skillVersion"
      }
    },
    "decisions": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/decision"
      }
    },
    "installState": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/installState"
      }
    },
    "governanceEvents": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/governanceEvent"
      }
    }
  },
  "$defs": {
    "nonEmptyString": {
      "type": "string",
      "minLength": 1
    },
    "nullableString": {
      "type": [
        "string",
        "null"
      ]
    },
    "nullableInteger": {
      "type": [
        "integer",
        "null"
      ],
      "minimum": 0
    },
    "jsonValue": {
      "type": [
        "object",
        "array",
        "string",
        "number",
        "boolean",
        "null"
      ]
    },
    "jsonArray": {
      "type": "array"
    },
    "session": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "adapterId",
        "harness",
        "state",
        "repoRoot",
        "startedAt",
        "endedAt",
        "snapshot"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "adapterId": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "harness": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "state": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "repoRoot": {
          "$ref": "#/$defs/nullableString"
        },
        "startedAt": {
          "$ref": "#/$defs/nullableString"
        },
        "endedAt": {
          "$ref": "#/$defs/nullableString"
        },
        "snapshot": {
          "type": [
            "object",
            "array"
          ]
        }
      }
    },
    "skillRun": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "skillId",
        "skillVersion",
        "sessionId",
        "taskDescription",
        "outcome",
        "failureReason",
        "tokensUsed",
        "durationMs",
        "userFeedback",
        "createdAt"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "skillId": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "skillVersion": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "sessionId": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "taskDescription": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "outcome": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "failureReason": {
          "$ref": "#/$defs/nullableString"
        },
        "tokensUsed": {
          "$ref": "#/$defs/nullableInteger"
        },
        "durationMs": {
          "$ref": "#/$defs/nullableInteger"
        },
        "userFeedback": {
          "$ref": "#/$defs/nullableString"
        },
        "createdAt": {
          "$ref": "#/$defs/nonEmptyString"
        }
      }
    },
    "skillVersion": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "skillId",
        "version",
        "contentHash",
        "amendmentReason",
        "promotedAt",
        "rolledBackAt"
      ],
      "properties": {
        "skillId": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "version": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "contentHash": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "amendmentReason": {
          "$ref": "#/$defs/nullableString"
        },
        "promotedAt": {
          "$ref": "#/$defs/nullableString"
        },
        "rolledBackAt": {
          "$ref": "#/$defs/nullableString"
        }
      }
    },
    "decision": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "sessionId",
        "title",
        "rationale",
        "alternatives",
        "supersedes",
        "status",
        "createdAt"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "sessionId": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "title": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "rationale": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "alternatives": {
          "$ref": "#/$defs/jsonArray"
        },
        "supersedes": {
          "$ref": "#/$defs/nullableString"
        },
        "status": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "createdAt": {
          "$ref": "#/$defs/nonEmptyString"
        }
      }
    },
    "installState": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "targetId",
        "targetRoot",
        "profile",
        "modules",
        "operations",
        "installedAt",
        "sourceVersion"
      ],
      "properties": {
        "targetId": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "targetRoot": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "profile": {
          "$ref": "#/$defs/nullableString"
        },
        "modules": {
          "$ref": "#/$defs/jsonArray"
        },
        "operations": {
          "$ref": "#/$defs/jsonArray"
        },
        "installedAt": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "sourceVersion": {
          "$ref": "#/$defs/nullableString"
        }
      }
    },
    "governanceEvent": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "id",
        "sessionId",
        "eventType",
        "payload",
        "resolvedAt",
        "resolution",
        "createdAt"
      ],
      "properties": {
        "id": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "sessionId": {
          "$ref": "#/$defs/nullableString"
        },
        "eventType": {
          "$ref": "#/$defs/nonEmptyString"
        },
        "payload": {
          "$ref": "#/$defs/jsonValue"
        },
        "resolvedAt": {
          "$ref": "#/$defs/nullableString"
        },
        "resolution": {
          "$ref": "#/$defs/nullableString"
        },
        "createdAt": {
          "$ref": "#/$defs/nonEmptyString"
        }
      }
    }
  }
}
