In June 2012, a tall, mysterious figure turned up at the Ecuadorian embassy in London. He rang the bell, walked in and asked for political asylum.

It was Julian Assange. Days earlier, he had lost his long legal fight against extradition to Sweden, where two women accused him of rape.

Soon afterwards, the left-leaning then president of Ecuador, Rafael Correa, agreed to Assange’s request, precipitating a major diplomatic row with the British government. And a full-blown siege. Metropolitan police officers swarmed outside the building, ready to arrest Assange should he venture out.

It was against this febrile backdrop that Correa authorised a secret programme named “Operation Guest”. It was later renamed “Operation Hotel”. The guest was Assange, politely referred to as el huésped.

Revealed: Ecuador spent millions on spy operation for Julian Assange

Read more

The goal, at first, was to stop detectives bursting into the modest ground-floor embassy and dragging Assange away.

But documents seen by the Guardian show it developed into something more complex. The aim seems to have changed from protecting Assange – which propped up WikiLeaks in the process – to spying on him.

Whatever its aims, WikiLeaks was able to keep going up to and beyond its role in the 2016 US presidential election, with all the seismic consequences.

It was WikiLeaks that published hacked emails belonging to the Democratic National Committee (DNC) and senior Democratic officials, leading to claims that it was part of an alleged Kremlin plot to undermine Hillary Clinton and help Donald Trump.

How WikiLeaks got the emails in the first place is an enduring mystery.

Did someone drop them off to Assange on a flash drive? Were the files uploaded on to an encrypted remote server? Or did they come in by some other route?

Robert Mueller, the special counsel investigating allegations that the Trump campaign colluded with Russia to win the election, is looking for the answer. The secret records seen by the Guardian and the magazine Focus Ecuador may give him important new clues.

According to the documents, the counter-espionage effort began nearly six years ago when a team secretly installed CCTV cameras in the Ecuadorian embassy. They covered the entrance lobby, a meeting room and the mini-balcony from which Assange would periodically address supporters. The agents filmed everyone who came in and out, and kept watch on the street.

Security personnel worked two-person shifts and were on duty 24 hours a day. They do not appear to have recorded Assange’s conversations, documents suggest. However, they were able to observe him closeup, noting his moods, habits and sleeping patterns.

Q&A

What would happen if Julian Assange left the Ecuadorian embassy?

Julian Assange has been in the Ecuadorian embassy in central London since June 2012. He initially sought asylum following a series of legal challenges through British courts to a European arrest warrant issued by Sweden, where he was accused of rape and sexual assault, which he denies.

In February, a judge upheld a warrant for his arrest for skipping bail, meaning that although Swedish prosecutors have dropped their investigation into alleged sexual offences, Assange faces being arrested if he leaves the embassy, and fears he would be subsequently extradited to the US for questioning about WikiLeaks’ activities.

Photograph: Frank Augstein/AP

Was this helpful?

Thank you for your feedback.

None of this came cheap.

The documents set out how Ecuador’s intelligence agency, known as Senain, hired an international security company. Its bill in 2012 was $55,000 (£40,000) a month, paid from a “special expenses” government budget.

They detail how the company’s agents slept 100 metres (330ft) away from the embassy, in a multi-tenant Edwardian mansion block in Basil Street. Unbeknown to Ecuadorian taxpayers, the operatives were camping out in one of the most expensive parts of London, albeit in a basic basement flat with two bedrooms, which cost £2,800 a month.

From a control room, the security team oversaw Assange’s contacts. They consulted with him about anyone wanting to visit. If he agreed, guests were allowed in. They were met in the foyer, went through a security check, and handed over their passports and mobile phones.

One of the bedrooms at the nearby apartment where the security agents stayed and monitored Assange. Photograph: Chestertons

The documents show that operatives wrote down passport numbers, nationalities and the purposes of the visits. These included “social reunion”, “medical”, “legal” or “VIP”. They clocked when the guest arrived and when they exited, in the early months past a phalanx of armed police waiting in uniform on the embassy’s front steps. At the beginning of the crisis, when the small Andean nation was facing off against the might of the British state, officers lurked on the interior stairs and next to the embassy toilet.

