@reticlehq/engine 3.4.0 → 3.6.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.
Files changed (35) hide show
  1. package/dist/disagreement/body-failures.js +2 -1
  2. package/dist/disagreement/contradiction-evidence.d.ts +7 -0
  3. package/dist/disagreement/contradiction-evidence.js +13 -4
  4. package/dist/disagreement/contradictions.d.ts +5 -1
  5. package/dist/disagreement/contradictions.js +109 -3
  6. package/dist/disagreement/echo-mismatch.js +42 -3
  7. package/dist/disagreement/reconcile.js +15 -0
  8. package/dist/disagreement/stale-response.js +32 -2
  9. package/dist/evidence/accepted-write.d.ts +26 -2
  10. package/dist/evidence/accepted-write.js +35 -18
  11. package/dist/evidence/already-true.d.ts +11 -0
  12. package/dist/evidence/already-true.js +27 -0
  13. package/dist/evidence/body-capture-remedy.d.ts +10 -0
  14. package/dist/evidence/body-capture-remedy.js +15 -2
  15. package/dist/evidence/verified.d.ts +2 -0
  16. package/dist/evidence/verified.js +10 -0
  17. package/dist/question/declared.d.ts +4 -4
  18. package/dist/question/declared.js +6 -4
  19. package/dist/question/predicate/name-near-miss.d.ts +24 -0
  20. package/dist/question/predicate/name-near-miss.js +39 -0
  21. package/dist/question/predicate/predicate-compare.js +13 -4
  22. package/dist/question/predicate/predicate-element.d.ts +1 -1
  23. package/dist/question/predicate/predicate-element.js +33 -3
  24. package/dist/question/predicate/predicate-eval.d.ts +16 -1
  25. package/dist/question/predicate/predicate-eval.js +70 -13
  26. package/dist/question/predicate/predicate-parse.js +119 -13
  27. package/dist/question/predicate/predicate-schema.js +58 -23
  28. package/dist/question/predicate/predicate-session.d.ts +12 -0
  29. package/dist/question/predicate/predicate-to-links.js +1 -0
  30. package/dist/question/predicate/predicate.js +76 -15
  31. package/dist/question/reaction.d.ts +1 -1
  32. package/dist/question/reaction.js +1 -1
  33. package/dist/window/ring-buffer.d.ts +14 -0
  34. package/dist/window/ring-buffer.js +69 -1
  35. package/package.json +3 -3
@@ -23,6 +23,7 @@ export function predicateToExpectedLinks(predicate) {
23
23
  kind: ConsequenceKind.NET,
24
24
  urlContains: predicate.urlContains,
25
25
  ...(predicate.status === undefined ? {} : { status: predicate.status }),
26
+ ...(predicate.method === undefined ? {} : { method: predicate.method }),
26
27
  },
27
28
  ];
