# Free sample: duplicate callback after simulated response loss

This standalone sample demonstrates one synthetic failure in two tiny SQLite models. It is independent of the paid Paid-Delivery Failure Lab package and imports none of its code. It contains one fixed public fixture, not the paid nine-case corpus.

In both models, a fake callback commits an event and entitlement. The observer then simulates losing the response, closes SQLite, opens a fresh connection, and replays the identical callback. No real request, payment, delivery, or network interruption occurs.

- **Broken:** the event is recorded once, but replay creates a second entitlement and a different receipt. The shared invariant fails as expected.
- **Corrected:** replay reads the receipt already stored in SQLite. One event and one entitlement remain, and the receipt is unchanged.

The successful demo exits zero only when it detects the broken result and the corrected model passes. The broken model is deliberate teaching code; do not copy it into an integration.

## Run locally

Requires Python 3.10+ with the standard `sqlite3` module. No package install is needed. Linux is verified for this preparation; macOS is untested. Windows is not supported.

From this folder, choose a new output directory whose parent already exists:

```sh
python3 -I -B sample.py --output ./demo-output
```

The following is expected:

```text
BROKEN: caught expected invariant failure (events=1, entitlements=2).
CORRECTED: invariant passed (events=1, entitlements=1; recovered same receipt=True).
PASS: simulated response-loss replay behaved as expected.
```

The `-I -B` flags isolate Python imports and suppress bytecode-cache writes. The script also accepts `python3 sample.py --output NEWDIR`.

The sample refuses credential-like environment variable **names**, including token, secret, password, API/access/private/recovery key, auth, database URL, seed, mnemonic, and keystore names. It does not inspect or print their values. This name check is a heuristic, not proof that an environment contains no credentials. Use a clean process to avoid refusal:

```sh
env -i PATH="$PATH" python3 -I -B sample.py --output ./demo-output
```

The chosen directory must not already exist. The script refuses `..`, symlinks in the target or any ancestor, and missing parent directories before creating output. It creates the new directory with mode `0700` and exclusively creates its own files. It does not overwrite an earlier run. An unexpected failure may leave an incomplete directory for inspection; choose a different new name when retrying.

These path checks assume an ordinary local filesystem. They do not protect against a hostile process changing paths concurrently or prove race/concurrency safety. Python/SQLite create local database journals while committing; no output outside the new directory is intended.

## Inspect the result

The output contains:

- `broken.sqlite3`: one event row, two entitlement rows.
- `corrected.sqlite3`: one event row, one entitlement row.
- `result.json`: deterministic counts, receipt comparisons, and invariant results.
- `result.txt`: the expected comparison transcript.

Compare `result.json` and `result.txt` with `expected-output.json` and `expected-output.txt`. The two SQLite files can be inspected using any local SQLite reader. For example, these read-only queries show the persisted state:

```sql
SELECT event_id, order_id FROM events;
SELECT receipt_id, order_id FROM entitlements;
```

All IDs are literal `demo-*` values. The fixture is intentionally fixed: the CLI refuses modified, extra, or real identifiers rather than accepting real payment data.

## Verify

```sh
python3 -I -B -m unittest discover -s . -p 'test_sample.py' -v
env -i PATH="$PATH" python3 -I -B sample.py --self-test
```

The test suite runs the real CLI in disposable new folders within this sample folder, then removes those test folders. It checks persisted counts, stored-receipt recovery, deterministic expected files, output refusals, modified fixtures, credential-name refusals, and guard behavior. It uses subprocesses to run the local sample; the sample itself does not launch processes.

`--self-test` directly emits seven Python audit events to verify rejection. It does not attempt a real socket connection, process launch, or provider operation. The CLI installs a supplemental CPython audit hook that rejects audited socket, process, and related events. Importing the module does not install a hook. This is an in-process guard, not an operating-system sandbox or protection against malicious code.

## What this demonstrates

This is a sequential, local reference example of one persisted state invariant and receipt recovery after a simulated lost response. It demonstrates neither a buyer's application nor the complete paid lab.

It does not verify provider authentication, webhook signatures, live payments, actual network behavior, concurrent callbacks, crash or power-loss durability, production access control, refunds, real file delivery, or House payment/recovery behavior. It is not an SDK adapter, integration certification, security audit, or production recovery service.

The observer retains the first receipt for comparison even though the simulated caller loses its response. The corrected replay actually reads the stored receipt; the tests change that stored value and reopen SQLite to verify retrieval.

See `sample-license.txt` for permission to use this free sample. This standalone free sample does not connect to paid checkout or real providers.
