/**
 * Verify-phase audit recorder. Captures schema-validation passes,
 * structural-check results, and retry events at the moments they happen
 * during analyse-runner execution.
 *
 * Forward-logging discipline (engines 6.22.x+): validation results are
 * recorded by the runValidate wrapper at the moment of validation;
 * structural-check results are recorded by the check pass after each
 * successful schema validation; retry events are recorded by the retry
 * loop when it triggers. No backward inference.
 *
 * Sidecars produced (by analyse-runner at end of run):
 *   - verify/invariants.json: each check + result + items requiring review
 *   - verify/retries.json: retry events with error categories and deltas
 */
export class InMemoryVerifyRecorder {
    passes = [];
    structuralChecks = [];
    humanReviewItems = [];
    retries = [];
    nextPassOrdinal = 1;
    nextRetryOrdinal = 1;
    recordPass(args) {
        const pass = { ordinal: this.nextPassOrdinal++, ...args };
        this.passes.push(pass);
        return pass;
    }
    recordStructuralCheck(check) {
        this.structuralChecks.push(check);
        // Promote violations to human-review items for visibility.
        for (const v of check.violations) {
            this.humanReviewItems.push({
                severity: check.ok ? 'info' : 'warning',
                kind: check.name,
                path: v.path,
                message: v.issue,
            });
        }
    }
    recordHumanReviewItem(item) {
        this.humanReviewItems.push(item);
    }
    recordRetry(args) {
        const r = { ordinal: this.nextRetryOrdinal++, ...args };
        this.retries.push(r);
        return r;
    }
    buildInvariants() {
        return {
            schemaVersion: '1.0',
            stage: 'verify',
            passes: this.passes,
            structuralChecks: this.structuralChecks,
            humanReviewItems: this.humanReviewItems,
        };
    }
    buildRetries() {
        return {
            schemaVersion: '1.0',
            stage: 'verify',
            retries: this.retries,
        };
    }
}
/** Categorise a validation error string by pattern match. Used for
 *  retry-event analytics. */
export function categoriseValidationError(errorText) {
    const categories = {};
    const lines = errorText.split('\n').filter((l) => l.trim());
    for (const line of lines) {
        let cat = 'other';
        if (/Unknown property/i.test(line))
            cat = 'unknown-property';
        else if (/must have required property/i.test(line))
            cat = 'missing-required';
        else if (/must be string|must be number|must be boolean/i.test(line))
            cat = 'wrong-type';
        else if (/must match pattern/i.test(line))
            cat = 'pattern-mismatch';
        else if (/must match a schema in anyOf|must match exactly one schema in oneOf/i.test(line))
            cat = 'schema-shape-mismatch';
        else if (/must be equal to one of/i.test(line))
            cat = 'enum-violation';
        else if (/realize:|name collision/i.test(line))
            cat = 'realize-collision';
        categories[cat] = (categories[cat] ?? 0) + 1;
    }
    return categories;
}
//# sourceMappingURL=verify-recorder.js.map