pr-shepherd 2.2.10 → 2.2.11
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.
- package/.agents/plugins/marketplace.json +2 -2
- package/.claude-plugin/marketplace.json +2 -2
- package/.claude-plugin/plugin.json +1 -1
- package/.grok-plugin/marketplace.json +2 -2
- package/bin/cli/help-command-pages.d.mts +2 -2
- package/bin/cli/help-iterate-poll-pages.d.mts +2 -2
- package/bin/cli/help-iterate-poll-pages.mjs +7 -0
- package/bin/cli/help-top-page.d.mts +1 -1
- package/bin/cli/help-top-page.mjs +1 -0
- package/bin/cli/help.d.mts +3 -3
- package/bin/cli/iterate-instructions.mjs +7 -4
- package/bin/cli/poll-handler.mjs +49 -67
- package/bin/cli/poll-loop-options.d.mts +25 -0
- package/bin/cli/poll-loop-options.mjs +58 -0
- package/bin/cli/poll-watch.d.mts +27 -0
- package/bin/cli/poll-watch.mjs +48 -0
- package/bin/cli/watch-report.d.mts +17 -0
- package/bin/cli/watch-report.mjs +33 -0
- package/bin/commands/iterate/api-usage.mjs +16 -1
- package/bin/commands/iterate/gh-cache.d.mts +12 -0
- package/bin/commands/iterate/gh-cache.mjs +98 -0
- package/bin/commands/iterate/merge-plan.d.mts +1 -1
- package/bin/commands/poll-options.d.mts +3 -0
- package/bin/commands/poll-summary.d.mts +3 -0
- package/bin/commands/poll-summary.mjs +5 -1
- package/bin/commands/poll-watch-verify.d.mts +6 -0
- package/bin/commands/poll-watch-verify.mjs +16 -0
- package/bin/commands/poll.mjs +3 -3
- package/bin/exit-codes.d.mts +2 -0
- package/bin/exit-codes.mjs +2 -0
- package/bin/state/process-start.d.mts +14 -0
- package/bin/state/process-start.mjs +35 -0
- package/bin/state/watch-lock.d.mts +29 -0
- package/bin/state/watch-lock.mjs +145 -0
- package/bin/state/watch-set.d.mts +23 -0
- package/bin/state/watch-set.mjs +58 -0
- package/bin/types/merge-action.d.mts +8 -0
- package/package.json +1 -1
- package/plugins/pr-shepherd/.claude-plugin/plugin.json +1 -1
- package/plugins/pr-shepherd/.codex-plugin/plugin.json +1 -1
- package/plugins/pr-shepherd/hooks/pipe-guard-parse.d.mts +4 -0
- package/plugins/pr-shepherd/hooks/pipe-guard-parse.mjs +142 -0
- package/plugins/pr-shepherd/hooks/poll-guard.d.mts +1 -0
- package/plugins/pr-shepherd/hooks/poll-guard.mjs +23 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { EXIT, ShepherdError } from "../exit-codes.mjs";
|
|
2
|
+
import { acquireWatchLock, verifyWatchLock, releaseWatchLock, } from "./watch-lock.mjs";
|
|
3
|
+
/** Thrown when a PR a run needs turns out to be watched by another live process. */
|
|
4
|
+
export class WatchedError extends ShepherdError {
|
|
5
|
+
repo;
|
|
6
|
+
holders;
|
|
7
|
+
constructor(repo, holders) {
|
|
8
|
+
super("PR is already watched by another pr-shepherd process", EXIT.WATCHED);
|
|
9
|
+
this.name = "WatchedError";
|
|
10
|
+
this.repo = repo;
|
|
11
|
+
this.holders = holders;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function createWatchSet(repo, flags) {
|
|
15
|
+
const held = new Set();
|
|
16
|
+
const release = (pr) => {
|
|
17
|
+
held.delete(pr);
|
|
18
|
+
releaseWatchLock(repo, pr);
|
|
19
|
+
};
|
|
20
|
+
const claim = (prs) => {
|
|
21
|
+
const blocked = [];
|
|
22
|
+
for (const pr of prs.filter((candidate) => !held.has(candidate))) {
|
|
23
|
+
const result = acquireWatchLock(repo, pr, flags);
|
|
24
|
+
if (result.acquired)
|
|
25
|
+
held.add(pr);
|
|
26
|
+
else
|
|
27
|
+
blocked.push(result.holder);
|
|
28
|
+
}
|
|
29
|
+
return blocked;
|
|
30
|
+
};
|
|
31
|
+
return {
|
|
32
|
+
repo,
|
|
33
|
+
claim,
|
|
34
|
+
sync(rows) {
|
|
35
|
+
for (const row of rows)
|
|
36
|
+
if (row.action === "cancel" && held.has(row.pr))
|
|
37
|
+
release(row.pr);
|
|
38
|
+
const blocked = claim(rows.filter((row) => row.action !== "cancel").map((row) => row.pr));
|
|
39
|
+
if (blocked.length > 0)
|
|
40
|
+
throw new WatchedError(repo, blocked);
|
|
41
|
+
},
|
|
42
|
+
verify() {
|
|
43
|
+
const lost = [];
|
|
44
|
+
for (const pr of held) {
|
|
45
|
+
const result = verifyWatchLock(repo, pr, flags);
|
|
46
|
+
if (result.acquired)
|
|
47
|
+
continue;
|
|
48
|
+
held.delete(pr);
|
|
49
|
+
lost.push(result.holder);
|
|
50
|
+
}
|
|
51
|
+
return lost;
|
|
52
|
+
},
|
|
53
|
+
releaseAll() {
|
|
54
|
+
for (const pr of held)
|
|
55
|
+
release(pr);
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
}
|
|
@@ -14,5 +14,13 @@ export interface MergeCommandPlan {
|
|
|
14
14
|
* `apply merge --transport rest` for a queue.
|
|
15
15
|
*/
|
|
16
16
|
restFallbackCommand?: ShellCommand;
|
|
17
|
+
/**
|
|
18
|
+
* gh's 24h HTTP cache holds a stale GraphQL rate-limit error that makes `gh pr merge` fail while
|
|
19
|
+
* Shepherd's live GraphQL quota is healthy. Present only when found; delete `paths` first.
|
|
20
|
+
*/
|
|
21
|
+
ghCacheStaleRateLimit?: {
|
|
22
|
+
paths: string[];
|
|
23
|
+
expiresAt: string;
|
|
24
|
+
};
|
|
17
25
|
}
|
|
18
26
|
export {};
|
package/package.json
CHANGED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare function isBalanced(text: string): boolean;
|
|
2
|
+
export declare function splitCommands(masked: string): { text: string; term: "pipe" | "flow" }[];
|
|
3
|
+
export declare function runsShepherd(words: string[]): boolean;
|
|
4
|
+
export declare function pipesShepherd(command: unknown): boolean;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
// A small, conservative shell reader for the pipe guard. It answers one question:
|
|
2
|
+
// is a pr-shepherd command's stdout certainly piped into another command? Anything
|
|
3
|
+
// it cannot read with confidence (unbalanced quotes, odd syntax) answers "no".
|
|
4
|
+
|
|
5
|
+
import { maskShell } from "./poll-guard-shell.mjs";
|
|
6
|
+
|
|
7
|
+
const ASSIGNMENT = /^[A-Za-z_]\w*=/;
|
|
8
|
+
const LEADING_KEYWORDS = new Set([
|
|
9
|
+
"!",
|
|
10
|
+
"time",
|
|
11
|
+
"then",
|
|
12
|
+
"do",
|
|
13
|
+
"else",
|
|
14
|
+
"elif",
|
|
15
|
+
"if",
|
|
16
|
+
"while",
|
|
17
|
+
"until",
|
|
18
|
+
]);
|
|
19
|
+
const FLOW = "flow";
|
|
20
|
+
const PIPE = "pipe";
|
|
21
|
+
|
|
22
|
+
/** True when every quote, backtick, and `$(…)` in `text` is closed. Heredoc text may defeat it, which only allows. */
|
|
23
|
+
export function isBalanced(text) {
|
|
24
|
+
const stack = [];
|
|
25
|
+
for (let i = 0; i < text.length; i++) {
|
|
26
|
+
const c = text[i];
|
|
27
|
+
const top = stack.at(-1);
|
|
28
|
+
if (c === "\\") i++;
|
|
29
|
+
else if (top === "d") {
|
|
30
|
+
if (c === '"') stack.pop();
|
|
31
|
+
else if (c === "`") stack.push("b");
|
|
32
|
+
else if (c === "$" && text[i + 1] === "(") {
|
|
33
|
+
stack.push("p");
|
|
34
|
+
i++;
|
|
35
|
+
}
|
|
36
|
+
} else if (c === "'") {
|
|
37
|
+
const end = singleQuoteEnd(text, i);
|
|
38
|
+
if (end === -1) return false;
|
|
39
|
+
i = end;
|
|
40
|
+
} else if (c === "$" && text[i + 1] === "'") {
|
|
41
|
+
const end = singleQuoteEnd(text, i + 1, true);
|
|
42
|
+
if (end === -1) return false;
|
|
43
|
+
i = end;
|
|
44
|
+
} else if (c === '"') stack.push("d");
|
|
45
|
+
else if (c === "`") {
|
|
46
|
+
if (top === "b") stack.pop();
|
|
47
|
+
else stack.push("b");
|
|
48
|
+
} else if (c === "(") stack.push("p");
|
|
49
|
+
else if (c === ")") {
|
|
50
|
+
if (top === "p") stack.pop();
|
|
51
|
+
} else if (c === "#" && (i === 0 || /[\s;&|(]/.test(text[i - 1]))) {
|
|
52
|
+
const end = text.indexOf("\n", i);
|
|
53
|
+
if (end === -1) break;
|
|
54
|
+
i = end;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return stack.length === 0;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Index of the quote closing the one at `from`, or -1. `$'…'` honors backslash escapes. */
|
|
61
|
+
function singleQuoteEnd(text, from, escapes = false) {
|
|
62
|
+
for (let j = from + 1; j < text.length; j++) {
|
|
63
|
+
if (escapes && text[j] === "\\") j++;
|
|
64
|
+
else if (text[j] === "'") return j;
|
|
65
|
+
}
|
|
66
|
+
return -1;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Splits masked text into simple commands. Each is `{ text, term }`, where `term` is
|
|
71
|
+
* "pipe" when a single `|` or `|&` follows it directly and "flow" for any other
|
|
72
|
+
* boundary. Redirections such as `2>&1`, `&>f`, and `>|f` are not boundaries.
|
|
73
|
+
*/
|
|
74
|
+
export function splitCommands(masked) {
|
|
75
|
+
const parts = [];
|
|
76
|
+
let start = 0;
|
|
77
|
+
const cut = (at, term, width = 1) => {
|
|
78
|
+
parts.push({ text: masked.slice(start, at), term });
|
|
79
|
+
start = at + width;
|
|
80
|
+
};
|
|
81
|
+
for (let i = 0; i < masked.length; i++) {
|
|
82
|
+
const c = masked[i];
|
|
83
|
+
const prev = masked[i - 1];
|
|
84
|
+
const next = masked[i + 1];
|
|
85
|
+
if (c === ";" || c === "\n" || c === "(" || c === ")" || c === "`") cut(i, FLOW);
|
|
86
|
+
else if (c === "&") {
|
|
87
|
+
if (next === "&") {
|
|
88
|
+
cut(i, FLOW, 2);
|
|
89
|
+
i++;
|
|
90
|
+
} else if (prev !== "<" && prev !== ">" && next !== ">") cut(i, FLOW);
|
|
91
|
+
} else if (c === "|" && prev !== ">") {
|
|
92
|
+
if (next === "|") {
|
|
93
|
+
cut(i, FLOW, 2);
|
|
94
|
+
i++;
|
|
95
|
+
} else if (next === "&") {
|
|
96
|
+
cut(i, PIPE, 2);
|
|
97
|
+
i++;
|
|
98
|
+
} else cut(i, PIPE);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
parts.push({ text: masked.slice(start), term: FLOW });
|
|
102
|
+
return parts;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** The command's words after leading keywords and `VAR=value` assignments. */
|
|
106
|
+
function commandWords(text) {
|
|
107
|
+
const words = text.split(/\s+/).filter(Boolean);
|
|
108
|
+
while (words.length > 0 && (ASSIGNMENT.test(words[0]) || LEADING_KEYWORDS.has(words[0]))) {
|
|
109
|
+
words.shift();
|
|
110
|
+
}
|
|
111
|
+
return words;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const isShepherd = (word) => /^pr-shepherd(?:@\S*)?$/.test(word);
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Whether the words run pr-shepherd: `pr-shepherd`, a path ending `/pr-shepherd`,
|
|
118
|
+
* or `npx [-flags] pr-shepherd` / `pnpm exec [-flags] pr-shepherd`.
|
|
119
|
+
*/
|
|
120
|
+
export function runsShepherd(words) {
|
|
121
|
+
const [first, ...rest] = words;
|
|
122
|
+
if (first === undefined) return false;
|
|
123
|
+
if (first === "pr-shepherd" || first.endsWith("/pr-shepherd")) return true;
|
|
124
|
+
let args;
|
|
125
|
+
if (first === "npx") args = rest;
|
|
126
|
+
else if (first === "pnpm" && rest[0] === "exec") args = rest.slice(1);
|
|
127
|
+
else return false;
|
|
128
|
+
while (args[0]?.startsWith("-")) args.shift();
|
|
129
|
+
return args[0] !== undefined && isShepherd(args[0]);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** True when a pr-shepherd invocation in `command` is certainly followed by a pipe. */
|
|
133
|
+
export function pipesShepherd(command) {
|
|
134
|
+
if (typeof command !== "string" || !isBalanced(command)) return false;
|
|
135
|
+
// A backslash-newline joins lines. Blank it (odd backslash runs only) before masking.
|
|
136
|
+
const joined = command.replace(/(\\+)\n/g, (m, run) =>
|
|
137
|
+
run.length % 2 === 1 ? `${run.slice(0, -1)} ` : m,
|
|
138
|
+
);
|
|
139
|
+
return splitCommands(maskShell(joined)).some(
|
|
140
|
+
({ text, term }) => term === PIPE && runsShepherd(commandWords(text)),
|
|
141
|
+
);
|
|
142
|
+
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
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
|
|
3
|
+
// GitHub PR or CI state instead of running one `pr-shepherd … --until-terminal`,
|
|
4
|
+
// and always blocks a Bash command that certainly pipes pr-shepherd's output.
|
|
4
5
|
//
|
|
5
6
|
// Level: PR_SHEPHERD_POLL_GUARD, else the Claude Code plugin option `poll_guard`
|
|
6
7
|
// (CLAUDE_PLUGIN_OPTION_POLL_GUARD): `disabled`, `advisory` (default: the call
|
|
@@ -12,11 +13,32 @@ import { isAbsolute, resolve } from "node:path";
|
|
|
12
13
|
import { pathToFileURL } from "node:url";
|
|
13
14
|
|
|
14
15
|
import { detectPoller, expandPath, guardMessage, readLevel } from "./poll-guard-detect.mjs";
|
|
16
|
+
import { pipesShepherd } from "./pipe-guard-parse.mjs";
|
|
17
|
+
|
|
18
|
+
export const PIPE_DENY_REASON =
|
|
19
|
+
"Piping pr-shepherd output drops its `## Instructions` section and the IDs you must act on; run pr-shepherd without a pipe and read the full output.";
|
|
15
20
|
|
|
16
21
|
const MAX_SCRIPT_BYTES = 64 * 1024;
|
|
17
22
|
|
|
18
23
|
/** Decides the hook result for one tool call. Returns { stdout, stderr, code }. */
|
|
19
24
|
export function runGuard(input, env, readText = readSmallFile) {
|
|
25
|
+
// A piped pr-shepherd always denies, whatever the poll level. The substring check
|
|
26
|
+
// is the cheap prefilter: most Bash commands mention neither `pr-shepherd` nor `|`.
|
|
27
|
+
const text = input?.tool_input?.command;
|
|
28
|
+
if (
|
|
29
|
+
input?.tool_name === "Bash" &&
|
|
30
|
+
typeof text === "string" &&
|
|
31
|
+
text.includes("pr-shepherd") &&
|
|
32
|
+
text.includes("|") &&
|
|
33
|
+
pipesShepherd(text)
|
|
34
|
+
) {
|
|
35
|
+
const hookSpecificOutput = {
|
|
36
|
+
hookEventName: "PreToolUse",
|
|
37
|
+
permissionDecision: "deny",
|
|
38
|
+
permissionDecisionReason: PIPE_DENY_REASON,
|
|
39
|
+
};
|
|
40
|
+
return { stdout: `${JSON.stringify({ hookSpecificOutput })}\n`, stderr: "", code: 0 };
|
|
41
|
+
}
|
|
20
42
|
const { level, error } = readLevel({
|
|
21
43
|
PR_SHEPHERD_POLL_GUARD: env.PR_SHEPHERD_POLL_GUARD || env.CLAUDE_PLUGIN_OPTION_POLL_GUARD,
|
|
22
44
|
});
|