import { terminalOutputInvalidObservation, } from "./runtime-observation.js";
import { classifyTerminalOutputInvalid, } from "./terminal-output-classifier.js";
import { assertValidProviderTurnActionPolicy } from "./provider-action-policy.js";
export function terminalOutputInvalidActionPolicy(input) {
    if (input.deliverableFailures.length > 0) {
        return intersectRepairPolicy(input.baseActionPolicy, {
            allowedActions: ["tool"],
            directResponseAllowed: false,
        });
    }
    const policy = {
        allowedActions: input.baseActionPolicy.allowedActions,
        directResponseAllowed: input.baseActionPolicy.directResponseAllowed &&
            input.baseActionPolicy.allowedActions.includes("respond"),
        required: input.baseActionPolicy.required?.kind === "tool"
            ? input.baseActionPolicy.required
            : undefined,
    };
    assertValidProviderTurnActionPolicy(policy);
    return policy;
}
export function buildTerminalOutputInvalidRepair(input) {
    const visibleToolNames = input.visibleTools.map((tool) => tool.name);
    const classification = classifyTerminalOutputInvalid(input.text, input.classificationToolNames ?? visibleToolNames);
    const actionPolicy = terminalOutputInvalidActionPolicy({
        baseActionPolicy: input.baseActionPolicy,
        deliverableFailures: input.deliverableFailures,
        invalidKind: classification.invalidKind,
        leakedToolKnown: classification.toolKnown,
        visibleTools: input.visibleTools,
    });
    const requiresNativeToolOnlyRepair = actionPolicy.allowedActions.length === 1 &&
        actionPolicy.allowedActions[0] === "tool" &&
        actionPolicy.directResponseAllowed === false;
    return {
        actionPolicy,
        classification,
        observation: terminalOutputInvalidObservation({
            actionPolicy,
            deliverableFailures: input.deliverableFailures,
            invalidKind: classification.invalidKind,
            leakedToolKnown: classification.toolKnown,
            leakedToolName: classification.toolName,
            reasonCode: classification.reasonCode,
            ...(input.observationToolNames ? { toolNames: input.observationToolNames } : {}),
        }),
        requiresDeliverableAssertion: input.deliverableFailures.length > 0,
        requiresNativeToolOnlyRepair,
        suppressStructuredControls: requiresNativeToolOnlyRepair,
    };
}
function intersectRepairPolicy(base, proposed) {
    const allowedActions = proposed.allowedActions.filter((action) => base.allowedActions.includes(action));
    const required = proposed.required ?? base.required;
    if (allowedActions.length === 0) {
        const policy = {
            allowedActions: base.allowedActions,
            directResponseAllowed: base.directResponseAllowed && base.allowedActions.includes("respond"),
            required: base.required,
        };
        assertValidProviderTurnActionPolicy(policy);
        return policy;
    }
    const policy = {
        allowedActions,
        directResponseAllowed: base.directResponseAllowed &&
            proposed.directResponseAllowed &&
            allowedActions.includes("respond"),
        required: required && allowedActions.includes(required.kind) ? required : undefined,
    };
    assertValidProviderTurnActionPolicy(policy);
    return policy;
}
//# sourceMappingURL=provider-terminal-output-handler.js.map