I'm tired tonight, but @johnnyyukon might have a good time cracking this. I will sleep on it.They were not preparing for war here the Psalmist is captive. God did not protect Israel from the Babylon because he was passing judgment on them for their disobedience. At a time when Israel was very prosperous God told Israel not to make a deal with a nation because they were going to betray them. Israel didn’t listen so when it happened God didn’t protect them. He also told them they would be held captive by this nation for a number of years and afterwards he would restore the nation. So God is not about to kill babies for an insolent nation he is punishing. The Psalmist is clearly bitter about what happened and is cursing Babylon promising to remember everything that was done to the nation of Israel and promising to take revenge.
As for Numbers 31 yes people were killed It was necessary to establish the nation and it was culturally customary in that time to kill the male children. I believe this was so they wouldn’t rise up a seek revenge when they came of age. Also this was a time of patriarchal societies so male children wouldn’t customarily be assimilated in to the conquering culture. The order to Kill the male children was given by Moses and did not come directly from God. God often worked within the culture of the time. No blood shed was not ideal but he allowed it. We also know that those who shead blood could not have anything to do with the Arc of the Covenant which was the most sacred ritual and artifact of the Jews. The entire tribe of Levi that was consecrated to deal with the Arc of the convent was forbidden from ever going to war so that they would not have to participate in blood shed.
After the people were killed in Numbers everyone who did the killing was not allowed back into the camp until they were cleansed by a series of rituals. Everything that they took had to be cleansed by holy fire. Anything that couldn’t be cleansed by fire needed to be cleansed by water. The virgins that they brought back also needed to be cleansed.This is all evidence that though it might have been ordered and was necessary it was in no way ideal and needed to be atoned for. This also old testament law. We can see the progression throughout the Bible where practices that call for blood are no longer prohibited. The brutal nature of the Old Testament should be viewed in the same way we view hangings and executions in the medieval era. They were cultural practices that improved overtime.
Now the women that were killed conspired with Balaam's against Israel causing the nation to sin and break the covenant with God. Balaam kept trying to curse Israel after they over through a neighboring nation. Balaam feared that Israel would do the same to him so he tried to get a holy man to curse them. After several warnings from God himself and the Holy man Balaam was cursed and told that Israel would certainly destroy his nation. To avoid the curse Balaam attempted to merge the two nations so he and the woman of his nation conspired to seduced and whore with the nation of Israel. They also incited them to attend ceremonies where sacrifices were made to their Gods. The men took the women as wives. One of the women that ended up marrying into a family of a high ranking Israelite and she herself was the daughter of a high ranking family. The man brought the woman to a scared religious ceremony which broke convent with God. God ordered that all the women who conspired with Balaam as well as the men that received them and married them to be killed. Holding both the men of Israel and the women sent by Balaam accountable. Which brings up one of the verses you bolded Moses wanted the women that were brought back by his men killed because they also conspired with Balaam who God cursed so they couldn’t be spared.
Numbers 31: 15-16 Moses said to them, “Have you let all the women live? 16 Behold, these, on Balaam's advice, caused the people of Israel to act treacherously against the LORD in the incident of Peor, and so the plague came among the congregation of the LORD.
The male children were customarily killed and the virgins were adopted into the nation because they didn’t conspire with Balaam to whore with the Israelite men.
So essentially Balaam brought judgment on his entire nation by disobeying God after many warnings and trying to outsmart God by conspiring to corrupt the nation of Israel causing massive amounts of people from both nations to die. I think something like over 600k Israelite men were killed because the spelt with and married the Women