/**
 * 设计到交付生命周期合同。
 *
 * 本模块是设计、实现、测试、验收各节点输入输出的唯一口径。
 * 其他门禁只能消费这里定义的阶段产物，不能在 sf_verify 或 hook 中临时新增文件假设。
 */
export const DESIGN_LIFECYCLE_TRACEABILITY_MATRIX_PATH = "docs/traceability/01-需求原型设计实现追踪矩阵.md";
export const DESIGN_LIFECYCLE_SLICE_PLAN_PATH = "docs/architecture/04-开发切片计划.md";
export const DESIGN_LIFECYCLE_LEGACY_SLICE_PLAN_PATHS = [
    "docs/architecture/04-第一阶段开发切片计划.md",
];
export const DESIGN_LIFECYCLE_SQL_ROOTS = [
    "db/migrations",
    "db/migration",
    "db/schema",
    "db/changelog",
    "docs/db/migration",
    "backend/db/migration",
    "backend/src/main/resources/db/migration",
    "server/src/main/resources/db/migration",
    "apps/backend/src/main/resources/db/migration",
];
const INPUT_REQUIREMENTS = {
    id: "requirements_document",
    role: "input",
    path: "docs/requirements.md",
    required: true,
    description_zh: "需求来源，提供 REQ-* 身份和业务目标",
    aliases: ["docs/需求文档.md", "docs/PRD.md"],
};
const INPUT_PROTOTYPE = {
    id: "prototype_specification",
    role: "input",
    path: "docs/prototype.md",
    required: true,
    description_zh: "原型说明，提供 PROTO-* 身份、页面和交互来源",
    aliases: ["docs/原型说明.md", "docs/prototype-platform.md"],
};
const TRACEABILITY_MATRIX = {
    id: "traceability_matrix",
    role: "authority",
    path: DESIGN_LIFECYCLE_TRACEABILITY_MATRIX_PATH,
    required: true,
    description_zh: "REQ/PROTO/ARCH/DD/PHASE/SLICE/AC 全链路追踪权威资产",
};
const LIFECYCLE_STAGES = [
    {
        stage_id: "requirements_intake",
        name_zh: "需求输入",
        purpose_zh: "把用户需求转成可追踪 REQ-*，作为架构和验收来源",
        inputs: [],
        outputs: [INPUT_REQUIREMENTS],
        authorities: [TRACEABILITY_MATRIX],
        gates: [
            {
                id: "requirements_id_gate",
                owner: "sf_expand",
                blocks: ["无 REQ-* 仍生成正式架构或实现"],
                required_evidence: ["需求条目已进入追踪矩阵"],
                recovery_command: "soloforge audit-design-artifacts",
            },
        ],
    },
    {
        stage_id: "prototype_extraction",
        name_zh: "原型提取",
        purpose_zh: "把原型页面、交互和字段转成 PROTO-*，支撑架构与验收",
        inputs: [],
        outputs: [INPUT_PROTOTYPE],
        authorities: [TRACEABILITY_MATRIX],
        gates: [
            {
                id: "prototype_id_gate",
                owner: "sf_expand",
                blocks: ["无 PROTO-* 或来源确认仍生成正式架构"],
                required_evidence: ["原型条目已进入追踪矩阵"],
                recovery_command: "soloforge audit-design-artifacts",
            },
        ],
    },
    {
        stage_id: "architecture_design",
        name_zh: "架构设计",
        purpose_zh: "基于需求和原型确认系统边界、技术决策、模块职责和全局约束",
        inputs: [INPUT_REQUIREMENTS, INPUT_PROTOTYPE],
        outputs: [
            {
                id: "architecture_decision_record",
                role: "output",
                path: "docs/architecture/00-架构决策记录.md",
                required: true,
                description_zh: "架构决策和人工确认记录",
            },
            {
                id: "architecture_document",
                role: "output",
                path: "docs/architecture/01-架构设计文档.md",
                required: true,
                description_zh: "总体架构、边界、模块、技术选型和 ARCH-* 定义",
            },
        ],
        authorities: [TRACEABILITY_MATRIX],
        gates: [
            {
                id: "architecture_source_gate",
                owner: "sf_expand",
                blocks: ["架构设计缺少 REQ-* 或 PROTO-* 来源"],
                required_evidence: ["架构文档引用 REQ-* 或 PROTO-*", "架构决策已确认"],
                recovery_command: "soloforge audit-design-artifacts",
            },
        ],
    },
    {
        stage_id: "detail_design",
        name_zh: "详细设计",
        purpose_zh: "把架构决策落实为可编码的数据、接口和实现约束；详细设计不是固定单文件",
        inputs: [
            {
                id: "architecture_document",
                role: "input",
                path: "docs/architecture/01-架构设计文档.md",
                required: true,
                description_zh: "ARCH-* 来源和全局约束",
            },
        ],
        outputs: [
            {
                id: "database_design_document",
                role: "output",
                path: "docs/architecture/02-数据库设计文档.md",
                required: true,
                description_zh: "数据库详细设计，承载 DD-T*、表字段、索引、审计、租户和生命周期语义",
            },
            {
                id: "api_specification_document",
                role: "output",
                path: "docs/architecture/03-API接口规格文档.md",
                required: true,
                description_zh: "API 详细设计，承载 DD-A*、端点、字段、错误、副作用和验收场景",
            },
            {
                id: "optional_implementation_detail_document",
                role: "output",
                path: "docs/architecture/04-详细设计文档.md",
                required: false,
                description_zh: "可选的单文档详细设计兼容口径；有 DB/API 拆分载体时不得作为必需项",
            },
        ],
        authorities: [
            {
                id: "openapi_authority",
                role: "authority",
                path: "docs/api/openapi.yaml",
                required: true,
                description_zh: "API 路径、操作和 schema 权威资产",
            },
            {
                id: "sql_migration_authority",
                role: "authority",
                path: "db/migrations",
                required: true,
                description_zh: "数据库物理结构权威资产，可由 SQL roots 任一有效目录承载",
                aliases: [...DESIGN_LIFECYCLE_SQL_ROOTS],
            },
            TRACEABILITY_MATRIX,
        ],
        gates: [
            {
                id: "detail_design_source_gate",
                owner: "sf_expand",
                blocks: ["详细设计缺少 ARCH-* 来源或 DD-* 身份"],
                required_evidence: ["数据库设计声明 DD-T* 并引用 ARCH-*", "API 规格声明 DD-A* 并引用 ARCH-*"],
                recovery_command: "soloforge audit-design-artifacts",
            },
            {
                id: "detail_design_authority_gate",
                owner: "sf_verify",
                blocks: ["只有 Markdown 说明，没有 OpenAPI 或 SQL/migration 权威资产"],
                required_evidence: ["OpenAPI 可解析且覆盖 API 文档", "SQL/migration 存在且可审计"],
                recovery_command: "soloforge audit-design-artifacts",
            },
        ],
    },
    {
        stage_id: "slice_planning",
        name_zh: "切片规划",
        purpose_zh: "把详细设计拆成可独立实现、测试和验收的 SLICE-* 工作包",
        inputs: [
            {
                id: "database_design_document",
                role: "input",
                path: "docs/architecture/02-数据库设计文档.md",
                required: true,
                description_zh: "DD-T* 数据库设计来源",
            },
            {
                id: "api_specification_document",
                role: "input",
                path: "docs/architecture/03-API接口规格文档.md",
                required: true,
                description_zh: "DD-A* API 设计来源",
            },
        ],
        outputs: [
            {
                id: "slice_plan",
                role: "output",
                path: DESIGN_LIFECYCLE_SLICE_PLAN_PATH,
                required: true,
                description_zh: "全项目开发切片计划索引，定义 PHASE-*、SLICE-*、设计来源、计划变更文件、状态和 AC-* 验收标准",
                aliases: [...DESIGN_LIFECYCLE_LEGACY_SLICE_PLAN_PATHS],
            },
        ],
        authorities: [TRACEABILITY_MATRIX],
        gates: [
            {
                id: "slice_design_acceptance_gate",
                owner: "sf_expand",
                blocks: ["切片缺少 PHASE-*、SLICE-*、DD/ARCH 来源或 AC-* 验收"],
                required_evidence: ["切片计划声明 PHASE-* 与 SLICE-*", "切片计划引用 DD-* 或 ARCH-*", "切片计划声明 AC-*"],
                recovery_command: "soloforge audit-design-artifacts",
            },
        ],
    },
    {
        stage_id: "implementation",
        name_zh: "实现",
        purpose_zh: "只按已确认切片、详细设计和验收标准修改业务代码",
        inputs: [
            TRACEABILITY_MATRIX,
            {
                id: "slice_plan",
                role: "input",
                path: DESIGN_LIFECYCLE_SLICE_PLAN_PATH,
                required: true,
                description_zh: "当前实现必须绑定的 PHASE-* 与 SLICE-* 来源",
                aliases: [...DESIGN_LIFECYCLE_LEGACY_SLICE_PLAN_PATHS],
            },
        ],
        outputs: [
            {
                id: "code_changes",
                role: "output",
                path: "changed_files",
                required: true,
                description_zh: "真实变更文件清单，必须完整进入 sf_verify",
            },
        ],
        authorities: [TRACEABILITY_MATRIX],
        gates: [
            {
                id: "implementation_binding_gate",
                owner: "sf_verify",
                blocks: ["业务代码变更缺少 PHASE-*、SLICE-*、DD-*、AC-* 绑定"],
                required_evidence: ["TaskContext 或意图中绑定 PHASE/SLICE/DD/AC", "changed_files 完整"],
                recovery_command: "soloforge next",
            },
        ],
    },
    {
        stage_id: "testing",
        name_zh: "测试",
        purpose_zh: "用真实命令和结果证明实现满足切片验收，不把验证计划当结果",
        inputs: [
            {
                id: "code_changes",
                role: "input",
                path: "changed_files",
                required: true,
                description_zh: "真实变更文件清单",
            },
            TRACEABILITY_MATRIX,
        ],
        outputs: [
            {
                id: "verification_result",
                role: "evidence",
                path: ".soloforge/state/{task_id}.json#verification",
                required: true,
                description_zh: "真实执行的构建、测试和验收命令结果",
            },
        ],
        authorities: [TRACEABILITY_MATRIX],
        gates: [
            {
                id: "verification_result_gate",
                owner: "sf_verify",
                blocks: ["验证计划被当成验证结果", "只验证部分 changed_files"],
                required_evidence: ["命令真实执行", "结果覆盖 changed_files 和 AC-*"],
                recovery_command: "soloforge next",
            },
        ],
    },
    {
        stage_id: "acceptance",
        name_zh: "验收",
        purpose_zh: "把设计一致性、测试结果和用户确认闭合为可交付证据",
        inputs: [
            {
                id: "verification_result",
                role: "input",
                path: ".soloforge/state/{task_id}.json#verification",
                required: true,
                description_zh: "测试阶段真实验证结果",
            },
            TRACEABILITY_MATRIX,
        ],
        outputs: [
            {
                id: "design_consistency_report",
                role: "evidence",
                path: "docs/architecture/99-设计一致性验收报告.md",
                required: true,
                description_zh: "设计产物一致性和复验闭环证据",
            },
        ],
        authorities: [TRACEABILITY_MATRIX],
        gates: [
            {
                id: "acceptance_closure_gate",
                owner: "sf_verify",
                blocks: ["缺少设计一致性、测试结果或用户确认仍交付"],
                required_evidence: ["design_artifact_pack.status=implementation_ready", "verification passed", "必要用户确认"],
                recovery_command: "soloforge audit-design-artifacts",
            },
        ],
    },
    {
        stage_id: "delivery",
        name_zh: "交付",
        purpose_zh: "只交付来源、实现、测试和验收均闭合的任务",
        inputs: [
            {
                id: "design_consistency_report",
                role: "input",
                path: "docs/architecture/99-设计一致性验收报告.md",
                required: true,
                description_zh: "验收阶段闭合证据",
            },
        ],
        outputs: [
            {
                id: "delivery_summary",
                role: "evidence",
                path: ".soloforge/state/{task_id}.json#delivery",
                required: true,
                description_zh: "交付摘要、阻断项和验证证据",
            },
        ],
        authorities: [TRACEABILITY_MATRIX],
        gates: [
            {
                id: "delivery_no_open_blockers_gate",
                owner: "sf_verify",
                blocks: ["仍有追踪、设计、测试或验收阻断项"],
                required_evidence: ["所有 hard_fail 清零", "最近验证 trace 可追踪"],
                recovery_command: "soloforge next",
            },
        ],
    },
];
export function listDesignLifecycleStageContracts() {
    return LIFECYCLE_STAGES.map((stage) => ({
        ...stage,
        inputs: [...stage.inputs],
        outputs: [...stage.outputs],
        authorities: [...stage.authorities],
        gates: [...stage.gates],
    }));
}
export function getDesignLifecycleStageContract(stageId) {
    const stage = LIFECYCLE_STAGES.find((candidate) => candidate.stage_id === stageId);
    if (!stage)
        throw new Error(`Unknown design lifecycle stage: ${stageId}`);
    return {
        ...stage,
        inputs: [...stage.inputs],
        outputs: [...stage.outputs],
        authorities: [...stage.authorities],
        gates: [...stage.gates],
    };
}
export function getDesignLifecycleArtifactPaths() {
    const architecture = getDesignLifecycleStageContract("architecture_design");
    const detail = getDesignLifecycleStageContract("detail_design");
    const slice = getDesignLifecycleStageContract("slice_planning");
    const acceptance = getDesignLifecycleStageContract("acceptance");
    const decisionRecord = architecture.outputs.find((artifact) => artifact.id === "architecture_decision_record");
    const architectureDocument = architecture.outputs.find((artifact) => artifact.id === "architecture_document");
    const databaseDocument = detail.outputs.find((artifact) => artifact.id === "database_design_document");
    const apiDocument = detail.outputs.find((artifact) => artifact.id === "api_specification_document");
    const optionalDetailDocument = detail.outputs.find((artifact) => artifact.id === "optional_implementation_detail_document");
    const openapi = detail.authorities.find((artifact) => artifact.id === "openapi_authority");
    const slicePlan = slice.outputs.find((artifact) => artifact.id === "slice_plan");
    const consistencyReport = acceptance.outputs.find((artifact) => artifact.id === "design_consistency_report");
    return {
        decision_record: decisionRecord.path,
        architecture_document: architectureDocument.path,
        database_document: databaseDocument.path,
        api_document: apiDocument.path,
        optional_detail_design_document: optionalDetailDocument.path,
        slice_plan: slicePlan.path,
        consistency_report: consistencyReport.path,
        traceability_matrix: DESIGN_LIFECYCLE_TRACEABILITY_MATRIX_PATH,
        openapi: openapi.path,
        sql_roots: [...DESIGN_LIFECYCLE_SQL_ROOTS],
    };
}
export function getDesignLifecycleArtifactAliases(path) {
    for (const stage of LIFECYCLE_STAGES) {
        for (const artifact of [...stage.inputs, ...stage.outputs, ...stage.authorities]) {
            if (artifact.path === path)
                return [...(artifact.aliases ?? [])];
        }
    }
    return [];
}
export function validateDesignLifecycleContract() {
    const findings = [];
    const stageIds = new Set();
    for (const stage of LIFECYCLE_STAGES) {
        if (stageIds.has(stage.stage_id)) {
            findings.push({
                code: "DESIGN_LIFECYCLE_DUPLICATE_STAGE",
                severity: "hard_fail",
                stage_id: stage.stage_id,
                message_zh: `生命周期阶段重复: ${stage.stage_id}`,
            });
        }
        stageIds.add(stage.stage_id);
        if (stage.gates.length === 0) {
            findings.push({
                code: "DESIGN_LIFECYCLE_STAGE_WITHOUT_GATE",
                severity: "hard_fail",
                stage_id: stage.stage_id,
                message_zh: `生命周期阶段缺少门禁: ${stage.stage_id}`,
            });
        }
    }
    const detail = getDesignLifecycleStageContract("detail_design");
    const requiredDetailOutputs = detail.outputs.filter((artifact) => artifact.required).map((artifact) => artifact.id);
    for (const required of ["database_design_document", "api_specification_document"]) {
        if (!requiredDetailOutputs.includes(required)) {
            findings.push({
                code: "DESIGN_LIFECYCLE_DETAIL_OUTPUT_MISSING",
                severity: "hard_fail",
                stage_id: "detail_design",
                message_zh: `详细设计缺少必需输出: ${required}`,
            });
        }
    }
    if (requiredDetailOutputs.includes("optional_implementation_detail_document")) {
        findings.push({
            code: "DESIGN_LIFECYCLE_OPTIONAL_DETAIL_DOC_REQUIRED",
            severity: "hard_fail",
            stage_id: "detail_design",
            message_zh: "详细设计不能强制要求 04-详细设计文档.md；DB/API 拆分载体必须可独立闭合",
        });
    }
    const slice = getDesignLifecycleStageContract("slice_planning");
    const slicePlan = slice.outputs.find((artifact) => artifact.id === "slice_plan" && artifact.required);
    if (!slicePlan) {
        findings.push({
            code: "DESIGN_LIFECYCLE_SLICE_PLAN_MISSING",
            severity: "hard_fail",
            stage_id: "slice_planning",
            message_zh: "实现前缺少必需切片计划输出",
        });
    }
    if (slicePlan?.path !== DESIGN_LIFECYCLE_SLICE_PLAN_PATH) {
        findings.push({
            code: "DESIGN_LIFECYCLE_SLICE_PLAN_CANONICAL_PATH_INVALID",
            severity: "hard_fail",
            stage_id: "slice_planning",
            message_zh: "开发切片计划必须是全项目索引 04-开发切片计划.md，不能绑定到某个固定阶段文件",
        });
    }
    const implementation = getDesignLifecycleStageContract("implementation");
    const implementationGate = implementation.gates.find((gate) => gate.id === "implementation_binding_gate");
    if (!implementationGate || !implementationGate.required_evidence.some((evidence) => evidence.includes("PHASE/SLICE/DD/AC"))) {
        findings.push({
            code: "DESIGN_LIFECYCLE_IMPLEMENTATION_BINDING_MISSING",
            severity: "hard_fail",
            stage_id: "implementation",
            message_zh: "实现阶段缺少 PHASE/SLICE/DD/AC 绑定证据要求",
        });
    }
    const testing = getDesignLifecycleStageContract("testing");
    if (!testing.gates.some((gate) => gate.blocks.some((block) => block.includes("验证计划被当成验证结果")))) {
        findings.push({
            code: "DESIGN_LIFECYCLE_TEST_EVIDENCE_WEAK",
            severity: "hard_fail",
            stage_id: "testing",
            message_zh: "测试阶段必须阻断把验证计划当成验证结果",
        });
    }
    return findings;
}
//# sourceMappingURL=design_lifecycle_contract.js.map