@reticlehq/engine 3.2.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 (32) hide show
  1. package/dist/disagreement/body-failures.js +32 -12
  2. package/dist/disagreement/contradiction-evidence.d.ts +1 -1
  3. package/dist/disagreement/contradiction-evidence.js +3 -3
  4. package/dist/disagreement/contradiction-types.d.ts +6 -0
  5. package/dist/disagreement/contradictions.js +4 -2
  6. package/dist/evidence/already-true.js +9 -2
  7. package/dist/evidence/baseline.d.ts +21 -0
  8. package/dist/evidence/baseline.js +129 -0
  9. package/dist/evidence/verified.d.ts +7 -0
  10. package/dist/evidence/verified.js +35 -1
  11. package/dist/question/declared.js +2 -1
  12. package/dist/question/lineage.d.ts +14 -5
  13. package/dist/question/lineage.js +65 -10
  14. package/dist/question/predicate/predicate-element.d.ts +3 -2
  15. package/dist/question/predicate/predicate-element.js +68 -3
  16. package/dist/question/predicate/predicate-eval.d.ts +6 -0
  17. package/dist/question/predicate/predicate-eval.js +13 -7
  18. package/dist/question/predicate/predicate-request-body.d.ts +1 -1
  19. package/dist/question/predicate/predicate-route.d.ts +2 -1
  20. package/dist/question/predicate/predicate-schema.d.ts +12 -93
  21. package/dist/question/predicate/predicate-schema.js +15 -288
  22. package/dist/question/predicate/predicate-session.d.ts +15 -1
  23. package/dist/question/predicate/predicate-session.js +25 -1
  24. package/dist/question/predicate/predicate-state.d.ts +15 -0
  25. package/dist/question/predicate/predicate-state.js +317 -0
  26. package/dist/question/predicate/predicate.d.ts +17 -3
  27. package/dist/question/predicate/predicate.js +139 -328
  28. package/dist/question/predicate/property.d.ts +24 -73
  29. package/dist/question/predicate/property.js +134 -20
  30. package/dist/window/json-salvage.d.ts +2 -1
  31. package/dist/window/json-salvage.js +2 -1
  32. package/package.json +2 -2
@@ -1,4 +1,6 @@
1
- import { z } from 'zod';
1
+ import { type PropertyAssertion } from '@reticlehq/core';
2
+ export { propertyAssertionSchema } from '@reticlehq/core';
3
+ export type { Delta, PropertyAssertion } from '@reticlehq/core';
2
4
  /**
3
5
  * Assert a PROPERTY of an observed value rather than its exact bytes.
4
6
  *
@@ -16,87 +18,36 @@ import { z } from 'zod';
16
18
  * A failing check always says why. An assertion that reports only `false` sends the reader back for
17
19
  * another call to find out what it saw, and that round trip is most of what a verdict costs.
18
20
  */
