import { checkGenericSpecOutput } from '../spec-quality/generic-output-gate.js';
import { checkGroundedSpecContract } from '../spec-grounding/contract.js';
export const DEFAULT_SDD_CONSTITUTION_RULES = [
    {
        id: 'sdd.grounded-contract',
        title: 'Grounded Spec Contract',
        level: 'blocking',
        category: 'grounding',
        description: 'Contract criteria and technical references must be grounded in user input, project evidence, or explicit review evidence.',
        nextAction: 'Add grounding metadata, remove ungrounded criteria from the contract, or move assumptions to advisory output.',
    },
    {
        id: 'sdd.no-generic-output',
        title: 'No Generic Spec Output',
        level: 'blocking',
        category: 'quality',
        description: 'Specs must not persist self-certifying criteria, placeholder ownership, unsupported verification claims, or example-only contract language.',
        nextAction: 'Replace generic output with grounded behavior, exact evidence, or a discovery question.',
    },
    {
        id: 'sdd.no-self-approval',
        title: 'No Self Approval',
        level: 'blocking',
        category: 'approval',
        description: 'The planner, spec author, or implementation agent must not approve their own spec or completion evidence.',
        nextAction: 'Attach independent reviewer evidence before approval or done.',
    },
    {
        id: 'sdd.done-evidence-required',
        title: 'Done Requires Evidence',
        level: 'blocking',
        category: 'evidence',
        description: 'Done requires traceability, validation evidence, reviewer evidence, and no unexplained drift from the approved contract.',
        nextAction: 'Add traceability rows, validation output, reviewer evidence, or reconcile approved scope changes.',
    },
    {
        id: 'sdd.actionable-metrics-only',
        title: 'Actionable Metrics Only',
        level: 'advisory',
        category: 'metrics',
        description: 'User-facing metrics must expose inputs and a concrete next action; vanity scores stay hidden.',
        nextAction: 'Show only metrics that identify a missing criterion, evidence gap, ambiguity, or drift risk.',
    },
];
export function loadSddConstitutionRules(overrides = []) {
    const overrideByRule = new Map(overrides.map((override) => [override.ruleId, override]));
    return DEFAULT_SDD_CONSTITUTION_RULES.map((rule) => {
        const override = overrideByRule.get(rule.id);
        if (!override) {
            return { ...rule, enabled: true };
        }
        return {
            ...rule,
            enabled: override.enabled ?? true,
            level: override.level ?? rule.level,
            overrideRationale: override.rationale,
        };
    });
}
export function validateSddConstitutionOverrides(overrides) {
    const known = new Set(DEFAULT_SDD_CONSTITUTION_RULES.map((rule) => rule.id));
    return overrides.flatMap((override) => {
        const issues = [];
        if (!known.has(override.ruleId)) {
            issues.push(`Unknown SDD constitution rule override: ${override.ruleId}`);
        }
        if (override.rationale.trim().length < 20) {
            issues.push(`Override ${override.ruleId} requires a rationale of at least 20 characters.`);
        }
        return issues;
    });
}
export function evaluateSddConstitutionRules(args) {
    const rules = loadSddConstitutionRules(args.overrides).filter((rule) => rule.enabled);
    const violations = [];
    if (rules.some((rule) => rule.id === 'sdd.grounded-contract')) {
        const grounding = checkGroundedSpecContract(args.spec, args.content);
        if (!grounding.passed) {
            violations.push(...grounding.issues.map((issue) => violation('sdd.grounded-contract', issue, [issue], rules)));
        }
    }
    if (rules.some((rule) => rule.id === 'sdd.no-generic-output')) {
        const generic = checkGenericSpecOutput(args.content);
        violations.push(...generic.issues.map((issue) => violation('sdd.no-generic-output', issue.reason, [issue.phrase], rules)));
    }
    return violations;
}
export function renderSddConstitutionRulesForHost() {
    const enabled = loadSddConstitutionRules();
    return [
        '### Constitutional SDD rules',
        ...enabled.map((rule) => `- ${rule.id} (${rule.level}): ${rule.description} Next action: ${rule.nextAction}`),
    ].join('\n');
}
function violation(ruleId, message, evidence, rules) {
    const rule = rules.find((candidate) => candidate.id === ruleId);
    return {
        ruleId,
        level: rule?.level ?? 'blocking',
        message,
        evidence,
        nextAction: rule?.nextAction ?? 'Fix the violated SDD constitution rule.',
    };
}
//# sourceMappingURL=sdd-rules-registry.js.map