@reticlehq/core 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.
Files changed (82) hide show
  1. package/dist/artifacts/flow-composition.d.ts +3 -2
  2. package/dist/artifacts/flow-constants.d.ts +59 -2
  3. package/dist/artifacts/flow-constants.js +63 -3
  4. package/dist/artifacts/flow-expect-flat.d.ts +260 -0
  5. package/dist/artifacts/flow-expect-flat.js +284 -0
  6. package/dist/artifacts/flow-mutation-target.d.ts +0 -1
  7. package/dist/artifacts/flow-mutation-target.js +26 -2
  8. package/dist/artifacts/flow-step-tool.d.ts +2 -0
  9. package/dist/artifacts/flow-types.d.ts +1071 -1225
  10. package/dist/artifacts/flow-types.js +96 -140
  11. package/dist/artifacts/impact.d.ts +200 -20
  12. package/dist/artifacts/impact.js +72 -0
  13. package/dist/artifacts/journal.d.ts +85 -6
  14. package/dist/artifacts/journal.js +51 -1
  15. package/dist/hooks/hook-events.d.ts +18 -18
  16. package/dist/hud-entry.d.ts +9 -0
  17. package/dist/hud-entry.js +9 -0
  18. package/dist/identity/brand.d.ts +46 -0
  19. package/dist/identity/brand.js +7 -0
  20. package/dist/identity/source-constants.d.ts +11 -0
  21. package/dist/identity/source-constants.js +11 -0
  22. package/dist/index.d.ts +9 -0
  23. package/dist/index.js +26 -2
  24. package/dist/onboarding.d.ts +2 -2
  25. package/dist/realm/registry.d.ts +0 -8
  26. package/dist/realm/registry.js +1 -11
  27. package/dist/schema/event-message.json +3 -1
  28. package/dist/schema/event-payloads.json +74 -1
  29. package/dist/schema/event-type.json +3 -1
  30. package/dist/schema/reticle-event.json +3 -1
  31. package/dist/schema/reticle-message.json +3 -1
  32. package/dist/source-constants.cjs +1 -0
  33. package/dist/source-constants.d.cts +1 -0
  34. package/dist/telemetry-feedback.d.ts +6 -6
  35. package/dist/telemetry-refusal.d.ts +2 -2
  36. package/dist/telemetry-session.d.ts +25 -2
  37. package/dist/telemetry-session.js +13 -0
  38. package/dist/telemetry.d.ts +184 -102
  39. package/dist/telemetry.js +29 -1
  40. package/dist/verdict/consequence.d.ts +10 -0
  41. package/dist/verdict/consequence.js +13 -0
  42. package/dist/verdict/intent.d.ts +36 -12
  43. package/dist/verdict/intent.js +66 -10
  44. package/dist/verdict/predicate-tree.d.ts +47 -0
  45. package/dist/verdict/predicate-tree.js +146 -0
  46. package/dist/verdict/predicate.d.ts +129 -0
  47. package/dist/verdict/predicate.js +394 -0
  48. package/dist/verdict/property-assertion.d.ts +191 -0
  49. package/dist/verdict/property-assertion.js +42 -0
  50. package/dist/verdict/security.js +16 -4
  51. package/dist/verdict/verdict-attribution.d.ts +52 -0
  52. package/dist/verdict/verdict-attribution.js +87 -0
  53. package/dist/verdict/verification-run.d.ts +98 -84
  54. package/dist/verdict/verification-run.js +8 -1
  55. package/dist/verdict/verified-constants.d.ts +18 -0
  56. package/dist/verdict/verified-constants.js +18 -0
  57. package/dist/wire/constants/constants.d.ts +108 -98
  58. package/dist/wire/constants/constants.js +116 -97
  59. package/dist/wire/constants/discovery.d.ts +25 -0
  60. package/dist/wire/constants/discovery.js +25 -0
  61. package/dist/wire/constants/event-types.d.ts +122 -0
  62. package/dist/wire/constants/event-types.js +121 -0
  63. package/dist/wire/constants/hud-controls.d.ts +19 -0
  64. package/dist/wire/constants/hud-controls.js +95 -0
  65. package/dist/wire/constants/hud-use.d.ts +27 -0
  66. package/dist/wire/constants/hud-use.js +24 -0
  67. package/dist/wire/constants/session-constants.d.ts +24 -0
  68. package/dist/wire/constants/session-constants.js +24 -0
  69. package/dist/wire/event-payloads.d.ts +70 -10
  70. package/dist/wire/event-payloads.js +20 -1
  71. package/dist/wire/messages.d.ts +90 -48
  72. package/dist/wire/messages.js +17 -0
  73. package/dist/wire/net.d.ts +42 -0
  74. package/dist/wire/net.js +48 -0
  75. package/dist/wire/snapshot-tree.d.ts +33 -0
  76. package/dist/wire/snapshot-tree.js +43 -0
  77. package/dist/wire/state-select.d.ts +2 -1
  78. package/dist/wire/state-select.js +56 -3
  79. package/dist/wire/toon.js +28 -7
  80. package/dist/wire/types.d.ts +91 -66
  81. package/dist/wire/types.js +23 -1
  82. package/package.json +5 -1
