Before the medivac bill passed last week, Prime Minister Scott Morrison and his security ministers were advised they could lessen the risk it posed to border protection by insisting medical transferees from Manus Island and Nauru were sent back after treatment – but they chose not to.

The Saturday Paper understands the government was advised by security agencies to act on the repatriation issue after the senate passed a version of the bill late last year. The reopening of the Christmas Island detention centre was also recommended.

The government’s decision not to attempt amendments to the medivac bill, which could have addressed the agencies’ concerns, has enabled it to accuse Labor of undermining Operation Sovereign Borders (OSB) by opening a path for those held on Manus Island and Nauru to reach Australia and stay.

Ensuring transferees could be returned was a measure designed to deter anyone hoping to use medical transfer as a backdoor to settling in Australia. Yet while the Christmas Island proposal was adopted, the repatriation recommendation was not.

The medivac issue has exposed increasing tension between the Australian government and its own security agencies, which are angry that their work is being co-opted for political purposes.

The Home Affairs Department secretary, Michael Pezzullo, alluded to this advice during a senate estimates committee hearing on Monday, while confirming his agency had also recommended sending those refugees and asylum seekers who qualified for medical transfer to the Australian territory of Christmas Island for treatment, instead of to the mainland.

The secretary said the department had conveyed its advice, produced with input from the Australian Security Intelligence Organisation (ASIO) and other agencies, to the government’s national security committee (NSC), of which Morrison is chair.

Pezzullo explained he was only willing to discuss the advice publicly at all because the government had taken the highly unusual step of declassifying much of it, after details of the classified version appeared in The Australian newspaper on February 7.

“Ultimately, the Department of Home Affairs – providing advice through its ministers to the NSC – came to the view that it’s better to manage this movement in a very controlled fashion,” he said of the medical transfers the bill facilitates.

Pezzullo said the agencies’ advice was to transfer people “almost like from one island to another island, provide as much medical support into Christmas Island as possible – hoping against hope that lawyers won’t get involved, but that’s a far-flung hope – then repatriating people once the care has been appropriately provided”.

On Thursday, the government announced that legal advice from the Australian Government Solicitor had confirmed a loophole in the medivac legislation prevents transferees from being sent back offshore. No reference was made to the unheeded repatriation advice.

The attorney-general, Christian Porter, released a summary but declined to make the full advice public, or to use the last parliamentary sitting day before a six-week break to try to fix the problem with amendments.

The Christmas Island decision has enraged independent Kerryn Phelps, who had sponsored the bill in the house of representatives. The Greens and refugee advocates share Phelps’ fury.

The government has chosen to blame Labor for the decision, launching a full-blown attack on its leader, Bill Shorten, over OSB.

“If Bill breaks this, he owns it,” Morrison said two weeks ago, before parliament resumed.

“If Bill breaks this, he has taken Australia into this position and it will be on his head in terms of what follows from this. We have always remained resolute in our position on border protection and we will remain exactly in that position.”

The Saturday Paper asked the spokesman for the minister for immigration why security agency advice was not followed. A spokesman for Immigration Minister David Coleman pointed only to comments the prime minister had made in the week before parliament resumed.

“There is no form of this bill that is acceptable,” Scott Morrison told journalists on February 9, three days before parliament resumed and before Labor had drafted its amendments.

“This bill is not only a bill that will undermine – in any form – the government’s successful border protection regime. It is also a completely unnecessary bill.”

Morrison cited his own pre-emptive announcement earlier that week – that he would establish an independent medical review panel to vet transfer requests – and condemned the medivac bill as “folly”.

“However positively it might be motivated by some, its detail, its construction, can only unpick and undermine the strong border protection framework that we have worked hard to put in place and that has been successful,” Morrison said.

Asked if he was prepared to “come to the table” and negotiate changes, he said: “There is no table to come to. This isn’t about a compromise.”

On Thursday, Attorney-General Porter revealed the government was considering – but not yet ready to implement – “legislative and non-legislative” options to restore the legal authority to send transferees back to Manus Island and Nauru, after obtaining legal advice that the soon-to-be-law medivac bill severed a link to the existing power in the Migration Act.

That power is already hard for the government to enforce.

It has confirmed that of the 898 people who were transferred to Australia for medical treatment under existing law in the past five years, only 195 have been returned, largely because of long-running test cases before the Federal Court.

Pezzullo told the estimates committee on Monday: “Doctors have got them here. Lawyers are preventing them from leaving.”

Porter blamed Labor for failing to amend the medivac bill to rectify the further repatriation issue it has created.

“This means anyone brought to Australia could remain in indefinite detention on Christmas Island,” he said.

But Pezzullo’s Monday evidence suggests the government was alerted to the repatriation issue well before Labor’s amendments were drafted and it did not act.

At the hearing, Pezzullo also detailed the security agencies’ broader concerns about the version of the medivac legislation the senate passed in December.

He said they believed that as originally drafted, it could undermine OSB and lead to a resumption of the people-smuggling trade that saw thousands of asylum seekers trying to reach Australia when Labor was last in government.

Specifically, the agencies were concerned that it allowed for people to be transferred for assessment not just for treatment, that support people could join them as well as family members, and that it could apply to people who refused treatment.

Labor’s amendments to the bill did not alter those provisions.

The agencies did not advise how to mitigate the bill’s impact on border protection when they briefed Opposition Leader Shorten and his deputy, Tanya Plibersek.

