Nine days after Facebook chief executive Mark Zuckerberg dismissed as "crazy" the idea that fake news on his company's social network played a key role in the US election, President Barack Obama pulled the youthful tech billionaire aside and delivered what he hoped would be a wake-up call.
For months leading up to the vote, Obama and his top aides quietly agonized over how to respond to Russia's brazen intervention on behalf of the Donald Trump campaign without making matters worse. Weeks after Trump's surprise victory, some of Obama's aides looked back with regret and wished they had done more.
Now huddled in a private room on the sidelines of a meeting of world leaders in Lima, Peru, two months before Trump's inauguration, Obama made a personal appeal to Zuckerberg to take the threat of fake news and political disinformation seriously. Unless Facebook and the government did more to address the threat, Obama warned, it would only get worse in the next presidential race.
Zuckerberg acknowledged the problem posed by fake news. But he told Obama those messages weren't widespread on Facebook and that there was no easy fix, according to people briefed on the exchange, who spoke on the condition of anonymity to share details of a private conversation.
The conversation on November 19 was a flashpoint in a tumultuous year in which Zuckerberg came to recognize the magnitude of a new threat - a coordinated assault on a US election by a shadowy foreign force that exploited the social network he created.
Like the US government, Facebook didn't foresee the wave of disinformation that was coming and the political pressure that followed. The company then grappled with a series of hard choices designed to shore up its own systems without impinging on free discourse for its users around the world.
One outcome of those efforts was Zuckerberg's admission on Thursday that Facebook had indeed been manipulated and that the company would now turn over to Congress more than 3,000 politically themed advertisements that were bought by suspected Russian operatives.
But that highly public moment came after months of maneuvering behind the scenes that has thrust Facebook, one of the world's most valuable companies - and one that's used by one-third of the world's population each month - into a multi-sided Washington power struggle in which the company has much to lose.
Some critics say Facebook dragged its feet and is acting only now because of outside political pressure.
"There's been a systematic failure of responsibility" on Facebook's part, said Zeynep Tufekci, as associate professor at the University of North Carolina at Chapel Hill who studies social media companies' impact on society and governments. "It's rooted in their overconfidence that they know best, their naivete about how the world works, their expensive effort to avoid oversight, and their business model of having very few employees so that no one is minding the store."
Facebook says it responded appropriately.
"We believe in the power of democracy, which is why we're taking this work on elections integrity so seriously, and have come forward at every opportunity to share what we've found," said Elliot Schrage, vice president for public policy and communications. A spokesperson for Obama declined to comment.
This account - based on interviews with more than a dozen people involved in the government's investigation and Facebook's response - provides the first detailed backstory of a 16-month journey in which the company came to terms with an unanticipated foreign attack on the US political system and its search for tools to limit the damage.
Among the revelations is how Facebook detected elements of the Russian information operation in June 2016 and then notified the FBI. Yet in the months that followed, the government and the private sector struggled to work together to diagnose and fix the problem.
The growing political drama over these issues has come at a time of broader reckoning for Facebook, as Zuckerberg has wrestled with whether to take a more active role in combatting an emerging dark side on the social network - including fake news, suicides on live video, and allegations that the company was censoring political speech.
These issues have forced Facebook and other Silicon Valley companies to weigh core values, including freedom of speech, against the problems created when malevolent actors use those same freedoms to pump messages of violence, hate and disinformation.
There has been a rising bipartisan clamor, meanwhile, for new regulation of a tech industry that, amid a historic surge in wealth and power over the past decade, has largely had its way in Washington despite concerns raised by critics about its behavior.
In particular, momentum is building in Congress and elsewhere in the federal government for a law requiring tech companies - like newspapers, television stations and other traditional carriers of campaign messages - to disclose who buys political ads and how much they spend on them.
"There is no question that the idea that Silicon Valley is the darling of our markets and of our society - that sentiment is definitely turning," said Tim O'Reilly, an adviser to tech executives and chief executive of the influential Silicon Valley-based publisher O'Reilly Media.
The encounter in Lima was not the first time Obama had sought Facebook's help.
In the aftermath of the December 2015 shooting in San Bernardino, California, the president dispatched members of his national security team - including Chief of Staff Denis McDonough, Homeland Security Secretary Jeh Johnson and top counter-terrorism adviser Lisa Monaco - to huddle with leading Silicon Valley executives over ways to thwart the Islamic State's practice of using US-based technology platforms to recruit members and inspire attacks.
The result was a summit, on January 8, 2016, which was attended by one of Zuckerberg's top deputies, Chief Operating Officer Sheryl Sandberg. The outreach effort paid off in the view of the Obama administration when Facebook agreed to set up a special unit to develop tools for finding Islamic State messages and blocking their dissemination.
Facebook's efforts were aided in part by the relatively transparent ways in which the extremist group sought to build its global brand. Most of its propaganda messages on Facebook incorporated the Islamic State's distinctive black flag - the kind of image that software programs can be trained to automatically detect.
In contrast, the Russian disinformation effort has proven far harder to track and combat because Russian operatives were taking advantage of Facebook's core functions, connecting users with shared content and with targeted native ads to shape the political environment in an unusually contentious political season, say people familiar with Facebook's response.
Unlike the Islamic State, what Russian operatives posted on Facebook was, for the most part, indistinguishable from legitimate political speech. The difference was the accounts that were set up to spread the misinformation and hate were illegitimate.
It turned out that Facebook, without realizing it, had stumbled into the Russian operation as it was getting underway in June 2016.
At the time, cybersecurity experts at the company were tracking a Russian hacker group known as APT28, or Fancy Bear, which US intelligence officials considered an arm of the Russian military intelligence service, the GRU, according to people familiar with Facebook's activities.
Members of the Russian hacker group were best known for stealing military plans and data from political targets, so the security experts assumed that they were planning some sort of espionage operation - not a sweeping disinformation campaign designed to shape the outcome of the US presidential race.
Facebook executives shared with the FBI their suspicions that a Russian espionage operation was in the works, a person familiar with the matter said. An FBI spokesperson had no immediate comment.
Soon thereafter, Facebook's cyber experts found evidence that members of APT28 were setting up a series of shadowy accounts - including a persona known as Guccifer 2.0 and a Facebook page called DCLeaks - to promote stolen emails and other documents during the presidential race. Facebook officials once again contacted the FBI to share what they had seen.
After the November election, Facebook began to look more broadly at the accounts that had been created during the campaign.
A review by the company found that most of the groups behind the problematic pages had clear financial motives, which suggested that they weren't working for a foreign government.
But amid the mass of data the company was analyzing, the security team did not find clear evidence of Russian disinformation or ad purchases by Russian-linked accounts.
Nor did any US law enforcement or intelligence officials visit the company to lay out what they knew, said people familiar with the effort, even after the nation's top intelligence official, James Clapper, testified on Capitol Hill in January that the Russians had waged a massive propaganda campaign online.
The sophistication of the Russian tactics caught Facebook off-guard. Its highly regarded security team had erected formidable defenses against traditional cyber attacks but failed to anticipate that Facebook users - deploying easily available automated tools such as ad micro-targeting - pumped skillfully crafted propaganda through the social network without setting off any alarm bells.
As Facebook struggled to find clear evidence of Russian manipulation, the idea was gaining credence in other influential quarters.
In the electrified aftermath of the election, aides to Hillary Clinton and Obama pored over polling numbers and turnout data, looking for clues to explain what they saw as an unnatural turn of events.
One of the theories to emerge from their post-mortem was that Russian operatives who were directed by the Kremlin to support Trump may have taken advantage of Facebook and other social media platforms to direct their messages to American voters in key demographic areas in order to increase enthusiasm for Trump and suppress support for Clinton.
These former advisers didn't have hard evidence that Russian trolls were using Facebook to micro-target voters in swing districts - at least not yet - but they shared their theories with the House and Senate intelligence committees, which launched parallel investigations into Russia's role in the presidential campaign in January.
Sen. Mark Warner, vice chairman of the Senate Intelligence Committee, initially wasn't sure what to make of Facebook's role. US intelligence agencies had briefed the Virginia Democrat and other members of the committee about alleged Russian contacts with the Trump campaign and about how the Kremlin leaked Democratic emails to WikiLeaks to undercut Clinton.
But the intelligence agencies had little data on Russia's use of Facebook and other US-based social media platforms, in part because of rules designed to protect the privacy of communications between Americans.
Facebook's effort to understand Russia's multifaceted influence campaign continued as well.
Zuckerberg announced in a 6,000-word blog post in February that Facebook needed to play a greater role in controlling its dark side.
"It is our responsibility," he wrote, "to amplify the good effects [of the Facebook platform] and mitigate the bad - to continue increasing diversity while strengthening our common understanding so our community can create the greatest positive impact on the world."
The extent of Facebook's internal self-examination became clear in April, when Facebook Chief Security Officer Alex Stamos co-authored a 13-page white paper detailing the results of a sprawling research effort that included input from experts from across the company, who in some cases also worked to build new software aimed specifically at detecting foreign propaganda.
"Facebook sits at a critical juncture," Stamos wrote in the paper, adding that the effort focused on "actions taken by organized actors (governments or non-state actors) to distort domestic or foreign political sentiment, most frequently to achieve a strategic and/or geopolitical outcome." He described how the company had used a technique known as machine learning to build specialized data-mining software that can detect patterns of behavior - for example, the repeated posting of the same content - that malevolent actors might use.
The software tool was given a secret designation, and Facebook is now deploying it and others in the run-up to elections around the world. It was used in the French election in May, where it helped disable 30,000 fake accounts, the company said. It was put to the test again on Sunday when Germans went to the polls. Facebook declined to share the software tool's code name. Another recently developed tool shows users when articles have been disputed by third-party fact checkers.
Notably, Stamos's paper did not raise the topic of political advertising - an omission that was noticed by Capitol Hill investigators. Facebook, worth US$495 billion (NZ$681b), is the largest online advertising company in the world after Google. Although not mentioned explicitly in the report, Stamos's team had searched extensively for evidence of foreign purchases of political advertising but had come up short.
A few weeks after the French election, Warner flew out to California to visit Facebook in person. It was an opportunity for the senator to press Stamos directly on whether the Russians had used the company's tools to disseminate anti-Clinton ads to key districts.
Officials said Stamos underlined to Warner the magnitude of the challenge Facebook faced policing political content that looked legitimate.
Stamos told Warner that Facebook had found no accounts that used advertising but agreed with the senator that some likely existed. The difficulty for Facebook was finding them.
Finally, Stamos appealed to Warner for help: If US intelligence agencies had any information about the Russian operation or the troll farms it used to disseminate misinformation, they should share it with Facebook. The company is still waiting, people involved in the matter said.
For months, a team of engineers at Facebook had been searching through accounts, looking for signs that they were set up by operatives working on behalf of the Kremlin. The task was immense.
Warner's visit spurred the company to make some changes in how it conducted its internal investigation. Instead of searching through impossibly large batches of data, Facebook decided to focus on a subset of political ads.
Technicians then searched for "indicators" that would link those ads to Russia. To narrow down the search further, Facebook zeroed in on a Russian entity known as the Internet Research Agency, which had been publicly identified as a troll farm.
"They worked backwards," a US official said of the process at Facebook.
The breakthrough moment came just days after a Facebook spokesman on July 20 told CNN that "we have seen no evidence that Russian actors bought ads on Facebook in connection with the election."
Facebook's talking points were about to change.
By early August, Facebook had identified more than 3,000 ads addressing social and political issues that ran in the United States between 2015 and 2017 and that appear to have come from accounts associated with the Internet Research Agency.
After making the discovery, Facebook reached out to Warner's staff to share what they had learned.
Congressional investigators say the disclosure only scratches the surface. One called Facebook's discoveries thus far "the tip of the iceberg." Nobody really knows how many accounts are out there and how to prevent more of them from being created to shape the next election - and turn American society against itself.