@reticlehq/engine 3.7.0 → 3.8.1

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.
Files changed (42) hide show
  1. package/dist/disagreement/contradiction-evidence.d.ts +70 -3
  2. package/dist/disagreement/contradiction-evidence.js +156 -7
  3. package/dist/disagreement/contradiction-types.d.ts +8 -0
  4. package/dist/disagreement/contradictions.js +21 -16
  5. package/dist/disagreement/stale-response.js +54 -15
  6. package/dist/evidence/baseline.js +1 -1
  7. package/dist/evidence/body-capture-remedy.d.ts +1 -15
  8. package/dist/evidence/body-capture-remedy.js +9 -2
  9. package/dist/evidence/step-effect.d.ts +20 -1
  10. package/dist/evidence/step-effect.js +30 -2
  11. package/dist/evidence/uncaptured-bodies.d.ts +18 -1
  12. package/dist/evidence/uncaptured-bodies.js +22 -1
  13. package/dist/evidence/unread-outcome.d.ts +7 -0
  14. package/dist/evidence/unread-outcome.js +22 -0
  15. package/dist/evidence/unsettled.js +25 -0
  16. package/dist/evidence/verified.d.ts +2 -0
  17. package/dist/evidence/verified.js +1 -1
  18. package/dist/exact-count.d.ts +19 -0
  19. package/dist/exact-count.js +25 -0
  20. package/dist/question/predicate/predicate-compare.js +7 -2
  21. package/dist/question/predicate/predicate-console.js +23 -10
  22. package/dist/question/predicate/predicate-element.d.ts +10 -1
  23. package/dist/question/predicate/predicate-element.js +138 -17
  24. package/dist/question/predicate/predicate-eval-kit.d.ts +3 -3
  25. package/dist/question/predicate/predicate-eval-kit.js +3 -7
  26. package/dist/question/predicate/predicate-eval.d.ts +10 -0
  27. package/dist/question/predicate/predicate-eval.js +169 -68
  28. package/dist/question/predicate/predicate-parse.d.ts +1 -1
  29. package/dist/question/predicate/predicate-parse.js +1 -1
  30. package/dist/question/predicate/predicate-request-body.d.ts +13 -0
  31. package/dist/question/predicate/predicate-request-body.js +23 -1
  32. package/dist/question/predicate/predicate-schema.d.ts +1 -1
  33. package/dist/question/predicate/predicate-schema.js +6 -5
  34. package/dist/question/predicate/predicate.js +54 -8
  35. package/dist/question/predicate/split-text-miss.d.ts +1 -0
  36. package/dist/question/predicate/split-text-miss.js +8 -1
  37. package/dist/state-bookkeeping.d.ts +2 -0
  38. package/dist/state-bookkeeping.js +4 -0
  39. package/dist/window/adversary.js +46 -40
  40. package/dist/window/network-detail-merge.d.ts +3 -4
  41. package/dist/window/network-detail-merge.js +43 -13
  42. package/package.json +3 -3
@@ -1,4 +1,4 @@
1
- import { ElementState, PredicateKind, THROTTLED_STARVED_NOTE, isSameDocument, } from '@reticlehq/core';
1
+ import { ElementState, EventType, MUTATING_METHODS, PredicateKind, THROTTLED_STARVED_NOTE, isSameDocument, } from '@reticlehq/core';
2
2
  import { predicateToExpectedLinks } from './predicate-to-links.js';
3
3
  import { isAmbient, ambientKeyOf } from '../../window/ambient.js';
4
4
  import { evalRoute } from './predicate-route.js';
@@ -38,6 +38,25 @@ const WINDOW_ONLY_KINDS = new Set([
38
38
  ]);
39
39
  /** Names the oracle that answered, in the same shape every other `assertion` here uses. */
40
40
  const SUPERSEDED_ASSERTION = 'window.evidence-superseded';
41
+ /**
42
+ * A WRITE the server answered: a mutating NET_REQUEST carrying a status or an `ok`.
43
+ *
44
+ * The answer to a write is a fact about the server, not page state, so it stays true after the
45
+ * page that sent it is replaced (#1529). The answer to a read is not: a GET from the old page says
46
+ * nothing about whether the page now on screen loaded its data, and keeping it would let a stale
47
+ * 200 pass `net GET 200` while the current page's own request failed. A request nobody answered,
48
+ * or a subresource whose answer could not be read, carries neither field and is never kept.
49
+ */
50
+ function isServerAnswer(e) {
51
+ const method = e.data['method'];
52
+ return (EventType.NET_REQUEST === e.type &&
53
+ 'string' === typeof method &&
54
+ MUTATING_METHODS.includes(method.toUpperCase()) &&
55
+ ('number' === typeof e.data['status'] || 'boolean' === typeof e.data['ok']));
56
+ }
57
+ /** Why a `net` miss is unknown when only a replaced page's answer would have matched (#1529). */
58
+ const ANSWERED_BEFORE_NAVIGATION = 'a matching request was answered under the page before the navigation, and this window has no ' +
59
+ '`since` floor to count it in. Pass the cursor from before the action as `since`.';
41
60
  /** The predicate's own event-time floor, if its kind carries one. */
