|
| 1 | +# Compare an environment with the application it came from |
| 2 | + |
| 3 | +Issue: https://github.com/morluto/rea/issues/4933 |
| 4 | + |
| 5 | +## Outcome |
| 6 | + |
| 7 | +An environment is only as useful as it is faithful: an agent trained or graded on it should meet the behaviour of the |
| 8 | +application it was captured from. REA checks an environment only against its own capture, so nothing says how far it |
| 9 | +drifts from the application on calls the capture did not make, after writes, or on errors. |
| 10 | + |
| 11 | +`rea environment-compare` measures that. It takes an environment (a manifest and its capture Evidence) and one or more |
| 12 | +**probe captures**, each a recording of one call sequence made against the live application, and reports how many of the |
| 13 | +probe's calls the environment answers as the application did, which tools disagree and how. |
| 14 | + |
| 15 | +The command only reads Evidence: it makes no network call. Whoever records the probes drives the application with its own |
| 16 | +credentials and resets it between sequences. |
| 17 | + |
| 18 | +## Requirements |
| 19 | + |
| 20 | +- Inputs: the manifest Evidence, its capture Evidence (`--evidence`), and one or more probe capture Evidence files |
| 21 | + (`--probe`, repeatable), each `inspect-web-network-capture` or `capture-browser-scenario` Evidence of one sequence. |
| 22 | +- Each probe is one sequence run against a freshly reset application. Before each probe the environment's state is reset, |
| 23 | + so sequences are independent on both sides. |
| 24 | +- Requests are taken in the probe's order (start time, then ordinal; ordinal alone when any record lacks a start |
| 25 | + time, as state extraction orders them). A request is matched to the manifest tool of the |
| 26 | + same method whose path template it fits with the most literal segments; format-rule parameters must fit their format |
| 27 | + once percent-decoded, as replay reads them. |
| 28 | + Query values the tool takes and a JSON body become the tool's arguments. A path argument is the raw segment: the |
| 29 | + environment decodes it once, so a segment encoded twice is not read as a second decoding. Replay matches that raw |
| 30 | + argument against a recorded segment that equals it, equals its one decoding, or decodes once to it. |
| 31 | +- A record without a usable request line (a redacted URL, a transaction the capture cannot attribute, an unparseable |
| 32 | + URL or no method) is not replayed, nor is a request to another origin than the probe's application (`other-origin`), |
| 33 | + which is the origin with the most JSON-like records, as the manifest chooses its own. A request with no recorded |
| 34 | + answer is made, since it may have changed the application, but not compared. Each is listed per probe with its |
| 35 | + reason and left out of agreement. |
| 36 | +- A request no tool matches is reported `no-tool`; a matched tool the environment does not offer, such as a write it |
| 37 | + withholds, is reported `not-served`. Neither is answered. |
| 38 | +- Ids differ between a re-seeded application and the capture. The comparison maps them: |
| 39 | + - seeded entities pair across the two answers by a natural key: a field present on every element of both arrays whose |
| 40 | + string values are unique on each side and shared between them; |
| 41 | + - identifier fields (`id`, `uuid`, `guid`, and names ending in `_id`, `Id`, `ID`, `_uuid` or `Uuid`) of paired objects, |
| 42 | + and of the two answers to the same call, map the live value to the environment's value; a create's two answers map |
| 43 | + the new ids, including a response that is only that id at the write plan's identity location. The `id`, `uuid` or |
| 44 | + `guid` of the answer's own entities (at the read view's entity or list location, a write's response location, or |
| 45 | + else the root) also maps within the collection the call names, since two collections can reuse a live id for |
| 46 | + different entities. An equal pair is kept in that collection, so another collection's mapping of the same live id |
| 47 | + does not replace it. A collection's map is a bijection: the first pairing of either side wins. Outside identifier |
| 48 | + fields, a string maps when the environment's state holds it as an id, and the keys of two objects keyed by ids map |
| 49 | + when their values pair (keys that partly overlap only when they are digits, UUIDs or their entries' identities, and |
| 50 | + every value pairs); |
| 51 | + - ids are learned only from an answer the environment gave this call, from its state or from a recording of exactly |
| 52 | + this call, its whole request line and its request body: a recording of another call that replay answers with |
| 53 | + instead holds other entities. A tool that takes a body does not match a recording whose body the probe did not send; |
| 54 | + - path, query and body values equal to a mapped live id are translated before the environment answers, a path |
| 55 | + parameter by the collection it names, except query values that page a list (`limit`, `offset`, `page` and the |
| 56 | + like), which count items rather than name one. A literal route segment is not translated. A nested collection is |
| 57 | + the live path's concrete parent values, each parameter decoded once, so two parents do not share one child map and |
| 58 | + spellings replay treats as equal name one. A second encoding stays a different parent. Learning and translation use |
| 59 | + that same live path. |
| 60 | + A path value the map does not know, and the environment's state does not hold, is reported `unmapped-id`. |
| 61 | +- A call is checked against the served tool's input schema first, as the MCP server checks it; a call it refuses is |
| 62 | + reported `refused`. A browser probe keeps its request bodies; one captured without response bodies is refused, since |
| 63 | + only statuses would compare, and so is one that makes POST, PUT or PATCH calls without recorded request bodies, since |
| 64 | + those calls would be made empty. |
| 65 | +- Answers compare after normalisation: empty response text equals no body (a JSON empty string stays one); mapped ids |
| 66 | + compare equal, in scalar sets as well, an entity's own identity within the call's collection and any other id by the |
| 67 | + probe's mapping, and an entity's own identity both sides hold verbatim differs when its collection's mapping gives it |
| 68 | + another environment value (a reference keeps raw equality, as the probe's mapping cannot tell which collection it |
| 69 | + names); two clock values (ISO-8601 strings, or epoch numbers under |
| 70 | + time-named fields) compare by format only; two different generated values (both UUIDs, or both of one length and the |
| 71 | + same character classes with a digit) that neither side maps compare equal and are counted. A body over the replay |
| 72 | + limit, text or binary, compares by status only, as does a live body the probe did not record, unless the answer has |
| 73 | + none by definition (204, 205, 304, or an answer to `HEAD`). Both are counted. |
| 74 | +- A step agrees when the statuses are equal and the normalised bodies are. Differences are categorised: `status`, |
| 75 | + `missing-field` (the application had it), `extra-field`, `value`, `length`, `order`, `clock-format`, `refused` (the |
| 76 | + environment refused the call), `not-served`, `no-tool` and `unmapped-id`, each with a JSON pointer and short values. |
| 77 | +- The report gives agreement overall, per probe and per tool, agreement ignoring item order beside it, the counts per |
| 78 | + category, the ids mapped and the values normalised, the records left uncompared, and each step's differences (at most |
| 79 | + 20 per step). |
| 80 | + |
| 81 | +## Non-goals |
| 82 | + |
| 83 | +- No network access, no credentials, no reset of the application: probes are recorded outside REA. |
| 84 | +- No generation of call sequences; a later change generates them where the environment disagrees. |
| 85 | +- No change to how environments serve. |
0 commit comments