Shorten and his staff asked for advice but it was refused because, unless specifically authorised, government agencies can only provide policy options to government, not to opposition parties.

The Labor representatives also objected during the meeting to the presence of three advisers from the offices of the prime minister, home affairs minister and immigration minister.

Shorten’s chief of staff, Ryan Liddell, lodged a complaint with the prime minister’s office afterwards.

But The Saturday Paper understands only pre-election security briefings and the ASIO chief’s statutory briefings of the opposition leader are conducted without government staff present. This briefing was different – a one-off, at Scott Morrison’s instigation, so the staff were there.

Labor was advised of the agencies’ concern that the version of the bill, which was current at the time of the briefing, would apply to new arrivals as well.

That advice prompted the drafting of Labor’s key amendment, restricting the medivac bill to what is described as the current cohort.

Pezzullo told estimates Labor’s so-called ring-fencing amendment had eased his department’s concern.

“This was a significant material change that shored up the otherwise significant unravelling of OSB that would have occurred had the senate version of the bill passed – which would have been catastrophic in terms of its effect on OSB.”

This week, Scott Morrison suggested it still was. “We know that when the Labor Party rushes legislation, the consequences are catastrophic,” he told parliament on Tuesday.

The Nauruan government appeared to help its Australian counterpart by passing its own law following the medivac bill, blocking medical transfers to Australia from its territory.

The medivac issue has exposed increasing tension between the Australian government and its own security agencies, which are angry that their work is being co-opted for political purposes.

Pezzullo’s revelation in the estimates hearing came as he and the director-general of security and head of ASIO, Duncan Lewis, expressed strong displeasure at seeing their agencies’ advice apparently leaked and then misconstrued and politicised during an increasingly volatile public debate over the medivac bill.

The chiefs told the estimates committee they were convinced the February 7 leak had not come from their agencies and they were prompted to act because the newspaper report specifically mentioned ASIO and mischaracterised its advice.

“ASIO does not and will not use its position to influence the national debate on security-relevant issues through unauthorised disclosures,” Lewis told the committee.

He said such issues went to the trust and confidence that the parliament and the Australian people needed to have in their security services.

“When reporting wrongly attributed advice from ASIO or where our classified advice is leaked, it undermines all that we stand for,” he said.

“… ASIO’s advice is provided to agencies to assist with policy development and there are strict controls on how that advice is managed and disseminated and breakdowns in these controls are seriously damaging.”

Labor senators alleged that the leak came directly from the government.

In what amounted to a warning to all sides of politics about compromising security agencies’ work, Pezzullo confirmed he had referred the leak to police after conferring with Lewis and nobody else – including his minister.

In another message to all engaged in the debate, Pezzullo emphasised what he said was the overall risk that new medivac legislation could fuel the perception of a weaker approach, even if that wasn’t the reality.

“Our concern is: this isn’t about strictly what’s passed the parliament, it’s about what can be messaged or signalled as what’s passed the parliament,” Pezzullo said. “… I just want to stress this is about appearances, not reality [of] a weakening of resolve. That’s why it’s particularly important that the government, and indeed future governments, indicate that their resolve to maintain OSB is undiminished.”

In anticipation of that warning – or perhaps after his own departmental briefing – Bill Shorten had begun to do that last week.

“Let me make it very clear to people smugglers in Indonesia or elsewhere: you and your trade are not welcome,” he said after the bill had passed. “Any government I lead will deploy the full force of the ring of steel of the Australian defence forces and our border forces.”

Shorten also accused the government of deliberately overstating the impact of the change. “I say to the Government: people smugglers are vicious criminals, they will twist the words of the current government and say somehow, people should get on unsafe boats,” he said.

But the planned reopening of the Christmas Island centre has also reopened divisions in Labor’s ranks over refugee policy, with Shorten initially objecting and later acquiescing to the move, while some of his left-wing colleagues, including his deputy, continue to raise concerns.

“I think if they can be adequately treated there, that’s up to the government to show,” Plibersek said on Wednesday. “I think it’s difficult to understand, and it’s up to the government to explain, how that medical treatment can be adequately provided.”

Last week, when it appeared the government was reopening the Christmas Island centre to boost its argument that the policy shift would prompt an influx of arrivals, even though it would not apply to them, Shorten condemned it as an attempt to “whip up hysteria” and “desperate political tactics”, spending a billion dollars to “try and score a political point”.

But asked about it on Tuesday, after the government had confirmed its true purpose, the opposition leader changed position.

“If the medical treatment is required and it’s delivered on Christmas Island and it makes people well, well that’s fine,” he said.

The shift prompted Attorney-General Porter to mock the Labor leader in parliament, noting opposition frontbenchers had variously labelled the Christmas Island decision “ridiculous” and “silly”, and the government “unhinged”.

“So, in one day we’ve gone from ‘ridiculous’ to ‘fine’, ‘unhinged’ to ‘fine’, ‘hysterical’ to ‘fine’ – ‘Nah, that’s okay! Let’s do it there,’ ” Porter hooted.

A Nine newspapers Ipsos opinion poll this week, which showed Labor’s lead suddenly narrowing amid the border protection furore, caused a shiver among the opposition.

If this weekend’s Newspoll reveals something similar, it may become a panic.

“I believe that the vast majority of Australians understand you can have a strong border and still treat people humanely within our care,” Shorten declared, defending the bill last week.

He and his colleagues will be desperately hoping he hasn’t misread the mood.