/**
 * 配置落盘边界。
 *
 * 区分空项目、已有项目和运行时推断，防止自动推断结果在未确认时写入用户工程。
 */
import fs from "node:fs";
import path from "node:path";
const PROJECT_MARKERS = [
    "package.json",
    "pom.xml",
    "build.gradle",
    "settings.gradle",
    "pyproject.toml",
    "go.mod",
    "Cargo.toml",
    "src",
];
export function classifyConfigContext(projectDir, opts) {
    if (opts?.runtime_inference) {
        return { kind: "runtime_inference", project_path: projectDir, evidence: ["runtime_inference"], confirmed: opts.confirmed === true };
    }
    const evidence = PROJECT_MARKERS.filter((m) => fs.existsSync(path.join(projectDir, m)));
    const hasSoloforgeState = fs.existsSync(path.join(projectDir, ".soloforge"))
        || fs.existsSync(path.join(projectDir, ".mcp.json"))
        || fs.existsSync(path.join(projectDir, "CLAUDE.md"));
    if (evidence.length === 0 && !hasSoloforgeState) {
        return { kind: "greenfield", project_path: projectDir, evidence: ["empty_project"], confirmed: opts?.confirmed === true };
    }
    return { kind: "existing_project", project_path: projectDir, evidence, confirmed: opts?.confirmed === true };
}
export function isWriteAllowed(context, targetPath) {
    const normalized = targetPath.replace(/\\/g, "/");
    const isSoloforgeManaged = normalized.includes(".soloforge/")
        || normalized.endsWith(".mcp.json")
        || normalized.endsWith("CLAUDE.md")
        || normalized.includes(".claude/");
    if (context.kind === "runtime_inference") {
        return {
            allowed: false,
            reason_zh: "运行时推断结果不得自动落盘，请使用 init --auto 或明确确认后写入。",
            requires_confirmation: true,
        };
    }
    if (context.kind === "existing_project" && isSoloforgeManaged && !context.confirmed) {
        return {
            allowed: false,
            reason_zh: "已有项目写入 SoloForge 管理文件需要用户确认。",
            requires_confirmation: true,
        };
    }
    return { allowed: true, reason_zh: "写入边界允许", requires_confirmation: false };
}
export function getWriteConfirmationPrompt(context, targetPath) {
    const decision = isWriteAllowed(context, targetPath);
    if (decision.allowed)
        return "无需额外确认。";
    return `即将向 ${context.kind} 项目写入 ${targetPath}。${decision.reason_zh}`;
}
export function validateWriteBoundary(context, writes) {
    const violations = [];
    for (const target of writes) {
        const decision = isWriteAllowed(context, target);
        if (!decision.allowed) {
            violations.push({ target_path: target, reason_zh: decision.reason_zh });
        }
        // [DOC-3] 同步模板不得覆盖用户已修改资产
        if (fs.existsSync(target)) {
            try {
                const stat = fs.statSync(target);
                // 如果文件存在且有实质内容（>100 字节视为用户修改），且不是 SoloForge 管理文件，则标记警告
                if (stat.size > 100 && !target.includes(".soloforge/")) {
                    violations.push({
                        target_path: target,
                        reason_zh: `文件 ${path.basename(target)} 已存在且有内容（${stat.size} 字节），同步模板可能覆盖用户修改。请确认或备份后重试。`,
                    });
                }
            }
            catch {
                // 文件无法读取则跳过检查
            }
        }
    }
    return violations;
}
//# sourceMappingURL=config_write_boundary.js.map