4. Let point $P$ be on the ellipse $\frac{x^{2}}{a^{2}}+\frac{y^{2}}{b^{2}}=1(a>b>0$, $c=\sqrt{a^{2}-b^{2}}$ ), and the equation of line $l$ be $x=-\frac{a^{2}}{c}$. The coordinates of point $F$ are $(-c, 0)$. Draw $P Q \perp l$ at point $Q$. If points $P$, $F$, and $Q$ form an isosceles right triangle, then the eccentricity of the ellipse is ( ).
(A) $\frac{\sqrt{2}}{2}$
(B) $\frac{-1+\sqrt{2}}{2}$
(C) $\frac{-1+2 \sqrt{2}}{3}$
(D) $\frac{-1+\sqrt{5}}{2}$
4. A.

As shown in Figure 3, let $T$ be the intersection of line $l$ with the $x$-axis, and construct $P R \perp x$-axis at point $R$. From the problem, we know
$$
\begin{array}{l}
\angle P F Q=90^{\circ}, \\
P F=Q F, P Q \parallel R T .
\end{array}
$$

Then $T F=Q T=P R$
$$
=F R \text {. }
$$

Thus, we have $y=x+c=-c+\frac{a^{-}}{c}$ (where, $\left.P(x, y)\right)$. Hence $x=\frac{a^{2}}{c}-2 c, y=\frac{a^{2}}{c}-c$.
Substituting into the ellipse equation gives
$$
\left(\frac{a^{2}-2 c^{2}}{c a}\right)^{2}+\left(\frac{a^{2}-c^{2}}{c b}\right)^{2}=1 \text {, }
$$

which simplifies to $\left(\frac{a}{c}-2 \times \frac{c}{a}\right)^{2}+\frac{a^{2}-c^{2}}{c^{2}}=1$,
or $\left(\frac{1}{e}-2 e\right)^{2}+\frac{1}{e^{2}}-1=1$.
Rearranging yields $2\left(e^{2}\right)^{2}-3 e^{2}+1=0$.
Since $0<e^{2}<1$, we have $e^{2}=\frac{1}{2}, e=\frac{\sqrt{2}}{2}$.