<?xml version="1.0" encoding="UTF-8"?>
<spec xmlns="https://vibevm.org/spec/1">
  <title id="root">CARD: scaffold-d-differential-oracle — Differential / Characterization Oracle (Go)</title>
  <status stage="spec" state="done"/>
  <p p="1"><fact id="status-line" status="impl/done">**Discipline v0.2 · BETA · T2 · Go**</fact></p>
  <p p="2"><fact id="reference-instance-note" status="impl/done">*Reference instance of the AI-Native Pattern Card format, Go projection.*</fact></p>
  <p p="3"><fact id="demonstrates-all-three-bands" status="impl/done">*Demonstrates
all three bands, especially the operational Band 3.*</fact></p>
  <p p="4"><fact id="card-is-beta" status="impl/done">*This card is itself BETA (its
conform checker is specified; the pilot instance is the `research/go-demo` fuzz
differential).*</fact></p>
  <section id="band-one-identity" title="Band 1 — Identity &amp; Recognition">
    <p p="5"><fact id="CLASSIFICATION" status="impl/done">**Classification:** layer = E (Verification coupling); mechanism = scaffold class D.</fact></p>
    <p p="6"><fact id="INTENT" status="impl/done">**Intent:** When code is replaced or refactored, pin its observable behavior with a
runnable check that compares the new implementation against the old one (differential)
or against a captured baseline (characterization), so that a reader — especially a weak
one — can change code freely and receive a pass/fail signal on whether behavior moved.
Go's projection stands on **native fuzzing**: the language ships the input generator,
the corpus store, and the minimizer in the standard toolchain.</fact></p>
    <p p="7"><fact id="ALSO-KNOWN-AS" status="spec/done">**Also Known As:** golden test; characterization test (Feathers); approval test;
back-to-back test; differential fuzzing; `FuzzXxx` target; `testdata/` corpus.</fact></p>
    <p p="8"><fact id="applicability-recognition-lead" status="impl/done">**Applicability / Recognition:** Apply when ANY of these signals are present —</fact></p>
    <list ordered="false" p="9">
      <item><fact id="SIGNAL-CELL-IS-BEING-REPLACED" status="impl/done">a cell is being *replaced* or its internals *rewritten* while its contract is meant to
  stay fixed (the replacement protocol, R-040, guide §10);</fact></item>
      <item><fact id="SIGNAL-LEGACY-BEHAVIOR-IS-UNDERSTOOD-BY-NOBODY" status="impl/done">legacy behavior exists that nobody fully understands but must be preserved;</fact></item>
      <item><fact id="SIGNAL-REFACTOR-SPANS-MULTIPLE-FILES" status="impl/done">a refactor spans multiple files and the reader cannot prove by inspection that
  behavior is unchanged;</fact></item>
      <item><fact id="SIGNAL-WEAK-AGENT-NEEDS-A-SAFETY-NET" status="impl/done">a weak agent is assigned a modification task and needs a safety net it cannot derive.</fact></item>
    </list>
    <p p="10"><fact id="DETECTOR-SEED" status="impl/done">*Detector seed:* a diff that modifies the body of an item carrying
`//spec:implements …` without a corresponding oracle artifact (a `Fuzz`/differential
test or a golden) in the cell's test files → recognition fires.</fact></p>
  </section>
  <section id="band-two-justification" title="Band 2 — Justification &amp; Tradeoffs">
    <p p="11"><fact id="MOTIVATION" status="spec/done">**Motivation:** A weak agent is asked to optimize the naive planner cell. It rewrites
