@streamsy/projection 0.14.0 → 0.16.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
@@ -1,4 +1,4 @@
1
- import { C as ItemOf, S as InputServices, T as Slices, _ as NonRetryable, a as Identity, b as Entry, c as ProjectionKey, d as RoutedStreamRecord, g as InvalidBinding, h as Unit, l as RetirementStatus, m as Range, n as CheckpointStore, r as CheckpointStoreApi, s as Loaded, t as CheckpointRecord, u as RouteRecords, v as NotAttached, w as Slice, x as InputMap, y as ProjectionFault } from "./checkpoint-store-Cy5myzCL.js";
1
+ import { C as ItemOf, S as InputServices, T as Slices, _ as NonRetryable, a as Identity, b as Entry, c as ProjectionKey, d as RoutedStreamRecord, g as InvalidBinding, h as Unit, l as RetirementStatus, m as Range, n as CheckpointStore, r as CheckpointStoreApi, s as Loaded, t as CheckpointRecord, u as RouteRecords, v as NotAttached, w as Slice, x as InputMap, y as ProjectionFault } from "./checkpoint-store-BQviDBDA.js";
2
2
  import { Cause, Context, Data, DateTime, Effect, Layer, Option, Pipeable, Schedule, Schema, Semaphore, Stream } from "effect";
3
3
  import { CommitBoundary, Decided, Decision, DurableState, Offset, OffsetMismatch, Storage, StorageFault, StreamAddress, StreamId, StreamsFault, Streamsy } from "@streamsy/core";
4
4
  import { MarkFailure, ProjectionMarkSummary } from "@streamsy/core/internal";