19
- export type PropertyAssertion =
20
- /** Produced something at all — the honest floor for any generated output. */
21
- {
22
- readonly property: 'nonEmpty';
23
- }
24
- /** A classification landed inside the allowed set. Exact membership, no coercion. */
25
- | {
26
- readonly property: 'oneOf';
27
- readonly values: readonly unknown[];
28
- }
29
- /** A number near enough to an expected one — inclusive on the bound. */
30
- | {
31
- readonly property: 'withinTolerance';
32
- readonly of: number;
33
- readonly tolerance: number;
34
- }
35
- /** The shape of the output, as a regular expression over its string form. */
36
- | {
37
- readonly property: 'matchesPattern';
38
- readonly pattern: string;
21
+ /**
22
+ * The reading taken BEFORE the action, when one was taken.
23
+ *
24
+ * A wrapper rather than a bare value because `undefined` is a legitimate reading — a store path
25
+ * that held nothing — and "the path was empty" and "nobody looked" must not collapse into the same
26
+ * answer. The first is a comparison; the second is a refusal to pretend one happened.
27
+ */
28
+ export interface Baseline {
29
+ readonly taken: true;
30
+ readonly value: unknown;
39
31
  }
40
- /** The right KIND of thing: `array` and `object` are distinguished, which `typeof` cannot do. */
41
- | {
42
- readonly property: 'type';
43
- readonly is: 'string' | 'number' | 'boolean' | 'array' | 'object';
44
- };
45
32
  export interface PropertyResult {
46
33
  readonly ok: boolean;
47
34
  /** Why, in the words a reader needs — always present, on pass and on fail. */
48
35
  readonly because: string;
36
+ /**
37
+ * Nothing was compared: a relative property was asked with no before-reading.
38
+ *
39
+ * `ok` is false because nothing was proven, but a false nobody could have made true is not a
40
+ * defect in the app, and reporting it as one puts our own gaps into somebody's bug count. The
41
+ * caller lifts this into `inconclusive`.
42
+ */
43
+ readonly unevaluated?: true;
49
44
  }
50
- export declare function satisfiesProperty(value: unknown, assertion: PropertyAssertion): PropertyResult;
45
+ export declare function satisfiesProperty(value: unknown, assertion: PropertyAssertion,
46
+ /** The reading taken before the action. Only the relative properties look at it. */
47
+ baseline?: Baseline): PropertyResult;
51
48
  /**
52
49
  * The wire shape of a property assertion.
53
50
  *
54
51
  * Lives here beside the evaluator on purpose: a schema in one file and the switch that consumes it
55
52
  * in another is how a new property comes to parse and then silently never match.
56
53
  */
57
- export declare const propertyAssertionSchema: z.ZodDiscriminatedUnion<"property", [z.ZodObject<{
58
- property: z.ZodLiteral<"nonEmpty">;
59
- }, "strict", z.ZodTypeAny, {
60
- property: "nonEmpty";
61
- }, {
62
- property: "nonEmpty";
63
- }>, z.ZodObject<{
64
- property: z.ZodLiteral<"oneOf">;
65
- values: z.ZodArray<z.ZodUnknown, "many">;
66
- }, "strict", z.ZodTypeAny, {
67
- values: unknown[];
68
- property: "oneOf";
69
- }, {
70
- values: unknown[];
71
- property: "oneOf";
72
- }>, z.ZodObject<{
73
- property: z.ZodLiteral<"withinTolerance">;
74
- of: z.ZodNumber;
75
- tolerance: z.ZodNumber;
76
- }, "strict", z.ZodTypeAny, {
77
- property: "withinTolerance";
78
- of: number;
79
- tolerance: number;
80
- }, {
81
- property: "withinTolerance";
82
- of: number;
83
- tolerance: number;
84
- }>, z.ZodObject<{
85
- property: z.ZodLiteral<"matchesPattern">;
86
- pattern: z.ZodString;
87
- }, "strict", z.ZodTypeAny, {
88
- property: "matchesPattern";
89
- pattern: string;
90
- }, {
91
- property: "matchesPattern";
92
- pattern: string;
93
- }>, z.ZodObject<{
94
- property: z.ZodLiteral<"type">;
95
- is: z.ZodEnum<["string", "number", "boolean", "array", "object"]>;
96
- }, "strict", z.ZodTypeAny, {
97
- property: "type";
98
- is: "string" | "number" | "boolean" | "object" | "array";
99
- }, {
100
- property: "type";
101
- is: "string" | "number" | "boolean" | "object" | "array";
102
- }>]>;
@@ -1,4 +1,105 @@
1
- import { z } from 'zod';
1
+ import { MeasureOp } from 'open-verification';
2
+ import {} from '@reticlehq/core';
3
+ /*
4
+ * The CONTRACT moved to core and is re-exported from here.
5
+ *
6
+ * A saved flow carries a `satisfies` now, so core has to be able to parse one. What stays is the
7
+ * half that DECIDES: `satisfiesProperty`, the baseline it compares against, and the two helpers
8
+ * (`show`, `numberIn`) that exist so a failure can quote what it saw. Core is the contract, this is
9
+ * the reasoning, and the split is the same one `predicate-schema.ts` next door makes.
10
+ */
11
+ export { propertyAssertionSchema } from '@reticlehq/core';
12
+ /** The relative properties: the ones that mean nothing without a before-reading. */
13
+ const RELATIVE = new Set(['changed', 'unchanged', 'increased', 'decreased']);
14
+ /**
15
+ * A number out of a reading, or undefined.
16
+ *
17
+ * A displayed total arrives as text — `"$1,234.50"` is a number to every reader of the page, and
18
+ * refusing it would make the one assertion this feature exists for unwritable against the DOM. The
19
+ * strip is deliberately narrow: currency symbols, thousands separators and surrounding space. A
20
+ * reading with no digits in it ("sold out", "—", an empty box) is NOT zero, and coercing it to zero
21
+ * is how "the balance went to nothing" reads as a pass.
22
+ */
23
+ function numberIn(value) {
24
+ if ('number' === typeof value)
25
+ return Number.isFinite(value) ? value : undefined;
26
+ if ('string' !== typeof value)
27
+ return undefined;
28
+ const stripped = value.replace(/[^0-9eE+.-]/g, '');
29
+ if (!/[0-9]/.test(stripped))
30
+ return undefined;
31
+ const parsed = Number(stripped);
32
+ return Number.isFinite(parsed) ? parsed : undefined;
33
+ }
34
+ /** Structural equality, so two readings of the same object are the same reading. */
35
+ function sameReading(a, b) {
36
+ if (Object.is(a, b))
37
+ return true;
38
+ if ('object' !== typeof a || 'object' !== typeof b || null === a || null === b)
39
+ return false;
40
+ try {
41
+ return JSON.stringify(a) === JSON.stringify(b);
42
+ }
43
+ catch {
44
+ return false;
45
+ }
46
+ }
47
+ /** Does a movement of `delta` satisfy the named bound? Tolerance widens, never narrows. */
48
+ function withinDelta(moved, by) {
49
+ const tolerance = by.tolerance ?? 0;
50
+ switch (by.op) {
51
+ case MeasureOp.EQUALS:
52
+ return Math.abs(moved - by.value) <= tolerance;
53
+ case MeasureOp.AT_LEAST:
54
+ return moved >= by.value - tolerance;
55
+ case MeasureOp.AT_MOST:
56
+ return moved <= by.value + tolerance;
57
+ }
58
+ }
59
+ function describeDelta(by) {
60
+ const tolerance = by.tolerance ?? 0;
61
+ const band = 0 === tolerance ? '' : ` (± ${String(tolerance)})`;
62
+ return `${by.op} ${String(by.value)}${band}`;
63
+ }
64
+ /**
65
+ * `increased`/`decreased`, which are the same question with the sign flipped.
66
+ *
67
+ * Both readings must be numbers. A non-numeric one is reported as such rather than compared,
68
+ * because "not a number" is a fact about the reading and `false` would be a claim about the app.
69
+ */
70
+ function movedBy(value, previous, direction, by) {
71
+ const now = numberIn(value);
72
+ const then = numberIn(previous);
73
+ if (now === undefined || then === undefined) {
74
+ const which = now === undefined ? show(value) : show(previous);
75
+ return {
76
+ ok: false,
77
+ because: `${which} is not a number, so nothing was subtracted — assert the value that carries the total, not the label around it`,
78
+ unevaluated: true,
79
+ };
80
+ }
81
+ const moved = 'increased' === direction ? now - then : then - now;
82
+ const movement = `${show(previous)} → ${show(value)} (${'increased' === direction ? '+' : '-'}${String(Math.abs(moved))})`;
83
+ if (moved <= 0) {
84
+ return { ok: false, because: `expected it to have ${direction}: ${movement}` };
85
+ }
86
+ if (by === undefined)
87
+ return { ok: true, because: movement };
88
+ const ok = withinDelta(moved, by);
89
+ if (ok)
90
+ return { ok, because: `${movement}, which is ${describeDelta(by)}` };
91
+ return {
92
+ ok,
93
+ // The float trap, named where it is hit. `100 - 88.13` is 11.870000000000005, so an exact
94
+ // `decreased by 11.87` — the assertion this feature exists for — misses by 5e-15 and reads as a
95
+ // money bug. The tolerance is the fix and the protocol already carries it; a silent epsilon here
96
+ // would be this engine quietly disagreeing with the vocabulary it borrowed.
97
+ because: `${movement}, and the change was required to be ${describeDelta(by)}` +
98
+ (MeasureOp.EQUALS === by.op && Math.abs(moved - by.value) < 1e-6
99
+ ? ' — off by less than a millionth, which is floating point, not the app. Give the assertion a `tolerance`'
100
+ : ''),
101
+ };
102
+ }
2
103
  /**
3
104
  * A short, readable rendering of any observed value.
4
105
  *
@@ -44,8 +145,39 @@ function typeOf(value) {
44
145
  return 'array';
45
146
  return typeof value;
46
147
  }
47
- export function satisfiesProperty(value, assertion) {
148
+ export function satisfiesProperty(value, assertion,
149
+ /** The reading taken before the action. Only the relative properties look at it. */
150
+ baseline) {
151
+ if (RELATIVE.has(assertion.property) && baseline === undefined) {
152
+ return {
153
+ ok: false,
154
+ because: `'${assertion.property}' compares this reading with the one before the action, and no before-reading was taken — nothing was compared`,
155
+ unevaluated: true,
156
+ };
157
+ }
158
+ const previous = baseline?.value;
48
159
  switch (assertion.property) {
160
+ case 'unchanged': {
161
+ const ok = sameReading(previous, value);
162
+ return {
163
+ ok,
164
+ because: ok
165
+ ? `still ${show(value)}`
166
+ : `was ${show(previous)} and is now ${show(value)} — it was required to hold its value`,
167
+ };
168
+ }
169
+ case 'changed': {
170
+ const ok = !sameReading(previous, value);
171
+ return {
172
+ ok,
173
+ because: ok
174
+ ? `${show(previous)} → ${show(value)}`
175
+ : `still ${show(value)} — nothing changed it`,
176
+ };
177
+ }
178
+ case 'increased':
179
+ case 'decreased':
180
+ return movedBy(value, previous, assertion.property, assertion.by);
49
181
  case 'nonEmpty': {
50
182
  const ok = isNonEmpty(value);
51
183
  return {
@@ -115,21 +247,3 @@ export function satisfiesProperty(value, assertion) {
115
247
  * Lives here beside the evaluator on purpose: a schema in one file and the switch that consumes it
116
248
  * in another is how a new property comes to parse and then silently never match.
117
249
  */
118
- export const propertyAssertionSchema = z.discriminatedUnion('property', [
119
- z.object({ property: z.literal('nonEmpty') }).strict(),
120
- z.object({ property: z.literal('oneOf'), values: z.array(z.unknown()).min(1) }).strict(),
121
- z
122
- .object({
123
- property: z.literal('withinTolerance'),
124
- of: z.number().finite(),
125
- tolerance: z.number().finite().nonnegative(),
126
- })
127
- .strict(),
128
- z.object({ property: z.literal('matchesPattern'), pattern: z.string().min(1) }).strict(),
129
- z
130
- .object({
131
- property: z.literal('type'),
132
- is: z.enum(['string', 'number', 'boolean', 'array', 'object']),
133
- })
134
- .strict(),
135
- ]);
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * Read what CAN be read out of a truncated JSON payload.
3
3
  *
4
- * Response bodies are capped (8192 bytes) so one enormous payload cannot blow the transport budget.
4
+ * Response bodies are capped (8192 bytes by default, `networkBodyMaxChars` to raise it) so one
5
+ * enormous payload cannot blow the transport budget.
5
6
  * The cap is right; treating what it produces as "no body" is not. Measured on a 10,000-row console:
6
7
  * the list response is 19,932 bytes, arrives truncated mid-object, `JSON.parse` throws, and every
7
8
  * body-based check — currency reconciliation, per-item failures, unit mismatches — silently reported
@@ -1,7 +1,8 @@
1
1
  /**
2
2
  * Read what CAN be read out of a truncated JSON payload.
3
3
  *
4
- * Response bodies are capped (8192 bytes) so one enormous payload cannot blow the transport budget.
4
+ * Response bodies are capped (8192 bytes by default, `networkBodyMaxChars` to raise it) so one
5
+ * enormous payload cannot blow the transport budget.
5
6
  * The cap is right; treating what it produces as "no body" is not. Measured on a 10,000-row console:
6
7
  * the list response is 19,932 bytes, arrives truncated mid-object, `JSON.parse` throws, and every
7
8
  * body-based check — currency reconciliation, per-item failures, unit mismatches — silently reported
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reticlehq/engine",
3
- "version": "3.2.0",
3
+ "version": "3.3.0",
4
4
  "description": "The rules that decide a verdict: read what happened in a running app and say whether the declared consequence held. No browser, no daemon, no command line - an implementation of the OpenVerification specification you can use on its own.",
5
5
  "keywords": [
6
6
  "verification",
@@ -56,7 +56,7 @@
56
56
  },
57
57
  "dependencies": {
58
58
  "zod": "^3.24.1",
59
- "@reticlehq/core": "3.2.0"
59
+ "@reticlehq/core": "3.3.0"
60
60
  },
61
61
  "devDependencies": {
62
62
  "open-verification": "1.0.0"