@reticlehq/engine 3.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +36 -0
- package/dist/disagreement/body-failures.d.ts +4 -0
- package/dist/disagreement/body-failures.js +113 -0
- package/dist/disagreement/contradiction-evidence.d.ts +122 -0
- package/dist/disagreement/contradiction-evidence.js +194 -0
- package/dist/disagreement/contradiction-folds.d.ts +61 -0
- package/dist/disagreement/contradiction-folds.js +71 -0
- package/dist/disagreement/contradiction-types.d.ts +164 -0
- package/dist/disagreement/contradiction-types.js +1 -0
- package/dist/disagreement/contradictions.d.ts +5 -0
- package/dist/disagreement/contradictions.js +613 -0
- package/dist/disagreement/echo-mismatch.d.ts +10 -0
- package/dist/disagreement/echo-mismatch.js +299 -0
- package/dist/disagreement/reconcile.d.ts +39 -0
- package/dist/disagreement/reconcile.js +211 -0
- package/dist/disagreement/stale-response.d.ts +3 -0
- package/dist/disagreement/stale-response.js +186 -0
- package/dist/disagreement/unit-mismatch.d.ts +16 -0
- package/dist/disagreement/unit-mismatch.js +167 -0
- package/dist/disagreement/witness-disagreement.d.ts +42 -0
- package/dist/disagreement/witness-disagreement.js +43 -0
- package/dist/evidence/accepted-write.d.ts +35 -0
- package/dist/evidence/accepted-write.js +54 -0
- package/dist/evidence/already-true.d.ts +16 -0
- package/dist/evidence/already-true.js +89 -0
- package/dist/evidence/blind-spots.d.ts +133 -0
- package/dist/evidence/blind-spots.js +253 -0
- package/dist/evidence/body-capture-remedy.d.ts +42 -0
- package/dist/evidence/body-capture-remedy.js +96 -0
- package/dist/evidence/gap-ledger.d.ts +52 -0
- package/dist/evidence/gap-ledger.js +63 -0
- package/dist/evidence/honesty.d.ts +113 -0
- package/dist/evidence/honesty.js +105 -0
- package/dist/evidence/instrumentation-gaps.d.ts +91 -0
- package/dist/evidence/instrumentation-gaps.js +102 -0
- package/dist/evidence/learned-guards.d.ts +75 -0
- package/dist/evidence/learned-guards.js +78 -0
- package/dist/evidence/observability.d.ts +38 -0
- package/dist/evidence/observability.js +40 -0
- package/dist/evidence/page-teardown.d.ts +15 -0
- package/dist/evidence/page-teardown.js +17 -0
- package/dist/evidence/step-effect.d.ts +41 -0
- package/dist/evidence/step-effect.js +27 -0
- package/dist/evidence/uncaptured-bodies.d.ts +23 -0
- package/dist/evidence/uncaptured-bodies.js +41 -0
- package/dist/evidence/undeclared-change.d.ts +51 -0
- package/dist/evidence/undeclared-change.js +56 -0
- package/dist/evidence/unread-outcome.d.ts +10 -0
- package/dist/evidence/unread-outcome.js +77 -0
- package/dist/evidence/unsettled.d.ts +58 -0
- package/dist/evidence/unsettled.js +127 -0
- package/dist/evidence/verdict-intent-gap.d.ts +30 -0
- package/dist/evidence/verdict-intent-gap.js +35 -0
- package/dist/evidence/verified.d.ts +158 -0
- package/dist/evidence/verified.js +378 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +25 -0
- package/dist/question/declared.d.ts +74 -0
- package/dist/question/declared.js +171 -0
- package/dist/question/lineage.d.ts +52 -0
- package/dist/question/lineage.js +122 -0
- package/dist/question/predicate/net-evidence.d.ts +21 -0
- package/dist/question/predicate/net-evidence.js +81 -0
- package/dist/question/predicate/observed-in-window.d.ts +24 -0
- package/dist/question/predicate/observed-in-window.js +38 -0
- package/dist/question/predicate/predicate-asks.d.ts +28 -0
- package/dist/question/predicate/predicate-asks.js +46 -0
- package/dist/question/predicate/predicate-console.d.ts +13 -0
- package/dist/question/predicate/predicate-console.js +138 -0
- package/dist/question/predicate/predicate-element.d.ts +13 -0
- package/dist/question/predicate/predicate-element.js +214 -0
- package/dist/question/predicate/predicate-eval-kit.d.ts +90 -0
- package/dist/question/predicate/predicate-eval-kit.js +126 -0
- package/dist/question/predicate/predicate-eval.d.ts +24 -0
- package/dist/question/predicate/predicate-eval.js +710 -0
- package/dist/question/predicate/predicate-parse.d.ts +15 -0
- package/dist/question/predicate/predicate-parse.js +173 -0
- package/dist/question/predicate/predicate-precheck.d.ts +8 -0
- package/dist/question/predicate/predicate-precheck.js +41 -0
- package/dist/question/predicate/predicate-request-body.d.ts +50 -0
- package/dist/question/predicate/predicate-request-body.js +120 -0
- package/dist/question/predicate/predicate-route.d.ts +66 -0
- package/dist/question/predicate/predicate-route.js +293 -0
- package/dist/question/predicate/predicate-schema.d.ts +170 -0
- package/dist/question/predicate/predicate-schema.js +509 -0
- package/dist/question/predicate/predicate-session.d.ts +72 -0
- package/dist/question/predicate/predicate-session.js +1 -0
- package/dist/question/predicate/predicate-to-links.d.ts +13 -0
- package/dist/question/predicate/predicate-to-links.js +38 -0
- package/dist/question/predicate/predicate.d.ts +26 -0
- package/dist/question/predicate/predicate.js +785 -0
- package/dist/question/predicate/property.d.ts +102 -0
- package/dist/question/predicate/property.js +135 -0
- package/dist/question/predicate/split-text-miss.d.ts +32 -0
- package/dist/question/predicate/split-text-miss.js +51 -0
- package/dist/question/predicate/testid-near-miss.d.ts +22 -0
- package/dist/question/predicate/testid-near-miss.js +33 -0
- package/dist/question/reaction.d.ts +17 -0
- package/dist/question/reaction.js +67 -0
- package/dist/window/adversary.d.ts +28 -0
- package/dist/window/adversary.js +90 -0
- package/dist/window/ambient.d.ts +63 -0
- package/dist/window/ambient.js +76 -0
- package/dist/window/appearance-budget.d.ts +41 -0
- package/dist/window/appearance-budget.js +47 -0
- package/dist/window/asset-noise.d.ts +42 -0
- package/dist/window/asset-noise.js +35 -0
- package/dist/window/body-relevance.d.ts +54 -0
- package/dist/window/body-relevance.js +78 -0
- package/dist/window/byte-size.d.ts +1 -0
- package/dist/window/byte-size.js +16 -0
- package/dist/window/engine-host.d.ts +35 -0
- package/dist/window/engine-host.js +20 -0
- package/dist/window/event-filters.d.ts +121 -0
- package/dist/window/event-filters.js +215 -0
- package/dist/window/json-salvage.d.ts +33 -0
- package/dist/window/json-salvage.js +96 -0
- package/dist/window/network-detail-merge.d.ts +8 -0
- package/dist/window/network-detail-merge.js +89 -0
- package/dist/window/ring-buffer.d.ts +41 -0
- package/dist/window/ring-buffer.js +152 -0
- package/package.json +71 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { ContradictionKind, EventType, MUTATING_METHODS } from '@reticlehq/core';
|
|
2
|
+
/**
|
|
3
|
+
* A write that returned OK and quietly did not apply part of what it was asked to.
|
|
4
|
+
*
|
|
5
|
+
* Measured on a desktop preferences write: the request asked for `{density:'compact', locale:'fr'}`,
|
|
6
|
+
* the response echoed `{"ok":true,"saved":{"density":"compact","locale":"en"}}`, the UI said
|
|
7
|
+
* "Preferences saved", and every channel agreed. `locale` was silently discarded. Nothing above the
|
|
8
|
+
* payload can see this — the status is 200, the body reports no failure, the UI advanced, the page
|
|
9
|
+
* settled. It is a false green with the evidence already in hand.
|
|
10
|
+
*
|
|
11
|
+
* The shape is ordinary in real backends: a field not in the UPDATE statement, a schema that strips
|
|
12
|
+
* unknown keys, a PATCH that honours a subset, an enum that falls back to a default.
|
|
13
|
+
*
|
|
14
|
+
* ## Why this is narrow on purpose
|
|
15
|
+
*
|
|
16
|
+
* Servers legitimately transform what they echo — trimming, lower-casing, rounding, canonicalising,
|
|
17
|
+
* filling defaults, assigning ids and timestamps. A rule that flagged every echoed field that differs
|
|
18
|
+
* would fire on healthy APIs constantly, and a contradiction kind that cries wolf gets filtered out
|
|
19
|
+
* by the agents reading it, taking the true positives with it. So:
|
|
20
|
+
*
|
|
21
|
+
* - only requests that are WRITES are considered. Reported from the field: a lookup that sends its
|
|
22
|
+
* key in the body (`POST /get-branding` with `{workspace_id}`) and gets a record back was graded
|
|
23
|
+
* as a half-applied write. The gate was on the RESPONSE being a success and never on the REQUEST
|
|
24
|
+
* being a write, so every successful call carrying a body read as a save — and the same key name
|
|
25
|
+
* legitimately means different things on the two sides of a request/response pair;
|
|
26
|
+
* - identity keys (`id`, `*_id`) and create sentinels (`0`, `""`, `null`) are not compared. A
|
|
27
|
+
* create that sends `sub_category_id: 0` and gets back `19314` assigned the row; two id spaces
|
|
28
|
+
* that share a field name were never expected to match. Both used to fire on every healthy POST;
|
|
29
|
+
* - only keys the request actually SENT are considered;
|
|
30
|
+
* - only scalars, since deep structural diffing is where the false positives live;
|
|
31
|
+
* - values are compared NORMALISED (trimmed, case-folded, numbers as numbers), so `FR` vs `fr` and
|
|
32
|
+
* `1` vs `1.0` stay silent while `fr` vs `en` speaks;
|
|
33
|
+
* - if the key appears anywhere in the response carrying the requested value, it is treated as
|
|
34
|
+
* applied — an envelope that echoes both the old and the new value is not a dropped write;
|
|
35
|
+
* - the response must look like a restatement of the request before any key is compared. Reported
|
|
36
|
+
* from the field: a command bus POSTed `{command:'chat.send', ...}` and the server answered with
|
|
37
|
+
* the current viewer snapshot. The message arrived over the socket and rendered. `id` in the
|
|
38
|
+
* snapshot meant the viewer, `id` in the request meant the message, and this kind fired. A
|
|
39
|
+
* snapshot that shares a key name is not an echo. A discriminator the request carries that the
|
|
40
|
+
* response does not, or a fat body that shares one coincidental key, is skipped. The existing
|
|
41
|
+
* single-value / not-echoed-at-all guards still trade false negatives away — this narrows on
|
|
42
|
+
* shape, not on value types.
|
|
43
|
+
*
|
|
44
|
+
* The residue after those filters is a field the caller set, the server echoed back, and the echoed
|
|
45
|
+
* value is a genuinely different value. That is worth one line of an agent's attention.
|
|
46
|
+
*/
|
|
47
|
+
/** Bodies larger than this are skipped — a diff over a huge payload is neither cheap nor legible. */
|
|
48
|
+
const MAX_ECHO_KEYS = 200;
|
|
49
|
+
function isRecord(value) {
|
|
50
|
+
return 'object' === typeof value && value !== null && !Array.isArray(value);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Any JSON value, not just an object. An HTTP write sends an object; an IPC call sends its ARGUMENT
|
|
54
|
+
* LIST, so `savePrefs({locale})` arrives as `[{"locale":"fr"}]`. Requiring a top-level object here
|
|
55
|
+
* silently excluded every desktop write from this check — the exact platform the finding came from.
|
|
56
|
+
*/
|
|
57
|
+
function parse(raw) {
|
|
58
|
+
if (typeof raw !== 'string' || 0 === raw.length)
|
|
59
|
+
return undefined;
|
|
60
|
+
try {
|
|
61
|
+
return JSON.parse(raw);
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/** Comparable form of a scalar. Non-scalars return undefined and are never compared. */
|
|
68
|
+
function normalize(value) {
|
|
69
|
+
if ('string' === typeof value)
|
|
70
|
+
return value.trim().toLowerCase();
|
|
71
|
+
if ('number' === typeof value)
|
|
72
|
+
return Number.isFinite(value) ? String(value) : undefined;
|
|
73
|
+
if ('boolean' === typeof value)
|
|
74
|
+
return String(value);
|
|
75
|
+
return undefined;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Values that mean "server, you decide", not a field the caller asked to persist. A create that
|
|
79
|
+
* sends `0` for the new row's id, or an empty string the backend fills in, is the usual REST shape
|
|
80
|
+
* — not a dropped write.
|
|
81
|
+
*/
|
|
82
|
+
function isSentinelValue(value) {
|
|
83
|
+
return 0 === value || '' === value || null === value;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Keys that name an identity, not a persisted attribute. The server assigns these (create returns a
|
|
87
|
+
* new id; a public id and an internal row id share a field name). Comparing them as echoes is how
|
|
88
|
+
* this kind fired on every healthy POST-create and poisoned later asserts in the same window.
|
|
89
|
+
*/
|
|
90
|
+
function isIdentityKey(key) {
|
|
91
|
+
const lower = key.toLowerCase();
|
|
92
|
+
return 'id' === lower || lower.endsWith('_id');
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Every scalar value the response carries for each key, at any depth.
|
|
96
|
+
*
|
|
97
|
+
* Depth matters because the echo is usually nested — `{ok:true, saved:{...}}`, `{data:{...}}`,
|
|
98
|
+
* `{result:{...}}` — and demanding the same path as the request would miss nearly every real API.
|
|
99
|
+
* Collecting ALL values for a key is what makes the "echoes both old and new" case safe.
|
|
100
|
+
*/
|
|
101
|
+
function scalarsByKey(body, into = new Map(), depth = 0, omitSentinels = false) {
|
|
102
|
+
if (depth > 6 || into.size > MAX_ECHO_KEYS)
|
|
103
|
+
return into;
|
|
104
|
+
if (Array.isArray(body)) {
|
|
105
|
+
for (const item of body)
|
|
106
|
+
scalarsByKey(item, into, depth + 1, omitSentinels);
|
|
107
|
+
return into;
|
|
108
|
+
}
|
|
109
|
+
if (!isRecord(body))
|
|
110
|
+
return into;
|
|
111
|
+
for (const [key, value] of Object.entries(body)) {
|
|
112
|
+
if (omitSentinels && isSentinelValue(value))
|
|
113
|
+
continue;
|
|
114
|
+
const scalar = normalize(value);
|
|
115
|
+
if (scalar === undefined) {
|
|
116
|
+
scalarsByKey(value, into, depth + 1, omitSentinels);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const seen = into.get(key) ?? new Set();
|
|
120
|
+
seen.add(scalar);
|
|
121
|
+
into.set(key, seen);
|
|
122
|
+
}
|
|
123
|
+
return into;
|
|
124
|
+
}
|
|
125
|
+
const OK_MIN = 200;
|
|
126
|
+
const OK_MAX = 300;
|
|
127
|
+
/**
|
|
128
|
+
* Body keys that name the operation rather than a field being persisted. A command bus puts one of
|
|
129
|
+
* these on the request; a snapshot of the current viewer does not echo it. Their absence on the
|
|
130
|
+
* response is the cheapest honest signal that the body is not a restatement of the write.
|
|
131
|
+
*/
|
|
132
|
+
const EchoDiscriminator = {
|
|
133
|
+
COMMAND: 'command',
|
|
134
|
+
ACTION: 'action',
|
|
135
|
+
OP: 'op',
|
|
136
|
+
PROCEDURE: 'procedure',
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* A response carrying this many times more scalar keys than the request, overlapping on fewer than
|
|
140
|
+
* `MIN_SHARED_KEYS`, is a snapshot that happened to reuse a name — not an echo of the write.
|
|
141
|
+
*/
|
|
142
|
+
/**
|
|
143
|
+
* The two halves of an ADDRESSED write: a key that NAMES the field to set, and a generic container
|
|
144
|
+
* holding the value to set it to — `{"slot":"_dim","value":"3D"}`, `{"field":"locale","value":"fr"}`.
|
|
145
|
+
*
|
|
146
|
+
* A container key cannot be compared across documents by name. `value` in the request means "the
|
|
147
|
+
* value of the field named by `slot`"; `value` in the response means "the value of whichever field
|
|
148
|
+
* this node belongs to", and a document holding many addressed fields holds many `value` nodes. The
|
|
149
|
+
* flat by-key comparison below therefore measures the request's value against an unrelated sibling
|
|
150
|
+
* field's value, which is not evidence about the target field at all.
|
|
151
|
+
*
|
|
152
|
+
* That produced a FALSE RED on a passing assertion: the request asked
|
|
153
|
+
* `{"slot":"_dim","value":"3D"}`; the server applied it (`brief.dim: "3D"`, and the interview
|
|
154
|
+
* advanced from `{kind:"question"}` to `{kind:"geometry", payload:{dim:"3D"}}`); and this kind
|
|
155
|
+
* reported "asked 3d, got cfd", where `cfd` is `brief.slots._family.value` — a different slot,
|
|
156
|
+
* set earlier in the same interview. The next call on the same endpoint DID mirror into
|
|
157
|
+
* `slots.<slot>.value` and passed clean, so the kind misfires exactly when a server persists an
|
|
158
|
+
* addressed field somewhere other than a mirror named after it. That is a normal API shape.
|
|
159
|
+
*
|
|
160
|
+
* Cost of getting this wrong: a manufactured red is worse than a missed one here. Reticle's whole
|
|
161
|
+
* proposition is that an agent must not talk itself past a red; a heuristic that invents them
|
|
162
|
+
* teaches, inside a single session, that a `contradicted` verdict may be noise worth arguing with.
|
|
163
|
+
*
|
|
164
|
+
* The addressing key itself is still compared. If the server echoes a DIFFERENT `slot` than the one
|
|
165
|
+
* requested, that is a genuine dropped write and this file should say so.
|
|
166
|
+
*/
|
|
167
|
+
const ADDRESSING_KEYS = new Set(['slot', 'field', 'key', 'path', 'property']);
|
|
168
|
+
const CONTAINER_KEYS = new Set(['value', 'val', 'newvalue']);
|
|
169
|
+
/** True when the request says "set the field named X" rather than carrying the fields themselves. */
|
|
170
|
+
function isAddressedWrite(asked) {
|
|
171
|
+
let addressed = false;
|
|
172
|
+
let container = false;
|
|
173
|
+
for (const key of asked.keys()) {
|
|
174
|
+
const lower = key.toLowerCase();
|
|
175
|
+
if (ADDRESSING_KEYS.has(lower))
|
|
176
|
+
addressed = true;
|
|
177
|
+
if (CONTAINER_KEYS.has(lower))
|
|
178
|
+
container = true;
|
|
179
|
+
}
|
|
180
|
+
return addressed && container;
|
|
181
|
+
}
|
|
182
|
+
const SNAPSHOT_KEY_RATIO = 3;
|
|
183
|
+
const MIN_SHARED_KEYS = 2;
|
|
184
|
+
/**
|
|
185
|
+
* True when the response looks like a restatement of the request, so overlapping key names can be
|
|
186
|
+
* read as an echo rather than a coincidence.
|
|
187
|
+
*
|
|
188
|
+
* Half the request's comparable keys must appear in the response. That keeps a partial echo
|
|
189
|
+
* (`{density, locale}` answered with only `locale`) in scope, and drops a three-field command whose
|
|
190
|
+
* snapshot shares one name. A request that names an operation the response does not repeat is not
|
|
191
|
+
* an echo at all, regardless of any other overlap.
|
|
192
|
+
*/
|
|
193
|
+
function responseRestatesRequest(asked, echoed) {
|
|
194
|
+
const comparable = [];
|
|
195
|
+
for (const [key, wanted] of asked) {
|
|
196
|
+
if (1 === wanted.size)
|
|
197
|
+
comparable.push(key);
|
|
198
|
+
}
|
|
199
|
+
if (0 === comparable.length)
|
|
200
|
+
return false;
|
|
201
|
+
let present = 0;
|
|
202
|
+
for (const key of comparable) {
|
|
203
|
+
if (echoed.has(key))
|
|
204
|
+
present += 1;
|
|
205
|
+
}
|
|
206
|
+
let requestHasDiscriminator = false;
|
|
207
|
+
let responseHasDiscriminator = false;
|
|
208
|
+
for (const key of Object.values(EchoDiscriminator)) {
|
|
209
|
+
if (asked.has(key))
|
|
210
|
+
requestHasDiscriminator = true;
|
|
211
|
+
if (echoed.has(key))
|
|
212
|
+
responseHasDiscriminator = true;
|
|
213
|
+
}
|
|
214
|
+
if (requestHasDiscriminator && !responseHasDiscriminator)
|
|
215
|
+
return false;
|
|
216
|
+
if (echoed.size > comparable.length * SNAPSHOT_KEY_RATIO && present < MIN_SHARED_KEYS) {
|
|
217
|
+
return false;
|
|
218
|
+
}
|
|
219
|
+
return present * 2 >= comparable.length;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Contradictions for writes whose response echoes a different value than the request asked for.
|
|
223
|
+
*
|
|
224
|
+
* `actionSince` is the attribution floor `findContradictions` already keeps — the event time the
|
|
225
|
+
* action that opened this window was dispatched. A finding about a request that had already settled
|
|
226
|
+
* before the agent acted otherwise reads as a verdict on the change it just made.
|
|
227
|
+
*/
|
|
228
|
+
export function findEchoMismatches(events, actionSince) {
|
|
229
|
+
const found = [];
|
|
230
|
+
for (const event of events) {
|
|
231
|
+
if (event.type !== EventType.NET_REQUEST)
|
|
232
|
+
continue;
|
|
233
|
+
// `MUTATING_METHODS` is the one definition of "is this a write" in the codebase — the same list
|
|
234
|
+
// `contradictions.ts`'s `isMutating` reads. A read has no fields to half-apply.
|
|
235
|
+
//
|
|
236
|
+
// KNOWN LIMIT, stated because it is easy to read this fix as bigger than it is: POST is in that
|
|
237
|
+
// list, so a lookup that POSTs its key and gets a record back — the shape actually reported —
|
|
238
|
+
// still reaches the comparison below. Method is the only signal available here, and a read-shaped
|
|
239
|
+
// POST is genuinely indistinguishable from a write by method alone.
|
|
240
|
+
//
|
|
241
|
+
// The tempting next step is to skip a pair whose JSON types disagree (a string key sent, a number
|
|
242
|
+
// echoed). Deliberately not taken: that also silences `{"qty":"5"}` answered with `3`, which is a
|
|
243
|
+
// real dropped write. A false negative here is a missed bug in someone's app, and that is not a
|
|
244
|
+
// trade this file makes to quieten a false positive.
|
|
245
|
+
const method = 'string' === typeof event.data['method'] ? event.data['method'] : '';
|
|
246
|
+
if (!MUTATING_METHODS.includes(method.toUpperCase()))
|
|
247
|
+
continue;
|
|
248
|
+
// IPC reports `ok` rather than a status; HTTP reports a status. Either must say success — a write
|
|
249
|
+
// that already failed is a different (and louder) finding than one that half-applied.
|
|
250
|
+
const status = event.data['status'];
|
|
251
|
+
const httpOk = 'number' === typeof status && status >= OK_MIN && status < OK_MAX;
|
|
252
|
+
if (!httpOk && event.data['ok'] !== true)
|
|
253
|
+
continue;
|
|
254
|
+
const request = parse(event.data['requestBody']);
|
|
255
|
+
const response = parse(event.data['responseBody']);
|
|
256
|
+
if (request === undefined || response === undefined)
|
|
257
|
+
continue;
|
|
258
|
+
const echoed = scalarsByKey(response);
|
|
259
|
+
const asked = scalarsByKey(request, new Map(), 0, true);
|
|
260
|
+
if (!responseRestatesRequest(asked, echoed))
|
|
261
|
+
continue;
|
|
262
|
+
// See ADDRESSING_KEYS: on an addressed write the container key names nothing on its own, so
|
|
263
|
+
// comparing it by name across the two documents compares two different fields.
|
|
264
|
+
const addressed = isAddressedWrite(asked);
|
|
265
|
+
const dropped = [];
|
|
266
|
+
for (const [key, wanted] of asked) {
|
|
267
|
+
if (addressed && CONTAINER_KEYS.has(key.toLowerCase()))
|
|
268
|
+
continue;
|
|
269
|
+
// More than one requested value for a key (a before/after pair, a list of items) makes "what
|
|
270
|
+
// was asked for" ambiguous, and a guess here is exactly how this kind would earn a reputation
|
|
271
|
+
// for crying wolf.
|
|
272
|
+
if (wanted.size !== 1)
|
|
273
|
+
continue;
|
|
274
|
+
if (isIdentityKey(key))
|
|
275
|
+
continue;
|
|
276
|
+
const values = echoed.get(key);
|
|
277
|
+
// Not echoed at all = no evidence either way. Only a key the server chose to report back can
|
|
278
|
+
// contradict the request, and silence is not a contradiction.
|
|
279
|
+
if (values === undefined || 0 === values.size)
|
|
280
|
+
continue;
|
|
281
|
+
const [want] = [...wanted];
|
|
282
|
+
if (want !== undefined && !values.has(want))
|
|
283
|
+
dropped.push(`${key}: asked ${want}, got ${[...values].join('/')}`);
|
|
284
|
+
}
|
|
285
|
+
if (0 === dropped.length)
|
|
286
|
+
continue;
|
|
287
|
+
const url = 'string' === typeof event.data['url'] ? event.data['url'] : '';
|
|
288
|
+
const attribution = actionSince !== undefined && event.t < actionSince
|
|
289
|
+
? ' — this request completed before the action being verified, so the finding is not about that change'
|
|
290
|
+
: '';
|
|
291
|
+
found.push({
|
|
292
|
+
kind: ContradictionKind.WRITE_FIELD_IGNORED,
|
|
293
|
+
claim: 'the write returned success and the page treated it as saved',
|
|
294
|
+
counter: `its own echo shows ${String(dropped.length)} field(s) that differ from the request — ${dropped.join('; ')}`,
|
|
295
|
+
detail: `${method} ${url} — the server answered OK and returned a different value than it was asked to set${attribution}`,
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
return found;
|
|
299
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compare what the API SAID against what the page SHOWS.
|
|
3
|
+
*
|
|
4
|
+
* Every other check in Reticle reads one side. This reads both, which is the one comparison only
|
|
5
|
+
* something living inside the app can make: the response body and the rendered text are in the same
|
|
6
|
+
* process, and nothing else holds them at the same time. A screenshot has the render and not the
|
|
7
|
+
* data; a network log has the data and not the render; a human reading either one sees a number that
|
|
8
|
+
* looks right.
|
|
9
|
+
*
|
|
10
|
+
* Two findings, both measured on a real payments dashboard:
|
|
11
|
+
*
|
|
12
|
+
* - **the wrong currency.** `GET /payments` returns `{amount: 7997, currency: "USD"}` and the row
|
|
13
|
+
* renders "₹79.97". The number is correct, the unit is not — a dollar amount presented as rupees.
|
|
14
|
+
* Every channel agrees: 200, the digits match the data, the page settles.
|
|
15
|
+
* - **a status that is never shown.** `GET /settlements` returns `status: "on_hold"` with
|
|
16
|
+
* `hold_reason: "kyc_documents_expired"`, and the badge reads "pending". A merchant reads
|
|
17
|
+
* "pending" and waits, while the payout is blocked on expired documents.
|
|
18
|
+
*
|
|
19
|
+
* Pure: response bodies and page text in, findings out. No session, no DOM, no clock.
|
|
20
|
+
*/
|
|
21
|
+
/** A finding: one field of one entity whose rendering disagrees with the data. */
|
|
22
|
+
export interface Mismatch {
|
|
23
|
+
entity: string;
|
|
24
|
+
field: string;
|
|
25
|
+
/** What the API returned. */
|
|
26
|
+
api: string;
|
|
27
|
+
/** What the page shows instead — absent when the value simply never appears. */
|
|
28
|
+
rendered?: string;
|
|
29
|
+
why: string;
|
|
30
|
+
}
|
|
31
|
+
/** Every object carrying an `id`, at any depth. These are the things a UI renders as rows. */
|
|
32
|
+
export declare function entitiesIn(node: unknown, out?: Record<string, unknown>[]): Record<string, unknown>[];
|
|
33
|
+
/**
|
|
34
|
+
* Compare entities against the page's rendered text.
|
|
35
|
+
*
|
|
36
|
+
* `pageText` is the visible text of the page — the snapshot's own tree is exactly this, so no new
|
|
37
|
+
* capability is needed to obtain it.
|
|
38
|
+
*/
|
|
39
|
+
export declare function reconcile(bodies: readonly unknown[], pageText: string): Mismatch[];
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compare what the API SAID against what the page SHOWS.
|
|
3
|
+
*
|
|
4
|
+
* Every other check in Reticle reads one side. This reads both, which is the one comparison only
|
|
5
|
+
* something living inside the app can make: the response body and the rendered text are in the same
|
|
6
|
+
* process, and nothing else holds them at the same time. A screenshot has the render and not the
|
|
7
|
+
* data; a network log has the data and not the render; a human reading either one sees a number that
|
|
8
|
+
* looks right.
|
|
9
|
+
*
|
|
10
|
+
* Two findings, both measured on a real payments dashboard:
|
|
11
|
+
*
|
|
12
|
+
* - **the wrong currency.** `GET /payments` returns `{amount: 7997, currency: "USD"}` and the row
|
|
13
|
+
* renders "₹79.97". The number is correct, the unit is not — a dollar amount presented as rupees.
|
|
14
|
+
* Every channel agrees: 200, the digits match the data, the page settles.
|
|
15
|
+
* - **a status that is never shown.** `GET /settlements` returns `status: "on_hold"` with
|
|
16
|
+
* `hold_reason: "kyc_documents_expired"`, and the badge reads "pending". A merchant reads
|
|
17
|
+
* "pending" and waits, while the payout is blocked on expired documents.
|
|
18
|
+
*
|
|
19
|
+
* Pure: response bodies and page text in, findings out. No session, no DOM, no clock.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Currency markers, symbol and code. Deliberately a closed list: currency is one of the few things an
|
|
23
|
+
* app renders that has a fixed, publicly agreed vocabulary, which is what makes this checkable
|
|
24
|
+
* without knowing anything about the app.
|
|
25
|
+
*/
|
|
26
|
+
const CURRENCY_MARKS = {
|
|
27
|
+
INR: ['₹', 'INR', 'Rs.', 'Rs '],
|
|
28
|
+
USD: ['$', 'USD', 'US$'],
|
|
29
|
+
EUR: ['€', 'EUR'],
|
|
30
|
+
GBP: ['£', 'GBP'],
|
|
31
|
+
JPY: ['¥', 'JPY'],
|
|
32
|
+
AED: ['AED', 'د.إ'],
|
|
33
|
+
SGD: ['SGD', 'S$'],
|
|
34
|
+
AUD: ['AUD', 'A$'],
|
|
35
|
+
};
|
|
36
|
+
/** Currencies whose minor unit is not 1/100. Rendering these needs a different divisor. */
|
|
37
|
+
const ZERO_DECIMAL = new Set(['JPY', 'KRW', 'VND']);
|
|
38
|
+
/** Fields that name a state — the ones a badge renders and a user acts on. */
|
|
39
|
+
const STATE_FIELDS = new Set(['status', 'state']);
|
|
40
|
+
const ITEM_KEYS = ['items', 'results', 'data', 'records', 'rows', 'entries'];
|
|
41
|
+
function isRecord(value) {
|
|
42
|
+
return 'object' === typeof value && value !== null && !Array.isArray(value);
|
|
43
|
+
}
|
|
44
|
+
/** Every object carrying an `id`, at any depth. These are the things a UI renders as rows. */
|
|
45
|
+
export function entitiesIn(node, out = []) {
|
|
46
|
+
if (Array.isArray(node)) {
|
|
47
|
+
for (const item of node)
|
|
48
|
+
entitiesIn(item, out);
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
if (!isRecord(node))
|
|
52
|
+
return out;
|
|
53
|
+
if (node['id'] !== undefined)
|
|
54
|
+
out.push(node);
|
|
55
|
+
for (const key of ITEM_KEYS) {
|
|
56
|
+
if (node[key] !== undefined)
|
|
57
|
+
entitiesIn(node[key], out);
|
|
58
|
+
}
|
|
59
|
+
return out;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Every way a UI might print a minor-unit integer as a major-unit amount.
|
|
63
|
+
*
|
|
64
|
+
* Real interfaces group digits, and they do not agree on how: "44,573.44" in most of the world,
|
|
65
|
+
* "44.573,44" across much of Europe, and "1,22,464.06" under the Indian lakh convention. Matching only
|
|
66
|
+
* the ungrouped form finds nothing on any app that formats its money, which is all of them — measured
|
|
67
|
+
* on a logistics console whose cells read "₹44,573.44" while the check searched for "44573.44".
|
|
68
|
+
*/
|
|
69
|
+
function majorForms(minor, currency) {
|
|
70
|
+
if (ZERO_DECIMAL.has(currency))
|
|
71
|
+
return [String(minor), group(String(minor)), indian(String(minor))];
|
|
72
|
+
const plain = (minor / 100).toFixed(2);
|
|
73
|
+
const [whole = '', fraction = ''] = plain.split('.');
|
|
74
|
+
return [
|
|
75
|
+
plain,
|
|
76
|
+
`${group(whole)}.${fraction}`,
|
|
77
|
+
`${indian(whole)}.${fraction}`,
|
|
78
|
+
// European: dots group, comma is the decimal separator.
|
|
79
|
+
`${group(whole).replace(/,/g, '.')},${fraction}`,
|
|
80
|
+
];
|
|
81
|
+
}
|
|
82
|
+
/** Western grouping: every three digits from the right. */
|
|
83
|
+
function group(whole) {
|
|
84
|
+
return whole.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
85
|
+
}
|
|
86
|
+
/** Indian grouping: the last three digits, then pairs — 1,22,464. */
|
|
87
|
+
function indian(whole) {
|
|
88
|
+
if (whole.length <= 3)
|
|
89
|
+
return whole;
|
|
90
|
+
const last = whole.slice(-3);
|
|
91
|
+
const rest = whole.slice(0, -3);
|
|
92
|
+
return `${rest.replace(/\B(?=(\d{2})+(?!\d))/g, ',')},${last}`;
|
|
93
|
+
}
|
|
94
|
+
/** Does the text show this amount marked as some OTHER currency? Returns that marker. */
|
|
95
|
+
function wrongCurrencyMark(text, amounts, expected) {
|
|
96
|
+
for (const amount of amounts) {
|
|
97
|
+
const mark = markBefore(text, amount, expected);
|
|
98
|
+
if (mark !== undefined)
|
|
99
|
+
return mark;
|
|
100
|
+
}
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
function markBefore(text, amount, expected) {
|
|
104
|
+
const at = text.indexOf(amount);
|
|
105
|
+
if (-1 === at)
|
|
106
|
+
return undefined; // this rendering is not on screen — try the next
|
|
107
|
+
// Look just before the number, where a symbol or code sits. A window, not the whole page: the
|
|
108
|
+
// point is what is attached to THIS amount.
|
|
109
|
+
const before = text.slice(Math.max(0, at - 12), at);
|
|
110
|
+
const expectedMarks = CURRENCY_MARKS[expected] ?? [expected];
|
|
111
|
+
if (expectedMarks.some((mark) => before.includes(mark)))
|
|
112
|
+
return undefined;
|
|
113
|
+
for (const [code, marks] of Object.entries(CURRENCY_MARKS)) {
|
|
114
|
+
if (code === expected)
|
|
115
|
+
continue;
|
|
116
|
+
for (const mark of marks) {
|
|
117
|
+
if (before.includes(mark))
|
|
118
|
+
return mark;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
123
|
+
/** Every form a state value might legitimately be rendered as: `on_hold`, `on hold`, `On Hold`. */
|
|
124
|
+
function renderedForms(value) {
|
|
125
|
+
const spaced = value.replace(/[_-]+/g, ' ');
|
|
126
|
+
return [value, spaced, spaced.replace(/\s+/g, '')];
|
|
127
|
+
}
|
|
128
|
+
function shows(text, value) {
|
|
129
|
+
const haystack = text.toLowerCase();
|
|
130
|
+
return renderedForms(value).some((form) => haystack.includes(form.toLowerCase()));
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Compare entities against the page's rendered text.
|
|
134
|
+
*
|
|
135
|
+
* `pageText` is the visible text of the page — the snapshot's own tree is exactly this, so no new
|
|
136
|
+
* capability is needed to obtain it.
|
|
137
|
+
*/
|
|
138
|
+
export function reconcile(bodies, pageText) {
|
|
139
|
+
const entities = bodies.flatMap((body) => entitiesIn(body));
|
|
140
|
+
const found = [];
|
|
141
|
+
const seen = new Set();
|
|
142
|
+
// Every value each state field took across the response set — used to tell "this value is simply
|
|
143
|
+
// not rendered anywhere" (uninteresting) from "a DIFFERENT value of this same field is rendered"
|
|
144
|
+
// (a mapping bug). Without that distinction the check would flag any field a UI chooses not to show.
|
|
145
|
+
const vocabulary = new Map();
|
|
146
|
+
for (const entity of entities) {
|
|
147
|
+
for (const [field, value] of Object.entries(entity)) {
|
|
148
|
+
if (!STATE_FIELDS.has(field) || typeof value !== 'string')
|
|
149
|
+
continue;
|
|
150
|
+
const values = vocabulary.get(field) ?? new Set();
|
|
151
|
+
values.add(value);
|
|
152
|
+
vocabulary.set(field, values);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
for (const entity of entities) {
|
|
156
|
+
// An id is a string or a number in every API worth reconciling; anything else is not an identity
|
|
157
|
+
// this can report against, so the record is skipped rather than stringified into nonsense.
|
|
158
|
+
const raw = entity['id'];
|
|
159
|
+
if (typeof raw !== 'string' && typeof raw !== 'number')
|
|
160
|
+
continue;
|
|
161
|
+
const id = String(raw);
|
|
162
|
+
// Any numeric field on an entity that declares a currency is money in THAT currency. Checking a
|
|
163
|
+
// fixed field name instead would only ever work on APIs that happen to call it `amount` — the
|
|
164
|
+
// three real ones measured here use `amount`, `declaredValueMinor` and `total`.
|
|
165
|
+
//
|
|
166
|
+
// The check is still tight, because a value only counts when the page renders it AS money: the
|
|
167
|
+
// number must appear with a currency marker attached. A non-money number that happens to share a
|
|
168
|
+
// decimal form (a weight, a count) is rendered bare and never reaches the comparison.
|
|
169
|
+
const currency = entity['currency'];
|
|
170
|
+
if ('string' === typeof currency) {
|
|
171
|
+
for (const [field, value] of Object.entries(entity)) {
|
|
172
|
+
if (typeof value !== 'number' || 'currency' === field)
|
|
173
|
+
continue;
|
|
174
|
+
const printed = majorForms(value, currency);
|
|
175
|
+
const wrong = wrongCurrencyMark(pageText, printed, currency);
|
|
176
|
+
const key = `${id}:currency`;
|
|
177
|
+
if (wrong === undefined || seen.has(key))
|
|
178
|
+
continue;
|
|
179
|
+
seen.add(key);
|
|
180
|
+
found.push({
|
|
181
|
+
entity: id,
|
|
182
|
+
field: 'currency',
|
|
183
|
+
api: currency,
|
|
184
|
+
rendered: wrong,
|
|
185
|
+
why: `the API reports ${currency} ${String(value)} (minor units, field "${field}") and the page shows it marked "${wrong}" — the number is right and the currency is not, so the amount reads as a different one entirely`,
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
for (const [field, value] of Object.entries(entity)) {
|
|
190
|
+
if (!STATE_FIELDS.has(field) || typeof value !== 'string')
|
|
191
|
+
continue;
|
|
192
|
+
if (shows(pageText, value))
|
|
193
|
+
continue;
|
|
194
|
+
// Only interesting if the page renders SOME value of this field — otherwise the UI just does
|
|
195
|
+
// not display it, which is a design choice and not a defect.
|
|
196
|
+
const others = [...(vocabulary.get(field) ?? [])].filter((v) => v !== value);
|
|
197
|
+
const shown = others.filter((v) => shows(pageText, v));
|
|
198
|
+
const key = `${id}:${field}`;
|
|
199
|
+
if (0 === shown.length || seen.has(key))
|
|
200
|
+
continue;
|
|
201
|
+
seen.add(key);
|
|
202
|
+
found.push({
|
|
203
|
+
entity: id,
|
|
204
|
+
field,
|
|
205
|
+
api: value,
|
|
206
|
+
why: `the API reports ${field}="${value}" for ${id}, and no rendering of that value appears on the page — while other values of the same field are shown (${shown.join(', ')}), so this record is being displayed as something it is not`,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return found;
|
|
211
|
+
}
|