"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VALIDATION_COMMAND_OUTPUT_CONTRACTS = exports.VALIDATION_FINDING_JSON_OUTPUT_CONTRACT = exports.VALIDATION_COMMAND_REUSE_POLICY = void 0;
exports.createCompactValidationFindingSummary = createCompactValidationFindingSummary;
exports.getValidationCommandOutputContracts = getValidationCommandOutputContracts;
exports.VALIDATION_COMMAND_REUSE_POLICY = {
    "validation-rollup": {
        category: "validation-rollup",
        commandFamily: "validation rollup",
        reusesSharedFindingEnvelope: true,
        preservesCommandEvidence: true,
        readOnly: true,
        requiredSharedFields: ["id", "code", "level", "severity", "ruleFamily", "artifactPath", "affectedPaths", "artifacts", "evidenceSource", "reason", "noWriteBoundary", "recommendedCommand", "recovery", "evidence"],
        deferredDetailsSchema: "downstream-validation-rollup-command",
        noWriteBoundary: "Report rollup evidence only; do not repair metadata, synchronize links, mutate manifests, update status, or generate artifacts."
    },
    "links-check": {
        category: "links-check",
        commandFamily: "links check",
        reusesSharedFindingEnvelope: true,
        preservesCommandEvidence: true,
        readOnly: true,
        requiredSharedFields: ["id", "code", "level", "severity", "ruleFamily", "artifactPath", "affectedPaths", "artifacts", "evidenceSource", "reason", "noWriteBoundary", "recommendedCommand", "recovery", "evidence"],
        deferredDetailsSchema: "downstream-links-check-command",
        noWriteBoundary: "Report trace-link evidence only; do not repair links, rewrite parent references, synchronize metadata, or generate artifacts."
    },
    "scope-guard": {
        category: "scope-guard",
        commandFamily: "scope guard",
        reusesSharedFindingEnvelope: true,
        preservesCommandEvidence: true,
        readOnly: true,
        requiredSharedFields: ["id", "code", "level", "severity", "ruleFamily", "artifactPath", "affectedPaths", "artifacts", "evidenceSource", "reason", "noWriteBoundary", "recommendedCommand", "recovery", "evidence"],
        deferredDetailsSchema: "downstream-scope-guard-command",
        noWriteBoundary: "Report declared-scope evidence only with recovery guidance; do not repair artifacts, mutate metadata, rewrite task or milestone scope, generate planning artifacts, create tasks, capture backlog entries, capture emergent specs, write checkpoints, apply recovery copies, discover changed files from Git, stage files, revert files, inspect remotes, or perform impact-analysis writes.",
        userDirectedFollowUpCommandFamilies: [
            "planning commands",
            "emergent-intake planning",
            "metadata update commands",
            "validation checkpoint",
            "task execution"
        ]
    },
    "diff-check": {
        category: "diff-check",
        commandFamily: "diff check",
        reusesSharedFindingEnvelope: true,
        preservesCommandEvidence: true,
        readOnly: true,
        requiredSharedFields: ["id", "code", "level", "severity", "ruleFamily", "artifactPath", "affectedPaths", "artifacts", "evidenceSource", "reason", "noWriteBoundary", "recommendedCommand", "recovery", "evidence"],
        deferredDetailsSchema: "downstream-diff-check-command",
        noWriteBoundary: "Report explicit changed-path evidence only with recovery guidance; do not repair artifacts, mutate metadata, rewrite task or milestone scope, generate planning artifacts, create tasks, capture backlog entries, capture emergent specs, write checkpoints, apply recovery copies, discover changed files from Git status, staged files, commits, diffs, or remotes, stage files, revert files, classify by mutation, or perform impact-analysis writes.",
        userDirectedFollowUpCommandFamilies: [
            "planning commands",
            "emergent-intake planning",
            "metadata update commands",
            "validation checkpoint",
            "task execution"
        ]
    },
    "impact-analysis": {
        category: "impact-analysis",
        commandFamily: "impact analysis",
        reusesSharedFindingEnvelope: true,
        preservesCommandEvidence: true,
        readOnly: true,
        requiredSharedFields: ["id", "code", "level", "severity", "ruleFamily", "artifactPath", "affectedPaths", "artifacts", "evidenceSource", "reason", "noWriteBoundary", "recommendedCommand", "recovery", "evidence"],
        deferredDetailsSchema: "downstream-impact-analysis-command",
        noWriteBoundary: "Report impact-analysis findings and input evidence only; blocking findings require no-write recovery and do not authorize partial writes, guessed repairs, metadata mutation, artifact generation, planning artifact generation, backlog capture, task creation, scope expansion, status updates, trace synchronization, checkpoint writes, staging, reverting, Git changed-file discovery, Git remote inspection, remote fetches, provider availability checks, package registry lookup, or scope-guard approval decisions."
    }
};
exports.VALIDATION_FINDING_JSON_OUTPUT_CONTRACT = {
    identityFields: ["id", "code"],
    messageFields: ["summary", "message", "detail"],
    levelFields: ["level", "severity"],
    pathFields: ["path", "affectedPaths"],
    artifactFields: ["artifacts"],
    recoveryField: "recovery",
    commandEvidence: {
        field: "evidence",
        categoryField: "category",
        detailsField: "details",
        detailsSchema: "command-defined-json-serializable"
    },
    readOnlyBoundary: "Findings are evidence and recovery guidance only; validation and impact JSON output does not authorize repair, synchronization, metadata mutation, manifest mutation, status transition, or artifact generation."
};
function createCompactValidationFindingSummary(findings) {
    const codeCounts = new Map();
    const blockingFindings = [];
    const severityCounts = {
        informational: 0,
        warning: 0,
        error: 0
    };
    for (const finding of findings) {
        severityCounts[finding.severity] += 1;
        codeCounts.set(finding.code, (codeCounts.get(finding.code) ?? 0) + 1);
        if (finding.severity !== "error")
            continue;
        blockingFindings.push({
            ...(finding.id === undefined ? {} : { id: finding.id }),
            code: finding.code,
            severity: "error",
            message: finding.message,
            ...(finding.artifactPath === undefined ? {} : { artifactPath: finding.artifactPath }),
            ...(finding.noWriteBoundary === undefined
                ? {}
                : { noWriteBoundary: finding.noWriteBoundary }),
            ...(finding.recommendedCommand === undefined
                ? {}
                : { recommendedCommand: finding.recommendedCommand }),
            ...(finding.affectedPaths === undefined
                ? {}
                : { affectedPaths: finding.affectedPaths.map((item) => cloneJsonRecord(item)) }),
            ...(finding.recovery === undefined
                ? {}
                : { recovery: typeof finding.recovery === "string" ? [finding.recovery] : [...finding.recovery] })
        });
    }
    return {
        totalCount: findings.length,
        blockingCount: severityCounts.error,
        advisoryCount: severityCounts.informational + severityCounts.warning,
        severityCounts,
        codeCounts: Object.fromEntries([...codeCounts].sort(([left], [right]) => left.localeCompare(right))),
        blockingFindings
    };
}
const VALIDATION_SHARED_FINDING_OUTPUT_FIELDS = [
    "id",
    "code",
    "level",
    "severity",
    "summary",
    "message",
    "detail",
    "ruleFamily",
    "artifactPath",
    "evidenceSource",
    "reason",
    "noWriteBoundary",
    "recommendedCommand",
    "affectedPaths",
    "artifacts",
    "recovery",
    "evidence"
];
const VALIDATION_ROLLUP_OUTPUT_LANES = [
    {
        id: "repository-root",
        textLabel: "Repository root",
        jsonField: "repositoryRoot",
        required: true,
        evidenceModel: "command-summary",
        notes: "Identifies the local repository root used for validation."
    },
    {
        id: "scope",
        textLabel: "Scope",
        jsonField: "scope",
        required: true,
        evidenceModel: "command-summary",
        notes: "Reports the supported local validation scope."
    },
    {
        id: "evidence-sources",
        textLabel: "Evidence sources",
        jsonField: "evidenceSourceInventory",
        required: true,
        evidenceModel: "source-lane",
        notes: "Preserves source inventory lanes instead of flattening producer evidence."
    },
    {
        id: "checks",
        textLabel: "Check results",
        jsonField: "checks",
        required: true,
        evidenceModel: "source-lane",
        notes: "Preserves individual check ids, status, and messages."
    },
    {
        id: "summary-counts",
        textLabel: "Summary counts",
        jsonField: "summary",
        required: true,
        evidenceModel: "command-summary",
        notes: "Reports derived totals without replacing source lanes."
    },
    {
        id: "readiness-status",
        textLabel: "Readiness status",
        jsonField: "readinessStatus",
        required: true,
        evidenceModel: "command-summary",
        notes: "Reports derived readiness while preserving underlying findings."
    },
    {
        id: "baseline-evidence",
        textLabel: "Forward-only baseline",
        jsonField: "baseline",
        required: true,
        evidenceModel: "source-lane",
        notes: "Reports existing baseline metadata and suppression counts without writing checkpoints."
    },
    {
        id: "inventory-findings",
        textLabel: "Inventory findings",
        jsonField: "inventoryFindings",
        required: true,
        evidenceModel: "source-lane",
        notes: "Preserves planning inventory findings as their own lane."
    },
    {
        id: "gaps",
        textLabel: "Planning gaps",
        jsonField: "gaps",
        required: true,
        evidenceModel: "source-lane",
        notes: "Preserves planning gap evidence with gap ids and paths."
    },
    {
        id: "next-actions",
        textLabel: "Safe next actions",
        jsonField: "nextActions",
        required: true,
        evidenceModel: "recovery-guidance",
        notes: "Reports advisory next steps without authorizing writes."
    },
    {
        id: "shared-findings",
        textLabel: "Structured findings",
        jsonField: "findings",
        required: true,
        evidenceModel: "shared-validation-finding",
        notes: "Uses the shared ValidationFinding envelope and preserves validation-rollup command evidence."
    }
];
const LINKS_CHECK_OUTPUT_LANES = [
    {
        id: "discovery",
        textLabel: "Discovery",
        jsonField: "discovery",
        required: true,
        evidenceModel: "source-lane",
        notes: "Preserves discovered artifacts, parse evidence, trace fields, and unsupported local artifact evidence."
    },
    {
        id: "resolution",
        textLabel: "Resolution",
        jsonField: "resolution",
        required: true,
        evidenceModel: "source-lane",
        notes: "Preserves local trace value resolution, hierarchy comparison, and paired-artifact check lanes."
    },
    {
        id: "relationship-evidence-lane",
        textLabel: "Relationship evidence",
        jsonField: "resolution.hierarchyChecks",
        required: true,
        evidenceModel: "source-lane",
        notes: "Preserves explicit parent-link and paired-artifact relationship evidence before finding projection."
    },
    {
        id: "summary",
        textLabel: "Summary",
        jsonField: "summary",
        required: true,
        evidenceModel: "command-summary",
        notes: "Reports command-level counts without flattening source evidence or shared findings."
    },
    {
        id: "checked-artifact-count",
        textLabel: "Checked artifacts",
        jsonField: "checkedArtifactCount",
        required: true,
        evidenceModel: "command-summary",
        notes: "Reports how many local artifacts were evaluated by the links-check surface."
    },
    {
        id: "checked-relationship-count",
        textLabel: "Checked relationships",
        jsonField: "checkedRelationshipCount",
        required: true,
        evidenceModel: "command-summary",
        notes: "Reports how many explicit trace-link relationships were evaluated."
    },
    {
        id: "trace-link-finding-count",
        textLabel: "Trace-link findings",
        jsonField: "traceLinkFindingCount",
        required: true,
        evidenceModel: "command-summary",
        notes: "Reports the number of shared findings emitted by trace-link validation."
    },
    {
        id: "affected-paths",
        textLabel: "Affected paths",
        jsonField: "findings[].affectedPaths",
        required: true,
        evidenceModel: "shared-validation-finding",
        notes: "Preserves affected path roles such as trace-link and finding-location."
    },
    {
        id: "artifact-references",
        textLabel: "Artifacts",
        jsonField: "findings[].artifacts",
        required: true,
        evidenceModel: "shared-validation-finding",
        notes: "Preserves artifact family, type, path, and source references where available."
    },
    {
        id: "relationship-evidence",
        textLabel: "Relationship evidence",
        jsonField: "findings[].evidence.details.relationship",
        required: true,
        evidenceModel: "shared-validation-finding",
        notes: "Preserves command-specific relationship evidence inside the shared finding envelope."
    },
    {
        id: "recovery-guidance",
        textLabel: "Recovery",
        jsonField: "findings[].recovery",
        required: true,
        evidenceModel: "recovery-guidance",
        notes: "Reports local-only recovery guidance without authorizing automatic repair or metadata synchronization."
    }
];
const SCOPE_FINDING_OUTPUT_LANES = [
    {
        id: "classification",
        textLabel: "Classification",
        jsonField: "findings[].evidence.details.category",
        required: true,
        evidenceModel: "shared-validation-finding",
        notes: "Preserves the scope guard or diff-check classification category inside the shared finding envelope."
    },
    {
        id: "identity",
        textLabel: "Finding id",
        jsonField: "findings[].id",
        required: true,
        evidenceModel: "shared-validation-finding",
        notes: "Preserves stable finding ids and codes for automation consumers."
    },
    {
        id: "level-and-severity",
        textLabel: "Level and severity",
        jsonField: "findings[].level",
        required: true,
        evidenceModel: "shared-validation-finding",
        notes: "Preserves shared informational, warning, and error severity semantics without command-local status fields."
    },
    {
        id: "affected-paths",
        textLabel: "Affected paths",
        jsonField: "findings[].affectedPaths",
        required: true,
        evidenceModel: "shared-validation-finding",
        notes: "Preserves requested, changed, and declared target path evidence using shared affected-path roles."
    },
    {
        id: "artifacts",
        textLabel: "Artifacts",
        jsonField: "findings[].artifacts",
        required: true,
        evidenceModel: "shared-validation-finding",
        notes: "Preserves artifact references when declared scope evidence points to planning artifacts."
    },
    {
        id: "recovery-guidance",
        textLabel: "Recovery",
        jsonField: "findings[].recovery",
        required: true,
        evidenceModel: "recovery-guidance",
        notes: "Reports recovery guidance without authorizing automatic writes."
    },
    {
        id: "next-action",
        textLabel: "Next action",
        jsonField: "findings[].evidence.details.nextAction",
        required: true,
        evidenceModel: "recovery-guidance",
        notes: "Reports the stable next-action category from the scope recovery guidance vocabulary."
    },
    {
        id: "blocking-state",
        textLabel: "Blocking",
        jsonField: "findings[].evidence.details.blocking",
        required: true,
        evidenceModel: "shared-validation-finding",
        notes: "Reports whether the classification blocks write-capable automation."
    },
    {
        id: "no-write-state",
        textLabel: "No-write required",
        jsonField: "findings[].evidence.details.noWriteRequired",
        required: true,
        evidenceModel: "shared-validation-finding",
        notes: "Reports whether the finding requires a no-write stop before follow-up automation."
    }
];
exports.VALIDATION_COMMAND_OUTPUT_CONTRACTS = {
    rollup: {
        surface: "rollup",
        commandEvidenceCategory: "validation-rollup",
        textOutput: {
            rendering: "finding-oriented",
            lanes: VALIDATION_ROLLUP_OUTPUT_LANES
        },
        jsonOutput: {
            rendering: "machine-readable",
            lanes: VALIDATION_ROLLUP_OUTPUT_LANES
        },
        sharedFindingFields: VALIDATION_SHARED_FINDING_OUTPUT_FIELDS,
        textAndJsonShareEvidenceModel: true,
        readOnlyRecoveryBoundary: "Rollup output is evidence and guidance only; it does not repair links, synchronize metadata, write checkpoints, mutate status, or generate planning artifacts."
    },
    "links-check": {
        surface: "links-check",
        commandEvidenceCategory: "links-check",
        textOutput: {
            rendering: "finding-oriented",
            lanes: LINKS_CHECK_OUTPUT_LANES
        },
        jsonOutput: {
            rendering: "machine-readable",
            lanes: LINKS_CHECK_OUTPUT_LANES
        },
        sharedFindingFields: VALIDATION_SHARED_FINDING_OUTPUT_FIELDS,
        textAndJsonShareEvidenceModel: true,
        readOnlyRecoveryBoundary: "Links-check output is evidence and local-only recovery guidance; it does not repair links, rewrite parent references, synchronize metadata, write checkpoints, mutate status, or generate planning artifacts."
    },
    "scope-guard": {
        surface: "scope-guard",
        commandEvidenceCategory: "scope-guard",
        textOutput: {
            rendering: "finding-oriented",
            lanes: SCOPE_FINDING_OUTPUT_LANES
        },
        jsonOutput: {
            rendering: "machine-readable",
            lanes: SCOPE_FINDING_OUTPUT_LANES
        },
        sharedFindingFields: VALIDATION_SHARED_FINDING_OUTPUT_FIELDS,
        textAndJsonShareEvidenceModel: true,
        readOnlyRecoveryBoundary: "Scope-guard output is evidence and recovery guidance only; it does not repair artifacts, mutate metadata, rewrite scope, discover changed files from Git, write checkpoints, create tasks, or apply recovery copies."
    },
    "diff-check": {
        surface: "diff-check",
        commandEvidenceCategory: "diff-check",
        textOutput: {
            rendering: "finding-oriented",
            lanes: SCOPE_FINDING_OUTPUT_LANES
        },
        jsonOutput: {
            rendering: "machine-readable",
            lanes: SCOPE_FINDING_OUTPUT_LANES
        },
        sharedFindingFields: VALIDATION_SHARED_FINDING_OUTPUT_FIELDS,
        textAndJsonShareEvidenceModel: true,
        readOnlyRecoveryBoundary: "Diff-check output is explicit changed-path evidence and recovery guidance only; it does not infer changed paths from Git, repair artifacts, mutate metadata, rewrite scope, write checkpoints, create tasks, or apply recovery copies."
    }
};
function getValidationCommandOutputContracts() {
    return {
        rollup: cloneValidationCommandOutputContract(exports.VALIDATION_COMMAND_OUTPUT_CONTRACTS.rollup),
        "links-check": cloneValidationCommandOutputContract(exports.VALIDATION_COMMAND_OUTPUT_CONTRACTS["links-check"]),
        "scope-guard": cloneValidationCommandOutputContract(exports.VALIDATION_COMMAND_OUTPUT_CONTRACTS["scope-guard"]),
        "diff-check": cloneValidationCommandOutputContract(exports.VALIDATION_COMMAND_OUTPUT_CONTRACTS["diff-check"])
    };
}
function cloneValidationCommandOutputContract(contract) {
    return {
        ...contract,
        textOutput: {
            ...contract.textOutput,
            lanes: contract.textOutput.lanes.map((lane) => ({ ...lane }))
        },
        jsonOutput: {
            ...contract.jsonOutput,
            lanes: contract.jsonOutput.lanes.map((lane) => ({ ...lane }))
        },
        sharedFindingFields: [...contract.sharedFindingFields]
    };
}
function cloneJsonRecord(value) {
    if (Array.isArray(value))
        return value.map((item) => cloneJsonRecord(item));
    if (typeof value === "object" && value !== null) {
        return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, cloneJsonRecord(item)]));
    }
    return value;
}
