Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(server): restore background capacity after urgent jobs

Afloat8ff26067d30b
diff --git a/server/host/warm-summary-host.test.ts b/server/host/warm-summary-host.test.ts
index 9209243160..c4ab7d1ec8 100644
--- a/server/host/warm-summary-host.test.ts
+++ b/server/host/warm-summary-host.test.ts
@@ -119,6 +119,44 @@ function setup(overrides: Partial<WarmSummaryHostDeps> = {}, options: { tokens?:
}
describe("warm summaries for a Claude conversation", () => {
+ it("does not give an awaited summary's extra capacity to a queued background job", async () => {
+ const slots = createJobSlots(1);
+ let finishBackground!: () => void;
+ const background = slots.run(() => new Promise<void>((resolve) => { finishBackground = resolve; }));
+ let finishSummary!: (result: IdleCompactResult) => void;
+ const summary = new Promise<IdleCompactResult>((resolve) => { finishSummary = resolve; });
+ let summaryStarted!: () => void;
+ const started = new Promise<void>((resolve) => { summaryStarted = resolve; });
+ const f = setup({ slots, instance: () => ({ adapter: { cacheKeepWarm: {
+ hold: () => true, compact: () => { summaryStarted(); return summary; },
+ } } }) }, { tokens: 170_000 });
+ const next = vi.fn(async () => undefined);
+ let queued: Promise<void> | undefined;
+ let turn: Promise<string> | undefined;
+ try {
+ f.finishTurn();
+ await f.advance(WARM_SUMMARY_SOON_MS);
+ queued = slots.run(next);
+ turn = f.host.beforeTurn("t1");
+ await started;
+ expect(slots.running).toBe(2);
+ finishSummary({ ok: true, session: "fork-session", sessionBefore: "live-session", input: 4, cacheRead: 150_000, cacheWrite: 40, output: 2_400, costUsd: 0.2, model: "claude-opus-5-5" });
+ expect(await turn).toBe("waited");
+ expect(f.task.resumeCursors.claude).toBe("fork-session");
+ expect(next).not.toHaveBeenCalled();
+ expect(slots.running).toBe(1);
+ expect(slots.waiting).toBe(1);
+ finishBackground();
+ await Promise.all([background, queued]);
+ expect(next).toHaveBeenCalledOnce();
+ } finally {
+ finishBackground();
+ finishSummary({ ok: false, reason: "test finished" });
+ f.host.dispose();
+ await Promise.all([background, queued, turn]);
+ }
+ });
+
it("skips a summary that waited for a free slot until its cache went cold, and writes one that waited less", async () => {
let finish!: () => void;
const slots = createJobSlots(1);
diff --git a/server/job-slots-urgent.test.ts b/server/job-slots-urgent.test.ts
new file mode 100644
index 0000000000..f9d358415e
--- /dev/null
+++ b/server/job-slots-urgent.test.ts
@@ -0,0 +1,68 @@
+import { describe, expect, it } from "vitest";
+
+import { createJobSlots } from "./job-slots.ts";
+
+function gate() {
+ let finish!: () => void;
+ const done = new Promise<void>((resolve) => { finish = resolve; });
+ return { done, finish };
+}
+
+describe("background capacity after urgent work", () => {
+ it.each([0, 1])("keeps ordinary jobs queued when running work still fills the limit after job %i ends", async (firstToFinish) => {
+ const slots = createJobSlots(1);
+ const gates = [gate(), gate(), gate()];
+ const started: number[] = [];
+ const runs = gates.map((hold, index) => slots.run(async () => {
+ started.push(index);
+ await hold.done;
+ }, index === 1 ? { urgent: () => true } : {}));
+ try {
+ await Promise.resolve();
+ expect(started).toEqual([0, 1]);
+ expect(slots.running).toBe(2);
+ gates[firstToFinish]!.finish();
+ await runs[firstToFinish];
+ expect(started).toEqual([0, 1]);
+ expect(slots.running).toBe(1);
+ expect(slots.waiting).toBe(1);
+ gates[1 - firstToFinish]!.finish();
+ await runs[1 - firstToFinish];
+ expect(started).toEqual([0, 1, 2]);
+ expect(slots.running).toBe(1);
+ } finally {
+ for (const hold of gates) hold.finish();
+ await Promise.all(runs);
+ }
+ expect(slots.running).toBe(0);
+ expect(slots.waiting).toBe(0);
+ });
+
+ it("drains several urgent jobs before handing a slot back to ordinary work", async () => {
+ const slots = createJobSlots(1);
+ const gates = [gate(), gate(), gate(), gate()];
+ const started: number[] = [];
+ const runs = gates.map((hold, index) => slots.run(async () => {
+ started.push(index);
+ await hold.done;
+ }, index === 1 || index === 2 ? { urgent: () => true } : {}));
+ try {
+ await Promise.resolve();
+ expect(started).toEqual([0, 1, 2]);
+ for (const index of [1, 2]) {
+ gates[index]!.finish();
+ await runs[index];
+ expect(started).toEqual([0, 1, 2]);
+ expect(slots.waiting).toBe(1);
+ }
+ gates[0]!.finish();
+ await runs[0];
+ expect(started).toEqual([0, 1, 2, 3]);
+ expect(slots.running).toBe(1);
+ } finally {
+ for (const hold of gates) hold.finish();
+ await Promise.all(runs);
+ }
+ expect(slots.running).toBe(0);
+ });
+});
diff --git a/server/job-slots.ts b/server/job-slots.ts
index 55f3b80332..45b751bb01 100644
--- a/server/job-slots.ts
+++ b/server/job-slots.ts
@@ -21,11 +21,12 @@ export interface JobSlots {
export function createJobSlots(limit: number): JobSlots {
let running = 0;
const waiting: Array<{ start: () => void; interactive: boolean }> = [];
- // A finished job hands its slot to the first waiter, so a caller arriving in between cannot take it.
+ // An urgent job's extra capacity must not pass to an ordinary waiter after that job ends.
const release = () => {
+ running -= 1;
+ if (running >= limit) return;
const next = waiting.shift();
- if (next) next.start();
- else running -= 1;
+ if (next) { running += 1; next.start(); }
};
const slot = ({ signal, urgent, interactive = false, onWait }: SlotOptions): Promise<void> => {
signal?.throwIfAborted();