|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require "minitest/autorun" |
| 5 | + |
| 6 | +ROOT = File.expand_path("../../..", __dir__) |
| 7 | +WORKFLOW_PATH = File.join(ROOT, "workflows/pr-processing.md") |
| 8 | +PR_BATCH_SKILL_PATH = File.join(ROOT, "skills/pr-batch/SKILL.md") |
| 9 | +PLAN_PR_BATCH_SKILL_PATH = File.join(ROOT, "skills/plan-pr-batch/SKILL.md") |
| 10 | + |
| 11 | +TEXT_FENCE = "```text\n" |
| 12 | +CANONICAL_CONTRACT_LINK = "../../workflows/pr-processing.md#goal-mode-completion-contract" |
| 13 | +PENDING_CHECKS_PRESSURE = "A batch with 5 PRs, 3 pending hosted checks, and clean review threads is NOT COMPLETE" |
| 14 | + |
| 15 | +def read_repo_file(path) |
| 16 | + File.read(path, encoding: "UTF-8") |
| 17 | +end |
| 18 | + |
| 19 | +def extract_goal_prompt_template(skill_text, heading, end_heading: /^##\s+/) |
| 20 | + heading_index = skill_text.index(heading) |
| 21 | + raise "missing #{heading} section" unless heading_index |
| 22 | + |
| 23 | + fence_start = skill_text.index(TEXT_FENCE, heading_index) |
| 24 | + raise "missing text fence in Goal Prompt section" unless fence_start |
| 25 | + |
| 26 | + fence_body_start = fence_start + TEXT_FENCE.length |
| 27 | + next_heading = skill_text.match(end_heading, fence_body_start) |
| 28 | + section_end = next_heading ? next_heading.begin(0) : skill_text.length |
| 29 | + section_body = skill_text[fence_body_start...section_end] |
| 30 | + fence_offsets = [] |
| 31 | + section_body.scan(/^```\s*$/) { fence_offsets << Regexp.last_match.begin(0) } |
| 32 | + |
| 33 | + raise "missing closing fence in Goal Prompt section" if fence_offsets.empty? |
| 34 | + if fence_offsets.length > 1 |
| 35 | + raise "goal prompt template contains a nested bare fence line; use a non-text fence type instead" |
| 36 | + end |
| 37 | + |
| 38 | + section_body[0...fence_offsets.first] |
| 39 | +end |
| 40 | + |
| 41 | +def extract_markdown_section(text, heading, end_heading: /^###\s+/) |
| 42 | + heading_index = text.index(heading) |
| 43 | + raise "missing #{heading} section" unless heading_index |
| 44 | + |
| 45 | + body_start = heading_index + heading.length |
| 46 | + next_heading = text.match(end_heading, body_start) |
| 47 | + body_end = next_heading ? next_heading.begin(0) : text.length |
| 48 | + text[body_start...body_end] |
| 49 | +end |
| 50 | + |
| 51 | +def contract_line(text) |
| 52 | + text.lines.grep(/^Goal Mode Completion Contract:/).first&.chomp |
| 53 | +end |
| 54 | + |
| 55 | +def assert_text_includes(text, phrase, label) |
| 56 | + assert text.include?(phrase), "#{label} is missing required phrase: #{phrase}" |
| 57 | +end |
| 58 | + |
| 59 | +class GoalCompletionContractTest < Minitest::Test |
| 60 | + def setup |
| 61 | + @workflow = read_repo_file(WORKFLOW_PATH) |
| 62 | + @pr_batch_skill = read_repo_file(PR_BATCH_SKILL_PATH) |
| 63 | + @plan_pr_batch_skill = read_repo_file(PLAN_PR_BATCH_SKILL_PATH) |
| 64 | + @workflow_contract_section = extract_markdown_section(@workflow, "### Goal Mode Completion Contract") |
| 65 | + @workflow_goal_prompt = extract_goal_prompt_template( |
| 66 | + @workflow, |
| 67 | + "### Plan To Goal Handoff", |
| 68 | + end_heading: /^###\s+/ |
| 69 | + ) |
| 70 | + @pr_batch_goal_prompt = extract_goal_prompt_template(@pr_batch_skill, "## Goal Prompt Template") |
| 71 | + @plan_goal_prompt = extract_goal_prompt_template(@plan_pr_batch_skill, "## Goal Prompt for pr-batch") |
| 72 | + end |
| 73 | + |
| 74 | + def test_canonical_contract_is_present_in_workflow_and_goal_sources |
| 75 | + { |
| 76 | + "workflows/pr-processing.md canonical contract" => @workflow_contract_section, |
| 77 | + "workflows/pr-processing.md goal prompt" => @workflow_goal_prompt, |
| 78 | + "skills/pr-batch goal prompt" => @pr_batch_goal_prompt, |
| 79 | + "skills/plan-pr-batch goal prompt" => @plan_goal_prompt |
| 80 | + }.each do |label, text| |
| 81 | + assert_text_includes text, "Goal Mode Completion Contract", label |
| 82 | + assert_text_includes text, "waiting-on-checks-or-review` is not an overall Goal-mode terminal state", label |
| 83 | + assert_text_includes text, "report NOT COMPLETE", label |
| 84 | + assert_text_includes text, "pending, missing, or untriaged current-head CI", label |
| 85 | + assert_text_includes text, "unresolved current-head review threads", label |
| 86 | + assert_text_includes text, "UNKNOWN", label |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + def test_skill_prose_points_to_canonical_contract_instead_of_pasting_it |
| 91 | + assert_text_includes @pr_batch_skill, CANONICAL_CONTRACT_LINK, "skills/pr-batch/SKILL.md" |
| 92 | + assert_equal 1, @pr_batch_skill.scan(PENDING_CHECKS_PRESSURE).length, |
| 93 | + "skills/pr-batch/SKILL.md should keep the detailed pressure scenario only in the dispatch prompt" |
| 94 | + end |
| 95 | + |
| 96 | + def test_canonical_and_dispatch_prompt_contracts_stay_byte_for_byte_aligned |
| 97 | + workflow_contract = contract_line(@workflow_contract_section) |
| 98 | + workflow_goal_contract = contract_line(@workflow_goal_prompt) |
| 99 | + pr_batch_contract = @pr_batch_goal_prompt.lines.grep(/^Goal Mode Completion Contract:/).first |
| 100 | + plan_contract = @plan_goal_prompt.lines.grep(/^Goal Mode Completion Contract:/).first |
| 101 | + |
| 102 | + refute_nil workflow_contract, "workflows/pr-processing.md is missing the canonical contract line" |
| 103 | + refute_nil workflow_goal_contract, "workflows/pr-processing.md goal prompt is missing the contract line" |
| 104 | + refute_nil pr_batch_contract, "skills/pr-batch goal prompt is missing the contract line" |
| 105 | + refute_nil plan_contract, "skills/plan-pr-batch goal prompt is missing the contract line" |
| 106 | + assert_equal workflow_contract, workflow_goal_contract |
| 107 | + assert_equal workflow_contract, pr_batch_contract.chomp |
| 108 | + assert_equal workflow_contract, plan_contract.chomp |
| 109 | + end |
| 110 | + |
| 111 | + def test_goal_prompt_extractor_rejects_nested_bare_fence_lines |
| 112 | + skill_text = <<~TEXT |
| 113 | + ## Goal Prompt Template |
| 114 | +
|
| 115 | + ```text |
| 116 | + Use $pr-batch. |
| 117 | + ``` |
| 118 | + stray prose |
| 119 | + ``` |
| 120 | +
|
| 121 | + ## Next Section |
| 122 | + TEXT |
| 123 | + |
| 124 | + error = assert_raises(RuntimeError) { extract_goal_prompt_template(skill_text, "## Goal Prompt Template") } |
| 125 | + assert_match(/nested bare fence/, error.message) |
| 126 | + end |
| 127 | + |
| 128 | + def test_pending_hosted_checks_pressure_scenario_is_not_complete |
| 129 | + { |
| 130 | + "workflows/pr-processing.md" => @workflow, |
| 131 | + "workflows/pr-processing.md goal prompt" => @workflow_goal_prompt, |
| 132 | + "skills/pr-batch goal prompt" => @pr_batch_goal_prompt, |
| 133 | + "skills/plan-pr-batch goal prompt" => @plan_goal_prompt |
| 134 | + }.each do |label, text| |
| 135 | + assert_text_includes text, PENDING_CHECKS_PRESSURE, label |
| 136 | + end |
| 137 | + end |
| 138 | + |
| 139 | + def test_ready_no_merge_authority_is_terminal_only_without_merge_authority |
| 140 | + { |
| 141 | + "workflows/pr-processing.md" => @workflow, |
| 142 | + "workflows/pr-processing.md goal prompt" => @workflow_goal_prompt, |
| 143 | + "skills/pr-batch goal prompt" => @pr_batch_goal_prompt, |
| 144 | + "skills/plan-pr-batch goal prompt" => @plan_goal_prompt |
| 145 | + }.each do |label, text| |
| 146 | + assert_text_includes text, "`ready-no-merge-authority` is terminal only when `merge_authority` does not allow merging", label |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + def test_auto_merge_done_means_merged_or_blocked |
| 151 | + { |
| 152 | + "workflows/pr-processing.md" => @workflow, |
| 153 | + "workflows/pr-processing.md goal prompt" => @workflow_goal_prompt, |
| 154 | + "skills/pr-batch goal prompt" => @pr_batch_goal_prompt, |
| 155 | + "skills/plan-pr-batch goal prompt" => @plan_goal_prompt |
| 156 | + }.each do |label, text| |
| 157 | + assert_text_includes text, "With `auto_merge_when_gates_pass`, done means merged and closed out unless a real blocker prevents it", label |
| 158 | + end |
| 159 | + end |
| 160 | + |
| 161 | + def test_normal_restart_stays_pause_resume_not_cancel_relaunch |
| 162 | + assert_text_includes @workflow, "pause, not cancellation", "workflows/pr-processing.md" |
| 163 | + assert_text_includes @workflow, "do not use this pause flow; use", "workflows/pr-processing.md" |
| 164 | + assert_text_includes @workflow, "Cancelling Or Stopping A Batch", "workflows/pr-processing.md" |
| 165 | + assert_text_includes @pr_batch_skill, "Preserve claims and worktrees", "skills/pr-batch/SKILL.md" |
| 166 | + assert_text_includes @pr_batch_skill, "updated skills", "skills/pr-batch/SKILL.md" |
| 167 | + assert_text_includes @pr_batch_skill, "launching fresh workers", "skills/pr-batch/SKILL.md" |
| 168 | + end |
| 169 | +end |
0 commit comments