In a society under the rule of law, few things matter more than upholding the proper line between legitimate and illegitimate action. Two high-profile public order cases this week exemplify the sensitivity of this truth in powerfully contrasting ways, from opposite sides of the barricades.

In the first, due in Southwark crown court today, an A-level student is charged with throwing a fire extinguisher from the roof of Conservative party headquarters during the tuition fee protests. Here, the dividing line is between the citizen's right to demonstrate and the crime of violent disorder, for which Edward Woollard, who has pleaded guilty, now faces a long prison sentence. In the second case, which collapsed in Nottingham crown court yesterday, six green activists went free when revelations about the undercover role of PC Mark Kennedy destroyed the reliability of a conspiracy case against them. This time the dividing line was between the proper police role of preventing crime – an alleged attempt to shut the Ratcliffe-on-Soar power station – and the improper one of potential entrapment of peaceful protesters, a possibility which raises many serious questions about public order policing today.

The green campaigners' solicitor yesterday highlighted some of these: the questionable legitimacy of mass pre-emptive arrests of more than 100 people; the imposition of controversial pre-charge bail conditions on them; the allegedly arbitrary winnowing of the initial numbers arrested down to those who were finally charged, and the restrictive disclosure rules under which the defendants were not permitted to learn about PC Kennedy's role or his evidence, which might have contributed to an acquittal. These are all serious questions about policing methods, which require official responses and further inquiry, perhaps by the home affairs select committee. Yet they are overshadowed by the wider implications of the dramatic revelations about the role of PC Kennedy, who is said to have been an undercover agent since 2000, and who since 2003 has infiltrated a variety of environmental, anti-racism and anarchist groups – and who is now reported to have thought better of this long-term role and to have become a sympathiser with the protesters.

Crime prevention – an aspect of being tough on the causes of crime – is every bit as important a police responsibility as crime solving. It follows that undercover operations to prevent crime can have a legitimate place in police work, just as they do in protecting national security. Yet to infiltrate an undercover officer into any undertaking is an expensive and risky business. The officer has to live under a new identity, in a specially obtained home, with a credible back story, for a prolonged period of time. Such an operation has to be justifiable in terms of the police's role of protecting the public, proportionate to the harm it is designed to prevent, and must be properly supervised to ensure that the boundaries – between intelligence and evidence of crime, and between evidence collection and entrapment – are rigorously upheld.

These boundaries have been properly upheld in the past in the policing of football hooligans. But they are not always easy to maintain, as the collapse of a major customs and excise fraud case in 2004 underlined, and as FBI over-exuberance has also illustrated. The infiltration of direct action protest groups – whether green, student or anti-cuts groups – is one of the most sensitive examples imaginable. Civil libertarian instinct revolts against it. The risk to public confidence from badly run operations is great. That is not a reason for proscribing undercover policing altogether. Yet every effort should always be made to use open negotiation and good public order policing to prevent potentially violent protest from crossing the line into the criminal law. Few would say we have got that balance quite right at the moment.