From 20afa773543ee88469e22786c36db87debe1bbf7 Mon Sep 17 00:00:00 2001 From: Samarth Rajesh Lad <142334685+samyyy2311@users.noreply.github.com> Date: Mon, 30 Nov 2026 12:55:06 +0530 Subject: [PATCH] jobs: keep the CPU window baseline when sampling drifts past 5 minutes --- server/jobs/supervisor.test.ts | 34 +++++++++++++++++++++++++++++++++- server/jobs/supervisor.ts | 2 +- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/server/jobs/supervisor.test.ts b/server/jobs/supervisor.test.ts index 2027227217..3b971f8bbc 100644 --- a/server/jobs/supervisor.test.ts +++ b/server/jobs/supervisor.test.ts @@ -4,7 +4,7 @@ import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; -import { CRASH_STREAK, JOB_TOKEN_PREFIX, JobSupervisor, restartDelay, type JobNote, type JobRecord, type JobSpec, type SupervisorOptions } from "./supervisor.ts"; +import { CRASH_STREAK, JOB_TOKEN_PREFIX, JobSupervisor, restartDelay, SAMPLE_MS, type JobNote, type JobRecord, type JobSpec, type SupervisorOptions } from "./supervisor.ts"; const roots: string[] = []; const supervisors: JobSupervisor[] = []; @@ -168,3 +168,35 @@ describe.skipIf(process.platform === "win32")("background job supervisor", () => expect(supervisor.list()).toEqual([]); }); }); + +describe("background job CPU limit", () => { + it("stops a run that stays over the CPU share for 5 minutes even when sample ticks drift", async () => { + let clock = 1_000_000, cpuMs = 0; + const killed: Array<[number, NodeJS.Signals]> = []; + const fakeProcess = { pid: 4242, stdout: null, stderr: null, once: () => {} }; + const { supervisor } = harness({ + now: () => clock, + launch: () => ({ process: fakeProcess, secrets: [] }), + kill: (pid, signal) => { killed.push([pid, signal]); }, + usage: async (pids) => new Map(pids.map((pid) => [pid, { rssBytes: 10, cpuMs }])), + }); + const { job } = supervisor.start(spec("busy")); + + for (let i = 0; i <= 10; i += 1) { + await supervisor.sample(); + clock += SAMPLE_MS + 5; + if (i < 4) cpuMs += SAMPLE_MS + 5; + } + expect(killed).toEqual([]); + + for (let i = 0; i <= 10; i += 1) { + await supervisor.sample(); + if (killed.length) break; + clock += SAMPLE_MS + 5; + cpuMs += SAMPLE_MS + 5; + } + expect(killed).toEqual([[4242, "SIGKILL"]]); + expect(supervisor.get(job.id, "bot-a")?.lastExit).toBe("cpu"); + expect(supervisor.logs(job.id, "bot-a")).toContain("stopped this run: cpu"); + }); +}); diff --git a/server/jobs/supervisor.ts b/server/jobs/supervisor.ts index b08e612935..d0167adfa0 100644 --- a/server/jobs/supervisor.ts +++ b/server/jobs/supervisor.ts @@ -288,7 +288,7 @@ export class JobSupervisor { continue; } live.cpu.push([at, used.cpuMs]); - while (live.cpu.length > 1 && at - live.cpu[0]![0] > CPU_WINDOW_MS) live.cpu.shift(); + while (live.cpu.length > 1 && at - live.cpu[1]![0] >= CPU_WINDOW_MS) live.cpu.shift(); const [firstAt, firstCpu] = live.cpu[0]!; if (at - firstAt >= CPU_WINDOW_MS && (used.cpuMs - firstCpu) / (at - firstAt) > this.limits.cpuShare) { this.endRun(job, live, "cpu: kept a whole CPU busy for 5 minutes");