const EXECUTION_EVIDENCE_EFFECTS = new Set(["execute", "process", "verify"]);
export function validateProviderFinalAnswer(input) {
    const userIntentText = [input.objective, recentUserIntentText(input.messages)]
        .filter((value) => Boolean(value?.trim()))
        .join("\n");
    if (!hasExecutionIntent(userIntentText) || !claimsExecutionSuccess(input.text)) {
        return { ok: true };
    }
    const currentMessages = currentTurnMessages(input.messages);
    const executionToolMessages = currentMessages.filter((message) => message.role === "tool" &&
        EXECUTION_EVIDENCE_EFFECTS.has(input.toolEffects.get(message.toolName) ?? "read"));
    if (executionToolMessages.length === 0) {
        return {
            ok: false,
            observation: {
                code: "FINAL_ANSWER_UNSUPPORTED",
                severity: "error",
                message: "The previous final answer claimed execution success, but there is no execution tool evidence for this turn.",
            },
        };
    }
    const latestExecutionMessage = executionToolMessages.at(-1);
    if (latestExecutionMessage?.outcome === "error") {
        return {
            ok: false,
            observation: {
                code: "FINAL_ANSWER_UNSUPPORTED",
                severity: "error",
                message: "The previous final answer claimed execution success, but the latest execution tool result is an error.",
            },
        };
    }
    return { ok: true };
}
function currentTurnMessages(messages) {
    let lastUserIndex = -1;
    for (let index = messages.length - 1; index >= 0; index -= 1) {
        if (messages[index]?.role === "user") {
            lastUserIndex = index;
            break;
        }
    }
    return lastUserIndex < 0 ? messages : messages.slice(lastUserIndex + 1);
}
function recentUserIntentText(messages) {
    const recentUserMessages = messages
        .filter((message) => message.role === "user")
        .slice(-3)
        .map((message) => message.content);
    return recentUserMessages.join("\n");
}
function hasExecutionIntent(text) {
    return (/\b(?:test|benchmark|build|lint|typecheck|verify)\b|(?:运行|执行|测试|跑|构建|验证|跑过)/iu.test(text) ||
        /\b(?:run|execute)\b[\s\S]{0,120}\b(?:tests?|benchmarks?|terminal-bench|build|lint|typecheck|verify|verification|suite|script|command|npm|pnpm|yarn|pytest|cargo|swift)\b/iu.test(text));
}
function claimsExecutionSuccess(text) {
    if (/\b(?:failed|failing|error|errored|blocked|could not|unable|cannot|can't)\b|(?:失败|错误|报错|阻塞|无法|不能)/iu.test(text)) {
        return false;
    }
    return /\b(?:success|succeeded|successful|passed|completed|ran|finished|green)\b|(?:成功|通过|完成|已运行|已经运行|跑完|执行完|测试通过)/iu.test(text);
}
//# sourceMappingURL=provider-final-validator.js.map