import { toDiagnosticErrorMessage } from "./diagnostic-utils.js";
// COMPAT(piCopilot413): added 2026-05-13 for Pi <= 0.73.1; target removal
// 2026-11-13, once upstream @mariozechner/pi-ai recognizes this overflow.
const PI_COPILOT_SHORT_413_OVERFLOW_PATTERN = /^413\s+failed to parse request$/i;
const PI_SESSION_RECOVERY_POLICIES = [
    {
        id: "piCopilot413",
        shouldRecover: shouldCompactForPiCopilot413,
        recover: compactPiSession,
    },
];
export async function applyPiSessionRecoveryPolicy(session) {
    const policy = PI_SESSION_RECOVERY_POLICIES.find((entry) => entry.shouldRecover(session));
    if (!policy) {
        return { applied: false };
    }
    await policy.recover(session);
    return { applied: true, policyId: policy.id };
}
function shouldCompactForPiCopilot413(session) {
    return (isPiCopilotShort413Overflow(getLatestAssistantErrorMessage(session)) ||
        isPiCopilotShort413Overflow(session.agent.state.errorMessage));
}
function isPiCopilotShort413Overflow(errorMessage) {
    const normalized = errorMessage?.trim();
    return normalized ? PI_COPILOT_SHORT_413_OVERFLOW_PATTERN.test(normalized) : false;
}
function getLatestAssistantErrorMessage(session) {
    for (let index = session.messages.length - 1; index >= 0; index -= 1) {
        const message = session.messages[index];
        if (message.role !== "assistant") {
            continue;
        }
        return message.stopReason === "error" ? (message.errorMessage?.trim() ?? null) : null;
    }
    return null;
}
async function compactPiSession(session) {
    try {
        await session.compact();
    }
    catch (error) {
        if (!isHarmlessPiCompactionError(error)) {
            throw error;
        }
    }
}
function isHarmlessPiCompactionError(error) {
    return /already compacted|nothing to compact/i.test(toDiagnosticErrorMessage(error));
}
//# sourceMappingURL=pi-session-recovery-policy.js.map