@@ -139,6 +139,19 @@ declare const withCompensation: {
139
139
  declare namespace commit_d_exports {
140
140
  export { Accumulated, Commit, Emitted, Folded, HandlerFailed, OutputBatch, Pending, RetireRequest, TransactionCommit, TypeId$1 as TypeId, Variance, atLeastOnce, checkpoint, make, producer, transaction, transactionWithCleanup, withCompensation };
141
141
  }
142
+ type StateSchema<A, I = unknown> = Schema.Codec<A, I>;
143
+ /** A value, or a function of the decoded params the projection was bound with. */
144
+ type Initial<A, P = Record<string, string>> = A | ((params: P) => A);
145
+ /** The `state:` field of a declaration. */
146
+ interface StateDefinition<A, I = unknown, P = Record<string, string>> {
147
+ readonly schema: StateSchema<A, I>;
148
+ readonly initial: Initial<A, P>;
149
+ }
150
+ /** The state of a bound projection. `initial` runs only when stored state is absent. */
151
+ interface BoundState<A> {
152
+ readonly schema: StateSchema<A>;
153
+ readonly initial: () => A;
154
+ }
142
155
  declare const TypeId: unique symbol;
143
156
  interface PerGeneration<D extends StreamDeclaration, N extends string, By extends string = never> {
144
157
  readonly [TypeId]: {
@@ -174,51 +187,12 @@ interface GenerationOutputBindings {
174
187
  readonly shared: boolean;
175
188
  }>;
176
189
  }
177
- /** Open one generation-owned stream in the current process transaction. */
178
- interface RoutedHandle<A, Params, R> {
179
- readonly open: (params: Params) => Effect.Effect<A, ProjectionFault, R>;
180
- }
181
- declare const RoutedOutputs: unique symbol;
182
- interface RoutedDefinition<R> {
183
- readonly matches: (streamId: string) => boolean;
184
- readonly restore: (streamId: string) => Streamsy.Address<unknown, R> | undefined;
185
- }
186
- interface RoutedOutputBindings<R> {
187
- readonly [RoutedOutputs]?: ReadonlyMap<string, RoutedDefinition<R>>;
188
- }
189
- type RoutedMap = Readonly<Record<string, RoutedOutput>>;
190
- type StaticOutputMap = Readonly<Record<string, Input | PerGeneration<StreamDeclaration, string>>>;
191
- type Item<T> = AddressOf<T> extends {
192
- readonly collections: infer C extends StreamAddress.Collections;
193
- } ? DurableState.Entry<C> : AddressOf<T> extends {
194
- readonly codec: Schema.Codec<infer A, any, any, any>;
195
- } ? A : never;
196
- type Services<O extends RoutedMap> = { [K in keyof O]: AddressOf<O[K]> extends Streamsy.Address<any, infer R> ? R : never; }[keyof O];
197
- interface BoundOutputs<R> extends GenerationOutputBindings, RoutedOutputBindings<R> {
198
- readonly streams?: Readonly<Record<string, Streamsy.Address<unknown, R>>>;
199
- readonly accepted?: Streamsy.Address<Streamsy.WakeEvent, R>;
200
- }
201
- /** Create the generation-bound outputs of one binding, using each output's owner. */
202
- declare const createOutputs: <R>(bound: BoundOutputs<R>) => Effect.Effect<void, ProjectionFault, R>;
203
- type StateSchema<A, I = unknown> = Schema.Codec<A, I>;
204
- /** A value, or a function of the decoded params the projection was bound with. */
205
- type Initial<A, P = Record<string, string>> = A | ((params: P) => A);
206
- /** The `state:` field of a declaration. */
207
- interface StateDefinition<A, I = unknown, P = Record<string, string>> {
208
- readonly schema: StateSchema<A, I>;
209
- readonly initial: Initial<A, P>;
210
- }
211
- /** The state of a bound projection. `initial` runs only when stored state is absent. */
212
- interface BoundState<A> {
213
- readonly schema: StateSchema<A>;
214
- readonly initial: () => A;
215
- }
216
190
  /** Common bound declaration consumed by the kernel. */
217
191
  interface Executable<I extends InputMap, E, R, Q, Result, P = never, S = unknown, OR = never> extends Identity, GenerationOutputBindings {
218
192
  readonly inputs: I;
219
193
  readonly state?: BoundState<S>;
220
194
  readonly streams?: Readonly<Record<string, Streamsy.Address<unknown, OR>>>;
221
- readonly accepted?: Streamsy.Address<Streamsy.WakeEvent, OR>;
195
+ readonly accepted?: Streamsy.Address<Streamsy.FrontierEvent, OR>;
222
196
  readonly commit: Commit<Result, P, Q>;
223
197
  readonly prepare?: (batch: Slices<I>, unit: Unit) => Effect.Effect<unknown, E, Q>;
224
198
  readonly process: (batch: Slices<I>, unit: Unit, encoded?: string, prepared?: unknown, attempt?: number) => Effect.Effect<Result, E, R | P>;
@@ -243,6 +217,47 @@ type Readable<Inputs extends InputMap, S, Q> = Identity & {
243
217
  * value and every offset is the start of its input.
244
218
  */
245
219
  declare const read: <Inputs extends InputMap, S = never, Q = never>(projection: Readable<Inputs, S, Q>) => Effect.Effect<Snapshot<StateValue<S>, Inputs>, ProjectionFault, Q>;
220
+ /** A Streamsy.frontierStream class. Use FrontierStream<{}> for a fixed-address trigger with no params. */
221
+ type FrontierStream<P = any> = StreamDeclaration & StreamRoute.StateRoute<P, Streamsy.FrontierStreamCollections> & {
222
+ readonly paramCodecs: StreamRoute.ParamCodecs;
223
+ };
224
+ interface FrontierDefinition<C extends StreamRoute.ParamCodecs, I extends Input, O> {
225
+ readonly id: string;
226
+ readonly generation?: number;
227
+ readonly params: C;
228
+ /** The one source stream. Its params must be projection params. */
229
+ readonly input: I & OneWithin<C, I>;
230
+ /** The State declaration with frontier stream collections that gets the row. Its params must be projection params. */
231
+ readonly output: O & OneWithin<C, O>;
232
+ }
233
+ type FrontierDeclaration<C extends StreamRoute.ParamCodecs, I extends Input, O extends Input> = AccumulatingDeclaration<C, {
234
+ readonly source: I;
235
+ }, {
236
+ readonly wake: O;
237
+ }, null, never, never, never, CheckpointStore>;
238
+ /**
239
+ * A frontier projection: one source stream gives one keyed row in one frontier stream.
240
+ * Each unit appends one upsert `{ stream, offset, closed? }` with
241
+ * `Commit.producer`, so several source writes in one unit fold into one row,
242
+ * and a retry answers `Duplicate`. The producer creates a missing frontier stream
243
+ * on its own owner. A frontier stream on another owner must exist before the first
244
+ * append: its owner creates it at start. When the source is removed or
245
+ * expires, the binding appends a State delete for its row and forgets itself.
246
+ *
247
+ * The row is the one that `frontier: output` would publish for the source, except that a
248
+ * gone source deletes its row, so that triggers clear their outputs. Rows that a State
249
+ * source carries are not forwarded.
250
+ *
251
+ * ```ts
252
+ * const filesFrontier = Projection.frontier({
253
+ * id: "files-wake",
254
+ * params: { user: UserKey, folderId: FolderKey },
255
+ * input: Files,
256
+ * output: UserFrontier,
257
+ * });
258
+ * ```
259
+ */
260
+ declare function frontier<C extends StreamRoute.ParamCodecs, const I extends Input, const O extends FrontierStream>(definition: FrontierDefinition<C, I, O>): FrontierDeclaration<C, I, O>;
246
261
  /** The part of a projection declaration that the attach runner reads. */
247
262
  interface Attachable<B, HE = never, HR = never, Params = DecodedParams> {
248
263
  readonly retry?: {
@@ -377,7 +392,7 @@ interface RegistryApi {
377
392
  * Use after a fold fix. The cursor survives restart in both durability modes.
378
393
  * An unknown trigger stream dies with RangeError. First attach already reads from the start.
379
394
  */
380
- readonly rescan: (target: string | Declaration$2, trigger: WakeOutput<{}> | string) => Effect.Effect<void, NotAttached | StorageFault>;
395
+ readonly rescan: (target: string | Declaration$2, trigger: FrontierStream<{}> | string) => Effect.Effect<void, NotAttached | StorageFault>;
381
396
  /**
382
397
  * Wait for the attached identity's checkpoint, subscribing before the first read.
383
398
  * Matching failed runs prompt a reread for committed progress. No timeout.
@@ -406,6 +421,18 @@ declare class Registry extends Registry_base {
406
421
  /** Provide below the attach layers so they see this same registry. */
407
422
  static readonly layer: Layer.Layer<Registry>;
408
423
  }
424
+ /** Open one generation-owned stream in the current process transaction. */
425
+ interface RoutedHandle<A, Params, R> {
426
+ readonly open: (params: Params) => Effect.Effect<A, ProjectionFault, R>;
427
+ }
428
+ declare const RoutedOutputs: unique symbol;
429
+ interface RoutedDefinition<R> {
430
+ readonly matches: (streamId: string) => boolean;
431
+ readonly restore: (streamId: string) => Streamsy.Address<unknown, R> | undefined;
432
+ }
433
+ interface RoutedOutputBindings<R> {
434
+ readonly [RoutedOutputs]?: ReadonlyMap<string, RoutedDefinition<R>>;
435
+ }
409
436
  /** A stream declaration. `.at(params)` resolves it with the projection params. */
410
437
  type StreamDeclaration = StreamRoute.StreamRoute<any, any> & Streamsy.Any & {
411
438
  readonly at: (params: any) => Streamsy.Address<any, any>;
@@ -438,10 +465,12 @@ interface Declaration<Codecs extends StreamRoute.ParamCodecs, Bound, HE = never,
438
465
  /** Tries declaration inputs in order. Shared inputs return only their declared subset. */
439
466
  readonly parse: (id: string) => Option.Option<Partial<StreamRoute.Params<Codecs>>>;
440
467
  }
441
- type AcceptedServices<W> = W extends AcceptedOutput ? Services<{
442
- accepted: W;
468
+ /** The services of a frontier stream. `frontier: true` and no frontier need none. */
469
+ type FrontierServices<W> = W extends FrontierOutput ? Services<{
470
+ frontier: W;
443
471
  }> : never;
444
- type AcceptedResult<Result, W> = W extends AcceptedOutput ? Result extends Folded<infer A> ? Accumulated<A, {}> : Result extends WakeAt ? Emitted<{}> & Pick<WakeAt, "wakeAt"> : Emitted<{}> : Result;
472
+ /** A frontier stream adds an output batch, so the Commit sees an output-writing result. */
473
+ type FrontierResult<Result, W> = W extends FrontierOutput ? Result extends Folded<infer A> ? Accumulated<A, {}> : Result extends WakeAt ? Emitted<{}> & Pick<WakeAt, "wakeAt"> : Emitted<{}> : Result;
445
474
  /** Binding retry timing and budget, shared by both attach hosts. */
446
475
  interface BindingRetry<Params, E = never, R = never> {
447
476
  /**
@@ -465,8 +494,12 @@ interface Common<C extends StreamRoute.ParamCodecs, I extends Inputs, W = undefi
465
494
  readonly generation?: number;
466
495
  readonly params: C;
467
496
  readonly inputs: I & Within<C, I>;
468
- /** Publish accepted input offsets in this wake stream as part of each unit. */
469
- readonly accepted?: W & OneOutputWithin<C, W>;
497
+ /**
498
+ * Publish the input offsets each unit accepted as frontier rows. `true` appends them to each
499
+ * fixed State output with a frontier collection; a frontier stream gets them as part of the
500
+ * unit. `true` is only allowed on the emit kinds.
501
+ */
502
+ readonly frontier?: W & OneOutputWithin<C, W>;
470
503
  }
471
504
  /** Decoded params, keyed by name. */
472
505
  type DecodedParams = Readonly<Record<string, unknown>>;
@@ -484,53 +517,53 @@ type OutputDeclaration<C extends StreamRoute.ParamCodecs, I extends Inputs, O ex
484
517
  };
485
518
  };
486
519
  type AccumulatingDeclaration<C extends StreamRoute.ParamCodecs, I extends Inputs, O extends RoutedMap, A, E, R, P, Q, OR = never, HE = never, HR = never> = OutputDeclaration<C, I, O, E, R, Accumulated<A, O>, P, Q, A, OR, HE, HR>;
487
- /** A Streamsy.wake class. Use WakeOutput<{}> for a fixed-address trigger with no params. */
488
- type WakeOutput<P = any> = StreamDeclaration & StreamRoute.StateRoute<P, Streamsy.WakeCollections> & {
489
- readonly paramCodecs: StreamRoute.ParamCodecs;
490
- };
491
- interface WakeDefinition<C extends StreamRoute.ParamCodecs, I extends Input, O, W = undefined> {
492
- readonly accepted?: W & OneOutputWithin<C, W>;
493
- readonly id: string;
494
- readonly generation?: number;
495
- readonly params: C;
496
- /** The one source stream. Its params must be projection params. */
497
- readonly input: I & OneWithin<C, I>;
498
- /** The State declaration with wake collections that gets the row. Its params must be projection params. */
499
- readonly output: O & OneWithin<C, O>;
520
+ type RoutedMap = Readonly<Record<string, RoutedOutput>>;
521
+ type StaticOutputMap = Readonly<Record<string, Input | PerGeneration<StreamDeclaration, string>>>;
522
+ type Item<T> = AddressOf<T> extends {
523
+ readonly collections: infer C extends StreamAddress.Collections;
524
+ } ? DurableState.Entry<C> : AddressOf<T> extends {
525
+ readonly codec: Schema.Codec<infer A, any, any, any>;
526
+ } ? A : never;
527
+ type Services<O extends RoutedMap> = { [K in keyof O]: AddressOf<O[K]> extends Streamsy.Address<any, infer R> ? R : never; }[keyof O];
528
+ interface BoundOutputs<R> extends GenerationOutputBindings, RoutedOutputBindings<R> {
529
+ readonly streams?: Readonly<Record<string, Streamsy.Address<unknown, R>>>;
530
+ readonly accepted?: Streamsy.Address<Streamsy.FrontierEvent, R>;
531
+ }
532
+ /** Create the generation-bound outputs of one binding, using each output's owner. */
533
+ declare const createOutputs: <R>(bound: BoundOutputs<R>) => Effect.Effect<void, ProjectionFault, R>;
534
+ /** A frontier stream: a `Streamsy.frontierStream` class, or `Projection.perGeneration` of one. */
535
+ type FrontierOutput = FrontierStream | PerGeneration<FrontierStream, string>;
536
+ /** @internal Shared with View.circuit. */
537
+ export declare const isFrontierCollection: (collection: unknown) => boolean;
538
+ interface FrontierContext {
539
+ readonly commit: Pick<Commit<never>, "name">;
540
+ readonly params: StreamRoute.ParamCodecs;
541
+ readonly outputs?: RoutedMap | undefined;
542
+ readonly inputTemplates?: ReadonlyArray<string> | undefined;
500
543
  }
501
- type WakeDeclaration<C extends StreamRoute.ParamCodecs, I extends Input, O extends Input, W = undefined> = AccumulatingDeclaration<C, {
502
- readonly source: I;
503
- }, {
504
- readonly wake: O;
505
- }, null, never, never, never, CheckpointStore, AcceptedServices<W>>;
506
544
  /**
507
- * A wake projection: one source stream gives one keyed row in one wake stream.
508
- * Each unit appends one upsert `{ stream, offset, closed? }` with
509
- * `Commit.producer`, so several source writes in one unit fold into one row,
510
- * and a retry answers `Duplicate`. The producer creates a missing wake stream
511
- * on its own owner. A wake stream on another owner must exist before the first
512
- * append: its owner creates it at start. When the source is removed or
513
- * expires, the binding appends a State delete for its row and forgets itself.
545
+ * Check the `frontier` option of a declaration. For `true`, answer the frontier collection
546
+ * of each output that gets the rows. A frontier stream answers `undefined`.
514
547
  *
515
- * ```ts
516
- * const filesWake = Projection.wake({
517
- * id: "files-wake",
518
- * params: { user: UserKey, folderId: FolderKey },
519
- * input: Files,
520
- * output: UserWake,
521
- * });
522
- * ```
548
+ * @internal Shared with View.circuit.
523
549
  */
524
- declare function wake<C extends StreamRoute.ParamCodecs, const I extends Input, const O extends WakeOutput, W extends AcceptedOutput | undefined = undefined>(definition: WakeDefinition<C, I, O, W>): WakeDeclaration<C, I, O, W>;
525
- type AcceptedOutput = WakeOutput | PerGeneration<WakeOutput, string>;
550
+ export declare const validateFrontier: (frontier: true | FrontierOutput | undefined, context: FrontierContext) => ReadonlyMap<string, string> | undefined;
551
+ /**
552
+ * The frontier rows of one unit, for either destination: the rows its State inputs carry,
553
+ * then one row for each input that moved or closed. `closed` holds the inputs whose close an
554
+ * earlier unit recorded.
555
+ *
556
+ * @internal Shared with View.circuit.
557
+ */
558
+ export declare const unitFrontiers: (inputs: InputMap, unit: Pick<Unit, "ranges">, batch: Slices<InputMap>, closed?: Readonly<Record<string, boolean>>) => ReadonlyArray<Streamsy.Frontier>;
526
559
  type Step<X, E = never, R = never> = X | Effect.Effect<X, E, R>;
527
560
  /** Each output keeps its item type here; missing keys emit nothing. */
528
561
  type Emits<O extends RoutedMap> = { readonly [K in keyof O]?: ReadonlyArray<Item<O[K]>>; };
529
562
  /** Fold one entry at a time. The default Commit saves state and offsets in one write. */
530
- declare function fold<C extends StreamRoute.ParamCodecs, I extends Inputs, A, SI, E = never, R = never, P = never, Q = CheckpointStore, W extends AcceptedOutput | undefined = undefined, HE = never, HR = never>(definition: Common<C, I, W, HE, HR> & ([W] extends [undefined] ? Partial<Committing<AcceptedResult<Folded<NoInfer<A>>, W>, P, Q>> : Committing<AcceptedResult<Folded<NoInfer<A>>, W>, P, Q>) & {
563
+ declare function fold<C extends StreamRoute.ParamCodecs, I extends Inputs, A, SI, E = never, R = never, P = never, Q = CheckpointStore, W extends FrontierOutput | undefined = undefined, HE = never, HR = never>(definition: Common<C, I, W, HE, HR> & ([W] extends [undefined] ? Partial<Committing<FrontierResult<Folded<NoInfer<A>>, W>, P, Q>> : Committing<FrontierResult<Folded<NoInfer<A>>, W>, P, Q>) & {
531
564
  readonly state: StateDefinition<A, SI, StreamRoute.Params<C>>;
532
565
  readonly fold: (state: NoInfer<A>, entry: Entry<Addresses<I>>, unit: Unit<StreamRoute.Params<C>>) => Step<A, E, R>;
533
- }): StatefulDeclaration<C, I, A, E, R, Folded<A>, P, Q, AcceptedServices<W>, HE, HR>;
566
+ }): StatefulDeclaration<C, I, A, E, R, Folded<A>, P, Q, FrontierServices<W>, HE, HR>;
534
567
  /** void unless the handler result can be a WakeAt. */
535
568
  type TimerResult<A> = 0 extends 1 & A ? void : [Extract<A, WakeAt>] extends [never] ? void : void | WakeAt;
536
569
  /** The third argument of `process`. */
@@ -554,40 +587,40 @@ type WithTransactionCommit<D extends {
554
587
  /** Run side effects once per unit, including recorded wakes. Return wakeAt(time) to
555
588
  * replace the timer, or void to clear it. Timers require atLeastOnce or transaction.
556
589
  * prepare runs at least once, outside the transaction. */
557
- declare function process$1<C extends StreamRoute.ParamCodecs, I extends Inputs, E = never, R = never, P = never, Q = never, Prepared = void, PE = never, PR = never, W extends AcceptedOutput | undefined = undefined, HE = never, HR = never, A = void>(definition: Common<C, I, W, HE, HR> & Committing<AcceptedResult<TimerResult<A>, W>, P, Q> & {
590
+ declare function process$1<C extends StreamRoute.ParamCodecs, I extends Inputs, E = never, R = never, P = never, Q = never, Prepared = void, PE = never, PR = never, W extends FrontierOutput | undefined = undefined, HE = never, HR = never, A = void>(definition: Common<C, I, W, HE, HR> & Committing<FrontierResult<TimerResult<A>, W>, P, Q> & {
558
591
  readonly outputs?: never;
559
592
  readonly prepare?: (batch: Slices<Addresses<I>>, unit: Unit<StreamRoute.Params<C>>) => Effect.Effect<Prepared, PE, PR>;
560
593
  readonly process: (batch: Slices<Addresses<I>>, unit: Unit<StreamRoute.Params<C>>, context: ProcessContext<Prepared>) => Effect.Effect<A, E, R>;
561
- }): RunningDeclaration<C, I, E | PE, R, TimerResult<A>, P, Q | PR, never, AcceptedServices<W>, HE, HR>;
562
- declare function process$1<C extends StreamRoute.ParamCodecs, I extends Inputs, const O extends RoutedMap, E = never, R = never, Q = never, Prepared = void, PE = never, PR = never, W extends AcceptedOutput | undefined = undefined, HE = never, HR = never, A = void>(definition: Common<C, I, W, HE, HR> & Committing<AcceptedResult<TimerResult<A>, W>, never, Q> & {
594
+ }): RunningDeclaration<C, I, E | PE, R, TimerResult<A>, P, Q | PR, never, FrontierServices<W>, HE, HR>;
595
+ declare function process$1<C extends StreamRoute.ParamCodecs, I extends Inputs, const O extends RoutedMap, E = never, R = never, Q = never, Prepared = void, PE = never, PR = never, W extends FrontierOutput | undefined = undefined, HE = never, HR = never, A = void>(definition: Common<C, I, W, HE, HR> & Committing<FrontierResult<TimerResult<A>, W>, never, Q> & {
563
596
  readonly commit: TransactionBrand;
564
597
  readonly outputs: O & OutputsWithin<C, O>;
565
598
  readonly prepare?: (batch: Slices<Addresses<I>>, unit: Unit<StreamRoute.Params<C>>, context: OutputsContext<O>) => Effect.Effect<Prepared, PE, PR>;
566
599
  readonly process: (batch: Slices<Addresses<I>>, unit: Unit<StreamRoute.Params<C>>, context: ProcessContext<Prepared> & OutputsContext<O>) => Effect.Effect<A, E, R>;
567
- }): WithTransactionCommit<OutputDeclaration<C, I, O, E | PE, R, TimerResult<A>, never, Q | PR, never, AcceptedServices<W>, HE, HR>>;
600
+ }): WithTransactionCommit<OutputDeclaration<C, I, O, E | PE, R, TimerResult<A>, never, Q | PR, never, FrontierServices<W>, HE, HR>>;
568
601
  /** Emit zero or more output items for each input entry, in entry order. */
569
- declare function emit<C extends StreamRoute.ParamCodecs, I extends Inputs, const O extends StaticOutputMap, X extends Emits<O>, E = never, R = never, P = never, Q = never, W extends AcceptedOutput | undefined = undefined, HE = never, HR = never>(definition: Common<C, I, W, HE, HR> & Committing<Emitted<NoInfer<O>>, P, Q> & {
602
+ declare function emit<C extends StreamRoute.ParamCodecs, I extends Inputs, const O extends StaticOutputMap, X extends Emits<O>, E = never, R = never, P = never, Q = never, W extends FrontierOutput | true | undefined = undefined, HE = never, HR = never>(definition: Common<C, I, W, HE, HR> & Committing<Emitted<NoInfer<O>>, P, Q> & {
570
603
  readonly outputs: O & OutputsWithin<C, O>;
571
604
  readonly emit: ((entry: Entry<Addresses<I>>, unit: Unit<StreamRoute.Params<C>>) => Step<X, E, R>) & (Exclude<keyof X, keyof O> extends never ? unknown : {
572
605
  readonly undeclaredOutput: Exclude<keyof X, keyof O>;
573
606
  });
574
- }): OutputDeclaration<C, I, O, E, R, Emitted<O>, P, Q, never, AcceptedServices<W>, HE, HR>;
607
+ }): OutputDeclaration<C, I, O, E, R, Emitted<O>, P, Q, never, FrontierServices<W>, HE, HR>;
575
608
  type Definition<C extends StreamRoute.ParamCodecs, I extends Inputs, O extends StaticOutputMap, A, SI, P, Q, W, HE, HR> = Common<C, I, W, HE, HR> & Committing<Accumulated<NoInfer<A>, NoInfer<O>>, P, Q> & {
576
609
  readonly outputs: O & OutputsWithin<C, O>;
577
610
  readonly state: StateDefinition<A, SI, StreamRoute.Params<C>>;
578
611
  };
579
612
  /** Thread the value and collect outputs, once per entry. */
580
- declare function mapAccum<C extends StreamRoute.ParamCodecs, I extends Inputs, const O extends StaticOutputMap, A, SI, X extends Emits<O>, E = never, R = never, P = never, Q = never, W extends AcceptedOutput | undefined = undefined, HE = never, HR = never>(definition: Definition<C, I, O, A, SI, P, Q, W, HE, HR> & {
613
+ declare function mapAccum<C extends StreamRoute.ParamCodecs, I extends Inputs, const O extends StaticOutputMap, A, SI, X extends Emits<O>, E = never, R = never, P = never, Q = never, W extends FrontierOutput | true | undefined = undefined, HE = never, HR = never>(definition: Definition<C, I, O, A, SI, P, Q, W, HE, HR> & {
581
614
  readonly mapAccum: ((state: NoInfer<A>, entry: Entry<Addresses<I>>, unit: Unit<StreamRoute.Params<C>>) => Step<readonly [A, X], E, R>) & (Exclude<keyof X, keyof O> extends never ? unknown : {
582
615
  readonly undeclaredOutput: Exclude<keyof X, keyof O>;
583
616
  });
584
- }): AccumulatingDeclaration<C, I, O, A, E, R, P, Q, AcceptedServices<W>, HE, HR>;
617
+ }): AccumulatingDeclaration<C, I, O, A, E, R, P, Q, FrontierServices<W>, HE, HR>;
585
618
  /** Compute the next value and outputs once for the whole unit batch. */
586
- declare function mapAccumBatch<C extends StreamRoute.ParamCodecs, I extends Inputs, const O extends StaticOutputMap, A, SI, X extends Emits<O>, E = never, R = never, P = never, Q = never, W extends AcceptedOutput | undefined = undefined, HE = never, HR = never>(definition: Definition<C, I, O, A, SI, P, Q, W, HE, HR> & {
619
+ declare function mapAccumBatch<C extends StreamRoute.ParamCodecs, I extends Inputs, const O extends StaticOutputMap, A, SI, X extends Emits<O>, E = never, R = never, P = never, Q = never, W extends FrontierOutput | true | undefined = undefined, HE = never, HR = never>(definition: Definition<C, I, O, A, SI, P, Q, W, HE, HR> & {
587
620
  readonly mapAccumBatch: ((state: NoInfer<A>, batch: Slices<Addresses<I>>, unit: Unit<StreamRoute.Params<C>>) => Step<readonly [A, X], E, R>) & (Exclude<keyof X, keyof O> extends never ? unknown : {
588
621
  readonly undeclaredOutput: Exclude<keyof X, keyof O>;
589
622
  });
590
- }): AccumulatingDeclaration<C, I, O, A, E, R, P, Q, AcceptedServices<W>, HE, HR>;
623
+ }): AccumulatingDeclaration<C, I, O, A, E, R, P, Q, FrontierServices<W>, HE, HR>;
591
624
  interface FollowOptions extends RunOptions {
592
625
  /** Upper bound on one wake wait; default 30000. */
593
626
  readonly repairIntervalMs?: number;
@@ -619,13 +652,13 @@ interface AttachOptions<Params = DecodedParams, E = never, R = never> {
619
652
  */
620
653
  readonly bindings?: Effect.Effect<ReadonlyArray<Params>, E, R>;
621
654
  /**
622
- * Fixed-address wake classes whose source ids request binding runs. A provided
655
+ * Fixed-address frontier stream classes whose source ids request binding runs. A provided
623
656
  * stream service on another storage owner is remote; an absent service or a
624
657
  * matching owner is local. Provide the remote class through Protocol.bind.
625
658
  * Projection.attach follows remote triggers in scope; ProjectionWakeup.attach
626
659
  * accepts only triggers on its own owner. Cursors persist in both modes.
627
660
  */
628
- readonly triggers?: ReadonlyArray<WakeOutput<{}>>;
661
+ readonly triggers?: ReadonlyArray<FrontierStream<{}>>;
629
662
  /** Units for each binding in one run. Default 100. */
630
663
  readonly limit?: number;
631
664
  /**
@@ -668,7 +701,7 @@ interface AttachOptions<Params = DecodedParams, E = never, R = never> {
668
701
  * window, and the layer runs due marks when it starts. Recorded process timers
669
702
  * wake at their deadline; after host suspend, a capped wait rechecks within 30 s.
670
703
  * Return a time after unit.due to avoid repeatedly running a passed deadline.
671
- * Inputs on another owner can use local or remote wake streams in `triggers`.
704
+ * Inputs on another owner can use local or remote frontier streams in `triggers`.
672
705
  * A provided trigger service on another owner is read one page per pass outside
673
706
  * the local transaction; durable marks and the page cursor then commit together.
674
707
  * A scoped live-read follower signals new remote rows. Remote read failures use
@@ -677,7 +710,7 @@ interface AttachOptions<Params = DecodedParams, E = never, R = never> {
677
710
  * an attached binding under `Projection.follow` too: use one writer per key.
678
711
  *
679
712
  * ```ts
680
- * const app = Layer.mergeAll(Api, Projection.attach(filesWake)).pipe(
713
+ * const app = Layer.mergeAll(Api, Projection.attach(filesFrontier)).pipe(
681
714
  * Layer.provideMerge(BunStorage.layer(streams, { path })),
682
715
  * );
683
716
  * ```
@@ -807,21 +840,42 @@ declare const retire: <C extends StreamRoute.ParamCodecs, R, Q, HE, HR>(declarat
807
840
  readonly streams?: StreamMode;
808
841
  }) => Effect.Effect<Retired, ProjectionFault, R | Q>;
809
842
  declare namespace projection_d_exports {
810
- export { InputLag, Lag, PerGeneration, Registry, RegistryApi, Retired, RetiredStream, RoutedHandle, RunnerEvent, RunnerFailure, RunnerStatus, Tails, WakeAt, WakeOutput, attach$1 as attach, bindings, caughtUp, createOutputs, current, decide, each, emit, fold, follow, forget, generationBinding, isPerGeneration, items, key$1 as key, lag, mapAccum, mapAccumBatch, pass, perGeneration, process$1 as process, read, retire, run, tails, wake, wakeAt };
843
+ export { FrontierOutput, FrontierServices, FrontierStream, InputLag, Lag, PerGeneration, Registry, RegistryApi, Retired, RetiredStream, RoutedHandle, RunnerEvent, RunnerFailure, RunnerStatus, Tails, WakeAt, attach$1 as attach, bindings, caughtUp, createOutputs, current, decide, each, emit, fold, follow, forget, frontier, generationBinding, isPerGeneration, items, key$1 as key, lag, mapAccum, mapAccumBatch, pass, perGeneration, process$1 as process, read, retire, run, tails, wakeAt };
811
844
  }
812
845
  declare const key$1: (projection: Identity) => ProjectionKey;
813
846
  /** Runs each tagged entry in order and discards its success value. */
814
847
  declare const each: <Inputs extends InputMap, E, R, P = Record<string, string>>(handle: (entry: Entry<Inputs>, unit: Unit<P>) => Effect.Effect<unknown, E, R>) => ((batch: Slices<Inputs>, unit: Unit<P>) => Effect.Effect<void, E, R>);
848
+ declare const BackendTypeId: unique symbol;
849
+ interface Opaque {
850
+ readonly [BackendTypeId]: true;
851
+ }
852
+ declare const StateTableBackend_base: Context.ServiceClass<StateTableBackend, "@streamsy/projection/StateTableBackend", Opaque>;
853
+ /** Host-selected row storage for StateTable and FoldByKey. */
854
+ export declare class StateTableBackend extends StateTableBackend_base {
855
+ /** Keep rows in the host database. Run StateTable.migrate with the same client. */
856
+ static readonly layerSql: Layer.Layer<StateTableBackend, never, SqlClient.SqlClient>;
857
+ /**
858
+ * Keep rows in the heap and join the memory owner's transaction. Every layer over one
859
+ * owner shares its rows. A read outside a unit, or on a fiber that the unit forked,
860
+ * sees committed rows only.
861
+ */
862
+ static readonly layerMemory: Layer.Layer<StateTableBackend, never, CommitBoundary>;
863
+ }
815
864
  declare namespace state_table_d_exports {
816
- export { Bound, Declaration$1 as Declaration, ListOptions, clear, define, migrate, rebuild, reset };
865
+ export { Bound, Declaration$1 as Declaration, ListOptions, UnitRows, clear, define, migrate, rebuild, reset, unit };
817
866
  }
818
- /** Create the SQLite row index at host startup, outside storage migrations. */
867
+ /**
868
+ * Create the row index on SQLite, Postgres or MySQL at host startup, outside storage
869
+ * migrations. A host with `StateTableBackend.layerSql` runs it with the same client.
870
+ */
819
871
  declare const migrate: (sql: SqlClient.SqlClient) => Effect.Effect<void, ProjectionFault | SqlError.SqlError, never>;
820
872
  /** Keyset pages are not snapshots. Follow changes from the first page's offset. */
821
873
  interface ListOptions {
822
874
  readonly after?: string | undefined;
823
875
  readonly limit?: number | undefined;
824
876
  }
877
+ /** Bound reads keep `SqlError` in their errors. Only SQL backends fail with it. */
878
+ type ReadError = SqlError.SqlError;
825
879
  /** A State stream bound to its params: a bound projection, plus the row reads and `sync`. */
826
880
  interface Bound<C extends StreamAddress.Collections, R> {
827
881
  readonly id: string;
@@ -831,21 +885,22 @@ interface Bound<C extends StreamAddress.Collections, R> {
831
885
  readonly inputs: {
832
886
  readonly state: Streamsy.Address<string, R>;
833
887
  };
834
- readonly accepted?: Streamsy.Address<Streamsy.WakeEvent, R>;
888
+ /** The frontier stream from `define`'s `frontier` option, if any. */
889
+ readonly accepted?: Streamsy.Address<Streamsy.FrontierEvent, R>;
835
890
  readonly commit: typeof transaction;
836
891
  readonly process: (batch: Slices<{
837
892
  readonly state: Streamsy.Address<string, R>;
838
- }>) => Effect.Effect<void, ProjectionFault, SqlClient.SqlClient | R>;
893
+ }>) => Effect.Effect<void, ProjectionFault, StateTableBackend | R>;
839
894
  /** The row of `key`. `value` is undefined when the row is absent. */
840
895
  readonly get: <K extends keyof C & string>(collection: K, key: string) => Effect.Effect<{
841
896
  readonly offset: string;
842
897
  readonly value: C[K]["schema"]["Type"] | undefined;
843
- }, Schema.SchemaError | SqlError.SqlError, SqlClient.SqlClient | C[K]["schema"]["DecodingServices"]>;
898
+ }, Schema.SchemaError | ReadError, StateTableBackend | C[K]["schema"]["DecodingServices"]>;
844
899
  /** The rows of `keys` that exist, by key. */
845
900
  readonly getMany: <K extends keyof C & string>(collection: K, keys: ReadonlyArray<string>) => Effect.Effect<{
846
901
  readonly offset: string;
847
902
  readonly rows: ReadonlyMap<string, C[K]["schema"]["Type"]>;
848
- }, Schema.SchemaError | SqlError.SqlError, SqlClient.SqlClient | C[K]["schema"]["DecodingServices"]>;
903
+ }, Schema.SchemaError | ReadError, StateTableBackend | C[K]["schema"]["DecodingServices"]>;
849
904
  /** The rows in key order. `next` is the last key of a full page. */
850
905
  readonly list: <K extends keyof C & string>(collection: K, options?: ListOptions) => Effect.Effect<{
851
906
  readonly offset: string;
@@ -854,11 +909,11 @@ interface Bound<C extends StreamAddress.Collections, R> {
854
909
  readonly value: C[K]["schema"]["Type"];
855
910
  }>;
856
911
  readonly next: string | undefined;
857
- }, ProjectionFault | Schema.SchemaError | SqlError.SqlError, SqlClient.SqlClient | C[K]["schema"]["DecodingServices"]>;
912
+ }, ProjectionFault | Schema.SchemaError | ReadError, StateTableBackend | C[K]["schema"]["DecodingServices"]>;
858
913
  /** The stream offset that the rows reflect. */
859
- readonly offset: () => Effect.Effect<string, SqlError.SqlError, SqlClient.SqlClient>;
914
+ readonly offset: () => Effect.Effect<string, ReadError, StateTableBackend>;
860
915
  /** Apply the stream from the head of the table to its tail, in the caller's transaction. */
861
- readonly sync: () => Effect.Effect<void, ProjectionFault, SqlClient.SqlClient | R>;
916
+ readonly sync: () => Effect.Effect<void, ProjectionFault, StateTableBackend | R>;
862
917
  }
863
918
  /** The declaration of a State table. Runners and `attach` take it like any declaration. */
864
919
  interface Declaration$1<Codecs extends StreamRoute.ParamCodecs, C extends StreamAddress.Collections, R> extends Omit<Declaration<Codecs, Bound<C, R>>, "inputs" | "parse"> {
@@ -873,15 +928,41 @@ type State<Codecs extends StreamRoute.ParamCodecs, C extends StreamAddress.Colle
873
928
  readonly at: (params: StreamRoute.Params<Codecs>) => StreamAddress.StateAddress<C> & Streamsy.Address<StreamAddress.CollectionsEvent<C>, R, StreamAddress.CollectionsDecodingServices<C>, StreamAddress.CollectionsEncodingServices<C>>;
874
929
  };
875
930
  /** A State stream's declaration, kept current by the ordinary projection runners. */
876
- declare function define<Codecs extends StreamRoute.ParamCodecs, C extends StreamAddress.Collections, R, W extends WakeOutput | undefined = undefined>(state: State<Codecs, C, R>, options?: {
877
- readonly accepted?: W & OneWithin<Codecs, W>;
878
- }): Declaration$1<Codecs, C, R | AcceptedServices<W>>;
931
+ declare function define<Codecs extends StreamRoute.ParamCodecs, C extends StreamAddress.Collections, R, W extends FrontierStream | undefined = undefined>(state: State<Codecs, C, R>, options?: {
932
+ /**
933
+ * A frontier stream that gets one row for the State stream at each applied offset, and the
934
+ * frontier rows that the State stream carries.
935
+ */
936
+ readonly frontier?: W & OneWithin<Codecs, W>;
937
+ }): Declaration$1<Codecs, C, R | FrontierServices<W>>;
879
938
  /** Delete cached rows and their applied offset, even if the State stream has been removed. */
880
- declare const clear: <C extends StreamAddress.Collections, R>(table: Bound<C, R>) => Effect.Effect<void, ProjectionFault, SqlClient.SqlClient | Transaction>;
939
+ declare const clear: <C extends StreamAddress.Collections, R>(table: Bound<C, R>) => Effect.Effect<void, ProjectionFault, StateTableBackend | Transaction>;
881
940
  /** Discard the index and replay the State stream atomically, retaining the binding checkpoint. */
882
- declare const rebuild: <C extends StreamAddress.Collections, R>(table: Bound<C, R>) => Effect.Effect<void, ProjectionFault, R | SqlClient.SqlClient | Transaction>;
941
+ declare const rebuild: <C extends StreamAddress.Collections, R>(table: Bound<C, R>) => Effect.Effect<void, ProjectionFault, R | StateTableBackend | Transaction>;
883
942
  /** Append a reset to the State stream. The next table run applies it. */
884
943
  declare const reset: <C extends StreamAddress.Collections, R>(table: Bound<C, R>) => Effect.Effect<import("@streamsy/core").AppendResult, Streams.AppendFailure<{}>, R>;
944
+ /** The rows of one State stream as one unit sees them, and the changes the unit makes. */
945
+ interface UnitRows<C extends StreamAddress.Collections> {
946
+ /** Read the stored rows of these keys into the unit, 50 keys for each query. */
947
+ readonly load: (collection: keyof C & string, keys: Iterable<string>) => Effect.Effect<void, ProjectionFault, StateTableBackend>;
948
+ /** The unit's own last write of the key, or else the stored row. A removed key gives undefined. */
949
+ readonly get: <K extends keyof C & string>(collection: K, key: string) => Effect.Effect<C[K]["schema"]["Type"] | undefined, ProjectionFault, StateTableBackend | C[K]["schema"]["DecodingServices"]>;
950
+ /** Record an upsert. False when the row equals what `get` returns, and then nothing is recorded. */
951
+ readonly upsert: <K extends keyof C & string>(collection: K, row: C[K]["schema"]["Type"], options?: {
952
+ readonly headers?: DurableState.ChangeHeaders;
953
+ }) => Effect.Effect<boolean, ProjectionFault, StateTableBackend | C[K]["schema"]["EncodingServices"]>;
954
+ /** Record a delete. False when the key has no row, and then nothing is recorded. */
955
+ readonly remove: (collection: keyof C & string, key: string, options?: {
956
+ readonly headers?: DurableState.ChangeHeaders;
957
+ }) => Effect.Effect<boolean, ProjectionFault, StateTableBackend>;
958
+ /** The recorded changes in call order. Return them under the output's name. */
959
+ readonly changes: () => ReadonlyArray<DurableState.Entry<C>>;
960
+ }
961
+ /**
962
+ * Create a missing unwrapped output, call `sync`, and open the unit's rows. Call it once
963
+ * for each unit, inside a `Commit.transaction` handler, and return `changes()`.
964
+ */
965
+ declare const unit: <C extends StreamAddress.Collections, R>(table: Bound<C, R>) => Effect.Effect<UnitRows<C>, ProjectionFault, R | StateTableBackend>;
885
966
  declare const FoldByKeyError_base: Schema.Class<FoldByKeyError, Schema.TaggedStruct<"FoldByKeyError", {
886
967
  readonly message: Schema.String;
887
968
  }>, import("effect/Cause").YieldableError & {
@@ -902,7 +983,7 @@ type Output<C extends StreamAddress.Collections, R> = StreamAddress.StateAddress
902
983
  */
903
984
  type Handler<C extends StreamAddress.Collections, O extends string, I extends InputMap, P, E, R, TR> = (batch: Slices<I>, unit: Unit<P>, context: {
904
985
  readonly outputs: Readonly<Record<O, Output<C, TR>>>;
905
- }) => Effect.Effect<void, E | FoldByKeyError | ProjectionFault, R | TR | SqlClient.SqlClient | StreamAddress.CollectionsDecodingServices<C> | StreamAddress.CollectionsEncodingServices<C>>;
986
+ }) => Effect.Effect<void, E | FoldByKeyError | ProjectionFault, R | TR | StateTableBackend | StreamAddress.CollectionsDecodingServices<C> | StreamAddress.CollectionsEncodingServices<C>>;
906
987
  interface Spec<C extends StreamAddress.Collections, K extends keyof C & string, O extends string, A, P, E, R> {
907
988
  readonly output: O;
908
989
  readonly collection: K;
@@ -964,4 +1045,4 @@ declare const attach: <I extends InputMap, E, R, Q, Result, P, OR = never, HE =
964
1045
  export declare const ProjectionWakeup: {
965
1046
  attach: typeof attach;
966
1047
  };
967
- export { type Accumulated, type AttachOptions, type Attachable, CheckpointRecord, CheckpointStore, type CheckpointStoreApi, commit_d_exports as Commit, type CurrentProjection, type DecideSpec, type Declaration, type Emits, type Emitted, type Entry, fold_by_key_d_exports as FoldByKey, type Folded, type FollowOptions, type Identity, type Initial, type InputMap, InvalidBinding, type ItemOf, type Loaded, NonRetryable, NotAttached, type Offsets, type OutputBatch, type OutputsContext, type PassResult, type ProcessContext, type Progress, projection_d_exports as Projection, ProjectionFault, type ProjectionKey, type Range, type RegistryApi, type RouteRecords, type RoutedHandle, type RoutedStreamRecord, type RunOptions, type RunnerFailure, type RunnerStatus, type Slice, type Slices, type Snapshot, type StateDefinition, type StateSchema, state_table_d_exports as StateTable, type Step, type Unit, type WakeAt, type WakeOutput };
1048
+ export { type Accumulated, type AttachOptions, type Attachable, CheckpointRecord, CheckpointStore, type CheckpointStoreApi, commit_d_exports as Commit, type CurrentProjection, type DecideSpec, type Declaration, type Emits, type Emitted, type Entry, fold_by_key_d_exports as FoldByKey, type Folded, type FollowOptions, type FrontierStream, type Identity, type Initial, type InputMap, InvalidBinding, type ItemOf, type Loaded, NonRetryable, NotAttached, type Offsets, type OutputBatch, type OutputsContext, type PassResult, type ProcessContext, type Progress, projection_d_exports as Projection, ProjectionFault, type ProjectionKey, type Range, type RegistryApi, type RouteRecords, type RoutedHandle, type RoutedStreamRecord, type RunOptions, type RunnerFailure, type RunnerStatus, type Slice, type Slices, type Snapshot, type StateDefinition, type StateSchema, state_table_d_exports as StateTable, type Step, type Unit, type WakeAt };