pr-shepherd 1.3.0 → 1.5.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.
Files changed (48) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/README.md +1 -0
  3. package/bin/api.mjs +4 -1
  4. package/bin/cli/api-usage-formatter.mjs +18 -8
  5. package/bin/cli/help-command-pages.d.mts +2 -2
  6. package/bin/cli/help-iterate-poll-pages.d.mts +2 -2
  7. package/bin/cli/help-iterate-poll-pages.mjs +3 -2
  8. package/bin/cli/help.d.mts +2 -2
  9. package/bin/cli-parser.mjs +5 -2
  10. package/bin/commands/poll-summary-explicit-instructions.mjs +1 -1
  11. package/bin/commands/tick.mjs +1 -0
  12. package/bin/github/api-telemetry-adopt.d.mts +4 -0
  13. package/bin/github/api-telemetry-adopt.mjs +52 -0
  14. package/bin/github/api-telemetry-aggregate.d.mts +6 -2
  15. package/bin/github/api-telemetry-aggregate.mjs +19 -52
  16. package/bin/github/api-telemetry-summary.d.mts +4 -0
  17. package/bin/github/api-telemetry-summary.mjs +63 -0
  18. package/bin/github/api-telemetry.d.mts +2 -0
  19. package/bin/github/api-telemetry.mjs +2 -43
  20. package/bin/github/app-eligibility.d.mts +9 -0
  21. package/bin/github/app-eligibility.mjs +21 -0
  22. package/bin/github/app-quota.d.mts +10 -0
  23. package/bin/github/app-quota.mjs +44 -0
  24. package/bin/github/auth-target.d.mts +16 -0
  25. package/bin/github/auth-target.mjs +29 -0
  26. package/bin/github/client.mjs +4 -2
  27. package/bin/github/github-app-config.d.mts +12 -0
  28. package/bin/github/github-app-config.mjs +102 -0
  29. package/bin/github/github-app-token.d.mts +13 -0
  30. package/bin/github/github-app-token.mjs +107 -0
  31. package/bin/github/graphql-http.mjs +18 -2
  32. package/bin/github/http-auth.d.mts +15 -2
  33. package/bin/github/http-auth.mjs +35 -4
  34. package/bin/github/http-intermediate.d.mts +1 -0
  35. package/bin/github/http-intermediate.mjs +1 -0
  36. package/bin/github/rest-http.mjs +19 -2
  37. package/bin/github/rest-text.mjs +22 -3
  38. package/bin/pr-reference.mjs +3 -0
  39. package/bin/types/api-usage.d.mts +14 -0
  40. package/package.json +1 -1
  41. package/plugins/pr-shepherd/.claude-plugin/plugin.json +1 -1
  42. package/plugins/pr-shepherd/.codex-plugin/plugin.json +1 -1
  43. package/plugins/pr-shepherd/.codex.mcp.json +1 -1
  44. package/plugins/pr-shepherd/.mcp.json +1 -1
  45. package/plugins/pr-shepherd/package-lock.json +4 -4
  46. package/plugins/pr-shepherd/package.json +1 -1
  47. package/plugins/pr-shepherd/skills/pr-shepherd/SKILL.md +2 -2
  48. package/plugins/pr-shepherd/skills/pr-shepherd/references/waiting.md +8 -0
@@ -4,6 +4,9 @@ import { formatRequestEntry, formatResponseEntry } from "../log/session.mjs";
4
4
  import { loadEtagEntry, storeEtagEntry } from "../state/rest-cache.mjs";
5
5
  import { GitHubRequestError } from "./errors.mjs";
6
6
  import { githubFetch } from "./github-fetch.mjs";
7
+ import { isAppEligibleRest } from "./app-eligibility.mjs";
8
+ import { withAppQuotaFallback } from "./app-quota.mjs";
9
+ import { authTargetFromRestPath } from "./auth-target.mjs";
7
10
  import { makeAuthHeaders } from "./http-auth.mjs";
8
11
  import { requestWithTokenRetry } from "./http-request.mjs";
9
12
  import { parseRateLimit, parseRetryAfter, redactToken, sanitizeBody, } from "./http-utils.mjs";
@@ -30,20 +33,28 @@ export async function rest(method, path, body) {
30
33
  const { data } = await restWithRateLimit(method, path, body);
31
34
  return data;
32
35
  }
