UNPKG

@mastra/core

Version:
116 lines • 6.56 kB
import type { IterationCompleteContext, OnIterationCompleteHandler } from '../../agent/agent.types.js'; import type { IMastraLogger } from '../../logger/index.js'; import type { LoopOptions } from '../types.js'; export declare const CONTINUATION_STOP_CAUSES: readonly ['model_finished', 'max_steps', 'stop_condition', 'iteration_hook', 'feedback_complete', 'delegation']; export type ContinuationStopCause = (typeof CONTINUATION_STOP_CAUSES)[number]; /** Decision returned by {@link decideContinuation}, projected back onto engine state by the glue. */ export interface ContinuationDecision { /** Authoritative loop stop boundary; absent while continuing. Not a provider finish reason. */ stopCause?: ContinuationStopCause; /** True when the loop must stop after this iteration. */ isFinal: boolean; /** * True when the onIterationComplete hook forced another turn that the * engine's own step result would not have taken (continue-override or * feedback resurrection). Glue must set its `isContinued` flag so * downstream consumers (stopWhen next iteration, finish reason) agree * with the decision. */ forceContinue: boolean; /** * Two-phase stop: `{ continue: false, feedback }` allows one more LLM turn * with the feedback, then stops. Glue persists this for the next predicate * evaluation (closure state on main, serialized IterationState on durable). */ nextPendingFeedbackStop: boolean; } /** * Per-engine continuation policy. Required (no default) so every call site's * choice is explicit and a missed site is a compile error. * * Two ladders rather than per-behavior knobs: the four behaviors that differ * between the engines (hard vs soft feedback stop, stopWhen gating, the * feedback turn past a matched stopWhen, and the finite-maxSteps guard) * interact inside one decision ladder — four booleans would generate 16 * states, of which only these 2 ever shipped. Each ladder is the contract * its engine shipped, verified line-for-line against the pre-extraction * predicates (`git show dc51cb24b3`). */ export type ContinuationPolicy = { mode: 'durable'; } | { mode: 'default'; /** * True when the run has a finite maxSteps. Only the default ladder's * feedback force-continue consumes this (which is why it lives on the * policy union, not the shared deps): the shipped default contract * never force-continued from the feedback branch on unbounded runs — a * hook that always returns feedback must not spin the loop forever. */ hasFiniteMaxSteps: boolean; }; export interface ContinuationDeps { /** Previous turn returned `{ continue: false, feedback }` — this turn ran with the feedback, stop now. */ pendingFeedbackStop: boolean; /** The engine's own continuation flag after all in-iteration steps ran (stepResult/lastStepResult.isContinued). */ llmWantsToContinue: boolean; /** Glue-computed: `iterations < maxSteps`, or `true` when the run is unbounded. */ underMaxSteps: boolean; /** Accumulated steps across iterations (including the current one), passed to stopWhen. */ steps: unknown[]; stopWhen?: LoopOptions['stopWhen']; /** Read AND clear the engine's delegation-bail flag (RunScope on main, IterationState on durable). */ consumeDelegationBail: () => boolean; /** A background task result was just injected — skip the hook, the loop is mid-task. */ backgroundTaskPending?: boolean; onIterationComplete?: OnIterationCompleteHandler; /** Lazy: only invoked when the hook runs. `isFinal` is the pre-hook decision. */ buildIterationContext: (isFinal: boolean) => IterationCompleteContext | Promise<IterationCompleteContext>; /** Append the hook's feedback as a synthetic assistant message the next turn will see. */ injectFeedback: (feedback: string) => void | Promise<void>; logger?: IMastraLogger; /** Which engine's shipped continuation contract to apply. */ policy: ContinuationPolicy; } /** * Shared dowhile-predicate decision core: given a settled iteration, decide * whether the loop runs another turn. Owns the two-phase * feedback stop, stopWhen evaluation, delegation bail, and the * onIterationComplete result ladder. Engine glue owns everything around it: * step accumulation, signal draining, message-boundary rotation, abort * checks, and emission. * * The mechanics are shared; the policy is per-engine, because each engine * shipped a different contract and each contract is grounded in that * engine's constraints: * * - `{ mode: 'durable' }` — the durable/evented predicate as it shipped * before extraction. `pendingFeedbackStop` and delegation bail are hard * stops the hook cannot override (the stop is recorded in a persisted step * record before the next step is scheduled; at-least-once redelivery * re-running the predicate must not reopen a settled record). stopWhen * only runs while the outcome is still open (settled records must not be * re-evaluated). `{ feedback, continue: false }` grants one feedback turn * even past a matched stopWhen (two-phase stop). * * - `{ mode: 'default' }` — the in-process predicate as it shipped before * extraction (the released contract users run against). The feedback stop * is soft: `{ continue: true }` can override it — there is no persisted * record and no redelivery, so the durable constraint doesn't exist here. * stopWhen runs whenever the LLM wants to continue (user predicates are * observable API — invocation counts and side effects are behavior). * `{ feedback, continue: false }` after a matched stopWhen injects the * feedback but still stops (no extra turn — an extra LLM turn would change * step count, latency, and billing against the released contract). The * feedback force-continue requires a finite maxSteps (runaway guard). * * Where the ladders disagree beyond the four policy behaviors above — * delegation bail ordering and feedback on resurrection — each ladder * keeps its own shipped behavior; see the inline comments for * the how and why of each split. Unifying them (e.g. fixing the default * engine's bail-flag leak or its dropped resurrection feedback) is an * observable behavior change to the released contract and ships separately * with its own product decision. */ export declare function decideContinuation(deps: ContinuationDeps): Promise<ContinuationDecision>; //# sourceMappingURL=continuation-core.d.ts.map