UNPKG

@mastra/core

Version:
221 lines • 9.64 kB
import { TTLCache } from '@isaacs/ttlcache'; import type { MastraScorer } from '../../evals/base.js'; import type { MastraLanguageModel } from '../../llm/model/shared.types.js'; import type { Mastra } from '../../mastra/index.js'; import type { CoreTool } from '../../tools/types.js'; import type { MessageList } from '../message-list/index.js'; import type { SaveQueueManager } from '../save-queue/index.js'; import type { RunRegistryEntry } from './types.js'; /** * Global registry for accessing run entries from workflow steps. * This is necessary because workflow steps don't have direct access to * the DurableAgent instance's registry. * * Entries are keyed by runId (which are unique UUIDs). * * Uses TTLCache to prevent unbounded memory growth: entries auto-expire * (refreshed on access) and the registry is hard-capped at 1000 concurrent * entries. * * The TTL is a crash/leak safety net, NOT an activity timeout. A run that is * actively executing — waiting on a tool, a sub-agent, or a model call — can * go arbitrarily long without anything reading its entry, and disposal is * destructive (it force-closes the run's stream and drops the live model and * tools). Such runs hold their entry alive explicitly via `markRunActive`. */ export declare const RUN_REGISTRY_TTL_MS: number; /** * How often active runs re-read their registry entry to refresh the sliding * TTL. Two orders of magnitude below the TTL, so a single missed tick is * harmless. */ export declare const RUN_REGISTRY_HEARTBEAT_MS: number; export declare const globalRunRegistry: TTLCache<string, RunRegistryEntry>; /** * Read a run's registry entry as seen from a workflow step running under `mastra`. * * Entries are keyed only by runId, so when another Mastra instance in this * process reuses the runId, a still-running workflow from the earlier run * would otherwise read (and write its transcript into) the new run's entry. * An entry owned by a different Mastra instance is treated as absent. An * ownerless entry (a placeholder or rehydrated entry) is claimed by the first * scoped reader, so a later instance reusing the runId cannot adopt it. */ export declare function getRunRegistryEntry(runId: string, mastra: Mastra | undefined): RunRegistryEntry | undefined; /** * Publish an entry for `runId`, owned by `mastra`, unless the slot holds * another Mastra instance's live run. Returns whether the entry was stored. */ export declare function setRunRegistryEntry(runId: string, entry: RunRegistryEntry, mastra: Mastra | undefined): boolean; /** Remove `runId`'s entry only if it belongs to `mastra` (or has no owner). */ export declare function deleteRunRegistryEntry(runId: string, mastra: Mastra | undefined): void; export declare function setRunScorerInstances(runId: string, scorers: Record<string, MastraScorer>, mastra: Mastra | undefined): void; /** Remove and return the run's scorer instances, unless another Mastra owns them. */ export declare function takeRunScorerInstances(runId: string, mastra: Mastra | undefined): Record<string, MastraScorer> | undefined; /** * Marks a resume of `toolCallId` on `runId` as in flight in this process, * synchronously, so a concurrent resume of the same tool call can fail fast * instead of queueing on the execution claim (#27365). * * @returns A release function, or `undefined` when that tool call already has * a resume in flight. Release is idempotent and only clears its own marker. */ export declare function markResumeInFlight(runId: string, toolCallId: string): (() => void) | undefined; /** * Clears the in-flight marker for a tool call whose resumed segment is * suspending again, so a later resume of it is not treated as a duplicate. */ export declare function clearResumeInFlight(runId: string, toolCallId: string): void; /** * Hold a run's registry entry alive while an operation is in flight. * * Durable steps await work that can outlast the registry TTL (a tool or * sub-agent call, a stalled model call). Nothing reads the entry while that * await is pending, so the sliding TTL would otherwise expire it mid-run and * dispose the live runtime state out from under the still-running workflow. * * Refcounted, so concurrent operations on the same run each hold their own * claim. The returned release is idempotent — calling it twice releases one * claim, not two. Always release in a `finally`; if a release is ever missed * the entry still expires once the heartbeat stops, so this degrades safely. * * @param runId - The unique run identifier * @returns A function that releases this claim */ export declare function markRunActive(runId: string): () => void; /** * Drop all activity claims and stop the heartbeat. Tests only — production * code releases claims through the function returned by `markRunActive`. */ export declare function __resetRunRegistryActivityForTests(): void; export declare function getActiveDurableAgentWorkflowExecutions(mastra: Mastra): Promise<unknown>[]; export declare function isDurableAgentExecutionDetached(mastra: Mastra, runId: string): boolean; /** * Abandon the durable-agent executions `mastra` is still running: each stops * without writing and releases its run, so recovery can take it right away. * Executions that already settled are left alone. */ export declare function abandonDurableAgentExecutions(mastra: Mastra): Promise<void>; /** * End a run's root spans (MODEL_GENERATION then AGENT_RUN) with an error so the trace * still exports — stores persist only span-end events. After a resume the fresh resume * spans are the active root, so prefer them. Ending an already-ended span is a no-op, * so the duplicate error paths (workflow failure + emitError) are safe. Never throws. */ export declare function endRunSpansWithError(runId: string, error: Error, options?: { endSpan?: boolean; }): void; /** * Registry for per-run non-serializable state. * * During durable execution, the DurableAgent needs to store non-serializable * objects (tools with execute functions, SaveQueueManager, etc.) that can't * flow through workflow state. This registry provides a way to store and * retrieve these objects keyed by runId. * * The registry is scoped to a single DurableAgent instance and entries are * cleaned up when a run completes. */ export declare class RunRegistry { #private; /** * Register non-serializable state for a run * @param runId - The unique run identifier * @param entry - The registry entry containing tools, saveQueueManager, etc. */ register(runId: string, entry: RunRegistryEntry): void; /** * Get the registry entry for a run * @param runId - The unique run identifier * @returns The registry entry or undefined if not found */ get(runId: string): RunRegistryEntry | undefined; /** * Get tools for a specific run * @param runId - The unique run identifier * @returns The tools record or an empty object if not found */ getTools(runId: string): Record<string, CoreTool>; /** * Get SaveQueueManager for a specific run * @param runId - The unique run identifier * @returns The SaveQueueManager or undefined if not found */ getSaveQueueManager(runId: string): SaveQueueManager | undefined; /** * Get the language model for a specific run * @param runId - The unique run identifier * @returns The MastraLanguageModel or undefined if not found */ getModel(runId: string): MastraLanguageModel | undefined; /** * Check if a run is registered * @param runId - The unique run identifier * @returns True if the run is registered */ has(runId: string): boolean; /** * Cleanup and remove a run's entry from the registry * @param runId - The unique run identifier */ cleanup(runId: string): void; /** * Get the number of active runs in the registry */ get size(): number; /** * Get all active run IDs */ get runIds(): string[]; /** * Clear all entries from the registry * Calls cleanup on each entry before removing */ clear(): void; } /** * Extended registry entry that also stores the MessageList reference. * This is useful for accessing message state outside of workflow steps * (e.g., for callbacks that need to read messages). */ export interface ExtendedRunRegistryEntry extends RunRegistryEntry { /** MessageList reference for callback access */ messageList?: MessageList; /** Thread ID for memory */ threadId?: string; /** Resource ID for memory */ resourceId?: string; } /** * Extended run registry that also stores MessageList references and memory info */ export declare class ExtendedRunRegistry extends RunRegistry { #private; /** * Register non-serializable state for a run including MessageList */ registerWithMessageList(runId: string, entry: RunRegistryEntry, messageList: MessageList, memoryInfo?: { threadId?: string; resourceId?: string; }): void; /** * Get MessageList for a specific run */ getMessageList(runId: string): MessageList | undefined; /** * Get memory info for a specific run */ getMemoryInfo(runId: string): { threadId?: string; resourceId?: string; } | undefined; /** * Override cleanup to also remove MessageList and memory info */ cleanup(runId: string): void; /** * Override clear to also clear MessageLists and memory info */ clear(): void; } //# sourceMappingURL=run-registry.d.ts.map