42
61
  function predicateSince(predicate) {
43
62
  return 'since' in predicate && 'number' === typeof predicate.since ? predicate.since : 0;
@@ -226,11 +245,19 @@ diagnose = true, baselines) {
226
245
  // `since` from the act it just performed is scoping the assertion to that action's aftermath. It is
227
246
  // applied here, once, rather than in each eval: the floor means the same thing for every kind that
228
247
  // reads the event stream, and net/console re-applying it is a no-op.
229
- const raw = session.eventsSince(Math.max(since, predicateSince(predicate)));
248
+ const floor = Math.max(since, predicateSince(predicate));
249
+ const raw = session.eventsSince(floor);
250
+ const onScreen = (e) => isSameDocument(e.documentId, session.currentDocumentId);
230
251
  // Scoped ONCE, here, for the same reason the contradiction pass scopes at its own choke point:
231
252
  // reasoning about a dead page's evidence is a defect in every oracle below rather than in whichever
232
253
  // one happened to read it, and this is the single place they all take their window from.
233
- const events = raw.filter((e) => isSameDocument(e.documentId, session.currentDocumentId));
254
+ //
255
+ // One exception (#1529). A `net` predicate inside an explicit `since` window also keeps the server's
256
+ // answers to the page that was just replaced: a write that navigates (delete, then redirect home)
257
+ // is proven by its response, and dropping it read a 204 as "never happened". The floor is what
258
+ // ties the request to the action; without one it could come from any earlier page.
259
+ const keepsAnswers = PredicateKind.NET === predicate.kind && 0 < floor;
260
+ const events = raw.filter((e) => onScreen(e) || (keepsAnswers && isServerAnswer(e)));
234
261
  const superseded = raw.length - events.length;
235
262
  // An empty window was always allowed to mean "it did not happen"; that reading is only unsafe once
236
263
  // supersession is what emptied it. Without this, dropping stale evidence would trade a wrong pass
@@ -254,14 +281,28 @@ diagnose = true, baselines) {
254
281
  case PredicateKind.TEXT: {
255
282
  // `scope` passes straight through: the text predicate has always been an element query with
256
283
  // only `text` filled in, so scoping it needs the field, not a second code path.
257
- const query = textPredicateQuery(predicate);
284
+ const query = textPredicateQuery(predicate, session.sdkVersion);
258
285
  const found = await evalElement(session, query, true === predicate.visible ? ElementState.VISIBLE : undefined, predicate.absent ?? false, diagnose);
259
286
  if (undefined === predicate.satisfies)
260
287
  return found;
261
288
  return withTextProperty(found, predicate.satisfies, JSON.stringify(query), baselines?.get(predicate));
262
289
  }
263
- case PredicateKind.NET:
264
- return evalNet(events, predicate);
290
+ case PredicateKind.NET: {
291
+ const result = evalNet(events, predicate);
292
+ if (result.pass || keepsAnswers || result.inconclusive !== undefined)
293
+ return result;
294
+ // No floor, so a replaced page's answers were left out. If one would have satisfied the
295
+ // predicate, nothing here can say whether it was this action's: unknown, never a plain no.
296
+ const withAnswers = raw.filter((e) => onScreen(e) || isServerAnswer(e));
297
+ if (withAnswers.length === events.length || !evalNet(withAnswers, predicate).pass) {
298
+ return result;
299
+ }
300
+ return {
301
+ ...result,
302
+ failureReason: ANSWERED_BEFORE_NAVIGATION,
303
+ inconclusive: ANSWERED_BEFORE_NAVIGATION,
304
+ };
305
+ }
265
306
  case PredicateKind.ROUTE:
266
307
  // `since` so an unanswered request already in flight before this window is not counted
267
308
  // against the app — see unansweredIn.
@@ -418,7 +459,7 @@ function assertsExactCount(predicate) {
418
459
  predicate.count !== undefined);
419
460
  }
