"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.runWithModelFallback = runWithModelFallback;
exports.runWithChainFallback = runWithChainFallback;
/**
 * Per-slot model fallback chain.
 *
 * A reviewer or doer slot can list multiple models in `models[]`. When the
 * primary returns null (subprocess produced no answer — quota, rate-limit,
 * crash, empty-stream), this helper falls through to the next model in the
 * list and retries. Auth and lineage selection are stable across the
 * fallback because all models in a slot share the same lineage; only the
 * `--model X` argv changes.
 *
 * Returning `null` from `attempt` is the agreed-upon "this model didn't
 * produce an answer, please try the next one" signal. Throwing is
 * propagated unchanged — a throw means something went wrong outside the
 * model run (e.g. the chat dir disappeared) and falling through wouldn't
 * help. Callers that want exceptions to engage the fallback should catch
 * them inside `attempt` and return null instead.
 *
 * `onFallback(fromModel, toModel, fromIdx)` fires once per transition so
 * the runner can emit a `cli_warning` event with `reason: 'model_fallback'`
 * and the cockpit can show "claude-opus-4-7 → claude-sonnet-4-6" on the
 * card. The index tells the cockpit how deep into the chain we are.
 *
 * If `models` is empty or undefined, exactly one attempt is made with
 * `undefined` as the model — this matches the existing "no model = lineage
 * default" semantics in the shim layer.
 */
async function runWithModelFallback(models, attempt, onFallback) {
    const list = models && models.length > 0 ? [...models] : [undefined];
    for (let i = 0; i < list.length; i++) {
        const result = await attempt(list[i]);
        if (result !== null)
            return result;
        if (i < list.length - 1) {
            onFallback(list[i], list[i + 1], i);
        }
    }
    return null;
}
async function runWithChainFallback(chain, attempt, onFallback) {
    if (chain.length === 0)
        return null;
    for (let i = 0; i < chain.length; i++) {
        const result = await attempt(chain[i]);
        if (result !== null)
            return result;
        if (i < chain.length - 1) {
            onFallback(chain[i], chain[i + 1], i);
        }
    }
    return null;
}
//# sourceMappingURL=run-with-fallback.js.map