@oakgreencc/graft 0.2.10 → 0.2.12

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.
@@ -106,7 +106,8 @@ them to the ruleset's bypass list.
106
106
  `graft setup project` asks whether to turn on **Allow auto-merge**, which
107
107
  `graft pr merge --auto` needs (`--auto-merge` / `--no-auto-merge` answer
108
108
  without asking). It defaults to yes when agents may merge into some base. The
109
- merge still waits for the ruleset's required checks and reviews.
109
+ merge still waits for the ruleset's required checks and reviews. A PR that is
110
+ already mergeable — GitHub will not arm auto-merge on one — is merged at once.
110
111
 
111
112
  Without `gh` logged in as a repository admin, both steps print where to do them
112
113
  by hand instead.
@@ -19,7 +19,7 @@ GitHub signs a commit an App creates **only when the request carries no `author`
19
19
 
20
20
  GitHub's secondary limit on content-creating requests (~80/minute, ~500/hour) is why text rides inline: a 610-file reformat once made 610 blob POSTs. The client waits out 403/429 refusals (`retry-after`, `x-ratelimit-reset`, or a "rate limit" body), doubling each time, and reports every wait on stderr.
21
21
 
22
- A 500/502/503/504 is different: it does not say whether GitHub performed the write. Only the content-addressed writes — `POST /git/blobs` and `POST /git/trees`, whose answer is a sha fixed by the body — are repeated on a 5xx (1s, doubling, four times, each wait reported). `POST /git/commits` and ref updates never are: a repeat could create a second commit.
22
+ A 500/502/503/504 is different: it does not say whether GitHub performed the write. Only the content-addressed writes — `POST /git/blobs` and `POST /git/trees`, whose answer is a sha fixed by the body — are repeated on a 5xx (1s, doubling, four times, each wait reported). So is a dropped connection (`fetch failed` with `UND_ERR_SOCKET`, `UND_ERR_CLOSED`, `ECONNRESET` or `EPIPE` — typically a keep-alive socket that went stale while git hooks ran), which says just as little. `POST /git/commits` and ref updates never are: a repeat could create a second commit.
23
23
 
24
24
  ## Merges
25
25
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oakgreencc/graft",
3
- "version": "0.2.10",
3
+ "version": "0.2.12",
4
4
  "description": "Let an agent work a GitHub project without the local git CLI's write path: Verified commits created by a GitHub App, the repo's git hooks run at the right moments, CODEOWNERS as the manifest of what needs a human, and a scoped maintainer mode for when one says yes.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "type": "module",
@@ -56,7 +56,7 @@ The worktree is mid-merge with something the branch does not contain. Publish it
56
56
 
57
57
  ## `graft pr merge` refusals
58
58
 
59
- - **base not in mergeableBases** — this project does not let agents merge into that branch; hand the PR to a human.
59
+ - **base not in mergeableBases** — this project does not let agents merge into that branch; hand the PR to a human. "has no graft policy yet" means the base carries no `.graft.json`, so the defaults applied (only the default branch); adding one with `mergeableBases` is a protected change.
60
60
  - **touches protected paths** — ask the code owner to approve the PR on GitHub, then run `graft pr merge <n>` again: an approval GitHub counts (an individual `@login` owner of every such path, on the current head — or on an earlier commit when the base's ruleset keeps stale approvals, as graft's does) lets it through, with a note naming the approver. The refusal's "No code-owner approval clears it: …" says what is missing. Paths owned only by a team, or protected only by `.graft.json`, are never cleared this way — the human merges those.
61
61
  - **base is red** — merging onto a known break buries it. If this PR is the fix: `graft pr update <n>`, let its required check finish green, then merge; a PR that contains the broken head and passes goes through.
62
62
  - **the policy on the base does not parse** — the base's `.graft.json` is broken, so the merge could only be judged by the defaults. Report it; fixing it is a protected change.
package/src/cli/pr.mjs CHANGED
@@ -10,7 +10,8 @@
10
10
  * in. `--auto` arms auto-merge instead — a deferred merge is still a merge,
11
11
  * and it is judged the same way, at arm time: arming it before a code owner's
