import { logger } from "../middleware/logger.js";
function buildIssueSnapshot(issue) {
    return {
        id: issue.id,
        identifier: issue.identifier ?? null,
        title: issue.title,
        description: issue.description,
        status: issue.status,
        priority: issue.priority,
    };
}
export function buildIssueReviewWakeupOptions(input) {
    const blockedInstructions = input.issue.status === "blocked"
        ? "The issue is blocked and has been routed to you as reviewer. Decide whether to confirm this as a human/external blocker, request changes, approve, or keep a specific follow-up open. If you confirm the blocker with `blocked`, write the next human action clearly in the review comment; Rudder will record the blocked reviewer decision and remove it from repeated reviewer pickup until the board changes the issue."
        : "The issue is ready for review.";
    return {
        source: "review",
        triggerDetail: "system",
        reason: "issue_review_requested",
        payload: { issueId: input.issue.id, mutation: input.mutation },
        requestedByActorType: input.requestedByActorType,
        requestedByActorId: input.requestedByActorId ?? null,
        contextSnapshot: {
            issueId: input.issue.id,
            source: input.contextSource,
            wakeSource: "review",
            wakeReason: "issue_review_requested",
            role: "reviewer",
            issue: buildIssueSnapshot(input.issue),
            reviewInstructions: `${blockedInstructions} Record one structured reviewer decision before exiting: approve, request_changes, needs_followup, or blocked. Use ` +
                "`rudder issue review`; do not rely on a free-form comment as the durable outcome. Do not take over implementation unless explicitly asked.",
        },
    };
}
export function buildIssueConvergenceReviewWakeupOptions(input) {
    const mutation = input.mutation ?? "convergence_escalation";
    return {
        source: "review",
        triggerDetail: "system",
        reason: "issue_convergence_review_requested",
        payload: {
            issueId: input.issue.id,
            mutation,
            originRunId: input.originRunId,
            previousRunId: input.previousRunId,
            attempts: input.attempts,
            maxAttempts: input.maxAttempts,
        },
        requestedByActorType: input.requestedByActorType,
        requestedByActorId: input.requestedByActorId ?? null,
        contextSnapshot: {
            issueId: input.issue.id,
            source: input.contextSource,
            wakeSource: "review",
            wakeReason: "issue_convergence_review_requested",
            role: "reviewer",
            issue: buildIssueSnapshot(input.issue),
            convergenceReview: {
                originRunId: input.originRunId,
                previousRunId: input.previousRunId,
                attempts: input.attempts,
                maxAttempts: input.maxAttempts,
            },
            reviewInstructions: "The assignee did not converge this issue after passive follow-up. Review the thread and decide the next step: request changes, mark blocked, escalate or reassign, or mark done only if the evidence is sufficient.",
        },
    };
}
export function buildIssueReviewCloseoutWakeupOptions(input) {
    const mutation = input.mutation ?? "review_outcome_missing";
    return {
        source: "review",
        triggerDetail: "system",
        reason: "issue_review_closeout_missing",
        payload: {
            issueId: input.issue.id,
            mutation,
            originRunId: input.originRunId,
            previousRunId: input.previousRunId,
            attempts: input.attempts,
            maxAttempts: input.maxAttempts,
        },
        requestedByActorType: input.requestedByActorType,
        requestedByActorId: input.requestedByActorId ?? null,
        contextSnapshot: {
            issueId: input.issue.id,
            source: input.contextSource,
            wakeSource: "review",
            wakeReason: "issue_review_closeout_missing",
            role: "reviewer",
            issue: buildIssueSnapshot(input.issue),
            reviewCloseout: {
                originRunId: input.originRunId,
                previousRunId: input.previousRunId,
                attempt: input.attempts,
                maxAttempts: input.maxAttempts,
            },
            reviewInstructions: "Your previous reviewer run ended without a structured decision. Inspect the current issue state and record exactly one outcome with `rudder issue review --decision approve|request_changes|needs_followup|blocked --comment-file <path>`.",
        },
    };
}
export function queueIssueReviewWakeup(input) {
    if (!input.issue.reviewerAgentId || (input.issue.status !== "in_review" && input.issue.status !== "blocked"))
        return;
    if (input.actorAgentId && input.issue.reviewerAgentId === input.actorAgentId)
        return;
    return input.heartbeat
        .wakeup(input.issue.reviewerAgentId, buildIssueReviewWakeupOptions(input))
        .catch((err) => {
        logger.warn({ err, issueId: input.issue.id }, "failed to wake reviewer on issue review request");
        if (input.rethrowOnError)
            throw err;
        return null;
    });
}
//# sourceMappingURL=issue-review-wakeup.js.map