Rollback and Learnings

When an agentic direction fails, extract the learnings, rollback to a clean state, and branch from there -- because the cost of failure approaches zero.

●●●○○ Complexity

Overview

Rollback and Learnings is a recovery pattern for agentic development. When an agent goes down a wrong path — accumulating technical debt, reward hacking, or simply making poor architectural choices — the correct response is not to fix forward. Instead: extract the learnings from the failed attempt, rollback to a clean state, and restart with those learnings incorporated.

This pattern is viable because agentic code cost approaches zero. What used to take months of developer time now takes hours. Projects routinely go through multiple major refactors per month, and each one makes economical sense.

Implement Fail Learn HUMAN-IN-THE-LOOP Extract learnings ROLLBACK Clean state Incorporate learnings Branch from clean LEARNINGS

The Mindset Shift

Before AI agents, failing on an implementation could mean weeks or months of wasted work. The natural response was to fix forward — patch, adjust, and salvage what exists.

With agentic engineering, the cost calculation inverts. A full code rewrite that would have taken a team weeks now takes an agent hours. Projects can sustain 3 or more major refactors per month (70%+ code rewrite) and each one makes economical sense because:

  • The agent produces the new code quickly
  • The learnings from the failed attempt make the new attempt significantly better
  • Accumulated technical debt from patching is avoided entirely

The learnings are the valuable artifact, not the code.

How It Works

def rollback_and_learn(task, agent, max_attempts=3):
    """Implement with rollback-and-learn recovery."""
    learnings = []

    for attempt in range(max_attempts):
        # Start from clean state with accumulated learnings
        checkpoint = git_checkpoint()
        context = format_learnings(learnings) if learnings else ""

        result = agent.run(
            task=task,
            additional_context=context
        )

        if result.success:
            return result

        # Failed -- extract learnings before rollback
        new_learnings = human_review(
            what_went_wrong=result.failure_reason,
            code_produced=result.output,
            agent_reasoning=result.reasoning_trace
        )
        learnings.append(new_learnings)

        # Rollback to clean state
        git_restore(checkpoint)

    return escalate("Max attempts reached", learnings=learnings)

The critical step is the human review between failure and rollback. The human (or a Hawk Agent) examines what went wrong and distills it into concrete learnings: “the agent mocked the payment API instead of fixing the credentials,” “the approach of using WebSockets won’t work because the proxy doesn’t support them,” “the data model needs a junction table.”

These learnings are then fed as additional context to the next attempt, which starts from a clean state — not from the corrupted output of the failed attempt.

Why Not Fix Forward?

Fix-forward is the natural instinct, but it fails in agentic contexts because of the memetic virus problem: agents imitate what they find. If the codebase contains a failed attempt — messy patches, workarounds, dead code — the next agent session will treat all of it as intentional. The bad patterns propagate through every subsequent agent interaction.

Rolling back eliminates the contamination. The new attempt starts clean, informed only by the distilled learnings.

Prerequisites

This pattern requires:

  • Human-in-the-Loop — So the agent can signal failure rather than digging deeper into a wrong approach.
  • A proper harness — State management and checkpointing to enable clean rollback (see State Management & Checkpointing).
  • Human judgment — To decide when to rollback vs. push forward, and to extract meaningful learnings from the failure.

When to Use

  • After detecting reward hacking — do not fix forward, rollback.
  • When an agent has accumulated significant technical debt or workarounds.
  • When the architecture chosen by the agent turns out to be wrong.
  • Anytime the cost of cleaning up the mess exceeds the cost of starting fresh (which, with agents, is almost always).

When Not to Use

  • Minor issues that can be fixed with a targeted change.
  • When the overall approach is sound but a specific implementation detail needs adjustment.
  • When rollback would lose important state that cannot be easily reproduced.
  • Iterative Refinement — Improve existing output. Rollback + Learnings discards and restarts.
  • Plan-and-Execute — The plan phase can incorporate learnings from previous failed executions.
  • Human-in-the-Loop — Required for the agent to signal failure early rather than compounding it.