33
- export async function restWithRateLimit(method, path, body, opts) {
36
+ export function restWithRateLimit(method, path, body, opts) {
37
+ return withAppQuotaFallback("core", () => restAttempt(method, path, body, opts));
38
+ }
39
+ async function restAttempt(method, path, body, opts) {
34
40
  const url = githubApiUrl(path);
35
41
  const n = nextEntry();
36
42
  appendEntry(formatRequestEntry({ n, kind: "REST", method, url, body }));
37
43
  const t0 = performance.now();
38
44
  let authSource = "unknown";
39
45
  let credentialFingerprint;
46
+ let githubApp = false;
40
47
  const cached = opts?.conditional && !opts.conditional.revalidate
41
48
  ? await loadEtagEntry(opts.conditional.key, opts.conditional.name)
42
49
  : null;
43
50
  const { res, attempt, retryT0 } = await requestWithTokenRetry(async () => {
44
- const auth = await makeAuthHeaders(cached ? { "If-None-Match": cached.etag } : undefined);
51
+ const auth = await makeAuthHeaders(cached ? { "If-None-Match": cached.etag } : undefined, {
52
+ target: authTargetFromRestPath(path),
53
+ appEligible: isAppEligibleRest(method, path),
54
+ });
45
55
  authSource = auth.source;
46
56
  credentialFingerprint = auth.fingerprint;
57
+ githubApp = auth.githubApp;
47
58
  return githubFetch(url, {
48
59
  method,
49
60
  headers: auth.headers,
@@ -58,6 +69,7 @@ export async function restWithRateLimit(method, path, body, opts) {
58
69
  durationMs,
59
70
  authSource,
60
71
  credentialFingerprint,
72
+ githubApp,
61
73
  }), method === "GET" || method === "HEAD");
62
74
  const durationMs = Math.round(performance.now() - retryT0);
63
75
  const rateLimit = parseRateLimit(res.headers) ?? undefined;
@@ -84,6 +96,7 @@ export async function restWithRateLimit(method, path, body, opts) {
84
96
  method,
85
97
  authSource,
86
98
  credentialFingerprint,
99
+ githubApp,
87
100
  rateLimit,
88
101
  notModified: true,
89
102
  });
@@ -116,6 +129,7 @@ export async function restWithRateLimit(method, path, body, opts) {
116
129
  method,
117
130
  authSource,
118
131
  credentialFingerprint,
132
+ githubApp,
119
133
  rateLimit,
120
134
  });
121
135
  const responseMessage = isRestSessionRefusalResponse(res.status, text)
@@ -155,6 +169,7 @@ export async function restWithRateLimit(method, path, body, opts) {
155
169
  method,
156
170
  authSource,
157
171
  credentialFingerprint,
172
+ githubApp,
158
173
  rateLimit,
159
174
  });
160
175
  if (!res.ok) {
@@ -187,6 +202,7 @@ export async function restWithRateLimit(method, path, body, opts) {
187
202
  method,
188
203
  authSource,
189
204
  credentialFingerprint,
205
+ githubApp,
190
206
  rateLimit,
191
207
  });
192
208
  const etag = opts?.conditional ? (res.headers.get("etag") ?? undefined) : undefined;
@@ -227,6 +243,7 @@ export async function restWithRateLimit(method, path, body, opts) {
227
243
  method,
228
244
  authSource,
229
245
  credentialFingerprint,
246
+ githubApp,
230
247
  rateLimit,
231
248
  });
232
249
  return { data: undefined, rateLimit, status: res.status };
@@ -3,23 +3,34 @@ import { formatRequestEntry, formatResponseEntry } from "../log/session.mjs";
3
3
  import { GitHubRequestError } from "./errors.mjs";
4
4
  import { githubFetch } from "./github-fetch.mjs";
5
5
  import { GITHUB_LOG_TIMEOUT_MS } from "./github-timeouts.mjs";
6
+ import { isAppEligibleRest } from "./app-eligibility.mjs";
7
+ import { withAppQuotaFallback } from "./app-quota.mjs";
8
+ import { authTargetFromRestPath } from "./auth-target.mjs";
6
9
  import { makeAuthHeaders } from "./http-auth.mjs";
7
10
  import { requestWithTokenRetry } from "./http-request.mjs";
8
11
  import { parseRateLimit, parseRetryAfter, redactUrl, sanitizeBody, } from "./http-utils.mjs";
9
12
  import { recordApiTelemetry } from "./api-telemetry.mjs";
10
13
  import { recordIntermediateResponse } from "./http-intermediate.mjs";
11
14
  const BASE_URL = "https://api.github.com";
