/**
 * 任务上下文 — 阶段指令计算（能力 6）
 */
import { debug } from "../../core/logger.js";
import { evaluateNoFalseCompletion, recordCepIntegration } from "../../audit/core_experience_principle.js";
import { MAX_REWORK } from "./constants.js";
/**
 * 基于代码审查结果和修复循环状态计算阶段指令（能力 6）。
 * 决策优先级: 审查通过 →「完成」> critical 发现需返工 > 返工超限 →「停止升级」> 不可自动修复 →「回退并标注」> 其他 →「推进下一阶段」。
 */
export function computePhaseDirective(ctx) {
    debug("task_context", "computePhaseDirective() 开始执行");
    // 审查通过 → 完成
    if (ctx.code_review && ctx.code_review.findings.length === 0) {
        return "完成";
    }
    // 有 critical 发现 → 需要修复后重验证
    if (ctx.code_review) {
        const criticalCount = ctx.code_review.findings.filter((f) => f.severity === "critical").length;
        if (criticalCount > 0) {
            // 检查返工历史
            const reworkCount = ctx.rework_history?.length ?? 0;
            if (reworkCount >= MAX_REWORK) {
                return "停止升级";
            }
            // CEP: 回退 决策点
            const cepRepairResult = evaluateNoFalseCompletion({
                verification_passed: false, build_passed: false,
                browser_acceptance_passed: false, guard_passed: true,
                evidence_sufficient: false, current_status: ctx.status,
            });
            recordCepIntegration("回退", {
                passed: cepRepairResult.decision === "回退",
                violations: cepRepairResult.decision === "blocked" ? [{
                        principle: "auto_repair_no_fake_completion", severity: "blocking",
                        reason_zh: cepRepairResult.reason_zh, evidence_refs: [], required_action: "回退",
                    }] : [],
            });
            return "回退";
        }
    }
    //： 主链路集成检查失败 → 需要修复后重验证
    if (ctx.verification?.main_path_integration && !ctx.verification.main_path_integration.passed) {
        const reworkCount = ctx.rework_history?.length ?? 0;
        if (reworkCount >= MAX_REWORK) {
            return "停止升级";
        }
        // CEP: 回退 决策点（主链路集成失败）
        const cepRepairResult2 = evaluateNoFalseCompletion({
            verification_passed: false, build_passed: true,
            browser_acceptance_passed: true, guard_passed: true,
            evidence_sufficient: false, current_status: ctx.status,
        });
        recordCepIntegration("回退", {
            passed: cepRepairResult2.decision === "回退",
            violations: cepRepairResult2.decision === "blocked" ? [{
                    principle: "auto_repair_no_fake_completion", severity: "blocking",
                    reason_zh: cepRepairResult2.reason_zh, evidence_refs: [], required_action: "回退",
                }] : [],
        });
        return "回退";
    }
    // 验证失败 → 根据修复循环状态决定
    if (ctx.execution?.repair_loop) {
        const loop = ctx.execution.repair_loop;
        if (!loop.auto_repairable) {
            return "回退并标注";
        }
        if (loop.attempt >= loop.max_attempts) {
            return "回退并标注";
        }
    }
    return "推进下一阶段";
}
//# sourceMappingURL=phase_directive.js.map