import { classifyRuntimeFailure } from "../failure-classification.js";
import { buildTerminalOutputInvalidRepair } from "./provider-terminal-output-handler.js";
import { appendRuntimeObservation } from "./runtime-observation.js";
export async function repairDurableGenerateTerminalOutput(input) {
    try {
        return {
            repaired: false,
            responseText: input.runtime.assertValidTerminalAssistantText(input.responseText),
        };
    }
    catch (error) {
        const failure = classifyRuntimeFailure(error);
        if (failure.errorCode !== "TERMINAL_OUTPUT_INVALID" || input.repairAttempted) {
            throw error;
        }
    }
    await input.runtime.recordTurnSettlement(await input.runtime.buildTurnSettlementRecord({
        rejectedReason: "invalid-terminal-output",
        terminalKind: "completed",
        unresolvedFailures: [],
    }));
    const terminalRepair = buildTerminalOutputInvalidRepair({
        baseActionPolicy: {
            allowedActions: ["respond"],
            directResponseAllowed: true,
        },
        classificationToolNames: input.availableTools.map((tool) => tool.name),
        deliverableFailures: [],
        text: input.responseText,
        visibleTools: [],
    });
    const repairMessages = appendRuntimeObservation(input.messages, terminalRepair.observation);
    const correctionTrace = input.runtime.buildCorrectionTrace({
        code: "TERMINAL_OUTPUT_INVALID",
        messages: input.messages,
        operation: input.operation,
        systemPrompt: input.systemPrompt,
    });
    const repairResponse = await input.runtime.provider.generate(input.turn.threadId, input.turn.id, {
        cacheControl: input.cacheControl,
        messages: repairMessages,
        systemPrompt: input.systemPrompt,
    }, "repair", input.runtime.buildProviderInvokeContext({
        correctionTrace,
        requestBudgetSnapshot: input.requestBudgetSnapshot,
        supersedePendingProviderRequest: true,
    }));
    return {
        repaired: true,
        responseText: input.runtime.assertValidTerminalAssistantText(repairResponse.text),
    };
}
//# sourceMappingURL=provider-terminal-repair-runner.js.map