@struktoai/mirage-core
Version:
A Unified Virtual Filesystem For AI Agents
59 lines • 2.7 kB
TypeScript
export type ContextCall = <R>(fn: () => R | Promise<R>) => R | Promise<R>;
export interface AsyncStorage<T> {
run<R>(store: T, fn: () => R | Promise<R>): R | Promise<R>;
getStore(): T | undefined;
/**
* Every store bound by a live `run`, oldest first.
*
* On an isolating runtime that is the current task's store alone,
* because `AsyncLocalStorage` cannot enumerate other tasks and the
* isolated world never needs it to. On the fallback it is the honest
* answer the single slot cannot give — all concurrently live
* bindings — so a reader can merge conservatively instead of
* trusting whichever binding happens to sit in the slot. The array
* is a snapshot; it does not track later binds or settles.
*/
liveStores(): readonly T[];
capture(): ContextCall;
}
/**
* The browser storage: no task isolation, one frame stack per storage.
*
* `getStore` answers the newest live frame, so a read that interleaves
* with another task's bind can still see that task's store — only real
* isolation fixes that, which is what `liveStores` exists to route
* around. What the stack does fix is the slot's two worse failures: a
* settle removes its own frame rather than blind-restoring a saved
* value, so an out-of-order settle can neither wipe a binding that is
* still live nor strand a stale one after every run has finished.
*
* Exported for the fallback-mode tests, which mock `createAsyncContext`
* with this class so the browser branch runs under node's test runner.
*/
export declare class FallbackStorage<T> implements AsyncStorage<T> {
private frames;
run<R>(s: T, fn: () => R | Promise<R>): R | Promise<R>;
private withFrame;
getStore(): T | undefined;
liveStores(): readonly T[];
capture(): ContextCall;
}
/**
* Whether the storage isolates concurrent tasks, verified behaviorally
* at module load rather than assumed from where the constructor came
* from.
*
* `AsyncLocalStorage` gives every task its own store. The fallback is
* one frame stack whose top a concurrent task can shadow, so a caller
* that binds a store for concurrent work has to know which it got.
*/
export declare const asyncContextIsolatesTasks: boolean;
/**
* Run `fn` with no store bound, as this module loaded: for work a line
* starts that outlives it and must not carry the line's state, such as
* a sandbox's SFTP server. Without task isolation there is nothing to
* leave, so `fn` runs as called.
*/
export declare function runDetached<R>(fn: () => R | Promise<R>): R | Promise<R>;
export declare function createAsyncContext<T>(): AsyncStorage<T>;
//# sourceMappingURL=async_context.d.ts.map