Just weeks before Ecuador descended into chaos, with prison riots, two escaped criminal kingpins and the brief siege of a television station, the country’s top prosecutor launched a major operation aimed at rooting out narco-corruption at the highest levels of government.

The investigation, called “Caso Metastasis,” led to raids across Ecuador and more than 30 arrests.

Among those charged were judges accused of granting gang leaders favorable rulings, police officials who were said to have altered evidence and delivered weapons to prisons, and the former director of the prison authority himself, who was accused of giving special treatment to a powerful drug trafficker.

They had been implicated by text chats and call logs retrieved from cellphones belonging to the drug trafficker, who was murdered while imprisoned.

When the attorney general, Diana Salazar, announced the charges last month, she said the investigation had revealed the spread of criminal groups through Ecuador’s institutions. She also warned of a possible “escalation in violence” in the days to come, and said that the executive branch had been put on alert.

This week, her prediction came true.

Interviews with security experts and intelligence sources reveal what might have set off the violence in Ecuador this week, which was so intense that it prompted the president, Daniel Noboa, to declare war on the gangs and impose a state of emergency.

According to the interviews, the attorney general’s investigation played a pivotal role.

“Metastasis is where everything starts,” said Mario Pazmiño, a retired colonel and former director of intelligence for the Ecuadorean Army who is an independent analyst on security matters.