/**
 * 工作流模板 — 校验和管理与 re-export
 *
 * generateWorkflowRules 和 getWorkflowRuleMechanismIds 已移至 engine 层
 * (workflow_rule_generator.ts)，修正 engine→adapter 的依赖方向违规。
 * 此文件保留校验和更新功能及向后兼容的 re-export。
 */
import fs from "node:fs";
import path from "node:path";
import crypto from "node:crypto";
import { findLayerMapByMechanismId } from "../../engine/contracts/dual_layer_mechanism_registry.js";
import { getWorkflowRuleMechanismIds as _getWorkflowRuleMechanismIds } from "../../engine/templates/workflow_rule_generator.js";
// 从 engine 层 re-export 以保持向后兼容
export { generateWorkflowRules, getWorkflowRuleMechanismIds, EMBEDDED_CHECKSUM } from "../../engine/templates/workflow_rule_generator.js";
/** 当前嵌入的权威规则校验和 — 由 `npm run update-workflow-checksum` 更新。 */
const EMBEDDED_CHECKSUM = "801b74d49a5d8ff2";
/**
 * 计算所有权威规则资产的第一行非注释内容组合的 SHA256 校验和。
 * 权威规则内容变化时校验和必然变化。
 */
export function computeAuthoritativeRulesChecksum(rootDir) {
    const mechanismIds = _getWorkflowRuleMechanismIds();
    const parts = [];
    for (const mid of mechanismIds) {
        const map = findLayerMapByMechanismId(mid);
        if (!map?.template_assets)
            continue;
        for (const asset of map.template_assets) {
            const fullPath = path.join(rootDir, asset.path);
            if (!fs.existsSync(fullPath))
                continue;
            const content = fs.readFileSync(fullPath, "utf-8");
            // 取前 200 字符（去掉 frontmatter 后的首段）作为校验依据
            const bodyStart = content.indexOf("---", content.indexOf("---") + 3);
            const body = bodyStart >= 0 ? content.substring(bodyStart + 3).trimStart() : content;
            parts.push(`${asset.path}:${body.substring(0, 500)}`);
        }
    }
    return crypto.createHash("sha256").update(parts.join("\n")).digest("hex").substring(0, 16);
}
function getEmbeddedChecksum() {
    return EMBEDDED_CHECKSUM;
}
/**
 * 校验工作流模板中每条 @sf-hard-rule 都能找到权威机制注册条目，
 * 且其 template_layer 资产文件存在，且内容校验和与嵌入值一致。
 */
export function validateWorkflowRuleSources(rootDir) {
    const mechanismIds = _getWorkflowRuleMechanismIds();
    const missing_mechanisms = [];
    const missing_assets = [];
    for (const mid of mechanismIds) {
        const map = findLayerMapByMechanismId(mid);
        if (!map) {
            missing_mechanisms.push(mid);
            continue;
        }
        if (map.template_assets) {
            for (const asset of map.template_assets) {
                if (!fs.existsSync(path.join(rootDir, asset.path))) {
                    missing_assets.push(asset.path);
                }
            }
        }
    }
    // 内容校验和验证
    const computedChecksum = computeAuthoritativeRulesChecksum(rootDir);
    const embeddedChecksum = getEmbeddedChecksum();
    const checksumMismatch = computedChecksum !== embeddedChecksum;
    return {
        valid: missing_mechanisms.length === 0 && missing_assets.length === 0 && !checksumMismatch,
        missing_mechanisms,
        missing_assets,
        checksum_mismatch: checksumMismatch,
        embedded_checksum: embeddedChecksum,
        computed_checksum: computedChecksum,
    };
}
/**
 * 更新嵌入校验和到源文件。由 CI 或开发者在确认权威规则变化后调用。
 */
export function updateEmbeddedChecksum(rootDir) {
    const newChecksum = computeAuthoritativeRulesChecksum(rootDir);
    const selfPath = path.join(rootDir, "src", "adapters", "shared", "workflow_template.ts");
    if (fs.existsSync(selfPath)) {
        let content = fs.readFileSync(selfPath, "utf-8");
        content = content.replace(/const EMBEDDED_CHECKSUM = "[^"]*";/, `const EMBEDDED_CHECKSUM = "${newChecksum}";`);
        fs.writeFileSync(selfPath, content, "utf-8");
    }
    return newChecksum;
}
//# sourceMappingURL=workflow_template.js.map