@rotorsoft/act 0.33.3 → 0.34.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/README.md +26 -2
- package/package.json +1 -1
- package/dist/.tsbuildinfo +0 -1
- package/dist/@types/act.d.ts +0 -598
- package/dist/@types/act.d.ts.map +0 -1
- package/dist/@types/adapters/console-logger.d.ts +0 -41
- package/dist/@types/adapters/console-logger.d.ts.map +0 -1
- package/dist/@types/adapters/in-memory-cache.d.ts +0 -34
- package/dist/@types/adapters/in-memory-cache.d.ts.map +0 -1
- package/dist/@types/adapters/in-memory-store.d.ts +0 -181
- package/dist/@types/adapters/in-memory-store.d.ts.map +0 -1
- package/dist/@types/adapters/index.d.ts +0 -4
- package/dist/@types/adapters/index.d.ts.map +0 -1
- package/dist/@types/builders/act-builder.d.ts +0 -160
- package/dist/@types/builders/act-builder.d.ts.map +0 -1
- package/dist/@types/builders/index.d.ts +0 -13
- package/dist/@types/builders/index.d.ts.map +0 -1
- package/dist/@types/builders/projection-builder.d.ts +0 -101
- package/dist/@types/builders/projection-builder.d.ts.map +0 -1
- package/dist/@types/builders/slice-builder.d.ts +0 -109
- package/dist/@types/builders/slice-builder.d.ts.map +0 -1
- package/dist/@types/builders/state-builder.d.ts +0 -424
- package/dist/@types/builders/state-builder.d.ts.map +0 -1
- package/dist/@types/config.d.ts +0 -119
- package/dist/@types/config.d.ts.map +0 -1
- package/dist/@types/index.d.ts +0 -14
- package/dist/@types/index.d.ts.map +0 -1
- package/dist/@types/internal/build-classify.d.ts +0 -44
- package/dist/@types/internal/build-classify.d.ts.map +0 -1
- package/dist/@types/internal/close-cycle.d.ts +0 -38
- package/dist/@types/internal/close-cycle.d.ts.map +0 -1
- package/dist/@types/internal/correlate-cycle.d.ts +0 -73
- package/dist/@types/internal/correlate-cycle.d.ts.map +0 -1
- package/dist/@types/internal/drain-cycle.d.ts +0 -113
- package/dist/@types/internal/drain-cycle.d.ts.map +0 -1
- package/dist/@types/internal/drain-ratio.d.ts +0 -26
- package/dist/@types/internal/drain-ratio.d.ts.map +0 -1
- package/dist/@types/internal/drain.d.ts +0 -41
- package/dist/@types/internal/drain.d.ts.map +0 -1
- package/dist/@types/internal/event-sourcing.d.ts +0 -96
- package/dist/@types/internal/event-sourcing.d.ts.map +0 -1
- package/dist/@types/internal/index.d.ts +0 -29
- package/dist/@types/internal/index.d.ts.map +0 -1
- package/dist/@types/internal/merge.d.ts +0 -31
- package/dist/@types/internal/merge.d.ts.map +0 -1
- package/dist/@types/internal/reactions.d.ts +0 -54
- package/dist/@types/internal/reactions.d.ts.map +0 -1
- package/dist/@types/internal/settle.d.ts +0 -60
- package/dist/@types/internal/settle.d.ts.map +0 -1
- package/dist/@types/internal/tracing.d.ts +0 -45
- package/dist/@types/internal/tracing.d.ts.map +0 -1
- package/dist/@types/lru-map.d.ts +0 -50
- package/dist/@types/lru-map.d.ts.map +0 -1
- package/dist/@types/ports.d.ts +0 -196
- package/dist/@types/ports.d.ts.map +0 -1
- package/dist/@types/signals.d.ts +0 -2
- package/dist/@types/signals.d.ts.map +0 -1
- package/dist/@types/types/action.d.ts +0 -444
- package/dist/@types/types/action.d.ts.map +0 -1
- package/dist/@types/types/errors.d.ts +0 -284
- package/dist/@types/types/errors.d.ts.map +0 -1
- package/dist/@types/types/index.d.ts +0 -39
- package/dist/@types/types/index.d.ts.map +0 -1
- package/dist/@types/types/ports.d.ts +0 -485
- package/dist/@types/types/ports.d.ts.map +0 -1
- package/dist/@types/types/reaction.d.ts +0 -291
- package/dist/@types/types/reaction.d.ts.map +0 -1
- package/dist/@types/types/registry.d.ts +0 -74
- package/dist/@types/types/registry.d.ts.map +0 -1
- package/dist/@types/types/schemas.d.ts +0 -117
- package/dist/@types/types/schemas.d.ts.map +0 -1
- package/dist/@types/utils.d.ts +0 -54
- package/dist/@types/utils.d.ts.map +0 -1
- package/dist/chunk-AGWZY6YT.js +0 -127
- package/dist/chunk-AGWZY6YT.js.map +0 -1
- package/dist/index.cjs +0 -2985
- package/dist/index.cjs.map +0 -1
- package/dist/index.js +0 -2816
- package/dist/index.js.map +0 -1
- package/dist/types/index.cjs +0 -166
- package/dist/types/index.cjs.map +0 -1
- package/dist/types/index.js +0 -33
- package/dist/types/index.js.map +0 -1
package/dist/index.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/adapters/console-logger.ts","../src/lru-map.ts","../src/adapters/in-memory-cache.ts","../src/types/errors.ts","../src/utils.ts","../src/config.ts","../src/types/schemas.ts","../src/types/index.ts","../src/adapters/in-memory-store.ts","../src/ports.ts","../src/signals.ts","../src/act.ts","../src/internal/build-classify.ts","../src/internal/close-cycle.ts","../src/internal/correlate-cycle.ts","../src/internal/drain-cycle.ts","../src/internal/drain-ratio.ts","../src/internal/merge.ts","../src/internal/reactions.ts","../src/internal/settle.ts","../src/internal/drain.ts","../src/internal/event-sourcing.ts","../src/internal/tracing.ts","../src/builders/act-builder.ts","../src/builders/projection-builder.ts","../src/builders/slice-builder.ts","../src/builders/state-builder.ts"],"sourcesContent":["import \"./signals.js\";\n\n/**\n * @packageDocumentation\n * @module act\n * Main entry point for the Act framework. Re-exports all core APIs.\n */\nexport * from \"./act.js\";\nexport * from \"./adapters/index.js\";\nexport * from \"./builders/index.js\";\nexport * from \"./config.js\";\nexport * from \"./ports.js\";\nexport * from \"./types/index.js\";\nexport * from \"./utils.js\";\n","/**\n * @module adapters/console-logger\n *\n * High-performance console logger inspired by pino's design:\n * - Numeric level comparison for O(1) gating\n * - stdout.write() in production for raw JSON lines (no console overhead)\n * - Colorized single-line output in development\n * - No-op method replacement when level is above threshold\n * - Child logger support with merged bindings\n */\nimport type { Logger } from \"../types/index.js\";\n\nconst LEVEL_VALUES: Record<string, number> = {\n fatal: 60,\n error: 50,\n warn: 40,\n info: 30,\n debug: 20,\n trace: 10,\n};\n\nconst LEVEL_COLORS: Record<string, string> = {\n fatal: \"\\x1b[41m\\x1b[37m\", // white on red bg\n error: \"\\x1b[31m\", // red\n warn: \"\\x1b[33m\", // yellow\n info: \"\\x1b[32m\", // green\n debug: \"\\x1b[36m\", // cyan\n trace: \"\\x1b[90m\", // gray\n};\n\nconst RESET = \"\\x1b[0m\";\n\nconst noop = () => {};\n\n/**\n * Default console logger for the Act framework.\n *\n * Production mode emits newline-delimited JSON (compatible with GCP, AWS\n * CloudWatch, Datadog, and other structured log ingestion systems).\n *\n * Development mode emits colorized, human-readable output.\n */\nexport class ConsoleLogger implements Logger {\n level: string;\n private readonly _pretty: boolean;\n\n readonly fatal: Logger[\"fatal\"];\n readonly error: Logger[\"error\"];\n readonly warn: Logger[\"warn\"];\n readonly info: Logger[\"info\"];\n readonly debug: Logger[\"debug\"];\n readonly trace: Logger[\"trace\"];\n\n constructor(\n options: {\n level?: string;\n pretty?: boolean;\n bindings?: Record<string, unknown>;\n } = {}\n ) {\n const {\n level = \"info\",\n pretty = process.env.NODE_ENV !== \"production\",\n bindings,\n } = options;\n this._pretty = pretty;\n this.level = level;\n\n const threshold = LEVEL_VALUES[level] ?? 30;\n const write = pretty\n ? this._prettyWrite.bind(this, bindings)\n : this._jsonWrite.bind(this, bindings);\n\n // Assign methods — noop when level is gated (like pino's level-based replacement)\n this.fatal = write.bind(this, \"fatal\", 60); // fatal is always enabled\n this.error = threshold <= 50 ? write.bind(this, \"error\", 50) : noop;\n this.warn = threshold <= 40 ? write.bind(this, \"warn\", 40) : noop;\n this.info = threshold <= 30 ? write.bind(this, \"info\", 30) : noop;\n this.debug = threshold <= 20 ? write.bind(this, \"debug\", 20) : noop;\n this.trace = threshold <= 10 ? write.bind(this, \"trace\", 10) : noop;\n }\n\n /** No-op — `console.log` has no resources to release. */\n async dispose(): Promise<void> {}\n\n /** @inheritDoc */\n child(bindings: Record<string, unknown>): Logger {\n return new ConsoleLogger({\n level: this.level,\n pretty: this._pretty,\n bindings,\n });\n }\n\n private _jsonWrite(\n bindings: Record<string, unknown> | undefined,\n level: string,\n _num: number,\n objOrMsg: unknown,\n msg?: string\n ): void {\n let obj: Record<string, unknown>;\n let message: string | undefined;\n\n if (typeof objOrMsg === \"string\") {\n message = objOrMsg;\n obj = {};\n } else if (objOrMsg !== null && typeof objOrMsg === \"object\") {\n message = msg;\n obj = { ...(objOrMsg as Record<string, unknown>) };\n } else {\n message = msg;\n obj = { value: objOrMsg };\n }\n\n const entry = Object.assign({ level, time: Date.now() }, bindings, obj);\n if (message) entry.msg = message;\n\n let line: string;\n try {\n line = JSON.stringify(entry);\n } catch {\n // Cyclic or unserializable payload — emit a minimal line rather\n // than crash the log call site.\n line = JSON.stringify({\n level,\n time: entry.time,\n msg: message ?? \"[unserializable]\",\n unserializable: true,\n });\n }\n process.stdout.write(line + \"\\n\");\n }\n\n private _prettyWrite(\n bindings: Record<string, unknown> | undefined,\n level: string,\n _num: number,\n objOrMsg: unknown,\n msg?: string\n ): void {\n const color = LEVEL_COLORS[level];\n const tag = `${color}${level.toUpperCase().padEnd(5)}${RESET}`;\n const ts = new Date().toISOString().slice(11, 23); // HH:mm:ss.SSS\n\n let message: string;\n let data: string | undefined;\n\n if (typeof objOrMsg === \"string\") {\n message = objOrMsg;\n } else {\n message = msg ?? \"\";\n if (objOrMsg !== undefined && objOrMsg !== null) {\n try {\n data = JSON.stringify(objOrMsg);\n } catch {\n data = \"[unserializable]\";\n }\n }\n }\n\n const bindStr =\n bindings && Object.keys(bindings).length\n ? ` ${JSON.stringify(bindings)}`\n : \"\";\n\n const parts = [ts, tag, message, data, bindStr].filter(Boolean);\n process.stdout.write(parts.join(\" \") + \"\\n\");\n }\n}\n","/**\n * @module lru-map\n * @category Internal\n *\n * Tiny bounded LRU map / set built on insertion-ordered `Map`. Used to cap\n * memory in long-running orchestrators that mint large numbers of keys —\n * notably:\n *\n * - {@link InMemoryCache}: stream → state checkpoint\n * - `Act._subscribed_streams`: stream → presence (LruSet)\n *\n * Apps with millions of dynamic streams (one target per aggregate) can't\n * afford an unbounded `Set<string>` — eviction is required.\n *\n * @internal\n */\n\n/**\n * Bounded LRU map. `get()` promotes; `has()` does not. `set()` always\n * promotes and evicts the oldest entry when at capacity.\n *\n * @internal\n */\nexport class LruMap<K, V> {\n private readonly _entries = new Map<K, V>();\n\n constructor(private readonly _maxSize: number) {}\n\n get(key: K): V | undefined {\n const v = this._entries.get(key);\n if (v === undefined) return undefined;\n // promote: delete + re-insert moves to most-recent position\n this._entries.delete(key);\n this._entries.set(key, v);\n return v;\n }\n\n has(key: K): boolean {\n return this._entries.has(key);\n }\n\n set(key: K, value: V): void {\n this._entries.delete(key);\n if (this._entries.size >= this._maxSize) {\n // size >= maxSize ≥ 1 → at least one entry exists → next().value\n // is the oldest key (asserted with `!`).\n const oldest = this._entries.keys().next().value!;\n this._entries.delete(oldest);\n }\n this._entries.set(key, value);\n }\n\n delete(key: K): boolean {\n return this._entries.delete(key);\n }\n\n clear(): void {\n this._entries.clear();\n }\n\n get size(): number {\n return this._entries.size;\n }\n}\n\n/**\n * Bounded LRU set built on top of {@link LruMap}. `has()` does not promote;\n * `add()` does (re-inserting if already present, evicting the oldest at\n * capacity).\n *\n * @internal\n */\nexport class LruSet<T> {\n private readonly _map: LruMap<T, true>;\n\n constructor(maxSize: number) {\n this._map = new LruMap(maxSize);\n }\n\n has(value: T): boolean {\n return this._map.has(value);\n }\n\n add(value: T): void {\n this._map.set(value, true);\n }\n\n delete(value: T): boolean {\n return this._map.delete(value);\n }\n\n clear(): void {\n this._map.clear();\n }\n\n get size(): number {\n return this._map.size;\n }\n}\n","import { LruMap } from \"../lru-map.js\";\nimport type { Cache, CacheEntry, Schema } from \"../types/index.js\";\n\n/**\n * In-memory LRU cache for stream snapshots.\n *\n * Backed by an internal `LruMap` for O(1) get/set with LRU eviction.\n * Configurable `maxSize` bounds memory usage.\n *\n * @example\n * ```typescript\n * import { cache } from \"@rotorsoft/act\";\n * import { InMemoryCache } from \"@rotorsoft/act\";\n *\n * cache(new InMemoryCache({ maxSize: 500 }));\n * ```\n */\n/* eslint-disable @typescript-eslint/require-await -- async interface for Redis-compatibility */\nexport class InMemoryCache implements Cache {\n // CacheEntry<any> lets `get<TState>` and `set<TState>` flow without casts:\n // any is bidirectionally compatible with the per-call TState binding, while\n // the public Cache interface still presents a typed surface to callers.\n private readonly _entries: LruMap<string, CacheEntry<any>>;\n\n constructor(options?: { maxSize?: number }) {\n this._entries = new LruMap(options?.maxSize ?? 1000);\n }\n\n /** @inheritDoc */\n async get<TState extends Schema>(\n stream: string\n ): Promise<CacheEntry<TState> | undefined> {\n return this._entries.get(stream);\n }\n\n /** @inheritDoc */\n async set<TState extends Schema>(\n stream: string,\n entry: CacheEntry<TState>\n ): Promise<void> {\n this._entries.set(stream, entry);\n }\n\n /** @inheritDoc */\n async invalidate(stream: string): Promise<void> {\n this._entries.delete(stream);\n }\n\n /** @inheritDoc */\n async clear(): Promise<void> {\n this._entries.clear();\n }\n\n /** @inheritDoc */\n async dispose(): Promise<void> {\n this._entries.clear();\n }\n\n /** Current number of entries held by the LRU. */\n get size(): number {\n return this._entries.size;\n }\n}\n","import type {\n Actor,\n Message,\n Schema,\n Schemas,\n Snapshot,\n Target,\n} from \"./action.js\";\n\n/**\n * @packageDocumentation\n * @module act/types\n * @category Types\n * Application error type constants and error classes for the Act Framework.\n *\n * - `ERR_VALIDATION`: Schema validation error\n * - `ERR_INVARIANT`: Invariant validation error\n * - `ERR_CONCURRENCY`: Optimistic concurrency validation error on commits\n */\nexport const Errors = {\n ValidationError: \"ERR_VALIDATION\",\n InvariantError: \"ERR_INVARIANT\",\n ConcurrencyError: \"ERR_CONCURRENCY\",\n StreamClosedError: \"ERR_STREAM_CLOSED\",\n} as const;\n\n/**\n * Thrown when an action or event payload fails Zod schema validation.\n *\n * This error indicates that data doesn't match the expected schema defined\n * for an action or event. The `details` property contains the Zod validation\n * error with specific information about what failed.\n *\n * @example Catching validation errors\n * ```typescript\n * import { ValidationError } from \"@rotorsoft/act\";\n *\n * try {\n * await app.do(\"createUser\", target, {\n * email: \"invalid-email\", // Missing @ symbol\n * age: -5 // Negative age\n * });\n * } catch (error) {\n * if (error instanceof ValidationError) {\n * console.error(\"Validation failed for:\", error.target);\n * console.error(\"Invalid payload:\", error.payload);\n * console.error(\"Validation details:\", error.details);\n * // details contains Zod error with field-level info\n * }\n * }\n * ```\n *\n * @example Logging validation details\n * ```typescript\n * try {\n * await app.do(\"updateProfile\", target, payload);\n * } catch (error) {\n * if (error instanceof ValidationError) {\n * error.details.errors.forEach((err) => {\n * console.error(`Field ${err.path.join(\".\")}: ${err.message}`);\n * });\n * }\n * }\n * ```\n *\n * @see {@link https://zod.dev | Zod documentation} for validation details\n */\nexport class ValidationError extends Error {\n constructor(\n /** The type of target being validated (e.g., \"action\", \"event\") */\n public readonly target: string,\n /** The invalid payload that failed validation */\n public readonly payload: any,\n /** Zod validation error details */\n public readonly details: any\n ) {\n super(`Invalid ${target} payload`);\n this.name = Errors.ValidationError;\n }\n}\n\n/**\n * Thrown when a business rule (invariant) is violated during action execution.\n *\n * Invariants are conditions that must hold true for an action to succeed.\n * They're checked after loading the current state but before emitting events.\n * This error provides complete context about what action was attempted and\n * why it was rejected.\n *\n * @template TState - State schema type\n * @template TEvents - Event schemas type\n * @template TActions - Action schemas type\n * @template TKey - Action name\n * @template TActor - Actor type extending base Actor\n *\n * @example Catching invariant violations\n * ```typescript\n * import { InvariantError } from \"@rotorsoft/act\";\n *\n * try {\n * await app.do(\"withdraw\",\n * { stream: \"account-123\", actor: { id: \"user1\", name: \"Alice\" } },\n * { amount: 1000 }\n * );\n * } catch (error) {\n * if (error instanceof InvariantError) {\n * console.error(\"Action:\", error.action);\n * console.error(\"Reason:\", error.description);\n * console.error(\"Current state:\", error.snapshot.state);\n * console.error(\"Attempted payload:\", error.payload);\n * }\n * }\n * ```\n *\n * @example User-friendly error messages\n * ```typescript\n * try {\n * await app.do(\"closeTicket\", target, payload);\n * } catch (error) {\n * if (error instanceof InvariantError) {\n * // Present friendly message to user\n * if (error.description === \"Ticket must be open\") {\n * return { error: \"This ticket is already closed\" };\n * } else if (error.description === \"Not authorized\") {\n * return { error: \"You don't have permission to close this ticket\" };\n * }\n * }\n * }\n * ```\n *\n * @example Logging with context\n * ```typescript\n * try {\n * await app.do(\"transfer\", target, { to: \"account2\", amount: 500 });\n * } catch (error) {\n * if (error instanceof InvariantError) {\n * logger.error({\n * action: error.action,\n * stream: error.target.stream,\n * actor: error.target.actor,\n * reason: error.description,\n * balance: error.snapshot.state.balance,\n * attempted: error.payload.amount\n * }, \"Invariant violation\");\n * }\n * }\n * ```\n *\n * @see {@link Invariant} for defining business rules\n */\nexport class InvariantError<\n TState extends Schema,\n TEvents extends Schemas,\n TActions extends Schemas,\n TKey extends keyof TActions,\n TActor extends Actor = Actor,\n> extends Error {\n constructor(\n /** The action that was attempted */\n readonly action: TKey,\n /** The action payload that was provided */\n readonly payload: Readonly<TActions[TKey]>,\n /** The target stream and actor context */\n readonly target: Target<TActor>,\n /** The current state snapshot when invariant was checked */\n readonly snapshot: Snapshot<TState, TEvents>,\n /** Human-readable description of why the invariant failed */\n readonly description: string\n ) {\n super(`${action as string} failed invariant: ${description}`);\n this.name = Errors.InvariantError;\n }\n}\n\n/**\n * Thrown when optimistic concurrency control detects a conflict.\n *\n * This error occurs when trying to commit events to a stream that has been\n * modified by another process since it was last loaded. The version number\n * doesn't match expectations, indicating a concurrent modification.\n *\n * This is a normal occurrence in distributed systems and should be handled\n * by reloading the current state and retrying the action.\n *\n * @example Handling concurrency conflicts with retry\n * ```typescript\n * import { ConcurrencyError } from \"@rotorsoft/act\";\n *\n * async function transferWithRetry(from, to, amount, maxRetries = 3) {\n * for (let attempt = 0; attempt < maxRetries; attempt++) {\n * try {\n * await app.do(\"transfer\",\n * { stream: from, actor: currentUser },\n * { to, amount }\n * );\n * return { success: true };\n * } catch (error) {\n * if (error instanceof ConcurrencyError) {\n * if (attempt < maxRetries - 1) {\n * console.log(`Concurrent modification detected, retrying... (${attempt + 1}/${maxRetries})`);\n * await sleep(100 * Math.pow(2, attempt)); // Exponential backoff\n * continue;\n * }\n * }\n * throw error;\n * }\n * }\n * return { success: false, reason: \"Too many concurrent modifications\" };\n * }\n * ```\n *\n * @example Logging concurrency conflicts\n * ```typescript\n * try {\n * await app.do(\"updateInventory\", target, payload);\n * } catch (error) {\n * if (error instanceof ConcurrencyError) {\n * logger.warn({\n * stream: error.stream,\n * expectedVersion: error.expectedVersion,\n * actualVersion: error.lastVersion,\n * events: error.events.map(e => e.name)\n * }, \"Concurrent modification detected\");\n * }\n * }\n * ```\n *\n * @example User feedback for conflicts\n * ```typescript\n * try {\n * await app.do(\"editDocument\", target, { content: newContent });\n * } catch (error) {\n * if (error instanceof ConcurrencyError) {\n * return {\n * error: \"This document was modified by another user. Please refresh and try again.\",\n * code: \"CONCURRENT_MODIFICATION\"\n * };\n * }\n * }\n * ```\n *\n * @see {@link Store.commit} for version checking details\n */\nexport class ConcurrencyError extends Error {\n constructor(\n /** The stream that had the concurrent modification */\n public readonly stream: string,\n /** The actual current version in the store */\n public readonly lastVersion: number,\n /** The events that were being committed */\n public readonly events: Message<Schemas, keyof Schemas>[],\n /** The version number that was expected */\n public readonly expectedVersion: number\n ) {\n // Message lists stream + event names only. Payloads remain accessible\n // via `error.events` for callers who need them — keeping them out of\n // the message avoids MB-scale strings on contended writes and keeps\n // potentially-sensitive data out of log streams.\n super(\n `Concurrency error committing \"${events\n .map((e) => `${stream}.${e.name}`)\n .join(\n \", \"\n )}\". Expected version ${expectedVersion} but found version ${lastVersion}.`\n );\n this.name = Errors.ConcurrencyError;\n }\n}\n\n/**\n * Thrown when attempting to write to a stream that has been closed\n * with a tombstone event.\n *\n * A tombstoned stream is permanently closed — no further actions can\n * be executed against it. The only way to reopen a tombstoned stream\n * is through `Act.close()` with a `restart` callback.\n *\n * @example\n * ```typescript\n * import { StreamClosedError } from \"@rotorsoft/act\";\n *\n * try {\n * await app.do(\"updateTicket\", target, payload);\n * } catch (error) {\n * if (error instanceof StreamClosedError) {\n * console.error(`Stream ${error.stream} is closed`);\n * }\n * }\n * ```\n *\n * @see {@link Act.close} for closing streams\n */\nexport class StreamClosedError extends Error {\n constructor(\n /** The stream that is closed */\n public readonly stream: string\n ) {\n super(`Stream \"${stream}\" is closed (tombstoned)`);\n this.name = Errors.StreamClosedError;\n }\n}\n","import { prettifyError, ZodError, type ZodType } from \"zod\";\nimport { config } from \"./config.js\";\nimport { ValidationError } from \"./types/index.js\";\n\n/**\n * @module utils\n * @category Utilities\n *\n * Small utilities used across the framework:\n * - {@link validate} — parse a payload against a Zod schema, throwing\n * {@link ValidationError} on failure.\n * - {@link extend} — validate a source object and merge into defaults.\n * - {@link sleep} — async delay (default duration from `config().sleepMs`).\n */\n\n/**\n * Parse `payload` against `schema`, returning the validated value or throwing\n * a {@link ValidationError} with prettified Zod details. When `schema` is\n * omitted, returns `payload` unchanged. The framework calls this for every\n * `app.do()` action, every emitted event, and every state init.\n *\n * @example\n * ```typescript\n * const UserSchema = z.object({ email: z.string().email() });\n * const user = validate(\"User\", { email: \"alice@example.com\" }, UserSchema);\n * ```\n *\n * @see {@link ValidationError}\n */\nexport const validate = <S>(\n target: string,\n payload: Readonly<S>,\n schema?: ZodType<S>\n): Readonly<S> => {\n try {\n return schema ? schema.parse(payload) : payload;\n } catch (error) {\n if (error instanceof ZodError) {\n throw new ValidationError(target, payload, prettifyError(error));\n }\n throw new ValidationError(target, payload, error);\n }\n};\n\n/**\n * Validate `source` against `schema` and return a new object that merges\n * `source` over the optional `target` defaults. Used by {@link config} for\n * env-var-overrides-defaults patterns; safe to call elsewhere — it never\n * mutates `target`.\n *\n * @example\n * ```typescript\n * const schema = z.object({ host: z.string(), port: z.number() });\n * const cfg = extend({ port: 8080 }, schema, { host: \"localhost\", port: 80 });\n * // → { host: \"localhost\", port: 8080 }\n * ```\n *\n * @throws {@link ValidationError} if `source` fails the schema.\n */\nexport const extend = <\n S extends Record<string, unknown>,\n T extends Record<string, unknown>,\n>(\n source: Readonly<S>,\n schema: ZodType<S>,\n target?: Readonly<T>\n): Readonly<S & T> => {\n const value = validate(\"config\", source, schema);\n return { ...target, ...value } as Readonly<S & T>;\n};\n\n/**\n * Pause for `ms` milliseconds (or `config().sleepMs` when omitted — `100ms`\n * in dev, `0ms` in tests). Used by adapters to simulate async I/O.\n *\n * @example\n * ```typescript\n * await sleep(); // default delay from config\n * await sleep(500); // explicit 500ms\n * ```\n */\nexport async function sleep(ms?: number) {\n return new Promise((resolve) => setTimeout(resolve, ms ?? config().sleepMs));\n}\n","/**\n * @packageDocumentation\n * Configuration utilities for Act Framework environment, logging, and package metadata.\n *\n * Provides type-safe configuration loading and validation using Zod schemas.\n *\n * @module config\n */\nimport * as fs from \"node:fs\";\nimport { z } from \"zod\";\nimport { log } from \"./ports.js\";\nimport {\n type Environment,\n Environments,\n type LogLevel,\n LogLevels,\n} from \"./types/index.js\";\nimport { extend } from \"./utils.js\";\n\n/**\n * Zod schema for validating package.json metadata.\n * @internal\n */\nexport const PackageSchema = z.object({\n name: z.string().min(1),\n version: z.string().min(1),\n description: z.string().min(1).optional(),\n author: z\n .object({ name: z.string().min(1), email: z.string().optional() })\n .optional()\n .or(z.string().min(1))\n .optional(),\n license: z.string().min(1).optional(),\n dependencies: z.record(z.string(), z.string()).optional(),\n});\n\n/**\n * Type representing the validated package.json metadata.\n *\n * @internal\n */\nexport type Package = z.infer<typeof PackageSchema>;\n\n/**\n * Fallback package metadata when `package.json` can't be read at module\n * load — happens when the framework is consumed from a CWD that doesn't\n * have one (bundled CLIs, Lambda layers, embedded scripts) or when the\n * file exists but is malformed.\n *\n * The values are deliberately synthetic so callers spot them immediately:\n * `config().name === \"act-fallback\"` is a runtime signal that the framework\n * couldn't load the host project's package.json.\n *\n * @internal\n */\nconst FALLBACK_PACKAGE: Package = {\n name: \"act-fallback\",\n version: \"0.0.0-fallback\",\n description: \"Synthetic fallback — package.json could not be loaded\",\n};\n\n/**\n * Loads and parses the local package.json file as a Package object. On\n * any read or parse failure, falls back to {@link FALLBACK_PACKAGE} and\n * stashes the error so {@link config} can surface it on first access —\n * we can't call `log()` here because the logger port memoizes on first\n * call and locking it at module load defeats user injection.\n *\n * @internal\n */\nconst getPackage = (): Package => {\n try {\n const raw = fs.readFileSync(\"package.json\");\n return JSON.parse(raw.toString()) as Package;\n } catch (err) {\n pkgLoadError = err;\n return FALLBACK_PACKAGE;\n }\n};\n\n/** Stashed read/parse error from {@link getPackage}, surfaced by config(). */\nlet pkgLoadError: unknown;\n\n/**\n * Zod schema for the full Act Framework configuration object.\n * Includes package metadata, environment, logging, and timing options.\n * @internal\n */\nconst BaseSchema = PackageSchema.extend({\n env: z.enum(Environments),\n logLevel: z.enum(LogLevels),\n logSingleLine: z.boolean(),\n sleepMs: z.number().int().min(0).max(5000),\n});\n\n/**\n * Type representing the validated Act Framework configuration object.\n */\nexport type Config = z.infer<typeof BaseSchema>;\n\nconst { NODE_ENV, LOG_LEVEL, LOG_SINGLE_LINE, SLEEP_MS } = process.env;\n\nconst env = (NODE_ENV || \"development\") as Environment;\nconst logLevel = (LOG_LEVEL ||\n (NODE_ENV === \"test\"\n ? \"fatal\"\n : NODE_ENV === \"production\"\n ? \"info\"\n : \"trace\")) as LogLevel;\nconst logSingleLine = (LOG_SINGLE_LINE || \"true\") === \"true\";\nconst sleepMs = parseInt(NODE_ENV === \"test\" ? \"0\" : (SLEEP_MS ?? \"100\"), 10);\n\nconst pkg = getPackage();\n\n// Lazily validated on first call. Cannot run extend() at module load\n// because of a utils.ts <-> config.ts cycle (utils imports config for\n// sleep()'s default). Inputs are frozen after import, so the cached\n// result is stable for the life of the process.\nlet _validated: Config | undefined;\n\n/**\n * Gets the current Act Framework configuration.\n *\n * Configuration is loaded from package.json and environment variables, providing\n * type-safe access to application metadata and runtime settings.\n *\n * **Environment Variables:**\n * - `NODE_ENV`: \"development\" | \"test\" | \"staging\" | \"production\" (default: \"development\")\n * - `LOG_LEVEL`: \"fatal\" | \"error\" | \"warn\" | \"info\" | \"debug\" | \"trace\"\n * - `LOG_SINGLE_LINE`: \"true\" | \"false\" (default: \"true\")\n * - `SLEEP_MS`: Milliseconds for sleep utility (default: 100, 0 for tests)\n *\n * **Defaults by environment:**\n * - test: logLevel=\"error\", sleepMs=0\n * - production: logLevel=\"info\"\n * - development: logLevel=\"trace\"\n *\n * @returns The validated configuration object\n *\n * @example Basic usage\n * ```typescript\n * import { config } from \"@rotorsoft/act\";\n *\n * const cfg = config();\n * console.log(`App: ${cfg.name} v${cfg.version}`);\n * console.log(`Environment: ${cfg.env}`);\n * console.log(`Log level: ${cfg.logLevel}`);\n * ```\n *\n * @example Environment-specific behavior\n * ```typescript\n * import { config } from \"@rotorsoft/act\";\n *\n * const cfg = config();\n *\n * if (cfg.env === \"production\") {\n * // Use PostgreSQL in production\n * store(new PostgresStore(prodConfig));\n * } else {\n * // Use in-memory store for dev/test\n * store(new InMemoryStore());\n * }\n * ```\n *\n * @example Adjusting log levels\n * ```typescript\n * // Set via environment variable:\n * // LOG_LEVEL=debug npm start\n *\n * // Or check in code:\n * const cfg = config();\n * if (cfg.logLevel === \"trace\") {\n * logger.trace(\"Detailed debugging enabled\");\n * }\n * ```\n *\n * @see {@link Config} for configuration type\n */\nexport const config = (): Config => {\n if (!_validated) {\n _validated = extend(\n { ...pkg, env, logLevel, logSingleLine, sleepMs },\n BaseSchema\n );\n if (pkgLoadError) {\n // Surface the fallback once, after _validated is set so the\n // recursive log() → config() call short-circuits. log() resolves\n // through the port singleton — respects user injection and level.\n const msg =\n pkgLoadError instanceof Error\n ? pkgLoadError.message\n : typeof pkgLoadError === \"string\"\n ? pkgLoadError\n : \"unknown error\";\n log().warn(\n `[act] Could not read package.json (${msg}); using synthetic ` +\n `name=\"${FALLBACK_PACKAGE.name}\" version=\"${FALLBACK_PACKAGE.version}\".`\n );\n pkgLoadError = undefined;\n }\n }\n return _validated;\n};\n","import { type ZodObject, type ZodRawShape, z } from \"zod\";\n\n/**\n * @packageDocumentation\n * @module act/types\n * @category Types\n * Zod schemas and helpers for the Act Framework.\n */\n\n/**\n * An empty Zod schema (no properties).\n */\nexport const ZodEmpty = z.record(z.string(), z.never());\n\n/**\n * Zod schema for an actor (user, system, etc.).\n */\nexport const ActorSchema = z\n .object({\n id: z.string(),\n name: z.string(),\n })\n .loose()\n .readonly();\n\n/**\n * Zod schema for a target (stream and actor info).\n */\nexport const TargetSchema = z\n .object({\n stream: z.string(),\n actor: ActorSchema,\n expectedVersion: z.number().optional(),\n })\n .loose()\n .readonly();\n\n/**\n * Zod schema for causation event metadata.\n */\nexport const CausationEventSchema = z.object({\n id: z.number(),\n name: z.string(),\n stream: z.string(),\n});\n\n/**\n * Zod schema for event metadata (correlation and causation).\n */\nexport const EventMetaSchema = z\n .object({\n correlation: z.string(),\n causation: z.object({\n action: TargetSchema.and(z.object({ name: z.string() })).optional(),\n event: CausationEventSchema.optional(),\n }),\n })\n .readonly();\n\n/**\n * Zod schema for committed event metadata (id, stream, version, created, meta).\n */\nexport const CommittedMetaSchema = z\n .object({\n id: z.number(),\n stream: z.string(),\n version: z.number(),\n created: z.date(),\n meta: EventMetaSchema,\n })\n .readonly();\n\n/**\n * Type representing the full state schema for a domain.\n * @property events - Map of event names to Zod schemas.\n * @property actions - Map of action names to Zod schemas.\n * @property state - Zod schema for the state object.\n */\nexport type StateSchema = Readonly<{\n events: Record<string, ZodObject<ZodRawShape> | typeof ZodEmpty>;\n actions: Record<string, ZodObject<ZodRawShape> | typeof ZodEmpty>;\n state: ZodObject<ZodRawShape>;\n}>;\n\n/**\n * Query options for event store queries.\n */\nexport const QuerySchema = z\n .object({\n stream: z.string().optional(),\n names: z.string().array().optional(),\n before: z.number().optional(),\n after: z.number().optional(),\n limit: z.number().optional(),\n created_before: z.date().optional(),\n created_after: z.date().optional(),\n backward: z.boolean().optional(),\n correlation: z.string().optional(),\n with_snaps: z.boolean().optional(),\n stream_exact: z.boolean().optional(),\n })\n .readonly();\n","/**\n * @packageDocumentation\n * @module act/types\n * Barrel file for Act Framework core types.\n *\n * Re-exports all major type definitions for actions, errors, ports, reactions, registries, and schemas.\n * Also defines common environment and log level types/constants for configuration and logging.\n *\n * @remarks\n * Import from this module to access all core framework types in one place.\n */\nexport type * from \"./action.js\";\nexport * from \"./errors.js\";\nexport type * from \"./ports.js\";\nexport type * from \"./reaction.js\";\nexport type * from \"./registry.js\";\nexport * from \"./schemas.js\";\n\n/**\n * Supported runtime environments for the framework.\n * - `development`: Local development\n * - `test`: Automated testing\n * - `staging`: Pre-production\n * - `production`: Live/production\n */\nexport const Environments = [\n \"development\",\n \"test\",\n \"staging\",\n \"production\",\n] as const;\n\n/**\n * Type representing a valid environment string.\n */\nexport type Environment = (typeof Environments)[number];\n\n/**\n * Supported log levels for framework logging.\n * - `fatal`, `error`, `warn`, `info`, `debug`, `trace`\n */\nexport const LogLevels = [\n \"fatal\",\n \"error\",\n \"warn\",\n \"info\",\n \"debug\",\n \"trace\",\n] as const;\n\n/**\n * Type representing a valid log level string.\n */\nexport type LogLevel = (typeof LogLevels)[number];\n","/**\n * @packageDocumentation\n * @module act/adapters\n * In-memory event store adapter for the Act Framework.\n *\n * This adapter implements the Store interface and is suitable for development, testing, and demonstration purposes.\n * All data is stored in memory and lost on process exit.\n *\n * @category Adapters\n */\nimport { SNAP_EVENT, TOMBSTONE_EVENT } from \"../ports.js\";\nimport { ConcurrencyError } from \"../types/errors.js\";\nimport type {\n BlockedLease,\n Committed,\n EventMeta,\n Lease,\n Message,\n Query,\n QueryStreams,\n QueryStreamsResult,\n Schema,\n Schemas,\n Store,\n StreamPosition,\n} from \"../types/index.js\";\nimport { sleep } from \"../utils.js\";\n\n/**\n * @internal\n * Represents an in-memory stream for event processing and leasing.\n */\nclass InMemoryStream {\n private _at = -1;\n private _retry = -1;\n private _blocked = false;\n private _error = \"\";\n private _leased_by: string | undefined = undefined;\n private _leased_until: Date | undefined = undefined;\n\n constructor(\n readonly stream: string,\n readonly source: string | undefined\n ) {}\n\n get is_available() {\n return (\n !this._blocked &&\n (!this._leased_until || this._leased_until <= new Date())\n );\n }\n\n get at() {\n return this._at;\n }\n\n get retry() {\n return this._retry;\n }\n\n get blocked() {\n return this._blocked;\n }\n\n get error() {\n return this._error;\n }\n\n get leased_by() {\n return this._leased_by;\n }\n\n get leased_until() {\n return this._leased_until;\n }\n\n /**\n * Attempt to lease this stream for processing.\n * @param lease - The lease request.\n * @param millis - Lease duration in milliseconds.\n * @returns The granted lease or undefined if blocked.\n */\n lease(lease: Lease, millis: number): Lease {\n if (millis > 0) {\n this._leased_by = lease.by;\n this._leased_until = new Date(Date.now() + millis);\n }\n this._retry = this._retry + 1;\n return {\n stream: this.stream,\n source: this.source,\n at: lease.at,\n by: lease.by,\n retry: this._retry,\n lagging: lease.lagging,\n };\n }\n\n /**\n * Acknowledge completion of processing for this stream.\n * @param lease - The lease request.\n */\n ack(lease: Lease) {\n if (this._leased_by === lease.by) {\n this._leased_by = undefined;\n this._leased_until = undefined;\n this._at = lease.at;\n this._retry = -1;\n return {\n stream: this.stream,\n source: this.source,\n at: this._at,\n by: lease.by,\n retry: this._retry,\n lagging: lease.lagging,\n };\n }\n }\n\n /**\n * Block a stream for processing after failing to process and reaching max retries with blocking enabled.\n * @param lease - The lease request.\n * @param error Blocked error message.\n */\n block(lease: Lease, error: string) {\n if (this._leased_by === lease.by) {\n this._blocked = true;\n this._error = error;\n return {\n stream: this.stream,\n source: this.source,\n at: this._at,\n by: this._leased_by,\n retry: this._retry,\n error: this._error,\n lagging: lease.lagging,\n };\n }\n }\n\n /**\n * Reset this stream's watermark and state for replay. The retry counter\n * resets to -1 to match the constructor + ack() invariant (\"released\n * stream\"); the next claim() bumps it to 0 (first attempt).\n */\n reset() {\n this._at = -1;\n this._retry = -1;\n this._blocked = false;\n this._error = \"\";\n this._leased_by = undefined;\n this._leased_until = undefined;\n }\n}\n\n/**\n * In-memory event store implementation.\n *\n * This is the default store used by Act when no other store is injected.\n * It stores all events in memory and is suitable for:\n * - Development and prototyping\n * - Unit and integration testing\n * - Demonstrations and examples\n *\n * **Not suitable for production** - all data is lost when the process exits.\n * Use {@link PostgresStore} for production deployments.\n *\n * The in-memory store provides:\n * - Full {@link Store} interface implementation\n * - Optimistic concurrency control\n * - Stream leasing for distributed processing simulation\n * - Snapshot support\n * - Fast performance (no I/O overhead)\n *\n * @example Using in tests\n * ```typescript\n * import { store } from \"@rotorsoft/act\";\n *\n * describe(\"Counter\", () => {\n * beforeEach(async () => {\n * // Reset store between tests\n * await store().seed();\n * });\n *\n * it(\"increments\", async () => {\n * await app.do(\"increment\", target, { by: 5 });\n * const snapshot = await app.load(Counter, \"counter-1\");\n * expect(snapshot.state.count).toBe(5);\n * });\n * });\n * ```\n *\n * @example Explicit instantiation\n * ```typescript\n * import { InMemoryStore } from \"@rotorsoft/act\";\n *\n * const testStore = new InMemoryStore();\n * await testStore.seed();\n *\n * // Use for specific test scenarios\n * await testStore.commit(\"test-stream\", events, meta);\n * ```\n *\n * @example Querying events\n * ```typescript\n * const events: any[] = [];\n * await store().query(\n * (event) => events.push(event),\n * { stream: \"test-stream\" }\n * );\n * console.log(`Found ${events.length} events`);\n * ```\n *\n * @see {@link Store} for the interface definition\n * @see {@link PostgresStore} for production use\n * @see {@link store} for injecting stores\n *\n * @category Adapters\n */\nexport class InMemoryStore implements Store {\n // stored events\n private _events: Committed<Schemas, keyof Schemas>[] = [];\n // stored stream positions and other metadata\n private _streams: Map<string, InMemoryStream> = new Map();\n // last committed version per stream — O(1) replacement for filter-on-commit\n private _streamVersions: Map<string, number> = new Map();\n // max non-snapshot event id per stream — drives the source-pattern probe in claim()\n // without scanning the full event log.\n private _maxEventIdByStream: Map<string, number> = new Map();\n // global max non-snapshot event id — fast pre-check for source-less streams in claim()\n private _maxNonSnapEventId = -1;\n\n private _resetIndexes() {\n this._events.length = 0;\n this._streamVersions.clear();\n this._maxEventIdByStream.clear();\n this._maxNonSnapEventId = -1;\n }\n\n /**\n * Dispose of the store and clear all events.\n * @returns Promise that resolves when disposal is complete.\n */\n async dispose() {\n await sleep();\n this._resetIndexes();\n }\n\n /**\n * Seed the store with initial data (no-op for in-memory).\n * @returns Promise that resolves when seeding is complete.\n */\n async seed() {\n await sleep();\n }\n\n /**\n * Drop all data from the store.\n * @returns Promise that resolves when the store is cleared.\n */\n async drop() {\n await sleep();\n this._resetIndexes();\n this._streams = new Map();\n }\n\n private in_query<E extends Schemas>(query: Query, e: Committed<E, keyof E>) {\n if (query.stream) {\n if (query.stream_exact) {\n if (e.stream !== query.stream) return false;\n } else if (!RegExp(`^${query.stream}$`).test(e.stream)) return false;\n }\n if (query.names && !query.names.includes(e.name as string)) return false;\n if (query.correlation && e.meta?.correlation !== query.correlation)\n return false;\n if (e.name === SNAP_EVENT && !query.with_snaps) return false;\n return true;\n }\n\n /**\n * Query events in the store, optionally filtered by query options.\n * @param callback - Function to call for each event.\n * @param query - Optional query options.\n * @returns The number of events processed.\n */\n async query<E extends Schemas>(\n callback: (event: Committed<E, keyof E>) => void,\n query?: Query\n ) {\n await sleep();\n let count = 0;\n if (query?.backward) {\n let i = (query?.before || this._events.length) - 1;\n while (i >= 0) {\n const e = this._events[i--];\n if (query && !this.in_query(query, e)) continue;\n if (query?.created_before && e.created >= query.created_before)\n continue;\n if (query.after && e.id <= query.after) break;\n if (query.created_after && e.created <= query.created_after) break;\n callback(e as Committed<E, keyof E>);\n count++;\n if (query?.limit && count >= query.limit) break;\n }\n } else {\n let i = (query?.after ?? -1) + 1;\n while (i < this._events.length) {\n const e = this._events[i++];\n if (query && !this.in_query(query, e)) continue;\n if (query?.created_after && e.created <= query.created_after) continue;\n if (query?.before && e.id >= query.before) break;\n if (query?.created_before && e.created >= query.created_before) break;\n callback(e as Committed<E, keyof E>);\n count++;\n if (query?.limit && count >= query.limit) break;\n }\n }\n return count;\n }\n\n /**\n * Commit one or more events to a stream.\n * @param stream - The stream name.\n * @param msgs - The events/messages to commit.\n * @param meta - Event metadata.\n * @param expectedVersion - Optional optimistic concurrency check.\n * @returns The committed events with metadata.\n * @throws ConcurrencyError if expectedVersion does not match.\n */\n async commit<E extends Schemas>(\n stream: string,\n msgs: Message<E, keyof E>[],\n meta: EventMeta,\n expectedVersion?: number\n ) {\n await sleep();\n const currentVersion = this._streamVersions.get(stream) ?? -1;\n if (\n typeof expectedVersion === \"number\" &&\n currentVersion !== expectedVersion\n ) {\n throw new ConcurrencyError(\n stream,\n currentVersion,\n msgs as Message<Schemas, keyof Schemas>[],\n expectedVersion\n );\n }\n\n let version = currentVersion + 1;\n let lastNonSnapId = -1;\n const committed = msgs.map(({ name, data }) => {\n const c: Committed<E, keyof E> = {\n id: this._events.length,\n stream,\n version,\n created: new Date(),\n name,\n data,\n meta,\n };\n this._events.push(c as Committed<Schemas, keyof Schemas>);\n if (name !== SNAP_EVENT) lastNonSnapId = c.id;\n version++;\n return c;\n });\n this._streamVersions.set(stream, version - 1);\n if (lastNonSnapId >= 0) {\n this._maxEventIdByStream.set(stream, lastNonSnapId);\n // commit always assigns a fresh id from this._events.length, so any\n // non-snap commit strictly raises the global max.\n this._maxNonSnapEventId = lastNonSnapId;\n }\n return committed;\n }\n\n /**\n * Atomically discovers and leases streams for processing.\n * Fuses poll + lease into a single operation.\n * @param lagging - Max streams from lagging frontier.\n * @param leading - Max streams from leading frontier.\n * @param by - Lease holder identifier.\n * @param millis - Lease duration in milliseconds.\n * @returns Granted leases.\n */\n async claim(lagging: number, leading: number, by: string, millis: number) {\n await sleep();\n // Cache compiled regexes — multiple subscribed streams typically share the\n // same source pattern, and the inner loop can run thousands of times per claim.\n const sourceRegex = new Map<string, RegExp>();\n const getRegex = (source: string) => {\n let re = sourceRegex.get(source);\n if (!re) {\n re = new RegExp(source);\n sourceRegex.set(source, re);\n }\n return re;\n };\n const hasWork = (s: InMemoryStream): boolean => {\n if (s.at < 0) return true;\n if (!s.source) return s.at < this._maxNonSnapEventId;\n const re = getRegex(s.source);\n for (const [streamName, maxId] of this._maxEventIdByStream) {\n if (maxId > s.at && re.test(streamName)) return true;\n }\n return false;\n };\n const available = [...this._streams.values()].filter(\n (s) => s.is_available && hasWork(s)\n );\n const lag = available\n .sort((a, b) => a.at - b.at)\n .slice(0, lagging)\n .map((s) => ({\n stream: s.stream,\n source: s.source,\n at: s.at,\n lagging: true,\n }));\n const lead = available\n .sort((a, b) => b.at - a.at)\n .slice(0, leading)\n .map((s) => ({\n stream: s.stream,\n source: s.source,\n at: s.at,\n lagging: false,\n }));\n // deduplicate (a stream can appear in both frontiers)\n const seen = new Set<string>();\n const combined = [...lag, ...lead].filter((p) => {\n if (seen.has(p.stream)) return false;\n seen.add(p.stream);\n return true;\n });\n // lease each atomically\n return combined\n .map((p) =>\n this._streams.get(p.stream)?.lease({ ...p, by, retry: 0 }, millis)\n )\n .filter((l) => !!l);\n }\n\n /**\n * Registers streams for event processing.\n * @param streams - Streams to register with optional source.\n * @returns subscribed count and current max watermark.\n */\n async subscribe(streams: Array<{ stream: string; source?: string }>) {\n await sleep();\n let subscribed = 0;\n for (const { stream, source } of streams) {\n if (!this._streams.has(stream)) {\n this._streams.set(stream, new InMemoryStream(stream, source));\n subscribed++;\n }\n }\n let watermark = -1;\n for (const s of this._streams.values()) {\n if (s.at > watermark) watermark = s.at;\n }\n return { subscribed, watermark };\n }\n\n /**\n * Acknowledge completion of processing for leased streams.\n * @param leases - Leases to acknowledge, including last processed watermark and lease holder.\n */\n async ack(leases: Lease[]) {\n await sleep();\n return leases\n .map((l) => this._streams.get(l.stream)?.ack(l))\n .filter((l) => !!l);\n }\n\n /**\n * Block a stream for processing after failing to process and reaching max retries with blocking enabled.\n * @param leases - Leases to block, including lease holder and last error message.\n * @returns Blocked leases.\n */\n async block(leases: BlockedLease[]) {\n await sleep();\n return leases\n .map((l) => this._streams.get(l.stream)?.block(l, l.error))\n .filter((l) => !!l);\n }\n\n /**\n * Reset watermarks for the given streams to -1, clearing retry, blocked,\n * error, and lease state so they can be replayed from the beginning.\n * @param streams - Stream names to reset.\n * @returns Count of streams that were actually reset.\n */\n async reset(streams: string[]) {\n await sleep();\n let count = 0;\n for (const name of streams) {\n const s = this._streams.get(name);\n if (s) {\n s.reset();\n count++;\n }\n }\n return count;\n }\n\n /**\n * Streams registered subscription positions to the callback, ordered by\n * stream name. Returns the highest event id in the store and the count\n * of positions emitted.\n */\n async query_streams(\n callback: (position: StreamPosition) => void,\n query?: QueryStreams\n ): Promise<QueryStreamsResult> {\n await sleep();\n const limit = query?.limit ?? 100;\n const after = query?.after;\n const blocked = query?.blocked;\n const streamRe =\n query?.stream && !query.stream_exact\n ? new RegExp(`^${query.stream}$`)\n : undefined;\n const sourceRe =\n query?.source && !query.source_exact\n ? new RegExp(`^${query.source}$`)\n : undefined;\n\n const sorted = [...this._streams.values()].sort((a, b) =>\n a.stream.localeCompare(b.stream)\n );\n\n let count = 0;\n for (const s of sorted) {\n if (after !== undefined && s.stream <= after) continue;\n if (query?.stream !== undefined) {\n if (\n query.stream_exact\n ? s.stream !== query.stream\n : !streamRe!.test(s.stream)\n )\n continue;\n }\n if (query?.source !== undefined) {\n if (s.source === undefined) continue;\n if (\n query.source_exact\n ? s.source !== query.source\n : !sourceRe!.test(s.source)\n )\n continue;\n }\n if (blocked !== undefined && s.blocked !== blocked) continue;\n callback({\n stream: s.stream,\n source: s.source,\n at: s.at,\n retry: s.retry,\n blocked: s.blocked,\n error: s.error,\n leased_by: s.leased_by,\n leased_until: s.leased_until,\n });\n count++;\n if (count >= limit) break;\n }\n return { maxEventId: this._events.length - 1, count };\n }\n\n /**\n * Atomically truncates streams and seeds each with a snapshot or tombstone.\n * @param targets - Streams to truncate with optional snapshot state and meta.\n * @returns Map keyed by stream name, each entry with `deleted` count and `committed` event.\n */\n async truncate(\n targets: Array<{\n stream: string;\n snapshot?: Schema;\n meta?: EventMeta;\n }>\n ) {\n await sleep();\n // Count per-stream deletions\n const deletedCounts = new Map<string, number>();\n const streamSet = new Set(targets.map((t) => t.stream));\n for (const e of this._events) {\n if (streamSet.has(e.stream)) {\n deletedCounts.set(e.stream, (deletedCounts.get(e.stream) ?? 0) + 1);\n }\n }\n this._events = this._events.filter((e) => !streamSet.has(e.stream));\n for (const stream of streamSet) {\n this._streams.delete(stream);\n this._streamVersions.delete(stream);\n this._maxEventIdByStream.delete(stream);\n }\n const result = new Map<\n string,\n { deleted: number; committed: Committed<Schemas, keyof Schemas> }\n >();\n for (const { stream, snapshot, meta } of targets) {\n const event: Committed<Schemas, keyof Schemas> = {\n id: this._events.length,\n stream,\n version: 0,\n created: new Date(),\n name: snapshot !== undefined ? SNAP_EVENT : TOMBSTONE_EVENT,\n data: snapshot ?? {},\n meta: meta ?? { correlation: \"\", causation: {} },\n };\n this._events.push(event);\n this._streamVersions.set(stream, 0);\n if (event.name !== SNAP_EVENT) {\n this._maxEventIdByStream.set(stream, event.id);\n }\n result.set(stream, {\n deleted: deletedCounts.get(stream) ?? 0,\n committed: event,\n });\n }\n // Recompute global max from the per-stream index — deletions may have\n // dropped the previous max, while new tombstones may have raised it.\n let max = -1;\n for (const id of this._maxEventIdByStream.values()) if (id > max) max = id;\n this._maxNonSnapEventId = max;\n return result;\n }\n}\n","import { ConsoleLogger } from \"./adapters/console-logger.js\";\nimport { InMemoryCache } from \"./adapters/in-memory-cache.js\";\nimport { InMemoryStore } from \"./adapters/in-memory-store.js\";\nimport { config } from \"./config.js\";\nimport type {\n Cache,\n Disposable,\n Disposer,\n Logger,\n Store,\n} from \"./types/index.js\";\n\n/**\n * Port/adapter infrastructure for the Act framework.\n *\n * All infrastructure concerns (logging, storage, caching) are managed as\n * singleton adapters injected via port functions. Each port follows the same\n * pattern: first call wins with a sensible default, optional adapter injection.\n *\n * - `log()` — structured logging (default: ConsoleLogger)\n * - `store()` — event persistence (default: InMemoryStore)\n * - `cache()` — state checkpoints (default: InMemoryCache)\n * - `dispose()` — register cleanup functions for graceful shutdown\n *\n * @module ports\n */\n\n/**\n * List of exit codes for process termination. Consumed by signal handlers\n * and {@link disposeAndExit}; not part of the user-facing surface.\n *\n * @internal\n */\nexport const ExitCodes = [\"ERROR\", \"EXIT\"] as const;\n\n/**\n * Type for allowed exit codes.\n *\n * - `\"ERROR\"` — abnormal termination (uncaught exception, unhandled rejection)\n * - `\"EXIT\"` — clean shutdown (SIGINT, SIGTERM, or manual trigger)\n *\n * @internal\n */\nexport type ExitCode = (typeof ExitCodes)[number];\n\n// ---------------------------------------------------------------------------\n// Port factory\n// ---------------------------------------------------------------------------\n\n/**\n * Factory function that creates or returns the injected adapter.\n * @internal\n */\ntype Injector<Port extends Disposable> = (adapter?: Port) => Port;\n\n/** Singleton adapter registry, keyed by injector function name. */\nconst adapters = new Map<string, Disposable>();\n\n/**\n * Creates a singleton port with optional adapter injection.\n *\n * The first call initializes the adapter (using the provided adapter or the\n * injector's default). Subsequent calls return the cached singleton. Adapters\n * are disposed in reverse registration order during {@link disposeAndExit}.\n *\n * @param injector - Named function that creates the default adapter\n * @returns Port function: call with no args to get the singleton, or pass an\n * adapter on the first call to override the default\n *\n * @example\n * ```typescript\n * const store = port(function store(adapter?: Store) {\n * return adapter || new InMemoryStore();\n * });\n * const s = store(); // InMemoryStore\n * ```\n */\nexport function port<Port extends Disposable>(injector: Injector<Port>) {\n return (adapter?: Port): Port => {\n if (!adapters.has(injector.name)) {\n const injected = injector(adapter);\n adapters.set(injector.name, injected);\n // log() is now in adapters (or this IS the log port we just registered),\n // so the recursive call resolves immediately. Routing through the logger\n // means level gating (e.g. silenced in tests at fatal) takes effect.\n log().info(`[act] + ${injector.name}:${injected.constructor.name}`);\n }\n return adapters.get(injector.name) as Port;\n };\n}\n\n// ---------------------------------------------------------------------------\n// Ports: log, store, cache\n// ---------------------------------------------------------------------------\n\n/**\n * Gets or injects the singleton logger.\n *\n * By default, Act uses a built-in {@link ConsoleLogger} that emits JSON lines\n * in production (compatible with GCP, AWS CloudWatch, Datadog) and colorized\n * output in development — zero external dependencies.\n *\n * For pino, inject a `PinoLogger` from `@rotorsoft/act-pino` before building\n * your application.\n *\n * @param adapter - Optional logger implementation to inject\n * @returns The singleton logger instance\n *\n * @example Default console logger\n * ```typescript\n * import { log } from \"@rotorsoft/act\";\n * const logger = log();\n * logger.info(\"Application started\");\n * ```\n *\n * @example Injecting pino\n * ```typescript\n * import { log } from \"@rotorsoft/act\";\n * import { PinoLogger } from \"@rotorsoft/act-pino\";\n * log(new PinoLogger({ level: \"debug\", pretty: true }));\n * ```\n *\n * @see {@link Logger} for the interface contract\n * @see {@link ConsoleLogger} for the default implementation\n */\nexport const log = port(function log(adapter?: Logger) {\n const cfg = config();\n return (\n adapter ||\n new ConsoleLogger({\n level: cfg.logLevel,\n pretty: cfg.env !== \"production\",\n })\n );\n});\n\n/**\n * Gets or injects the singleton event store.\n *\n * By default, Act uses an {@link InMemoryStore} suitable for development and\n * testing. For production, inject a persistent store like `PostgresStore` from\n * `@rotorsoft/act-pg` before building your application.\n *\n * **Important:** Store injection must happen before creating any Act instances.\n * Once set, the store cannot be changed without restarting the application.\n *\n * @param adapter - Optional store implementation to inject\n * @returns The singleton store instance\n *\n * @example Default in-memory store\n * ```typescript\n * import { store } from \"@rotorsoft/act\";\n * const s = store();\n * ```\n *\n * @example Injecting PostgreSQL\n * ```typescript\n * import { store } from \"@rotorsoft/act\";\n * import { PostgresStore } from \"@rotorsoft/act-pg\";\n *\n * store(new PostgresStore({\n * host: \"localhost\",\n * port: 5432,\n * database: \"myapp\",\n * user: \"postgres\",\n * password: \"secret\",\n * }));\n * ```\n *\n * @see {@link Store} for the interface contract\n * @see {@link InMemoryStore} for the default implementation\n */\nexport const store = port(function store(adapter?: Store) {\n return adapter || new InMemoryStore();\n});\n\n/**\n * Gets or injects the singleton cache.\n *\n * By default, Act uses an {@link InMemoryCache} (LRU, maxSize 1000). For\n * distributed deployments, inject a Redis-backed cache before building your\n * application.\n *\n * @param adapter - Optional cache implementation to inject\n * @returns The singleton cache instance\n *\n * @see {@link Cache} for the interface contract\n * @see {@link InMemoryCache} for the default implementation\n */\nexport const cache = port(function cache(adapter?: Cache) {\n return adapter || new InMemoryCache();\n});\n\n// ---------------------------------------------------------------------------\n// Disposal\n// ---------------------------------------------------------------------------\n\n/** Registered cleanup functions, executed in reverse order during shutdown. */\nconst disposers: Disposer[] = [];\n\n/**\n * Disposes all registered adapters and disposers, then exits the process.\n *\n * Execution order:\n * 1. Custom disposers (registered via {@link dispose}) — in reverse order\n * 2. Port adapters (log, store, cache) — in reverse registration order\n * 3. Adapter registry is cleared\n * 4. Process exits (skipped in test environment)\n *\n * In production, `\"ERROR\"` exits are silently ignored to avoid crashing on\n * transient failures (e.g. an uncaught promise in a non-critical path).\n *\n * @param code - Exit code: `\"EXIT\"` for clean shutdown (exit 0),\n * `\"ERROR\"` for abnormal termination (exit 1)\n */\nexport async function disposeAndExit(code: ExitCode = \"EXIT\"): Promise<void> {\n if (code === \"ERROR\" && config().env === \"production\") {\n // Surface the swallow so incident triage can see it. Without this\n // log the framework looks unresponsive after an uncaught exception\n // in prod — exactly when operators most need a breadcrumb.\n log().warn(\n \"disposeAndExit('ERROR') ignored in production — process kept alive\"\n );\n return;\n }\n\n // Run sequentially in reverse registration order so a disposer can rely on\n // later-registered disposers (and adapters on later-registered adapters)\n // having already finished — Promise.all would race them.\n for (const disposer of [...disposers].reverse()) {\n await disposer();\n }\n for (const adapter of [...adapters.values()].reverse()) {\n await adapter.dispose();\n log().info(`[act] - ${adapter.constructor.name}`);\n }\n adapters.clear();\n config().env !== \"test\" && process.exit(code === \"ERROR\" ? 1 : 0);\n}\n\n/**\n * Registers a cleanup function for graceful shutdown.\n *\n * Disposers are called automatically on SIGINT, SIGTERM, uncaught exceptions,\n * and unhandled rejections. They execute in reverse registration order before\n * port adapters are disposed.\n *\n * @param disposer - Async function to call during cleanup. Omit to get a\n * reference to {@link disposeAndExit} without registering.\n * @returns Function to manually trigger disposal and exit\n *\n * @example\n * ```typescript\n * import { dispose } from \"@rotorsoft/act\";\n *\n * const db = connectDatabase();\n * dispose(async () => await db.close());\n *\n * // In tests\n * afterAll(async () => await dispose()());\n * ```\n *\n * @see {@link disposeAndExit} for the full shutdown sequence\n */\nexport function dispose(\n disposer?: Disposer\n): (code?: ExitCode) => Promise<void> {\n disposer && disposers.push(disposer);\n return disposeAndExit;\n}\n\n// ---------------------------------------------------------------------------\n// Constants\n// ---------------------------------------------------------------------------\n\n/**\n * Event name used internally for snapshot events in the event store.\n * Snapshot events store a full state checkpoint, enabling efficient cold-start\n * recovery without replaying the entire event stream.\n */\nexport const SNAP_EVENT = \"__snapshot__\";\n\n/**\n * Event name used internally for tombstone events in the event store.\n * A tombstone marks a stream as permanently closed — no further writes\n * are permitted until the stream is explicitly restarted via `close()`.\n *\n * @see {@link Act.close} for the close-the-books API\n */\nexport const TOMBSTONE_EVENT = \"__tombstone__\";\n","import { disposeAndExit, log } from \"./ports.js\";\n\n// Resolve the logger lazily inside each handler — calling log() here at\n// module load would register the default ConsoleLogger before user code\n// can inject (port singletons are first-call-wins).\nprocess.once(\"SIGINT\", async (arg?: any) => {\n log().info(arg, \"SIGINT\");\n await disposeAndExit(\"EXIT\");\n});\nprocess.once(\"SIGTERM\", async (arg?: any) => {\n log().info(arg, \"SIGTERM\");\n await disposeAndExit(\"EXIT\");\n});\nprocess.once(\"uncaughtException\", async (arg?: any) => {\n log().error(arg, \"Uncaught Exception\");\n await disposeAndExit(\"ERROR\");\n});\nprocess.once(\"unhandledRejection\", async (arg?: any) => {\n log().error(arg, \"Unhandled Rejection\");\n await disposeAndExit(\"ERROR\");\n});\n","import EventEmitter from \"node:events\";\nimport {\n buildDrain,\n buildEs,\n buildHandle,\n buildHandleBatch,\n CorrelateCycle,\n classifyRegistry,\n DrainController,\n type DrainOps,\n type EsOps,\n type Handle,\n type HandleBatch,\n runCloseCycle,\n SettleLoop,\n} from \"./internal/index.js\";\nimport { dispose, log, store } from \"./ports.js\";\nimport type {\n Actor,\n AsOf,\n BatchHandler,\n BlockedLease,\n CloseResult,\n CloseTarget,\n Committed,\n Drain,\n DrainOptions,\n IAct,\n Lease,\n Logger,\n Query,\n Registry,\n Schema,\n SchemaRegister,\n Schemas,\n SettleOptions,\n Snapshot,\n State,\n Target,\n} from \"./types/index.js\";\n\n/**\n * @category Orchestrator\n * @see Store\n *\n * Main orchestrator for event-sourced state machines and workflows.\n *\n * It manages the lifecycle of actions, reactions, and event streams, providing APIs for loading state, executing actions, querying events, and draining reactions.\n *\n * ## Usage\n *\n * ```typescript\n * const app = new Act(registry, 100);\n * await app.do(\"increment\", { stream: \"counter1\", actor }, { by: 1 });\n * const snapshot = await app.load(Counter, \"counter1\");\n * await app.drain();\n * ```\n *\n * - Register event listeners with `.on(\"committed\", ...)` and `.on(\"acked\", ...)` to react to lifecycle events.\n * - Use `.query()` to analyze event streams for analytics or debugging.\n *\n * @template TSchemaReg SchemaRegister for state\n * @template TEvents Schemas for events\n * @template TActions Schemas for actions\n * @template TStateMap Map of state names to state schemas\n * @template TActor Actor type extending base Actor\n */\n/**\n * Default LRU cap for the subscribed-streams cache. Apps that mint many\n * dynamic targets (one per aggregate) should override via\n * {@link ActOptions.maxSubscribedStreams} based on expected concurrency.\n */\nexport const DEFAULT_MAX_SUBSCRIBED_STREAMS = 1000;\n\n/**\n * Default debounce window (ms) for `settle()` when neither the per-call\n * `SettleOptions.debounceMs` nor `ActOptions.settleDebounceMs` is set.\n * Coalesces commits in the same tick and small bursts; sub-perceptible\n * latency on the `\"settled\"` signal.\n */\nexport const DEFAULT_SETTLE_DEBOUNCE_MS = 10;\n\n/**\n * Lifecycle events emitted by {@link Act}, mapped to their payload type.\n * Drives the typing of `emit` / `on` / `off` — the event-name argument\n * narrows its payload at the call site.\n */\nexport type ActLifecycleEvents<\n TSchemaReg extends SchemaRegister<TActions>,\n TEvents extends Schemas,\n TActions extends Schemas,\n> = {\n committed: Snapshot<TSchemaReg, TEvents>[];\n acked: Lease[];\n blocked: BlockedLease[];\n settled: Drain<TEvents>;\n closed: CloseResult;\n};\n\n/**\n * Options for {@link Act} construction (passed via {@link ActBuilder.build}).\n *\n * @property maxSubscribedStreams - Cap for the LRU set tracking already-\n * subscribed reaction streams. Default: {@link DEFAULT_MAX_SUBSCRIBED_STREAMS}.\n * @property settleDebounceMs - Debounce window (ms) used by `settle()` when\n * the caller doesn't pass `SettleOptions.debounceMs`. Tune this once per\n * Act instance instead of threading the value through every call site.\n * Default: {@link DEFAULT_SETTLE_DEBOUNCE_MS}.\n */\nexport type ActOptions = {\n readonly maxSubscribedStreams?: number;\n readonly settleDebounceMs?: number;\n};\n\nexport class Act<\n TSchemaReg extends SchemaRegister<TActions>,\n TEvents extends Schemas,\n TActions extends Schemas,\n TStateMap extends Record<string, Schema> = Record<string, never>,\n TActor extends Actor = Actor,\n> implements IAct<TEvents, TActions, TActor>\n{\n private _emitter = new EventEmitter();\n /** Event names with at least one registered reaction (computed at build time) */\n private readonly _reactive_events: ReadonlySet<string>;\n /** Drain pipeline driver: armed flag, concurrency lock, adaptive ratio. */\n private readonly _drain: DrainController<TEvents, TActions, TSchemaReg>;\n /** Correlation state machine: lazy init, dynamic-resolver scan, periodic worker. */\n private readonly _correlate: CorrelateCycle<TSchemaReg, TEvents, TActions>;\n /** Debounced correlate→drain catch-up loop. */\n private readonly _settle: SettleLoop<TEvents>;\n\n /**\n * Emit a lifecycle event. The payload type is inferred from the event name\n * via {@link ActLifecycleEvents}.\n */\n emit<E extends keyof ActLifecycleEvents<TSchemaReg, TEvents, TActions>>(\n event: E,\n args: ActLifecycleEvents<TSchemaReg, TEvents, TActions>[E]\n ): boolean {\n return this._emitter.emit(event, args);\n }\n\n /**\n * Register a listener for a lifecycle event. The listener receives the\n * event-specific payload.\n */\n on<E extends keyof ActLifecycleEvents<TSchemaReg, TEvents, TActions>>(\n event: E,\n listener: (\n args: ActLifecycleEvents<TSchemaReg, TEvents, TActions>[E]\n ) => void\n ): this {\n this._emitter.on(event, listener);\n return this;\n }\n\n /**\n * Remove a previously registered lifecycle listener.\n */\n off<E extends keyof ActLifecycleEvents<TSchemaReg, TEvents, TActions>>(\n event: E,\n listener: (\n args: ActLifecycleEvents<TSchemaReg, TEvents, TActions>[E]\n ) => void\n ): this {\n this._emitter.off(event, listener);\n return this;\n }\n\n /** Batch handlers for static-target projections (target → handler) */\n private readonly _batch_handlers: Map<string, BatchHandler<TEvents>>;\n /** Event-sourcing handlers, optionally wrapped with trace decorators */\n private readonly _es: EsOps;\n /** Correlate/drain pipeline ops, optionally wrapped with trace decorators */\n private readonly _cd: DrainOps<TEvents>;\n /**\n * Event-name → owning state, computed at build time. The duplicate-event\n * guard in merge.ts ensures one event name maps to at most one state, so\n * this lookup is unambiguous. Used by `close()` to pick the right reducer\n * set when seeding a `restart` snapshot in multi-state apps.\n */\n private readonly _event_to_state: ReadonlyMap<string, State<any, any, any>>;\n /** Logger resolved at construction time (after user port configuration) */\n private readonly _logger: Logger = log();\n /** Pre-bound IAct methods reused across drain cycles. Only `do` varies per\n * payload (it captures the triggering event for reactingTo auto-inject). */\n private readonly _bound_do = this.do.bind(this);\n private readonly _bound_load = this.load.bind(this);\n private readonly _bound_query = this.query.bind(this);\n private readonly _bound_query_array = this.query_array.bind(this);\n /** Reaction dispatchers built once and handed to runDrainCycle each cycle. */\n private readonly _handle: Handle<TEvents>;\n private readonly _handle_batch: HandleBatch<TEvents>;\n\n /**\n * Create a new Act orchestrator. Prefer the {@link act} builder over\n * direct construction — `act()...build()` wires the registry, merges\n * partial states, and collects batch handlers from registered slices\n * and projections in one pass.\n *\n * @param registry Schemas for every event and action across registered states\n * @param _states Merged map of state name → state definition\n * @param batchHandlers Static-target projection batch handlers (target → handler)\n * @param options Tuning knobs — see {@link ActOptions}\n */\n constructor(\n public readonly registry: Registry<TSchemaReg, TEvents, TActions>,\n private readonly _states: Map<string, State<any, any, any>> = new Map(),\n batchHandlers: Map<string, BatchHandler<any>> = new Map(),\n options: ActOptions = {}\n ) {\n this._batch_handlers = batchHandlers;\n this._es = buildEs(this._logger);\n this._cd = buildDrain<TEvents>(this._logger);\n this._handle = buildHandle<TEvents, TActions, TActor>({\n logger: this._logger,\n boundDo: this._bound_do,\n boundLoad: this._bound_load,\n boundQuery: this._bound_query,\n boundQueryArray: this._bound_query_array,\n });\n this._handle_batch = buildHandleBatch<TEvents>(this._logger);\n\n const { staticTargets, hasDynamicResolvers, reactiveEvents, eventToState } =\n classifyRegistry(this.registry, this._states);\n this._reactive_events = reactiveEvents;\n this._event_to_state = eventToState;\n\n this._drain = new DrainController({\n logger: this._logger,\n ops: this._cd,\n registry: this.registry,\n batchHandlers: this._batch_handlers,\n handle: this._handle,\n handleBatch: this._handle_batch,\n onAcked: (acked) => this.emit(\"acked\", acked),\n onBlocked: (blocked) => this.emit(\"blocked\", blocked),\n });\n\n this._correlate = new CorrelateCycle(\n this.registry,\n staticTargets,\n hasDynamicResolvers,\n this._cd,\n options.maxSubscribedStreams ?? DEFAULT_MAX_SUBSCRIBED_STREAMS,\n // Cold start: assume drain is needed (historical events may need processing)\n () => {\n if (this._reactive_events.size > 0) this._drain.arm();\n }\n );\n this._settle = new SettleLoop<TEvents>(\n {\n logger: this._logger,\n init: () => this._correlate.init(),\n checkpoint: () => this._correlate.checkpoint,\n correlate: (q) => this.correlate(q),\n drain: (o) => this.drain(o),\n onSettled: (drain) => this.emit(\"settled\", drain),\n },\n options.settleDebounceMs ?? DEFAULT_SETTLE_DEBOUNCE_MS\n );\n\n dispose(() => {\n this._emitter.removeAllListeners();\n this.stop_correlations();\n this.stop_settling();\n return Promise.resolve();\n });\n }\n\n /**\n * Executes an action on a state instance, committing resulting events.\n *\n * This is the primary method for modifying state. It:\n * 1. Validates the action payload against the schema\n * 2. Loads the current state snapshot\n * 3. Checks invariants (business rules)\n * 4. Executes the action handler to generate events\n * 5. Applies events to create new state\n * 6. Commits events to the store with optimistic concurrency control\n *\n * @template TKey - Action name from registered actions\n * @param action - The name of the action to execute\n * @param target - Target specification with stream ID and actor context\n * @param payload - Action payload matching the action's schema\n * @param reactingTo - Optional event that triggered this action (for correlation)\n * @param skipValidation - Skip schema validation (use carefully, for performance)\n * @returns Array of snapshots for all affected states (usually one)\n *\n * @throws {ValidationError} If payload doesn't match action schema\n * @throws {InvariantError} If business rules are violated\n * @throws {ConcurrencyError} If another process modified the stream\n *\n * @example Basic action execution\n * ```typescript\n * const snapshots = await app.do(\n * \"increment\",\n * {\n * stream: \"counter-1\",\n * actor: { id: \"user1\", name: \"Alice\" }\n * },\n * { by: 5 }\n * );\n *\n * console.log(snapshots[0].state.count); // Current count after increment\n * ```\n *\n * @example With error handling\n * ```typescript\n * try {\n * await app.do(\n * \"withdraw\",\n * { stream: \"account-123\", actor: { id: \"user1\", name: \"Alice\" } },\n * { amount: 1000 }\n * );\n * } catch (error) {\n * if (error instanceof InvariantError) {\n * console.error(\"Business rule violated:\", error.description);\n * } else if (error instanceof ConcurrencyError) {\n * console.error(\"Concurrent modification detected, retry...\");\n * } else if (error instanceof ValidationError) {\n * console.error(\"Invalid payload:\", error.details);\n * }\n * }\n * ```\n *\n * @example Reaction triggering another action (reactingTo auto-injected)\n * ```typescript\n * const app = act()\n * .withState(Order)\n * .withState(Inventory)\n * .on(\"OrderPlaced\")\n * .do(async function reduceInventory(event, _stream, app) {\n * // Inside reaction handlers, reactingTo is auto-injected when omitted.\n * // The triggering event is used by default, maintaining the correlation chain.\n * await app.do(\n * \"reduceStock\",\n * { stream: \"inventory-1\", actor: { id: \"sys\", name: \"system\" } },\n * { amount: event.data.items.length }\n * );\n * // To use a different correlation, pass reactingTo explicitly:\n * // await app.do(\"reduceStock\", target, payload, customEvent);\n * })\n * .to(\"inventory-1\")\n * .build();\n * ```\n *\n * @see {@link Target} for target structure\n * @see {@link Snapshot} for return value structure\n * @see {@link ValidationError}, {@link InvariantError}, {@link ConcurrencyError}\n */\n async do<TKey extends keyof TActions>(\n action: TKey,\n target: Target<TActor>,\n payload: Readonly<TActions[TKey]>,\n reactingTo?: Committed<TEvents, string & keyof TEvents>,\n skipValidation = false\n ) {\n const snapshots = await this._es.action(\n this.registry.actions[action],\n action,\n target,\n payload,\n reactingTo,\n skipValidation\n );\n // Arm the drain when any committed event has reactions.\n // Skip the scan entirely when no event has any reaction (common in\n // pure-state-machine apps).\n if (this._reactive_events.size > 0) {\n for (const snap of snapshots) {\n if (\n snap.event?.name &&\n this._reactive_events.has(snap.event.name as string)\n ) {\n this._drain.arm();\n break;\n }\n }\n }\n this.emit(\"committed\", snapshots as Snapshot<TSchemaReg, TEvents>[]);\n return snapshots;\n }\n\n /**\n * Loads the current state snapshot for a specific stream.\n *\n * Reconstructs the current state by replaying events from the event store.\n * Uses snapshots when available to optimize loading performance.\n *\n * Accepts either a State definition object or a state name string. When\n * using a string, the merged state (from partial states registered via\n * `.withState()`) is resolved by name.\n *\n * @template TNewState - State schema type\n * @template TNewEvents - Event schemas type\n * @template TNewActions - Action schemas type\n * @param state - The state definition or state name to load\n * @param stream - The stream ID (state instance identifier)\n * @param callback - Optional callback invoked with the loaded snapshot\n * @returns The current state snapshot for the stream\n *\n * @example Load by state definition\n * ```typescript\n * const snapshot = await app.load(Counter, \"counter-1\");\n * console.log(snapshot.state.count); // Current count\n * console.log(snapshot.patches); // Events since last snapshot\n * ```\n *\n * @example Load by state name (useful with partial states)\n * ```typescript\n * const snapshot = await app.load(\"Ticket\", \"ticket-123\");\n * console.log(snapshot.state.title); // Merged state from all partials\n * ```\n *\n * @example Load multiple states\n * ```typescript\n * const [user, account] = await Promise.all([\n * app.load(User, \"user-123\"),\n * app.load(BankAccount, \"account-456\")\n * ]);\n * ```\n *\n * @see {@link Snapshot} for snapshot structure\n */\n async load<\n TNewState extends Schema,\n TNewEvents extends Schemas,\n TNewActions extends Schemas,\n >(\n state: State<TNewState, TNewEvents, TNewActions>,\n stream: string,\n callback?: (snapshot: Snapshot<TNewState, TNewEvents>) => void,\n asOf?: AsOf\n ): Promise<Snapshot<TNewState, TNewEvents>>;\n async load<TKey extends keyof TStateMap & string>(\n name: TKey,\n stream: string,\n callback?: (snapshot: Snapshot<TStateMap[TKey], TEvents>) => void,\n asOf?: AsOf\n ): Promise<Snapshot<TStateMap[TKey], TEvents>>;\n async load<TNewState extends Schema>(\n stateOrName: State<TNewState, any, any> | string,\n stream: string,\n callback?: (snapshot: Snapshot<any, any>) => void,\n asOf?: AsOf\n ): Promise<Snapshot<any, any>> {\n let merged: State<any, any, any>;\n if (typeof stateOrName === \"string\") {\n const found = this._states.get(stateOrName);\n if (!found) throw new Error(`State \"${stateOrName}\" not found`);\n merged = found;\n } else {\n merged = this._states.get(stateOrName.name) || stateOrName;\n }\n return await this._es.load(merged, stream, callback, asOf);\n }\n\n /**\n * Queries the event store for events matching a filter.\n *\n * Use this for analyzing event streams, generating reports, or debugging.\n * The callback is invoked for each matching event, and the method returns\n * summary information (first event, last event, total count).\n *\n * For small result sets, consider using {@link query_array} instead.\n *\n * @param query - Filter criteria — see {@link Query} for available fields\n * (`stream`, `name`, `after`, `before`, `created_after`, `created_before`,\n * `limit`, `with_snaps`, `stream_exact`)\n * @param callback - Optional callback invoked for each matching event\n * @returns Object with first event, last event, and total count\n *\n * @example Query all events for a stream\n * ```typescript\n * const { first, last, count } = await app.query(\n * { stream: \"counter-1\" },\n * (event) => console.log(event.name, event.data)\n * );\n * console.log(`Found ${count} events from ${first?.id} to ${last?.id}`);\n * ```\n *\n * @example Query specific event types\n * ```typescript\n * const { count } = await app.query(\n * { name: \"UserCreated\", limit: 100 },\n * (event) => {\n * console.log(\"User created:\", event.data.email);\n * }\n * );\n * ```\n *\n * @example Query events in time range\n * ```typescript\n * const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000);\n * const { count } = await app.query({\n * created_after: yesterday,\n * stream: \"user-123\"\n * });\n * console.log(`User had ${count} events in last 24 hours`);\n * ```\n *\n * @see {@link query_array} for loading events into memory\n */\n async query(\n query: Query,\n callback?: (event: Committed<TEvents, keyof TEvents>) => void\n ): Promise<{\n first?: Committed<TEvents, keyof TEvents>;\n last?: Committed<TEvents, keyof TEvents>;\n count: number;\n }> {\n let first: Committed<TEvents, keyof TEvents> | undefined;\n let last: Committed<TEvents, keyof TEvents> | undefined;\n const count = await store().query<TEvents>((e) => {\n if (!first) first = e;\n last = e;\n callback?.(e);\n }, query);\n return { first, last, count };\n }\n\n /**\n * Queries the event store and returns all matching events in memory.\n *\n * **Use with caution** - this loads all results into memory. For large result sets,\n * use {@link query} with a callback instead to process events incrementally.\n *\n * @param query - The query filter (same as {@link query})\n * @returns Array of all matching events\n *\n * @example Load all events for a stream\n * ```typescript\n * const events = await app.query_array({ stream: \"counter-1\" });\n * console.log(`Loaded ${events.length} events`);\n * events.forEach(event => console.log(event.name, event.data));\n * ```\n *\n * @example Get recent events\n * ```typescript\n * const recent = await app.query_array({\n * stream: \"user-123\",\n * limit: 10\n * });\n * ```\n *\n * @see {@link query} for large result sets\n */\n async query_array(\n query: Query\n ): Promise<Committed<TEvents, keyof TEvents>[]> {\n const events: Committed<TEvents, keyof TEvents>[] = [];\n await store().query<TEvents>((e) => events.push(e), query);\n return events;\n }\n\n /**\n * Processes pending reactions by draining uncommitted events from the event store.\n *\n * Runs a single drain cycle:\n * 1. Polls the store for streams with uncommitted events\n * 2. Leases streams to prevent concurrent processing\n * 3. Fetches events for each leased stream\n * 4. Executes matching reaction handlers\n * 5. Acknowledges successful reactions or blocks failing ones\n *\n * Drain uses a dual-frontier strategy to balance processing of new streams (lagging)\n * vs active streams (leading). The ratio adapts based on event pressure.\n *\n * Call `correlate()` before `drain()` to discover target streams. For a higher-level\n * API that handles debouncing, correlation, and signaling automatically, use {@link settle}.\n *\n * @param options - Drain configuration — see {@link DrainOptions} for fields\n * (`streamLimit`, `eventLimit`, `leaseMillis`).\n * @returns Drain statistics with fetched, leased, acked, and blocked counts\n *\n * @example In tests and scripts\n * ```typescript\n * await app.do(\"createUser\", target, payload);\n * await app.correlate();\n * await app.drain();\n * ```\n *\n * @example In production, prefer settle()\n * ```typescript\n * await app.do(\"CreateItem\", target, input);\n * app.settle(); // debounced correlate→drain, emits \"settled\"\n * ```\n *\n * @see {@link settle} for debounced correlate→drain with lifecycle events\n * @see {@link correlate} for dynamic stream discovery\n * @see {@link start_correlations} for automatic correlation\n */\n async drain(options: DrainOptions = {}): Promise<Drain<TEvents>> {\n return this._drain.drain(options);\n }\n\n /**\n * Discovers and registers new streams dynamically based on reaction resolvers.\n *\n * Correlation enables \"dynamic reactions\" where target streams are determined at runtime\n * based on event content. For example, you might create a stats stream for each user\n * when they perform certain actions.\n *\n * This method scans events matching the query and identifies new target streams based\n * on reaction resolvers. It then registers these streams so they'll be picked up by\n * the next drain cycle.\n *\n * @param query - Query filter to scan for new correlations\n * @param query - Scan filter — see {@link Query} for fields (typically\n * `{ after: <event-id>, limit: <count> }`)\n * @returns Object with newly leased streams and last scanned event ID\n *\n * @example Manual correlation\n * ```typescript\n * // Scan for new streams\n * const { leased, last_id } = await app.correlate({ after: 0, limit: 100 });\n * console.log(`Found ${leased.length} new streams`);\n *\n * // Save last_id for next scan\n * await saveCheckpoint(last_id);\n * ```\n *\n * @example Dynamic stream creation\n * ```typescript\n * const app = act()\n * .withState(User)\n * .withState(UserStats)\n * .on(\"UserLoggedIn\")\n * .do(async (event) => [\"incrementLoginCount\", {}])\n * .to((event) => ({\n * target: `stats-${event.stream}` // Dynamic target per user\n * }))\n * .build();\n *\n * // Discover stats streams as users log in\n * await app.correlate();\n * ```\n *\n * @see {@link start_correlations} for automatic periodic correlation\n * @see {@link stop_correlations} to stop automatic correlation\n */\n async correlate(\n query: Query = { after: -1, limit: 10 }\n ): Promise<{ subscribed: number; last_id: number }> {\n return this._correlate.correlate(query);\n }\n\n /**\n * Starts automatic periodic correlation worker for discovering new streams.\n *\n * The correlation worker runs in the background, scanning for new events and identifying\n * new target streams based on reaction resolvers. It maintains a sliding window that\n * advances with each scan, ensuring all events are eventually correlated.\n *\n * This is useful for dynamic stream creation patterns where you don't know all streams\n * upfront - they're discovered as events arrive.\n *\n * **Note:** Only one correlation worker can run at a time per Act instance.\n *\n * @param query - Query filter for correlation scans — see {@link Query}\n * (typically `{ after: -1, limit: 100 }`)\n * @param frequency - Correlation frequency in milliseconds (default: 10000)\n * @param callback - Optional callback invoked with newly discovered streams\n * @returns `true` if worker started, `false` if already running\n *\n * @example Start automatic correlation\n * ```typescript\n * // Start correlation worker scanning every 5 seconds\n * app.start_correlations(\n * { after: 0, limit: 100 },\n * 5000,\n * (leased) => {\n * console.log(`Discovered ${leased.length} new streams`);\n * }\n * );\n *\n * // Later, stop it\n * app.stop_correlations();\n * ```\n *\n * @example With checkpoint persistence\n * ```typescript\n * // Load last checkpoint\n * const lastId = await loadCheckpoint();\n *\n * app.start_correlations(\n * { after: lastId, limit: 100 },\n * 10000,\n * async (leased) => {\n * // Save checkpoint for next restart\n * if (leased.length) {\n * const maxId = Math.max(...leased.map(l => l.at));\n * await saveCheckpoint(maxId);\n * }\n * }\n * );\n * ```\n *\n * @see {@link correlate} for manual one-time correlation\n * @see {@link stop_correlations} to stop the worker\n */\n start_correlations(\n query: Query = {},\n frequency = 10_000,\n callback?: (subscribed: number) => void\n ): boolean {\n return this._correlate.startPolling(query, frequency, callback);\n }\n\n /**\n * Stops the automatic correlation worker.\n *\n * Call this to stop the background correlation worker started by {@link start_correlations}.\n * This is automatically called when the Act instance is disposed.\n *\n * @example\n * ```typescript\n * // Start correlation\n * app.start_correlations();\n *\n * // Later, stop it\n * app.stop_correlations();\n * ```\n *\n * @see {@link start_correlations}\n */\n stop_correlations() {\n this._correlate.stopPolling();\n }\n\n /**\n * Cancels any pending or active settle cycle.\n *\n * @see {@link settle}\n */\n stop_settling() {\n this._settle.stop();\n }\n\n /**\n * Reset reaction stream watermarks and request a drain on the next\n * `drain()` / `settle()` cycle.\n *\n * Use this to replay events through projections (or other reaction targets)\n * after changing handler logic. Equivalent to calling `store().reset(streams)`\n * directly, but also raises the orchestrator's internal \"needs drain\" flag —\n * `store().reset(...)` alone leaves the flag untouched, so a settled app\n * would short-circuit and skip the replay.\n *\n * Pair with `app.settle()` (or a single `app.drain()` for small streams).\n * `settle()` loops correlate→drain until no progress is made, so one call\n * fully catches up paginated streams without forcing callers to roll\n * their own loop.\n *\n * @param streams - Reaction target streams (e.g., projection names) to reset\n * @returns Count of streams that were actually reset\n *\n * @example Rebuild a projection (production)\n * ```typescript\n * await app.reset([\"my-projection\"]);\n * app.settle({ eventLimit: 1000 }); // emits \"settled\" when fully replayed\n * ```\n *\n * @example Rebuild a projection (tests / scripts)\n * ```typescript\n * await app.reset([\"my-projection\"]);\n * await app.drain({ eventLimit: 1000 }); // small streams: one pass is enough\n * ```\n *\n * @see {@link Store.reset} for the underlying store primitive\n * @see {@link settle} for the debounced full-catch-up loop\n */\n async reset(streams: string[]): Promise<number> {\n const count = await store().reset(streams);\n if (count > 0 && this._reactive_events.size > 0) this._drain.arm();\n return count;\n }\n\n /**\n * Close the books — guard, archive, truncate, and optionally restart streams.\n *\n * Safely removes historical events from the operational store:\n *\n * 1. **Correlate** — discover pending reaction targets\n * 2. **Safety check** — skip streams with pending reactions (skipped when no reactive events)\n * 3. **Guard** — commit `__tombstone__` with `expectedVersion` to block concurrent writes\n * 4. **Load state** — for streams in `snapshots`, load final state while guarded (no races)\n * 5. **Archive** — user callback per stream (abort-all on failure, streams are guarded)\n * 6. **Truncate + seed** — atomic: delete all events, insert `__snapshot__` or `__tombstone__`\n * 7. **Cache** — invalidate (tombstoned) or warm (restarted)\n * 8. **Emit \"closed\"** — lifecycle event with results\n *\n * @param targets - Per-stream close options (stream, restart?, archive?)\n * @returns `{ truncated: TruncateResult, skipped: string[] }`\n *\n * @example Archive and close\n * ```typescript\n * await app.close([\n * { stream: \"order-123\", archive: async () => { await archiveToS3(\"order-123\"); } },\n * { stream: \"order-456\" },\n * ]);\n * ```\n *\n * @example Close with restart (state loaded automatically after guard)\n * ```typescript\n * await app.close([\n * { stream: \"counter-1\", restart: true },\n * { stream: \"counter-2\" }, // tombstoned\n * ]);\n * ```\n */\n async close(targets: CloseTarget[]): Promise<CloseResult> {\n if (!targets.length) return { truncated: new Map(), skipped: [] };\n\n // Correlate first so dynamic reaction targets are discovered before\n // the safety check examines subscription positions.\n await this.correlate({ limit: 1000 });\n\n const result = await runCloseCycle(targets, {\n reactiveEventsSize: this._reactive_events.size,\n eventToState: this._event_to_state,\n load: this._es.load,\n tombstone: this._es.tombstone,\n logger: this._logger,\n });\n\n this.emit(\"closed\", result);\n return result;\n }\n\n /**\n * Debounced, non-blocking correlate→drain cycle.\n *\n * Call this after `app.do()` (or `app.reset()`) to schedule a background\n * drain. Multiple rapid calls within the debounce window are coalesced\n * into a single cycle. Runs correlate→drain in a loop until a pass makes\n * no progress — no new subscriptions, no acks, no blocks — then emits\n * the `\"settled\"` lifecycle event. This means a single `settle()` call\n * fully catches up paginated streams (e.g. after `reset()` on a long\n * projection) without forcing callers to loop.\n *\n * @param options - Settle configuration — see {@link SettleOptions} for fields:\n * `debounceMs` (default 10), `correlate` (default `{ after: -1, limit: 100 }`),\n * `maxPasses` (default `Infinity` — kill-switch for runaway loops),\n * `streamLimit` (default 10), `eventLimit` (default 10),\n * `leaseMillis` (default 10000).\n *\n * @example API mutations\n * ```typescript\n * await app.do(\"CreateItem\", target, input);\n * app.settle(); // non-blocking, returns immediately\n *\n * app.on(\"settled\", (drain) => {\n * // notify SSE clients, invalidate caches, etc.\n * });\n * ```\n *\n * @see {@link drain} for single synchronous drain cycles\n * @see {@link correlate} for manual correlation\n */\n settle(options: SettleOptions = {}): void {\n this._settle.schedule(options);\n }\n}\n","/**\n * @module build-classify\n * @category Internal\n *\n * Build-time classification of the registry + state map. The Act constructor\n * needs four pre-computed inputs to wire its runtime subsystems:\n *\n * - `staticTargets` — known-up-front reaction targets (statics get\n * subscribed once at init; dynamics scan per event)\n * - `hasDynamicResolvers` — short-circuit flag for `correlate()`\n * - `reactiveEvents` — event names with at least one reaction (drives\n * the drain skip-flag in `do()` and `reset()`)\n * - `eventToState` — event-name → owning state, for `close({restart})`\n * seed loading in multi-state apps\n *\n * Pure function — single pass over events + states, fully testable without\n * instantiating Act.\n *\n * @internal\n */\n\nimport type {\n Registry,\n Schema,\n SchemaRegister,\n Schemas,\n State,\n} from \"../types/index.js\";\nimport type { StaticTarget } from \"./correlate-cycle.js\";\n\n/**\n * Classification result. Returned by {@link classifyRegistry}; consumed\n * piecewise by Act's constructor.\n *\n * @internal\n */\nexport type Classification = {\n readonly staticTargets: StaticTarget[];\n readonly hasDynamicResolvers: boolean;\n readonly reactiveEvents: ReadonlySet<string>;\n readonly eventToState: ReadonlyMap<string, State<any, any, any>>;\n};\n\n/**\n * Walk the registry once to collect static reaction targets, the dynamic-\n * resolvers flag, the set of reactive event names, and the event-to-state\n * map. Static targets are deduplicated by (target, source) — two reactions\n * routing to the same projection produce one subscription.\n *\n * @internal\n */\nexport function classifyRegistry<\n TSchemaReg extends SchemaRegister<TActions>,\n TEvents extends Schemas,\n TActions extends Schemas,\n>(\n registry: Registry<TSchemaReg, TEvents, TActions>,\n states: ReadonlyMap<string, State<Schema, any, any>>\n): Classification {\n const statics = new Map<string, StaticTarget>();\n const reactiveEvents = new Set<string>();\n let hasDynamicResolvers = false;\n\n for (const [name, register] of Object.entries(registry.events)) {\n if (register.reactions.size > 0) reactiveEvents.add(name);\n for (const reaction of register.reactions.values()) {\n if (typeof reaction.resolver === \"function\") {\n hasDynamicResolvers = true;\n } else {\n const { target, source } = reaction.resolver;\n const key = `${target}|${source ?? \"\"}`;\n if (!statics.has(key)) statics.set(key, { stream: target, source });\n }\n }\n }\n\n // Event-name → owning state. Duplicate event names are rejected at\n // registration time (merge.ts), so each entry is unambiguous.\n const eventToState = new Map<string, State<any, any, any>>();\n for (const merged of states.values()) {\n for (const eventName of Object.keys(merged.events)) {\n eventToState.set(eventName, merged);\n }\n }\n\n return {\n staticTargets: [...statics.values()],\n hasDynamicResolvers,\n reactiveEvents,\n eventToState,\n };\n}\n","/**\n * @module close-cycle\n * @category Internal\n *\n * Pure orchestration of the close-the-books flow: scan stream heads,\n * partition by reaction safety, guard with tombstones, optionally seed\n * restart state, run user archive callbacks, atomically truncate, and\n * update the cache.\n *\n * The Act orchestrator owns lifecycle (correlate gate, emit(\"closed\")) and\n * the registry-derived inputs (reactive-event count, event→state map). All\n * sequential phase work between those state touches lives here.\n *\n * @internal\n */\n\nimport { randomUUID } from \"node:crypto\";\nimport { cache, store, TOMBSTONE_EVENT } from \"../ports.js\";\nimport type {\n CloseResult,\n CloseTarget,\n Logger,\n Schema,\n State,\n} from \"../types/index.js\";\nimport type { EsOps } from \"./event-sourcing.js\";\n\n/**\n * Dependencies the close cycle needs from the Act orchestrator. Decoupled\n * from `Act` itself so the cycle can be exercised from tests in isolation.\n *\n * @internal\n */\nexport type CloseCycleDeps = {\n readonly reactiveEventsSize: number;\n readonly eventToState: ReadonlyMap<string, State<any, any, any>>;\n readonly load: EsOps[\"load\"];\n readonly tombstone: EsOps[\"tombstone\"];\n readonly logger: Logger;\n};\n\n/**\n * Per-stream scan result: latest non-tombstone domain event metadata.\n * `lastEventName` is always defined — the scan filters tombstones in the\n * callback and queries without `with_snaps`, so any event reaching the\n * callback is a domain event whose name we capture alongside id/version.\n */\ntype StreamHead = {\n readonly maxId: number;\n readonly version: number;\n readonly lastEventName: string;\n};\n\n/**\n * Run the full close cycle for the given targets. Caller owns the\n * lifecycle event emission.\n *\n * @internal\n */\nexport async function runCloseCycle(\n targets: CloseTarget[],\n deps: CloseCycleDeps\n): Promise<CloseResult> {\n // Caller (Act.close) filters empty targets; runCloseCycle assumes at\n // least one target.\n const targetMap = new Map(targets.map((t) => [t.stream, t]));\n const streams = [...targetMap.keys()];\n const skipped: string[] = [];\n\n // 1. Scan: find the latest non-tombstone event per stream\n const streamInfo = await scanStreamHeads(streams);\n\n // 2. Partition: skip streams with pending reactions in flight\n const safe = await partitionBySafety(\n streamInfo,\n deps.reactiveEventsSize,\n skipped\n );\n if (!safe.length) return { truncated: new Map(), skipped };\n\n // 3. Guard: commit a tombstone with expectedVersion per safe stream\n const correlation = randomUUID();\n const { guarded, guardEvents } = await guardWithTombstones(\n safe,\n streamInfo,\n correlation,\n deps.tombstone,\n skipped\n );\n if (!guarded.length) return { truncated: new Map(), skipped };\n\n // 4. Seed: load final state for restart targets through the owning state\n const seedStates = await loadRestartSeeds(\n guarded,\n targetMap,\n streamInfo,\n deps.eventToState,\n deps.load,\n deps.logger\n );\n\n // 5. Archive: user-provided per-stream callback while guarded\n await runArchiveCallbacks(guarded, targetMap);\n\n // 6. Truncate + seed: atomic per-store transaction\n const truncated = await truncateAndWarmCache(\n guarded,\n seedStates,\n guardEvents,\n correlation\n );\n\n return { truncated, skipped };\n}\n\n// ---------------------------------------------------------------------------\n// Phase 1 — scan stream heads\n// ---------------------------------------------------------------------------\n\nasync function scanStreamHeads(\n streams: string[]\n): Promise<Map<string, StreamHead>> {\n const out = new Map<string, StreamHead>();\n await Promise.all(\n streams.map(async (s) => {\n let maxId = -1;\n let version = -1;\n let lastEventName = \"\";\n await store().query(\n (e) => {\n // backward iteration: first non-tombstone is the most recent\n // domain event. snaps are filtered server-side (no with_snaps).\n if (e.name === TOMBSTONE_EVENT || maxId !== -1) return;\n maxId = e.id;\n version = e.version;\n lastEventName = e.name;\n },\n { stream: s, stream_exact: true, backward: true, limit: 1 }\n );\n if (maxId >= 0) out.set(s, { maxId, version, lastEventName });\n })\n );\n return out;\n}\n\n// ---------------------------------------------------------------------------\n// Phase 2 — partition by safety\n// ---------------------------------------------------------------------------\n\nasync function partitionBySafety(\n streamInfo: Map<string, StreamHead>,\n reactiveEventsSize: number,\n skipped: string[]\n): Promise<string[]> {\n if (reactiveEventsSize === 0) return [...streamInfo.keys()];\n\n // Read-only probe: query_streams returns subscription positions without\n // leasing or mutating retry state.\n const pendingSet = new Set<string>();\n await store().query_streams((position) => {\n const sourceRe = position.source ? RegExp(position.source) : undefined;\n for (const [stream, info] of streamInfo) {\n if ((!sourceRe || sourceRe.test(stream)) && position.at < info.maxId) {\n pendingSet.add(stream);\n }\n }\n });\n\n const safe: string[] = [];\n for (const [stream] of streamInfo) {\n if (pendingSet.has(stream)) skipped.push(stream);\n else safe.push(stream);\n }\n return safe;\n}\n\n// ---------------------------------------------------------------------------\n// Phase 3 — guard with tombstones\n// ---------------------------------------------------------------------------\n\nasync function guardWithTombstones(\n safe: string[],\n streamInfo: Map<string, StreamHead>,\n correlation: string,\n tombstone: EsOps[\"tombstone\"],\n skipped: string[]\n): Promise<{\n guarded: string[];\n guardEvents: Map<string, { id: number; stream: string }>;\n}> {\n const guarded: string[] = [];\n const guardEvents = new Map<string, { id: number; stream: string }>();\n await Promise.all(\n safe.map(async (stream) => {\n const info = streamInfo.get(stream)!;\n const committed = await tombstone(stream, info.version, correlation);\n if (committed) {\n guarded.push(stream);\n guardEvents.set(stream, { id: committed.id, stream });\n } else {\n // ConcurrencyError → another writer beat the guard\n skipped.push(stream);\n }\n })\n );\n return { guarded, guardEvents };\n}\n\n// ---------------------------------------------------------------------------\n// Phase 4 — load restart seeds\n// ---------------------------------------------------------------------------\n\nasync function loadRestartSeeds(\n guarded: string[],\n targetMap: Map<string, CloseTarget>,\n streamInfo: Map<string, StreamHead>,\n eventToState: ReadonlyMap<string, State<any, any, any>>,\n load: EsOps[\"load\"],\n logger: Logger\n): Promise<Map<string, Schema>> {\n const seedStates = new Map<string, Schema>();\n await Promise.all(\n guarded\n .filter((s) => targetMap.get(s)?.restart)\n .map(async (stream) => {\n // streamInfo entry is guaranteed (guarded ⊆ streamInfo.keys()).\n const lastEventName = streamInfo.get(stream)!.lastEventName;\n const ownerState = eventToState.get(lastEventName);\n if (!ownerState) {\n // No registered state owns the stream's events (deleted state,\n // schema versioning gone wrong, etc.). Tombstone instead of\n // seeding a corrupted snapshot.\n logger.error(\n `Cannot seed restart for \"${stream}\": no registered state owns event \"${lastEventName}\". Stream will be tombstoned instead.`\n );\n return;\n }\n const snap = await load(ownerState, stream);\n seedStates.set(stream, snap.state as Schema);\n })\n );\n return seedStates;\n}\n\n// ---------------------------------------------------------------------------\n// Phase 5 — archive callbacks\n// ---------------------------------------------------------------------------\n\nasync function runArchiveCallbacks(\n guarded: string[],\n targetMap: Map<string, CloseTarget>\n): Promise<void> {\n // Sequential — user callbacks may share resources (S3 client, etc.) and\n // a failure should propagate to the caller without leaving partial state.\n for (const stream of guarded) {\n const archiveFn = targetMap.get(stream)?.archive;\n if (archiveFn) await archiveFn();\n }\n}\n\n// ---------------------------------------------------------------------------\n// Phase 6 — atomic truncate + cache warm\n// ---------------------------------------------------------------------------\n\nasync function truncateAndWarmCache(\n guarded: string[],\n seedStates: Map<string, Schema>,\n guardEvents: Map<string, { id: number; stream: string }>,\n correlation: string\n): Promise<CloseResult[\"truncated\"]> {\n const truncTargets = guarded.map((stream) => {\n const snapshot = seedStates.get(stream);\n const guard = guardEvents.get(stream)!;\n return {\n stream,\n snapshot,\n meta: {\n correlation,\n causation: {\n event: { id: guard.id, name: TOMBSTONE_EVENT, stream: guard.stream },\n },\n },\n };\n });\n const truncated = await store().truncate(truncTargets);\n\n // Cache invalidate / warm — use real event IDs from committed events\n await Promise.all(\n guarded.map(async (stream) => {\n const entry = truncated.get(stream);\n const state = seedStates.get(stream);\n if (state && entry) {\n await cache().set(stream, {\n state,\n version: entry.committed.version,\n event_id: entry.committed.id,\n patches: 0,\n snaps: 1,\n });\n } else {\n await cache().invalidate(stream);\n }\n })\n );\n\n return truncated;\n}\n","/**\n * @module correlate-cycle\n * @category Internal\n *\n * Correlation — the discovery half of the correlate→drain pair. Owns the\n * lazy init (subscribe static targets, read cold-start watermark), the\n * dynamic-resolver scan that registers new streams as events arrive, and\n * the periodic timer that drives background discovery.\n *\n * The Act orchestrator passes registry + classification (which static\n * targets to subscribe, whether any dynamic resolvers exist) at build\n * time; everything past that lives here.\n *\n * @internal\n */\n\nimport { LruSet } from \"../lru-map.js\";\nimport { log, store } from \"../ports.js\";\nimport type {\n Query,\n ReactionPayload,\n Registry,\n SchemaRegister,\n Schemas,\n} from \"../types/index.js\";\nimport type { DrainOps } from \"./drain.js\";\n\n/**\n * Static resolver target collected at build time. Subscribed once during\n * init; never re-evaluated.\n *\n * @internal\n */\nexport type StaticTarget = {\n readonly stream: string;\n readonly source?: string;\n};\n\n/**\n * Drives correlation for one Act instance. Owns the checkpoint, the\n * subscribed-streams LRU, and the periodic timer.\n *\n * @internal\n */\nexport class CorrelateCycle<\n TSchemaReg extends SchemaRegister<TActions>,\n TEvents extends Schemas,\n TActions extends Schemas,\n> {\n private _checkpoint = -1;\n private _initialized = false;\n private _timer: ReturnType<typeof setInterval> | undefined = undefined;\n private readonly _subscribed: LruSet<string>;\n\n constructor(\n private readonly registry: Registry<TSchemaReg, TEvents, TActions>,\n private readonly staticTargets: ReadonlyArray<StaticTarget>,\n private readonly hasDynamicResolvers: boolean,\n private readonly cd: DrainOps<TEvents>,\n maxSubscribedStreams: number,\n private readonly onInit?: () => void\n ) {\n this._subscribed = new LruSet(maxSubscribedStreams);\n }\n\n /** Last correlated event id. */\n get checkpoint(): number {\n return this._checkpoint;\n }\n\n /**\n * Initialize correlation state on first call.\n * - Reads max(at) from store as cold-start checkpoint\n * - Subscribes static resolver targets (idempotent upsert)\n * - Populates the subscribed-streams LRU\n * - Fires `onInit` once (Act uses this to flag a cold-start drain)\n */\n async init(): Promise<void> {\n if (this._initialized) return;\n this._initialized = true;\n\n const { watermark } = await store().subscribe([...this.staticTargets]);\n this._checkpoint = watermark;\n this.onInit?.();\n for (const { stream } of this.staticTargets) {\n this._subscribed.add(stream);\n }\n }\n\n /**\n * Discover dynamic-resolver targets in the events past the checkpoint\n * and register any new streams via `cd.subscribe`. Static targets are\n * subscribed at init time, so this only walks dynamic resolvers.\n */\n async correlate(\n query: Query = { after: -1, limit: 10 }\n ): Promise<{ subscribed: number; last_id: number }> {\n await this.init();\n\n // No dynamic resolvers — nothing to discover\n if (!this.hasDynamicResolvers)\n return { subscribed: 0, last_id: this._checkpoint };\n\n // Use checkpoint as floor, allow explicit query.after to override upward\n const after = Math.max(this._checkpoint, query.after || -1);\n const correlated = new Map<\n string,\n { source?: string; payloads: ReactionPayload<TEvents>[] }\n >();\n let last_id = after;\n await store().query<TEvents>(\n (event) => {\n last_id = event.id;\n const register = this.registry.events[event.name];\n // skip events with no registered reactions\n if (register) {\n for (const reaction of register.reactions.values()) {\n // only evaluate dynamic resolvers — statics are subscribed at init\n if (typeof reaction.resolver !== \"function\") continue;\n const resolved = reaction.resolver(event);\n if (resolved && !this._subscribed.has(resolved.target)) {\n const entry = correlated.get(resolved.target) || {\n source: resolved.source,\n payloads: [],\n };\n entry.payloads.push({\n ...reaction,\n source: resolved.source,\n event,\n });\n correlated.set(resolved.target, entry);\n }\n }\n }\n },\n { ...query, after }\n );\n\n if (correlated.size) {\n const streams = [...correlated.entries()].map(([stream, { source }]) => ({\n stream,\n source,\n }));\n const { subscribed } = await this.cd.subscribe(streams);\n // Advance checkpoint only after subscribe succeeds\n this._checkpoint = last_id;\n if (subscribed) {\n // Track newly subscribed dynamic targets\n for (const { stream } of streams) {\n this._subscribed.add(stream);\n }\n }\n return { subscribed, last_id };\n }\n // No streams to subscribe — safe to advance\n this._checkpoint = last_id;\n return { subscribed: 0, last_id };\n }\n\n /**\n * Start a periodic correlation worker. Returns false if one is already\n * running. Errors from `correlate()` are routed through `log()` so they\n * land in the configured logger (the timer keeps running on failure).\n */\n startPolling(\n query: Query = {},\n frequency = 10_000,\n callback?: (subscribed: number) => void\n ): boolean {\n if (this._timer) return false;\n\n const limit = query.limit || 100;\n this._timer = setInterval(\n () =>\n this.correlate({ ...query, after: this._checkpoint, limit })\n .then((result) => {\n if (callback && result.subscribed) callback(result.subscribed);\n })\n .catch((err) => log().error(err)),\n frequency\n );\n return true;\n }\n\n /** Stop the periodic correlation worker. Idempotent. */\n stopPolling(): void {\n if (this._timer) {\n clearInterval(this._timer);\n this._timer = undefined;\n }\n }\n}\n","/**\n * @module drain-cycle\n * @category Internal\n *\n * Two layers of the drain pipeline:\n *\n * - {@link runDrainCycle} — pure function for one round-trip of\n * claim → fetch → group → dispatch → ack/block. No orchestrator state.\n * Reusable for property tests and standalone benchmarks.\n *\n * - {@link DrainController} — stateful driver that owns the armed flag,\n * the concurrency lock, and the adaptive lag/lead ratio. Wraps\n * `runDrainCycle` with the lifecycle decisions Act used to make inline.\n *\n * @internal\n */\n\nimport { randomUUID } from \"node:crypto\";\nimport type {\n BatchHandler,\n BlockedLease,\n Drain,\n DrainOptions,\n Fetch,\n Lease,\n Logger,\n ReactionPayload,\n Registry,\n SchemaRegister,\n Schemas,\n} from \"../types/index.js\";\nimport type { DrainOps } from \"./drain.js\";\nimport { computeLagLeadRatio } from \"./drain-ratio.js\";\n\n/**\n * Outcome of processing a single leased stream — produced by Act's `handle`\n * / `handleBatch` dispatchers, consumed by `runDrainCycle` to drive ack/block.\n *\n * @internal\n */\nexport type HandleResult = Readonly<{\n lease: Lease;\n handled: number;\n at: number;\n error?: string;\n block?: boolean;\n}>;\n\n/**\n * Per-event reaction dispatcher signature (matches `Act.handle`).\n * @internal\n */\nexport type Handle<TEvents extends Schemas> = (\n lease: Lease,\n payloads: ReactionPayload<TEvents>[]\n) => Promise<HandleResult>;\n\n/**\n * Bulk reaction dispatcher signature (matches `Act.handleBatch`).\n * @internal\n */\nexport type HandleBatch<TEvents extends Schemas> = (\n lease: Lease,\n payloads: ReactionPayload<TEvents>[],\n batchHandler: BatchHandler<TEvents>\n) => Promise<HandleResult>;\n\n/**\n * One drain cycle's results. Returned by {@link runDrainCycle}; consumed by\n * `Act.drain()` to update lifecycle state, the lag/lead ratio, and emit the\n * `acked` / `blocked` lifecycle events.\n *\n * @internal\n */\nexport type DrainCycle<TEvents extends Schemas> = {\n readonly leased: Lease[];\n readonly fetched: Fetch<TEvents>;\n readonly handled: HandleResult[];\n readonly acked: Lease[];\n readonly blocked: BlockedLease[];\n};\n\n/**\n * Run one drain cycle: claim streams, fetch their events, dispatch\n * matching reactions, ack the successes, block the retries-exhausted.\n *\n * Returns `undefined` when nothing was claimed — caller can short-circuit\n * the rest of the drain pass.\n *\n * @internal\n */\nexport async function runDrainCycle<\n TEvents extends Schemas,\n TActions extends Schemas,\n TSchemaReg extends SchemaRegister<TActions>,\n>(\n ops: DrainOps<TEvents>,\n registry: Registry<TSchemaReg, TEvents, TActions>,\n batchHandlers: Map<string, BatchHandler<TEvents>>,\n handle: Handle<TEvents>,\n handleBatch: HandleBatch<TEvents>,\n lagging: number,\n leading: number,\n eventLimit: number,\n leaseMillis: number\n): Promise<DrainCycle<TEvents> | undefined> {\n // Atomically discover and lease streams (competing consumer pattern)\n const leased = await ops.claim(lagging, leading, randomUUID(), leaseMillis);\n if (!leased.length) return undefined;\n\n // Fetch events for each leased stream\n const fetched = await ops.fetch(leased, eventLimit);\n\n // Build a single index keyed by stream — collapses two passes\n // (payloadsMap build + per-lease fetched.find) into one Map lookup.\n type FetchEntry = (typeof fetched)[number];\n const fetchMap = new Map<\n string,\n { fetch: FetchEntry; payloads: ReactionPayload<TEvents>[] }\n >();\n\n // compute fetch window max event id\n const fetch_window_at = fetched.reduce(\n (max, { at, events }) => Math.max(max, events.at(-1)?.id || at),\n 0\n );\n\n for (const f of fetched) {\n const { stream, events } = f;\n const payloads = events.flatMap((event) => {\n const register = registry.events[event.name];\n if (!register) return [];\n return [...register.reactions.values()]\n .filter((reaction) => {\n const resolved =\n typeof reaction.resolver === \"function\"\n ? reaction.resolver(event)\n : reaction.resolver;\n return resolved && resolved.target === stream;\n })\n .map((reaction) => ({ ...reaction, event }));\n });\n fetchMap.set(stream, { fetch: f, payloads });\n }\n\n const handled = await Promise.all(\n leased.map((lease) => {\n // fetch() returns one entry per leased stream — fetchMap.get is\n // always defined here (asserted with `!`).\n const entry = fetchMap.get(lease.stream)!;\n // fast-forward watermark using fetched events or window max\n const at = entry.fetch.events.at(-1)?.id || fetch_window_at;\n const { payloads } = entry;\n const batchHandler = batchHandlers.get(lease.stream);\n if (batchHandler && payloads.length > 0) {\n return handleBatch({ ...lease, at }, payloads, batchHandler);\n }\n return handle({ ...lease, at }, payloads);\n })\n );\n\n const acked = await ops.ack(\n handled\n .filter(({ error }) => !error)\n .map(({ at, lease }) => ({ ...lease, at }))\n );\n\n const blocked = await ops.block(\n handled\n .filter(({ block }) => block)\n .map(({ lease, error }) => ({ ...lease, error: error! }))\n );\n\n return { leased, fetched, handled, acked, blocked };\n}\n\n/**\n * Empty drain result returned when the controller short-circuits (not\n * armed, locked out by a concurrent caller, claim returned nothing,\n * cycle threw).\n *\n * @internal\n */\nconst EMPTY_DRAIN: Drain<Schemas> = {\n fetched: [],\n leased: [],\n acked: [],\n blocked: [],\n};\n\n/**\n * Dependencies the {@link DrainController} needs from the orchestrator.\n * The lifecycle event sinks (`onAcked` / `onBlocked`) are callbacks so\n * this module doesn't reach back into Act's emitter.\n *\n * @internal\n */\nexport type DrainControllerDeps<\n TEvents extends Schemas,\n TActions extends Schemas,\n TSchemaReg extends SchemaRegister<TActions>,\n> = {\n readonly logger: Logger;\n readonly ops: DrainOps<TEvents>;\n readonly registry: Registry<TSchemaReg, TEvents, TActions>;\n readonly batchHandlers: Map<string, BatchHandler<TEvents>>;\n readonly handle: Handle<TEvents>;\n readonly handleBatch: HandleBatch<TEvents>;\n readonly onAcked: (acked: Lease[]) => void;\n readonly onBlocked: (blocked: BlockedLease[]) => void;\n};\n\n/**\n * Stateful driver around {@link runDrainCycle}. Owns:\n *\n * - `_armed` — has any commit / reset / cold-start signaled work to do?\n * - `_locked` — concurrent-call guard (overlapping `drain()` calls return\n * an empty result instead of running twice)\n * - `_ratio` — adaptive lag-to-lead frontier split, updated per cycle\n *\n * The orchestrator owns commits, lifecycle emission, and `arm()` triggers\n * — the controller owns everything between those edges.\n *\n * @internal\n */\nexport class DrainController<\n TEvents extends Schemas,\n TActions extends Schemas,\n TSchemaReg extends SchemaRegister<TActions>,\n> {\n private _armed = false;\n private _locked = false;\n private _ratio = 0.5;\n\n constructor(\n private readonly deps: DrainControllerDeps<TEvents, TActions, TSchemaReg>\n ) {}\n\n /**\n * Signal that a commit (or reset / cold-start) may have produced work.\n * Subsequent `drain()` calls will run the pipeline; once the pipeline\n * settles to no-progress, the controller disarms itself.\n */\n arm(): void {\n this._armed = true;\n }\n\n /** Read-only flag — true while a commit / reset is unprocessed. */\n get armed(): boolean {\n return this._armed;\n }\n\n /** Run one drain pass. Short-circuits when not armed or already running. */\n async drain({\n streamLimit = 10,\n eventLimit = 10,\n leaseMillis = 10_000,\n }: DrainOptions = {}): Promise<Drain<TEvents>> {\n if (!this._armed) return EMPTY_DRAIN as Drain<TEvents>;\n if (this._locked) return EMPTY_DRAIN as Drain<TEvents>;\n\n try {\n this._locked = true;\n const lagging = Math.ceil(streamLimit * this._ratio);\n const leading = streamLimit - lagging;\n\n const cycle = await runDrainCycle(\n this.deps.ops,\n this.deps.registry,\n this.deps.batchHandlers,\n this.deps.handle,\n this.deps.handleBatch,\n lagging,\n leading,\n eventLimit,\n leaseMillis\n );\n\n if (!cycle) {\n // claim() returned no leases — fully caught up\n this._armed = false;\n return EMPTY_DRAIN as Drain<TEvents>;\n }\n\n const { leased, fetched, handled, acked, blocked } = cycle;\n\n // Adapt next cycle's frontier split to where the pressure is.\n this._ratio = computeLagLeadRatio(handled, lagging, leading);\n\n if (acked.length) this.deps.onAcked(acked);\n if (blocked.length) this.deps.onBlocked(blocked);\n\n // Disarm only when fully caught up. Errors keep the flag set so\n // retries flow through the next drain.\n const hasErrors = handled.some(({ error }) => error);\n if (!acked.length && !blocked.length && !hasErrors) this._armed = false;\n\n return { fetched, leased, acked, blocked };\n } catch (error) {\n this.deps.logger.error(error);\n return EMPTY_DRAIN as Drain<TEvents>;\n } finally {\n this._locked = false;\n }\n }\n}\n","/**\n * @module drain-ratio\n * @category Internal\n *\n * Adaptive lag-to-lead ratio for the dual-frontier drain strategy.\n *\n * The orchestrator splits its per-cycle stream budget between two frontiers:\n *\n * - **lagging** — newly subscribed or behind streams catching up.\n * - **leading** — actively-processing streams at the head of the log.\n *\n * After each cycle, this helper looks at how many events were actually\n * handled in each frontier and shifts the next cycle's split toward\n * whichever frontier had the higher per-stream throughput. The result is\n * clamped to `[0.2, 0.8]` so neither frontier can be starved.\n *\n * @internal\n */\n\nimport type { HandleResult } from \"./drain-cycle.js\";\n\n/** Floor / ceiling for the lag-to-lead ratio so neither frontier starves. */\nconst RATIO_MIN = 0.2;\nconst RATIO_MAX = 0.8;\n/** Default ratio when no events were handled in either frontier. */\nconst RATIO_DEFAULT = 0.5;\n\n/**\n * Compute the next lag-to-lead ratio from the cycle's handled events and\n * the frontier sizes used to claim them. Returns `RATIO_DEFAULT` when no\n * progress was made (nothing to base a decision on).\n */\nexport function computeLagLeadRatio(\n handled: ReadonlyArray<HandleResult>,\n lagging: number,\n leading: number\n): number {\n let lagging_handled = 0;\n let leading_handled = 0;\n for (const { lease, handled: count } of handled) {\n if (lease.lagging) lagging_handled += count;\n else leading_handled += count;\n }\n const lagging_avg = lagging > 0 ? lagging_handled / lagging : 0;\n const leading_avg = leading > 0 ? leading_handled / leading : 0;\n const total = lagging_avg + leading_avg;\n if (total === 0) return RATIO_DEFAULT;\n return Math.max(RATIO_MIN, Math.min(RATIO_MAX, lagging_avg / total));\n}\n","/**\n * @module merge\n * @category Internal\n *\n * Shared utilities for merging partial states and projections across builders.\n * Lives in `internal/` because the symbols are consumed by the builder layer\n * (`act-builder`, `slice-builder`, `projection-builder`) but aren't part of\n * the public package surface.\n *\n * @internal\n */\nimport { ZodObject, type ZodType } from \"zod\";\nimport type { Projection } from \"../builders/projection-builder.js\";\nimport type { Schema, State } from \"../types/index.js\";\n\n/**\n * Unwraps wrapper types (ZodOptional, ZodNullable, ZodDefault, ZodReadonly)\n * to find the base type name, e.g. `z.string().optional()` -> `\"ZodString\"`.\n */\nfunction baseTypeName(zodType: ZodType): string {\n let t: any = zodType;\n while (typeof t.unwrap === \"function\") {\n t = t.unwrap();\n }\n return t.constructor.name;\n}\n\n/**\n * Merges two Zod schemas. If both are ZodObject instances, checks for\n * overlapping shape keys with incompatible base types (throws descriptive\n * error), then merges via `.extend()`. Falls back to keeping existing\n * schema if either is not a ZodObject.\n */\nfunction mergeSchemas(\n existing: ZodType,\n incoming: ZodType,\n stateName: string\n): ZodType {\n if (existing instanceof ZodObject && incoming instanceof ZodObject) {\n const existingShape = existing.shape as Record<string, ZodType>;\n const incomingShape = incoming.shape as Record<string, ZodType>;\n for (const key of Object.keys(incomingShape)) {\n if (key in existingShape) {\n const existingBase = baseTypeName(existingShape[key]);\n const incomingBase = baseTypeName(incomingShape[key]);\n if (existingBase !== incomingBase) {\n throw new Error(\n `Schema conflict in \"${stateName}\": key \"${key}\" has type \"${existingBase}\" but incoming partial declares \"${incomingBase}\"`\n );\n }\n }\n }\n return existing.extend(incomingShape);\n }\n return existing;\n}\n\n/**\n * Merges two init functions by spreading both results together.\n * Each partial only provides its own defaults.\n */\nfunction mergeInits<TState extends Schema>(\n existing: () => Readonly<TState>,\n incoming: () => Readonly<TState>\n): () => Readonly<TState> {\n return () => ({ ...existing(), ...incoming() });\n}\n\n/**\n * Registers a state into a states map and action/event registries,\n * merging with existing same-name states (partial state support).\n */\nexport function registerState(\n state: State<any, any, any>,\n states: Map<string, State<any, any, any>>,\n actions: Record<string, any>,\n events: Record<string, any>\n): void {\n const existing = states.get(state.name);\n if (existing) {\n mergeIntoExisting(state, existing, states, actions, events);\n } else {\n registerNewState(state, states, actions, events);\n }\n}\n\n/**\n * Registers a state for the first time. All action/event names must be unique\n * across the registry; collisions throw.\n */\nfunction registerNewState(\n state: State<any, any, any>,\n states: Map<string, State<any, any, any>>,\n actions: Record<string, any>,\n events: Record<string, any>\n): void {\n states.set(state.name, state);\n for (const name of Object.keys(state.actions)) {\n if (actions[name]) throw new Error(`Duplicate action \"${name}\"`);\n actions[name] = state;\n }\n for (const name of Object.keys(state.events)) {\n if (events[name]) throw new Error(`Duplicate event \"${name}\"`);\n events[name] = { schema: state.events[name], reactions: new Map() };\n }\n}\n\n/**\n * Merges an incoming partial state into an existing same-name state and\n * updates the action/event registries. Splits into four phases:\n * 1. validate no cross-state action/event collisions\n * 2. merge per-event patches (one custom patch per event)\n * 3. build the merged state and replace it in the states map\n * 4. update action→state pointers and register new events\n */\nfunction mergeIntoExisting(\n state: State<any, any, any>,\n existing: State<any, any, any>,\n states: Map<string, State<any, any, any>>,\n actions: Record<string, any>,\n events: Record<string, any>\n): void {\n // 1. Validate no cross-state collisions for actions/events\n for (const name of Object.keys(state.actions)) {\n // Same schema reference means the same partial re-registered via another slice\n if (existing.actions[name] === state.actions[name]) continue;\n if (actions[name]) throw new Error(`Duplicate action \"${name}\"`);\n }\n for (const name of Object.keys(state.events)) {\n // Same schema reference means the same partial re-registered via another slice\n if (existing.events[name] === state.events[name]) continue;\n // Allow same-name state partials to redeclare the same event\n // (e.g., a partial that only needs the event name for .on() reactions)\n if (existing.events[name]) continue;\n if (events[name]) throw new Error(`Duplicate event \"${name}\"`);\n }\n\n // 2. Merge patches with custom-vs-passthrough resolution\n const mergedPatch = mergePatches(existing.patch, state.patch, state.name);\n\n // 3. Build merged state\n const merged = {\n ...existing,\n state: mergeSchemas(existing.state, state.state, state.name),\n init: mergeInits(existing.init, state.init),\n events: { ...existing.events, ...state.events },\n actions: { ...existing.actions, ...state.actions },\n patch: mergedPatch,\n on: { ...existing.on, ...state.on },\n given: { ...existing.given, ...state.given },\n snap:\n state.snap && existing.snap && state.snap !== existing.snap\n ? (() => {\n throw new Error(\n `Duplicate snap strategy for state \"${state.name}\"`\n );\n })()\n : state.snap || existing.snap,\n };\n states.set(state.name, merged);\n\n // 4. Update action→state pointers; register events not yet seen\n for (const name of Object.keys(merged.actions)) {\n actions[name] = merged;\n }\n for (const name of Object.keys(state.events)) {\n if (events[name]) continue; // already registered, preserve reactions\n events[name] = { schema: state.events[name], reactions: new Map() };\n }\n}\n\n/**\n * Merges two patch maps. Only one custom (non-passthrough) patch per event is\n * allowed; passthroughs always yield to custom reducers, and re-registering\n * the same custom patch (same reference, e.g. across slices) is a no-op.\n */\nfunction mergePatches(\n existing: Record<string, any>,\n incoming: Record<string, any>,\n stateName: string\n): Record<string, any> {\n const merged = { ...existing };\n for (const name of Object.keys(incoming)) {\n const existingP = existing[name];\n const incomingP = incoming[name];\n if (!existingP) {\n merged[name] = incomingP;\n continue;\n }\n const existingIsDefault = existingP._passthrough;\n const incomingIsDefault = incomingP._passthrough;\n if (!existingIsDefault && !incomingIsDefault && existingP !== incomingP) {\n throw new Error(\n `Duplicate custom patch for event \"${name}\" in state \"${stateName}\"`\n );\n }\n // Keep whichever is custom; if both passthrough or existing custom, keep existing\n if (existingIsDefault && !incomingIsDefault) {\n merged[name] = incomingP;\n }\n }\n return merged;\n}\n\n/**\n * Merges reactions from one event register into another. The target is\n * assumed to already contain entries for every event name in the source\n * (e.g., act-builder's `.withSlice()` registers the slice's states first,\n * which seeds the target events). Reaction names collide by `set()`\n * semantics — last write wins.\n */\nexport function mergeEventRegister(\n target: Record<string, { reactions: Map<string, unknown> }>,\n source: Record<string, { reactions: Map<string, unknown> }>\n): void {\n for (const [eventName, sourceReg] of Object.entries(source)) {\n const targetReg = target[eventName];\n if (!targetReg) continue;\n for (const [name, reaction] of sourceReg.reactions) {\n targetReg.reactions.set(name, reaction);\n }\n }\n}\n\n/**\n * Merges a projection's event schemas and reactions into an event registry,\n * deduplicating reaction names by appending \"_p\" on collision.\n */\nexport function mergeProjection(\n proj: Projection<any>,\n events: Record<string, any>\n): void {\n for (const eventName of Object.keys(proj.events)) {\n const projRegister = proj.events[eventName];\n const existing = events[eventName];\n if (!existing) {\n events[eventName] = {\n schema: projRegister.schema,\n reactions: new Map(projRegister.reactions),\n };\n } else {\n for (const [name, reaction] of projRegister.reactions) {\n let key = name;\n while (existing.reactions.has(key)) key = `${key}_p`;\n existing.reactions.set(key, reaction);\n }\n }\n }\n}\n\n// Resolves the event stream as source and target (default)\nexport const _this_ = ({ stream }: { stream: string }) => ({\n source: stream,\n target: stream,\n});\n","/**\n * @module reactions\n * @category Internal\n *\n * Reaction dispatch — what runs inside the drain pipeline once `runDrainCycle`\n * has fetched events for a leased stream. Two shapes:\n *\n * - per-event `handle`: walks payloads sequentially, builds a scoped `IAct`\n * that auto-injects `reactingTo` so handlers don't have to thread it\n * through manually\n * - bulk `handleBatch`: hands every event for a static-target projection to\n * a single batch callback, enabling one-transaction replays\n *\n * Both share `_finalize`, which collapses the retry-vs-block decision and\n * the \"report error only when nothing was handled\" rule.\n *\n * @internal\n */\n\nimport type {\n Actor,\n BatchHandler,\n Committed,\n IAct,\n Lease,\n Logger,\n ReactionOptions,\n ReactionPayload,\n Schemas,\n Target,\n} from \"../types/index.js\";\nimport type { Handle, HandleBatch, HandleResult } from \"./drain-cycle.js\";\n\n/**\n * Dependencies a reaction handler needs from the orchestrator: the logger\n * for retry/error breadcrumbs, plus the bound `IAct` methods that the scoped\n * proxy hands to user reaction code.\n *\n * @internal\n */\nexport type ReactionDeps<\n TEvents extends Schemas,\n TActions extends Schemas,\n TActor extends Actor = Actor,\n> = {\n readonly logger: Logger;\n readonly boundDo: IAct<TEvents, TActions, TActor>[\"do\"];\n readonly boundLoad: IAct<TEvents, TActions, TActor>[\"load\"];\n readonly boundQuery: IAct<TEvents, TActions, TActor>[\"query\"];\n readonly boundQueryArray: IAct<TEvents, TActions, TActor>[\"query_array\"];\n};\n\n/**\n * Shared finalization: log the error, decide retry vs. block, surface the\n * error string only when nothing was handled (in batch mode `handled` is\n * always 0 on failure, so the rule degenerates to \"always reported\").\n */\nfunction finalize(\n lease: Lease,\n handled: number,\n at: number,\n error: Error | undefined,\n options: ReactionOptions,\n logger: Logger\n): HandleResult {\n if (!error) return { lease, handled, at };\n logger.error(error);\n const block = lease.retry >= options.maxRetries && options.blockOnError;\n if (block)\n logger.error(`Blocking ${lease.stream} after ${lease.retry} retries.`);\n return {\n lease,\n handled,\n at,\n error: handled === 0 ? error.message : undefined,\n block,\n };\n}\n\n/**\n * Builds the per-event reaction dispatcher passed to `runDrainCycle`.\n *\n * The scoped `IAct` proxy auto-injects the triggering event as `reactingTo`\n * when handlers call `do()` without it (#587), keeping the correlation\n * chain by default. The non-do methods are reused across all dispatches —\n * only `do` rebinds per payload because it captures the triggering event.\n *\n * @internal\n */\nexport function buildHandle<\n TEvents extends Schemas,\n TActions extends Schemas,\n TActor extends Actor = Actor,\n>(deps: ReactionDeps<TEvents, TActions, TActor>): Handle<TEvents> {\n const { logger, boundDo, boundLoad, boundQuery, boundQueryArray } = deps;\n return async (lease, payloads) => {\n if (payloads.length === 0) return { lease, handled: 0, at: lease.at };\n\n const stream = lease.stream;\n let at = payloads.at(0)!.event.id;\n let handled = 0;\n\n if (lease.retry > 0)\n logger.warn(`Retrying ${stream}@${at} (${lease.retry}).`);\n\n const scopedApp: IAct<TEvents, TActions, TActor> = {\n do: boundDo,\n load: boundLoad,\n query: boundQuery,\n query_array: boundQueryArray,\n };\n\n for (const payload of payloads) {\n const { event, handler } = payload;\n scopedApp.do = <TKey extends keyof TActions & string>(\n action: TKey,\n target: Target<TActor>,\n actionPayload: Readonly<TActions[TKey]>,\n reactingTo?: Committed<Schemas, string>,\n skipValidation?: boolean\n ) =>\n boundDo(\n action,\n target,\n actionPayload,\n (reactingTo ?? event) as Committed<TEvents, string & keyof TEvents>,\n skipValidation\n );\n try {\n await handler(event, stream, scopedApp);\n at = event.id;\n handled++;\n } catch (error) {\n return finalize(\n lease,\n handled,\n at,\n error as Error,\n payload.options,\n logger\n );\n }\n }\n return finalize(lease, handled, at, undefined, payloads[0].options, logger);\n };\n}\n\n/**\n * Builds the bulk reaction dispatcher passed to `runDrainCycle`. All events\n * for a static-target projection are handed to a single callback so the\n * projection can do one transaction per drain (catch-up replays especially).\n *\n * @internal\n */\nexport function buildHandleBatch<TEvents extends Schemas>(\n logger: Logger\n): HandleBatch<TEvents> {\n return async (\n lease: Lease,\n payloads: ReactionPayload<TEvents>[],\n batchHandler: BatchHandler<TEvents>\n ) => {\n const stream = lease.stream;\n const events = payloads.map((p) => p.event);\n const options = payloads[0].options;\n\n if (lease.retry > 0)\n logger.warn(`Retrying batch ${stream}@${events[0].id} (${lease.retry}).`);\n\n try {\n await batchHandler(events, stream);\n return finalize(\n lease,\n events.length,\n events.at(-1)!.id,\n undefined,\n options,\n logger\n );\n } catch (error) {\n return finalize(lease, 0, lease.at, error as Error, options, logger);\n }\n };\n}\n","/**\n * @module settle\n * @category Internal\n *\n * Debounced correlate→drain loop. Sits one level above both correlation\n * and drain: schedule() coalesces rapid callers into a single cycle, then\n * runs correlate+drain in a loop until a pass produces no progress.\n *\n * Owns the debounce timer and the reentrancy flag. Everything else is\n * supplied via the `SettleDeps` callbacks so this module stays free of\n * orchestrator state.\n *\n * @internal\n */\n\nimport type {\n Drain,\n DrainOptions,\n Logger,\n Query,\n Schemas,\n SettleOptions,\n} from \"../types/index.js\";\n\n/**\n * Callbacks the settle loop needs from the orchestrator. Modeled as an\n * input bag so this file doesn't import `Act` (avoids a cycle) and stays\n * independently testable.\n *\n * @internal\n */\nexport type SettleDeps<TEvents extends Schemas> = {\n readonly logger: Logger;\n readonly init: () => Promise<void>;\n readonly checkpoint: () => number;\n readonly correlate: (\n query: Query\n ) => Promise<{ subscribed: number; last_id: number }>;\n readonly drain: (options: DrainOptions) => Promise<Drain<TEvents>>;\n readonly onSettled: (drain: Drain<TEvents>) => void;\n};\n\n/**\n * Drives the debounced correlate→drain catch-up cycle. One instance per\n * Act orchestrator.\n *\n * @internal\n */\nexport class SettleLoop<TEvents extends Schemas> {\n private _timer: ReturnType<typeof setTimeout> | undefined = undefined;\n private _running = false;\n\n constructor(\n private readonly deps: SettleDeps<TEvents>,\n /** Debounce window applied when the caller doesn't override via `SettleOptions.debounceMs`. */\n private readonly defaultDebounceMs: number\n ) {}\n\n /**\n * Schedule a settle pass. Multiple calls inside the debounce window\n * coalesce into one cycle. The cycle runs correlate→drain in a loop\n * until no progress is made (no new subscriptions, no acks, no blocks)\n * or `maxPasses` is reached, then emits the `\"settled\"` lifecycle event\n * via {@link SettleDeps.onSettled}.\n */\n schedule(options: SettleOptions = {}): void {\n const {\n debounceMs = this.defaultDebounceMs,\n correlate: correlateQuery = { after: -1, limit: 100 },\n maxPasses = Infinity,\n ...drainOptions\n } = options;\n\n if (this._timer) clearTimeout(this._timer);\n this._timer = setTimeout(() => {\n this._timer = undefined;\n if (this._running) return;\n this._running = true;\n\n (async () => {\n await this.deps.init();\n let lastDrain: Drain<TEvents> | undefined;\n // Loop correlate→drain until a pass produces no work — this fully\n // catches up paginated streams (e.g. after `reset()` on a long\n // projection) without forcing callers to roll their own loop.\n // `maxPasses` caps runtime in pathological cases.\n for (let i = 0; i < maxPasses; i++) {\n const { subscribed } = await this.deps.correlate({\n ...correlateQuery,\n after: this.deps.checkpoint(),\n });\n lastDrain = await this.deps.drain(drainOptions);\n const made_progress =\n subscribed > 0 ||\n lastDrain.acked.length > 0 ||\n lastDrain.blocked.length > 0;\n if (!made_progress) break;\n }\n if (lastDrain) this.deps.onSettled(lastDrain);\n })()\n .catch((err) => this.deps.logger.error(err))\n .finally(() => {\n this._running = false;\n });\n }, debounceMs);\n }\n\n /** Cancel any pending or active settle cycle. Idempotent. */\n stop(): void {\n if (this._timer) {\n clearTimeout(this._timer);\n this._timer = undefined;\n }\n }\n}\n","/**\n * @module drain\n * @category Internal\n *\n * Pipeline operations consumed by the drain/correlate loop. Each op is a\n * single async step the orchestrator invokes per drain cycle:\n *\n * - `claim` — atomically discover and lock streams for processing\n * - `fetch` — read events for each leased stream\n * - `ack` — release leases for successfully handled streams\n * - `block` — flag leases that exceeded the retry budget\n * - `subscribe` — register newly correlated streams with the store\n *\n * This module exposes only the bare implementations as plain async functions,\n * mirroring the shape of {@link \"event-sourcing\"}. Trace decoration is\n * layered on top in {@link \"tracing\"} and selected by the orchestrator at\n * construction time. No tracing imports here.\n *\n * @internal\n */\n\nimport { store } from \"../ports.js\";\nimport type {\n BlockedLease,\n Committed,\n Fetch,\n Lease,\n Schemas,\n} from \"../types/index.js\";\n\n/** @internal */\nexport interface DrainOps<TEvents extends Schemas> {\n claim: typeof claim;\n fetch: typeof fetch<TEvents>;\n ack: typeof ack;\n block: typeof block;\n subscribe: typeof subscribe;\n}\n\nexport const claim = (\n lagging: number,\n leading: number,\n by: string,\n millis: number\n): Promise<Lease[]> => store().claim(lagging, leading, by, millis);\n\nexport async function fetch<TEvents extends Schemas>(\n leased: Lease[],\n eventLimit: number\n): Promise<Fetch<TEvents>> {\n return Promise.all(\n leased.map(async ({ stream, source, at, lagging }) => {\n const events: Committed<TEvents, keyof TEvents>[] = [];\n await store().query<TEvents>((e) => events.push(e), {\n stream: source,\n after: at,\n limit: eventLimit,\n });\n return { stream, source, at, lagging, events } as const;\n })\n );\n}\n\nexport const ack = (leases: Lease[]): Promise<Lease[]> => store().ack(leases);\n\nexport const block = (leases: BlockedLease[]): Promise<BlockedLease[]> =>\n store().block(leases);\n\nexport const subscribe = (\n streams: Array<{ stream: string; source?: string }>\n): Promise<{ subscribed: number; watermark: number }> =>\n store().subscribe(streams);\n","/**\n * @module event-sourcing\n * @category Internal\n *\n * Pure event-sourcing primitives: `snap` persists state checkpoints, `load`\n * reconstructs state by replaying events through reducers, and `action`\n * validates an action, runs invariants, emits events, and commits them\n * atomically. `tombstone` commits the close-the-books guard with optimistic\n * concurrency.\n *\n * These are the bare implementations — observability is layered on top in\n * {@link \"tracing\"} and wired by the orchestrator at construction time.\n * No tracing imports here, no module-level mutable state.\n *\n * @internal\n */\n\nimport { randomUUID } from \"node:crypto\";\nimport { patch } from \"@rotorsoft/act-patch\";\nimport { cache, log, SNAP_EVENT, store, TOMBSTONE_EVENT } from \"../ports.js\";\nimport {\n ConcurrencyError,\n InvariantError,\n StreamClosedError,\n} from \"../types/errors.js\";\nimport type {\n AsOf,\n Committed,\n Emitted,\n EventMeta,\n Schema,\n Schemas,\n Snapshot,\n State,\n Target,\n} from \"../types/index.js\";\nimport { validate } from \"../utils.js\";\n\n/** @internal */\nexport interface EsOps {\n snap: typeof snap;\n load: typeof load;\n action: typeof action;\n tombstone: typeof tombstone;\n}\n\n/**\n * Event sourcing utilities for snapshotting, loading, and committing actions/events.\n * Used internally by Act and state machines.\n */\n\n/**\n * Saves a snapshot of the state to the store.\n *\n * Snapshots are used to optimize state reconstruction for aggregates with long event streams.\n *\n * @template TState The type of state\n * @template TEvents The type of events\n * @param snapshot The snapshot to save\n * @returns Promise that resolves when the snapshot is saved\n *\n * @example\n * await snap(snapshot);\n */\nexport async function snap<TState extends Schema, TEvents extends Schemas>(\n snapshot: Snapshot<TState, TEvents>\n): Promise<void> {\n try {\n const { id, stream, name, meta, version } = snapshot.event!;\n await store().commit(\n stream,\n [{ name: SNAP_EVENT, data: snapshot.state }],\n {\n correlation: meta.correlation,\n causation: { event: { id, name: name as string, stream } },\n },\n version // IMPORTANT! - state events are committed right after the snapshot event\n );\n } catch (error) {\n log().error(error);\n }\n}\n\n/**\n * Commits a tombstone event with optimistic concurrency, returning the\n * committed record on success or `undefined` if the stream moved past\n * `expectedVersion` (concurrent write detected). Other store errors\n * propagate.\n *\n * Used by `close()` to guard a stream while archive/truncate runs:\n * subsequent `action()` calls see the tombstone at head and reject with\n * {@link StreamClosedError} until the close completes.\n *\n * @internal\n */\nexport async function tombstone(\n stream: string,\n expectedVersion: number,\n correlation: string\n): Promise<Committed<Schemas, keyof Schemas> | undefined> {\n try {\n const [committed] = await store().commit(\n stream,\n [{ name: TOMBSTONE_EVENT, data: {} }],\n { correlation, causation: {} },\n expectedVersion\n );\n return committed;\n } catch (error) {\n if (error instanceof ConcurrencyError) return undefined;\n throw error;\n }\n}\n\n/**\n * Loads a snapshot of the state from the store by replaying events and applying patches.\n *\n * First checks the cache for a checkpoint, then queries the store for events\n * committed after the cached position. On cache miss, replays from the store\n * (using snapshots if available to avoid full replay).\n *\n * @template TState The type of state\n * @template TEvents The type of events\n * @template TActions The type of actions\n * @param me The state machine definition\n * @param stream The stream (instance) to load\n * @param callback (Optional) Callback to receive the loaded snapshot as it is built\n * @returns The snapshot of the loaded state\n *\n * @example\n * const snapshot = await load(Counter, \"counter1\");\n */\nexport async function load<\n TState extends Schema,\n TEvents extends Schemas,\n TActions extends Schemas,\n>(\n me: State<TState, TEvents, TActions>,\n stream: string,\n callback?: (snapshot: Snapshot<TState, TEvents>) => void,\n asOf?: AsOf\n): Promise<Snapshot<TState, TEvents>> {\n const timeTravel = !!asOf && Object.values(asOf).some((v) => v !== undefined);\n const cached = timeTravel ? undefined : await cache().get<TState>(stream);\n const cache_hit = !!cached;\n let state = cached?.state ?? (me.init ? me.init() : ({} as TState));\n let patches = cached?.patches ?? 0;\n let snaps = cached?.snaps ?? 0;\n // version always reflects stream head: starts from the cached version\n // (or -1 for a fresh stream / cache miss), advances per event seen.\n let version = cached?.version ?? -1;\n // replayed counts events processed by THIS load only (snap or patch);\n // distinct from `patches` (the snap-distance accumulator carried over\n // from the cache).\n let replayed = 0;\n let event: Committed<TEvents, string> | undefined;\n\n await store().query(\n (e) => {\n event = e as Committed<TEvents, string>;\n version = e.version;\n if (e.name === SNAP_EVENT) {\n state = e.data as TState;\n snaps++;\n patches = 0;\n replayed++;\n } else if (me.patch[e.name]) {\n state = patch(state, me.patch[e.name](event, state));\n patches++;\n replayed++;\n } else if (e.name !== TOMBSTONE_EVENT) {\n // Unknown event — not in this state's reducer map. Causes:\n // deleted/renamed event in a versioned schema, load() called with\n // the wrong state, or stream contamination. Skipping silently\n // would corrupt replay; warn so the operator can investigate.\n log().warn(\n `Skipping unknown event \"${String(e.name)}\" on stream \"${stream}\" (id=${e.id}) — no reducer in state \"${me.name}\"`\n );\n }\n callback?.({\n event,\n state,\n version,\n patches,\n snaps,\n cache_hit,\n replayed,\n });\n },\n {\n stream,\n stream_exact: true,\n ...(cached ? { after: cached.event_id } : { with_snaps: true, ...asOf }),\n }\n );\n\n // Populate the cache when this load actually processed events. Without\n // this, read-heavy paths (UI loops calling load() many times between\n // commits) miss the cache forever — only action() would ever warm it.\n // No race-protection re-check needed: the cache is a state checkpoint\n // at (version, event_id), and any subsequent load queries past\n // event_id, picks up missed events, and replays — so an \"older\" cache\n // write from a concurrent slower load is self-correcting on next access.\n // Time-travel loads bypass cache entirely and skip this too.\n if (replayed > 0 && !timeTravel && event) {\n await cache().set(stream, {\n state,\n version,\n event_id: event.id,\n patches,\n snaps,\n });\n }\n\n return { event, state, version, patches, snaps, cache_hit, replayed };\n}\n\n/**\n * Executes an action and emits an event to be committed by the store.\n *\n * This function validates the action, applies business invariants, emits events, and commits them to the event store.\n *\n * @template TState The type of state\n * @template TEvents The type of events\n * @template TActions The type of actionSchemas\n * @template TKey The type of action to execute\n * @param me The state machine definition\n * @param action The action to execute\n * @param target The target (stream, actor, etc.)\n * @param payload The payload of the action\n * @param reactingTo (Optional) The event that the action is reacting to\n * @param skipValidation (Optional) Whether to skip validation (not recommended)\n * @returns The snapshot of the committed event\n *\n * @example\n * const snapshot = await action(Counter, \"increment\", { stream: \"counter1\", actor }, { by: 1 });\n */\nexport async function action<\n TState extends Schema,\n TEvents extends Schemas,\n TActions extends Schemas,\n TKey extends keyof TActions,\n>(\n me: State<TState, TEvents, TActions>,\n action: TKey,\n target: Target,\n payload: Readonly<TActions[TKey]>,\n reactingTo?: Committed<Schemas, keyof Schemas>,\n skipValidation = false\n): Promise<Snapshot<TState, TEvents>[]> {\n const { stream, expectedVersion, actor } = target;\n if (!stream) throw new Error(\"Missing target stream\");\n\n const validated = skipValidation\n ? payload\n : validate(action as string, payload, me.actions[action]);\n\n const snapshot = await load(me, stream);\n if (snapshot.event?.name === TOMBSTONE_EVENT)\n throw new StreamClosedError(stream);\n const expected = expectedVersion ?? snapshot.event?.version;\n\n if (me.given) {\n const invariants = me.given[action] || [];\n invariants.forEach(({ valid, description }) => {\n if (!valid(snapshot.state, actor))\n throw new InvariantError(\n action,\n validated,\n target,\n snapshot,\n description\n );\n });\n }\n\n const result = me.on[action](validated, snapshot, target);\n if (!result) return [snapshot];\n\n // An empty array means no events were emitted\n if (Array.isArray(result) && result.length === 0) {\n return [snapshot];\n }\n\n const tuples = Array.isArray(result[0])\n ? (result as Emitted<TEvents>[]) // array of tuples\n : ([result] as Emitted<TEvents>[]); // single tuple\n\n const emitted = tuples.map(([name, data]) => ({\n name,\n data: skipValidation\n ? data\n : validate(name as string, data, me.events[name]),\n }));\n\n const meta: EventMeta = {\n correlation: reactingTo?.meta.correlation || randomUUID(),\n causation: {\n action: {\n name: action as string,\n ...target,\n // payload intentionally omitted: it can be large or contain PII,\n // and callers correlate via the correlation id when they need it.\n },\n event: reactingTo\n ? {\n id: reactingTo.id,\n name: reactingTo.name,\n stream: reactingTo.stream,\n }\n : undefined,\n },\n };\n\n let committed: Committed<TEvents, keyof TEvents>[];\n try {\n committed = await store().commit(\n stream,\n emitted,\n meta,\n // Reactions skip optimistic concurrency: they always append against the\n // current head. Stream leasing already serializes concurrent reactions,\n // and forcing version checks here would turn ordinary catch-up into\n // spurious retries.\n reactingTo ? undefined : expected\n );\n } catch (error) {\n // Invalidate cache on concurrency errors — cached state is stale\n if (error instanceof ConcurrencyError) {\n await cache().invalidate(stream);\n }\n throw error;\n }\n\n let { state, patches } = snapshot;\n const snapshots = committed.map((event) => {\n const p = me.patch[event.name](event, state);\n state = patch(state, p);\n patches++;\n // cache_hit / replayed propagate from the initial load — these\n // post-commit snapshots all derive from the same loaded state.\n // version advances per committed event (each is a new stream head).\n return {\n event,\n state,\n version: event.version,\n patches,\n snaps: snapshot.snaps,\n patch: p,\n cache_hit: snapshot.cache_hit,\n replayed: snapshot.replayed,\n };\n });\n\n // fire and forget snaps\n const last = snapshots.at(-1)!;\n const snapped = me.snap?.(last);\n\n // Update cache with post-commit state (reset patches if snapped).\n // Fire-and-forget — log but don't fail the action on cache write errors\n // (e.g., transient network failures in a custom Cache adapter).\n cache()\n .set<TState>(stream, {\n state: last.state,\n version: last.event.version,\n event_id: last.event.id,\n patches: snapped ? 0 : last.patches,\n snaps: snapped ? last.snaps + 1 : last.snaps,\n })\n .catch((err) => log().error(err));\n\n // Persist snap to store for cold-start durability. Fire-and-forget:\n // snap() has its own try/catch that logs failures, so the rejection\n // can never escape — `void` is just to silence the floating-promise\n // lint (action() doesn't await store durability for the snapshot).\n if (snapped) void snap(last);\n\n return snapshots;\n}\n","/**\n * @module tracing\n * @category Internal\n *\n * Centralized observability for the framework's internal pipelines.\n *\n * Trace decorators wrap a bare implementation with `logger.trace(...)` calls\n * at well-defined moments — entry points for {@link \"event-sourcing\"} (`load`,\n * `snap`, `action`) and exit points for the {@link \"drain\"} pipeline (`claim`,\n * `fetch`, `ack`, `block`, `subscribe`). `action` carries both an entry log\n * and a post-commit log to preserve the diagnostic value of the historical\n * mid-function trace points.\n *\n * Output styles:\n * - **Pretty mode** (`config().env !== \"production\"`) — event-sourcing logs\n * show only the colored target body (color carries the operation/phase),\n * drain logs keep a colored caption.\n * - **Plain mode** (production / log aggregators) — every log gets a textual\n * prefix; event-sourcing uses `caption: body`, drain uses `caption body`.\n *\n * The two factories — {@link buildEs} and {@link buildDrain} — let the\n * orchestrator choose bare or traced variants once at `.build()` time based\n * on the configured log level. Outside this module, no other source file\n * imports tracing primitives.\n *\n * @internal\n */\n\nimport { config } from \"../config.js\";\nimport type { AsOf, Logger, Schemas } from \"../types/index.js\";\nimport type { DrainOps } from \"./drain.js\";\nimport * as drain from \"./drain.js\";\nimport type { EsOps } from \"./event-sourcing.js\";\nimport * as es from \"./event-sourcing.js\";\n\ntype AsyncFn = (...args: any[]) => Promise<any>;\n\nconst PRETTY = config().env !== \"production\";\n\n// 256-color codes for distinctive, theme-friendly hues\nconst C_BLUE = \"\\x1b[38;5;39m\"; // vivid sky blue (action)\nconst C_ORANGE = \"\\x1b[38;5;208m\"; // true orange (committed)\nconst C_GREEN = \"\\x1b[38;5;42m\"; // emerald (load)\nconst C_MAGENTA = \"\\x1b[38;5;165m\"; // bright magenta (snap)\nconst C_DRAIN = \"\\x1b[38;5;244m\"; // muted gray for all drain ops\n// load-trace cache marker shades — distinguishable from C_GREEN body color\nconst C_HIT = \"\\x1b[38;5;82m\"; // lime — fast path, blends visually\nconst C_MISS = \"\\x1b[38;5;220m\"; // amber — non-trivial work happened\nconst C_RESET = \"\\x1b[0m\";\n\n/**\n * Format an event-sourcing trace line. Pretty mode renders just the colored\n * body (the color is the cue for which op/phase fired); plain mode prepends\n * `caption: ` so log aggregators stay readable without ANSI.\n */\nconst es_caption = (caption: string, color: string, body: string): string =>\n PRETTY ? `${color}${body}${C_RESET}` : `${caption}: ${body}`;\n\n/**\n * Format a drain-pipeline caption. Drain logs keep a `>>` marker for easy\n * spotting in mixed log streams, plus a `caption` (past tense — every drain\n * trace fires on exit). All drain ops share one color (gray) so the pipeline\n * reads as a single channel; the caption disambiguates the phase.\n */\nconst drain_caption = (caption: string): string => {\n const tag = `>> ${caption}`;\n return PRETTY ? `${C_DRAIN}${tag}${C_RESET}` : tag;\n};\n\n/**\n * Format the cache hit/miss marker for the load trace. In pretty mode the\n * word is colored (lime for hit, amber for miss) and the surrounding\n * `C_GREEN` body color is restored after — embedded ANSI inside `es_caption`'s\n * outer wrap. Plain mode returns the bare word.\n */\nconst cache_marker = (hit: boolean): string => {\n const word = hit ? \"hit\" : \"miss\";\n if (!PRETTY) return word;\n return `${hit ? C_HIT : C_MISS}${word}${C_RESET}${C_GREEN}`;\n};\n\n/**\n * Format the load stats (`v=N replayed=N snaps=N patches=N`) for the load\n * trace. Muted gray in pretty mode so the cache marker reads as the most\n * important cue; plain mode returns the bare text.\n *\n * - `v` — stream head version (the version of the last event applied)\n * - `replayed` — events processed by THIS load past the cache point\n * - `snaps` — cumulative snapshots taken on this stream\n * - `patches` — events since the last snap (snap-policy accumulator)\n */\nconst stats_marker = (\n version: number,\n replayed: number,\n snaps: number,\n patches: number\n): string => {\n const text = `v=${version} replayed=${replayed} snaps=${snaps} patches=${patches}`;\n if (!PRETTY) return text;\n return `${C_DRAIN}${text}${C_RESET}${C_GREEN}`;\n};\n\n/**\n * Format the as-of marker for time-travel loads. Surfaces the active filter\n * fields (before id, created_before/after timestamps, limit) so an operator\n * can tell at a glance which slice was loaded. Empty `asOf` returns \"\" —\n * non-time-travel loads skip the marker entirely.\n */\nconst as_of_marker = (asOf: AsOf | undefined): string => {\n if (!asOf) return \"\";\n const parts: string[] = [];\n if (asOf.before !== undefined) parts.push(`before=${asOf.before}`);\n if (asOf.created_before !== undefined)\n parts.push(`created_before=${asOf.created_before.toISOString()}`);\n if (asOf.created_after !== undefined)\n parts.push(`created_after=${asOf.created_after.toISOString()}`);\n if (asOf.limit !== undefined) parts.push(`limit=${asOf.limit}`);\n return parts.length ? ` (as-of ${parts.join(\" \")})` : \" (as-of)\";\n};\n\n/**\n * Wraps an async function with optional `exit` and `entry` callbacks. Each\n * callback fires at the corresponding phase; both receive the call args, and\n * `exit` additionally receives the resolved result. Used to layer\n * `logger.trace` calls onto bare ops without changing their signatures.\n *\n * @internal\n */\nconst traced = <F extends AsyncFn>(\n inner: F,\n exit?: (result: Awaited<ReturnType<F>>, ...args: Parameters<F>) => void,\n entry?: (...args: Parameters<F>) => void\n): F =>\n (async (...args: Parameters<F>) => {\n entry?.(...args);\n const result = (await inner(...args)) as Awaited<ReturnType<F>>;\n exit?.(result, ...args);\n return result;\n }) as F;\n\n/**\n * Selects bare or traced event-sourcing handlers. Called once by the\n * orchestrator constructor.\n *\n * @internal\n */\nexport function buildEs(logger: Logger): EsOps {\n if (logger.level !== \"trace\") {\n return {\n snap: es.snap,\n load: es.load,\n action: es.action,\n tombstone: es.tombstone,\n };\n }\n return {\n snap: traced(es.snap, undefined, (snapshot) => {\n logger.trace(\n es_caption(\n \"snap\",\n C_MAGENTA,\n `${snapshot.event!.stream}@${snapshot.event!.version}`\n )\n );\n }),\n load: traced(es.load, (result, _me, stream, _cb, asOf) => {\n const stats = stats_marker(\n result.version,\n result.replayed,\n result.snaps,\n result.patches\n );\n logger.trace(\n es_caption(\n \"load\",\n C_GREEN,\n `${stream}${as_of_marker(asOf)} ${cache_marker(result.cache_hit)} ${stats}`\n )\n );\n }),\n action: traced(\n es.action,\n (snapshots, _me, _action, target) => {\n const committed = snapshots.filter((s) => s.event);\n if (committed.length) {\n logger.trace(\n committed.map((s) => s.event!.data),\n es_caption(\n \"committed\",\n C_ORANGE,\n `${target.stream}.${committed.map((s) => s.event!.name).join(\", \")}`\n )\n );\n }\n },\n (_me, action, target, payload) => {\n logger.trace(\n payload as object,\n es_caption(\"action\", C_BLUE, `${target.stream}.${action}`)\n );\n }\n ),\n tombstone: traced(es.tombstone, (committed, stream) => {\n if (committed)\n logger.trace(\n es_caption(\"tombstoned\", C_ORANGE, `${stream}@${committed.version}`)\n );\n }),\n };\n}\n\n/**\n * Selects bare or traced drain-pipeline ops. Called once by the orchestrator\n * constructor.\n *\n * @internal\n */\nexport function buildDrain<TEvents extends Schemas>(\n logger: Logger\n): DrainOps<TEvents> {\n if (logger.level !== \"trace\") {\n return {\n claim: drain.claim,\n fetch: drain.fetch,\n ack: drain.ack,\n block: drain.block,\n subscribe: drain.subscribe,\n };\n }\n return {\n claim: traced(drain.claim, (leased) => {\n if (leased.length) {\n const data = Object.fromEntries(\n leased.map(({ stream, at, retry }) => [stream, { at, retry }])\n );\n logger.trace(data, drain_caption(\"claimed\"));\n }\n }),\n fetch: traced(drain.fetch<TEvents>, (fetched) => {\n const data = Object.fromEntries(\n fetched.map(({ stream, source, events }) => {\n const key = source ? `${stream}<-${source}` : stream;\n const value = Object.fromEntries(\n events.map(({ id, stream, name }) => [id, { [stream]: name }])\n );\n return [key, value];\n })\n );\n logger.trace(data, drain_caption(\"fetched\"));\n }),\n ack: traced(drain.ack, (acked) => {\n if (acked.length) {\n const data = Object.fromEntries(\n acked.map(({ stream, at, retry }) => [stream, { at, retry }])\n );\n logger.trace(data, drain_caption(\"acked\"));\n }\n }),\n block: traced(drain.block, (blocked) => {\n if (blocked.length) {\n const data = Object.fromEntries(\n blocked.map(({ stream, at, retry, error }) => [\n stream,\n { at, retry, error },\n ])\n );\n logger.trace(data, drain_caption(\"blocked\"));\n }\n }),\n subscribe: traced(drain.subscribe, (result, streams) => {\n if (result.subscribed) {\n const data = streams.map(({ stream }) => stream).join(\" \");\n logger.trace(`${drain_caption(\"correlated\")} ${data}`);\n }\n }),\n };\n}\n","/**\n * @module act-builder\n * @category Builders\n *\n * Fluent builder for composing event-sourced applications.\n */\nimport { Act, type ActOptions } from \"../act.js\";\nimport {\n _this_,\n mergeEventRegister,\n mergeProjection,\n registerState,\n} from \"../internal/index.js\";\nimport type {\n Actor,\n BatchHandler,\n Committed,\n EventRegister,\n IAct,\n Reaction,\n ReactionOptions,\n ReactionResolver,\n Registry,\n Schema,\n SchemaRegister,\n Schemas,\n Snapshot,\n State,\n} from \"../types/index.js\";\nimport type { Projection } from \"./projection-builder.js\";\nimport type { Slice } from \"./slice-builder.js\";\n\n/**\n * Registers a projection's batch handler against its target stream, throwing\n * if a different handler is already registered for the same target. Two\n * projections silently overwriting each other's batch handlers used to be a\n * latent footgun.\n */\nfunction registerBatchHandler(\n proj: Projection<any>,\n batchHandlers: Map<string, BatchHandler<any>>\n): void {\n if (!proj.batchHandler || !proj.target) return;\n const existing = batchHandlers.get(proj.target);\n if (existing && existing !== proj.batchHandler) {\n throw new Error(`Duplicate batch handler for target \"${proj.target}\"`);\n }\n batchHandlers.set(proj.target, proj.batchHandler);\n}\n\n/**\n * Fluent builder interface for composing event-sourced applications.\n *\n * Provides a chainable API for:\n * - Registering states via `.withState()`\n * - Registering slices via `.withSlice()`\n * - Registering projections via `.withProjection()`\n * - Locking a custom actor type via `.withActor<TActor>()`\n * - Defining event reactions via `.on()` → `.do()` → `.to()`\n * - Building the orchestrator via `.build()`\n *\n * @template TSchemaReg - Schema register for states (maps action names to state schemas)\n * @template TEvents - Event schemas (maps event names to event data schemas)\n * @template TActions - Action schemas (maps action names to action payload schemas)\n * @template TStateMap - Map of state names to state schemas\n * @template TActor - Actor type extending base Actor\n *\n * @see {@link act} for usage examples\n * @see {@link Act} for the built orchestrator API\n */\nexport type ActBuilder<\n TSchemaReg extends SchemaRegister<TActions>,\n TEvents extends Schemas,\n TActions extends Schemas,\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n TStateMap extends Record<string, Schema> = {},\n TActor extends Actor = Actor,\n> = {\n /**\n * Registers a state definition with the builder.\n *\n * State names, action names, and event names must be unique across the\n * application (partial states with the same name are merged automatically).\n *\n * @throws {Error} If duplicate action or event names are detected\n */\n withState: <\n TNewState extends Schema,\n TNewEvents extends Schemas,\n TNewActions extends Schemas,\n TNewName extends string = string,\n >(\n state: State<TNewState, TNewEvents, TNewActions, TNewName>\n ) => ActBuilder<\n TSchemaReg & { [K in keyof TNewActions]: TNewState },\n TEvents & TNewEvents,\n TActions & TNewActions,\n TStateMap & { [K in TNewName]: TNewState },\n TActor\n >;\n /**\n * Registers a slice with the builder.\n *\n * Merges all the slice's states and reactions into the application.\n * State names, action names, and event names must be unique across the\n * application (partial states with the same name are merged automatically).\n *\n * @throws {Error} If duplicate action or event names are detected\n */\n withSlice: <\n TNewSchemaReg extends SchemaRegister<TNewActions>,\n TNewEvents extends Schemas,\n TNewActions extends Schemas,\n TNewMap extends Record<string, Schema>,\n >(\n slice: Slice<TNewSchemaReg, TNewEvents, TNewActions, TNewMap>\n ) => ActBuilder<\n TSchemaReg & TNewSchemaReg,\n TEvents & TNewEvents,\n TActions & TNewActions,\n TStateMap & TNewMap,\n TActor\n >;\n /**\n * Registers a standalone projection with the builder.\n *\n * The projection's events must be a subset of events already registered\n * via `.withState()` or `.withSlice()`.\n */\n withProjection: <TNewEvents extends Schemas>(\n projection: [Exclude<keyof TNewEvents, keyof TEvents>] extends [never]\n ? Projection<TNewEvents>\n : never\n ) => ActBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor>;\n /**\n * Locks a custom actor type for this application.\n *\n * This is a pure type-level method — it returns the same builder at\n * runtime but narrows the `TActor` generic so that `app.do()` and\n * reaction dispatchers require the richer actor shape.\n *\n * @template TNewActor - Custom actor type extending base Actor\n * @returns The same builder with `TActor` locked to `TNewActor`\n *\n * @example\n * ```typescript\n * type MyActor = { id: string; name: string; role: string; tenantId: string };\n *\n * const app = act()\n * .withActor<MyActor>()\n * .withState(Counter)\n * .build();\n *\n * // Now app.do() requires MyActor in the target\n * await app.do(\"increment\", {\n * stream: \"counter-1\",\n * actor: { id: \"1\", name: \"Alice\", role: \"admin\", tenantId: \"t1\" }\n * }, { by: 5 });\n * ```\n */\n withActor: <TNewActor extends Actor>() => ActBuilder<\n TSchemaReg,\n TEvents,\n TActions,\n TStateMap,\n TNewActor\n >;\n /**\n * Begins defining a reaction to a specific event.\n *\n * Reactions are event handlers that respond to state changes. They can trigger\n * additional actions, update external systems, or perform side effects. Reactions\n * are processed asynchronously during drain cycles.\n *\n * @template TKey - Event name (must be a registered event)\n * @param event - The event name to react to\n * @returns An object with `.do()` method to define the reaction handler\n */\n on: <TKey extends keyof TEvents>(\n event: TKey\n ) => {\n do: (\n handler: (\n event: Committed<TEvents, TKey>,\n stream: string,\n app: IAct<TEvents, TActions, TActor>\n ) => Promise<Snapshot<Schema, TEvents> | void>,\n options?: Partial<ReactionOptions>\n ) => ActBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor> & {\n to: (\n resolver: ReactionResolver<TEvents, TKey> | string\n ) => ActBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor>;\n };\n };\n /**\n * Builds and returns the Act orchestrator instance.\n *\n * @param options - Optional runtime overrides (see {@link ActOptions}).\n * @returns The Act orchestrator instance\n *\n * @see {@link Act} for available orchestrator methods\n */\n build: (\n options?: ActOptions\n ) => Act<TSchemaReg, TEvents, TActions, TStateMap, TActor>;\n /**\n * The registered event schemas and their reaction maps.\n */\n readonly events: EventRegister<TEvents>;\n};\n\n/* eslint-disable @typescript-eslint/no-empty-object-type -- {} used as generic defaults */\n\n/**\n * Creates a new Act orchestrator builder for composing event-sourced applications.\n *\n * @example Basic application with single state\n * ```typescript\n * const app = act()\n * .withState(Counter)\n * .build();\n * ```\n *\n * @example Application with custom actor type\n * ```typescript\n * type MyActor = { id: string; name: string; role: string };\n *\n * const app = act()\n * .withActor<MyActor>()\n * .withState(Counter)\n * .build();\n * ```\n *\n * @example Application with slices (vertical slice architecture)\n * ```typescript\n * const CounterSlice = slice()\n * .withState(Counter)\n * .on(\"Incremented\")\n * .do(async (event) => { console.log(\"incremented!\"); })\n * .to(\"counter-target\")\n * .build();\n *\n * const app = act()\n * .withSlice(CounterSlice)\n * .build();\n * ```\n *\n *\n * @see {@link ActBuilder} for available builder methods\n * @see {@link Act} for orchestrator API methods\n * @see {@link state} for defining states\n * @see {@link slice} for defining slices\n */\nexport function act<\n // @ts-expect-error empty schema\n TSchemaReg extends SchemaRegister<TActions> = {},\n TEvents extends Schemas = {},\n TActions extends Schemas = {},\n TStateMap extends Record<string, Schema> = {},\n TActor extends Actor = Actor,\n>(): ActBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor> {\n // Mutable runtime state — one set of references shared across the entire\n // fluent chain. Each `with*` / `on` call mutates these and returns the\n // same builder cast to the widened generic; type fanout is preserved\n // through the public type signatures, runtime allocation is not.\n const states = new Map<string, State<any, any, any>>();\n const registry: Registry<TSchemaReg, TEvents, TActions> = {\n actions: {} as Registry<TSchemaReg, TEvents, TActions>[\"actions\"],\n events: {} as Registry<TSchemaReg, TEvents, TActions>[\"events\"],\n };\n const pendingProjections: Projection<any>[] = [];\n const batchHandlers = new Map<string, BatchHandler<any>>();\n\n // The `as` chain on `self` is the type fanout: each fluent method\n // mutates state and returns `self` cast to its post-call generic\n // signature. Internal-only — public types stay narrow.\n const builder: ActBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor> =\n {\n withState: (state) => {\n registerState(state, states, registry.actions, registry.events);\n return builder as never;\n },\n withSlice: (input) => {\n for (const s of input.states.values()) {\n registerState(s, states, registry.actions, registry.events);\n }\n mergeEventRegister(registry.events, input.events);\n pendingProjections.push(...input.projections);\n return builder as never;\n },\n withProjection: (proj) => {\n mergeProjection(proj as Projection<any>, registry.events);\n registerBatchHandler(proj as Projection<any>, batchHandlers);\n return builder;\n },\n withActor: <TNewActor extends Actor>() =>\n builder as unknown as ActBuilder<\n TSchemaReg,\n TEvents,\n TActions,\n TStateMap,\n TNewActor\n >,\n on: <TKey extends keyof TEvents>(event: TKey) => ({\n do: (\n handler: (\n event: Committed<TEvents, TKey>,\n stream: string,\n app: IAct<TEvents, TActions, TActor>\n ) => Promise<Snapshot<Schema, TEvents> | void>,\n options?: Partial<ReactionOptions>\n ) => {\n const reaction: Reaction<TEvents, TKey, TActions, TActor> = {\n handler: handler,\n resolver: _this_,\n options: {\n blockOnError: options?.blockOnError ?? true,\n maxRetries: options?.maxRetries ?? 3,\n },\n };\n if (!handler.name)\n throw new Error(\n `Reaction handler for \"${String(event)}\" must be a named function`\n );\n // Register once with the default _this_ resolver. If `.to()` is\n // chained next, it patches the same reaction's resolver in place\n // — no second Map.set() round-trip.\n registry.events[event].reactions.set(handler.name, reaction);\n return Object.assign(builder, {\n to(resolver: ReactionResolver<TEvents, TKey> | string) {\n reaction.resolver =\n typeof resolver === \"string\" ? { target: resolver } : resolver;\n return builder;\n },\n });\n },\n }),\n build: (options?: ActOptions) => {\n for (const proj of pendingProjections) {\n mergeProjection(proj, registry.events as Record<string, any>);\n registerBatchHandler(proj, batchHandlers);\n }\n return new Act<TSchemaReg, TEvents, TActions, TStateMap, TActor>(\n registry,\n states,\n batchHandlers,\n options\n );\n },\n events: registry.events,\n };\n return builder;\n}\n","/**\n * @module projection-builder\n * @category Builders\n *\n * Fluent builder for composing projection handlers — read-model updaters\n * that react to events and update external state (databases, caches, etc.).\n *\n * Projections differ from slices: they don't contain states, don't dispatch\n * actions, and are pure side-effect handlers routed to a named stream.\n */\nimport type { ZodType } from \"zod\";\nimport { _this_ } from \"../internal/index.js\";\nimport type {\n BatchHandler,\n Committed,\n EventRegister,\n Reaction,\n ReactionResolver,\n Schema,\n Schemas,\n} from \"../types/index.js\";\n\n/**\n * A self-contained projection grouping read-model update handlers.\n * Projections are composed into an Act orchestrator via `act().withProjection(projection)`.\n *\n * @template TEvents - Event schemas handled by this projection\n */\nexport type Projection<TEvents extends Schemas> = {\n readonly _tag: \"Projection\";\n readonly events: EventRegister<TEvents>;\n readonly target?: string;\n readonly batchHandler?: BatchHandler<TEvents>;\n};\n\n/** Helper: a single-key record mapping an event name to its Zod schema. */\ntype EventEntry<TKey extends string = string, TData extends Schema = Schema> = {\n [P in TKey]: ZodType<TData>;\n};\n\n/** Infer the handler-result type after registering one event. */\ntype DoResult<\n TEvents extends Schemas,\n TKey extends string,\n TData extends Schema,\n TTarget extends string | undefined = undefined,\n> = ProjectionBuilder<TEvents & { [P in TKey]: TData }, TTarget> & {\n to: (\n resolver: ReactionResolver<TEvents & { [P in TKey]: TData }, TKey> | string\n ) => ProjectionBuilder<TEvents & { [P in TKey]: TData }, TTarget>;\n};\n\n/**\n * Fluent builder interface for composing projections.\n *\n * When a static target is provided via `projection(\"target\")`, the builder\n * exposes a `.batch()` method for registering a batch handler that processes\n * all events in a single call.\n *\n * @template TEvents - Event schemas\n * @template TTarget - Static target string or undefined\n */\nexport type ProjectionBuilder<\n TEvents extends Schemas,\n TTarget extends string | undefined = undefined,\n> = {\n /**\n * Begins defining a projection handler for a specific event.\n *\n * Pass a `{ EventName: schema }` record — use shorthand `{ EventName }`\n * when the variable name matches the event name. The key becomes the\n * event name, the value the Zod schema.\n */\n on: <TKey extends string, TData extends Schema>(\n entry: EventEntry<TKey, TData>\n ) => {\n do: (\n handler: (\n event: Committed<TEvents & { [P in TKey]: TData }, TKey>,\n stream: string\n ) => Promise<void>\n ) => DoResult<TEvents, TKey, TData, TTarget>;\n };\n /**\n * Builds and returns the Projection data structure.\n */\n build: () => Projection<TEvents>;\n /**\n * The registered event schemas and their reaction maps.\n */\n readonly events: EventRegister<TEvents>;\n} & (TTarget extends string\n ? {\n /**\n * Registers a batch handler that processes all events in a single call.\n *\n * Only available on projections with a static target (`projection(\"target\")`).\n * The handler receives a discriminated union of all declared events,\n * enabling bulk DB operations in a single transaction.\n *\n * When defined, the batch handler is always called — even for a single event.\n * Individual `.do()` handlers serve as fallback for projections without `.batch()`.\n */\n batch: (handler: BatchHandler<TEvents>) => {\n build: () => Projection<TEvents>;\n };\n }\n : // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n {});\n\n/* eslint-disable @typescript-eslint/no-empty-object-type -- {} used as generic defaults */\n\n/**\n * Creates a new projection builder for composing read-model update handlers.\n *\n * Projections enable separation of read-model concerns from command handling.\n * Each `.on({ Event }).do(handler)` call registers a handler that updates\n * a projection (database table, cache, etc.) in response to events.\n *\n * Pass a target stream name to `projection(\"target\")` so every handler\n * inherits that resolver automatically. Omit it and use per-handler\n * `.to()` when handlers route to different streams.\n *\n * @param target - Optional default target stream for all handlers\n *\n * @example Default target (all handlers routed to \"tickets\")\n * ```typescript\n * const TicketProjection = projection(\"tickets\")\n * .on({ TicketOpened })\n * .do(async ({ stream, data }) => {\n * await db.insert(tickets).values({ id: stream, ...data });\n * })\n * .on({ TicketClosed })\n * .do(async ({ stream, data }) => {\n * await db.update(tickets).set(data).where(eq(tickets.id, stream));\n * })\n * .build();\n * ```\n *\n * @example Per-handler routing\n * ```typescript\n * const MultiProjection = projection()\n * .on({ OrderPlaced })\n * .do(async (event) => { ... })\n * .to(\"orders\")\n * .on({ PaymentReceived })\n * .do(async (event) => { ... })\n * .to(\"payments\")\n * .build();\n * ```\n *\n * @see {@link ProjectionBuilder} for builder methods\n * @see {@link Projection} for the output type\n */\n/**\n * @internal Build the core builder object (shared between overloads). One\n * mutable `events` register threaded through every fluent call; .on()\n * mutates and returns the same builder cast to its widened generic.\n */\nfunction _projection<\n TEvents extends Schemas,\n TTarget extends string | undefined,\n>(target: TTarget): ProjectionBuilder<TEvents, TTarget> {\n const events = {} as EventRegister<TEvents>;\n const defaultResolver: { target: string } | undefined =\n typeof target === \"string\" ? { target } : undefined;\n\n // Mutable runtime bag — typed loosely; the public projection() return\n // type narrows back to the user-facing `ProjectionBuilder<TEvents, TTarget>`.\n\n const base: any = {\n on: <TKey extends string, TData extends Schema>(\n entry: EventEntry<TKey, TData>\n ) => {\n const keys = Object.keys(entry);\n if (keys.length !== 1) throw new Error(\".on() requires exactly one key\");\n const event = keys[0] as TKey;\n const schema = entry[event];\n\n // Register the event schema if not already present\n if (!(event in events)) {\n (events as Record<string, unknown>)[event] = {\n schema,\n reactions: new Map(),\n };\n }\n\n return {\n do: (\n handler: (\n event: Committed<TEvents & { [P in TKey]: TData }, TKey>,\n stream: string\n ) => Promise<void>\n ) => {\n const reaction: Reaction<TEvents & { [P in TKey]: TData }, TKey> = {\n handler: handler,\n resolver: defaultResolver ?? _this_,\n options: {\n blockOnError: true,\n maxRetries: 3,\n },\n };\n const register = (events as Record<string, any>)[event];\n if (!handler.name)\n throw new Error(\n `Projection handler for \"${event}\" must be a named function`\n );\n register.reactions.set(handler.name, reaction);\n\n // Same builder, widened generic — no recursive call.\n const widened = base as unknown as ProjectionBuilder<\n TEvents & { [P in TKey]: TData },\n TTarget\n >;\n return Object.assign(widened, {\n to(\n resolver:\n | ReactionResolver<TEvents & { [P in TKey]: TData }, TKey>\n | string\n ) {\n // Patch the same reaction in place — no second Map.set().\n reaction.resolver =\n typeof resolver === \"string\" ? { target: resolver } : resolver;\n return widened;\n },\n });\n },\n };\n },\n build: () => ({\n _tag: \"Projection\" as const,\n events,\n ...(target !== undefined && { target }),\n }),\n events,\n };\n\n // Add .batch() only for static-target projections\n if (typeof target === \"string\") {\n return Object.assign(base, {\n batch: (handler: BatchHandler<TEvents>) => ({\n build: () => ({\n _tag: \"Projection\" as const,\n events,\n target,\n batchHandler: handler,\n }),\n }),\n }) as ProjectionBuilder<TEvents, TTarget>;\n }\n\n return base as ProjectionBuilder<TEvents, TTarget>;\n}\n\n/**\n * Creates a new projection builder with a static target stream.\n *\n * All handlers inherit the target resolver automatically. Enables `.batch()`\n * for bulk event processing in a single transaction.\n *\n * @param target - Static target stream for all handlers\n */\nexport function projection<TEvents extends Schemas = {}>(\n target: string\n): ProjectionBuilder<TEvents, string>;\n/**\n * Creates a new projection builder without a default target.\n *\n * Use per-handler `.to()` to route events to different streams.\n */\nexport function projection<TEvents extends Schemas = {}>(\n target?: undefined\n): ProjectionBuilder<TEvents, undefined>;\nexport function projection<TEvents extends Schemas = {}>(\n target?: string\n): ProjectionBuilder<TEvents, string | undefined> {\n return _projection<TEvents, string | undefined>(target);\n}\n","/**\n * @module slice-builder\n * @category Builders\n *\n * Fluent builder for composing partial states with scoped reactions into\n * self-contained functional slices (vertical slice architecture).\n */\nimport { _this_, registerState } from \"../internal/index.js\";\nimport type {\n Actor,\n Committed,\n EventRegister,\n IAct,\n Reaction,\n ReactionOptions,\n ReactionResolver,\n Schema,\n SchemaRegister,\n Schemas,\n Snapshot,\n State,\n} from \"../types/index.js\";\nimport type { Projection } from \"./projection-builder.js\";\n\n/**\n * A self-contained functional slice grouping partial states with their\n * scoped reactions. Slices are composed into an Act orchestrator via\n * `act().withSlice(slice)`.\n *\n * @template TSchemaReg - Schema register for states\n * @template TEvents - Event schemas from this slice's states\n * @template TActions - Action schemas from this slice's states\n * @template TStateMap - Map of state names to state schemas\n * @template TActor - Actor type extending base Actor\n */\nexport type Slice<\n TSchemaReg extends SchemaRegister<TActions>,\n TEvents extends Schemas,\n TActions extends Schemas,\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n TStateMap extends Record<string, Schema> = {},\n TActor extends Actor = Actor,\n> = {\n readonly _tag: \"Slice\";\n readonly states: Map<string, State<any, any, any>>;\n readonly events: EventRegister<TEvents>;\n readonly projections: ReadonlyArray<Projection<any>>;\n /** @internal phantom field for type-level state schema tracking */\n readonly _S?: TSchemaReg;\n /** @internal phantom field for type-level state name tracking */\n readonly _M?: TStateMap;\n /** @internal phantom field for type-level actor tracking */\n readonly _TActor?: TActor;\n};\n\n/**\n * Fluent builder interface for composing functional slices.\n *\n * Provides a chainable API for registering states and projections,\n * and defining reactions scoped to the slice's own events.\n *\n * @template TSchemaReg - Schema register for states\n * @template TEvents - Event schemas\n * @template TActions - Action schemas\n * @template TStateMap - Map of state names to state schemas\n * @template TActor - Actor type extending base Actor\n */\nexport type SliceBuilder<\n TSchemaReg extends SchemaRegister<TActions>,\n TEvents extends Schemas,\n TActions extends Schemas,\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n TStateMap extends Record<string, Schema> = {},\n TActor extends Actor = Actor,\n> = {\n /**\n * Registers a state definition with the slice.\n *\n * Include every state whose actions your reaction handlers need to\n * dispatch. Duplicate registrations (same state in multiple slices)\n * are handled automatically at composition time.\n */\n withState: <\n TNewState extends Schema,\n TNewEvents extends Schemas,\n TNewActions extends Schemas,\n TNewName extends string = string,\n >(\n state: State<TNewState, TNewEvents, TNewActions, TNewName>\n ) => SliceBuilder<\n TSchemaReg & { [K in keyof TNewActions]: TNewState },\n TEvents & TNewEvents,\n TActions & TNewActions,\n TStateMap & { [K in TNewName]: TNewState },\n TActor\n >;\n /**\n * Embeds a built Projection within this slice. The projection's events\n * must be a subset of events from states already registered via\n * `.withState()`. Projection handlers preserve their `(event, stream)`\n * signature and do not receive the app interface.\n */\n withProjection: <TNewEvents extends Schemas>(\n projection: [Exclude<keyof TNewEvents, keyof TEvents>] extends [never]\n ? Projection<TNewEvents>\n : never\n ) => SliceBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor>;\n /**\n * Begins defining a reaction scoped to this slice's events.\n */\n on: <TKey extends keyof TEvents>(\n event: TKey\n ) => {\n do: (\n handler: (\n event: Committed<TEvents, TKey>,\n stream: string,\n app: IAct<TEvents, TActions, TActor>\n ) => Promise<Snapshot<Schema, TEvents> | void>,\n options?: Partial<ReactionOptions>\n ) => SliceBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor> & {\n to: (\n resolver: ReactionResolver<TEvents, TKey> | string\n ) => SliceBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor>;\n };\n };\n /**\n * Builds and returns the Slice data structure.\n */\n build: () => Slice<TSchemaReg, TEvents, TActions, TStateMap, TActor>;\n /**\n * The registered event schemas and their reaction maps.\n */\n readonly events: EventRegister<TEvents>;\n};\n\n/* eslint-disable @typescript-eslint/no-empty-object-type -- {} used as generic defaults */\n\n/**\n * Creates a new slice builder for composing partial states with scoped reactions.\n *\n * Slices enable vertical slice architecture by grouping related states and\n * reactions into self-contained feature modules. Reactions defined in a slice\n * are type-scoped to events from that slice's states only.\n *\n * @example Single-state slice with typed dispatch\n * ```typescript\n * const CounterSlice = slice()\n * .withState(Counter)\n * .on(\"Incremented\")\n * .do(async (event, _stream, app) => {\n * await app.do(\"reset\", target, {});\n * })\n * .to(\"counter-target\")\n * .build();\n * ```\n *\n * @example Cross-state dispatch (include both states)\n * ```typescript\n * const CreationSlice = slice()\n * .withState(TicketCreation)\n * .withState(TicketOperations) // handler can dispatch AssignTicket\n * .on(\"TicketOpened\").do(async (event, _stream, app) => {\n * await app.do(\"AssignTicket\", target, payload, event);\n * })\n * .build();\n * ```\n *\n * @see {@link SliceBuilder} for builder methods\n * @see {@link Slice} for the output type\n */\nexport function slice<\n // @ts-expect-error empty schema\n TSchemaReg extends SchemaRegister<TActions> = {},\n TEvents extends Schemas = {},\n TActions extends Schemas = {},\n TStateMap extends Record<string, Schema> = {},\n TActor extends Actor = Actor,\n>(): SliceBuilder<TSchemaReg, TEvents, TActions, TStateMap, TActor> {\n // One mutable state shared across the entire fluent chain. Each\n // `withState` / `withProjection` / `on` call mutates these and returns\n // the same builder cast to the widened generic; type fanout is preserved\n // through the public type signatures, runtime allocation is not.\n const states = new Map<string, State<any, any, any>>();\n const actions: Record<string, any> = {};\n const events = {} as EventRegister<TEvents>;\n const projections: Projection<any>[] = [];\n\n const builder: SliceBuilder<\n TSchemaReg,\n TEvents,\n TActions,\n TStateMap,\n TActor\n > = {\n withState: (state) => {\n registerState(state, states, actions, events as Record<string, unknown>);\n return builder as never;\n },\n withProjection: (proj) => {\n projections.push(proj as Projection<any>);\n return builder;\n },\n on: <TKey extends keyof TEvents>(event: TKey) => ({\n do: (\n handler: (\n event: Committed<TEvents, TKey>,\n stream: string,\n app: IAct<TEvents, TActions, TActor>\n ) => Promise<Snapshot<Schema, TEvents> | void>,\n options?: Partial<ReactionOptions>\n ) => {\n const reaction: Reaction<TEvents, TKey, TActions, TActor> = {\n handler: handler,\n resolver: _this_,\n options: {\n blockOnError: options?.blockOnError ?? true,\n maxRetries: options?.maxRetries ?? 3,\n },\n };\n if (!handler.name)\n throw new Error(\n `Reaction handler for \"${String(event)}\" must be a named function`\n );\n // Register once with the default _this_ resolver. If `.to()` is\n // chained next, it patches the same reaction's resolver in place\n // — no second Map.set() round-trip.\n events[event].reactions.set(handler.name, reaction);\n return Object.assign(builder, {\n to(resolver: ReactionResolver<TEvents, TKey> | string) {\n reaction.resolver =\n typeof resolver === \"string\" ? { target: resolver } : resolver;\n return builder;\n },\n });\n },\n }),\n build: () => ({\n _tag: \"Slice\" as const,\n states,\n events,\n projections,\n }),\n events,\n };\n return builder;\n}\n","/**\n * @module state-builder\n * @category Builders\n *\n * Fluent interface for defining a strongly-typed state machine using Zod schemas.\n */\nimport type { ZodType } from \"zod\";\nimport type {\n ActionHandler,\n GivenHandlers,\n Invariant,\n PassthroughPatchHandler,\n PatchHandlers,\n Schema,\n Schemas,\n Snapshot,\n State,\n ZodTypes,\n} from \"../types/index.js\";\n\n/**\n * Builder interface for defining a state with event sourcing.\n *\n * Provides a fluent API to configure the initial state, event types,\n * and event handlers (reducers) before moving to action configuration.\n *\n * @template TState - State schema type\n * @template TName - State name literal type\n *\n * @see {@link state} for usage examples\n * @see {@link ActionBuilder} for action configuration\n */\nexport type StateBuilder<\n TState extends Schema,\n TName extends string = string,\n> = {\n /**\n * Defines the initial state for new state instances.\n *\n * The init function is called when a new stream is created (first event).\n * It can accept initial data or return a default state.\n *\n * @param init - Function returning the initial state\n * @returns A builder with `.emits()` to declare event types\n *\n * @example\n * ```typescript\n * .init(() => ({ count: 0, created: new Date() }))\n * ```\n *\n * @example With initial data\n * ```typescript\n * .init((data) => ({ ...data, createdAt: new Date() }))\n * ```\n */\n init: (init: () => Readonly<TState>) => {\n /**\n * Declares the event types that this state can emit.\n *\n * Events represent facts that have happened - they should be named in past tense.\n * Each event is defined with a Zod schema for type safety and runtime validation.\n *\n * @template TEvents - Event schemas type\n * @param events - Object mapping event names to Zod schemas\n * @returns An ActionBuilder (with optional `.patch()` to override specific reducers)\n *\n * @example\n * ```typescript\n * .emits({\n * Incremented: z.object({ amount: z.number() }),\n * Decremented: z.object({ amount: z.number() }),\n * Reset: z.object({})\n * })\n * ```\n */\n emits: <TEvents extends Schemas>(\n events: ZodTypes<TEvents>\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type -- {} avoids string index signature that Record<string, never> would add, keeping keyof A precise\n ) => ActionBuilder<TState, TEvents, {}, TName> & {\n /**\n * Overrides specific event reducers. Events without a custom patch\n * default to passthrough: `({ data }) => data` (event data merges\n * into state).\n *\n * @param patch - Partial map of event names to patch handler functions\n * @returns An ActionBuilder for defining actions\n *\n * @example Only override the events that need custom logic\n * ```typescript\n * .emits({ TicketOpened, TicketClosed, TicketResolved })\n * .patch({\n * TicketOpened: ({ data }) => {\n * const { message, messageId, userId, ...other } = data;\n * return { ...other, userId, messages: { [messageId]: { ... } } };\n * },\n * })\n * // TicketClosed and TicketResolved use passthrough\n * ```\n */\n patch: (\n patch: Partial<PatchHandlers<TState, TEvents>>\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type -- {} avoids string index signature that Record<string, never> would add, keeping keyof A precise\n ) => ActionBuilder<TState, TEvents, {}, TName>;\n };\n };\n};\n\n/** Helper: a single-key record mapping a state name to its Zod schema. */\ntype StateEntry<\n TKey extends string = string,\n TState extends Schema = Schema,\n> = {\n [P in TKey]: ZodType<TState>;\n};\n\n/** Helper: a single-key record mapping an action name to its Zod schema. */\ntype ActionEntry<\n TKey extends string = string,\n TNewActions extends Schema = Schema,\n> = {\n [P in TKey]: ZodType<TNewActions>;\n};\n\n/**\n * Builder interface for defining actions (commands) on a state.\n *\n * Actions represent user/system intents to modify state. Each action is validated\n * against a schema, can have business rule invariants, and must emit one or more events.\n *\n * @template TState - State schema type\n * @template TEvents - Event schemas type\n * @template TActions - Action schemas type\n * @template TName - State name literal type\n *\n * @see {@link state} for complete usage examples\n */\nexport type ActionBuilder<\n TState extends Schema,\n TEvents extends Schemas,\n TActions extends Schemas,\n TName extends string = string,\n> = {\n /**\n * Defines an action (command) that can be executed on this state.\n *\n * Actions represent intents to change state - they should be named in imperative form\n * (e.g., \"CreateUser\", \"IncrementCounter\", \"PlaceOrder\"). Actions are validated against\n * their schema and must emit at least one event.\n *\n * Pass a `{ ActionName: schema }` record — use shorthand `{ ActionName }`\n * when the variable name matches the action name. The key becomes the\n * action name, the value the Zod schema.\n *\n * @template TKey - Action name (string literal type)\n * @template TNewActions - Action payload schema type\n * @param entry - Single-key record `{ ActionName: schema }`\n * @returns An object with `.given()` and `.emit()` for further configuration\n *\n * @example Simple action without invariants\n * ```typescript\n * .on({ increment: z.object({ by: z.number() }) })\n * .emit((action) => [\"Incremented\", { amount: action.by }])\n * ```\n *\n * @example Action with business rules\n * ```typescript\n * .on({ withdraw: z.object({ amount: z.number() }) })\n * .given([\n * (_, snap) => snap.state.balance >= 0 || \"Account closed\",\n * (_, snap, action) => snap.state.balance >= action.amount || \"Insufficient funds\"\n * ])\n * .emit((action) => [\"Withdrawn\", { amount: action.amount }])\n * ```\n *\n * @example Action with shorthand (variable name matches action name)\n * ```typescript\n * const OpenTicket = z.object({ title: z.string() });\n * .on({ OpenTicket })\n * .emit((action) => [\"TicketOpened\", { title: action.title }])\n * ```\n */\n on: <TKey extends string, TNewActions extends Schema>(\n entry: ActionEntry<TKey, TNewActions>\n ) => {\n /**\n * Adds business rule invariants that must hold before the action can execute.\n *\n * Invariants are checked after loading the current state but before emitting events.\n * Each invariant should return `true` or an error message string. All invariants\n * must pass for the action to succeed.\n *\n * @param rules - Array of invariant functions\n * @returns An object with `.emit()` to finalize the action\n *\n * @example\n * ```typescript\n * .given([\n * (_, snap) => snap.state.status === \"active\" || \"Must be active\",\n * (target, snap) => snap.state.ownerId === target.actor.id || \"Not authorized\"\n * ])\n * ```\n */\n given: (rules: Invariant<TState>[]) => {\n /**\n * Defines the action handler that emits events.\n *\n * The handler receives the action payload and current state snapshot,\n * and must return one or more events to emit. Events are applied to state\n * via the patch handlers defined earlier.\n *\n * Pass a string event name for passthrough: the action payload becomes\n * the event data directly.\n *\n * @param handler - Function that returns events to emit, or event name string for passthrough\n * @returns The ActionBuilder for chaining more actions\n *\n * @example Custom handler\n * ```typescript\n * .emit((action, snapshot) => {\n * const newBalance = snapshot.state.balance + action.amount;\n * return [\"Deposited\", { amount: action.amount, newBalance }];\n * })\n * ```\n *\n * @example Passthrough (action payload = event data)\n * ```typescript\n * .emit(\"TicketAssigned\")\n * ```\n */\n emit: {\n /** Custom handler — receives `(action, snapshot)` and returns one\n * or more `[EventName, data]` tuples (or `undefined`). */\n (\n handler: ActionHandler<\n TState,\n TEvents,\n { [P in TKey]: TNewActions },\n TKey\n >\n ): ActionBuilder<\n TState,\n TEvents,\n TActions & { [P in TKey]: TNewActions },\n TName\n >;\n /** Passthrough — the action payload becomes the event data\n * directly. Must reference an event declared in `.emits()`. */\n (\n eventName: keyof TEvents & string\n ): ActionBuilder<\n TState,\n TEvents,\n TActions & { [P in TKey]: TNewActions },\n TName\n >;\n };\n };\n /**\n * Defines the action handler that emits events. Same two overloads as\n * the post-`.given()` form above:\n *\n * - **Function** — receives `(action, snapshot)` and returns one or\n * more `[EventName, data]` tuples (or `undefined`).\n * - **String** — passthrough: the action payload becomes the event\n * data directly. Must reference an event declared in `.emits()`.\n *\n * The two overloads are kept separate (rather than merged into a\n * `handler | string` union) so that TS contextual typing of the\n * function alternative isn't degraded by considering the string\n * branch — under the union form `TState` could collapse to its\n * `Schema` constraint inside the callback.\n *\n * @example Passthrough (action payload = event data)\n * ```typescript\n * .emit(\"Incremented\")\n * ```\n *\n * @example Single event\n * ```typescript\n * .emit((action) => [\"Incremented\", { amount: action.by }])\n * ```\n *\n * @example Multiple events\n * ```typescript\n * .emit((action) => [\n * [\"Incremented\", { amount: action.by }],\n * [\"LogUpdated\", { message: `Incremented by ${action.by}` }]\n * ])\n * ```\n */\n emit: {\n (\n handler: ActionHandler<\n TState,\n TEvents,\n { [P in TKey]: TNewActions },\n TKey\n >\n ): ActionBuilder<\n TState,\n TEvents,\n TActions & { [P in TKey]: TNewActions },\n TName\n >;\n (\n eventName: keyof TEvents & string\n ): ActionBuilder<\n TState,\n TEvents,\n TActions & { [P in TKey]: TNewActions },\n TName\n >;\n };\n };\n /**\n * Defines a snapshotting strategy to optimize state reconstruction.\n *\n * Snapshots store the current state at a point in time, allowing faster state loading\n * by avoiding replaying all events from the beginning. The snap function is called\n * after each event is applied and should return `true` when a snapshot should be taken.\n *\n * @param snap - Predicate function that returns true when a snapshot should be taken\n * @returns The ActionBuilder for chaining\n *\n * @example Snapshot every 10 events\n * ```typescript\n * .snap((snapshot) => snapshot.patches >= 10)\n * ```\n *\n * @example Snapshot based on state size\n * ```typescript\n * .snap((snapshot) => {\n * const estimatedSize = JSON.stringify(snapshot.state).length;\n * return estimatedSize > 10000 || snapshot.patches >= 50;\n * })\n * ```\n *\n * @example Time-based snapshotting\n * ```typescript\n * .snap((snapshot) => {\n * const hoursSinceLastSnapshot = snapshot.patches * 0.1; // Estimate\n * return hoursSinceLastSnapshot >= 24;\n * })\n * ```\n */\n snap: (\n snap: (snapshot: Snapshot<TState, TEvents>) => boolean\n ) => ActionBuilder<TState, TEvents, TActions, TName>;\n /**\n * Finalizes and builds the state definition.\n *\n * Call this method after defining all actions, invariants, and patches to create\n * the complete State object that can be registered with Act.\n *\n * @returns The complete strongly-typed State definition\n *\n * @example\n * ```typescript\n * const Counter = state({ Counter: schema })\n * .init(() => ({ count: 0 }))\n * .emits({ Incremented: z.object({ amount: z.number() }) })\n * .patch({ Incremented: ({ data }, state) => ({ count: state.count + data.amount }) })\n * .on({ increment: z.object({ by: z.number() }) })\n * .emit((action) => [\"Incremented\", { amount: action.by }])\n * .build(); // Returns State<TState, TEvents, TActions, TName>\n * ```\n */\n build: () => State<TState, TEvents, TActions, TName>;\n};\n\n/**\n * Creates a new state definition with event sourcing capabilities.\n *\n * States are the core building blocks of Act. Each state represents a consistency\n * boundary (aggregate) that processes actions, emits events, and maintains its own\n * state through event patches (reducers). States use event sourcing to maintain a\n * complete audit trail and enable time-travel capabilities.\n *\n * The state builder provides a fluent API for defining:\n * 1. Initial state via `.init()`\n * 2. Event types via `.emits()` — all events default to passthrough (`({ data }) => data`)\n * 3. Custom event reducers via `.patch()` (optional — only for events that need custom logic)\n * 4. Actions (commands) via `.on()` → `.emit()` — pass an event name string for passthrough\n * 5. Business rules (invariants) via `.given()`\n * 6. Snapshotting strategy via `.snap()`\n *\n * @template TState - Zod schema type defining the shape of the state\n * @param entry - Single-key record mapping state name to Zod schema (e.g., `{ Counter: z.object({ count: z.number() }) }`)\n * @returns A StateBuilder instance for fluent API configuration\n *\n * @example Basic counter state (with custom patch)\n * ```typescript\n * import { state } from \"@rotorsoft/act\";\n * import { z } from \"zod\";\n *\n * const Counter = state({ Counter: z.object({ count: z.number() }) })\n * .init(() => ({ count: 0 }))\n * .emits({\n * Incremented: z.object({ amount: z.number() })\n * })\n * .patch({ // optional — only for events needing custom reducers\n * Incremented: ({ data }, state) => ({ count: state.count + data.amount })\n * })\n * .on({ increment: z.object({ by: z.number() }) })\n * .emit((action) => [\"Incremented\", { amount: action.by }])\n * .build();\n * ```\n *\n * @example Passthrough state (no custom patch or emit needed)\n * ```typescript\n * const DigitBoard = state({ DigitBoard: z.object({ digit: z.string() }) })\n * .init(() => ({ digit: \"\" }))\n * .emits({ DigitCounted: z.object({ digit: z.string() }) })\n * // no .patch() — passthrough is the default (event data merges into state)\n * .on({ CountDigit: z.object({ digit: z.string() }) })\n * .emit(\"DigitCounted\") // string passthrough — action payload becomes event data\n * .build();\n * ```\n *\n * @example State with multiple events and invariants\n * ```typescript\n * const BankAccount = state({ BankAccount: z.object({\n * balance: z.number(),\n * currency: z.string(),\n * status: z.enum([\"open\", \"closed\"])\n * }) })\n * .init(() => ({ balance: 0, currency: \"USD\", status: \"open\" }))\n * .emits({\n * Deposited: z.object({ amount: z.number() }),\n * Withdrawn: z.object({ amount: z.number() }),\n * Closed: z.object({})\n * })\n * .patch({ // only override events needing custom logic\n * Deposited: ({ data }, state) => ({ balance: state.balance + data.amount }),\n * Withdrawn: ({ data }, state) => ({ balance: state.balance - data.amount }),\n * Closed: () => ({ status: \"closed\", balance: 0 })\n * })\n * .on({ deposit: z.object({ amount: z.number() }) })\n * .given([\n * (_, snap) => snap.state.status === \"open\" || \"Account must be open\"\n * ])\n * .emit(\"Deposited\") // passthrough — action payload { amount } becomes event data\n * .on({ withdraw: z.object({ amount: z.number() }) })\n * .given([\n * (_, snap) => snap.state.status === \"open\" || \"Account must be open\",\n * (_, snap, action) =>\n * snap.state.balance >= action.amount || \"Insufficient funds\"\n * ])\n * .emit(\"Withdrawn\")\n * .on({ close: z.object({}) })\n * .given([\n * (_, snap) => snap.state.status === \"open\" || \"Already closed\",\n * (_, snap) => snap.state.balance === 0 || \"Balance must be zero\"\n * ])\n * .emit(\"Closed\")\n * .build();\n * ```\n *\n * @example State with snapshotting\n * ```typescript\n * const User = state({ User: z.object({\n * name: z.string(),\n * email: z.string(),\n * loginCount: z.number()\n * }) })\n * .init((data) => ({ ...data, loginCount: 0 }))\n * .emits({\n * UserCreated: z.object({ name: z.string(), email: z.string() }),\n * UserLoggedIn: z.object({})\n * })\n * .patch({ // only override events needing custom logic\n * UserLoggedIn: (_, state) => ({ loginCount: state.loginCount + 1 })\n * })\n * // UserCreated uses passthrough — event data merges into state\n * .on({ createUser: z.object({ name: z.string(), email: z.string() }) })\n * .emit(\"UserCreated\") // passthrough\n * .on({ login: z.object({}) })\n * .emit(\"UserLoggedIn\")\n * .snap((snap) => snap.patches >= 10) // Snapshot every 10 events\n * .build();\n * ```\n *\n * @see {@link StateBuilder} for available builder methods\n * @see {@link ActionBuilder} for action configuration methods\n * @see {@link https://rotorsoft.github.io/act-root/docs/intro | Getting Started Guide}\n * @see {@link https://rotorsoft.github.io/act-root/docs/examples/calculator | Calculator Example}\n */\nexport function state<TName extends string, TState extends Schema>(\n entry: StateEntry<TName, TState>\n): StateBuilder<TState, TName> {\n const keys = Object.keys(entry);\n if (keys.length !== 1) throw new Error(\"state() requires exactly one key\");\n const name = keys[0] as TName;\n const stateSchema = (entry as Record<string, ZodType<TState>>)[name];\n return {\n init(init) {\n return {\n emits<TEvents extends Schema>(events: ZodTypes<TEvents>) {\n // Default passthrough patches: event data merges into state\n const defaultPatch = Object.fromEntries(\n Object.keys(events).map((k) => {\n const fn = Object.assign(({ data }: { data: any }) => data, {\n _passthrough: true as const,\n }) satisfies PassthroughPatchHandler;\n return [k, fn];\n })\n ) as unknown as PatchHandlers<TState, TEvents>;\n\n // Build one mutable state object the action_builder threads\n // through every fluent call. patch() (if invoked) just mutates\n // the patch map in place — no re-builder wasted.\n const internal: State<TState, TEvents, Schemas, TName> = {\n events,\n actions: {},\n state: stateSchema,\n name,\n init,\n patch: defaultPatch,\n on: {},\n };\n\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type -- {} avoids string index signature\n const builder = action_builder<TState, TEvents, {}, TName>(internal);\n\n return Object.assign(builder, {\n patch(customPatch: Partial<PatchHandlers<TState, TEvents>>) {\n Object.assign(internal.patch, customPatch);\n return builder;\n },\n });\n },\n };\n },\n };\n}\n\n/**\n * Internal action-builder. The runtime object is a single mutable bag —\n * each fluent call (`on`, `snap`) mutates it and returns the same builder\n * cast to the widened generic type. Type-level fanout is preserved; the\n * O(N) `{...state}` spreads per call are not.\n *\n * Generics are erased to `Schemas` at runtime — the cast on return narrows\n * back to the call-site's widened types.\n */\nfunction action_builder<\n TState extends Schema,\n TEvents extends Schemas,\n TActions extends Schemas,\n TName extends string = string,\n>(\n state: State<TState, TEvents, TActions, TName>\n): ActionBuilder<TState, TEvents, TActions, TName> {\n // The mutable bag — typed loosely since callers narrow on return.\n const internal = state as unknown as State<TState, TEvents, Schemas, TName>;\n\n const builder: ActionBuilder<TState, TEvents, TActions, TName> = {\n on<TKey extends string, TNewActions extends Schema>(\n entry: ActionEntry<TKey, TNewActions>\n ) {\n const keys = Object.keys(entry);\n if (keys.length !== 1) throw new Error(\".on() requires exactly one key\");\n const action = keys[0] as TKey;\n const schema = entry[action];\n\n if (action in internal.actions)\n throw new Error(`Duplicate action \"${action}\"`);\n\n type MergedActions = TActions & { [P in TKey]: TNewActions };\n (internal.actions as Record<string, ZodType<Schema>>)[action] = schema;\n\n function given(rules: Invariant<TState>[]) {\n internal.given ??= {} as GivenHandlers<TState, Schemas>;\n (internal.given as Record<string, Invariant<TState>[]>)[action] = rules;\n return { emit };\n }\n\n function emit(\n handler:\n | ActionHandler<TState, TEvents, MergedActions, TKey>\n | (keyof TEvents & string)\n ) {\n if (typeof handler === \"string\") {\n const eventName = handler;\n (internal.on as Record<string, unknown>)[action] = (payload: any) => [\n eventName,\n payload,\n ];\n } else {\n (internal.on as Record<string, unknown>)[action] = handler;\n }\n return builder as unknown as ActionBuilder<\n TState,\n TEvents,\n MergedActions,\n TName\n >;\n }\n\n return { given, emit };\n },\n\n snap(snap: (snapshot: Snapshot<TState, TEvents>) => boolean) {\n internal.snap = snap;\n return builder;\n },\n\n build(): State<TState, TEvents, TActions, TName> {\n return internal as unknown as State<TState, TEvents, TActions, TName>;\n },\n };\n return builder;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACYA,IAAM,eAAuC;AAAA,EAC3C,OAAO;AAAA,EACP,OAAO;AAAA,EACP,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO;AAAA,EACP,OAAO;AACT;AAEA,IAAM,eAAuC;AAAA,EAC3C,OAAO;AAAA;AAAA,EACP,OAAO;AAAA;AAAA,EACP,MAAM;AAAA;AAAA,EACN,MAAM;AAAA;AAAA,EACN,OAAO;AAAA;AAAA,EACP,OAAO;AAAA;AACT;AAEA,IAAM,QA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AAO,SAAS,OAAO,WAAW;AACzC,qBAAO;AAAA,YACT;AAAA,UACF,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF;AACF;AAWA,SAAS,eAMPC,QACiD;AAEjD,QAAM,WAAWA;AAEjB,QAAM,UAA2D;AAAA,IAC/D,GACE,OACA;AACA,YAAM,OAAO,OAAO,KAAK,KAAK;AAC9B,UAAI,KAAK,WAAW,EAAG,OAAM,IAAI,MAAM,gCAAgC;AACvE,YAAMC,UAAS,KAAK,CAAC;AACrB,YAAM,SAAS,MAAMA,OAAM;AAE3B,UAAIA,WAAU,SAAS;AACrB,cAAM,IAAI,MAAM,qBAAqBA,OAAM,GAAG;AAGhD,MAAC,SAAS,QAA4CA,OAAM,IAAI;AAEhE,eAAS,MAAM,OAA4B;AACzC,iBAAS,UAAU,CAAC;AACpB,QAAC,SAAS,MAA8CA,OAAM,IAAI;AAClE,eAAO,EAAE,KAAK;AAAA,MAChB;AAEA,eAAS,KACP,SAGA;AACA,YAAI,OAAO,YAAY,UAAU;AAC/B,gBAAM,YAAY;AAClB,UAAC,SAAS,GAA+BA,OAAM,IAAI,CAAC,YAAiB;AAAA,YACnE;AAAA,YACA;AAAA,UACF;AAAA,QACF,OAAO;AACL,UAAC,SAAS,GAA+BA,OAAM,IAAI;AAAA,QACrD;AACA,eAAO;AAAA,MAMT;AAEA,aAAO,EAAE,OAAO,KAAK;AAAA,IACvB;AAAA,IAEA,KAAKC,OAAwD;AAC3D,eAAS,OAAOA;AAChB,aAAO;AAAA,IACT;AAAA,IAEA,QAAiD;AAC/C,aAAO;AAAA,IACT;AAAA,EACF;AACA,SAAO;AACT;","names":["action","import_zod","import_zod","log","store","cache","tombstone","load","snap","state","import_node_crypto","block","import_zod","state","block","action","import_node_crypto","state","action","action","stream","EventEmitter","action","snap","state","state","state","action","snap"]}
|