Diffinery

codician/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.

96 lines · 4.2 KBRaw History
1import type { JobCompletion, JobSpec } from "@diffinery/shared";
2import { LogBatcher, RunnerApi } from "./api.js";
3import { JobRunner, errMsg } from "./job.js";
4import { UsageSampler, describeUsage } from "./usage.js";
5
6export interface AgentOptions {
7 /** `${apiUrl}/runner/jobs/${jobId}`, or null for offline mode (logs to stdout, no reports). */
8 apiBase: string | null;
9 workRoot: string;
10 toolCache?: string;
11 echoLogs?: boolean;
12 /** Install a SIGTERM/SIGINT handler that cancels the job. */
13 handleSignals?: boolean;
14 /**
15 * Hosted runners: on SIGTERM (a platform rollout replacing this container), keep
16 * running the job for up to this long before cancelling it. SIGINT or a second
17 * SIGTERM still cancels at once.
18 */
19 drainOnSigtermMs?: number;
20 /** Exposes the runner (e.g. so tests can cancel). */
21 onRunner?: (r: JobRunner) => void;
22 logIntervalMs?: number;
23 baseEnv?: NodeJS.ProcessEnv;
24 /**
25 * Measure what the job uses of the machine and report it with the result (hosted runners, where the
26 * container is the job's own machine; on a shared self-hosted machine the numbers would be the host's).
27 */
28 measureUsage?: boolean | UsageSampler;
29 /** Hosted runners: start the Docker daemon as the job begins when the job will use Docker (docker.ts prewarm). */
30 prewarmDocker?: boolean;
31}
32
33export function apiBaseFor(apiUrl: string | undefined | null, jobId: string): string | null {
34 if (!apiUrl) return null;
35 return `${apiUrl.replace(/\/$/, "")}/runner/jobs/${encodeURIComponent(jobId)}`;
36}
37
38/** Runs one job end-to-end: execute steps, ship logs, POST completion. */
39export async function runAgentJob(spec: JobSpec, o: AgentOptions): Promise<JobCompletion> {
40 const api = new RunnerApi(o.apiBase, spec.token);
41 const batcher = api.online
42 ? new LogBatcher((lines) => api.postLogs(lines), {
43 intervalMs: o.logIntervalMs ?? 500,
44 onError: (e) => process.stderr.write(`[diffinery-runner] log upload failed: ${errMsg(e)}\n`),
45 })
46 : null;
47 const runner = new JobRunner(spec, api, null, { workRoot: o.workRoot, toolCache: o.toolCache, echoLogs: o.echoLogs ?? !api.online, baseEnv: o.baseEnv, prewarmDocker: o.prewarmDocker }, batcher);
48 o.onRunner?.(runner);
49 const onSignal = (sig: string) => {
50 runner.logger.line(-1, `##[error]Received ${sig}; cancelling the job.`);
51 runner.cancel("cancelled");
52 };
53 let draining: NodeJS.Timeout | null = null;
54 const sigterm = () => {
55 if (!o.drainOnSigtermMs || draining) return onSignal("SIGTERM");
56 const minutes = Math.round(o.drainOnSigtermMs / 60_000);
57 runner.replacing = true;
58 runner.logger.line(-1, `##[warning]The platform is stopping this runner (a rollout replacing it with a new version, or the platform moving it). The job keeps running for up to ${minutes} minutes; if the platform stops a step or the time runs out, the job ends, and Diffinery starts it again on a new runner when doing so can't repeat anything outside the run.`);
59 draining = setTimeout(() => onSignal("SIGTERM"), o.drainOnSigtermMs);
60 draining.unref();
61 };
62 const sigint = () => onSignal("SIGINT");
63 if (o.handleSignals) {
64 process.on("SIGTERM", sigterm);
65 process.on("SIGINT", sigint);
66 }
67 const sampler = o.measureUsage instanceof UsageSampler ? o.measureUsage : o.measureUsage ? new UsageSampler() : null;
68 sampler?.start();
69 let completion: JobCompletion;
70 try {
71 completion = await runner.run();
72 } catch (e) {
73 completion = { conclusion: "failure", outputs: {}, failure: { message: `Runner error: ${errMsg(e)}` } };
74 }
75 const usage = sampler?.stop();
76 if (usage) {
77 completion.usage = usage;
78 runner.logger.line(-1, describeUsage(usage));
79 }
80 if (batcher) {
81 // After a SIGTERM the platform kills the container soon, so the result must not wait long on logs.
82 const ok = await batcher.close(draining ? 30_000 : 120_000);
83 if (!ok) process.stderr.write(`[diffinery-runner] gave up shipping ${batcher.pending} log lines\n`);
84 }
85 try {
86 await api.complete(completion);
87 } catch (e) {
88 process.stderr.write(`[diffinery-runner] failed to report completion: ${errMsg(e)}\n`);
89 }
90 if (draining) clearTimeout(draining);
91 if (o.handleSignals) {
92 process.off("SIGTERM", sigterm);
93 process.off("SIGINT", sigint);
94 }
95 return completion;
96}