UNPKG

@mastra/core

Version:
58 lines • 2.79 kB
/** * RunScope — per-run bag of *non-serializable* runtime state. * * The evented workflow engine routes step inputs/outputs through `JSON.stringify` * (storage snapshots, `UnixSocketPubSub` frames). Live class instances, function * closures, abort controllers, and stream transports cannot survive that round * trip even with the Phase 2 codec. RunScope keeps those values off the wire: * step factories read and write through a typed key-value bag keyed by `runId` * and held on the parent `Mastra` instance. Nothing in the scope is ever * persisted or published. * * Scopes are refcounted alongside the internal-workflow registry on `Mastra`: * `__registerInternalWorkflow` / `__unregisterInternalWorkflow` hold and * release them, and call sites that need a scope before registration pair * `__createRunScope` with `__releaseRunScope`. They share the same TTL sweep * as the run-scoped workflow registrations they back, so a scope dies with its * run. * * Never put RunScope values on step input/output schemas. Step I/O crosses the * wire and must stay JSON-safe: the codec in `events/codec` round-trips a fixed * set of built-ins and registered classes at the `UnixSocketPubSub` boundary, * but live handles and closures are dropped. * * Keys are branded symbols carrying their value type as a phantom parameter, * so `scope.get(SAVE_QUEUE_MANAGER)` returns `SaveQueueManager | undefined` * without any cast at the call site. Concrete key registries live next to the * consumers (see `loop/run-scope-keys.ts`, `agent/workflows/prepare-stream/ * run-scope-keys.ts`) to avoid pulling domain types into the `mastra` layer. */ /** * A typed handle to a slot in a {@link RunScope}. The phantom `__t` parameter * carries the value type so `get` / `set` are inferred correctly without an * explicit type argument. */ export interface RunScopeKey<T> extends Symbol { /** Phantom marker — never read at runtime. */ readonly __t?: T; } /** * Create a new {@link RunScopeKey}. Each call produces a fresh `Symbol`, so two * keys with the same label do not collide. */ export declare function createRunScopeKey<T>(label: string): RunScopeKey<T>; export interface RunScope { get<T>(key: RunScopeKey<T>): T | undefined; /** * Read a required slot. Throws if missing — use this to make a missing * invariant fail loudly instead of producing a downstream `undefined`. */ getOrThrow<T>(key: RunScopeKey<T>): T; set<T>(key: RunScopeKey<T>, value: T): void; delete<T>(key: RunScopeKey<T>): void; has<T>(key: RunScopeKey<T>): boolean; /** Test-only: number of slots currently populated. */ readonly size: number; } export declare function createRunScope(): RunScope; //# sourceMappingURL=run-scope.d.ts.map