"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.baselinePlans = exports.TaskBaselineContractImpactError = exports.TaskBaselineRuntimeAcceptanceContractError = exports.TASK_CONTRACT_IMPACT_BASELINE_BLOCKED = exports.TASK_BASELINE_RUNTIME_ACCEPTANCE_CONTRACT_INVALID = exports.TASK_RUNTIME_BASELINE_PRECONDITION_BLOCKED = exports.MAX_BASELINE_PLAN_PREDECESSOR_CANDIDATES = exports.MAX_STATUS_PATHS = exports.MAX_STATUS_BYTES = exports.SUMMARY_BLOCK = exports.BASELINE_BLOCK = exports.MAX_HASHED_BYTES = exports.MAX_HASHED_FILES = exports.SOURCE_REVISION_PATTERN = exports.CONTENT_HASH_PATTERN = exports.IDENTITY_PATTERN = void 0;
exports.evaluateTaskRuntimeBaselinePreconditions = evaluateTaskRuntimeBaselinePreconditions;
exports.taskRuntimeBaselinePreconditionMessage = taskRuntimeBaselinePreconditionMessage;
const planning_document_utils_1 = require("../planning-document-utils");
const runtime_acceptance_1 = require("../runtime-acceptance");
exports.IDENTITY_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{2,127}$/;
exports.CONTENT_HASH_PATTERN = /^sha256:[0-9a-f]{64}$/;
exports.SOURCE_REVISION_PATTERN = /^[0-9a-f]{40,64}$/;
exports.MAX_HASHED_FILES = 5_000;
exports.MAX_HASHED_BYTES = 128_000_000;
exports.BASELINE_BLOCK = "runtime_attribution_baseline";
exports.SUMMARY_BLOCK = "runtime_attribution";
exports.MAX_STATUS_BYTES = 2_000_000;
exports.MAX_STATUS_PATHS = 16_384;
exports.MAX_BASELINE_PLAN_PREDECESSOR_CANDIDATES = 64;
exports.TASK_RUNTIME_BASELINE_PRECONDITION_BLOCKED = "TASK_RUNTIME_BASELINE_PRECONDITION_BLOCKED";
exports.TASK_BASELINE_RUNTIME_ACCEPTANCE_CONTRACT_INVALID = exports.TASK_RUNTIME_BASELINE_PRECONDITION_BLOCKED;
exports.TASK_CONTRACT_IMPACT_BASELINE_BLOCKED = "TASK_CONTRACT_IMPACT_BASELINE_BLOCKED";
function evaluateTaskRuntimeBaselinePreconditions(options) {
    if (options.evidenceProfile !== "runtime" && options.evidenceProfile !== "hybrid") {
        return {
            applicable: false,
            acceptance: null,
            issues: [],
            findings: []
        };
    }
    const artifactPath = (0, planning_document_utils_1.toRepositoryLikePath)(options.artifactPath);
    const issues = [];
    if ((0, planning_document_utils_1.readTopLevelField)(options.frontMatterLines, "runtime_attribution_required")?.trim() !== "true") {
        issues.push(`runtime_attribution_required must be explicitly set to true for evidence_profile '${options.evidenceProfile}'.`);
    }
    const acceptance = (0, runtime_acceptance_1.inspectRuntimeAcceptance)({
        artifactPath,
        artifactKind: "task",
        frontMatterLines: options.frontMatterLines,
        markdownText: options.markdownText,
        requireExplicitForExecutableEvidence: true
    });
    issues.push(...acceptance.issues);
    if (acceptance.metadata === null && acceptance.issues.length === 0) {
        issues.push("runtime_acceptance metadata must be explicitly declared for runtime or hybrid baseline eligibility.");
    }
    else if (acceptance.metadata !== null && !acceptance.metadata.required) {
        issues.push("runtime_acceptance.required must be true for runtime or hybrid baseline eligibility.");
    }
    const findings = issues.length === 0
        ? []
        : [{
                code: exports.TASK_RUNTIME_BASELINE_PRECONDITION_BLOCKED,
                message: taskRuntimeBaselinePreconditionMessage(artifactPath, issues),
                severity: "error",
                path: artifactPath,
                artifactPath,
                ruleFamily: "task-runtime-baseline-precondition",
                evidenceSource: "planning-contract",
                reason: "planning-contract",
                noWriteBoundary: "Readiness review and rejected baseline capture do not mutate the selected task pair."
            }];
    return {
        applicable: true,
        acceptance,
        issues,
        findings
    };
}
class TaskBaselineRuntimeAcceptanceContractError extends Error {
    code = exports.TASK_BASELINE_RUNTIME_ACCEPTANCE_CONTRACT_INVALID;
    classification = "planning-contract";
    taskDefinitionPath;
    issues;
    constructor(taskDefinitionPath, issues) {
        super(taskRuntimeBaselinePreconditionMessage(taskDefinitionPath, issues));
        this.name = "TaskBaselineRuntimeAcceptanceContractError";
        this.taskDefinitionPath = taskDefinitionPath;
        this.issues = [...issues];
    }
}
exports.TaskBaselineRuntimeAcceptanceContractError = TaskBaselineRuntimeAcceptanceContractError;
class TaskBaselineContractImpactError extends Error {
    code = exports.TASK_CONTRACT_IMPACT_BASELINE_BLOCKED;
    taskDefinitionPath;
    contractImpact;
    suppliedFingerprint;
    constructor(options) {
        super(options.reason);
        this.name = "TaskBaselineContractImpactError";
        this.taskDefinitionPath = options.taskDefinitionPath;
        this.contractImpact = options.contractImpact;
        this.suppliedFingerprint = options.suppliedFingerprint;
    }
}
exports.TaskBaselineContractImpactError = TaskBaselineContractImpactError;
function taskRuntimeBaselinePreconditionMessage(taskDefinitionPath, issues) {
    return [
        `Task runtime baseline preconditions are blocked at '${(0, planning_document_utils_1.toRepositoryLikePath)(taskDefinitionPath)}'.`,
        ...issues
    ].join(" ");
}
exports.baselinePlans = new WeakMap();
