@edgehero/pi-dispatch-receiver 1.0.0 → 1.1.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/package.json +1 -1
- package/src/filter-azure.mjs +7 -0
- package/src/filter-forgejo.mjs +7 -0
- package/src/filter-gitlab.mjs +7 -0
- package/src/poller.mjs +4 -1
- package/src/receiver.mjs +45 -20
- package/src/start.mjs +8 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@edgehero/pi-dispatch-receiver",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Webhook receiver for pi-dispatch: the always-on edge that verifies GitHub, GitLab, Forgejo and Azure DevOps deliveries and enqueues (at most) one job per event for the worker.",
|
|
6
6
|
"keywords": [
|
package/src/filter-azure.mjs
CHANGED
|
@@ -116,6 +116,10 @@ export function filterAzure(subset, triggers, knownFlows, selfId, authorized, de
|
|
|
116
116
|
...(resolved.skillsDir !== undefined ? { skillsDir: resolved.skillsDir } : {}),
|
|
117
117
|
...(resolved.instructions !== undefined ? { instructions: resolved.instructions } : {}),
|
|
118
118
|
...(resolved.resume !== undefined ? { resume: resolved.resume } : {}),
|
|
119
|
+
// How many independent sandboxes race this flow (REQ-REPLICA-RUNS). At JOB level and conditional for the
|
|
120
|
+
// reasons filter.mjs states in full: receiver.mjs reads this to decide how many times to enqueue, and an
|
|
121
|
+
// unflagged job's data must stay byte-identical to today's.
|
|
122
|
+
...(resolved.replicas !== undefined ? { replicas: resolved.replicas } : {}),
|
|
119
123
|
trigger: {
|
|
120
124
|
event,
|
|
121
125
|
action: resolved.action,
|
|
@@ -179,6 +183,7 @@ function matchLabelRules(subset, triggers, labels, action) {
|
|
|
179
183
|
skillsDir: rule.skillsDir,
|
|
180
184
|
instructions: rule.instructions,
|
|
181
185
|
resume: rule.resume,
|
|
186
|
+
replicas: rule.replicas,
|
|
182
187
|
matched: { index: rule.index, type: "label", label: matchedLabel(L, rule.predicate) },
|
|
183
188
|
target: { type: "issue", number: subset.target?.number, title: subset.target?.title, body: subset.target?.body },
|
|
184
189
|
};
|
|
@@ -219,6 +224,7 @@ function routeComment(subset, triggers, knownFlows, targetType) {
|
|
|
219
224
|
skillsDir: triggers.comment.skillsDir,
|
|
220
225
|
instructions: triggers.comment.instructions,
|
|
221
226
|
resume: triggers.comment.resume,
|
|
227
|
+
replicas: triggers.comment.replicas,
|
|
222
228
|
matched: { index: triggers.comment.index, type: "comment", phrase },
|
|
223
229
|
// No author_association: Azure has none, and the authority that admitted this comment was resolved
|
|
224
230
|
// from the graph, not read off the body.
|
|
@@ -248,6 +254,7 @@ function routePullRequest(subset, triggers, action) {
|
|
|
248
254
|
skillsDir: rule.skillsDir,
|
|
249
255
|
instructions: rule.instructions,
|
|
250
256
|
resume: rule.resume,
|
|
257
|
+
replicas: rule.replicas,
|
|
251
258
|
matched: { index: rule.index, type: "pull_request", action },
|
|
252
259
|
target: {
|
|
253
260
|
type: "pull_request",
|
package/src/filter-forgejo.mjs
CHANGED
|
@@ -118,6 +118,10 @@ export function filterForgejo(eventName, subset, triggers, knownFlows, selfId, a
|
|
|
118
118
|
...(resolved.skillsDir !== undefined ? { skillsDir: resolved.skillsDir } : {}),
|
|
119
119
|
...(resolved.instructions !== undefined ? { instructions: resolved.instructions } : {}),
|
|
120
120
|
...(resolved.resume !== undefined ? { resume: resolved.resume } : {}),
|
|
121
|
+
// How many independent sandboxes race this flow (REQ-REPLICA-RUNS). At JOB level and conditional for the
|
|
122
|
+
// reasons filter.mjs states in full: receiver.mjs reads this to decide how many times to enqueue, and an
|
|
123
|
+
// unflagged job's data must stay byte-identical to today's.
|
|
124
|
+
...(resolved.replicas !== undefined ? { replicas: resolved.replicas } : {}),
|
|
121
125
|
trigger: {
|
|
122
126
|
event: eventName,
|
|
123
127
|
// Forgejo's OWN word, not our translation of it. The run record should say what the forge said.
|
|
@@ -147,6 +151,7 @@ function routeIssueLabel(subset, triggers) {
|
|
|
147
151
|
skillsDir: rule.skillsDir,
|
|
148
152
|
instructions: rule.instructions,
|
|
149
153
|
resume: rule.resume,
|
|
154
|
+
replicas: rule.replicas,
|
|
150
155
|
matched: { index: rule.index, type: "label", label: matchedLabel(L, rule.predicate) },
|
|
151
156
|
target: { type: "issue", number: subset.issue?.number, title: subset.issue?.title, body: subset.issue?.body },
|
|
152
157
|
};
|
|
@@ -192,6 +197,7 @@ function routeComment(subset, triggers, knownFlows) {
|
|
|
192
197
|
skillsDir: triggers.comment.skillsDir,
|
|
193
198
|
instructions: triggers.comment.instructions,
|
|
194
199
|
resume: triggers.comment.resume,
|
|
200
|
+
replicas: triggers.comment.replicas,
|
|
195
201
|
matched: { index: triggers.comment.index, type: "comment", phrase },
|
|
196
202
|
// The invoking comment rides on the trigger. No author_association: Forgejo has none, and the
|
|
197
203
|
// authority that admitted this comment was resolved from the API, not read off the body.
|
|
@@ -219,6 +225,7 @@ function routePullRequest(subset, triggers, action) {
|
|
|
219
225
|
skillsDir: rule.skillsDir,
|
|
220
226
|
instructions: rule.instructions,
|
|
221
227
|
resume: rule.resume,
|
|
228
|
+
replicas: rule.replicas,
|
|
222
229
|
matched: { index: rule.index, type: "pull_request", action },
|
|
223
230
|
target: {
|
|
224
231
|
type: "pull_request",
|
package/src/filter-gitlab.mjs
CHANGED
|
@@ -111,6 +111,10 @@ export function filterGitLab(subset, triggers, knownFlows, selfId, authorized, d
|
|
|
111
111
|
// Conditional like packages/image, and for the same reason: an unflagged job's data must stay
|
|
112
112
|
// byte-identical to today's, so the key is absent rather than present-and-undefined.
|
|
113
113
|
...(resolved.resume !== undefined ? { resume: resolved.resume } : {}),
|
|
114
|
+
// How many independent sandboxes race this flow (REQ-REPLICA-RUNS). At JOB level and conditional for the
|
|
115
|
+
// reasons filter.mjs states in full: receiver.mjs reads this to decide how many times to enqueue, and an
|
|
116
|
+
// unflagged job's data must stay byte-identical to today's.
|
|
117
|
+
...(resolved.replicas !== undefined ? { replicas: resolved.replicas } : {}),
|
|
114
118
|
trigger: {
|
|
115
119
|
event: kind,
|
|
116
120
|
action: subset.action,
|
|
@@ -145,6 +149,7 @@ function routeLabel(subset, triggers, targetType) {
|
|
|
145
149
|
skillsDir: rule.skillsDir,
|
|
146
150
|
instructions: rule.instructions,
|
|
147
151
|
resume: rule.resume,
|
|
152
|
+
replicas: rule.replicas,
|
|
148
153
|
matched: { index: rule.index, type: "label", label: matchedLabel(added, rule.predicate) },
|
|
149
154
|
target: buildTarget(subset, targetType),
|
|
150
155
|
};
|
|
@@ -205,6 +210,7 @@ function mrResult(subset, rule, matched) {
|
|
|
205
210
|
skillsDir: rule.skillsDir,
|
|
206
211
|
instructions: rule.instructions,
|
|
207
212
|
resume: rule.resume,
|
|
213
|
+
replicas: rule.replicas,
|
|
208
214
|
matched,
|
|
209
215
|
target: buildTarget(subset, "pull_request"),
|
|
210
216
|
};
|
|
@@ -251,6 +257,7 @@ function routeNote(subset, triggers, knownFlows) {
|
|
|
251
257
|
skillsDir: triggers.comment.skillsDir,
|
|
252
258
|
instructions: triggers.comment.instructions,
|
|
253
259
|
resume: triggers.comment.resume,
|
|
260
|
+
replicas: triggers.comment.replicas,
|
|
254
261
|
matched: { index: triggers.comment.index, type: "comment", phrase },
|
|
255
262
|
target: buildTarget(subset, targetType),
|
|
256
263
|
// The invoking comment rides the job as DATA (CONST-ISSUE-TEXT-IS-DATA). No author_association
|
package/src/poller.mjs
CHANGED
|
@@ -804,7 +804,10 @@ function makeAppInstallationTokenFn(github, { fetchFn, readFile, now }) {
|
|
|
804
804
|
let cached = null; // { token, expiresAtMs }
|
|
805
805
|
return async () => {
|
|
806
806
|
if (cached !== null && cached.expiresAtMs - now() > 5 * 60_000) return cached.token;
|
|
807
|
-
|
|
807
|
+
// Inline key (GITHUB_APP_PRIVATE_KEY) when the operator supplied one, the file otherwise. The shared
|
|
808
|
+
// loadGitHubAuth normalised and shape-checked it at load and refuses both-set, so there is nothing to
|
|
809
|
+
// decide here (issue #208).
|
|
810
|
+
const pem = github.privateKey ?? (await readFile(github.privateKeyPath, "utf8"));
|
|
808
811
|
const jwt = appJwt(github.appId, pem, now());
|
|
809
812
|
const res = await fetchFn(`${API_URL}/app/installations/${github.installationId}/access_tokens`, {
|
|
810
813
|
method: "POST",
|
package/src/receiver.mjs
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* The webhook receiver: a thin producer that turns a verified
|
|
2
|
+
* The webhook receiver: a thin producer that turns a verified forge webhook into (at most) one queued
|
|
3
3
|
* job -- or, when the matched trigger opted into `run.replicas`, into exactly that many independent ones
|
|
4
|
-
* (REQ-REPLICA-RUNS
|
|
4
|
+
* (REQ-REPLICA-RUNS, on every forge since #187; the poller stays github-only, so replicas on the other
|
|
5
|
+
* three arrive by webhook alone). It composes the three pieces that own the hard parts -- `makeVerifiedHandler` (the
|
|
5
6
|
* HMAC trust boundary), `filter` (the trigger/author gate), and the SHARED `enqueueGitHubJob` -- and adds
|
|
6
7
|
* only the glue: parse the verified body, project the payload subset, route on the filter's verdict, and
|
|
7
8
|
* map the outcome to a status code.
|
|
@@ -169,6 +170,36 @@ function pathOf(url) {
|
|
|
169
170
|
return raw.length > 1 && raw.endsWith("/") ? raw.slice(0, -1) : raw;
|
|
170
171
|
}
|
|
171
172
|
|
|
173
|
+
/**
|
|
174
|
+
* REPLICA FANOUT (REQ-REPLICA-RUNS). The one place a single delivery becomes more than one job, shared by
|
|
175
|
+
* all four forge arms since #187 widened `run.replicas` past github.
|
|
176
|
+
*
|
|
177
|
+
* ONE body rather than four, for the reason `enqueueForgeJob` gives for collapsing its own wrappers
|
|
178
|
+
* (queue.mjs): four copies is four places for one of them to be quietly weakened while every test stays
|
|
179
|
+
* green. What would be weakened here is the `replicas > 1` conditional, whose whole job is byte-identity
|
|
180
|
+
* for an unflagged delivery -- spread `replica: i` unconditionally in one arm and that forge's `data`, its
|
|
181
|
+
* jobId and its dedup id all change for every ordinary job, with no test outside that forge to notice.
|
|
182
|
+
*
|
|
183
|
+
* Absent `replicas` is `1` and the call below is byte-identical to the single enqueue it replaced: no
|
|
184
|
+
* `replica` key on the job, so the jobId, the dedup id and `data` are exactly what they were.
|
|
185
|
+
*
|
|
186
|
+
* PARTIAL FAILURE IS IDEMPOTENT BY CONSTRUCTION, which is why there is no compensating logic here: if
|
|
187
|
+
* replica k throws, the caller's catch answers 503, the forge redelivers, replicas 1..k-1 dedup on their
|
|
188
|
+
* own now-taken jobIds and k..n enqueue. The retry converges on exactly n jobs rather than n + (k-1).
|
|
189
|
+
*
|
|
190
|
+
* `enqueue` is a callback because the four arms spell their enqueue differently (a named github/gitlab
|
|
191
|
+
* wrapper, or `enqueueForgeJob` with an explicit kind); the fanout itself is forge-blind.
|
|
192
|
+
*
|
|
193
|
+
* @returns {Promise<number>} how many jobs were enqueued, for the caller's `enqueued` log line.
|
|
194
|
+
*/
|
|
195
|
+
async function fanout(job, enqueue) {
|
|
196
|
+
const replicas = job.replicas ?? 1;
|
|
197
|
+
for (let i = 1; i <= replicas; i++) {
|
|
198
|
+
await enqueue(replicas > 1 ? { ...job, replica: i } : job);
|
|
199
|
+
}
|
|
200
|
+
return replicas;
|
|
201
|
+
}
|
|
202
|
+
|
|
172
203
|
/**
|
|
173
204
|
* The GitHub arm. Returns `makeVerifiedHandler`'s handler directly, so it only ever sees an already-verified
|
|
174
205
|
* request. `onVerified` owns parse, filter, enqueue, and response; a good signature is the sole
|
|
@@ -189,19 +220,10 @@ function makeGitHubHandler({ queue, selfId, cfg, log }) {
|
|
|
189
220
|
return respond(res, 204);
|
|
190
221
|
}
|
|
191
222
|
|
|
192
|
-
//
|
|
193
|
-
|
|
194
|
-
// the call below is byte-identical to the single enqueue it replaced -- no `replica` key on the job,
|
|
195
|
-
// so the jobId, the dedup id and `data` are all exactly what they were.
|
|
196
|
-
//
|
|
197
|
-
// PARTIAL FAILURE IS IDEMPOTENT BY CONSTRUCTION, which is why there is no compensating logic here: if
|
|
198
|
-
// replica k throws, the catch below answers 503, GitHub redelivers, replicas 1..k-1 dedup on their own
|
|
199
|
-
// now-taken jobIds and k..n enqueue. The retry converges on exactly n jobs rather than n + (k-1).
|
|
200
|
-
const replicas = result.job.replicas ?? 1;
|
|
223
|
+
// Fanout (REQ-REPLICA-RUNS) lives in `fanout` above; the 202/503 decision stays here, where it always was.
|
|
224
|
+
let replicas;
|
|
201
225
|
try {
|
|
202
|
-
|
|
203
|
-
await enqueueGitHubJob(queue, replicas > 1 ? { ...result.job, replica: i } : result.job);
|
|
204
|
-
}
|
|
226
|
+
replicas = await fanout(result.job, (j) => enqueueGitHubJob(queue, j));
|
|
205
227
|
} catch (err) {
|
|
206
228
|
// Own try/catch so a Valkey-down enqueue is a 503 (retryable), not verify's outer 500.
|
|
207
229
|
log?.({ event: "enqueue_failed", delivery, reason: err?.message });
|
|
@@ -249,8 +271,9 @@ function makeGitLabHandler({ queue, cfg, log, mode, secret, selfId, resolveAutho
|
|
|
249
271
|
return respond(res, 204);
|
|
250
272
|
}
|
|
251
273
|
|
|
274
|
+
let replicas;
|
|
252
275
|
try {
|
|
253
|
-
await enqueueGitLabJob(queue,
|
|
276
|
+
replicas = await fanout(result.job, (j) => enqueueGitLabJob(queue, j));
|
|
254
277
|
} catch (err) {
|
|
255
278
|
log?.({ event: "enqueue_failed", delivery, reason: err?.message });
|
|
256
279
|
return respond(res, 503, { error: "enqueue-failed" }); // GitLab redelivers; dedup by webhook-id coalesces
|
|
@@ -259,7 +282,7 @@ function makeGitLabHandler({ queue, cfg, log, mode, secret, selfId, resolveAutho
|
|
|
259
282
|
// `!` for a merge request, `#` for an issue -- GitLab's own notation, and the same discrimination
|
|
260
283
|
// the semantic dedup key makes, because the two are separate number sequences.
|
|
261
284
|
const sep = result.job.target.type === "pull_request" ? "!" : "#";
|
|
262
|
-
log?.({ event: "enqueued", delivery, repo: result.job.repo, target: `${result.job.repo}${sep}${result.job.target.number}`, flow: result.job.flow });
|
|
285
|
+
log?.({ event: "enqueued", delivery, repo: result.job.repo, target: `${result.job.repo}${sep}${result.job.target.number}`, flow: result.job.flow, replicas });
|
|
263
286
|
return respond(res, 202, { status: "queued" });
|
|
264
287
|
});
|
|
265
288
|
}
|
|
@@ -300,14 +323,15 @@ function makeForgejoHandler({ queue, cfg, log, secret, selfId, resolveAuthority
|
|
|
300
323
|
return respond(res, 204);
|
|
301
324
|
}
|
|
302
325
|
|
|
326
|
+
let replicas;
|
|
303
327
|
try {
|
|
304
|
-
await enqueueForgeJob(queue, "forgejo",
|
|
328
|
+
replicas = await fanout(result.job, (j) => enqueueForgeJob(queue, "forgejo", j));
|
|
305
329
|
} catch (err) {
|
|
306
330
|
log?.({ event: "enqueue_failed", delivery, reason: err?.message });
|
|
307
331
|
return respond(res, 503, { error: "enqueue-failed" }); // Forgejo redelivers; dedup by GUID coalesces
|
|
308
332
|
}
|
|
309
333
|
|
|
310
|
-
log?.({ event: "enqueued", delivery, repo: result.job.repo, target: `${result.job.target.type}#${result.job.target.number}`, flow: result.job.flow });
|
|
334
|
+
log?.({ event: "enqueued", delivery, repo: result.job.repo, target: `${result.job.target.type}#${result.job.target.number}`, flow: result.job.flow, replicas });
|
|
311
335
|
return respond(res, 202, { status: "queued" });
|
|
312
336
|
});
|
|
313
337
|
}
|
|
@@ -357,8 +381,9 @@ function makeAzureHandler({ queue, cfg, log, mode, secret, headerName, selfId, r
|
|
|
357
381
|
return respond(res, 204);
|
|
358
382
|
}
|
|
359
383
|
|
|
384
|
+
let replicas;
|
|
360
385
|
try {
|
|
361
|
-
await enqueueForgeJob(queue, "azure",
|
|
386
|
+
replicas = await fanout(result.job, (j) => enqueueForgeJob(queue, "azure", j));
|
|
362
387
|
} catch (err) {
|
|
363
388
|
log?.({ event: "enqueue_failed", delivery, reason: err?.message });
|
|
364
389
|
return respond(res, 503, { error: "enqueue-failed" });
|
|
@@ -367,7 +392,7 @@ function makeAzureHandler({ queue, cfg, log, mode, secret, headerName, selfId, r
|
|
|
367
392
|
// `!` for a pull request, `#` for a work item -- Azure numbers them separately, and this is the same
|
|
368
393
|
// discrimination the semantic dedup key makes.
|
|
369
394
|
const sep = result.job.target.type === "pull_request" ? "!" : "#";
|
|
370
|
-
log?.({ event: "enqueued", delivery, repo: result.job.repo, target: `${result.job.repo}${sep}${result.job.target.number}`, flow: result.job.flow });
|
|
395
|
+
log?.({ event: "enqueued", delivery, repo: result.job.repo, target: `${result.job.repo}${sep}${result.job.target.number}`, flow: result.job.flow, replicas });
|
|
371
396
|
return respond(res, 202, { status: "queued" });
|
|
372
397
|
});
|
|
373
398
|
}
|
package/src/start.mjs
CHANGED
|
@@ -28,6 +28,7 @@ import { watch } from "node:fs";
|
|
|
28
28
|
import { dirname, basename } from "node:path";
|
|
29
29
|
import { loadReceiverConfig, triggersFilePath, reloadTriggers } from "./config.mjs";
|
|
30
30
|
import { makeReceiver } from "./receiver.mjs";
|
|
31
|
+
import { entryExitCode } from "./cli.mjs";
|
|
31
32
|
import { makeGitHubAuth } from "@edgehero/pi-dispatch/get-token";
|
|
32
33
|
import { resolveGitLabSelfId } from "@edgehero/pi-dispatch/gitlab-identity";
|
|
33
34
|
import { resolveForgejoSelfId } from "@edgehero/pi-dispatch/forgejo-identity";
|
|
@@ -192,6 +193,12 @@ function watchTriggers(env, cfg, log) {
|
|
|
192
193
|
if (import.meta.url === `file://${process.argv[1]}` || process.argv[1]?.endsWith("start.mjs")) {
|
|
193
194
|
startReceiver(process.env).catch((err) => {
|
|
194
195
|
process.stderr.write(`${JSON.stringify({ event: "receiver_start_failed", reason: err?.message })}\n`);
|
|
195
|
-
|
|
196
|
+
// entryExitCode, NOT a bare 1. This file is what `receiver.service` execs -- cli.mjs is not on that
|
|
197
|
+
// path -- so the mapping cli.mjs documents ("a supervisor restarting on exit 2 would loop on a config
|
|
198
|
+
// that can never parse") only reaches a real deployment from here. A tagged config refusal exits 2
|
|
199
|
+
// and `RestartPreventExitStatus=2` stops the unit; anything else is infra and stays retryable at 1.
|
|
200
|
+
// IMPORTED rather than restated: two copies of an exit-code rule is one place for it to drift, and
|
|
201
|
+
// the copy that drifts is the one nobody is looking at.
|
|
202
|
+
process.exitCode = entryExitCode(err);
|
|
196
203
|
});
|
|
197
204
|
}
|