"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.IMPACT_FAILURE_AND_BLOCKING_POLICY = exports.IMPACT_CERTAINTY_POLICY = exports.IMPACT_RECOVERY_GUIDANCE = exports.IMPACT_FAILURE_POLICY_PROHIBITED_RECOVERY_ACTIONS = exports.IMPACT_FAILURE_POLICY_ADVISORY_CONDITIONS = exports.IMPACT_FAILURE_POLICY_BLOCKING_CONDITIONS = exports.IMPACT_CERTAINTY_LABELS = exports.IMPACT_TYPES = exports.IMPACT_FINDING_IDS = void 0;
exports.getImpactFindingContract = getImpactFindingContract;
exports.getImpactFailureAndBlockingPolicy = getImpactFailureAndBlockingPolicy;
exports.getImpactNonOverlapContract = getImpactNonOverlapContract;
exports.buildImpactFindingsFromEvidence = buildImpactFindingsFromEvidence;
exports.createImpactFinding = createImpactFinding;
exports.getImpactCertaintyPolicy = getImpactCertaintyPolicy;
exports.getImpactRecoveryGuidance = getImpactRecoveryGuidance;
const output_1 = require("../output");
const paths_1 = require("../paths");
exports.IMPACT_FINDING_IDS = [
    "IMPACT_DIRECT",
    "IMPACT_INFERRED",
    "IMPACT_WEAK_EVIDENCE",
    "IMPACT_MISSING_EVIDENCE",
    "IMPACT_STALE_EVIDENCE",
    "IMPACT_UNREADABLE_EVIDENCE",
    "IMPACT_MALFORMED_EVIDENCE",
    "IMPACT_CONTRADICTORY_EVIDENCE",
    "IMPACT_POSSIBLE_DISCOVERED_WORK",
    "IMPACT_UNRESOLVED_QUESTION"
];
exports.IMPACT_TYPES = [
    "direct-impact",
    "inferred-impact",
    "weak-evidence",
    "missing-evidence",
    "stale-evidence",
    "unreadable-evidence",
    "malformed-evidence",
    "contradictory-evidence",
    "possible-discovered-work",
    "unresolved-impact-question"
];
exports.IMPACT_CERTAINTY_LABELS = [
    "direct-evidence",
    "inferred-evidence",
    "weak-evidence",
    "missing-evidence",
    "stale-evidence",
    "unreadable-evidence",
    "malformed-evidence",
    "placeholder-evidence",
    "contradictory-evidence",
    "possible-discovered-work",
    "unresolved-impact-question"
];
exports.IMPACT_FAILURE_POLICY_BLOCKING_CONDITIONS = [
    "invalid-command-input",
    "malformed-changed-path-input",
    "malformed-required-local-artifact-evidence",
    "unreadable-required-local-artifact",
    "broken-traceability",
    "contradictory-deterministic-evidence",
    "unsafe-scope-mismatch"
];
exports.IMPACT_FAILURE_POLICY_ADVISORY_CONDITIONS = [
    "weak-evidence",
    "missing-optional-evidence",
    "stale-readable-evidence",
    "placeholder-evidence",
    "inferred-impact",
    "possible-discovered-work",
    "backlog-candidate",
    "unresolved-impact-question"
];
exports.IMPACT_FAILURE_POLICY_PROHIBITED_RECOVERY_ACTIONS = [
    "partial writes",
    "guessed repairs",
    "metadata mutation",
    "artifact generation",
    "planning artifact generation",
    "backlog capture",
    "task creation",
    "scope expansion"
];
exports.IMPACT_RECOVERY_GUIDANCE = {
    "direct-evidence": {
        nextAction: "none",
        recovery: "No recovery is required. Keep the cited impact evidence available for downstream review.",
        noWriteRequired: false
    },
    "inferred-evidence": {
        nextAction: "review-scope",
        recovery: "Review the deterministic local relationship before downstream handoff; inferred evidence does not prove complete impact coverage.",
        noWriteRequired: false
    },
    "weak-evidence": {
        nextAction: "review-scope",
        recovery: "Inspect the cited artifact evidence and add deterministic validation, target-path, changed-path, or trace evidence through a separate authorized workflow before relying on impact output.",
        noWriteRequired: false
    },
    "missing-evidence": {
        nextAction: "revise-planning",
        recovery: "Inspect the cited artifact, traceability field, or validation section and provide deterministic evidence before downstream handoff.",
        noWriteRequired: false
    },
    "stale-evidence": {
        nextAction: "revise-planning",
        recovery: "Reconcile stale planning or implementation evidence with the source spec, index metadata, upstream links, or paired task artifacts before downstream handoff.",
        noWriteRequired: false
    },
    "unreadable-evidence": {
        nextAction: "stop-unsafe-automation",
        recovery: "Restore or correct the cited local artifact path through a separate authorized operation, then rerun impact analysis from the original input.",
        noWriteRequired: true
    },
    "malformed-evidence": {
        nextAction: "correct-input",
        recovery: "Correct the malformed local artifact value or caller-provided path input, then rerun impact analysis without relying on guessed partial evidence.",
        noWriteRequired: true
    },
    "placeholder-evidence": {
        nextAction: "revise-planning",
        recovery: "Replace placeholder closeout, changed-path, validation, or trace evidence through a separate authorized workflow before relying on impact output.",
        noWriteRequired: false
    },
    "contradictory-evidence": {
        nextAction: "stop-unsafe-automation",
        recovery: "Resolve contradictory planning coordinates, artifact family evidence, or trace links before downstream handoff.",
        noWriteRequired: true
    },
    "possible-discovered-work": {
        nextAction: "capture-discovered-work",
        recovery: "Review the declared scope and changed-path evidence; if the item is confirmed as emergent work, capture it through explicit planning before treating it as current scope.",
        noWriteRequired: false
    },
    "unresolved-impact-question": {
        nextAction: "review-scope",
        recovery: "Review the impact question and record a decision, follow-up, or non-issue note before downstream handoff.",
        noWriteRequired: false
    }
};
const IMPACT_ADVISORY_RECOVERY_GUIDANCE = {
    "backlog-candidate": {
        nextAction: "route-backlog",
        recovery: "Route the cited item through planning or backlog triage before treating it as current implementation scope.",
        noWriteRequired: false
    }
};
exports.IMPACT_CERTAINTY_POLICY = {
    "direct-evidence": {
        certainty: "direct-evidence",
        impactType: "direct-impact",
        sourceEvidenceRule: "Exact structured artifact, trace-link, declared target path, implementation-summary changed path, valid upstream reference, or exact changed-path association evidence.",
        defaultLevel: "informational",
        noWriteRequired: false
    },
    "inferred-evidence": {
        certainty: "inferred-evidence",
        impactType: "inferred-impact",
        sourceEvidenceRule: "Deterministic local relationship evidence, including path-prefix changed-path associations and planning parent-child hierarchy links.",
        defaultLevel: "informational",
        noWriteRequired: false
    },
    "weak-evidence": {
        certainty: "weak-evidence",
        impactType: "weak-evidence",
        sourceEvidenceRule: "Contextual or explicit prose evidence without structured path, changed-path, trace-link, or declared child evidence.",
        defaultLevel: "warning",
        advisoryReason: "weak-evidence",
        noWriteRequired: false
    },
    "missing-evidence": {
        certainty: "missing-evidence",
        impactType: "missing-evidence",
        sourceEvidenceRule: "Required local artifact metadata, spec reference, body section, or trace evidence is absent from the bounded input artifacts.",
        defaultLevel: "warning",
        advisoryReason: "weak-evidence",
        noWriteRequired: false
    },
    "stale-evidence": {
        certainty: "stale-evidence",
        impactType: "stale-evidence",
        sourceEvidenceRule: "Implementation or planning evidence is present but incomplete, pending, draft, or stale for downstream impact review.",
        defaultLevel: "warning",
        advisoryReason: "weak-evidence",
        noWriteRequired: false
    },
    "unreadable-evidence": {
        certainty: "unreadable-evidence",
        impactType: "unreadable-evidence",
        sourceEvidenceRule: "A bounded local artifact input could not be read.",
        defaultLevel: "error",
        blockingReason: "broken-traceability",
        noWriteRequired: true
    },
    "malformed-evidence": {
        certainty: "malformed-evidence",
        impactType: "malformed-evidence",
        sourceEvidenceRule: "A bounded input value is malformed or invalid command input, including invalid caller-provided changed paths.",
        defaultLevel: "error",
        blockingReason: "invalid-command-input",
        noWriteRequired: true
    },
    "placeholder-evidence": {
        certainty: "placeholder-evidence",
        impactType: "weak-evidence",
        sourceEvidenceRule: "A required local evidence section contains placeholder text rather than final implementation or validation evidence.",
        defaultLevel: "warning",
        advisoryReason: "weak-evidence",
        noWriteRequired: false
    },
    "contradictory-evidence": {
        certainty: "contradictory-evidence",
        impactType: "contradictory-evidence",
        sourceEvidenceRule: "Bounded local evidence contradicts another deterministic signal, such as artifact type versus artifact path family.",
        defaultLevel: "error",
        blockingReason: "broken-traceability",
        noWriteRequired: true
    },
    "possible-discovered-work": {
        certainty: "possible-discovered-work",
        impactType: "possible-discovered-work",
        sourceEvidenceRule: "Impact output points to a candidate scope item or planning surface that needs human triage before it becomes current work.",
        defaultLevel: "warning",
        advisoryReason: "possible-discovered-work",
        noWriteRequired: false
    },
    "unresolved-impact-question": {
        certainty: "unresolved-impact-question",
        impactType: "unresolved-impact-question",
        sourceEvidenceRule: "Impact output raises a review question that cannot be resolved from deterministic local evidence alone.",
        defaultLevel: "warning",
        advisoryReason: "impact-question",
        noWriteRequired: false
    }
};
exports.IMPACT_FAILURE_AND_BLOCKING_POLICY = {
    blockingConditions: [...exports.IMPACT_FAILURE_POLICY_BLOCKING_CONDITIONS],
    advisoryConditions: [...exports.IMPACT_FAILURE_POLICY_ADVISORY_CONDITIONS],
    blockingReasonByCondition: {
        "invalid-command-input": "invalid-command-input",
        "malformed-changed-path-input": "invalid-command-input",
        "malformed-required-local-artifact-evidence": "invalid-command-input",
        "unreadable-required-local-artifact": "broken-traceability",
        "broken-traceability": "broken-traceability",
        "contradictory-deterministic-evidence": "broken-traceability",
        "unsafe-scope-mismatch": "unsafe-scope-mismatch"
    },
    advisoryReasonByCondition: {
        "weak-evidence": "weak-evidence",
        "missing-optional-evidence": "weak-evidence",
        "stale-readable-evidence": "weak-evidence",
        "placeholder-evidence": "weak-evidence",
        "inferred-impact": "none",
        "possible-discovered-work": "possible-discovered-work",
        "backlog-candidate": "backlog-candidate",
        "unresolved-impact-question": "impact-question"
    },
    noWriteBlockingRule: "Blocking impact findings stop automation and require no-write recovery through the shared validation blocking reason vocabulary; they do not authorize partial writes, guessed repairs, metadata mutation, artifact generation, backlog capture, task creation, or scope expansion.",
    advisoryBoundaryRule: "Weak, optional, stale-readable, placeholder, inferred, possible discovered work, backlog candidate, and unresolved impact question evidence remains advisory unless paired with an existing blocking reason from invalid input, broken traceability, contradictory deterministic evidence, unreadable required artifacts, or unsafe scope mismatch.",
    prohibitedRecoveryActions: [...exports.IMPACT_FAILURE_POLICY_PROHIBITED_RECOVERY_ACTIONS],
    sharedEnvelope: "ValidationFinding",
    evidenceCategory: "impact-analysis",
    readOnly: true
};
function getImpactFindingContract() {
    return {
        findingIds: [...exports.IMPACT_FINDING_IDS],
        impactTypes: [...exports.IMPACT_TYPES],
        certaintyValues: [...exports.IMPACT_CERTAINTY_LABELS],
        sharedEnvelope: "ValidationFinding",
        evidenceCategory: "impact-analysis",
        planningSurfaces: [
            "source-spec",
            "slice",
            "phase",
            "milestone",
            "task",
            "implementation-summary",
            "reconciliation",
            "spec-index",
            "unsupported"
        ],
        nonOverlap: getImpactNonOverlapContract(),
        failurePolicy: getImpactFailureAndBlockingPolicy(),
        readOnly: true,
        noSeparateOutputModel: true
    };
}
function getImpactFailureAndBlockingPolicy() {
    return {
        ...exports.IMPACT_FAILURE_AND_BLOCKING_POLICY,
        blockingConditions: [...exports.IMPACT_FAILURE_AND_BLOCKING_POLICY.blockingConditions],
        advisoryConditions: [...exports.IMPACT_FAILURE_AND_BLOCKING_POLICY.advisoryConditions],
        blockingReasonByCondition: {
            ...exports.IMPACT_FAILURE_AND_BLOCKING_POLICY.blockingReasonByCondition
        },
        advisoryReasonByCondition: {
            ...exports.IMPACT_FAILURE_AND_BLOCKING_POLICY.advisoryReasonByCondition
        },
        prohibitedRecoveryActions: [...exports.IMPACT_FAILURE_AND_BLOCKING_POLICY.prohibitedRecoveryActions]
    };
}
function getImpactNonOverlapContract() {
    return {
        consumedEvidenceCategories: [
            "scope-guard",
            "diff-check",
            "links-check",
            "validation-rollup"
        ],
        consumedEvidenceRule: "Impact analysis may summarize bounded local evidence from other validation surfaces only as consumed context; it must preserve the original command evidence category, finding code, blocking or advisory state, and owning command recovery path instead of normalizing those findings into impact-analysis alone.",
        independentReportableCategories: [
            "scope-guard",
            "diff-check",
            "links-check",
            "validation-rollup"
        ],
        excludedResponsibilities: [
            "declared-scope approval decisions",
            "changed-path diff classification ownership",
            "trace-link repair",
            "hierarchy repair",
            "validation checkpoint writing",
            "repository-wide rollup replacement"
        ],
        recoveryRouting: {
            "scope-guard": "Use validation scope-guard or revise planning when the next action concerns requested paths versus declared target-path evidence.",
            "diff-check": "Use validation diff-check when the next action concerns changed-path classification against declared scope.",
            "links-check": "Use validation links-check or explicit metadata/planning repair when the next action concerns broken traceability or hierarchy links.",
            "validation-rollup": "Use validation rollup or smoke/checkpoint surfaces when the next action concerns repository-wide validation aggregation or forward-only baselines.",
            "metadata-update": "Use metadata update commands for authorized metadata mutation; impact analysis remains read-only.",
            planning: "Use planning workflows for new scope, discovered work, backlog routing, or hierarchy decisions.",
            "task-execution": "Use task execution workflows for implementation or paired artifact closeout."
        },
        readOnly: true
    };
}
function buildImpactFindingsFromEvidence(input) {
    return {
        findings: input.evidenceResult.evidence.map((evidence) => createImpactFinding({
            id: findingIdForQuality(evidence.quality),
            impactType: impactTypeForCertainty(certaintyForQuality(evidence.quality)),
            certainty: certaintyForQuality(evidence.quality),
            traceContext: evidence.traceContext,
            sourceEvidenceKind: evidence.evidenceKind,
            sourceEvidenceQuality: evidence.quality,
            sourceEvidenceState: evidence.sourceState,
            sourceReason: evidence.reason,
            detail: evidence.reason,
            ...(evidence.findingBoundary === "blocking"
                ? {
                    blockingReason: blockingReasonForQuality(evidence.quality)
                }
                : advisoryReasonForQuality(evidence.quality) === undefined
                    ? {
                        level: "informational"
                    }
                    : {
                        advisoryReason: advisoryReasonForQuality(evidence.quality)
                    })
        })),
        readOnly: true
    };
}
function createImpactFinding(input) {
    const taxonomy = taxonomyForImpactType(input.impactType);
    const certaintyPolicy = getImpactCertaintyPolicy(input.certainty);
    const blockingReason = input.blockingReason ?? certaintyPolicy.blockingReason;
    const recoveryGuidance = applyBlockingRecoveryOverride(getImpactRecoveryGuidance(input.certainty, input.traceContext, input.advisoryReason), blockingReason);
    const id = input.id ?? taxonomy.id;
    const artifact = createImpactArtifactReference(input.traceContext);
    const affectedPaths = createImpactAffectedPaths(input.traceContext, artifact);
    const evidenceDetails = {
        impactType: input.impactType,
        certainty: input.certainty,
        certaintySourceRule: certaintyPolicy.sourceEvidenceRule,
        nextAction: recoveryGuidance.nextAction,
        noWriteRequired: recoveryGuidance.noWriteRequired,
        affectedPlanningSurfaces: [createAffectedPlanningSurface(input.traceContext)],
        traceContext: input.traceContext,
        sourceEvidenceKind: input.sourceEvidenceKind,
        sourceEvidenceQuality: input.sourceEvidenceQuality,
        sourceEvidenceState: input.sourceEvidenceState,
        sourceReason: input.sourceReason
    };
    const evidence = (0, output_1.createValidationCommandEvidence)({
        category: "impact-analysis",
        details: evidenceDetails
    });
    const common = {
        id,
        summary: input.summary ?? taxonomy.summary,
        detail: input.detail,
        artifacts: [artifact],
        affectedPaths,
        recovery: input.recovery ?? recoveryGuidance.recovery,
        evidence
    };
    if (blockingReason !== undefined) {
        return (0, output_1.createBlockingValidationFinding)({
            ...common,
            blockingReason
        });
    }
    const advisoryReason = input.advisoryReason ?? certaintyPolicy.advisoryReason;
    if (advisoryReason !== undefined) {
        return (0, output_1.createAdvisoryValidationFinding)({
            ...common,
            level: input.level === "informational" ? "informational" : undefined,
            advisoryReason
        });
    }
    return (0, output_1.createValidationFinding)({
        ...common,
        level: input.level ?? taxonomy.defaultLevel
    });
}
function applyBlockingRecoveryOverride(guidance, blockingReason) {
    if (blockingReason === undefined || guidance.noWriteRequired) {
        return guidance;
    }
    return {
        ...guidance,
        nextAction: blockingReason === "invalid-command-input"
            ? "correct-input"
            : "stop-unsafe-automation",
        noWriteRequired: true
    };
}
function getImpactCertaintyPolicy(certainty) {
    return { ...exports.IMPACT_CERTAINTY_POLICY[certainty] };
}
function getImpactRecoveryGuidance(certainty, traceContext, advisoryReason) {
    const guidance = advisoryReason === undefined
        ? undefined
        : IMPACT_ADVISORY_RECOVERY_GUIDANCE[advisoryReason];
    const baseGuidance = guidance ?? exports.IMPACT_RECOVERY_GUIDANCE[certainty];
    const location = traceContext === undefined ? "" : ` Inspect ${describeImpactRecoveryLocation(traceContext)}.`;
    return {
        nextAction: baseGuidance.nextAction,
        recovery: `${baseGuidance.recovery}${location}`,
        noWriteRequired: baseGuidance.noWriteRequired
    };
}
function taxonomyForImpactType(impactType) {
    switch (impactType) {
        case "direct-impact":
            return {
                id: "IMPACT_DIRECT",
                summary: "Direct impact evidence is present.",
                defaultLevel: "informational",
                recovery: ["Keep direct impact evidence available for downstream review."]
            };
        case "inferred-impact":
            return {
                id: "IMPACT_INFERRED",
                summary: "Inferred impact evidence is present.",
                defaultLevel: "informational",
                recovery: ["Review the deterministic local relationship before downstream handoff."]
            };
        case "weak-evidence":
            return {
                id: "IMPACT_WEAK_EVIDENCE",
                summary: "Impact evidence is weak.",
                defaultLevel: "warning",
                recovery: ["Review the cited evidence and add structured trace evidence before relying on it."]
            };
        case "missing-evidence":
            return {
                id: "IMPACT_MISSING_EVIDENCE",
                summary: "Impact evidence is missing.",
                defaultLevel: "warning",
                recovery: ["Add the missing task, summary, trace, or validation evidence before downstream handoff."]
            };
        case "stale-evidence":
            return {
                id: "IMPACT_STALE_EVIDENCE",
                summary: "Impact evidence is stale.",
                defaultLevel: "warning",
                recovery: ["Refresh stale implementation or planning evidence before downstream handoff."]
            };
        case "unreadable-evidence":
            return {
                id: "IMPACT_UNREADABLE_EVIDENCE",
                summary: "Impact evidence could not be read.",
                defaultLevel: "error",
                recovery: ["Restore readability for the cited artifact, then rerun impact analysis."]
            };
        case "malformed-evidence":
            return {
                id: "IMPACT_MALFORMED_EVIDENCE",
                summary: "Impact evidence is malformed.",
                defaultLevel: "error",
                recovery: ["Correct the malformed local evidence value, then rerun impact analysis."]
            };
        case "contradictory-evidence":
            return {
                id: "IMPACT_CONTRADICTORY_EVIDENCE",
                summary: "Impact evidence is contradictory.",
                defaultLevel: "error",
                recovery: ["Resolve the contradictory artifact or trace evidence before downstream handoff."]
            };
        case "possible-discovered-work":
            return {
                id: "IMPACT_POSSIBLE_DISCOVERED_WORK",
                summary: "Impact evidence may represent discovered work.",
                defaultLevel: "warning",
                recovery: ["Confirm whether the item is in scope, then capture it through planning instead of guessed repair."]
            };
        case "unresolved-impact-question":
            return {
                id: "IMPACT_UNRESOLVED_QUESTION",
                summary: "Impact evidence raises an unresolved question.",
                defaultLevel: "warning",
                recovery: ["Record a review decision, follow-up, or non-issue note before downstream handoff."]
            };
    }
}
function findingIdForQuality(quality) {
    return taxonomyForImpactType(impactTypeForCertainty(certaintyForQuality(quality))).id;
}
function certaintyForQuality(quality) {
    switch (quality) {
        case "direct":
            return "direct-evidence";
        case "inferred":
            return "inferred-evidence";
        case "weak":
            return "weak-evidence";
        case "placeholder":
            return "placeholder-evidence";
        case "missing":
            return "missing-evidence";
        case "stale":
            return "stale-evidence";
        case "unreadable":
            return "unreadable-evidence";
        case "malformed":
            return "malformed-evidence";
        case "contradictory":
            return "contradictory-evidence";
    }
}
function impactTypeForCertainty(certainty) {
    return exports.IMPACT_CERTAINTY_POLICY[certainty].impactType;
}
function advisoryReasonForQuality(quality) {
    return exports.IMPACT_CERTAINTY_POLICY[certaintyForQuality(quality)].advisoryReason;
}
function blockingReasonForQuality(quality) {
    return exports.IMPACT_CERTAINTY_POLICY[certaintyForQuality(quality)].blockingReason ?? "broken-traceability";
}
function describeImpactRecoveryLocation(traceContext) {
    const parts = [`artifact '${traceContext.sourceArtifactPath}'`];
    if (traceContext.sourceField !== undefined) {
        parts.push(`field '${traceContext.sourceField}'`);
    }
    if (traceContext.sourceSection !== undefined) {
        parts.push(`section '## ${traceContext.sourceSection}'`);
    }
    if (traceContext.changedPath !== undefined) {
        parts.push(`changed path '${traceContext.changedPath}'`);
    }
    if (traceContext.affectedPath !== undefined && traceContext.affectedPath !== traceContext.changedPath) {
        parts.push(`affected path '${traceContext.affectedPath}'`);
    }
    if (traceContext.parentLink !== undefined) {
        parts.push(`parent trace link '${traceContext.parentLink}'`);
    }
    if (traceContext.upstreamSpecReference !== undefined) {
        parts.push(`upstream spec reference '${traceContext.upstreamSpecReference}'`);
    }
    return parts.join(", ");
}
function createImpactArtifactReference(traceContext) {
    return (0, output_1.createValidationArtifactReference)({
        family: artifactFamilyForImpact(traceContext.artifactFamily),
        artifactType: artifactTypeForImpact(traceContext.artifactFamily),
        path: safeRepositoryPath(traceContext.sourceArtifactPath, "impact-analysis/invalid-command-input"),
        source: traceContext.upstreamSpecReference === undefined
            ? undefined
            : {
                id: "impact-upstream-spec",
                ref: traceContext.upstreamSpecReference,
                sourceType: "markdown"
            }
    });
}
function createImpactAffectedPaths(traceContext, artifact) {
    const inputs = [
        {
            role: "finding-location",
            path: safeRepositoryPath(traceContext.sourceArtifactPath, "impact-analysis/invalid-command-input"),
            artifact: artifact,
            description: "Artifact containing the impact evidence."
        }
    ];
    if (traceContext.affectedPath !== undefined && isRepositoryRelativePath(traceContext.affectedPath)) {
        inputs.push({
            role: traceContext.affectedPath === traceContext.changedPath ? "changed-path" : "trace-link",
            path: traceContext.affectedPath,
            artifact: artifact,
            description: "Path affected by the impact evidence."
        });
    }
    if (traceContext.changedPath !== undefined
        && traceContext.changedPath !== traceContext.affectedPath
        && isRepositoryRelativePath(traceContext.changedPath)) {
        inputs.push({
            role: "changed-path",
            path: traceContext.changedPath,
            artifact: artifact,
            description: "Caller-provided changed path associated with the impact evidence."
        });
    }
    return inputs.map((input) => (0, output_1.createValidationAffectedPath)(input));
}
function safeRepositoryPath(pathValue, fallbackPath) {
    try {
        return (0, paths_1.parseRepositoryRelativePath)(pathValue).normalizedPath;
    }
    catch {
        return fallbackPath;
    }
}
function isRepositoryRelativePath(pathValue) {
    try {
        (0, paths_1.parseRepositoryRelativePath)(pathValue);
        return true;
    }
    catch {
        return false;
    }
}
function createAffectedPlanningSurface(traceContext) {
    return {
        surface: planningSurfaceForImpact(traceContext.artifactFamily),
        path: traceContext.sourceArtifactPath,
        sourceField: traceContext.sourceField,
        sourceSection: traceContext.sourceSection,
        planningCoordinates: traceContext.planningCoordinates
    };
}
function artifactFamilyForImpact(artifactFamily) {
    if (artifactFamily === "unsupported") {
        return "other";
    }
    return artifactFamily;
}
function artifactTypeForImpact(artifactFamily) {
    return artifactFamily === "unsupported" ? "unsupported" : artifactFamily;
}
function planningSurfaceForImpact(artifactFamily) {
    switch (artifactFamily) {
        case "specification":
            return "source-spec";
        case "slice_overview":
            return "slice";
        case "phase_overview":
            return "phase";
        case "milestone_overview":
            return "milestone";
        case "task_definition":
            return "task";
        case "implementation_summary":
            return "implementation-summary";
        case "reconciliation":
            return "reconciliation";
        case "unsupported":
            return "unsupported";
    }
}
