[BZ-999360] avoid infinite loop when using declarative agenda
(cherry picked from commit 1db3ccc4245888a80e913c8b38d295add850dcdc)