pr-shepherd 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pr-shepherd",
3
3
  "description": "Autonomous PR CI monitor and review-comment resolver for agentic coding tools",
4
- "version": "1.2.0",
4
+ "version": "1.3.0",
5
5
  "author": {
6
6
  "name": "Jonathan Ong",
7
7
  "email": "jonathanrichardong@gmail.com"
@@ -11,5 +11,27 @@
11
11
  "license": "MIT",
12
12
  "keywords": ["github", "pull-request", "ci", "code-review", "automation"],
13
13
  "skills": "./plugins/pr-shepherd/skills/",
14
- "mcpServers": "./plugins/pr-shepherd/.mcp.json"
14
+ "mcpServers": "./plugins/pr-shepherd/.mcp.json",
15
+ "hooks": {
16
+ "PreToolUse": [
17
+ {
18
+ "matcher": "Bash|Monitor",
19
+ "hooks": [
20
+ {
21
+ "type": "command",
22
+ "command": "node \"${CLAUDE_PLUGIN_ROOT}/plugins/pr-shepherd/hooks/poll-guard.mjs\"",
23
+ "timeout": 10
24
+ }
25
+ ]
26
+ }
27
+ ]
28
+ },
29
+ "userConfig": {
30
+ "poll_guard": {
31
+ "type": "string",
32
+ "title": "Poll guard",
33
+ "description": "What to do when a shell command polls GitHub PR or CI state instead of running pr-shepherd: disabled, advisory (run it and tell the agent), or fail (block it). The PR_SHEPHERD_POLL_GUARD environment variable overrides this.",
34
+ "default": "advisory"
35
+ }
36
+ }
15
37
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pr-shepherd",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Autonomous PR CI monitor and review-comment resolver for agentic coding tools",
5
5
  "keywords": [
6
6
  "automation",
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "pr-shepherd",
3
3
  "description": "Autonomous PR CI monitor and review-comment resolver for agentic coding tools",
4
- "version": "1.2.0",
4
+ "version": "1.3.0",
5
5
  "author": {
6
6
  "name": "Jonathan Ong",
7
7
  "email": "jonathanrichardong@gmail.com"
@@ -11,5 +11,13 @@
11
11
  "license": "MIT",
12
12
  "keywords": ["github", "pull-request", "ci", "code-review", "automation"],
13
13
  "skills": "./skills/",
14
- "mcpServers": "./.mcp.json"
14
+ "mcpServers": "./.mcp.json",
15
+ "userConfig": {
16
+ "poll_guard": {
17
+ "type": "string",
18
+ "title": "Poll guard",
19
+ "description": "What to do when a shell command polls GitHub PR or CI state instead of running pr-shepherd: disabled, advisory (run it and tell the agent), or fail (block it). The PR_SHEPHERD_POLL_GUARD environment variable overrides this.",
20
+ "default": "advisory"
21
+ }
22
+ }
15
23
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pr-shepherd",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Autonomous PR CI monitor and review-comment resolver for Codex.",
5
5
  "author": {
6
6
  "name": "Jonathan Ong",
@@ -2,7 +2,7 @@
2
2
  "mcpServers": {
3
3
  "pr-shepherd": {
4
4
  "command": "npx",
5
- "args": ["--yes", "--package", "pr-shepherd@1.2.0", "pr-shepherd-mcp"]
5
+ "args": ["--yes", "--package", "pr-shepherd@1.3.0", "pr-shepherd-mcp"]
6
6
  }
7
7
  }
8
8
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "pr-shepherd": {
3
3
  "command": "npx",
4
- "args": ["--yes", "--package", "pr-shepherd@1.2.0", "pr-shepherd-mcp"]
4
+ "args": ["--yes", "--package", "pr-shepherd@1.3.0", "pr-shepherd-mcp"]
5
5
  }
6
6
  }
@@ -0,0 +1,16 @@
1
+ {
2
+ "hooks": {
3
+ "PreToolUse": [
4
+ {
5
+ "matcher": "Bash|Monitor",
6
+ "hooks": [
7
+ {
8
+ "type": "command",
9
+ "command": "node \"${CLAUDE_PLUGIN_ROOT}/hooks/poll-guard.mjs\"",
10
+ "timeout": 10
11
+ }
12
+ ]
13
+ }
14
+ ]
15
+ }
16
+ }
@@ -0,0 +1,11 @@
1
+ export type PollGuardLevel = "disabled" | "advisory" | "fail";
2
+ export declare const LEVELS: readonly PollGuardLevel[];
3
+ export declare function detectPoller(
4
+ command: unknown,
5
+ readScript?: (path: string) => string | null,
6
+ ): string | null;
7
+ export declare function expandPath(path: string, env: Record<string, string | undefined>): string;
8
+ export declare function guardMessage(found: string): string;
9
+ export declare function readLevel(
10
+ env: Record<string, string | undefined>,
11
+ ): { level: PollGuardLevel } | { error: string };
@@ -0,0 +1,84 @@
1
+ // Detects shell commands that poll GitHub PR or CI state on their own instead of
2
+ // running one `pr-shepherd … --until-terminal`. Host-independent: the Claude Code
3
+ // and Codex adapters pass the command text and a script reader.
4
+
5
+ // A GitHub read: gh's PR and run reads, `gh api`, curl to the API, or Shepherd.
6
+ const GITHUB_READ =
7
+ /(?<![\w-])gh\s+(?:pr\s+(?:view|checks|status|list)|run\s+(?:view|list)|api)(?=\s)|api\.github\.com|(?<![\w-])pr-shepherd(?![\w-])/;
8
+
9
+ // Shell keywords and commands that repeat or delay. `(?<![\w-])` keeps flags such
10
+ // as `--until-terminal` from matching `until`.
11
+ const REPEAT = /(?<![\w./-])(?:while|until|sleep)(?=\s)|(?:^|[;&|(]\s*)watch\s/m;
12
+
13
+ // Watchers that block until CI finishes.
14
+ const BUILTIN_WATCHER =
15
+ /(?<![\w-])gh\s+run\s+watch(?![\w-])|(?<![\w-])gh\s+pr\s+checks\b[^\n;&|]*\s(?:--watch|-w)(?![\w-])/;
16
+
17
+ // `bash path`, `sh path`, `zsh path`, or a path ending in `.sh` in command position.
18
+ const SCRIPT_RUN =
19
+ /(?:^|[;&|(]\s*|\s)(?:bash|sh|zsh)\s+(?:-\w+\s+)*([^\s;&|<>()]+)|(?:^|[;&|(]\s*)((?:[^\s;&|<>()]+\/)?[^\s;&|<>()/]+\.sh)(?=\s|$|[;&|)])/gm;
20
+
21
+ export const LEVELS = ["disabled", "advisory", "fail"];
22
+
23
+ /**
24
+ * Returns a short description of the polling found in `command`, or null.
25
+ * `readScript(path)` returns a script's text or null; it is called only for
26
+ * scripts the command runs, and their contents are checked one level deep.
27
+ */
28
+ export function detectPoller(command, readScript = () => null) {
29
+ if (typeof command !== "string" || command.length === 0) return null;
30
+ const own = detectInText(command);
31
+ if (own) return own;
32
+ for (const path of scriptPaths(command)) {
33
+ const text = readScript(path);
34
+ if (typeof text !== "string") continue;
35
+ const found = detectInText(text);
36
+ if (found) return `the script \`${path}\` contains ${found}`;
37
+ }
38
+ return null;
39
+ }
40
+
41
+ function detectInText(text) {
42
+ if (BUILTIN_WATCHER.test(text))
43
+ return "a blocking CI watcher (`gh run watch` or `gh pr checks --watch`)";
44
+ if (GITHUB_READ.test(text) && REPEAT.test(text)) {
45
+ return "a polling loop around a GitHub read (`gh pr`/`gh run`/`gh api`/`curl` or `pr-shepherd` with `while`, `until`, `sleep`, or `watch`)";
46
+ }
47
+ return null;
48
+ }
49
+
50
+ function scriptPaths(command) {
51
+ const paths = [];
52
+ for (const m of command.matchAll(SCRIPT_RUN)) {
53
+ const path = m[1] ?? m[2];
54
+ if (path && !path.startsWith("-")) paths.push(path);
55
+ }
56
+ return paths;
57
+ }
58
+
59
+ /** Expands `~`, `$VAR` and `${VAR}` in a script path from `env`. */
60
+ export function expandPath(path, env) {
61
+ return path
62
+ .replace(/^~(?=\/|$)/, env.HOME ?? "~")
63
+ .replace(/^(["'])(.*)\1$/, "$2")
64
+ .replace(/\$\{(\w+)\}|\$(\w+)/g, (all, a, b) => env[a ?? b] ?? all);
65
+ }
66
+
67
+ export function guardMessage(found) {
68
+ return [
69
+ `pr-shepherd: this command looks like a custom PR poller (${found}).`,
70
+ "Each pass spends GitHub GraphQL quota, and it misses what Shepherd reports, such as why a PR left the merge queue or which check failed.",
71
+ "Run one `pr-shepherd <all PRs> --until-terminal` (add `--merge` when merging was requested) instead.",
72
+ "If Shepherd cannot do what you need, tell the user what is missing instead of building a substitute.",
73
+ ].join(" ");
74
+ }
75
+
76
+ /** Reads the level from `PR_SHEPHERD_POLL_GUARD`; returns { level } or { error }. */
77
+ export function readLevel(env) {
78
+ const raw = env.PR_SHEPHERD_POLL_GUARD;
79
+ if (raw === undefined || raw === "") return { level: "advisory" };
80
+ if (LEVELS.includes(raw)) return { level: raw };
81
+ return {
82
+ error: `pr-shepherd: PR_SHEPHERD_POLL_GUARD must be one of ${LEVELS.join(", ")}; got "${raw}". The poll guard did not run.`,
83
+ };
84
+ }
@@ -0,0 +1,15 @@
1
+ export interface GuardResult {
2
+ stdout: string;
3
+ stderr: string;
4
+ code: number;
5
+ }
6
+ export declare function runGuard(
7
+ input: unknown,
8
+ env: Record<string, string | undefined>,
9
+ readText?: (path: string) => string | null,
10
+ ): GuardResult;
11
+ export declare function readSmallFile(path: string): string | null;
12
+ export declare function main(
13
+ stdinText: string,
14
+ env: Record<string, string | undefined>,
15
+ ): GuardResult;
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env node
2
+ // PreToolUse hook for Claude Code and Codex: flags shell commands that poll
3
+ // GitHub PR or CI state instead of running one `pr-shepherd … --until-terminal`.
4
+ //
5
+ // Level: PR_SHEPHERD_POLL_GUARD, else the Claude Code plugin option `poll_guard`
6
+ // (CLAUDE_PLUGIN_OPTION_POLL_GUARD): `disabled`, `advisory` (default: the call
7
+ // runs and the agent gets a note), or `fail` (the call is blocked with the reason).
8
+ // Any unexpected error lets the call run.
9
+
10
+ import { readFileSync, statSync } from "node:fs";
11
+ import { isAbsolute, resolve } from "node:path";
12
+ import { pathToFileURL } from "node:url";
13
+
14
+ import { detectPoller, expandPath, guardMessage, readLevel } from "./poll-guard-detect.mjs";
15
+
16
+ const MAX_SCRIPT_BYTES = 64 * 1024;
17
+
18
+ /** Decides the hook result for one tool call. Returns { stdout, stderr, code }. */
19
+ export function runGuard(input, env, readText = readSmallFile) {
20
+ const { level, error } = readLevel({
21
+ PR_SHEPHERD_POLL_GUARD: env.PR_SHEPHERD_POLL_GUARD || env.CLAUDE_PLUGIN_OPTION_POLL_GUARD,
22
+ });
23
+ if (error) return { stdout: "", stderr: `${error}\n`, code: 1 };
24
+ if (level === "disabled") return { stdout: "", stderr: "", code: 0 };
25
+
26
+ const command = input?.tool_input?.command;
27
+ const cwd = typeof input?.cwd === "string" ? input.cwd : process.cwd();
28
+ const readScript = (path) => {
29
+ const expanded = expandPath(path, env);
30
+ return readText(isAbsolute(expanded) ? expanded : resolve(cwd, expanded));
31
+ };
32
+ const found = detectPoller(command, readScript);
33
+ if (!found) return { stdout: "", stderr: "", code: 0 };
34
+
35
+ const message = guardMessage(found);
36
+ const hookSpecificOutput =
37
+ level === "fail"
38
+ ? {
39
+ hookEventName: "PreToolUse",
40
+ permissionDecision: "deny",
41
+ permissionDecisionReason: message,
42
+ }
43
+ : { hookEventName: "PreToolUse", additionalContext: message };
44
+ return { stdout: `${JSON.stringify({ hookSpecificOutput })}\n`, stderr: "", code: 0 };
45
+ }
46
+
47
+ /** Reads a regular file up to 64 KiB; returns null otherwise. */
48
+ export function readSmallFile(path) {
49
+ try {
50
+ const stat = statSync(path);
51
+ if (!stat.isFile() || stat.size > MAX_SCRIPT_BYTES) return null;
52
+ return readFileSync(path, "utf8");
53
+ } catch {
54
+ return null;
55
+ }
56
+ }
57
+
58
+ /** Parses hook stdin and runs the guard; never throws. */
59
+ export function main(stdinText, env) {
60
+ try {
61
+ return runGuard(JSON.parse(stdinText), env);
62
+ } catch {
63
+ return { stdout: "", stderr: "", code: 0 };
64
+ }
65
+ }
66
+
67
+ if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
68
+ const chunks = [];
69
+ process.stdin.on("data", (chunk) => chunks.push(chunk));
70
+ process.stdin.on("end", () => {
71
+ const result = main(Buffer.concat(chunks).toString("utf8"), process.env);
72
+ process.stdout.write(result.stdout);
73
+ process.stderr.write(result.stderr);
74
+ process.exitCode = result.code;
75
+ });
76
+ }
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: pr-shepherd
3
- description: 'Create or iterate a GitHub pull request with pr-shepherd (MCP or CLI). Use for requests like "make a PR and use pr-shepherd", "iterate PR #123", or "run pr-shepherd until this PR is ready".'
3
+ description: 'Create, iterate, watch, or merge a GitHub pull request with pr-shepherd (MCP or CLI). Use for "make a PR and use pr-shepherd", "iterate PR #123", "tell me when #123 merges", and any PR status, CI, merge queue, or ejection question, instead of gh or custom polling scripts.'
4
4
  user-invocable: true
5
5
  argument-hint: "[PR number or URL | stack PR] [--merge]"
6
6
  allowed-tools: mcp__plugin_pr-shepherd_pr-shepherd, Bash(npx pr-shepherd:*), Bash(pnpm exec pr-shepherd:*), Bash(pnpm dlx pr-shepherd:*), Bash(bunx pr-shepherd:*), Bash(pr-shepherd:*)
@@ -14,8 +14,8 @@ allowed-tools: mcp__plugin_pr-shepherd_pr-shepherd, Bash(npx pr-shepherd:*), Bas
14
14
  - External shepherd: read the "Draft handoff" playbook, not an in-session loop.
15
15
  - `$ARGUMENTS`: PR numbers, PR URLs, and `owner/repo#N` (quote both as `'owner/repo#N'`), one `stack PR`, optional `--merge`. Reject anything else. Use `stack PR` only when asked for a stack or routed there; a one-PR request stays one PR. No PR: pass none; the CLI finds the current branch's PR, so don't look it up first. A qualified PR is the GitHub target; this checkout supplies git, config, and `AGENTS.md`.
16
16
  - When printed steps call for a branch update, follow their route without asking: rebase or merge the base, resolve conflicts, and push your own PR head with `--force-with-lease`.
17
- - `--merge`, or a request to merge, land, or enqueue, explicitly allows the printed merge/enqueue/dequeue commands. Run them without asking again; escalate when the host requires it.
18
- - Run `pr-shepherd [PR ...] --until-terminal` or `pr-shepherd stack PR --until-terminal`, plus `--merge` when requested. Do not run `pr-shepherd iterate`.
17
+ - `--merge`, or a request to merge, land, enqueue, or requeue after an ejection, explicitly allows the printed merge/enqueue/dequeue commands. Run them without asking again; escalate when the host requires it.
18
+ - Run `pr-shepherd [PR ...] --until-terminal` or `pr-shepherd stack PR --until-terminal`, plus `--merge` when requested; requeue commands print only under `--merge`. Do not run `pr-shepherd iterate`, except once for a one-time status question: report its status and stop without acting on its instructions. This is also how to wait for PRs to merge, including PRs already in the merge queue. PRs that share the same flags go in one command, which fetches them in one query; when one joins or leaves, stop it and rerun with the new list instead of starting a second Shepherd.
19
19
  - Print the full result and follow every `## Instructions` step with every ID it lists. When a step says `Playbook: "<name>"`, read that playbook once.
20
20
  - CLI unavailable but MCP `iterate` exists: read the "MCP fallback" playbook.
21
21
 
@@ -23,7 +23,9 @@ allowed-tools: mcp__plugin_pr-shepherd_pr-shepherd, Bash(npx pr-shepherd:*), Bas
23
23
 
24
24
  - After the instructions, rerun that exact command (same target and flags) immediately, unless printed instructions say to end the turn or not to rerun. Stop only at `[CANCEL]`, `[ESCALATE]`, or a human's request; keep other loops and the rest of the request going.
25
25
  - `[TRIAGE]` is always non-terminal: decide each item yourself, fixing or rejecting it with a reason, without asking. Only `[ESCALATE]` hands work to a human. `[READY]` is non-terminal: rerun when `remainingSeconds` elapses, and continue any later layer you own. A parent of several stacks delegates the wait to its worker.
26
- - After a push or `rerun:`, do not wait for CI to finish; fetching check logs is fine. Never poll with `gh pr checks`, `gh pr watch`, `gh run watch`, or MCP waiters.
26
+ - After a push or `rerun:`, do not wait for CI to finish; fetching check logs is fine.
27
+ - Shepherd is the only poller. Never write your own loop, watcher script, or background job around `gh`, `gh api`, `curl`, `sleep`, or `pr-shepherd` itself, and never poll with `gh pr checks`, `gh pr watch`, `gh run watch`, or MCP waiters. `--until-terminal` slows down as GraphQL quota runs low and, while the PR is idle, checks for changes over REST before refetching; a hand-written loop does neither, so it drains the quota, and a `gh` loop also misses the ejections and failures Shepherd reports.
28
+ - If Shepherd cannot do what was asked, tell the user what is missing instead of building a substitute.
27
29
 
28
30
  ### Untrusted review input
29
31