There is an unwritten code among the personal security details that protect people like the CIA director: What happens in the bubble stays in the bubble.

“Yes, there is a code,” said Howard Richards, who worked on the personal protective details of four CIA directors. “They want people around them to be people they can trust.”

The assumption, Richards said, is that anyone who spends private time with the boss “would be vetted by the director’s staff.” Never would he meet alone with someone without the knowledge of the security detail.

Even under those circumstances, opportunities exist for the kind of illicit relationship that cost David H. Petraeus his job as CIA director, according to people who have worked in the job and others who have witnessed the interaction between the director and those who protect him.

Petraeus, a retired four-star general, resigned Friday following 14 months at the CIA, after acknowledging an extramarital affair with Paula Broadwell, a former Army major who wrote a biography of him.

Associates of Petraeus said Broadwell’s role as biographer gave her unusual access to him, both when he was commander of U.S. and international forces in Afghanistan in 2010 and early 2011, and after he became CIA chief in mid-2011. They said the affair began soon after Petraeus became CIA director and ended about four months ago.

Although some have speculated about how the chief of an intelligence agency could maintain a secret relationship, interviews with Richards and others with knowledge of security practices say it is not hard.

Members of the CIA director’s security detail guard his office, but they are not inside with him at all times. They live in the basement of his private home, but they do not roam freely through the residence. They accompany him to personal and professional meetings, but they do not always go into the room. They protect his hotel suite, even deploying motion detectors in the hallway, but they do not step in uninvited. They ride in his agency-provided jet, but they are not usually inside his VIP cabin.

There are times when the director might require a completely private meeting with a jumpy foreign head of state, a nervous covert asset or an undercover U.S. government employee. The security detail is trained to protect and remain silent.

“They actually are willing to take bullets for the man — of course they aren’t going to tell,” said a former senior CIA official who worked closely with several directors. He spoke on the condition of anonymity to comment about security matters. “Their duty is clear: You don’t tell anyone anything.”

Broadwell spent hours alone with Petraeus interviewing him for the biography. This past summer, Broadwell told a reporter that she and Petraeus were considering working on a second book, which would have given her a reason for spending more time with him.

Infidelity in close proximity to armed bodyguards is nothing new. President Bill Clinton spent hours alone in the White House with an intern, Monica Lewinsky, with whom he engaged in what he described as an “improper relationship.”

In 2008, Eliot Spitzer resigned as governor of New York after he was caught on a federal wiretap booking a room at the Mayflower Hotel in Washington to meet a high-priced prostitute.

At the CIA, the people who guard the director are among the agency’s most highly vetted employees. They not only must pass extensive background checks for security reasons but also must be willing to put themselves in harm’s way to protect the boss. They shadow the director 24 hours a day, at home and abroad, on weekends and holidays. Most of the time, they must “remain in line of sight of the official they are protecting,” according to a description in a General Accounting Office report.

Just as important is an ability to fit in and maintain the trust of a public figure they sometimes see in pajamas — or asleep in bed next to his wife if they are required to wake him in an emergency. They witness family arguments, temper tantrums and, once in a while, the emotional breakdowns and extreme fatigue that come with a high-stress job.

Richards, now a sportscaster in Missouri, said it was not unusual to find himself engaged in personal exchanges with his boss, such as when he accompanied one of the directors on long hikes.

“These are very casual moments away from D.C., away from the media,” he said. “You share private moments. There’s bonding and fellowship. You share jokes and stories. That’s how close you become.”

From 1991 to 1999, Richards worked for four CIA directors — Robert M. Gates, James Woolsey, John Deutch and George Tenet. He called Petraeus’s infidelity on the job “unprecedented.”

Richards said that he did not condone Petraeus’s actions on a personal level but that an affair would be a “personal decision. It has nothing to do with the job.”

Only if the relationship created a possible security risk, such as being in an unsafe neighborhood or raising his public profile, would a security officer be required to report it to a supervisor, who would probably take the issue to the CIA’s chief of staff.