28
29
  case PredicateKind.STATE: {
@@ -71,22 +71,36 @@ function annotateThrottledMiss(session, predicate, result) {
71
71
  return result;
72
72
  if (failureRestsOnSeeing(predicate))
73
73
  return result;
74
- if (foundTextSplitAcrossChildren(result))
74
+ if (evidenceProvesPageRendered(result))
75
75
  return result;
76
76
  return { ...result, inconclusive: THROTTLED_STARVED_NOTE };
77
77
  }
78
78
  /**
79
- * Did the miss come with proof that the page rendered the very string it was looking for?
79
+ * Did the miss come with proof that the page rendered?
80
80
  *
81
- * The starved-tab caveat is for a page that may not have painted. A text miss that carries a
82
- * split-text owner is the browser saying the string IS in the rendered page, split across one
83
- * container's children — so the tab ran, and the failure is the locator's. Reported from Next's
84
- * template: the throttle note led a response whose own near-miss named the heading holding the text,
85
- * and the agent was sent to wait out a starvation that had not happened.
81
+ * The starved-tab caveat is for a page that may not have painted. Evidence that the page IS alive
82
+ * means the miss is real, not an artefact of throttling.
83
+ *
84
+ * - `splitText`: the browser found the requested string in the rendered page, split across
85
+ * children. Target-specific — the text IS the thing that was asked for.
86
+ * - `nearMiss` from `element.state`: the target element exists but in the wrong state. The DOM
87
+ * painted the element itself, so starvation is not the explanation.
88
+ *
89
+ * Excluded:
90
+ * - `nearMiss` from `element.role+name`: a role-only match on a partially rendered page does not
91
+ * prove the named target rendered. The same role may come from a static shell.
92
+ * - `presentTestids`: testids can come from a static shell or another part of a partially rendered
93
+ * page while the requested target remains starved.
86
94
  */
87
- function foundTextSplitAcrossChildren(result) {
95
+ function evidenceProvesPageRendered(result) {
88
96
  const evidence = result.evidence;
89
- return 'object' === typeof evidence && null !== evidence && 'splitText' in evidence;
97
+ if ('object' !== typeof evidence || null === evidence)
98
+ return false;
99
+ if ('splitText' in evidence)
100
+ return true;
101
+ if ('nearMiss' in evidence && 'element.state' === result.assertion)
102
+ return true;
103
+ return false;
90
104
  }
91
105
  /**
92
106
  * Has this predicate's failure ALREADY been adjudicated, one level down?
@@ -272,7 +286,12 @@ diagnose = true, baselines) {
272
286
  // heuristic, never causation, so a chat message arriving mid-window must not hold settle open.
273
287
  const counts = session.ambientCounts?.();
274
288
  const settleEvents = counts === undefined ? events : events.filter((e) => !isAmbient(counts, ambientKeyOf(e)));
275
- return evalSettled(settleEvents, predicate, session.elapsed());
289
+ // The same two facts the act settle-wait reads, so `settled` and `act_and_wait`'s own wait stop
290
+ // disagreeing about what is in flight — see `SettleScope`.
291
+ return evalSettled(settleEvents, predicate, session.elapsed(), {
292
+ appUrl: session.url,
293
+ background: session.background,
294
+ });
276
295
  }
277
296
  case PredicateKind.COMPARE:
278
297
  return evalCompare(session, events, predicate, diagnose);
@@ -367,6 +386,23 @@ const MIN_RECHECK_GAP_MS = 25;
367
386
  * assertion that latency, so this trades an unbounded false green for a bounded one and says so.
368
387
  */
369
388
  const COUNT_CONFIRM_MS = 300;
389
+ /**
390
+ * The least time an absence claim keeps watching after it first reads true, before the page's
391
+ * settle is consulted.
392
+ *
393
+ * An absence is not a count: the thing it rules out is usually a reaction the action scheduled, such
394
+ * as a `setTimeout(() => console.error(...), 800)` in a click handler. Held for COUNT_CONFIRM_MS, that
395
+ * step passed and the error landed in nobody's window (#1344). So an absence holds for this floor
396
+ * AND until the page has gone quiet by the `settled` oracle's own rule, whichever ends later, and the
397
+ * caller's budget still ends both.
398
+ *
399
+ * It is a ceiling, not a proof: an error scheduled more than a second after an otherwise silent
400
+ * action still passes. Nothing on the page says a timer is pending, so a longer floor is the only
401
+ * way to see further, and every live absence check would pay it.
402
+ */
403
+ const ABSENCE_HOLD_MS = 1_000;
404
+ /** "Has the page gone quiet", asked of the same window the absence is about. */
405
+ const SETTLED_PROBE = { kind: PredicateKind.SETTLED };
370
406
  /**
371
407
  * Does this predicate assert an exact cardinality anywhere inside it?
372
408
  *
@@ -393,7 +429,7 @@ function assertsExactCount(predicate) {
393
429
  * Such a claim is about the END of a window for the same reason an exact count is, and reads true
394
430
  * at the start of every window by construction. Settled on its first reading, a clean-console check
395
431
  * passed on a page whose `console.error` landed a few milliseconds later, which the bench measured
396
- * once the page ran slower. It holds for the same bounded window as a count; see COUNT_CONFIRM_MS.
432
+ * once the page ran slower. It holds until the page goes quiet; see ABSENCE_HOLD_MS.
397
433
  */
398
434
  function claimsAbsence(predicate) {
399
435
  if (PredicateKind.NOT === predicate.kind)
@@ -415,6 +451,9 @@ baselines) {
415
451
  const reader = withinBudget(session, () => deadline - session.elapsed());
416
452
  return new Promise((resolve) => {
417
453
  let done = false;
454
+ // Released in `finish`, on every exit path. Without it a flood inside the window can evict the
455
+ // event the predicate is armed on, and the verdict blames the app (#668).
456
+ const releaseWindow = session.protectWindow?.(since);
418
457
  // A read that threw never looked at the app: the page did not answer, or went away. That is
419
458
  // "could not tell", not "looked and it was false", so it is inconclusive, which the verdict rule
420
459
  // turns into unknown. As a plain false it was graded assertion_failed: a command timeout on a
@@ -431,8 +470,9 @@ baselines) {
431
470
  /** One-shot re-check timed to when a time-based predicate could first pass. See retryAfterMs. */
432
471
  let hintTimer;
433
472
  // An exact count or an absence keeps watching after it first reads true — see COUNT_CONFIRM_MS
434
- // and claimsAbsence.
435
- const holdsForCount = assertsExactCount(predicate) || claimsAbsence(predicate);
473
+ // and ABSENCE_HOLD_MS.
474
+ const holdsForAbsence = claimsAbsence(predicate);
475
+ const holdsForCount = assertsExactCount(predicate) || holdsForAbsence;
436
476
  let confirming = false;
437
477
  let confirmTimer;
438
478
  /** Report a wait that could not run, and END it — see guardedCheck. */
@@ -451,6 +491,7 @@ baselines) {
451
491
  if (done)
452
492
  return;
453
493
  done = true;
494
+ releaseWindow?.();
454
495
  unsub();
455
496
  unsubDisconnect?.();
456
497
  clearInterval(interval);
@@ -519,13 +560,33 @@ baselines) {
519
560
  if (confirming)
520
561
  return;
521
562
  confirming = true;
522
- confirmTimer = setTimeout(() => {
563
+ const confirm = () => {
523
564
  void evaluatePredicate(reader, predicate, since, true, baselines)
524
565
  .then(finish)
525
566
  .catch((error) => {
526
567
  finish(failed(error));
527
568
  });
528
- }, COUNT_CONFIRM_MS);
569
+ };
570
+ if (!holdsForAbsence) {
571
+ confirmTimer = setTimeout(confirm, COUNT_CONFIRM_MS);
572
+ return;
573
+ }
574
+ // An absence waits out ABSENCE_HOLD_MS, then for the page to go quiet. A settle read that
575
+ // could not answer is not a reason to hold on: the confirming read still decides.
576
+ const holdUntilQuiet = () => {
577
+ void evaluatePredicate(reader, SETTLED_PROBE, since, false)
578
+ .then((settled) => {
579
+ if (done)
580
+ return;
581
+ if (settled.pass) {
582
+ confirm();
583
+ return;
584
+ }
585
+ confirmTimer = setTimeout(holdUntilQuiet, settled.retryAfterMs ?? POLL_INTERVAL_MS);
586
+ })
587
+ .catch(confirm);
588
+ };
589
+ confirmTimer = setTimeout(holdUntilQuiet, ABSENCE_HOLD_MS);
529
590
  })
530
591
  .catch((error) => {
531
592
  finish(failed(error));
@@ -4,7 +4,7 @@ interface ReactionReport {
4
4
  events: ReticleEvent[];
5
5
  summary: ReactionSummary;
6
6
  }
7
- /** Turn a slice of the event buffer into the structured "what the app did" report (plan/05). */
7
+ /** Turn a slice of the event buffer into the structured "what the app did" report. */
8
8
  export declare function buildReactionReport(events: ReticleEvent[], windowMs: number): ReactionReport;
9
9
  /**
10
10
  * Drop the per-event `events` array, keeping the window and counts. The counts already answer "what
@@ -1,5 +1,5 @@
1
1
  import { EventType, } from '@reticlehq/core';
2
- /** Turn a slice of the event buffer into the structured "what the app did" report (plan/05). */
2
+ /** Turn a slice of the event buffer into the structured "what the app did" report. */
3
3
  export function buildReactionReport(events, windowMs) {
4
4
  const summary = {
5
5
  total: events.length,
@@ -24,6 +24,20 @@ export declare class RingBuffer {
24
24
  since(cursor: number): ReticleEvent[];
25
25
  /** Events within the last `windowMs`, relative to `now`. */
26
26
  window(windowMs: number, now: number): ReticleEvent[];
27
+ /**
28
+ * Protect events at or after `cursor` from count-cap eviction until the returned function runs.
29
+ *
30
+ * Call it when a wait arms and call the result when the verdict is graded, in a `finally`: a
31
+ * leaked protection would hold the allowance open for the life of the session. Releasing twice is
32
+ * harmless.
33
+ *
34
+ * This is a bounded promise, not an absolute one. Past {@link ARMED_WINDOW_OVERFLOW} events, or
35
+ * under byte pressure, the window is evicted anyway and the loss is recorded as scarce -- so
36
+ * `lostSince` turns the verdict undecidable instead of returning the false red that pinning
37
+ * exists to prevent. Degrading into "I could not see" is right; degrading into "it did not
38
+ * happen" is the defect.
39
+ */
40
+ protect(cursor: number): () => void;
27
41
  /**
28
42
  * Did this buffer evict scarce evidence that belonged to a window opened at `cursor`?
29
43
  *
@@ -2,6 +2,21 @@ import { CHURN_TYPES, RING_BUFFER_DEFAULTS } from '@reticlehq/core';
2
2
  import { byteSizeOf } from './byte-size.js';
3
3
  /** How far forward to look for a churn event to sacrifice before falling back to plain FIFO. */
4
4
  const CHURN_SCAN_LIMIT = 256;
5
+ /**
6
+ * Extra events the count cap may hold while a verdict window is armed.
7
+ *
8
+ * A predicate's own match being evicted INSIDE the window it is being graded against is the worst
9
+ * shape of false negative: the verdict says the flow is red while the flow was green, and the
10
+ * caller cannot detect it, because `dropped` is a session-wide counter with no way to attribute a
11
+ * loss to this assertion's own `since` (#668).
12
+ *
13
+ * The buffer is time-ordered and eviction takes from the head, so "pin the armed window" is the
14
+ * same thing as "let the buffer run over its count cap rather than eat into that window". The
15
+ * allowance is a constant, so a wait that never ends cannot grow the buffer without bound, and the
16
+ * BYTE cap is deliberately left hard -- bytes are the real memory bound, and a window that floods
17
+ * past it degrades through the existing honest path rather than through this one.
18
+ */
19
+ const ARMED_WINDOW_OVERFLOW = 2000;
5
20
  /**
6
21
  * Bounded, time-aware event store per session. The single data structure that powers
7
22
  * observe/wait_for/assert — it lets us look both backward (recent buffer) and
@@ -34,6 +49,15 @@ export class RingBuffer {
34
49
  * `lostSince` and `ring-buffer-window-loss.test.ts`.
35
50
  */
36
51
  #lastScarceLossT;
52
+ /**
53
+ * Cursors of the verdict windows currently being graded, one entry per armed wait.
54
+ *
55
+ * A multiset rather than a single value: concurrent waits are ordinary (an `act_and_wait` whose
56
+ * `until` is an `allOf`, a background `wait_for`), and the buffer must protect back to the
57
+ * EARLIEST of them. Held as counts so releasing one wait does not unprotect another armed on the
58
+ * same cursor.
59
+ */
60
+ #armedWindows = new Map();
37
61
  constructor(options = {}) {
38
62
  this.#maxEvents = options.maxEvents ?? RING_BUFFER_DEFAULTS.MAX_EVENTS;
39
63
  this.#maxAgeMs = options.maxAgeMs ?? RING_BUFFER_DEFAULTS.MAX_AGE_MS;
@@ -83,7 +107,15 @@ export class RingBuffer {
83
107
  // `dropped` moved. Bytes is a soft cap and per-value serialization already bounds any one event, so
84
108
  // keeping the sole survivor over the budget is the correct trade. The count cap (maxEvents, ~2000)
85
109
  // is a hard bound and stays `> maxEvents`.
86
- while (this.#liveCount() > this.#maxEvents ||
110
+ // While a verdict window is armed, the count cap gets a bounded allowance -- but only while the
111
+ // event about to be dropped actually belongs to that window. A buffer full of events older than
112
+ // every armed cursor is evicted exactly as before, so an idle-but-armed session does not sit on
113
+ // a larger buffer for nothing.
114
+ const floor = this.#armedFloor();
115
+ const countCap = () => floor !== undefined && (this.#events[this.#head]?.t ?? -Infinity) >= floor
116
+ ? this.#maxEvents + ARMED_WINDOW_OVERFLOW
117
+ : this.#maxEvents;
118
+ while (this.#liveCount() > countCap() ||
87
119
  (this.#totalBytes > this.#maxBytes && this.#liveCount() > 1)) {
88
120
  if (CHURN_TYPES.has(this.#events[this.#head]?.type ?? '')) {
89
121
  this.#totalBytes -= this.#eventBytes[this.#head] ?? 0;
@@ -117,6 +149,42 @@ export class RingBuffer {
117
149
  this.#head = 0;
118
150
  }
119
151
  }
152
+ /**
153
+ * Protect events at or after `cursor` from count-cap eviction until the returned function runs.
154
+ *
155
+ * Call it when a wait arms and call the result when the verdict is graded, in a `finally`: a
156
+ * leaked protection would hold the allowance open for the life of the session. Releasing twice is
157
+ * harmless.
158
+ *
159
+ * This is a bounded promise, not an absolute one. Past {@link ARMED_WINDOW_OVERFLOW} events, or
160
+ * under byte pressure, the window is evicted anyway and the loss is recorded as scarce -- so
161
+ * `lostSince` turns the verdict undecidable instead of returning the false red that pinning
162
+ * exists to prevent. Degrading into "I could not see" is right; degrading into "it did not
163
+ * happen" is the defect.
164
+ */
165
+ protect(cursor) {
166
+ this.#armedWindows.set(cursor, (this.#armedWindows.get(cursor) ?? 0) + 1);
167
+ let released = false;
168
+ return () => {
169
+ if (released)
170
+ return;
171
+ released = true;
172
+ const remaining = (this.#armedWindows.get(cursor) ?? 1) - 1;
173
+ if (remaining <= 0)
174
+ this.#armedWindows.delete(cursor);
175
+ else
176
+ this.#armedWindows.set(cursor, remaining);
177
+ };
178
+ }
179
+ /** The earliest armed cursor, or `undefined` when no verdict window is open. */
180
+ #armedFloor() {
181
+ let floor;
182
+ for (const cursor of this.#armedWindows.keys()) {
183
+ if (floor === undefined || cursor < floor)
184
+ floor = cursor;
185
+ }
186
+ return floor;
187
+ }
120
188
  /** Record an eviction at `index` as scarce loss, unless it was the churn floor being sacrificed. */
121
189
  #noteScarceLoss(index) {
122
190
  const victim = this.#events[index];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reticlehq/engine",
3
- "version": "3.4.0",
3
+ "version": "3.6.0",
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",
@@ -57,8 +57,8 @@
57
57
  },
58
58
  "dependencies": {
59
59
  "zod": "^3.24.1",
60
- "open-verification": "1.0.0",
61
- "@reticlehq/core": "3.4.0"
60
+ "@reticlehq/core": "3.6.0",
61
+ "open-verification": "1.0.0"
62
62
  },
63
63
  "scripts": {
64
64
  "build": "tsc -b && node ../scripts/alias-dist.mjs",