4. Given the sequence $\left\{a_{n}\right\}$ satisfies
$$
a_{1}=a_{2}=3, a_{n+2}=3 a_{n+1}-2 a_{n}-1 \text {, }
$$

where, $n \in \mathbf{N}_{+}, S_{n}$ is the sum of the first $n$ terms of $\left\{a_{n}\right\}$. Then the maximum value of $S_{n}$ is $\qquad$ .
4. 8 .

From the given information, we have
$$
\begin{array}{l}
a_{n+2}-a_{n+1}-1=2\left(a_{n+1}-a_{n}-1\right) \\
=2^{n}\left(a_{2}-a_{1}-1\right)=-2^{n} \\
\Rightarrow a_{n}-a_{n-1}=1-2^{n-2} \\
\Rightarrow a_{n}=n+3-2^{n-1}\left(n \in \mathbf{N}_{+}\right)
\end{array}
$$

Therefore, $a_{3}=2$.
Also, when $n \geqslant 4$,
$$
\begin{array}{l}
2^{n-1}-(n+3)=(1+1)^{n-1}-(n+3) \\
\geqslant 2\left(1+C_{n-1}^{1}\right)-(n+3)=n-3>0,
\end{array}
$$

which means $a_{n}<0$.
Hence, $\left(S_{n}\right)_{\max }=S_{3}=8$.