Both Gen. David Petraeus and Gen. John Allen intervened in the same nasty child custody battle involving Natalie Khawam, the “psychologically unstable” twin sister of Jill Kelley, whose bombshell claims of being threatened by Petraeus’ lover led to the top spy’s resignation last week, the Post has learned.

Allen, the four-star general top commander in Afghanistan, was revealed last night to have exchanged thousands of pages of of emails with Kelley, who went to the feds after receiving threatening e-mails from Paula Broadwell, the married mistress of Petraeus.

A judge noted in the file that Khawam “has attached letters from Gen. David H. Petraeus averring to her ability to appropriately parent the child, and is prepared to present corroborating testimony at trial.”

And in court documents filed by Kelley’s sister Natalie Khawam, she name-drops both Sen. John Kerry of Massachusetts and Sen. Sheldon Whitehouse of Rhode Island — who both have ties to a Providence, RI, lawyer/Democratic fundraiser who loaned a whopping $300,000 to Khawam.

TWIST IN PETRAEUS SEX SCANDAL: AFGHAN COMMANDER NOW PROBED FOR RELATIONSHIP WITH JILL KELLEY

PETRAEUS GETS ‘PROMOTION’ IN NEW ‘CALL OF DUTY’ GAME

A spokesman for Whitehouse today that lawyer, Gerald Harrington, has dated and “may have been engaged to Khawam.” Harrington has not returned a call seeking comment.

Khawam claimed in a July 12 letter to her estranged husband that she took their now 4-year-old son “on vacation last year to Martha Vineyard,” where their son and “I had a great time at the DSCC [Democratic Senate Campaign Committee] event.”

“Sen. John Kerry asked if [her son] would be coming again this year,” Khawam wrote. “[Their son] was a superstar at the DSCC last year.”

A spokeswoman for Kerry – who the Washington Post reports is being considered as President Obama’s next secretary of defense — in an email comment wrote, “Senator Kerry’s friend Jerry Harrington introduced him to his girlfriend (Natalie) at a DSCC event.”

Also filed in that court case by Khawam is a letter from Whitehouse, who like Kerry is a Democrat.

That letter was written to Harrington, who has been a fundraiser for Kerry and other Democrats out of Rhode Island. Harrington, according to Khawam’s federal bankruptcy filing earlier this year in Florida, gave her a personal loan of $300,000.

“Derry Gerry,” Whitehouse wrote. “I am excited to hear that you and [Khawam’s son] may be coming to the Family Clambake. That would be terrific! All the best wishes, Sheldon.”

A related email from Khawam to her estranged husband — from whom she was seeking permission to take their son to Whitehouse’s annual fundraising clambake — said that their son “knows Sen. Whitehouse and his family from spending time together with them last summer in Newport, R.I.

A spokesman for Whitehouse confirmed that the senator had written that invitation at the request of Harrington.

“Gerry Harrington is a pretty prominent political activist in Rhode Island,” said Whitehouse’s spokesman “I think Gerry has either been dating or may have been engaged to Natalie . . . Sheldon has met Natalie through Gerry.”

Both Petraeus and Allen apparently decided intervene in the same nasty court fight involving Khawam’s 4-year-old, siding with the mother who, according to court documents, took her son to Florida from Washington, DC, when he was four months old after a heated argument with her husband.

The generals’ letters to the court — written in the past two months — supported a motion to overturn a ruling made nearly a year earlier by a judge who resoundingly denied custody to Khawam, because of serious reservations about her honesty and mental stability, court records show.

The father, Grayson Wolfe, was unable to see the child for more than a year, according to court documents. The judge overseeing the case cited Khawam with “outrageous conduct,” “bad faith litigation tactics,” and “illogical thinking,” awarding full custody to the father and socking the mom with $350,000 in legal fees in 2011.

The judge gave Wolfe sole custody of the couple’s son after finding that Khawam, a lawyer, repeatedly lied under oath and filed bogus domestic-violence and child-abuse claims against her husband after their one-year marriage began crumbling in 2009.

That judge also found that Khawam routinely defied court orders to let the child see his dad and sent harassing e-mails to Wolfe’s friends and business partners that “excoriated Mr. Wolfe for being a horrible father and husband.”

The judge blasted Khawam for giving false evidence, and noted that a court-ordered shrink had found her domestic-violence allegations to be “part of an ever-expanding set of sensational accusations . . . that are so numerous, so extraordinary and [so] distorted that they defy any common-sense view of reality.”

The judge also noted that she “is a psychologically unstable person.”

“My wife and I have known Natalie for approximately three years, getting to know her while serving in Tampa, Florida, through our friendship with Dr. and Mrs. Scott Kelley,”Petraeus wrote in a letterintroduced as part of a legal motion by Natalie Khawam’s lawyer.

“It is clear to me that [child’s name] would benefit from much more time with his Mother and from removal of the burdensome restrictions imposed on her when she does get to spend time with him,” Petraeus wrote.

Petraeus said he had observed Natalie and her son, “including when we hosted them and the Kelley family for Christmas dinner this past year. In each case, we have seen a very loving relationship – a Mother working hard to provide her son enjoyable, educational and developmental experiences,” he wrote.

“In view of this, it is unfortunate, in my view, that her interaction with her son has been so limited by the custody settlement,” Petraeus continued.

A separate letter from Allen is dated Sept. 22, two days after the Petraeus letter. “Natalie clearly loves [child’s name] and cherishes each and every opportunity she has to spend time with him. She is a dedicated mother,” Allen wrote. “In light of Natalie’s maturity, integrity and steadfast commitment to raising her child, I humbly request your reconsideration of the existing mandated custody settlement,” Allen wrote. He said he got to observe the mother and child “at command social functions.”

He signed his letter: “Gen. John R. Allen, General, United States Marine Corps,” on what appears to be official letterhead.

Allen’s letter does not mention any romantic relationship between himself and Kelley.

Petraeus, who just stepped down as CIA chief, signed his letter: “General, U.S. Army (Retired).

Politico reported that Gen. Allen, the top commander in Afghanistan and a former top deputy to Petraeus at Central Command in Tampa, exchanged “potentially inappropriate” emails with Kelley, citing a senior defense official.

Court lettes from Gens. Allen and Petraeus