@crvouga/mockingbird-service-resource-groups 1.2.0 → 1.4.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/server.d.ts CHANGED
@@ -144,7 +144,7 @@ type ClockState$1 = {
144
144
  now: number;
145
145
  /** True while time does not advance on its own. */
146
146
  frozen: boolean;
147
- /** Milliseconds this clock adds to its underlying source. */
147
+ /** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
148
148
  offsetMs: number;
149
149
  };
150
150
  type Clock$1 = {
@@ -180,7 +180,10 @@ type Rng$1 = {
180
180
  state(): number;
181
181
  /** Restore a state previously returned by {@link state}. */
182
182
  setState(state: number): void;
183
- seed: number;
183
+ /** Restart the stream from a new seed. `reset` then returns to this seed. */
184
+ reseed(seed: number | string): void;
185
+ /** The seed the stream currently starts from. */
186
+ readonly seed: number;
184
187
  };
185
188
 
186
189
  /**
@@ -189,44 +192,83 @@ type Rng$1 = {
189
192
  * This is how a suite reaches the vendor's failure modes without the vendor: the
190
193
  * quota error that only appears when a shared sandbox is full, the 429 that only
191
194
  * appears under load, the 5xx that proves a retry path works.
195
+ *
196
+ * A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
197
+ * it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
198
+ * and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
192
199
  */
193
200
  type FaultRule$1 = {
194
- /** Stable id, so a suite can retire exactly the rule it added. */
201
+ /** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
195
202
  id: string;
196
203
  /** Fault only this operation. Omit to match every operation. */
197
204
  operationId?: string;
198
205
  /** Fault only this HTTP method, case-insensitive. Omit to match every method. */
199
206
  method?: string;
207
+ /** Fault only this exact path. */
208
+ path?: string;
200
209
  /** Fault only paths starting with this prefix. Omit to match every path. */
201
210
  pathPrefix?: string;
211
+ /** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
212
+ pathPattern?: string;
213
+ /** Fault only requests carrying these headers. `"*"` matches any value. */
214
+ requestHeaders?: Record<string, string>;
215
+ /** Fault only requests carrying these query parameters. `"*"` matches any value. */
216
+ query?: Record<string, string>;
202
217
  /**
203
218
  * Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
204
219
  * an in-process caller usually wants, and what a parallel worker usually does not:
205
220
  * rules added through `POST /__admin/faults` default to the calling namespace.
206
221
  */
207
222
  namespace?: string;
223
+ /** Let this many matching requests through first. */
224
+ after?: number;
225
+ /** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
226
+ every?: number;
227
+ /** Retire the rule after this many faults. Omit to keep it until removed. */
228
+ count?: number;
229
+ /** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
230
+ rate?: number;
231
+ /**
232
+ * Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
233
+ * clock when the rule was added. Listed as epoch ms.
234
+ */
235
+ from?: Instant$1 | Duration$1;
236
+ /** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
237
+ until?: Instant$1 | Duration$1;
208
238
  /**
209
- * Status of the injected response. Omit for a rule that only delays (`delayMs` /
210
- * `latencyMs`), only drops the connection (`drop`), or only switches on an `effect`:
211
- * the request then still reaches the service.
239
+ * Status of the injected response. Omit for a rule that only delays, times out, drops,
240
+ * truncates, or switches on an `effect`: the request then still reaches the service.
212
241
  */
213
242
  status?: number;
214
243
  /** Response body, serialized as JSON. A string is sent as-is. */
215
244
  body?: unknown;
245
+ /** Response headers. */
216
246
  headers?: Record<string, string>;
217
- /** Retire the rule after this many faults. Omit to keep it until removed. */
218
- count?: number;
219
- /** Fault this fraction of matching requests, `0`–`1`. Default `1`. */
220
- rate?: number;
221
- /** Hold the response back this long, to exercise timeouts. */
247
+ /** Hold the response back this long. Listed as `delayMs`. */
248
+ delay?: Duration$1;
249
+ /** `delay`, in milliseconds. */
222
250
  delayMs?: number;
223
251
  /** Alias of `delayMs`. */
224
252
  latencyMs?: number;
253
+ /** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
254
+ jitter?: Duration$1;
255
+ jitterMs?: number;
256
+ /**
257
+ * Never answer: hold the request this long, then drop the connection. Listed as
258
+ * `timeoutMs`. With virtual time the wait is spent on the mock clock.
259
+ */
260
+ timeout?: Duration$1;
261
+ timeoutMs?: number;
225
262
  /**
226
263
  * Drop the connection instead of answering: an in-process `fetch` rejects with a
227
264
  * `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
228
265
  */
229
266
  drop?: boolean;
267
+ /**
268
+ * Cut the response body off after this many bytes: the status and headers arrive, then
269
+ * the connection dies mid-body.
270
+ */
271
+ truncate?: number;
230
272
  /**
231
273
  * A named service behaviour to switch on for the matching request instead of (or
232
274
  * before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
@@ -235,8 +277,8 @@ type FaultRule$1 = {
235
277
  effect?: string;
236
278
  /** Parameters for `effect`. */
237
279
  params?: Record<string, unknown>;
238
- /** From the preset this rule was expanded from, if any. */
239
- preset?: string;
280
+ /** The preset this rule was expanded from, if any. */
281
+ fromPreset?: string;
240
282
  };
241
283
  /** A fault that fired for one request. */
