AI: fix some bugs caught by asserts