  The covariant phase space method of Iyer, Lee, Wald, and Zoupas gives an
elegant way to understand the Hamiltonian dynamics of Lagrangian field theories
without breaking covariance. The original literature however does not
systematically treat total derivatives and boundary terms, which has led to
some confusion about how exactly to apply the formalism in the presence of
boundaries. In particular the original construction of the canonical
Hamiltonian relies on the assumed existence of a certain boundary quantity
"$B$", whose physical interpretation has not been clear. We here give an
algorithmic procedure for applying the covariant phase space formalism to field
theories with spatial boundaries, from which the term in the Hamiltonian
involving $B$ emerges naturally. Our procedure also produces an additional
boundary term, which was not present in the original literature and which so
far has only appeared implicitly in specific examples, and which is already
nonvanishing even in general relativity with sufficiently permissive boundary
conditions. The only requirement we impose is that at solutions of the
equations of motion the action is stationary modulo future/past boundary terms
under arbitrary variations obeying the spatial boundary conditions; from this
the symplectic structure and the Hamiltonian for any diffeomorphism that
preserves the theory are unambiguously constructed. We show in examples that
the Hamiltonian so constructed agrees with previous results. We also show that
the Poisson bracket on covariant phase space directly coincides with the
Peierls bracket, without any need for non-covariant intermediate steps, and we
discuss possible implications for the entropy of dynamical black hole horizons.