Every month, the security company sent a confidential list of Assange’s visitors to the Ecuadorian president. There were additional “extraordinary” reports. Sometimes, the company included stills from secret video footage of interesting guests, plus profiles and analysis. They also reported when a packet of sweets was lobbed on to the balcony, seemingly a present for Assange.

It is these visitor logs that will interest Mueller. He is reportedly close to indicting Russian hackers allegedly behind the raid on the Democrats’ electronic servers and seems to view WikiLeaks as an integral part of the Kremlin’s multifaceted espionage operation. The FBI has interviewed at least one source close to Operation Guest, it is understood.

Assange has refused to say how the emails got to WikiLeaks. He denies they came from a “state actor”. The FBI does not appear to believe him. Giving evidence last year, the agency’s then director, James Comey, told Congress that Moscow was behind the DNC cyber-raid. An “intermediary”, he said, passed the emails to WikiLeaks.

Assange’s relationship with Moscow is certainly longstanding. In 2011, he signed a deal with the Kremlin-owned television network RT. The Julian Assange Show featured 10 guests, including Correa. Weeks after Assange’s interview with Correa was broadcast, the Australian sought refuge in the Ecuadorian embassy.

The logs record that Assange had more than 80 visitors in one month in 2016. They included the WikiLeaks staff member Sarah Harrison and longtime supporters such as Craig Murray, the UK’s former ambassador to Uzbekistan. Another was the Croatian philosopher Srećko Horvat.

Why does Ecuador want Assange out of its London embassy?

Read more

There were also visits from Assange’s lawyer Gareth Peirce and Vaughan Smith, the founder of the Frontline Club, who hosted WikiLeaks at his Norfolk mansion for 13 months up until Assange jumped bail. On 23 June 2016, RT broadcast a referendum special from inside the embassy, with the final guests leaving at 4.30am.

The external surveillance team wrote up descriptions of these encounters. They were sent back to Senain and Correa, sources say. The then Ecuadorian foreign minister, Ricardo Patiño, visited Assange in London, talking with him into the early hours, and criticised Britain for failing to find a solution to the standoff. The UK was unmoved. Asked what the government planned to do about Assange, the then foreign minister, Hugo Swire, said: “Not my stone, not my shoe.”

Correa had no immediate comment on the operation. He has previously decried the UK’s attempts to arrest Assange, saying his country would not tolerate “colonialism of any kind”.

Meanwhile, leaked invoices show the Ecuadorian government spent as much as $28m on surveillance equipment, sold only to states. What it was used for and by whom is unclear.

The WikiLeaks staff member Sarah Harrison, second right, with Edward Snowden and others at a ceremony in Moscow. Photograph: Sunshinepress/Getty Images

By 2014, it appears tensions over Assange were growing. The documents reveal that Ecuador’s then ambassador to the UK, Juan Falconí Puig, forwarded a complaint to Senain after a bill arrived for unpaid council tax from Kensington and Chelsea. The tax was payable on the agents’ clandestine flat in Basil Mansions. Falconí had no idea it existed. He rang Patiño, demanding to know what was going on, according to sources in Quito.

That autumn, the security consultants reported that Assange had somehow set up his own secret communications network.

Moreover, they said he had “violated” the embassy’s computers and was apparently reading confidential diplomatic traffic, the company reported. Embassy staff apparently kept their personal files on Assange in a safe – the one place he could not access.

The head of Ecuadorian intelligence, Rommy Vallejo, held a meeting with Patiño to discuss the Assange situation and the “risks” he posed to embassy staff. Over time, Assange came to take up more than one-third of the embassy’s ground-floor space. Initially, he slept in narrow room next to the balcony. He subsequently colonised a back room as a bedroom and occupied half the kitchen. Assange’s unkempt realm was home to WikiLeaks and its computers, and was entirely private. There was no CCTV. It was off limits to diplomats, the rooms only accessible via special codes.

Patiño brushed off these concerns. His solution to this internal takeover was to dispatch three undercover intelligence officers from Quito to increase security, documents show. The Assange bill was already causing friction with Ecuador’s financial chief. The new state agents cost an extra $35,000 a month, paid out on top of the security company’s monthly invoice, which had climbed to $97,000.

