// engine/workflow-validator/worktree-protocol.ts — SPEC-053
// Worktree base validation and integration readiness checks.
import { execSync } from 'node:child_process';
// === Helpers ===
function runCmd(cmd, cwd) {
    try {
        const output = execSync(cmd, {
            cwd,
            encoding: 'utf-8',
            stdio: ['pipe', 'pipe', 'pipe'],
        }).trim();
        return { output, exitCode: 0 };
    }
    catch (err) {
        const error = err;
        return {
            output: (error.stdout ?? '') + (error.stderr ?? ''),
            exitCode: error.status ?? 1,
        };
    }
}
function getWorktreeBranchBase(worktreePath) {
    const result = runCmd('git rev-parse --abbrev-ref HEAD', worktreePath);
    if (result.exitCode !== 0) {
        return '';
    }
    // Get the merge-base of the current branch with develop
    const developResult = runCmd('git rev-parse develop', worktreePath);
    if (developResult.exitCode !== 0) {
        return 'main'; // develop doesn't exist — likely based from main
    }
    const mergeBaseResult = runCmd(`git merge-base HEAD ${developResult.output}`, worktreePath);
    if (mergeBaseResult.exitCode !== 0) {
        return '';
    }
    // Check if merge-base equals develop HEAD (branch was from develop)
    if (mergeBaseResult.output === developResult.output) {
        return 'develop';
    }
    // Check if merge-base equals main HEAD
    const mainResult = runCmd('git rev-parse main', worktreePath);
    if (mainResult.exitCode === 0 && mergeBaseResult.output === mainResult.output) {
        return 'main';
    }
    return 'develop'; // assume develop as default
}
// === C13: Validate worktree was created from develop ===
export function validateWorktreeBase(worktreePath) {
    const baseBranch = getWorktreeBranchBase(worktreePath);
    if (!baseBranch) {
        return {
            valid: false,
            baseBranch: 'unknown',
            message: 'Could not determine the base branch of this worktree.',
            suggestion: 'Ensure git is initialized and both develop and main branches exist.',
        };
    }
    if (baseBranch === 'develop') {
        return {
            valid: true,
            baseBranch: 'develop',
            message: 'Worktree is correctly based on develop.',
        };
    }
    return {
        valid: false,
        baseBranch,
        message: `Worktree was created from '${baseBranch}' instead of 'develop'.`,
        suggestion: 'Always create worktrees from develop: git worktree add .claude/worktrees/agent-XXX develop',
    };
}
// === C15: Integration readiness — typecheck + lint must pass ===
export function validateIntegrationReadiness(projectPath) {
    const issues = [];
    const typecheckResult = runCmd('pnpm typecheck', projectPath);
    const typecheckPassed = typecheckResult.exitCode === 0;
    if (!typecheckPassed) {
        issues.push('TypeScript typecheck failed — fix all type errors before committing.');
    }
    const lintResult = runCmd('pnpm lint', projectPath);
    const lintPassed = lintResult.exitCode === 0;
    if (!lintPassed) {
        issues.push('ESLint failed — fix all lint errors/warnings before committing.');
    }
    return {
        ready: typecheckPassed && lintPassed,
        typecheckPassed,
        lintPassed,
        issues,
    };
}
//# sourceMappingURL=worktree-protocol.js.map