242
284
  type FaultHit$1 = {
@@ -244,6 +286,10 @@ type FaultHit$1 = {
244
286
  /** The injected response; absent when the rule only delays, drops, or sets an effect. */
245
287
  response?: Response;
246
288
  drop?: boolean;
289
+ /** Set with `drop` when the rule held the request for its `timeout` first. */
290
+ timeout?: boolean;
291
+ /** Bytes of response body to let through before the connection dies. */
292
+ truncate?: number;
247
293
  effect?: {
248
294
  name: string;
249
295
  params: Record<string, unknown>;
@@ -255,38 +301,202 @@ type FaultCandidate$1 = {
255
301
  method: string;
256
302
  path: string;
257
303
  namespace: string;
304
+ /** Request headers and query, for rules that match on them. */
305
+ headers?: Headers;
306
+ query?: URLSearchParams;
307
+ /** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
308
+ now?: number;
309
+ /** How to spend a delay for this request. Default: the registry's own sleep. */
310
+ wait?: (ms: number) => Promise<void>;
311
+ };
312
+ /** A stored rule and how often it was consulted. */
313
+ type FaultListing$1 = FaultRule$1 & {
314
+ /** Faults left before the rule retires. `null` when it has no `count`. */
315
+ remaining: number | null;
316
+ /** Times the rule faulted a request. */
317
+ hits: number;
318
+ /** Requests that matched the rule, faulted or not. */
319
+ seen: number;
320
+ };
321
+ /** A namespace's own rules, with their counters, in the order they are tried. */
322
+ type FaultSnapshot$1 = {
323
+ /**
324
+ * `position` is how many `"*"` rules are tried before the rule, which is what its place
325
+ * in the order means to a request and holds in any process.
326
+ */
327
+ rules: (FaultListing$1 & {
328
+ position?: number;
329
+ })[];
330
+ /** How many `fault_<n>` ids the namespace has handed out. */
331
+ nextId?: number;
258
332
  };
259
333
  type FaultRegistry$1 = {
260
- isolateNamespaces(): void;
261
- snapshot(namespace: string): {
262
- rules: (FaultRule$1 & {
263
- remaining: number | null;
264
- hits: number;
265
- position: number;
266
- })[];
267
- rngState: number;
268
- };
334
+ snapshot(namespace: string): FaultSnapshot$1;
335
+ /** Add a rule, or replace the rule with the same id in the same namespace. */
269
336
  add(rule: FaultRule$1): FaultRule$1;
270
- list(): (FaultRule$1 & {
271
- remaining: number | null;
272
- hits: number;
273
- })[];
274
- remove(id: string): boolean;
275
- clear(): void;
276
- restore(namespace: string, state: {
277
- rules: (FaultRule$1 & {
278
- remaining: number | null;
279
- hits: number;
280
- position: number;
281
- })[];
282
- rngState: number;
283
- }): void;
337
+ /** The next unused `fault_<n>` id in `namespace`. */
338
+ nextId(namespace: string): string;
339
+ /** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
340
+ list(namespace?: string): FaultListing$1[];
341
+ get(id: string, namespace?: string): FaultListing$1 | undefined;
342
+ /** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
343
+ remove(id: string, namespace?: string): boolean;
344
+ /** Remove every rule, or one namespace's own rules. */
345
+ clear(namespace?: string): void;
346
+ /** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
347
+ restore(namespace: string, state: FaultSnapshot$1): void;
284
348
  /**
285
349
  * Every fault this request should get, in rule order, stopping at the first that answers
286
350
  * or drops (effect-only and delay-only rules let later rules match too). Consumes one of
287
351
  * each matching rule's remaining uses.
288
352
  */
289
353
  take(candidate: FaultCandidate$1): Promise<FaultHit$1[]>;
354
+ /**
355
+ * Fire these rules at one request without storing them: no matching, no schedule, in order,
356
+ * stopping at the first that answers or drops. Jitter draws from the namespace's stream.
357
+ */
358
+ fire(rules: readonly FaultRule$1[], candidate: FaultCandidate$1): Promise<FaultHit$1[]>;
359
+ };
360
+
361
+ /**
362
+ * The admin controls every mock shares, as one document.
363
+ *
364
+ * The same shape is accepted in three places, and means the same thing in each:
365
+ *
366
+ * - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
367
+ * `MOCKINGBIRD_ADMIN_CONTROLS`
368
+ * - over HTTP: `PUT /__admin/controls`
369
+ * - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
370
+ *
371
+ * Every control is deterministic: given the same seed, clock and requests, a mock answers
372
+ * the same bytes. Nothing here reads the wall clock or `Math.random`.
373
+ */
374
+ /** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
375
+ type Duration$1 = number | string;
376
+ /** Epoch milliseconds, or an ISO-8601 string. */
377
+ type Instant$1 = number | string;
378
+ type ClockControls$1 = {
379
+ /** Move the clock to this instant. */
380
+ now?: Instant$1;
381
+ /** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
382
+ frozen?: boolean;
383
+ /**
384
+ * Step the clock forward by this much before every vendor request. With a frozen clock
385
+ * each request still gets its own, reproducible timestamp.
386
+ */
387
+ autoAdvance?: Duration$1;
388
+ };
389
+ /** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
390
+ type LatencyRuleInput$1 = {
391
+ delay?: Duration$1;
392
+ jitter?: Duration$1;
393
+ };
394
+ type LatencyControls$1 = LatencyRuleInput$1 & {
395
+ /** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
396
+ operations?: Record<string, Duration$1 | LatencyRuleInput$1>;
397
+ };
398
+ /** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
399
+ type FaultInput$1 = (Omit<FaultRule$1, "id"> & {
400
+ id?: string;
401
+ }) | ({
402
+ preset: string;
403
+ } & Partial<FaultRule$1>);
404
+ /** Identifies a {@link SnapshotDocument}. */
405
+ declare const SNAPSHOT_FORMAT$1 = "mockingbird.snapshot/v1";
406
+ /**
407
+ * A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
408
+ * `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
409
+ * `records` are required, so a hand-written seed file is a valid snapshot.
410
+ */
411
+ type SnapshotDocument$1 = {
412
+ format: typeof SNAPSHOT_FORMAT$1;
413
+ /** The service that wrote it. A snapshot is rejected by any other service. */
414
+ service?: string;
415
+ /**
416
+ * Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
417
+ * the stored text of a row that is not a plain record, and replaces `value` when present.
418
+ */
419
+ records: {
420
+ collection: string;
421
+ id: string;
422
+ seq?: number;
423
+ value?: unknown;
424
+ raw?: string;
425
+ }[];
426
+ /** Id counters, so the next generated id continues where the snapshot left off. */
427
+ sequences?: {
428
+ name: string;
429
+ kind: string;
430
+ value: number;
431
+ }[];
432
+ clock?: {
433
+ now: number;
434
+ frozen: boolean;
435
+ autoAdvanceMs?: number;
436
+ };
437
+ seed?: number | string;
438
+ /** Positions of the seeded random streams. */
439
+ rng?: {
440
+ state: number;
441
+ faults?: number;
442
+ latency?: number;
443
+ };
444
+ faults?: unknown;
445
+ latency?: unknown;
446
+ virtualTime?: boolean;
447
+ /** Quiescent webhook endpoints, delivery history and fault state. Contains private configuration. */
448
+ webhooks?: unknown;
449
+ };
450
+ /** Controls for one namespace. */
451
+ type NamespaceControls$1 = {
452
+ /** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
453
+ seed?: number | string;
454
+ clock?: ClockControls$1;
455
+ /**
456
+ * Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
457
+ * wall clock: the request returns at once, the clock has moved, and the journal records
458
+ * the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
459
+ */
460
+ virtualTime?: boolean;
461
+ /** Baseline response time for every vendor request. A bare duration is its `delay`. */
462
+ latency?: Duration$1 | LatencyControls$1;
463
+ faults?: readonly FaultInput$1[];
464
+ /** Start from this state instead of the service's own seed data. */
465
+ snapshot?: SnapshotDocument$1;
466
+ };
467
+ /** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
468
+ type MockControls$1 = NamespaceControls$1 & {
469
+ /** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
470
+ credentials?: Record<string, string>;
471
+ /** Overrides for named namespaces. A key given here replaces the shared value. */
472
+ namespaces?: Record<string, NamespaceControls$1>;
473
+ };
474
+ /** A latency rule in milliseconds. */
475
+ type LatencyRule$1 = {
476
+ delayMs: number;
477
+ jitterMs: number;
478
+ };
479
+ /** Baseline latency for a namespace, normalized. */
480
+ type LatencyState$1 = LatencyRule$1 & {
481
+ operations: Record<string, LatencyRule$1>;
482
+ };
483
+ /** What {@link controlHeaders} turns into request headers. */
484
+ type ControlHeaderInput$1 = {
485
+ namespace?: string;
486
+ adminKey?: string;
487
+ branch?: string;
488
+ at?: string;
489
+ reset?: boolean;
490
+ restore?: string;
491
+ seed?: number | string;
492
+ time?: Instant$1 | Date;
493
+ advance?: Duration$1;
494
+ virtualTime?: boolean;
495
+ delay?: Duration$1;
496
+ timeout?: Duration$1;
497
+ /** A status, `"drop"`, a preset name, or a fault rule. */
498
+ fault?: number | string | Record<string, unknown>;
499
+ snapshot?: string;
290
500
  };
291
501
 
292
502
  /**
@@ -326,6 +536,8 @@ type RequestLog$1 = {
326
536
  unmatched: boolean;
327
537
  /** Set when a fault rule produced the response. */
328
538
  faultId?: string;
539
+ /** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
540
+ delayMs?: number;
329
541
  /** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
330
542
  ids?: Record<string, string>;
331
543
  /** Set when the service created a resource the request referred to but that did not exist. */
@@ -371,6 +583,12 @@ type JournalQuery$1 = {
371
583
  /** Only this namespace. Omit for every namespace, oldest first across all of them. */
372
584
  namespace?: string;
373
585
  operationId?: string;
586
+ /** Only this HTTP method, case-insensitive. */
587
+ method?: string;
588
+ /** Only this exact path. */
589
+ path?: string;
590
+ /** Only requests this fault rule answered or shaped. */
591
+ faultId?: string;
374
592
  status?: number;
375
593
  /** Only entries at or after this instant (epoch ms). */
376
594
  since?: number;
@@ -385,6 +603,50 @@ type Journal$1 = {
385
603
  clear(namespace?: string): void;
386
604
  };
387
605
 
606
+ /**
607
+ * Baseline response time, per namespace: how long every vendor request takes before any
608
+ * fault is considered. Jitter draws from a stream the caller owns, one per namespace and
609
+ * separate from the fault stream, so adding latency never shifts which requests a `rate`
610
+ * fault hits.
611
+ */
612
+ type LatencyRegistry$1 = {
613
+ get(namespace: string): LatencyState$1;
614
+ set(namespace: string, state: LatencyState$1): LatencyState$1;
615
+ /** Remove one namespace's latency, or every namespace's. */
616
+ clear(namespace?: string): void;
617
+ /** Milliseconds this request waits. Draws jitter, so call it once per request. */
618
+ take(namespace: string, operationId: string | undefined): number;
619
+ };
620
+
621
+ /** Clock state as the admin API reports it. */
622
+ type ControlClock$1 = ClockState$1 & {
623
+ /** `now`, as ISO-8601. */
624
+ iso: string;
625
+ /** Milliseconds the clock steps before every vendor request. */
626
+ autoAdvanceMs: number;
627
+ };
628
+ /** One namespace's controls: the body of `GET /__admin/controls`. */
629
+ type ControlState$1 = {
630
+ namespace: string;
631
+ seed: number | string;
632
+ clock: ControlClock$1;
633
+ virtualTime: boolean;
634
+ latency: LatencyState$1;
635
+ faults: FaultListing$1[];
636
+ };
637
+
638
+ /** What `GET /__admin/snapshots` lists for each snapshot. */
639
+ type SnapshotInfo$1 = {
640
+ id: string;
641
+ namespace: string;
642
+ /** Stored rows in the snapshot. */
643
+ records: number;
644
+ /** The namespace clock when it was taken, epoch ms. */
645
+ at: number;
646
+ /** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
647
+ portable: boolean;
648
+ };
649
+
388
650
  /** Credential → namespace mapping behind `PUT /__admin/credentials`. */
389
651
  type CredentialRegistry$1 = {
390
652
  set(credential: string, namespace: string): void;
@@ -397,6 +659,8 @@ type CredentialRegistry$1 = {
397
659
  }[];
398
660
  };
399
661
 
662
+ type OwnedTransport$1 = (request: Request, namespace: string, send: typeof fetch) => Promise<Response>;
663
+
400
664
  /**
401
665
  * A point-in-time copy of everything a service namespace holds.
402
666
  *
@@ -514,8 +778,16 @@ type PublishInput$1 = {
514
778
  id?: string;
515
779
  };
516
780
  type WebhookHub$1 = {
781
+ observeDeliveries(listener: (event: {
782
+ namespace: string;
783
+ state: string;
784
+ deliveryId: string;
785
+ }) => void): () => void;
786
+ setTransportPolicy(policy: OwnedTransport$1): void;
517
787
  pending(namespace: string): number;
518
788
  snapshot(namespace: string): unknown;
789
+ exportSnapshot(handle: unknown): unknown;
790
+ importSnapshot(namespace: string, document: unknown): unknown;
519
791
  restore(namespace: string, snapshot: unknown): void;
520
792
  publish(input: PublishInput$1): WebhookMessage$1;
521
793
  /** Replace a namespace's own endpoints (`PUT /__admin/webhook-endpoints`). */
@@ -528,7 +800,7 @@ type WebhookHub$1 = {
528
800
  /** Run every pending retry (and release held reordered messages) now, and every retry those attempts schedule, until nothing is pending. */
529
801
  flush(): Promise<void>;
530
802
  /** Resolve once nothing is in flight. */
531
- idle(): Promise<void>;
803
+ idle(namespace?: string): Promise<void>;
532
804
  fault(namespace: string, fault: WebhookFault$1): void;
533
805
  clear(namespace?: string): void;
534
806
  };
@@ -539,7 +811,10 @@ type ServiceInstance$1 = FetchAPI$1 & {
539
811
  };
540
812
  type ServiceTimelineState$1 = Readonly<{
541
813
  snapshot: NamespaceSnapshot$1;
542
- clock: Readonly<ReturnType<Clock$1["state"]>>;
814
+ /** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
815
+ clock: Readonly<ClockState$1 & {
816
+ pinned?: boolean;
817
+ }>;
543
818
  rngState: number;
544
819
  }>;
545
820
  type ServiceCheckpoint$1 = Checkpoint$1<ServiceTimelineState$1>;
@@ -552,27 +827,63 @@ type ServiceRuntime$1<T extends ServiceInstance$1> = FetchAPI$1 & {
552
827
  readonly sqlite: SqliteClient$2;
553
828
  readonly clock: Clock$1;
554
829
  readonly faults: FaultRegistry$1;
830
+ /** Baseline response time per namespace. */
831
+ readonly latency: LatencyRegistry$1;
555
832
  readonly metrics: Metrics$1;
556
833
  readonly journal: Journal$1;
557
834
  readonly rng: Rng$1;
558
835
  readonly credentials: CredentialRegistry$1;
559
836
  /** The webhook hub, when the service has outbound webhooks. */
560
837
  readonly webhooks: WebhookHub$1 | undefined;
561
- /** Enable independent namespace clocks and PRNGs before serving a fleet. */
562
- isolateNamespaces(): void;
838
+ /**
839
+ * A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
840
+ * offset and a freeze of its own, so moving one never moves another.
841
+ */
563
842
  namespaceClock(namespace?: string): Clock$1;
564
843
  namespaceOf(request: Request): string;
565
844
  /** Opaque in-process checkpoint including records, controls and webhook history. */
566
845
  fleetSnapshot(namespace: string): object;
567
846
  fleetRestore(snapshot: unknown, namespace: string): void;
847
+ fleetExport(snapshot: unknown): SnapshotDocument$1;
848
+ fleetImport(namespace: string, document: unknown): object;
568
849
  /** Expand a named preset into fault rules (and webhook faults) for `namespace`. */
569
850
  applyPreset(name: string, namespace?: string, overrides?: Partial<FaultRule$1>): FaultRule$1[];
570
851
  /** The instance behind `namespace` (the default one when omitted), created on first use. */
571
852
  instance(namespace?: string): T;
572
853
  /** Public names of every namespace created so far. */
573
854
  namespaces(): string[];
574
- /** Reset one namespace, or every namespace with `"*"`. */
855
+ /** Return one namespace to its boot state, or every namespace with `"*"`. */
575
856
  reset(namespace?: string): Promise<void>;
857
+ /** Reset a namespace and forget it, releasing everything it held. */
858
+ dropNamespace(namespace: string): Promise<void>;
859
+ /** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
860
+ controls(namespace?: string): ControlState$1 & {
861
+ snapshots: SnapshotInfo$1[];
862
+ };
863
+ /** Apply a control document to a namespace. A given key replaces that control. */
864
+ configure(controls: NamespaceControls$1, namespace?: string): ControlState$1 & {
865
+ snapshots: SnapshotInfo$1[];
866
+ };
867
+ /** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
868
+ readonly snapshots: {
869
+ /** Capture the namespace now. Reusing a name replaces that snapshot. */
870
+ create(options?: {
871
+ name?: string;
872
+ namespace?: string;
873
+ }): SnapshotInfo$1;
874
+ list(namespace?: string): SnapshotInfo$1[];
875
+ /** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
876
+ export(id: string, namespace?: string): SnapshotDocument$1;
877
+ /** Store a document as a snapshot without restoring it. */
878
+ import(document: SnapshotDocument$1, options?: {
879
+ name?: string;
880
+ namespace?: string;
881
+ }): SnapshotInfo$1;
882
+ restore(id: string, namespace?: string): void;
883
+ delete(id: string, namespace?: string): boolean;
884
+ };
885
+ /** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
886
+ headers(input: ControlHeaderInput$1): Record<string, string>;
576
887
  snapshot(namespace?: string): NamespaceSnapshot$1;
577
888
  restore(snapshot: NamespaceSnapshot$1, namespace?: string): void;
578
889
  /** Capture the current branch. Mutating HTTP calls do this automatically. */
@@ -615,6 +926,8 @@ type CommonServeOptions = {
615
926
  adminKey: string | undefined;
616
927
  seed: string | undefined;
617
928
  onLog: ((entry: RequestLog$1) => void) | undefined;
929
+ /** Admin controls to boot with: clock, latency, faults, a starting snapshot. */
930
+ adminControls?: MockControls$1;
618
931
  /** Synthetic provider fixture objects supplied by a fleet entry. */
619
932
  fixtures?: Record<string, unknown>;
620
933
  baseUrl?: string;
@@ -625,6 +938,7 @@ type CommonServeOptions = {
625
938
  * `serveTarget`, which is also how `serve --config` finds services by name.
626
939
  */
627
940
  type ServeTarget = {
941
+ packageVersion?: string;
628
942
  name: string;
629
943
  defaultPort: number;
630
944
  /** Serve flags beyond the common ones. */
@@ -882,7 +1196,7 @@ type ClockState = {
882
1196
  now: number;
883
1197
  /** True while time does not advance on its own. */
884
1198
  frozen: boolean;
885
- /** Milliseconds this clock adds to its underlying source. */
1199
+ /** Milliseconds a running clock adds to its underlying source. `0` while frozen. */
886
1200
  offsetMs: number;
887
1201
  };
888
1202
  type Clock = {
@@ -918,7 +1232,10 @@ type Rng = {
918
1232
  state(): number;
919
1233
  /** Restore a state previously returned by {@link state}. */
920
1234
  setState(state: number): void;
921
- seed: number;
1235
+ /** Restart the stream from a new seed. `reset` then returns to this seed. */
1236
+ reseed(seed: number | string): void;
1237
+ /** The seed the stream currently starts from. */
1238
+ readonly seed: number;
922
1239
  };
923
1240
 
924
1241
  /**
@@ -927,44 +1244,83 @@ type Rng = {
927
1244
  * This is how a suite reaches the vendor's failure modes without the vendor: the
928
1245
  * quota error that only appears when a shared sandbox is full, the 429 that only
929
1246
  * appears under load, the 5xx that proves a retry path works.
1247
+ *
1248
+ * A rule has three parts. What it **matches** (`operationId`, `method`, `path`, …), **when**
1249
+ * it fires among the matching requests (`after`, `every`, `count`, `rate`, `from`, `until`),
1250
+ * and what it **does** (`status`, `delay`, `timeout`, `drop`, `truncate`, `effect`).
930
1251
  */
931
1252
  type FaultRule = {
932
- /** Stable id, so a suite can retire exactly the rule it added. */
1253
+ /** Stable id, so a suite can retire exactly the rule it added. Unique within a namespace. */
933
1254
  id: string;
934
1255
  /** Fault only this operation. Omit to match every operation. */
935
1256
  operationId?: string;
936
1257
  /** Fault only this HTTP method, case-insensitive. Omit to match every method. */
937
1258
  method?: string;
1259
+ /** Fault only this exact path. */
1260
+ path?: string;
938
1261
  /** Fault only paths starting with this prefix. Omit to match every path. */
939
1262
  pathPrefix?: string;
1263
+ /** Fault only paths matching this regular expression (JavaScript syntax, unanchored). */
1264
+ pathPattern?: string;
1265
+ /** Fault only requests carrying these headers. `"*"` matches any value. */
1266
+ requestHeaders?: Record<string, string>;
1267
+ /** Fault only requests carrying these query parameters. `"*"` matches any value. */
1268
+ query?: Record<string, string>;
940
1269
  /**
941
1270
  * Fault only this namespace. Omit (or `"*"`) to fault every namespace — which is what
942
1271
  * an in-process caller usually wants, and what a parallel worker usually does not:
943
1272
  * rules added through `POST /__admin/faults` default to the calling namespace.
944
1273
  */
945
1274
  namespace?: string;
1275
+ /** Let this many matching requests through first. */
1276
+ after?: number;
1277
+ /** Then fault only every nth matching request: `3` faults the 3rd, 6th, 9th. */
1278
+ every?: number;
1279
+ /** Retire the rule after this many faults. Omit to keep it until removed. */
1280
+ count?: number;
1281
+ /** Fault this fraction of the remaining requests, `0`–`1`, drawn from the seed. Default `1`. */
1282
+ rate?: number;
1283
+ /**
1284
+ * Fault only from this instant on the namespace clock. A duration (`"5m"`) counts from the
1285
+ * clock when the rule was added. Listed as epoch ms.
1286
+ */
1287
+ from?: Instant | Duration;
1288
+ /** Stop faulting at this instant on the namespace clock. A duration counts from when added. */
1289
+ until?: Instant | Duration;
946
1290
  /**
947
- * Status of the injected response. Omit for a rule that only delays (`delayMs` /
948
- * `latencyMs`), only drops the connection (`drop`), or only switches on an `effect`:
949
- * the request then still reaches the service.
1291
+ * Status of the injected response. Omit for a rule that only delays, times out, drops,
1292
+ * truncates, or switches on an `effect`: the request then still reaches the service.
950
1293
  */
951
1294
  status?: number;
952
1295
  /** Response body, serialized as JSON. A string is sent as-is. */
953
1296
  body?: unknown;
1297
+ /** Response headers. */
954
1298
  headers?: Record<string, string>;
955
- /** Retire the rule after this many faults. Omit to keep it until removed. */
956
- count?: number;
957
- /** Fault this fraction of matching requests, `0`–`1`. Default `1`. */
958
- rate?: number;
959
- /** Hold the response back this long, to exercise timeouts. */
1299
+ /** Hold the response back this long. Listed as `delayMs`. */
1300
+ delay?: Duration;
1301
+ /** `delay`, in milliseconds. */
960
1302
  delayMs?: number;
961
1303
  /** Alias of `delayMs`. */
962
1304
  latencyMs?: number;
1305
+ /** Extra seeded delay in `[0, jitter]`. Listed as `jitterMs`. */
1306
+ jitter?: Duration;
1307
+ jitterMs?: number;
1308
+ /**
1309
+ * Never answer: hold the request this long, then drop the connection. Listed as
1310
+ * `timeoutMs`. With virtual time the wait is spent on the mock clock.
1311
+ */
1312
+ timeout?: Duration;
1313
+ timeoutMs?: number;
963
1314
  /**
964
1315
  * Drop the connection instead of answering: an in-process `fetch` rejects with a
965
1316
  * `TypeError`, and a served mock destroys the socket. Models "unknown outcome" failures.
966
1317
  */
967
1318
  drop?: boolean;
1319
+ /**
1320
+ * Cut the response body off after this many bytes: the status and headers arrive, then
1321
+ * the connection dies mid-body.
1322
+ */
1323
+ truncate?: number;
968
1324
  /**
969
1325
  * A named service behaviour to switch on for the matching request instead of (or
970
1326
  * before) a canned response, e.g. `created_but_500` or `numeric_tracking_id`. Services
@@ -973,8 +1329,8 @@ type FaultRule = {
973
1329
  effect?: string;
974
1330
  /** Parameters for `effect`. */
975
1331
  params?: Record<string, unknown>;
976
- /** From the preset this rule was expanded from, if any. */
977
- preset?: string;
1332
+ /** The preset this rule was expanded from, if any. */
1333
+ fromPreset?: string;
978
1334
  };
979
1335
  /** A fault that fired for one request. */
980
1336
  type FaultHit = {
@@ -982,6 +1338,10 @@ type FaultHit = {
982
1338
  /** The injected response; absent when the rule only delays, drops, or sets an effect. */
983
1339
  response?: Response;
984
1340
  drop?: boolean;
1341
+ /** Set with `drop` when the rule held the request for its `timeout` first. */
1342
+ timeout?: boolean;
1343
+ /** Bytes of response body to let through before the connection dies. */
1344
+ truncate?: number;
985
1345
  effect?: {
986
1346
  name: string;
987
1347
  params: Record<string, unknown>;
@@ -993,38 +1353,202 @@ type FaultCandidate = {
993
1353
  method: string;
994
1354
  path: string;
995
1355
  namespace: string;
1356
+ /** Request headers and query, for rules that match on them. */
1357
+ headers?: Headers;
1358
+ query?: URLSearchParams;
1359
+ /** The namespace clock, for `from` / `until`. Rules with a window are skipped without it. */
1360
+ now?: number;
1361
+ /** How to spend a delay for this request. Default: the registry's own sleep. */
1362
+ wait?: (ms: number) => Promise<void>;
1363
+ };
1364
+ /** A stored rule and how often it was consulted. */
1365
+ type FaultListing = FaultRule & {
1366
+ /** Faults left before the rule retires. `null` when it has no `count`. */
1367
+ remaining: number | null;
1368
+ /** Times the rule faulted a request. */
1369
+ hits: number;
1370
+ /** Requests that matched the rule, faulted or not. */
1371
+ seen: number;
1372
+ };
1373
+ /** A namespace's own rules, with their counters, in the order they are tried. */
1374
+ type FaultSnapshot = {
1375
+ /**
1376
+ * `position` is how many `"*"` rules are tried before the rule, which is what its place
1377
+ * in the order means to a request and holds in any process.
1378
+ */
1379
+ rules: (FaultListing & {
1380
+ position?: number;
1381
+ })[];
1382
+ /** How many `fault_<n>` ids the namespace has handed out. */
1383
+ nextId?: number;
996
1384
  };
997
1385
  type FaultRegistry = {
998
- isolateNamespaces(): void;
999
- snapshot(namespace: string): {
1000
- rules: (FaultRule & {
1001
- remaining: number | null;
1002
- hits: number;
1003
- position: number;
1004
- })[];
1005
- rngState: number;
1006
- };
1386
+ snapshot(namespace: string): FaultSnapshot;
1387
+ /** Add a rule, or replace the rule with the same id in the same namespace. */
1007
1388
  add(rule: FaultRule): FaultRule;
1008
- list(): (FaultRule & {
1009
- remaining: number | null;
1010
- hits: number;
1011
- })[];
1012
- remove(id: string): boolean;
1013
- clear(): void;
1014
- restore(namespace: string, state: {
1015
- rules: (FaultRule & {
1016
- remaining: number | null;
1017
- hits: number;
1018
- position: number;
1019
- })[];
1020
- rngState: number;
1021
- }): void;
1389
+ /** The next unused `fault_<n>` id in `namespace`. */
1390
+ nextId(namespace: string): string;
1391
+ /** Every rule, or the rules a namespace's requests can hit: its own and the global ones. */
1392
+ list(namespace?: string): FaultListing[];
1393
+ get(id: string, namespace?: string): FaultListing | undefined;
1394
+ /** Remove a rule by id: from `namespace` (its own, then a global one), or wherever it is. */
1395
+ remove(id: string, namespace?: string): boolean;
1396
+ /** Remove every rule, or one namespace's own rules. */
1397
+ clear(namespace?: string): void;
1398
+ /** Replace a namespace's own rules with a snapshot's, wherever the snapshot was taken. */
1399
+ restore(namespace: string, state: FaultSnapshot): void;
1022
1400
  /**
1023
1401
  * Every fault this request should get, in rule order, stopping at the first that answers
1024
1402
  * or drops (effect-only and delay-only rules let later rules match too). Consumes one of
1025
1403
  * each matching rule's remaining uses.
1026
1404
  */
1027
1405
  take(candidate: FaultCandidate): Promise<FaultHit[]>;
1406
+ /**
1407
+ * Fire these rules at one request without storing them: no matching, no schedule, in order,
1408
+ * stopping at the first that answers or drops. Jitter draws from the namespace's stream.
1409
+ */
1410
+ fire(rules: readonly FaultRule[], candidate: FaultCandidate): Promise<FaultHit[]>;
1411
+ };
1412
+
1413
+ /**
1414
+ * The admin controls every mock shares, as one document.
1415
+ *
1416
+ * The same shape is accepted in three places, and means the same thing in each:
1417
+ *
1418
+ * - at boot: `createRuntime({ adminControls })`, `serve --admin-controls`,
1419
+ * `MOCKINGBIRD_ADMIN_CONTROLS`
1420
+ * - over HTTP: `PUT /__admin/controls`
1421
+ * - per request: the `x-mockingbird-*` headers in {@link CONTROL_HEADERS}
1422
+ *
1423
+ * Every control is deterministic: given the same seed, clock and requests, a mock answers
1424
+ * the same bytes. Nothing here reads the wall clock or `Math.random`.
1425
+ */
1426
+ /** Milliseconds, or a string like `"250ms"`, `"2s"`, `"15m"`, `"1h"`, `"3d"`. */
1427
+ type Duration = number | string;
1428
+ /** Epoch milliseconds, or an ISO-8601 string. */
1429
+ type Instant = number | string;
1430
+ type ClockControls = {
1431
+ /** Move the clock to this instant. */
1432
+ now?: Instant;
1433
+ /** Stop time. A frozen clock moves only when told to, so timestamps replay exactly. */
1434
+ frozen?: boolean;
1435
+ /**
1436
+ * Step the clock forward by this much before every vendor request. With a frozen clock
1437
+ * each request still gets its own, reproducible timestamp.
1438
+ */
1439
+ autoAdvance?: Duration;
1440
+ };
1441
+ /** How long a response takes: a fixed delay plus seeded jitter in `[0, jitter]`. */
1442
+ type LatencyRuleInput = {
1443
+ delay?: Duration;
1444
+ jitter?: Duration;
1445
+ };
1446
+ type LatencyControls = LatencyRuleInput & {
1447
+ /** Per-operation overrides, keyed by operationId. A bare duration is its `delay`. */
1448
+ operations?: Record<string, Duration | LatencyRuleInput>;
1449
+ };
1450
+ /** A fault to add: a rule (the id is assigned when omitted), or a named preset with overrides. */
1451
+ type FaultInput = (Omit<FaultRule, "id"> & {
1452
+ id?: string;
1453
+ }) | ({
1454
+ preset: string;
1455
+ } & Partial<FaultRule>);
1456
+ /** Identifies a {@link SnapshotDocument}. */
1457
+ declare const SNAPSHOT_FORMAT = "mockingbird.snapshot/v1";
1458
+ /**
1459
+ * A namespace, as portable JSON: what `GET /__admin/snapshots/:id` returns and what
1460
+ * `PUT /__admin/snapshots/:id` and the boot `snapshot` control accept. Only `format` and
1461
+ * `records` are required, so a hand-written seed file is a valid snapshot.
1462
+ */
1463
+ type SnapshotDocument = {
1464
+ format: typeof SNAPSHOT_FORMAT;
1465
+ /** The service that wrote it. A snapshot is rejected by any other service. */
1466
+ service?: string;
1467
+ /**
1468
+ * Stored rows. `seq` orders a collection; omitted, rows keep their listed order. `raw` is
1469
+ * the stored text of a row that is not a plain record, and replaces `value` when present.
1470
+ */
1471
+ records: {
1472
+ collection: string;
1473
+ id: string;
1474
+ seq?: number;
1475
+ value?: unknown;
1476
+ raw?: string;
1477
+ }[];
1478
+ /** Id counters, so the next generated id continues where the snapshot left off. */
1479
+ sequences?: {
1480
+ name: string;
1481
+ kind: string;
1482
+ value: number;
1483
+ }[];
1484
+ clock?: {
1485
+ now: number;
1486
+ frozen: boolean;
1487
+ autoAdvanceMs?: number;
1488
+ };
1489
+ seed?: number | string;
1490
+ /** Positions of the seeded random streams. */
1491
+ rng?: {
1492
+ state: number;
1493
+ faults?: number;
1494
+ latency?: number;
1495
+ };
1496
+ faults?: unknown;
1497
+ latency?: unknown;
1498
+ virtualTime?: boolean;
1499
+ /** Quiescent webhook endpoints, delivery history and fault state. Contains private configuration. */
1500
+ webhooks?: unknown;
1501
+ };
1502
+ /** Controls for one namespace. */
1503
+ type NamespaceControls = {
1504
+ /** Seeds every random choice: fault rates, jitter, and whatever the service invents. */
1505
+ seed?: number | string;
1506
+ clock?: ClockControls;
1507
+ /**
1508
+ * Spend injected waits (latency, fault delays, timeouts) on the mock clock instead of the
1509
+ * wall clock: the request returns at once, the clock has moved, and the journal records
1510
+ * the wait exactly. Default `false`: waits are real, so a client's own timeout can fire.
1511
+ */
1512
+ virtualTime?: boolean;
1513
+ /** Baseline response time for every vendor request. A bare duration is its `delay`. */
1514
+ latency?: Duration | LatencyControls;
1515
+ faults?: readonly FaultInput[];
1516
+ /** Start from this state instead of the service's own seed data. */
1517
+ snapshot?: SnapshotDocument;
1518
+ };
1519
+ /** {@link NamespaceControls} for every namespace, with per-namespace overrides. */
1520
+ type MockControls = NamespaceControls & {
1521
+ /** Vendor credential → namespace, for SDKs that cannot send a namespace header. */
1522
+ credentials?: Record<string, string>;
1523
+ /** Overrides for named namespaces. A key given here replaces the shared value. */
1524
+ namespaces?: Record<string, NamespaceControls>;
1525
+ };
1526
+ /** A latency rule in milliseconds. */
1527
+ type LatencyRule = {
1528
+ delayMs: number;
1529
+ jitterMs: number;
1530
+ };
1531
+ /** Baseline latency for a namespace, normalized. */
1532
+ type LatencyState = LatencyRule & {
1533
+ operations: Record<string, LatencyRule>;
1534
+ };
1535
+ /** What {@link controlHeaders} turns into request headers. */
1536
+ type ControlHeaderInput = {
1537
+ namespace?: string;
1538
+ adminKey?: string;
1539
+ branch?: string;
1540
+ at?: string;
1541
+ reset?: boolean;
1542
+ restore?: string;
1543
+ seed?: number | string;
1544
+ time?: Instant | Date;
1545
+ advance?: Duration;
1546
+ virtualTime?: boolean;
1547
+ delay?: Duration;
1548
+ timeout?: Duration;
1549
+ /** A status, `"drop"`, a preset name, or a fault rule. */
1550
+ fault?: number | string | Record<string, unknown>;
1551
+ snapshot?: string;
1028
1552
  };
1029
1553
 
1030
1554
  /**
@@ -1064,6 +1588,8 @@ type RequestLog = {
1064
1588
  unmatched: boolean;
1065
1589
  /** Set when a fault rule produced the response. */
1066
1590
  faultId?: string;
1591
+ /** Milliseconds of injected wait: latency, fault delays and timeouts. Absent when none. */
1592
+ delayMs?: number;
1067
1593
  /** Resource ids the handler touched (`userId`, `orderId`, …), when the service reports them. */
1068
1594
  ids?: Record<string, string>;
1069
1595
  /** Set when the service created a resource the request referred to but that did not exist. */
@@ -1109,6 +1635,12 @@ type JournalQuery = {
1109
1635
  /** Only this namespace. Omit for every namespace, oldest first across all of them. */
1110
1636
  namespace?: string;
1111
1637
  operationId?: string;
1638
+ /** Only this HTTP method, case-insensitive. */
1639
+ method?: string;
1640
+ /** Only this exact path. */
1641
+ path?: string;
1642
+ /** Only requests this fault rule answered or shaped. */
1643
+ faultId?: string;
1112
1644
  status?: number;
1113
1645
  /** Only entries at or after this instant (epoch ms). */
1114
1646
  since?: number;
@@ -1123,6 +1655,50 @@ type Journal = {
1123
1655
  clear(namespace?: string): void;
1124
1656
  };
1125
1657
 
1658
+ /**
1659
+ * Baseline response time, per namespace: how long every vendor request takes before any
1660
+ * fault is considered. Jitter draws from a stream the caller owns, one per namespace and
1661
+ * separate from the fault stream, so adding latency never shifts which requests a `rate`
1662
+ * fault hits.
1663
+ */
1664
+ type LatencyRegistry = {
1665
+ get(namespace: string): LatencyState;
1666
+ set(namespace: string, state: LatencyState): LatencyState;
1667
+ /** Remove one namespace's latency, or every namespace's. */
1668
+ clear(namespace?: string): void;
1669
+ /** Milliseconds this request waits. Draws jitter, so call it once per request. */
1670
+ take(namespace: string, operationId: string | undefined): number;
1671
+ };
1672
+
1673
+ /** Clock state as the admin API reports it. */
1674
+ type ControlClock = ClockState & {
1675
+ /** `now`, as ISO-8601. */
1676
+ iso: string;
1677
+ /** Milliseconds the clock steps before every vendor request. */
1678
+ autoAdvanceMs: number;
1679
+ };
1680
+ /** One namespace's controls: the body of `GET /__admin/controls`. */
1681
+ type ControlState = {
1682
+ namespace: string;
1683
+ seed: number | string;
1684
+ clock: ControlClock;
1685
+ virtualTime: boolean;
1686
+ latency: LatencyState;
1687
+ faults: FaultListing[];
1688
+ };
1689
+
1690
+ /** What `GET /__admin/snapshots` lists for each snapshot. */
1691
+ type SnapshotInfo = {
1692
+ id: string;
1693
+ namespace: string;
1694
+ /** Stored rows in the snapshot. */
1695
+ records: number;
1696
+ /** The namespace clock when it was taken, epoch ms. */
1697
+ at: number;
1698
+ /** Whether `GET /snapshots/:id` can return it as a {@link SnapshotDocument}. */
1699
+ portable: boolean;
1700
+ };
1701
+
1126
1702
  /** Credential → namespace mapping behind `PUT /__admin/credentials`. */
1127
1703
  type CredentialRegistry = {
1128
1704
  set(credential: string, namespace: string): void;
@@ -1135,6 +1711,8 @@ type CredentialRegistry = {
1135
1711
  }[];
1136
1712
  };
1137
1713
 
1714
+ type OwnedTransport = (request: Request, namespace: string, send: typeof fetch) => Promise<Response>;
1715
+
1138
1716
  /**
1139
1717
  * A point-in-time copy of everything a service namespace holds.
1140
1718
  *
@@ -1252,8 +1830,16 @@ type PublishInput = {
1252
1830
  id?: string;
1253
1831
  };
1254
1832
  type WebhookHub = {
1833
+ observeDeliveries(listener: (event: {
1834
+ namespace: string;
1835
+ state: string;
1836
+ deliveryId: string;
1837
+ }) => void): () => void;
1838
+ setTransportPolicy(policy: OwnedTransport): void;
1255
1839
  pending(namespace: string): number;
1256
1840
  snapshot(namespace: string): unknown;
1841
+ exportSnapshot(handle: unknown): unknown;
1842
+ importSnapshot(namespace: string, document: unknown): unknown;
1257
1843
  restore(namespace: string, snapshot: unknown): void;
1258
1844
  publish(input: PublishInput): WebhookMessage;
1259
1845
  /** Replace a namespace's own endpoints (`PUT /__admin/webhook-endpoints`). */
@@ -1266,7 +1852,7 @@ type WebhookHub = {
1266
1852
  /** Run every pending retry (and release held reordered messages) now, and every retry those attempts schedule, until nothing is pending. */
1267
1853
  flush(): Promise<void>;
1268
1854
  /** Resolve once nothing is in flight. */
1269
- idle(): Promise<void>;
1855
+ idle(namespace?: string): Promise<void>;
1270
1856
  fault(namespace: string, fault: WebhookFault): void;
1271
1857
  clear(namespace?: string): void;
1272
1858
  };
@@ -1277,7 +1863,10 @@ type ServiceInstance = FetchAPI & {
1277
1863
  };
1278
1864
  type ServiceTimelineState = Readonly<{
1279
1865
  snapshot: NamespaceSnapshot;
1280
- clock: Readonly<ReturnType<Clock["state"]>>;
1866
+ /** `pinned`: the namespace's clock was frozen itself, not just following a frozen base. */
1867
+ clock: Readonly<ClockState & {
1868
+ pinned?: boolean;
1869
+ }>;
1281
1870
  rngState: number;
1282
1871
  }>;
1283
1872
  type ServiceCheckpoint = Checkpoint<ServiceTimelineState>;
@@ -1290,27 +1879,63 @@ type ServiceRuntime<T extends ServiceInstance> = FetchAPI & {
1290
1879
  readonly sqlite: SqliteClient$1;
1291
1880
  readonly clock: Clock;
1292
1881
  readonly faults: FaultRegistry;
1882
+ /** Baseline response time per namespace. */
1883
+ readonly latency: LatencyRegistry;
1293
1884
  readonly metrics: Metrics;
1294
1885
  readonly journal: Journal;
1295
1886
  readonly rng: Rng;
1296
1887
  readonly credentials: CredentialRegistry;
1297
1888
  /** The webhook hub, when the service has outbound webhooks. */
1298
1889
  readonly webhooks: WebhookHub | undefined;
1299
- /** Enable independent namespace clocks and PRNGs before serving a fleet. */
1300
- isolateNamespaces(): void;
1890
+ /**
1891
+ * A namespace's clock. Each follows the base clock, {@link ServiceRuntime.clock}, with an
1892
+ * offset and a freeze of its own, so moving one never moves another.
1893
+ */
1301
1894
  namespaceClock(namespace?: string): Clock;
1302
1895
  namespaceOf(request: Request): string;
1303
1896
  /** Opaque in-process checkpoint including records, controls and webhook history. */
1304
1897
  fleetSnapshot(namespace: string): object;
1305
1898
  fleetRestore(snapshot: unknown, namespace: string): void;
1899
+ fleetExport(snapshot: unknown): SnapshotDocument;
1900
+ fleetImport(namespace: string, document: unknown): object;
1306
1901
  /** Expand a named preset into fault rules (and webhook faults) for `namespace`. */
1307
1902
  applyPreset(name: string, namespace?: string, overrides?: Partial<FaultRule>): FaultRule[];
1308
1903
  /** The instance behind `namespace` (the default one when omitted), created on first use. */
1309
1904
  instance(namespace?: string): T;
1310
1905
  /** Public names of every namespace created so far. */
1311
1906
  namespaces(): string[];
1312
- /** Reset one namespace, or every namespace with `"*"`. */
1907
+ /** Return one namespace to its boot state, or every namespace with `"*"`. */
1313
1908
  reset(namespace?: string): Promise<void>;
1909
+ /** Reset a namespace and forget it, releasing everything it held. */
1910
+ dropNamespace(namespace: string): Promise<void>;
1911
+ /** A namespace's controls: seed, clock, virtual time, latency, faults and snapshots. */
1912
+ controls(namespace?: string): ControlState & {
1913
+ snapshots: SnapshotInfo[];
1914
+ };
1915
+ /** Apply a control document to a namespace. A given key replaces that control. */
1916
+ configure(controls: NamespaceControls, namespace?: string): ControlState & {
1917
+ snapshots: SnapshotInfo[];
1918
+ };
1919
+ /** Named snapshots of a namespace: records, clock, random streams, faults and latency. */
1920
+ readonly snapshots: {
1921
+ /** Capture the namespace now. Reusing a name replaces that snapshot. */
1922
+ create(options?: {
1923
+ name?: string;
1924
+ namespace?: string;
1925
+ }): SnapshotInfo;
1926
+ list(namespace?: string): SnapshotInfo[];
1927
+ /** The snapshot as portable JSON, to commit beside a suite or boot another mock from. */
1928
+ export(id: string, namespace?: string): SnapshotDocument;
1929
+ /** Store a document as a snapshot without restoring it. */
1930
+ import(document: SnapshotDocument, options?: {
1931
+ name?: string;
1932
+ namespace?: string;
1933
+ }): SnapshotInfo;
1934
+ restore(id: string, namespace?: string): void;
1935
+ delete(id: string, namespace?: string): boolean;
1936
+ };
1937
+ /** Request headers that carry controls, checked: spread them into a `fetch` or an SDK's defaults. */
1938
+ headers(input: ControlHeaderInput): Record<string, string>;
1314
1939
  snapshot(namespace?: string): NamespaceSnapshot;
1315
1940
  restore(snapshot: NamespaceSnapshot, namespace?: string): void;
1316
1941
  /** Capture the current branch. Mutating HTTP calls do this automatically. */
@@ -1394,6 +2019,8 @@ type RuntimeOptions = {
1394
2019
  accountId?: string;
1395
2020
  adminPrefix?: string;
1396
2021
  adminKey?: string;
2022
+ /** Admin controls to boot with: clock, latency, faults, seed, a starting snapshot. */
2023
+ adminControls?: MockControls;
1397
2024
  onLog?: (entry: RequestLog) => void;
1398
2025
  };
1399
2026
  type Runtime = ServiceRuntime<ResourceGroupsAPI>;