/**
 * 工作流规则生成器
 *
 * 从 adapters/shared/workflow_template.ts 提取至 engine 层，
 * 修正 engine→adapter 的依赖方向违规。
 */
import { debug } from "../core/logger.js";
import { WORKFLOW_TEMPLATE_HARD_RULES } from "./asset_manifest.js";
const SF_HARD_RULE_RE = /@sf-hard-rule[^>]*mechanism=(\S+)/g;
/** 从结构化清单生成 @sf-hard-rule 标记 */
function hardRuleTag(ruleId) {
    const rule = WORKFLOW_TEMPLATE_HARD_RULES.find(r => r.rule_id === ruleId);
    if (!rule)
        return `<!-- @sf-hard-rule id=${ruleId} MISSING_IN_MANIFEST -->`;
    return `<!-- @sf-hard-rule id=${rule.rule_id} mechanism=${rule.mechanism_id} enforcement=${rule.enforcement_status} summary="${rule.rule_summary}" -->`;
}
/** 渲染一条规则的 marker + user_instruction */
function renderRule(ruleId) {
    const rule = WORKFLOW_TEMPLATE_HARD_RULES.find(r => r.rule_id === ruleId);
    const tag = hardRuleTag(ruleId);
    const text = rule?.user_instruction ?? "";
    if (text)
        return `${tag}\n${text}`;
    return tag;
}
/** 渲染一组规则（marker + user_instruction） */
function renderRules(ruleIds) {
    return ruleIds.map(id => renderRule(id)).join("\n");
}
/** 工作流步骤定义: 每步包含标题和有序规则 ID */
const WORKFLOW_STEPS = [
    { title: "", rule_ids: ["wf-privacy-before-expand", "wf-knowledge-injection", "wf-core-principles"] },
    { title: "### 步骤 1: 分类", rule_ids: ["wf-classify-first"] },
    { title: "### 步骤 1.5: 入口裁决", rule_ids: ["wf-routing-gate", "wf-strategy-no-route"] },
    { title: "### 步骤 1.6: 多阶段拆解", rule_ids: ["wf-multi-stage-loop"] },
    { title: "### 步骤 2: 执行", rule_ids: ["wf-dispatch-by-route", "wf-command-contract", "wf-tool-contract", "wf-architecture-workshop-first", "wf-design-artifact-pack", "wf-standard-asset-contract", "wf-template-asset-visibility", "wf-code-observability"] },
    { title: "### 步骤 2.5: 导航", rule_ids: ["wf-navigation-contract"] },
    { title: "### 步骤 3: 验证", rule_ids: ["wf-verify-after-change", "wf-evidence-grounding", "wf-no-lazy-code"] },
    { title: "### 步骤 4: 学习", rule_ids: ["wf-learn-after-verify"] },
    { title: "### 步骤 5: 汇报", rule_ids: ["wf-no-auto-capability-change"] },
];
/** 当前嵌入的权威规则校验和 — 由 `npm run update-workflow-checksum` 更新。 */
const EMBEDDED_CHECKSUM = "801b74d49a5d8ff2";
export { EMBEDDED_CHECKSUM };
/**
 * 生成工作流规则模板内容。
 * 从 WORKFLOW_TEMPLATE_HARD_RULES 结构化清单循环渲染每条规则的 marker + user_instruction。
 * @param config - 项目配置
 * @returns 工作流规则文本
 */
export function generateWorkflowRules(_config) {
    debug("工作流模板", "生成工作流规则");
    const header = `# SoloForge 集成规则

## 强制工作流

当收到开发指令时（非纯对话/提问），必须按以下顺序执行:`;
    const sections = WORKFLOW_STEPS.map(step => {
        const rulesText = renderRules(step.rule_ids);
        if (step.title)
            return `${step.title}\n${rulesText}`;
        return rulesText;
    }).join("\n\n");
    const footer = `## 项目信息

项目事实以 config 配置及 tool 输出为准，不在模板中硬编码。

<!-- @sf-checksum: ${EMBEDDED_CHECKSUM} -->`;
    return `${header}\n\n${sections}\n\n${footer}`;
}
/**
 * 提取 generateWorkflowRules 输出中所有 @sf-hard-rule 引用的机制 ID（去重）。
 */
export function getWorkflowRuleMechanismIds() {
    const text = generateWorkflowRules({});
    const ids = new Set();
    let m;
    while ((m = SF_HARD_RULE_RE.exec(text)) !== null) {
        ids.add(m[1]);
    }
    return [...ids];
}
//# sourceMappingURL=workflow_rule_generator.js.map