<?xml version="1.0" encoding="UTF-8"?>
<spec xmlns="https://vibevm.org/spec/1">
  <title id="root">The Raid Playbook — Layered Refactoring Sweeps</title>
  <status stage="spec" state="done"/>
  <p p="1"><fact id="status-line" status="impl/done">**Discipline v0.2 · status: BETA · T1**</fact></p>
  <p p="2"><fact id="RAID-IS-THE-MACRO-RHYTHM" status="impl/done">*The macro-rhythm of the Discipline.*</fact></p>
  <p p="3"><fact id="INLINE-PER-EDIT-VERSUS-RAID-PER-LAYER" status="impl/done">*Inline triggers (the micro-rhythm) apply cards per-edit where cheap; a RAID applies a set of cards across a whole layer when per-edit triggers cannot keep up — because attention budget is exhausted, or a new card is adopted repo-wide.*</fact></p>
  <p p="4"><fact id="RAID-IS-ITSELF-SPEC-DRIVEN" status="impl/done">*A raid is itself spec-driven and follows the same gate discipline as the original terraform.*</fact></p>
  <section id="when-a-raid" title="0. When a raid, not an inline trigger">
    <p p="5"><fact id="INLINE-HANDLING-IS-ALWAYS-PREFERRED" status="impl/done">Inline (edit-time) handling is always preferred.</fact></p>
    <p p="6"><fact id="escalate-to-a-raid-lead" status="impl/done">Escalate to a raid when:</fact></p>
    <list ordered="false" p="7">
      <item><fact id="ESCALATE-TRIGGER-IS-RAID-MODE" status="impl/done">a card's trigger is **raid-mode** by nature (e.g. "naming uniformity across a crate" — not worth firing per keystroke, only meaningful in bulk);</fact></item>
      <item><fact id="ESCALATE-NEW-CARD-ADOPTED" status="impl/done">a **new card is adopted** and must be applied to existing code repo-wide;</fact></item>
      <item><fact id="ESCALATE-DEBT-PAST-THRESHOLD" status="impl/done">**debt accumulates** past a threshold (the A6 debt ledger trips a tripwire);</fact></item>
      <item><fact id="ESCALATE-ATTENTION-BUDGET-INSUFFICIENT" status="impl/done">the swarm's **attention budget is structurally insufficient** for a class of cross-cutting concern, so it is swept periodically instead of held active.</fact></item>
    </list>
  </section>
  <section id="plan-skeleton" title="1. Raid plan skeleton (every raid is authored to this shape)">
    <list ordered="true" p="8">
      <item><fact id="SKELETON-SCOPE-AND-FREEZE" status="impl/done">**Scope &amp; freeze.** Which layer(s)/crates are in scope; which surfaces are frozen for the raid's duration. Frozen surfaces may not change except by the raid.</fact></item>
      <item><fact id="SKELETON-CARD-SET-AND-ORDER" status="impl/done">**Card set &amp; order.** The cards to apply, **topologically sorted** by their Band-3 `raid_role.order` dependencies. Example ordering constraint: naming-uniformity (Class B/names) BEFORE contract-extraction (Class C), because contracts cite names; differential-oracle (Class D) wraps every behavior-changing card as a gate.</fact></item>
      <item><fact id="SKELETON-PER-LAYER-PHASES" status="impl/done">**Per-layer phases.** The sweep proceeds layer by layer (seams → cells → registry → tests, for Rust), each phase gated green before the next begins. This is the owner's "refactor everything by layers."</fact></item>
      <item><fact id="SKELETON-BATCH-UNITS-AND-CHECKPOINTS" status="impl/done">**Batch units &amp; checkpoints.** Per-cell or per-crate batches; each batch has a green-gate checkpoint. The raid is **resumable** — a crash or pause never loses progress, and the raid is never one giant diff (R3-013 determinism; phantom-diff avoidance). WAL-backed where the project keeps a WAL (recommended); otherwise the plan-status-line fallback (`06-WAL-CONVENTION.xml`, `05-CAMPAIGN-FORM.xml` §4).</fact></item>
      <item><fact id="SKELETON-DIFFERENTIAL-SAFETY" status="impl/done">**Differential safety.** Every card application that changes behavior carries its Class-D oracle. The raid **cannot move behavior silently** — a behavior change without a passing oracle blocks the batch.</fact></item>
      <item><fact id="SKELETON-EXIT-CRITERIA" status="impl/done">**Exit criteria.** All targeted cards' checkers green across scope; the raid's debt ledger at zero; a **raid REPORT** (modeled on the terraform REPORT) listing what the sweep learned — including cards that misfired, false-positive triggers, and routines that overloaded weak readers. The REPORT feeds card revision (cards are beta, revised on pilot evidence only).</fact></item>
    </list>
  </section>
  <section id="roles" title="2. Roles in a raid">
    <list ordered="false" p="9">
      <item><fact id="ROLE-STRONG-AUTHOR-ORCHESTRATOR" status="impl/done">**Strong author/orchestrator** — authors the raid plan, sets scope and order, adjudicates review-mode triggers.</fact></item>
      <item><fact id="ROLE-WEAK-SWARM" status="impl/done">**Weak swarm** — executes per-batch routines (the Band-3 extract of each card), one batch per agent, meeting only at the merge (R3-013: parallel agents share no state; the merge is the contention point).</fact></item>
      <item><fact id="ROLE-TOOLCHAIN" status="impl/done">**The toolchain** — runs checkers per batch (conform tiers, `cargo test -p &lt;cell&gt;`, oracles), emits structured diagnostics (Class F) as the agents' percepts.</fact></item>
    </list>
  </section>
  <section id="terraform-relationship" title="3. Relationship to the original terraform">
    <p p="10"><fact id="first-terraform-proved-the-machinery" status="spec/done">The first pilot terraform proved the gate-and-phase machinery: phases −1…6, frozen baselines, green gates, a closing REPORT of what the discipline learned about itself.</fact></p>
    <p p="11"><fact id="MACHINERY-DISTILLED-INTO-SHIPPED-DOCUMENTS" status="impl/done">That machinery is now distilled into shipped documents: a raid is the *in-flight* generalization (this playbook — the standing mechanism for applying any card-set across any layer, any time), and `05-CAMPAIGN-FORM.xml` is the *paper trail* (plan / baseline / predictions / log / report) every campaign writes so it can be executed cold and resumed at any phase boundary.</fact></p>
    <p p="12"><fact id="BROWNFIELD-ADOPTION-IS-A-SEQUENCE-OF-RAIDS" status="impl/done">Adopting the Discipline on an existing codebase follows `mechanisms/BROWNFIELD-PROTOCOL-v0.1.xml` and is itself executed as a sequence of raids; the standing between-campaigns counterpart is `04-SWEEP-PLAYBOOK.xml`.</fact></p>
  </section>
  <section id="raid-cadence" title="4. Cadence">
    <list ordered="false" p="13">
      <item><fact id="CADENCE-MICRO-CONTINUOUS" status="impl/done">**Micro (continuous):** inline triggers in the per-cell loop.</fact></item>
      <item><fact id="CADENCE-GATE-PER-MERGE" status="impl/done">**Gate (per-merge):** gate-mode triggers at the cell's verification gate.</fact></item>
      <item><fact id="CADENCE-RAID-SCHEDULED" status="impl/done">**Raid (scheduled/on-adoption):** layered sweeps per this playbook.</fact></item>
      <item><fact id="CADENCE-REVIEW-AS-FLAGGED" status="impl/done">**Review (as-flagged):** review-mode triggers escalated to a stronger reader.</fact></item>
    </list>
    <p p="14"><fact id="answers-when-to-switch-on-refactoring" status="impl/done">Together these answer "when do we switch on rethinking and refactoring": continuously where cheap, in planned sweeps where not.</fact></p>
  </section>
</spec>
