// engine/multi-teammate-review/arbitrator-runner.ts — SPEC-727
// Wraps the arbitrator LLM call and validates output via Zod-equivalent shape.
import { z } from 'zod';
import { ARBITRATOR_PROMPT } from './arbitrator-prompt.js';
// ---------------------------------------------------------------------------
// Custom error
// ---------------------------------------------------------------------------
export class ArbitratorOutputInvalid extends Error {
    constructor(reason) {
        super(`ArbitratorOutputInvalid: ${reason}`);
        this.name = 'ArbitratorOutputInvalid';
    }
}
// ---------------------------------------------------------------------------
// Zod schema for ArbitratorDecision validation
// ---------------------------------------------------------------------------
const SeveritySchema = z.enum(['critical', 'suggestion', 'nit', 'false-positive']);
const SpecialistRoles = [
    'security',
    'correctness',
    'performance',
    'maintainability',
    'architecture',
];
const ArbitratorDecisionItemSchema = z.object({
    severity: SeveritySchema,
    file: z.string(),
    line: z.number(),
    message: z.string(),
    recommendation: z.string(),
    contributors: z.array(z.enum(['security', 'correctness', 'performance', 'maintainability', 'architecture'])),
    justification: z.string(),
});
const ArbitratorResponseSchema = z.object({
    decisions: z.array(ArbitratorDecisionItemSchema),
});
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Invoke the Arbitrator LLM and validate the output.
 *
 * Throws `ArbitratorOutputInvalid` if:
 * - The raw response cannot be parsed as JSON.
 * - The parsed response does not match the `ArbitratorDecision` Zod schema.
 * - The `decisions` array is missing.
 *
 * On success, returns the validated `ArbitratorDecision[]`.
 */
export async function runArbitrator(input) {
    const raw = await input.invoker.invoke(ARBITRATOR_PROMPT, input.userMessage);
    // Extract JSON object from raw LLM response (may have surrounding text)
    const match = /\{[\s\S]*\}/.exec(raw);
    if (!match) {
        throw new ArbitratorOutputInvalid('Response contains no JSON object.');
    }
    let parsed;
    try {
        parsed = JSON.parse(match[0]);
    }
    catch (err) {
        const msg = err instanceof Error ? err.message : String(err);
        throw new ArbitratorOutputInvalid(`JSON parse failed: ${msg}`);
    }
    // Validate with Zod
    const result = ArbitratorResponseSchema.safeParse(parsed);
    if (!result.success) {
        throw new ArbitratorOutputInvalid(`Schema validation failed: ${result.error.message}`);
    }
    // Map validated items to canonical ArbitratorDecision objects
    return result.data.decisions.map((d) => ({
        severity: d.severity,
        file: d.file,
        line: d.line,
        message: d.message,
        recommendation: d.recommendation,
        contributors: d.contributors.filter((r) => SpecialistRoles.includes(r)),
        justification: d.justification,
    }));
}
//# sourceMappingURL=arbitrator-runner.js.map