@reticlehq/engine 3.5.0 → 3.7.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 (57) hide show
  1. package/dist/disagreement/body-failures.js +2 -1
  2. package/dist/disagreement/contradiction-evidence.d.ts +73 -1
  3. package/dist/disagreement/contradiction-evidence.js +154 -6
  4. package/dist/disagreement/contradiction-types.d.ts +8 -1
  5. package/dist/disagreement/contradictions.d.ts +17 -1
  6. package/dist/disagreement/contradictions.js +111 -38
  7. package/dist/disagreement/echo-mismatch.js +42 -3
  8. package/dist/disagreement/reconcile.js +15 -0
  9. package/dist/disagreement/stale-response.js +37 -2
  10. package/dist/evidence/accepted-write.d.ts +26 -2
  11. package/dist/evidence/accepted-write.js +35 -18
  12. package/dist/evidence/already-true.d.ts +16 -0
  13. package/dist/evidence/already-true.js +48 -0
  14. package/dist/evidence/baseline.js +2 -6
  15. package/dist/evidence/blind-spots.d.ts +14 -3
  16. package/dist/evidence/blind-spots.js +48 -10
  17. package/dist/evidence/body-capture-remedy.d.ts +10 -0
  18. package/dist/evidence/body-capture-remedy.js +15 -2
  19. package/dist/evidence/instrumentation-gaps.js +3 -1
  20. package/dist/evidence/state-bookkeeping.d.ts +2 -0
  21. package/dist/evidence/state-bookkeeping.js +2 -0
  22. package/dist/evidence/superseded-checks.d.ts +29 -0
  23. package/dist/evidence/superseded-checks.js +30 -0
  24. package/dist/evidence/unsettled.js +4 -2
  25. package/dist/evidence/verified.d.ts +8 -1
  26. package/dist/evidence/verified.js +42 -4
  27. package/dist/question/declared.d.ts +24 -4
  28. package/dist/question/declared.js +26 -5
  29. package/dist/question/predicate/name-near-miss.d.ts +24 -0
  30. package/dist/question/predicate/name-near-miss.js +39 -0
  31. package/dist/question/predicate/net-blind-spots.d.ts +49 -0
  32. package/dist/question/predicate/net-blind-spots.js +153 -0
  33. package/dist/question/predicate/predicate-compare.js +13 -4
  34. package/dist/question/predicate/predicate-element.d.ts +15 -1
  35. package/dist/question/predicate/predicate-element.js +108 -7
  36. package/dist/question/predicate/predicate-eval-kit.d.ts +25 -0
  37. package/dist/question/predicate/predicate-eval-kit.js +31 -0
  38. package/dist/question/predicate/predicate-eval.d.ts +17 -2
  39. package/dist/question/predicate/predicate-eval.js +117 -180
  40. package/dist/question/predicate/predicate-parse.js +153 -13
  41. package/dist/question/predicate/predicate-request-body.d.ts +6 -1
  42. package/dist/question/predicate/predicate-request-body.js +7 -2
  43. package/dist/question/predicate/predicate-route.d.ts +2 -0
  44. package/dist/question/predicate/predicate-route.js +30 -0
  45. package/dist/question/predicate/predicate-schema.d.ts +11 -0
  46. package/dist/question/predicate/predicate-schema.js +110 -24
  47. package/dist/question/predicate/predicate-session.d.ts +10 -0
  48. package/dist/question/predicate/predicate-state.js +7 -0
  49. package/dist/question/predicate/predicate-to-links.js +1 -0
  50. package/dist/question/predicate/predicate.js +76 -25
  51. package/dist/question/predicate/split-text-miss.d.ts +6 -0
  52. package/dist/question/predicate/split-text-miss.js +10 -0
  53. package/dist/question/reaction.d.ts +1 -1
  54. package/dist/question/reaction.js +1 -1
  55. package/dist/state-bookkeeping.d.ts +25 -0
  56. package/dist/state-bookkeeping.js +70 -0
  57. package/package.json +2 -2
@@ -6,11 +6,12 @@
6
6
  * back (`residualQueryChecks`), the absent/present asymmetry, and the near-miss diagnostics that only
7
7
  * ever enrich a failure. Nothing else in the predicate layer talks to `ReticleCommand.MATCH`.
8
8
  */