420
461
  /**
421
- * Does this predicate claim that something did NOT happen: an `absent` check, or a `not`?
462
+ * Does this predicate claim that something did NOT happen: an `absent` check, a `count: 0`, or a `not`?
422
463
  *
423
464
  * Such a claim is about the END of a window for the same reason an exact count is, and reads true
424
465
  * at the start of every window by construction. Settled on its first reading, a clean-console check
@@ -431,7 +472,12 @@ function claimsAbsence(predicate) {
431
472
  if (PredicateKind.ALL_OF === predicate.kind || PredicateKind.ANY_OF === predicate.kind) {
432
473
  return predicate.predicates.some(claimsAbsence);
433
474
  }
434
- return 'absent' in predicate && true === predicate.absent;
475
+ if ('absent' in predicate && true === predicate.absent) {
476
+ return true;
477
+ }
478
+ // `count: 0` is absence written as arithmetic. Held only for COUNT_CONFIRM_MS, a request sent
479
+ // 500 ms after the action passed a "no request" check.
480
+ return 'count' in predicate && 0 === predicate.count;
435
481
  }
436
482
  /**
437
483
  * Evaluate now, else wait for it to become true (on each event + a poll) until timeout. `since` is
@@ -19,6 +19,7 @@
19
19
  /** The container descriptor the browser attaches to a missed text query. */
20
20
  interface SplitTextOwner {
21
21
  ref?: string;
22
+ selector?: string;
22
23
  role?: string;
23
24
  name?: string;
24
25
  }
