The problem was that this setting was incorrect because the RHS of the rule requires a vbundle, not a metric. This was breaking the canonicalization algorithm.
SetOptions[ToCanonical,UseMetricOnVBundle->g];
In fact, when working with partial derivatives of the metric, it is better not to allow metric lowering and raising of indices.

Load packages

<<xAct`xTensor`

Configuration

$PrePrint=ScreenDollarIndices;​​$RiemannSign=-1;
SetOptions[ToCanonical,UseMetricOnVBundle->{}];​​SetOptions[ContractMetric,OverDerivatives->True];

Initialize fundamental quantities

(*Dimensionofthemanifold*)​​DefConstantSymbol[dim];​​$Assumptions=dim>2;
(*Manifold*)​​DefManifold[M,dim,Join[IndexRange[a,e],IndexRange[i,n]]];
(*Lorentzianmetric*)​​DefMetric[-1,g[-a,-b],lc];
(*Metricaffinecovariantderivative*)​​DefCovD[cd[-a],Torsion->True,{"|",""}];

Initialize additional tensors

(*Torsionvector
T
a
*)​​DefTensor[T[-a],M];
(*
f
a
*)​​DefTensor[f[-a],M];
(*Shifttensor
a
S
bc
*)​​DefTensor[S[a,-b,-c],M,Symmetric[{-b,-c}]];

Define tensors

(*Shifttensor:
a
S
bc
=
a
δ
b
f
c
+
a
δ
c
f
b
-
g
bc
a
f
*)​​IndexSet[S[a_,-b_,-c_],g[a,-b]f[-c]+g[a,-c]f[-b]-g[-b,-c]f[a]];
(*Metricaffineconnection:
a
Γ
bc
=
a
{
bc
+
1
D-1
T
b
a
δ
c
+
1
D-2
a
S
bc
*)​​rule=IndexRuleChristoffelcd[a_,-b_,-c_],Christoffellc[a,-b,-c]+
1
dim-1
T[-b]g[a,-c]+
1
dim-2
S[a,-b,-c];

Compute curvature related quantities

Riemann:
Riemanncd[-c,-d,-b,a]-Riemannlc[-c,-d,-b,a]//RiemannToChristoffel
-
Γ[]
a
d
e
Γ[]
e
c
b
+
Γ[]
a
c
e
Γ[]
e
d
b
+
Γ[▽]
a
d
e
Γ[▽]
e
c
b
-
Γ[▽]
a
c
e
Γ[▽]
e
d
b
+
∂
c
Γ[]
a
d
b
-
∂
c
Γ[▽]
a
d
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im
∂
c
f
b
-2+dim
+
∂
c
T
b
-1+dim
Ricci scalar:
g[b,c]%//ContractMetric//Simplification
1
-2+dim
-(-1+dim)
Γ[▽]
a
b
b
f
a
+
Γ[▽]
b
a
b
f
a
-
Γ[▽]
b
b
a
f
a
-
Γ[▽]
b
a
b
f
a
+dim
Γ[▽]
b
a
b
f
a
-
f
a
f
a
+dim
f
a
f
a
-2
∂
a
f
a
+2dim
∂
a
f
a
+
f
a
g
b
c
∂
a
g
b
c
-2
f
a
g
b
c
∂
c
g
a
b
+dim
f
a
g
b
c
∂
c
g
a
b

%//ChristoffelToGradMetric//ContractMetric//Simplification
(-1+dim)(
f
a
f
a
+2
∂
a
f
a
+
f
a
g
b
c
∂
a
g
b
c
)
-2+dim