/**
 * 用户承诺契约 — 问题三十 (CLI / README User Promise)。
 *
 * 管理 CLI 命令行为矩阵、README 命令漂移检测和用户心智边界。
 * - CliCommandContract 必须反映真实能力状态
 * - README 命令漂移必须检测
 * - 文档承诺不得超出真实能力
 */
// ── 内置 CLI 命令契约 ──
const BUILTIN_CLI_COMMANDS = [
    {
        command: "soloforge init",
        capability_ids: [],
        contract_ids: ["init_command"],
        default_mode: "write",
        reads: ["项目结构"],
        writes: ["目录", "模板"],
        side_effects: ["file_write"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge init --auto",
        capability_ids: [],
        contract_ids: ["init_auto_command"],
        default_mode: "write",
        reads: ["项目结构", "语言指纹"],
        writes: ["config"],
        side_effects: ["file_write", "config_write"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge init --auto --dry-run",
        capability_ids: [],
        contract_ids: ["init_auto_dry_run_command"],
        default_mode: "dry_run",
        reads: ["项目结构", "语言指纹"],
        writes: [],
        side_effects: ["none"],
        requires_confirmation: false,
        dry_run_available: true,
        documented_in_readme: true,
    },
    {
        command: "soloforge init --interactive",
        capability_ids: [],
        contract_ids: ["init_interactive_command"],
        default_mode: "write",
        reads: ["项目结构"],
        writes: ["config", "目录", "模板"],
        side_effects: ["file_write", "config_write"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge init --adapter claude",
        capability_ids: [],
        contract_ids: ["init_adapter_claude_command"],
        default_mode: "write",
        reads: [],
        writes: ["适配器配置"],
        side_effects: ["file_write", "config_write"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge init --adapter codex",
        capability_ids: [],
        contract_ids: ["init_adapter_codex_command"],
        default_mode: "write",
        reads: [],
        writes: ["适配器配置"],
        side_effects: ["file_write", "config_write"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge init --adapter trae",
        capability_ids: [],
        contract_ids: ["init_adapter_trae_command"],
        default_mode: "write",
        reads: [],
        writes: ["适配器配置"],
        side_effects: ["file_write", "config_write"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge init --adapter all",
        capability_ids: [],
        contract_ids: ["init_adapter_all_command"],
        default_mode: "write",
        reads: [],
        writes: ["适配器配置"],
        side_effects: ["file_write", "config_write"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge status",
        capability_ids: [],
        contract_ids: ["status_command"],
        default_mode: "read_only",
        reads: ["项目状态", "版本信息"],
        writes: [],
        side_effects: ["none"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge validate",
        capability_ids: [],
        contract_ids: ["validate_command"],
        default_mode: "read_only",
        reads: ["配置", "知识", "机制"],
        writes: [],
        side_effects: ["none"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge validate-release",
        capability_ids: [],
        contract_ids: ["validate_release_command"],
        default_mode: "read_only",
        reads: ["全部契约", "场景矩阵", "消费 trace", "双层机制"],
        writes: [],
        side_effects: ["none"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge cleanup --dry-run",
        capability_ids: [],
        contract_ids: ["cleanup_dry_run_command"],
        default_mode: "dry_run",
        reads: ["任务状态", "evidence 引用"],
        writes: [],
        side_effects: ["none"],
        requires_confirmation: false,
        dry_run_available: true,
        documented_in_readme: true,
    },
    {
        command: "soloforge cleanup --apply",
        capability_ids: [],
        contract_ids: ["cleanup_apply_command"],
        default_mode: "write",
        reads: ["任务状态", "evidence 引用"],
        writes: ["状态文件", "evidence 文件"],
        side_effects: ["file_write", "state_write"],
        requires_confirmation: true,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge audit-template-mechanisms",
        capability_ids: [],
        contract_ids: ["audit_template_mechanisms_command"],
        default_mode: "read_only",
        reads: ["模板", "机制注册表", "资产清单"],
        writes: [],
        side_effects: ["none"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge sync-templates",
        capability_ids: [],
        contract_ids: ["sync_templates_command"],
        default_mode: "dry_run",
        reads: ["模板清单", "版本信息"],
        writes: [],
        side_effects: ["none"],
        requires_confirmation: false,
        dry_run_available: true,
        documented_in_readme: true,
    },
    {
        command: "soloforge sync-templates --apply --confirm",
        capability_ids: [],
        contract_ids: ["sync_templates_apply_command"],
        default_mode: "write",
        reads: ["模板清单"],
        writes: ["模板文件"],
        side_effects: ["file_write", "knowledge_write"],
        requires_confirmation: true,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge sync-adapters",
        capability_ids: [],
        contract_ids: ["sync_adapters_command"],
        default_mode: "dry_run",
        reads: ["适配器 Prompt"],
        writes: [],
        side_effects: ["none"],
        requires_confirmation: false,
        dry_run_available: true,
        documented_in_readme: true,
    },
    {
        command: "soloforge sync-adapters --apply --confirm",
        capability_ids: [],
        contract_ids: ["sync_adapters_apply_command"],
        default_mode: "write",
        reads: ["适配器 Prompt"],
        writes: ["适配器文件"],
        side_effects: ["file_write", "config_write"],
        requires_confirmation: true,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge migrate",
        capability_ids: [],
        contract_ids: ["migrate_command"],
        default_mode: "dry_run",
        reads: ["项目版本", "schema 信息"],
        writes: [],
        side_effects: ["none"],
        requires_confirmation: false,
        dry_run_available: true,
        documented_in_readme: true,
    },
    {
        command: "soloforge migrate --apply --confirm",
        capability_ids: [],
        contract_ids: ["migrate_apply_command"],
        default_mode: "write",
        reads: ["项目版本"],
        writes: ["schema 文件", "config"],
        side_effects: ["file_write", "state_write", "config_write"],
        requires_confirmation: true,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge config resolve",
        capability_ids: [],
        contract_ids: ["config_resolve_command"],
        default_mode: "read_only",
        reads: ["配置"],
        writes: [],
        side_effects: ["none"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge config explain",
        capability_ids: [],
        contract_ids: ["config_explain_command"],
        default_mode: "read_only",
        reads: ["配置"],
        writes: [],
        side_effects: ["none"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
    {
        command: "soloforge generate-hooks",
        capability_ids: [],
        contract_ids: ["generate_hooks_command"],
        default_mode: "write",
        reads: [],
        writes: ["hooks 配置"],
        side_effects: ["file_write", "config_write"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: false,
    },
    {
        command: "soloforge generate-claude-md",
        capability_ids: [],
        contract_ids: ["generate_claude_md_command"],
        default_mode: "write",
        reads: [],
        writes: ["CLAUDE.md"],
        side_effects: ["file_write"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: false,
    },
    {
        command: "soloforge version",
        capability_ids: [],
        contract_ids: ["version_command"],
        default_mode: "read_only",
        reads: [],
        writes: [],
        side_effects: ["none"],
        requires_confirmation: false,
        dry_run_available: false,
        documented_in_readme: true,
    },
];
// ── 核心函数 ──
/**
 * 获取所有内置 CLI 命令契约。
 */
export function getCliCommandContracts() {
    return BUILTIN_CLI_COMMANDS.map((c) => ({ ...c }));
}
/**
 * 按命令名查找契约。
 */
export function findCliCommandContract(command) {
    return BUILTIN_CLI_COMMANDS.find((c) => c.command === command);
}
/**
 * 检测 README 命令漂移。
 * 硬规则：README 命令必须映射到真实 CliCommandContract。
 */
export function detectReadmeDrift(readmeCommands, cliContracts) {
    const driftItems = [];
    const cliCommandSet = new Set(cliContracts.map((c) => c.command));
    // README 中存在但 CLI 中不存在的命令
    for (const cmd of readmeCommands) {
        if (!cliCommandSet.has(cmd)) {
            driftItems.push({
                command: cmd,
                drift_type: "missing_in_cli",
                message_zh: `README 命令 "${cmd}" 在 CLI 契约中不存在`,
            });
        }
    }
    // CLI 中存在且标记 documented_in_readme 但 README 中不存在的命令
    for (const contract of cliContracts) {
        if (contract.documented_in_readme && !readmeCommands.includes(contract.command)) {
            driftItems.push({
                command: contract.command,
                drift_type: "missing_in_readme",
                cli_contract: contract,
                message_zh: `CLI 命令 "${contract.command}" 标记为需要在 README 中说明但未找到`,
            });
        }
    }
    return {
        has_drift: driftItems.length > 0,
        drift_items: driftItems,
    };
}
/**
 * 验证用户承诺一致性。
 * 硬规则：
 * - README 不得出现未实现命令
 * - advisory 能力不得写成 enforced
 * - removed/deprecated 命令不得写成可用
 * - dry-run 命令不得写成 apply
 */
export function validateUserPromise(readmeCommands, cliContracts) {
    const findings = [];
    const driftResult = detectReadmeDrift(readmeCommands, cliContracts);
    for (const item of driftResult.drift_items) {
        if (item.drift_type === "missing_in_cli") {
            findings.push({
                severity: "hard_fail",
                rule: "readme_command_must_exist",
                command: item.command,
                message_zh: item.message_zh,
            });
        }
        if (item.drift_type === "missing_in_readme") {
            findings.push({
                severity: "warning",
                rule: "cli_command_should_be_documented",
                command: item.command,
                message_zh: item.message_zh,
            });
        }
    }
    return {
        valid: !findings.some((f) => f.severity === "hard_fail"),
        findings,
    };
}
/**
 * 验证 CLI 命令默认行为安全性。
 * 硬规则：
 * - sync-templates 默认 dry_run
 * - migrate 默认 dry_run
 * - sync-adapters 默认 dry_run
 * - init 不得覆盖已有知识
 */
export function validateCommandDefaults(contracts) {
    const findings = [];
    for (const c of contracts) {
        if (c.command.includes("sync-templates") &&
            !c.command.includes("--apply") &&
            c.default_mode !== "dry_run") {
            findings.push({
                severity: "hard_fail",
                rule: "sync_templates_default_dry_run",
                command: c.command,
                message_zh: `sync-templates 默认模式必须是 dry_run，当前为 ${c.default_mode}`,
            });
        }
        if (c.command.includes("sync-adapters") &&
            !c.command.includes("--apply") &&
            c.default_mode !== "dry_run") {
            findings.push({
                severity: "hard_fail",
                rule: "sync_adapters_default_dry_run",
                command: c.command,
                message_zh: `sync-adapters 默认模式必须是 dry_run，当前为 ${c.default_mode}`,
            });
        }
        if (c.command.includes("migrate") &&
            !c.command.includes("--apply") &&
            c.default_mode !== "dry_run") {
            findings.push({
                severity: "hard_fail",
                rule: "migrate_default_dry_run",
                command: c.command,
                message_zh: `migrate 默认模式必须是 dry_run，当前为 ${c.default_mode}`,
            });
        }
    }
    return findings;
}
//# sourceMappingURL=user_promise.js.map