@reticlehq/engine 3.1.0 → 3.3.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/dist/disagreement/body-failures.js +32 -12
- package/dist/disagreement/contradiction-evidence.d.ts +1 -1
- package/dist/disagreement/contradiction-evidence.js +3 -3
- package/dist/disagreement/contradiction-types.d.ts +6 -0
- package/dist/disagreement/contradictions.js +101 -50
- package/dist/evidence/already-true.js +9 -2
- package/dist/evidence/baseline.d.ts +21 -0
- package/dist/evidence/baseline.js +129 -0
- package/dist/evidence/body-capture-remedy.js +6 -1
- package/dist/evidence/honesty.d.ts +10 -0
- package/dist/evidence/honesty.js +4 -0
- package/dist/evidence/verified.d.ts +7 -0
- package/dist/evidence/verified.js +35 -1
- package/dist/question/declared.js +2 -1
- package/dist/question/lineage.d.ts +14 -5
- package/dist/question/lineage.js +65 -10
- package/dist/question/predicate/body-key-fragment.d.ts +25 -0
- package/dist/question/predicate/body-key-fragment.js +57 -0
- package/dist/question/predicate/predicate-element.d.ts +3 -2
- package/dist/question/predicate/predicate-element.js +68 -3
- package/dist/question/predicate/predicate-eval-kit.d.ts +21 -0
- package/dist/question/predicate/predicate-eval-kit.js +28 -1
- package/dist/question/predicate/predicate-eval.d.ts +6 -0
- package/dist/question/predicate/predicate-eval.js +89 -26
- package/dist/question/predicate/predicate-precheck.d.ts +7 -0
- package/dist/question/predicate/predicate-precheck.js +81 -0
- package/dist/question/predicate/predicate-request-body.d.ts +1 -1
- package/dist/question/predicate/predicate-request-body.js +10 -19
- package/dist/question/predicate/predicate-route.d.ts +2 -1
- package/dist/question/predicate/predicate-schema.d.ts +12 -91
- package/dist/question/predicate/predicate-schema.js +15 -267
- package/dist/question/predicate/predicate-session.d.ts +15 -1
- package/dist/question/predicate/predicate-session.js +25 -1
- package/dist/question/predicate/predicate-state.d.ts +15 -0
- package/dist/question/predicate/predicate-state.js +317 -0
- package/dist/question/predicate/predicate.d.ts +17 -3
- package/dist/question/predicate/predicate.js +139 -328
- package/dist/question/predicate/property.d.ts +24 -73
- package/dist/question/predicate/property.js +134 -20
- package/dist/window/json-salvage.d.ts +2 -1
- package/dist/window/json-salvage.js +2 -1
- package/package.json +2 -2
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Refuse an unevaluable predicate BEFORE the action fires.
|
|
3
|
+
*
|
|
4
|
+
* `reticle_act_and_wait` dispatches, then evaluates `until`. So a locator the browser cannot use —
|
|
5
|
+
* one naming fields the element resolver drops — was detected only after the click had landed and
|
|
6
|
+
* the page had already moved. The result came back `verified:"unknown", inconclusive`, which reads
|
|
7
|
+
* as "Reticle could not tell what happened".
|
|
8
|
+
*
|
|
9
|
+
* That is not true, and it is the wrong direction of error for a verification tool. Reticle could
|
|
10
|
+
* tell perfectly well; what it could not do was evaluate a predicate the CALLER wrote badly. Charging
|
|
11
|
+
* that to the app teaches the reader to distrust a verdict that was never about the app, and the
|
|
12
|
+
* action has already been spent by the time they learn.
|
|
13
|
+
*
|
|
14
|
+
* The refusal shape already exists elsewhere and is exactly right: "target matched no element.
|
|
15
|
+
* Nothing was acted on and no verdict is possible… This is a miss, not a Reticle defect: there is
|
|
16
|
+
* nothing to report." This is that, applied one step earlier.
|
|
17
|
+
*/
|
|
18
|
+
import { PredicateKind } from '@reticlehq/core';
|
|
1
19
|
import { residualQueryChecks } from './predicate-schema.js';
|
|
2
20
|
/** Composite predicates nest; the unusable one can be at any depth. */
|
|
3
21
|
const NESTED_KEYS = ['predicates', 'predicate'];
|
|
@@ -39,3 +57,66 @@ export function unevaluablePredicateReason(predicate) {
|
|
|
39
57
|
}
|
|
40
58
|
return undefined;
|
|
41
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Predicates that are true the moment they are written, and so cannot be evidence of anything.
|
|
62
|
+
*
|
|
63
|
+
* Only `signal` and `net` can be declared with no specifics: both read the event stream from the
|
|
64
|
+
* act's own cursor, so a bare one cannot be answered by something that had already happened. The
|
|
65
|
+
* kinds checked here are read BEFORE the action as well as after (see `already-true.ts`), precisely
|
|
66
|
+
* so a condition that already held cannot be sold as something the action caused — and a bare one of
|
|
67
|
+
* them always already held. There is always a current route, and there is always some store.
|
|
68
|
+
*
|
|
69
|
+
* The result was not an error but a `no-fault` verdict with reason `already_true`: a verdict-shaped
|
|
70
|
+
* object carrying no proof, which an agent skimming for `ok` reads as a pass. A refusal teaches; a
|
|
71
|
+
* no-fault verdict misleads, and it costs the action as well, because the caller only learns after
|
|
72
|
+
* the click has landed and the page has moved.
|
|
73
|
+
*/
|
|
74
|
+
const DECLARE_INSTEAD = 'Declare something the action CHANGES: a `signal` (by name) or a `net` call (method, urlContains, ' +
|
|
75
|
+
'status) can be declared without knowing the app, a `route` needs a `pathname` or `contains`, and ' +
|
|
76
|
+
'a `state` needs a non-empty `path` or a value to compare. One reticle_observe call gives you the ' +
|
|
77
|
+
"app's real signal names and endpoint paths to name here. Nothing was acted on: this predicate was " +
|
|
78
|
+
'already true before the action, so the only thing it could ever have proved is that the action ' +
|
|
79
|
+
'was not needed.';
|
|
80
|
+
/** A `path` that selects the whole store root, in any spelling a caller writes it. */
|
|
81
|
+
function selectsWholeStore(path) {
|
|
82
|
+
return undefined === path || ('string' === typeof path && 0 === path.trim().length);
|
|
83
|
+
}
|
|
84
|
+
/** The reason THIS leaf is unconditionally true, or undefined when it makes a real claim. */
|
|
85
|
+
function vacuousLeafReason(p) {
|
|
86
|
+
if (PredicateKind.ROUTE === p['kind'] &&
|
|
87
|
+
undefined === p['pathname'] &&
|
|
88
|
+
undefined === p['contains'])
|
|
89
|
+
return ('a bare `route` predicate is true the moment it is written — there is always a current route, ' +
|
|
90
|
+
`so ${JSON.stringify(p)} would have held before the action as well as after it. ${DECLARE_INSTEAD}`);
|
|
91
|
+
if (PredicateKind.STATE === p['kind'] &&
|
|
92
|
+
selectsWholeStore(p['path']) &&
|
|
93
|
+
undefined === p['equals'] &&
|
|
94
|
+
undefined === p['satisfies'])
|
|
95
|
+
return ('a `state` predicate with no path and nothing to compare is true the moment it is written — ' +
|
|
96
|
+
`there is always some store, so ${JSON.stringify(p)} would have held before the action as ` +
|
|
97
|
+
`well as after it. ${DECLARE_INSTEAD}`);
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The reason this predicate proves nothing whatever the app does, or undefined when it makes a claim.
|
|
102
|
+
*
|
|
103
|
+
* Checked at any depth, because a composite inherits it: one unconditionally-true branch of an
|
|
104
|
+
* `anyOf` satisfies the whole thing, so the branch that WOULD have been evidence never has to hold.
|
|
105
|
+
*/
|
|
106
|
+
export function vacuousPredicateReason(predicate) {
|
|
107
|
+
if (!isRecord(predicate))
|
|
108
|
+
return undefined;
|
|
109
|
+
const leaf = vacuousLeafReason(predicate);
|
|
110
|
+
if (leaf !== undefined)
|
|
111
|
+
return leaf;
|
|
112
|
+
for (const key of NESTED_KEYS) {
|
|
113
|
+
const nested = predicate[key];
|
|
114
|
+
const children = Array.isArray(nested) ? nested : [nested];
|
|
115
|
+
for (const child of children) {
|
|
116
|
+
const reason = vacuousPredicateReason(child);
|
|
117
|
+
if (reason !== undefined)
|
|
118
|
+
return reason;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return undefined;
|
|
122
|
+
}
|
|
@@ -16,7 +16,7 @@ interface RequestBodyPredicate {
|
|
|
16
16
|
* Accumulated during the single pass over events rather than recomputed after it, the way the
|
|
17
17
|
* response-side trackers already are.
|
|
18
18
|
*/
|
|
19
|
-
|
|
19
|
+
interface RequestBodyState {
|
|
20
20
|
/** A call matched everything else but carried no recorded request body. */
|
|
21
21
|
unrecorded: boolean;
|
|
22
22
|
/** The request body a clause was checked against and did not match. */
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
* Extracted from `predicate-eval.ts`, which was at the 1000-line cap.
|
|
15
15
|
*/
|
|
16
16
|
import { REDACTED_VALUE } from '@reticlehq/core';
|
|
17
|
-
import { clipBody,
|
|
17
|
+
import { clipBody, describeNetFilter, matchJsonBody, str, } from './predicate-eval-kit.js';
|
|
18
18
|
export function newRequestBodyState() {
|
|
19
19
|
return { unrecorded: false };
|
|
20
20
|
}
|
|
@@ -46,25 +46,16 @@ export function checkRequestBody(data, predicate, state) {
|
|
|
46
46
|
return note();
|
|
47
47
|
if (requestBodyMatches === undefined)
|
|
48
48
|
return true;
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
// `requestBodyContains` for those.
|
|
57
|
-
return note();
|
|
58
|
-
}
|
|
59
|
-
if (null === payload || typeof payload !== 'object' || Array.isArray(payload))
|
|
49
|
+
// `matchJsonBody` is the shared one, and a body that is not a JSON object counts as a mismatch
|
|
50
|
+
// there for the reason this side already had written down: guessing at form encoding would answer
|
|
51
|
+
// a different question than the one asked, and `requestBodyContains` exists for those bodies.
|
|
52
|
+
const verdict = matchJsonBody(sent, requestBodyMatches);
|
|
53
|
+
if ('match' === verdict)
|
|
54
|
+
return true;
|
|
55
|
+
if ('mismatch' === verdict)
|
|
60
56
|
return note();
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (redacted !== undefined) {
|
|
64
|
-
state.redactedField ??= redacted;
|
|
65
|
-
return false;
|
|
66
|
-
}
|
|
67
|
-
return dataMatches(actual, requestBodyMatches) ? true : note();
|
|
57
|
+
state.redactedField ??= verdict.redacted;
|
|
58
|
+
return false;
|
|
68
59
|
}
|
|
69
60
|
/**
|
|
70
61
|
* The verdict for a request-body clause nothing satisfied, or `undefined` to fall through.
|
|
@@ -38,7 +38,7 @@ export declare function routePathOf(pathname: string, hash: string): string;
|
|
|
38
38
|
* is the NAVIGABLE value a `reticle_navigate` can be pointed at. Call sites pick; collapsing
|
|
39
39
|
* them into one string is how a hash-router miss lands as "every page is `/`".
|
|
40
40
|
*/
|
|
41
|
-
|
|
41
|
+
interface RouteParts {
|
|
42
42
|
routePath: string;
|
|
43
43
|
docPath: string;
|
|
44
44
|
hash: string;
|
|
@@ -64,3 +64,4 @@ export declare function evalRoute(events: ReticleEvent[], p: Extract<Predicate,
|
|
|
64
64
|
currentUrl?: string,
|
|
65
65
|
/** Event-time floor for the window, so a request already in flight before it is not counted. */
|
|
66
66
|
since?: number): EvalResult;
|
|
67
|
+
export {};
|
|
@@ -1,93 +1,16 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* What the predicate engine adds to the CONTRACT: the fields a browser locator drops, and the
|
|
3
|
+
* introspection the tool surface builds its descriptions from.
|
|
4
|
+
*
|
|
5
|
+
* The shape itself — `Predicate`, `PredicateSchema`, the aliases and the union — moved to
|
|
6
|
+
* `@reticlehq/core`, because a saved flow now carries one and core has to be able to parse what it
|
|
7
|
+
* reads off disk. Core is the contract; this is the reasoning over it. Both halves are re-exported
|
|
8
|
+
* from here, so every existing importer of this module keeps working unchanged.
|
|
9
|
+
*/
|
|
2
10
|
import { z } from 'zod';
|
|
3
|
-
import { type
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
query: ElementQuery;
|
|
7
|
-
state?: ElementState;
|
|
8
|
-
absent?: boolean;
|
|
9
|
-
} | {
|
|
10
|
-
kind: typeof PredicateKind.TEXT;
|
|
11
|
-
contains: string;
|
|
12
|
-
visible?: boolean;
|
|
13
|
-
absent?: boolean;
|
|
14
|
-
/**
|
|
15
|
-
* Restrict the match to a subtree, as a CSS selector or a ref — the same field, and the same
|
|
16
|
-
* meaning, as `scope` on an element query.
|
|
17
|
-
*
|
|
18
|
-
* Without it the match is page-wide, and a word that appears both in a background tab label
|
|
19
|
-
* and in the dialog that just opened satisfies the predicate BEFORE the action runs, so
|
|
20
|
-
* `act_and_wait` reports `already_true` for an action that did exactly the right thing.
|
|
21
|
-
*/
|
|
22
|
-
scope?: string;
|
|
23
|
-
/** Match the scope root itself and check its combined subtree text. Requires `scope`. */
|
|
24
|
-
self?: boolean;
|
|
25
|
-
} | {
|
|
26
|
-
kind: typeof PredicateKind.NET;
|
|
27
|
-
method?: string;
|
|
28
|
-
urlContains?: string;
|
|
29
|
-
status?: number;
|
|
30
|
-
/** Did the call succeed? The honest field for IPC, which has no status code. */
|
|
31
|
-
ok?: boolean;
|
|
32
|
-
since?: number;
|
|
33
|
-
count?: number;
|
|
34
|
-
/** A substring the RESPONSE body must contain — what the server answered, not what was sent. */
|
|
35
|
-
bodyContains?: string;
|
|
36
|
-
/** A substring the REQUEST body must contain — what the UI sent, not what came back. */
|
|
37
|
-
requestBodyContains?: string;
|
|
38
|
-
/** Shallow JSON match over the REQUEST body, in the style of `signal.dataMatches`. */
|
|
39
|
-
requestBodyMatches?: Record<string, unknown>;
|
|
40
|
-
} | {
|
|
41
|
-
kind: typeof PredicateKind.ROUTE;
|
|
42
|
-
pathname?: string;
|
|
43
|
-
contains?: string;
|
|
44
|
-
since?: number;
|
|
45
|
-
} | {
|
|
46
|
-
kind: typeof PredicateKind.CONSOLE;
|
|
47
|
-
level?: string;
|
|
48
|
-
/**
|
|
49
|
-
* A substring the captured message must contain. With `absent: true` this flips the meaning
|
|
50
|
-
* from "no console entries at all" to "THIS message did not appear" — the assertion people
|
|
51
|
-
* actually write regression checks for (deprecation warnings, no-op handler notices).
|
|
52
|
-
*/
|
|
53
|
-
contains?: string;
|
|
54
|
-
absent?: boolean;
|
|
55
|
-
since?: number;
|
|
56
|
-
} | {
|
|
57
|
-
kind: typeof PredicateKind.ANIMATION;
|
|
58
|
-
name?: string;
|
|
59
|
-
target?: string;
|
|
60
|
-
completed?: boolean;
|
|
61
|
-
since?: number;
|
|
62
|
-
} | {
|
|
63
|
-
kind: typeof PredicateKind.SIGNAL;
|
|
64
|
-
name?: string;
|
|
65
|
-
dataMatches?: Record<string, unknown>;
|
|
66
|
-
/**
|
|
67
|
-
* Exact number of matching signals in the window — the same cardinality assertion `net`
|
|
68
|
-
* carries, on the channel the app itself speaks. Omit for presence (≥1).
|
|
69
|
-
*/
|
|
70
|
-
count?: number;
|
|
71
|
-
since?: number;
|
|
72
|
-
} | {
|
|
73
|
-
kind: typeof PredicateKind.STATE;
|
|
74
|
-
store?: string;
|
|
75
|
-
path: string;
|
|
76
|
-
equals?: unknown;
|
|
77
|
-
satisfies?: PropertyAssertion;
|
|
78
|
-
} | {
|
|
79
|
-
kind: typeof PredicateKind.SETTLED;
|
|
80
|
-
quietMs?: number;
|
|
81
|
-
} | {
|
|
82
|
-
kind: typeof PredicateKind.ALL_OF;
|
|
83
|
-
predicates: Predicate[];
|
|
84
|
-
} | {
|
|
85
|
-
kind: typeof PredicateKind.ANY_OF;
|
|
86
|
-
predicates: Predicate[];
|
|
87
|
-
} | {
|
|
88
|
-
kind: typeof PredicateKind.NOT;
|
|
89
|
-
predicate: Predicate;
|
|
90
|
-
};
|
|
11
|
+
import { type ElementDescriptor, type ElementQuery } from '@reticlehq/core';
|
|
12
|
+
export { PredicateSchema } from '@reticlehq/core';
|
|
13
|
+
export type { Predicate, PropertyAssertion } from '@reticlehq/core';
|
|
91
14
|
interface ResidualQueryChecks {
|
|
92
15
|
/** Dropped fields this side CAN check, as [field, wanted value] pairs. */
|
|
93
16
|
checks: [string, string][];
|
|
@@ -107,7 +30,6 @@ export declare function residualQueryChecks(query: ElementQuery): ResidualQueryC
|
|
|
107
30
|
export declare function satisfiesResiduals(element: ElementDescriptor, checks: readonly [string, string][]): boolean;
|
|
108
31
|
/** How the element's own reading of a dropped field should be REPORTED back on a failure. */
|
|
109
32
|
export declare function describeResidual(element: ElementDescriptor, field: string): string;
|
|
110
|
-
export declare const PredicateSchema: z.ZodType<Predicate>;
|
|
111
33
|
/**
|
|
112
34
|
* The fields a given predicate kind accepts, read off the schema itself.
|
|
113
35
|
*
|
|
@@ -167,4 +89,3 @@ export declare function nestedKeysOf(schema: z.ZodTypeAny | undefined): readonly
|
|
|
167
89
|
* list is worse than none, because the agent trusts it and retries into the same wall.
|
|
168
90
|
*/
|
|
169
91
|
export declare function predicateNestedFieldsFor(kind: string): Readonly<Record<string, readonly string[]>>;
|
|
170
|
-
export {};
|
|
@@ -1,54 +1,17 @@
|
|
|
1
|
-
import { ElementQuerySchema, ElementState, PredicateKind, QueryBy, } from '@reticlehq/core';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { propertyAssertionSchema } from './property.js';
|
|
4
|
-
/**
|
|
5
|
-
* Spellings an agent plausibly reaches for, mapped to the real field.
|
|
6
|
-
*
|
|
7
|
-
* `route` spells its field `pathname` while `state`, in the same union, spells its `path`. Every one
|
|
8
|
-
* of these was silently DROPPED before, and because these kinds have all-optional fields, dropping
|
|
9
|
-
* the only key supplied left a predicate that asserts nothing and passes on anything.
|
|
10
|
-
*/
|
|
11
|
-
const PREDICATE_ALIASES = {
|
|
12
|
-
// `text`/`value` on a `text` predicate: the kind is called "text", so `text:` is the first thing
|
|
13
|
-
// anyone writes for it, and `value` follows from the element query's own `value` field. Both were
|
|
14
|
-
// hard rejections, and a rejected predicate produces NO verdict at all — the drive ends with
|
|
15
|
-
// nothing rather than with a failure, which is the worst outcome of the three.
|
|
16
|
-
[PredicateKind.TEXT]: { text: 'contains', value: 'contains' },
|
|
17
|
-
// `urlContains`/`url` reported from the field: an agent that had just written
|
|
18
|
-
// `net { urlContains }` applied the same word to `route`, which spells it `contains`, and got
|
|
19
|
-
// `unrecognized_keys` with no list of what would have worked. The parallel it assumed is a fair
|
|
20
|
-
// one — route's `contains` matches the WHOLE route (path + query + fragment), so "the URL
|
|
21
|
-
// contains this" is precisely what it does — and asserting a redirect after login is the single
|
|
22
|
-
// most common thing an agent reaches for here.
|
|
23
|
-
[PredicateKind.ROUTE]: { path: 'pathname', urlContains: 'contains', url: 'contains' },
|
|
24
|
-
[PredicateKind.NET]: { url: 'urlContains' },
|
|
25
|
-
// `textContains` on a `console` predicate: the reporter who hit the missing matcher reached for
|
|
26
|
-
// it first, by analogy with `net { urlContains }` — and that parallel is exactly right, since
|
|
27
|
-
// both mean "this entry must contain this substring". Accepted as an alias for `contains`.
|
|
28
|
-
[PredicateKind.CONSOLE]: { textContains: 'contains' },
|
|
29
|
-
[PredicateKind.SIGNAL]: { data: 'dataMatches' },
|
|
30
|
-
// `of` on a composite: it reads naturally, several assertion libraries spell it that way, and it has
|
|
31
|
-
// no other meaning here. Observed twice in one drive on a real app, and each rejection cost a round
|
|
32
|
-
// trip AND produced no verdict — a composite is what an agent reaches for precisely when it has two
|
|
33
|
-
// things to prove at once, so failing it is expensive at the worst moment.
|
|
34
|
-
[PredicateKind.ALL_OF]: { of: 'predicates' },
|
|
35
|
-
[PredicateKind.ANY_OF]: { of: 'predicates' },
|
|
36
|
-
// `of` on `not` for the same reason it is accepted on the composites above — an agent that learned
|
|
37
|
-
// the spelling one line earlier applies it to the third composite, and `not` was the one that
|
|
38
|
-
// rejected it. The refusal did name `predicate`, but naming a field still costs the round trip
|
|
39
|
-
// that produced no verdict, and the guess is unambiguous: `not` has exactly one child.
|
|
40
|
-
[PredicateKind.NOT]: { of: 'predicate' },
|
|
41
|
-
};
|
|
42
1
|
/**
|
|
43
|
-
*
|
|
2
|
+
* What the predicate engine adds to the CONTRACT: the fields a browser locator drops, and the
|
|
3
|
+
* introspection the tool surface builds its descriptions from.
|
|
44
4
|
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* lifting it is unambiguous, and an explicit `kind` still wins.
|
|
5
|
+
* The shape itself — `Predicate`, `PredicateSchema`, the aliases and the union — moved to
|
|
6
|
+
* `@reticlehq/core`, because a saved flow now carries one and core has to be able to parse what it
|
|
7
|
+
* reads off disk. Core is the contract; this is the reasoning over it. Both halves are re-exported
|
|
8
|
+
* from here, so every existing importer of this module keeps working unchanged.
|
|
50
9
|
*/
|
|
51
|
-
|
|
10
|
+
import { z } from 'zod';
|
|
11
|
+
import { PredicateKind, PredicateSchema, QueryBy, predicateShapeFor, } from '@reticlehq/core';
|
|
12
|
+
// One public surface, two packages. A caller asking this module for the shape is asking the right
|
|
13
|
+
// module; it simply no longer OWNS it.
|
|
14
|
+
export { PredicateSchema } from '@reticlehq/core';
|
|
52
15
|
/**
|
|
53
16
|
* Element-query fields an agent writes FLAT on an `element` predicate instead of nested under
|
|
54
17
|
* `query`. `reticle_query` takes exactly these at the top level, so an agent that has just located
|
|
@@ -164,215 +127,6 @@ export function describeResidual(element, field) {
|
|
|
164
127
|
: element.name;
|
|
165
128
|
return `${element.role} "${element.name}" ${field}=${JSON.stringify(reading)}`;
|
|
166
129
|
}
|
|
167
|
-
/** `type` read as the discriminator when — and only when — `kind` is absent. */
|
|
168
|
-
function renameKindSpelling(obj) {
|
|
169
|
-
if (obj['kind'] !== undefined || 'string' !== typeof obj[KIND_SPELLING])
|
|
170
|
-
return obj;
|
|
171
|
-
const out = { ...obj, kind: obj[KIND_SPELLING] };
|
|
172
|
-
delete out[KIND_SPELLING];
|
|
173
|
-
return out;
|
|
174
|
-
}
|
|
175
|
-
/** Rename known aliases before parse; an explicit canonical key always wins. */
|
|
176
|
-
function applyPredicateAliases(input) {
|
|
177
|
-
if (typeof input !== 'object' || null === input || Array.isArray(input))
|
|
178
|
-
return input;
|
|
179
|
-
const given = input;
|
|
180
|
-
const obj = renameKindSpelling(given);
|
|
181
|
-
const kind = 'string' === typeof obj['kind'] ? obj['kind'] : '';
|
|
182
|
-
const aliases = PREDICATE_ALIASES[kind];
|
|
183
|
-
let out = obj;
|
|
184
|
-
if (aliases !== undefined) {
|
|
185
|
-
out = { ...obj };
|
|
186
|
-
for (const [from, to] of Object.entries(aliases)) {
|
|
187
|
-
if (out[from] === undefined)
|
|
188
|
-
continue;
|
|
189
|
-
if (out[to] === undefined)
|
|
190
|
-
out[to] = out[from];
|
|
191
|
-
delete out[from];
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
return PredicateKind.ELEMENT === kind ? liftElementQuery(out) : out;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Fold flat query fields into `query`. An explicit `query` wins outright — a caller that supplied
|
|
198
|
-
* both told us which one it meant, and merging the two would invent a locator neither side wrote.
|
|
199
|
-
*/
|
|
200
|
-
function liftElementQuery(obj) {
|
|
201
|
-
const loose = ELEMENT_QUERY_FIELDS.filter((field) => obj[field] !== undefined);
|
|
202
|
-
if (0 === loose.length)
|
|
203
|
-
return obj;
|
|
204
|
-
const out = { ...obj };
|
|
205
|
-
const query = {};
|
|
206
|
-
for (const field of loose) {
|
|
207
|
-
query[field] = out[field];
|
|
208
|
-
delete out[field];
|
|
209
|
-
}
|
|
210
|
-
if (out['query'] === undefined)
|
|
211
|
-
out['query'] = query;
|
|
212
|
-
return out;
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Strict on every branch. A key that is nobody's spelling is now a schema error naming it, instead of
|
|
216
|
-
* a stripped field and a green — see predicate-strict.test.ts for the MCP session that found this.
|
|
217
|
-
*
|
|
218
|
-
* Built on demand rather than at module scope: the `allOf`/`anyOf`/`not` branches reference
|
|
219
|
-
* `PredicateSchema` itself, which does not exist yet while this module is initialising. Calling it
|
|
220
|
-
* after init is what makes the union introspectable — see `predicateFieldsFor`.
|
|
221
|
-
*/
|
|
222
|
-
function predicateUnion() {
|
|
223
|
-
return z.discriminatedUnion('kind', [
|
|
224
|
-
z
|
|
225
|
-
.object({
|
|
226
|
-
kind: z.literal(PredicateKind.ELEMENT),
|
|
227
|
-
query: ElementQuerySchema,
|
|
228
|
-
state: z.nativeEnum(ElementState).optional(),
|
|
229
|
-
absent: z.boolean().optional(),
|
|
230
|
-
})
|
|
231
|
-
.strict(),
|
|
232
|
-
z
|
|
233
|
-
.object({
|
|
234
|
-
kind: z.literal(PredicateKind.TEXT),
|
|
235
|
-
contains: z.string(),
|
|
236
|
-
visible: z.boolean().optional(),
|
|
237
|
-
absent: z.boolean().optional(),
|
|
238
|
-
scope: z.string().optional(),
|
|
239
|
-
self: z.boolean().optional(),
|
|
240
|
-
})
|
|
241
|
-
.strict(),
|
|
242
|
-
z
|
|
243
|
-
.object({
|
|
244
|
-
kind: z.literal(PredicateKind.NET),
|
|
245
|
-
method: z.string().optional(),
|
|
246
|
-
urlContains: z.string().optional(),
|
|
247
|
-
status: z.number().optional(),
|
|
248
|
-
ok: z.boolean().optional(),
|
|
249
|
-
since: z.number().optional(),
|
|
250
|
-
count: z.number().int().nonnegative().optional(),
|
|
251
|
-
/**
|
|
252
|
-
* A substring the call's RESPONSE body must contain - what the server answered.
|
|
253
|
-
*
|
|
254
|
-
* The only channel that can catch a UI echoing its own input instead of the server's answer.
|
|
255
|
-
* Reported from a real payments UI: a refund posted `{"amount":"1187.01"}`, the server read it
|
|
256
|
-
* as paise and answered 200 with `{"refunded":11.87}`, and the page displayed the number the
|
|
257
|
-
* user had typed. Request fired, exactly once, status 200, console clean, page settled — every
|
|
258
|
-
* assertable channel green on a hundred-fold wrong refund, so the verdict was `yes`.
|
|
259
|
-
*
|
|
260
|
-
* A substring rather than a JSON path, deliberately: `"refunded":11.87` is the whole assertion
|
|
261
|
-
* for the money case, it needs no schema for the body, and it works the same on JSON, form
|
|
262
|
-
* encoding and plain text. A path-and-equals form can be added later if a real case needs one;
|
|
263
|
-
* this is the shape that turns "a blob I read" into a verdict.
|
|
264
|
-
*
|
|
265
|
-
* Requires body capture (`reticle({ captureNetworkBodies: true })`), and says so when the body
|
|
266
|
-
* was never recorded rather than reporting an ordinary mismatch.
|
|
267
|
-
*/
|
|
268
|
-
bodyContains: z.string().min(1).optional(),
|
|
269
|
-
/**
|
|
270
|
-
* A substring the REQUEST body must contain — the other half of `bodyContains`.
|
|
271
|
-
*
|
|
272
|
-
* `bodyContains` deliberately searches only the response, and the comment above says why:
|
|
273
|
-
* searching the request too would let it pass on the very defect it exists to catch. That
|
|
274
|
-
* reasoning is sound and it leaves a real gap. For a filter, a search or a form, the thing
|
|
275
|
-
* under test IS the outgoing payload: "applying this filter actually sends it" had no
|
|
276
|
-
* verdict at all, only `reticle_network { bodies: true }` and a human reading `requestBody`
|
|
277
|
-
* by eye (#798).
|
|
278
|
-
*
|
|
279
|
-
* A separate field rather than a mode on `bodyContains`, so neither assertion can ever be
|
|
280
|
-
* satisfied by the wrong half of the exchange.
|
|
281
|
-
*
|
|
282
|
-
* Requires body capture, and says so when the request body was never recorded rather than
|
|
283
|
-
* reporting an ordinary mismatch.
|
|
284
|
-
*/
|
|
285
|
-
requestBodyContains: z.string().min(1).optional(),
|
|
286
|
-
/**
|
|
287
|
-
* Shallow JSON match over the REQUEST body, keyed like `signal.dataMatches` and sharing its
|
|
288
|
-
* `matchValue` operators (`*`, `$gte`, `$contains`, …).
|
|
289
|
-
*
|
|
290
|
-
* `{ requestBodyMatches: { filter: "manual_review" } }` is the one-call verdict for "the UI
|
|
291
|
-
* put the selected value into the payload", and it does not care about key order,
|
|
292
|
-
* whitespace or how the client serialised the object — all of which a substring does.
|
|
293
|
-
*
|
|
294
|
-
* A field whose captured value was REDACTED cannot be matched, and the verdict says so
|
|
295
|
-
* instead of reporting a mismatch: a redacted field is unknown, not different.
|
|
296
|
-
*/
|
|
297
|
-
requestBodyMatches: z.record(z.string(), z.unknown()).optional(),
|
|
298
|
-
})
|
|
299
|
-
.strict(),
|
|
300
|
-
z
|
|
301
|
-
.object({
|
|
302
|
-
kind: z.literal(PredicateKind.ROUTE),
|
|
303
|
-
pathname: z.string().optional(),
|
|
304
|
-
contains: z.string().optional(),
|
|
305
|
-
since: z.number().optional(),
|
|
306
|
-
})
|
|
307
|
-
.strict(),
|
|
308
|
-
z
|
|
309
|
-
.object({
|
|
310
|
-
kind: z.literal(PredicateKind.CONSOLE),
|
|
311
|
-
level: z.string().optional(),
|
|
312
|
-
contains: z.string().min(1).optional(),
|
|
313
|
-
absent: z.boolean().optional(),
|
|
314
|
-
since: z.number().optional(),
|
|
315
|
-
})
|
|
316
|
-
.strict(),
|
|
317
|
-
z
|
|
318
|
-
.object({
|
|
319
|
-
kind: z.literal(PredicateKind.ANIMATION),
|
|
320
|
-
name: z.string().optional(),
|
|
321
|
-
target: z.string().optional(),
|
|
322
|
-
completed: z.boolean().optional(),
|
|
323
|
-
since: z.number().optional(),
|
|
324
|
-
})
|
|
325
|
-
.strict(),
|
|
326
|
-
z
|
|
327
|
-
.object({
|
|
328
|
-
kind: z.literal(PredicateKind.SIGNAL),
|
|
329
|
-
name: z.string().optional(),
|
|
330
|
-
dataMatches: z.record(z.unknown()).optional(),
|
|
331
|
-
/**
|
|
332
|
-
* Exact number of matching signals since the action — presence becomes cardinality.
|
|
333
|
-
*
|
|
334
|
-
* The double-fire is the defect no state-only oracle can see: a handler wired twice fires
|
|
335
|
-
* the signal twice, the store ends up in the right shape either way, and a presence check
|
|
336
|
-
* is green on both. The wrong-name variant is the same blind spot from the other side — the
|
|
337
|
-
* intended signal fires once while a mistyped sibling fires alongside it, and only a count
|
|
338
|
-
* scoped to the matched name tells the two apart. Omit = presence (≥1); `0` asserts the
|
|
339
|
-
* signal never fired, which is a claim in its own right and NOT the same as omitting it.
|
|
340
|
-
*/
|
|
341
|
-
count: z.number().int().nonnegative().optional(),
|
|
342
|
-
since: z.number().optional(),
|
|
343
|
-
})
|
|
344
|
-
.strict(),
|
|
345
|
-
z
|
|
346
|
-
.object({
|
|
347
|
-
kind: z.literal(PredicateKind.STATE),
|
|
348
|
-
store: z.string().optional(),
|
|
349
|
-
path: z.string(),
|
|
350
|
-
equals: z.unknown().optional(),
|
|
351
|
-
/*
|
|
352
|
-
* Assert a PROPERTY instead of exact bytes, for output that is right differently every run.
|
|
353
|
-
* `equals` cannot express a generated summary, a classification or a computed total; this
|
|
354
|
-
* can, and stays decidable here with no model and no network. Supplying both is allowed and
|
|
355
|
-
* both must hold.
|
|
356
|
-
*/
|
|
357
|
-
satisfies: propertyAssertionSchema.optional(),
|
|
358
|
-
})
|
|
359
|
-
.strict(),
|
|
360
|
-
z
|
|
361
|
-
.object({
|
|
362
|
-
kind: z.literal(PredicateKind.SETTLED),
|
|
363
|
-
quietMs: z.number().positive().optional(),
|
|
364
|
-
})
|
|
365
|
-
.strict(),
|
|
366
|
-
z
|
|
367
|
-
.object({ kind: z.literal(PredicateKind.ALL_OF), predicates: z.array(PredicateSchema) })
|
|
368
|
-
.strict(),
|
|
369
|
-
z
|
|
370
|
-
.object({ kind: z.literal(PredicateKind.ANY_OF), predicates: z.array(PredicateSchema) })
|
|
371
|
-
.strict(),
|
|
372
|
-
z.object({ kind: z.literal(PredicateKind.NOT), predicate: PredicateSchema }).strict(),
|
|
373
|
-
]);
|
|
374
|
-
}
|
|
375
|
-
export const PredicateSchema = z.lazy(() => z.preprocess(applyPredicateAliases, predicateUnion()));
|
|
376
130
|
/**
|
|
377
131
|
* The fields a given predicate kind accepts, read off the schema itself.
|
|
378
132
|
*
|
|
@@ -390,17 +144,11 @@ export function predicateFieldsFor(kind) {
|
|
|
390
144
|
* The union option for `kind`, or null when the kind is not one.
|
|
391
145
|
*
|
|
392
146
|
* One place that walks the union, so the field list and the nested lookup cannot disagree about
|
|
393
|
-
* which option a kind resolves to
|
|
394
|
-
* copy
|
|
147
|
+
* which option a kind resolves to. The walk itself is now core's, because the union is: a second
|
|
148
|
+
* copy here would be a second answer to "what fields does this kind take", and the rejection
|
|
149
|
+
* messages built from it would eventually name a field set the schema no longer has.
|
|
395
150
|
*/
|
|
396
|
-
|
|
397
|
-
for (const option of predicateUnion().options) {
|
|
398
|
-
const literal = option.shape['kind'];
|
|
399
|
-
if (literal instanceof z.ZodLiteral && literal.value === kind)
|
|
400
|
-
return option.shape;
|
|
401
|
-
}
|
|
402
|
-
return null;
|
|
403
|
-
}
|
|
151
|
+
const shapeForKind = predicateShapeFor;
|
|
404
152
|
/**
|
|
405
153
|
* Is this tool parameter ACTUALLY the predicate union?
|
|
406
154
|
*
|
|
@@ -10,7 +10,8 @@ import type { AmbientCounts } from '../../window/ambient.js';
|
|
|
10
10
|
*/
|
|
11
11
|
/** The subset of Session the predicate engine needs — keeps it testable with a fake. */
|
|
12
12
|
export interface PredicateSession {
|
|
13
|
-
|
|
13
|
+
/** `timeoutMs` bounds the page's answer; omitted, the session's own default applies. */
|
|
14
|
+
command(name: string, args?: Record<string, unknown>, timeoutMs?: number): Promise<CommandResult>;
|
|
14
15
|
eventsSince(cursor: number): ReticleEvent[];
|
|
15
16
|
onEvent(listener: (event: ReticleEvent) => void): () => void;
|
|
16
17
|
/** Milliseconds since connect — the same clock that stamps event `t` (injected, testable). */
|
|
@@ -70,3 +71,16 @@ export interface PredicateSession {
|
|
|
70
71
|
*/
|
|
71
72
|
keepCallerContext?: KeepCallerContextFn;
|
|
72
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* The least time a page read is given, even at the very end of a budget: the final read at the
|
|
76
|
+
* deadline must still be able to hear a live page answer.
|
|
77
|
+
*/
|
|
78
|
+
export declare const MIN_READ_MS = 1500;
|
|
79
|
+
/**
|
|
80
|
+
* `session`, with every page command bounded by what the caller's budget has left.
|
|
81
|
+
*
|
|
82
|
+
* Without it each read carried the session's fixed command timeout, so a `timeout_ms: 3000` assert on
|
|
83
|
+
* a page that never answered came back after eight seconds. A Proxy rather than a copy because the
|
|
84
|
+
* real Session keeps `#private` fields: every other member is forwarded bound to the original.
|
|
85
|
+
*/
|
|
86
|
+
export declare function withinBudget(session: PredicateSession, remainingMs: () => number): PredicateSession;
|
|
@@ -1 +1,25 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* The least time a page read is given, even at the very end of a budget: the final read at the
|
|
3
|
+
* deadline must still be able to hear a live page answer.
|
|
4
|
+
*/
|
|
5
|
+
export const MIN_READ_MS = 1_500;
|
|
6
|
+
/**
|
|
7
|
+
* `session`, with every page command bounded by what the caller's budget has left.
|
|
8
|
+
*
|
|
9
|
+
* Without it each read carried the session's fixed command timeout, so a `timeout_ms: 3000` assert on
|
|
10
|
+
* a page that never answered came back after eight seconds. A Proxy rather than a copy because the
|
|
11
|
+
* real Session keeps `#private` fields: every other member is forwarded bound to the original.
|
|
12
|
+
*/
|
|
13
|
+
export function withinBudget(session, remainingMs) {
|
|
14
|
+
return new Proxy(session, {
|
|
15
|
+
get(target, key) {
|
|
16
|
+
if ('command' === key) {
|
|
17
|
+
return (name, args) => target.command(name, args, Math.max(MIN_READ_MS, remainingMs()));
|
|
18
|
+
}
|
|
19
|
+
const value = Reflect.get(target, key, target);
|
|
20
|
+
return 'function' === typeof value
|
|
21
|
+
? value.bind(target)
|
|
22
|
+
: value;
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `state` predicate: everything that reads a registered store.
|
|
3
|
+
*
|
|
4
|
+
* Split out of `predicate.ts` when it crossed the file cap, and it is a cohesive unit on its own
|
|
5
|
+
* terms — one command (`STATE_READ`), one kind, and the whole scoped-vs-whole-store negotiation
|
|
6
|
+
* that exists because an older SDK ignores `path` and answers with the entire store. Nothing else
|
|
7
|
+
* in the predicate layer talks to that command.
|
|
8
|
+
*/
|
|
9
|
+
import { PredicateKind } from '@reticlehq/core';
|
|
10
|
+
import { type EvalResult, type Predicate } from './predicate-eval.js';
|
|
11
|
+
import { type Baseline } from './property.js';
|
|
12
|
+
import type { PredicateSession } from './predicate-session.js';
|
|
13
|
+
export declare function evalState(session: PredicateSession, p: Extract<Predicate, {
|
|
14
|
+
kind: typeof PredicateKind.STATE;
|
|
15
|
+
}>, baseline?: Baseline): Promise<EvalResult>;
|