# report-adapter — result ## Changed files - `web/src/swarm.ts`: appended the native IMD.fun report reader; the first import line now also pulls `purchasedEventIds` and `validateBundle` from `./engine/report`. Every legacy export (`fetchReport`, `retryMissing`, `download`, `hashMatches`, `GATEWAYS`, `Fetched`, …) is unchanged. - `web/tests/native-report.test.mjs` (new): 8 offline tests. - `web/tests/fixtures/native-report/historical-snapshot.json` (new): a snapshot shaped like the one for historical job 3be0bfc6-0c5d-429d-b70e-1a4c89fe2c2a. I copied the format only. The test fills in the hash and size. - I left `web/src/predict.ts` and `web/src/engine/report.ts` alone. The existing `parseBundle`, `validateBundle`, `purchasedEventIds` and `validate` already cover the checks, so no change was needed there. ## New exports (`web/src/swarm.ts`) - `fetchNativeReport(input:{requestId:string,jobId:string,body:string,resultHash:string,uri:string,deliveredAtMs:number}, signal?:AbortSignal): Promise<{f:Fetched,complete:boolean,issues:string[]}>` - `declaredOutputs(body:string): DeclaredOutput[]` reads `outputs` from a JSON body. An entry can be a string or `{name|path,mediaType}`. `report.md` and `report.json` are always added. A body that isn't JSON gets the defaults. - `matchOutput(out, files)` matches an output to a snapshot file by the base name of its path or name and by a compatible media type. It returns null when there is no match or more than one. - The types `DeclaredOutput` and `NativeSnapshot`, and the constants `NATIVE_SNAPSHOT_MAX` (1 MB), `NATIVE_ARTIFACT_MAX` (8 MB) and `NATIVE_DEFAULT_OUTPUTS`. ## How it works 1. It downloads the completion URI through the existing `GATEWAYS` (for `ipfs://`) or a single `https://` URL. Each source gets one retry on 5xx or 429. Reads are size-limited as they stream. It aborts through `signal`. A final redirect to a non-HTTPS address is refused. A network or CORS failure is recorded as an issue. 2. It computes Keccak-256 over the **original bytes** and compares it with `resultHash`. It never re-serializes the JSON and never compares against `/jobs//result`. If the hash doesn't match, it returns `hashOk:false` and parses nothing. 3. It parses the snapshot and requires `jobId === input.jobId`, `complete === true` and `state === "completed"`. 4. For each declared output it matches one snapshot file, which must have a SHA-256, a whole-number byte count and an https or ipfs URL. It downloads at most the declared size plus 1 byte and requires both the length and the SHA-256 to match. Only then is the text decoded (strict UTF-8). It never builds sibling URLs. Files that fail end up in `f.missing`, and in `f.artifacts` with `ok:false/null` and an error. 5. `report.json` goes through `parseBundle`, then `validateBundle(b, deliveredAtMs, {purchased, maxEvents, requireComplete:true})`. That checks the schema, probabilities, intervals, EV, verdicts, coverage of every purchased event, and timing against the confirmed delivery time. It also checks that the bundle's `requestId` matches, and adds citation checks: each source needs an http(s) URL and a fetch time that parses. 6. The output is a `Fetched` that `ReportView` can read. `json` is produced by the existing `legacyFromV3`, so `json.v3` keeps the full bundle: probabilities, intervals (the uncertainty), arguments, risks and sources. `text` is report.md, and `bodies` holds every verified file, including translations. 7. `complete` is true only when every declared output verified, the confirmed time is valid, and there are no validation or citation errors. 8. Historical Markdown-only delivery: the text is shown, and `complete` is false with the issue "missing structured output: no verified report.json". Predictions are recovered from the hashed Markdown only through the existing `embedded`/`fromSummaryTable` path, labelled `jsonSource:"embedded"`. No report.json is created. A matching hash only proves these are the delivered bytes. It does not verify the facts in the report. ## Checks run (in `web/`) - `npm ci` installed with exit 0. - `npx tsc --noEmit` printed no errors. - `node --test tests/native-report.test.mjs` passed 8 of 8: - a successful v3 bundle; - the raw snapshot hash against changed live metadata and against a whitespace-only reserialization; - wrong job, complete:false, and a state that isn't "completed"; - a tampered report.json hash and a report.md with the wrong length; - incomplete event coverage, invalid probabilities, missing citations, a `javascript:` citation, and false pre-event timing; - the Markdown-only legacy snapshot built from the fixture; - a declared translation that is missing, present, or of the wrong media type; - CORS/network failure, a redirect off HTTPS, and an oversized body. - `node --test tests/*.test.mjs` passed 55 of 55, including the existing v11 `fetchReport` regressions. Note that `node --test tests/` fails on Node 24 because it treats the directory as a file; the glob form works. ## Unresolved gaps / checks deferred until merge - The name and shape of `outputs` in the body are my assumption: strings or `{path|name, mediaType}`. The checkout/job-body item in this wave has to write the same field. If it uses a different key, only the defaults (`report.md` and `report.json`) will be required. - No caller of `fetchNativeReport` exists yet, so the page and order tracking can't use it. Whoever wires it in has to pass the confirmed `deliveredAtMs` and the original completion URI and hash. - Not done: a live fetch from api.imd.fun inside the tests, any deployment, and any paid end-to-end test. - I didn't test real browser CORS behaviour. It's simulated by a thrown `TypeError`.