A federal ranger, tasked with patrolling a swath of land along the California-Mexico border, parks his car on the Embarcadero across from San Francisco’s Pier 5, where the vehicle is broken into and his secondary service gun is stolen.

Four days later, the gun is found less than a mile away on the Embarcadero by a homeless Mexican citizen who had come into the country illegally and was being sought for deportation.

The series of events, outlined Thursday in a San Francisco courtroom, would lead to the killing of Kate Steinle, as she strolled on Pier 14 with her father, on July 1, 2015. The crime would fuel a national debate about immigration and sanctuary laws.

But the seemingly random connections that emerged on the fourth day in Jose Ines Garcia Zarate’s murder trial hint that the case was steeped in the issue of immigration even before then.

Prosecutors and the defense both have worked to separate the criminal proceedings from the politics, asking jurors to instead focus on the main question in the trial: Whether the 45-year-old defendant had intentionally fired the shot that killed Steinle and should be convicted of second-degree murder, or if it were an accident — manslaughter — as Garcia Zarate’s attorneys contend.

John William Woychowski Jr., a ranger for the federal Bureau of Land Management, took the stand Thursday to answer Assistant District Attorney Diana Garcia’s questions about the car break-in that resulted in the theft of his .40-caliber Sig Sauer handgun four days before Steinle was shot.

He testified that he was traveling to an assignment in Montana, and brought his fiancé and three children with him. They stopped in San Francisco for dinner at about 9:30 p.m. on June 27, 2015, and he chose to park on the Embarcadero because it was well-lit and felt safe.

Woychowski tucked the backpack that held his secondary handgun, which was fully loaded with one in the chamber and a full magazine of seven rounds, behind the driver’s side seat because the back windows were tinted and driver’s seat pushed back automatically when the car parked. He said he spoke to a man he believed to either be a parking enforcement officer or a security guard, who told him to return by 11 p.m.

When he and the family returned, the window closest to where the backpack was placed was smashed and black glass littered the ground.

Garcia Zarate’s attorney, Matt Gonzalez, said in his opening statement that Woychowski is not on trial, but he has long maintained that the issue around the case was less about immigration and more about gun safety. On Thursday, the attorney tried to ask Woychowski why he believed it was safe to leave a fully loaded gun in a backpack behind the driver’s side seat, but Garcia, the prosecutor, repeatedly objected to the questioning with success.

“Do you believe you bear any responsibility for what happened with your firearm four days later?” Gonzalez asked. Garcia objected, and Woychowski did not have to answer the question.

Gonzalez says the gun accidentally discharged in Garcia Zarate’s hands after he found it wrapped in a T-shirt or cloth under a bench, and is expected to call expert witnesses to testify that the particular gun model used in Steinle’s death has a hair trigger and is prone to accidental discharges.

In his cross examination of Woychowski, Gonzalez attempted to establish whether the gun had been set in single-action mode or double-action mode. A gun in double-action mode requires extra pressure on the trigger to cock the hammer and fire the bullet. A gun in single-action mode already has the hammer cocked and requires less trigger pressure.

Woychowski testified that he always left that gun in double-action mode. Outside court, Gonzalez said he wasn’t satisfied with his answer — and either way, no one knows what happened to the gun in the four days before the shooting

The Steinle family is suing the Bureau of Land Management in a lawsuit that is still ongoing. Following Steinle’s death, legislators pushed to tighten requirements for securing weapons, especially in unattended cars, for gun owners and law enforcement officers.

Garcia Zarate had been on track for a sixth deportation when the Sheriff’s Department, relying on San Francisco’s sanctuary policies, released him from County Jail in spring 2015 despite a federal request to hold him for deportation. He had ended up in the city on a transfer from federal custody in March 2015 on an old warrant alleging that he fled marijuana charges in 1995.

The case sparked a push against the policies, and the House of Representatives in June passed “Kate’s Law,” which would boost punishment for people who repeatedly enter the U.S. illegally.

San Francisco has stood by its sanctuary laws, with officials saying they benefit public safety by ensuring that immigrants, including witnesses to crimes, feel comfortable working with authorities. Gov. Jerry Brown this month signed a bill to create a statewide sanctuary policy.

Vivian Ho is a San Francisco Chronicle staff writer. Email: vho@sfchronicle.com

Twitter: @VivianHo