12
12
  * approval exists is refused like the merge. (Its green therefore ages until
13
- * the merge lands; the base's own post-merge CI is the backstop.)
13
+ * the merge lands; the base's own post-merge CI is the backstop.) A PR that is
14
+ * already mergeable, which GitHub will not arm, is merged on the spot.
14
15
  *
15
16
  * `update` brings a PR up to date with its base (`update-branch`). It lands
16
17
  * nothing on the base, so it carries no policy — and it is the first step of
@@ -158,12 +159,20 @@ export async function run(argv, { client: given } = {}) {
158
159
  for (const n of out.notes) console.log(n)
159
160
 
160
161
  if (has(argv, '--auto')) {
161
- await client.graphql(
162
- 'mutation($id:ID!,$m:PullRequestMergeMethod!){enablePullRequestAutoMerge(input:{pullRequestId:$id,mergeMethod:$m}){clientMutationId}}',
163
- { id: pr.node_id, m: method.toUpperCase() },
164
- )
165
- console.log(`auto-merge armed on ${full}#${number} (${method}) — lands when its required checks pass`)
166
- return
162
+ try {
163
+ await client.graphql(
164
+ 'mutation($id:ID!,$m:PullRequestMergeMethod!){enablePullRequestAutoMerge(input:{pullRequestId:$id,mergeMethod:$m}){clientMutationId}}',
165
+ { id: pr.node_id, m: method.toUpperCase() },
166
+ )
167
+ console.log(`auto-merge armed on ${full}#${number} (${method}) — lands when its required checks pass`)
168
+ return
169
+ } catch (err) {
170
+ // GitHub will not arm auto-merge on a PR that can merge right now
171
+ // ("Pull request is in clean status"): nothing is left to wait for, so
172
+ // it merges now — judged by the same verdict as above.
173
+ if (!/is in clean status/.test(String(/** @type {Error} */ (err)?.message))) throw err
174
+ console.log(`${full}#${number} is already mergeable — merging now instead of arming auto-merge`)
175
+ }
167
176
  }
168
177
  // `sha` pins the head the policy judged: a push in between is refused (409).
169
178
  const merged = await client.json(`/repos/${owner}/${repo}/pulls/${number}/merge`, 'PUT', {
@@ -269,34 +269,54 @@ const noop = () => {}
269
269
 
270
270
  /**
271
271
  * How many times {@link retryServerErrors} repeats a request GitHub answered
272
- * with a 5xx. With the doubling in {@link serverErrorWait} that is 1+2+4+8s.
272
+ * with a 5xx, or that died on a dropped connection. With the doubling in
273
+ * {@link serverErrorWait} that is 1+2+4+8s.
273
274
  */
274
275
  export const SERVER_ERROR_RETRIES = 4
275
276
 
276
277
  const SERVER_ERRORS = new Set([500, 502, 503, 504])
277
278
 
279
+ /**
280
+ * The `cause.code`s of a `fetch failed` whose connection dropped under it —
281
+ * typically a keep-alive socket that went stale while git hooks ran.
282
+ */
283
+ const DROPPED_CONNECTION = new Set(['UND_ERR_SOCKET', 'UND_ERR_CLOSED', 'ECONNRESET', 'EPIPE'])
284
+
285
+ /**
286
+ * The code of a request that died on a dropped connection, or `null`.
287
+ *
288
+ * @param {any} err
289
+ * @returns {string | null}
290
+ */
291
+ export function droppedConnection(err) {
292
+ const code = err?.cause?.code ?? err?.code
293
+ return DROPPED_CONNECTION.has(code) ? code : null
294
+ }
295
+
278
296
  /**
279
297
  * How long to wait before repeating a request GitHub answered 500, 502, 503
280
- * or 504 — one second, doubling per `attempt` — or `null` for anything else.
298
+ * or 504, or that died on a dropped connection ({@link droppedConnection}) —
299
+ * one second, doubling per `attempt` — or `null` for anything else.
281
300
  *
282
301
  * Deliberately NOT part of {@link rateLimitWait} or the client's own retry:
283
- * a 5xx does not say whether GitHub performed the write. Repeating is safe
284
- * only for a content-addressed write — `POST /git/blobs`, `POST /git/trees`,
285
- * whose answer is a sha fixed by the body — and never for `POST /git/commits`
286
- * (a second commit object) or a ref update. So the caller opts in, per call
287
- * site, with {@link retryServerErrors}.
302
+ * a 5xx, or a connection that dropped mid-request, does not say whether
303
+ * GitHub performed the write. Repeating is safe only for a content-addressed
304
+ * write — `POST /git/blobs`, `POST /git/trees`, whose answer is a sha fixed by
305
+ * the body — and never for `POST /git/commits` (a second commit object) or a
306
+ * ref update. So the caller opts in, per call site, with {@link retryServerErrors}.
288
307
  *
289
308
  * @param {any} err what `request()` threw
290
309
  * @param {number} attempt 0 for the first retry
291
310
  */
292
311
  export function serverErrorWait(err, attempt) {
293
- return SERVER_ERRORS.has(err?.status) ? 1_000 * 2 ** attempt : null
312
+ return SERVER_ERRORS.has(err?.status) || droppedConnection(err) ? 1_000 * 2 ** attempt : null
294
313
  }
295
314
 
296
315
  /**
297
- * Run `call`, repeating it on a 5xx (see {@link serverErrorWait}) up to
298
- * `retries` times and `report`ing each wait. For idempotent, content-
299
- * addressed writes only; `label` names the request in the report.
316
+ * Run `call`, repeating it on a 5xx or a dropped connection (see
317
+ * {@link serverErrorWait}) up to `retries` times and `report`ing each wait.
318
+ * For idempotent, content-addressed writes only; `label` names the request in
319
+ * the report.
300
320
  *
301
321
  * @template T
302
322
  * @param {string} label
@@ -315,8 +335,9 @@ export async function retryServerErrors(
315
335
  } catch (err) {
316
336
  const wait = attempt < retries ? serverErrorWait(err, attempt) : null
317
337
  if (wait === null) throw err
338
+ const dropped = droppedConnection(err)
318
339
  report(
319
- `server error (${/** @type {any} */ (err).status}) on ${label}: waiting ${Math.ceil(wait / 1000)}s (retry ${attempt + 1}/${retries})`,
340
+ `${dropped ? `connection dropped (${dropped})` : `server error (${/** @type {any} */ (err).status})`} on ${label}: waiting ${Math.ceil(wait / 1000)}s (retry ${attempt + 1}/${retries})`,
320
341
  )
321
342
  await sleep(wait)
322
343
  }
package/src/pr-policy.mjs CHANGED
@@ -83,11 +83,18 @@ export async function mergeVerdict({
83
83
 
84
84
  // --- base -----------------------------------------------------------------
85
85
  if (!allowedBases.includes(pr.base)) {
86
+ const bases = allowedBases.map((b) => `\`${b}\``).join(', ')
86
87
  found.push({
87
88
  liftable: true,
88
- text: allowedBases.length
89
- ? `${label} targets \`${pr.base}\`; agents merge only into ${allowedBases.map((b) => `\`${b}\``).join(', ')} (mergeableBases in .graft.json).`
90
- : `${label}: this project lets no agent merge (mergeableBases is empty in .graft.json).`,
89
+ // No policy on the base at all (not even a broken one): the defaults
90
+ // applied, and `allowedBases` is GitHub's default branch — not a rule
91
+ // anyone wrote on this base. Say so, rather than cite a .graft.json.
92
+ text:
93
+ policy.configSource === null && !policyProblem && allowedBases.length
94
+ ? `${label} targets \`${pr.base}\`, which has no graft policy yet (no .graft.json there), so the defaults apply: agents merge only into ${bases}, the repo's default branch.`
95
+ : allowedBases.length
96
+ ? `${label} targets \`${pr.base}\`; agents merge only into ${bases} (mergeableBases in .graft.json).`
97
+ : `${label}: this project lets no agent merge (mergeableBases is empty in .graft.json).`,
91
98
  })
92
99
  }
93
100
 
@@ -342,8 +342,9 @@ export async function publishTree({
342
342
  }
343
343
 
344
344
  // Blob and tree writes are content-addressed — the answer is a sha fixed by
345
- // the body — so a 5xx is safe to repeat. Commit and ref writes are not (a
346
- // 5xx does not say whether GitHub made the commit or moved the ref, and a
345
+ // the body — so a 5xx or a dropped connection (a keep-alive socket gone
346
+ // stale while hooks ran) is safe to repeat. Commit and ref writes are not
347
+ // (neither says whether GitHub made the commit or moved the ref, and a
347
348
  // repeat could make a second commit), so they are never wrapped.
348
349
  const blobsPath = `/repos/${owner}/${repo}/git/blobs`
349
350
  const postBlobs = async (blobs) => {