12
- export async function restText(path, onRateLimit) {
15
+ export function restText(path, onRateLimit) {
16
+ return withAppQuotaFallback("core", () => restTextAttempt(path, onRateLimit));
17
+ }
18
+ async function restTextAttempt(path, onRateLimit) {
13
19
  const url = `${BASE_URL}${path}`;
14
20
  const n = nextEntry();
15
21
  appendEntry(formatRequestEntry({ n, kind: "restText", method: "GET", url }));
16
22
  const t0 = performance.now();
17
23
  let authSource = "unknown";
18
24
  let credentialFingerprint;
25
+ let githubApp = false;
19
26
  const { res, attempt, retryT0 } = await requestWithTokenRetry(async () => {
20
- const auth = await makeAuthHeaders();
27
+ const auth = await makeAuthHeaders(undefined, {
28
+ target: authTargetFromRestPath(path),
29
+ appEligible: isAppEligibleRest("GET", path),
30
+ });
21
31
  authSource = auth.source;
22
32
  credentialFingerprint = auth.fingerprint;
33
+ githubApp = auth.githubApp;
23
34
  return githubFetch(url, { method: "GET", headers: auth.headers, redirect: "manual" }, GITHUB_LOG_TIMEOUT_MS);
24
35
  }, t0, (response, durationMs) => recordIntermediateResponse({
25
36
  n,
@@ -30,12 +41,20 @@ export async function restText(path, onRateLimit) {
30
41
  durationMs,
31
42
  authSource,
32
43
  credentialFingerprint,
44
+ githubApp,
33
45
  }));
34
46
  const durationMs = Math.round(performance.now() - retryT0);
35
47
  const rateLimit = parseRateLimit(res.headers) ?? undefined;
36
48
  onRateLimit?.(rateLimit);
37
49
  const retryAfterSeconds = parseRetryAfter(res.headers);
38
- recordApiTelemetry({ kind: "REST", method: "GET", authSource, credentialFingerprint, rateLimit });
50
+ recordApiTelemetry({
51
+ kind: "REST",
52
+ method: "GET",
53
+ authSource,
54
+ credentialFingerprint,
55
+ githubApp,
56
+ rateLimit,
57
+ });
39
58
  if ([301, 302, 307, 308].includes(res.status)) {
40
59
  const redirected = await followRestTextRedirect(res, {
41
60
  n,
@@ -1,3 +1,4 @@
1
+ import { setAuthTarget } from "./github/auth-target.mjs";
1
2
  /** Parses API PR references without performing repository or GitHub I/O. */
2
3
  export function parsePrReference(pr) {
3
4
  if (pr === undefined)
@@ -42,6 +43,8 @@ export function resolveParsedPrTarget(parsed) {
42
43
  const [owner, name] = parsed.repository.split("/");
43
44
  if (!owner || !name)
44
45
  throw new Error(`Invalid repository reference: ${parsed.repository}`);
46
+ // A repository the caller named selects the credential for the whole command.
47
+ setAuthTarget({ owner, name }, "explicit");
45
48
  return {
46
49
  prNumber: parsed.number,
47
50
  targetRepository: { owner, name },
@@ -28,7 +28,19 @@ export interface QuotaWarningBudget {
28
28
  used?: number;
29
29
  resetAt: number;
30
30
  }
31
+ /** One GitHub App installation's usage, accounted separately from the user's token chain. */
32
+ export interface GithubAppApiUsage {
33
+ /** `GitHub App <appId> (installation <id>)`. */
34
+ source: string;
35
+ graphql?: GraphqlApiUsage;
36
+ rest?: ApiResourceUsage[];
37
+ }
38
+ /**
39
+ * Top-level fields describe only the user's token chain (non-App requests); quota state and
40
+ * warnings are driven by these alone.
41
+ */
31
42
  export interface ApiUsage {
43
+ /** User-chain credential sources; empty when only a GitHub App answered. */
32
44
  credentialSources: string[];
33
45
  graphql?: GraphqlApiUsage;
34
46
  rest?: ApiResourceUsage[];
@@ -37,6 +49,8 @@ export interface ApiUsage {
37
49
  * Code proxy's `ccr` routes). Omitted when 0.
38
50
  */
39
51
  unmeteredRestRequestCount?: number;
52
+ /** One entry per GitHub App installation used, in order of first use. Omitted when none. */
53
+ githubApps?: GithubAppApiUsage[];
40
54
  }
41
55
  export interface GraphqlQuotaWarning {
42
56
  resource: "graphql" | "core" | "combined";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pr-shepherd",
3
- "version": "1.3.0",
3
+ "version": "1.5.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.3.0",
4
+ "version": "1.5.0",
5
5
  "author": {
6
6
  "name": "Jonathan Ong",
7
7
  "email": "jonathanrichardong@gmail.com"
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pr-shepherd",
3
- "version": "1.3.0",
3
+ "version": "1.5.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.3.0", "pr-shepherd-mcp"]
5
+ "args": ["--yes", "--package", "pr-shepherd@1.5.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.3.0", "pr-shepherd-mcp"]
4
+ "args": ["--yes", "--package", "pr-shepherd@1.5.0", "pr-shepherd-mcp"]
5
5
  }
6
6
  }
@@ -7,7 +7,7 @@
7
7
  "name": "pr-shepherd-plugin",
8
8
  "license": "MIT",
9
9
  "dependencies": {
10
- "pr-shepherd": "1.1.2"
10
+ "pr-shepherd": "1.4.0"
11
11
  }
12
12
  },
13
13
  "node_modules/@hono/node-server": {
@@ -889,9 +889,9 @@
889
889
  }
890
890
  },
891
891
  "node_modules/pr-shepherd": {
892
- "version": "1.1.2",
893
- "resolved": "https://registry.npmjs.org/pr-shepherd/-/pr-shepherd-1.1.2.tgz",
894
- "integrity": "sha512-3UxX0r4kOkjxKteSRfM3QrE8Ty0FDA3qd8ZEjcikxc6lv8Eb/Y9td+HnmCtuSXBJjSb3MyC5s+83jk1MJty1bQ==",
892
+ "version": "1.4.0",
893
+ "resolved": "https://registry.npmjs.org/pr-shepherd/-/pr-shepherd-1.4.0.tgz",
894
+ "integrity": "sha512-3eYsELFSn/tR1fPUFjYVm1zc5w7GfhxcDUvhALvhtdipT2T1ynyCr9lwAc2eX8G/32FMltpsC0YQTuV/gDulQw==",
895
895
  "license": "MIT",
896
896
  "dependencies": {
897
897
  "@modelcontextprotocol/sdk": "^1.30.0",
@@ -4,6 +4,6 @@
4
4
  "description": "Pins the pr-shepherd package that .mcp.json launches",
5
5
  "license": "MIT",
6
6
  "dependencies": {
7
- "pr-shepherd": "1.1.2"
7
+ "pr-shepherd": "1.4.0"
8
8
  }
9
9
  }
@@ -15,7 +15,7 @@ allowed-tools: mcp__plugin_pr-shepherd_pr-shepherd, Bash(npx pr-shepherd:*), Bas
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
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.
18
+ - Run `pr-shepherd [PR ...]` or `pr-shepherd stack PR`, plus `--merge` when requested; add `--until-terminal` in the background only if its exit wakes you, else in the foreground ([Waiting](references/waiting.md)); 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
 
@@ -24,7 +24,7 @@ allowed-tools: mcp__plugin_pr-shepherd_pr-shepherd, Bash(npx pr-shepherd:*), Bas
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
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.
27
+ - Shepherd is the only poller. Never write a loop, watcher, or background job around `gh`, `gh api`, `curl`, `sleep`, or `pr-shepherd` (except a backgrounded `--until-terminal`), 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
28
  - If Shepherd cannot do what was asked, tell the user what is missing instead of building a substitute.
29
29
 
30
30
  ### Untrusted review input
@@ -0,0 +1,8 @@
1
+ # Waiting
2
+
3
+ Apply when picking how to wait for Shepherd. Choose by what your host does when a command exits.
4
+
5
+ 1. A background command's exit wakes you: run `pr-shepherd [PR ...] --until-terminal` (plus `--merge` when requested) as a background command, end the turn, and act on its output when it exits. Do not read or poll its output before then. Exit 10–16 is a Shepherd action, not a crash: read the output. Exit 64 and above is a command failure (75 is retryable): handle it as one.
6
+ 2. No wake-up on exit (a foreground subagent, for example): run the bounded `pr-shepherd [PR ...]` in the foreground. If your command-time limit is under the default 270s, pass a smaller `--timeout`, and set the tool's own timeout above the poll when it allows. On a non-terminal `WAIT` or `READY`, rerun the same command. Never background it and check its output on a loop.
7
+ 3. Single ticks (cloud sessions, MCP, `iterate`): follow the "Event loop" playbook.
8
+ 4. One watcher per selection: before starting Shepherd with different PRs or flags (such as adding `--merge`), stop the running one. Never run two on the same PR.