  In his work on modularity theorems, Wiles proved a numerical criterion for a
map of rings $R\to T$ to be an isomorphism of complete intersections. He used
this to show that certain deformation rings and Hecke algebras associated to a
mod $p$ Galois representation at non-minimal level were isomorphic and complete
intersections, provided the same was true at minimal level.
  In this paper we study Hecke algebras acting on cohomology of Shimura curves
arising from maximal orders in indefinite quaternion algebras over the
rationals localized at a semistable irreducible mod $p$ Galois representation
$\overline \rho$. If $\overline \rho$ is scalar at some primes dividing the
discriminant of the quaternion algebra, then Hecke algebra is still isomorphic
to the deformation ring, but is not a complete intersection, or even
Gorenstein, so the Wiles numerical criterion cannot apply.
  We consider a weight 2 newform $f$ which contributes to the cohomology of the
Shimura curve and gives rise to an augmentation $\lambda_f$ of the Hecke
algebra. We quantify the failure of the Wiles numerical criterion at
$\lambda_f$ by computing the associated {\it Wiles defect} purely in terms of
the local behavior at primes dividing the discriminant of the global Galois
representation $\rho_f$ which $f$ gives rise to by the Eichler--Shimura
construction. One of the main tools used in the proof is Taylor--Wiles--Kisin
patching.
