  The origin of ultra-high energy cosmic rays $($UHECRs$)$ remains a mystery.
It has been suggested that UHECRs can be produced by the stochastic
acceleration in relativistic jets of gamma-ray bursts $($GRBs$)$ at the early
afterglow phase. Here, we develop a time-dependent model for proton
energization by cascading compressible waves in GRB jets considering the
concurrent effect of the jet's dynamics and the mutual interactions between
turbulent waves and particles. Considering fast mode of magnetosonic wave as
the dominant particle scatterer and assuming interstellar medium $($ISM$)$ for
the circumburst environment, our numerical results suggest that protons can be
accelerated up to $\textrm{10}^{\textrm{19}}\,$eV during the early afterglow.
An estimation shows ultra-high energy nuclei can easily survive
photodisintegration in the external shocks in most cases, thus allowing the
acceleration of $\textrm{10}^{\textrm{20}}\,$eV cosmic-ray nuclei in the
proposed frame. The spectral slope can be as hard as
$\textrm{d}\textit{N}/\textrm{d}\textit{E} \propto \textit{E}^\textrm{0}$,
which is consistent with the requirement for the interpretation of
intermediate-mass composition of UHECR as measured by the Pierre Auger
Observatory.
