# Phases 4 and 5: Structured output and independent verification

Goal: one machine-checked file of every candidate and a short report that says nothing the file does not.

## Phase 4: write opportunities.json

Write a JSON array to `<output directory>/opportunities.json`.
Every candidate from Phase 3 gets one record, including rejected ones.
The contract is [opportunity-schema.json](opportunity-schema.json); [opportunities.example.json](opportunities.example.json) shows one record of each shape from a real scan.

### Record shapes

| `verdict` | When | Required beyond the common fields |
| --- | --- | --- |
| `confirmed_opportunity` | survived the disproof checklist and Phase 5 reproduced it | `score`, `demand`, `weaknesses`, `estimate`, `complaint_themes`, `monetization`, `wedge`, `verification` |
| `needs_validation` | a decisive fact is not public | `score`, `blocker`, `missing`, `next_step` |
| `rejected` | a checklist line holds | `reason`, `detail` |

Common fields on every record:

- `fingerprint`: `store:country:id`, for example `apple:us:6444708069` or `play:us:com.calm.android`. The same app in the same country always has the same fingerprint, so a later hunt can match it.
- `app`: `store`, `country`, `id`, `name`, `developer`, optional `url`.
- `sources`: each call that produced a number, with its `fetched_at` time in UTC.

Copy `score` exactly as the engine returned it (`score`, `demand`, `weakness`, `flags`); do not edit numbers by hand.
Sort records by `fingerprint`.

### Validate

From this skill folder:

```bash
node validate-opportunities.cjs <output directory>/opportunities.json
```

Exit 0 prints `ok: N records valid`.
Exit 1 prints one line per problem; fix the record, never the schema or the validator.
Exit 2 means the file could not be read or is not JSON.

Besides the schema, the validator checks:

- fingerprints match `app`, are unique and are sorted;
- the score follows from demand and weakness within 2 points;
- a confirmed record scores 60 or more, and its Phase 5 score is within 10 points;
- every score flag has a matching weakness, and `low_rating` needs a rating under 4.2;
- estimate ranges have low at or below high, and an MRR range has a currency;
- `installs` as a velocity source appears only on Google Play;
- quotes hold no link, email address or handle, and stay within 160 characters.

## Phase 5: independent verification

Verification must not reuse Phase 3 notes.
Use a fresh agent when the platform has one; otherwise start a fresh pass that reads only `opportunities.json`.

For each `confirmed_opportunity`:

1. Call `app_tracker_lookup` again for the same `app.id` and `country`.
2. Check the score: within 10 points of the recorded one.
3. Check the top weakness: the same flag is still present, or the top complaint theme still has a share of 0.25 or more.
4. Check the estimate: the new downloads range overlaps the recorded one.
5. All three hold: set `verification` to `{"result":"reproduced","checked_at":"<now>","score_seen":<new score>}`.
6. Any check fails: change the record to `needs_validation` with `missing: ["score_not_reproduced"]`, a `blocker` that names the failed check, and a `next_step`.

Re-run the validator after every change.

## Report

Write `<output directory>/REPORT.md` only after the validator passes.
Derive every line from `opportunities.json`; if prose and JSON disagree, the JSON wins and the prose is wrong.

```markdown
# App opportunities: <genre>, <store>, <country>, <date>

Scanned <N> chart entries and <M> search results; <C> confirmed, <V> need validation, <R> rejected.

## Confirmed
| App | Score | Weakness | Estimate (downloads per month, MRR) | Wedge |

## Needs validation
| App | Score | Missing | Next step |

## Rejected
| App | Reason |

## Method and limits
Stale Winner score, estimate method, fetch times, and any warnings.
```

Keep the report under one screen per section.
Give estimates as ranges with the word estimate, and name the store and country in the title.
