agenticlately · GH-600 Study Prep
Home Phase B Lesson 2.7
PHASE B · LESSON 2.7

Safe execution — error handling, retries, rollbacks

An autonomous agent without safety gear is just unsupervised damage. Safe execution is the gear: catch bad actions before they run (error handling), retry transient failures, and undo cleanly (rollbacks) — all inside limits that stop a runaway. The main mechanism: hooks.

~11 minread 4quiz questions Tier 1source cited
Story

No serious site lets people work at height without safety gear. There's a harness that catches a fall before it happens, a breaker that trips when a circuit pulls too much, and — when something's built wrong — the ability to rip it out and redo rather than live with it.

An autonomous agent (lesson 2.6) needs the same three things, or "autonomous" just means "unsupervised damage." Safe execution is the gear: catch bad actions before they run (error handling), try again sensibly when something's flaky (retries), and undo cleanly when a change is wrong (rollbacks) — all inside limits that stop a runaway.

The idea, in plain English

Three official sub-skills:

Plus the execution limits that bound a run so it can't loop forever. The main mechanism for catching things is hooks — scripts that fire at key moments and can block a tool before it runs.

Error handling — catch it before it runs

Hooks are the agent's tripwires: scripts that run automatically at session moments (config = JSON with "version": 1 in .github/hooks/*.json or ~/.copilot/hooks/). The key one for safety:

The crucial nuance — hooks are FAIL-OPEN

"Hook failures are logged but never stop the agent." A hook is a guardrail, not a hard security boundary — if it errors, the agent keeps going. (Rhymes with 2.4's "allowlist isn't tamper-proof": agent safety controls are guardrails, not locks.) In Actions, error handling also uses status functions like if: failure() to run a recovery step only when something failed.

Retries + rollbacks

Retries — try again, sensibly:

Rollbacks — undo cleanly:

Execution limits — bound the run

ControlWhat it bounds
--max-autopilot-continueshow many times the agent auto-continues (default varies by CLI version — recent versions cap it; set it explicitly to be sure)
subagent nesting depth limithow deep agent-spawns-agent can go
hook timeoutSec (default 30s)how long a hook may run
temporary cloud filesystemhow long a change persists
Worked example — block a dangerous command before it runs
// .github/hooks/guard.json
{ "version": 1,
  "preToolUse": [
    { "matcher": "shell",
      "command": "grep -q 'rm -rf' <<< \"$TOOL_ARGS\" && echo '{\"permissionDecision\":\"deny\",\"permissionDecisionReason\":\"blocked rm -rf\"}' || true",
      "timeoutSec": 10 } ] }
One-look contrast — the safety knobs

error handling = stop it before it happens (preToolUse deny) · retries = try transient failures again (continueOnAutoMode) · rollbacks = undo after the fact (/rewind, revert PR) · limits = stop runaways (max-continues, timeout). But the deny only holds if the hook itself runs — fail-open means a broken hook won't protect you.

The cert-language version

Safe execution combines error handling (hooks like preToolUse that allow/deny/modify a tool call before it runs — but fail-open), retries (e.g. continueOnAutoMode, Actions if: failure()), rollbacks (/rewind the last turn, or revert/don't-merge the PR), and execution limits (--max-autopilot-continues, subagent depth, hook timeout). These are guardrails layered on top of least-privilege and human review — not hard locks.

Our summary · grounded in GitHub Docs (Copilot hooks reference, CLI command reference, Actions expressions) + MS Learn — Agent tooling… · fetched 2026-05-30

Common confusions (read these or lose points)

Ticks this lesson done on the home roadmap. Saved in this browser.

Quiz · Lock it in

0 / 0 answered
Q1 · multiple choice

A preToolUse hook denies a dangerous tool call. What's the critical caveat?

Answer · C. Hook failures are logged but never stop the agent — fail-open. So hooks are guardrails layered on least-privilege + human review, not hard security boundaries.
Q2 · multiple choice

A preToolUse hook returns permissionDecision: "ask" while running in the cloud agent. What happens?

Answer · B. In the cloud agent there's no interactive user, so 'ask' becomes 'deny'. (Same reason ask_user is useless in the cloud — lesson 2.1/2.6.)
Q3 · multiple choice

An agent opened a PR with a bad change. What's the cleanest rollback?

Answer · D. Because the agent works on a branch → PR, the base branch is untouched until merge. Not-merging or reverting the PR is the rollback; /rewind undoes the last agent turn including file edits.
Q4 · explain back

In your own words: give one mechanism each for error handling, retries, rollbacks, and execution limits — and state the big caveat about hooks.

Suggested answer

Error handling: a preToolUse hook that returns deny to block a tool before it runs (Actions: if: failure()). Retries: continueOnAutoMode retries when rate-limited. Rollbacks: /rewind undoes the last turn + file edits, or revert/don't-merge the PR. Limits: --max-autopilot-continues (set it explicitly — default varies by CLI version), subagent depth, hook timeoutSec (30s). Caveat: hooks are fail-open — a broken hook is logged but doesn't stop the agent, so it's a guardrail, not a hard lock.


  
Source · GitHub Docs (Copilot hooks reference, CLI command reference, Actions expressions) + MS Learn — Agent tooling, MCP, and execution environments · fetched 2026-05-30

Unofficial study material. Not affiliated with, endorsed by, or sponsored by GitHub or Microsoft. “GH-600” and “GitHub” are trademarks of their respective owners, used for identification only.