import type { TaskResult, TaskSpec } from "../core/types.js";
import type { RunInternals } from "../core/runner/contracts.js";
import { type SpendAccount } from "../core/spend-account.js";
/**
 * What a composition's later legs run on. A composition that dispatches several legs of ONE task — the verification gate's
 * implementation and fix legs, the repair loop's attempts — is bounded by the task's WHOLE-TASK ceilings, not each leg by them:
 *  · the whole-task ceilings are `resourceSuspend.totalBudgetUsd` / `totalTokens` on a task that opted into resource
 *    suspension (its `limits` windows pace each slice, and each leg opens one exactly as a resume does), and
 *    `limits.maxCostUsd` / `maxTokens` / `maxTurns` otherwise (a task that cannot suspend runs one slice, so its window is
 *    the whole task's);
 *  · tokens and cost are the spend account's: the composition opens ONE node of the account under the caller's
 *    ({@link compositionAccount}) whose ceilings ARE the whole-task ceilings, and every leg it dispatches hangs under it by a
 *    task link — the first one included, so what the first leg leaves running (a session-scoped worker) spends from the same
 *    node as the legs after it, and each model request of any of them is admitted against what the legs together have left
 *    before it is sent. Spend the node could not see — a first leg handed in already run, the slices of a resumed chain
 *    before this call — is taken off the node's ceilings once ({@link chargeFirstLeg}). A later leg's spec carries the
 *    caller's token and cost ceilings unchanged;
 *  · turns are not the account's (it counts tokens and cost only): a later leg's spec carries the turns the legs before it
 *    left ({@link laterLegTurns});
 *  · an axis with nothing left dispatches no further leg ({@link compositionExhausted} for tokens and cost, {@link laterLegTurns}
 *    for turns): the composition stops where its own ceilings stop it, and says which axis (`AllowanceExhausted`) — it never
 *    mints a leg's terminal for a leg that did not run.
 * The account is the one call's: the ceilings are the ones this call was handed and the spend is what its legs spent, so across
 * a durable pause it does not see the parking call's other legs or the totals frozen on the checkpoint (`docs/KNOWN-LIMITS.md`
 * KL-3170-c, closed by #3176).
 */
/** The whole-task axes a composition's legs share. */
export type AllowanceAxis = "cost" | "tokens" | "turns";
/** What a composition's first leg spent (its whole subtree: what its own ceilings were held against). `costMicroUsd` is
 *  undefined when any of it was unpriced. */
export interface CompositionSpend {
    readonly costMicroUsd: number | undefined;
    readonly tokens: number;
    readonly turns: number;
}
/**
 * A whole-task axis with nothing left, so the composition dispatched no further leg. `remaining` is in the axis's own unit
 * (micro-USD for `cost`, tokens, turns) and is `0` once the legs spent the ceiling; it is absent when the spend on the axis
 * cannot be known (a leg ran unpriced under a cost ceiling; a reported figure that is NaN, infinite or negative, on any
 * axis), which is never read as room to spend.
 */
export interface AllowanceExhausted {
    readonly axis: AllowanceAxis;
    readonly remaining?: number;
}
/** A later leg's dispatch: the spec carrying the turns left, or the axis with nothing left. */
export type LaterLegAllowance<S> = {
    readonly spec: S;
    readonly exhausted?: undefined;
} | {
    readonly spec?: undefined;
    readonly exhausted: AllowanceExhausted;
};
type AllowanceSpec = Pick<TaskSpec, "limits" | "resourceSuspend">;
/** A leg's spend as its own ceilings were held against it: its own figures and everything it started (the account's subtree). */
export declare function legSpendOf(stats: TaskResult["stats"]): CompositionSpend;
/** The composition's node of the spend account, with the facts its exhaustion reading needs. */
export interface CompositionAccount {
    /** Where every later leg hangs (`RunInternals.spendParent`), by a task link. */
    readonly spendParent: NonNullable<RunInternals["spendParent"]>;
    /** The node itself. */
    readonly account: SpendAccount;
    /** The first leg's spend on an armed axis could not be read (unpriced under a cost ceiling, or a figure that is not one). */
    readonly unreadable: {
        tokens: boolean;
        cost: boolean;
    };
}
/**
 * The node a composition's legs spend from — under the caller's account (`parent`, the caller's `RunInternals.spendParent`)
 * or a root — with the whole-task token and cost ceilings as its own. Opened before the composition dispatches its first leg,
 * which hangs under it like every later one. `undefined` when the spec arms neither: the legs then hang where the caller's
 * legs hang.
 */
export declare function compositionAccount(spec: AllowanceSpec, parent: RunInternals["spendParent"]): CompositionAccount | undefined;
/** A leg's internals hung under the composition's node (by a task link) when there is one; as they came otherwise. */
export declare function onComposition(internals: RunInternals | undefined, c: CompositionAccount | undefined): RunInternals | undefined;
/**
 * Take the first leg's spend (`first`, as its result reports it) into the node, once: what of it the node did not see — all of
 * it for a leg handed in already run, the earlier slices for a resumed chain, nothing for a leg the composition dispatched
 * under the node — comes off the node's ceilings. A spend on an armed axis that cannot be read leaves no provable room there.
 */
export declare function chargeFirstLeg(c: CompositionAccount | undefined, first: CompositionSpend): void;
/**
 * Whether the composition's node has nothing left on the token or cost axis (read in the engine's order: tokens, then cost), so
 * no further leg is dispatched; `undefined` while both have room (a leg dispatched then is held by the account request by
 * request, and a request that does not fit ends that leg on the budget). A leg under the node that ran unpriced under a cost
 * ceiling leaves no provable room on it.
 */
export declare function compositionExhausted(c: CompositionAccount | undefined): AllowanceExhausted | undefined;
/**
 * The spec a composition's later leg is dispatched with, given the turns the legs before it took: the whole-task turn ceiling
 * replaced by what is left of it; none armed ⇒ the caller's spec itself (the same object). `maxTurns: 0` is the explicit
 * "unbounded" sentinel, so it arms nothing; a suspendable task has no whole-task turn ceiling.
 */
export declare function laterLegTurns<S extends AllowanceSpec>(spec: S, turnsSpent: number): LaterLegAllowance<S>;
export {};
