@rotorsoft/act 1.23.0 → 1.23.2

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.
Files changed (38) hide show
  1. package/dist/.tsbuildinfo +1 -1
  2. package/dist/@types/act.d.ts +18 -0
  3. package/dist/@types/act.d.ts.map +1 -1
  4. package/dist/@types/adapters/console-logger.d.ts.map +1 -1
  5. package/dist/@types/adapters/in-memory-cache.d.ts.map +1 -1
  6. package/dist/@types/adapters/in-memory-store.d.ts +10 -0
  7. package/dist/@types/adapters/in-memory-store.d.ts.map +1 -1
  8. package/dist/@types/config.d.ts +5 -5
  9. package/dist/@types/csv.d.ts.map +1 -1
  10. package/dist/@types/internal/builder-utils.d.ts.map +1 -1
  11. package/dist/@types/internal/circuit-breaker.d.ts.map +1 -1
  12. package/dist/@types/internal/close-cycle.d.ts +16 -0
  13. package/dist/@types/internal/close-cycle.d.ts.map +1 -1
  14. package/dist/@types/internal/close-signal.d.ts.map +1 -1
  15. package/dist/@types/internal/correlate-cycle.d.ts.map +1 -1
  16. package/dist/@types/internal/defer-signal.d.ts.map +1 -1
  17. package/dist/@types/internal/defer-timer.d.ts.map +1 -1
  18. package/dist/@types/internal/drain-cycle.d.ts.map +1 -1
  19. package/dist/@types/internal/drain.d.ts.map +1 -1
  20. package/dist/@types/internal/lru-map.d.ts.map +1 -1
  21. package/dist/@types/internal/merge.d.ts.map +1 -1
  22. package/dist/@types/internal/settle.d.ts.map +1 -1
  23. package/dist/@types/ports.d.ts +1 -1
  24. package/dist/@types/ports.d.ts.map +1 -1
  25. package/dist/@types/types/errors.d.ts.map +1 -1
  26. package/dist/@types/types/index.d.ts.map +1 -1
  27. package/dist/@types/utils.d.ts.map +1 -1
  28. package/dist/{chunk-YVLB5MMB.js → chunk-4HYYZOEH.js} +9 -2
  29. package/dist/chunk-4HYYZOEH.js.map +1 -0
  30. package/dist/index.cjs +70 -15
  31. package/dist/index.cjs.map +1 -1
  32. package/dist/index.js +63 -15
  33. package/dist/index.js.map +1 -1
  34. package/dist/test/index.cjs +8 -1
  35. package/dist/test/index.cjs.map +1 -1
  36. package/dist/test/index.js +1 -1
  37. package/package.json +2 -2
  38. package/dist/chunk-YVLB5MMB.js.map +0 -1
