# Synthetic run records and replay comparison — public example v1

This educational example was newly implemented and executed on 2026-10-07. It is not the original runner or a reconstruction of the September 21–22 experiments cited in Parts 7 and 8 of the quant research series. It contains no actual ETF data, model training, backtests, or trading functionality.

## Run in a fresh directory

The code uses Python 3.9+ syntax and only the standard library. For a new installation, use a currently supported Python 3 release. It was checked with Python 3.14.5 and 3.9.6 on the same Mac. This is not a claim of Windows/Linux validation or independent reproduction by another person.

Create an empty working directory and run these commands. On Windows, your installation may use `py -3` instead of `python3`.

```sh
curl -fLO https://forge-fate.com/examples/quant-records-v1.zip
python3 -m zipfile -e quant-records-v1.zip .
cd quant-records-v1
python3 -m unittest -v
python3 experiment.py demo --out my-runs
python3 experiment.py verify --out my-runs
```

The 10 unit tests end with `OK`. Both `demo` and `verify` should print:

```text
checks PASS (9 checks)
success: SYN_A=0.5, SYN_B=0.0, SYN_C=0.5, cash_weight=0.0
failure: FAILED / ValueError / no result.json
evidence: my-runs/verification.json
```

`demo` refuses to reuse an existing output directory. Use a new name such as `--out my-runs-2` for another experiment. `verify` reads and rechecks the saved files without modifying them. You can also verify the actual reference outputs shipped in the ZIP:

```sh
python3 experiment.py verify --out reference-runs
```

## Inputs and expected results

| File | Content | Expected outcome |
| --- | --- | --- |
| `inputs/success.json` | SYN_A 0.60, SYN_B 0.45, SYN_C 0.70 | A 0.5, B 0.0, C 0.5, cash weight 0.0 |
| `inputs/failure.json` | SYN_A 1.20 | FAILED, ValueError, no result.json |
| `inputs/config.json` | Threshold 0.55 | Equal weights for values at or above the threshold; all cash if none qualify |

These probabilities are manually chosen synthetic numbers, not model predictions or investment performance. The threshold and allocation rule exist to demonstrate the recording and checking process.

The calculation rejects empty inputs, out-of-range probabilities, strings, booleans, NaN, infinity, and invalid thresholds. Calculation validation is separate from file parsing and I/O failures. For example, JSON containing NaN is rejected while reading; this does not guarantee a FAILED record in a run folder. The included failure case is valid JSON containing the out-of-range value `1.20`.

## What gets saved

`demo` creates baseline success and failure runs, then reads their saved inputs and configuration to repeat both in new UUID folders. The output contains four run folders and `verification.json`. Folder names differ on every execution.

```text
my-runs/
├── <baseline success UUID>/ inputs.json, config.json, record.json, result.json, report.md
├── <baseline failure UUID>/ inputs.json, config.json, record.json, report.md
├── <replay success UUID>/   inputs.json, config.json, record.json, result.json, report.md
├── <replay failure UUID>/   inputs.json, config.json, record.json, report.md
└── verification.json
```

`record.json` stores UTC start/finish times, Python version, SHA-256 hashes of the code, inputs and configuration, the run ID, status, and error details. Inputs and configuration use sorted keys, two-space indentation, UTF-8, and a final newline; their saved bytes are hashed. Reports read record.json and, on success, result.json from disk instead of copying results from memory.

## Read the nine replay checks

| Key in verification.json | What it checks |
| --- | --- |
| `success_expected_result` | Both success results match expected/success-result.json |
| `failure_expected_record` | Both failures match the status, type and reason in expected/failure.json |
| `failure_result_absent` | Neither failure folder has a result.json |
| `distinct_run_directories` | All four runs have distinct folders |
| `matching_stable_file_bytes` | Baseline/replay inputs, config, result and report have identical presence and bytes |
| `matching_fixed_metadata` | Code/input/config hashes, version, status and errors match; hashes agree with actual files |
| `new_run_ids` | Recorded IDs match folder names and replay IDs differ from baseline IDs |
| `baseline_files_unchanged` | All baseline files match the snapshots taken before replay |
| `report_matches_expected` | Success/failure report bytes match the reports in expected/ |

The complete record.json files are not expected to match: run IDs must differ and timestamps are excluded from comparison. Reports omit these fields so outputs for the same inputs can be compared directly. Your IDs, timestamps and Python version may differ from those in reference-runs.

Verification uses explicit exceptions and still runs under `python3 -O`. Tests cover altered results, changed hashes, missing results, and protection against overwriting an existing folder. `verify` recomputes comparisons instead of trusting previously saved PASS flags.

## Files and limitations

- `experiment.py`: complete runner, records, report generation and replay comparison.
- `test_experiment.py`: 10 tests of normal/failure cases, change detection and CLI behavior.
- `inputs/`, `expected/`: synthetic inputs, configuration, expected results and reports.
- `reference-runs/`: four actual runs and verification evidence generated on 2026-10-07 with Python 3.14.5.
- `validation.json`: tested environments and scope.
- `SHA256SUMS`: file inventory and SHA-256 values, not a security signature or tamper-prevention mechanism.

`checks PASS` covers these two synthetic cases and the specified comparisons. Disk errors, forced termination, atomic writes, environment locking and complete research reproducibility are not implemented. Interruptions may leave RUNNING records or incomplete folders. The example does not prevent records and hashes from being altered together or guarantee identical bytes for all inputs across operating systems and Python versions.
