I am not sure if, following Dan's advice, you were able to correctly rescore the lattices; I was wondering, reading your message, how you were able, in certain cases, to correctly enter the secondary fst if 1) your lattice does not contain any "nonterm" output and 2) your secondary fst does and at the very beginning; I was thinking that a matching failure would always occur during composition
(btw, I'm not sure why you need to invert the fst before replace)
this grammar-fst lattice rescoring task has never been adressed in a definitive way, if I have not missed something; and I think it might be something as simple as an epsilon arc introduced by fstreplace while transitioning the secondary fst that always causes matching failures
I actually came up with something different in the implementation but that should be conceptually similar (or equivalent)
1- I retain all non-terminals in the output (nonterm_unk, nonterm_begin, nonterm_end)
2- I depth-visit the lattice and mark each arc as generated by whatever fst, which I know because the nonterminals arcs can give me that info
3- I compose lattice arcs with corresponding arcs of the corresponding fst (this is done by for subtracting and adding, so it's the usual rescoring pipeline)
then again, you need to be careful about matching at the transition; fstconcat of nonterm_begin and the n-gram secondary fst actually generates and epsilon arc between the two and since I use BackoffDeterministicOnDemandFst that arc was considered the back off arc, so I was never really composing with the actual secondary n-gram.
I suspect my struggle with fstreplace might have been related to just a similar issue