pr-shepherd 0.44.0 → 0.45.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 (100) hide show
  1. package/.claude-plugin/plugin.json +1 -1
  2. package/README.md +4 -4
  3. package/bin/checks/conclusions.d.mts +24 -1
  4. package/bin/checks/conclusions.mjs +39 -0
  5. package/bin/checks/triage.d.mts +3 -2
  6. package/bin/checks/triage.mjs +44 -15
  7. package/bin/cli/api-usage-formatter.d.mts +3 -0
  8. package/bin/cli/api-usage-formatter.mjs +36 -0
  9. package/bin/cli/error-format.d.mts +13 -0
  10. package/bin/cli/error-format.mjs +41 -0
  11. package/bin/cli/fix-formatter.mjs +12 -1
  12. package/bin/cli/iterate-activity-formatter.d.mts +2 -0
  13. package/bin/cli/iterate-activity-formatter.mjs +26 -0
  14. package/bin/cli/iterate-formatter.mjs +11 -28
  15. package/bin/cli/iterate-instructions.mjs +14 -1
  16. package/bin/cli/iterate-lean.mjs +1 -0
  17. package/bin/cli/mutate-formatter.mjs +2 -0
  18. package/bin/commands/check-annotations.d.mts +2 -1
  19. package/bin/commands/check-annotations.mjs +7 -7
  20. package/bin/commands/check.mjs +6 -4
  21. package/bin/commands/iterate/api-usage.d.mts +2 -0
  22. package/bin/commands/iterate/api-usage.mjs +42 -0
  23. package/bin/commands/iterate/check-instructions.d.mts +2 -2
  24. package/bin/commands/iterate/check-instructions.mjs +27 -8
  25. package/bin/commands/iterate/fix-code.mjs +42 -8
  26. package/bin/commands/iterate/index.mjs +6 -3
  27. package/bin/commands/iterate/merge-state.mjs +5 -2
  28. package/bin/commands/iterate/render.d.mts +1 -1
  29. package/bin/commands/iterate/render.mjs +9 -5
  30. package/bin/commands/iterate/run.d.mts +2 -0
  31. package/bin/commands/iterate/run.mjs +8 -0
  32. package/bin/commands/mark-files-as-viewed.mjs +8 -0
  33. package/bin/commands/poll-run.d.mts +2 -0
  34. package/bin/commands/poll-run.mjs +8 -0
  35. package/bin/commands/poll.d.mts +1 -2
  36. package/bin/commands/poll.mjs +29 -10
  37. package/bin/comments/rate-limit.d.mts +4 -0
  38. package/bin/comments/rate-limit.mjs +6 -0
  39. package/bin/config/load.d.mts +6 -1
  40. package/bin/config/load.mjs +33 -1
  41. package/bin/config.json +6 -1
  42. package/bin/exit-codes.d.mts +1 -1
  43. package/bin/exit-codes.mjs +1 -1
  44. package/bin/github/api-telemetry-aggregate.d.mts +32 -0
  45. package/bin/github/api-telemetry-aggregate.mjs +84 -0
  46. package/bin/github/api-telemetry.d.mts +13 -0
  47. package/bin/github/api-telemetry.mjs +128 -0
  48. package/bin/github/check-annotations.d.mts +14 -1
  49. package/bin/github/check-annotations.mjs +29 -2
  50. package/bin/github/errors.d.mts +8 -0
  51. package/bin/github/errors.mjs +29 -1
  52. package/bin/github/gql/batch-pr-page.gql +8 -0
  53. package/bin/github/gql/batch-pr.gql +8 -0
  54. package/bin/github/gql/check-run-annotations.gql +8 -0
  55. package/bin/github/gql/commit-check-contexts.gql +8 -0
  56. package/bin/github/gql/get-pr-body.gql +8 -0
  57. package/bin/github/gql/get-pr-head-sha.gql +8 -0
  58. package/bin/github/gql/pr-number-by-branch.gql +8 -0
  59. package/bin/github/gql/review-thread-comments.gql +8 -0
  60. package/bin/github/gql/suggestion-threads.gql +8 -0
  61. package/bin/github/graphql-http.mjs +66 -10
  62. package/bin/github/graphql-internal-retry.d.mts +2 -0
  63. package/bin/github/graphql-internal-retry.mjs +50 -0
  64. package/bin/github/graphql-response.mjs +11 -1
  65. package/bin/github/http-auth.d.mts +9 -1
  66. package/bin/github/http-auth.mjs +35 -14
  67. package/bin/github/http-intermediate.d.mts +9 -0
  68. package/bin/github/http-intermediate.mjs +20 -0
  69. package/bin/github/http-request.d.mts +1 -1
  70. package/bin/github/http-request.mjs +1 -1
  71. package/bin/github/http-utils.d.mts +6 -0
  72. package/bin/github/http-utils.mjs +10 -1
  73. package/bin/github/rest-http.d.mts +16 -1
  74. package/bin/github/rest-http.mjs +71 -8
  75. package/bin/github/rest-text.mjs +41 -5
  76. package/bin/index.mjs +2 -1
  77. package/bin/log/session.d.mts +11 -0
  78. package/bin/log/session.mjs +22 -0
  79. package/bin/mcp/server.mjs +16 -2
  80. package/bin/quota-warning.d.mts +2 -0
  81. package/bin/quota-warning.mjs +6 -0
  82. package/bin/state/graphql-quota-claims.d.mts +2 -0
  83. package/bin/state/graphql-quota-claims.mjs +61 -0
  84. package/bin/state/graphql-quota-policy.d.mts +17 -0
  85. package/bin/state/graphql-quota-policy.mjs +43 -0
  86. package/bin/state/graphql-quota-warnings.d.mts +6 -0
  87. package/bin/state/graphql-quota-warnings.mjs +105 -0
  88. package/bin/state/rest-cache.d.mts +48 -0
  89. package/bin/state/rest-cache.mjs +91 -0
  90. package/bin/types/api-usage.d.mts +31 -0
  91. package/bin/types/api-usage.mjs +1 -0
  92. package/bin/types/iterate.d.mts +4 -1
  93. package/bin/types/report.d.mts +2 -0
  94. package/bin/types.d.mts +1 -0
  95. package/bin/types.mjs +1 -0
  96. package/package.json +1 -1
  97. package/plugins/pr-shepherd/.codex-plugin/plugin.json +1 -1
  98. package/plugins/pr-shepherd/.codex.mcp.json +1 -1
  99. package/plugins/pr-shepherd/.mcp.json +1 -1
  100. package/plugins/pr-shepherd/skills/pr-shepherd/SKILL.md +14 -9
