Like the 2-year-old girl he allegedly kicked and stomped to death last week, Cody Feran-Baum, 17, of Minneapolis had drawn the attention of Hennepin County child protection.

As far back as 2012, the county began a process to remove the then-14-year-old from his dangerous living situation. Feran-Baum told staffers that his father left him with girlfriends and strangers to sell drugs, court documents say. He also reported that his mother had been a drug addict for as long as he could remember, and that she sometimes hallucinated he had a twin brother.

His unstable home life led to drug use and poor school attendance, and he was caught at school with a knife, the ­documents say.

In 2013, his grandmother, Barbara Whetstone, asked for and got legal custody of Feran-Baum. The court looked to her as someone it said had started to stabilize his chaotic life.

But the court acknowledged that Whetstone, too, had been the subject of child protection investigations. Officials also stated they had grave concerns for the welfare of her 2-year-old granddaughter, Maci, in 2012 because Whetstone’s live-in boyfriend had a long criminal history.

Feran-Baum now sits in a juvenile detention center, charged with second-degree murder in the death of 2-year-old Sophia O’Neill. He told police that when she wouldn’t stop crying, he kicked her in the back and stomped on her stomach, an attack that the medical examiner said tore organs in half and broke ribs.

“It is clear that this child, Cody, has suffered greatly throughout the years without proper intervention,” say court documents filed before Sophia’s death.

While child protection worked to find a safer home for Feran-Baum, it appears that ­little action was taken for Sophia. Her father, Max O’Neill, 22, who shared custody of the child with her 20-year-old mother, has said that child protection received at least two reports, the most recent in February, about bruises on the girl’s face.

Feran-Baum had been dating Sophia’s mother, Sunshine Smith, for the past year, and mother and child were staying with him at his and Whetstone’s home in the 3800 block of 30th Avenue S., where the fatal beating occurred.

“There is no justifying what Cody did,” said Rex Holzemer, assistant Hennepin County administrator of human services. “But when you start digging into his background and see what he faced, you can somewhat understand what happened might have come out of it.”

Kristina Baum and Gerald Feran, Feran-Baum’s parents; Whetstone and Smith couldn’t be reached for comment. Whetstone, 57, previously told the Star Tribune that Feran-Baum loved Sophia and that she would continue to support him. Whetstone and Baum appeared with Feran-Baum during his court appearance.

The most recent documents that mention Feran-Baum’s interaction with child protection were filed in 2012. The documents say child protection had received at least 10 reports about his parents and Whetstone since 1998. When Whetstone petitioned for custody of the teen, his parents were either in prison or jail, and she admitted that she had been the subject of child ­protection investigations.

Jean Peterson, a court-appointed guardian ad litem, did an investigative report on Whetstone, Feran-Baum and his family. The report said Feran didn’t mind Whetstone having temporary custody of his son, but that he wanted him back after his release from prison. Baum refused to cooperate with Peterson.

Feran-Baum had seen his mother overdose on drugs, twitching on the ground and foaming from the mouth, Peterson said. He wanted a relationship with his father, but believed it was only a ­matter of time before he went back to dealing drugs.

Peterson listed the many reports child protection had received since 1998 about Whetstone and her boyfriend, who still lives in her home. The reports — several of which triggered long investigations — ranged from allegations of sexual abuse to violent behavior involving Whetstone’s boyfriend’s young son.

Peterson said Whetstone would be an appropriate permanent placement for Feran-Baum, but “family court is not equipped to assess her ability to care for him, given her own troubled past.”

In November 2012, Judge Anne McKeig ordered a “private” CHIPS (child in need of protection or services) petition on Feran-Baum, which is done by the court when a child is placed in foster care or a family won’t cooperate with voluntary protection services. A case plan involves scheduled court appearances, with the goal of reuniting the family.

It is unclear who sought the petition on Feran-Baum’s behalf. A private CHIPS petition is a rare child-protection remedy. Almost all petitions are filed by family services, recommended by the county attorney’s office and approved by the court. A private petition is filed by any reputable person who has reported to family services that a child needs protection, but wasn’t satisfied with the response. The court still must approve the petition.

When Whetstone gained custody of Feran-Baum in September 2013, Peterson said in a court document that there wasn’t a need to file a CHIPS petition because Whetstone has been “appropriately attending all of the minor child’s needs and that as a result, the minor child is doing very well.”

He had attended inpatient treatment for his marijuana use and had passed a series of drug tests. Peterson said he appeared to be hopeful and happy. He made eye contact, stood up straighter and spoke proudly about being clean and sober, she said.

Such behavior was markedly different from that described in several reports Peterson had written earlier in 2013. He had been suspended from high school for smoking marijuana and bringing a knife, court documents said. A school therapist had diagnosed anxiety, ADHD and learning disabilities.

Whetstone said in 2013 that her male friend whom the court had been concerned about was no longer in her life, court documents said. But he was at her home the day after Sophia died.

“This is my house,” Feran-Baum was quoted as saying in the 2013 documents. “She puts clothes on my back and food in my stomach. I feel safe with her.”

He, Sophia and her mother, Sunshine Smith, were living in Whetstone’s house when the baby was rushed to the hospital June 10. That was two days after Feran-Baum allegedly kicked and stomped the girl. She died of multiple blunt-force injuries, the medical examiner’s office reported.

Feran-Baum didn’t appear to have contact with child protection after 2013.

Holzemer, of county human services, said even when all of the proper steps are taken, including supervision by case workers and the court, “some things can happen in ways we regret,” he said. “Frankly, we can’t monitor people’s daily actions all the time.”

Feran-Baum may be accused of killing somebody, but he’s a victim, too, Holzemer said. “This was a tragedy for both the little girl and Cody,” he said. “This was two tragedies that intersected at the wrong place.”