I think I got it, when the contraction is complete, the simplification algorithm performs a complete contraction and returns a number. Unfortunately for the trace, this number multiplies a unit 4x4 matrix, whose trace is 4. My code ignores the unit matrix. The missing factor 4 fixes this problem.

Tomorrow I'll fix this.