When the partner of journalist Glenn Greenwald was detained at Heathrow airport last August under the Terrorism Act, MI5 were pulling the strings and knew full well that he wasn't a terrorist

On Sunday 18 August, David Miranda, the 28-year-old partner of Glenn Greenwald, was detained at Heathrow airport under schedule 7 of the Terrorism Act.

The terrorism law, enacted in 2000, is aimed at killers. It is designed to allow police to stop possible jihadists or IRA members planning bombings, as they enter Britain. It is a draconian piece of legislation: no "probable cause" or specific suspicion is needed. The purpose of the stop is a grave one: to assess whether someone may be involved in the "commission, instigation or preparation of acts of terrorism".

Miranda wasn't a terrorist. The British authorities knew that perfectly well. He was the partner of a journalist. They suspected he was in fact carrying copies of Edward Snowden's NSA and GCHQ files, which Greenwald was engaged in researching and publishing. Their prime purpose, as they were later to admit, was simply to get hold of the files, and find out how much Greenwald knew.

The two police officers who rifled through Miranda's backpack and questioned him in a custody suite asked virtually nothing about terrorism. They didn't inquire if Miranda was a member of a terrorist group. Miranda says the questions he was asked "seemed random and unfocused ... They gave me the impression that they were questioning me just to give themselves time to examine the material."

Documents obtained in the subsequent legal proceedings from MI5 explain this lack of curiosity.MI5 and the NSA decided several days earlier to have Miranda stopped at Heathrow and his documents seized. They knew for certain he was carrying the data – either through intercepts or an informant – and were desperate to find out how much Snowden had leaked. For the spies, it was an extraordinarily lucky opportunity. But they seem to have been anxious not to let Miranda and his friends realise they had been betrayed.

On 15 August – three days before the stop – MI5 contacted the Metropolitan Police's counter-terrorism command, SO15. The agency requested detective superintendent James Stokley to have Miranda grabbed. The agency filled in what is known as a "ports circulation sheet" (PCS) with the official request. In a box that asked the author to confirm that possible terrorism was involved, MI5 wrote: "Not applicable." Unfortunately, the police had only one power to search and seize passengers' baggage without the need to give any sort of a reason. This was schedule 7 of the act. The police pointed out the problem. MI5 redrafted the PCS form. Twice.

In its final version, MI5 claimed: "Intelligence indicates that MIRANDA is likely to be involved in espionage activity which has the potential to act against the interests of UK national security ... We assess that MIRANDA is knowingly carrying material, the release of which would endanger people's lives. Additionally the disclosure, or threat of the disclosure, is designed to influence a government, and is made for the purpose of promoting a political or ideological cause. This therefore falls within the definition of terrorism and as such we request that the subject is examined under schedule 7."

It was an absurd account. It was written to mimic the wording of the language in the act defining "terrorism". But of course, the authors knew it was not Miranda's intention to make threats to endanger anyone's life, least of all to achieve some "ideological objective".

The definition in the act was supposedly aimed at a fanatic who threatened to blow up a plane. MI5 explained their anxiety: "Please do not make any reference to espionage activity. It is vital that MIRANDA is not aware of the reason for this ports stop. We would be grateful if this stop could be made to seem as routine as possible, and that it appears that this stop is not at the request of the Security Service."

The use of schedule 7 against someone who was known not to be a terrorist was a blatant abuse – and an alarming precedent in which a government matched journalism with terrorism.

This was the first time the much-criticised section of the act had been used against a journalist carrying source material. Coming on top of the forced destruction of the Guardian's computers on 20 July, it looked like a chilling attack on press freedom.