hcju/stacksqa huggingface dataset:
Let $\mathcal{H} = \SheafHom_{\mathcal{O}_X}(\mathcal{G}, \mathcal{F})$. This is a coherent $\mathcal{O}_X$-module because its restriction of schemes \'etale over $X$ is coherent by Modules, Lemma \ref{modules-lemma-internal-hom-locally-kernel-direct-sum}. By Lemma \ref{lemma-completion-internal-hom} the map $$ \lim_n H^0(X, \mathcal{H}/\mathcal{I}^n\mathcal{H}) \to \Mor_{\textit{Coh}(X, \mathcal{I})} (\mathcal{G}^\wedge, \mathcal{F}^\wedge) $$ is bijective. Let $i : Z \to X$ be the scheme theoretic support of $\mathcal{H}$. It is clear that $Z$ is a closed subspace such that $|Z|$ is contained in the intersection of the supports of $\mathcal{F}$ and $\mathcal{G}$. Hence $Z \to \Spec(A)$ is proper by assumption (see Derived Categories of Spaces, Section \ref{spaces-perfect-section-proper-over-base}). Write $\mathcal{H} = i_*\mathcal{H}'$ for some coherent $\mathcal{O}_Z$-module $\mathcal{H}'$. We have $i_*(\mathcal{H}'/I^n\mathcal{H}') = \mathcal{H}/I^n\mathcal{H}$. Hence we obtain \begin{align*} \lim_n H^0(X, \mathcal{H}/\mathcal{I}^n\mathcal{H}) & = \lim_n H^0(Z, \mathcal{H}'/\mathcal{I}^n\mathcal{H}') \\ & = H^0(Z, \mathcal{H}') \\ & = H^0(X, \mathcal{H}) \\ & =  \Mor_{\textit{Coh}(\mathcal{O}_X)}(\mathcal{F}, \mathcal{G}) \end{align*} the second equality by the theorem on formal functions (Cohomology of Spaces, Lemma \ref{spaces-cohomology-lemma-spell-out-theorem-formal-functions}). This proves the lemma.
The uniqueness and \'etale descent for objects of $\textit{Coh}(X, \mathcal{I})$ and $\textit{Coh}(\mathcal{O}_X)$ implies it suffices to construct $(\mathcal{F}, a, \beta)$ \'etale locally on $X$. Thus we reduce to the case of schemes which is Cohomology of Schemes, Lemma \ref{coherent-lemma-existence-easy}.
Since $f$ is a proper morphism we see that $f_*\mathcal{F}$ is a coherent $\mathcal{O}_Y$-module (Cohomology of Spaces, Lemma \ref{spaces-cohomology-lemma-proper-pushforward-coherent}). Thus the statement of the lemma makes sense. Consider the compositions $$ \gamma_n : \mathcal{G}_n \to f_*f^*\mathcal{G}_n \to f_*(\mathcal{F}/\mathcal{I}^n\mathcal{F}). $$ Here the first map is the adjunction map and the second is $f_*\beta_n$. We claim that there exists a unique $\alpha$ as in the lemma such that the compositions $$ \mathcal{G}_n \xrightarrow{\alpha_n} f_*\mathcal{F}/\mathcal{J}^nf_*\mathcal{F} \to f_*(\mathcal{F}/\mathcal{I}^n\mathcal{F}) $$ equal $\gamma_n$ for all $n$. Because of the uniqueness and \'etale descent for $\textit{Coh}(Y, \mathcal{J})$ it suffices to prove this \'etale locally on $Y$. Thus we may assume $Y$ is the spectrum of a Noetherian ring. As $f$ is representable we see that $X$ is a scheme as well. Thus we reduce to the case of schemes, see proof of Cohomology of Schemes, Lemma \ref{coherent-lemma-inverse-systems-push-pull}.
Let's write $j_n : Z_n \to X_n$ for the vertical morphisms. As the squares in the statement are cartesian we see that the base change of $j_n$ to $X_1$ is $j_1$. Thus Limits of Spaces, Lemma \ref{spaces-limits-lemma-check-closed-infinitesimally} shows that $j_n$ is a closed immersion. Set $\mathcal{F}_n = j_{n, *}\mathcal{O}_{Z_n}$, so that $j_n^\sharp$ is a surjection $\mathcal{O}_{X_n} \to \mathcal{F}_n$. Again using that the squares are cartesian we see that the pullback of $\mathcal{F}_{n + 1}$ to $X_n$ is $\mathcal{F}_n$. Hence Grothendieck's existence theorem, as reformulated in Remark \ref{remark-reformulate-existence-theorem}, tells us there exists a map $\mathcal{O}_X \to \mathcal{F}$ of coherent $\mathcal{O}_X$-modules whose restriction to $X_n$ recovers $\mathcal{O}_{X_n} \to \mathcal{F}_n$. Moreover, the support of $\mathcal{F}$ is proper over $S$. As the completion functor is exact (Lemma \ref{lemma-exact}) we see that $\mathcal{O}_X \to \mathcal{F}$ is surjective. Thus $\mathcal{F} = \mathcal{O}_X/\mathcal{J}$ for some quasi-coherent sheaf of ideals $\mathcal{J}$. Setting $Z = V(\mathcal{J})$ finishes the proof.
Let's write $f_n : Y_n \to X_n$ for the vertical morphisms. As the squares in the statement are cartesian we see that the base change of $f_n$ to $X_1$ is $f_1$. Thus Lemma \ref{lemma-thicken-property-morphisms-cartesian} shows that $f_n$ is a finite morphism. Set $\mathcal{F}_n = f_{n, *}\mathcal{O}_{Y_n}$. Using that the squares are cartesian we see that the pullback of $\mathcal{F}_{n + 1}$ to $X_n$ is $\mathcal{F}_n$. Hence Grothendieck's existence theorem, as reformulated in Remark \ref{remark-reformulate-existence-theorem}, tells us there exists a coherent $\mathcal{O}_X$-module $\mathcal{F}$ whose restriction to $X_n$ recovers $\mathcal{F}_n$. Moreover, the support of $\mathcal{F}$ is proper over $S$. As the completion functor is fully faithful (Theorem \ref{theorem-grothendieck-existence}) we see that the multiplication maps $\mathcal{F}_n \otimes_{\mathcal{O}_{X_n}} \mathcal{F}_n \to \mathcal{F}_n$ fit together to give an algebra structure on $\mathcal{F}$. Setting $Y = \underline{\Spec}_X(\mathcal{F})$ finishes the proof.
The morphisms $(1, g_n) : X_n \to X_n \times_S Y_n$ are closed immersions because $Y_n \to S_n$ is separated (Morphisms of Spaces, Lemma \ref{spaces-morphisms-lemma-section-immersion}). Thus by Lemma \ref{lemma-algebraize-formal-closed-subscheme} there exists a closed subspace $Z \subset X \times_S Y$ proper over $S$ whose base change to $S_n$ recovers $X_n \subset X_n \times_S Y_n$. The first projection $p : Z \to X$ is a proper morphism (as $Z$ is proper over $S$, see Morphisms of Spaces, Lemma \ref{spaces-morphisms-lemma-universally-closed-permanence}) whose base change to $S_n$ is an isomorphism for all $n$. In particular, $p : Z \to X$ is quasi-finite on an open subspace of $Z$ containing every point of $Z_0$ for example by Morphisms of Spaces, Lemma \ref{spaces-morphisms-lemma-locally-finite-type-quasi-finite-part}. As $Z$ is proper over $S$ this open neighbourhood is all of $Z$. We conclude that $p : Z \to X$ is finite by Zariski's main theorem (for example apply Lemma \ref{lemma-quasi-finite-separated-pass-through-finite} and use properness of $Z$ over $X$ to see that the immersion is a closed immersion). Applying the equivalence of Theorem \ref{theorem-grothendieck-existence} we see that $p_*\mathcal{O}_Z = \mathcal{O}_X$ as this is true modulo $I^n$ for all $n$. Hence $p$ is an isomorphism and we obtain the morphism $g$ as the composition $X \cong Z \to Y$. We omit the proof of uniqueness.
By Spaces over Fields, Lemma \ref{spaces-over-fields-lemma-codim-1-point-in-schematic-locus} the algebraic space $X_1$ is a scheme. Hence $X_1$ is a proper scheme of dimension $\leq 1$ over $\kappa$. By Varieties, Lemma \ref{varieties-lemma-dim-1-proper-projective} we see that $X_1$ is H-projective over $\kappa$. Let $\mathcal{L}_1$ be an ample invertible sheaf on $X_1$. \medskip\noindent We are going to show that $\mathcal{L}_1$ lifts to a compatible system $\{\mathcal{L}_n\}$ of invertible sheaves on $\{X_n\}$. Observe that $X_n$ is a scheme too by Lemma \ref{lemma-thickening-scheme}. Recall that $X_1 \to X_n$ induces homeomorphisms of underlying topological spaces. In the rest of the proof we do not distinguish between sheaves on $X_n$ and sheaves on $X_1$. Suppose, given a lift $\mathcal{L}_n$ to $X_n$. We consider the exact sequence $$ 1 \to (1 + \mathfrak m^n\mathcal{O}_{X_{n + 1}})^* \to \mathcal{O}_{X_{n + 1}}^* \to \mathcal{O}_{X_n}^* \to 1 $$ of sheaves on $X_{n + 1}$. The class of $\mathcal{L}_n$ in $H^1(X_n, \mathcal{O}_{X_n}^*)$ (see Cohomology, Lemma \ref{cohomology-lemma-h1-invertible}) can be lifted to an element of $H^1(X_{n + 1}, \mathcal{O}_{X_{n + 1}}^*)$ if and only if the obstruction in $H^2(X_{n + 1}, (1 + \mathfrak m^n\mathcal{O}_{X_{n + 1}})^*)$ is zero. As $X_1$ is a Noetherian scheme of dimension $\leq 1$ this cohomology group vanishes (Cohomology, Proposition \ref{cohomology-proposition-vanishing-Noetherian}). \medskip\noindent By Grothendieck's algebraization theorem (Cohomology of Schemes, Theorem \ref{coherent-theorem-algebraization}) we find a projective morphism of schemes $X \to S = \Spec(A)$ and a compatible system of isomorphisms $X_n = S_n \times_S X$.
Set $X_n = X \times_{\Spec(A)} \Spec(A/\mathfrak m^n)$. By Lemma \ref{lemma-formal-algebraic-space-proper-reldim-1} there exists a projective morphism $Y \to \Spec(A)$ and compatible isomorphisms $Y \times_{\Spec(A)} \Spec(A/\mathfrak m^n) \cong X \times_{\Spec(A)} \Spec(A/\mathfrak m^n)$. By Lemma \ref{lemma-algebraize-morphism} we see that $X \cong Y$ and the proof is complete.
Let us prove the equivalence of (1) and (2). The implication (1) $\Rightarrow$ (2) is immediate. Assume $$ \xymatrix{ X \times_Y V \ar[d] \ar[r] & V \ar[d] \\ X \ar[r]^f & Y } \quad\quad \xymatrix{ X \times_Y V' \ar[d] \ar[r] & V' \ar[d] \\ X \ar[r]^f & Y } $$ are two diagrams as in the lemma. Assume $V \to Y$ is surjective and $X \times_Y V \to V$ has property $\mathcal{P}$. To show that (2) implies (1) we have to prove that $X \times_Y V' \to V'$ has $\mathcal{P}$. To do this consider the diagram $$ \xymatrix{ X \times_Y V \ar[d] & (X \times_Y V) \times_X (X \times_Y V') \ar[l] \ar[d] \ar[r] & X \times_Y V' \ar[d] \\ V & V \times_Y V' \ar[l] \ar[r] & V' } $$ By our assumption that $\mathcal{P}$ is \'etale local on the source, we see that $\mathcal{P}$ is preserved under \'etale base change, see Descent, Lemma \ref{descent-lemma-pullback-property-local-target}. Hence if the left vertical arrow has $\mathcal{P}$ the so does the middle vertical arrow. Since $U \times_X U' \to U'$ is surjective and \'etale (hence defines an \'etale covering of $U'$) this implies (as $\mathcal{P}$ is assumed local for the \'etale topology on the target) that the left vertical arrow has $\mathcal{P}$. \medskip\noindent If $X$ and $Y$ are representable, then we can take $\text{id}_Y : Y \to Y$ as our \'etale covering to see the final statement of the lemma is true.
Since $\mathcal{C}_{Z/Y}$ is finite locally free (see Lemma \ref{lemma-quasi-regular-immersion} and Lemma \ref{lemma-regular-quasi-regular-immersion}) it suffices to prove that the sequence is exact. It suffices to show that the first map is injective as the sequence is already right exact in general. After \'etale localization on $X$ this reduces to the case of schemes, see Divisors, Lemma \ref{divisors-lemma-transitivity-conormal-quasi-regular}.
Part (1) implies (3) by More on Morphisms, Lemma \ref{more-morphisms-lemma-qcoh-relative-pseudo-coherence-characterize}. \medskip\noindent Assume (3). Pick any commutative diagram as in (1) with $U \to X$ surjective. Choose an affine open covering $V = \bigcup V_j$ and affine open coverings $(U \to V)^{-1}(V_j) = \bigcup U_{ij}$. By (3) and More on Morphisms, Lemma \ref{more-morphisms-lemma-qcoh-relative-pseudo-coherence-characterize} we see that $E|_U$ is $m$-pseudo-coherent relative to $V$. Thus (3) implies (2). \medskip\noindent Assume (2). Choose a commutative diagram $$ \xymatrix{ U \ar[d] \ar[r] & V \ar[d] \\ X \ar[r] & Y } $$ where $U$, $V$ are schemes, the vertical arrows are \'etale, the morphism $U \to X$ is surjective, and $E|_U$ is $m$-pseudo-coherent relative to $V$. Next, suppose given a second commutative diagram $$ \xymatrix{ U' \ar[d] \ar[r] & V' \ar[d] \\ X \ar[r] & Y } $$ with \'etale vertical arrows and $U', V'$ schemes. We want to show that $E|_{U'}$ is $m$-pseudo-coherent relative to $V'$. The morphism $U'' = U \times_X U' \to U'$ is surjective \'etale and $U'' \to V'$ factors through $V'' = V' \times_Y V$ which is \'etale over $V'$. Hence it suffices to show that $E|_{U''}$ is $m$-pseudo-coherent relative to $V''$, see More on Morphisms, Lemmas \ref{more-morphisms-lemma-relative-pseudo-coherent-descends-fppf} and \ref{more-morphisms-lemma-relative-pseudo-coherent-post-compose}. Using the second lemma once more it suffices to show that $E|_{U''}$ is $m$-pseudo-coherent relative to $V$. This is true by More on Morphisms, Lemma \ref{more-morphisms-lemma-pull-relative-pseudo-coherent} and the fact that an \'etale morphism of schemes is pseudo-coherent by More on Morphisms, Lemma \ref{more-morphisms-lemma-flat-finite-presentation-pseudo-coherent}.
We claim that Derived Categories of Spaces, Lemma \ref{spaces-perfect-lemma-perfect-direct-image} applies. Conditions (1) and (2) are immediate. Condition (3) is local on $X$. Thus we may assume $X$ and $Y$ affine and $E$ represented by a strictly perfect complex of $\mathcal{O}_X$-modules. Thus it suffices to show that $\mathcal{O}_X$ has finite tor dimension as a sheaf of $f^{-1}\mathcal{O}_Y$-modules on the \'etale site. By Derived Categories of Spaces, Lemma \ref{spaces-perfect-lemma-tor-dimension-rel} it suffices to check this on the Zariski site. This is equivalent to being perfect for finite type morphisms of schemes by More on Morphisms, Lemma \ref{more-morphisms-lemma-check-perfect-stalks}.
Choose a scheme $V$ and a surjective \'etale morphism $V \to Y$. Choose a scheme $W$ and a surjective \'etale morphism $W \to P \times_Y V$. Set $U' = U \times_P W$, which is a scheme \'etale over $U$. We have to show that $U' \to W$ is a Koszul-regular immersion of schemes, see Definition \ref{definition-regular-immersion}. By Definition \ref{definition-lci} above the morphism of schemes $U' \to V$ is a local complete intersection morphism. Hence the result follows from More on Morphisms, Lemma \ref{more-morphisms-lemma-lci}.
Let $i : X \to Y$ be a Koszul-regular immersion of algebraic spaces over $S$. By definition there exists a surjective \'etale morphism $V \to Y$ where $V$ is a scheme such that $X \times_Y V$ is a scheme and the base change $X \times_Y V \to V$ is a Koszul-regular immersion of schemes. By More on Morphisms, Lemma \ref{more-morphisms-lemma-regular-immersion-lci} we see that $X \times_Y V \to V$ is a local complete intersection morphism. From Definition \ref{definition-lci} we conclude that $i$ is a local complete intersection morphism of algebraic spaces.
Choose a scheme $W$ and a surjective \'etale morphism $W \to Z$. Choose a scheme $V$ and a surjective \'etale morphism $V \to W \times_Z Y$. Choose a scheme $U$ and a surjective \'etale morphism $U \to V \times_Y X$. Then $U \to W$ is a local complete intersection morphism of schemes and $V \to W$ is a smooth morphism of schemes. By the result for schemes (More on Morphisms, Lemma \ref{more-morphisms-lemma-lci-permanence}) we conclude that $U \to V$ is a local complete intersection morphism. By definition this means that $f$ is a local complete intersection morphism.
Let $S$ be a scheme. Let $f : X \to Y$ be a morphism of algebraic spaces over $S$. Let $\{Y_i \to Y\}$ be an fpqc covering (Topologies on Spaces, Definition \ref{spaces-topologies-definition-fpqc-covering}). Let $f_i : X_i \to Y_i$ be the base change of $f$ by $Y_i \to Y$. If $f$ is a local complete intersection morphism, then each $f_i$ is a local complete intersection morphism by Lemma \ref{lemma-flat-base-change-lci}. \medskip\noindent Conversely, assume each $f_i$ is a local complete intersection morphism. We may replace the covering by a refinement (again because flat base change preserves the property of being a local complete intersection morphism). Hence we may assume $Y_i$ is a scheme for each $i$, see Topologies on Spaces, Lemma \ref{spaces-topologies-lemma-refine-fpqc-schemes}. Choose a scheme $V$ and a surjective \'etale morphism $V \to Y$. Choose a scheme $U$ and a surjective \'etale morphism $U \to V \times_Y X$. We have to show that $U \to V$ is a local complete intersection morphism of schemes. By Topologies on Spaces, Lemma \ref{spaces-topologies-lemma-recognize-fpqc-covering} we have that $\{Y_i \times_Y V \to V\}$ is an fpqc covering of schemes. By the case of schemes (More on Morphisms, Lemma \ref{more-morphisms-lemma-descending-property-lci}) it suffices to prove the base change $$ U \times_Y Y_i = U \times_V (V \times_Y Y_i) \longrightarrow V $$ of $U \to V$ by $V \times_Y Y_i \to V$ is a local complete intersection morphism. We can write this as the composition $$ U \times_Y Y_i \longrightarrow (V \times_Y X) \times_Y Y_i = V \times_Y X_i \longrightarrow V \times_Y Y_i $$ The first arrow is an \'etale morphism of schemes (as a base change of $U \to V \times_Y X$) and the second arrow is a local complete intersection morphism of schemes as a flat base change of $f_i$. The result follows as being a local complete intersection morphism is syntomic local on the source and since \'etale morphisms are syntomic (More on Morphisms, Lemma \ref{more-morphisms-lemma-lci-syntomic-local-source} and Morphisms, Lemma \ref{morphisms-lemma-etale-syntomic}).
This lemma is the analogue of More on Morphisms, Lemma \ref{more-morphisms-lemma-base-change-lci-fibres} and in fact we will deduce the lemma from it. By Definition \ref{definition-lci} the set $\{x \in |X| : f \text{ is Koszul at }x\}$ is open in $|X|$ hence by Properties of Spaces, Lemma \ref{spaces-properties-lemma-open-subspaces} it corresponds to an open subspace $U(f)$ of $X$. Hence we only need to prove the final statement. \medskip\noindent Choose a scheme $W$ and a surjective \'etale morphism $W \to Z$. Choose a scheme $V$ and a surjective \'etale morphism $V \to W \times_Z Y$. Choose a scheme $U$ and a surjective \'etale morphism $U \to V \times_Y X$. Finally, choose a scheme $W'$ and a surjective \'etale morphism $W' \to W \times_Z Z'$. Set $V' = W' \times_W V$ and $U' = W' \times_W U$, so that we obtain surjective \'etale morphisms $V' \to Y'$ and $U' \to X'$. We will use without further mention an \'etale morphism of algebraic spaces induces an open map of associated topological spaces (see Properties of Spaces, Lemma \ref{spaces-properties-lemma-etale-open}). Note that by definition $U(f)$ is the image in $|X|$ of the set $T$ of points in $U$ where the morphism of schemes $U \to V$ is Koszul. Similarly, $U(f')$ is the image in $|X'|$ of the set $T'$ of points in $U'$ where the morphism of schemes $U' \to V'$ is Koszul. Now, by construction the diagram $$ \xymatrix{ U' \ar[r] \ar[d] & U \ar[d] \\ V' \ar[r] & V } $$ is cartesian (in the category of schemes). Hence the aforementioned More on Morphisms, Lemma \ref{more-morphisms-lemma-base-change-lci-fibres} applies to show that $T'$ is the inverse image of $T$. Since $|U'| \to |X'|$ is surjective this implies the lemma.
This follows from the corresponding result for morphisms of schemes, see More on Morphisms, Lemma \ref{more-morphisms-lemma-unramified-lci}, by \'etale localization, see Lemma \ref{lemma-universal-thickening-localize}. (Note that in the situation of this lemma the morphism $V \to U$ is unramified and a local complete intersection morphism by definition.)
Choose a scheme $U$ and a surjective \'etale morphism $U \to X$. Choose a scheme $V$ and a surjective \'etale morphism $V \to U \times_X Y$. Choose a scheme $W$ and a surjective \'etale morphism $W \to V \times_Y Z$. By Lemma \ref{lemma-universal-thickening-localize} the morphisms $W \to V$ and $V \to U$ are formally unramified. Moreover the sequence $i^*\mathcal{C}_{Y/X} \to \mathcal{C}_{Z/X} \to \mathcal{C}_{Z/Y} \to 0$ restricts to the corresponding sequence $i^*\mathcal{C}_{V/U} \to \mathcal{C}_{W/U} \to \mathcal{C}_{W/V} \to 0$ for $W \to V \to U$. Hence the result follows from the result for schemes (More on Morphisms, Lemma \ref{more-morphisms-lemma-transitivity-conormal-lci}) as by definition the morphism $W \to V$ is a local complete intersection morphism.
By Morphisms of Spaces, Lemma \ref{spaces-morphisms-lemma-where-unramified} there exists an open subspace $U(f) \subset X$ which is the set of points where $f$ is unramified. Moreover, formation of $U(f)$ commutes with arbitrary base change. Let $W \subset Z$ be the open subspace (see Properties of Spaces, Lemma \ref{spaces-properties-lemma-open-subspaces}) with underlying set of points $$ |W| = |Z| \setminus |p|\left(|X| \setminus |U(f)|\right) $$ i.e., $z \in |Z|$ is a point of $W$ if and only if $f$ is unramified at every point of $X$ above $z$. Note that this is open because we assumed that $p$ is closed. Since the formation of $U(f)$ commutes with arbitrary base change we immediately see (using Properties of Spaces, Lemma \ref{spaces-properties-lemma-factor-through-open-subspace}) that $W$ has the desired universal property.
After replacing $Z$ by the open subspace found in Lemma \ref{lemma-where-unramified} we may assume that $f$ is already unramified; note that this does not destroy assumption (2) or (3). By Morphisms of Spaces, Lemma \ref{spaces-morphisms-lemma-diagonal-unramified-morphism} we see that $\Delta_{X/Y} : X \to X \times_Y X$ is an open immersion. This remains true after any base change. Hence by Morphisms of Spaces, Lemma \ref{spaces-morphisms-lemma-universally-injective} we see that $f_{Z'}$ is universally injective if and only if the base change of the diagonal $X_{Z'} \to (X \times_Y X)_{Z'}$ is an isomorphism. Let $W \subset Z$ be the open subspace (see Properties of Spaces, Lemma \ref{spaces-properties-lemma-open-subspaces}) with underlying set of points $$ |W| = |Z| \setminus |p_2|\left(|X \times_Y X| \setminus \Im(|\Delta_{X/Y}|)\right) $$ i.e., $z \in |Z|$ is a point of $W$ if and only if the fibre of $|X \times_Y X| \to |Z|$ over $z$ is in the image of $|X| \to |X \times_Y X|$. Then it is clear from the discussion above that the restriction $p^{-1}(W) \to q^{-1}(W)$ of $f$ is unramified and universally injective. \medskip\noindent Conversely, suppose that $f_{Z'}$ is unramified and universally injective. In order to show that $Z' \to Z$ factors through $W$ it suffices to show that $|Z'| \to |Z|$ has image contained in $|W|$, see Properties of Spaces, Lemma \ref{spaces-properties-lemma-factor-through-open-subspace}. Hence it suffices to prove the result when $Z'$ is the spectrum of a field. Denote $z \in |Z|$ the image of $|Z'| \to |Z|$. The discussion above shows that $$ |X_{Z'}| \longrightarrow |(X \times_Y X)_{Z'}| $$ is surjective. By Properties of Spaces, Lemma \ref{spaces-properties-lemma-points-cartesian} in the commutative diagram $$ \xymatrix{ |X_{Z'}| \ar[d] \ar[r] & |(X \times_Y X)_{Z'}| \ar[d] \\ |p|^{-1}(\{z\}) \ar[r] & |p_2|^{-1}(\{z\}) } $$ the vertical arrows are surjective. It follows that $z \in |W|$ as desired.
We will use the characterization of closed immersions as universally closed, unramified, and universally injective morphisms, see Lemma \ref{lemma-characterize-closed-immersion}. First, note that since $p$ is universally closed and $q$ is separated, we see that $f$ is universally closed, see Morphisms of Spaces, Lemma \ref{spaces-morphisms-lemma-universally-closed-permanence}. It follows that any base change of $f$ is universally closed, see Morphisms of Spaces, Lemma \ref{spaces-morphisms-lemma-base-change-universally-closed}. Thus to finish the proof of the lemma it suffices to prove that the assumptions of Lemma \ref{lemma-where-unramified-universally-injective} are satisfied. The projection $\text{pr}_0 : X \times_Y X \to X$ is universally closed as a base change of $f$, see Morphisms of Spaces, Lemma \ref{spaces-morphisms-lemma-base-change-universally-closed}. Hence $X \times_Y X \to Z$ is universally closed as a composition of universally closed morphisms (see Morphisms of Spaces, Lemma \ref{spaces-morphisms-lemma-composition-universally-closed}). This finishes the proof of the lemma.
By Lemma \ref{lemma-base-change-flatness-fibres} the set $$ A = \{x \in |X| : X\text{ flat at }x \text{ over }Y\}. $$ is open in $|X|$ and its formation commutes with arbitrary base change. Let $W \subset Z$ be the open subspace (see Properties of Spaces, Lemma \ref{spaces-properties-lemma-open-subspaces}) with underlying set of points $$ |W| = |Z| \setminus |p|\left(|X| \setminus A\right) $$ i.e., $z \in |Z|$ is a point of $W$ if and only if the whole fibre of $|X| \to |Z|$ over $z$ is contained in $A$. This is open because $p$ is closed. Since the formation of $A$ commutes with arbitrary base change it follows that $W$ works.