After that drive, Ms. Barra made her own decision to speed up, convinced that such cars were worth betting the company on. Within six months after what she called her "aha! moment" in San Francisco, a fleet of self-driving Chevrolet Bolts, the company's new electric car, was being built at a G.M. assembly plant in Michigan, the pace accelerated at the direction of Ms. Barra and her senior management team.

Driving in a situation like that, "you have to make a decision," she recalled in a recent interview. "Generally if you decide to go, you decide to speed up. Or you stop." If the technology works, she said, it will make the right decision: "The car knows."

She wanted to see for herself whether automation was ready to take over from a driver — safely, and on a mass scale. How would it react, for example, when it reached an intersection as a light turned yellow?

Mary T. Barra, a G.M. lifer who had worked her way from engineer to the top, was in the back seat of a prototype self-driving electric car as it wound its way through the city's downtown a year ago.

The chief executive of General Motors , an automaker synonymous with Detroit, saw the future of driving not in the Motor City but on the streets of San Francisco.

I do believe General Motors is a tech company. We put these products on the road that integrate 30,000 parts and have hundreds of millions of lines of code in them already. And we have to make them durable and work in all environments.

It was a first for any major car company, and the first leg of a race she is determined to win. The question now is whether a company identified with the industry's bygone glory days can be a trendsetter in 21st-century transportation — and beat out Silicon Valley rivals like Google, Tesla and Uber with no legacy business to encumber them.

"The auto industry is on the cusp of significant change, and G.M. has to prove that a longtime established player can be up to the task," said Michelle Krebs, a senior analyst with the car-shopping site Autotrader, who has followed the company since the 1980s. "If you look at their past performance, their record has been spotty at best."

General Motors is making a big wager that it can succeed, shedding overseas operations while investing $600 million this year in self-driving cars and other advanced technologies. It spent $1 billion on Cruise Automation, a Silicon Valley start-up that developed the driverless technology powering Ms. Barra's ride in San Francisco.

It is a perilous challenge — balancing the demands of a global automotive business with an aggressive push into expensive high-tech models — that has already claimed victims. Last month, Ford Motor ousted its chief executive, Mark Fields, aiming to send a signal that it could keep pace.

But G.M. is making a case that it can be a leader in the auto industry of both today and tomorrow. "We are very, very serious and intent on putting something on the road," Ms. Barra said of the company's automated vehicles. "We definitely want to be first."

Like its rivals, G.M. has ridden the economic recovery and low gas prices to record demand and profits. But it faces headwinds as sales flatten and new technologies increasingly cast a shadow over its traditional business. Tesla, the electric-car upstart, is now valued more highly than any Detroit automaker.

In shaping its future, G.M. is betting on products that have yet to gain a foothold in the marketplace and could take years to develop fully.

It is doing so while enduring criticism from President Trump for building vehicles in Mexico and for laying off workers at home. At the same time, Ms. Barra has built a closer relationship with Mr. Trump than other auto executives have, serving on his business advisory panel and taking the lead for the industry in talks on fuel-economy rules.

The company is also fending off a campaign by a dissident investor to create two classes of stock, aimed at increasing G.M.'s value to shareholders. One class would pay a steady dividend, while the other would appreciate based on the company's growth. The fight will culminate on Tuesday at the annual shareholder meeting.

In a letter to investors last week, the hedge fund that floated the dual-class proposal, Greenlight Capital, called G.M.'s board — including Ms. Barra, its chairwoman — "uninspired" for opposing its proposal. "After seven years of a flat stock price, how much longer must shareholders wait?" the letter said.

While she agrees the company is undervalued, Ms. Barra insists that G.M.'s performance will ultimately pay off in better returns. "Does it frustrate me?" she said of the stock price. "Yes, but I want to do the right things over the long term to improve the business."

In beginning to assemble fully automated Bolts in January, G.M. was a step ahead of Google and Uber, which are converting mass-market minivans and sedans into driverless models. It went beyond what Tesla has achieved with autonomous controls on its own models. And it reflected the feverish competition underway.

"We don't go in to compete," Ms. Barra said. "We don't go in to have an entry. With every new product we're doing, we are going in to win."

For Ms. Barra, 55, who took the helm at G.M. in 2014 as the first female chief executive of a major automaker, it is the latest chapter in the company's long comeback.

Eight years ago, G.M. was so deeply debt-ridden and burdened by high costs and faltering brands that some thought it would be left for dead. It was rescued through a government-engineered bankruptcy, only to find itself under fire over defective ignitions that were tied to 124 deaths.

Under Ms. Barra, the company weathered that crisis and is moving to become leaner and stronger. It is selling off its money-losing European division and unwinding its operations in other international markets like Australia, India and Russia. It has cut unprofitable sales of vehicles to rental fleets and reduced shifts and workers at some American factories that make slow-selling cars.

"G.M. used to constantly talk about where we were going to spend more money," said Mark L. Reuss, the company's executive vice president for product development. "What we were doing was robbing our good businesses to fix the broken ones."

As G.M. streamlines operations in the United States and elsewhere, it is redirecting energy and capital to its self-driving initiative.

"I do believe General Motors is a tech company," Ms. Barra said. "We put these products on the road that integrate 30,000 parts and have hundreds of millions of lines of code in them already. And we have to make them durable and work in all environments."

Its commitment is on display at a sprawling plant in Orion Township, Mich., about a 45-minute drive northwest of Detroit, where the company builds conventional subcompact cars as well as Bolts.

One area of the factory is dedicated to a small-scale assembly line where workers customize Bolts with self-driving equipment, including a large computer in the trunk and modules packed with sensors on the roof and fenders.

"We're on the cutting edge here," said Bill Rock, a 22-year G.M. veteran who works on the autonomous vehicle line. "It's a very complicated process, but it's the direction the company needs to go."

G.M. has built about 140 autonomous Bolts to be used for testing in Arizona, California and Michigan by Cruise Automation, now a subsidiary. Kyle Vogt, a founder of Cruise, said G.M. had given its engineers a distinct advantage by building a state-of-the-art autonomous vehicle, rather than retrofitting a conventional car.

"These vehicles coming off the assembly line have been built from the ground up," he said. "That's a real car, and not a mash-up of off-the-shelf parts that need to be integrated into the system."

Ms. Barra's view of what G.M. can accomplish was forged in her years as one of its first female plant managers. And she believes G.M. can lead the self-driving revolution without additional acquisitions or partners. "We have everything we need," she said.

Jeffrey A. Sonnenfeld, a business professor at Yale and a leadership expert who has known most of Ms. Barra's predecessors dating back decades, is convinced of her resolve. "Mary is the right person at the right time, and she does it without an ounce of showmanship," he said.

The challenge of autonomous vehicles is hardly her first test as chief executive. Her baptism by fire came during the congressional hearings and government investigations surrounding the faulty ignition switch.

Ms. Barra methodically directed the company to fix the defective parts, revamp its safety procedures, compensate accident victims and their families and negotiate settlements and fines with regulators and the Justice Department.

She says the episode informed her approach to self-driving cars, which she believes herald a new era of road safety, and left her impatient for results. And she says she is determined for G.M. to lead the technological revolution changing the industry.

"If anyone can," she said, "why shouldn't we be the ones to do it?"