pr-shepherd 2.1.0 → 2.2.1
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 +28 -22
- package/bin/api.d.mts +10 -2
- package/bin/api.mjs +30 -6
- package/bin/checks/pending-deployments.d.mts +10 -0
- package/bin/checks/pending-deployments.mjs +66 -0
- package/bin/checks/run-workflow-paths.d.mts +7 -0
- package/bin/checks/run-workflow-paths.mjs +19 -0
- package/bin/checks/triage.mjs +3 -2
- package/bin/checks/unreported-required.d.mts +11 -1
- package/bin/checks/unreported-required.mjs +8 -3
- package/bin/checks/workflow-concurrency.d.mts +9 -0
- package/bin/checks/workflow-concurrency.mjs +84 -0
- package/bin/checks/workflow-path.d.mts +2 -0
- package/bin/checks/workflow-path.mjs +4 -0
- package/bin/cli/arg-flags.mjs +3 -0
- package/bin/cli/clean-handler.mjs +1 -1
- package/bin/cli/default-poll.mjs +1 -0
- package/bin/cli/handlers.mjs +2 -0
- package/bin/cli/help-command-pages.d.mts +5 -4
- package/bin/cli/help-command-pages.mjs +2 -1
- package/bin/cli/help-iterate-poll-pages.d.mts +3 -3
- package/bin/cli/help-iterate-poll-pages.mjs +10 -2
- package/bin/cli/help-top-page.d.mts +1 -1
- package/bin/cli/help-top-page.mjs +2 -2
- package/bin/cli/help.d.mts +6 -5
- package/bin/cli/in-progress-run-format.d.mts +3 -0
- package/bin/cli/in-progress-run-format.mjs +14 -0
- package/bin/cli/iterate-activity-formatter.d.mts +2 -0
- package/bin/cli/iterate-activity-formatter.mjs +11 -0
- package/bin/cli/iterate-flags.d.mts +3 -0
- package/bin/cli/iterate-flags.mjs +18 -1
- package/bin/cli/iterate-formatter.mjs +2 -1
- package/bin/cli/iterate-lean.mjs +2 -0
- package/bin/cli/pending-deployments-format.d.mts +5 -0
- package/bin/cli/pending-deployments-format.mjs +19 -0
- package/bin/cli/poll-handler.d.mts +1 -1
- package/bin/cli/poll-handler.mjs +33 -10
- package/bin/cli/poll-targets.mjs +2 -0
- package/bin/cli/stack-merge-args.d.mts +10 -0
- package/bin/cli/stack-merge-args.mjs +27 -0
- package/bin/cli/stack-overview.d.mts +1 -0
- package/bin/cli/stack-overview.mjs +5 -1
- package/bin/cli/stalled-check-format.d.mts +3 -0
- package/bin/cli/stalled-check-format.mjs +23 -0
- package/bin/cli/suggestion-renderer.mjs +6 -0
- package/bin/cli/triage-formatter.mjs +14 -0
- package/bin/commands/check.mjs +7 -2
- package/bin/commands/clean.d.mts +1 -1
- package/bin/commands/clean.mjs +7 -0
- package/bin/commands/iterate/check-follow-up.d.mts +9 -0
- package/bin/commands/iterate/check-follow-up.mjs +34 -0
- package/bin/commands/iterate/ci-start-stall.d.mts +23 -0
- package/bin/commands/iterate/ci-start-stall.mjs +33 -0
- package/bin/commands/iterate/duration-approx.d.mts +2 -0
- package/bin/commands/iterate/duration-approx.mjs +8 -0
- package/bin/commands/iterate/escalate.d.mts +8 -17
- package/bin/commands/iterate/escalate.mjs +19 -48
- package/bin/commands/iterate/helpers.mjs +1 -0
- package/bin/commands/iterate/in-progress-runs.d.mts +10 -0
- package/bin/commands/iterate/in-progress-runs.mjs +120 -0
- package/bin/commands/iterate/index.mjs +12 -6
- package/bin/commands/iterate/merge-plan.d.mts +2 -0
- package/bin/commands/iterate/merge-plan.mjs +2 -1
- package/bin/commands/iterate/merge-state.d.mts +2 -0
- package/bin/commands/iterate/merge-state.mjs +8 -21
- package/bin/commands/iterate/native-stack-rebase.d.mts +3 -2
- package/bin/commands/iterate/native-stack-rebase.mjs +32 -11
- package/bin/commands/iterate/queue-removal.d.mts +19 -0
- package/bin/commands/iterate/queue-removal.mjs +64 -0
- package/bin/commands/iterate/render.d.mts +2 -1
- package/bin/commands/iterate/render.mjs +4 -1
- package/bin/commands/iterate/stack-boundary.d.mts +17 -0
- package/bin/commands/iterate/stack-boundary.mjs +95 -0
- package/bin/commands/iterate/stall-repair.d.mts +17 -0
- package/bin/commands/iterate/stall-repair.mjs +25 -0
- package/bin/commands/iterate/stall.d.mts +1 -1
- package/bin/commands/iterate/stall.mjs +27 -18
- package/bin/commands/iterate/state-unavailable.d.mts +8 -0
- package/bin/commands/iterate/state-unavailable.mjs +23 -0
- package/bin/commands/iterate/triage-loop-instruction.mjs +1 -1
- package/bin/commands/iterate/triage.mjs +41 -74
- package/bin/commands/iterate/unreported-required.d.mts +5 -2
- package/bin/commands/iterate/unreported-required.mjs +13 -8
- package/bin/commands/iterate/workflow-files.d.mts +27 -0
- package/bin/commands/iterate/workflow-files.mjs +85 -0
- package/bin/commands/poll-summary-explicit-instructions.mjs +15 -1
- package/bin/commands/poll-summary-instructions.d.mts +4 -3
- package/bin/commands/poll-summary-instructions.mjs +60 -22
- package/bin/commands/poll-summary.mjs +38 -10
- package/bin/commands/poll.mjs +4 -2
- package/bin/commands/stack-drain.d.mts +4 -4
- package/bin/commands/stack-drain.mjs +19 -113
- package/bin/commands/stack-handoffs.d.mts +3 -3
- package/bin/commands/stack-handoffs.mjs +8 -8
- package/bin/commands/stack-lower-ready-note.d.mts +6 -0
- package/bin/commands/stack-lower-ready-note.mjs +35 -0
- package/bin/commands/stack-merge-escalations.d.mts +17 -0
- package/bin/commands/stack-merge-escalations.mjs +57 -0
- package/bin/commands/stack-merge-plan.d.mts +10 -0
- package/bin/commands/stack-merge-plan.mjs +132 -0
- package/bin/commands/stack-merge-scope.d.mts +42 -0
- package/bin/commands/stack-merge-scope.mjs +113 -0
- package/bin/commands/stack-ownership.d.mts +25 -0
- package/bin/commands/stack-ownership.mjs +85 -0
- package/bin/commands/stack-stall.d.mts +2 -0
- package/bin/commands/stack-stall.mjs +25 -3
- package/bin/commands/stack-targets-error.d.mts +16 -0
- package/bin/commands/stack-targets-error.mjs +44 -0
- package/bin/commands/stack-targets.d.mts +30 -0
- package/bin/commands/stack-targets.mjs +121 -0
- package/bin/commands/stack-work.d.mts +4 -3
- package/bin/commands/stack-work.mjs +7 -4
- package/bin/commands/tick.mjs +14 -2
- package/bin/config/renames.mjs +1 -1
- package/bin/config/schema.d.mts +4 -0
- package/bin/config/schema.mjs +2 -0
- package/bin/github/batch-parse-suites.d.mts +4 -1
- package/bin/github/batch-parse-suites.mjs +15 -3
- package/bin/github/batch-raw-types.d.mts +6 -0
- package/bin/github/gql/commit-check-suites.gql +6 -0
- package/bin/github/gql/poll-summary-fragment.gql +6 -0
- package/bin/github/graphql-http.mjs +8 -6
- package/bin/github/graphql-tolerated-errors.d.mts +3 -0
- package/bin/github/graphql-tolerated-errors.mjs +20 -0
- package/bin/github/poll-summary-command.d.mts +15 -1
- package/bin/github/poll-summary-command.mjs +39 -3
- package/bin/github/poll-summary-projector.d.mts +1 -1
- package/bin/github/poll-summary-projector.mjs +1 -4
- package/bin/github/poll-summary-raw.d.mts +6 -0
- package/bin/github/poll-summary-route.mjs +4 -1
- package/bin/github/poll-summary.mjs +10 -3
- package/bin/github/rest-batch-read.mjs +17 -6
- package/bin/github/rest-batch-snapshot.d.mts +2 -2
- package/bin/github/rest-batch-snapshot.mjs +3 -0
- package/bin/github/rest-check-read.d.mts +1 -18
- package/bin/github/rest-check-read.mjs +1 -2
- package/bin/github/rest-stack-read.d.mts +9 -0
- package/bin/github/rest-stack-read.mjs +40 -0
- package/bin/github/rest-suite-types.d.mts +23 -0
- package/bin/github/rest-suite-types.mjs +1 -0
- package/bin/github/rest-summary-read.mjs +2 -0
- package/bin/mcp/server.mjs +29 -3
- package/bin/pr-reference.d.mts +2 -0
- package/bin/pr-reference.mjs +1 -1
- package/bin/reporters/agent.mjs +3 -0
- package/bin/state/check-follow-up.d.mts +30 -0
- package/bin/state/check-follow-up.mjs +51 -0
- package/bin/state/ci-start-follow-up.d.mts +14 -0
- package/bin/state/ci-start-follow-up.mjs +21 -0
- package/bin/state/queue-removal-follow-up.d.mts +20 -0
- package/bin/state/queue-removal-follow-up.mjs +23 -0
- package/bin/state/signature-marker.d.mts +10 -0
- package/bin/state/signature-marker.mjs +28 -0
- package/bin/state/stack-merge-approval.d.mts +26 -0
- package/bin/state/stack-merge-approval.mjs +39 -0
- package/bin/state/stall-repair.d.mts +16 -0
- package/bin/state/stall-repair.mjs +47 -0
- package/bin/types/activity.d.mts +2 -1
- package/bin/types/escalate.d.mts +9 -2
- package/bin/types/github.d.mts +18 -0
- package/bin/types/in-progress-run.d.mts +20 -0
- package/bin/types/in-progress-run.mjs +1 -0
- package/bin/types/iterate.d.mts +22 -1
- package/bin/types/poll-summary.d.mts +18 -1
- package/bin/types/report.d.mts +5 -1
- package/bin/types.d.mts +1 -0
- package/bin/types.mjs +1 -0
- package/package.json +2 -2
- 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/plugins/pr-shepherd/skills/pr-shepherd/SKILL.md +8 -7
- package/plugins/pr-shepherd/skills/pr-shepherd/references/check-follow-up.md +14 -0
- package/plugins/pr-shepherd/skills/pr-shepherd/references/ci-failure-triage.md +6 -5
- package/plugins/pr-shepherd/skills/pr-shepherd/references/ci-start-stall.md +15 -0
- package/plugins/pr-shepherd/skills/pr-shepherd/references/create-a-pr.md +4 -1
- package/plugins/pr-shepherd/skills/pr-shepherd/references/mcp-fallback.md +1 -1
- package/plugins/pr-shepherd/skills/pr-shepherd/references/merge-queue-ejection.md +6 -5
- package/plugins/pr-shepherd/skills/pr-shepherd/references/required-checks-unreported.md +16 -0
- package/pr-shepherd.schema.json +11 -0
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { stackPosition } from "./stack-merge-scope.mjs";
|
|
2
|
+
import { stackRerunTarget } from "./stack-work.mjs";
|
|
3
|
+
/** `#41 (@alice, "Add parser")` for each layer, so the user can recognize whose work it is. */
|
|
4
|
+
function describeLayers(items) {
|
|
5
|
+
return items
|
|
6
|
+
.map((i) => `#${i.pr} (${i.authorLogin ? `@${i.authorLogin}, ` : ""}"${i.title}")`)
|
|
7
|
+
.join(", ");
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The escalation for a merge that would land layers the caller did not list. It names each layer
|
|
11
|
+
* with its author and title, says what merging would do, and prints the commands that approve.
|
|
12
|
+
*/
|
|
13
|
+
export function unlistedEscalation(target, unlisted, rerunCommand, approve) {
|
|
14
|
+
const commands = approve?.(unlisted.map((item) => item.pr));
|
|
15
|
+
const owner = unlisted.length === 1 ? "its owner" : "their owners";
|
|
16
|
+
const them = unlisted.length === 1 ? "that layer" : "those layers";
|
|
17
|
+
const listAll = commands
|
|
18
|
+
? `run \`${commands.listed}\``
|
|
19
|
+
: `rerun ${stackRerunTarget(rerunCommand)} with those PRs listed after \`stack\``;
|
|
20
|
+
const approval = commands
|
|
21
|
+
? `If they approve, run that same command, or \`${commands.whole}\` (the stack number owns every layer). Otherwise wait for ${owner}.`
|
|
22
|
+
: `They either approve it, and you ${listAll}, or wait for ${owner}.`;
|
|
23
|
+
return escalation(`\`unowned-stack-layers\`: merging PR #${target.pr} also merges ${describeLayers(unlisted)}, which you did not list as yours. Nothing was merged, and never merge only #${target.pr} while leaving ${them} open. If the user's request already covers ${them} (they asked for the whole stack, or for every layer up to #${target.pr}), ${listAll} now. Otherwise stop and tell the user that merging #${target.pr} also merges ${list(unlisted)}. ${approval}`);
|
|
24
|
+
}
|
|
25
|
+
export const list = (items) => items.map((i) => `#${i.pr}`).join(", ");
|
|
26
|
+
/** Layers that joined the stack after the merge was requested: the request never covered them. */
|
|
27
|
+
export function addedLayersEscalation(result, open, scope, rerunCommand) {
|
|
28
|
+
const added = result.addedLayers ?? [];
|
|
29
|
+
if (added.length === 0)
|
|
30
|
+
return undefined;
|
|
31
|
+
const approved = open.filter((item) => !added.includes(item.pr));
|
|
32
|
+
const lastApproved = approved.reduce((top, item) => (!top || stackPosition(item) > stackPosition(top) ? item : top), undefined);
|
|
33
|
+
const above = !!lastApproved &&
|
|
34
|
+
!open.some((item) => added.includes(item.pr) && stackPosition(item) < stackPosition(lastApproved));
|
|
35
|
+
const recovery = !lastApproved
|
|
36
|
+
? "If no, merge nothing."
|
|
37
|
+
: above
|
|
38
|
+
? `If no, rerun with \`--merge-through ${lastApproved.pr}\`, the last approved PR.`
|
|
39
|
+
: "If no, do not use `--merge-through`: the new layer sits below an approved one and would land with it. Rerun with only the approved PRs listed after `stack`, which stops at the unowned layer, or wait for its owner.";
|
|
40
|
+
const request = scope.kind === "ready"
|
|
41
|
+
? "so `--merge-ready` does not cover "
|
|
42
|
+
: "so the request does not cover ";
|
|
43
|
+
return escalation(`\`unapproved-stack-layer\`: ${added.map((pr) => `PR #${pr}`).join(", ")} joined this stack after the merge was requested, ${request}${added.length === 1 ? "it" : "them"}. Nothing was merged. Stop and ask the user whether ${added.length === 1 ? "that layer is" : "those layers are"} covered. If yes, rerun ${stackRerunTarget(rerunCommand)} with the covered PRs listed explicitly after \`stack\`. ${recovery}`);
|
|
44
|
+
}
|
|
45
|
+
export function scopeDone(open) {
|
|
46
|
+
const many = open.length !== 1;
|
|
47
|
+
return {
|
|
48
|
+
action: "cancel",
|
|
49
|
+
stackMergeable: true,
|
|
50
|
+
instructions: [
|
|
51
|
+
`1. Stop — every layer in the merge scope is merged. ${list(open)} ${many ? "stay" : "stays"} open outside it; leave ${many ? "them" : "it"} to ${many ? "their" : "its"} owner.`,
|
|
52
|
+
],
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export function escalation(text) {
|
|
56
|
+
return { action: "escalate", stackMergeable: false, instructions: [`1. ${text}`] };
|
|
57
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { PollSummaryResult, StackMergeScope } from "../types.mts";
|
|
2
|
+
import { type StackPlan } from "./stack-drain.mts";
|
|
3
|
+
import { type ApproveCommands } from "./stack-merge-escalations.mts";
|
|
4
|
+
export type { ApproveCommands };
|
|
5
|
+
/**
|
|
6
|
+
* The merge the requested scope allows, an escalation when the scope cannot be merged safely, or
|
|
7
|
+
* undefined when ordinary shepherding continues. A merge lands its target and every unmerged layer
|
|
8
|
+
* below it, so it is only emitted once every open layer it would land is ready and owned.
|
|
9
|
+
*/
|
|
10
|
+
export declare function planStackMerge(result: PollSummaryResult, scope: StackMergeScope | undefined, rerunCommand?: string, approve?: ApproveCommands): StackPlan | undefined;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { getGithubTransport } from "../github/transport.mjs";
|
|
2
|
+
import { buildMergeCommandPlan, renderMergeCommand } from "./iterate/merge.mjs";
|
|
3
|
+
import { appendAutonomousInstructions, idleWaitPlan, appendHumanHandoffInstructions, findHumanHandoffs, hasListedOwnership, } from "./stack-drain.mjs";
|
|
4
|
+
import { isStackLayerReady, aboveThroughTarget, closedInScope, mergeScopeLayers, openLayers, readyPrefix, stackPosition, } from "./stack-merge-scope.mjs";
|
|
5
|
+
import { stackMergeFlag } from "./stack-merge-flag.mjs";
|
|
6
|
+
import { addedLayersEscalation, escalation, list, scopeDone, unlistedEscalation, } from "./stack-merge-escalations.mjs";
|
|
7
|
+
import { splitStackWork, stackRerunTarget } from "./stack-work.mjs";
|
|
8
|
+
const NO_PER_LAYER = "One command lands every listed layer. Do not merge the layers one at a time with `gh pr merge`, and do not rebase or push between layers; GitHub retargets the remaining layers itself.";
|
|
9
|
+
/**
|
|
10
|
+
* The merge the requested scope allows, an escalation when the scope cannot be merged safely, or
|
|
11
|
+
* undefined when ordinary shepherding continues. A merge lands its target and every unmerged layer
|
|
12
|
+
* below it, so it is only emitted once every open layer it would land is ready and owned.
|
|
13
|
+
*/
|
|
14
|
+
export function planStackMerge(result, scope, rerunCommand, approve) {
|
|
15
|
+
const open = openLayers(result);
|
|
16
|
+
if (!scope || open.length === 0)
|
|
17
|
+
return undefined;
|
|
18
|
+
const added = addedLayersEscalation(result, open, scope, rerunCommand);
|
|
19
|
+
if (scope.kind === "ready") {
|
|
20
|
+
if (added)
|
|
21
|
+
return added;
|
|
22
|
+
const prefix = readyPrefix(result, true);
|
|
23
|
+
const top = prefix.at(-1);
|
|
24
|
+
if (top)
|
|
25
|
+
return mergePlan(result, top, scope, rerunCommand);
|
|
26
|
+
const bottom = open.reduce((low, item) => stackPosition(item) < stackPosition(low) ? item : low);
|
|
27
|
+
if (!bottom.owned && isStackLayerReady(bottom) && open.some((item) => item.owned)) {
|
|
28
|
+
const above = open.filter((item) => item.owned).at(-1);
|
|
29
|
+
return unlistedEscalation(above, [bottom], rerunCommand, approve);
|
|
30
|
+
}
|
|
31
|
+
return open.some((item) => item.owned) || closedInScope(result, scope).length > 0
|
|
32
|
+
? undefined
|
|
33
|
+
: scopeDone(open);
|
|
34
|
+
}
|
|
35
|
+
if (scope.kind === "through") {
|
|
36
|
+
const row = result.prs.find((item) => item.pr === scope.pr);
|
|
37
|
+
if (!row || row.state === "CLOSED") {
|
|
38
|
+
return escalation(`\`merge-through-target-closed\`: the \`--merge-through\` target PR #${scope.pr} ${row ? "was closed without merging" : "is no longer a layer of this stack"}, so the request no longer names a layer to merge up to. Nothing was merged. Stop and tell the user. They either name another target, and you rerun ${stackRerunTarget(rerunCommand)} with that PR as \`--merge-through\`, or restore PR #${scope.pr}.`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const { target } = mergeScopeLayers(result, scope);
|
|
42
|
+
if (!target)
|
|
43
|
+
return closedInScope(result, scope).length > 0 ? undefined : scopeDone(open);
|
|
44
|
+
if (added)
|
|
45
|
+
return added;
|
|
46
|
+
const unowned = open.filter((item) => !item.owned && stackPosition(item) <= stackPosition(target));
|
|
47
|
+
if (unowned.length > 0)
|
|
48
|
+
return unlistedEscalation(target, unowned, rerunCommand, approve);
|
|
49
|
+
return readyPrefix(result, false).some((item) => item.pr === target.pr)
|
|
50
|
+
? mergePlan(result, target, scope, rerunCommand)
|
|
51
|
+
: undefined;
|
|
52
|
+
}
|
|
53
|
+
/** The one merge for `top`, which lands it and every unmerged layer below it. */
|
|
54
|
+
function mergePlan(result, top, scope, rerunCommand) {
|
|
55
|
+
const stack = top.stack;
|
|
56
|
+
const outside = aboveThroughTarget(result, scope);
|
|
57
|
+
const open = openLayers(result);
|
|
58
|
+
const lands = open.filter((item) => stackPosition(item) <= stackPosition(top));
|
|
59
|
+
const above = open.filter((item) => stackPosition(item) > stackPosition(top));
|
|
60
|
+
const gaps = result.stackAncestry ?? [];
|
|
61
|
+
const staleChildren = new Set(gaps.map((gap) => gap.childPr));
|
|
62
|
+
const work = splitStackWork(above.filter((item) => item.action !== "escalate" &&
|
|
63
|
+
!outside.has(item.pr) &&
|
|
64
|
+
(!isStackLayerReady(item) || staleChildren.has(item.pr))), staleChildren);
|
|
65
|
+
const landing = lands.length === 1 ? `PR #${top.pr} alone` : `PRs ${list(lands)}`;
|
|
66
|
+
const partial = result.selection.kind === "stack" && result.selection.owned !== undefined;
|
|
67
|
+
const ownership = partial
|
|
68
|
+
? ` (${above.every((item) => item.owned) ? "owned" : above.some((item) => item.owned) ? "owned and unowned" : "not owned"})`
|
|
69
|
+
: "";
|
|
70
|
+
const left = above.length > 0
|
|
71
|
+
? ` ${list(above)} stay${above.length === 1 ? "s" : ""} open above it${ownership}; GitHub retargets ${above.length === 1 ? "it" : "them"} onto \`${stack.baseRefName}\`.`
|
|
72
|
+
: "";
|
|
73
|
+
const first = mergeStep(result, top, landing, left);
|
|
74
|
+
if ("unavailable" in first) {
|
|
75
|
+
if (!first.refresh)
|
|
76
|
+
return escalation(first.unavailable);
|
|
77
|
+
const waiting = idleWaitPlan(lands);
|
|
78
|
+
return { ...waiting, instructions: [`1. ${first.unavailable}`] };
|
|
79
|
+
}
|
|
80
|
+
const instructions = [first.text];
|
|
81
|
+
appendAutonomousInstructions(instructions, work.sessions, hasListedOwnership(result));
|
|
82
|
+
const handoffs = findHumanHandoffs(result);
|
|
83
|
+
if (handoffs)
|
|
84
|
+
appendHumanHandoffInstructions(instructions, handoffs, false);
|
|
85
|
+
instructions.push(`${instructions.length + 1}. After the merge attempt, rerun ${rerunCommand ? stackRerunTarget(rerunCommand) : "this same `stack` merge command"}; GitHub retargets the next layer onto \`${stack.baseRefName}\`. Shepherd any layer that GitHub rejects or ejects.`);
|
|
86
|
+
return {
|
|
87
|
+
action: "merge",
|
|
88
|
+
stackMergeable: gaps.length === 0 && open.every(isStackLayerReady),
|
|
89
|
+
instructions,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
function mergeStep(result, top, landing, left) {
|
|
93
|
+
const stack = top.stack;
|
|
94
|
+
if (getGithubTransport() === "rest" || top.transport === "rest") {
|
|
95
|
+
// Upper layers target their parent; the prefix merges into the stack trunk.
|
|
96
|
+
const trunk = result.prs.find((item) => item.baseRefName === stack.baseRefName);
|
|
97
|
+
const plan = buildMergeCommandPlan({
|
|
98
|
+
transport: "rest",
|
|
99
|
+
pr: top.pr,
|
|
100
|
+
repo: result.repo,
|
|
101
|
+
nodeId: "",
|
|
102
|
+
headSha: top.headRefOid,
|
|
103
|
+
queue: trunk?.requiresMergeQueue === true,
|
|
104
|
+
queueKnown: trunk?.requiresMergeQueue !== undefined,
|
|
105
|
+
allowedMergeMethods: result.allowedMergeMethods,
|
|
106
|
+
expectedStack: {
|
|
107
|
+
number: stack.number,
|
|
108
|
+
baseRefName: stack.baseRefName,
|
|
109
|
+
prefix: [...result.prs]
|
|
110
|
+
.sort((left, right) => stackPosition(left) - stackPosition(right))
|
|
111
|
+
.filter((item) => stackPosition(item) <= stackPosition(top))
|
|
112
|
+
.map((item) => ({
|
|
113
|
+
pr: item.pr,
|
|
114
|
+
headRefName: item.headRefName,
|
|
115
|
+
headRefOid: item.headRefOid,
|
|
116
|
+
baseRefName: item.baseRefName,
|
|
117
|
+
})),
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
if ("unavailable" in plan)
|
|
121
|
+
return { unavailable: plan.unavailable, ...(plan.refresh && { refresh: true }) };
|
|
122
|
+
return {
|
|
123
|
+
text: `1. Run \`${renderMergeCommand(plan.command)}\` to request merging ${landing} of native stack #${stack.number}.${left} ${NO_PER_LAYER} If status is \`pending\`, rerun that command at the configured cadence to resume its UUID. \`enqueued\` is not merged. Shepherd revalidates every open lower layer's READY receipt before submission.`,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const method = stackMergeFlag(result.allowedMergeMethods);
|
|
127
|
+
if ("unavailable" in method)
|
|
128
|
+
return { unavailable: method.unavailable };
|
|
129
|
+
return {
|
|
130
|
+
text: `1. Run \`GH_REPO=${result.repo} gh stack merge ${top.pr} --yes ${method.flag}\` in \`${result.repo}\` to merge ${landing} of stack #${stack.number}.${left} ${NO_PER_LAYER} If \`gh stack\` is an unknown command, run \`gh extension install github/gh-stack\`, then rerun that merge command.`,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { PollSummaryItem, PollSummaryResult, StackMergeScope } from "../types.mts";
|
|
2
|
+
/** The scope a caller's options request: an explicit one, or `all` for a bare merge. */
|
|
3
|
+
export declare function requestedMergeScope(opts: {
|
|
4
|
+
merge?: boolean;
|
|
5
|
+
stackMerge?: StackMergeScope;
|
|
6
|
+
}): StackMergeScope | undefined;
|
|
7
|
+
export type StackLayer = PollSummaryItem & {
|
|
8
|
+
stack: NonNullable<PollSummaryItem["stack"]>;
|
|
9
|
+
};
|
|
10
|
+
export declare function isStackLayerReady(item: PollSummaryItem): boolean;
|
|
11
|
+
export declare function stackPosition(item: PollSummaryItem): number;
|
|
12
|
+
export declare function openLayers(result: PollSummaryResult): PollSummaryItem[];
|
|
13
|
+
/**
|
|
14
|
+
* The contiguous open layers from the bottom that a merge command could land now: each is ready,
|
|
15
|
+
* has no stale ancestry, and no closed layer sits below it. `ownedOnly` ends the prefix at the
|
|
16
|
+
* first layer the caller does not own.
|
|
17
|
+
*/
|
|
18
|
+
export declare function readyPrefix(result: PollSummaryResult, ownedOnly: boolean): StackLayer[];
|
|
19
|
+
/**
|
|
20
|
+
* The open layers a merge scope covers, and the layer a merge would target. Both scopes cover
|
|
21
|
+
* only owned open layers: `all` is every one, `through` those up to and including that PR. The
|
|
22
|
+
* `through` target is that PR itself while it is open; once it is closed or gone there is no
|
|
23
|
+
* target, so a merge never retargets to a lower layer.
|
|
24
|
+
*/
|
|
25
|
+
export declare function mergeScopeLayers(result: PollSummaryResult, scope: Exclude<StackMergeScope, {
|
|
26
|
+
kind: "ready";
|
|
27
|
+
}>): {
|
|
28
|
+
inScope: PollSummaryItem[];
|
|
29
|
+
target?: PollSummaryItem;
|
|
30
|
+
};
|
|
31
|
+
/** PRs above a `--merge-through` target: outside the request, so they never get a session. */
|
|
32
|
+
export declare function aboveThroughTarget(result: PollSummaryResult, scope: StackMergeScope | undefined): Set<number>;
|
|
33
|
+
/** In-scope layers closed without merging: the request is not complete while any exists. */
|
|
34
|
+
export declare function closedInScope(result: PollSummaryResult, scope: StackMergeScope): PollSummaryItem[];
|
|
35
|
+
/**
|
|
36
|
+
* Whole-stack ownership only: the first tick of a `--merge`, `--merge-ready` or `--merge-through`
|
|
37
|
+
* request records the open owned in-scope layers; later ticks of that same request (same command
|
|
38
|
+
* line, within the record's TTL) report any open in-scope layer that is not in that record. A
|
|
39
|
+
* different command line is a new request and records afresh. The record is dropped once nothing
|
|
40
|
+
* in scope is open. Explicitly listed PRs need no record.
|
|
41
|
+
*/
|
|
42
|
+
export declare function stackAddedLayers(result: Pick<PollSummaryResult, "repo" | "selection" | "prs" | "stackAncestry">, scope: StackMergeScope | undefined, command: string): Promise<number[]>;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { clearApprovedLayers, readApprovedLayers, writeApprovedLayers, } from "../state/stack-merge-approval.mjs";
|
|
2
|
+
import { stackLayerBlockReason } from "./stack-layer-readiness.mjs";
|
|
3
|
+
/** The scope a caller's options request: an explicit one, or `all` for a bare merge. */
|
|
4
|
+
export function requestedMergeScope(opts) {
|
|
5
|
+
return opts.stackMerge ?? (opts.merge ? { kind: "all" } : undefined);
|
|
6
|
+
}
|
|
7
|
+
export function isStackLayerReady(item) {
|
|
8
|
+
return item.stack !== undefined && stackLayerBlockReason(item) === undefined;
|
|
9
|
+
}
|
|
10
|
+
export function stackPosition(item) {
|
|
11
|
+
return item.stack?.position ?? Number.MAX_SAFE_INTEGER;
|
|
12
|
+
}
|
|
13
|
+
function isStackLayer(item) {
|
|
14
|
+
return item.stack !== undefined;
|
|
15
|
+
}
|
|
16
|
+
export function openLayers(result) {
|
|
17
|
+
return result.prs
|
|
18
|
+
.filter((item) => item.state === "OPEN")
|
|
19
|
+
.sort((left, right) => stackPosition(left) - stackPosition(right));
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The contiguous open layers from the bottom that a merge command could land now: each is ready,
|
|
23
|
+
* has no stale ancestry, and no closed layer sits below it. `ownedOnly` ends the prefix at the
|
|
24
|
+
* first layer the caller does not own.
|
|
25
|
+
*/
|
|
26
|
+
export function readyPrefix(result, ownedOnly) {
|
|
27
|
+
const layers = [...result.prs].sort((left, right) => stackPosition(left) - stackPosition(right));
|
|
28
|
+
if (layers.some((item) => item.state === "OPEN" && item.isInMergeQueue))
|
|
29
|
+
return [];
|
|
30
|
+
const open = openLayers(result);
|
|
31
|
+
const bottom = open[0];
|
|
32
|
+
if (!bottom || !isStackLayer(bottom) || bottom.baseRefName !== bottom.stack.baseRefName) {
|
|
33
|
+
return [];
|
|
34
|
+
}
|
|
35
|
+
const stale = new Set((result.stackAncestry ?? []).map((gap) => gap.childPr));
|
|
36
|
+
const prefix = [];
|
|
37
|
+
for (const item of open) {
|
|
38
|
+
if (!isStackLayer(item))
|
|
39
|
+
break;
|
|
40
|
+
if (item.action === "escalate" || !isStackLayerReady(item) || stale.has(item.pr))
|
|
41
|
+
break;
|
|
42
|
+
if (ownedOnly && !item.owned)
|
|
43
|
+
break;
|
|
44
|
+
if (layers.some((l) => (l.state === "CLOSED" || l.state === "UNKNOWN") && stackPosition(l) < stackPosition(item)))
|
|
45
|
+
break;
|
|
46
|
+
prefix.push(item);
|
|
47
|
+
}
|
|
48
|
+
return prefix;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The open layers a merge scope covers, and the layer a merge would target. Both scopes cover
|
|
52
|
+
* only owned open layers: `all` is every one, `through` those up to and including that PR. The
|
|
53
|
+
* `through` target is that PR itself while it is open; once it is closed or gone there is no
|
|
54
|
+
* target, so a merge never retargets to a lower layer.
|
|
55
|
+
*/
|
|
56
|
+
export function mergeScopeLayers(result, scope) {
|
|
57
|
+
const open = openLayers(result);
|
|
58
|
+
if (scope.kind === "all") {
|
|
59
|
+
const inScope = open.filter((item) => item.owned === true);
|
|
60
|
+
return { inScope, target: inScope.at(-1) };
|
|
61
|
+
}
|
|
62
|
+
// A through PR that is not an open row of the stack covers nothing rather than everything.
|
|
63
|
+
const row = result.prs.find((item) => item.pr === scope.pr);
|
|
64
|
+
const target = row?.state === "OPEN" ? row : undefined;
|
|
65
|
+
const limit = target ? stackPosition(target) : 0;
|
|
66
|
+
const inScope = open.filter((item) => stackPosition(item) <= limit && item.owned === true);
|
|
67
|
+
return { inScope, target };
|
|
68
|
+
}
|
|
69
|
+
/** PRs above a `--merge-through` target: outside the request, so they never get a session. */
|
|
70
|
+
export function aboveThroughTarget(result, scope) {
|
|
71
|
+
if (scope?.kind !== "through")
|
|
72
|
+
return new Set();
|
|
73
|
+
const target = result.prs.find((item) => item.pr === scope.pr);
|
|
74
|
+
const limit = target ? stackPosition(target) : 0;
|
|
75
|
+
return new Set(result.prs.filter((item) => stackPosition(item) > limit).map((item) => item.pr));
|
|
76
|
+
}
|
|
77
|
+
/** In-scope layers closed without merging: the request is not complete while any exists. */
|
|
78
|
+
export function closedInScope(result, scope) {
|
|
79
|
+
const outside = aboveThroughTarget(result, scope);
|
|
80
|
+
return result.prs.filter((item) => item.state === "CLOSED" &&
|
|
81
|
+
(scope.kind === "through" ? !outside.has(item.pr) : item.owned === true));
|
|
82
|
+
}
|
|
83
|
+
/** The scope's key in the approval record. */
|
|
84
|
+
function scopeKey(scope) {
|
|
85
|
+
return scope.kind === "through" ? `through-${scope.pr}` : scope.kind;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Whole-stack ownership only: the first tick of a `--merge`, `--merge-ready` or `--merge-through`
|
|
89
|
+
* request records the open owned in-scope layers; later ticks of that same request (same command
|
|
90
|
+
* line, within the record's TTL) report any open in-scope layer that is not in that record. A
|
|
91
|
+
* different command line is a new request and records afresh. The record is dropped once nothing
|
|
92
|
+
* in scope is open. Explicitly listed PRs need no record.
|
|
93
|
+
*/
|
|
94
|
+
export async function stackAddedLayers(result, scope, command) {
|
|
95
|
+
if (!scope || result.selection.kind !== "stack" || result.selection.owned)
|
|
96
|
+
return [];
|
|
97
|
+
const [owner, repo] = result.repo.split("/");
|
|
98
|
+
const key = { owner, repo, stack: result.selection.stackNumber, scope: scopeKey(scope) };
|
|
99
|
+
const layers = scope.kind === "ready"
|
|
100
|
+
? openLayers(result).filter((item) => item.owned === true)
|
|
101
|
+
: mergeScopeLayers(result, scope).inScope;
|
|
102
|
+
const current = layers.map((item) => item.pr);
|
|
103
|
+
if (current.length === 0) {
|
|
104
|
+
await clearApprovedLayers(key);
|
|
105
|
+
return [];
|
|
106
|
+
}
|
|
107
|
+
const approved = await readApprovedLayers(key, command);
|
|
108
|
+
if (!approved) {
|
|
109
|
+
await writeApprovedLayers(key, current, command);
|
|
110
|
+
return [];
|
|
111
|
+
}
|
|
112
|
+
return current.filter((pr) => !approved.includes(pr));
|
|
113
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { PollSummaryItem, PollSummaryResult } from "../types.mts";
|
|
2
|
+
/**
|
|
3
|
+
* Branch updates when the caller owns only some layers. A one-PR session prints a whole-stack
|
|
4
|
+
* `gh stack rebase` / `gh stack push`, which would rewrite the layers the caller does not own.
|
|
5
|
+
* This step bounds the update at the first unowned layer; undefined when it does not apply.
|
|
6
|
+
*/
|
|
7
|
+
export declare function ownershipBoundaryStep(result: PollSummaryResult): string | undefined;
|
|
8
|
+
/** Listed PRs that are no longer layers of the stack (a teammate ran `gh stack unstack` or `modify`). */
|
|
9
|
+
export declare function missingListedLayers(prs: PollSummaryItem[], listed: number[] | undefined): number[];
|
|
10
|
+
/**
|
|
11
|
+
* The escalation for a listed PR that left the stack after the command started. The caller named
|
|
12
|
+
* its own layers, so the CLI cannot tell which layers are now the caller's; nothing runs.
|
|
13
|
+
*/
|
|
14
|
+
export declare function listedLayerLeftStack(result: PollSummaryResult, missing: number[], rerunTarget: string): PollSummaryResult;
|
|
15
|
+
/**
|
|
16
|
+
* Mark the layers the caller declared as its own: every layer when it listed none, otherwise the
|
|
17
|
+
* listed PRs that are still layers. Ownership is declared, never inferred from the author. When
|
|
18
|
+
* the caller owns only some layers, each owned row's `pollCommand` carries the layer ownership.
|
|
19
|
+
*/
|
|
20
|
+
export declare function applyStackOwnership(selection: Extract<PollSummaryResult["selection"], {
|
|
21
|
+
kind: "stack";
|
|
22
|
+
}>, prs: PollSummaryItem[], listed: number[] | undefined): {
|
|
23
|
+
selection: PollSummaryResult["selection"];
|
|
24
|
+
prs: PollSummaryItem[];
|
|
25
|
+
};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { renderShellCommand } from "../cli/runner.mjs";
|
|
2
|
+
import { formatStackLayers } from "./iterate/stack-boundary.mjs";
|
|
3
|
+
import { isStackLayerReady, openLayers, stackPosition } from "./stack-merge-scope.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Branch updates when the caller owns only some layers. A one-PR session prints a whole-stack
|
|
6
|
+
* `gh stack rebase` / `gh stack push`, which would rewrite the layers the caller does not own.
|
|
7
|
+
* This step bounds the update at the first unowned layer; undefined when it does not apply.
|
|
8
|
+
*/
|
|
9
|
+
export function ownershipBoundaryStep(result) {
|
|
10
|
+
if (result.selection.kind !== "stack" || !result.selection.owned)
|
|
11
|
+
return undefined;
|
|
12
|
+
const open = openLayers(result);
|
|
13
|
+
const stale = new Set((result.stackAncestry ?? []).map((gap) => gap.childPr));
|
|
14
|
+
const unowned = open.filter((item) => !item.owned);
|
|
15
|
+
const needsWork = open.filter((item) => item.owned && (!isStackLayerReady(item) || stale.has(item.pr)));
|
|
16
|
+
const boundary = unowned[0];
|
|
17
|
+
if (!boundary || needsWork.length === 0)
|
|
18
|
+
return undefined;
|
|
19
|
+
const names = unowned.map((item) => `#${item.pr}`).join(", ");
|
|
20
|
+
const below = open.filter((item) => item.owned && stackPosition(item) < stackPosition(boundary));
|
|
21
|
+
const top = below.at(-1);
|
|
22
|
+
const head = `Branch updates: unowned ${unowned.length === 1 ? "layer" : "layers"} ${names} ${unowned.length === 1 ? "sits" : "sit"} in this stack and must not be rebased or pushed. A \`gh stack rebase\` or \`gh stack push\` printed by a one-PR session covers the whole stack, so do not run it as printed.`;
|
|
23
|
+
if (!top) {
|
|
24
|
+
return `${head} No owned layer sits below the first unowned layer (#${boundary.pr}), so no owned branch can be restacked without rewriting it. Do not rebase or push; report that the owned layers wait on #${boundary.pr}'s owner.`;
|
|
25
|
+
}
|
|
26
|
+
const branches = below.map((item) => `\`${item.headRefName}\``).join(", ");
|
|
27
|
+
return `${head} Instead, stop at the first unowned layer (#${boundary.pr}): check out \`${top.headRefName}\` (PR #${top.pr}), run \`gh stack rebase --downstack\`, then push only the owned branches (${branches}) with \`git push --force-with-lease <push-remote> <branch>\` (substitute this repository's push remote for \`<push-remote>\`, the remote \`gh stack push\` uses). Owned layers above #${boundary.pr} cannot be restacked this way. After the update, ${names} (and anything above) will be stale: tell the user their owner must restack ${unowned.length === 1 ? "it" : "them"}.`;
|
|
28
|
+
}
|
|
29
|
+
/** Listed PRs that are no longer layers of the stack (a teammate ran `gh stack unstack` or `modify`). */
|
|
30
|
+
export function missingListedLayers(prs, listed) {
|
|
31
|
+
const members = new Set(prs.map((item) => item.pr));
|
|
32
|
+
return (listed ?? []).filter((pr) => !members.has(pr));
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The escalation for a listed PR that left the stack after the command started. The caller named
|
|
36
|
+
* its own layers, so the CLI cannot tell which layers are now the caller's; nothing runs.
|
|
37
|
+
*/
|
|
38
|
+
export function listedLayerLeftStack(result, missing, rerunTarget) {
|
|
39
|
+
const many = missing.length > 1;
|
|
40
|
+
const names = missing.map((pr) => `PR #${pr}`).join(", ");
|
|
41
|
+
const stackNumber = result.selection.kind === "stack" ? result.selection.stackNumber : 0;
|
|
42
|
+
return {
|
|
43
|
+
...result,
|
|
44
|
+
reason: "actionable",
|
|
45
|
+
nextAction: "escalate",
|
|
46
|
+
stackMergeable: false,
|
|
47
|
+
instructions: [
|
|
48
|
+
`1. \`listed-pr-left-stack\`: ${names} ${many ? "are" : "is"} no longer in native stack #${stackNumber}, so the layers you listed no longer match the stack. Nothing was merged or changed. Stop and tell the user. If ${many ? "they were" : "it was"} removed on purpose, rerun ${rerunTarget} with only the PRs that remain in the stack listed after \`stack\`.`,
|
|
49
|
+
],
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Hand a routed one-PR command the stack's layer ownership, so that session never prints a
|
|
54
|
+
* whole-stack rebase or push while an open layer is not the caller's.
|
|
55
|
+
*/
|
|
56
|
+
function withLayerOwnership(item, prs) {
|
|
57
|
+
if (!item.pollCommand || !item.owned)
|
|
58
|
+
return item;
|
|
59
|
+
const layers = prs
|
|
60
|
+
.filter((layer) => layer.state === "OPEN" && layer.stack)
|
|
61
|
+
.map((layer) => ({
|
|
62
|
+
pr: layer.pr,
|
|
63
|
+
position: layer.stack.position,
|
|
64
|
+
owned: layer.owned === true,
|
|
65
|
+
branch: layer.headRefName,
|
|
66
|
+
}));
|
|
67
|
+
if (layers.every((layer) => layer.owned))
|
|
68
|
+
return item;
|
|
69
|
+
const flag = renderShellCommand(["--layers", formatStackLayers(layers)]);
|
|
70
|
+
return { ...item, pollCommand: `${item.pollCommand} ${flag}` };
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Mark the layers the caller declared as its own: every layer when it listed none, otherwise the
|
|
74
|
+
* listed PRs that are still layers. Ownership is declared, never inferred from the author. When
|
|
75
|
+
* the caller owns only some layers, each owned row's `pollCommand` carries the layer ownership.
|
|
76
|
+
*/
|
|
77
|
+
export function applyStackOwnership(selection, prs, listed) {
|
|
78
|
+
const members = new Set(prs.map((item) => item.pr));
|
|
79
|
+
const owned = listed ? new Set(listed) : members;
|
|
80
|
+
const marked = prs.map(({ owned: _author, ...item }) => owned.has(item.pr) ? { ...item, owned: true } : item);
|
|
81
|
+
return {
|
|
82
|
+
selection: listed ? { ...selection, owned: listed } : selection,
|
|
83
|
+
prs: listed ? marked.map((item) => withLayerOwnership(item, marked)) : marked,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -17,3 +17,5 @@ export declare function applyStackStallGuard(planned: {
|
|
|
17
17
|
* stack timer is running. A single tick schedules its next tick no later than this.
|
|
18
18
|
*/
|
|
19
19
|
export declare function readStackStallDeadline(result: PollSummaryResult, stallTimeoutSeconds: number): Promise<number | undefined>;
|
|
20
|
+
/** True for the one repair tick whose marker is already spent: debounce and re-planning must keep it. */
|
|
21
|
+
export declare function isStackRepairTick(result: PollSummaryResult): boolean;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { clearStackStallState, readStackStallState, writeStackStallState, } from "../state/stack-stall.mjs";
|
|
2
|
+
import { readStallRepair, writeStallRepair } from "../state/stall-repair.mjs";
|
|
2
3
|
import { formatDurationApprox } from "./iterate/escalate.mjs";
|
|
4
|
+
import { stallRepairStep } from "./iterate/stall-repair.mjs";
|
|
3
5
|
import { summaryStatusSignature } from "./poll-summary-signature.mjs";
|
|
4
6
|
import { describeIdleLayers } from "./stack-drain.mjs";
|
|
5
7
|
/**
|
|
@@ -21,12 +23,12 @@ export async function applyStackStallGuard(planned, repo, stallTimeoutSeconds) {
|
|
|
21
23
|
const nowSeconds = Math.floor(Date.now() / 1000);
|
|
22
24
|
const read = await readStackStallState(key);
|
|
23
25
|
if (!read.ok)
|
|
24
|
-
return stackStateUnavailable(result, read.reason);
|
|
26
|
+
return await stackStateUnavailable(result, read.reason);
|
|
25
27
|
const stored = read.state;
|
|
26
28
|
if (!stored || stored.fingerprint !== fingerprint || stored.firstSeenAt > nowSeconds) {
|
|
27
29
|
const wrote = await writeStackStallState(key, { fingerprint, firstSeenAt: nowSeconds });
|
|
28
30
|
if (!wrote.ok)
|
|
29
|
-
return stackStateUnavailable(result, wrote.reason);
|
|
31
|
+
return await stackStateUnavailable(result, wrote.reason);
|
|
30
32
|
return result;
|
|
31
33
|
}
|
|
32
34
|
const ageSeconds = nowSeconds - stored.firstSeenAt;
|
|
@@ -53,7 +55,27 @@ export async function readStackStallDeadline(result, stallTimeoutSeconds) {
|
|
|
53
55
|
const read = await readStackStallState({ owner, repo, stack: result.selection.stackNumber });
|
|
54
56
|
return read.ok && read.state ? read.state.firstSeenAt + stallTimeoutSeconds : undefined;
|
|
55
57
|
}
|
|
56
|
-
|
|
58
|
+
/** Not serialized and carried by object spread, so it survives the poll loop's result copies. */
|
|
59
|
+
const REPAIR_TICK = Symbol("stackRepairTick");
|
|
60
|
+
/** True for the one repair tick whose marker is already spent: debounce and re-planning must keep it. */
|
|
61
|
+
export function isStackRepairTick(result) {
|
|
62
|
+
return result[REPAIR_TICK] === true;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The first time the stack stall timer cannot be saved, the plan keeps its action and gains one
|
|
66
|
+
* repair step. The unchanged failure on the next poll escalates `stall-state-unavailable`.
|
|
67
|
+
*/
|
|
68
|
+
async function stackStateUnavailable(result, reason) {
|
|
69
|
+
const scope = `${result.repo} stack ${result.selection.kind === "stack" ? result.selection.stackNumber : ""}`;
|
|
70
|
+
if (!(await readStallRepair(scope, reason)) && (await writeStallRepair(scope, reason))) {
|
|
71
|
+
const steps = result.instructions ?? [];
|
|
72
|
+
return Object.assign({
|
|
73
|
+
...result,
|
|
74
|
+
reason: "actionable",
|
|
75
|
+
nextAction: "shepherd",
|
|
76
|
+
instructions: [...steps, `${steps.length + 1}. ${stallRepairStep(reason)}`],
|
|
77
|
+
}, { [REPAIR_TICK]: true });
|
|
78
|
+
}
|
|
57
79
|
return {
|
|
58
80
|
...result,
|
|
59
81
|
reason: "actionable",
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** The refs a caller passed that landed in one native stack. */
|
|
2
|
+
export interface StackGroup {
|
|
3
|
+
stack: number;
|
|
4
|
+
/** PR numbers the caller listed. */
|
|
5
|
+
prs: number[];
|
|
6
|
+
/** Stack numbers the caller listed (at most the stack itself). */
|
|
7
|
+
stackRefs: number[];
|
|
8
|
+
/** Every PR in the stack. */
|
|
9
|
+
members?: number[];
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* The error for refs that span several stacks: it names each stack, the refs that belong to it,
|
|
13
|
+
* and the exact command to run for it, so the caller can run them as written. `tool` names the
|
|
14
|
+
* `stack` tool call (MCP or library) instead of the CLI command.
|
|
15
|
+
*/
|
|
16
|
+
export declare function multiStackError(repo: string, groups: StackGroup[], merge: "merge" | "ready" | "through" | undefined, through: number | undefined, tool?: boolean): string;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { formatPrUrl } from "../pr-reference.mjs";
|
|
2
|
+
import { buildPrShepherdCommand } from "../cli/runner.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* The error for refs that span several stacks: it names each stack, the refs that belong to it,
|
|
5
|
+
* and the exact command to run for it, so the caller can run them as written. `tool` names the
|
|
6
|
+
* `stack` tool call (MCP or library) instead of the CLI command.
|
|
7
|
+
*/
|
|
8
|
+
export function multiStackError(repo, groups, merge, through, tool = false) {
|
|
9
|
+
const lines = groups.map((group) => {
|
|
10
|
+
const refs = [
|
|
11
|
+
...group.stackRefs.map((n) => `stack #${n}`),
|
|
12
|
+
...group.prs.map((n) => `#${n}`),
|
|
13
|
+
].join(", ");
|
|
14
|
+
const targets = [
|
|
15
|
+
...group.stackRefs.map((n) => `${repo}#${n}`),
|
|
16
|
+
...group.prs.map((n) => formatPrUrl(repo, n)),
|
|
17
|
+
];
|
|
18
|
+
const flags = merge === "through"
|
|
19
|
+
? through !== undefined && group.members?.includes(through)
|
|
20
|
+
? ["--merge-through", formatPrUrl(repo, through)]
|
|
21
|
+
: []
|
|
22
|
+
: merge === "ready"
|
|
23
|
+
? ["--merge-ready"]
|
|
24
|
+
: merge === "merge"
|
|
25
|
+
? ["--merge"]
|
|
26
|
+
: [];
|
|
27
|
+
const command = tool
|
|
28
|
+
? toolCall(targets, merge, through !== undefined && group.members?.includes(through) ? through : undefined, repo)
|
|
29
|
+
: buildPrShepherdCommand(["stack", ...targets, "--until-terminal", ...flags]).text;
|
|
30
|
+
return `- stack #${group.stack} (${refs}): \`${command}\``;
|
|
31
|
+
});
|
|
32
|
+
return [`These refs span ${groups.length} stacks. Run one command per stack:`, ...lines].join("\n");
|
|
33
|
+
}
|
|
34
|
+
/** The `stack` tool call for one group, in the tool's own argument names. */
|
|
35
|
+
function toolCall(refs, merge, through, repo) {
|
|
36
|
+
const args = {
|
|
37
|
+
refs,
|
|
38
|
+
...(merge === "merge" && { merge: true }),
|
|
39
|
+
...(merge === "ready" && { mergeReady: true }),
|
|
40
|
+
...(merge === "through" &&
|
|
41
|
+
through !== undefined && { mergeThrough: formatPrUrl(repo, through) }),
|
|
42
|
+
};
|
|
43
|
+
return `stack ${JSON.stringify(args)}`;
|
|
44
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type RepoInfo } from "../github/client.mts";
|
|
2
|
+
import { type ParsedPrReference } from "../pr-reference.mts";
|
|
3
|
+
import type { StackMergeScope } from "../types.mts";
|
|
4
|
+
export type StackMergeFlag = "merge" | "ready" | "through";
|
|
5
|
+
export declare function stackMergeScope(flag: StackMergeFlag | undefined, through: number | undefined): StackMergeScope | undefined;
|
|
6
|
+
export interface StackTargetInput {
|
|
7
|
+
/** One stack number, or one or more PRs from one native stack. */
|
|
8
|
+
refs: ParsedPrReference[];
|
|
9
|
+
/** `--merge-through`: a PR of the selected stack. */
|
|
10
|
+
through?: ParsedPrReference;
|
|
11
|
+
/** Which merge flag the caller passed, so a multi-stack error can repeat it. */
|
|
12
|
+
merge?: StackMergeFlag;
|
|
13
|
+
/** The caller is the library or MCP `stack` tool: errors name tool calls, not CLI commands. */
|
|
14
|
+
tool?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface StackTarget {
|
|
17
|
+
/** A PR of the selected stack, used to read it. */
|
|
18
|
+
anchor: number;
|
|
19
|
+
/** The PRs the caller listed, and so owns; omitted only for a stack number, which owns every layer. */
|
|
20
|
+
owned?: number[];
|
|
21
|
+
through?: number;
|
|
22
|
+
targetRepository?: RepoInfo;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Resolve `stack` refs to one native stack and the layers the caller owns. A number is a PR when
|
|
26
|
+
* GitHub has one, else a stack number (the two share a sequence). Ownership is declared: a stack
|
|
27
|
+
* number selects and owns every layer, and listed PRs own only themselves, so one PR does not
|
|
28
|
+
* claim the layers around it.
|
|
29
|
+
*/
|
|
30
|
+
export declare function resolveStackTarget(input: StackTargetInput): Promise<StackTarget>;
|