@rulvar/testing 1.14.0 → 1.15.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/index.d.ts CHANGED
@@ -36,12 +36,25 @@ declare function replayRun<A, R>(wf: Workflow<A, R>, args: A, options: ReplayRun
36
36
  * an ordinary test run.
37
37
  */
38
38
  declare function liveTestEnabled(...requiredEnvKeys: string[]): boolean;
39
+ /** Default total `runLiveSmoke` attempts including the first. */
40
+ declare const DEFAULT_LIVE_SMOKE_ATTEMPTS = 3;
41
+ /**
42
+ * Hard ceiling on `runLiveSmoke` attempts. The helper's whole contract
43
+ * is a bounded spend, so it refuses configurations that are not.
44
+ */
45
+ declare const MAX_LIVE_SMOKE_ATTEMPTS = 10;
39
46
  interface RunLiveSmokeOptions {
40
- /** Total attempts including the first (default 3, minimum 1). */
47
+ /**
48
+ * Total attempts including the first: an integer from 1 to
49
+ * {@link MAX_LIVE_SMOKE_ATTEMPTS} (default 3). Anything else, NaN and
50
+ * Infinity included, rejects with ConfigError before any stream opens.
51
+ */
41
52
  attempts?: number;
42
53
  /**
43
- * Backoff before retry n (1-based) is `baseDelayMs * n` (default
44
- * 2000). Pass 0 to retry without sleeping (unit tests).
54
+ * Backoff before retry n (1-based) is `baseDelayMs * n`: a
55
+ * non-negative integer (default 2000). Pass 0 to retry without
56
+ * sleeping (unit tests). Anything else rejects with ConfigError
57
+ * before any stream opens.
45
58
  */
46
59
  baseDelayMs?: number;
47
60
  }
@@ -67,13 +80,18 @@ type LiveSmokeOutcome = {
67
80
  status: "no-terminal";
68
81
  attempts: number;
69
82
  events: ChatEvent[];
83
+ } | {
84
+ status: "contract-violation";
85
+ attempts: number;
86
+ reason: "multiple-terminals" | "terminal-not-final";
87
+ events: ChatEvent[];
70
88
  };
71
89
  /**
72
90
  * Drains `adapter.stream(req)` with a bounded retry policy and classifies
73
91
  * the outcome instead of throwing:
74
92
  *
75
- * - `'ok'`: a `finish` event arrived (the events of the successful
76
- * attempt are included for further assertions).
93
+ * - `'ok'`: the stream ended on a single terminal `finish` (the events of
94
+ * the successful attempt are included for further assertions).
77
95
  * - `'failed'`: a terminal error with `retryable: false`; never retried,
78
96
  * diagnostics preserved.
79
97
  * - `'exhausted'`: every attempt ended in a `retryable: true` error; the
@@ -81,10 +99,18 @@ type LiveSmokeOutcome = {
81
99
  * - `'no-terminal'`: the stream ended with neither `finish` nor `error`,
82
100
  * which violates the provider SPI; never retried (spending again on a
83
101
  * misbehaving adapter is wrong).
102
+ * - `'contract-violation'`: the stream carried more than one terminal
103
+ * event (`'multiple-terminals'`, e.g. an error followed by a finish) or
104
+ * its single terminal was not the final event
105
+ * (`'terminal-not-final'`). Equally an SPI violation, equally never
106
+ * retried, and never reported as a pass.
84
107
  *
85
- * Retries only ever follow typed retryable errors, so a live smoke never
86
- * converts a real adapter failure into a pass and never spends more than
87
- * `attempts` calls.
108
+ * Retries only ever follow a well-formed stream whose single final
109
+ * terminal is a typed retryable error, so a live smoke never converts a
110
+ * real adapter failure or a malformed stream into a pass and never
111
+ * spends more than `attempts` calls. Options are validated first:
112
+ * invalid `attempts` or `baseDelayMs` reject with ConfigError before any
113
+ * adapter call.
88
114
  */
89
115
  declare function runLiveSmoke(adapter: Pick<ProviderAdapter, "stream">, req: ChatRequest, options?: RunLiveSmokeOptions): Promise<LiveSmokeOutcome>;
90
116
  //#endregion
@@ -213,4 +239,4 @@ declare function replay(options: {
213
239
  adapters?: ProviderAdapter[];
214
240
  }): ProviderAdapter[];
215
241
  //#endregion
216
- export { type CassetteFixture, type CreateTestEngineOptions, FAKE_MODEL, FAKE_MODEL_REF, FakeAdapter, type FakeAdapterOptions, type FakeCall, type FakeResponder, type FakeToolCallsValue, type FakeWireErrorValue, type LiveSmokeOutcome, M6_ORCH_GOAL, M6_ORCH_PROFILES, M6_ORCH_RUN_ID, RedactFn, type ReplayRunOptions, type RunLiveSmokeOptions, type TestEngine, type TestRunHandle, VcrCassette, VcrMissError, VcrRow, buildFrozenV1JournalRaw, buildM2CassetteFixtures, buildV2GoldenIdentity, createTestEngine, defaultRedact, fakeToolCalls, fakeWireError, handlesInRequest, liveTestEnabled, normalizeM6Entries, readCassette, record, recordLiveCassettes, recordOrchestratorCrash, replay, replayRun, requestHash, runLiveSmoke };
242
+ export { type CassetteFixture, type CreateTestEngineOptions, DEFAULT_LIVE_SMOKE_ATTEMPTS, FAKE_MODEL, FAKE_MODEL_REF, FakeAdapter, type FakeAdapterOptions, type FakeCall, type FakeResponder, type FakeToolCallsValue, type FakeWireErrorValue, type LiveSmokeOutcome, M6_ORCH_GOAL, M6_ORCH_PROFILES, M6_ORCH_RUN_ID, MAX_LIVE_SMOKE_ATTEMPTS, RedactFn, type ReplayRunOptions, type RunLiveSmokeOptions, type TestEngine, type TestRunHandle, VcrCassette, VcrMissError, VcrRow, buildFrozenV1JournalRaw, buildM2CassetteFixtures, buildV2GoldenIdentity, createTestEngine, defaultRedact, fakeToolCalls, fakeWireError, handlesInRequest, liveTestEnabled, normalizeM6Entries, readCassette, record, recordLiveCassettes, recordOrchestratorCrash, replay, replayRun, requestHash, runLiveSmoke };
package/dist/index.js CHANGED
@@ -355,6 +355,27 @@ async function replayRun(wf, args, options) {
355
355
  //#endregion
356
356
  //#region src/live.ts
357
357
  /**
358
+ * Live-test opt-in gate and the bounded live smoke.
359
+ *
360
+ * A provider key in the environment is not an opt-in: key-gated live
361
+ * tests spend provider budget, so they additionally require the explicit
362
+ * RULVAR_LIVE_TESTS=1 switch (the repository's `pnpm test:live` sets it
363
+ * for its child run only). runLiveSmoke drains one adapter stream per
364
+ * attempt and classifies the terminal event: a typed retryable error
365
+ * (429 rate limit, 529 overload, transport) is retried a bounded number
366
+ * of times with linear backoff; a non-retryable error (authentication,
367
+ * invalid model, invalid request) fails immediately with the typed
368
+ * WireError intact. The provider SPI requires exactly one terminal event
369
+ * per stream, as its final event: a stream with no terminal is
370
+ * `'no-terminal'`, one with multiple terminals or a terminal followed by
371
+ * more events is `'contract-violation'`, and neither is ever retried
372
+ * (spending again cannot repair a broken adapter contract). A stream
373
+ * that THROWS propagates unchanged: adapters surface failures as typed
374
+ * error events, so a raw throw is itself a contract violation the caller
375
+ * must see. Options are validated before any stream is opened; invalid
376
+ * values reject with ConfigError instead of being clamped or defaulted.
377
+ */
378
+ /**
358
379
  * True only when `RULVAR_LIVE_TESTS` is exactly `'1'` AND every named
359
380
  * environment key is set to a non-empty value. Gate live tests as
360
381
  * `it.skipIf(!liveTestEnabled('ANTHROPIC_API_KEY'))(...)` so an
@@ -368,12 +389,19 @@ function liveTestEnabled(...requiredEnvKeys) {
368
389
  return value !== void 0 && value !== "";
369
390
  });
370
391
  }
392
+ /** Default total `runLiveSmoke` attempts including the first. */
393
+ const DEFAULT_LIVE_SMOKE_ATTEMPTS = 3;
394
+ /**
395
+ * Hard ceiling on `runLiveSmoke` attempts. The helper's whole contract
396
+ * is a bounded spend, so it refuses configurations that are not.
397
+ */
398
+ const MAX_LIVE_SMOKE_ATTEMPTS = 10;
371
399
  /**
372
400
  * Drains `adapter.stream(req)` with a bounded retry policy and classifies
373
401
  * the outcome instead of throwing:
374
402
  *
375
- * - `'ok'`: a `finish` event arrived (the events of the successful
376
- * attempt are included for further assertions).
403
+ * - `'ok'`: the stream ended on a single terminal `finish` (the events of
404
+ * the successful attempt are included for further assertions).
377
405
  * - `'failed'`: a terminal error with `retryable: false`; never retried,
378
406
  * diagnostics preserved.
379
407
  * - `'exhausted'`: every attempt ended in a `retryable: true` error; the
@@ -381,36 +409,57 @@ function liveTestEnabled(...requiredEnvKeys) {
381
409
  * - `'no-terminal'`: the stream ended with neither `finish` nor `error`,
382
410
  * which violates the provider SPI; never retried (spending again on a
383
411
  * misbehaving adapter is wrong).
412
+ * - `'contract-violation'`: the stream carried more than one terminal
413
+ * event (`'multiple-terminals'`, e.g. an error followed by a finish) or
414
+ * its single terminal was not the final event
415
+ * (`'terminal-not-final'`). Equally an SPI violation, equally never
416
+ * retried, and never reported as a pass.
384
417
  *
385
- * Retries only ever follow typed retryable errors, so a live smoke never
386
- * converts a real adapter failure into a pass and never spends more than
387
- * `attempts` calls.
418
+ * Retries only ever follow a well-formed stream whose single final
419
+ * terminal is a typed retryable error, so a live smoke never converts a
420
+ * real adapter failure or a malformed stream into a pass and never
421
+ * spends more than `attempts` calls. Options are validated first:
422
+ * invalid `attempts` or `baseDelayMs` reject with ConfigError before any
423
+ * adapter call.
388
424
  */
389
425
  async function runLiveSmoke(adapter, req, options) {
390
- const attempts = Math.max(1, options?.attempts ?? 3);
391
- const baseDelayMs = options?.baseDelayMs ?? 2e3;
426
+ const attempts = validatedAttempts(options?.attempts);
427
+ const baseDelayMs = validatedBaseDelayMs(options?.baseDelayMs);
392
428
  const retryableErrors = [];
393
429
  for (let attempt = 1; attempt <= attempts; attempt += 1) {
394
430
  const events = [];
395
431
  for await (const event of adapter.stream(req)) events.push(event);
396
- if (events.some((event) => event.type === "finish")) return {
397
- status: "ok",
432
+ const terminals = events.filter((event) => event.type === "finish" || event.type === "error");
433
+ const terminal = terminals[0];
434
+ if (terminal === void 0) return {
435
+ status: "no-terminal",
398
436
  attempts: attempt,
399
437
  events
400
438
  };
401
- const errorEvent = events.find((event) => event.type === "error");
402
- if (errorEvent === void 0) return {
403
- status: "no-terminal",
439
+ if (terminals.length > 1) return {
440
+ status: "contract-violation",
404
441
  attempts: attempt,
442
+ reason: "multiple-terminals",
405
443
  events
406
444
  };
407
- if (!errorEvent.error.retryable) return {
445
+ if (terminal !== events.at(-1)) return {
446
+ status: "contract-violation",
447
+ attempts: attempt,
448
+ reason: "terminal-not-final",
449
+ events
450
+ };
451
+ if (terminal.type === "finish") return {
452
+ status: "ok",
453
+ attempts: attempt,
454
+ events
455
+ };
456
+ if (!terminal.error.retryable) return {
408
457
  status: "failed",
409
458
  attempts: attempt,
410
- error: errorEvent.error,
459
+ error: terminal.error,
411
460
  events
412
461
  };
413
- retryableErrors.push(errorEvent.error);
462
+ retryableErrors.push(terminal.error);
414
463
  if (attempt < attempts && baseDelayMs > 0) await delay(baseDelayMs * attempt);
415
464
  }
416
465
  return {
@@ -419,6 +468,16 @@ async function runLiveSmoke(adapter, req, options) {
419
468
  errors: retryableErrors
420
469
  };
421
470
  }
471
+ function validatedAttempts(value) {
472
+ if (value === void 0) return 3;
473
+ if (!Number.isSafeInteger(value) || value < 1 || value > 10) throw new ConfigError(`runLiveSmoke attempts must be an integer from 1 to 10, got ${String(value)}`);
474
+ return value;
475
+ }
476
+ function validatedBaseDelayMs(value) {
477
+ if (value === void 0) return 2e3;
478
+ if (!Number.isSafeInteger(value) || value < 0) throw new ConfigError(`runLiveSmoke baseDelayMs must be a non-negative integer, got ${String(value)}`);
479
+ return value;
480
+ }
422
481
  function delay(ms) {
423
482
  return new Promise((resolve) => {
424
483
  setTimeout(resolve, ms);
@@ -2089,4 +2148,4 @@ function replay(options) {
2089
2148
  });
2090
2149
  }
2091
2150
  //#endregion
2092
- export { FAKE_MODEL, FAKE_MODEL_REF, FakeAdapter, M6_ORCH_GOAL, M6_ORCH_PROFILES, M6_ORCH_RUN_ID, VcrMissError, buildFrozenV1JournalRaw, buildM2CassetteFixtures, buildV2GoldenIdentity, createTestEngine, defaultRedact, fakeToolCalls, fakeWireError, handlesInRequest, liveTestEnabled, normalizeM6Entries, readCassette, record, recordLiveCassettes, recordOrchestratorCrash, replay, replayRun, requestHash, runLiveSmoke };
2151
+ export { DEFAULT_LIVE_SMOKE_ATTEMPTS, FAKE_MODEL, FAKE_MODEL_REF, FakeAdapter, M6_ORCH_GOAL, M6_ORCH_PROFILES, M6_ORCH_RUN_ID, MAX_LIVE_SMOKE_ATTEMPTS, VcrMissError, buildFrozenV1JournalRaw, buildM2CassetteFixtures, buildV2GoldenIdentity, createTestEngine, defaultRedact, fakeToolCalls, fakeWireError, handlesInRequest, liveTestEnabled, normalizeM6Entries, readCassette, record, recordLiveCassettes, recordOrchestratorCrash, replay, replayRun, requestHash, runLiveSmoke };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulvar/testing",
3
- "version": "1.14.0",
3
+ "version": "1.15.0",
4
4
  "description": "Rulvar test harness: createTestEngine, FakeAdapter, VCR cassettes, replay-strict runs, matchers.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
@@ -26,7 +26,7 @@
26
26
  "access": "public"
27
27
  },
28
28
  "dependencies": {
29
- "@rulvar/core": "1.14.0"
29
+ "@rulvar/core": "1.15.0"
30
30
  },
31
31
  "devDependencies": {
32
32
  "@types/node": "^22.20.0",