From e7bbf8543226dc1c21c16ec8eed822a5b13e6dd0 Mon Sep 17 00:00:00 2001 From: Sebastien Tardif Date: Mon, 23 Nov 2026 22:33:22 -0800 Subject: [PATCH 1/3] fix: refuse a release merge when the guard cannot read the PR A failed gh lookup used to print that the pull was safe to merge. The 0.10.0 title is chore(main): release patchloom 0.10.0. The guard now exits 2 when the title cannot be read, and any autorelease label counts as a release. A fake-gh test covers that title. Signed-off-by: Sebastien Tardif --- .github/workflows/ci.yml | 4 + AGENTS.md | 2 +- scripts/guard-no-release-merge.sh | 49 ++++++++---- scripts/guard-no-release-merge.test.sh | 100 +++++++++++++++++++++++++ 4 files changed, 139 insertions(+), 16 deletions(-) create mode 100644 scripts/guard-no-release-merge.test.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 88cbb0d..973fcbf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -68,6 +68,10 @@ jobs: with: persist-credentials: false + - name: Release merge guard + shell: bash + run: bash scripts/guard-no-release-merge.test.sh + - name: Setup Node.js uses: ./.github/actions/setup-node diff --git a/AGENTS.md b/AGENTS.md index 1199fee..2b289fd 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -185,7 +185,7 @@ When you encounter a release PR (during triage, gate check, `gh pr list`, or sta ``` bash scripts/guard-no-release-merge.sh ``` - The script will abort with guidance unless `ALLOW_RELEASE_MERGE=yes` is set (only after user yes). + The script will abort with guidance unless `ALLOW_RELEASE_MERGE=yes` is set (only after user yes). If it cannot read the PR, it exits 2. That is not permission to merge. 5. If checks pass and user said yes: `gh pr merge --squash` (or let auto if user directed). This rule was strengthened after an incident where `gh pr merge 144 --auto` (under a broad "merge everything" instruction) resulted in v0.0.5 being published without explicit per-release "yes". diff --git a/scripts/guard-no-release-merge.sh b/scripts/guard-no-release-merge.sh index c156900..4c92c8c 100755 --- a/scripts/guard-no-release-merge.sh +++ b/scripts/guard-no-release-merge.sh @@ -3,49 +3,68 @@ # # Usage: # bash scripts/guard-no-release-merge.sh [PR_NUMBER] -# # or with current branch context (will try to detect open PR) # -# Exits 0 if safe to merge (non-release PR). -# Exits 1 and prints guidance if release PR detected, unless ALLOW_RELEASE_MERGE=yes. +# Exits 0 if the PR is not a release. +# Exits 1 if it is a release, unless ALLOW_RELEASE_MERGE=yes. +# Exits 2 if the PR title cannot be read. That is not permission to merge. # -# This is a defense-in-depth guard for agents and humans. -# See AGENTS.md for the full "Release PRs - Strong Guard" policy. -# Per global rules: report the PR, ask user "Should I merge it?", only proceed after explicit "yes". +# A failed `gh` lookup used to print "safe to merge". PR #332 +# (chore(main): release patchloom 0.10.0) was merged after that OK line. set -euo pipefail pr="${1:-}" if [[ -z "$pr" ]]; then - # Try to detect PR from current context (works if gh pr view succeeds for head) if pr=$(gh pr view --json number --jq '.number' 2>/dev/null); then : else - echo "ERROR: No PR number provided and could not auto-detect current PR." + echo "ERROR: No PR number provided and could not read the current PR." + echo "Refusing to treat that as safe to merge." echo "Usage: $0 " exit 2 fi fi +repo_args=() +if [[ -n "${GH_REPO:-}" ]]; then + repo_args=(--repo "$GH_REPO") +fi + echo "Guard: inspecting PR #$pr for release-please markers..." -title=$(gh pr view "$pr" --json title --jq '.title' 2>/dev/null || echo "") -labels=$(gh pr view "$pr" --json labels --jq '.labels[].name' 2>/dev/null || true) -body=$(gh pr view "$pr" --json body --jq '.body' 2>/dev/null | head -c 500 || true) +if ! title=$(gh pr view "$pr" "${repo_args[@]}" --json title --jq '.title' 2>/dev/null); then + echo "ERROR: could not read PR #$pr. Refusing to treat it as safe to merge." + exit 2 +fi +if [[ -z "${title//[[:space:]]/}" ]]; then + echo "ERROR: PR #$pr has an empty title. Refusing to treat it as safe to merge." + exit 2 +fi + +labels=$(gh pr view "$pr" "${repo_args[@]}" --json labels --jq '.labels[].name' 2>/dev/null || true) +body=$(gh pr view "$pr" "${repo_args[@]}" --json body --jq '.body' 2>/dev/null || true) is_release=false reason="" -if echo "$labels" | grep -q 'autorelease: pending'; then +shopt -s nocasematch +release_title_re='^(chore|release)(\([^)]*\))?:[[:space:]]*release[[:space:]]' +release_version_re='release[[:space:]]+v?[0-9]+\.[0-9]+' +if [[ "$labels" == *autorelease:* ]]; then + is_release=true + reason="has an autorelease label" +elif [[ "$title" =~ $release_title_re ]]; then is_release=true - reason="has label 'autorelease: pending'" -elif echo "$title" | grep -qiE '^(chore|release).*release |release v?[0-9]+\.[0-9]+'; then + reason="title looks like release: '$title'" +elif [[ "$title" =~ $release_version_re ]]; then is_release=true reason="title looks like release: '$title'" -elif echo "$body" | grep -qi 'release-please'; then +elif [[ "$body" == *release-please* ]]; then is_release=true reason="body mentions release-please" fi +shopt -u nocasematch if [[ "$is_release" == "true" ]]; then echo "" diff --git a/scripts/guard-no-release-merge.test.sh b/scripts/guard-no-release-merge.test.sh new file mode 100644 index 0000000..6885090 --- /dev/null +++ b/scripts/guard-no-release-merge.test.sh @@ -0,0 +1,100 @@ +#!/usr/bin/env bash +# Classifier tests for guard-no-release-merge.sh. Uses a fake gh. +set -euo pipefail + +root="$(cd "$(dirname "$0")/.." && pwd)" +guard="$root/scripts/guard-no-release-merge.sh" +tmp="$(mktemp -d)" +trap 'rm -rf "$tmp"' EXIT + +cat >"$tmp/gh" <<'EOF' +#!/usr/bin/env bash +set -euo pipefail +if [[ "${GUARD_GH_FAIL:-}" == "1" ]]; then + echo "gh failed" >&2 + exit 1 +fi +field="" +prev="" +for arg in "$@"; do + if [[ "$prev" == "--json" ]]; then + field="$arg" + fi + prev="$arg" +done +case "$field" in + title) printf '%s\n' "${GUARD_TITLE-}" ;; + labels) printf '%s\n' "${GUARD_LABELS-}" ;; + body) printf '%s\n' "${GUARD_BODY-}" ;; + *) + echo "unexpected gh: $*" >&2 + exit 1 + ;; +esac +EOF +chmod +x "$tmp/gh" + +expect() { + local want="$1" + local needle="$2" + shift 2 + local out got + set +e + out="$(PATH="$tmp:$PATH" "$@" 2>&1)" + got=$? + set -e + if [[ "$got" != "$want" ]]; then + echo "FAIL: exit $got, wanted $want" + printf '%s\n' "$out" + exit 1 + fi + if ! printf '%s\n' "$out" | grep -q "$needle"; then + echo "FAIL: missing [$needle]" + printf '%s\n' "$out" + exit 1 + fi + echo "OK: exit $want [$needle]" +} + +GUARD_GH_FAIL=1 \ + expect 2 "could not read" \ + bash "$guard" 332 + +GUARD_GH_FAIL= \ + GUARD_TITLE="" \ + expect 2 "empty title" \ + bash "$guard" 332 + +GUARD_GH_FAIL= \ + GUARD_TITLE="chore(main): release patchloom 0.10.0" \ + GUARD_LABELS="" \ + GUARD_BODY="" \ + expect 1 "STRONG GUARD TRIGGERED" \ + bash "$guard" 332 + +ALLOW_RELEASE_MERGE=yes \ + GUARD_GH_FAIL= \ + GUARD_TITLE="chore(main): release patchloom 0.10.0" \ + expect 0 "BYPASS" \ + bash "$guard" 332 + +ALLOW_RELEASE_MERGE= \ + GUARD_TITLE="fix: correct status and install messages" \ + GUARD_LABELS="" \ + GUARD_BODY="ordinary bugfix" \ + expect 0 "does not appear to be a release PR" \ + bash "$guard" 332 + +GUARD_TITLE="chore: deps" \ + GUARD_LABELS="autorelease: tagged" \ + GUARD_BODY="" \ + expect 1 "autorelease" \ + bash "$guard" 332 + +GUARD_TITLE="docs: notes" \ + GUARD_LABELS="" \ + GUARD_BODY=":robot: I have created a release *beep* *boop* release-please" \ + expect 1 "release-please" \ + bash "$guard" 332 + +echo "DONE: release guard classifier" From 88d949840cbce252c66dcc87a361604aea51246b Mon Sep 17 00:00:00 2001 From: Sebastien Tardif Date: Mon, 23 Nov 2026 22:33:22 -0800 Subject: [PATCH 2/3] test: wait for the MCP server before deleting its directory On Windows the tools/call test deleted the server cwd while the process still held it, so rmdir failed with EBUSY. Wait for exit, then retry the delete. Signed-off-by: Sebastien Tardif --- test/unit/patchloomCli.test.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/test/unit/patchloomCli.test.ts b/test/unit/patchloomCli.test.ts index 4e71f6d..2fa2104 100644 --- a/test/unit/patchloomCli.test.ts +++ b/test/unit/patchloomCli.test.ts @@ -1295,7 +1295,12 @@ describe("managed install end-to-end MCP", { timeout: 120_000 }, async () => { await new Promise((resolve) => setTimeout(resolve, 100)); } - child.kill(); + if (child.exitCode === null && child.signalCode === null) { + await new Promise((resolve) => { + child.once("exit", () => resolve()); + child.kill(); + }); + } // Verify the tool call succeeded const lines = stdout.trim().split("\n"); @@ -1314,7 +1319,25 @@ describe("managed install end-to-end MCP", { timeout: 120_000 }, async () => { const content = JSON.parse(await fs.readFile(path.join(workDir, "config.json"), "utf8")) as Record; assert.equal(content.port, 8080, "doc_set should have changed port to 8080"); - await fs.rm(workDir, { recursive: true, force: true }); + // Windows keeps the MCP server's cwd locked for a moment after exit. + let removeError: unknown; + for (let attempt = 0; attempt < 10; attempt++) { + try { + await fs.rm(workDir, { recursive: true, force: true }); + removeError = undefined; + break; + } catch (error) { + removeError = error; + const code = (error as NodeJS.ErrnoException).code; + if (code !== "EBUSY" && code !== "EPERM") { + throw error; + } + await new Promise((resolve) => setTimeout(resolve, 100)); + } + } + if (removeError) { + throw removeError; + } }); // Cleanup From ba9fa52ac9bad58c50c4d60b95a43682d97ce3a1 Mon Sep 17 00:00:00 2001 From: Sebastien Tardif Date: Mon, 23 Nov 2026 22:35:17 -0800 Subject: [PATCH 3/3] fix: avoid an empty array under bash 3.2 in the release guard macOS /bin/bash treats "${repo_args[@]}" as unbound when the array is empty and set -u is on. The guard then exited before it could read the title. Call gh with --repo only when GH_REPO is set. Signed-off-by: Sebastien Tardif --- scripts/guard-no-release-merge.sh | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/scripts/guard-no-release-merge.sh b/scripts/guard-no-release-merge.sh index 4c92c8c..33e952a 100755 --- a/scripts/guard-no-release-merge.sh +++ b/scripts/guard-no-release-merge.sh @@ -26,14 +26,21 @@ if [[ -z "$pr" ]]; then fi fi -repo_args=() -if [[ -n "${GH_REPO:-}" ]]; then - repo_args=(--repo "$GH_REPO") -fi +# An empty array expansion is an unbound variable under set -u on bash 3.2 +# (macOS /bin/bash). Pass --repo only when GH_REPO is set. +gh_view() { + local json_field="$1" + local jq_expr="$2" + if [[ -n "${GH_REPO:-}" ]]; then + gh pr view "$pr" --repo "$GH_REPO" --json "$json_field" --jq "$jq_expr" + else + gh pr view "$pr" --json "$json_field" --jq "$jq_expr" + fi +} echo "Guard: inspecting PR #$pr for release-please markers..." -if ! title=$(gh pr view "$pr" "${repo_args[@]}" --json title --jq '.title' 2>/dev/null); then +if ! title=$(gh_view title '.title' 2>/dev/null); then echo "ERROR: could not read PR #$pr. Refusing to treat it as safe to merge." exit 2 fi @@ -42,8 +49,8 @@ if [[ -z "${title//[[:space:]]/}" ]]; then exit 2 fi -labels=$(gh pr view "$pr" "${repo_args[@]}" --json labels --jq '.labels[].name' 2>/dev/null || true) -body=$(gh pr view "$pr" "${repo_args[@]}" --json body --jq '.body' 2>/dev/null || true) +labels=$(gh_view labels '.labels[].name' 2>/dev/null || true) +body=$(gh_view body '.body' 2>/dev/null || true) is_release=false reason=""