"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWorkerWorkflowPlanReviewPrompt = buildWorkerWorkflowPlanReviewPrompt;
const dedicated_prompt_support_1 = require("./dedicated-prompt-support");
const prompt_repair_1 = require("./prompt-repair");
const prompt_text_1 = require("./prompt-text");
const system_prompt_names_1 = require("./system-prompt-names");
const workflow_plan_review_contract_1 = require("./workflow-plan-review-contract");
function buildWorkerWorkflowPlanReviewPrompt(workerName, input, repairContext) {
    const contract = (0, workflow_plan_review_contract_1.buildWorkerWorkflowPlanReviewContract)(input);
    return (0, dedicated_prompt_support_1.renderDedicatedPromptEnvelope)({
        preambleLines: (0, dedicated_prompt_support_1.renderDedicatedPromptHeader)({
            workerName,
            runDescription: "This is a dedicated workflow plan review run.",
            instructionLines: [
                "Read repository instructions in AGENTS.md and CLAUDE.md before acting. For plan review capability checks, use AlpacaLoop capability catalog commands instead of reading repo-local skill bodies unless exact local skill instructions are needed for a finding.",
                (0, prompt_text_1.buildResponseLanguageInstruction)(input.outputLanguage),
                "Review whether the generated workflow plan can achieve the user's requested outcome before execution begins.",
                "Do not revise the plan in this run. Return only use_plan, request_revision, or block_execution with structured findings.",
                "Do not delegate to subagents or parallel reviewers during this plan review run.",
                "Treat the current plan, contract, execution guidance, predicted touched paths, issue text, and latest comments as review evidence.",
                "For report or note deliverables, review whether the plan uses comment-only completion, the configured issue notes path with `issue-<issue-number>-<slug>.md` naming, or durable project documentation according to the deliverable's expected lifetime.",
                "Do not accept a durable project-documentation destination unless the plan shows why the report should become project knowledge and chooses the path from the repository's existing documentation structure rather than a fixed directory.",
                "Before accepting a plan, treat any step as capability-sensitive when completing or validating it depends on expertise, current external context, governed process rules, live organizational or service access, specialized artifact standards, or dedicated tools beyond the base worker's ordinary repository/workspace inspection and execution abilities; inspect reusable skill/MCP capabilities for those steps.",
                "When command execution is available, use bounded AlpacaLoop capability catalog evidence for those checks (`alpaca capability search/list`, `alpaca skill search/list`, `alpaca mcp search/list/verify`); do not substitute memory or bulk SKILL.md/MCP file reads for catalog evidence unless the catalog is unavailable or a finding depends on exact local instructions.",
                'For planning-time capability judgment, search installed and installable candidates by default with `alpaca capability search "<query>" --for all`; use `--installed-only` only when the plan claims a capability can be used immediately, and do not add unsupported flags or worker-assisted search flags such as `--with-worker`.',
                "Build capability search queries from concise domain terms in the issue or plan rather than broad composite descriptions; if a capability, skill, MCP, discipline, artifact type, or tool family is named or strongly implied, include that exact term in at least one catalog query. Do not read skill bodies merely to learn catalog command syntax.",
                "Prefer domain-specific search result items over broad capability inventory output. Use `alpaca capability list` only to check currently usable refs, and when a top domain match is installable but not installed, ask the plan to route through skill/MCP resolution instead of ignoring that candidate.",
                "A capability-sensitive plan is acceptable only when each such step either uses a fitting available skill/MCP, routes a missing reusable capability through skill/MCP resolution before dependent work, or gives a concrete reason why no specialized capability is needed.",
                "Accept an explicit prior capability-discovery or resolution step when its `doneWhen` requires selected skill/MCP refs, resolution output, or a no-specialized-capability rationale before dependent work; request revision when dependent work can proceed before that evidence exists or the criteria are too vague to verify.",
                "When catalog evidence finds fitting capability candidates, treat generic capability labels without exact skill refs, usable installed MCP refs, MCP catalog ids, or search result ids as too vague; request revision unless a prior readiness or resolution step requires exact selected, deferred, rejected, or no-specialized-capability decisions before dependent work.",
                "Do not treat MCP catalog ids or search result ids as valid `requires.mcp` refs unless `alpaca mcp list` or `alpaca mcp verify --ref <ref>` confirms a usable ref; apply the same distinction between skill catalog/search ids and usable `requires.skills` refs.",
            ],
            systemPrompts: input.systemPrompts,
            systemPromptNames: system_prompt_names_1.WORKFLOW_PLAN_REVIEW_SYSTEM_PROMPT_NAMES,
            allowSubagents: false,
        }),
        repairLines: repairContext
            ? (0, prompt_repair_1.renderWorkflowPlanReviewRepairSectionLines)(repairContext)
            : undefined,
        inputLines: [
            "Workflow plan review input:",
            ...(0, dedicated_prompt_support_1.renderDedicatedWorkspaceFields)(input),
            `- Planning intent: ${input.planningIntent}`,
            `- Workflow surface: ${input.workflowSurface}`,
            `- Template: ${input.templateName ?? prompt_text_1.PROMPT_TEXT.none}`,
            `- Reviewed planRevision: ${String(input.reviewedPlanRevision)}`,
            ...(input.issueNotesPath !== undefined
                ? [`- Issue notes path: ${input.issueNotesPath}`]
                : []),
            "- Execution issue:",
            `  - Title: ${input.issue.title}`,
            "  - Body:",
            "```text",
            input.issue.body || prompt_text_1.PROMPT_TEXT.empty,
            "```",
            "- Latest issue comments:",
            ...renderWorkflowPlanReviewComments(input.latestComments),
            "- Contract:",
            "```json",
            JSON.stringify(input.contract, null, 2),
            "```",
            "- Plan:",
            "```json",
            JSON.stringify(input.plan, null, 2),
            "```",
            "- Execution guidance:",
            ...renderStringList(input.executionGuidance),
            "- Predicted touched paths:",
            ...renderStringList(input.predictedTouchedPaths),
        ],
        contract: {
            heading: "Workflow plan review contract:",
            rules: contract.rules,
            promptSchema: contract.promptSchema,
            example: contract.example,
        },
    });
}
function renderWorkflowPlanReviewComments(comments) {
    if (comments.length === 0) {
        return [`  ${prompt_text_1.PROMPT_TEXT.none}`];
    }
    return comments.flatMap((comment) => [
        `  - ${comment.createdAt} ${comment.author}:`,
        "```text",
        comment.body || prompt_text_1.PROMPT_TEXT.empty,
        "```",
    ]);
}
function renderStringList(values) {
    if (values.length === 0) {
        return [`  ${prompt_text_1.PROMPT_TEXT.none}`];
    }
    return values.map((value) => `  - ${value}`);
}