the diff loop. By inspection, neither the agent nor a fast reviewer can be sure the
change preserved behavior across edge states. With a differential fuzz target —
`FuzzPlannersAgree` feeding identical generated (desired, actual) state pairs to
`naiveplanner` and `batchplanner` and asserting equal action sets — the agent gets an
immediate mechanical verdict: behavior held, or here is a minimized counterexample the
fuzzer already shrank. The expensive cognition ("what are all the edge states?") was
materialized once as a harness plus a committed seed corpus; the weak agent consumes
the verdict.</fact></p>
    <p p="12"><fact id="structure-and-participants-lead" status="impl/done">**Structure &amp; Participants:**</fact></p>
    <list ordered="false" p="13">
      <item><fact id="PARTICIPANT-SUBJECT-OLD" status="impl/done">*Subject-old* — the prior implementation (kept temporarily, or captured as goldens in
  `testdata/`).</fact></item>
      <item><fact id="PARTICIPANT-SUBJECT-NEW" status="impl/done">*Subject-new* — the replacement.</fact></item>
      <item><fact id="PARTICIPANT-INPUT-SOURCE" status="impl/done">*Input source* — a `FuzzXxx` target's generated inputs + the committed `testdata/`
  seed corpus (runs deterministically in plain `go test`; `-fuzz` explores locally).</fact></item>
      <item><fact id="PARTICIPANT-COMPARATOR" status="impl/done">*Comparator* — the equality/equivalence predicate (deep-equal, or a documented
  divergence list).</fact></item>
      <item><fact id="PARTICIPANT-ORACLE-HARNESS" status="impl/done">*Oracle harness* — the fuzz/differential test in the cell's `_test.go`, run with
  `-race`.</fact></item>
    </list>
    <p p="14"><fact id="COLLABORATIONS" status="impl/done">**Collaborations:** Pairs with Class B (defined types shrink the input space the oracle
must cover) and Class C (contracts define what "equivalent" means). Consumes Class E
(the per-package loop runs the seeds). Emits Class F diagnostics (a failure cites the
violated REQ + the minimized counterexample). In a raid, this card is the
*differential-safety* gate every behavior-changing card application must pass.</fact></p>
    <p p="15"><fact id="goals-and-non-goals-lead" status="impl/done">**Goals / Non-Goals:**</fact></p>
    <list ordered="false" p="16">
      <item><fact id="GOALS" status="impl/done">*Goals:* detect unintended behavior change during replacement/refactor; give weak
  readers a modification safety net; make "behavior preserved" a machine fact.</fact></item>
      <item><fact id="NON-GOALS" status="impl/done">*Non-Goals:* NOT a correctness proof (new-vs-old agreement inherits the old bugs);
  NOT a substitute for the spec; NOT for greenfield code with no prior behavior;
  NOT open-ended CI fuzzing (CI runs the committed seeds — deterministic; exploration
  is a local/scheduled activity).</fact></item>
    </list>
    <p p="17"><fact id="consequences-lead" status="impl/done">**Consequences:**</fact></p>
    <list ordered="false" p="18">
      <item><fact id="CONSEQUENCE-REFACTORING-BECOMES-SAFE" status="spec/done">(+) Aggressive refactoring becomes safe; drift is caught mechanically, with shrunk
  counterexamples for free (the toolchain minimizes).</fact></item>
      <item><fact id="CONSEQUENCE-IMPLEMENTATION-AND-CONTRACT-VARY-INDEPENDENTLY" status="spec/done">(+) "Change the implementation" and "preserve the contract" vary independently.</fact></item>
      <item><fact id="CONSEQUENCE-ENCODING-AND-COMPARATOR-COST-EFFORT" status="spec/done">(−) Authoring the input encoding for fuzz args (fuzzing takes primitive-typed args;
  structured states need a decode step) and the comparator costs effort.</fact></item>
      <item><fact id="CONSEQUENCE-GOLDENS-ENSHRINE-CURRENT-BEHAVIOR" status="spec/done">(−) Characterization goldens enshrine current behavior including bugs — pair with a
  spec edge marking intentional vs incidental; goldens must fail loudly, never
  auto-update (the `-update` flag never runs in CI).</fact></item>
    </list>
    <p p="19"><fact id="alternatives-lead" status="impl/done">**Alternatives:**</fact></p>
    <list ordered="false" p="20">
      <item><fact id="ALTERNATIVE-FORMAL-PROOF" status="spec/done">*Formal proof:* Go has no mainstream Kani/Creusot analogue — the differential/property
  oracle carries proportionally more of the modification-safety load here (the same
  honest asymmetry the TS card records).</fact></item>
      <item><fact id="ALTERNATIVE-MANUAL-REVIEW" status="spec/done">*Manual review:* fails exactly where needed (large multi-file edits, weak readers).</fact></item>
      <item><fact id="ALTERNATIVE-FRESH-UNIT-TESTS" status="spec/done">*Fresh unit tests:* test what the author thought to test; the differential oracle
  tests behavior the author never enumerated.</fact></item>
    </list>
    <p p="21"><fact id="risks-and-assumptions-lead" status="impl/done">**Risks &amp; Assumptions:**</fact></p>
    <list ordered="false" p="22">
      <item><fact id="RISK-OLD-IMPLEMENTATION-IS-AVAILABLE" status="spec/done">Assumes the old implementation is available or capturable.</fact></item>
      <item><fact id="RISK-FUZZ-ENCODING-REACHES-REPRESENTATIVE-STATES" status="spec/done">Assumes the fuzz encoding reaches representative states; a weak encoding gives false
  confidence — seed the corpus with the known hard cases.</fact></item>
      <item><fact id="RISK-SUNSET" status="spec/done">*Sunset:* if generation-time tooling plus contracts ever make behavior-preservation
  statically provable for a class of cells, the oracle retires for that class.</fact></item>
      <item><fact id="RISK-TRANSFER" status="spec/done">Transfer risk: executable-scaffold value for *modification* is [E-mid]; this card is
  a prime pilot validation target on `research/go-demo`.</fact></item>
    </list>
    <p p="23"><fact id="EVIDENCE-AND-TRANSFER-STRENGTH" status="spec/done">**Evidence &amp; Transfer-strength:** R-040 (replacement protocol, production), R2C-008
