pr-shepherd 1.5.0 → 1.6.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.
- package/.claude-plugin/plugin.json +1 -1
- package/README.md +1 -0
- package/bin/api.d.mts +1 -1
- package/bin/commands/iterate/index.mjs +4 -0
- package/bin/commands/poll-events.d.mts +44 -0
- package/bin/commands/poll-events.mjs +123 -0
- package/bin/commands/poll-options.d.mts +9 -0
- package/bin/commands/poll-options.mjs +1 -0
- package/bin/commands/poll-progress.d.mts +2 -0
- package/bin/commands/poll-progress.mjs +6 -4
- package/bin/commands/poll-quota.d.mts +2 -0
- package/bin/commands/poll-quota.mjs +4 -0
- package/bin/commands/poll-summary.d.mts +1 -0
- package/bin/commands/poll-summary.mjs +30 -18
- package/bin/commands/poll-tick.d.mts +17 -0
- package/bin/commands/poll-tick.mjs +24 -0
- package/bin/commands/poll.d.mts +3 -9
- package/bin/commands/poll.mjs +64 -60
- package/bin/commands/stall-deadline.d.mts +20 -0
- package/bin/commands/stall-deadline.mjs +42 -0
- package/bin/commands/tick.mjs +1 -16
- package/bin/config/schema.d.mts +14 -0
- package/bin/config/schema.mjs +6 -0
- package/bin/events/config.d.mts +7 -0
- package/bin/events/config.mjs +66 -0
- package/bin/events/envelope.d.mts +4 -0
- package/bin/events/envelope.mjs +27 -0
- package/bin/events/filters.d.mts +8 -0
- package/bin/events/filters.mjs +27 -0
- package/bin/events/health.d.mts +14 -0
- package/bin/events/health.mjs +27 -0
- package/bin/events/matcher.d.mts +2 -0
- package/bin/events/matcher.mjs +32 -0
- package/bin/events/relay.d.mts +11 -0
- package/bin/events/relay.mjs +86 -0
- package/bin/events/serve.d.mts +10 -0
- package/bin/events/serve.mjs +137 -0
- package/bin/events/sse.d.mts +10 -0
- package/bin/events/sse.mjs +50 -0
- package/bin/events/types.d.mts +69 -0
- package/bin/events/types.mjs +1 -0
- package/bin/events/waiter.d.mts +47 -0
- package/bin/events/waiter.mjs +80 -0
- package/bin/github/batch-first-page.d.mts +17 -0
- package/bin/github/batch-first-page.mjs +48 -0
- package/bin/github/batch-page.d.mts +2 -0
- package/bin/github/batch-page.mjs +2 -2
- package/bin/github/batch-stack-evidence.d.mts +3 -1
- package/bin/github/batch-stack-evidence.mjs +4 -2
- package/bin/github/batch-viewer.d.mts +24 -0
- package/bin/github/batch-viewer.mjs +66 -0
- package/bin/github/batch.d.mts +1 -1
- package/bin/github/batch.mjs +30 -35
- package/bin/github/check-annotations-batch.mjs +1 -7
- package/bin/github/gql/batch-pr-viewer.gql +46 -0
- package/bin/github/gql-strip-viewer.d.mts +7 -0
- package/bin/github/gql-strip-viewer.mjs +14 -0
- package/bin/github/queries.d.mts +8 -0
- package/bin/github/queries.mjs +9 -0
- package/bin/github/thread-comments.d.mts +1 -1
- package/bin/github/thread-comments.mjs +7 -7
- package/bin/types/iterate.d.mts +9 -0
- package/bin/util/chunks.d.mts +2 -0
- package/bin/util/chunks.mjs +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/.codex.mcp.json +1 -1
- package/plugins/pr-shepherd/.mcp.json +1 -1
- package/plugins/pr-shepherd/package-lock.json +4 -4
- package/plugins/pr-shepherd/package.json +1 -1
- package/pr-shepherd.schema.json +21 -0
package/README.md
CHANGED
|
@@ -140,6 +140,7 @@ See [docs/graphql-usage.md](docs/graphql-usage.md) and [docs/rest-usage.md](docs
|
|
|
140
140
|
- Cursor and other stdio clients use the [MCP server](docs/mcp.md).
|
|
141
141
|
- Shell and CI use the [CLI](docs/cli-usage.md).
|
|
142
142
|
- In Claude Code cloud sessions, Shepherd switches to the [REST transport](docs/rest.md) and [single ticks](docs/cloud.md).
|
|
143
|
+
- Optional [event-driven waiting](docs/hook-harness.md) wakes a poll on GitHub webhook events instead of a fixed sleep.
|
|
143
144
|
- `stack` reports every layer of a native GitHub stacked PR and shepherds the layers you own.
|
|
144
145
|
|
|
145
146
|
## Use cases
|
package/bin/api.d.mts
CHANGED
|
@@ -27,7 +27,7 @@ export interface CreatePrShepherdOptions {
|
|
|
27
27
|
}
|
|
28
28
|
/** A positive PR number, GitHub pull-request URL, or owner/repo#number reference. */
|
|
29
29
|
export type PrReference = number | string;
|
|
30
|
-
type IterateOptions = Omit<IterateCommandOptions, "format" | "prNumber" | "targetRepository" | "persistSeen" | "deferEditedCommentMarkers" | "fingerprintCache" | "stallDeadlineSink" | "deferQuotaWarning" | "quotaWarningMinimumPollIntervalMinutes">;
|
|
30
|
+
type IterateOptions = Omit<IterateCommandOptions, "format" | "prNumber" | "targetRepository" | "persistSeen" | "deferEditedCommentMarkers" | "fingerprintCache" | "stallDeadlineSink" | "eventTargetSink" | "deferQuotaWarning" | "quotaWarningMinimumPollIntervalMinutes">;
|
|
31
31
|
export type SingleIterateInput = IterateOptions & {
|
|
32
32
|
pr?: PrReference;
|
|
33
33
|
prs?: never;
|
|
@@ -78,6 +78,10 @@ async function runIterateCore(opts, tick) {
|
|
|
78
78
|
reviewOutputMaxChars: config.iterate["review-output-max-chars"],
|
|
79
79
|
}, checkContext);
|
|
80
80
|
tick.report = report;
|
|
81
|
+
if (opts.eventTargetSink) {
|
|
82
|
+
opts.eventTargetSink.headSha = report.headSha;
|
|
83
|
+
opts.eventTargetSink.headRef = report.headRefName;
|
|
84
|
+
}
|
|
81
85
|
const [repoOwner, repoName] = report.repo.split("/");
|
|
82
86
|
if (!repoOwner || !repoName) {
|
|
83
87
|
throw new ShepherdError(`Unexpected repo format: "${report.repo}" (expected "owner/name")`, EXIT.DATAERR);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { IterateResult, PollSummaryResult } from "../types.mts";
|
|
2
|
+
import type { WatchTarget } from "../events/types.mts";
|
|
3
|
+
interface WaitTickInput {
|
|
4
|
+
/** Progress prefix, e.g. `poll tick 3 / +45s`. */
|
|
5
|
+
label: string;
|
|
6
|
+
targets: readonly WatchTarget[];
|
|
7
|
+
sleepMs: number;
|
|
8
|
+
intervalMs: number;
|
|
9
|
+
tickStartedAt: number;
|
|
10
|
+
/** Time left in the poll window; `Infinity` for `--until-terminal`. */
|
|
11
|
+
remainingMs: number;
|
|
12
|
+
recheck: boolean;
|
|
13
|
+
/** Epoch ms of a time-based deadline that caps the healthy event wait. */
|
|
14
|
+
deadlineAt?: number;
|
|
15
|
+
/** Which timer `deadlineAt` belongs to: `stall-timeout`, `ready-delay`, or `CI-start`. */
|
|
16
|
+
deadlineReason?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface PollEvents {
|
|
19
|
+
engaged(targets: readonly WatchTarget[]): boolean;
|
|
20
|
+
/** Wait for the next tick. Returns true when the poll window elapsed with no event. */
|
|
21
|
+
waitTick(input: WaitTickInput): Promise<boolean>;
|
|
22
|
+
close(): void;
|
|
23
|
+
}
|
|
24
|
+
/** Run a poll with its event wait (if configured), always closing the source afterwards. */
|
|
25
|
+
export declare function withPollEvents<T>(verbose: boolean, run: (events: PollEvents | undefined) => Promise<T>): Promise<T>;
|
|
26
|
+
export interface PauseInput extends WaitTickInput {
|
|
27
|
+
events: PollEvents | undefined;
|
|
28
|
+
/** `viaEvents` is true when the pause waits for an event rather than sleeping. */
|
|
29
|
+
announce: (viaEvents: boolean) => void;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Pause until the next tick, by event wait when engaged and by plain sleep otherwise. Returns
|
|
33
|
+
* true when the poll should stop: the window is over, or (without events) the next sleep would
|
|
34
|
+
* not fit in what is left. `announce` runs once the pause is certain, before it starts.
|
|
35
|
+
*/
|
|
36
|
+
export declare function pauseBeforeNextTick(input: PauseInput): Promise<boolean>;
|
|
37
|
+
/** The one PR a single-PR tick watched; head SHA and ref come from that tick's batch data. */
|
|
38
|
+
export declare function onePrTargets(result: Pick<IterateResult, "repo" | "pr" | "baseBranch">, head: {
|
|
39
|
+
headSha?: string;
|
|
40
|
+
headRef?: string;
|
|
41
|
+
}): WatchTarget[];
|
|
42
|
+
/** Every open PR of an aggregate tick. */
|
|
43
|
+
export declare function aggregateTargets(result: PollSummaryResult): WatchTarget[];
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { loadConfig } from "../config/load.mjs";
|
|
2
|
+
import { resolveEventsSource } from "../events/config.mjs";
|
|
3
|
+
import { createRelayTransport, RELAY_REQUEST_TIMEOUT_MS } from "../events/relay.mjs";
|
|
4
|
+
import { createServeTransport } from "../events/serve.mjs";
|
|
5
|
+
import { createEventWaiter } from "../events/waiter.mjs";
|
|
6
|
+
import { githubFetch } from "../github/github-fetch.mjs";
|
|
7
|
+
import { appendEntry } from "../log/log-file.mjs";
|
|
8
|
+
import { sleep } from "../util/sleep.mjs";
|
|
9
|
+
const TIMER_DRIFT_TOLERANCE_MS = 500;
|
|
10
|
+
function describe(wake, degraded) {
|
|
11
|
+
if (wake.kind === "timer")
|
|
12
|
+
return degraded ? "timer (events unavailable)" : "fallback timer";
|
|
13
|
+
const { event, action, repo } = wake.event;
|
|
14
|
+
return `${action ? `${event}.${action}` : event} ${repo ?? ""}`.trim();
|
|
15
|
+
}
|
|
16
|
+
function createTransport(startMs) {
|
|
17
|
+
const source = resolveEventsSource();
|
|
18
|
+
return source.kind === "serve"
|
|
19
|
+
? createServeTransport(source, startMs, {
|
|
20
|
+
fetch: (input, init) => globalThis.fetch(input, init),
|
|
21
|
+
})
|
|
22
|
+
: createRelayTransport(source, startMs, {
|
|
23
|
+
fetch: (input, init) => githubFetch(input, init, RELAY_REQUEST_TIMEOUT_MS),
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* The event-driven wait for a poll loop, or `undefined` when `events.source` is `none`. Captures
|
|
28
|
+
* the start time now, before the first tick snapshots GitHub, so no event in between is missed.
|
|
29
|
+
* Throws a config error when hook-harness is selected but no source resolves.
|
|
30
|
+
*/
|
|
31
|
+
function createPollEvents(verbose) {
|
|
32
|
+
const config = loadConfig().events;
|
|
33
|
+
if (config.source === "none")
|
|
34
|
+
return undefined;
|
|
35
|
+
const note = (message) => {
|
|
36
|
+
if (verbose)
|
|
37
|
+
process.stderr.write(`${message}\n`);
|
|
38
|
+
appendEntry(`- ${message}\n`);
|
|
39
|
+
};
|
|
40
|
+
const waiter = createEventWaiter({
|
|
41
|
+
transport: createTransport(Date.now()),
|
|
42
|
+
fallbackMs: config["fallback-interval-seconds"] * 1000,
|
|
43
|
+
deps: { now: () => Date.now(), sleep },
|
|
44
|
+
note,
|
|
45
|
+
});
|
|
46
|
+
return {
|
|
47
|
+
engaged: (targets) => waiter.engaged(targets),
|
|
48
|
+
close: () => waiter.close(),
|
|
49
|
+
async waitTick(input) {
|
|
50
|
+
const waitStart = Date.now();
|
|
51
|
+
const { wake, degraded } = await waiter.wait({
|
|
52
|
+
targets: input.targets,
|
|
53
|
+
sleepMs: input.sleepMs,
|
|
54
|
+
windowMs: input.remainingMs,
|
|
55
|
+
recheck: input.recheck,
|
|
56
|
+
// Events never make ticks more frequent than plain polling would.
|
|
57
|
+
minGapMs: input.sleepMs,
|
|
58
|
+
tickStartedAt: input.tickStartedAt,
|
|
59
|
+
...(input.deadlineAt !== undefined && { deadlineAt: input.deadlineAt }),
|
|
60
|
+
});
|
|
61
|
+
const atDeadline = wake.kind === "timer" &&
|
|
62
|
+
!degraded &&
|
|
63
|
+
input.deadlineAt !== undefined &&
|
|
64
|
+
Date.now() + TIMER_DRIFT_TOLERANCE_MS >= input.deadlineAt;
|
|
65
|
+
note(`[${input.label}] woke on ${atDeadline ? `${input.deadlineReason ?? "time-based"} deadline` : describe(wake, degraded)}`);
|
|
66
|
+
return (wake.kind === "timer" &&
|
|
67
|
+
Date.now() - waitStart + TIMER_DRIFT_TOLERANCE_MS >= input.remainingMs);
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/** Run a poll with its event wait (if configured), always closing the source afterwards. */
|
|
72
|
+
export async function withPollEvents(verbose, run) {
|
|
73
|
+
const events = createPollEvents(verbose);
|
|
74
|
+
try {
|
|
75
|
+
return await run(events);
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
events?.close();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Pause until the next tick, by event wait when engaged and by plain sleep otherwise. Returns
|
|
83
|
+
* true when the poll should stop: the window is over, or (without events) the next sleep would
|
|
84
|
+
* not fit in what is left. `announce` runs once the pause is certain, before it starts.
|
|
85
|
+
*/
|
|
86
|
+
export async function pauseBeforeNextTick(input) {
|
|
87
|
+
const { events, announce, ...wait } = input;
|
|
88
|
+
const viaEvents = events?.engaged(wait.targets) === true;
|
|
89
|
+
const { remainingMs, sleepMs } = wait;
|
|
90
|
+
if (remainingMs <= 0 || (!viaEvents && remainingMs + TIMER_DRIFT_TOLERANCE_MS < sleepMs)) {
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
announce(viaEvents);
|
|
94
|
+
if (!events || !viaEvents) {
|
|
95
|
+
await sleep(sleepMs);
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
return events.waitTick(wait);
|
|
99
|
+
}
|
|
100
|
+
/** The one PR a single-PR tick watched; head SHA and ref come from that tick's batch data. */
|
|
101
|
+
export function onePrTargets(result, head) {
|
|
102
|
+
return [
|
|
103
|
+
{
|
|
104
|
+
repo: result.repo,
|
|
105
|
+
pr: result.pr,
|
|
106
|
+
baseRef: result.baseBranch,
|
|
107
|
+
...(head.headSha && { headSha: head.headSha }),
|
|
108
|
+
...(head.headRef && { headRef: head.headRef }),
|
|
109
|
+
},
|
|
110
|
+
];
|
|
111
|
+
}
|
|
112
|
+
/** Every open PR of an aggregate tick. */
|
|
113
|
+
export function aggregateTargets(result) {
|
|
114
|
+
return result.prs
|
|
115
|
+
.filter((item) => item.state === "OPEN")
|
|
116
|
+
.map((item) => ({
|
|
117
|
+
repo: item.repo,
|
|
118
|
+
pr: item.pr,
|
|
119
|
+
headSha: item.headRefOid,
|
|
120
|
+
headRef: item.headRefName,
|
|
121
|
+
baseRef: item.baseRefName,
|
|
122
|
+
}));
|
|
123
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { IterateCommandOptions } from "../types.mts";
|
|
2
|
+
export interface PollCommandOptions extends IterateCommandOptions {
|
|
3
|
+
intervalSeconds: number;
|
|
4
|
+
timeoutSeconds: number;
|
|
5
|
+
/** Settle window after first TRIAGE before returning. Default 60. 0 disables. */
|
|
6
|
+
debounceSeconds?: number;
|
|
7
|
+
quietStatus?: boolean;
|
|
8
|
+
untilTerminal?: boolean;
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -6,6 +6,8 @@ export declare function writeWaitProgress(opts: {
|
|
|
6
6
|
tick: number;
|
|
7
7
|
elapsedMs: number;
|
|
8
8
|
sleepMs: number;
|
|
9
|
+
/** The pause waits for a hook-harness event, so `sleepMs` is not its length. */
|
|
10
|
+
viaEvents?: boolean;
|
|
9
11
|
result: WaitResult;
|
|
10
12
|
quietStatus: boolean;
|
|
11
13
|
verbose: boolean;
|
|
@@ -23,7 +23,7 @@ function waitSignature(result) {
|
|
|
23
23
|
reviewItemsSinceLatestCommit: activity.reviewItemsSinceLatestCommit.length,
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
|
-
function writeQuietStatus(tick, elapsedSeconds, sleepSeconds, result) {
|
|
26
|
+
function writeQuietStatus(tick, elapsedSeconds, sleepSeconds, result, viaEvents) {
|
|
27
27
|
const activeChecks = result.inProgressChecks ?? [];
|
|
28
28
|
const activeCheckText = activeChecks.map((c) => `${c.name} (${c.status})`).join(", ");
|
|
29
29
|
const active = activeChecks.length > 0 ? ` · active: ${activeCheckText}` : "";
|
|
@@ -33,7 +33,7 @@ function writeQuietStatus(tick, elapsedSeconds, sleepSeconds, result) {
|
|
|
33
33
|
const commitSeg = commitCount > 0 ? ` · ${commitCount} commits` : "";
|
|
34
34
|
const reviewRoundSeg = reviewRounds > 0 ? ` · ${reviewRounds} review rounds` : "";
|
|
35
35
|
const reviewSeg = reviewItems > 0 ? ` · ${reviewItems} review items since latest commit` : "";
|
|
36
|
-
process.stderr.write(`[poll tick ${tick} / +${elapsedSeconds}s] WAIT ${result.status}/${result.mergeStateStatus}/${result.reviewDecision ?? "NO_REVIEW_DECISION"}${active}${commitSeg}${reviewRoundSeg}${reviewSeg} — sleeping ${sleepSeconds}s\n`);
|
|
36
|
+
process.stderr.write(`[poll tick ${tick} / +${elapsedSeconds}s] WAIT ${result.status}/${result.mergeStateStatus}/${result.reviewDecision ?? "NO_REVIEW_DECISION"}${active}${commitSeg}${reviewRoundSeg}${reviewSeg} — ${viaEvents ? "waiting for an event" : `sleeping ${sleepSeconds}s`}\n`);
|
|
37
37
|
}
|
|
38
38
|
export function writeWaitProgress(opts) {
|
|
39
39
|
const elapsedSeconds = Math.round(opts.elapsedMs / 1000);
|
|
@@ -41,12 +41,14 @@ export function writeWaitProgress(opts) {
|
|
|
41
41
|
if (!opts.quietStatus) {
|
|
42
42
|
const reason = waitReason(opts.result);
|
|
43
43
|
// The tick interval is the caller's own `--interval`; only verbose mode repeats it.
|
|
44
|
-
writeTickProgress(opts.tick, elapsedSeconds, opts.verbose
|
|
44
|
+
writeTickProgress(opts.tick, elapsedSeconds, opts.verbose
|
|
45
|
+
? `${reason} — ${opts.viaEvents ? "waiting for an event" : `sleeping ${sleepSeconds}s`}`
|
|
46
|
+
: reason);
|
|
45
47
|
return opts.lastWaitSignature;
|
|
46
48
|
}
|
|
47
49
|
const signature = waitSignature(opts.result);
|
|
48
50
|
if (signature !== opts.lastWaitSignature) {
|
|
49
|
-
writeQuietStatus(opts.tick, elapsedSeconds, sleepSeconds, opts.result);
|
|
51
|
+
writeQuietStatus(opts.tick, elapsedSeconds, sleepSeconds, opts.result, opts.viaEvents === true);
|
|
50
52
|
}
|
|
51
53
|
return signature;
|
|
52
54
|
}
|
|
@@ -7,6 +7,8 @@ export declare function quotaPollIntervalMs(bands: GraphqlQuotaWarningBand[], us
|
|
|
7
7
|
graphql?: Pick<GraphqlApiUsage, "remaining" | "limit">;
|
|
8
8
|
rest?: Pick<ApiResourceUsage, "resource" | "remaining" | "limit">[];
|
|
9
9
|
} | undefined, fallbackMs: number, maxMs: number, transport?: "graphql" | "rest"): number;
|
|
10
|
+
/** The plain poll sleep for a single-PR tick: the interval raised by any quota band. */
|
|
11
|
+
export declare function onePrSleepMs(bands: GraphqlQuotaWarningBand[], usage: Parameters<typeof quotaPollIntervalMs>[1], intervalMs: number, maxMs: number): number;
|
|
10
12
|
export interface RateLimitRetry {
|
|
11
13
|
ms: number;
|
|
12
14
|
resource: string;
|
|
@@ -25,6 +25,10 @@ export function quotaPollIntervalMs(bands, usage, fallbackMs, maxMs, transport)
|
|
|
25
25
|
const core = usage?.rest?.find((item) => item.resource === "core");
|
|
26
26
|
return Math.max(graphqlQuotaPollIntervalMs(bands, transport === "rest" ? undefined : usage?.graphql, fallbackMs, maxMs), graphqlQuotaPollIntervalMs(bands, core, fallbackMs, maxMs));
|
|
27
27
|
}
|
|
28
|
+
/** The plain poll sleep for a single-PR tick: the interval raised by any quota band. */
|
|
29
|
+
export function onePrSleepMs(bands, usage, intervalMs, maxMs) {
|
|
30
|
+
return quotaPollIntervalMs(bands, usage, intervalMs, maxMs, getGithubTransport());
|
|
31
|
+
}
|
|
28
32
|
/**
|
|
29
33
|
* Retry delay for `--until-terminal` when GitHub exhausts a primary quota or
|
|
30
34
|
* returns a secondary limit. `null` means the error is not a retryable rate limit.
|
|
@@ -5,6 +5,7 @@ export interface AggregatePollCommandOptions extends PollSummaryCommandOptions {
|
|
|
5
5
|
debounceSeconds?: number;
|
|
6
6
|
quietStatus?: boolean;
|
|
7
7
|
untilTerminal?: boolean;
|
|
8
|
+
verbose?: boolean;
|
|
8
9
|
}
|
|
9
10
|
export declare function runPollSummary(opts: PollSummaryCommandOptions): Promise<PollSummaryResult>;
|
|
10
11
|
export declare function runAggregatePoll(opts: AggregatePollCommandOptions): Promise<PollSummaryResult>;
|
|
@@ -7,15 +7,15 @@ import { withApiTelemetryScope, summarizeApiTelemetry } from "../github/api-tele
|
|
|
7
7
|
import { aggregateRerunCommand } from "../github/poll-summary-command.mjs";
|
|
8
8
|
import { fetchPollSummary } from "../github/poll-summary.mjs";
|
|
9
9
|
import { writeStackHandoffs } from "../github/stack-handoff.mjs";
|
|
10
|
-
import { sleep } from "../util/sleep.mjs";
|
|
11
10
|
import { aggregateQuotaWarning, carryQuotaWarning, quotaPollIntervalMs } from "./poll-quota.mjs";
|
|
12
11
|
import { aggregateCancelFromPulls, aggregateRateLimitTargets } from "./poll-rate-limit-cancel.mjs";
|
|
13
12
|
import { createUntilTerminalRateLimitRetry } from "./poll-rate-limit-wait.mjs";
|
|
14
13
|
import { planPollSummary, withPollSummaryInstructions } from "./poll-summary-instructions.mjs";
|
|
15
14
|
import { summaryStatusSignature } from "./poll-summary-signature.mjs";
|
|
16
15
|
import { applyStackStallGuard } from "./stack-stall.mjs";
|
|
16
|
+
import { aggregateDeadline } from "./stall-deadline.mjs";
|
|
17
|
+
import { aggregateTargets, pauseBeforeNextTick, withPollEvents, } from "./poll-events.mjs";
|
|
17
18
|
const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
18
|
-
const TIMER_DRIFT_TOLERANCE_MS = 500;
|
|
19
19
|
export function runPollSummary(opts) {
|
|
20
20
|
return withApiTelemetryScope(async () => {
|
|
21
21
|
const tick = await readPollSummaryTick(opts);
|
|
@@ -24,7 +24,7 @@ export function runPollSummary(opts) {
|
|
|
24
24
|
});
|
|
25
25
|
}
|
|
26
26
|
export function runAggregatePoll(opts) {
|
|
27
|
-
return withApiTelemetryScope(() => runAggregatePollCore(opts));
|
|
27
|
+
return withPollEvents(opts.verbose === true, (events) => withApiTelemetryScope(() => runAggregatePollCore(opts, events)));
|
|
28
28
|
}
|
|
29
29
|
async function runPollSummaryCore(opts) {
|
|
30
30
|
return (await readPollSummaryTick(opts)).result;
|
|
@@ -65,7 +65,7 @@ async function readPollSummaryTick(opts) {
|
|
|
65
65
|
const result = await applyStackStallGuard(planned, repo, opts.stallTimeoutSeconds ?? loadConfig().iterate["stall-timeout-minutes"] * 60);
|
|
66
66
|
return { result, repo, ...(fetched.handoffSource && { handoffSource: fetched.handoffSource }) };
|
|
67
67
|
}
|
|
68
|
-
async function runAggregatePollCore(opts) {
|
|
68
|
+
async function runAggregatePollCore(opts, events) {
|
|
69
69
|
const intervalMs = Math.min(opts.intervalSeconds * 1000, MAX_TIMER_MS);
|
|
70
70
|
const timeoutMs = Math.min(opts.timeoutSeconds * 1000, MAX_TIMER_MS);
|
|
71
71
|
const debounceMs = Math.min((opts.debounceSeconds ?? 60) * 1000, MAX_TIMER_MS);
|
|
@@ -80,6 +80,7 @@ async function runAggregatePollCore(opts) {
|
|
|
80
80
|
let pendingQuotaWarning;
|
|
81
81
|
while (true) {
|
|
82
82
|
tick += 1;
|
|
83
|
+
const tickStartedAt = Date.now();
|
|
83
84
|
try {
|
|
84
85
|
lastTick = await readPollSummaryTick(opts);
|
|
85
86
|
last = lastTick.result;
|
|
@@ -178,20 +179,31 @@ async function runAggregatePollCore(opts) {
|
|
|
178
179
|
const sleepMs = debounceUntil
|
|
179
180
|
? Math.min(intervalMs, Math.max(debounceUntil - Date.now(), 0))
|
|
180
181
|
: quotaPollIntervalMs(quotaBands, summarizeApiTelemetry(), intervalMs, MAX_TIMER_MS, getGithubTransport());
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
182
|
+
const useEvents = debounceUntil ? undefined : events;
|
|
183
|
+
const deadline = useEvents && (await aggregateDeadline(last, opts.stallTimeoutSeconds));
|
|
184
|
+
const stop = await pauseBeforeNextTick({
|
|
185
|
+
// The settle window after a first TRIAGE is a plain timer, never an event wait.
|
|
186
|
+
events: useEvents,
|
|
187
|
+
...deadline,
|
|
188
|
+
targets: aggregateTargets(last),
|
|
189
|
+
recheck: last.prs.some((item) => item.mergeStateStatus === "UNKNOWN"),
|
|
190
|
+
sleepMs,
|
|
191
|
+
intervalMs,
|
|
192
|
+
label: `aggregate poll tick ${tick} / +${Math.round(elapsedMs / 1000)}s`,
|
|
193
|
+
tickStartedAt,
|
|
194
|
+
remainingMs: opts.untilTerminal || debounceUntil !== null ? Infinity : timeoutMs - elapsedMs,
|
|
195
|
+
announce: () => {
|
|
196
|
+
const statusSignature = summaryStatusSignature(last);
|
|
197
|
+
if (!opts.quietStatus || hasFix || statusSignature !== lastStatusSignature) {
|
|
198
|
+
process.stderr.write(`[aggregate poll tick ${tick} / +${Math.round(elapsedMs / 1000)}s] ${last.prs
|
|
199
|
+
.map((item) => `#${item.pr} ${item.action.toUpperCase()}`)
|
|
200
|
+
.join(", ")}\n`);
|
|
201
|
+
}
|
|
202
|
+
lastStatusSignature = statusSignature;
|
|
203
|
+
},
|
|
204
|
+
});
|
|
205
|
+
if (stop)
|
|
206
|
+
return attachUsage({ ...last, reason: "timeout" }, opts);
|
|
195
207
|
}
|
|
196
208
|
}
|
|
197
209
|
/** An auto-resolve is a one-tick event; forward it so a swallowed tick still records it. */
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { IterateResult } from "../types.mts";
|
|
2
|
+
import type { RateLimitProbePull, RateLimitPullTarget } from "./poll-rate-limit-cancel.mts";
|
|
3
|
+
import type { createUntilTerminalRateLimitRetry } from "./poll-rate-limit-wait.mts";
|
|
4
|
+
export interface TickRunnerInput {
|
|
5
|
+
/** One iterate read; `fingerprintCache` allows reuse of a previous WAIT-shaped report. */
|
|
6
|
+
run: (fingerprintCache: boolean) => Promise<IterateResult>;
|
|
7
|
+
retry: ReturnType<typeof createUntilTerminalRateLimitRetry>;
|
|
8
|
+
untilTerminal: boolean;
|
|
9
|
+
intervalMs: number;
|
|
10
|
+
tickLabel: string;
|
|
11
|
+
startedAt: number;
|
|
12
|
+
/** Read lazily: the last result can change between a tick and its retry. */
|
|
13
|
+
targets: () => RateLimitPullTarget[];
|
|
14
|
+
onAllTerminal: (pulls: RateLimitProbePull[]) => IterateResult | undefined;
|
|
15
|
+
}
|
|
16
|
+
/** One poll tick that waits out GitHub rate limits instead of failing. */
|
|
17
|
+
export declare function createTickRunner(input: TickRunnerInput): (fingerprintCache: boolean) => Promise<IterateResult>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** One poll tick that waits out GitHub rate limits instead of failing. */
|
|
2
|
+
export function createTickRunner(input) {
|
|
3
|
+
return async (fingerprintCache) => {
|
|
4
|
+
while (true) {
|
|
5
|
+
try {
|
|
6
|
+
const result = await input.run(fingerprintCache);
|
|
7
|
+
input.retry.reset();
|
|
8
|
+
return result;
|
|
9
|
+
}
|
|
10
|
+
catch (err) {
|
|
11
|
+
const early = await input.retry.wait(err, {
|
|
12
|
+
untilTerminal: input.untilTerminal,
|
|
13
|
+
intervalMs: input.intervalMs,
|
|
14
|
+
tickLabel: input.tickLabel,
|
|
15
|
+
startedAt: input.startedAt,
|
|
16
|
+
targets: input.targets(),
|
|
17
|
+
onAllTerminal: input.onAllTerminal,
|
|
18
|
+
});
|
|
19
|
+
if (early)
|
|
20
|
+
return early;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
package/bin/commands/poll.d.mts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
timeoutSeconds: number;
|
|
5
|
-
/** Settle window after first TRIAGE before returning. Default 60. 0 disables. */
|
|
6
|
-
debounceSeconds?: number;
|
|
7
|
-
quietStatus?: boolean;
|
|
8
|
-
untilTerminal?: boolean;
|
|
9
|
-
}
|
|
1
|
+
import type { IterateResult } from "../types.mts";
|
|
2
|
+
import type { PollCommandOptions } from "./poll-options.mts";
|
|
3
|
+
export type { PollCommandOptions } from "./poll-options.mts";
|
|
10
4
|
export declare function runPoll(opts: PollCommandOptions): Promise<IterateResult>;
|
package/bin/commands/poll.mjs
CHANGED
|
@@ -4,18 +4,20 @@ import { markEditedCommentsSeen } from "./iterate/edited-comment-markers.mjs";
|
|
|
4
4
|
import { sleep } from "../util/sleep.mjs";
|
|
5
5
|
import { withPollApiUsage } from "./poll-run.mjs";
|
|
6
6
|
import { loadConfig } from "../config/load.mjs";
|
|
7
|
-
import { carryQuotaWarning,
|
|
7
|
+
import { carryQuotaWarning, onePrSleepMs } from "./poll-quota.mjs";
|
|
8
8
|
import { onePrCancelFromPulls, onePrRateLimitTargets } from "./poll-rate-limit-cancel.mjs";
|
|
9
9
|
import { createUntilTerminalRateLimitRetry } from "./poll-rate-limit-wait.mjs";
|
|
10
|
+
import { createTickRunner } from "./poll-tick.mjs";
|
|
10
11
|
import { writeDebounceProgress, writeRuleAutoResolveProgress, writeWaitProgress, } from "./poll-progress.mjs";
|
|
11
|
-
import {
|
|
12
|
+
import { onePrDeadline } from "./stall-deadline.mjs";
|
|
13
|
+
import { onePrTargets, pauseBeforeNextTick, withPollEvents, } from "./poll-events.mjs";
|
|
12
14
|
const DEFAULT_POLL_DEBOUNCE_SECONDS = 60;
|
|
13
15
|
const MAX_TIMER_MS = 2 ** 31 - 1;
|
|
14
16
|
const TIMER_DRIFT_TOLERANCE_MS = 500;
|
|
15
17
|
export function runPoll(opts) {
|
|
16
|
-
return withPollApiUsage(() => runPollCore(opts), opts.untilTerminal === true, opts.intervalSeconds / 60);
|
|
18
|
+
return withPollEvents(opts.verbose === true, (events) => withPollApiUsage(() => runPollCore(opts, events), opts.untilTerminal === true, opts.intervalSeconds / 60));
|
|
17
19
|
}
|
|
18
|
-
async function runPollCore(opts) {
|
|
20
|
+
async function runPollCore(opts, events) {
|
|
19
21
|
const { intervalSeconds, timeoutSeconds, debounceSeconds: debounceSecondsOpt, quietStatus: quietStatusOpt, untilTerminal: untilTerminalOpt, ...iterateOpts } = opts;
|
|
20
22
|
const intervalMs = Math.min(intervalSeconds * 1000, MAX_TIMER_MS);
|
|
21
23
|
const timeoutMs = Math.min(timeoutSeconds * 1000, MAX_TIMER_MS);
|
|
@@ -34,43 +36,34 @@ async function runPollCore(opts) {
|
|
|
34
36
|
let prNumber = opts.prNumber;
|
|
35
37
|
let debounceUntil = null;
|
|
36
38
|
const rateLimitRetry = createUntilTerminalRateLimitRetry();
|
|
39
|
+
const head = {};
|
|
37
40
|
while (true) {
|
|
38
41
|
tick += 1;
|
|
42
|
+
const tickStartedAt = Date.now();
|
|
39
43
|
const pastDebounce = debounceUntil !== null && Date.now() >= debounceUntil;
|
|
40
44
|
const seenOptions = pollTickSeenOptions(debounceSeconds, pastDebounce);
|
|
41
45
|
const remainingBefore = untilTerminal
|
|
42
46
|
? Number.POSITIVE_INFINITY
|
|
43
47
|
: timeoutMs - (Date.now() - start);
|
|
44
48
|
const allowCache = debounceUntil === null && remainingBefore + TIMER_DRIFT_TOLERANCE_MS >= intervalMs;
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
const runTick = createTickRunner({
|
|
50
|
+
run: (fingerprintCache) => runIterate({
|
|
51
|
+
...iterateOpts,
|
|
52
|
+
prNumber,
|
|
53
|
+
...seenOptions,
|
|
54
|
+
...(events && { eventTargetSink: head, stallDeadlineSink: head }),
|
|
55
|
+
fingerprintCache,
|
|
56
|
+
deferQuotaWarning: !untilTerminal,
|
|
57
|
+
quotaWarningMinimumPollIntervalMinutes: intervalSeconds / 60,
|
|
58
|
+
}),
|
|
59
|
+
retry: rateLimitRetry,
|
|
60
|
+
untilTerminal,
|
|
61
|
+
intervalMs,
|
|
62
|
+
tickLabel: `poll tick ${tick}`,
|
|
63
|
+
startedAt: start,
|
|
64
|
+
targets: () => onePrRateLimitTargets(iterateOpts.targetRepository, prNumber, lastResult?.repo),
|
|
65
|
+
onAllTerminal: (pulls) => onePrCancelFromPulls(iterateOpts.targetRepository, lastResult?.repo, pulls),
|
|
52
66
|
});
|
|
53
|
-
const runTick = async (fingerprintCache) => {
|
|
54
|
-
while (true) {
|
|
55
|
-
try {
|
|
56
|
-
const result = await iterateTick(fingerprintCache);
|
|
57
|
-
rateLimitRetry.reset();
|
|
58
|
-
return result;
|
|
59
|
-
}
|
|
60
|
-
catch (err) {
|
|
61
|
-
const early = await rateLimitRetry.wait(err, {
|
|
62
|
-
untilTerminal,
|
|
63
|
-
intervalMs,
|
|
64
|
-
tickLabel: `poll tick ${tick}`,
|
|
65
|
-
startedAt: start,
|
|
66
|
-
targets: onePrRateLimitTargets(iterateOpts.targetRepository, prNumber, lastResult?.repo),
|
|
67
|
-
onAllTerminal: (pulls) => onePrCancelFromPulls(iterateOpts.targetRepository, lastResult?.repo, pulls),
|
|
68
|
-
});
|
|
69
|
-
if (early)
|
|
70
|
-
return early;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
67
|
lastResult = await runTick(allowCache);
|
|
75
68
|
prNumber ??= lastResult.pr;
|
|
76
69
|
pendingQuotaWarning = carryQuotaWarning(pendingQuotaWarning, lastResult.quotaWarning);
|
|
@@ -80,6 +73,23 @@ async function runPollCore(opts) {
|
|
|
80
73
|
lastResult = await runTick(false);
|
|
81
74
|
pendingQuotaWarning = carryQuotaWarning(pendingQuotaWarning, lastResult.quotaWarning);
|
|
82
75
|
};
|
|
76
|
+
/** Wait for the next tick; true when the poll window ends first and the poll should stop. */
|
|
77
|
+
const pause = async (result, announce) => {
|
|
78
|
+
const deadline = events && (await onePrDeadline(result, iterateOpts, head));
|
|
79
|
+
const sleepMs = onePrSleepMs(quotaBands, result.apiUsage, intervalMs, MAX_TIMER_MS);
|
|
80
|
+
return pauseBeforeNextTick({
|
|
81
|
+
events,
|
|
82
|
+
targets: onePrTargets(result, head),
|
|
83
|
+
recheck: result.mergeStateStatus === "UNKNOWN",
|
|
84
|
+
sleepMs,
|
|
85
|
+
intervalMs,
|
|
86
|
+
label: `poll tick ${tick} / +${Math.round((Date.now() - start) / 1000)}s`,
|
|
87
|
+
tickStartedAt,
|
|
88
|
+
...deadline,
|
|
89
|
+
remainingMs: untilTerminal ? Infinity : timeoutMs - (Date.now() - start),
|
|
90
|
+
announce: (viaEvents) => announce(sleepMs, viaEvents),
|
|
91
|
+
});
|
|
92
|
+
};
|
|
83
93
|
if (untilTerminal &&
|
|
84
94
|
pendingQuotaWarning !== undefined &&
|
|
85
95
|
!(lastResult.action === "triage" && debounceSeconds > 0 && !pastDebounce) &&
|
|
@@ -106,40 +116,34 @@ async function runPollCore(opts) {
|
|
|
106
116
|
if (pendingQuotaWarning === undefined)
|
|
107
117
|
debounceUntil = null;
|
|
108
118
|
const elapsedMs = Date.now() - start;
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
quietStatus,
|
|
123
|
-
verbose,
|
|
124
|
-
lastWaitSignature,
|
|
119
|
+
const waiting = lastResult;
|
|
120
|
+
const stop = await pause(waiting, (sleepMs, viaEvents) => {
|
|
121
|
+
lastWaitSignature = writeWaitProgress({
|
|
122
|
+
tick,
|
|
123
|
+
elapsedMs,
|
|
124
|
+
sleepMs,
|
|
125
|
+
viaEvents,
|
|
126
|
+
result: waiting,
|
|
127
|
+
quietStatus,
|
|
128
|
+
verbose,
|
|
129
|
+
lastWaitSignature,
|
|
130
|
+
});
|
|
131
|
+
writeRuleAutoResolveProgress(waiting);
|
|
125
132
|
});
|
|
126
|
-
|
|
127
|
-
|
|
133
|
+
if (stop) {
|
|
134
|
+
await refreshIfReturning();
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
128
137
|
continue;
|
|
129
138
|
}
|
|
130
139
|
if (swallowsMarkReady && lastResult.action === "mark_ready" && !pastDebounce) {
|
|
131
140
|
debounceUntil = null;
|
|
132
|
-
const
|
|
133
|
-
const
|
|
134
|
-
if (
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
await refreshIfReturning();
|
|
138
|
-
break;
|
|
139
|
-
}
|
|
141
|
+
const ready = lastResult;
|
|
142
|
+
const stop = await pause(ready, () => writeRuleAutoResolveProgress(ready));
|
|
143
|
+
if (stop) {
|
|
144
|
+
await refreshIfReturning();
|
|
145
|
+
break;
|
|
140
146
|
}
|
|
141
|
-
writeRuleAutoResolveProgress(lastResult);
|
|
142
|
-
await sleep(sleepMs);
|
|
143
147
|
continue;
|
|
144
148
|
}
|
|
145
149
|
if (lastResult.action === "triage" && debounceSeconds > 0 && !pastDebounce) {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { IterateResult, PollSummaryResult } from "../types.mts";
|
|
2
|
+
export declare function earliestDeadline(...deadlines: (number | undefined)[]): number | undefined;
|
|
3
|
+
/**
|
|
4
|
+
* The `deadlineAt` (epoch ms) for an aggregate tick's event wait: a stack stall timer or a
|
|
5
|
+
* ready-delay countdown. No event announces either, so the wait must not outlast them.
|
|
6
|
+
*/
|
|
7
|
+
export declare function aggregateDeadline(result: PollSummaryResult, stallTimeoutSeconds: number | undefined): Promise<WaitDeadline | undefined>;
|
|
8
|
+
/** The `deadlineAt` for a single-PR wait: the stall timeout or the CI-start timeout. */
|
|
9
|
+
export declare function onePrDeadline(result: IterateResult, opts: {
|
|
10
|
+
stallTimeoutSeconds?: number;
|
|
11
|
+
}, sink: {
|
|
12
|
+
ciStartDeadlineSeconds?: number;
|
|
13
|
+
}): Promise<WaitDeadline | undefined>;
|
|
14
|
+
/** A capped event wait's deadline (epoch ms) and the timer it belongs to, for the wake log. */
|
|
15
|
+
export interface WaitDeadline {
|
|
16
|
+
deadlineAt: number;
|
|
17
|
+
deadlineReason: string;
|
|
18
|
+
}
|
|
19
|
+
/** When an unchanged state would trip the stall timeout, or undefined when no timer is running. */
|
|
20
|
+
export declare function readStallDeadline(result: IterateResult, stallTimeoutSeconds: number | undefined): Promise<number | undefined>;
|