@@ -38,7 +38,14 @@ export function describeSplitTextMiss(owner, searchedText) {
38
38
  if (owner === undefined)
39
39
  return undefined;
40
40
  const where = nameOf(owner);
41
- const scope = owner.ref !== undefined && owner.ref.length > 0 ? owner.ref : undefined;
41
+ // A selector wins over the ref: it can be written into an `until` before the action runs,
42
+ // which is when the recovery has to exist. A ref is minted by a snapshot the agent may not
43
+ // have taken yet (#1147).
44
+ const scope = owner.selector !== undefined && owner.selector.length > 0
45
+ ? owner.selector
46
+ : owner.ref !== undefined && owner.ref.length > 0
47
+ ? owner.ref
48
+ : undefined;
42
49
  const recovery = scope === undefined || searchedText === undefined
43
50
  ? 'scope to that container and match it as a whole'
44
51
  : `retry as ${JSON.stringify({
@@ -23,3 +23,5 @@ export declare const CACHE_BOOKKEEPING_KEYS: ReadonlySet<string>;
23
23
  export declare function isCacheBookkeepingDiff(from: unknown, to: unknown): boolean;
24
24
  /** Whether a state event carries movement that can support state-vs-render. */
25
25
  export declare function isMeaningfulStateChange(event: ReticleEvent): boolean;
26
+ /** True for a signal or store that Reticle sends about itself. It says nothing about the app. */
27
+ export declare const isReticleVoice: (name: string) => boolean;
@@ -68,3 +68,7 @@ export function isMeaningfulStateChange(event) {
68
68
  const after = 'value' in event.data ? event.data['value'] : event.data['new'];
69
69
  return !isCacheBookkeepingDiff(event.data['old'], after);
70
70
  }
71
+ /** The name prefixes of Reticle's own signals and stores (`reticle:busy`, `__reticle…`). */
72
+ const RETICLE_VOICE = /^(?:reticle:|__reticle)/;
73
+ /** True for a signal or store that Reticle sends about itself. It says nothing about the app. */
74
+ export const isReticleVoice = (name) => RETICLE_VOICE.test(name);
@@ -18,57 +18,63 @@
18
18
  * rejection is a good outcome. It sends input that is entirely VALID and entirely unhelpful, which
19
19
  * is what a real minimal client looks like and what no fixture in this repository resembles.
20
20
  */
21
- import { z } from 'zod';
21
+ import * as z from 'zod';
22
22
  import { EVENT_PAYLOAD_SCHEMAS, EventType } from '@reticlehq/core';
23
23
  /** A value that satisfies a schema while carrying as little as the contract allows. */
24
24
  function barestValue(schema) {
25
- const def = schema._def;
26
- switch (def.typeName) {
27
- // An optional field is the whole point: leave it out.
28
- case 'ZodOptional':
29
- case 'ZodNullable':
30
- return undefined;
31
- case 'ZodDefault':
32
- return undefined;
33
- case 'ZodString': {
34
- // A minimum length is part of the contract, not decoration: `note` and `anchor` on a human
35
- // mark both demand one. The barest string that still parses is one character, not none.
36
- const checks = def.checks ?? [];
37
- const min = checks.find((c) => 'min' === c.kind)?.value ?? 0;
38
- return 'x'.repeat(min);
39
- }
40
- case 'ZodNumber': {
41
- const checks = def.checks ?? [];
42
- return checks.find((c) => 'min' === c.kind)?.value ?? 0;
43
- }
44
- case 'ZodBoolean':
45
- return false;
46
- case 'ZodArray':
47
- return [];
48
- case 'ZodLiteral':
49
- return def.value;
50
- case 'ZodEnum':
51
- return schema.options[0];
52
- case 'ZodNativeEnum':
53
- return Object.values(schema._def.values)[0];
54
- case 'ZodUnion':
55
- return barestValue((def.options ?? [])[0]);
56
- case 'ZodRecord':
57
- return {};
58
- case 'ZodObject':
59
- return barestObject(schema);
60
- default:
61
- // Unknown, any, and anything else the contract does not constrain.
62
- return undefined;
25
+ // An optional field is the whole point: leave it out.
26
+ if (schema instanceof z.ZodOptional ||
27
+ schema instanceof z.ZodNullable ||
28
+ schema instanceof z.ZodDefault) {
29
+ return undefined;
30
+ }
31
+ if (schema instanceof z.ZodString) {
32
+ // A minimum length is part of the contract, not decoration: `note` and `anchor` on a human
33
+ // mark both demand one. The barest string that still parses is one character, not none.
34
+ return 'x'.repeat(schema.minLength ?? 0);
35
+ }
36
+ if (schema instanceof z.ZodNumber) {
37
+ // `.int()` reports the smallest safe integer as its minimum; that is no lower bound to honour.
38
+ const min = schema.minValue;
39
+ const value = null !== min && Number.MIN_SAFE_INTEGER < min ? min : 0;
40
+ return schema.safeParse(value).success ? value : value + 1;
41
+ }
42
+ if (schema instanceof z.ZodBoolean) {
43
+ return false;
44
+ }
45
+ if (schema instanceof z.ZodArray) {
46
+ return [];
47
+ }
48
+ if (schema instanceof z.ZodRecord) {
49
+ return {};
63
50
  }
51
+ // A tuple has a fixed length, so the barest one still has every item.
52
+ if (schema instanceof z.ZodTuple) {
53
+ return schema.def.items.map((item) => barestValue(item));
54
+ }
55
+ if (schema instanceof z.ZodLiteral) {
56
+ return schema.value;
57
+ }
58
+ if (schema instanceof z.ZodEnum) {
59
+ return schema.options[0];
60
+ }
61
+ if (schema instanceof z.ZodUnion) {
62
+ return barestValue(schema.options[0]);
63
+ }
64
+ if (schema instanceof z.ZodObject) {
65
+ return barestObject(schema);
66
+ }
67
+ // Unknown, any, and anything else the contract does not constrain.
68
+ return undefined;
64
69
  }
65
70
  /** The smallest object that still parses: required keys only, each at its barest value. */
66
71
  function barestObject(schema) {
67
72
  const out = {};
68
73
  for (const [key, field] of Object.entries(schema.shape)) {
69
74
  const value = barestValue(field);
70
- if (value !== undefined)
75
+ if (value !== undefined) {
71
76
  out[key] = value;
77
+ }
72
78
  }
73
79
  return out;
74
80
  }
@@ -1,8 +1,7 @@
1
1
  import { type ReticleEvent } from '@reticlehq/core';
2
2
  /**
3
- * Fold each NET_DETAIL onto the matching in-page NET_REQUEST (by method+url), enriching it with the
4
- * authoritative headers/resourceType the page-side wrapper couldn't see — WITHOUT clobbering fields the
5
- * in-page event already carries. A NET_DETAIL with no matching request is kept as its own event (a
6
- * response the wrapper missed is signal, never silently dropped). Pure over the event list.
3
+ * Fold wire details by method+url, and SDK response bodies by request id plus method+url.
4
+ * SDK body details finish background capture without adding another request to counts or settle.
5
+ * Keep unmatched details rather than attaching them to another request. Pure over the event list.
7
6
  */
8
7
  export declare function mergeNetworkDetail(events: readonly ReticleEvent[]): ReticleEvent[];
@@ -28,28 +28,55 @@ function keyOf(data) {
28
28
  return `${m} ${urlForMatch(data)}`;
29
29
  }
30
30
  /**
31
- * Fold each NET_DETAIL onto the matching in-page NET_REQUEST (by method+url), enriching it with the
32
- * authoritative headers/resourceType the page-side wrapper couldn't see — WITHOUT clobbering fields the
33
- * in-page event already carries. A NET_DETAIL with no matching request is kept as its own event (a
34
- * response the wrapper missed is signal, never silently dropped). Pure over the event list.
31
+ * Fold wire details by method+url, and SDK response bodies by request id plus method+url.
32
+ * SDK body details finish background capture without adding another request to counts or settle.
33
+ * Keep unmatched details rather than attaching them to another request. Pure over the event list.
35
34
  */
36
35
  export function mergeNetworkDetail(events) {
37
36
  const requestByKey = new Map();
37
+ const requestById = new Map();
38
38
  for (const e of events) {
39
- if (e.type === EventType.NET_REQUEST)
40
- requestByKey.set(keyOf(e.data), e);
39
+ if (e.type !== EventType.NET_REQUEST) {
40
+ continue;
41
+ }
42
+ requestByKey.set(keyOf(e.data), e);
43
+ if ('string' === typeof e.data['id']) {
44
+ requestById.set(e.data['id'], e);
45
+ }
41
46
  }
42
47
  const out = [];
43
48
  const enriched = new Map();
44
49
  for (const e of events) {
45
- if (e.type === EventType.NET_DETAIL) {
46
- const match = requestByKey.get(keyOf(e.data));
47
- if (match === undefined) {
50
+ if (e.type === EventType.NET_DETAIL || e.type === EventType.NET_RESPONSE_BODY) {
51
+ const sdkBody = e.type === EventType.NET_RESPONSE_BODY;
52
+ const responseBody = e.data['responseBody'];
53
+ const id = e.data['id'];
54
+ // SDK body details carry the page's request id. URL alone mixes concurrent streams at one endpoint.
55
+ let match;
56
+ if (sdkBody) {
57
+ if ('string' === typeof id) {
58
+ match = requestById.get(id);
59
+ }
60
+ }
61
+ else {
62
+ match = requestByKey.get(keyOf(e.data));
63
+ }
64
+ if (match === undefined || keyOf(match.data) !== keyOf(e.data)) {
48
65
  out.push(e); // unmatched detail survives on its own
49
66
  continue;
50
67
  }
51
68
  const base = enriched.get(match) ?? match;
52
69
  const data = { ...base.data };
70
+ if (sdkBody && 'string' === typeof responseBody) {
71
+ data['responseBody'] = responseBody;
72
+ if (true === e.data['responseBodyTruncated']) {
73
+ data['responseBodyTruncated'] = true;
74
+ }
75
+ else {
76
+ delete data['responseBodyTruncated'];
77
+ }
78
+ delete data['responseBodyReason'];
79
+ }
53
80
  if (data['headers'] === undefined && e.data['headers'] !== undefined) {
54
81
  data['headers'] = e.data['headers'];
55
82
  }
@@ -71,18 +98,21 @@ export function mergeNetworkDetail(events) {
71
98
  // rather than fills a gap. Carried over when the wire body was cut, deleted when it was not:
72
99
  // a stale `true` from the page capture would caveat a body that is now whole, and a missing
73
100
  // one would let a cut body read as complete.
74
- if (true === e.data['requestBodyTruncated'])
101
+ if (true === e.data['requestBodyTruncated']) {
75
102
  data['requestBodyTruncated'] = true;
76
- else
103
+ }
104
+ else {
77
105
  delete data['requestBodyTruncated'];
106
+ }
78
107
  }
79
108
  enriched.set(match, { ...base, data });
80
109
  continue; // the detail is absorbed into the request
81
110
  }
82
111
  }
83
112
  for (const e of events) {
84
- if (e.type === EventType.NET_DETAIL)
85
- continue; // handled above
113
+ if (e.type === EventType.NET_DETAIL || e.type === EventType.NET_RESPONSE_BODY) {
114
+ continue;
115
+ } // handled above
86
116
  out.push(enriched.get(e) ?? e);
87
117
  }
88
118
  return out;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reticlehq/engine",
3
- "version": "3.7.0",
3
+ "version": "3.8.1",
4
4
  "description": "The rules that decide a verdict: read what happened in a running app and say whether the declared consequence held. No browser, no daemon, no command line - an implementation of the OpenVerification specification you can use on its own.",
5
5
  "keywords": [
6
6
  "verification",
@@ -56,8 +56,8 @@
56
56
  "access": "public"
57
57
  },
58
58
  "dependencies": {
59
- "zod": "^3.24.1",
60
- "@reticlehq/core": "3.7.0",
59
+ "zod": "^4.6.6",
60
+ "@reticlehq/core": "3.8.1",
61
61
  "open-verification": "1.0.0"
62
62
  },
63
63
  "scripts": {