Stopping and undoing an agent — study guide
The concept's fragments, read in order.
Stopping is something you build
An agent that only reads can be stopped by closing the terminal. An agent with side effects is harder. By the time you decide it is misbehaving, it may have started processes you cannot see, it still holds credentials that work, and it has changed files, branches or records somewhere else. The usual account of how a run ends, a limit runs out or someone cancels the run, says nothing about what canceling has to accomplish. Stopping an agent on purpose takes four things, and each has to be built before the day you need it.
The first is a stop you can confirm: the agent's processes are gone because you observed them gone, not because you sent a signal. The second is credentials you can revoke, which cut off the agent at the systems it acts on even when you cannot reach the process. The third is a sandbox you can quarantine when you cannot show it stopped, so nothing is collected from a run that may still be running. The fourth is a record of every effect, kept by something other than the agent, so you can find what it changed and revert what can be reverted. Security guidance for agents asks for the same capability in plainer words: OWASP's cheat sheet on AI agent security lists letting users interrupt and roll back agent operations among its human-in-the-loop controls.
The pages in this domain share a few words. The controller is the program that starts the agent, holds its credentials, runs the checks and decides what happens next; the model proposes and the controller acts. Evidence is something the controller observed itself, and a claim is something the agent said. Stopping is where the difference bites hardest. An agent reporting that it has stopped is a claim, and so is a parent process that exited.
The running example is a coding agent in a sandbox, because the incidents here come from a system that drives coding agents. Nothing in it is specific to code. Anything that calls a model in a loop and runs the tools it requests, and so can change something outside itself, needs the same four things. None of them makes an agent safe. They shorten how long a misbehaving agent keeps acting and widen how much of what it did you can find, and some of what it did cannot be taken back at all.
A stop you saw, not one you assumed
A stop has to be available at any moment, including in the middle of the controller's work cycle. In one real system, two controls are always admitted. Stop holds the work until someone explicitly resumes it. Abandon puts the work back to its previous status with new dispatch held, and only once termination is confirmed. Both depend on a definition of stopped that does not rest on anyone's word.
A parent process exiting proves nothing about its children, and a signal sent proves nothing about what is left. On Linux, children can keep running after their parent dies, and a descendant can move into a new process group or session where a signal sent to the old group never reaches it. A control group tracks membership independently of both, so the controller can ask the kernel whether anything is still inside instead of trusting that its signal landed. Even a kill of the whole group removes only what is already inside; it does nothing about something about to come in. The page on containing where an agent runs tells in full how one real system closes that gap: the order a stop follows, and the waiting launcher a reviewer saw enter a sandbox after it had been read as empty.
For stopping, the rule that matters is that only an observation ends a stop, like a lifeguard clearing a pool at closing time, who shuts the gate first so nobody new comes in, then checks the water is empty, instead of assuming everyone left because the whistle blew. Process ids and process groups help with diagnosis, but they cannot prove that nothing able to write remains. A kill signal sent is not a stop observed, in the same way that a timeout means you stopped waiting, not that the work failed. Until the controller has seen the sandbox closed and empty, nothing the run produced is collected.
When you cannot show it stopped
Sometimes the controller cannot make the final observation that ends a stop. The service manager that owns the sandbox's control group does not answer, the group's membership cannot be read, or the sandbox will not close to new launches. The tempting move is to assume the kill worked, mark the run stopped and carry on. One real system's design refuses to guess. A sandbox whose emptiness and closure cannot be established is quarantined.
Quarantine is a set of refusals. The run stays unfinished and is marked quarantined, and so is its workspace. The run's authority does not quietly lapse: it becomes a quarantine reservation that cannot be reused for new work, so nothing new is launched under the identity of something that may still be running. A blocker names the processes that were seen. And nothing is collected from it, no results read, no files copied out, no evidence counted, because anything a possibly live process can still write is not a finished result.
Getting out is just as strict. On every cycle the controller looks again, and it releases the sandbox only when it observes the sandbox empty and closed. A quarantine never ends because someone says it can. Several things that look like evidence of termination are explicitly not accepted as it. If the service manager is unreachable, nothing is signaled at all. A restart of the manager is not taken to mean the sandboxes it ran have died. And a workspace directory that has been removed and created again is not a termination, only a new directory.
Quarantine has a cost: a stuck run holds its workspace, and its blocker stays in view, until the controller observes the sandbox empty and closed. That is the trade, and it is the right way round. The alternative is a controller that reports a run stopped, then collects its results, commits its changes or resumes its work while part of it may still be running and writing.
The stop that works from the other end
Killing processes only works if the controller can reach them. There is a second kind of stop that works from the other end. An agent acts through credentials, and the systems it acts on check a credential when it is presented. Revoke the agent's credentials where they are issued, and once that revocation reaches the systems that check them, its next call is refused there, wherever the process is and whether or not you have confirmed it stopped. It works like canceling a lost bank card: you do not need to find the card or whoever has it, because every shop that tries it is refused at checkout.
Revocation also covers what a process stop cannot. An agent that holds its own key can read it, so anything it runs can copy the key into its workspace or send it to a host it is allowed to reach. Killing the process does nothing about the copy; revoking the key makes the copy worthless. This is one more reason to give the agent a credential of its own, never a person's account or a shared deploy key: you can revoke it without locking anyone else out.
Field recordIn one real system, the owner pasted a temporary access token into an orchestrating agent's conversation, and it was flagged for revocation because it now sat in the transcript.
Rotation, replacing a credential after any suspected leak, is the same act, and here it doubles as the stop.
Revocation is less immediate than it sounds, and the details decide how much. With OAuth 2.0 a client can ask the issuer to revoke a token (RFC 7009), but an access token that is self-contained, such as a signed JWT, can be checked by the receiving service without asking the issuer. Unless the deployment adds some non-standard way to tell that service, the token keeps working until it expires. The standard answers are short-lived tokens and a service that asks the issuer whether a token is still active (RFC 7662), and even that leaves a window if the answer is cached. Revoking only the access token in use may leave a refresh token that can mint a new one, so revoke the refresh token too. And revoking one credential does not prove that every path to access is closed.
Money works the same way. A budget alert on the account is a warning, not a stop. The backstop for spending is a hard cap at the provider, on a project or workspace that holds only the agent's dedicated key, which the controller can only record as configured; the page on the controller's authority explains why the cap's scope is set by the provider and why enforcement can lag slightly. Revocation and caps act after the fact on whatever the agent tries next, which is why they sit beside the process stop rather than replacing it.
Write the intent down before the effect
A stop, a crash and a timeout leave the same question behind: of the effects this run was making, which ones happened? The worker's memory is gone, and the agent's account is a claim. One real system answers it with a journal. Every repository change and every external effect is an operation with an idempotency key, and it is written durably to the journal before it runs, like writing a check's number and amount in the register before you mail it, so that if you are not sure it arrived you ask the bank whether it cleared instead of writing a second one. The entry then moves through intended, applied, confirmed and finalized. An entry whose outcome is unclear becomes ambiguous, with a blocker attached.
Ambiguous is resolved by reading the target, never by blindly trying again. A timeout means you stopped waiting; it does not mean the work failed, so a timed-out write may have succeeded. The controller looks at the branch, record or deployment it was changing and decides from what it finds. If the change is there, the entry is confirmed and the write is not repeated. If it is absent and the original attempt can no longer land, it can be retried under the same key, which is safe only where the target honors that key. If the target cannot be read, the entry stays ambiguous and a person is told. Repeated deploy requests are merged, so a lost response never becomes a second deployment, and a failed deployment never replaces the last verified one.
Both halves were learned the hard way in one real system.
Field recordA retried write that was not idempotent duplicated a summary, and the duplicate tripped a no-progress breaker after a single run. Afterwards each write was classified as safe to retry or not, and an ambiguous result was re-read before anything was decided.
Field recordSeparately, a local network outage during a heavy benchmark stalled a run for about 110 minutes, and although the exact call it was stuck in was never identified, the source had external calls with no deadline at all; the page on containing where an agent runs tells that story in full.
A deadline turns a hang into an ambiguous outcome, and the journal is what settles it: a write that times out is never blindly retried.
The journal is what makes a stop recoverable rather than merely final. After a crash or a forced stop, the controller can say for every effect whether it happened, did not happen, or is unknown, and unknown is recorded as unknown. One limit stays in plain view: notification delivery is never claimed to be exactly once, and a channel that cannot confirm delivery leaves the notification marked unknown.
Changes the controller did not make
Finding what an agent changed starts with knowing what the controller changed, and noticing when the two disagree. In one real system, the controller registers the value of every branch and ref it manages and checks them on every cycle and again at each gate. If a value has moved and the controller did not move it, it records an integrity observation and blocks the affected gates until a person decides. For code there are three choices: discard the change, adopt it, or stash it. Discard keeps the stray commits on a separate ref, so nothing is lost to whoever investigates. A changed value is never adopted silently.
Deployed environments get a stricter rule, because there is no adopt. An environment the controller did not create can be torn down, or acknowledged, and acknowledging leaves a persistent degraded marker. The same caution runs the other way: stopping or removing anything needs positive proof that the controller created it, in the form of names derived from the controller's identity and recorded before creation. Finding things whose names start with the right prefix is a read, not a license to delete them.
The failure this answers is an ordinary one.
Field recordAn external review of one real project found no branch protection anywhere and a bot identity pushing straight to the main branch: 34 of the project's 74 early commits were the bot's own plan, changelog and release commits. Delivery was described as gated on evidence, and the review's verdict was that wherever the platform did not enforce it, this was discipline, not mechanism. In an earlier project, branch protection sat behind a paywall on the private repository, so merging only when green stayed on the honor system, and one stage merged straight to the main branch with no review.
Hosted git platforms can require a pull request, approving reviews and passing checks before anything reaches a branch, and can refuse direct pushes. They also offer bypass lists that exempt administrators, roles, teams or apps, and an automated account on that list is back to pushing directly. Turn the protection on and keep agents off the bypass list. Then check anyway, because the controller's own reading of its refs catches whatever a missing or bypassed rule lets through.
Resume as a new run
After a stop the work usually still needs doing, and the tempting shortcut is to pick the stopped run up where it left off, with the same session, the same credentials and the same context. One real system never does. Resuming interrupted work always creates a new run. Its context is rebuilt from durable records, not carried over from the old run. Its capabilities are scoped afresh, and secrets stay in protected storage instead of passing across with the work. The revision and the approvals it relies on are verified again, and every effect the old run left ambiguous is reconciled before the new run starts. The model provider's own conversation session is never resumed as a substitute.
The reason is that a stopped run may not be as stopped as it looks, and the new run is not the only thing that might still act in its name. Queues show the shape of the problem: when a lease runs out the job goes to a second worker, and the expired lease does not stop the first one. The standard defense is a fencing token, a number that goes up each time the right to act is granted. Every write carries it, and the resource refuses a write whose number is lower than the newest it has accepted, so a paused holder with token 41 is turned away once a new holder's 42 has been seen. The check has to happen at the resource, together with the write, and a new holder should establish its number there before it acts, because an old write that arrives first is still accepted.
In that system the number is a lease generation, and every action taken on behalf of a run presents it. Stale generations are rejected, including late success callbacks from work that finished after its run was replaced, like a ticket for last night's show, which is perfectly genuine and still does not get you in tonight. After a pause, a lease is granted again only after a fresh challenge with a nonce the agent cannot forge, and a heartbeat queued before the pause proves nothing. The re-grant never extends the deadline or restores budget already spent, so stopping and resuming is never a way for a run to get more time or more money than it started with.
When an agent goes wrong, an order of operations
When an agent goes wrong, the order of what you do matters more than speed at any single step. A workable order has six steps, and each makes the next one safe. First, stop new work: hold dispatch, so nothing more is handed to the agent or to any run like it while you look. Second, stop the run and confirm it: close its sandbox, kill it and observe it empty, or quarantine it when you cannot. Third, revoke its credentials, which cuts off anything the process stop missed, including copies of its key. Revoking does not have to wait for a confirmed stop; it works whether or not the process is gone, so it can run alongside the second step.
Fourth, find what it changed, and take that from the controller's record rather than from the agent's account. The record is the journal of intended and applied effects, the refs and commits the controller registered, and an audit log the agent cannot alter. In one real system that log, with the ledgers and receipts beside it, cannot be updated or deleted at the database level, and every mutating request is written to it before any effect, refusals included; if the audit write fails, the request is refused. The refusals earn their place on a day like this, because what the agent tried and was denied shows what it was attempting.
Fifth, revert. Commits on a shared branch are undone with git revert, which adds a new commit reversing the old one and leaves history intact, rather than a reset and a force push that rewrite history other people and processes may depend on. A deployment goes back to the last verified version. Damaged data is restored or repaired, which is slower and has precautions of its own. Sixth, and only then, investigate the cause.
This is the general incident rule applied to an agent: stop the bleeding, then investigate, and stop the source of damage before repairing what it did. Two refinements carry over. Keep what is cheap to keep before you act, since the quarantined workspace, the journal and the audit log are the evidence; NIST's incident-response guidance (revised in 2025) treats collecting incident data and preserving its integrity as part of the response itself. And rehearse the steps while nothing is wrong. A stop or a revocation you have never run is a hope, not a tool.
What a stop cannot take back
A stop prevents the next effect. It does nothing about the last one. Once an effect has been delivered to a system you do not control, an email in someone's inbox, a payment settled, a message posted, a call another service has already acted on, no stop, revocation or revert inside your own systems reaches it. This is why approval belongs in front of such actions. OWASP's cheat sheet on AI agent security asks for explicit approval of high-impact or irreversible actions, and the approval has to come before they run, because afterwards is too late.
It helps to sort effects by how far an undo reaches. Effects inside your own systems can usually be reverted: a commit by a revert commit, a deployment by redeploying the last verified version. Damaged data can be repaired, from a backup or with a corrective change, at the cost of whatever changed after the recovery point. Effects delivered elsewhere can only be compensated: a refund for a payment, a correction after an email. A compensating action is a new effect, not an undo. The refund is a second transaction and the customer may see both; the correction arrives after the original was read, and perhaps acted on. Some effects cannot even be compensated. A secret the agent disclosed stays disclosed, and the only remedy is to revoke and replace it so the copy is worthless.
Two design rules follow. Put irreversible external actions behind the strictest checks and an approval of the exact action, since they are the ones no stop can recover; one real system counts them among its sensitive areas, which get their required checks at every risk level. And journal them like every other effect, so that after a stop you know which ones went out. Even that has an edge. A channel that cannot confirm delivery leaves the effect unknown, and unknown is what the record should say, not sent and not failed.
An agent you can switch off
Running a coding agent you don't have to trust means building the controller so it does not depend on the agent behaving well, and stopping is where that gets tested hardest. An agent you can stop only by asking it to stop, or by hoping its processes died with their parent, is an agent you are trusting. So is one whose changes you can find only by reading its own summary of what it did.
For a coding agent the pieces are concrete. Each sandbox gets a control group that is closed to launches before it is killed and observed empty afterwards, and a quarantine state the controller will not leave on anyone's say-so. The agent gets a dedicated, short-lived credential you can revoke at the issuer without locking yourself out, with a hard cap at the provider, on a project or workspace that holds only the agent's dedicated key. Every commit, push and external call goes into a journal before it happens, and the controller checks its registered branches on every cycle. Interrupted work resumes as a fresh run with a new generation, and an append-only log records every request to change anything, refusals included.
The same pieces apply to any agent with side effects. An assistant that sends email, an agent that files tickets or changes infrastructure, a helper on a homelab box that restarts services: each can be wrong or hijacked, and each needs a stop you can confirm, a credential you can cut, and a record that says what it did. The agents that touch money or send messages need the last part most, because their effects are the ones a stop cannot recall.
Rehearse the stop the way you would rehearse a rollback. Start a harmless run, stop it, confirm the stop from the controller's side, revoke its key and check that the next call is refused, then find its changes from the record alone. If any step surprises you, you have learned it on a quiet day instead of the day an agent goes wrong.
Sources
The published studies, standards and guidance this page cites. Links open on other sites. Field records come from the author's own records and have no public source.
- AI Agent Security Cheat Sheet (OWASP Cheat Sheet Series) cheatsheetseries.owasp.org
- RFC 7009: OAuth 2.0 Token Revocation (IETF, 2013) www.rfc-editor.org
- RFC 7662: OAuth 2.0 Token Introspection (IETF, 2015) www.rfc-editor.org
- Incident Response Recommendations and Considerations for Cybersecurity Risk Management, SP 800-61 Rev. 3 (NIST, 2025) csrc.nist.gov