import type { IMastraLogger } from '../../../logger/index.js';
import type { Mastra } from '../../../mastra/index.js';
import type { MastraMemory } from '../../../memory/memory.js';
import type { TracingContext } from '../../../observability/index.js';
import type { OutputResult } from '../../../processors/index.js';
import { RequestContext } from '../../../request-context/index.js';
import type { ChunkType } from '../../../stream/types.js';
import type { Agent } from '../../agent.js';
import type { SerializedMessageListState } from '../../message-list/state/index.js';
import type { DurableAgenticWorkflowInput, RunRegistryEntry } from '../types.js';
type GenerateThreadTitleArgs = Parameters<NonNullable<RunRegistryEntry['generateThreadTitle']>>[0];
type AnyAgent = Agent<any, any, any, any>;
export interface DurableFinishSideEffectsOptions {
    runId: string;
    initData: DurableAgenticWorkflowInput;
    messageListState: SerializedMessageListState;
    mastra?: Mastra;
    requestContext?: RequestContext;
    tracingContext?: TracingContext;
    logger?: IMastraLogger;
    outputResult?: OutputResult;
    /**
     * The current response message id. Signals, goals and isTaskComplete rotate it during
     * the run, so `initData.messageId` can name an earlier reply. Defaults to that id.
     */
    messageId?: string;
}
export interface DurableFinishSideEffectsResult {
    messageListState: SerializedMessageListState;
    outputText: string;
    /**
     * True when processOutputResult changed `outputText`, including emptying it. Only then should
     * callers apply `outputText` to the run's last step: otherwise the step's own text stands,
     * e.g. the narration of a final tool step whose last message has no text.
     */
    outputTextChanged: boolean;
    titleGeneration?: Promise<void>;
    /**
     * Set when this stream() run should emit a `data-thread-title` chunk (`generateTitle.emitEvent`).
     * Resolves with the saved title, or undefined when none was generated. Callers await it
     * before FINISH so the chunk lands first, matching the in-process loop.
     */
    titleForStream?: Promise<string | undefined>;
    /** Chunks from the `structuredOutput.model` structuring pass, to publish before FINISH. */
    structuredOutputChunks?: ChunkType[];
    /**
     * Chunks output processors wrote with `writer.custom()` in processOutputResult. Publish them
     * marked output-processed, after `structuredOutputChunks` and before the tripwire or FINISH,
     * matching the in-process loop.
     */
    outputResultChunks?: ChunkType[];
    /** Set when an output processor aborted in processOutputResult; nothing was persisted. */
    tripwire?: {
        reason: string;
        processorId?: string;
        retry?: boolean;
        metadata?: unknown;
    };
}
/**
 * Run the side effects that happen after the durable agentic loop reaches its
 * terminal state. Every durable engine calls this before emitting its finish
 * event so remote workers provide the same behavior as the built-in engine.
 *
 * The returned state is the SAME MessageList that output processors mutated
 * and memory persistence flushed. Callers must use it in their final output;
 * rebuilding from the pre-processor state would discard redactions and other
 * processOutputResult changes.
 */
export declare function runDurableFinishSideEffects({ runId, initData, messageListState, mastra, requestContext, tracingContext, logger, outputResult, messageId, }: DurableFinishSideEffectsOptions): Promise<DurableFinishSideEffectsResult>;
/**
 * What a total deadline saves: the steps that completed in time, plus the step in flight. A step cut off
 * mid-stream contributes the text it already streamed, which never reached the step processors. A step cut off
 * while its tools ran is kept whole, with the tool results that came back in time or during the deadline's grace
 * window and any call still running saved as aborted.
 */
export declare function deadlineMessageListState(entry: RunRegistryEntry, fallback: SerializedMessageListState): {
    messageListState: SerializedMessageListState;
    cutOffText?: string;
};
/**
 * Give the output processors (and with them memory) their one pass when a total deadline ends a
 * durable run. The run never reaches its finish step, so this runs over what the deadline saves
 * (see `deadlineMessageListState`), as the default loop's deadline pass does.
 */
export declare function runDurableDeadlineOutputPass({ runId, initData, messageListState, cutOffText, entry, mastra, tracingContext, logger, }: {
    runId: string;
    initData: Pick<DurableAgenticWorkflowInput, 'runId' | 'agentId' | 'agentName' | 'messageId' | 'state' | 'options' | 'requestContextEntries'>;
    messageListState: SerializedMessageListState;
    cutOffText?: string;
    entry: RunRegistryEntry;
    mastra?: Mastra;
    /** The run's agent span, so the deadline pass shows in the run's trace as the finish pass does. */
    tracingContext?: TracingContext;
    logger: IMastraLogger;
}): Promise<void>;
/**
 * Generate a durable thread title from messages belonging to that thread.
 * This is standalone so a remote worker can call it with the agent and memory
 * it rebuilt from its own Mastra instance.
 */
export declare function generateDurableThreadTitle({ agent, memory, threadId, resourceId, memoryConfig, messageListState, requestContext, tracingContext, }: GenerateThreadTitleArgs & {
    agent: AnyAgent;
    memory: MastraMemory;
}): Promise<string | undefined>;
export {};
//# sourceMappingURL=finalize-run.d.ts.map