Documents show the private security team’s duties included keeping an eye on Hans Crescent, the street outside the embassy. It reported demonstrations in support of Assange, noting that they became smaller each year. There was a strange visit by an American superfan who spent two days outside the embassy sleeping in a car. Protesters who took part in an anti-Correa demonstration were surreptitiously photographed.

In November 2017, the team filed an “extraordinary” 10-page report on a visit by two Catalan separatists, Oriol Soler and Arnau Grinyó. The meeting might “trigger criticism” from Madrid if it leaked, they concluded. Assange tweeted that same day in support of Catalan secession.

Over time, Ecuador’s counter-espionage operation became mundane. In January, the agents noted that a new cleaner had started work. Typically, Assange would hold private conversations in the women’s bathroom, they wrote in a daily log. Visitors noticed that he used a “squelch box”, a counter-surveillance device that emitted a loud sound.

Letters arrived for him, as did flowers, the agents noted. The flowers were thrown away “according to protocol”. Sometimes, Assange smoked a pipe. His mood was generally placid. On occasion, there were angry eruptions, sympathetically interpreted by the agents who could understand his frustration after being in the embassy for such a long time.

The agents faithfully recorded visits from Bolivia’s former foreign minister, as well as such high-profile guests as Pamela Anderson, Vivienne Westwood and Yanis Varoufakis. Early this year, Assange’s most frequent visitor was the legal analyst Stella Morris, a member of his legal team.

Vivienne Westwood was one of Assange’s most high-profile visitors. Photograph: Jonathan Brady/PA

Journalists and film-makers have been frequent guests. They included the Guardian’s Ewen MacAskill, who visited twice in January and February to discuss Assange’s confinement and his prospects for the future.

In February, there were no more visits recorded for two weeks. This barren spell was marked across a line as “sin visitas”.

Friends conceded that after almost six years in the embassy, Assange was visibly suffering, and in poor health, although they described him as mentally robust. At weekends, embassy staff go home, leaving him with no one to talk to apart from a solitary guard.

The logs do not explain how WikiLeaks received the 70,000 Democratic emails that had been hacked.

By the spring of this year, developments in Quito had moved in an ominous direction for Assange. Ecuador’s president, Lenín Moreno, pronounced him a costly “inherited problem” and made it clear that he viewed Assange as an obstacle to better relations with the US.

As a candidate, Donald Trump praised WikiLeaks when it revealed embarrassing Democratic party secrets. When Assange released the email correspondence of the Clinton aide John Podesta, Trump exclaimed: “I love WikiLeaks.” Before and after the election, Donald Trump Jr exchanged a handful of direct Twitter messages with Assange.

In March, Assange’s internet access was cut off. A jammer was installed to prevent him from accessing emails. For more than a month, he has been forbidden visitors. Friends say he is passing the time by reading. They say he is leafing through books on the US election, which WikiLeaks did so much to influence and – its critics allege – subvert. Meanwhile, Correa, Assange’s protector and patron, has moved to Belgium. He has since landed at job at RT.

Last week, the embassy appeared deserted. Nearby, well-heeled passengers emerged from Bentleys and Mercedes outside Harrods. Six supporters held banners that said “Free Assange” and #ReconnectJulian. A violinist serenaded him from the pavement. There was no sign of the guest, but the window next to his balcony was open.

Assange faces a tough choice. If he walks out of the embassy, he can expect arrest and could spend up to a year in prison for breaking his bail conditions.

The US might then seek to extradite him. He would contest any attempt, and might win, but would face a long, uncomfortable spell behind bars while his case is decided.

Ecuadorian government sources say Quito is considering a plan for where Assange might go next. Russia would be an obvious choice, though this would cause problems with the US. Cuba and Venezuela are also said to be under discussion. Since January, Assange has officially been an Ecuadorian diplomat. However, this probably would not stop the UK arresting him if it had the chance.

If Assange remains in the embassy, cut off from the world, WikiLeaks cannot function. Either way, it is clear that Ecuador no longer wants him. His future is more uncertain than ever. The most likely scenario is that he will soon walk out the door.