@@ -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/ports.ts","../src/utils.ts","../src/adapters/in-memory-store.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 * @module lru-map\n * @category Internal\n *\n * Tiny bounded LRU map / set built on insertion-ordered `Map`. Used to cap\n * memory in long-running orchestrators that mint large numbers of keys —\n * notably:\n *\n * - {@link InMemoryCache}: stream → state checkpoint\n * - `Act._subscribed_streams`: stream → presence (LruSet)\n *\n * Apps with millions of dynamic streams (one target per aggregate) can't\n * afford an unbounded `Set<string>` — eviction is required.\n *\n * @internal\n */\n\n/**\n * Bounded LRU map. `get()` promotes; `has()` does not. `set()` always\n * promotes and evicts the oldest entry when at capacity.\n *\n * @internal\n */\nexport class LruMap<K, V> {\n private readonly _entries = new Map<K, V>();\n 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\n/**\n * Bounded LRU set built on top of {@link LruMap}. `has()` does not promote;\n * `add()` does (re-inserting if already present, evicting the oldest at\n * capacity).\n *\n * @internal\n */\nexport class LruSet<T> {\n private readonly _map: LruMap<T, true>;\n\n constructor(maxSize: number) {\n this._map = new LruMap(maxSize);\n }\n\n has(value: T): boolean {\n return this._map.has(value);\n }\n\n add(value: T): void {\n this._map.set(value, true);\n }\n\n delete(value: T): boolean {\n return this._map.delete(value);\n }\n\n clear(): void {\n this._map.clear();\n }\n\n get size(): number {\n return this._map.size;\n }\n}\n","import { LruMap } from \"../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 */\n/* eslint-disable @typescript-eslint/require-await -- async interface for Redis-compatibility */\nexport class InMemoryCache implements Cache {\n // CacheEntry<any> lets `get<TState>` and `set<TState>` flow without casts:\n // any is bidirectionally compatible with the per-call TState binding, while\n // the public Cache interface still presents a typed surface to callers.\n private readonly _entries: LruMap<string, CacheEntry<any>>;\n\n constructor(options?: { maxSize?: number }) {\n this._entries = new LruMap(options?.maxSize ?? 1000);\n }\n\n /** @inheritDoc */\n async get<TState extends Schema>(\n stream: string\n ): Promise<CacheEntry<TState> | undefined> {\n return this._entries.get(stream);\n }\n\n /** @inheritDoc */\n async set<TState extends Schema>(\n stream: string,\n entry: CacheEntry<TState>\n ): Promise<void> {\n this._entries.set(stream, entry);\n }\n\n /** @inheritDoc */\n async invalidate(stream: string): Promise<void> {\n this._entries.delete(stream);\n }\n\n /** @inheritDoc */\n async clear(): Promise<void> {\n this._entries.clear();\n }\n\n /** @inheritDoc */\n async dispose(): Promise<void> {\n this._entries.clear();\n }\n\n /** Current number of entries held by the LRU. */\n get size(): number {\n return this._entries.size;\n }\n}\n","/**\n * @packageDocumentation\n * Configuration utilities for Act Framework environment, logging, and package metadata.\n *\n * Provides type-safe configuration loading and validation using Zod schemas.\n *\n * @module config\n */\nimport * as fs from \"node:fs\";\nimport { z } from \"zod\";\nimport { log } from \"./ports.js\";\nimport {\n type Environment,\n Environments,\n type LogLevel,\n LogLevels,\n} from \"./types/index.js\";\nimport { extend } from \"./utils.js\";\n\n/**\n * Zod schema for validating package.json metadata.\n * @internal\n */\nexport const PackageSchema = z.object({\n name: z.string().min(1),\n version: z.string().min(1),\n description: z.string().min(1).optional(),\n author: z\n .object({ name: z.string().min(1), email: z.string().optional() })\n .optional()\n .or(z.string().min(1))\n .optional(),\n license: z.string().min(1).optional(),\n dependencies: z.record(z.string(), z.string()).optional(),\n});\n\n/**\n * Type representing the validated package.json metadata.\n *\n * @internal\n */\nexport type Package = z.infer<typeof PackageSchema>;\n\n/**\n * Fallback package metadata when `package.json` can't be read at module\n * load — happens when the framework is consumed from a CWD that doesn't\n * have one (bundled CLIs, Lambda layers, embedded scripts) or when the\n * file exists but is malformed.\n *\n * The values are deliberately synthetic so callers spot them immediately:\n * `config().name === \"act-fallback\"` is a runtime signal that the framework\n * couldn't load the host project's package.json.\n *\n * @internal\n */\nconst FALLBACK_PACKAGE: Package = {\n name: \"act-fallback\",\n version: \"0.0.0-fallback\",\n description: \"Synthetic fallback — package.json could not be loaded\",\n};\n\n/**\n * Loads and parses the local package.json file as a Package object. On\n * any read or parse failure, falls back to {@link FALLBACK_PACKAGE} and\n * stashes the error so {@link config} can surface it on first access —\n * we can't call `log()` here because the logger port memoizes on first\n * call and locking it at module load defeats user injection.\n *\n * @internal\n */\nconst get_package = (): Package => {\n try {\n const raw = fs.readFileSync(\"package.json\");\n return JSON.parse(raw.toString()) 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 = PackageSchema.extend({\n env: z.enum(Environments),\n logLevel: z.enum(LogLevels),\n logSingleLine: z.boolean(),\n sleepMs: z.number().int().min(0).max(5000),\n});\n\n/**\n * Type representing the validated Act Framework configuration object.\n */\nexport type Config = z.infer<typeof BaseSchema>;\n\nconst { NODE_ENV, LOG_LEVEL, LOG_SINGLE_LINE, SLEEP_MS } = process.env;\n\nconst env = (NODE_ENV || \"development\") as Environment;\nconst logLevel = (LOG_LEVEL ||\n (NODE_ENV === \"test\"\n ? \"fatal\"\n : NODE_ENV === \"production\"\n ? \"info\"\n : \"trace\")) as LogLevel;\nconst logSingleLine = (LOG_SINGLE_LINE || \"true\") === \"true\";\nconst sleepMs = parseInt(NODE_ENV === \"test\" ? \"0\" : (SLEEP_MS ?? \"100\"), 10);\n\nconst pkg = get_package();\n\n// Lazily validated on first call. Cannot run extend() at module load\n// because of a utils.ts <-> config.ts cycle (utils imports config for\n// sleep()'s default). Inputs are frozen after import, so the cached\n// result is stable for the life of the process.\nlet _validated: Config | undefined;\n\n/**\n * Gets the current Act Framework configuration.\n *\n * Configuration is loaded from package.json and environment variables, providing\n * type-safe access to application metadata and runtime settings.\n *\n * **Environment Variables:**\n * - `NODE_ENV`: \"development\" | \"test\" | \"staging\" | \"production\" (default: \"development\")\n * - `LOG_LEVEL`: \"fatal\" | \"error\" | \"warn\" | \"info\" | \"debug\" | \"trace\"\n * - `LOG_SINGLE_LINE`: \"true\" | \"false\" (default: \"true\")\n * - `SLEEP_MS`: Milliseconds for sleep utility (default: 100, 0 for tests)\n *\n * **Defaults by environment:**\n * - test: logLevel=\"error\", sleepMs=0\n * - production: logLevel=\"info\"\n * - development: logLevel=\"trace\"\n *\n * @returns The validated configuration object\n *\n * @example Basic usage\n * ```typescript\n * import { config } from \"@rotorsoft/act\";\n *\n * const cfg = config();\n * console.log(`App: ${cfg.name} v${cfg.version}`);\n * console.log(`Environment: ${cfg.env}`);\n * console.log(`Log level: ${cfg.logLevel}`);\n * ```\n *\n * @example Environment-specific behavior\n * ```typescript\n * import { config } from \"@rotorsoft/act\";\n *\n * const cfg = config();\n *\n * if (cfg.env === \"production\") {\n * // Use PostgreSQL in production\n * store(new PostgresStore(prodConfig));\n * } else {\n * // Use in-memory store for dev/test\n * store(new InMemoryStore());\n * }\n * ```\n *\n * @example Adjusting log levels\n * ```typescript\n * // Set via environment variable:\n * // LOG_LEVEL=debug npm start\n *\n * // Or check in code:\n * const cfg = config();\n * if (cfg.logLevel === \"trace\") {\n * logger.trace(\"Detailed debugging enabled\");\n * }\n * ```\n *\n * @see {@link Config} for configuration type\n */\nexport const config = (): Config => {\n if (!_validated) {\n _validated = extend(\n { ...pkg, env, logLevel, logSingleLine, sleepMs },\n BaseSchema\n );\n if (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 { AsyncLocalStorage } from \"node:async_hooks\";\nimport { ConsoleLogger } from \"./adapters/console-logger.js\";\nimport { InMemoryCache } from \"./adapters/in-memory-cache.js\";\nimport { InMemoryStore } from \"./adapters/in-memory-store.js\";\nimport { config } from \"./config.js\";\nimport type {\n Cache,\n Disposable,\n Disposer,\n Logger,\n Store,\n} from \"./types/index.js\";\n\n/** Per-Act ports bag (ACT-501). Both required together — a shared cache across stores would collide on stream keys. */\nexport type Scoped = {\n readonly store: Store;\n readonly cache: Cache;\n};\n\n/** AsyncLocalStorage carrying the active Act's ports. Internal — not re-exported. */\nexport const scoped = new AsyncLocalStorage<Scoped>();\n\n/**\n * Port/adapter infrastructure for the Act framework.\n *\n * All infrastructure concerns (logging, storage, caching) are managed as\n * singleton adapters injected via port functions. Each port follows the same\n * pattern: first call wins with a sensible default, optional adapter injection.\n *\n * - `log()` — structured logging (default: ConsoleLogger)\n * - `store()` — event persistence (default: InMemoryStore)\n * - `cache()` — state checkpoints (default: InMemoryCache)\n * - `dispose()` — register cleanup functions for graceful shutdown\n *\n * @module ports\n */\n\n/**\n * List of exit codes for process termination. Consumed by signal handlers\n * and {@link disposeAndExit}; not part of the user-facing surface.\n *\n * @internal\n */\nexport const ExitCodes = [\"ERROR\", \"EXIT\"] as const;\n\n/**\n * Type for allowed exit codes.\n *\n * - `\"ERROR\"` — abnormal termination (uncaught exception, unhandled rejection)\n * - `\"EXIT\"` — clean shutdown (SIGINT, SIGTERM, or manual trigger)\n *\n * @internal\n */\nexport type ExitCode = (typeof ExitCodes)[number];\n\n// ---------------------------------------------------------------------------\n// Port factory\n// ---------------------------------------------------------------------------\n\n/**\n * Factory function that creates or returns the injected adapter.\n * @internal\n */\ntype Injector<Port extends Disposable> = (adapter?: Port) => Port;\n\n/** Singleton adapter registry, keyed by injector function name. */\nconst adapters = new Map<string, Disposable>();\n\n/**\n * Creates a singleton port with optional adapter injection.\n *\n * The first call initializes the adapter (using the provided adapter or the\n * injector's default). Subsequent calls return the cached singleton. Adapters\n * are disposed in reverse registration order during {@link disposeAndExit}.\n *\n * @param injector - Named function that creates the default adapter\n * @returns Port function: call with no args to get the singleton, or pass an\n * adapter on the first call to override the default\n *\n * @example\n * ```typescript\n * const store = port(function store(adapter?: Store) {\n * return adapter || new InMemoryStore();\n * });\n * const s = store(); // InMemoryStore\n * ```\n */\nexport function port<Port extends Disposable>(injector: Injector<Port>) {\n return (adapter?: Port): Port => {\n if (!adapters.has(injector.name)) {\n const injected = injector(adapter);\n adapters.set(injector.name, injected);\n // log() is now in adapters (or this IS the log port we just registered),\n // so the recursive call resolves immediately. Routing through the logger\n // means level gating (e.g. silenced in tests at fatal) takes effect.\n log().info(`[act] + ${injector.name}:${injected.constructor.name}`);\n }\n return adapters.get(injector.name) as Port;\n };\n}\n\n// ---------------------------------------------------------------------------\n// Ports: log, store, cache\n// ---------------------------------------------------------------------------\n\n/**\n * Gets or injects the singleton logger.\n *\n * By default, Act uses a built-in {@link ConsoleLogger} that emits JSON lines\n * in production (compatible with GCP, AWS CloudWatch, Datadog) and colorized\n * output in development — zero external dependencies.\n *\n * For pino, inject a `PinoLogger` from `@rotorsoft/act-pino` before building\n * your application.\n *\n * @param adapter - Optional logger implementation to inject\n * @returns The singleton logger instance\n *\n * @example Default console logger\n * ```typescript\n * import { log } from \"@rotorsoft/act\";\n * const logger = log();\n * logger.info(\"Application started\");\n * ```\n *\n * @example Injecting pino\n * ```typescript\n * import { log } from \"@rotorsoft/act\";\n * import { PinoLogger } from \"@rotorsoft/act-pino\";\n * log(new PinoLogger({ level: \"debug\", pretty: true }));\n * ```\n *\n * @see {@link Logger} for the interface contract\n * @see {@link ConsoleLogger} for the default implementation\n */\nexport const log = port(function log(adapter?: Logger) {\n const cfg = config();\n return (\n adapter ||\n new ConsoleLogger({\n level: cfg.logLevel,\n pretty: cfg.env !== \"production\",\n })\n );\n});\n\n/**\n * Gets or injects the singleton event store.\n *\n * By default, Act uses an {@link InMemoryStore} suitable for development and\n * testing. For production, inject a persistent store like `PostgresStore` from\n * `@rotorsoft/act-pg` before building your application.\n *\n * **Important:** Store injection must happen before creating any Act instances.\n * Once set, the store cannot be changed without restarting the application.\n *\n * @param adapter - Optional store implementation to inject\n * @returns The singleton store instance\n *\n * @example Default in-memory store\n * ```typescript\n * import { store } from \"@rotorsoft/act\";\n * const s = store();\n * ```\n *\n * @example Injecting PostgreSQL\n * ```typescript\n * import { store } from \"@rotorsoft/act\";\n * import { PostgresStore } from \"@rotorsoft/act-pg\";\n *\n * store(new PostgresStore({\n * host: \"localhost\",\n * port: 5432,\n * database: \"myapp\",\n * user: \"postgres\",\n * password: \"secret\",\n * }));\n * ```\n *\n * @see {@link Store} for the interface contract\n * @see {@link InMemoryStore} for the default implementation\n */\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 return adapter ?? new InMemoryStore();\n});\n\nexport const store = ((adapter?: Store): Store => {\n return scoped.getStore()?.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 scoped.getStore()?.cache ?? _cache(adapter);\n}) as (adapter?: Cache) => Cache;\n\n// ---------------------------------------------------------------------------\n// Disposal\n// ---------------------------------------------------------------------------\n\n/** Registered cleanup functions, executed in reverse order during shutdown. */\nconst disposers: Disposer[] = [];\n\n/**\n * Disposes all registered adapters and disposers, then exits the process.\n *\n * Execution order:\n * 1. Custom disposers (registered via {@link dispose}) — in reverse order\n * 2. Port adapters (log, store, cache) — in reverse registration order\n * 3. Adapter registry is cleared\n * 4. Process exits (skipped in test environment)\n *\n * In production, `\"ERROR\"` exits are silently ignored to avoid crashing on\n * transient failures (e.g. an uncaught promise in a non-critical path).\n *\n * @param code - Exit code: `\"EXIT\"` for clean shutdown (exit 0),\n * `\"ERROR\"` for abnormal termination (exit 1)\n */\nexport async function disposeAndExit(code: ExitCode = \"EXIT\"): Promise<void> {\n if (code === \"ERROR\" && config().env === \"production\") {\n // Surface the swallow so incident triage can see it. Without this\n // log the framework looks unresponsive after an uncaught exception\n // in prod — exactly when operators most need a breadcrumb.\n log().warn(\n \"disposeAndExit('ERROR') ignored in production — process kept alive\"\n );\n return;\n }\n\n // Run sequentially in reverse registration order so a disposer can rely on\n // later-registered disposers (and adapters on later-registered adapters)\n // having already finished — Promise.all would race them.\n for (const disposer of [...disposers].reverse()) {\n await disposer();\n }\n for (const adapter of [...adapters.values()].reverse()) {\n await adapter.dispose();\n log().info(`[act] - ${adapter.constructor.name}`);\n }\n adapters.clear();\n config().env !== \"test\" && process.exit(code === \"ERROR\" ? 1 : 0);\n}\n\n/**\n * Registers a cleanup function for graceful shutdown.\n *\n * Disposers are called automatically on SIGINT, SIGTERM, uncaught exceptions,\n * and unhandled rejections. They execute in reverse registration order before\n * port adapters are disposed.\n *\n * @param disposer - Async function to call during cleanup. Omit to get a\n * reference to {@link disposeAndExit} without registering.\n * @returns Function to manually trigger disposal and exit\n *\n * @example\n * ```typescript\n * import { dispose } from \"@rotorsoft/act\";\n *\n * const db = connectDatabase();\n * dispose(async () => await db.close());\n *\n * // In tests\n * afterAll(async () => await dispose()());\n * ```\n *\n * @see {@link disposeAndExit} for the full shutdown sequence\n */\nexport function dispose(\n disposer?: Disposer\n): (code?: ExitCode) => Promise<void> {\n disposer && disposers.push(disposer);\n return disposeAndExit;\n}\n\n// ---------------------------------------------------------------------------\n// Constants\n// ---------------------------------------------------------------------------\n\n/**\n * Event name used internally for snapshot events in the event store.\n * Snapshot events store a full state checkpoint, enabling efficient cold-start\n * recovery without replaying the entire event stream.\n */\nexport const SNAP_EVENT = \"__snapshot__\";\n\n/**\n * Event name used internally for tombstone events in the event store.\n * A tombstone marks a stream as permanently closed — no further writes\n * are permitted until the stream is explicitly restarted via `close()`.\n *\n * @see {@link Act.close} for the close-the-books API\n */\nexport const TOMBSTONE_EVENT = \"__tombstone__\";\n\n/**\n * Name of the implicit lane every reaction lands in unless its `.to({lane})`\n * declaration says otherwise (ACT-1103). Acts that don't call\n * `.withLane(...)` see only this lane, and behavior is identical to\n * pre-1103 single-controller drain.\n *\n * Persisted on `streams.lane` and threaded as the strict-typed default in\n * builder generics — `lane?: TLanes` always includes `\"default\"`.\n */\nexport const DEFAULT_LANE = \"default\";\n","import { prettifyError, ZodError, type ZodType } from \"zod\";\nimport { config } from \"./config.js\";\nimport { ValidationError } from \"./types/index.js\";\n\n/**\n * @module utils\n * @category Utilities\n *\n * Small utilities used across the framework:\n * - {@link validate} — parse a payload against a Zod schema, throwing\n * {@link ValidationError} on failure.\n * - {@link extend} — validate a source object and merge into defaults.\n * - {@link sleep} — async delay (default duration from `config().sleepMs`).\n */\n\n/**\n * Parse `payload` against `schema`, returning the validated value or throwing\n * a {@link ValidationError} with prettified Zod details. When `schema` is\n * omitted, returns `payload` unchanged. The framework calls this for every\n * `app.do()` action, every emitted event, and every state init.\n *\n * @example\n * ```typescript\n * const UserSchema = z.object({ email: z.string().email() });\n * const user = validate(\"User\", { email: \"alice@example.com\" }, UserSchema);\n * ```\n *\n * @see {@link ValidationError}\n */\nexport const validate = <S>(\n target: string,\n payload: Readonly<S>,\n schema?: ZodType<S>\n): Readonly<S> => {\n try {\n return schema ? schema.parse(payload) : payload;\n } catch (error) {\n if (error instanceof ZodError) {\n throw new ValidationError(target, payload, prettifyError(error));\n }\n throw new ValidationError(target, payload, error);\n }\n};\n\n/**\n * Validate `source` against `schema` and return a new object that merges\n * `source` over the optional `target` defaults. Used by {@link config} for\n * env-var-overrides-defaults patterns; safe to call elsewhere — it never\n * mutates `target`.\n *\n * @example\n * ```typescript\n * const schema = z.object({ host: z.string(), port: z.number() });\n * const cfg = extend({ port: 8080 }, schema, { host: \"localhost\", port: 80 });\n * // → { host: \"localhost\", port: 8080 }\n * ```\n *\n * @throws {@link ValidationError} if `source` fails the schema.\n */\nexport const extend = <\n S extends Record<string, unknown>,\n T extends Record<string, unknown>,\n>(\n source: Readonly<S>,\n schema: ZodType<S>,\n target?: Readonly<T>\n): Readonly<S & T> => {\n const value = validate(\"config\", source, schema);\n return { ...target, ...value } as Readonly<S & T>;\n};\n\n/**\n * Pause for `ms` milliseconds (or `config().sleepMs` when omitted — `100ms`\n * in dev, `0ms` in tests). Used by adapters to simulate async I/O.\n *\n * @example\n * ```typescript\n * await sleep(); // default delay from config\n * await sleep(500); // explicit 500ms\n * ```\n */\nexport async function sleep(ms?: number) {\n return new Promise((resolve) => setTimeout(resolve, ms ?? config().sleepMs));\n}\n\n/**\n * Regex metacharacters that, when present in a reaction `source`, make it a\n * pattern rather than a literal stream name. A source containing none of\n * these is a bare stream name — the common case — and every claim/fetch\n * site matches it by string equality on the store's stream index. A source\n * containing any of them is compiled as a RegExp and matched against\n * candidate streams with the caller's own anchoring (e.g. `^(A|B)$`).\n */\nconst SOURCE_METACHARACTERS = /[\\^$.*+?()[\\]{}|\\\\]/;\n\n/**\n * True when `source` is a **literal** stream name — it carries no regex\n * metacharacter, so every adapter treats it as an exact match. This is the\n * fast, index-friendly path and covers every autoclose/dynamic-resolver\n * source (bare stream names). A `false` return means the source is a\n * **pattern** (contains `^ $ . * + ? ( ) [ ] { } | \\`) and must be compiled\n * as a RegExp before matching — the shape the calculator's static\n * `source: \"^(A|B)$\"` reaction relies on.\n *\n * The single source of truth for literal-vs-pattern classification across\n * the InMemory has-work probe, the drain fetch path, and the SQL adapters,\n * so all three agree on which sources take the exact path.\n *\n * @example\n * ```typescript\n * is_literal_source(\"Board\"); // → true (exact lookup)\n * is_literal_source(\"^(A|B)$\"); // → false (compile as RegExp)\n * ```\n */\nexport function is_literal_source(source: string): boolean {\n return !SOURCE_METACHARACTERS.test(source);\n}\n","/**\n * @packageDocumentation\n * @module act/adapters\n * In-memory event store adapter for the Act Framework.\n *\n * This adapter implements the Store interface and is suitable for development, testing, and demonstration purposes.\n * All data is stored in memory and lost on process exit.\n *\n * @category Adapters\n */\n\nimport { DEFAULT_LANE, SNAP_EVENT, TOMBSTONE_EVENT } from \"../ports.js\";\nimport { ConcurrencyError } from \"../types/errors.js\";\nimport type {\n BlockedLease,\n Committed,\n EventMeta,\n Lease,\n Message,\n Query,\n QueryStatsOptions,\n QueryStreams,\n QueryStreamsResult,\n Schema,\n Schemas,\n Store,\n StreamFilter,\n StreamPosition,\n StreamStats,\n} from \"../types/index.js\";\nimport { is_literal_source, sleep } from \"../utils.js\";\n\n/**\n * @internal\n * Represents an in-memory stream for event processing and leasing.\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 = 0;\n private _lane: string = DEFAULT_LANE;\n // Persisted next-visit time (#1090). When set and still in the future, the\n // stream is held out of `claim` entirely — so a deferred reaction is not\n // re-claimed (and `retry` is never bumped) until its due-time passes. Unlike\n // in-process backoff, this is durable store state shared across workers.\n private _deferred_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 priority() {\n return this._priority;\n }\n\n get lane() {\n return this._lane;\n }\n\n /** Replace on every subscribe — current builder config wins on restart. */\n set lane(value: string) {\n this._lane = value;\n }\n\n /**\n * Bump the priority via {@link subscribe}: keeps the maximum across\n * reactions so the highest-priority registrant wins.\n */\n bump_priority(priority: number) {\n if (priority > this._priority) this._priority = priority;\n }\n\n /**\n * Set the priority outright via {@link prioritize}: operator\n * runtime override that ignores the build-time `max()` invariant.\n */\n set_priority(priority: number) {\n this._priority = priority;\n }\n\n get is_available() {\n return (\n !this._blocked &&\n (!this._leased_until || this._leased_until <= new Date()) &&\n // A stream deferred to a future time is not claimable until due (#1090).\n (!this._deferred_at || this._deferred_at <= Date.now())\n );\n }\n\n /**\n * Hold this stream out of `claim` until `deferred_at` (ms since epoch).\n * Set by a deliberate `defer` outcome — not a failure, so retry/blocked\n * state is untouched. Cleared by ack/block/reset/unblock.\n */\n defer(deferred_at: number) {\n this._deferred_at = deferred_at;\n // A defer is not a failure: reset retry so the redelivery after the\n // due-time is a fresh attempt, never accumulating toward maxRetries.\n this._retry = -1;\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 /** Persisted next-visit time (#1090/#1221), or undefined when no active defer. */\n get deferred_at() {\n return this._deferred_at;\n }\n\n /**\n * Attempt to lease this stream for processing.\n * @param lease - The lease request.\n * @param millis - Lease duration in milliseconds.\n * @returns The granted lease or undefined if blocked.\n */\n lease(lease: Lease, millis: number): Lease {\n if (millis > 0) {\n this._leased_by = lease.by;\n this._leased_until = new Date(Date.now() + millis);\n }\n this._retry = this._retry + 1;\n return {\n stream: this.stream,\n source: this.source,\n at: lease.at,\n by: lease.by,\n retry: this._retry,\n lagging: lease.lagging,\n lane: this._lane,\n };\n }\n\n /**\n * Finalize this stream's lease: ack (advance the watermark) or, when the\n * lease carries a `due` marker, defer (persist the schedule, hold the\n * watermark) — see {@link Store.ack}.\n * @param lease - The lease request.\n */\n ack(lease: Lease) {\n if (this._leased_by === lease.by) {\n this._leased_by = undefined;\n this._leased_until = undefined;\n this._retry = -1;\n if (lease.due !== undefined) {\n // Defer marker: schedule the re-visit, keep the watermark.\n // Deferred entries are not part of ack's return value.\n this._deferred_at = lease.due;\n return undefined;\n }\n this._at = lease.at;\n // Advancing the watermark ends any active defer schedule.\n this._deferred_at = undefined;\n return {\n stream: this.stream,\n source: this.source,\n at: this._at,\n by: lease.by,\n retry: this._retry,\n lagging: lease.lagging,\n lane: this._lane,\n };\n }\n }\n\n /**\n * Block a stream for processing after failing to process and reaching max retries with blocking enabled.\n * @param lease - The lease request.\n * @param error Blocked error message.\n */\n block(lease: Lease, error: string) {\n if (this._leased_by === lease.by) {\n this._blocked = true;\n this._error = error;\n // A blocked stream is poison; clear any pending defer (#1090).\n this._deferred_at = undefined;\n return {\n stream: this.stream,\n source: this.source,\n at: this._at,\n by: this._leased_by,\n retry: this._retry,\n error: this._error,\n lagging: lease.lagging,\n lane: this._lane,\n };\n }\n }\n\n /**\n * Reset this stream's watermark and state for replay. The retry counter\n * resets to -1 to match the constructor + ack() invariant (\"released\n * stream\"); the next claim() bumps it to 0 (first attempt).\n */\n reset() {\n this._at = -1;\n this._retry = -1;\n this._blocked = false;\n this._error = \"\";\n this._leased_by = undefined;\n this._leased_until = undefined;\n this._deferred_at = undefined;\n }\n\n /**\n * Clear the blocked flag and lease bookkeeping without touching the\n * watermark. Returns true if the stream was actually blocked (and is\n * now flipped); false otherwise.\n */\n unblock(): boolean {\n if (!this._blocked) return false;\n this._blocked = false;\n this._retry = -1;\n this._error = \"\";\n this._leased_by = undefined;\n this._leased_until = undefined;\n this._deferred_at = undefined;\n return true;\n }\n}\n\n/**\n * In-memory event store implementation.\n *\n * This is the default store used by Act when no other store is injected.\n * It stores all events in memory and is suitable for:\n * - Development and prototyping\n * - Unit and integration testing\n * - Demonstrations and examples\n *\n * **Not suitable for production** - all data is lost when the process exits.\n * Use {@link PostgresStore} for production deployments.\n *\n * The in-memory store provides:\n * - Full {@link Store} interface implementation\n * - Optimistic concurrency control\n * - Stream leasing for distributed processing simulation\n * - Snapshot support\n * - Fast performance (no I/O overhead)\n *\n * **`Store.notify` is intentionally not implemented.** The notify hook is a\n * cross-process wake-up signal — local commits already arm the drain via\n * `do()`. An in-memory store is single-process by definition, so there is\n * no remote writer to be notified of. The {@link Act} orchestrator\n * detects the absence and falls back to the existing debounce/poll path.\n *\n * @example Using in tests\n * ```typescript\n * import { store } from \"@rotorsoft/act\";\n *\n * describe(\"Counter\", () => {\n * beforeEach(async () => {\n * // Reset store between tests\n * await store().seed();\n * });\n *\n * it(\"increments\", async () => {\n * await app.do(\"increment\", target, { by: 5 });\n * const snapshot = await app.load(Counter, \"counter-1\");\n * expect(snapshot.state.count).toBe(5);\n * });\n * });\n * ```\n *\n * @example Explicit instantiation\n * ```typescript\n * import { InMemoryStore } from \"@rotorsoft/act\";\n *\n * const testStore = new InMemoryStore();\n * await testStore.seed();\n *\n * // Use for specific test scenarios\n * await testStore.commit(\"test-stream\", events, meta);\n * ```\n *\n * @example Querying events\n * ```typescript\n * const events: any[] = [];\n * await store().query(\n * (event) => events.push(event),\n * { stream: \"test-stream\" }\n * );\n * console.log(`Found ${events.length} events`);\n * ```\n *\n * @see {@link Store} for the interface definition\n * @see {@link PostgresStore} for production use\n * @see {@link store} for injecting stores\n *\n * @category Adapters\n */\nexport class InMemoryStore implements Store {\n // stored events\n private _events: Committed<Schemas, keyof Schemas>[] = [];\n // next event id — monotonic, never reused. Deletions (truncate, windowed\n // prune) punch holes in the id sequence, so ids are NOT array indexes and\n // NOT `_events.length`; they only stay sorted ascending in `_events`.\n private _next_id = 0;\n // stored stream positions and other metadata\n private _streams: Map<string, InMemoryStream> = new Map();\n // last committed version per stream — O(1) replacement for filter-on-commit\n private _stream_versions: Map<string, number> = new Map();\n // max non-snapshot event id per stream — drives the has-work probe in\n // claim(): an O(1) lookup for a literal source, and the per-stream max\n // that a pattern source scans, without touching the full event log.\n private _max_event_id_by_stream: Map<string, number> = new Map();\n // global max non-snapshot event id — fast pre-check for source-less streams in claim()\n private _max_non_snap_event_id = -1;\n // stream → (event_id → cloned sensitive payload). Two-level so `forget_pii`\n // is O(1) — drop the inner Map for the stream and the wipe is done — mirroring\n // the `DELETE WHERE stream = ?` scope that durable adapters get from their\n // stream index. Entries exist only for events committed with a non-null\n // `pii` field; absence means \"no PII\" (returned as `null` on load).\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._stream_versions.clear();\n this._max_event_id_by_stream.clear();\n this._max_non_snap_event_id = -1;\n this._pii.clear();\n }\n\n // First index whose event id is greater than `after`. `_events` stays\n // sorted ascending by id (append-only commits; deletions preserve\n // order), so id-bounded scans binary-search their start instead of\n // assuming id === index — an invariant that truncation breaks.\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 // Attach the isolated PII payload (or null) to an event before handing it to\n // a caller. Allocation-free for events without PII — by far the common case.\n private _with_pii<E extends Schemas>(\n e: Committed<E, keyof E>\n ): Committed<E, keyof 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 /**\n * Dispose of the store and clear all events.\n * @returns Promise that resolves when disposal is complete.\n */\n async dispose() {\n await sleep();\n this._reset_indexes();\n }\n\n /**\n * Seed the store with initial data (no-op for in-memory).\n * @returns Promise that resolves when seeding is complete.\n */\n async seed() {\n await sleep();\n }\n\n /**\n * Drop all data from the store.\n * @returns Promise that resolves when the store is cleared.\n */\n async drop() {\n await sleep();\n this._reset_indexes();\n this._streams = new Map();\n }\n\n private in_query<E extends Schemas>(query: Query, e: Committed<E, keyof E>) {\n if (query.stream) {\n if (query.stream_exact) {\n if (e.stream !== query.stream) return false;\n } else if (!RegExp(query.stream).test(e.stream)) return false;\n }\n if (query.names && !query.names.includes(e.name as string)) return false;\n if (query.correlation && e.meta?.correlation !== query.correlation)\n return false;\n if (e.name === SNAP_EVENT && !query.with_snaps) return false;\n return true;\n }\n\n /**\n * Query events in the store, optionally filtered by query options.\n * @param callback - Function to call for each event.\n * @param query - Optional query options.\n * @returns The number of events processed.\n */\n async query<E extends Schemas>(\n callback: (event: Committed<E, keyof E>) => void,\n query?: Query\n ) {\n await sleep();\n let count = 0;\n if (query?.backward) {\n let i =\n (query?.before !== undefined\n ? this._first_index_after(query.before - 1)\n : this._events.length) - 1;\n while (i >= 0) {\n const e = this._events[i--];\n if (query && !this.in_query(query, e)) continue;\n if (query?.created_before && e.created >= query.created_before)\n continue;\n if (query.after !== undefined && e.id <= query.after) break;\n if (query.created_after && e.created <= query.created_after) break;\n await Promise.resolve(\n callback(this._with_pii(e as Committed<E, keyof E>))\n );\n count++;\n if (query?.limit && count >= query.limit) break;\n }\n } else {\n let i = this._first_index_after(query?.after ?? -1);\n // with_snaps resumes at the latest snapshot for an exact single\n // stream (no explicit `after`): start the scan at that snapshot's\n // position so pre-snapshot events aren't read. No snapshot → full\n // scan; an explicit `after` wins.\n if (\n query?.with_snaps &&\n query.stream_exact &&\n query.stream !== undefined &&\n query.after === undefined\n ) {\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) {\n i = j;\n break;\n }\n }\n }\n while (i < this._events.length) {\n const e = this._events[i++];\n if (query && !this.in_query(query, e)) continue;\n if (query?.created_after && e.created <= query.created_after) continue;\n if (query?.before !== undefined && e.id >= query.before) break;\n if (query?.created_before && e.created >= query.created_before) break;\n await Promise.resolve(\n callback(this._with_pii(e as Committed<E, keyof E>))\n );\n count++;\n if (query?.limit && count >= query.limit) break;\n }\n }\n return count;\n }\n\n /**\n * Commit one or more events to a stream.\n * @param stream - The stream name.\n * @param msgs - The events/messages to commit.\n * @param meta - Event metadata.\n * @param expectedVersion - Optional optimistic concurrency check.\n * @returns The committed events with metadata.\n * @throws ConcurrencyError if expectedVersion does not match.\n */\n async commit<E extends Schemas>(\n stream: string,\n msgs: Message<E, keyof E>[],\n meta: EventMeta,\n expectedVersion?: number\n ) {\n await sleep();\n const current_version = this._stream_versions.get(stream) ?? -1;\n if (\n typeof expectedVersion === \"number\" &&\n current_version !== expectedVersion\n ) {\n throw new ConcurrencyError(\n stream,\n current_version,\n msgs as Message<Schemas, keyof Schemas>[],\n expectedVersion\n );\n }\n\n let version = current_version + 1;\n let last_non_snap_id = -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,\n created: new Date(),\n name,\n data,\n meta,\n };\n // The stored event is the pii-less view — `forget_pii` only has to\n // drop the inner Map for the stream, never the event row. Mandatory\n // clone on the pii payload defends against caller-side mutation.\n this._events.push(c as Committed<Schemas, keyof Schemas>);\n if (pii != null) {\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(c.id, structuredClone(pii) as Record<string, unknown>);\n }\n if (name !== SNAP_EVENT) last_non_snap_id = c.id;\n version++;\n return this._with_pii(c);\n });\n this._stream_versions.set(stream, version - 1);\n if (last_non_snap_id >= 0) {\n this._max_event_id_by_stream.set(stream, last_non_snap_id);\n // commit always assigns a fresh id from the monotonic _next_id, so\n // any non-snap commit strictly raises the global max.\n this._max_non_snap_event_id = last_non_snap_id;\n }\n return committed;\n }\n\n /**\n * Atomically discovers and leases streams for processing.\n * Fuses poll + lease into a single operation.\n * @param lagging - Max streams from lagging frontier.\n * @param leading - Max streams from leading frontier.\n * @param by - Lease holder identifier.\n * @param millis - Lease duration in milliseconds.\n * @returns Granted leases.\n */\n async claim(\n lagging: number,\n leading: number,\n by: string,\n millis: number,\n lane?: string\n ) {\n await sleep();\n // Has-work probe. A **literal** `source` (no regex metacharacters) is\n // matched by exact map lookup — the fast, index-friendly path that\n // covers every autoclose/dynamic-resolver source. A **pattern** source\n // (e.g. the calculator's `^(A|B)$`) is compiled to a RegExp once per\n // candidate subscription and tested against every stream whose max\n // event id has advanced past the watermark.\n const has_work = (s: InMemoryStream): boolean => {\n if (s.at < 0) return true;\n if (!s.source) return s.at < this._max_non_snap_event_id;\n if (is_literal_source(s.source)) {\n const max_id = this._max_event_id_by_stream.get(s.source);\n return max_id !== undefined && max_id > s.at;\n }\n const re = new RegExp(s.source);\n for (const [stream_name, max_id] of this._max_event_id_by_stream) {\n if (max_id > s.at && re.test(stream_name)) return true;\n }\n return false;\n };\n const available = [...this._streams.values()].filter(\n (s) =>\n s.is_available && has_work(s) && (lane === undefined || s.lane === lane)\n );\n // Lagging frontier orders by priority DESC (higher first), then by\n // watermark ASC (most-behind first). Mirrors the PG `claim()` SQL\n // — see `libs/act-pg/PERFORMANCE.md` for the benchmark that\n // motivated the priority dimension. A fairness reserve (ACT-1223)\n // carves `fair` slots off the budget and fills them by pure watermark\n // order (priority ignored) so a default-priority lagging stream can\n // never be starved out of the frontier by sustained higher-priority\n // load. With everyone at the same priority both slices order by `at`,\n // so this is a behavioral no-op for existing workloads.\n const fair = lagging >= 2 ? Math.max(1, Math.floor(lagging / 4)) : 0;\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 // deduplicate (a stream can appear in both frontiers)\n const seen = new Set<string>();\n const combined = [...lag, ...lead].filter((p) => {\n if (seen.has(p.stream)) return false;\n seen.add(p.stream);\n return true;\n });\n // lease each atomically\n return combined\n .map((p) =>\n this._streams.get(p.stream)?.lease({ ...p, by, retry: 0 }, millis)\n )\n .filter((l) => !!l);\n }\n\n /**\n * Registers streams for event processing. When the same stream is\n * resubscribed with a different priority, the **maximum** wins — so\n * the highest-priority registered reaction sets the scheduling lane.\n * Use {@link prioritize} for operator runtime overrides.\n *\n * @param streams - Streams to register with optional source + priority.\n * @returns subscribed count and current max watermark.\n */\n async subscribe(\n streams: Array<{\n stream: string;\n source?: string;\n priority?: number;\n lane?: string;\n }>\n ) {\n await sleep();\n let subscribed = 0;\n for (const {\n stream,\n source,\n priority = 0,\n lane = DEFAULT_LANE,\n } of streams) {\n const existing = this._streams.get(stream);\n if (existing) {\n existing.bump_priority(priority);\n existing.lane = lane;\n } else {\n this._streams.set(\n stream,\n new InMemoryStream(stream, source, priority, lane)\n );\n subscribed++;\n }\n }\n let watermark = -1;\n for (const s of this._streams.values()) {\n if (s.at > watermark) watermark = s.at;\n }\n return { subscribed, watermark };\n }\n\n /**\n * Acknowledge completion of processing for leased streams.\n * @param leases - Leases to acknowledge, including last processed watermark and lease holder.\n */\n async ack(leases: Lease[]) {\n await sleep();\n // Acks and defer schedules land in one synchronous pass — the\n // in-memory equivalent of the single-transaction contract on\n // {@link Store.ack}: no await between entries, so a caller\n // never observes a cycle's acks without its schedules. `due`-carrying\n // entries defer (and return undefined), the rest ack.\n return leases\n .map((l) => this._streams.get(l.stream)?.ack(l))\n .filter((l) => !!l);\n }\n\n /**\n * Block a stream for processing after failing to process and reaching max retries with blocking enabled.\n * @param leases - Leases to block, including lease holder and last error message.\n * @returns Blocked leases.\n */\n async block(leases: BlockedLease[]) {\n await sleep();\n return leases\n .map((l) => this._streams.get(l.stream)?.block(l, l.error))\n .filter((l) => !!l);\n }\n\n /**\n * Hold the matched streams out of {@link claim} until `deferred_at`\n * (ms since epoch) — see {@link Store.defer}. Accepts an explicit list\n * of names or a {@link StreamFilter}, mirroring {@link reset}/{@link unblock}.\n * Persisted store state (unlike in-process backoff), so the skip is honored\n * by every competing worker. Unknown names are silently skipped.\n *\n * @returns Count of streams whose `deferred_at` was set.\n */\n async defer(input: string[] | StreamFilter, deferred_at: number) {\n await sleep();\n let count = 0;\n if (Array.isArray(input)) {\n for (const name of input) {\n const s = this._streams.get(name);\n if (s) {\n s.defer(deferred_at);\n count++;\n }\n }\n } else {\n const matches = this._filter_predicate(input);\n for (const s of this._streams.values()) {\n if (matches(s)) {\n s.defer(deferred_at);\n count++;\n }\n }\n }\n return count;\n }\n\n /**\n * Build a predicate from a {@link StreamFilter}. Compiled regexes are\n * cached in the closure so callers can apply it across the streams\n * map without re-compiling per iteration.\n */\n private _filter_predicate(\n filter: StreamFilter\n ): (s: InMemoryStream) => boolean {\n const stream_re =\n filter.stream && !filter.stream_exact\n ? new RegExp(filter.stream)\n : undefined;\n const source_re =\n filter.source && !filter.source_exact\n ? new RegExp(filter.source)\n : undefined;\n return (s) => {\n if (filter.stream !== undefined) {\n if (\n filter.stream_exact\n ? s.stream !== filter.stream\n : !stream_re!.test(s.stream)\n )\n return false;\n }\n if (filter.source !== undefined) {\n if (s.source === undefined) return false;\n if (\n filter.source_exact\n ? s.source !== filter.source\n : !source_re!.test(s.source)\n )\n return false;\n }\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 /**\n * Reset watermarks to -1, clearing retry, blocked, error, and lease\n * state so the matched streams can be replayed from the beginning.\n * Accepts either an explicit list of names or a {@link StreamFilter}.\n *\n * @param input - Stream names or a filter selecting the streams to reset.\n * @returns Count of streams that were actually reset.\n */\n async reset(input: string[] | StreamFilter) {\n await sleep();\n let count = 0;\n if (Array.isArray(input)) {\n for (const name of input) {\n const s = this._streams.get(name);\n if (s) {\n s.reset();\n count++;\n }\n }\n } else {\n const matches = this._filter_predicate(input);\n for (const s of this._streams.values()) {\n if (matches(s)) {\n s.reset();\n count++;\n }\n }\n }\n return count;\n }\n\n /**\n * Clear the blocked flag (and retry / error / lease) on the matched\n * streams without touching the watermark. Streams that aren't blocked\n * at call time are silently skipped. Accepts either an explicit list\n * of names or a {@link StreamFilter}. The filter form always restricts\n * to blocked streams — passing `blocked: false` matches nothing.\n * See {@link Store.unblock}.\n *\n * @param input - Stream names or a filter selecting the streams to unblock.\n * @returns Count of streams that were actually flipped (were blocked).\n */\n /**\n * Wipe the sensitive-data payload for every event on the stream — see\n * {@link Store.forget_pii}. O(1) drop of the stream's inner Map; the size of\n * that Map is the count of events that had PII. Idempotent: a second call\n * finds no inner Map and returns `0`.\n *\n * @param stream - Target stream.\n * @returns Count of events whose isolated PII payload was deleted.\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 let count = 0;\n if (Array.isArray(input)) {\n for (const name of input) {\n const s = this._streams.get(name);\n if (s?.unblock()) count++;\n }\n } else {\n // Filter form: always restrict to blocked streams. An explicit\n // `blocked: false` in the filter is silently overridden — there\n // is no use case for \"unblock unblocked streams.\"\n const matches = this._filter_predicate({ ...input, blocked: true });\n for (const s of this._streams.values()) {\n if (matches(s) && s.unblock()) count++;\n }\n }\n return count;\n }\n\n /**\n * Bulk-update priority of streams matching `filter`. Mirrors\n * {@link query_streams}'s filter semantics — see {@link Store.prioritize}.\n * Unlike {@link subscribe} (which keeps `max()` of registered\n * priorities), this sets the priority outright — operator override\n * for the build-time scheduling policy.\n *\n * @returns Count of streams whose priority changed.\n */\n async prioritize(filter: StreamFilter, priority: number) {\n await sleep();\n const matches = this._filter_predicate(filter);\n let count = 0;\n for (const s of this._streams.values()) {\n if (!matches(s)) continue;\n if (s.priority !== priority) {\n s.set_priority(priority);\n count++;\n }\n }\n return count;\n }\n\n /**\n * Streams registered subscription positions to the callback, ordered by\n * stream name. Returns the highest event id in the store and the count\n * of positions emitted.\n */\n async query_streams(\n callback: (position: StreamPosition) => void,\n query?: QueryStreams\n ): Promise<QueryStreamsResult> {\n await sleep();\n const limit = query?.limit ?? 100;\n const after = query?.after;\n const blocked = query?.blocked;\n const source_matches = query?.source_matches;\n const stream_re =\n query?.stream && !query.stream_exact\n ? new RegExp(query.stream)\n : undefined;\n const source_re =\n query?.source && !query.source_exact\n ? new RegExp(query.source)\n : undefined;\n // Reverse-match: a stream qualifies when its stored `source` pattern\n // matches at least one of the requested names. Patterns are compiled\n // once and cached — many subscriptions share one source pattern.\n const reverse_cache = new Map<string, RegExp>();\n const reverse_match = (source: string): boolean => {\n let re = reverse_cache.get(source);\n if (!re) {\n re = new RegExp(source);\n reverse_cache.set(source, re);\n }\n return source_matches!.some((name) => re!.test(name));\n };\n\n const sorted = [...this._streams.values()].sort((a, b) =>\n a.stream.localeCompare(b.stream)\n );\n\n let count = 0;\n for (const s of sorted) {\n if (after !== undefined && s.stream <= after) continue;\n if (query?.stream !== undefined) {\n if (\n query.stream_exact\n ? s.stream !== query.stream\n : !stream_re!.test(s.stream)\n )\n continue;\n }\n if (query?.source !== undefined) {\n if (s.source === undefined) continue;\n if (\n query.source_exact\n ? s.source !== query.source\n : !source_re!.test(s.source)\n )\n continue;\n }\n if (source_matches !== undefined) {\n // Absent/empty source = no source constraint = consumes from\n // every stream, so it matches any requested name. Only a\n // present source that matches none of them is excluded.\n if (s.source && !reverse_match(s.source)) continue;\n }\n if (blocked !== undefined && s.blocked !== blocked) continue;\n if (query?.lane !== undefined && s.lane !== query.lane) continue;\n callback({\n stream: s.stream,\n source: s.source,\n at: s.at,\n retry: s.retry,\n blocked: s.blocked,\n error: s.error,\n 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 });\n count++;\n if (count >= limit) break;\n }\n return { maxEventId: this._events.at(-1)?.id ?? -1, count };\n }\n\n /**\n * Per-stream aggregated stats — see {@link Store.query_stats}.\n *\n * Single forward scan over the in-memory event list, accumulating per\n * stream. The \"cheap heads\" cost tier from durable adapters doesn't\n * apply here (InMemory has no indexes); correctness is the goal, perf\n * is a non-issue.\n *\n * Scope rules:\n * - Array `input` — explicit stream names, regardless of subscription.\n * - Filter `input` — `stream`/`stream_exact` match against event-bearing\n * stream names; `source`/`source_exact`/`blocked` require a\n * corresponding subscription in `_streams` (those are subscription\n * concepts, not event concepts). Empty filter `{}` matches every\n * event-bearing stream.\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 // Pre-compile per-stream scope predicate, cached as we go so each\n // distinct stream evaluates the regex once.\n const array_targets = Array.isArray(input) ? new Set(input) : null;\n const filter = Array.isArray(input) ? null : input;\n const stream_re =\n filter?.stream && !filter.stream_exact\n ? new RegExp(filter.stream)\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 let ok = true;\n if (array_targets) {\n ok = array_targets.has(stream);\n } else if (filter?.stream !== undefined) {\n ok = filter.stream_exact\n ? stream === filter.stream\n : // stream_re set when stream is regex\n stream_re!.test(stream);\n }\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 initialized above when want_names\n a.names![n] = (a.names![n] ?? 0) + 1;\n }\n }\n\n // Order by stream name so `after`/`limit` keyset-paginate\n // deterministically (matches query_streams ordering).\n const ordered = [...acc.keys()].sort((x, y) => x.localeCompare(y));\n const out = new Map<string, StreamStats<E>>();\n for (const stream of ordered) {\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 if (limit !== undefined && out.size >= limit) break;\n }\n return out;\n }\n\n /**\n * Atomically truncates streams and seeds each with a snapshot or tombstone.\n * Windowed targets (`before` set) prune the prefix below the closest safe\n * `__snapshot__` instead — no seed, subscriptions untouched, no-op when no\n * snapshot qualifies.\n * @param targets - Streams to truncate with optional snapshot state and meta,\n * or a `before`/`max_id` boundary for a windowed prefix delete.\n * @returns Map keyed by stream name, each entry with `deleted` count and `committed` event.\n */\n async truncate(\n targets: Array<{\n stream: string;\n snapshot?: Schema;\n meta?: EventMeta;\n 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 // Windowed targets: pure prefix delete behind the closest safe snapshot.\n const windowed = targets.filter((t) => t.before !== undefined);\n if (windowed.length) {\n const drop = new Set<number>();\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 let deleted = 0;\n for (const e of this._events) {\n if (e.stream === stream && e.id < boundary.id) {\n drop.add(e.id);\n this._pii.get(stream)?.delete(e.id);\n deleted++;\n }\n }\n result.set(stream, { deleted, committed: boundary, before });\n }\n if (drop.size) this._events = this._events.filter((e) => !drop.has(e.id));\n }\n\n const full = targets.filter((t) => t.before === undefined);\n // Count per-stream deletions\n const deleted_counts = new Map<string, number>();\n const stream_set = new Set(full.map((t) => t.stream));\n for (const e of this._events) {\n if (stream_set.has(e.stream)) {\n deleted_counts.set(e.stream, (deleted_counts.get(e.stream) ?? 0) + 1);\n }\n }\n this._events = this._events.filter((e) => !stream_set.has(e.stream));\n for (const stream of stream_set) {\n this._streams.delete(stream);\n this._stream_versions.delete(stream);\n this._max_event_id_by_stream.delete(stream);\n // The pii payloads die with the event rows, matching the durable\n // adapters' `DELETE WHERE stream = ?` scope.\n this._pii.delete(stream);\n }\n for (const { stream, snapshot, meta } of full) {\n const event: Committed<Schemas, keyof Schemas> = {\n id: this._next_id++,\n stream,\n version: 0,\n created: new Date(),\n name: snapshot !== undefined ? SNAP_EVENT : TOMBSTONE_EVENT,\n data: snapshot ?? {},\n meta: meta ?? { correlation: \"\", causation: {} },\n };\n this._events.push(event);\n this._stream_versions.set(stream, 0);\n if (event.name !== SNAP_EVENT) {\n this._max_event_id_by_stream.set(stream, event.id);\n }\n result.set(stream, {\n deleted: deleted_counts.get(stream) ?? 0,\n committed: event,\n });\n }\n // Recompute global max from the per-stream index — deletions may have\n // dropped the previous max, while new tombstones may have raised it.\n let max = -1;\n for (const id of this._max_event_id_by_stream.values())\n if (id > max) max = id;\n this._max_non_snap_event_id = max;\n return result;\n }\n\n /**\n * Atomically wipe-and-rebuild the store under an in-process snapshot.\n *\n * Captures every index state up front, clears it, then hands the\n * orchestrator a per-event insert `callback` via the driver. Any\n * throw inside the driver restores the snapshot, leaving the store\n * byte-for-byte unchanged from the operator's perspective.\n *\n * `id`s are reassigned `0..N-1` as events arrive (dense — the\n * monotonic id counter restarts at 0 for the rebuild). `created` is\n * preserved verbatim from the source.\n */\n async restore(\n driver: (\n callback: (event: Committed<Schemas, keyof Schemas>) => Promise<number>\n ) => Promise<void>\n ): Promise<void> {\n await sleep();\n // Snapshot every index so we can roll back on throw.\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_max_event_id_by_stream = this._max_event_id_by_stream;\n const prev_max_non_snap_event_id = this._max_non_snap_event_id;\n // Swap in fresh state for the duration of the rebuild.\n this._events = [];\n this._next_id = 0;\n this._streams = new Map();\n this._stream_versions = new Map();\n this._max_event_id_by_stream = new Map();\n this._max_non_snap_event_id = -1;\n try {\n await driver(async (event) => {\n const id = this._next_id++;\n const committed: Committed<Schemas, keyof Schemas> = { ...event, id };\n this._events.push(committed);\n // Last event per stream wins for the version watermark — the\n // source is expected to be in commit order, so this is also\n // the highest version. Out-of-order sources get last-wins,\n // matching the legacy raw-SQL restore.\n this._stream_versions.set(event.stream, event.version);\n if (event.name !== SNAP_EVENT) {\n this._max_event_id_by_stream.set(event.stream, id);\n this._max_non_snap_event_id = id;\n }\n return id;\n });\n } catch (err) {\n // Roll back to the captured snapshot — every index restored\n // exactly as it was before the call started.\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._max_event_id_by_stream = prev_max_event_id_by_stream;\n this._max_non_snap_event_id = prev_max_non_snap_event_id;\n throw err;\n }\n }\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,MAAM;AAAC;AAUb,IAAM,gBAAN,MAAM,eAAgC;AAAA,EAC3C;AAAA,EACiB;AAAA,EAER;AAAA,EACA;AA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A,QACJ,sCAAsC,GAAG,4BAC9B,iBAAiB,IAAI,cAAc,iBAAiB,OAAO;AAAA,MACxE;AACA,uBAAiB;AAAA,IACnB;AAAA,EACF;AACA,SAAO;AACT;","names":["log","store","cache"]}
package/dist/index.cjs CHANGED
@@ -1026,7 +1026,14 @@ var InMemoryStore = class {
1026
1026
  const available = [...this._streams.values()].filter(
1027
1027
  (s) => s.is_available && has_work(s) && (lane === void 0 || s.lane === lane)
1028
1028
  );
1029
- const lag = available.sort((a, b) => b.priority - a.priority || a.at - b.at).slice(0, lagging).map((s) => ({
1029
+ const fair = lagging >= 2 ? Math.max(1, Math.floor(lagging / 4)) : 0;
1030
+ const by_priority = [...available].sort(
1031
+ (a, b) => b.priority - a.priority || a.at - b.at
1032
+ );
1033
+ const priority_slice = by_priority.slice(0, lagging - fair);
1034
+ const picked = new Set(priority_slice.map((s) => s.stream));
1035
+ const fair_slice = [...available].sort((a, b) => a.at - b.at).filter((s) => !picked.has(s.stream)).slice(0, fair);
1036
+ const lag = [...priority_slice, ...fair_slice].map((s) => ({
1030
1037
  stream: s.stream,
1031
1038
  source: s.source,
1032
1039
  at: s.at,
@@ -2740,21 +2747,37 @@ async function run_windowed_closes(windowed, deps, skipped) {
2740
2747
  windowed.map((t) => t.stream),
2741
2748
  deps.probe_page_size ?? SAFETY_PROBE_PAGE_SIZE
2742
2749
  ) : /* @__PURE__ */ new Map();
2750
+ const with_lock = deps.with_stream_lock ?? ((_stream, work) => work());
2751
+ const truncated = /* @__PURE__ */ new Map();
2743
2752
  for (const t of windowed) {
2744
- if (t.archive) await t.archive();
2745
- }
2746
- const truncated = await store2().truncate(
2747
- windowed.map((t) => ({
2748
- stream: t.stream,
2749
- before: t.before,
2750
- max_id: min_at.get(t.stream)
2751
- }))
2752
- );
2753
- for (const t of windowed) {
2754
- if (!truncated.has(t.stream)) skipped.push(t.stream);
2753
+ const entry = await with_lock(t.stream, async () => {
2754
+ const max_id = min_at.get(t.stream);
2755
+ if (!await windowed_prune_pending(t.stream, t.before, max_id))
2756
+ return void 0;
2757
+ if (t.archive) await t.archive();
2758
+ const result = await store2().truncate([
2759
+ { stream: t.stream, before: t.before, max_id }
2760
+ ]);
2761
+ return result.get(t.stream);
2762
+ });
2763
+ if (entry) truncated.set(t.stream, entry);
2764
+ else skipped.push(t.stream);
2755
2765
  }
2756
2766
  return truncated;
2757
2767
  }
2768
+ async function windowed_prune_pending(stream, before, max_id) {
2769
+ let boundary_id;
2770
+ let min_id;
2771
+ await store2().query(
2772
+ (event) => {
2773
+ if (min_id === void 0 || event.id < min_id) min_id = event.id;
2774
+ if (event.name === SNAP_EVENT && event.created < before && (max_id === void 0 || event.id <= max_id) && (boundary_id === void 0 || event.id > boundary_id))
2775
+ boundary_id = event.id;
2776
+ },
2777
+ { stream, stream_exact: true, with_snaps: true, after: -1 }
2778
+ );
2779
+ return boundary_id !== void 0 && min_id < boundary_id;
2780
+ }
2758
2781
  async function probe_min_watermarks(streams, page_size) {
2759
2782
  const min_at = /* @__PURE__ */ new Map();
2760
2783
  const source_regex = /* @__PURE__ */ new Map();
@@ -3029,8 +3052,10 @@ var CorrelateCycle = class {
3029
3052
  lane: resolved.lane,
3030
3053
  payloads: []
3031
3054
  };
3032
- if (incoming_priority > entry.priority)
3055
+ if (incoming_priority > entry.priority) {
3033
3056
  entry.priority = incoming_priority;
3057
+ entry.lane = resolved.lane;
3058
+ }
3034
3059
  entry.payloads.push({
3035
3060
  ...reaction,
3036
3061
  source: resolved.source,
@@ -4435,6 +4460,34 @@ var Act = class {
4435
4460
  _handle_batch;
4436
4461
  /** Declared drain lanes (ACT-1103). */
4437
4462
  _lanes;
4463
+ /**
4464
+ * Per-stream close serialization tails (#1222). Chains each stream's
4465
+ * windowed-close work behind the previous one so a manual
4466
+ * `app.close([{stream, before}])` and an autoclose windowed close for
4467
+ * the same stream never run their guard-free prune concurrently — the
4468
+ * manual path bypasses the `__autoclose__:X` drain lease that would
4469
+ * otherwise exclude them, so without this both closers archive the
4470
+ * same prefix. Process-local: both racers run on the same Act
4471
+ * instance. Entries are dropped once their tail resolves so the map
4472
+ * doesn't grow with distinct stream names.
4473
+ */
4474
+ _close_locks = /* @__PURE__ */ new Map();
4475
+ /**
4476
+ * Run `work` under the per-stream close lock (#1222). Serializes
4477
+ * windowed-close critical sections for the same stream while letting
4478
+ * different streams proceed in parallel.
4479
+ */
4480
+ _with_close_lock(stream, work) {
4481
+ const prev = this._close_locks.get(stream) ?? Promise.resolve();
4482
+ const next = prev.then(work, work);
4483
+ this._close_locks.set(stream, next);
4484
+ const cleanup = () => {
4485
+ if (this._close_locks.get(stream) === next)
4486
+ this._close_locks.delete(stream);
4487
+ };
4488
+ next.then(cleanup, cleanup);
4489
+ return next;
4490
+ }
4438
4491
  /** Drain lanes declared via `.withLane(...)`. Implicit default not included. */
4439
4492
  get lanes() {
4440
4493
  return this._lanes;
@@ -4556,7 +4609,8 @@ var Act = class {
4556
4609
  load: this._es.load,
4557
4610
  tombstone: this._es.tombstone,
4558
4611
  logger: this._logger,
4559
- correlation: close_correlation(this._correlator, close_actor)
4612
+ correlation: close_correlation(this._correlator, close_actor),
4613
+ with_stream_lock: (stream, work) => this._with_close_lock(stream, work)
4560
4614
  });
4561
4615
  this.emit("closed", result);
4562
4616
  },
@@ -5546,7 +5600,8 @@ var Act = class {
5546
5600
  load: this._es.load,
5547
5601
  tombstone: this._es.tombstone,
5548
5602
  logger: this._logger,
5549
- correlation: close_correlation(this._correlator, close_actor)
5603
+ correlation: close_correlation(this._correlator, close_actor),
5604
+ with_stream_lock: (stream, work) => this._with_close_lock(stream, work)
5550
5605
  });
5551
5606
  this.emit("closed", result);
5552
5607
  return result;