@reticlehq/engine 3.4.0 → 3.5.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.
@@ -14,6 +14,8 @@ export interface PredicateSession {
14
14
  command(name: string, args?: Record<string, unknown>, timeoutMs?: number): Promise<CommandResult>;
15
15
  eventsSince(cursor: number): ReticleEvent[];
16
16
  onEvent(listener: (event: ReticleEvent) => void): () => void;
17
+ /** Hold this wait's window against eviction while it is graded; returns the release (#668). */
18
+ protectWindow?(cursor: number): () => void;
17
19
  /** Milliseconds since connect — the same clock that stamps event `t` (injected, testable). */
18
20
  elapsed(): number;
19
21
  /**
@@ -415,6 +415,9 @@ baselines) {
415
415
  const reader = withinBudget(session, () => deadline - session.elapsed());
416
416
  return new Promise((resolve) => {
417
417
  let done = false;
418
+ // Released in `finish`, on every exit path. Without it a flood inside the window can evict the
419
+ // event the predicate is armed on, and the verdict blames the app (#668).
420
+ const releaseWindow = session.protectWindow?.(since);
418
421
  // A read that threw never looked at the app: the page did not answer, or went away. That is
419
422
  // "could not tell", not "looked and it was false", so it is inconclusive, which the verdict rule
420
423
  // turns into unknown. As a plain false it was graded assertion_failed: a command timeout on a
@@ -451,6 +454,7 @@ baselines) {
451
454
  if (done)
452
455
  return;
453
456
  done = true;
457
+ releaseWindow?.();
454
458
  unsub();
455
459
  unsubDisconnect?.();
456
460
  clearInterval(interval);
@@ -24,6 +24,20 @@ export declare class RingBuffer {
24
24
  since(cursor: number): ReticleEvent[];
25
25
  /** Events within the last `windowMs`, relative to `now`. */
26
26
  window(windowMs: number, now: number): ReticleEvent[];
27
+ /**
28
+ * Protect events at or after `cursor` from count-cap eviction until the returned function runs.
29
+ *
30
+ * Call it when a wait arms and call the result when the verdict is graded, in a `finally`: a
31
+ * leaked protection would hold the allowance open for the life of the session. Releasing twice is
32
+ * harmless.
33
+ *
34
+ * This is a bounded promise, not an absolute one. Past {@link ARMED_WINDOW_OVERFLOW} events, or
35
+ * under byte pressure, the window is evicted anyway and the loss is recorded as scarce -- so
36
+ * `lostSince` turns the verdict undecidable instead of returning the false red that pinning
37
+ * exists to prevent. Degrading into "I could not see" is right; degrading into "it did not
38
+ * happen" is the defect.
39
+ */
40
+ protect(cursor: number): () => void;
27
41
  /**
28
42
  * Did this buffer evict scarce evidence that belonged to a window opened at `cursor`?
29
43
  *
@@ -2,6 +2,21 @@ import { CHURN_TYPES, RING_BUFFER_DEFAULTS } from '@reticlehq/core';
2
2
  import { byteSizeOf } from './byte-size.js';
3
3
  /** How far forward to look for a churn event to sacrifice before falling back to plain FIFO. */
4
4
  const CHURN_SCAN_LIMIT = 256;
5
+ /**
6
+ * Extra events the count cap may hold while a verdict window is armed.
7
+ *
8
+ * A predicate's own match being evicted INSIDE the window it is being graded against is the worst
9
+ * shape of false negative: the verdict says the flow is red while the flow was green, and the
10
+ * caller cannot detect it, because `dropped` is a session-wide counter with no way to attribute a
11
+ * loss to this assertion's own `since` (#668).
12
+ *
13
+ * The buffer is time-ordered and eviction takes from the head, so "pin the armed window" is the
14
+ * same thing as "let the buffer run over its count cap rather than eat into that window". The
15
+ * allowance is a constant, so a wait that never ends cannot grow the buffer without bound, and the
16
+ * BYTE cap is deliberately left hard -- bytes are the real memory bound, and a window that floods
17
+ * past it degrades through the existing honest path rather than through this one.
18
+ */
19
+ const ARMED_WINDOW_OVERFLOW = 2000;
5
20
  /**
6
21
  * Bounded, time-aware event store per session. The single data structure that powers
7
22
  * observe/wait_for/assert — it lets us look both backward (recent buffer) and
@@ -34,6 +49,15 @@ export class RingBuffer {
34
49
  * `lostSince` and `ring-buffer-window-loss.test.ts`.
35
50
  */
36
51
  #lastScarceLossT;
52
+ /**
53
+ * Cursors of the verdict windows currently being graded, one entry per armed wait.
54
+ *
55
+ * A multiset rather than a single value: concurrent waits are ordinary (an `act_and_wait` whose
56
+ * `until` is an `allOf`, a background `wait_for`), and the buffer must protect back to the
57
+ * EARLIEST of them. Held as counts so releasing one wait does not unprotect another armed on the
58
+ * same cursor.
59
+ */
60
+ #armedWindows = new Map();
37
61
  constructor(options = {}) {
38
62
  this.#maxEvents = options.maxEvents ?? RING_BUFFER_DEFAULTS.MAX_EVENTS;
39
63
  this.#maxAgeMs = options.maxAgeMs ?? RING_BUFFER_DEFAULTS.MAX_AGE_MS;
@@ -83,7 +107,15 @@ export class RingBuffer {
83
107
  // `dropped` moved. Bytes is a soft cap and per-value serialization already bounds any one event, so
84
108
  // keeping the sole survivor over the budget is the correct trade. The count cap (maxEvents, ~2000)
85
109
  // is a hard bound and stays `> maxEvents`.
86
- while (this.#liveCount() > this.#maxEvents ||
110
+ // While a verdict window is armed, the count cap gets a bounded allowance -- but only while the
111
+ // event about to be dropped actually belongs to that window. A buffer full of events older than
112
+ // every armed cursor is evicted exactly as before, so an idle-but-armed session does not sit on
113
+ // a larger buffer for nothing.
114
+ const floor = this.#armedFloor();
115
+ const countCap = () => floor !== undefined && (this.#events[this.#head]?.t ?? -Infinity) >= floor
116
+ ? this.#maxEvents + ARMED_WINDOW_OVERFLOW
117
+ : this.#maxEvents;
118
+ while (this.#liveCount() > countCap() ||
87
119
  (this.#totalBytes > this.#maxBytes && this.#liveCount() > 1)) {
88
120
  if (CHURN_TYPES.has(this.#events[this.#head]?.type ?? '')) {
89
121
  this.#totalBytes -= this.#eventBytes[this.#head] ?? 0;
@@ -117,6 +149,42 @@ export class RingBuffer {
117
149
  this.#head = 0;
118
150
  }
119
151
  }
152
+ /**
153
+ * Protect events at or after `cursor` from count-cap eviction until the returned function runs.
154
+ *
155
+ * Call it when a wait arms and call the result when the verdict is graded, in a `finally`: a
156
+ * leaked protection would hold the allowance open for the life of the session. Releasing twice is
157
+ * harmless.
158
+ *
159
+ * This is a bounded promise, not an absolute one. Past {@link ARMED_WINDOW_OVERFLOW} events, or
160
+ * under byte pressure, the window is evicted anyway and the loss is recorded as scarce -- so
161
+ * `lostSince` turns the verdict undecidable instead of returning the false red that pinning
162
+ * exists to prevent. Degrading into "I could not see" is right; degrading into "it did not
163
+ * happen" is the defect.
164
+ */
165
+ protect(cursor) {
166
+ this.#armedWindows.set(cursor, (this.#armedWindows.get(cursor) ?? 0) + 1);
167
+ let released = false;
168
+ return () => {
169
+ if (released)
170
+ return;
171
+ released = true;
172
+ const remaining = (this.#armedWindows.get(cursor) ?? 1) - 1;
173
+ if (remaining <= 0)
174
+ this.#armedWindows.delete(cursor);
175
+ else
176
+ this.#armedWindows.set(cursor, remaining);
177
+ };
178
+ }
179
+ /** The earliest armed cursor, or `undefined` when no verdict window is open. */
180
+ #armedFloor() {
181
+ let floor;
182
+ for (const cursor of this.#armedWindows.keys()) {
183
+ if (floor === undefined || cursor < floor)
184
+ floor = cursor;
185
+ }
186
+ return floor;
187
+ }
120
188
  /** Record an eviction at `index` as scarce loss, unless it was the churn floor being sacrificed. */
121
189
  #noteScarceLoss(index) {
122
190
  const victim = this.#events[index];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reticlehq/engine",
3
- "version": "3.4.0",
3
+ "version": "3.5.0",
4
4
  "description": "The rules that decide a verdict: read what happened in a running app and say whether the declared consequence held. No browser, no daemon, no command line - an implementation of the OpenVerification specification you can use on its own.",
5
5
  "keywords": [
6
6
  "verification",
@@ -57,8 +57,8 @@
57
57
  },
58
58
  "dependencies": {
59
59
  "zod": "^3.24.1",
60
- "open-verification": "1.0.0",
61
- "@reticlehq/core": "3.4.0"
60
+ "@reticlehq/core": "3.5.0",
61
+ "open-verification": "1.0.0"
62
62
  },
63
63
  "scripts": {
64
64
  "build": "tsc -b && node ../scripts/alias-dist.mjs",