Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ export class Engine implements DecisionEngine {
state: JsonValue,
seed: number,
): Promise<DecisionResult> {
if (this.waiting >= this.settings.maxQueue) {
if (this.waiting >= this.settings.maxInflight + this.settings.maxQueue) {
throw new OverloadedError("LocalJev is at capacity. Retry shortly.");
}
this.waiting += 1;
Expand Down
39 changes: 39 additions & 0 deletions test/engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, test } from "bun:test";
import { loadSettings } from "../src/config";
import {
Engine,
OverloadedError,
buildSystemPrompt,
confidence,
decodeAnswers,
Expand Down Expand Up @@ -142,3 +143,41 @@ test("engine retries malformed output and sends upstream authentication", async
expect(secondBody.messages.at(-1).role).toBe("user");
expect(secondBody.response_format.type).toBe("json_schema");
});

test("allows maxQueue waiting decisions in addition to maxInflight calls", async () => {
let unblockFirst!: () => void;
let signalFirstStarted!: () => void;
const firstBlocked = new Promise<void>((resolve) => {
unblockFirst = resolve;
});
const firstStarted = new Promise<void>((resolve) => {
signalFirstStarted = resolve;
});
let calls = 0;
const engine = new Engine(
loadSettings({ maxInflight: 1, maxQueue: 1, malformedRetries: 0 }),
async () => {
calls += 1;
if (calls === 1) {
signalFirstStarted();
await firstBlocked;
}
return Response.json({
choices: [{ message: { content: JSON.stringify({ answers: { q1: 0.5 } }) } }],
});
},
);
const oneQuestion: Record<string, Question> = {
urgent: { type: "noul", instructions: "Is it urgent?", criteria: null },
};

const active = engine.decide(oneQuestion, "message", 1);
await firstStarted;
const queued = engine.decide(oneQuestion, "message", 2);
const overCapacity = engine.decide(oneQuestion, "message", 3);

await expect(overCapacity).rejects.toBeInstanceOf(OverloadedError);
unblockFirst();
await expect(Promise.all([active, queued])).resolves.toHaveLength(2);
expect(calls).toBe(2);
});