@@ -34,7 +34,7 @@ export const EXIT = Object.freeze({
34
34
  UNAVAILABLE: 69,
35
35
  /** Unexpected/unclassified internal error — the fallback. */
36
36
  SOFTWARE: 70,
37
- /** Retryable GitHub failure: 429, 5xx, rate limit exhausted, `Retry-After` present. */
37
+ /** Retryable GitHub failure: 429, 5xx, rate limit exhausted, Retry-After, GraphQL INTERNAL. */
38
38
  TEMPFAIL: 75,
39
39
  /** GitHub 401/403 — missing token or insufficient PAT scopes. */
40
40
  NOPERM: 77,
@@ -0,0 +1,32 @@
1
+ import type { ApiTelemetryEvent } from "./api-telemetry.mts";
2
+ import type { RateLimitInfo } from "./http-utils.mts";
3
+ export interface TelemetryAggregate {
4
+ eventCount: number;
5
+ credentialSources: Map<string, number>;
6
+ graphql: {
7
+ requestCount: number;
8
+ measuredQueryCost: number;
9
+ unmeasuredRequestCount: number;
10
+ nodeCount: number;
11
+ rateLimit?: RateLimitInfo;
12
+ };
13
+ rest: Map<string, {
14
+ requestCount: number;
15
+ rateLimit?: RateLimitInfo;
16
+ }>;
17
+ }
18
+ export interface SequencedApiTelemetryEvent extends ApiTelemetryEvent {
19
+ sequence: number;
20
+ }
21
+ export interface TelemetryStore {
22
+ events: SequencedApiTelemetryEvent[];
23
+ compacted: TelemetryAggregate;
24
+ eventCount: number;
25
+ clock: {
26
+ next: number;
27
+ };
28
+ }
29
+ export declare function emptyAggregate(): TelemetryAggregate;
30
+ export declare function aggregateEvents(events: SequencedApiTelemetryEvent[]): TelemetryAggregate;
31
+ export declare function mergeAggregate(target: TelemetryAggregate, source: TelemetryAggregate): void;
32
+ export declare function aggregateStore(active: TelemetryStore): TelemetryAggregate;
@@ -0,0 +1,84 @@
1
+ export function emptyAggregate() {
2
+ return {
3
+ eventCount: 0,
4
+ credentialSources: new Map(),
5
+ graphql: { requestCount: 0, measuredQueryCost: 0, unmeasuredRequestCount: 0, nodeCount: 0 },
6
+ rest: new Map(),
7
+ };
8
+ }
9
+ export function aggregateEvents(events) {
10
+ const aggregate = emptyAggregate();
11
+ for (const event of events) {
12
+ aggregate.eventCount += 1;
13
+ if (!aggregate.credentialSources.has(event.authSource)) {
14
+ aggregate.credentialSources.set(event.authSource, event.sequence);
15
+ }
16
+ if (event.kind === "GraphQL") {
17
+ aggregate.graphql.requestCount += 1;
18
+ if (event.rateLimit?.cost === undefined)
19
+ aggregate.graphql.unmeasuredRequestCount += 1;
20
+ else
21
+ aggregate.graphql.measuredQueryCost += event.rateLimit.cost;
22
+ aggregate.graphql.nodeCount += event.rateLimit?.nodeCount ?? 0;
23
+ if (event.rateLimit !== undefined) {
24
+ aggregate.graphql.rateLimit = selectAuthoritativeRateLimit(aggregate.graphql.rateLimit, event.rateLimit);
25
+ }
26
+ continue;
27
+ }
28
+ const resource = event.rateLimit?.resource ?? "unknown";
29
+ const group = aggregate.rest.get(resource) ?? { requestCount: 0 };
30
+ group.requestCount += 1;
31
+ if (event.rateLimit !== undefined) {
32
+ group.rateLimit = selectAuthoritativeRateLimit(group.rateLimit, event.rateLimit);
33
+ }
34
+ aggregate.rest.set(resource, group);
35
+ }
36
+ return aggregate;
37
+ }
38
+ export function mergeAggregate(target, source) {
39
+ target.eventCount += source.eventCount;
40
+ for (const [credentialSource, sequence] of source.credentialSources) {
41
+ const current = target.credentialSources.get(credentialSource);
42
+ if (current === undefined || sequence < current) {
43
+ target.credentialSources.set(credentialSource, sequence);
44
+ }
45
+ }
46
+ target.graphql.requestCount += source.graphql.requestCount;
47
+ target.graphql.measuredQueryCost += source.graphql.measuredQueryCost;
48
+ target.graphql.unmeasuredRequestCount += source.graphql.unmeasuredRequestCount;
49
+ target.graphql.nodeCount += source.graphql.nodeCount;
50
+ if (source.graphql.rateLimit !== undefined) {
51
+ target.graphql.rateLimit = selectAuthoritativeRateLimit(target.graphql.rateLimit, source.graphql.rateLimit);
52
+ }
53
+ for (const [resource, sourceGroup] of source.rest) {
54
+ const targetGroup = target.rest.get(resource) ?? { requestCount: 0 };
55
+ targetGroup.requestCount += sourceGroup.requestCount;
56
+ if (sourceGroup.rateLimit !== undefined) {
57
+ targetGroup.rateLimit = selectAuthoritativeRateLimit(targetGroup.rateLimit, sourceGroup.rateLimit);
58
+ }
59
+ target.rest.set(resource, targetGroup);
60
+ }
61
+ }
62
+ export function aggregateStore(active) {
63
+ const all = emptyAggregate();
64
+ mergeAggregate(all, active.compacted);
65
+ mergeAggregate(all, aggregateEvents(active.events));
66
+ return all;
67
+ }
68
+ function selectAuthoritativeRateLimit(current, candidate) {
69
+ if (current === undefined)
70
+ return { ...candidate };
71
+ if (candidate.resetAt !== current.resetAt) {
72
+ return candidate.resetAt > current.resetAt ? { ...candidate } : current;
73
+ }
74
+ if (candidate.remaining !== current.remaining) {
75
+ return candidate.remaining < current.remaining ? { ...candidate } : current;
76
+ }
77
+ if (candidate.used !== current.used) {
78
+ if (candidate.used === undefined)
79
+ return current;
80
+ if (current.used === undefined || candidate.used > current.used)
81
+ return { ...candidate };
82
+ }
83
+ return current;
84
+ }
@@ -0,0 +1,13 @@
1
+ import type { ApiUsage } from "../types.mts";
2
+ import type { RateLimitInfo } from "./http-utils.mts";
3
+ export interface ApiTelemetryEvent {
4
+ kind: "GraphQL" | "REST";
5
+ method: string;
6
+ authSource: string;
7
+ rateLimit?: RateLimitInfo;
8
+ }
9
+ /** Isolates a top-level CLI/MCP command while allowing nested iterate ticks to aggregate. */
10
+ export declare function withApiTelemetryScope<T>(fn: () => Promise<T>): Promise<T>;
11
+ export declare function recordApiTelemetry(event: ApiTelemetryEvent): void;
12
+ export declare function mergeGraphqlRateLimit(headerRateLimit: RateLimitInfo | null, data: unknown): RateLimitInfo | null;
13
+ export declare function summarizeApiTelemetry(): ApiUsage | undefined;
@@ -0,0 +1,128 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ import { aggregateEvents, aggregateStore, emptyAggregate, mergeAggregate, } from "./api-telemetry-aggregate.mjs";
3
+ const GRAPHQL_RATE_LIMIT_ALIAS = "_shepherdRateLimit";
4
+ const eventStorage = new AsyncLocalStorage();
5
+ function store() {
6
+ return eventStorage.getStore();
7
+ }
8
+ /** Isolates a top-level CLI/MCP command while allowing nested iterate ticks to aggregate. */
9
+ export function withApiTelemetryScope(fn) {
10
+ const parent = store();
11
+ const child = {
12
+ events: [],
13
+ compacted: emptyAggregate(),
14
+ eventCount: 0,
15
+ clock: parent?.clock ?? { next: 0 },
16
+ };
17
+ return eventStorage.run(child, async () => {
18
+ try {
19
+ return await fn();
20
+ }
21
+ finally {
22
+ mergeAggregate(child.compacted, aggregateEvents(child.events));
23
+ child.events = [];
24
+ if (parent !== undefined) {
25
+ mergeAggregate(parent.compacted, aggregateEvents(parent.events));
26
+ parent.events = [];
27
+ mergeAggregate(parent.compacted, child.compacted);
28
+ parent.eventCount += child.eventCount;
29
+ }
30
+ }
31
+ });
32
+ }
33
+ export function recordApiTelemetry(event) {
34
+ const active = store();
35
+ if (active === undefined)
36
+ return;
37
+ active.events.push({
38
+ ...event,
39
+ rateLimit: event.rateLimit ? { ...event.rateLimit } : undefined,
40
+ sequence: active.clock.next++,
41
+ });
42
+ active.eventCount += 1;
43
+ }
44
+ export function mergeGraphqlRateLimit(headerRateLimit, data) {
45
+ const payload = extractGraphqlRateLimit(data);
46
+ if (headerRateLimit === null && payload === null)
47
+ return null;
48
+ const resetAt = payload === null ? undefined : Date.parse(payload.resetAt) / 1000;
49
+ const fallback = payload !== null && Number.isFinite(resetAt)
50
+ ? {
51
+ limit: payload.limit,
52
+ used: payload.used,
53
+ remaining: payload.remaining,
54
+ resetAt: resetAt,
55
+ resource: "graphql",
56
+ }
57
+ : null;
58
+ const base = headerRateLimit ?? fallback;
59
+ if (base === null)
60
+ return null;
61
+ return {
62
+ ...base,
63
+ resource: base.resource ?? "graphql",
64
+ ...(payload !== null && Number.isFinite(payload.cost) && { cost: payload.cost }),
65
+ ...(payload !== null && Number.isFinite(payload.nodeCount) && { nodeCount: payload.nodeCount }),
66
+ };
67
+ }
68
+ export function summarizeApiTelemetry() {
69
+ const active = store();
70
+ if (active === undefined)
71
+ return undefined;
72
+ const selected = aggregateStore(active);
73
+ if (selected.eventCount === 0)
74
+ return undefined;
75
+ const credentialSources = [...selected.credentialSources.entries()]
76
+ .sort((left, right) => left[1] - right[1])
77
+ .map(([source]) => source);
78
+ const graphql = summarizeGraphql(selected.graphql);
79
+ const rest = summarizeRest(selected.rest);
80
+ return {
81
+ credentialSources,
82
+ ...(graphql !== undefined && { graphql }),
83
+ ...(rest.length > 0 && { rest }),
84
+ };
85
+ }
86
+ function summarizeGraphql(selected) {
87
+ const rateLimit = selected.rateLimit;
88
+ if (rateLimit === undefined)
89
+ return undefined;
90
+ return {
91
+ ...resourceUsage(rateLimit, selected.requestCount, "graphql"),
92
+ measuredQueryCost: selected.measuredQueryCost,
93
+ unmeasuredRequestCount: selected.unmeasuredRequestCount,
94
+ nodeCount: selected.nodeCount,
95
+ };
96
+ }
97
+ function summarizeRest(selected) {
98
+ return [...selected.entries()].flatMap(([resource, group]) => {
99
+ return group.rateLimit ? [resourceUsage(group.rateLimit, group.requestCount, resource)] : [];
100
+ });
101
+ }
102
+ function resourceUsage(rateLimit, requestCount, fallbackResource) {
103
+ return {
104
+ resource: rateLimit.resource ?? fallbackResource,
105
+ requestCount,
106
+ limit: rateLimit.limit,
107
+ ...(rateLimit.used !== undefined && { used: rateLimit.used }),
108
+ remaining: rateLimit.remaining,
109
+ resetAt: rateLimit.resetAt,
110
+ };
111
+ }
112
+ function extractGraphqlRateLimit(data) {
113
+ if (data === null || typeof data !== "object" || Array.isArray(data))
114
+ return null;
115
+ const value = data[GRAPHQL_RATE_LIMIT_ALIAS];
116
+ if (value === null || typeof value !== "object" || Array.isArray(value))
117
+ return null;
118
+ const record = value;
119
+ if (!Number.isFinite(record["cost"]) ||
120
+ !Number.isFinite(record["limit"]) ||
121
+ !Number.isFinite(record["nodeCount"]) ||
122
+ !Number.isFinite(record["remaining"]) ||
123
+ typeof record["resetAt"] !== "string" ||
124
+ !Number.isFinite(record["used"])) {
125
+ return null;
126
+ }
127
+ return record;
128
+ }
@@ -1,2 +1,15 @@
1
+ import { type StateKey } from "../state/rest-cache.mts";
1
2
  import type { CheckAnnotation } from "../types.mts";
2
- export declare function fetchCheckRunAnnotations(checkRunId: string): Promise<CheckAnnotation[]>;
3
+ export interface AnnotationCacheOptions {
4
+ stateKey: StateKey;
5
+ headSha?: string;
6
+ }
7
+ /**
8
+ * Fetches all inline annotations for a check run.
9
+ *
10
+ * When `cacheOpts` is provided, the result is cached by `checkRunId` — safe
11
+ * because callers only pass `cacheOpts` for COMPLETED check runs (a re-run
12
+ * mints a new check-run node id, so a COMPLETED run's annotations are
13
+ * immutable once fetched).
14
+ */
15
+ export declare function fetchCheckRunAnnotations(checkRunId: string, cacheOpts?: AnnotationCacheOptions): Promise<CheckAnnotation[]>;
@@ -1,11 +1,34 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { graphql } from "./client.mjs";
3
3
  import { CHECK_RUN_ANNOTATIONS_QUERY } from "./queries.mjs";
4
+ import { loadDerived, storeDerived } from "../state/rest-cache.mjs";
4
5
  const ANNOTATIONS_PER_PAGE = 100;
5
6
  const MAX_ANNOTATION_PAGES = 10;
6
7
  const ANNOTATION_TEXT_MAX_CHARS = 4_000;
7
8
  const TRUNCATED_SUFFIX = "\n[truncated]";
8
- export async function fetchCheckRunAnnotations(checkRunId) {
9
+ /**
10
+ * Some Checks-API publishers (e.g. SonarCloud) PATCH additional annotations
11
+ * onto an already-COMPLETED check run without minting a new check-run node
12
+ * ID, so "COMPLETED" is not a reliable immutability signal on its own. Bound
13
+ * the cache instead of trusting it forever, so a long-running poll session
14
+ * eventually revalidates.
15
+ */
16
+ const ANNOTATION_CACHE_MAX_AGE_MS = 60 * 60 * 1000;
17
+ /**
18
+ * Fetches all inline annotations for a check run.
19
+ *
20
+ * When `cacheOpts` is provided, the result is cached by `checkRunId` — safe
21
+ * because callers only pass `cacheOpts` for COMPLETED check runs (a re-run
22
+ * mints a new check-run node id, so a COMPLETED run's annotations are
23
+ * immutable once fetched).
24
+ */
25
+ export async function fetchCheckRunAnnotations(checkRunId, cacheOpts) {
26
+ const cacheName = `annotations-${checkRunId}`;
27
+ if (cacheOpts) {
28
+ const cached = await loadDerived(cacheOpts.stateKey, cacheName);
29
+ if (cached && Date.now() - cached.storedAt < ANNOTATION_CACHE_MAX_AGE_MS)
30
+ return cached.value;
31
+ }
9
32
  let cursor = null;
10
33
  const nodes = [];
11
34
  for (let page = 1; page <= MAX_ANNOTATION_PAGES; page++) {
@@ -20,7 +43,11 @@ export async function fetchCheckRunAnnotations(checkRunId) {
20
43
  }
21
44
  cursor = result.pageInfo.endCursor;
22
45
  }
23
- return nodes.map((node) => toCheckAnnotation(checkRunId, node));
46
+ const annotations = nodes.map((node) => toCheckAnnotation(checkRunId, node));
47
+ if (cacheOpts) {
48
+ await storeDerived(cacheOpts.stateKey, cacheName, annotations, cacheOpts.headSha);
49
+ }
50
+ return annotations;
24
51
  }
25
52
  async function fetchAnnotationPage(checkRunId, cursor) {
26
53
  const res = await graphql(CHECK_RUN_ANNOTATIONS_QUERY, {
@@ -3,17 +3,25 @@ import type { RateLimitInfo } from "./http.mts";
3
3
  export interface GitHubGraphQlError {
4
4
  message: string;
5
5
  path?: unknown;
6
+ /** GitHub's GraphQL `type` field — `INTERNAL` on engine crashes. */
7
+ type?: string;
8
+ /** GraphQL `extensions`; GitHub often sets `{ code: "INTERNAL" }`. */
9
+ extensions?: unknown;
6
10
  }
11
+ /** GitHub GraphQL engine crash: HTTP 200, `data: null`, INTERNAL type/code or message. */
12
+ export declare function isRetryableGraphQlInternal(graphqlErrors?: GitHubGraphQlError[]): boolean;
7
13
  export declare class GitHubRequestError extends ShepherdError {
8
14
  readonly status: number;
9
15
  readonly rateLimit?: RateLimitInfo;
10
16
  readonly retryAfterSeconds?: number;
11
17
  readonly graphqlErrors?: GitHubGraphQlError[];
18
+ readonly authSource?: string;
12
19
  constructor(message: string, opts: {
13
20
  status: number;
14
21
  rateLimit?: RateLimitInfo;
15
22
  retryAfterSeconds?: number;
16
23
  graphqlErrors?: GitHubGraphQlError[];
24
+ authSource?: string;
17
25
  /**
18
26
  * Bypasses status-based classification entirely — for callers that already
19
27
  * know the failure kind better than the HTTP status can express (e.g. a
@@ -5,16 +5,42 @@ import { EXIT, ShepherdError } from "../exit-codes.mjs";
5
5
  // be resolved. Status alone can't see this, so classification must also inspect the
6
6
  // GraphQL error messages themselves.
7
7
  const GRAPHQL_PERMISSION_ERROR = /resource not accessible/i;
8
+ const GRAPHQL_INTERNAL_MESSAGE = /something went wrong while executing your query/i;
9
+ function isInternalToken(value) {
10
+ return typeof value === "string" && value.toUpperCase() === "INTERNAL";
11
+ }
12
+ function extensionsCode(extensions) {
13
+ if (typeof extensions !== "object" || extensions === null || Array.isArray(extensions)) {
14
+ return undefined;
15
+ }
16
+ return extensions["code"];
17
+ }
8
18
  function hasPermissionError(graphqlErrors) {
9
19
  return graphqlErrors?.some((e) => GRAPHQL_PERMISSION_ERROR.test(e.message)) ?? false;
10
20
  }
21
+ /** GitHub GraphQL engine crash: HTTP 200, `data: null`, INTERNAL type/code or message. */
22
+ export function isRetryableGraphQlInternal(graphqlErrors) {
23
+ if (!graphqlErrors?.length)
24
+ return false;
25
+ return graphqlErrors.some((error) => {
26
+ if (isInternalToken(error.type))
27
+ return true;
28
+ if (isInternalToken(extensionsCode(error.extensions)))
29
+ return true;
30
+ return GRAPHQL_INTERNAL_MESSAGE.test(error.message);
31
+ });
32
+ }
11
33
  function classifyStatus(status, rateLimit, retryAfterSeconds, graphqlErrors) {
12
34
  // Retry signals take priority over everything else: GitHub's secondary rate limit
13
35
  // returns 403 with a Retry-After header, which is a transient throttle — not the
14
36
  // permission-denied 403 a bad/missing token produces. Treat any retry signal as
15
37
  // TEMPFAIL first so it isn't shadowed by the checks below.
16
38
  const rateLimitExhausted = rateLimit !== undefined && rateLimit.remaining <= 0;
17
- if (status === 429 || status >= 500 || retryAfterSeconds !== undefined || rateLimitExhausted) {
39
+ if (status === 429 ||
40
+ status >= 500 ||
41
+ retryAfterSeconds !== undefined ||
42
+ rateLimitExhausted ||
43
+ isRetryableGraphQlInternal(graphqlErrors)) {
18
44
  return EXIT.TEMPFAIL;
19
45
  }
20
46
  if (status === 401 || status === 403 || hasPermissionError(graphqlErrors))
@@ -26,6 +52,7 @@ export class GitHubRequestError extends ShepherdError {
26
52
  rateLimit;
27
53
  retryAfterSeconds;
28
54
  graphqlErrors;
55
+ authSource;
29
56
  constructor(message, opts) {
30
57
  super(message, opts.exitCodeOverride ??
31
58
  classifyStatus(opts.status, opts.rateLimit, opts.retryAfterSeconds, opts.graphqlErrors));
@@ -34,5 +61,6 @@ export class GitHubRequestError extends ShepherdError {
34
61
  this.rateLimit = opts.rateLimit;
35
62
  this.retryAfterSeconds = opts.retryAfterSeconds;
36
63
  this.graphqlErrors = opts.graphqlErrors;
64
+ this.authSource = opts.authSource;
37
65
  }
38
66
  }
@@ -18,6 +18,14 @@ query BatchPrPage(
18
18
  $includeChecks: Boolean!
19
19
  $checksCursor: String
20
20
  ) {
21
+ _shepherdRateLimit: rateLimit {
22
+ cost
23
+ limit
24
+ nodeCount
25
+ remaining
26
+ resetAt
27
+ used
28
+ }
21
29
  repository(owner: $owner, name: $repo) {
22
30
  pullRequest(number: $pr) {
23
31
  reviewThreads(last: 100, before: $threadsCursor) @include(if: $includeThreads) {
@@ -1,6 +1,14 @@
1
1
  # First page only. Extra pages use batch-pr-page.gql so follow-ups do not
2
2
  # re-select every connection (GraphQL points scale with possible nodes).
3
3
  query BatchPr($owner: String!, $repo: String!, $pr: Int!) {
4
+ _shepherdRateLimit: rateLimit {
5
+ cost
6
+ limit
7
+ nodeCount
8
+ remaining
9
+ resetAt
10
+ used
11
+ }
4
12
  repository(owner: $owner, name: $repo) {
5
13
  viewerPermission
6
14
  viewerCanAdminister
@@ -1,4 +1,12 @@
1
1
  query CheckRunAnnotations($id: ID!, $cursor: String) {
2
+ _shepherdRateLimit: rateLimit {
3
+ cost
4
+ limit
5
+ nodeCount
6
+ remaining
7
+ resetAt
8
+ used
9
+ }
2
10
  node(id: $id) {
3
11
  __typename
4
12
  ... on CheckRun {
@@ -1,4 +1,12 @@
1
1
  query CommitCheckContexts($owner: String!, $repo: String!, $oid: String!, $cursor: String) {
2
+ _shepherdRateLimit: rateLimit {
3
+ cost
4
+ limit
5
+ nodeCount
6
+ remaining
7
+ resetAt
8
+ used
9
+ }
2
10
  repository(owner: $owner, name: $repo) {
3
11
  object(expression: $oid) {
4
12
  __typename
@@ -1,4 +1,12 @@
1
1
  query GetPrBody($owner: String!, $repo: String!, $pr: Int!) {
2
+ _shepherdRateLimit: rateLimit {
3
+ cost
4
+ limit
5
+ nodeCount
6
+ remaining
7
+ resetAt
8
+ used
9
+ }
2
10
  repository(owner: $owner, name: $repo) {
3
11
  pullRequest(number: $pr) {
4
12
  id
@@ -1,4 +1,12 @@
1
1
  query GetPrHeadSha($owner: String!, $repo: String!, $pr: Int!) {
2
+ _shepherdRateLimit: rateLimit {
3
+ cost
4
+ limit
5
+ nodeCount
6
+ remaining
7
+ resetAt
8
+ used
9
+ }
2
10
  repository(owner: $owner, name: $repo) {
3
11
  pullRequest(number: $pr) {
4
12
  headRefOid
@@ -1,4 +1,12 @@
1
1
  query PrNumberByBranch($owner: String!, $repo: String!, $branch: String!) {
2
+ _shepherdRateLimit: rateLimit {
3
+ cost
4
+ limit
5
+ nodeCount
6
+ remaining
7
+ resetAt
8
+ used
9
+ }
2
10
  repository(owner: $owner, name: $repo) {
3
11
  pullRequests(headRefName: $branch, states: OPEN, first: 1) {
4
12
  nodes {
@@ -1,4 +1,12 @@
1
1
  query ReviewThreadComments($threadId: ID!, $commentsCursor: String) {
2
+ _shepherdRateLimit: rateLimit {
3
+ cost
4
+ limit
5
+ nodeCount
6
+ remaining
7
+ resetAt
8
+ used
9
+ }
2
10
  node(id: $threadId) {
3
11
  __typename
4
12
  ... on PullRequestReviewThread {
@@ -1,4 +1,12 @@
1
1
  query SuggestionThreads($owner: String!, $repo: String!, $pr: Int!, $threadIds: [ID!]!) {
2
+ _shepherdRateLimit: rateLimit {
3
+ cost
4
+ limit
5
+ nodeCount
6
+ remaining
7
+ resetAt
8
+ used
9
+ }
2
10
  repository(owner: $owner, name: $repo) {
3
11
  pullRequest(number: $pr) {
4
12
  headRefOid