package/dist/telemetry.js CHANGED
@@ -253,11 +253,34 @@ export const BrowserLaunchKind = {
253
253
  /** A pooled headless context leased for a parallel flow. */
254
254
  POOLED: 'pooled',
255
255
  };
256
+ /**
257
+ * WHO drove the verdict.
258
+ *
259
+ * `actor` already splits a typed `reticle` command from an agent's own tool call, and both halves of
260
+ * an explore drive land on the agent side of that line: a model inside the daemon drives through the
261
+ * same chokepoint, under the same session, and its verdicts were indistinguishable from the ones the
262
+ * user's own agent earned. That matters for every rate computed per session — a session whose only
263
+ * verification came from a drive we ran on the user's behalf is not a session that verified.
264
+ *
265
+ * Absent means the agent's own call, which is the overwhelming majority: a value on every row would
266
+ * cost a field to say the ordinary thing.
267
+ */
268
+ export const VerificationDriver = {
269
+ /** A model inside the daemon drove this, on the user's behalf. */
270
+ HARNESS: 'harness',
271
+ };
256
272
  /** The outcome of one verification. `verified` is Reticle's own honesty grade, not merely pass/fail. */
257
273
  export const VerificationSchema = z.object({
258
274
  /** Which tool produced the verdict (`reticle_assert`, `reticle_flow_verify`, …). */
259
275
  via: z.string().min(1).max(64),
260
- /** `yes` | `no` | `unknown` — the honest verdict, where `unknown` means the evidence could not decide. */
276
+ /**
277
+ * `yes` | `no` | `unknown` | `no-fault` — the honest verdict, from `Verified`.
278
+ *
279
+ * FOUR values, not three. This comment said three for long enough that a dashboard written from it
280
+ * silently drops the bucket that is most actionable: `no-fault` means the window was clean and
281
+ * nothing was declared to prove, which is a teachable agent behaviour rather than a Reticle or an
282
+ * app problem, and `unknown` means the evidence could not decide.
283
+ */
261
284
  verified: z.string().min(1).max(16),
262
285
  /** Did the underlying assertion pass? Distinct from `verified` on purpose. */
263
286
  passed: z.boolean(),
@@ -312,6 +335,11 @@ export const VerificationSchema = z.object({
312
335
  * ownership — Reticle's before the page's. Absent on every verdict whose capture was clean.
313
336
  */
314
337
  uncleanLoss: z.nativeEnum(CaptureLoss).optional(),
338
+ /**
339
+ * Set when one of our OWN drives produced this verdict rather than the user's agent — see
340
+ * `VerificationDriver`. Absent is the ordinary case and means the agent called the tool itself.
341
+ */
342
+ driven: z.nativeEnum(VerificationDriver).optional(),
315
343
  });
316
344
  /** Which way the installed version moved, and between which versions. */
317
345
  export const VersionChangeSchema = z.object({
@@ -49,6 +49,16 @@ export declare const PredicateKind: {
49
49
  };
50
50
  export type PredicateKind = (typeof PredicateKind)[keyof typeof PredicateKind];
51
51
  export declare const CONSEQUENCE_KINDS: ReadonlySet<string>;
52
+ /**
53
+ * The kinds that grade a flow as PRESENCE-ONLY rather than assertion-free.
54
+ *
55
+ * `element` and `text` are the weak checks `PresenceKind` names. `route` joins them here and only
56
+ * here: a route change is observed on a channel rather than queried from the DOM, so it is not a
57
+ * presence check in the sense `isPresenceKind` means, but a route-only expect must still grade as
58
+ * presence rather than falling through to `assertion-free` — which would be a permanent green
59
+ * wearing an assertion.
60
+ */
61
+ export declare const PRESENCE_GRADED: ReadonlySet<string>;
52
62
  /** True when `kind` (a predicate/expect kind) asserts a consequence (signal/net/state). */
53
63
  export declare function isConsequenceKind(kind: string): boolean;
54
64
  /** True when `kind` is a weak presence check (element/text). */
@@ -47,6 +47,19 @@ export const PredicateKind = {
47
47
  };
48
48
  export const CONSEQUENCE_KINDS = new Set(Object.values(ConsequenceKind));
49
49
  const PRESENCE_KINDS = new Set(Object.values(PresenceKind));
50
+ /**
51
+ * The kinds that grade a flow as PRESENCE-ONLY rather than assertion-free.
52
+ *
53
+ * `element` and `text` are the weak checks `PresenceKind` names. `route` joins them here and only
54
+ * here: a route change is observed on a channel rather than queried from the DOM, so it is not a
55
+ * presence check in the sense `isPresenceKind` means, but a route-only expect must still grade as
56
+ * presence rather than falling through to `assertion-free` — which would be a permanent green
57
+ * wearing an assertion.
58
+ */
59
+ export const PRESENCE_GRADED = new Set([
60
+ ...PRESENCE_KINDS,
61
+ PredicateKind.ROUTE,
62
+ ]);
50
63
  /** True when `kind` (a predicate/expect kind) asserts a consequence (signal/net/state). */
51
64
  export function isConsequenceKind(kind) {
52
65
  return CONSEQUENCE_KINDS.has(kind);
@@ -39,12 +39,12 @@ export declare const IntentSurfaceSchema: z.ZodObject<{
39
39
  flow: z.ZodOptional<z.ZodString>;
40
40
  files: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
41
41
  }, "strip", z.ZodTypeAny, {
42
- route?: string | undefined;
43
42
  flow?: string | undefined;
43
+ route?: string | undefined;
44
44
  files?: string[] | undefined;
45
45
  }, {
46
- route?: string | undefined;
47
46
  flow?: string | undefined;
47
+ route?: string | undefined;
48
48
  files?: string[] | undefined;
49
49
  }>;
50
50
  export type IntentSurface = z.infer<typeof IntentSurfaceSchema>;
@@ -75,12 +75,12 @@ export declare const IntentSchema: z.ZodObject<{
75
75
  flow: z.ZodOptional<z.ZodString>;
76
76
  files: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
77
77
  }, "strip", z.ZodTypeAny, {
78
- route?: string | undefined;
79
78
  flow?: string | undefined;
79
+ route?: string | undefined;
80
80
  files?: string[] | undefined;
81
81
  }, {
82
- route?: string | undefined;
83
82
  flow?: string | undefined;
83
+ route?: string | undefined;
84
84
  files?: string[] | undefined;
85
85
  }>>;
86
86
  provenBy: z.ZodOptional<z.ZodObject<{
@@ -113,8 +113,8 @@ export declare const IntentSchema: z.ZodObject<{
113
113
  statement: string;
114
114
  declaredAt: number;
115
115
  surface?: {
116
- route?: string | undefined;
117
116
  flow?: string | undefined;
117
+ route?: string | undefined;
118
118
  files?: string[] | undefined;
119
119
  } | undefined;
120
120
  binding?: unknown;
@@ -133,8 +133,8 @@ export declare const IntentSchema: z.ZodObject<{
133
133
  statement: string;
134
134
  declaredAt: number;
135
135
  surface?: {
136
- route?: string | undefined;
137
136
  flow?: string | undefined;
137
+ route?: string | undefined;
138
138
  files?: string[] | undefined;
139
139
  } | undefined;
140
140
  binding?: unknown;
@@ -164,12 +164,12 @@ export declare const IntentFileSchema: z.ZodObject<{
164
164
  flow: z.ZodOptional<z.ZodString>;
165
165
  files: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
166
166
  }, "strip", z.ZodTypeAny, {
167
- route?: string | undefined;
168
167
  flow?: string | undefined;
168
+ route?: string | undefined;
169
169
  files?: string[] | undefined;
170
170
  }, {
171
- route?: string | undefined;
172
171
  flow?: string | undefined;
172
+ route?: string | undefined;
173
173
  files?: string[] | undefined;
174
174
  }>>;
175
175
  provenBy: z.ZodOptional<z.ZodObject<{
@@ -202,8 +202,8 @@ export declare const IntentFileSchema: z.ZodObject<{
202
202
  statement: string;
203
203
  declaredAt: number;
204
204
  surface?: {
205
- route?: string | undefined;
206
205
  flow?: string | undefined;
206
+ route?: string | undefined;
207
207
  files?: string[] | undefined;
208
208
  } | undefined;
209
209
  binding?: unknown;
@@ -222,8 +222,8 @@ export declare const IntentFileSchema: z.ZodObject<{
222
222
  statement: string;
223
223
  declaredAt: number;
224
224
  surface?: {
225
- route?: string | undefined;
226
225
  flow?: string | undefined;
226
+ route?: string | undefined;
227
227
  files?: string[] | undefined;
228
228
  } | undefined;
229
229
  binding?: unknown;
@@ -245,8 +245,8 @@ export declare const IntentFileSchema: z.ZodObject<{
245
245
  statement: string;
246
246
  declaredAt: number;
247
247
  surface?: {
248
- route?: string | undefined;
249
248
  flow?: string | undefined;
249
+ route?: string | undefined;
250
250
  files?: string[] | undefined;
251
251
  } | undefined;
252
252
  binding?: unknown;
@@ -268,8 +268,8 @@ export declare const IntentFileSchema: z.ZodObject<{
268
268
  statement: string;
269
269
  declaredAt: number;
270
270
  surface?: {
271
- route?: string | undefined;
272
271
  flow?: string | undefined;
272
+ route?: string | undefined;
273
273
  files?: string[] | undefined;
274
274
  } | undefined;
275
275
  binding?: unknown;
@@ -307,6 +307,16 @@ export declare function dischargeIntent(intent: Intent, proof: {
307
307
  grade: string;
308
308
  at: number;
309
309
  }): Intent;
310
+ /**
311
+ * What declaring an intent that may already exist should leave stored.
312
+ *
313
+ * Declaring is what a re-run does — a flow re-saved, a feature's intents declared again in a later
314
+ * session — and it replaced the record outright, so a proved rule lost its check and its proof every
315
+ * time somebody said it again. Same words: nothing about the promise changed, so nothing is
316
+ * discarded. Different words: the check usually survives a rewording, the proof cannot, because it
317
+ * was evidence for words that are gone. The amendment itself is recorded by `upsertIntent`.
318
+ */
319
+ export declare function redeclareIntent(existing: Intent | undefined, fresh: Intent): Intent;
310
320
  /**
311
321
  * Add or amend an intent, keeping the previous statement in its history.
312
322
  *
@@ -315,6 +325,20 @@ export declare function dischargeIntent(intent: Intent, proof: {
315
325
  * amendment somebody needs to see.
316
326
  */
317
327
  export declare function upsertIntent(file: IntentFile, intent: Intent): IntentFile;
328
+ /**
329
+ * The next version of an intent, carrying the previous wording into its history when it changed.
330
+ *
331
+ * One rule for every store that keeps intents, so a flat ledger and a sharded one cannot keep two
332
+ * different histories of the same promise.
333
+ */
334
+ export declare function amendIntent(previous: Intent | undefined, next: Intent): Intent;
335
+ /**
336
+ * Is this a description of what was DONE rather than a statement of what must be TRUE?
337
+ *
338
+ * An intent is the business rule a teammate reads in six months. A step label is a log line, and a
339
+ * ledger full of them buries the rules behind "click button \"Cancel\"" and inflates what is owed.
340
+ */
341
+ export declare function isActionLabel(statement: string): boolean;
318
342
  /** Everything not yet proved — what an agent asking "am I done?" still owes. */
319
343
  export declare function openIntents(file: IntentFile): Intent[];
320
344
  /** Parse an intent file, failing soft to empty. Never throws — a cache must not take a daemon down. */
@@ -75,7 +75,13 @@ export function declareIntent(input) {
75
75
  }
76
76
  /** Attach the predicate that would prove it. Pure — returns a new intent. */
77
77
  export function bindIntent(intent, binding) {
78
- return { ...intent, state: IntentState.BOUND, binding };
78
+ // The same check again — what re-saving a flow does — leaves a proved intent proved.
79
+ // ponytail: JSON equality, so key order matters; both bindings come from the same writers.
80
+ if (JSON.stringify(intent.binding) === JSON.stringify(binding))
81
+ return intent;
82
+ // A different check has proved nothing yet, whatever the old one did.
83
+ const { provenBy: _proof, ...unproved } = intent;
84
+ return { ...unproved, state: IntentState.BOUND, binding };
79
85
  }
80
86
  /**
81
87
  * Record that a verdict proved it.
@@ -90,6 +96,30 @@ export function dischargeIntent(intent, proof) {
90
96
  return intent;
91
97
  return { ...intent, state: IntentState.PROVED, provenBy: proof };
92
98
  }
99
+ /**
100
+ * What declaring an intent that may already exist should leave stored.
101
+ *
102
+ * Declaring is what a re-run does — a flow re-saved, a feature's intents declared again in a later
103
+ * session — and it replaced the record outright, so a proved rule lost its check and its proof every
104
+ * time somebody said it again. Same words: nothing about the promise changed, so nothing is
105
+ * discarded. Different words: the check usually survives a rewording, the proof cannot, because it
106
+ * was evidence for words that are gone. The amendment itself is recorded by `upsertIntent`.
107
+ */
108
+ export function redeclareIntent(existing, fresh) {
109
+ if (existing === undefined)
110
+ return fresh;
111
+ const surface = existing.surface ?? fresh.surface;
112
+ const withSurface = surface === undefined ? {} : { surface };
113
+ if (existing.statement === fresh.statement)
114
+ return { ...existing, ...withSurface };
115
+ const { provenBy: _proof, ...unproved } = existing;
116
+ return {
117
+ ...unproved,
118
+ statement: fresh.statement,
119
+ state: existing.binding === undefined ? IntentState.DECLARED : IntentState.BOUND,
120
+ ...withSurface,
121
+ };
122
+ }
93
123
  /**
94
124
  * Add or amend an intent, keeping the previous statement in its history.
95
125
  *
@@ -98,19 +128,45 @@ export function dischargeIntent(intent, proof) {
98
128
  * amendment somebody needs to see.
99
129
  */
100
130
  export function upsertIntent(file, intent) {
101
- const previous = file.intents[intent.id];
102
- const changed = previous !== undefined && previous.statement !== intent.statement;
103
- const amended = changed
104
- ? [...(previous.amended ?? []), { statement: previous.statement, at: previous.declaredAt }]
105
- : previous?.amended;
106
131
  return {
107
132
  version: INTENT_FILE_VERSION,
108
- intents: {
109
- ...file.intents,
110
- [intent.id]: { ...intent, ...(amended === undefined ? {} : { amended }) },
111
- },
133
+ intents: { ...file.intents, [intent.id]: amendIntent(file.intents[intent.id], intent) },
112
134
  };
113
135
  }
136
+ /**
137
+ * The next version of an intent, carrying the previous wording into its history when it changed.
138
+ *
139
+ * One rule for every store that keeps intents, so a flat ledger and a sharded one cannot keep two
140
+ * different histories of the same promise.
141
+ */
142
+ export function amendIntent(previous, next) {
143
+ const changed = previous !== undefined && previous.statement !== next.statement;
144
+ const amended = changed
145
+ ? [...(previous.amended ?? []), { statement: previous.statement, at: previous.declaredAt }]
146
+ : previous?.amended;
147
+ return { ...next, ...(amended === undefined ? {} : { amended }) };
148
+ }
149
+ /**
150
+ * A step verb directly followed by an element role: `click button "Save"`, `check switch "X"`.
151
+ *
152
+ * The shape an ACTION DESCRIPTION takes, which is what tools generate from a snapshot line. Narrow on
153
+ * purpose: "click the Save button and the order appears" names a consequence and is kept, as is any
154
+ * gerund ("clicking Send makes the badge read…"). Refusing a real intent costs more than admitting a
155
+ * label, so only the unmistakable shape is refused.
156
+ */
157
+ const STEP_LABEL = /^(click|dblclick|fill|type|clear|select|check|uncheck|press|hover|focus|tap|drag|scroll|submit|upload)\s+(button|link|textbox|searchbox|switch|checkbox|menuitem|menuitemcheckbox|tab|combobox|option|radio|slider|spinbutton|heading|img|row|cell|listitem|treeitem)\b/i;
158
+ /** What an automated drive used to file as its "intent": a count of what it did. */
159
+ const DRIVE_LOG = /^Autonomous coverage drive\b/i;
160
+ /**
161
+ * Is this a description of what was DONE rather than a statement of what must be TRUE?
162
+ *
163
+ * An intent is the business rule a teammate reads in six months. A step label is a log line, and a
164
+ * ledger full of them buries the rules behind "click button \"Cancel\"" and inflates what is owed.
165
+ */
166
+ export function isActionLabel(statement) {
167
+ const text = statement.trim();
168
+ return STEP_LABEL.test(text) || DRIVE_LOG.test(text);
169
+ }
114
170
  /** Everything not yet proved — what an agent asking "am I done?" still owes. */
115
171
  export function openIntents(file) {
116
172
  return Object.values(file.intents).filter((intent) => intent.state !== IntentState.PROVED);
@@ -0,0 +1,47 @@
1
+ import type { Predicate } from './predicate.js';
2
+ /** Every clause at the top level: the predicate itself, or the members of a top-level `allOf`. */
3
+ export declare function conjuncts(predicate: Predicate | undefined): readonly Predicate[];
4
+ /** The first top-level clause of this kind, or undefined. */
5
+ export declare function clauseOfKind<K extends Predicate['kind']>(predicate: Predicate | undefined, kind: K): Extract<Predicate, {
6
+ kind: K;
7
+ }> | undefined;
8
+ /** Does this expectation assert store state at the top level? The drift kind branches on it. */
9
+ export declare function assertsState(predicate: Predicate | undefined): boolean;
10
+ /**
11
+ * The testid an element clause says must be PRESENT, when the expectation has one at the top level.
12
+ *
13
+ * An `absent: true` clause names a testid that must be gone, and asking the replay runner to
14
+ * require it made a correct dismissal fail. That clause is left to the predicate wait instead.
15
+ */
16
+ export declare function expectedElementTestid(predicate: Predicate | undefined): string | undefined;
17
+ /**
18
+ * Rebuild the expectation without the top-level clauses a caller wants gone, collapsing as it goes.
19
+ *
20
+ * One clause left is that clause, not an `allOf` of one: a single-member composite grades weaker
21
+ * than its own member and reads worse in a report, and `combine()` has had to be taught about
22
+ * zero-branch composites once already.
23
+ */
24
+ export declare function withoutClauses(predicate: Predicate | undefined, drop: (clause: Predicate) => boolean): Predicate | undefined;
25
+ /**
26
+ * Name what was asserted, for the drift row's `anchor` column.
27
+ *
28
+ * Reports the STRONGEST clause rather than the first, because that is the one a reader is asking
29
+ * about: an `allOf[settled, net]` is about the request, and answering "settled" names the gate
30
+ * instead of the claim.
31
+ */
32
+ export declare function expectLabel(predicate: Predicate | undefined): string;
33
+ /**
34
+ * An element ref: `e` and a sequence number, minted per session by the SDK's ref table. It is an
35
+ * address inside ONE session's numbering, not an identity.
36
+ */
37
+ export declare const SESSION_REF: RegExp;
38
+ /** Why a flow refused a predicate holding a ref, naming the field. */
39
+ export declare function sessionRefRefusal(field: string): string;
40
+ /**
41
+ * The first field anywhere in the tree that holds a session ref, as `kind.field "value"`.
42
+ *
43
+ * DEEP, unlike every reader above: those walk the top level because dropping a nested clause would
44
+ * change what a flow means, while this only asks whether the predicate can mean anything at all in
45
+ * another session. A ref under a `not` is exactly as meaningless as one at the top.
46
+ */
47
+ export declare function sessionBoundField(predicate: Predicate): string | undefined;
@@ -0,0 +1,146 @@
1
+ /**
2
+ * Reading a predicate's TOP-LEVEL clauses.
3
+ *
4
+ * Structural, not evaluative: these answer "what does this predicate say" from the shape alone, the
5
+ * same basis `predicateShapeFor` sits here on. Nothing here decides anything about an app.
6
+ *
7
+ * It lived beside the replay code until the journal's flow-naming needed it too, and a
8
+ * `journal -> flows` reach is a file filed in the wrong place rather than a dependency anybody
9
+ * wanted. The thing both callers want is a reader for a core type, so it lives with the type.
10
+ *
11
+ * v1 stored one slot per kind, so "does this step assert state?" was a property read and "drop the
12
+ * element clause" was a `delete`. A `Predicate` nests, so every one of those questions is a walk.
13
+ *
14
+ * The walks here are deliberately SHALLOW, and that is the whole design. A lifted v1 file produces
15
+ * either a single leaf or one flat `allOf`, so a top-level walk reproduces v1 behaviour exactly. A
16
+ * hand-written v2 predicate can nest arbitrarily, and going deeper would silently change what an
17
+ * existing flow asserts:
18
+ *
19
+ * - dropping a leaf from an `allOf` removes a conjunct, which is what v1's `delete` did;
20
+ * - dropping a branch from an `anyOf` removes an ALTERNATIVE, which makes the predicate STRICTER
21
+ * and can turn a passing flow red;
22
+ * - dropping anything under a `not` inverts the meaning of the thing you dropped.
23
+ *
24
+ * So the composite cases are handled where they mean what v1 meant, and left alone everywhere else.
25
+ */
26
+ import { PredicateKind } from './consequence.js';
27
+ /** Every clause at the top level: the predicate itself, or the members of a top-level `allOf`. */
28
+ export function conjuncts(predicate) {
29
+ if (predicate === undefined)
30
+ return [];
31
+ return PredicateKind.ALL_OF === predicate.kind ? predicate.predicates : [predicate];
32
+ }
33
+ /** The first top-level clause of this kind, or undefined. */
34
+ export function clauseOfKind(predicate, kind) {
35
+ return conjuncts(predicate).find((one) => one.kind === kind);
36
+ }
37
+ /** Does this expectation assert store state at the top level? The drift kind branches on it. */
38
+ export function assertsState(predicate) {
39
+ return clauseOfKind(predicate, PredicateKind.STATE) !== undefined;
40
+ }
41
+ /**
42
+ * The testid an element clause says must be PRESENT, when the expectation has one at the top level.
43
+ *
44
+ * An `absent: true` clause names a testid that must be gone, and asking the replay runner to
45
+ * require it made a correct dismissal fail. That clause is left to the predicate wait instead.
46
+ */
47
+ export function expectedElementTestid(predicate) {
48
+ const element = clauseOfKind(predicate, PredicateKind.ELEMENT);
49
+ return true === element?.absent ? undefined : element?.query.testid;
50
+ }
51
+ /**
52
+ * Rebuild the expectation without the top-level clauses a caller wants gone, collapsing as it goes.
53
+ *
54
+ * One clause left is that clause, not an `allOf` of one: a single-member composite grades weaker
55
+ * than its own member and reads worse in a report, and `combine()` has had to be taught about
56
+ * zero-branch composites once already.
57
+ */
58
+ export function withoutClauses(predicate, drop) {
59
+ if (predicate === undefined)
60
+ return undefined;
61
+ const kept = conjuncts(predicate).filter((clause) => !drop(clause));
62
+ const [only] = kept;
63
+ if (0 === kept.length)
64
+ return undefined;
65
+ if (1 === kept.length && only !== undefined)
66
+ return only;
67
+ return { kind: PredicateKind.ALL_OF, predicates: [...kept] };
68
+ }
69
+ /**
70
+ * Name what was asserted, for the drift row's `anchor` column.
71
+ *
72
+ * Reports the STRONGEST clause rather than the first, because that is the one a reader is asking
73
+ * about: an `allOf[settled, net]` is about the request, and answering "settled" names the gate
74
+ * instead of the claim.
75
+ */
76
+ export function expectLabel(predicate) {
77
+ const clauses = conjuncts(predicate);
78
+ const signal = clauses.find((c) => PredicateKind.SIGNAL === c.kind);
79
+ if (signal !== undefined && PredicateKind.SIGNAL === signal.kind) {
80
+ return `signal:${signal.name ?? '*'}`;
81
+ }
82
+ const net = clauses.find((c) => PredicateKind.NET === c.kind);
83
+ if (net !== undefined && PredicateKind.NET === net.kind) {
84
+ return `net:${net.urlContains ?? net.method ?? '*'}`;
85
+ }
86
+ const state = clauses.find((c) => PredicateKind.STATE === c.kind);
87
+ if (state !== undefined && PredicateKind.STATE === state.kind)
88
+ return `state:${state.path}`;
89
+ const console_ = clauses.find((c) => PredicateKind.CONSOLE === c.kind);
90
+ if (console_ !== undefined && PredicateKind.CONSOLE === console_.kind) {
91
+ return `console:${console_.level ?? '*'}`;
92
+ }
93
+ const element = clauses.find((c) => PredicateKind.ELEMENT === c.kind);
94
+ if (element !== undefined && PredicateKind.ELEMENT === element.kind) {
95
+ const q = element.query;
96
+ return q.testid ?? q.name ?? q.role ?? 'element';
97
+ }
98
+ const text = clauses.find((c) => PredicateKind.TEXT === c.kind);
99
+ if (text !== undefined && PredicateKind.TEXT === text.kind)
100
+ return `text:${text.contains ?? '*'}`;
101
+ const route = clauses.find((c) => PredicateKind.ROUTE === c.kind);
102
+ if (route !== undefined && PredicateKind.ROUTE === route.kind) {
103
+ return `route:${route.pathname ?? route.contains ?? '*'}`;
104
+ }
105
+ return 'expect';
106
+ }
107
+ /**
108
+ * An element ref: `e` and a sequence number, minted per session by the SDK's ref table. It is an
109
+ * address inside ONE session's numbering, not an identity.
110
+ */
111
+ export const SESSION_REF = /^e\d+$/;
112
+ /** Why a flow refused a predicate holding a ref, naming the field. */
113
+ export function sessionRefRefusal(field) {
114
+ return (`${field} is an element ref, which only exists in the session that minted it — ` +
115
+ 'scope by a CSS selector or a testid so the flow can check it again');
116
+ }
117
+ /**
118
+ * The first field anywhere in the tree that holds a session ref, as `kind.field "value"`.
119
+ *
120
+ * DEEP, unlike every reader above: those walk the top level because dropping a nested clause would
121
+ * change what a flow means, while this only asks whether the predicate can mean anything at all in
122
+ * another session. A ref under a `not` is exactly as meaningless as one at the top.
123
+ */
124
+ export function sessionBoundField(predicate) {
125
+ const ref = (field, value) => value !== undefined && SESSION_REF.test(value) ? `${field} "${value}"` : undefined;
126
+ switch (predicate.kind) {
127
+ case PredicateKind.ALL_OF:
128
+ case PredicateKind.ANY_OF:
129
+ for (const one of predicate.predicates) {
130
+ const found = sessionBoundField(one);
131
+ if (found !== undefined)
132
+ return found;
133
+ }
134
+ return undefined;
135
+ case PredicateKind.NOT:
136
+ return sessionBoundField(predicate.predicate);
137
+ case PredicateKind.TEXT:
138
+ return ref('text.scope', predicate.scope);
139
+ case PredicateKind.ELEMENT:
140
+ return ref('element.query.scope', predicate.query.scope);
141
+ case PredicateKind.ANIMATION:
142
+ return ref('animation.target', predicate.target);
143
+ default:
144
+ return undefined;
145
+ }
146
+ }
@@ -0,0 +1,129 @@
1
+ /**
2
+ * The predicate: what a caller can declare, as a shape that crosses the wire.
3
+ *
4
+ * This is the CONTRACT half of the predicate language, and it is here for the reason everything
5
+ * else in this package is: a message that crosses browser ↔ bridge ↔ agent is defined in core, and
6
+ * a predicate is now one. `FlowStep.expect` is a `Predicate`, which means a saved flow file carries
7
+ * one, which means core has to be able to parse what it reads off disk.
8
+ *
9
+ * It was in `@reticlehq/engine` and it was already half here: `PredicateKind` has always lived in
10
+ * `verdict/consequence.ts`, so the engine's schema imported its own vocabulary back from its parent.
11
+ * This finishes that migration rather than starting one.
12
+ *
13
+ * WHAT DID NOT COME WITH IT, and the line is deliberate. Evaluation stays in the engine:
14
+ * `satisfiesProperty`, the residual-query checks, the grammar and nested-key introspection. Core is
15
+ * the contract, not the reasoning. A reader looking for "what may I write" is in the right file; a
16
+ * reader looking for "what does it decide" is one package out.
17
+ */
18
+ import { z } from 'zod';
19
+ import { type ElementQuery } from '../wire/types.js';
20
+ import { ElementState } from '../wire/constants/constants.js';
21
+ import { PredicateKind } from './consequence.js';
22
+ import { type PropertyAssertion } from './property-assertion.js';
23
+ export type Predicate = {
24
+ kind: typeof PredicateKind.ELEMENT;
25
+ query: ElementQuery;
26
+ state?: ElementState;
27
+ absent?: boolean;
28
+ } | {
29
+ kind: typeof PredicateKind.TEXT;
30
+ contains?: string;
31
+ visible?: boolean;
32
+ absent?: boolean;
33
+ /**
34
+ * Restrict the match to a subtree, as a CSS selector or a ref — the same field, and the same
35
+ * meaning, as `scope` on an element query.
36
+ *
37
+ * Without it the match is page-wide, and a word that appears both in a background tab label
38
+ * and in the dialog that just opened satisfies the predicate BEFORE the action runs, so
39
+ * `act_and_wait` reports `already_true` for an action that did exactly the right thing.
40
+ */
41
+ scope?: string;
42
+ /** Match the scope root itself and check its combined subtree text. Requires `scope`. */
43
+ self?: boolean;
44
+ /**
45
+ * Assert a PROPERTY of the rendered text rather than its exact bytes.
46
+ *
47
+ * The same reasoning `state` carries, applied where the user actually reads the value: an
48
+ * app whose output IS a model's output is different on every run and correct on every one of
49
+ * them, so `contains` is the one assertion it cannot satisfy twice. Both may be supplied and
50
+ * then both must hold — `satisfies` narrows, it never excuses.
51
+ */
52
+ satisfies?: PropertyAssertion;
53
+ } | {
54
+ kind: typeof PredicateKind.NET;
55
+ method?: string;
56
+ urlContains?: string;
57
+ status?: number;
58
+ /** Did the call succeed? The honest field for IPC, which has no status code. */
59
+ ok?: boolean;
60
+ since?: number;
61
+ count?: number;
62
+ /** A substring the RESPONSE body must contain — what the server answered, not what was sent. */
63
+ bodyContains?: string;
64
+ /** Shallow JSON match over the RESPONSE body: this FIELD holds this value (#987). */
65
+ bodyMatches?: Record<string, unknown>;
66
+ /** A substring the REQUEST body must contain — what the UI sent, not what came back. */
67
+ requestBodyContains?: string;
68
+ /** Shallow JSON match over the REQUEST body, in the style of `signal.dataMatches`. */
69
+ requestBodyMatches?: Record<string, unknown>;
70
+ } | {
71
+ kind: typeof PredicateKind.ROUTE;
72
+ pathname?: string;
73
+ contains?: string;
74
+ since?: number;
75
+ } | {
76
+ kind: typeof PredicateKind.CONSOLE;
77
+ level?: string;
78
+ /**
79
+ * A substring the captured message must contain. With `absent: true` this flips the meaning
80
+ * from "no console entries at all" to "THIS message did not appear" — the assertion people
81
+ * actually write regression checks for (deprecation warnings, no-op handler notices).
82
+ */
83
+ contains?: string;
84
+ absent?: boolean;
85
+ since?: number;
86
+ } | {
87
+ kind: typeof PredicateKind.ANIMATION;
88
+ name?: string;
89
+ target?: string;
90
+ completed?: boolean;
91
+ since?: number;
92
+ } | {
93
+ kind: typeof PredicateKind.SIGNAL;
94
+ name?: string;
95
+ dataMatches?: Record<string, unknown>;
96
+ /**
97
+ * Exact number of matching signals in the window — the same cardinality assertion `net`
98
+ * carries, on the channel the app itself speaks. Omit for presence (≥1).
99
+ */
100
+ count?: number;
101
+ since?: number;
102
+ } | {
103
+ kind: typeof PredicateKind.STATE;
104
+ store?: string;
105
+ path: string;
106
+ equals?: unknown;
107
+ satisfies?: PropertyAssertion;
108
+ } | {
109
+ kind: typeof PredicateKind.SETTLED;
110
+ quietMs?: number;
111
+ } | {
112
+ kind: typeof PredicateKind.ALL_OF;
113
+ predicates: Predicate[];
114
+ } | {
115
+ kind: typeof PredicateKind.ANY_OF;
116
+ predicates: Predicate[];
117
+ } | {
118
+ kind: typeof PredicateKind.NOT;
119
+ predicate: Predicate;
120
+ };
121
+ export declare const PredicateSchema: z.ZodType<Predicate>;
122
+ /**
123
+ * The fields one predicate kind accepts, as the schema itself declares them.
124
+ *
125
+ * Exported because the ENGINE's introspection needs it and the union it reads is built here. A
126
+ * second copy of the union next door is how a rejection message comes to name a stale field set,
127
+ * which is worse than naming none: the agent trusts it and retries into the same wall.
128
+ */
129
+ export declare function predicateShapeFor(kind: string): z.ZodRawShape | null;