(executable scaffolds transformative, benchmark), Feathers characterization
(production). Class: production + benchmark. Tag: **[E-mid]**.</fact></p>
  </section>
  <section id="band-three-operation" title="Band 3 — Operation">
    <p p="24"><fact id="TRIGGER" status="impl/done">**Trigger:** WHEN a diff modifies the body of an item bearing `//spec:implements …`,
OR a cell is marked for replacement (`replaces=` in its manifest directive), OR a
refactor touches &gt; 1 file in a cell whose contract is unchanged — THEN apply before
merge.</fact></p>
    <p p="25"><fact id="MODE" status="impl/done">**Mode:** gate.</fact></p>
    <p p="26"><fact id="routine-lead" status="impl/done">**Routine** (≤7 steps, each verifiable):</fact></p>
    <list ordered="true" p="27">
      <item><fact id="ROUTINE-IDENTIFY-THE-BEHAVIORAL-SURFACE" status="impl/done">Identify the behavioral surface to preserve (the seam's methods).</fact></item>
      <item><fact id="ROUTINE-KEEP-OLD-REACHABLE" status="impl/done">Keep `old` reachable (the replaced cell stays in the tree until the oracle is green),
   or capture goldens from it into `testdata/`.</fact></item>
      <item><fact id="ROUTINE-WRITE-THE-DIFFERENTIAL-TARGET" status="impl/done">Write/extend a `FuzzXxx` differential target decoding fuzz bytes into representative
   seam inputs; seed `testdata/` with the known hard cases.</fact></item>
      <item><fact id="ROUTINE-BIND-OLD-VS-NEW" status="impl/done">Bind `old` vs `new` under the comparator (documented divergence list otherwise).</fact></item>
      <item><fact id="ROUTINE-RUN-SEEDS-IN-THE-LOOP" status="impl/done">Run seeds in the per-package loop (`go test -race`); on a counterexample, fix `new`
   (NOT the oracle) until green; let `-fuzz` explore locally before landing.</fact></item>
      <item><fact id="ROUTINE-REMOVE-OLD-ONCE-GREEN" status="impl/done">Once green, remove `old` (or commit the goldens) and leave the oracle + corpus in
   place.</fact></item>
      <item><fact id="ROUTINE-TAG-THE-ORACLE" status="impl/done">Tag the oracle `//spec:verifies &lt;uri&gt; r=&lt;N&gt;`.</fact></item>
    </list>
    <p p="28"><fact id="CHECKER" status="spec/done">**Checker:** conform rule `replacement-has-oracle` — flags a modified
`//spec:implements` item whose cell lacks a differential/characterization test
referencing it; backed by `go test ./&lt;cell&gt;/ -race` running the seeds. *(Status:
specified; pilot task.)*</fact></p>
    <p p="29"><fact id="RAID-ROLE" status="impl/done">**Raid role:** layer = *behavior-preserving* (wraps every behavior-changing card in any
raid); batch = per-cell.</fact></p>
    <p p="30"><fact id="BUDGET" status="impl/done">**Budget:** gate-time, does not crowd the edit-time active set; first-signal = one
per-package seed run (target &lt; 60s; corpus size tuned to stay in budget).</fact></p>
  </section>
</spec>
