# Stable record sorting: generated failure and correction evidence

This original teaching kit calls real small sorting implementations with pinned
Hypothesis, retains generated and shrunk counterexamples, and replays the
retained inputs after correction. It is not a benchmark, a production-system
test, an exhaustive domain check, or a proof of purity or correctness.

Inputs are immutable tuples of 0–8 records `(key, tag)`. Keys are integers -2..2,
tags are exactly `a`, `b`, or `c`, and complete records may repeat. Sorting uses
the key alone; exact record multiplicity and stable equal-key order are required.
The full fictional policy and exclusions are in `fixture/requirements.md`.

## Reproduce with isolated dependencies

Use Python 3.11 or newer, as required by the pinned Hypothesis package. The
retained run records the actual version used, not a tested version matrix.
In an extracted kit directory run these commands, one at a time:

```sh
python3 -m venv .venv
.venv/bin/python -m pip install -r requirements.txt
.venv/bin/python -B reproduce.py --output local-rerun
```

On Windows use `.venv\Scripts\python` in place of `.venv/bin/python`.
Dependency installation needs the package index; the tests themselves need no
network or Proof installation. The output directory must not already exist.
The retained `captured/` is never overwritten by these commands.

## Six jobs, with distinct meanings

Two weak campaigns check ordered output, input preservation, repeat-call
determinism and sorting idempotence. Both the duplicate-dropping baseline and
the always-empty implementation can pass those weak assertions. Additional
observed diagnostics expose their multiplicity failures; set equality is also
recorded because it cannot detect the deduplication defect.

Two genuine Hypothesis searches assert multiplicity on the baseline and stable
equal-key order on an unstable alternative. They enable generation and shrinking,
disable the example database, and retain raw debug output, actual AssertionError
notes, every observed harness evaluation and the first/final failing input.
`counterexample.json` reports the shrunk input under this strategy and run. It
does not claim a globally smallest counterexample. The actual shrinking log is
inspectable in each search's captured stdout.

The corrected campaign asserts the combined contract against generated values.
An explicit regression job separately runs eight named cases and the two
retained generated inputs against all four implementations. Those ten case
entries overlap; they are not ten unique inputs or another generated campaign.
`regression-replay/counterexample-regression.json` contains the compact
before/after replay observations for the two retained inputs.

Child exit 1 in a search records the expected assertion failure. Child exit 0
on a weak campaign means only its weak asserted gate passed. The wrapper exits
0 only when the expected contrasting outcomes all reproduced and source hashes
stayed unchanged. It does not mean every implementation satisfies the contract.
Child exceptions, missing artifacts or missing shrink evidence are not accepted
as successful failure reproduction.

## Count observations, not configuration

`max_examples=200` is configuration. `summary.json` records actual harness body
evaluations, distinct primary inputs for that job, actual implementation calls,
and each property's pass/fail/not-assessed counts. Each harness evaluation calls
the selected implementation three times: primary, repeat on the original input,
and sorting the primary output for idempotence. Calls to Python's reference
`sorted` are not counted as calls to the teaching implementation.

Generation, shrinking and Hypothesis repetition can revisit inputs. The raw
engine's internal draw/shrink counters can differ from body-entry counts. Do
not relabel either as unique input coverage. The saved JSONL observations are
compressed separately from compact summaries. Per-job distinct inputs are not
added into a union across jobs. Replay entries also overlap generated inputs.

The seed helps rerun the pinned configuration. Changes to the strategy, test,
Python, Hypothesis or external state can change generation. For durable
regressions, use the explicit saved input rather than relying only on a seed or
the version-specific `@reproduce_failure` blob retained in the failure notes.

## Inspect implementation linkage and limits

`fixture/sorting.py` contains the insertion-sort correction and three deliberate
wrong versions. It has no imports or mutable module data; its transitive helper
call remains in that file. It compares keys only, uses local lists and returns
immutable tuples of immutable values. This static review supports the declared
pure boundary. Finite repeat-call and input-preservation observations do not
establish absence of hidden state or I/O for arbitrary code.

The independent checks use adjacent keys, `Counter` over complete records and
per-key subsequences; stability is not assessed when multiplicity has failed.
The readable expected output uses Python's documented stable key sort, rather
than the insertion algorithm under test. Idempotence is justified for this
sorting contract; it is not required of every pure function. No commutativity
or associativity claim applies to this unary function.

No Proof command, SMT solver or model verifier was executed. Proof's authored
model analysis and model-derived fixture generation are separate from the real
implementation calls in this kit. See `captured/execution-report.json` for exact
commands, timestamps, runtime, exits and source/output hashes. `SHA256SUMS` covers
kit source and original capture; the ZIP has a separate checksum.
