Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <number>
```
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 <number> --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".
Expand Down
56 changes: 41 additions & 15 deletions scripts/guard-no-release-merge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,75 @@
#
# 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 <pr-number>"
exit 2
fi
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..."

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_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
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_view labels '.labels[].name' 2>/dev/null || true)
body=$(gh_view body '.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 label 'autorelease: pending'"
elif echo "$title" | grep -qiE '^(chore|release).*release |release v?[0-9]+\.[0-9]+'; then
reason="has an autorelease label"
elif [[ "$title" =~ $release_title_re ]]; then
is_release=true
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 ""
Expand Down
100 changes: 100 additions & 0 deletions scripts/guard-no-release-merge.test.sh
Original file line number Diff line number Diff line change
@@ -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"
27 changes: 25 additions & 2 deletions test/unit/patchloomCli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>((resolve) => {
child.once("exit", () => resolve());
child.kill();
});
}

// Verify the tool call succeeded
const lines = stdout.trim().split("\n");
Expand All @@ -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<string, unknown>;
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
Expand Down
Loading