@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 1,591 lines 58.4 KB
Raw
1 import * as Cause from "effect/Cause";
2 import * as Context from "effect/Context";
3 import * as DateTime from "effect/DateTime";
4 import * as Effect from "effect/Effect";
5 import * as Exit from "effect/Exit";
6 import * as Schema from "effect/Schema";
7 import * as Aggregate from "./Aggregate.ts";
8 import type { Plan } from "./Backfill.ts";
9 import * as CatchUp from "./CatchUp.ts";
10 import * as Command from "./Command.ts";
11 import {
12 ConsumesItself,
13 DefinitionError,
14 DuplicateDefinition,
15 DuplicateEventTag,
16 DuplicateName,
17 InvalidInitial,
18 MissingInitial,
19 MissingKeyOf,
20 NoEmitter,
21 type Reason,
22 TwoWriters,
23 NewerGeneration,
24 UndeclaredEmit,
25 UndeclaredEvent,
26 UnhandledCommand,
27 UnknownEvent,
28 UnknownSource,
29 UnknownStream,
30 UnmatchedRoute,
31 WriterKeyMismatch,
32 } from "./DefinitionError.ts";
33 import type * as Event from "./Event.ts";
34 import { judge, rejectionTag, settle } from "./internal.ts";
35 import * as Key from "./Key.ts";
36 import type {
37 Accepted,
38 AggregateKit,
39 Delivery,
40 EnvelopeJson,
41 HandleResult,
42 Json,
43 PolicyKit,
44 ReceiptJson,
45 StoredEvent,
46 StreamEntry,
47 ViewEntry,
48 ViewKit,
49 ViewSnapshot,
50 } from "./Platform.ts";
51 import type * as Policy from "./Policy.ts";
52 import type * as Projection from "./Projection.ts";
53 import * as RequestContext from "./RequestContext.ts";
54 import type * as DomainStream from "./Stream.ts";
55 import * as Telemetry from "./Telemetry.ts";
56 import type * as View from "./View.ts";
57
58 /**
59 * The entities of a Domain.
60 */
61 export interface Definitions {
62 readonly aggregates: ReadonlyArray<Aggregate.Any>;
63 readonly views: ReadonlyArray<View.Any>;
64 readonly policies: ReadonlyArray<Policy.Any>;
65 readonly projections: ReadonlyArray<Projection.Any>;
66 readonly streams: ReadonlyArray<DomainStream.Any>;
67 }
68
69 /**
70 * What a projection input carries for `Stream.append`, out of sight of the
71 * handler's types: who is writing, where in its input log, and the source
72 * event being handled. Appends count up per input, so a re-run of the same
73 * input makes exactly the same writes.
74 *
75 * @internal
76 */
77 export const InputMeta = Symbol.for("alchemy/Domain/Projection/InputMeta");
78
79 /** @internal */
80 export interface InputMetaValue {
81 readonly projection: string;
82 readonly key: string;
83 readonly position: number;
84 readonly source: string;
85 readonly sourceSeq: number;
86 readonly at: number;
87 readonly commandId: string;
88 /** Appends made for this input so far in this run. */
89 appends: number;
90 }
91
92 export { DefinitionError };
93
94 interface JsonCodec {
95 readonly encode: (value: unknown) => Json;
96 readonly decode: (json: Json) => any;
97 }
98
99 const jsonCodec = (schema: Schema.Top): JsonCodec => {
100 const codec = Schema.toCodecJson(schema as any) as any;
101 return {
102 // oxlint-disable-next-line effecttsgo/schema-sync -- JsonCodec is sync by contract
103 encode: Schema.encodeUnknownSync(codec),
104 // oxlint-disable-next-line effecttsgo/schema-sync -- JsonCodec is sync by contract
105 decode: Schema.decodeUnknownSync(codec),
106 };
107 };
108
109 /** @internal */
110 export const encodeEnvelope = (
111 envelope: Aggregate.Envelope,
112 trace?: Telemetry.TraceJson,
113 ): EnvelopeJson => ({
114 id: envelope.id,
115 stream: envelope.stream,
116 seq: envelope.seq,
117 at: DateTime.toEpochMillis(envelope.at),
118 commandId: envelope.commandId,
119 correlationId: envelope.correlationId,
120 ...(trace ? { trace } : {}),
121 });
122
123 /** @internal */
124 export const decodeEnvelope = (json: EnvelopeJson): Aggregate.Envelope => ({
125 id: json.id,
126 stream: json.stream,
127 seq: json.seq,
128 at: DateTime.makeUnsafe(json.at),
129 commandId: json.commandId,
130 correlationId: json.correlationId ?? json.commandId,
131 });
132
133 /** The structural attributes of a delivery a reaction handles. */
134 const deliveryAttributes = (delivery: Delivery): Record<string, unknown> => ({
135 "domain.source": delivery.source,
136 "domain.seq": delivery.seq,
137 "domain.event": delivery.tag,
138 "domain.command_id": delivery.envelope.commandId,
139 "domain.correlation_id": delivery.envelope.correlationId ?? delivery.envelope.commandId,
140 });
141
142 /** A reaction to one delivery continues the trace of the commit that caused it. */
143 const parentOf = (delivery: Delivery) =>
144 delivery.envelope.trace ? Telemetry.spanOf(delivery.envelope.trace) : undefined;
145
146 /**
147 * The pure semantics shared by every platform: decide, evolve, reply,
148 * routing and view application, plus every codec.
149 */
150 export interface Kernel {
151 readonly aggregateKit: (
152 aggregate: Aggregate.Any,
153 deliver: (d: Delivery) => Effect.Effect<void>,
154 ) => Omit<AggregateKit, "push">;
155 readonly viewKit: (
156 view: View.Any,
157 deliver: (d: Delivery) => Effect.Effect<void>,
158 ) => Omit<ViewKit, "push">;
159 readonly policyKit: (policy: Policy.Any, handler: Policy.Handler<unknown>) => PolicyKit;
160 /** Decode a run of a projection key's input log into handler inputs. */
161 readonly projectionInputs: (
162 projection: Projection.Any,
163 key: string,
164 batch: ReadonlyArray<Accepted>,
165 ) => Effect.Effect<ReadonlyArray<unknown>>;
166 /** The keys of a projection an aggregate's committed event is routed to (empty if it is not consumed). */
167 readonly projectionKeys: (
168 projection: Projection.Any,
169 aggregate: string,
170 id: string,
171 event: { readonly _tag: string },
172 seq: number,
173 ) => ReadonlyArray<string>;
174 /** Every view and projection in the model, by `CatchUp.targetId`: what catches up. */
175 readonly targets: ReadonlyArray<string>;
176 /**
177 * Whether every key of a consumer (by `CatchUp.targetId`) hears from exactly one
178 * source instance, the one with its own id: it needs no merge and no barrier.
179 */
180 readonly singleSource: (target: string) => boolean;
181 /**
182 * The sources (aggregates and views, by name) a consumer reads, by
183 * `CatchUp.targetId`; `undefined` for one this model doesn't have.
184 */
185 readonly sourcesOf: (target: string) => ReadonlyArray<string> | undefined;
186 /** What a backfill needs to know of a consumer; `undefined` for one this model doesn't have. */
187 readonly planOf: (target: string) => Plan | undefined;
188 /**
189 * Route a source instance's stored rows (as its `log` returns them) to one
190 * consumer only, as history: each row's seq and its deliveries.
191 */
192 readonly history: (
193 target: string,
194 source: string,
195 id: string,
196 rows: ReadonlyArray<unknown>,
197 ) => Effect.Effect<
198 ReadonlyArray<{ readonly seq: number; readonly deliveries: ReadonlyArray<Delivery> }>
199 >;
200 /**
201 * Set a view's or projection's version (`view:Name` or `projection:Name`).
202 * Bumping it rebuilds the consumer, and every view or projection that reads
203 * it, from the history. Set every version before the platform starts.
204 */
205 readonly setVersion: (consumer: string, version: number) => void;
206 /** A consumer's generation: its version plus those of the views it reads. */
207 readonly generationOf: (kind: "view" | "projection", name: string) => number;
208 /** The generation of a consumer's storage: a view rebuilt in place keeps it. */
209 readonly classGenerationOf: (kind: "view" | "projection", name: string) => number;
210 /** A consumer's id, by `CatchUp.targetId`. */
211 readonly targetIdOf: (kind: "view" | "projection", name: string) => string;
212 /** Whether a view is rebuilt in place: keyed by the one aggregate it reads. */
213 readonly inPlace: (kind: "view" | "projection", name: string) => boolean;
214 /** The projection that writes a stream, if any. */
215 readonly writerOf: (stream: DomainStream.Any) => Projection.Any | undefined;
216 readonly decodeStreamEntry: (
217 entry: StreamEntry,
218 ) => Effect.Effect<DomainStream.Entry<unknown, string>>;
219 readonly encodeCommand: (
220 aggregate: Aggregate.Any,
221 command: { readonly _tag: string },
222 ) => Effect.Effect<Json>;
223 readonly decodeRejection: (aggregate: Aggregate.Any, json: Json) => unknown;
224 readonly decodeReceipt: (
225 aggregate: Aggregate.Any,
226 commandTag: string,
227 json: ReceiptJson,
228 ) => Effect.Effect<Aggregate.Receipt>;
229 readonly decodeState: (aggregate: Aggregate.Any, json: Json) => unknown;
230 readonly decodeViewState: (view: View.Any, json: Json) => unknown;
231 readonly decodeViewEntry: (
232 view: View.Any,
233 entry: ViewEntry,
234 ) => Effect.Effect<View.Entry<unknown, unknown>>;
235 readonly decodeEvent: (json: Json) => Effect.Effect<unknown>;
236 readonly encodeEvent: (event: { readonly _tag: string }) => Effect.Effect<Json>;
237 }
238
239 /**
240 * Build the kernel for a set of definitions. Validates tags and routing, and
241 * dies with a {@link DefinitionError} for the first problem.
242 */
243 export const make = (
244 definitions: Definitions,
245 telemetry: Telemetry.Resolved = Telemetry.resolve(undefined),
246 ): Effect.Effect<Kernel> =>
247 Effect.suspend(() => {
248 const { kernel, problems } = collect(definitions, telemetry);
249 return problems.length > 0 ? Effect.die(problems[0]) : Effect.succeed(kernel);
250 });
251
252 /**
253 * Every problem {@link make} would die with, without building anything.
254 * Empty when the definitions are valid.
255 */
256 export const verify = (definitions: Definitions): ReadonlyArray<DefinitionError> =>
257 collect(definitions, Telemetry.resolve(undefined)).problems;
258
259 /** Build the kernel and collect every definition problem, each once, in order. */
260 const collect = (definitions: Definitions, telemetry: Telemetry.Resolved) => {
261 const problems: Array<DefinitionError> = [];
262 const seen = new Set<string>();
263 const kernel = build(definitions, telemetry, (problem) => {
264 if (seen.has(problem.message)) return;
265 seen.add(problem.message);
266 problems.push(problem);
267 });
268 return { kernel, problems };
269 };
270
271 /**
272 * Build the kernel. Each definition problem goes to `report`, and the build
273 * goes on past it, so {@link verify} sees every problem.
274 */
275 const build = (
276 definitions: Definitions,
277 telemetry: Telemetry.Resolved,
278 report: (problem: DefinitionError) => void,
279 ): Kernel => {
280 const problem = (reason: Reason) => report(new DefinitionError({ reason }));
281 // ---------------------------------------------------------------- events
282 const eventCodecs = new Map<string, { readonly cls: Event.Any; readonly codec: JsonCodec }>();
283 const registerEvent = (cls: Event.Any) => {
284 const existing = eventCodecs.get(cls.tag);
285 if (existing && existing.cls !== cls) {
286 return problem(new DuplicateEventTag({ tag: cls.tag }));
287 }
288 if (!existing) eventCodecs.set(cls.tag, { cls, codec: jsonCodec(cls) });
289 };
290 for (const aggregate of definitions.aggregates) {
291 for (const event of aggregate.definition.events) registerEvent(event);
292 }
293 const eventCodec = (tag: string) => {
294 const entry = eventCodecs.get(tag);
295 return entry
296 ? Effect.succeed(entry.codec)
297 : Effect.die(new DefinitionError({ reason: new UnknownEvent({ event: tag }) }));
298 };
299 const encodeEvent = (event: { readonly _tag: string }): Effect.Effect<Json> =>
300 Effect.map(eventCodec(event._tag), (codec) => codec.encode(event));
301 const decodeEvent = (json: Json): Effect.Effect<unknown> =>
302 Effect.map(eventCodec((json as { _tag: string })._tag), (codec) => codec.decode(json));
303 /** The span attributes an event's declaration adds. */
304 const eventAttributes = (event: { readonly _tag: string }) =>
305 Telemetry.attributesOf(eventCodecs.get(event._tag)?.cls, event);
306
307 // ----------------------------------------------------- aggregate codecs
308 interface AggregateCodecs {
309 readonly state: JsonCodec;
310 readonly initial: Json;
311 readonly commands: Map<
312 string,
313 { readonly cls: Command.Any; readonly codec: JsonCodec; readonly reply?: JsonCodec }
314 >;
315 readonly rejections: Map<string, JsonCodec>;
316 readonly events: Set<string>;
317 }
318 const aggregateCodecs = new Map<string, AggregateCodecs>();
319 const aggregateByName = new Map<string, Aggregate.Any>();
320 for (const aggregate of definitions.aggregates) {
321 if (aggregateByName.has(aggregate.aggregateName)) {
322 problem(new DuplicateDefinition({ kind: "aggregate", entity: aggregate.aggregateName }));
323 continue;
324 }
325 aggregateByName.set(aggregate.aggregateName, aggregate);
326 const def = aggregate.definition;
327 const state = jsonCodec(def.state);
328 const commands = new Map<string, { cls: Command.Any; codec: JsonCodec; reply?: JsonCodec }>();
329 const rejections = new Map<string, JsonCodec>();
330 for (const command of def.commands) {
331 commands.set(command.tag, {
332 cls: command,
333 codec: jsonCodec(command),
334 reply: command.reply ? jsonCodec(command.reply) : undefined,
335 });
336 for (const rejection of command.rejects) {
337 const tag = rejectionTag(rejection);
338 if (!rejections.has(tag)) rejections.set(tag, jsonCodec(rejection));
339 }
340 }
341 const encodedInitial = Schema.encodeUnknownExit(Schema.toCodecJson(def.state as any) as any)(
342 def.initial,
343 );
344 const initial: Json = Exit.isSuccess(encodedInitial) ? encodedInitial.value : null;
345 if (Exit.isFailure(encodedInitial)) {
346 problem(
347 new InvalidInitial({
348 owner: aggregate.aggregateName,
349 cause: Cause.squash(encodedInitial.cause),
350 }),
351 );
352 }
353 aggregateCodecs.set(aggregate.aggregateName, {
354 state,
355 initial,
356 commands,
357 rejections,
358 events: new Set(def.events.map((e: Event.Any) => e.tag)),
359 });
360 }
361 const codecsOf = (aggregate: Aggregate.Any) => aggregateCodecs.get(aggregate.aggregateName)!;
362
363 // --------------------------------------------------------------- sources
364 type SourceEntry =
365 | { readonly kind: "aggregate"; readonly aggregate: Aggregate.Any }
366 | { readonly kind: "view"; readonly view: View.Any };
367 const sources = new Map<string, SourceEntry>();
368 const addSource = (name: string, entry: SourceEntry) => {
369 if (sources.has(name)) return problem(new DuplicateName({ entity: name }));
370 sources.set(name, entry);
371 };
372 for (const aggregate of definitions.aggregates) {
373 addSource(aggregate.aggregateName, { kind: "aggregate", aggregate });
374 }
375 for (const view of definitions.views) {
376 addSource(view.viewName, { kind: "view", view });
377 for (const event of view.definition.events ?? []) registerEvent(event);
378 }
379 const nameOf = (source: View.Source) =>
380 source.kind === "Aggregate"
381 ? (source as Aggregate.Any).aggregateName
382 : (source as View.Any).viewName;
383 const sourceEntry = (owner: string, source: View.Source): SourceEntry | undefined => {
384 const entry = sources.get(nameOf(source));
385 const entity = entry?.kind === "aggregate" ? entry.aggregate : entry?.view;
386 if (entity !== source) {
387 problem(new UnknownSource({ consumer: owner, source: nameOf(source) }));
388 return undefined;
389 }
390 return entry;
391 };
392 /** The key that identifies a source's instances: its aggregate, or a view's key. */
393 const keyOfSource = (entry: SourceEntry): Key.Any =>
394 entry.kind === "aggregate" ? entry.aggregate : entry.view.definition.key;
395
396 for (const stream of definitions.streams) {
397 if (sources.has(stream.streamName)) {
398 problem(new DuplicateName({ entity: stream.streamName }));
399 }
400 for (const event of stream.definition.entries) registerEvent(event);
401 }
402
403 /** Does a view take this event from this source? */
404 const consumes = (view: View.Any, tag: string, source: string) => {
405 const def = view.definition;
406 return Boolean(
407 def.evolve?.[tag] ||
408 def.evolve?.[source] ||
409 def.emit?.[tag] ||
410 def.emit?.[source] ||
411 def.events?.some((event) => event.tag === tag),
412 );
413 };
414 /** The tags of the events a source produces. */
415 const outputs = new Map<string, ReadonlySet<string>>();
416 const visiting = new Set<string>();
417 const outputTags = (entry: SourceEntry): ReadonlySet<string> => {
418 if (entry.kind === "aggregate") return codecsOf(entry.aggregate).events;
419 const view = entry.view;
420 const known = outputs.get(view.viewName);
421 if (known) return known;
422 if (view.definition.events) {
423 const declared = new Set(view.definition.events.map((event) => event.tag));
424 outputs.set(view.viewName, declared);
425 return declared;
426 }
427 if (visiting.has(view.viewName)) {
428 problem(new ConsumesItself({ view: view.viewName }));
429 return new Set();
430 }
431 visiting.add(view.viewName);
432 const tags = new Set<string>();
433 for (const source of view.definition.from) {
434 const upstream = sourceEntry(view.viewName, source);
435 if (!upstream) continue;
436 for (const tag of outputTags(upstream)) {
437 if (consumes(view, tag, nameOf(source))) tags.add(tag);
438 }
439 }
440 visiting.delete(view.viewName);
441 outputs.set(view.viewName, tags);
442 return tags;
443 };
444
445 // --------------------------------------------------------------- routing
446 /** What routing sees of an event. */
447 interface RouteInput {
448 readonly event: unknown;
449 /** The source instance: a ref, or a Schema-keyed view's key value. */
450 readonly source: unknown;
451 readonly envelope: Aggregate.Envelope;
452 readonly state: unknown;
453 /** The source stream, e.g. `Account/a-1`. */
454 readonly stream: string;
455 }
456 type Route = (input: RouteInput) => ReadonlyArray<{
457 readonly key: string;
458 readonly routes?: ReadonlyArray<string>;
459 }>;
460 interface Subscriber {
461 readonly kind: "view" | "policy" | "projection";
462 readonly name: string;
463 readonly route: Route;
464 }
465 type Handler = (...args: ReadonlyArray<unknown>) => unknown;
466 type HandlerMap = { readonly [key: string]: Handler | Record<string, Handler | undefined> };
467 const handlers = (value: unknown) => value as HandlerMap | undefined;
468
469 const subscribers = new Map<string, Map<string, Array<Subscriber>>>();
470 const subscribe = (source: string, tag: string, subscriber: Subscriber) => {
471 const byTag = subscribers.get(source) ?? new Map<string, Array<Subscriber>>();
472 subscribers.set(source, byTag);
473 const list = byTag.get(tag) ?? [];
474 byTag.set(tag, list);
475 list.push(subscriber);
476 };
477
478 /** Routes that key an event by its own source instance: key `x` hears only from instance `x`. */
479 const identityRoutes = new WeakSet<Route>();
480
481 /** How a consumer keyed by `def.key` routes `tag` from `source`. */
482 const keyRoute = (
483 owner: string,
484 def: { readonly key: Key.Any; readonly keyOf?: unknown },
485 upstream: SourceEntry,
486 source: string,
487 tag: string,
488 ): Route => {
489 const custom = handlers(def.keyOf)?.[tag] ?? handlers(def.keyOf)?.[source];
490 const routing = (input: RouteInput) => ({
491 event: input.event,
492 source: input.source,
493 state: input.state,
494 envelope: input.envelope,
495 });
496 // A route returns a ref for an aggregate key, or the value of a Schema key.
497 if (typeof custom === "function") {
498 return (input) => {
499 const ref = custom(routing(input));
500 return ref === undefined ? [] : [{ key: Aggregate.targetId(ref) }];
501 };
502 }
503 if (custom) {
504 const routes = Object.entries(custom);
505 return (input) => {
506 const byKey = new Map<string, Array<string>>();
507 for (const [route, fn] of routes) {
508 const ref = fn?.(routing(input));
509 if (ref === undefined) continue;
510 const key = Aggregate.targetId(ref);
511 const list = byKey.get(key) ?? [];
512 byKey.set(key, list);
513 list.push(route);
514 }
515 return [...byKey].map(([key, names]) => ({ key, routes: names }));
516 };
517 }
518 if (keyOfSource(upstream) === def.key) {
519 const route: Route = (input) => [{ key: Aggregate.targetId(input.source) }];
520 identityRoutes.add(route);
521 return route;
522 }
523 problem(
524 new MissingKeyOf({
525 consumer: owner,
526 event: tag,
527 source,
528 keyedBy: Key.isAggregate(def.key) ? def.key.aggregateName : undefined,
529 }),
530 );
531 return () => [];
532 };
533
534 for (const view of definitions.views) {
535 const def = view.definition;
536 if (def.state !== undefined && def.initial === undefined) {
537 problem(new MissingInitial({ view: view.viewName }));
538 }
539 // Route-keyed handlers need the same routes in keyOf.
540 for (const field of ["evolve", "emit"] as const) {
541 for (const [name, handler] of Object.entries(handlers(def[field]) ?? {})) {
542 if (typeof handler === "function" || !handler) continue;
543 const routes = handlers(def.keyOf)?.[name];
544 for (const route of Object.keys(handler)) {
545 if (typeof routes !== "object" || !(route in routes)) {
546 problem(
547 new UnmatchedRoute({ view: view.viewName, handler: field, source: name, route }),
548 );
549 }
550 }
551 }
552 }
553 for (const source of def.from) {
554 const upstream = sourceEntry(view.viewName, source);
555 if (!upstream) continue;
556 const name = nameOf(source);
557 for (const tag of outputTags(upstream)) {
558 if (!consumes(view, tag, name)) continue;
559 subscribe(name, tag, {
560 kind: "view",
561 name: view.viewName,
562 route: keyRoute(view.viewName, def, upstream, name, tag),
563 });
564 }
565 }
566 }
567
568 const fromList = (from: Policy.From): ReadonlyArray<View.Source> =>
569 Array.isArray(from) ? from : [from as View.Source];
570
571 /** Subscribe a consumer to the `on` events its sources produce. */
572 const subscribeConsumer = (
573 kind: "policy" | "projection",
574 owner: string,
575 from: ReadonlyArray<View.Source>,
576 on: ReadonlyArray<Event.Any>,
577 route: (upstream: SourceEntry, source: string, tag: string) => Route,
578 ) => {
579 for (const event of on) {
580 let produced = false;
581 for (const source of from) {
582 const upstream = sourceEntry(owner, source);
583 if (!upstream) {
584 produced = true; // already reported
585 continue;
586 }
587 if (!outputTags(upstream).has(event.tag)) continue;
588 produced = true;
589 const name = nameOf(source);
590 subscribe(name, event.tag, {
591 kind,
592 name: owner.replace(/^(Policy|Projection) /, ""),
593 route: route(upstream, name, event.tag),
594 });
595 }
596 if (!produced) {
597 problem(new NoEmitter({ consumer: owner, sources: from.map(nameOf), event: event.tag }));
598 }
599 }
600 };
601
602 const policyByName = new Map<string, Policy.Any>();
603 for (const policy of definitions.policies) {
604 if (policyByName.has(policy.policyName)) {
605 problem(new DuplicateDefinition({ kind: "policy", entity: policy.policyName }));
606 continue;
607 }
608 policyByName.set(policy.policyName, policy);
609 const def = policy.definition;
610 const owner = `Policy ${policy.policyName}`;
611 subscribeConsumer("policy", owner, fromList(def.from), def.on, (upstream, source, tag) =>
612 def.key
613 ? keyRoute(owner, { key: def.key, keyOf: def.keyOf }, upstream, source, tag)
614 : // Unkeyed: one partition per source instance, so its triggers run in order.
615 (input) => [{ key: input.stream }],
616 );
617 }
618
619 const projectionByName = new Map<string, Projection.Any>();
620 const writers = new Map<string, Projection.Any>();
621 const streamNames = new Set(definitions.streams.map((stream) => stream.streamName));
622 for (const projection of definitions.projections) {
623 const name = projection.projectionName;
624 if (projectionByName.has(name)) {
625 problem(new DuplicateDefinition({ kind: "projection", entity: name }));
626 continue;
627 }
628 projectionByName.set(name, projection);
629 const def = projection.definition;
630 const owner = `Projection ${name}`;
631 subscribeConsumer("projection", owner, def.from, def.on, (upstream, source, tag) =>
632 keyRoute(owner, def, upstream, source, tag),
633 );
634 for (const stream of def.writes ?? []) {
635 if (!streamNames.has(stream.streamName)) {
636 problem(new UnknownStream({ writer: owner, stream: stream.streamName }));
637 continue;
638 }
639 const existing = writers.get(stream.streamName);
640 if (existing && existing !== projection) {
641 problem(
642 new TwoWriters({
643 stream: stream.streamName,
644 first: existing.projectionName,
645 second: name,
646 }),
647 );
648 continue;
649 }
650 if (stream.definition.key !== def.key) {
651 problem(
652 new WriterKeyMismatch({
653 writer: owner,
654 key: Key.describe(def.key),
655 stream: stream.streamName,
656 streamKey: Key.describe(stream.definition.key),
657 }),
658 );
659 continue;
660 }
661 writers.set(stream.streamName, projection);
662 }
663 }
664
665 // ------------------------------------------------------------ generations
666 const versions = new Map<string, number>();
667 const generations = new Map<string, number>();
668 const viewSources = (from: ReadonlyArray<View.Source>) =>
669 from.flatMap((source) => (source.kind === "Aggregate" ? [] : [(source as View.Any).viewName]));
670 const viewByName = new Map(definitions.views.map((view) => [view.viewName, view]));
671 /** A consumer's own version: a view's from its declaration, a projection's from its Layer. */
672 const versionOf = (kind: "view" | "projection", name: string) =>
673 versions.get(`${kind}:${name}`) ??
674 (kind === "view" ? (viewByName.get(name)?.definition.version ?? 0) : 0);
675 /**
676 * A consumer's generation: its own version plus the generations of the
677 * views it reads, so rebuilding a view rebuilds everything built on it.
678 */
679 const generationOf = (kind: "view" | "projection" | "policy", name: string): number => {
680 if (kind === "policy") return 0;
681 const id = `${kind}:${name}`;
682 const known = generations.get(id);
683 if (known !== undefined) return known;
684 const from =
685 kind === "view"
686 ? viewByName.get(name)?.definition.from
687 : projectionByName.get(name)?.definition.from;
688 const generation =
689 versionOf(kind, name) +
690 viewSources(from ?? []).reduce((sum, view) => sum + generationOf("view", view), 0);
691 generations.set(id, generation);
692 return generation;
693 };
694 /**
695 * A view keyed by the one aggregate it reads is rebuilt in place: every key
696 * holds exactly its own instance's events, so it can read them again itself.
697 */
698 const inPlace = (kind: "view" | "projection" | "policy", name: string): boolean => {
699 if (kind !== "view") return false;
700 const from = viewByName.get(name)?.definition.from ?? [];
701 return (
702 from.length === 1 &&
703 from[0]!.kind === "Aggregate" &&
704 singleSource(CatchUp.targetId({ kind, name }))
705 );
706 };
707 /**
708 * The generation of a consumer's storage (its Durable Object class): its
709 * generation, except that a view rebuilt in place keeps its storage.
710 */
711 const classGenerationOf = (kind: "view" | "projection", name: string): number =>
712 inPlace(kind, name) ? 0 : generationOf(kind, name);
713 /** Where a subscriber's deliveries go, with its generation when it has one. */
714 const targetOf = (subscriber: Pick<Subscriber, "kind" | "name">): Delivery["target"] => {
715 const generation = generationOf(subscriber.kind, subscriber.name);
716 return {
717 kind: subscriber.kind,
718 name: subscriber.name,
719 ...(generation > 0 ? { generation } : {}),
720 ...(generation > 0 && inPlace(subscriber.kind, subscriber.name) ? { inPlace: true } : {}),
721 };
722 };
723 /** A consumer's id, by `CatchUp.targetId`, in this code. */
724 const targetIdOf = (kind: "view" | "projection", name: string) =>
725 CatchUp.targetId(targetOf({ kind, name }));
726
727 /** Every delivery an event produces; only to the consumers in `only`, if given. */
728 const fanOut = (
729 input: RouteInput,
730 delivery: Omit<Delivery, "target" | "key" | "routes">,
731 only?: ReadonlySet<string>,
732 ): Array<Delivery> => {
733 const out: Array<Delivery> = [];
734 for (const subscriber of subscribers.get(delivery.origin)?.get(delivery.tag) ?? []) {
735 const target = targetOf(subscriber);
736 if (only && !only.has(CatchUp.targetId(target))) continue;
737 for (const { key, routes } of subscriber.route(input)) {
738 out.push({
739 ...delivery,
740 target,
741 key,
742 ...(routes ? { routes } : {}),
743 });
744 }
745 }
746 return out;
747 };
748
749 /** The views and projections that consume a source: the consumers it catches up. */
750 const targetsOf = (source: string): ReadonlyArray<string> => {
751 const out = new Set<string>();
752 for (const list of subscribers.get(source)?.values() ?? []) {
753 for (const subscriber of list) {
754 if (subscriber.kind !== "policy") out.add(CatchUp.targetId(targetOf(subscriber)));
755 }
756 }
757 return [...out].sort();
758 };
759
760 /**
761 * Whether every key of a consumer has exactly one source: it hears only from
762 * the instance with its own id, of one source. Such a key has nothing to merge
763 * and nothing to wait for: it can take its history as soon as its source has it.
764 */
765 const singleSource = (target: string): boolean => {
766 const [kind, rest] = target.split(":") as [string, string];
767 const name = rest.replace(/[#@]\d+$/, "");
768 const sources = new Set<string>();
769 for (const [source, byTag] of subscribers) {
770 for (const list of byTag.values()) {
771 for (const subscriber of list) {
772 if (subscriber.kind !== kind || subscriber.name !== name) continue;
773 if (!identityRoutes.has(subscriber.route)) return false;
774 sources.add(source);
775 }
776 }
777 }
778 return sources.size === 1;
779 };
780
781 /**
782 * The sources (aggregates and views, by name) a consumer reads, by
783 * `CatchUp.targetId`; `undefined` for one this model doesn't have.
784 */
785 const sourcesOf = (target: string): ReadonlyArray<string> | undefined => {
786 const [kind, rest] = target.split(":") as [string, string];
787 const name = (rest ?? "").replace(/[#@]\d+$/, "");
788 const known =
789 kind === "view"
790 ? definitions.views.some((view) => view.viewName === name)
791 : definitions.projections.some((projection) => projection.projectionName === name);
792 if (!known) return undefined;
793 const sources = new Set<string>();
794 for (const [source, byTag] of subscribers) {
795 for (const list of byTag.values()) {
796 if (list.some((subscriber) => subscriber.kind === kind && subscriber.name === name))
797 sources.add(source);
798 }
799 }
800 return [...sources].sort();
801 };
802
803 const planOf = (target: string): Plan | undefined => {
804 const sources = sourcesOf(target);
805 if (sources === undefined) return undefined;
806 // Rebuilt views among its sources: their keys are the objects named for their generation.
807 const generations: Record<string, number> = {};
808 for (const source of sources) {
809 if (!definitions.views.some((view) => view.viewName === source)) continue;
810 // Where the source's keys are stored: a view rebuilt in place keeps its storage.
811 const generation = classGenerationOf("view", source);
812 if (generation > 0) generations[source] = generation;
813 }
814 return {
815 sources,
816 singleSource: singleSource(target),
817 ...(Object.keys(generations).length > 0 ? { generations } : {}),
818 };
819 };
820
821 /** Each source's replay, built once: what routes its stored rows as history. */
822 const replayers = new Map<
823 string,
824 {
825 readonly replay: (input: CatchUp.ReplayInput<any>) => Effect.Effect<CatchUp.ReplayResult>;
826 readonly seqOf: (row: any) => number;
827 }
828 >();
829 const replayerOf = (source: string) => {
830 let found = replayers.get(source);
831 if (found) return found;
832 const aggregate = definitions.aggregates.find((a) => a.aggregateName === source);
833 const view = definitions.views.find((v) => v.viewName === source);
834 if (aggregate)
835 found = {
836 replay: aggregateKit(aggregate, () => Effect.void).replay,
837 seqOf: (row: StoredEvent) => row.envelope.seq,
838 };
839 else if (view)
840 found = {
841 replay: viewKit(view, () => Effect.void).replay,
842 seqOf: (row: ViewEntry) => row.seq,
843 };
844 if (found) replayers.set(source, found);
845 return found;
846 };
847 const history = (
848 target: string,
849 source: string,
850 id: string,
851 rows: ReadonlyArray<unknown>,
852 ): Effect.Effect<
853 ReadonlyArray<{ readonly seq: number; readonly deliveries: ReadonlyArray<Delivery> }>
854 > => {
855 const replayer = replayerOf(source);
856 if (!replayer) return Effect.succeed([]);
857 const targets = targetsOf(source);
858 return Effect.forEach(rows, (row) => {
859 const seq = replayer.seqOf(row);
860 return Effect.map(
861 replayer.replay({
862 id,
863 routed: Object.fromEntries(
864 targets.map((t) => [t, t === target ? seq - 1 : Number.MAX_SAFE_INTEGER]),
865 ),
866 rows: [row],
867 }),
868 ({ deliveries }) => ({ seq, deliveries }),
869 );
870 });
871 };
872
873 /** Replay stored rows to the consumers lagging behind them, in order. */
874 const replay = Effect.fnUntraced(function* <Row>(
875 targets: ReadonlyArray<string>,
876 input: CatchUp.ReplayInput<Row>,
877 seqOf: (row: Row) => number,
878 route: (row: Row, only: ReadonlySet<string>) => Effect.Effect<ReadonlyArray<Delivery>>,
879 ) {
880 const routed: Record<string, number> = { ...input.routed };
881 const deliveries: Array<Delivery> = [];
882 for (const row of input.rows) {
883 const seq = seqOf(row);
884 const lagging = new Set(targets.filter((target) => (routed[target] ?? 0) < seq));
885 if (lagging.size === 0) continue;
886 deliveries.push(...(yield* route(row, lagging)));
887 for (const target of lagging) routed[target] = seq;
888 }
889 return { deliveries, routed } satisfies CatchUp.ReplayResult;
890 });
891
892 const viewCodecs = new Map<string, JsonCodec>();
893 const viewCodec = (view: View.Any) => {
894 let codec = viewCodecs.get(view.viewName);
895 if (!codec) viewCodecs.set(view.viewName, (codec = jsonCodec(view.definition.state!)));
896 return codec;
897 };
898 /** Decode the state a delivery carries from a view source. */
899 const sourceState = (entry: SourceEntry, json: Json) =>
900 json === undefined || json === null || entry.kind !== "view" || !entry.view.definition.state
901 ? json
902 : viewCodec(entry.view).decode(json);
903
904 // ------------------------------------------------------------------ kits
905 const aggregateKit = (
906 aggregate: Aggregate.Any,
907 deliver: (d: Delivery) => Effect.Effect<void>,
908 ): Omit<AggregateKit, "push"> => {
909 const def = aggregate.definition;
910 const codecs = codecsOf(aggregate);
911 // oxlint-disable-next-line effecttsgo/schema-sync -- ids decode synchronously when building refs
912 const decodeId = Schema.decodeUnknownSync(def.id as any) as (u: unknown) => unknown;
913 const commit = Effect.fnUntraced(function* (
914 id: string,
915 state: unknown,
916 version: number,
917 decided: ReadonlyArray<{ readonly _tag: string }>,
918 now: DateTime.Utc,
919 commandId: string,
920 history: boolean,
921 correlationId: string = commandId,
922 trace?: Telemetry.TraceJson,
923 ) {
924 const stream = `${aggregate.aggregateName}/${id}`;
925 const events: Array<StoredEvent> = [];
926 const deliveries: Array<Delivery> = [];
927 const sourceRef = Aggregate.makeRef(aggregate, id);
928 let next = state;
929 for (const [index, event] of decided.entries()) {
930 if (!codecs.events.has(event._tag)) {
931 return yield* Effect.die(
932 new DefinitionError({
933 reason: new UndeclaredEvent({
934 aggregate: aggregate.aggregateName,
935 event: event._tag,
936 }),
937 }),
938 );
939 }
940 const envelope: Aggregate.Envelope = {
941 id,
942 stream,
943 seq: version + index + 1,
944 at: now,
945 commandId,
946 correlationId,
947 };
948 next = (def.evolve as Record<string, (...args: any[]) => unknown>)[event._tag](
949 next,
950 event,
951 envelope,
952 );
953 const stored: StoredEvent = {
954 event: yield* encodeEvent(event),
955 envelope: encodeEnvelope(envelope, trace),
956 };
957 events.push(stored);
958 deliveries.push(
959 ...fanOut(
960 { event, source: sourceRef, envelope, state: undefined, stream },
961 {
962 source: stream,
963 seq: envelope.seq,
964 origin: aggregate.aggregateName,
965 tag: event._tag,
966 event: stored.event,
967 envelope: stored.envelope,
968 ...(history ? { history } : {}),
969 },
970 ),
971 );
972 }
973 return { next, events, deliveries };
974 });
975 return {
976 name: aggregate.aggregateName,
977 deliver,
978 // Read when used: versions are set once the whole model is built.
979 consumerSingleSource: (target: string) => singleSource(target),
980 get targets() {
981 return targetsOf(aggregate.aggregateName);
982 },
983 replay: (input) => {
984 const targets = targetsOf(aggregate.aggregateName);
985 const id = decodeId(input.id) as string;
986 const stream = `${aggregate.aggregateName}/${id}`;
987 const sourceRef = Aggregate.makeRef(aggregate, id);
988 return replay(
989 targets,
990 input,
991 (row) => row.envelope.seq,
992 (row, only) =>
993 Effect.map(decodeEvent(row.event), (event) =>
994 fanOut(
995 {
996 event,
997 source: sourceRef,
998 envelope: decodeEnvelope(row.envelope),
999 state: undefined,
1000 stream,
1001 },
1002 {
1003 source: stream,
1004 seq: row.envelope.seq,
1005 origin: aggregate.aggregateName,
1006 tag: (row.event as { readonly _tag: string })._tag,
1007 event: row.event,
1008 envelope: row.envelope,
1009 history: true,
1010 },
1011 only,
1012 ),
1013 ),
1014 );
1015 },
1016 seed: Effect.fnUntraced(function* (input) {
1017 const id = decodeId(input.id) as string;
1018 const state = codecs.state.decode(input.state ?? codecs.initial);
1019 const decided = (yield* Effect.forEach(input.events, decodeEvent)) as ReadonlyArray<{
1020 readonly _tag: string;
1021 }>;
1022 const result = yield* commit(
1023 id,
1024 state,
1025 input.version,
1026 decided,
1027 DateTime.makeUnsafe(input.now),
1028 input.commandId,
1029 true,
1030 );
1031 return {
1032 state: codecs.state.encode(result.next),
1033 version: input.version + decided.length,
1034 events: result.events,
1035 deliveries: result.deliveries,
1036 };
1037 }),
1038 handle: (input) => {
1039 const tag = (input.command as { readonly _tag: string })._tag;
1040 const correlationId = input.correlationId ?? input.commandId;
1041 const base = {
1042 "domain.aggregate": aggregate.aggregateName,
1043 "domain.command": tag,
1044 "domain.id": String(input.id),
1045 "domain.stream": `${aggregate.aggregateName}/${input.id}`,
1046 "domain.command_id": input.commandId,
1047 "domain.correlation_id": correlationId,
1048 };
1049 const decideAndCommit = Effect.gen(function* () {
1050 const command = codecs.commands.get(tag);
1051 if (!command) {
1052 return yield* Effect.die(
1053 new DefinitionError({
1054 reason: new UnhandledCommand({ aggregate: aggregate.aggregateName, command: tag }),
1055 }),
1056 );
1057 }
1058 const decoded = command.codec.decode(input.command) as { readonly _tag: string };
1059 yield* Effect.annotateCurrentSpan(Telemetry.attributesOf(command.cls, decoded));
1060 const state = codecs.state.decode(input.state ?? codecs.initial);
1061 const id = decodeId(input.id) as string;
1062 const now = DateTime.makeUnsafe(input.now);
1063 let decision = (def.decide as Record<string, (...args: any[]) => unknown>)[decoded._tag](
1064 state,
1065 decoded,
1066 {
1067 id,
1068 now,
1069 commandId: input.commandId,
1070 stream: `${aggregate.aggregateName}/${id}`,
1071 version: input.version,
1072 },
1073 );
1074 // An effectful decision runs here, inside the decide span, with the
1075 // context the command was sent with (its only requirement, enforced
1076 // by `Aggregate.Decision`).
1077 if (Effect.isEffect(decision)) {
1078 const context = yield* RequestContext.restore(input.context);
1079 const settled = settle(
1080 yield* Effect.exit(
1081 Effect.provideContext(decision, context as Context.Context<unknown>),
1082 ),
1083 );
1084 if (settled._tag === "Failed")
1085 return yield* Effect.failCause(settled.cause as Cause.Cause<never>);
1086 decision = settled.decision;
1087 }
1088 const judged = judge(aggregate.aggregateName, command.cls, decision);
1089 if (judged._tag === "Undeclared") return yield* Effect.die(judged.error);
1090 if (judged._tag === "Rejected") {
1091 const rejection = judged.rejection;
1092 const codec = codecs.rejections.get(rejection._tag)!;
1093 const declared = command.cls.rejects.find((r) => rejectionTag(r) === rejection._tag)!;
1094 yield* Effect.annotateCurrentSpan({
1095 "domain.outcome": rejection._tag,
1096 ...Telemetry.attributesOf(declared, rejection),
1097 });
1098 return { _tag: "Rejected", rejection: codec.encode(rejection) } as HandleResult;
1099 }
1100 const trace = yield* Telemetry.currentTrace;
1101 const { next, events, deliveries } = yield* commit(
1102 id,
1103 state,
1104 input.version,
1105 judged.events,
1106 now,
1107 input.commandId,
1108 false,
1109 correlationId,
1110 trace,
1111 );
1112 const replyFn = (def.reply as Record<string, (...args: any[]) => unknown> | undefined)?.[
1113 decoded._tag
1114 ];
1115 const reply =
1116 replyFn && command.reply ? command.reply.encode(replyFn(next, judged.events)) : null;
1117 const decided = judged.events;
1118 yield* Effect.annotateCurrentSpan({
1119 "domain.outcome": "accepted",
1120 "domain.events": decided.length,
1121 "domain.version": input.version + decided.length,
1122 });
1123 const span = yield* Effect.orElseSucceed(Effect.currentSpan, () => undefined);
1124 const at = BigInt(input.now) * 1_000_000n;
1125 for (const [index, event] of decided.entries()) {
1126 span?.event(event._tag, at, {
1127 "domain.seq": input.version + index + 1,
1128 ...eventAttributes(event),
1129 });
1130 yield* Telemetry.record(Telemetry.events, {
1131 "domain.aggregate": aggregate.aggregateName,
1132 "domain.event": event._tag,
1133 });
1134 }
1135 return {
1136 _tag: "Accepted",
1137 state: codecs.state.encode(next),
1138 version: input.version + decided.length,
1139 events,
1140 reply,
1141 deliveries,
1142 } as HandleResult;
1143 });
1144 return Effect.gen(function* () {
1145 const started = yield* Effect.sync(() => Date.now());
1146 const result = yield* decideAndCommit.pipe(
1147 Effect.annotateLogs(base),
1148 Effect.withSpan(`Domain.decide ${aggregate.aggregateName}.${tag}`, {
1149 kind: "server",
1150 attributes: base,
1151 ...(input.trace && !input.link ? { parent: Telemetry.spanOf(input.trace) } : {}),
1152 ...(input.trace && input.link
1153 ? { links: [{ span: Telemetry.spanOf(input.trace), attributes: {} }] }
1154 : {}),
1155 }),
1156 );
1157 const outcome =
1158 result._tag === "Accepted"
1159 ? "accepted"
1160 : ((result.rejection as { readonly _tag?: string })._tag ?? "rejected");
1161 const tags = { "domain.aggregate": aggregate.aggregateName, "domain.command": tag };
1162 yield* Telemetry.record(Telemetry.commands, { ...tags, "domain.outcome": outcome });
1163 yield* Telemetry.record(
1164 Telemetry.commandDuration,
1165 tags,
1166 (yield* Effect.sync(() => Date.now())) - started,
1167 );
1168 return result;
1169 });
1170 },
1171 };
1172 };
1173
1174 /** The handler for an input: by event tag, else by source name; per route if routed. */
1175 const pick = (
1176 map: HandlerMap | undefined,
1177 tag: string,
1178 source: string,
1179 route: string | undefined,
1180 ): Handler | undefined => {
1181 const handler = map?.[tag] ?? map?.[source];
1182 if (typeof handler === "function") return handler;
1183 return handler && route !== undefined ? handler[route] : undefined;
1184 };
1185
1186 const viewKit = (
1187 view: View.Any,
1188 deliver: (d: Delivery) => Effect.Effect<void>,
1189 ): Omit<ViewKit, "push"> => {
1190 const def = view.definition;
1191 const stateful = def.state !== undefined;
1192 const codec = stateful ? viewCodec(view) : undefined;
1193 const declared = def.events ? new Set(def.events.map((event) => event.tag)) : undefined;
1194 const evolve = handlers(def.evolve);
1195 const emit = handlers(def.emit);
1196 const encodeState = (state: unknown) =>
1197 codec && state !== null && state !== undefined ? codec.encode(state) : null;
1198 return {
1199 name: view.viewName,
1200 deliver,
1201 consumerSingleSource: (target: string) => singleSource(target),
1202 get targets() {
1203 return targetsOf(view.viewName);
1204 },
1205 get generation() {
1206 return generationOf("view", view.viewName);
1207 },
1208 get classGeneration() {
1209 return classGenerationOf("view", view.viewName);
1210 },
1211 get target() {
1212 return targetIdOf("view", view.viewName);
1213 },
1214 get singleSource() {
1215 return singleSource(`view:${view.viewName}`);
1216 },
1217 history: (target, source, id, rows) => history(target, source, id, rows),
1218 replay: (input) => {
1219 const targets = targetsOf(view.viewName);
1220 const keyRef = Key.refOf(def.key, input.id);
1221 const stream = `${view.viewName}/${input.id}`;
1222 return replay(
1223 targets,
1224 input,
1225 (row) => row.seq,
1226 (row, only) =>
1227 Effect.map(decodeEvent(row.event), (event) =>
1228 fanOut(
1229 {
1230 event,
1231 source: keyRef,
1232 envelope: decodeEnvelope(row.envelope),
1233 state: codec && row.state !== null ? codec.decode(row.state) : null,
1234 stream,
1235 },
1236 {
1237 source: stream,
1238 seq: row.seq,
1239 origin: view.viewName,
1240 tag: (row.event as { readonly _tag: string })._tag,
1241 event: row.event,
1242 envelope: row.envelope,
1243 state: row.state,
1244 history: true,
1245 },
1246 only,
1247 ),
1248 ),
1249 );
1250 },
1251 observe: (key, batch) => (effect) => {
1252 if (!telemetry.views || batch.every((d) => d.history)) return effect;
1253 const traces = batch.flatMap((d) => (d.envelope.trace ? [d.envelope.trace] : []));
1254 const single = batch.length === 1 && traces.length === 1;
1255 const attributes = {
1256 "domain.view": view.viewName,
1257 "domain.key": key,
1258 "domain.batch": batch.length,
1259 ...(batch.length === 1 ? deliveryAttributes(batch[0]!) : {}),
1260 };
1261 return effect.pipe(
1262 Effect.withSpan(`Domain.view ${view.viewName}`, {
1263 kind: "consumer",
1264 attributes,
1265 ...(single
1266 ? { parent: Telemetry.spanOf(traces[0]!) }
1267 : { links: traces.map((t) => ({ span: Telemetry.spanOf(t), attributes: {} })) }),
1268 }),
1269 Effect.tap(() =>
1270 Telemetry.record(Telemetry.runs, {
1271 "domain.kind": "view",
1272 "domain.consumer": view.viewName,
1273 "domain.outcome": "ok",
1274 }),
1275 ),
1276 );
1277 },
1278 apply: Effect.fnUntraced(function* (key, snapshot, batch) {
1279 let current: unknown =
1280 codec && snapshot.state !== null ? codec.decode(snapshot.state) : null;
1281 let { version, seq } = snapshot;
1282 const checkpoints: Record<string, number> = { ...snapshot.checkpoints };
1283 let changed = false;
1284 const entries: Array<ViewEntry> = [];
1285 const downstream: Array<Delivery> = [];
1286 const keyRef = Key.refOf(def.key, key);
1287 const stream = `${view.viewName}/${key}`;
1288 const generation = generationOf("view", view.viewName);
1289 for (const delivery of batch) {
1290 const sent = delivery.target.generation ?? 0;
1291 // For an earlier generation: sent by an instance still on older code.
1292 if (sent < generation) continue;
1293 if (sent > generation) {
1294 return yield* Effect.die(
1295 new DefinitionError({
1296 reason: new NewerGeneration({ consumer: view.viewName, sent, runs: generation }),
1297 }),
1298 );
1299 }
1300 if ((checkpoints[delivery.source] ?? 0) >= delivery.seq) continue;
1301 checkpoints[delivery.source] = delivery.seq;
1302 changed = true;
1303 const origin = sources.get(delivery.origin)!;
1304 const event = (yield* decodeEvent(delivery.event)) as { readonly _tag: string };
1305 const envelope = decodeEnvelope(delivery.envelope);
1306 const context = {
1307 key: keyRef,
1308 source: Key.refOf(keyOfSource(origin), envelope.id),
1309 state: sourceState(origin, delivery.state),
1310 envelope,
1311 at: envelope.at,
1312 };
1313 for (const route of delivery.routes ?? [undefined]) {
1314 const before = stateful ? (current ?? def.initial) : null;
1315 const evolveFn = stateful
1316 ? pick(evolve, delivery.tag, delivery.origin, route)
1317 : undefined;
1318 if (evolveFn) {
1319 current = evolveFn(before, event, context) ?? null;
1320 version += 1;
1321 }
1322 const emitFn = pick(emit, delivery.tag, delivery.origin, route);
1323 const emitted = !declared
1324 ? evolveFn
1325 ? [event]
1326 : []
1327 : emitFn
1328 ? [emitFn(event, { ...context, before, after: current })].flat()
1329 : declared.has(delivery.tag)
1330 ? [event]
1331 : [];
1332 for (const out of emitted as ReadonlyArray<{ readonly _tag: string } | undefined>) {
1333 if (out === undefined) continue;
1334 if (declared && !declared.has(out._tag)) {
1335 return yield* Effect.die(
1336 new DefinitionError({
1337 reason: new UndeclaredEmit({ view: view.viewName, event: out._tag }),
1338 }),
1339 );
1340 }
1341 seq += 1;
1342 const outEnvelope: Aggregate.Envelope = {
1343 id: key,
1344 stream,
1345 seq,
1346 at: envelope.at,
1347 commandId: envelope.commandId,
1348 correlationId: envelope.correlationId,
1349 };
1350 const entry: ViewEntry = {
1351 seq,
1352 event: yield* encodeEvent(out),
1353 envelope: encodeEnvelope(outEnvelope, delivery.envelope.trace),
1354 state: encodeState(current),
1355 };
1356 entries.push(entry);
1357 downstream.push(
1358 ...fanOut(
1359 { event: out, source: keyRef, envelope: outEnvelope, state: current, stream },
1360 {
1361 source: stream,
1362 seq,
1363 origin: view.viewName,
1364 tag: out._tag,
1365 event: entry.event,
1366 envelope: entry.envelope,
1367 state: entry.state,
1368 ...(delivery.history ? { history: true } : {}),
1369 },
1370 ),
1371 );
1372 }
1373 }
1374 }
1375 const next: ViewSnapshot = {
1376 state: encodeState(current),
1377 version,
1378 checkpoints,
1379 seq,
1380 ...(generation > 0 ? { generation } : {}),
1381 };
1382 return { snapshot: next, changed, entries, downstream };
1383 }),
1384 };
1385 };
1386
1387 const policyKit = (policy: Policy.Any, handler: Policy.Handler<unknown>): PolicyKit => {
1388 const policyKey = policy.definition.key;
1389 return {
1390 name: policy.policyName,
1391 run: (delivery) => {
1392 // Seeded history is the past; policies only react to what happens now.
1393 if (delivery.history) return Effect.void;
1394 const upstream = sources.get(delivery.origin)!;
1395 const envelope = decodeEnvelope(delivery.envelope);
1396 const source = Key.refOf(keyOfSource(upstream), envelope.id);
1397 const key = policyKey ? Key.refOf(policyKey, delivery.key) : source;
1398 const state = sourceState(upstream, delivery.state);
1399 const attributes = {
1400 "domain.policy": policy.policyName,
1401 "domain.key": delivery.key,
1402 ...deliveryAttributes(delivery),
1403 };
1404 const parent = parentOf(delivery);
1405 return Effect.flatMap(decodeEvent(delivery.event), (event) =>
1406 Effect.forEach(
1407 delivery.routes ?? [undefined],
1408 (route) => {
1409 let n = 0;
1410 const prefix = `${delivery.source}:${delivery.seq}:${policy.policyName}${route === undefined ? "" : `:${route}`}`;
1411 return handler({ key, route, source, event, envelope, state }).pipe(
1412 Effect.provideContext(
1413 Context.make(Aggregate.PolicyRun, { next: () => `${prefix}:${n++}` }).pipe(
1414 Context.add(Telemetry.Correlation, { id: envelope.correlationId }),
1415 ),
1416 ),
1417 );
1418 },
1419 { discard: true },
1420 ).pipe(
1421 Effect.tap(() =>
1422 Effect.annotateCurrentSpan(eventAttributes(event as { readonly _tag: string })),
1423 ),
1424 ),
1425 ).pipe(
1426 Effect.onExit((exit) =>
1427 Effect.gen(function* () {
1428 const now = yield* Effect.sync(() => Date.now());
1429 const tags = { "domain.kind": "policy", "domain.consumer": policy.policyName };
1430 yield* Telemetry.record(Telemetry.lag, tags, now - delivery.envelope.at);
1431 yield* Telemetry.record(Telemetry.runs, {
1432 ...tags,
1433 "domain.outcome": Exit.isSuccess(exit) ? "ok" : "failed",
1434 });
1435 }),
1436 ),
1437 Effect.annotateLogs(attributes),
1438 Effect.withSpan(`Domain.policy ${policy.policyName}`, {
1439 kind: "consumer",
1440 attributes,
1441 ...(parent ? { parent } : {}),
1442 }),
1443 );
1444 },
1445 };
1446 };
1447
1448 const projectionInputs = (
1449 projection: Projection.Any,
1450 key: string,
1451 batch: ReadonlyArray<Accepted>,
1452 ): Effect.Effect<ReadonlyArray<unknown>> =>
1453 Effect.gen(function* () {
1454 const keyRef = Key.refOf(projection.definition.key, key);
1455 const inputs: Array<unknown> = [];
1456 for (const { delivery, position } of batch) {
1457 const upstream = sources.get(delivery.origin)!;
1458 const envelope = decodeEnvelope(delivery.envelope);
1459 const source = Key.refOf(keyOfSource(upstream), envelope.id);
1460 const event = yield* decodeEvent(delivery.event);
1461 const state = sourceState(upstream, delivery.state);
1462 // Shared by every route the delivery takes, so their appends count up together.
1463 const meta: InputMetaValue = {
1464 projection: projection.projectionName,
1465 key,
1466 position,
1467 source: delivery.source,
1468 sourceSeq: delivery.seq,
1469 at: delivery.envelope.at,
1470 commandId: delivery.envelope.commandId,
1471 appends: 0,
1472 };
1473 for (const route of delivery.routes ?? [undefined]) {
1474 const input = { key: keyRef, route, source, event, envelope, state, position };
1475 Object.defineProperty(input, InputMeta, { value: meta, enumerable: false });
1476 inputs.push(input);
1477 }
1478 }
1479 return inputs;
1480 });
1481
1482 const projectionKeys = (
1483 projection: Projection.Any,
1484 aggregate: string,
1485 id: string,
1486 event: { readonly _tag: string },
1487 seq: number,
1488 ): ReadonlyArray<string> => {
1489 const entry = sources.get(aggregate);
1490 if (entry?.kind !== "aggregate") return [];
1491 const stream = `${aggregate}/${id}`;
1492 const envelope: Aggregate.Envelope = {
1493 id,
1494 stream,
1495 seq,
1496 at: DateTime.makeUnsafe(0),
1497 commandId: "",
1498 correlationId: "",
1499 };
1500 const keys: Array<string> = [];
1501 for (const subscriber of subscribers.get(aggregate)?.get(event._tag) ?? []) {
1502 if (subscriber.kind !== "projection" || subscriber.name !== projection.projectionName)
1503 continue;
1504 const input = {
1505 event,
1506 source: Aggregate.makeRef(entry.aggregate, id),
1507 envelope,
1508 state: undefined,
1509 stream,
1510 };
1511 for (const { key } of subscriber.route(input)) keys.push(key);
1512 }
1513 return keys;
1514 };
1515
1516 return {
1517 aggregateKit,
1518 viewKit,
1519 policyKit,
1520 projectionInputs,
1521 projectionKeys,
1522 get targets() {
1523 return [
1524 ...definitions.views.map((view) => targetIdOf("view", view.viewName)),
1525 ...definitions.projections.map((projection) =>
1526 targetIdOf("projection", projection.projectionName),
1527 ),
1528 ].sort();
1529 },
1530 singleSource,
1531 sourcesOf,
1532 planOf,
1533 history,
1534 setVersion: (consumer, version) => {
1535 versions.set(consumer, version);
1536 generations.clear();
1537 },
1538 generationOf,
1539 classGenerationOf,
1540 targetIdOf,
1541 inPlace,
1542 writerOf: (stream) => writers.get(stream.streamName),
1543 decodeStreamEntry: (entry) =>
1544 Effect.map(decodeEvent(entry.event), (event) => ({
1545 event,
1546 envelope: decodeEnvelope(entry.envelope),
1547 })),
1548 decodeEvent,
1549 encodeEvent,
1550 encodeCommand: (aggregate, command) => {
1551 const entry = codecsOf(aggregate).commands.get(command._tag);
1552 return entry
1553 ? Effect.sync(() => entry.codec.encode(command))
1554 : Effect.die(
1555 new DefinitionError({
1556 reason: new UnhandledCommand({
1557 aggregate: aggregate.aggregateName,
1558 command: command._tag,
1559 }),
1560 }),
1561 );
1562 },
1563 decodeRejection: (aggregate, json) =>
1564 codecsOf(aggregate)
1565 .rejections.get((json as { _tag: string })._tag)!
1566 .decode(json),
1567 decodeReceipt: (aggregate, commandTag, json) => {
1568 const reply = codecsOf(aggregate).commands.get(commandTag)?.reply;
1569 return Effect.map(Effect.forEach(json.events, decodeEvent), (events) => ({
1570 stream: json.stream,
1571 version: json.version,
1572 events,
1573 reply: reply && json.reply !== null ? reply.decode(json.reply) : undefined,
1574 })) as Effect.Effect<Aggregate.Receipt>;
1575 },
1576 decodeState: (aggregate, json) => {
1577 const codecs = codecsOf(aggregate);
1578 return codecs.state.decode(json ?? codecs.initial);
1579 },
1580 decodeViewState: (view, json) => viewCodec(view).decode(json),
1581 decodeViewEntry: (view, entry) =>
1582 Effect.map(decodeEvent(entry.event), (event) => ({
1583 event,
1584 state:
1585 entry.state === null || !view.definition.state
1586 ? null
1587 : viewCodec(view).decode(entry.state),
1588 envelope: decodeEnvelope(entry.envelope),
1589 })),
1590 };
1591 };