@rotorsoft/act 2.2.4 → 2.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/.tsbuildinfo +1 -1
- package/dist/@types/adapters/in-memory-store.d.ts.map +1 -1
- package/dist/{chunk-2CYRPAJN.js → chunk-SJJVHRME.js} +8 -1
- package/dist/chunk-SJJVHRME.js.map +1 -0
- package/dist/{chunk-XG3UD63Y.cjs → chunk-UYNNDDE7.cjs} +8 -1
- package/dist/chunk-UYNNDDE7.cjs.map +1 -0
- package/dist/index.cjs +115 -115
- package/dist/index.js +1 -1
- package/dist/test/index.cjs +3 -3
- package/dist/test/index.js +1 -1
- package/package.json +2 -2
- package/dist/chunk-2CYRPAJN.js.map +0 -1
- package/dist/chunk-XG3UD63Y.cjs.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory-store.d.ts","sourceRoot":"","sources":["../../../src/adapters/in-memory-store.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAQH,OAAO,KAAK,EACV,MAAM,EACN,YAAY,EACZ,SAAS,EACT,SAAS,EACT,KAAK,EACL,KAAK,EACL,iBAAiB,EACjB,YAAY,EACZ,kBAAkB,EAClB,MAAM,EACN,OAAO,EACP,KAAK,EACL,YAAY,EACZ,cAAc,EACd,WAAW,EACX,cAAc,EACf,MAAM,mBAAmB,CAAC;AAkN3B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,qBAAa,aAAc,YAAW,KAAK;IACzC,OAAO,CAAC,OAAO,CAA2C;IAE1D,OAAO,CAAC,QAAQ,CAAK;IACrB,OAAO,CAAC,QAAQ,CAA0C;IAE1D,OAAO,CAAC,cAAc,CAAM;IAE5B,OAAO,CAAC,YAAY,CAGhB;IACJ,OAAO,CAAC,gBAAgB,CAAkC;IAG1D,OAAO,CAAC,IAAI,CAAgE;IAE5E,OAAO,CAAC,cAAc;IAStB,OAAO,CAAC,QAAQ;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AAO,EAAE,MAAM,OAAO,CAAC;iBACnC,IAAI;QAuDlB;IAED,+EAA+E;IACzE,OAAO,CACX,MAAM,EAAE,CACN,QAAQ,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,OAAO,EAAE,MAAM,OAAO,CAAC,KAAK,OAAO,CAAC,MAAM,CAAC,KACpE,OAAO,CAAC,IAAI,CAAC,GACjB,OAAO,CAAC,IAAI,CAAC,CAkDf;CACF"}
|
|
@@ -820,8 +820,15 @@ var InMemoryStore = class {
|
|
|
820
820
|
this._streams = /* @__PURE__ */ new Map();
|
|
821
821
|
this._stream_versions = /* @__PURE__ */ new Map();
|
|
822
822
|
this._pii = /* @__PURE__ */ new Map();
|
|
823
|
+
const seen = /* @__PURE__ */ new Set();
|
|
823
824
|
try {
|
|
824
825
|
await driver(async (event) => {
|
|
826
|
+
const at = `${event.version}:${event.stream}`;
|
|
827
|
+
if (seen.has(at))
|
|
828
|
+
throw new Error(
|
|
829
|
+
`restore: stream "${event.stream}" has version ${event.version} twice`
|
|
830
|
+
);
|
|
831
|
+
seen.add(at);
|
|
825
832
|
const id = this._next_id++;
|
|
826
833
|
const { pii, ...rest } = event;
|
|
827
834
|
const committed = { ...rest, id };
|
|
@@ -1347,4 +1354,4 @@ export {
|
|
|
1347
1354
|
disposeAndExit,
|
|
1348
1355
|
dispose
|
|
1349
1356
|
};
|
|
1350
|
-
//# sourceMappingURL=chunk-
|
|
1357
|
+
//# sourceMappingURL=chunk-SJJVHRME.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/adapters/console-logger.ts","../src/internal/lru-map.ts","../src/adapters/in-memory-cache.ts","../src/config.ts","../src/utils.ts","../src/adapters/in-memory-store.ts","../src/disposers.ts","../src/internal/config.ts","../src/internal/defer-timer.ts","../src/scoped.ts","../src/ports.ts"],"sourcesContent":["/**\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._pretty_write.bind(this, bindings)\n : this._json_write.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 _json_write(\n bindings: Record<string, unknown> | undefined,\n level: string,\n _num: number,\n obj_or_msg: unknown,\n msg?: string\n ): void {\n let obj: Record<string, unknown>;\n let message: string | undefined;\n\n if (typeof obj_or_msg === \"string\") {\n message = obj_or_msg;\n obj = {};\n } else if (obj_or_msg instanceof Error) {\n // Error instances spread to `{}` — capture the salient fields\n // explicitly so structured log aggregators see them.\n message = msg ?? obj_or_msg.message;\n obj = {\n error: { message: obj_or_msg.message, name: obj_or_msg.name },\n stack: obj_or_msg.stack,\n };\n } else if (obj_or_msg !== null && typeof obj_or_msg === \"object\") {\n message = msg;\n obj = { ...(obj_or_msg as Record<string, unknown>) };\n } else {\n message = msg;\n obj = { value: obj_or_msg };\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 _pretty_write(\n bindings: Record<string, unknown> | undefined,\n level: string,\n _num: number,\n obj_or_msg: 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 obj_or_msg === \"string\") {\n message = obj_or_msg;\n } else if (obj_or_msg instanceof Error) {\n // Error instances don't serialize their `message`/`stack` via\n // JSON.stringify — `JSON.stringify(err) === \"{}\"`. Render the\n // message inline and the stack as the data payload so operators\n // see something useful instead of an empty object.\n message = msg ?? obj_or_msg.message;\n data = obj_or_msg.stack;\n } else {\n message = msg ?? \"\";\n if (obj_or_msg !== undefined && obj_or_msg !== null) {\n try {\n data = JSON.stringify(obj_or_msg);\n } catch {\n data = \"[unserializable]\";\n }\n }\n }\n\n const bind_str =\n bindings && Object.keys(bindings).length\n ? ` ${JSON.stringify(bindings)}`\n : \"\";\n\n const parts = [ts, tag, message, data, bind_str].filter(Boolean);\n process.stdout.write(parts.join(\" \") + \"\\n\");\n }\n}\n","/**\n * Tiny bounded LRU map built on insertion-ordered `Map`. Caps memory in\n * long-running orchestrators that mint large numbers of keys:\n *\n * - {@link InMemoryCache}: stream → state checkpoint\n * - correlate's dynamic subscriptions (stream → priority and lane) and its\n * compiled source patterns\n *\n * Apps with millions of dynamic streams (one target per aggregate) can't\n * afford an unbounded map: eviction is required.\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 */\nexport class LruMap<K, V> {\n private readonly _entries = new Map<K, V>();\n private readonly _max_size: number;\n\n constructor(maxSize: number) {\n this._max_size = maxSize;\n }\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._max_size) {\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","import { LruMap } from \"../internal/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 */\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","/**\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 Config,\n type Environment,\n Environments,\n type LogLevel,\n LogLevels,\n type Package,\n} from \"./types/index.js\";\nimport { validate } from \"./utils.js\";\n\n/**\n * Zod schema for the package.json fields {@link config} validates.\n */\nexport const package_schema = 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}) satisfies z.ZodType<Package>;\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. `name` and `version` also fall back one by\n * one when the file has no valid value for them.\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 the local package.json as a Package object, keeping each field only\n * when it is valid: `npm init -y` writes an empty `description` and\n * `author`, and a bare `{\"type\":\"module\"}` has no name or version. A\n * missing or invalid `name` / `version` falls back to\n * {@link FALLBACK_PACKAGE}. On a read or parse failure, falls back entirely\n * and 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 get_package = (): Package => {\n try {\n const raw = JSON.parse(fs.readFileSync(\"package.json\").toString());\n const pkg: Record<string, unknown> = {\n name: FALLBACK_PACKAGE.name,\n version: FALLBACK_PACKAGE.version,\n };\n for (const [key, schema] of Object.entries(package_schema.shape)) {\n const field = schema.safeParse(raw?.[key]);\n if (field.success && field.data !== undefined) pkg[key] = field.data;\n }\n return pkg as Package;\n } catch (err) {\n pkg_load_error = err;\n return FALLBACK_PACKAGE;\n }\n};\n\n/** Stashed read/parse error from {@link get_package}, surfaced by config(). */\nlet pkg_load_error: 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 = package_schema.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}) satisfies z.ZodType<Config>;\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 = get_package();\n\n// Lazily validated on first call. Cannot run validate() 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 * @see {@link Config} for configuration type\n */\nexport const config = (): Config => {\n if (!_validated) {\n _validated = validate(\n \"config\",\n { ...pkg, env, logLevel, logSingleLine, sleepMs },\n BaseSchema\n );\n if (pkg_load_error) {\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 pkg_load_error instanceof Error\n ? pkg_load_error.message\n : typeof pkg_load_error === \"string\"\n ? pkg_load_error\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 pkg_load_error = undefined;\n }\n }\n return _validated;\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 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 * 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 * @module act/adapters\n * In-memory event store adapter for development and tests.\n *\n * @category Adapters\n */\n\nimport {\n checkAppends,\n checkVersion,\n fairSlots,\n seedEvent,\n} from \"../adapter.js\";\nimport type {\n Append,\n BlockedLease,\n Committed,\n EventMeta,\n Lease,\n Query,\n QueryStatsOptions,\n QueryStreams,\n QueryStreamsResult,\n Schema,\n Schemas,\n Store,\n StreamFilter,\n StreamPosition,\n StreamStats,\n SubscribeInput,\n} from \"../types/index.js\";\nimport {\n DEFAULT_LANE,\n DEFAULT_QUERY_STREAMS_LIMIT,\n SNAP_EVENT,\n} from \"../types/index.js\";\nimport { sleep } from \"../utils.js\";\n\n/**\n * One subscription row: watermark, lease, retry and schedule. Its state is\n * private and changes only through the methods below, which carry the\n * row's rules; the store reads it through the getters.\n */\nclass InMemoryStream {\n readonly stream: string;\n readonly source: string | undefined;\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 private _priority: number;\n private _lane: string;\n // While in the future, the stream is held out of `claim`.\n private _deferred_at: number | undefined = undefined;\n // Work mark; `undefined` = never correlated, so `claim` skips it.\n private _correlated_at: number | undefined = undefined;\n\n constructor(\n stream: string,\n source: string | undefined,\n priority = 0,\n lane: string = DEFAULT_LANE\n ) {\n this.stream = stream;\n this.source = source;\n this._priority = priority;\n this._lane = lane;\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 get priority() {\n return this._priority;\n }\n\n get lane() {\n return this._lane;\n }\n\n get deferred_at() {\n return this._deferred_at;\n }\n\n get correlated_at() {\n return this._correlated_at;\n }\n\n get is_available() {\n return (\n !this._blocked &&\n (!this._leased_until || this._leased_until <= new Date()) &&\n (!this._deferred_at || this._deferred_at <= Date.now())\n );\n }\n\n /**\n * A later subscribe of this row keeps the maximum priority, and takes the\n * new lane only at or above the stored priority (compared before the\n * bump), so a lower-priority resolution can't re-lane the stream. The\n * lane is replaced on every qualifying subscribe, so the current config\n * wins on restart.\n */\n resubscribe(priority: number, lane: string) {\n if (priority >= this._priority) this._lane = lane;\n if (priority > this._priority) this._priority = priority;\n }\n\n /** Operator override; ignores the subscribe-time `max()`. */\n prioritize(priority: number) {\n this._priority = priority;\n }\n\n /** A mark never regresses, including at zero and negatives. */\n mark(correlated_at: number) {\n if (\n this._correlated_at === undefined ||\n correlated_at > this._correlated_at\n )\n this._correlated_at = correlated_at;\n }\n\n defer(deferred_at: number) {\n this._deferred_at = deferred_at;\n // A defer is not a failure, so the redelivery is a fresh attempt.\n this._retry = -1;\n }\n\n // The lease this row hands back, at the given watermark.\n private _lease(lease: Lease, at: number) {\n return {\n stream: this.stream,\n source: this.source,\n at,\n by: lease.by,\n retry: this._retry,\n lagging: lease.lagging,\n lane: this._lane,\n };\n }\n\n lease(lease: Lease, millis: number): Lease {\n // The holder is recorded even at zero `millis`: `ack` is gated on it,\n // so a lease with no holder would drop every ack.\n this._leased_by = lease.by;\n this._leased_until = new Date(Date.now() + millis);\n this._retry = this._retry + 1;\n return this._lease(lease, lease.at);\n }\n\n /** Ack, or with `due`, also persist the schedule. See {@link Store.ack}. */\n ack(lease: Lease) {\n if (this._leased_by === lease.by) {\n this._leased_by = undefined;\n this._leased_until = undefined;\n if (lease.due !== undefined) {\n // Advance past the handled prefix and schedule the re-visit with the\n // caller's retry (-1 for a defer, the climbing count for a backoff).\n this._at = lease.at;\n this._retry = lease.retry;\n this._deferred_at = lease.due;\n return undefined;\n }\n this._retry = -1;\n this._at = lease.at;\n this._deferred_at = undefined;\n return this._lease(lease, this._at);\n }\n }\n\n block(lease: Lease, error: string) {\n // Re-blocking is a no-op, like the SQL `AND blocked = false` guard.\n if (this._leased_by === lease.by && !this._blocked) {\n this._blocked = true;\n this._error = error;\n this._deferred_at = undefined;\n return { ...this._lease(lease, this._at), error };\n }\n }\n\n // Clears the block, the lease and the schedule; retry back to -1.\n private _release() {\n this._retry = -1;\n this._blocked = false;\n this._error = \"\";\n this._leased_by = undefined;\n this._leased_until = undefined;\n this._deferred_at = undefined;\n }\n\n /** Retry resets to -1 (released); the next claim makes it 0. */\n reset() {\n this._at = -1;\n this._release();\n }\n\n /** True only when the stream was blocked. */\n unblock(): boolean {\n if (!this._blocked) return false;\n this._release();\n return true;\n }\n}\n\n/**\n * Tests a name against a stream or source filter: exact, or a regex\n * compiled once.\n */\nconst name_test = (\n pattern: string,\n exact?: boolean\n): ((name: string) => boolean) => {\n if (exact) return (name) => name === pattern;\n const re = new RegExp(pattern);\n return (name) => re.test(name);\n};\n\n/**\n * In-memory event store: the default when no other store is injected.\n * For development, tests and examples; all data is lost when the process\n * exits. Use {@link PostgresStore} in production.\n *\n * `Store.notify` is not implemented: a single process has no remote writer\n * to hear from, and local commits already arm the drain.\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 * @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 private _events: Committed<Schemas, keyof Schemas>[] = [];\n // Never reused. Deletions punch holes, so ids are not array indexes.\n private _next_id = 0;\n private _streams: Map<string, InMemoryStream> = new Map();\n // Correlate checkpoint: how far the log has been READ.\n private _correlated_at = -1;\n // Per-correlator checkpoint and lease; keyless callers use `_correlated_at`.\n private _correlators = new Map<\n string,\n { at: number; by: string; until: number }\n >();\n private _stream_versions: Map<string, number> = new Map();\n // stream → (event_id → cloned pii). Two levels so `forget_pii` drops one\n // inner Map. Absence means no PII.\n private _pii: Map<string, Map<number, Record<string, unknown>>> = new Map();\n\n private _reset_indexes() {\n this._events.length = 0;\n this._next_id = 0;\n this._correlated_at = -1;\n this._stream_versions.clear();\n this._pii.clear();\n }\n\n // Stores a cloned copy, so caller-side mutation can't reach it.\n private _put_pii(stream: string, id: number, pii: Record<string, unknown>) {\n let per_stream = this._pii.get(stream);\n if (!per_stream) {\n per_stream = new Map();\n this._pii.set(stream, per_stream);\n }\n per_stream.set(id, structuredClone(pii));\n }\n\n // First index whose id is greater than `after` (ids are sorted, not dense).\n private _first_index_after(after: number): number {\n let lo = 0;\n let hi = this._events.length;\n while (lo < hi) {\n const mid = (lo + hi) >>> 1;\n if (this._events[mid].id > after) hi = mid;\n else lo = mid + 1;\n }\n return lo;\n }\n\n // Allocation-free for events without PII, the common case.\n private _with_pii<E extends Schemas>(\n e: Committed<E, keyof E>,\n wanted = true\n ): Committed<E, keyof E> {\n if (!wanted) return e;\n const pii = this._pii.get(e.stream)?.get(e.id);\n return pii ? ({ ...e, pii } as Committed<E, keyof E>) : e;\n }\n\n async dispose() {\n await sleep();\n this._reset_indexes();\n }\n\n async seed() {\n await sleep();\n }\n\n async drop() {\n await sleep();\n this._reset_indexes();\n this._streams = new Map();\n }\n\n private _in_query(\n query: Query\n ): (e: Committed<Schemas, keyof Schemas>) => boolean {\n const stream_ok = query.stream\n ? name_test(query.stream, query.stream_exact)\n : undefined;\n return (e) => {\n if (stream_ok && !stream_ok(e.stream)) return false;\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 // `created` is not monotonic with `id` (restore keeps it), so time\n // bounds filter; they never end the scan.\n if (query.created_after && e.created <= query.created_after) return false;\n if (query.created_before && e.created >= query.created_before)\n return false;\n return true;\n };\n }\n\n // Index of the stream's latest snapshot, or -1; an explicit `after` wins.\n private _snap_floor(query: Query | undefined): number {\n if (\n !query?.with_snaps ||\n !query.stream_exact ||\n query.stream === undefined ||\n query.after !== undefined\n )\n return -1;\n for (let j = this._events.length - 1; j >= 0; j--) {\n const e = this._events[j];\n if (e.stream === query.stream && e.name === SNAP_EVENT) return j;\n }\n return -1;\n }\n\n async query<E extends Schemas>(\n callback: (event: Committed<E, keyof E>) => void,\n query?: Query\n ) {\n await sleep();\n // The id bounds (`after`, `before`, the snapshot floor) are one index\n // window over the id-ordered log; every other filter is per event.\n const floor = this._snap_floor(query);\n const lo = floor >= 0 ? floor : this._first_index_after(query?.after ?? -1);\n const hi =\n query?.before !== undefined\n ? this._first_index_after(query.before - 1)\n : this._events.length;\n const with_pii = query?.with_pii !== false;\n const in_query = this._in_query(query ?? {});\n let count = 0;\n for (let k = 0; k < hi - lo; k++) {\n const e = this._events[query?.backward ? hi - 1 - k : lo + k];\n if (!in_query(e)) continue;\n await Promise.resolve(\n callback(this._with_pii(e as Committed<E, keyof E>, with_pii))\n );\n count++;\n if (query?.limit && count >= query.limit) break;\n }\n return count;\n }\n\n async commit<E extends Schemas>(\n appends: readonly Append<E>[],\n meta: EventMeta\n ) {\n await sleep();\n checkAppends(appends);\n // Every version is checked before anything is appended, so a stale\n // append leaves every stream as it was.\n for (const append of appends)\n checkVersion(append, this._stream_versions.get(append.stream) ?? -1);\n const created = new Date();\n return appends.flatMap(({ stream, msgs }) => {\n let version = this._stream_versions.get(stream) ?? -1;\n const committed = msgs.map(({ name, data, pii }) => {\n const c: Committed<E, keyof E> = {\n id: this._next_id++,\n stream,\n version: ++version,\n created,\n name,\n data,\n meta,\n };\n // Stored pii-less, so `forget_pii` never touches the event row.\n this._events.push(c as Committed<Schemas, keyof Schemas>);\n if (pii != null) this._put_pii(stream, c.id, pii);\n return this._with_pii(c);\n });\n this._stream_versions.set(stream, version);\n return committed;\n });\n }\n\n async claim(\n lagging: number,\n leading: number,\n by: string,\n millis: number,\n lane?: string\n ) {\n await sleep();\n // Eligibility is `at < correlated_at` on the row alone; the event log\n // is never read. An unmarked row is not claimable.\n const has_work = (s: InMemoryStream): boolean =>\n s.correlated_at !== undefined && s.at < s.correlated_at;\n const available = [...this._streams.values()].filter(\n (s) =>\n s.is_available && has_work(s) && (lane === undefined || s.lane === lane)\n );\n // Lagging: priority DESC, then watermark ASC, like the PG SQL. The\n // `fair` slots ignore priority so sustained high-priority load can't\n // starve a default-priority stream.\n const fair = fairSlots(lagging);\n const by_priority = [...available].sort(\n (a, b) => b.priority - a.priority || a.at - b.at\n );\n const priority_slice = by_priority.slice(0, lagging - fair);\n const picked = new Set(priority_slice.map((s) => s.stream));\n const fair_slice = [...available]\n .sort((a, b) => a.at - b.at)\n .filter((s) => !picked.has(s.stream))\n .slice(0, fair);\n const lag = [...priority_slice, ...fair_slice].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 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 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 async subscribe(\n streams: SubscribeInput[],\n correlated_at?: number,\n correlator?: { key: string; by: string; millis: number }\n ) {\n await sleep();\n\n // The checkpoint is monotonic. With a correlator, position and lease\n // are per key.\n let correlating: boolean | undefined;\n let keyed_at: number | undefined;\n if (correlator) {\n const now = Date.now();\n const held = this._correlators.get(correlator.key);\n // A key with no row yet inherits the shared position, so a new\n // correlator does not re-read history.\n const at = Math.max(held?.at ?? this._correlated_at, correlated_at ?? -1);\n keyed_at = at;\n if (!held || held.until < now || held.by === correlator.by) {\n correlating = true;\n this._correlators.set(correlator.key, {\n at,\n by: correlator.by,\n // Release outright, or a successor in the same ms is still refused.\n until: correlator.millis > 0 ? now + correlator.millis : 0,\n });\n } else {\n // Refused, but the position still advances: this caller may already\n // have scanned and marked.\n correlating = false;\n held.at = at;\n }\n // How far any correlator has read.\n if (correlated_at !== undefined && correlated_at > this._correlated_at)\n this._correlated_at = correlated_at;\n } else if (\n correlated_at !== undefined &&\n correlated_at > this._correlated_at\n )\n this._correlated_at = correlated_at;\n\n let subscribed = 0;\n for (const {\n stream,\n source,\n priority = 0,\n lane = DEFAULT_LANE,\n correlated_at,\n } of streams) {\n const existing = this._streams.get(stream);\n if (existing) {\n existing.resubscribe(priority, lane);\n if (correlated_at !== undefined) existing.mark(correlated_at);\n } else {\n const created = new InMemoryStream(stream, source, priority, lane);\n if (correlated_at !== undefined) created.mark(correlated_at);\n this._streams.set(stream, created);\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 {\n subscribed,\n watermark,\n correlated_at: keyed_at ?? this._correlated_at,\n ...(correlating === undefined ? {} : { correlating }),\n };\n }\n\n async ack(leases: Lease[]) {\n await sleep();\n // One synchronous pass: the in-memory form of the single transaction.\n return leases\n .map((l) => this._streams.get(l.stream)?.ack(l))\n .filter((l) => !!l);\n }\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 async defer(input: string[] | StreamFilter, deferred_at: number) {\n await sleep();\n const selected = this._select(input);\n for (const s of selected) s.defer(deferred_at);\n return selected.length;\n }\n\n // De-duplicated like the SQL `stream = ANY(...)`; unknown names skipped.\n private _select(input: string[] | StreamFilter): InMemoryStream[] {\n if (Array.isArray(input))\n return [...new Set(input)]\n .map((name) => this._streams.get(name))\n .filter((s) => s !== undefined);\n return [...this._streams.values()].filter(this._filter_predicate(input));\n }\n\n private _filter_predicate(\n filter: StreamFilter\n ): (s: InMemoryStream) => boolean {\n const stream_ok =\n filter.stream !== undefined\n ? name_test(filter.stream, filter.stream_exact)\n : undefined;\n const source_ok =\n filter.source !== undefined\n ? name_test(filter.source, filter.source_exact)\n : undefined;\n return (s) => {\n if (stream_ok && !stream_ok(s.stream)) return false;\n if (source_ok && (s.source === undefined || !source_ok(s.source)))\n return false;\n if (filter.blocked !== undefined && s.blocked !== filter.blocked)\n return false;\n if (filter.lane !== undefined && s.lane !== filter.lane) return false;\n return true;\n };\n }\n\n async reset(input: string[] | StreamFilter) {\n await sleep();\n const selected = this._select(input);\n for (const s of selected) s.reset();\n return selected.length;\n }\n\n async forget_pii(stream: string): Promise<number> {\n await sleep();\n const count = this._pii.get(stream)?.size ?? 0;\n this._pii.delete(stream);\n return count;\n }\n\n async unblock(input: string[] | StreamFilter) {\n await sleep();\n const selected = this._select(\n Array.isArray(input) ? input : { ...input, blocked: true }\n );\n return selected.filter((s) => s.unblock()).length;\n }\n\n async prioritize(filter: StreamFilter, priority: number) {\n await sleep();\n const changed = this._select(filter).filter((s) => s.priority !== priority);\n for (const s of changed) s.prioritize(priority);\n return changed.length;\n }\n\n async query_streams(\n callback: (position: StreamPosition) => void,\n query?: QueryStreams\n ): Promise<QueryStreamsResult> {\n await sleep();\n const limit = query?.limit ?? DEFAULT_QUERY_STREAMS_LIMIT;\n const after = query?.after;\n const source_matches = query?.source_matches;\n const matches = this._filter_predicate(query ?? {});\n // Reverse-match: the stored `source` pattern must match a requested name.\n const reverse_match = (source: string): boolean => {\n const re = new RegExp(source);\n return source_matches!.some((name) => re.test(name));\n };\n\n // The default sort is code-unit order, which agrees with the `<=`\n // cursor and the SQL binary collation; `localeCompare` would not.\n const sorted = [...this._streams.keys()]\n .sort()\n .map((stream) => this._streams.get(stream) as InMemoryStream);\n\n let count = 0;\n for (const s of sorted) {\n if (after !== undefined && s.stream <= after) continue;\n if (!matches(s)) continue;\n if (source_matches !== undefined) {\n // No source consumes every stream, so it matches any name.\n if (s.source && !reverse_match(s.source)) continue;\n }\n // Before emitting, so `limit: 0` returns none.\n if (count >= limit) break;\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 priority: s.priority,\n leased_by: s.leased_by,\n leased_until: s.leased_until,\n lane: s.lane,\n deferred_at: s.deferred_at,\n correlated_at: s.correlated_at,\n });\n count++;\n }\n return { maxEventId: this._events.at(-1)?.id ?? -1, count };\n }\n\n async query_stats<E extends Schemas>(\n input: string[] | Pick<StreamFilter, \"stream\" | \"stream_exact\">,\n options?: QueryStatsOptions<E>\n ): Promise<Map<string, StreamStats<E>>> {\n await sleep();\n const exclude = new Set<string>(options?.exclude ?? []);\n const want_tail = options?.tail ?? false;\n const want_count = options?.count ?? false;\n const want_names = options?.names ?? false;\n const before = options?.before;\n const after = options?.after;\n const limit = options?.limit;\n\n const array_targets = Array.isArray(input) ? new Set(input) : null;\n const stream_ok =\n !Array.isArray(input) && input.stream !== undefined\n ? name_test(input.stream, input.stream_exact)\n : undefined;\n\n const scope_cache = new Map<string, boolean>();\n const in_scope = (stream: string): boolean => {\n const cached = scope_cache.get(stream);\n if (cached !== undefined) return cached;\n const ok = array_targets\n ? array_targets.has(stream)\n : (stream_ok?.(stream) ?? true);\n scope_cache.set(stream, ok);\n return ok;\n };\n\n type Acc = {\n head: Committed<Schemas, keyof Schemas>;\n tail?: Committed<Schemas, keyof Schemas>;\n count: number;\n names?: Record<string, number>;\n };\n const acc = new Map<string, Acc>();\n for (const e of this._events) {\n if (before !== undefined && e.id >= before) continue;\n if (!in_scope(e.stream)) continue;\n if (exclude.has(e.name as string)) continue;\n let a = acc.get(e.stream);\n if (!a) {\n a = { head: e, count: 0 };\n if (want_tail) a.tail = e;\n if (want_names) a.names = {};\n acc.set(e.stream, a);\n }\n a.head = e;\n a.count++;\n if (want_names) {\n const n = String(e.name);\n a.names![n] = (a.names![n] ?? 0) + 1;\n }\n }\n\n // Code-unit order, to agree with the `<=` cursor (see query_streams).\n const ordered = [...acc.keys()].sort();\n const out = new Map<string, StreamStats<E>>();\n for (const stream of ordered) {\n if (limit !== undefined && out.size >= limit) break;\n if (after !== undefined && stream <= after) continue;\n const a = acc.get(stream)!;\n const stats: {\n head: Committed<Schemas, keyof Schemas>;\n tail?: Committed<Schemas, keyof Schemas>;\n count?: number;\n names?: Record<string, number>;\n } = { head: a.head };\n if (want_tail) stats.tail = a.tail;\n if (want_count) stats.count = a.count;\n if (want_names) stats.names = a.names;\n out.set(stream, stats as StreamStats<E>);\n }\n return out;\n }\n\n async truncate(\n targets: Array<{\n stream: string;\n snapshot?: Schema;\n meta?: EventMeta;\n before?: Date;\n max_id?: number;\n }>\n ) {\n await sleep();\n const result = new Map<\n string,\n {\n deleted: number;\n committed: Committed<Schemas, keyof Schemas>;\n before?: Date;\n }\n >();\n\n // Per target, like the SQL adapters: a later target sees what an\n // earlier one in the same call already pruned.\n const windowed = targets.filter((t) => t.before !== undefined);\n for (const { stream, before, max_id } of windowed) {\n let boundary: Committed<Schemas, keyof Schemas> | undefined;\n for (const e of this._events) {\n if (\n e.stream === stream &&\n e.name === SNAP_EVENT &&\n e.created < before! &&\n (max_id === undefined || e.id <= max_id) &&\n (!boundary || e.id > boundary.id)\n )\n boundary = e;\n }\n if (!boundary) continue; // no qualifying snapshot → no-op\n const below = boundary.id;\n let deleted = 0;\n this._events = this._events.filter((e) => {\n if (e.stream !== stream || e.id >= below) return true;\n this._pii.get(stream)?.delete(e.id);\n deleted++;\n return false;\n });\n result.set(stream, { deleted, committed: boundary, before });\n }\n\n const full = targets.filter((t) => t.before === undefined);\n // Per target, like the SQL adapters, so a stream listed twice gets one seed.\n for (const { stream, snapshot, meta } of full) {\n let deleted = 0;\n this._events = this._events.filter((e) => {\n if (e.stream !== stream) return true;\n deleted++;\n return false;\n });\n // Subscriptions untouched; `seed()` reaps inert ones.\n this._stream_versions.delete(stream);\n this._pii.delete(stream);\n const event: Committed<Schemas, keyof Schemas> = {\n id: this._next_id++,\n stream,\n version: 0,\n created: new Date(),\n ...seedEvent(snapshot, meta),\n };\n this._events.push(event);\n this._stream_versions.set(stream, 0);\n result.set(stream, { deleted, committed: event });\n }\n return result;\n }\n\n /** A throw inside the driver restores the captured state. Ids restart at 0. */\n async restore(\n driver: (\n callback: (event: Committed<Schemas, keyof Schemas>) => Promise<number>\n ) => Promise<void>\n ): Promise<void> {\n await sleep();\n const prev_events = this._events;\n const prev_next_id = this._next_id;\n const prev_streams = this._streams;\n const prev_stream_versions = this._stream_versions;\n const prev_pii = this._pii;\n const prev_correlated_at = this._correlated_at;\n const prev_correlators = new Map(\n [...this._correlators].map(([k, v]) => [k, { ...v }])\n );\n // Renumbered from 0, so checkpoints restart too; leases are kept.\n this._correlated_at = -1;\n for (const c of this._correlators.values()) c.at = -1;\n this._events = [];\n this._next_id = 0;\n this._streams = new Map();\n this._stream_versions = new Map();\n this._pii = new Map();\n // A stream holds each version once, as the SQL stores' unique index\n // enforces: a rename that maps two streams onto one name is refused.\n const seen = new Set<string>();\n try {\n await driver(async (event) => {\n const at = `${event.version}:${event.stream}`;\n if (seen.has(at))\n throw new Error(\n `restore: stream \"${event.stream}\" has version ${event.version} twice`\n );\n seen.add(at);\n const id = this._next_id++;\n // Split `pii` out like `commit`, so `forget_pii` erases restored PII.\n const { pii, ...rest } = event;\n const committed: Committed<Schemas, keyof Schemas> = { ...rest, id };\n this._events.push(committed);\n if (pii != null) this._put_pii(event.stream, id, pii);\n // Last event per stream wins.\n this._stream_versions.set(event.stream, event.version);\n return id;\n });\n } catch (err) {\n this._events = prev_events;\n this._next_id = prev_next_id;\n this._streams = prev_streams;\n this._stream_versions = prev_stream_versions;\n this._pii = prev_pii;\n this._correlated_at = prev_correlated_at;\n this._correlators = prev_correlators;\n throw err;\n }\n }\n}\n","import type { Disposer } from \"./types/index.js\";\n\n/**\n * The shape {@link register_weak_disposer} needs from a `WeakRef`. Declared\n * structurally so tests can hand in a stub that reports its target as\n * collected — real garbage collection is not schedulable, and a guarantee\n * about releasing memory deserves a deterministic test.\n */\nexport type RefLike<T extends object> = { deref: () => T | undefined };\n\n/**\n * A registered cleanup function, optionally tied to the lifetime of the\n * object it cleans up. `ref` present means \"run this only while its target\n * is still reachable\" — the entry holds no strong reference to that target,\n * so registering never keeps it alive. The target is handed back to `run`,\n * so a weak disposer never has to re-check what the registry just proved.\n */\ntype Entry = {\n readonly run: (target: never) => Promise<void>;\n readonly ref?: RefLike<object>;\n};\n\n/** Registered cleanup functions, executed in reverse order during shutdown. */\nconst entries: Entry[] = [];\n\n/**\n * Drop entries whose target has been collected.\n *\n * Called on every registration so the registry stays proportional to the\n * live objects rather than to every object ever built. That matters for apps\n * that mint short-lived Acts — one per tenant, per request, per test — where\n * the registry would otherwise grow without bound for the process lifetime.\n */\nconst prune = (): void => {\n for (let i = entries.length - 1; i >= 0; i--) {\n const { ref } = entries[i];\n if (ref && !ref.deref()) entries.splice(i, 1);\n }\n};\n\n/** Register a cleanup function that runs unconditionally at teardown. */\nexport const register_disposer = (run: Disposer): void => {\n prune();\n entries.push({ run });\n};\n\n/**\n * Register a cleanup function bound to `ref`'s target, held weakly.\n *\n * The registry never retains the target, so an object that becomes\n * unreachable is collectable whether or not it was cleaned up first, and its\n * entry is skipped at teardown — there is nothing left to clean up.\n */\nexport const register_weak_disposer = <T extends object>(\n ref: RefLike<T>,\n run: (target: T) => Promise<void>\n): void => {\n prune();\n entries.push({\n run: run as (target: never) => Promise<void>,\n ref: ref as RefLike<object>,\n });\n};\n\n/**\n * Run every live disposer in reverse registration order, sequentially, so a\n * disposer can rely on later-registered ones having already finished.\n *\n * A disposer that throws doesn't stop the others: every one runs, and the\n * failures are returned for the caller to report.\n *\n * Registrations are left in place, so a second teardown call re-runs them,\n * exactly as before this registry learned about weak entries.\n */\nexport const run_disposers = async (): Promise<unknown[]> => {\n const failures: unknown[] = [];\n for (const { run, ref } of [...entries].reverse()) {\n const target = ref?.deref();\n if (ref && !target) continue;\n try {\n await (ref ? run(target as never) : (run as Disposer)());\n } catch (error) {\n failures.push(error);\n }\n }\n return failures;\n};\n","/**\n * Every option bag act-core accepts, checked in one place. Each bag has an\n * internal schema below; `check(kind, options)` parses it, throws `ZodError`\n * on a bad value and returns the parsed, defaulted bag, which a caller that\n * only needs the throw ignores. Validation refuses only what is broken\n * (`NaN`, `±Infinity`, nonsensical negatives) and never newly refuses a\n * value that works. The rules are in .claude/rules/config-validation.md.\n */\n\nimport { z } from \"zod\";\nimport type { AutocloseConfig, CircuitBreakerOptions } from \"../types/act.js\";\nimport type { AutoclosePolicy, BackoffOptions } from \"../types/index.js\";\nimport { MAX_TIMER_DELAY_MS } from \"./defer-timer.js\";\n\n// Backoff — retry pacing, nested in the reaction and action bags. No\n// `maxMs >= baseMs` constraint: a cap below `baseMs` on `exponential` is\n// documented behavior.\nconst BackoffOptionsSchema = z.object({\n strategy: z.enum([\"fixed\", \"linear\", \"exponential\"]),\n baseMs: z.number().min(0),\n maxMs: z.number().gt(0).optional(),\n jitter: z.boolean().optional(),\n});\n\n/**\n * Retry pacing for a reaction that declares no backoff, and for projections\n * and failed fetches, which have no options. Ten seconds, like the default\n * lease; the lease itself only keeps two workers off one stream.\n */\nexport const DEFAULT_BACKOFF: BackoffOptions = {\n strategy: \"fixed\",\n baseMs: 10_000,\n};\n\n// Reaction — `.do(handler, options)`, fully defaulted by the builder.\n// `maxRetries` gates the block decision, so a `NaN` would retry a poison\n// message forever.\n\n/** Retries a reaction gets before it blocks, unless `.do()` says otherwise. */\nexport const DEFAULT_MAX_RETRIES = 3;\n/** Whether a reaction blocks its stream once retries run out. */\nexport const DEFAULT_BLOCK_ON_ERROR = true;\n\nconst ReactionOptionsSchema = z.object({\n blockOnError: z.boolean(),\n maxRetries: z.number().int().min(0),\n backoff: BackoffOptionsSchema.optional(),\n});\n\n// Action — `.on(entry, options)`. `maxRetries` ends the command retry loop,\n// so a `NaN` could spin it forever on a contended stream.\nconst ActionOptionsSchema = z.object({\n maxRetries: z.number().int().min(0).optional(),\n backoff: BackoffOptionsSchema.optional(),\n});\n\n// Target — `.to({ target, priority })`, a resolver's result and\n// `app.prioritize`. Postgres stores `priority` as a 32-bit `int`.\nconst TargetOptionsSchema = z.object({ priority: z.int32().optional() });\n\n// Restore — `app.restore(source, options)`. The stores read a falsy limit as\n// \"no limit\", so a NaN or 0 page reads the whole source at once, and a\n// negative or fractional one ends the scan early, after the sink was wiped.\nconst ScanOptionsSchema = z.object({\n batch_size: z.number().int().min(1).optional(),\n});\n\n// Audit — `app.audit(categories, options)`. A NaN cutoff fails every\n// comparison, so it would report a clean store.\nconst AuditOptionsSchema = z.object({\n thresholds: z\n .object({\n idle_days: z.number().min(0).optional(),\n restart_min: z.number().min(0).optional(),\n stuck_minutes: z.number().min(0).optional(),\n near_block: z.number().min(0).optional(),\n deprecated_min: z.number().min(0).optional(),\n drift_min: z.number().min(0).optional(),\n })\n .optional(),\n});\n\n// A timer's delay. Node runs a timer below 1 ms, above 2^31 − 1 ms or NaN\n// every millisecond, so every delay a bag sets a timer from is held to this.\nconst TimerMsSchema = z.number().min(0).max(MAX_TIMER_DELAY_MS);\n\n// Page — the `limit` and bounds of every query: `app.query`, `app.load`'s\n// `asOf`, `app.correlate`, `app.start_correlations`, `app.blocked_streams`\n// and settle's `correlate`. 0 stays (the poll reads it as its default); a NaN\n// limit read the whole log, and a NaN or Invalid Date bound was ignored by\n// one store and threw a raw driver error in another.\nconst LimitSchema = z.number().int().min(0).optional();\nconst PageOptionsSchema = z.object({\n limit: LimitSchema,\n before: z.number().optional(),\n after: z.number().optional(),\n created_before: z.date().optional(),\n created_after: z.date().optional(),\n});\n\n// Poll — `app.start_correlations`' frequency.\nconst PollOptionsSchema = z.object({\n frequency: TimerMsSchema,\n});\n\n// Drain / Settle — the knobs of `app.drain(...)` and `app.settle(...)`.\n\n/** Streams a drain claims per cycle. */\nexport const DEFAULT_STREAM_LIMIT = 10;\n/** Events a drain reads per stream per cycle. */\nexport const DEFAULT_EVENT_LIMIT = 10;\n/** How long a drain holds a stream's lease (ms). */\nexport const DEFAULT_LEASE_MILLIS = 10_000;\n\n// A drain claims at least one stream: 0 would claim nothing and read as\n// caught up, so the lane would never run.\nconst DrainOptionsSchema = z.object({\n streamLimit: z.number().int().min(1).optional(),\n eventLimit: z.number().int().min(0).optional(),\n leaseMillis: z.number().min(0).optional(),\n});\n\n// Lane — `.withLane({...})`, per-lane overrides of the drain budget.\nconst LaneConfigSchema = DrainOptionsSchema.pick({\n streamLimit: true,\n leaseMillis: true,\n}).extend({\n name: z.string().min(1),\n cycleMs: TimerMsSchema.optional(),\n});\n\nconst SettleOptionsSchema = DrainOptionsSchema.extend({\n debounceMs: TimerMsSchema.optional(),\n correlate: PageOptionsSchema.optional(),\n // `0` runs no passes; `Infinity` is the documented default (no cap), which\n // an integer check alone would refuse.\n maxPasses: z\n .union([z.number().int().min(0), z.literal(Number.POSITIVE_INFINITY)])\n .optional(),\n});\n\n// Shutdown — the grace budget `app.shutdown(...)` gives in-flight cycles.\n\n/**\n * Ceiling on the derived shutdown grace budget, so one parked handler on a\n * long-lease lane can't hold a rolling deploy open for that whole lease. 30s\n * is the longest `leaseMillis` the production checklist recommends.\n */\nexport const MAX_SHUTDOWN_GRACE_MS = 30_000;\n\n// `0` stops scheduling and returns without waiting.\nconst ShutdownOptionsSchema = z.object({\n graceMs: TimerMsSchema.optional(),\n});\n\n// Act — the scalar knobs of `act().build(options)`. The autoclose window and\n// the circuit breaker are their own bags below.\nconst ActOptionsSchema = z.object({\n maxSubscribedStreams: z.number().int().min(1).optional(),\n settleDebounceMs: TimerMsSchema.int().optional(),\n autoSettle: z.boolean().optional(),\n});\n\n// Autoclose window — `ActOptions.autocloseWindow`, the off-hours window. The\n// hour math stays in `autoclose-window.ts`.\n\n/** True when `tz` is a zone the runtime's `Intl` accepts. */\nfunction is_time_zone(tz: string): boolean {\n try {\n new Intl.DateTimeFormat(\"en-US\", { timeZone: tz });\n return true;\n } catch {\n return false;\n }\n}\n\nconst hour = (field: string) =>\n z\n .number()\n .int()\n .min(0)\n .max(23, {\n message: `autocloseWindow.${field} must be an hour in [0, 23]`,\n });\n\nconst AutocloseOptionsSchema = z.object({\n autocloseWindow: z\n .object({\n start: hour(\"start\"),\n end: hour(\"end\"),\n timeZone: z\n .string()\n .refine(is_time_zone, {\n message: \"autocloseWindow.timeZone must be a valid IANA time zone\",\n })\n .default(\"UTC\"),\n })\n .refine((w) => w.start !== w.end, {\n message:\n \"autocloseWindow.start and end must differ — an empty window disables autoclose\",\n })\n .optional(),\n}) satisfies z.ZodType<AutocloseConfig>;\n\n// Circuit breaker — `ActOptions.circuitBreaker`, the store-op breaker. The\n// state machine stays in `circuit-breaker.ts`.\nconst CircuitBreakerOptionsSchema = z.object({\n failureThreshold: z.number().int().min(1).default(5),\n cooldownMs: z.number().int().min(100).max(3_600_000).default(30_000),\n}) satisfies z.ZodType<CircuitBreakerConfig, CircuitBreakerOptions>;\n\n/** Resolved circuit-breaker configuration. */\nexport type CircuitBreakerConfig = Required<CircuitBreakerOptions>;\n\n// Fold — the batch-fold knobs of `projection(name).of(state, options)`.\nconst FoldOptionsSchema = z.object({\n flushEvery: z.number().int().min(1).default(1_000),\n maxCachedStates: z.number().int().min(1).default(10_000),\n});\n\n/** Resolved fold configuration. */\nexport type FoldConfig = z.infer<typeof FoldOptionsSchema>;\n\n// Defer — the literal schedule of `.defer(when)`, checked at build; the\n// function form is checked when it runs. Exactly one of `after` / `at`; a\n// duration's fields add up (`{ hours: 1, minutes: 30 }` is 90 minutes).\nconst DeferWhenSchema = z\n .object({\n after: z\n .object({\n days: z.number().positive().optional(),\n hours: z.number().positive().optional(),\n minutes: z.number().positive().optional(),\n })\n .strict()\n .refine((d) => d.days != null || d.hours != null || d.minutes != null, {\n message:\n \"defer: a duration needs at least one of days, hours, or minutes\",\n })\n .optional(),\n at: z.date().optional(),\n })\n .strict()\n .refine((w) => (w.after === undefined) !== (w.at === undefined), {\n message: \"defer: specify exactly one of `after` or `at`\",\n });\n\n// Autoclose policy — `.autocloses({...})`. Top-level fields AND together; an\n// `or` block is the alternative path, with the same fields minus `or`.\n// `.strict()` turns a typo into a build error. Windows are in days: `after`\n// is at least a minute and `keep` at least a day, since anything shorter is\n// a misconfiguration.\nconst window_schema = (\n field: \"after\" | \"keep\",\n min_days: number,\n floor: string\n) =>\n z\n .object({\n days: z\n .number({ message: `autocloses: ${field}.days must be a number` })\n .positive(`autocloses: ${field}.days must be > 0`),\n })\n .refine((o) => o.days >= min_days, {\n message: `autocloses: ${field} resolves to < ${floor}, which is too short to be a meaningful retention window`,\n });\n\nconst AfterSchema = window_schema(\"after\", 1 / 1440, \"one minute\");\n\nconst IsSchema = z.union([\n z.string().min(1, \"autocloses: is must be a non-empty event name\"),\n z\n .array(\n z.string().min(1, \"autocloses: is entries must be non-empty event names\")\n )\n .min(1, \"autocloses: is must include at least one event name\")\n .readonly(),\n]);\n\nconst ReachesSchema = z\n .number({ message: \"autocloses: reaches must be a number\" })\n .int(\"autocloses: reaches must be an integer\")\n .min(1, \"autocloses: reaches must be >= 1\");\n\nconst OrBlockSchema = z\n .object({\n after: AfterSchema.optional(),\n is: IsSchema.optional(),\n reaches: ReachesSchema.optional(),\n })\n .strict()\n .refine(\n (o) =>\n o.after !== undefined || o.is !== undefined || o.reaches !== undefined,\n {\n message:\n \"autocloses: `or` block must include at least one of after / is / reaches\",\n }\n );\n\nconst AutoclosePolicySchema = z\n .object({\n after: AfterSchema.optional(),\n is: IsSchema.optional(),\n reaches: ReachesSchema.optional(),\n or: OrBlockSchema.optional(),\n keep: window_schema(\"keep\", 1, \"one day\").optional(),\n })\n .strict()\n .refine(\n (o) =>\n o.after !== undefined ||\n o.is !== undefined ||\n o.reaches !== undefined ||\n o.or !== undefined ||\n o.keep !== undefined,\n {\n message:\n \"autocloses: at least one of after / is / reaches / or / keep must be specified — empty `{}` is a misconfiguration\",\n }\n ) satisfies z.ZodType<AutoclosePolicy>;\n\n// Store — `store(adapter)` and `ActOptions.scoped.store`. A store written for\n// `commit(stream, msgs, meta, expectedVersion)`, the shape before 2.0, would\n// forward its arguments and treat the appends as a stream name, sending\n// events and their sensitive fields wherever that name goes.\nconst StoreSchema = z.object({\n commit: z.custom<(...args: never[]) => unknown>(\n (commit) => typeof commit === \"function\" && commit.length !== 4,\n {\n message:\n \"This store's commit takes (stream, msgs, meta, expectedVersion), the shape before @rotorsoft/act 2.0. Upgrade it, and every @rotorsoft/act-* package, together: https://rotorsoft.github.io/act-root/docs/guides/migrating-to-2.x\",\n }\n ),\n});\n\nconst SCHEMAS = {\n act: ActOptionsSchema,\n action: ActionOptionsSchema,\n audit: AuditOptionsSchema,\n autoclose: AutocloseOptionsSchema,\n autoclose_policy: AutoclosePolicySchema,\n circuit_breaker: CircuitBreakerOptionsSchema,\n defer: DeferWhenSchema,\n drain: DrainOptionsSchema,\n fold: FoldOptionsSchema,\n lane: LaneConfigSchema,\n page: PageOptionsSchema,\n poll: PollOptionsSchema,\n reaction: ReactionOptionsSchema,\n scan: ScanOptionsSchema,\n settle: SettleOptionsSchema,\n shutdown: ShutdownOptionsSchema,\n store: StoreSchema,\n target: TargetOptionsSchema,\n};\n\ntype Schemas = typeof SCHEMAS;\n\n/** Parse an option bag with its schema. Throws `ZodError` on a bad value. */\nexport function check<K extends keyof Schemas>(\n kind: K,\n options: z.input<Schemas[K]>\n): z.output<Schemas[K]> {\n return SCHEMAS[kind].parse(options) as z.output<Schemas[K]>;\n}\n","/**\n * The shared \"next visit time\" primitive. A `DeferTimer` holds a\n * `stream → due-time` map and a single collapsed wake timer: it parks\n * streams that should be re-visited later and fires one `on_wake` callback\n * at the earliest pending due-time.\n *\n * The {@link \"drain-cycle\".DrainController} rides it for per-reaction\n * backoff (a retry's `next_attempt_at`) and for the `defer` outcome, which\n * holds a stream pending without advancing the watermark or bumping `retry`.\n * The wake runs a drain, and the drain re-schedules the timer when it ends.\n * An entry stays until its stream is acked or blocked, re-parked, or a\n * drain that started after it came due claims nothing ({@link prune}): a\n * wake that lands during another drain, or a pass that reaches only some of\n * the due streams, leaves the rest due, so the next re-schedule fires again\n * at once.\n *\n * Lives in process memory, per worker — the same per-worker pacing trade-off\n * documented for backoff. Durability comes from the data the due-time is\n * *derived* from (an un-advanced watermark, an event's `created` timestamp),\n * not from the map. A restart empties the map, so the cold-start rebuild is\n * explicit: `CorrelateCycle.init` seeds each still-future `deferred_at` back\n * onto the owning lane's timer via {@link \"drain-cycle\".DrainController.seed_defer},\n * so an idle deferred stream re-arms at its due-time with no\n * intervening commit.\n */\n\n/**\n * Node's `setTimeout` delay is a 32-bit signed int (~24.8 days). A larger\n * delay overflows and fires immediately, so we cap at this and re-arm.\n */\nexport const MAX_TIMER_DELAY_MS = 2_147_483_647;\n\n/** A linear scan finds the earliest entry: the map is bounded by claim limits. */\nexport class DeferTimer {\n private readonly _due = new Map<string, number>();\n private _timer: ReturnType<typeof setTimeout> | undefined;\n private readonly _on_wake: () => void;\n\n /**\n * @param on_wake - invoked once each time the earliest due-time elapses.\n * The consumer re-schedules for whatever is still parked.\n */\n constructor(on_wake: () => void) {\n this._on_wake = on_wake;\n }\n\n /** Number of currently parked streams. */\n get size(): number {\n return this._due.size;\n }\n\n /**\n * Park `stream` for a re-visit at `at` (ms since epoch). A plain\n * overwrite: the caller computes the authoritative next-visit for the\n * stream (the drain's `handle` already reconciles a stream's reactions\n * into one result per cycle), so there is no stale value to merge against.\n */\n set(stream: string, at: number): void {\n this._due.set(stream, at);\n }\n\n /** Drop `stream` from the parked set (e.g. on a successful ack or block). */\n delete(stream: string): void {\n this._due.delete(stream);\n }\n\n /**\n * Drop every entry due at or before `before`. The drain calls it when a\n * pass that started at `before` claimed nothing: whatever was due by then\n * was handled elsewhere (another worker) or has no work left, so waking\n * for it again would spin.\n */\n prune(before: number): void {\n for (const [stream, at] of this._due)\n if (at <= before) this._due.delete(stream);\n }\n\n /**\n * (Re)schedule the wake timer at the earliest pending due-time. Idempotent\n * — collapses many parked streams into a single timer. A no-op clears any\n * pending timer when the map is empty.\n *\n * The timer is `unref()`-ed so pending re-visits never keep the process\n * alive on their own.\n */\n schedule(): void {\n if (this._timer) clearTimeout(this._timer);\n if (this._due.size === 0) {\n this._timer = undefined;\n return;\n }\n let earliest = Number.POSITIVE_INFINITY;\n for (const t of this._due.values()) if (t < earliest) earliest = t;\n // A longer due-time (a 90-day autoclose cooldown) wakes at the ceiling;\n // the entry stays and the consumer re-schedules for the rest.\n const delay = Math.min(\n Math.max(0, earliest - Date.now()),\n MAX_TIMER_DELAY_MS\n );\n this._timer = setTimeout(() => {\n this._timer = undefined;\n this._on_wake();\n }, delay);\n this._timer.unref();\n }\n\n /** Cancel any pending wake timer. Idempotent. Leaves the parked set intact. */\n stop(): void {\n if (this._timer) {\n clearTimeout(this._timer);\n this._timer = undefined;\n }\n }\n}\n","/**\n * @module scoped\n * @category Internal\n *\n * Ambient execution context — every `AsyncLocalStorage` the framework owns\n * lives here, and so does every operation on one. No other module calls\n * `.run()` or `.getStore()`; they ask for a runner or a reader instead, so\n * this file is the single place to look at the abstraction.\n *\n * Two contexts:\n *\n * - **ports** — the active Act's store/cache bag, so `store()` / `cache()`\n * resolve per-Act rather than per-process. Installed by the\n * orchestrator via {@link make_run_scoped}, read by the port singletons\n * via {@link current_ports}.\n * - **reaction** — the event a handler is processing, so a dispatch made\n * anywhere inside that handler can thread `reactingTo` whichever `IAct`\n * reference made the call. Installed via {@link run_reacting},\n * read at the orchestrator boundary via {@link current_reacting}.\n *\n * Keeping the mechanics here keeps ambient state out of `internal/`, whose\n * modules are stateless implementations that receive what they need.\n *\n * @internal\n */\n\nimport { AsyncLocalStorage } from \"node:async_hooks\";\nimport type { Actor, Committed, IAct, Schemas, Scoped } from \"./types/index.js\";\n\n/**\n * What an Act actually installs in its frame: the ports a caller passed,\n * plus per-Act settings that runtime code has to read from the *running*\n * Act rather than from whatever closure it was built in.\n *\n * `autoclose_window` is here because the autoclose reactions are\n * synthesized once into the shared registry and handed by reference to\n * every Act built from that builder, so a window captured at synthesis is\n * the first tenant's window for everyone. The frame is per-Act, so\n * reading it here is what makes the setting per-Act too.\n *\n * Wider than {@link Scoped} on purpose: `Scoped` types what a caller\n * *passes* through `ActOptions.scoped` and is public; this is what the\n * orchestrator *installs* and is not.\n *\n * @internal\n */\nexport type ActFrame = Scoped & {\n readonly autoclose_window?: {\n readonly start: number;\n readonly end: number;\n readonly timeZone: string;\n };\n};\n\n/**\n * AsyncLocalStorage carrying the active Act's ports.\n *\n * Exported only for the scope-overhead bench; no entry point re-exports it.\n * Everything else uses the helpers below.\n */\nexport const scoped = new AsyncLocalStorage<ActFrame>();\n\n/**\n * The reaction currently running, as a box the handler can empty.\n *\n * A frame captured by work the handler started and did not await outlives the\n * handler and can never be unbound. The *frame* is unreclaimable, but the\n * *box inside it* is not: clearing `event` as the handler settles turns every\n * later read through that frame into \"no reaction\", without anyone having to\n * ask a second question.\n *\n * Not reachable from `index.ts`.\n */\ntype Reacting = { event: Committed<Schemas, string> | undefined };\n\nconst reacting = new AsyncLocalStorage<Reacting>();\n\n/**\n * Builds the runner an Act uses to enter its own port scope.\n *\n * Every Act gets a frame, including one built without `ActOptions.scoped` —\n * that one carries the singleton adapters (see `unscoped_ports` in `ports.ts`).\n * Entering unconditionally is what makes an Act's ports its own: a runner that\n * collapsed to `fn()` for a singleton Act did not leave whatever frame it was\n * called from, so dispatching into a shared Act from inside a tenant's handler\n * resolved `store()` to that tenant and wrote the shared Act's events into the\n * tenant's log.\n *\n * @internal\n */\nexport function make_run_scoped(\n bag: ActFrame\n): <T>(fn: () => Promise<T>) => Promise<T> {\n return (fn) => scoped.run(bag, fn);\n}\n\n/**\n * The running Act's off-hours autoclose window, or `undefined` when it\n * declared none (or when read outside any Act).\n *\n * Read at resolution time rather than captured at synthesis: the autoclose\n * reactions belong to the shared registry, and only the frame knows which\n * Act is running them.\n *\n * @internal\n */\nexport function current_autoclose_window(): ActFrame[\"autoclose_window\"] {\n return scoped.getStore()?.autoclose_window;\n}\n\n/**\n * The active Act's ports, or `undefined` outside any Act.\n *\n * Every Act runs in a frame, so this is `undefined` only for a call made\n * outside one — `store()` and `cache()` fall back to the singleton adapters\n * there, which is what a bare `store()` in application setup expects.\n *\n * @internal\n */\nexport function current_ports(): Scoped | undefined {\n return scoped.getStore();\n}\n\n/**\n * Runs `fn` with `event` installed as the triggering-event context.\n *\n * Entered per payload rather than per lease: the context has to unwind with\n * the handler so it never reaches the drain cycle, and work a handler started\n * without awaiting has to resume into its own event's frame.\n *\n * @internal\n */\nfunction run_reacting<T>(\n event: Committed<Schemas, string>,\n fn: () => Promise<T>\n): Promise<T> {\n const box: Reacting = { event };\n return reacting.run(box, async () => {\n try {\n return await fn();\n } finally {\n // Anything still running keeps this frame; from here it reads empty.\n box.event = undefined;\n }\n });\n}\n\n/**\n * The event being reacted to, or `undefined` outside a running handler —\n * including inside work that outlived one.\n *\n * @internal\n */\nexport function current_reacting(): Committed<Schemas, string> | undefined {\n return reacting.getStore()?.event;\n}\n\n/**\n * Everything a reaction handler runs inside: the `IAct` facade it is handed\n * as its third argument, and the triggering-event context that every dispatch\n * made within it resolves — including one through a captured `app`.\n *\n * Built here rather than in the dispatcher so the dispatcher never has to\n * know how either half works; it receives this whole and calls it.\n *\n * @internal\n */\nexport type ReactionScope<\n TEvents extends Schemas,\n TActions extends Schemas,\n TActor extends Actor = Actor,\n> = {\n readonly app: IAct<TEvents, TActions, TActor>;\n readonly run: <T>(\n event: Committed<Schemas, string>,\n fn: () => Promise<T>\n ) => Promise<T>;\n};\n\n/**\n * Assembles the reaction scope from the orchestrator's bound `IAct` methods.\n *\n * @internal\n */\nexport function make_reaction_scope<\n TEvents extends Schemas,\n TActions extends Schemas,\n TActor extends Actor = Actor,\n>(\n app: IAct<TEvents, TActions, TActor>\n): ReactionScope<TEvents, TActions, TActor> {\n return { app, run: run_reacting };\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 { register_disposer, run_disposers } from \"./disposers.js\";\nimport { check } from \"./internal/config.js\";\nimport { current_ports } from \"./scoped.js\";\nimport type {\n Cache,\n Disposable,\n Disposer,\n ExitCode,\n Logger,\n Scoped,\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// 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 * @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 * @see {@link Store} for the interface contract\n * @see {@link InMemoryStore} for the default implementation\n */\n// ALS check lives outside `port()` — its cache fires only once, so the\n// per-call branch on a scoped Act has to be in the public wrapper.\nconst _store = port(function store(adapter?: Store): Store {\n if (adapter) check(\"store\", adapter);\n return adapter ?? new InMemoryStore();\n});\n\nexport const store = ((adapter?: Store): Store => {\n return current_ports()?.store ?? _store(adapter);\n}) as (adapter?: Store) => Store;\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 */\nconst _cache = port(function cache(adapter?: Cache) {\n return adapter ?? new InMemoryCache();\n});\n\nexport const cache = ((adapter?: Cache): Cache => {\n return current_ports()?.cache ?? _cache(adapter);\n}) as (adapter?: Cache) => Cache;\n\n/**\n * The ports bag an Act without `ActOptions.scoped` runs in: the singleton\n * adapters, read through the same frame a scoped Act uses.\n *\n * Every Act entering a frame is what keeps its ports its own. Without one, a\n * shared Act called from inside a tenant's handler inherited that tenant's\n * frame and committed to the tenant's store.\n *\n * The properties are getters on purpose. The adapters are resolved lazily and\n * injected after `act().build()` in the normal case — `store(new PgStore())`\n * in application setup, or a test's `beforeEach` — so capturing them when the\n * bag is made would pin whatever existed at build time. They read the raw\n * resolvers rather than the public `store()`/`cache()`, which consult the\n * frame this bag *is* and would recurse.\n */\nexport const unscoped_ports = (): Scoped => DEFAULT_SCOPE;\n\nconst DEFAULT_SCOPE: Scoped = {\n get store() {\n return _store();\n },\n get cache() {\n return _cache();\n },\n};\n\n// ---------------------------------------------------------------------------\n// Disposal\n// ---------------------------------------------------------------------------\n\n// Registered cleanup functions live in `disposers.ts`, which holds\n// lifetime-bound entries weakly so registering never pins its target.\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), with 1 when any of them threw:\n * a failure is logged and the rest still run\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. A failure is\n // logged and the rest still run, so the process always gets to exit.\n const failures = await run_disposers();\n for (const adapter of [...adapters.values()].reverse()) {\n try {\n await adapter.dispose();\n log().info(`[act] - ${adapter.constructor.name}`);\n } catch (error) {\n failures.push(error);\n }\n }\n adapters.clear();\n for (const error of failures) log().error(error, \"Dispose failed\");\n const failed = code === \"ERROR\" || failures.length > 0;\n config().env !== \"test\" && process.exit(failed ? 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 && register_disposer(disposer);\n return disposeAndExit;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAYA,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,QAAQ;AAEd,IAAM,OAAO,6BAAM;AAAC,GAAP;AAUN,IAAM,gBAAN,MAAM,eAAgC;AAAA,EA1C7C,OA0C6C;AAAA;AAAA;AAAA,EAC3C;AAAA,EACiB;AAAA,EAER;AAAA,EACA;AAAA,EACA;AAAA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AiB,OAAO;AAAA,MACxE;AACA,uBAAiB;AAAA,IACnB;AAAA,EACF;AACA,SAAO;AACT,GAzBsB;","names":["z","correlated_at","log","store","cache","z","pkg"]}
|
|
@@ -820,8 +820,15 @@ var InMemoryStore = (_class3 = class {constructor() { _class3.prototype.__init10
|
|
|
820
820
|
this._streams = /* @__PURE__ */ new Map();
|
|
821
821
|
this._stream_versions = /* @__PURE__ */ new Map();
|
|
822
822
|
this._pii = /* @__PURE__ */ new Map();
|
|
823
|
+
const seen = /* @__PURE__ */ new Set();
|
|
823
824
|
try {
|
|
824
825
|
await driver(async (event) => {
|
|
826
|
+
const at = `${event.version}:${event.stream}`;
|
|
827
|
+
if (seen.has(at))
|
|
828
|
+
throw new Error(
|
|
829
|
+
`restore: stream "${event.stream}" has version ${event.version} twice`
|
|
830
|
+
);
|
|
831
|
+
seen.add(at);
|
|
825
832
|
const id = this._next_id++;
|
|
826
833
|
const { pii, ...rest } = event;
|
|
827
834
|
const committed = { ...rest, id };
|
|
@@ -1347,4 +1354,4 @@ var config = /* @__PURE__ */ _chunk4GA3CHQWcjs.__name.call(void 0, () => {
|
|
|
1347
1354
|
|
|
1348
1355
|
|
|
1349
1356
|
exports.ConsoleLogger = ConsoleLogger; exports.LruMap = LruMap; exports.InMemoryCache = InMemoryCache; exports.package_schema = package_schema; exports.config = config; exports.validate = validate; exports.sleep = sleep; exports.InMemoryStore = InMemoryStore; exports.register_weak_disposer = register_weak_disposer; exports.DeferTimer = DeferTimer; exports.DEFAULT_BACKOFF = DEFAULT_BACKOFF; exports.DEFAULT_MAX_RETRIES = DEFAULT_MAX_RETRIES; exports.DEFAULT_BLOCK_ON_ERROR = DEFAULT_BLOCK_ON_ERROR; exports.DEFAULT_STREAM_LIMIT = DEFAULT_STREAM_LIMIT; exports.DEFAULT_EVENT_LIMIT = DEFAULT_EVENT_LIMIT; exports.DEFAULT_LEASE_MILLIS = DEFAULT_LEASE_MILLIS; exports.MAX_SHUTDOWN_GRACE_MS = MAX_SHUTDOWN_GRACE_MS; exports.check = check; exports.make_run_scoped = make_run_scoped; exports.current_autoclose_window = current_autoclose_window; exports.current_reacting = current_reacting; exports.make_reaction_scope = make_reaction_scope; exports.port = port; exports.log = log; exports.store = store2; exports.cache = cache2; exports.unscoped_ports = unscoped_ports; exports.disposeAndExit = disposeAndExit; exports.dispose = dispose;
|
|
1350
|
-
//# sourceMappingURL=chunk-
|
|
1357
|
+
//# sourceMappingURL=chunk-UYNNDDE7.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/act-root/act-root/libs/act/dist/chunk-UYNNDDE7.cjs","../src/adapters/console-logger.ts","../src/internal/lru-map.ts","../src/adapters/in-memory-cache.ts","../src/config.ts","../src/utils.ts","../src/adapters/in-memory-store.ts","../src/disposers.ts","../src/internal/config.ts","../src/internal/defer-timer.ts","../src/scoped.ts","../src/ports.ts"],"names":["z","correlated_at","pkg"],"mappings":"AAAA;AACE;AACA;AACA;AACA;AACF,wDAA6B;AAC7B;AACE;AACA;AACA;AACA;AACA;AACA;AACA;AACF,wDAA6B;AAC7B;AACA;ACJA,IAAM,aAAA,EAAuC;AAAA,EAC3C,KAAA,EAAO,EAAA;AAAA,EACP,KAAA,EAAO,EAAA;AAAA,EACP,IAAA,EAAM,EAAA;AAAA,EACN,IAAA,EAAM,EAAA;AAAA,EACN,KAAA,EAAO,EAAA;AAAA,EACP,KAAA,EAAO;AACT,CAAA;AAEA,IAAM,aAAA,EAAuC;AAAA,EAC3C,KAAA,EAAO,kBAAA;AAAA;AAAA,EACP,KAAA,EAAO,UAAA;AAAA;AAAA,EACP,IAAA,EAAM,UAAA;AAAA;AAAA,EACN,IAAA,EAAM,UAAA;AAAA;AAAA,EACN,KAAA,EAAO,UAAA;AAAA;AAAA,EACP,KAAA,EAAO;AAAA;AACT,CAAA;AAEA,IAAM,MAAA,EAAQ,SAAA;AAEd,IAAM,KAAA,kBAAO,sCAAA,CAAA,EAAA,GAAM;AAAC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;AAmBT;AAEP;AACQ,EAAA;AACH,IAAA;AACT,EAAA;AACY,EAAA;AACH,IAAA;AACT,EAAA;AACF;AAyBsB;AACP,EAAA;AAIL,IAAA;AACJ,MAAA;AACF,IAAA;AACA,IAAA;AACF,EAAA;AAMM,EAAA;AACK,EAAA;AACL,IAAA;AACI,MAAA;AACK,MAAA;AACJ,IAAA;AACE,MAAA;AACX,IAAA;AACF,EAAA;AACe,EAAA;AACJ,EAAA;AACI,EAAA;AACN,EAAA;AACX;AA5BsB;AAuDpB;AAEY,EAAA;AACL,EAAA;AACT;AALgB;AX49BC;AACA;AIttCJ;AACH,EAAA;AACG,EAAA;AACED,EAAAA;AAEV,EAAA;AAIQ,EAAA;AACGA,EAAAA;AACf;AAeK;AACE,EAAA;AACG,EAAA;AACI,EAAA;AACf;AAcM;AACA,EAAA;AACU,IAAA;AACyB,IAAA;AAC7B,MAAA;AACG,MAAA;AACX,IAAA;AACY,IAAA;AACJ,MAAA;AACI,MAAA;AACZ,IAAA;AACOE,IAAAA;AACK,EAAA;AACZ,IAAA;AACO,IAAA;AACT,EAAA;AAfkB;AAmBhB;AAOe;AACL,EAAA;AACA,EAAA;AACGF,EAAAA;AACJ,EAAA;AACZ;AAEiB;AAEL;AACK;AAMZ;AACU;AAEJ;AAMR;AAiCkB;AACf,EAAA;AACU,IAAA;AACX,MAAA;AACU,MAAA;AACV,MAAA;AACF,IAAA;AACI,IAAA;AAII,MAAA;AAMA,MAAA;AACJ,QAAA;AAEF,MAAA;AACA,MAAA;AACF,IAAA;AACF,EAAA;AACO,EAAA;AAxBa;AJopCL;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA","file":"/home/runner/work/act-root/act-root/libs/act/dist/chunk-UYNNDDE7.cjs","sourcesContent":[null,"/**\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._pretty_write.bind(this, bindings)\n : this._json_write.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 _json_write(\n bindings: Record<string, unknown> | undefined,\n level: string,\n _num: number,\n obj_or_msg: unknown,\n msg?: string\n ): void {\n let obj: Record<string, unknown>;\n let message: string | undefined;\n\n if (typeof obj_or_msg === \"string\") {\n message = obj_or_msg;\n obj = {};\n } else if (obj_or_msg instanceof Error) {\n // Error instances spread to `{}` — capture the salient fields\n // explicitly so structured log aggregators see them.\n message = msg ?? obj_or_msg.message;\n obj = {\n error: { message: obj_or_msg.message, name: obj_or_msg.name },\n stack: obj_or_msg.stack,\n };\n } else if (obj_or_msg !== null && typeof obj_or_msg === \"object\") {\n message = msg;\n obj = { ...(obj_or_msg as Record<string, unknown>) };\n } else {\n message = msg;\n obj = { value: obj_or_msg };\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 _pretty_write(\n bindings: Record<string, unknown> | undefined,\n level: string,\n _num: number,\n obj_or_msg: 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 obj_or_msg === \"string\") {\n message = obj_or_msg;\n } else if (obj_or_msg instanceof Error) {\n // Error instances don't serialize their `message`/`stack` via\n // JSON.stringify — `JSON.stringify(err) === \"{}\"`. Render the\n // message inline and the stack as the data payload so operators\n // see something useful instead of an empty object.\n message = msg ?? obj_or_msg.message;\n data = obj_or_msg.stack;\n } else {\n message = msg ?? \"\";\n if (obj_or_msg !== undefined && obj_or_msg !== null) {\n try {\n data = JSON.stringify(obj_or_msg);\n } catch {\n data = \"[unserializable]\";\n }\n }\n }\n\n const bind_str =\n bindings && Object.keys(bindings).length\n ? ` ${JSON.stringify(bindings)}`\n : \"\";\n\n const parts = [ts, tag, message, data, bind_str].filter(Boolean);\n process.stdout.write(parts.join(\" \") + \"\\n\");\n }\n}\n","/**\n * Tiny bounded LRU map built on insertion-ordered `Map`. Caps memory in\n * long-running orchestrators that mint large numbers of keys:\n *\n * - {@link InMemoryCache}: stream → state checkpoint\n * - correlate's dynamic subscriptions (stream → priority and lane) and its\n * compiled source patterns\n *\n * Apps with millions of dynamic streams (one target per aggregate) can't\n * afford an unbounded map: eviction is required.\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 */\nexport class LruMap<K, V> {\n private readonly _entries = new Map<K, V>();\n private readonly _max_size: number;\n\n constructor(maxSize: number) {\n this._max_size = maxSize;\n }\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._max_size) {\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","import { LruMap } from \"../internal/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 */\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","/**\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 Config,\n type Environment,\n Environments,\n type LogLevel,\n LogLevels,\n type Package,\n} from \"./types/index.js\";\nimport { validate } from \"./utils.js\";\n\n/**\n * Zod schema for the package.json fields {@link config} validates.\n */\nexport const package_schema = 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}) satisfies z.ZodType<Package>;\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. `name` and `version` also fall back one by\n * one when the file has no valid value for them.\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 the local package.json as a Package object, keeping each field only\n * when it is valid: `npm init -y` writes an empty `description` and\n * `author`, and a bare `{\"type\":\"module\"}` has no name or version. A\n * missing or invalid `name` / `version` falls back to\n * {@link FALLBACK_PACKAGE}. On a read or parse failure, falls back entirely\n * and 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 get_package = (): Package => {\n try {\n const raw = JSON.parse(fs.readFileSync(\"package.json\").toString());\n const pkg: Record<string, unknown> = {\n name: FALLBACK_PACKAGE.name,\n version: FALLBACK_PACKAGE.version,\n };\n for (const [key, schema] of Object.entries(package_schema.shape)) {\n const field = schema.safeParse(raw?.[key]);\n if (field.success && field.data !== undefined) pkg[key] = field.data;\n }\n return pkg as Package;\n } catch (err) {\n pkg_load_error = err;\n return FALLBACK_PACKAGE;\n }\n};\n\n/** Stashed read/parse error from {@link get_package}, surfaced by config(). */\nlet pkg_load_error: 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 = package_schema.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}) satisfies z.ZodType<Config>;\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 = get_package();\n\n// Lazily validated on first call. Cannot run validate() 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 * @see {@link Config} for configuration type\n */\nexport const config = (): Config => {\n if (!_validated) {\n _validated = validate(\n \"config\",\n { ...pkg, env, logLevel, logSingleLine, sleepMs },\n BaseSchema\n );\n if (pkg_load_error) {\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 pkg_load_error instanceof Error\n ? pkg_load_error.message\n : typeof pkg_load_error === \"string\"\n ? pkg_load_error\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 pkg_load_error = undefined;\n }\n }\n return _validated;\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 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 * 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 * @module act/adapters\n * In-memory event store adapter for development and tests.\n *\n * @category Adapters\n */\n\nimport {\n checkAppends,\n checkVersion,\n fairSlots,\n seedEvent,\n} from \"../adapter.js\";\nimport type {\n Append,\n BlockedLease,\n Committed,\n EventMeta,\n Lease,\n Query,\n QueryStatsOptions,\n QueryStreams,\n QueryStreamsResult,\n Schema,\n Schemas,\n Store,\n StreamFilter,\n StreamPosition,\n StreamStats,\n SubscribeInput,\n} from \"../types/index.js\";\nimport {\n DEFAULT_LANE,\n DEFAULT_QUERY_STREAMS_LIMIT,\n SNAP_EVENT,\n} from \"../types/index.js\";\nimport { sleep } from \"../utils.js\";\n\n/**\n * One subscription row: watermark, lease, retry and schedule. Its state is\n * private and changes only through the methods below, which carry the\n * row's rules; the store reads it through the getters.\n */\nclass InMemoryStream {\n readonly stream: string;\n readonly source: string | undefined;\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 private _priority: number;\n private _lane: string;\n // While in the future, the stream is held out of `claim`.\n private _deferred_at: number | undefined = undefined;\n // Work mark; `undefined` = never correlated, so `claim` skips it.\n private _correlated_at: number | undefined = undefined;\n\n constructor(\n stream: string,\n source: string | undefined,\n priority = 0,\n lane: string = DEFAULT_LANE\n ) {\n this.stream = stream;\n this.source = source;\n this._priority = priority;\n this._lane = lane;\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 get priority() {\n return this._priority;\n }\n\n get lane() {\n return this._lane;\n }\n\n get deferred_at() {\n return this._deferred_at;\n }\n\n get correlated_at() {\n return this._correlated_at;\n }\n\n get is_available() {\n return (\n !this._blocked &&\n (!this._leased_until || this._leased_until <= new Date()) &&\n (!this._deferred_at || this._deferred_at <= Date.now())\n );\n }\n\n /**\n * A later subscribe of this row keeps the maximum priority, and takes the\n * new lane only at or above the stored priority (compared before the\n * bump), so a lower-priority resolution can't re-lane the stream. The\n * lane is replaced on every qualifying subscribe, so the current config\n * wins on restart.\n */\n resubscribe(priority: number, lane: string) {\n if (priority >= this._priority) this._lane = lane;\n if (priority > this._priority) this._priority = priority;\n }\n\n /** Operator override; ignores the subscribe-time `max()`. */\n prioritize(priority: number) {\n this._priority = priority;\n }\n\n /** A mark never regresses, including at zero and negatives. */\n mark(correlated_at: number) {\n if (\n this._correlated_at === undefined ||\n correlated_at > this._correlated_at\n )\n this._correlated_at = correlated_at;\n }\n\n defer(deferred_at: number) {\n this._deferred_at = deferred_at;\n // A defer is not a failure, so the redelivery is a fresh attempt.\n this._retry = -1;\n }\n\n // The lease this row hands back, at the given watermark.\n private _lease(lease: Lease, at: number) {\n return {\n stream: this.stream,\n source: this.source,\n at,\n by: lease.by,\n retry: this._retry,\n lagging: lease.lagging,\n lane: this._lane,\n };\n }\n\n lease(lease: Lease, millis: number): Lease {\n // The holder is recorded even at zero `millis`: `ack` is gated on it,\n // so a lease with no holder would drop every ack.\n this._leased_by = lease.by;\n this._leased_until = new Date(Date.now() + millis);\n this._retry = this._retry + 1;\n return this._lease(lease, lease.at);\n }\n\n /** Ack, or with `due`, also persist the schedule. See {@link Store.ack}. */\n ack(lease: Lease) {\n if (this._leased_by === lease.by) {\n this._leased_by = undefined;\n this._leased_until = undefined;\n if (lease.due !== undefined) {\n // Advance past the handled prefix and schedule the re-visit with the\n // caller's retry (-1 for a defer, the climbing count for a backoff).\n this._at = lease.at;\n this._retry = lease.retry;\n this._deferred_at = lease.due;\n return undefined;\n }\n this._retry = -1;\n this._at = lease.at;\n this._deferred_at = undefined;\n return this._lease(lease, this._at);\n }\n }\n\n block(lease: Lease, error: string) {\n // Re-blocking is a no-op, like the SQL `AND blocked = false` guard.\n if (this._leased_by === lease.by && !this._blocked) {\n this._blocked = true;\n this._error = error;\n this._deferred_at = undefined;\n return { ...this._lease(lease, this._at), error };\n }\n }\n\n // Clears the block, the lease and the schedule; retry back to -1.\n private _release() {\n this._retry = -1;\n this._blocked = false;\n this._error = \"\";\n this._leased_by = undefined;\n this._leased_until = undefined;\n this._deferred_at = undefined;\n }\n\n /** Retry resets to -1 (released); the next claim makes it 0. */\n reset() {\n this._at = -1;\n this._release();\n }\n\n /** True only when the stream was blocked. */\n unblock(): boolean {\n if (!this._blocked) return false;\n this._release();\n return true;\n }\n}\n\n/**\n * Tests a name against a stream or source filter: exact, or a regex\n * compiled once.\n */\nconst name_test = (\n pattern: string,\n exact?: boolean\n): ((name: string) => boolean) => {\n if (exact) return (name) => name === pattern;\n const re = new RegExp(pattern);\n return (name) => re.test(name);\n};\n\n/**\n * In-memory event store: the default when no other store is injected.\n * For development, tests and examples; all data is lost when the process\n * exits. Use {@link PostgresStore} in production.\n *\n * `Store.notify` is not implemented: a single process has no remote writer\n * to hear from, and local commits already arm the drain.\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 * @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 private _events: Committed<Schemas, keyof Schemas>[] = [];\n // Never reused. Deletions punch holes, so ids are not array indexes.\n private _next_id = 0;\n private _streams: Map<string, InMemoryStream> = new Map();\n // Correlate checkpoint: how far the log has been READ.\n private _correlated_at = -1;\n // Per-correlator checkpoint and lease; keyless callers use `_correlated_at`.\n private _correlators = new Map<\n string,\n { at: number; by: string; until: number }\n >();\n private _stream_versions: Map<string, number> = new Map();\n // stream → (event_id → cloned pii). Two levels so `forget_pii` drops one\n // inner Map. Absence means no PII.\n private _pii: Map<string, Map<number, Record<string, unknown>>> = new Map();\n\n private _reset_indexes() {\n this._events.length = 0;\n this._next_id = 0;\n this._correlated_at = -1;\n this._stream_versions.clear();\n this._pii.clear();\n }\n\n // Stores a cloned copy, so caller-side mutation can't reach it.\n private _put_pii(stream: string, id: number, pii: Record<string, unknown>) {\n let per_stream = this._pii.get(stream);\n if (!per_stream) {\n per_stream = new Map();\n this._pii.set(stream, per_stream);\n }\n per_stream.set(id, structuredClone(pii));\n }\n\n // First index whose id is greater than `after` (ids are sorted, not dense).\n private _first_index_after(after: number): number {\n let lo = 0;\n let hi = this._events.length;\n while (lo < hi) {\n const mid = (lo + hi) >>> 1;\n if (this._events[mid].id > after) hi = mid;\n else lo = mid + 1;\n }\n return lo;\n }\n\n // Allocation-free for events without PII, the common case.\n private _with_pii<E extends Schemas>(\n e: Committed<E, keyof E>,\n wanted = true\n ): Committed<E, keyof E> {\n if (!wanted) return e;\n const pii = this._pii.get(e.stream)?.get(e.id);\n return pii ? ({ ...e, pii } as Committed<E, keyof E>) : e;\n }\n\n async dispose() {\n await sleep();\n this._reset_indexes();\n }\n\n async seed() {\n await sleep();\n }\n\n async drop() {\n await sleep();\n this._reset_indexes();\n this._streams = new Map();\n }\n\n private _in_query(\n query: Query\n ): (e: Committed<Schemas, keyof Schemas>) => boolean {\n const stream_ok = query.stream\n ? name_test(query.stream, query.stream_exact)\n : undefined;\n return (e) => {\n if (stream_ok && !stream_ok(e.stream)) return false;\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 // `created` is not monotonic with `id` (restore keeps it), so time\n // bounds filter; they never end the scan.\n if (query.created_after && e.created <= query.created_after) return false;\n if (query.created_before && e.created >= query.created_before)\n return false;\n return true;\n };\n }\n\n // Index of the stream's latest snapshot, or -1; an explicit `after` wins.\n private _snap_floor(query: Query | undefined): number {\n if (\n !query?.with_snaps ||\n !query.stream_exact ||\n query.stream === undefined ||\n query.after !== undefined\n )\n return -1;\n for (let j = this._events.length - 1; j >= 0; j--) {\n const e = this._events[j];\n if (e.stream === query.stream && e.name === SNAP_EVENT) return j;\n }\n return -1;\n }\n\n async query<E extends Schemas>(\n callback: (event: Committed<E, keyof E>) => void,\n query?: Query\n ) {\n await sleep();\n // The id bounds (`after`, `before`, the snapshot floor) are one index\n // window over the id-ordered log; every other filter is per event.\n const floor = this._snap_floor(query);\n const lo = floor >= 0 ? floor : this._first_index_after(query?.after ?? -1);\n const hi =\n query?.before !== undefined\n ? this._first_index_after(query.before - 1)\n : this._events.length;\n const with_pii = query?.with_pii !== false;\n const in_query = this._in_query(query ?? {});\n let count = 0;\n for (let k = 0; k < hi - lo; k++) {\n const e = this._events[query?.backward ? hi - 1 - k : lo + k];\n if (!in_query(e)) continue;\n await Promise.resolve(\n callback(this._with_pii(e as Committed<E, keyof E>, with_pii))\n );\n count++;\n if (query?.limit && count >= query.limit) break;\n }\n return count;\n }\n\n async commit<E extends Schemas>(\n appends: readonly Append<E>[],\n meta: EventMeta\n ) {\n await sleep();\n checkAppends(appends);\n // Every version is checked before anything is appended, so a stale\n // append leaves every stream as it was.\n for (const append of appends)\n checkVersion(append, this._stream_versions.get(append.stream) ?? -1);\n const created = new Date();\n return appends.flatMap(({ stream, msgs }) => {\n let version = this._stream_versions.get(stream) ?? -1;\n const committed = msgs.map(({ name, data, pii }) => {\n const c: Committed<E, keyof E> = {\n id: this._next_id++,\n stream,\n version: ++version,\n created,\n name,\n data,\n meta,\n };\n // Stored pii-less, so `forget_pii` never touches the event row.\n this._events.push(c as Committed<Schemas, keyof Schemas>);\n if (pii != null) this._put_pii(stream, c.id, pii);\n return this._with_pii(c);\n });\n this._stream_versions.set(stream, version);\n return committed;\n });\n }\n\n async claim(\n lagging: number,\n leading: number,\n by: string,\n millis: number,\n lane?: string\n ) {\n await sleep();\n // Eligibility is `at < correlated_at` on the row alone; the event log\n // is never read. An unmarked row is not claimable.\n const has_work = (s: InMemoryStream): boolean =>\n s.correlated_at !== undefined && s.at < s.correlated_at;\n const available = [...this._streams.values()].filter(\n (s) =>\n s.is_available && has_work(s) && (lane === undefined || s.lane === lane)\n );\n // Lagging: priority DESC, then watermark ASC, like the PG SQL. The\n // `fair` slots ignore priority so sustained high-priority load can't\n // starve a default-priority stream.\n const fair = fairSlots(lagging);\n const by_priority = [...available].sort(\n (a, b) => b.priority - a.priority || a.at - b.at\n );\n const priority_slice = by_priority.slice(0, lagging - fair);\n const picked = new Set(priority_slice.map((s) => s.stream));\n const fair_slice = [...available]\n .sort((a, b) => a.at - b.at)\n .filter((s) => !picked.has(s.stream))\n .slice(0, fair);\n const lag = [...priority_slice, ...fair_slice].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 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 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 async subscribe(\n streams: SubscribeInput[],\n correlated_at?: number,\n correlator?: { key: string; by: string; millis: number }\n ) {\n await sleep();\n\n // The checkpoint is monotonic. With a correlator, position and lease\n // are per key.\n let correlating: boolean | undefined;\n let keyed_at: number | undefined;\n if (correlator) {\n const now = Date.now();\n const held = this._correlators.get(correlator.key);\n // A key with no row yet inherits the shared position, so a new\n // correlator does not re-read history.\n const at = Math.max(held?.at ?? this._correlated_at, correlated_at ?? -1);\n keyed_at = at;\n if (!held || held.until < now || held.by === correlator.by) {\n correlating = true;\n this._correlators.set(correlator.key, {\n at,\n by: correlator.by,\n // Release outright, or a successor in the same ms is still refused.\n until: correlator.millis > 0 ? now + correlator.millis : 0,\n });\n } else {\n // Refused, but the position still advances: this caller may already\n // have scanned and marked.\n correlating = false;\n held.at = at;\n }\n // How far any correlator has read.\n if (correlated_at !== undefined && correlated_at > this._correlated_at)\n this._correlated_at = correlated_at;\n } else if (\n correlated_at !== undefined &&\n correlated_at > this._correlated_at\n )\n this._correlated_at = correlated_at;\n\n let subscribed = 0;\n for (const {\n stream,\n source,\n priority = 0,\n lane = DEFAULT_LANE,\n correlated_at,\n } of streams) {\n const existing = this._streams.get(stream);\n if (existing) {\n existing.resubscribe(priority, lane);\n if (correlated_at !== undefined) existing.mark(correlated_at);\n } else {\n const created = new InMemoryStream(stream, source, priority, lane);\n if (correlated_at !== undefined) created.mark(correlated_at);\n this._streams.set(stream, created);\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 {\n subscribed,\n watermark,\n correlated_at: keyed_at ?? this._correlated_at,\n ...(correlating === undefined ? {} : { correlating }),\n };\n }\n\n async ack(leases: Lease[]) {\n await sleep();\n // One synchronous pass: the in-memory form of the single transaction.\n return leases\n .map((l) => this._streams.get(l.stream)?.ack(l))\n .filter((l) => !!l);\n }\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 async defer(input: string[] | StreamFilter, deferred_at: number) {\n await sleep();\n const selected = this._select(input);\n for (const s of selected) s.defer(deferred_at);\n return selected.length;\n }\n\n // De-duplicated like the SQL `stream = ANY(...)`; unknown names skipped.\n private _select(input: string[] | StreamFilter): InMemoryStream[] {\n if (Array.isArray(input))\n return [...new Set(input)]\n .map((name) => this._streams.get(name))\n .filter((s) => s !== undefined);\n return [...this._streams.values()].filter(this._filter_predicate(input));\n }\n\n private _filter_predicate(\n filter: StreamFilter\n ): (s: InMemoryStream) => boolean {\n const stream_ok =\n filter.stream !== undefined\n ? name_test(filter.stream, filter.stream_exact)\n : undefined;\n const source_ok =\n filter.source !== undefined\n ? name_test(filter.source, filter.source_exact)\n : undefined;\n return (s) => {\n if (stream_ok && !stream_ok(s.stream)) return false;\n if (source_ok && (s.source === undefined || !source_ok(s.source)))\n return false;\n if (filter.blocked !== undefined && s.blocked !== filter.blocked)\n return false;\n if (filter.lane !== undefined && s.lane !== filter.lane) return false;\n return true;\n };\n }\n\n async reset(input: string[] | StreamFilter) {\n await sleep();\n const selected = this._select(input);\n for (const s of selected) s.reset();\n return selected.length;\n }\n\n async forget_pii(stream: string): Promise<number> {\n await sleep();\n const count = this._pii.get(stream)?.size ?? 0;\n this._pii.delete(stream);\n return count;\n }\n\n async unblock(input: string[] | StreamFilter) {\n await sleep();\n const selected = this._select(\n Array.isArray(input) ? input : { ...input, blocked: true }\n );\n return selected.filter((s) => s.unblock()).length;\n }\n\n async prioritize(filter: StreamFilter, priority: number) {\n await sleep();\n const changed = this._select(filter).filter((s) => s.priority !== priority);\n for (const s of changed) s.prioritize(priority);\n return changed.length;\n }\n\n async query_streams(\n callback: (position: StreamPosition) => void,\n query?: QueryStreams\n ): Promise<QueryStreamsResult> {\n await sleep();\n const limit = query?.limit ?? DEFAULT_QUERY_STREAMS_LIMIT;\n const after = query?.after;\n const source_matches = query?.source_matches;\n const matches = this._filter_predicate(query ?? {});\n // Reverse-match: the stored `source` pattern must match a requested name.\n const reverse_match = (source: string): boolean => {\n const re = new RegExp(source);\n return source_matches!.some((name) => re.test(name));\n };\n\n // The default sort is code-unit order, which agrees with the `<=`\n // cursor and the SQL binary collation; `localeCompare` would not.\n const sorted = [...this._streams.keys()]\n .sort()\n .map((stream) => this._streams.get(stream) as InMemoryStream);\n\n let count = 0;\n for (const s of sorted) {\n if (after !== undefined && s.stream <= after) continue;\n if (!matches(s)) continue;\n if (source_matches !== undefined) {\n // No source consumes every stream, so it matches any name.\n if (s.source && !reverse_match(s.source)) continue;\n }\n // Before emitting, so `limit: 0` returns none.\n if (count >= limit) break;\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 priority: s.priority,\n leased_by: s.leased_by,\n leased_until: s.leased_until,\n lane: s.lane,\n deferred_at: s.deferred_at,\n correlated_at: s.correlated_at,\n });\n count++;\n }\n return { maxEventId: this._events.at(-1)?.id ?? -1, count };\n }\n\n async query_stats<E extends Schemas>(\n input: string[] | Pick<StreamFilter, \"stream\" | \"stream_exact\">,\n options?: QueryStatsOptions<E>\n ): Promise<Map<string, StreamStats<E>>> {\n await sleep();\n const exclude = new Set<string>(options?.exclude ?? []);\n const want_tail = options?.tail ?? false;\n const want_count = options?.count ?? false;\n const want_names = options?.names ?? false;\n const before = options?.before;\n const after = options?.after;\n const limit = options?.limit;\n\n const array_targets = Array.isArray(input) ? new Set(input) : null;\n const stream_ok =\n !Array.isArray(input) && input.stream !== undefined\n ? name_test(input.stream, input.stream_exact)\n : undefined;\n\n const scope_cache = new Map<string, boolean>();\n const in_scope = (stream: string): boolean => {\n const cached = scope_cache.get(stream);\n if (cached !== undefined) return cached;\n const ok = array_targets\n ? array_targets.has(stream)\n : (stream_ok?.(stream) ?? true);\n scope_cache.set(stream, ok);\n return ok;\n };\n\n type Acc = {\n head: Committed<Schemas, keyof Schemas>;\n tail?: Committed<Schemas, keyof Schemas>;\n count: number;\n names?: Record<string, number>;\n };\n const acc = new Map<string, Acc>();\n for (const e of this._events) {\n if (before !== undefined && e.id >= before) continue;\n if (!in_scope(e.stream)) continue;\n if (exclude.has(e.name as string)) continue;\n let a = acc.get(e.stream);\n if (!a) {\n a = { head: e, count: 0 };\n if (want_tail) a.tail = e;\n if (want_names) a.names = {};\n acc.set(e.stream, a);\n }\n a.head = e;\n a.count++;\n if (want_names) {\n const n = String(e.name);\n a.names![n] = (a.names![n] ?? 0) + 1;\n }\n }\n\n // Code-unit order, to agree with the `<=` cursor (see query_streams).\n const ordered = [...acc.keys()].sort();\n const out = new Map<string, StreamStats<E>>();\n for (const stream of ordered) {\n if (limit !== undefined && out.size >= limit) break;\n if (after !== undefined && stream <= after) continue;\n const a = acc.get(stream)!;\n const stats: {\n head: Committed<Schemas, keyof Schemas>;\n tail?: Committed<Schemas, keyof Schemas>;\n count?: number;\n names?: Record<string, number>;\n } = { head: a.head };\n if (want_tail) stats.tail = a.tail;\n if (want_count) stats.count = a.count;\n if (want_names) stats.names = a.names;\n out.set(stream, stats as StreamStats<E>);\n }\n return out;\n }\n\n async truncate(\n targets: Array<{\n stream: string;\n snapshot?: Schema;\n meta?: EventMeta;\n before?: Date;\n max_id?: number;\n }>\n ) {\n await sleep();\n const result = new Map<\n string,\n {\n deleted: number;\n committed: Committed<Schemas, keyof Schemas>;\n before?: Date;\n }\n >();\n\n // Per target, like the SQL adapters: a later target sees what an\n // earlier one in the same call already pruned.\n const windowed = targets.filter((t) => t.before !== undefined);\n for (const { stream, before, max_id } of windowed) {\n let boundary: Committed<Schemas, keyof Schemas> | undefined;\n for (const e of this._events) {\n if (\n e.stream === stream &&\n e.name === SNAP_EVENT &&\n e.created < before! &&\n (max_id === undefined || e.id <= max_id) &&\n (!boundary || e.id > boundary.id)\n )\n boundary = e;\n }\n if (!boundary) continue; // no qualifying snapshot → no-op\n const below = boundary.id;\n let deleted = 0;\n this._events = this._events.filter((e) => {\n if (e.stream !== stream || e.id >= below) return true;\n this._pii.get(stream)?.delete(e.id);\n deleted++;\n return false;\n });\n result.set(stream, { deleted, committed: boundary, before });\n }\n\n const full = targets.filter((t) => t.before === undefined);\n // Per target, like the SQL adapters, so a stream listed twice gets one seed.\n for (const { stream, snapshot, meta } of full) {\n let deleted = 0;\n this._events = this._events.filter((e) => {\n if (e.stream !== stream) return true;\n deleted++;\n return false;\n });\n // Subscriptions untouched; `seed()` reaps inert ones.\n this._stream_versions.delete(stream);\n this._pii.delete(stream);\n const event: Committed<Schemas, keyof Schemas> = {\n id: this._next_id++,\n stream,\n version: 0,\n created: new Date(),\n ...seedEvent(snapshot, meta),\n };\n this._events.push(event);\n this._stream_versions.set(stream, 0);\n result.set(stream, { deleted, committed: event });\n }\n return result;\n }\n\n /** A throw inside the driver restores the captured state. Ids restart at 0. */\n async restore(\n driver: (\n callback: (event: Committed<Schemas, keyof Schemas>) => Promise<number>\n ) => Promise<void>\n ): Promise<void> {\n await sleep();\n const prev_events = this._events;\n const prev_next_id = this._next_id;\n const prev_streams = this._streams;\n const prev_stream_versions = this._stream_versions;\n const prev_pii = this._pii;\n const prev_correlated_at = this._correlated_at;\n const prev_correlators = new Map(\n [...this._correlators].map(([k, v]) => [k, { ...v }])\n );\n // Renumbered from 0, so checkpoints restart too; leases are kept.\n this._correlated_at = -1;\n for (const c of this._correlators.values()) c.at = -1;\n this._events = [];\n this._next_id = 0;\n this._streams = new Map();\n this._stream_versions = new Map();\n this._pii = new Map();\n // A stream holds each version once, as the SQL stores' unique index\n // enforces: a rename that maps two streams onto one name is refused.\n const seen = new Set<string>();\n try {\n await driver(async (event) => {\n const at = `${event.version}:${event.stream}`;\n if (seen.has(at))\n throw new Error(\n `restore: stream \"${event.stream}\" has version ${event.version} twice`\n );\n seen.add(at);\n const id = this._next_id++;\n // Split `pii` out like `commit`, so `forget_pii` erases restored PII.\n const { pii, ...rest } = event;\n const committed: Committed<Schemas, keyof Schemas> = { ...rest, id };\n this._events.push(committed);\n if (pii != null) this._put_pii(event.stream, id, pii);\n // Last event per stream wins.\n this._stream_versions.set(event.stream, event.version);\n return id;\n });\n } catch (err) {\n this._events = prev_events;\n this._next_id = prev_next_id;\n this._streams = prev_streams;\n this._stream_versions = prev_stream_versions;\n this._pii = prev_pii;\n this._correlated_at = prev_correlated_at;\n this._correlators = prev_correlators;\n throw err;\n }\n }\n}\n","import type { Disposer } from \"./types/index.js\";\n\n/**\n * The shape {@link register_weak_disposer} needs from a `WeakRef`. Declared\n * structurally so tests can hand in a stub that reports its target as\n * collected — real garbage collection is not schedulable, and a guarantee\n * about releasing memory deserves a deterministic test.\n */\nexport type RefLike<T extends object> = { deref: () => T | undefined };\n\n/**\n * A registered cleanup function, optionally tied to the lifetime of the\n * object it cleans up. `ref` present means \"run this only while its target\n * is still reachable\" — the entry holds no strong reference to that target,\n * so registering never keeps it alive. The target is handed back to `run`,\n * so a weak disposer never has to re-check what the registry just proved.\n */\ntype Entry = {\n readonly run: (target: never) => Promise<void>;\n readonly ref?: RefLike<object>;\n};\n\n/** Registered cleanup functions, executed in reverse order during shutdown. */\nconst entries: Entry[] = [];\n\n/**\n * Drop entries whose target has been collected.\n *\n * Called on every registration so the registry stays proportional to the\n * live objects rather than to every object ever built. That matters for apps\n * that mint short-lived Acts — one per tenant, per request, per test — where\n * the registry would otherwise grow without bound for the process lifetime.\n */\nconst prune = (): void => {\n for (let i = entries.length - 1; i >= 0; i--) {\n const { ref } = entries[i];\n if (ref && !ref.deref()) entries.splice(i, 1);\n }\n};\n\n/** Register a cleanup function that runs unconditionally at teardown. */\nexport const register_disposer = (run: Disposer): void => {\n prune();\n entries.push({ run });\n};\n\n/**\n * Register a cleanup function bound to `ref`'s target, held weakly.\n *\n * The registry never retains the target, so an object that becomes\n * unreachable is collectable whether or not it was cleaned up first, and its\n * entry is skipped at teardown — there is nothing left to clean up.\n */\nexport const register_weak_disposer = <T extends object>(\n ref: RefLike<T>,\n run: (target: T) => Promise<void>\n): void => {\n prune();\n entries.push({\n run: run as (target: never) => Promise<void>,\n ref: ref as RefLike<object>,\n });\n};\n\n/**\n * Run every live disposer in reverse registration order, sequentially, so a\n * disposer can rely on later-registered ones having already finished.\n *\n * A disposer that throws doesn't stop the others: every one runs, and the\n * failures are returned for the caller to report.\n *\n * Registrations are left in place, so a second teardown call re-runs them,\n * exactly as before this registry learned about weak entries.\n */\nexport const run_disposers = async (): Promise<unknown[]> => {\n const failures: unknown[] = [];\n for (const { run, ref } of [...entries].reverse()) {\n const target = ref?.deref();\n if (ref && !target) continue;\n try {\n await (ref ? run(target as never) : (run as Disposer)());\n } catch (error) {\n failures.push(error);\n }\n }\n return failures;\n};\n","/**\n * Every option bag act-core accepts, checked in one place. Each bag has an\n * internal schema below; `check(kind, options)` parses it, throws `ZodError`\n * on a bad value and returns the parsed, defaulted bag, which a caller that\n * only needs the throw ignores. Validation refuses only what is broken\n * (`NaN`, `±Infinity`, nonsensical negatives) and never newly refuses a\n * value that works. The rules are in .claude/rules/config-validation.md.\n */\n\nimport { z } from \"zod\";\nimport type { AutocloseConfig, CircuitBreakerOptions } from \"../types/act.js\";\nimport type { AutoclosePolicy, BackoffOptions } from \"../types/index.js\";\nimport { MAX_TIMER_DELAY_MS } from \"./defer-timer.js\";\n\n// Backoff — retry pacing, nested in the reaction and action bags. No\n// `maxMs >= baseMs` constraint: a cap below `baseMs` on `exponential` is\n// documented behavior.\nconst BackoffOptionsSchema = z.object({\n strategy: z.enum([\"fixed\", \"linear\", \"exponential\"]),\n baseMs: z.number().min(0),\n maxMs: z.number().gt(0).optional(),\n jitter: z.boolean().optional(),\n});\n\n/**\n * Retry pacing for a reaction that declares no backoff, and for projections\n * and failed fetches, which have no options. Ten seconds, like the default\n * lease; the lease itself only keeps two workers off one stream.\n */\nexport const DEFAULT_BACKOFF: BackoffOptions = {\n strategy: \"fixed\",\n baseMs: 10_000,\n};\n\n// Reaction — `.do(handler, options)`, fully defaulted by the builder.\n// `maxRetries` gates the block decision, so a `NaN` would retry a poison\n// message forever.\n\n/** Retries a reaction gets before it blocks, unless `.do()` says otherwise. */\nexport const DEFAULT_MAX_RETRIES = 3;\n/** Whether a reaction blocks its stream once retries run out. */\nexport const DEFAULT_BLOCK_ON_ERROR = true;\n\nconst ReactionOptionsSchema = z.object({\n blockOnError: z.boolean(),\n maxRetries: z.number().int().min(0),\n backoff: BackoffOptionsSchema.optional(),\n});\n\n// Action — `.on(entry, options)`. `maxRetries` ends the command retry loop,\n// so a `NaN` could spin it forever on a contended stream.\nconst ActionOptionsSchema = z.object({\n maxRetries: z.number().int().min(0).optional(),\n backoff: BackoffOptionsSchema.optional(),\n});\n\n// Target — `.to({ target, priority })`, a resolver's result and\n// `app.prioritize`. Postgres stores `priority` as a 32-bit `int`.\nconst TargetOptionsSchema = z.object({ priority: z.int32().optional() });\n\n// Restore — `app.restore(source, options)`. The stores read a falsy limit as\n// \"no limit\", so a NaN or 0 page reads the whole source at once, and a\n// negative or fractional one ends the scan early, after the sink was wiped.\nconst ScanOptionsSchema = z.object({\n batch_size: z.number().int().min(1).optional(),\n});\n\n// Audit — `app.audit(categories, options)`. A NaN cutoff fails every\n// comparison, so it would report a clean store.\nconst AuditOptionsSchema = z.object({\n thresholds: z\n .object({\n idle_days: z.number().min(0).optional(),\n restart_min: z.number().min(0).optional(),\n stuck_minutes: z.number().min(0).optional(),\n near_block: z.number().min(0).optional(),\n deprecated_min: z.number().min(0).optional(),\n drift_min: z.number().min(0).optional(),\n })\n .optional(),\n});\n\n// A timer's delay. Node runs a timer below 1 ms, above 2^31 − 1 ms or NaN\n// every millisecond, so every delay a bag sets a timer from is held to this.\nconst TimerMsSchema = z.number().min(0).max(MAX_TIMER_DELAY_MS);\n\n// Page — the `limit` and bounds of every query: `app.query`, `app.load`'s\n// `asOf`, `app.correlate`, `app.start_correlations`, `app.blocked_streams`\n// and settle's `correlate`. 0 stays (the poll reads it as its default); a NaN\n// limit read the whole log, and a NaN or Invalid Date bound was ignored by\n// one store and threw a raw driver error in another.\nconst LimitSchema = z.number().int().min(0).optional();\nconst PageOptionsSchema = z.object({\n limit: LimitSchema,\n before: z.number().optional(),\n after: z.number().optional(),\n created_before: z.date().optional(),\n created_after: z.date().optional(),\n});\n\n// Poll — `app.start_correlations`' frequency.\nconst PollOptionsSchema = z.object({\n frequency: TimerMsSchema,\n});\n\n// Drain / Settle — the knobs of `app.drain(...)` and `app.settle(...)`.\n\n/** Streams a drain claims per cycle. */\nexport const DEFAULT_STREAM_LIMIT = 10;\n/** Events a drain reads per stream per cycle. */\nexport const DEFAULT_EVENT_LIMIT = 10;\n/** How long a drain holds a stream's lease (ms). */\nexport const DEFAULT_LEASE_MILLIS = 10_000;\n\n// A drain claims at least one stream: 0 would claim nothing and read as\n// caught up, so the lane would never run.\nconst DrainOptionsSchema = z.object({\n streamLimit: z.number().int().min(1).optional(),\n eventLimit: z.number().int().min(0).optional(),\n leaseMillis: z.number().min(0).optional(),\n});\n\n// Lane — `.withLane({...})`, per-lane overrides of the drain budget.\nconst LaneConfigSchema = DrainOptionsSchema.pick({\n streamLimit: true,\n leaseMillis: true,\n}).extend({\n name: z.string().min(1),\n cycleMs: TimerMsSchema.optional(),\n});\n\nconst SettleOptionsSchema = DrainOptionsSchema.extend({\n debounceMs: TimerMsSchema.optional(),\n correlate: PageOptionsSchema.optional(),\n // `0` runs no passes; `Infinity` is the documented default (no cap), which\n // an integer check alone would refuse.\n maxPasses: z\n .union([z.number().int().min(0), z.literal(Number.POSITIVE_INFINITY)])\n .optional(),\n});\n\n// Shutdown — the grace budget `app.shutdown(...)` gives in-flight cycles.\n\n/**\n * Ceiling on the derived shutdown grace budget, so one parked handler on a\n * long-lease lane can't hold a rolling deploy open for that whole lease. 30s\n * is the longest `leaseMillis` the production checklist recommends.\n */\nexport const MAX_SHUTDOWN_GRACE_MS = 30_000;\n\n// `0` stops scheduling and returns without waiting.\nconst ShutdownOptionsSchema = z.object({\n graceMs: TimerMsSchema.optional(),\n});\n\n// Act — the scalar knobs of `act().build(options)`. The autoclose window and\n// the circuit breaker are their own bags below.\nconst ActOptionsSchema = z.object({\n maxSubscribedStreams: z.number().int().min(1).optional(),\n settleDebounceMs: TimerMsSchema.int().optional(),\n autoSettle: z.boolean().optional(),\n});\n\n// Autoclose window — `ActOptions.autocloseWindow`, the off-hours window. The\n// hour math stays in `autoclose-window.ts`.\n\n/** True when `tz` is a zone the runtime's `Intl` accepts. */\nfunction is_time_zone(tz: string): boolean {\n try {\n new Intl.DateTimeFormat(\"en-US\", { timeZone: tz });\n return true;\n } catch {\n return false;\n }\n}\n\nconst hour = (field: string) =>\n z\n .number()\n .int()\n .min(0)\n .max(23, {\n message: `autocloseWindow.${field} must be an hour in [0, 23]`,\n });\n\nconst AutocloseOptionsSchema = z.object({\n autocloseWindow: z\n .object({\n start: hour(\"start\"),\n end: hour(\"end\"),\n timeZone: z\n .string()\n .refine(is_time_zone, {\n message: \"autocloseWindow.timeZone must be a valid IANA time zone\",\n })\n .default(\"UTC\"),\n })\n .refine((w) => w.start !== w.end, {\n message:\n \"autocloseWindow.start and end must differ — an empty window disables autoclose\",\n })\n .optional(),\n}) satisfies z.ZodType<AutocloseConfig>;\n\n// Circuit breaker — `ActOptions.circuitBreaker`, the store-op breaker. The\n// state machine stays in `circuit-breaker.ts`.\nconst CircuitBreakerOptionsSchema = z.object({\n failureThreshold: z.number().int().min(1).default(5),\n cooldownMs: z.number().int().min(100).max(3_600_000).default(30_000),\n}) satisfies z.ZodType<CircuitBreakerConfig, CircuitBreakerOptions>;\n\n/** Resolved circuit-breaker configuration. */\nexport type CircuitBreakerConfig = Required<CircuitBreakerOptions>;\n\n// Fold — the batch-fold knobs of `projection(name).of(state, options)`.\nconst FoldOptionsSchema = z.object({\n flushEvery: z.number().int().min(1).default(1_000),\n maxCachedStates: z.number().int().min(1).default(10_000),\n});\n\n/** Resolved fold configuration. */\nexport type FoldConfig = z.infer<typeof FoldOptionsSchema>;\n\n// Defer — the literal schedule of `.defer(when)`, checked at build; the\n// function form is checked when it runs. Exactly one of `after` / `at`; a\n// duration's fields add up (`{ hours: 1, minutes: 30 }` is 90 minutes).\nconst DeferWhenSchema = z\n .object({\n after: z\n .object({\n days: z.number().positive().optional(),\n hours: z.number().positive().optional(),\n minutes: z.number().positive().optional(),\n })\n .strict()\n .refine((d) => d.days != null || d.hours != null || d.minutes != null, {\n message:\n \"defer: a duration needs at least one of days, hours, or minutes\",\n })\n .optional(),\n at: z.date().optional(),\n })\n .strict()\n .refine((w) => (w.after === undefined) !== (w.at === undefined), {\n message: \"defer: specify exactly one of `after` or `at`\",\n });\n\n// Autoclose policy — `.autocloses({...})`. Top-level fields AND together; an\n// `or` block is the alternative path, with the same fields minus `or`.\n// `.strict()` turns a typo into a build error. Windows are in days: `after`\n// is at least a minute and `keep` at least a day, since anything shorter is\n// a misconfiguration.\nconst window_schema = (\n field: \"after\" | \"keep\",\n min_days: number,\n floor: string\n) =>\n z\n .object({\n days: z\n .number({ message: `autocloses: ${field}.days must be a number` })\n .positive(`autocloses: ${field}.days must be > 0`),\n })\n .refine((o) => o.days >= min_days, {\n message: `autocloses: ${field} resolves to < ${floor}, which is too short to be a meaningful retention window`,\n });\n\nconst AfterSchema = window_schema(\"after\", 1 / 1440, \"one minute\");\n\nconst IsSchema = z.union([\n z.string().min(1, \"autocloses: is must be a non-empty event name\"),\n z\n .array(\n z.string().min(1, \"autocloses: is entries must be non-empty event names\")\n )\n .min(1, \"autocloses: is must include at least one event name\")\n .readonly(),\n]);\n\nconst ReachesSchema = z\n .number({ message: \"autocloses: reaches must be a number\" })\n .int(\"autocloses: reaches must be an integer\")\n .min(1, \"autocloses: reaches must be >= 1\");\n\nconst OrBlockSchema = z\n .object({\n after: AfterSchema.optional(),\n is: IsSchema.optional(),\n reaches: ReachesSchema.optional(),\n })\n .strict()\n .refine(\n (o) =>\n o.after !== undefined || o.is !== undefined || o.reaches !== undefined,\n {\n message:\n \"autocloses: `or` block must include at least one of after / is / reaches\",\n }\n );\n\nconst AutoclosePolicySchema = z\n .object({\n after: AfterSchema.optional(),\n is: IsSchema.optional(),\n reaches: ReachesSchema.optional(),\n or: OrBlockSchema.optional(),\n keep: window_schema(\"keep\", 1, \"one day\").optional(),\n })\n .strict()\n .refine(\n (o) =>\n o.after !== undefined ||\n o.is !== undefined ||\n o.reaches !== undefined ||\n o.or !== undefined ||\n o.keep !== undefined,\n {\n message:\n \"autocloses: at least one of after / is / reaches / or / keep must be specified — empty `{}` is a misconfiguration\",\n }\n ) satisfies z.ZodType<AutoclosePolicy>;\n\n// Store — `store(adapter)` and `ActOptions.scoped.store`. A store written for\n// `commit(stream, msgs, meta, expectedVersion)`, the shape before 2.0, would\n// forward its arguments and treat the appends as a stream name, sending\n// events and their sensitive fields wherever that name goes.\nconst StoreSchema = z.object({\n commit: z.custom<(...args: never[]) => unknown>(\n (commit) => typeof commit === \"function\" && commit.length !== 4,\n {\n message:\n \"This store's commit takes (stream, msgs, meta, expectedVersion), the shape before @rotorsoft/act 2.0. Upgrade it, and every @rotorsoft/act-* package, together: https://rotorsoft.github.io/act-root/docs/guides/migrating-to-2.x\",\n }\n ),\n});\n\nconst SCHEMAS = {\n act: ActOptionsSchema,\n action: ActionOptionsSchema,\n audit: AuditOptionsSchema,\n autoclose: AutocloseOptionsSchema,\n autoclose_policy: AutoclosePolicySchema,\n circuit_breaker: CircuitBreakerOptionsSchema,\n defer: DeferWhenSchema,\n drain: DrainOptionsSchema,\n fold: FoldOptionsSchema,\n lane: LaneConfigSchema,\n page: PageOptionsSchema,\n poll: PollOptionsSchema,\n reaction: ReactionOptionsSchema,\n scan: ScanOptionsSchema,\n settle: SettleOptionsSchema,\n shutdown: ShutdownOptionsSchema,\n store: StoreSchema,\n target: TargetOptionsSchema,\n};\n\ntype Schemas = typeof SCHEMAS;\n\n/** Parse an option bag with its schema. Throws `ZodError` on a bad value. */\nexport function check<K extends keyof Schemas>(\n kind: K,\n options: z.input<Schemas[K]>\n): z.output<Schemas[K]> {\n return SCHEMAS[kind].parse(options) as z.output<Schemas[K]>;\n}\n","/**\n * The shared \"next visit time\" primitive. A `DeferTimer` holds a\n * `stream → due-time` map and a single collapsed wake timer: it parks\n * streams that should be re-visited later and fires one `on_wake` callback\n * at the earliest pending due-time.\n *\n * The {@link \"drain-cycle\".DrainController} rides it for per-reaction\n * backoff (a retry's `next_attempt_at`) and for the `defer` outcome, which\n * holds a stream pending without advancing the watermark or bumping `retry`.\n * The wake runs a drain, and the drain re-schedules the timer when it ends.\n * An entry stays until its stream is acked or blocked, re-parked, or a\n * drain that started after it came due claims nothing ({@link prune}): a\n * wake that lands during another drain, or a pass that reaches only some of\n * the due streams, leaves the rest due, so the next re-schedule fires again\n * at once.\n *\n * Lives in process memory, per worker — the same per-worker pacing trade-off\n * documented for backoff. Durability comes from the data the due-time is\n * *derived* from (an un-advanced watermark, an event's `created` timestamp),\n * not from the map. A restart empties the map, so the cold-start rebuild is\n * explicit: `CorrelateCycle.init` seeds each still-future `deferred_at` back\n * onto the owning lane's timer via {@link \"drain-cycle\".DrainController.seed_defer},\n * so an idle deferred stream re-arms at its due-time with no\n * intervening commit.\n */\n\n/**\n * Node's `setTimeout` delay is a 32-bit signed int (~24.8 days). A larger\n * delay overflows and fires immediately, so we cap at this and re-arm.\n */\nexport const MAX_TIMER_DELAY_MS = 2_147_483_647;\n\n/** A linear scan finds the earliest entry: the map is bounded by claim limits. */\nexport class DeferTimer {\n private readonly _due = new Map<string, number>();\n private _timer: ReturnType<typeof setTimeout> | undefined;\n private readonly _on_wake: () => void;\n\n /**\n * @param on_wake - invoked once each time the earliest due-time elapses.\n * The consumer re-schedules for whatever is still parked.\n */\n constructor(on_wake: () => void) {\n this._on_wake = on_wake;\n }\n\n /** Number of currently parked streams. */\n get size(): number {\n return this._due.size;\n }\n\n /**\n * Park `stream` for a re-visit at `at` (ms since epoch). A plain\n * overwrite: the caller computes the authoritative next-visit for the\n * stream (the drain's `handle` already reconciles a stream's reactions\n * into one result per cycle), so there is no stale value to merge against.\n */\n set(stream: string, at: number): void {\n this._due.set(stream, at);\n }\n\n /** Drop `stream` from the parked set (e.g. on a successful ack or block). */\n delete(stream: string): void {\n this._due.delete(stream);\n }\n\n /**\n * Drop every entry due at or before `before`. The drain calls it when a\n * pass that started at `before` claimed nothing: whatever was due by then\n * was handled elsewhere (another worker) or has no work left, so waking\n * for it again would spin.\n */\n prune(before: number): void {\n for (const [stream, at] of this._due)\n if (at <= before) this._due.delete(stream);\n }\n\n /**\n * (Re)schedule the wake timer at the earliest pending due-time. Idempotent\n * — collapses many parked streams into a single timer. A no-op clears any\n * pending timer when the map is empty.\n *\n * The timer is `unref()`-ed so pending re-visits never keep the process\n * alive on their own.\n */\n schedule(): void {\n if (this._timer) clearTimeout(this._timer);\n if (this._due.size === 0) {\n this._timer = undefined;\n return;\n }\n let earliest = Number.POSITIVE_INFINITY;\n for (const t of this._due.values()) if (t < earliest) earliest = t;\n // A longer due-time (a 90-day autoclose cooldown) wakes at the ceiling;\n // the entry stays and the consumer re-schedules for the rest.\n const delay = Math.min(\n Math.max(0, earliest - Date.now()),\n MAX_TIMER_DELAY_MS\n );\n this._timer = setTimeout(() => {\n this._timer = undefined;\n this._on_wake();\n }, delay);\n this._timer.unref();\n }\n\n /** Cancel any pending wake timer. Idempotent. Leaves the parked set intact. */\n stop(): void {\n if (this._timer) {\n clearTimeout(this._timer);\n this._timer = undefined;\n }\n }\n}\n","/**\n * @module scoped\n * @category Internal\n *\n * Ambient execution context — every `AsyncLocalStorage` the framework owns\n * lives here, and so does every operation on one. No other module calls\n * `.run()` or `.getStore()`; they ask for a runner or a reader instead, so\n * this file is the single place to look at the abstraction.\n *\n * Two contexts:\n *\n * - **ports** — the active Act's store/cache bag, so `store()` / `cache()`\n * resolve per-Act rather than per-process. Installed by the\n * orchestrator via {@link make_run_scoped}, read by the port singletons\n * via {@link current_ports}.\n * - **reaction** — the event a handler is processing, so a dispatch made\n * anywhere inside that handler can thread `reactingTo` whichever `IAct`\n * reference made the call. Installed via {@link run_reacting},\n * read at the orchestrator boundary via {@link current_reacting}.\n *\n * Keeping the mechanics here keeps ambient state out of `internal/`, whose\n * modules are stateless implementations that receive what they need.\n *\n * @internal\n */\n\nimport { AsyncLocalStorage } from \"node:async_hooks\";\nimport type { Actor, Committed, IAct, Schemas, Scoped } from \"./types/index.js\";\n\n/**\n * What an Act actually installs in its frame: the ports a caller passed,\n * plus per-Act settings that runtime code has to read from the *running*\n * Act rather than from whatever closure it was built in.\n *\n * `autoclose_window` is here because the autoclose reactions are\n * synthesized once into the shared registry and handed by reference to\n * every Act built from that builder, so a window captured at synthesis is\n * the first tenant's window for everyone. The frame is per-Act, so\n * reading it here is what makes the setting per-Act too.\n *\n * Wider than {@link Scoped} on purpose: `Scoped` types what a caller\n * *passes* through `ActOptions.scoped` and is public; this is what the\n * orchestrator *installs* and is not.\n *\n * @internal\n */\nexport type ActFrame = Scoped & {\n readonly autoclose_window?: {\n readonly start: number;\n readonly end: number;\n readonly timeZone: string;\n };\n};\n\n/**\n * AsyncLocalStorage carrying the active Act's ports.\n *\n * Exported only for the scope-overhead bench; no entry point re-exports it.\n * Everything else uses the helpers below.\n */\nexport const scoped = new AsyncLocalStorage<ActFrame>();\n\n/**\n * The reaction currently running, as a box the handler can empty.\n *\n * A frame captured by work the handler started and did not await outlives the\n * handler and can never be unbound. The *frame* is unreclaimable, but the\n * *box inside it* is not: clearing `event` as the handler settles turns every\n * later read through that frame into \"no reaction\", without anyone having to\n * ask a second question.\n *\n * Not reachable from `index.ts`.\n */\ntype Reacting = { event: Committed<Schemas, string> | undefined };\n\nconst reacting = new AsyncLocalStorage<Reacting>();\n\n/**\n * Builds the runner an Act uses to enter its own port scope.\n *\n * Every Act gets a frame, including one built without `ActOptions.scoped` —\n * that one carries the singleton adapters (see `unscoped_ports` in `ports.ts`).\n * Entering unconditionally is what makes an Act's ports its own: a runner that\n * collapsed to `fn()` for a singleton Act did not leave whatever frame it was\n * called from, so dispatching into a shared Act from inside a tenant's handler\n * resolved `store()` to that tenant and wrote the shared Act's events into the\n * tenant's log.\n *\n * @internal\n */\nexport function make_run_scoped(\n bag: ActFrame\n): <T>(fn: () => Promise<T>) => Promise<T> {\n return (fn) => scoped.run(bag, fn);\n}\n\n/**\n * The running Act's off-hours autoclose window, or `undefined` when it\n * declared none (or when read outside any Act).\n *\n * Read at resolution time rather than captured at synthesis: the autoclose\n * reactions belong to the shared registry, and only the frame knows which\n * Act is running them.\n *\n * @internal\n */\nexport function current_autoclose_window(): ActFrame[\"autoclose_window\"] {\n return scoped.getStore()?.autoclose_window;\n}\n\n/**\n * The active Act's ports, or `undefined` outside any Act.\n *\n * Every Act runs in a frame, so this is `undefined` only for a call made\n * outside one — `store()` and `cache()` fall back to the singleton adapters\n * there, which is what a bare `store()` in application setup expects.\n *\n * @internal\n */\nexport function current_ports(): Scoped | undefined {\n return scoped.getStore();\n}\n\n/**\n * Runs `fn` with `event` installed as the triggering-event context.\n *\n * Entered per payload rather than per lease: the context has to unwind with\n * the handler so it never reaches the drain cycle, and work a handler started\n * without awaiting has to resume into its own event's frame.\n *\n * @internal\n */\nfunction run_reacting<T>(\n event: Committed<Schemas, string>,\n fn: () => Promise<T>\n): Promise<T> {\n const box: Reacting = { event };\n return reacting.run(box, async () => {\n try {\n return await fn();\n } finally {\n // Anything still running keeps this frame; from here it reads empty.\n box.event = undefined;\n }\n });\n}\n\n/**\n * The event being reacted to, or `undefined` outside a running handler —\n * including inside work that outlived one.\n *\n * @internal\n */\nexport function current_reacting(): Committed<Schemas, string> | undefined {\n return reacting.getStore()?.event;\n}\n\n/**\n * Everything a reaction handler runs inside: the `IAct` facade it is handed\n * as its third argument, and the triggering-event context that every dispatch\n * made within it resolves — including one through a captured `app`.\n *\n * Built here rather than in the dispatcher so the dispatcher never has to\n * know how either half works; it receives this whole and calls it.\n *\n * @internal\n */\nexport type ReactionScope<\n TEvents extends Schemas,\n TActions extends Schemas,\n TActor extends Actor = Actor,\n> = {\n readonly app: IAct<TEvents, TActions, TActor>;\n readonly run: <T>(\n event: Committed<Schemas, string>,\n fn: () => Promise<T>\n ) => Promise<T>;\n};\n\n/**\n * Assembles the reaction scope from the orchestrator's bound `IAct` methods.\n *\n * @internal\n */\nexport function make_reaction_scope<\n TEvents extends Schemas,\n TActions extends Schemas,\n TActor extends Actor = Actor,\n>(\n app: IAct<TEvents, TActions, TActor>\n): ReactionScope<TEvents, TActions, TActor> {\n return { app, run: run_reacting };\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 { register_disposer, run_disposers } from \"./disposers.js\";\nimport { check } from \"./internal/config.js\";\nimport { current_ports } from \"./scoped.js\";\nimport type {\n Cache,\n Disposable,\n Disposer,\n ExitCode,\n Logger,\n Scoped,\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// 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 * @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 * @see {@link Store} for the interface contract\n * @see {@link InMemoryStore} for the default implementation\n */\n// ALS check lives outside `port()` — its cache fires only once, so the\n// per-call branch on a scoped Act has to be in the public wrapper.\nconst _store = port(function store(adapter?: Store): Store {\n if (adapter) check(\"store\", adapter);\n return adapter ?? new InMemoryStore();\n});\n\nexport const store = ((adapter?: Store): Store => {\n return current_ports()?.store ?? _store(adapter);\n}) as (adapter?: Store) => Store;\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 */\nconst _cache = port(function cache(adapter?: Cache) {\n return adapter ?? new InMemoryCache();\n});\n\nexport const cache = ((adapter?: Cache): Cache => {\n return current_ports()?.cache ?? _cache(adapter);\n}) as (adapter?: Cache) => Cache;\n\n/**\n * The ports bag an Act without `ActOptions.scoped` runs in: the singleton\n * adapters, read through the same frame a scoped Act uses.\n *\n * Every Act entering a frame is what keeps its ports its own. Without one, a\n * shared Act called from inside a tenant's handler inherited that tenant's\n * frame and committed to the tenant's store.\n *\n * The properties are getters on purpose. The adapters are resolved lazily and\n * injected after `act().build()` in the normal case — `store(new PgStore())`\n * in application setup, or a test's `beforeEach` — so capturing them when the\n * bag is made would pin whatever existed at build time. They read the raw\n * resolvers rather than the public `store()`/`cache()`, which consult the\n * frame this bag *is* and would recurse.\n */\nexport const unscoped_ports = (): Scoped => DEFAULT_SCOPE;\n\nconst DEFAULT_SCOPE: Scoped = {\n get store() {\n return _store();\n },\n get cache() {\n return _cache();\n },\n};\n\n// ---------------------------------------------------------------------------\n// Disposal\n// ---------------------------------------------------------------------------\n\n// Registered cleanup functions live in `disposers.ts`, which holds\n// lifetime-bound entries weakly so registering never pins its target.\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), with 1 when any of them threw:\n * a failure is logged and the rest still run\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. A failure is\n // logged and the rest still run, so the process always gets to exit.\n const failures = await run_disposers();\n for (const adapter of [...adapters.values()].reverse()) {\n try {\n await adapter.dispose();\n log().info(`[act] - ${adapter.constructor.name}`);\n } catch (error) {\n failures.push(error);\n }\n }\n adapters.clear();\n for (const error of failures) log().error(error, \"Dispose failed\");\n const failed = code === \"ERROR\" || failures.length > 0;\n config().env !== \"test\" && process.exit(failed ? 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 && register_disposer(disposer);\n return disposeAndExit;\n}\n"]}
|