9
- import { ReticleCommand, } from '@reticlehq/core';
10
- import { describeUnusableElementQuery, residualQueryChecks, satisfiesResiduals, describeResidual, } from './predicate-eval.js';
9
+ import { QueryBy, ReticleCommand, ElementState, } from '@reticlehq/core';
10
+ import { describeAltOnNonImage, describeUnusableElementQuery, residualQueryChecks, withAltProjected, satisfiesResiduals, describeResidual, } from './predicate-eval.js';
11
11
  import { describeTestidMiss } from './testid-near-miss.js';
12
- import { describeSplitTextMiss } from './split-text-miss.js';
12
+ import { describeCaseMiss, describeSplitTextMiss } from './split-text-miss.js';
13
13
  import { satisfiesProperty } from './property.js';
14
+ import { describeNameNearMiss } from './name-near-miss.js';
14
15
  /**
15
16
  * The caveat for a present-testid list that was cut at its cap, or nothing when it was whole.
16
17
  *
@@ -23,6 +24,27 @@ function describePresentTestidsCut(shown, total) {
23
24
  return '';
24
25
  return ` (the present-testid list shows the first ${String(shown)} of ${String(total)} in document order — absence from it proves nothing)`;
25
26
  }
27
+ /**
28
+ * Why an `absent` check failed. `absent` means removed from the DOM, so an element the app hid but
29
+ * kept mounted still fails it, correctly. But "found 1" beside `visible: false` evidence reads as a
30
+ * stuck UI, so when every match is hidden the reason says so and names `state: "hidden"`, the
31
+ * predicate for "no longer shown" (#1360). Only when every match was described: with a truncated
32
+ * list a visible one may be among the rest, and the hint would then be wrong. And only when the
33
+ * check names no state: with `state: "hidden"` the advice would repeat the check itself, and with any
34
+ * other state it would ask the caller to drop the condition they actually wrote.
35
+ */
36
+ function describeAbsentMiss(match, state) {
37
+ const found = `expected element to be absent but found ${String(match.count)}`;
38
+ const allDescribedHidden = undefined === state &&
39
+ match.elements.length > 0 &&
40
+ match.elements.length === match.count &&
41
+ match.elements.every((element) => !element.visible);
42
+ if (!allDescribedHidden)
43
+ return found;
44
+ const which = 1 === match.count ? 'it is' : 'every one is';
45
+ return (`${found}, but ${which} hidden — \`absent\` means removed from the DOM; ` +
46
+ `to assert it isn't shown, use \`state: "${ElementState.HIDDEN}"\``);
47
+ }
26
48
  async function matchOnce(session, query, state) {
27
49
  const res = await session.command(ReticleCommand.MATCH, { query, state });
28
50
  if (!res.ok)
@@ -118,7 +140,7 @@ export async function evalElement(session, query, state, absent, diagnose) {
118
140
  const reason = describeUnusableElementQuery(query, residual.unusable);
119
141
  return { pass: false, failureReason: reason, inconclusive: reason };
120
142
  }
121
- let match = await matchOnce(session, query, state);
143
+ let match = await matchOnce(session, withAltProjected(query, residual.checks), state);
122
144
  const subject = JSON.stringify(query);
123
145
  // A residual narrows the SET; `count` is every match while `elements` is only the described prefix,
124
146
  // so a locator broad enough to be truncated cannot be narrowed honestly. Say so instead of guessing.
@@ -126,6 +148,13 @@ export async function evalElement(session, query, state, absent, diagnose) {
126
148
  const reason = `${String(match.count)} elements matched ${subject} and only ${String(match.elements.length)} were described, so ${residual.checks.map(([f]) => `\`${f}\``).join(', ')} could not be checked against all of them — narrow the locator`;
127
149
  return { pass: false, failureReason: reason, inconclusive: reason };
128
150
  }
151
+ // `alt` is only meaningful on an image, and the role that says so is on the matched descriptor, so
152
+ // this cannot be refused before the round-trip the way an uncheckable field is. It runs AFTER the
153
+ // truncation check: judged on a cut-off prefix, "none of these is an image" would be a guess.
154
+ const altRefusal = describeAltOnNonImage(residual.checks, match.elements);
155
+ if (altRefusal !== undefined) {
156
+ return { pass: false, failureReason: altRefusal, inconclusive: altRefusal };
157
+ }
129
158
  const kept = match.elements.filter((element) => satisfiesResiduals(element, residual.checks));
130
159
  // The locator found something and the dropped fields disagree with it. Reported separately from a
131
160
  // plain miss because the fixes are opposite: the element IS there, its value is not what was claimed.
@@ -161,7 +190,7 @@ export async function evalElement(session, query, state, absent, diagnose) {
161
190
  return match.matched
162
191
  ? {
163
192
  pass: false,
164
- failureReason: `expected element to be absent but found ${String(match.count)}`,
193
+ failureReason: describeAbsentMiss(match, state),
165
194
  observed: `${String(match.count)} element(s) matching ${subject}`,
166
195
  expected: `no element matching ${subject}`,
167
196
  assertion: 'element.absent',
@@ -244,6 +273,22 @@ export async function evalElement(session, query, state, absent, diagnose) {
244
273
  evidence: { nearMiss: roleOnly.elements },
245
274
  };
246
275
  }
276
+ // Nothing on the page has that role at all. The control is often there under ANOTHER role — a
277
+ // contenteditable composer is not a `textbox`, a styled div is not a `button` — and the name is
278
+ // the part the agent got right. Name the controls carrying it, with their real roles, so the
279
+ // retry targets what exists instead of guessing again. A diagnosis only: `pass` stays false.
280
+ const byName = await nearestByName(session, query.name, state);
281
+ if (byName.length > 0) {
282
+ const seen = byName.map((e) => `${'' === e.role ? 'no role' : e.role} "${'' === e.name ? (e.text ?? '') : e.name}" (${e.ref})`);
283
+ return {
284
+ pass: false,
285
+ failureReason: `no '${query.role}' named '${query.name}'; that name is on: ${seen.join(', ')}`,
286
+ observed: `no element has role '${query.role}'; elements named '${query.name}': ${seen.join(', ')}`,
287
+ expected: `a '${query.role}' named '${query.name}'`,
288
+ assertion: 'element.role+name',
289
+ evidence: { nearestByName: byName },
290
+ };
291
+ }
247
292
  }
248
293
  // The testid near-miss: name what IS here, so a typo is one step from fixed rather than a dead
249
294
  // end. reticle_query has always done this; the predicate path had no equivalent. See
@@ -254,7 +299,19 @@ export async function evalElement(session, query, state, absent, diagnose) {
254
299
  // element that never rendered. Naming the container is the difference between a retry and a bug
255
300
  // report against working code. See split-text-miss.ts.
256
301
  const splitText = describeSplitTextMiss(match.hint?.splitText, query.text);
257
- const clause = splitText ?? (alsoHere === undefined || '' === alsoHere ? undefined : alsoHere);
302
+ // Same asymmetry one field over: an exact role+name miss reads like an element that never
303
+ // rendered, while the identical failure through reticle_query lists the labels that role really
304
+ // has. The browser computed them on its way to reporting zero. See name-near-miss.ts.
305
+ // Scoped by role in both spellings, as `query.ts` resolves it: `{ by: 'role', value, name }` carries
306
+ // the role in `value`, and without this the sentence said "the page has" for a list the browser had
307
+ // already limited to that role.
308
+ const nearMissRole = QueryBy.ROLE === query.by ? query.value : query.role;
309
+ const nearMiss = describeNameNearMiss(match.hint?.nameNearMiss, query.name, nearMissRole);
310
+ const caseMiss = describeCaseMiss(match.hint?.caseMiss);
311
+ const clause = caseMiss ??
312
+ splitText ??
313
+ nearMiss ??
314
+ (alsoHere === undefined || '' === alsoHere ? undefined : alsoHere);
258
315
  const suffix = clause === undefined ? '' : ` — ${clause}`;
259
316
  // The evidence list is capped in document order, so a region low on the page is exactly what it
260
317
  // drops. Handed back with no marker it reads as the whole page, and the field report this came
@@ -263,6 +320,13 @@ export async function evalElement(session, query, state, absent, diagnose) {
263
320
  const cut = describePresentTestidsCut(present.length, total);
264
321
  return {
265
322
  pass: false,
323
+ // The page shows the text in another case. Still a failure — see EvalResult.caseOnlyMiss.
324
+ ...(caseMiss === undefined
325
+ ? {}
326
+ : {
327
+ caseOnlyMiss: true,
328
+ wouldHold: `the same check with ignoreCase: true (the page shows "${match.hint?.caseMiss ?? ''}")`,
329
+ }),
266
330
  failureReason: `no element matched ${subject}${state === undefined ? '' : ` in state '${state}'`}${suffix}${cut}`,
267
331
  observed: `no matching element on the page${suffix}`,
268
332
  expected: `an element matching ${subject}${state === undefined ? '' : ` in state '${state}'`}`,
@@ -270,12 +334,14 @@ export async function evalElement(session, query, state, absent, diagnose) {
270
334
  // `splitText` rides on the evidence because it is a POSITIVE observation: the browser found the
271
335
  // string in the rendered page. `annotateThrottledMiss` reads it so a miss on a backgrounded tab
272
336
  // is not blamed on a render that demonstrably happened.
273
- ...(present.length > 0 || splitText !== undefined
337
+ ...(present.length > 0 || splitText !== undefined || caseMiss !== undefined
274
338
  ? {
275
339
  evidence: {
276
340
  ...(present.length > 0 ? { presentTestids: present } : {}),
277
341
  ...(total === undefined || 0 === present.length ? {} : { presentTestidsTotal: total }),
278
342
  ...(splitText === undefined ? {} : { splitText: match.hint?.splitText }),
343
+ // Positive too: the page rendered the text, in another case.
344
+ ...(caseMiss === undefined ? {} : { caseMiss: match.hint?.caseMiss }),
279
345
  },
280
346
  }
281
347
  : {}),
@@ -326,3 +392,38 @@ export function withTextProperty(base, assertion, subject, baseline) {
326
392
  evidence: described,
327
393
  };
328
394
  }
395
+ /** How many same-name controls a role miss names. */
396
+ const MAX_BY_NAME = 3;
397
+ /**
398
+ * Elements carrying an accessible name, whatever their role: by label (aria-label, <label>), then by
399
+ * placeholder. The first locator that finds any wins; at most MAX_BY_NAME are named.
400
+ */
401
+ async function nearestByName(session, name, state) {
402
+ for (const query of [{ label: name }, { placeholder: name }]) {
403
+ const found = await matchOnce(session, query, state);
404
+ if (found.matched)
405
+ return found.elements.slice(0, MAX_BY_NAME);
406
+ }
407
+ return [];
408
+ }
409
+ /**
410
+ * The element query a text predicate is evaluated through — one builder for the verdict and its
411
+ * baseline, so the two cannot disagree about what text was asked for.
412
+ *
413
+ * `matchCase` unless the predicate opts out: a check that matched "Saved Priya" against "Saved
414
+ * priya" could never catch a capitalisation defect. An undefined `contains` must not reach the query
415
+ * as `text: undefined` — that is a locator asking for the empty string.
416
+ */
417
+ export function textPredicateQuery(predicate) {
418
+ return {
419
+ ...(undefined === predicate.contains
420
+ ? {}
421
+ : {
422
+ text: predicate.contains,
423
+ ...(true === predicate.ignoreCase ? {} : { matchCase: true }),
424
+ }),
425
+ ...(undefined === predicate.scope
426
+ ? {}
427
+ : { scope: predicate.scope, ...(true === predicate.self ? { self: true } : {}) }),
428
+ };
429
+ }
@@ -66,6 +66,13 @@ export interface EvalResult {
66
66
  observed?: string;
67
67
  expected?: string;
68
68
  assertion?: string;
69
+ /**
70
+ * The text IS on the page, in another case. `pass` stays false — a capitalisation defect is a
71
+ * defect — and `wouldHold` names the check that would have held, so the retry is one decision
72
+ * (does case matter here?) rather than a hunt.
73
+ */
74
+ caseOnlyMiss?: boolean;
75
+ wouldHold?: string;
69
76
  }
70
77
  export declare function str(value: unknown): string | undefined;
71
78
  export declare function num(value: unknown): number | undefined;
@@ -74,6 +81,24 @@ export declare function num(value: unknown): number | undefined;
74
81
  * `{$gte,$lte,$gt,$lt}` (numbers), `{$contains}` (array membership or substring), `{$length}`.
75
82
  */
76
83
  export declare function matchValue(got: unknown, want: unknown): boolean;
84
+ /**
85
+ * Detects the #1117 pattern: an MCP client stringified a primitive before sending it, so
86
+ * `false` arrived as `"false"`, `42` as `"42"`, `null` as `"null"`. The store holds the real
87
+ * type and the comparison is a type mismatch, not a value mismatch — inconclusive, not `no`.
88
+ */
89
+ export declare function isStringifiedPrimitiveMismatch(got: unknown, want: unknown): boolean;
90
+ /**
91
+ * The inconclusive result for a stringified-primitive mismatch on a state `equals` check.
92
+ * Shared by both `evalStateNamed` and `evalState` so the reason and evidence shape stay in one
93
+ * place.
94
+ */
95
+ export declare function stringifiedPrimitiveResult(storeName: string, path: string, got: unknown, want: unknown, capDepth: (v: unknown, d: number) => unknown): {
96
+ pass: false;
97
+ failureReason: string;
98
+ inconclusive: string;
99
+ assertion: string;
100
+ evidence: Record<string, unknown>;
101
+ };
77
102
  /** One pattern key read out of a payload: its value, or where the walk stopped. */
78
103
  export interface FieldReading {
79
104
  found: boolean;
@@ -66,6 +66,37 @@ export function matchValue(got, want) {
66
66
  }
67
67
  return structurallyEqual(got, want);
68
68
  }
69
+ /**
70
+ * Detects the #1117 pattern: an MCP client stringified a primitive before sending it, so
71
+ * `false` arrived as `"false"`, `42` as `"42"`, `null` as `"null"`. The store holds the real
72
+ * type and the comparison is a type mismatch, not a value mismatch — inconclusive, not `no`.
73
+ */
74
+ export function isStringifiedPrimitiveMismatch(got, want) {
75
+ if ('string' !== typeof want)
76
+ return false;
77
+ if ('boolean' === typeof got || 'number' === typeof got || null === got) {
78
+ return String(got) === want;
79
+ }
80
+ return false;
81
+ }
82
+ /**
83
+ * The inconclusive result for a stringified-primitive mismatch on a state `equals` check.
84
+ * Shared by both `evalStateNamed` and `evalState` so the reason and evidence shape stay in one
85
+ * place.
86
+ */
87
+ export function stringifiedPrimitiveResult(storeName, path, got, want, capDepth) {
88
+ const gotType = null === got ? 'null' : typeof got;
89
+ const reason = `state '${path}' is ${JSON.stringify(got)} (${gotType}), ` +
90
+ `but the expected value ${JSON.stringify(want)} is a string that looks like its stringified form — ` +
91
+ `use the ${gotType} literal ${JSON.stringify(got)} instead`;
92
+ return {
93
+ pass: false,
94
+ failureReason: reason,
95
+ inconclusive: reason,
96
+ assertion: 'state.equals',
97
+ evidence: { store: storeName, path, value: capDepth(got, 1) },
98
+ };
99
+ }
69
100
  /**
70
101
  * Value equality for the leaf comparison, because `===` could never match a literal.
71
102
  *
@@ -19,12 +19,27 @@ export declare function evalAnimation(events: ReticleEvent[], p: Extract<Predica
19
19
  export declare function evalSignal(events: ReticleEvent[], p: Extract<Predicate, {
20
20
  kind: typeof PredicateKind.SIGNAL;
21
21
  }>): EvalResult;
22
+ /**
23
+ * What the caller knows about whose traffic this is.
24
+ *
25
+ * Both members are OPTIONAL and both fail OPEN. `settled` used to answer "has the app finished?" over
26
+ * whatever bytes were in the buffer, so a request that no other settle decision counts could hold it
27
+ * open forever — and an assertion that can never pass is one agents delete from their `until`, which
28
+ * costs them the honesty the predicate exists to provide. The fix is to ask the same two questions
29
+ * the act wait asks; the caller who cannot answer them gets exactly the behaviour this had before.
30
+ */
31
+ export interface SettleScope {
32
+ /** The page under test — what makes a request third-party. Absent: nothing is foreign. */
33
+ appUrl?: string | undefined;
34
+ /** Same-origin endpoints the project declared as background. Absent: nothing is excluded. */
35
+ background?: readonly string[] | undefined;
36
+ }
22
37
  /**
23
38
  * "The page has gone quiet": no network/DOM/animation activity for at least `quietMs`. Needs the
24
39
  * wall-clock `now` (in the buffer's time base) because "no activity in the last N ms" is relative to
25
- * now, not to any buffered event — so `now` is injected (CLAUDE.md rule 7), and the wait loop's
40
+ * now, not to any buffered event — so `now` is injected (AGENTS.md rule 7), and the wait loop's
26
41
  * poll interval is what eventually flips this to pass once activity stops.
27
42
  */
28
43
  export declare function evalSettled(allEvents: ReticleEvent[], p: Extract<Predicate, {
29
44
  kind: typeof PredicateKind.SETTLED;
30
- }>, now: number): EvalResult;
45
+ }>, now: number, scope?: SettleScope): EvalResult;