mandrel 2.81.0 → 2.82.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/.agents/scripts/lib/audit-to-stories/parse-audit-md.js +49 -24
- package/.agents/scripts/lib/baselines/duplication-scanner.js +9 -5
- package/.agents/scripts/lib/bootstrap/manifest.js +128 -159
- package/.agents/scripts/lib/orchestration/git-cleanup/phases/cli.js +31 -27
- package/.agents/scripts/lib/orchestration/git-cleanup/phases/render.js +25 -21
- package/.agents/scripts/lib/orchestration/pr-watch-poll.js +107 -0
- package/.agents/scripts/lib/orchestration/pr-watch.js +168 -235
- package/.agents/scripts/lib/orchestration/reassert-status-column.js +102 -72
- package/.agents/scripts/lib/orchestration/single-story-close/phases/post-land.js +21 -3
- package/.agents/scripts/lib/orchestration/single-story-close/phases/pre-gate-steps.js +44 -2
- package/.agents/scripts/lib/orchestration/story-close/duplication-close-rescore.js +257 -0
- package/.agents/scripts/lib/orchestration/story-close/ratchet-shrink-writeback.js +341 -0
- package/.agents/scripts/lib/orchestration/ticketing.js +0 -9
- package/docs/CHANGELOG.md +19 -0
- package/package.json +1 -1
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* pr-watch-poll.js — the pure outcome helpers and the bounded poll loop
|
|
3
|
+
* behind `watchPrToTerminal` (pr-watch.js).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { checkVerdict, classifyRequiredCheck } from './check-state.js';
|
|
7
|
+
|
|
8
|
+
/** Entries with a string `name`; `[]` for malformed input. */
|
|
9
|
+
export function parseGhPrChecks(stdout) {
|
|
10
|
+
const trimmed = String(stdout ?? '').trim();
|
|
11
|
+
if (trimmed.length === 0) return [];
|
|
12
|
+
try {
|
|
13
|
+
const parsed = JSON.parse(trimmed);
|
|
14
|
+
if (!Array.isArray(parsed)) return [];
|
|
15
|
+
return parsed.filter(
|
|
16
|
+
(e) => e && typeof e === 'object' && typeof e.name === 'string',
|
|
17
|
+
);
|
|
18
|
+
} catch {
|
|
19
|
+
return [];
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** `{ checkName: outcome }`; a repeated name (matrix, retry) — last wins. */
|
|
24
|
+
export function reduceOutcomes(entries) {
|
|
25
|
+
const out = {};
|
|
26
|
+
for (const e of entries) {
|
|
27
|
+
out[e.name] = classifyRequiredCheck(e);
|
|
28
|
+
}
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Only `'pending'` is non-terminal (the classifier collapses running states). */
|
|
33
|
+
export function allTerminal(outcomes) {
|
|
34
|
+
for (const v of Object.values(outcomes)) {
|
|
35
|
+
if (v === 'pending') return false;
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Slow CI, not red: "re-arm the watch", never "the change is broken". */
|
|
41
|
+
const STILL_RUNNING = 'still-running';
|
|
42
|
+
|
|
43
|
+
/** Leftover `'pending'` → `'still-running'` (the schema forbids pending). */
|
|
44
|
+
export function promotePendingToStillRunning(outcomes) {
|
|
45
|
+
const out = {};
|
|
46
|
+
for (const [k, v] of Object.entries(outcomes)) {
|
|
47
|
+
out[k] = v === 'pending' ? STILL_RUNNING : v;
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** A genuinely red check: the hard stop that consumes no resume budget. */
|
|
53
|
+
export function hasFailingCheck(outcomes) {
|
|
54
|
+
return Object.values(outcomes).some((v) => checkVerdict(v) === 'fail');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function identityDiscount(_entries, outcomes) {
|
|
58
|
+
return outcomes;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A transient `gh` failure is skipped; `maxPolls` bounds a broken `gh`.
|
|
63
|
+
*
|
|
64
|
+
* @param {object} opts
|
|
65
|
+
* @param {string} opts.prUrl
|
|
66
|
+
* @param {string} opts.cwd
|
|
67
|
+
* @param {string|null} [opts.repo] `owner/repo` passed to `gh` as `--repo`.
|
|
68
|
+
* @param {object} opts.outcomes Initial `{ checkName: outcome }` map.
|
|
69
|
+
* @param {number} opts.polls Current poll counter.
|
|
70
|
+
* @param {number} opts.maxPolls Hard cap on total poll iterations.
|
|
71
|
+
* @param {Function} opts.ghPrChecksFn
|
|
72
|
+
* @param {number} opts.pollIntervalMs
|
|
73
|
+
* @param {Function} opts.sleepFn
|
|
74
|
+
* @param {Function} [opts.discountFn]
|
|
75
|
+
* @param {{ warn?: Function }} opts.logger
|
|
76
|
+
* @returns {Promise<{ outcomes: object, polls: number }>}
|
|
77
|
+
*/
|
|
78
|
+
export async function pollUntilTerminal({
|
|
79
|
+
prUrl,
|
|
80
|
+
cwd,
|
|
81
|
+
repo = null,
|
|
82
|
+
outcomes,
|
|
83
|
+
polls,
|
|
84
|
+
maxPolls,
|
|
85
|
+
ghPrChecksFn,
|
|
86
|
+
pollIntervalMs,
|
|
87
|
+
sleepFn,
|
|
88
|
+
discountFn = identityDiscount,
|
|
89
|
+
logger,
|
|
90
|
+
}) {
|
|
91
|
+
let currentOutcomes = outcomes;
|
|
92
|
+
let currentPolls = polls;
|
|
93
|
+
while (!allTerminal(currentOutcomes) && currentPolls < maxPolls) {
|
|
94
|
+
await sleepFn(pollIntervalMs);
|
|
95
|
+
currentPolls += 1;
|
|
96
|
+
const probe = ghPrChecksFn({ prUrl, cwd, repo });
|
|
97
|
+
const entries = parseGhPrChecks(probe.stdout);
|
|
98
|
+
if (entries.length === 0 && probe.status !== 0 && probe.status !== 8) {
|
|
99
|
+
logger.warn?.(
|
|
100
|
+
`[Watcher] gh pr checks transient failure (status=${probe.status}): ${probe.stderr}`,
|
|
101
|
+
);
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
currentOutcomes = await discountFn(entries, reduceOutcomes(entries));
|
|
105
|
+
}
|
|
106
|
+
return { outcomes: currentOutcomes, polls: currentPolls };
|
|
107
|
+
}
|
|
@@ -9,7 +9,15 @@ import { spawnSync } from 'node:child_process';
|
|
|
9
9
|
|
|
10
10
|
import { applyBehindUpdate } from './behind-recovery.js';
|
|
11
11
|
import { createCancelledRunDiscount } from './cancelled-run-discount.js';
|
|
12
|
-
import { checkVerdict
|
|
12
|
+
import { checkVerdict } from './check-state.js';
|
|
13
|
+
import {
|
|
14
|
+
allTerminal,
|
|
15
|
+
hasFailingCheck,
|
|
16
|
+
parseGhPrChecks,
|
|
17
|
+
pollUntilTerminal,
|
|
18
|
+
promotePendingToStillRunning,
|
|
19
|
+
reduceOutcomes,
|
|
20
|
+
} from './pr-watch-poll.js';
|
|
13
21
|
|
|
14
22
|
/**
|
|
15
23
|
* `gh` has no `<owner/repo>#<number>` form (it parses one as a branch), so
|
|
@@ -23,10 +31,22 @@ function ghRepoFlag(repo) {
|
|
|
23
31
|
return trimmed.length > 0 ? ['--repo', trimmed] : [];
|
|
24
32
|
}
|
|
25
33
|
|
|
34
|
+
function runGh(args, { cwd, repo, spawnFn = spawnSync }) {
|
|
35
|
+
const result = spawnFn('gh', [...args, ...ghRepoFlag(repo)], {
|
|
36
|
+
cwd,
|
|
37
|
+
encoding: 'utf-8',
|
|
38
|
+
shell: false,
|
|
39
|
+
});
|
|
40
|
+
return {
|
|
41
|
+
status: result.status ?? 1,
|
|
42
|
+
stdout: result.stdout ?? '',
|
|
43
|
+
stderr: result.stderr ?? '',
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
26
47
|
/** `--required` makes the returned set authoritative for protection gating. */
|
|
27
|
-
function ghPrChecks({ prUrl,
|
|
28
|
-
|
|
29
|
-
'gh',
|
|
48
|
+
function ghPrChecks({ prUrl, ...port }) {
|
|
49
|
+
return runGh(
|
|
30
50
|
[
|
|
31
51
|
'pr',
|
|
32
52
|
'checks',
|
|
@@ -34,28 +54,13 @@ function ghPrChecks({ prUrl, cwd, repo, spawnFn = spawnSync }) {
|
|
|
34
54
|
'--required',
|
|
35
55
|
'--json',
|
|
36
56
|
'name,state,bucket,workflow,link',
|
|
37
|
-
...ghRepoFlag(repo),
|
|
38
57
|
],
|
|
39
|
-
|
|
58
|
+
port,
|
|
40
59
|
);
|
|
41
|
-
return {
|
|
42
|
-
status: result.status ?? 1,
|
|
43
|
-
stdout: result.stdout ?? '',
|
|
44
|
-
stderr: result.stderr ?? '',
|
|
45
|
-
};
|
|
46
60
|
}
|
|
47
61
|
|
|
48
|
-
function ghPrView({ prUrl,
|
|
49
|
-
|
|
50
|
-
'gh',
|
|
51
|
-
['pr', 'view', prUrl, '--json', 'mergeStateStatus', ...ghRepoFlag(repo)],
|
|
52
|
-
{ cwd, encoding: 'utf-8', shell: false },
|
|
53
|
-
);
|
|
54
|
-
return {
|
|
55
|
-
status: result.status ?? 1,
|
|
56
|
-
stdout: result.stdout ?? '',
|
|
57
|
-
stderr: result.stderr ?? '',
|
|
58
|
-
};
|
|
62
|
+
function ghPrView({ prUrl, ...port }) {
|
|
63
|
+
return runGh(['pr', 'view', prUrl, '--json', 'mergeStateStatus'], port);
|
|
59
64
|
}
|
|
60
65
|
|
|
61
66
|
/** Malformed input → `''`, which callers treat as "not BEHIND". */
|
|
@@ -72,21 +77,8 @@ function parseMergeStateStatus(stdout) {
|
|
|
72
77
|
}
|
|
73
78
|
}
|
|
74
79
|
|
|
75
|
-
function ghPrUpdateBranch({ prUrl,
|
|
76
|
-
|
|
77
|
-
'gh',
|
|
78
|
-
['pr', 'update-branch', prUrl, ...ghRepoFlag(repo)],
|
|
79
|
-
{
|
|
80
|
-
cwd,
|
|
81
|
-
encoding: 'utf-8',
|
|
82
|
-
shell: false,
|
|
83
|
-
},
|
|
84
|
-
);
|
|
85
|
-
return {
|
|
86
|
-
status: result.status ?? 1,
|
|
87
|
-
stdout: result.stdout ?? '',
|
|
88
|
-
stderr: result.stderr ?? '',
|
|
89
|
-
};
|
|
80
|
+
function ghPrUpdateBranch({ prUrl, ...port }) {
|
|
81
|
+
return runGh(['pr', 'update-branch', prUrl], port);
|
|
90
82
|
}
|
|
91
83
|
|
|
92
84
|
/** Gate for BEHIND recovery: never auto-update into a failing PR. */
|
|
@@ -96,109 +88,132 @@ function allGreen(outcomes) {
|
|
|
96
88
|
return values.every((v) => checkVerdict(v) === 'pass');
|
|
97
89
|
}
|
|
98
90
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
const trimmed = String(stdout ?? '').trim();
|
|
102
|
-
if (trimmed.length === 0) return [];
|
|
103
|
-
try {
|
|
104
|
-
const parsed = JSON.parse(trimmed);
|
|
105
|
-
if (!Array.isArray(parsed)) return [];
|
|
106
|
-
return parsed.filter(
|
|
107
|
-
(e) => e && typeof e === 'object' && typeof e.name === 'string',
|
|
108
|
-
);
|
|
109
|
-
} catch {
|
|
110
|
-
return [];
|
|
111
|
-
}
|
|
91
|
+
function defaultSleep(ms) {
|
|
92
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
112
93
|
}
|
|
113
94
|
|
|
114
|
-
/**
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
95
|
+
/**
|
|
96
|
+
* Never poll an empty name set: `allTerminal({})` is vacuously true and would
|
|
97
|
+
* report a red verdict with no failing check. `gh` overloads non-zero for
|
|
98
|
+
* "none attached yet" and a real fault, so classification is the caller's.
|
|
99
|
+
*/
|
|
100
|
+
function emptyRequiredChecksResult(first, logger) {
|
|
101
|
+
// `gh` exits 8 while checks are pending — expected, not a failure.
|
|
102
|
+
const ghFaulted = first.status !== 0 && first.status !== 8;
|
|
103
|
+
if (ghFaulted) {
|
|
104
|
+
logger.warn?.(
|
|
105
|
+
`[Watcher] gh pr checks failed (status=${first.status}): ${first.stderr}`,
|
|
106
|
+
);
|
|
119
107
|
}
|
|
120
|
-
return
|
|
108
|
+
return {
|
|
109
|
+
outcomes: {},
|
|
110
|
+
requiredChecks: [],
|
|
111
|
+
polls: 0,
|
|
112
|
+
updatesApplied: 0,
|
|
113
|
+
resumesApplied: 0,
|
|
114
|
+
terminal: false,
|
|
115
|
+
green: false,
|
|
116
|
+
stillRunning: false,
|
|
117
|
+
requiredChecksEmpty: true,
|
|
118
|
+
error: `gh-checks-${ghFaulted ? 'failed' : 'empty'}:status=${first.status}`,
|
|
119
|
+
};
|
|
121
120
|
}
|
|
122
121
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
122
|
+
function updateBranchPort(ctx) {
|
|
123
|
+
return async () => {
|
|
124
|
+
const update = ctx.ghPrUpdateBranchFn(ctx.target);
|
|
125
|
+
return update.status === 0
|
|
126
|
+
? { ok: true }
|
|
127
|
+
: { ok: false, detail: `status=${update.status}: ${update.stderr}` };
|
|
128
|
+
};
|
|
129
129
|
}
|
|
130
130
|
|
|
131
|
-
/**
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
131
|
+
/** One BEHIND-recovery attempt; true only when the branch head moved. */
|
|
132
|
+
async function recoverBehind(ctx, updatesUsed) {
|
|
133
|
+
const view = ctx.ghPrViewFn(ctx.target);
|
|
134
|
+
if (view.status !== 0) {
|
|
135
|
+
ctx.logger.warn?.(
|
|
136
|
+
`[Watcher] gh pr view failed (status=${view.status}): ${view.stderr}`,
|
|
137
|
+
);
|
|
138
|
+
return false;
|
|
139
139
|
}
|
|
140
|
-
|
|
140
|
+
const recovery = await applyBehindUpdate({
|
|
141
|
+
mergeStateStatus: parseMergeStateStatus(view.stdout),
|
|
142
|
+
updatesUsed,
|
|
143
|
+
maxUpdates: ctx.maxUpdates,
|
|
144
|
+
updateBranch: updateBranchPort(ctx),
|
|
145
|
+
onUpdateFailed: (detail) =>
|
|
146
|
+
ctx.logger.warn?.(`[Watcher] gh pr update-branch failed (${detail})`),
|
|
147
|
+
});
|
|
148
|
+
return recovery.updated;
|
|
141
149
|
}
|
|
142
150
|
|
|
143
|
-
/**
|
|
144
|
-
|
|
145
|
-
return
|
|
151
|
+
/** BEHIND recovery only when all green; `maxUpdates` stops a base race. */
|
|
152
|
+
function canRecoverBehind(ctx, state) {
|
|
153
|
+
return (
|
|
154
|
+
allTerminal(state.outcomes) &&
|
|
155
|
+
allGreen(state.outcomes) &&
|
|
156
|
+
state.updatesApplied < ctx.maxUpdates
|
|
157
|
+
);
|
|
146
158
|
}
|
|
147
159
|
|
|
148
|
-
|
|
149
|
-
|
|
160
|
+
/** One poll-to-cap + BEHIND-recovery arm; mutates `state`. */
|
|
161
|
+
async function runPollArm(ctx, state) {
|
|
162
|
+
while (state.polls < ctx.maxPolls) {
|
|
163
|
+
const polled = await pollUntilTerminal({
|
|
164
|
+
...ctx.target,
|
|
165
|
+
outcomes: state.outcomes,
|
|
166
|
+
polls: state.polls,
|
|
167
|
+
maxPolls: ctx.maxPolls,
|
|
168
|
+
ghPrChecksFn: ctx.ghPrChecksFn,
|
|
169
|
+
pollIntervalMs: ctx.pollIntervalMs,
|
|
170
|
+
sleepFn: ctx.sleepFn,
|
|
171
|
+
discountFn: ctx.discountFn,
|
|
172
|
+
logger: ctx.logger,
|
|
173
|
+
});
|
|
174
|
+
state.outcomes = polled.outcomes;
|
|
175
|
+
state.polls = polled.polls;
|
|
176
|
+
if (!canRecoverBehind(ctx, state)) return;
|
|
177
|
+
// A failed update must not re-poll as though the head moved.
|
|
178
|
+
if (!(await recoverBehind(ctx, state.updatesApplied))) return;
|
|
179
|
+
state.updatesApplied += 1;
|
|
180
|
+
ctx.logger.info?.(
|
|
181
|
+
`[Watcher] PR BEHIND base — issued gh pr update-branch (#${state.updatesApplied}/${ctx.maxUpdates}); re-polling required checks.`,
|
|
182
|
+
);
|
|
183
|
+
await ctx.sleepFn(ctx.pollIntervalMs);
|
|
184
|
+
// The new head invalidates the previous terminal outcomes.
|
|
185
|
+
state.outcomes = Object.fromEntries(
|
|
186
|
+
ctx.requiredChecks.map((name) => [name, 'pending']),
|
|
187
|
+
);
|
|
188
|
+
}
|
|
150
189
|
}
|
|
151
190
|
|
|
152
|
-
|
|
153
|
-
|
|
191
|
+
/** Re-arm only when pending-but-not-failed with resume budget left. */
|
|
192
|
+
function shouldResume(ctx, state) {
|
|
193
|
+
return (
|
|
194
|
+
!allTerminal(state.outcomes) &&
|
|
195
|
+
!hasFailingCheck(state.outcomes) &&
|
|
196
|
+
state.resumesApplied < ctx.maxResumes
|
|
197
|
+
);
|
|
154
198
|
}
|
|
155
199
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
export async function pollUntilTerminal({
|
|
174
|
-
prUrl,
|
|
175
|
-
cwd,
|
|
176
|
-
repo = null,
|
|
177
|
-
outcomes,
|
|
178
|
-
polls,
|
|
179
|
-
maxPolls,
|
|
180
|
-
ghPrChecksFn,
|
|
181
|
-
pollIntervalMs,
|
|
182
|
-
sleepFn,
|
|
183
|
-
discountFn = identityDiscount,
|
|
184
|
-
logger,
|
|
185
|
-
}) {
|
|
186
|
-
let currentOutcomes = outcomes;
|
|
187
|
-
let currentPolls = polls;
|
|
188
|
-
while (!allTerminal(currentOutcomes) && currentPolls < maxPolls) {
|
|
189
|
-
await sleepFn(pollIntervalMs);
|
|
190
|
-
currentPolls += 1;
|
|
191
|
-
const probe = ghPrChecksFn({ prUrl, cwd, repo });
|
|
192
|
-
const entries = parseGhPrChecks(probe.stdout);
|
|
193
|
-
if (entries.length === 0 && probe.status !== 0 && probe.status !== 8) {
|
|
194
|
-
logger.warn?.(
|
|
195
|
-
`[Watcher] gh pr checks transient failure (status=${probe.status}): ${probe.stderr}`,
|
|
196
|
-
);
|
|
197
|
-
continue;
|
|
198
|
-
}
|
|
199
|
-
currentOutcomes = await discountFn(entries, reduceOutcomes(entries));
|
|
200
|
-
}
|
|
201
|
-
return { outcomes: currentOutcomes, polls: currentPolls };
|
|
200
|
+
function finalVerdict(ctx, state) {
|
|
201
|
+
const terminal = allTerminal(state.outcomes);
|
|
202
|
+
// Never `'timed_out'` — the auto-merge predicate would read it as failure.
|
|
203
|
+
const stillRunning = !terminal && !hasFailingCheck(state.outcomes);
|
|
204
|
+
const outcomes = terminal
|
|
205
|
+
? state.outcomes
|
|
206
|
+
: promotePendingToStillRunning(state.outcomes);
|
|
207
|
+
return {
|
|
208
|
+
outcomes,
|
|
209
|
+
requiredChecks: ctx.requiredChecks,
|
|
210
|
+
polls: state.polls,
|
|
211
|
+
updatesApplied: state.updatesApplied,
|
|
212
|
+
resumesApplied: state.resumesApplied,
|
|
213
|
+
terminal,
|
|
214
|
+
green: terminal && allGreen(outcomes),
|
|
215
|
+
stillRunning,
|
|
216
|
+
};
|
|
202
217
|
}
|
|
203
218
|
|
|
204
219
|
/**
|
|
@@ -255,123 +270,41 @@ export async function watchPrToTerminal({
|
|
|
255
270
|
logger,
|
|
256
271
|
firstProbe,
|
|
257
272
|
}) {
|
|
258
|
-
const
|
|
259
|
-
|
|
273
|
+
const target = { prUrl, cwd, repo };
|
|
274
|
+
const first = firstProbe ?? ghPrChecksFn(target);
|
|
260
275
|
const firstEntries = parseGhPrChecks(first.stdout);
|
|
261
276
|
if (firstEntries.length === 0) {
|
|
262
|
-
|
|
263
|
-
// would report a red verdict with no failing check. Names resolve once
|
|
264
|
-
// per call, so the caller's attach window re-resolves by calling again.
|
|
265
|
-
const ghFaulted = first.status !== 0 && first.status !== 8;
|
|
266
|
-
if (ghFaulted) {
|
|
267
|
-
logger.warn?.(
|
|
268
|
-
`[Watcher] gh pr checks failed (status=${first.status}): ${first.stderr}`,
|
|
269
|
-
);
|
|
270
|
-
}
|
|
271
|
-
return {
|
|
272
|
-
outcomes: {},
|
|
273
|
-
requiredChecks: [],
|
|
274
|
-
polls: 0,
|
|
275
|
-
updatesApplied: 0,
|
|
276
|
-
resumesApplied: 0,
|
|
277
|
-
terminal: false,
|
|
278
|
-
green: false,
|
|
279
|
-
stillRunning: false,
|
|
280
|
-
requiredChecksEmpty: true,
|
|
281
|
-
// `gh` overloads non-zero for "none attached yet" and a real fault, and
|
|
282
|
-
// stderr is not a contract, so classification is left to the caller.
|
|
283
|
-
error: `gh-checks-${ghFaulted ? 'failed' : 'empty'}:status=${first.status}`,
|
|
284
|
-
};
|
|
277
|
+
return emptyRequiredChecksResult(first, logger);
|
|
285
278
|
}
|
|
286
|
-
|
|
287
|
-
const requiredChecks = firstEntries.map((e) => e.name);
|
|
288
|
-
|
|
289
279
|
const discountFn = createCancelledRunDiscount({ repo, ghApiFn, logger });
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
280
|
+
const ctx = {
|
|
281
|
+
target,
|
|
282
|
+
requiredChecks: firstEntries.map((e) => e.name),
|
|
283
|
+
maxPolls,
|
|
284
|
+
maxUpdates,
|
|
285
|
+
maxResumes,
|
|
286
|
+
pollIntervalMs,
|
|
287
|
+
ghPrChecksFn,
|
|
288
|
+
ghPrViewFn,
|
|
289
|
+
ghPrUpdateBranchFn,
|
|
290
|
+
sleepFn,
|
|
291
|
+
discountFn,
|
|
292
|
+
logger,
|
|
293
|
+
};
|
|
294
|
+
const state = {
|
|
295
|
+
outcomes: await discountFn(firstEntries, reduceOutcomes(firstEntries)),
|
|
296
|
+
polls: 0,
|
|
297
|
+
updatesApplied: 0,
|
|
298
|
+
resumesApplied: 0,
|
|
299
|
+
};
|
|
295
300
|
for (;;) {
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
repo,
|
|
301
|
-
outcomes,
|
|
302
|
-
polls,
|
|
303
|
-
maxPolls,
|
|
304
|
-
ghPrChecksFn,
|
|
305
|
-
pollIntervalMs,
|
|
306
|
-
sleepFn,
|
|
307
|
-
discountFn,
|
|
308
|
-
logger,
|
|
309
|
-
}));
|
|
310
|
-
// BEHIND recovery only when all green; `maxUpdates` stops a racing
|
|
311
|
-
// base from ping-ponging forever.
|
|
312
|
-
if (!allTerminal(outcomes) || !allGreen(outcomes)) break;
|
|
313
|
-
// Checked before the `gh pr view` spawn to save the round-trip.
|
|
314
|
-
if (updatesApplied >= maxUpdates) break;
|
|
315
|
-
const view = ghPrViewFn({ prUrl, cwd, repo });
|
|
316
|
-
if (view.status !== 0) {
|
|
317
|
-
logger.warn?.(
|
|
318
|
-
`[Watcher] gh pr view failed (status=${view.status}): ${view.stderr}`,
|
|
319
|
-
);
|
|
320
|
-
break;
|
|
321
|
-
}
|
|
322
|
-
const recovery = await applyBehindUpdate({
|
|
323
|
-
mergeStateStatus: parseMergeStateStatus(view.stdout),
|
|
324
|
-
updatesUsed: updatesApplied,
|
|
325
|
-
maxUpdates,
|
|
326
|
-
updateBranch: async () => {
|
|
327
|
-
const update = ghPrUpdateBranchFn({ prUrl, cwd, repo });
|
|
328
|
-
return update.status === 0
|
|
329
|
-
? { ok: true }
|
|
330
|
-
: {
|
|
331
|
-
ok: false,
|
|
332
|
-
detail: `status=${update.status}: ${update.stderr}`,
|
|
333
|
-
};
|
|
334
|
-
},
|
|
335
|
-
onUpdateFailed: (detail) =>
|
|
336
|
-
logger.warn?.(`[Watcher] gh pr update-branch failed (${detail})`),
|
|
337
|
-
});
|
|
338
|
-
// A failed update must not re-poll as though the head moved.
|
|
339
|
-
if (!recovery.updated) break;
|
|
340
|
-
updatesApplied += 1;
|
|
341
|
-
logger.info?.(
|
|
342
|
-
`[Watcher] PR BEHIND base — issued gh pr update-branch (#${updatesApplied}/${maxUpdates}); re-polling required checks.`,
|
|
343
|
-
);
|
|
344
|
-
await sleepFn(pollIntervalMs);
|
|
345
|
-
// The new head invalidates the previous terminal outcomes.
|
|
346
|
-
outcomes = {};
|
|
347
|
-
for (const name of requiredChecks) outcomes[name] = 'pending';
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
// Re-arm only when pending-but-not-failed with resume budget left.
|
|
351
|
-
if (allTerminal(outcomes) || hasFailingCheck(outcomes)) break;
|
|
352
|
-
if (resumesApplied >= maxResumes) break;
|
|
353
|
-
resumesApplied += 1;
|
|
354
|
-
polls = 0;
|
|
301
|
+
await runPollArm(ctx, state);
|
|
302
|
+
if (!shouldResume(ctx, state)) break;
|
|
303
|
+
state.resumesApplied += 1;
|
|
304
|
+
state.polls = 0;
|
|
355
305
|
logger.info?.(
|
|
356
|
-
`[Watcher] poll cap reached with checks still pending; re-arming watch (resume #${resumesApplied}/${maxResumes}).`,
|
|
306
|
+
`[Watcher] poll cap reached with checks still pending; re-arming watch (resume #${state.resumesApplied}/${maxResumes}).`,
|
|
357
307
|
);
|
|
358
308
|
}
|
|
359
|
-
|
|
360
|
-
const terminal = allTerminal(outcomes);
|
|
361
|
-
const failing = hasFailingCheck(outcomes);
|
|
362
|
-
// Never `'timed_out'` — the auto-merge predicate would read it as failure.
|
|
363
|
-
const stillRunning = !terminal && !failing;
|
|
364
|
-
const finalOutcomes = terminal
|
|
365
|
-
? outcomes
|
|
366
|
-
: promotePendingToStillRunning(outcomes);
|
|
367
|
-
return {
|
|
368
|
-
outcomes: finalOutcomes,
|
|
369
|
-
requiredChecks,
|
|
370
|
-
polls,
|
|
371
|
-
updatesApplied,
|
|
372
|
-
resumesApplied,
|
|
373
|
-
terminal,
|
|
374
|
-
green: terminal && allGreen(finalOutcomes),
|
|
375
|
-
stillRunning,
|
|
376
|
-
};
|
|
309
|
+
return finalVerdict(ctx, state);
|
|
377
310
|
}
|