// engine/teammate-router/tdd-router.ts — SPEC-750
// Routes spec implementation through TestArchitect when tdd: strict.
import { validateRedTestsHandoff, buildTestArchitectPrompt } from './roles/test-architect.js';
import { verifyRedPhase } from './red-phase-verifier.js';
import { verifyGreenPhase } from './green-phase-verifier.js';
// ---------------------------------------------------------------------------
// Routing decision
// ---------------------------------------------------------------------------
/**
 * Determine if a spec's tdd frontmatter value requires TestArchitect routing.
 * Only `tdd: strict` triggers the two-phase TDD flow.
 */
export function decideTddRouting(tddValue) {
    if (tddValue === 'strict') {
        return {
            requiresTestArchitect: true,
            reason: 'Spec has tdd: strict — TestArchitect must produce red tests before Implementer runs.',
        };
    }
    return {
        requiresTestArchitect: false,
        reason: `Spec has tdd: ${tddValue ?? 'flexible'} — single-Implementer flow applies.`,
    };
}
// ---------------------------------------------------------------------------
// Red phase orchestration
// ---------------------------------------------------------------------------
/**
 * Execute the TestArchitect red phase.
 * In production this spawns the TestArchitect agent; here we validate the
 * handoff JSON that the agent should have written.
 */
export async function runRedPhase(specId, specBody, projectRoot, testFramework, handoffRaw) {
    void buildTestArchitectPrompt({ specId, specBody, projectRoot, testFramework });
    const validation = validateRedTestsHandoff(handoffRaw);
    if (!validation.ok) {
        return { ok: false, error: validation.error };
    }
    const redResult = await verifyRedPhase(validation.handoff, projectRoot);
    if (redResult.status !== 'red') {
        return {
            ok: false,
            error: `RED_PHASE_NOT_RED: expected tests to fail but suite returned status=${redResult.status}`,
        };
    }
    return { ok: true, handoff: validation.handoff };
}
// ---------------------------------------------------------------------------
// Green phase orchestration
// ---------------------------------------------------------------------------
/**
 * Execute the green phase gate.
 * Ensures Implementer's code turns tests green without deleting them.
 */
export async function runGreenPhase(redHandoff, projectRoot) {
    const greenResult = await verifyGreenPhase(redHandoff, projectRoot);
    if (greenResult.status === 'tampered') {
        return {
            ok: false,
            status: 'tampered',
            removedTests: greenResult.removedTests,
            error: `RED_TESTS_DELETED: Implementer removed expected red tests: ${greenResult.removedTests.join(', ')}`,
        };
    }
    if (greenResult.status !== 'green') {
        return {
            ok: false,
            status: 'red',
            error: 'GREEN_PHASE_FAILED: Tests are still failing after Implementer committed.',
        };
    }
    return { ok: true, status: 'green', removedTests: [] };
}
//# sourceMappingURL=tdd-router.js.map