Diffinery

homeflare/diffinerypublic

Open-source, agent-first GitHub and GitHub Actions alternative that runs on your own Cloudflare account: git hosting, pull requests, code review and drop-in GitHub Actions YAML on Workers, Durable Objects, R2 and Containers.

Merge pull request #15 from codician-team/claude/festive-wing-8c08e4

GitHub runners: start a runner for a job whose runner took another job
altugank authored · GitHub committed 2 parents a8c69dd29629cacommit a9efb8ea1b60322374aad27884b1a752d7ae8f43Browse files
3 files changed+94−7
modifiedapps/diffinery/docs/actions/github-runners.md+4−4View file
@@ -1,7 +1,7 @@
11 ---
22 title: GitHub runners
33 description: Keep your code on GitHub and run its Actions jobs on Diffinery's hosted runners: install the app, link the account that pays, and set runs-on to diffinery.
4-updated: 2026-10-30
4+updated: 2026-10-31
55 ---
66
77 You don't have to move your code to use Diffinery's runners. Keep your repositories and workflows on GitHub, install Diffinery's GitHub App, and change a job's `runs-on` to `diffinery`. The job then runs on a Diffinery hosted runner (a fresh Ubuntu 24.04 container with 4 vCPUs on Cloudflare) instead of a GitHub-hosted one. Logs, checks, caches, artifacts and `GITHUB_TOKEN` stay on GitHub, as with any self-hosted runner.
@@ -93,10 +93,10 @@
9393 | starting | The runner is registered and its container is starting, or it waits for GitHub to hand it the job. |
9494 | running | GitHub handed the runner a job. |
9595 | done | The container stopped. The GitHub job's conclusion is shown under it. |
96-| skipped | The job ended before its runner started (cancelled, or another runner took it), or the installation was suspended or unlinked. |
96+| skipped | The job ended before its runner started (cancelled, or run by a runner that isn't one of this account's Diffinery runners), or the installation was suspended or unlinked. |
9797 | failed | No runner was started: the reason is shown, such as a kill switch, minutes used up, or GitHub refusing to register a runner. |
9898
99-GitHub hands a runner any queued job of the repository whose labels it has, so the job a runner ran may be another one with the same labels than the job it was started for. Each queued job still gets one runner.
99+GitHub hands a runner any queued job of the repository whose labels it has, so a runner may run a different queued job with the same labels instead of the one it was started for. Each queued job still gets one runner: the runner's row then shows the job it ran, and the job it was started for waits for the next runner, in its place in line, with its own labels and size.
100100
101101 ## Security
102102
@@ -124,7 +124,7 @@
124124
125125 **"Could not register a runner on GitHub: ... Resource not accessible by integration".** The app lacks the **Administration: Read and write** permission on the repository. An owner of the GitHub account reviews and accepts the app's new permissions in the installation's settings on GitHub (**Choose repositories on GitHub** opens them).
126126
127-**"No job took this runner within 10 minutes".** The job it was started for was cancelled, or ran on another runner with the same labels, and GitHub had no other job for it. Diffinery stopped the runner and removed it from GitHub; nothing to do.
127+**"No job took this runner within 10 minutes".** The job it was started for was cancelled, or ran on another runner with the same labels, and GitHub had no other job for it. This also happens, rarely, when a job is cancelled just as the runner started for it takes a different queued job: Diffinery then starts one more runner for the cancelled job. Diffinery stopped the runner and removed it from GitHub; nothing to do.
128128
129129 **"The runner stopped before it took a job (exit code N); starting another".** GitHub's runner exited before a job started, for example because GitHub was unreachable. Diffinery tries up to 3 runners for a job.
130130
modifiedapps/diffinery/src/github-runners/service.ts+27−2View file
@@ -170,9 +170,10 @@
170170 .bind(ev.conclusion, ev.githubJobId, ev.jobName, ev.installationId, ev.runnerName)
171171 .run();
172172 }
173+ await this.took(ev.installationId, ev.runnerName, ev.githubJobId);
173174 }
174- // Cancelled before its runner started, or taken by another runner (one started for another job with the same labels):
175- // a runner started for it now would only wait for IDLE_MS.
175+ // Cancelled before its runner started, or ran on a runner that isn't one of this account's: a runner started
176+ // for it now would only wait for IDLE_MS.
176177 if (ev.action === "completed" || ev.runnerName) {
177178 await this.db
178179 .prepare("UPDATE github_runner_jobs SET status = 'skipped', message = ?, conclusion = ?, ended_at = ? WHERE installation_id = ? AND github_job_id = ? AND status = 'queued'")
@@ -182,6 +183,30 @@
182183 }
183184
184185 /**
186+ * A runner took `jobId`, which may not be the job its row was started for. When that job's own row still waits,
187+ * the two rows trade jobs: the runner's row describes the job it runs, and the other row waits for the job the
188+ * runner was started for, which still needs a runner, in that job's place in line.
189+ */
190+ private async took(installationId: number, runnerName: string, jobId: number): Promise<void> {
191+ const a = await this.db.prepare("SELECT * FROM github_runner_jobs WHERE installation_id = ? AND runner_name = ?").bind(installationId, runnerName).first<RunnerJobRow>();
192+ if (!a || a.github_job_id === jobId) return;
193+ const b = await this.db.prepare("SELECT * FROM github_runner_jobs WHERE installation_id = ? AND repo = ? AND github_job_id = ? AND status = 'queued'").bind(installationId, a.repo, jobId).first<RunnerJobRow>();
194+ if (!b) return;
195+ // (installation_id, github_job_id) is unique, so b's job id is negated (GitHub's never are) while a takes it.
196+ // The first step changes nothing unless both rows still hold the jobs they were read with, and the other two
197+ // only follow it. a keeps its size, which is its container's.
198+ const set = "github_job_id = ?, run_id = ?, workflow_name = ?, job_name = ?, html_url = ?, labels = ?, created_at = ?";
199+ const job = (r: RunnerJobRow) => [r.github_job_id, r.run_id, r.workflow_name, r.job_name, r.html_url, r.labels, r.created_at];
200+ await this.db.batch([
201+ this.db
202+ .prepare("UPDATE github_runner_jobs SET github_job_id = -github_job_id WHERE id = ? AND github_job_id = ? AND status = 'queued' AND EXISTS (SELECT 1 FROM github_runner_jobs WHERE id = ? AND github_job_id = ?)")
203+ .bind(b.id, b.github_job_id, a.id, a.github_job_id),
204+ this.db.prepare(`UPDATE github_runner_jobs SET ${set} WHERE id = ? AND EXISTS (SELECT 1 FROM github_runner_jobs WHERE id = ? AND github_job_id = ?)`).bind(...job(b), a.id, b.id, -b.github_job_id),
205+ this.db.prepare(`UPDATE github_runner_jobs SET ${set}, size = ? WHERE id = ? AND github_job_id = ?`).bind(...job(a), a.size, b.id, -b.github_job_id),
206+ ]);
207+ }
208+
209+ /**
185210 * Starts runners for the account's queued jobs, oldest first, and stops runners that took no job or ran too
186211 * long. Returns how soon to look again: null when nothing waits or runs.
187212 */
modifiedapps/diffinery/test/unit/github-runners.test.ts+63−1View file
@@ -187,7 +187,7 @@
187187 await w.svc.queued("ann", job(1), "large");
188188 await w.svc.jobEvent({ installationId: 11, githubJobId: 1, action: "completed", runnerName: null, jobName: "test 1", conclusion: "cancelled" });
189189 expect(w.rows()[0]).toMatchObject({ status: "skipped", conclusion: "cancelled", message: "The job ended before a runner started for it" });
190- // Another Diffinery runner, started for another job with the same labels, took it.
190+ // A Diffinery runner this account didn't start (another instance's, say) took it.
191191 await w.svc.queued("ann", job(2), "large");
192192 await w.svc.jobEvent({ installationId: 11, githubJobId: 2, action: "in_progress", runnerName: "diffinery-other-1", jobName: "test 2", conclusion: null });
193193 expect(w.rows()[1]).toMatchObject({ status: "skipped", message: "The job ran on diffinery-other-1 before a runner started for it" });
@@ -203,6 +203,68 @@
203203 expect(v.rows()[1]).toMatchObject({ status: "skipped", message: "The GitHub installation is no longer linked to this account" });
204204 });
205205
206+ it("still start a runner for a job when the runner started for it took another queued job", async () => {
207+ const w = world();
208+ const byJob = (id: number) => w.rows().find((r) => r.github_job_id === id)!;
209+ await w.svc.queued("ann", job(1, ["diffinery-small", "diffinery-medium"]), "medium");
210+ await w.svc.pump("ann");
211+ const first = w.rows()[0]!;
212+ // Job 2 is queued after job 1, and GitHub hands it to job 1's runner before a runner starts for it.
213+ w.clock.advance(1000);
214+ await w.svc.queued("ann", job(2, ["diffinery-small"]), "small");
215+ const queued2 = byJob(2).created_at;
216+ await w.svc.jobEvent({ installationId: 11, githubJobId: 2, action: "in_progress", runnerName: first.runner_name, jobName: "test 2", conclusion: null });
217+ // The rows trade jobs: the runner's row shows the job it runs, the other waits for job 1 in job 1's place in line.
218+ expect(byJob(2)).toMatchObject({ id: first.id, status: "running", job_name: "test 2", html_url: job(2).htmlUrl, labels: '["diffinery-small"]', size: "medium", created_at: queued2, ran_job_id: 2, container: first.container });
219+ expect(byJob(1)).toMatchObject({ status: "queued", job_name: "test 1", html_url: job(1).htmlUrl, labels: '["diffinery-small","diffinery-medium"]', size: "medium", created_at: first.created_at, runner_name: null, ran_job_id: null });
220+ const waiting = byJob(1);
221+
222+ await w.svc.pump("ann");
223+ expect(byJob(1)).toMatchObject({ id: waiting.id, status: "starting", runner_name: `diffinery-${waiting.id}-1`, container: `gh-${waiting.id}-1.medium` });
224+ expect(w.calls.filter((c) => c.op === "jit").map((c) => c.args[3])).toEqual([
225+ ["diffinery-small", "diffinery-medium"],
226+ ["diffinery-small", "diffinery-medium"],
227+ ]);
228+ expect(w.compute().map((c) => [c.id, c.ended_at])).toEqual([
229+ [first.container, null],
230+ [`gh-${waiting.id}-1.medium`, null],
231+ ]);
232+ // Each runner's row ends with its own container.
233+ await w.svc.jobEvent({ installationId: 11, githubJobId: 2, action: "completed", runnerName: first.runner_name, jobName: "test 2", conclusion: "success" });
234+ await w.svc.stopped(first.id, 0);
235+ expect(byJob(2)).toMatchObject({ status: "completed", conclusion: "success", exit_code: 0 });
236+ expect(byJob(1)).toMatchObject({ status: "starting" });
237+ expect(w.compute().map((c) => c.ended_at === null)).toEqual([false, true]);
238+
239+ // Job 1 cancelled while its row waits: the row is skipped and no runner starts for it.
240+ const v = world();
241+ await v.svc.queued("ann", job(1), "large");
242+ await v.svc.pump("ann");
243+ const runner = v.rows()[0]!.runner_name;
244+ await v.svc.queued("ann", job(2), "large");
245+ await v.svc.jobEvent({ installationId: 11, githubJobId: 2, action: "in_progress", runnerName: runner, jobName: "test 2", conclusion: null });
246+ await v.svc.jobEvent({ installationId: 11, githubJobId: 1, action: "completed", runnerName: null, jobName: "test 1", conclusion: "cancelled" });
247+ expect(v.rows().map((r) => [r.github_job_id, r.status, r.message])).toEqual([
248+ [1, "skipped", "The job ended before a runner started for it"],
249+ [2, "running", null],
250+ ]);
251+ await v.svc.pump("ann");
252+ expect(v.started).toHaveLength(1);
253+
254+ // A job whose own runner already started keeps it: GitHub hands that runner the job the other was started for.
255+ const u = world();
256+ await u.svc.queued("ann", job(1), "large");
257+ await u.svc.queued("ann", job(2), "large");
258+ await u.svc.pump("ann");
259+ const [r1, r2] = u.rows();
260+ await u.svc.jobEvent({ installationId: 11, githubJobId: 2, action: "in_progress", runnerName: r1!.runner_name, jobName: "test 2", conclusion: null });
261+ await u.svc.jobEvent({ installationId: 11, githubJobId: 1, action: "in_progress", runnerName: r2!.runner_name, jobName: "test 1", conclusion: null });
262+ expect(u.rows().map((r) => [r.id, r.github_job_id, r.status, r.ran_job_id])).toEqual([
263+ [r1!.id, 1, "running", 2],
264+ [r2!.id, 2, "running", 1],
265+ ]);
266+ });
267+
206268 it("run a size this Diffinery doesn't have on a large runner", async () => {
207269 const w = world({}, ["large"]);
208270 await w.svc.queued("ann", job(1, ["diffinery-tiny"]), "tiny");