Skip to content

Linger is An Open-Source Personal Agent That Runs Fully on Your PC. We aim to break Big Tech’s grip on AI by bringing powerful local AI to everyday computers through efficient model–harness co-design, so everyone can run their own.

fix(node-services): remember the limit an endpoint came down from

LeoXing7faa4fcebd26
diff --git a/apps/node-services/src/agent/runner-gateway/adaptive-limit.ts b/apps/node-services/src/agent/runner-gateway/adaptive-limit.ts
index d0ee6a64..b7fab109 100644
--- a/apps/node-services/src/agent/runner-gateway/adaptive-limit.ts
+++ b/apps/node-services/src/agent/runner-gateway/adaptive-limit.ts
@@ -52,6 +52,10 @@ const RAISE_AT_MOST = 1.3;
const LOWER_ABOVE = 1.8;
/** No raise for this long after the endpoint refused work. */
const OVERLOAD_COOLDOWN_MS = 60_000;
+/** First wait before a limit found too slow or refused is tried again; each
+ * failed retry doubles it, up to the most. */
+const RETRY_CEILING_MS = 5 * 60_000;
+const RETRY_CEILING_MAX_MS = 30 * 60_000;
/** Below this, fixed per-request overhead dominates the latency. */
const MIN_WORK_TOKENS = 1_000;
@@ -66,10 +70,18 @@ interface Latencies {
* fast as with one unit running; it shrinks by one when they are clearly
* slower and halves when the endpoint refuses work. Running units are never
* interrupted, so a lower limit only holds back new ones.
+ *
+ * A limit it had to come down from is the ceiling: it is not tried again
+ * until the retry time, since one unit running always looks fast and would
+ * otherwise send the limit straight back up. A retry that holds clears the
+ * ceiling; one that fails waits twice as long for the next.
*/
export class AdaptiveLimit {
#limit: number;
#cooldownUntil = 0;
+ #ceiling: number | undefined;
+ #retryAt = 0;
+ #retryAfterMs = RETRY_CEILING_MS;
readonly #latencies = new Map<string, Latencies>();
constructor(readonly range: ConcurrencyRange) {
@@ -80,6 +92,11 @@ export class AdaptiveLimit {
return this.#limit;
}
+ /** The lowest limit found too slow or refused, until a retry holds. */
+ get ceiling(): number | undefined {
+ return this.#ceiling;
+ }
+
observe(
signal: ModelSignal,
endpoint: {
@@ -91,7 +108,11 @@ export class AdaptiveLimit {
if (signal.type === "overload") {
this.#cooldownUntil = endpoint.now + OVERLOAD_COOLDOWN_MS;
return {
- change: this.#set(Math.max(1, Math.floor(this.#limit / 2)), "overload"),
+ change: this.#lower(
+ Math.max(1, Math.floor(this.#limit / 2)),
+ "overload",
+ endpoint.now,
+ ),
};
}
const perThousand =
@@ -124,24 +145,49 @@ export class AdaptiveLimit {
if (ratio > LOWER_ABOVE)
return {
latency,
- change: this.#set(Math.max(1, this.#limit - 1), "slower", ratio),
+ change: this.#lower(
+ Math.max(1, this.#limit - 1),
+ "slower",
+ endpoint.now,
+ ratio,
+ ),
};
+ if (this.#ceiling !== undefined && this.#limit >= this.#ceiling) {
+ this.#ceiling = undefined;
+ this.#retryAfterMs = RETRY_CEILING_MS;
+ }
+ const next = Math.min(this.range.max, this.#limit + 1);
if (
ratio <= RAISE_AT_MOST &&
endpoint.saturated &&
- endpoint.now >= this.#cooldownUntil
+ endpoint.now >= this.#cooldownUntil &&
+ (this.#ceiling === undefined ||
+ next < this.#ceiling ||
+ endpoint.now >= this.#retryAt)
)
- return {
- latency,
- change: this.#set(
- Math.min(this.range.max, this.#limit + 1),
- "faster",
- ratio,
- ),
- };
+ return { latency, change: this.#set(next, "faster", ratio) };
return { latency };
}
+ #lower(
+ to: number,
+ reason: LimitChange["reason"],
+ now: number,
+ ratio?: number,
+ ): LimitChange | undefined {
+ const from = this.#limit;
+ const change = this.#set(to, reason, ratio);
+ if (!change) return undefined;
+ if (this.#ceiling !== undefined && from >= this.#ceiling)
+ this.#retryAfterMs = Math.min(
+ this.#retryAfterMs * 2,
+ RETRY_CEILING_MAX_MS,
+ );
+ this.#ceiling = from;
+ this.#retryAt = now + this.#retryAfterMs;
+ return change;
+ }
+
#set(
to: number,
reason: LimitChange["reason"],
diff --git a/apps/node-services/src/agent/runner-gateway/model-queue.ts b/apps/node-services/src/agent/runner-gateway/model-queue.ts
index 32bf4c99..a74e2fed 100644
--- a/apps/node-services/src/agent/runner-gateway/model-queue.ts
+++ b/apps/node-services/src/agent/runner-gateway/model-queue.ts
@@ -128,6 +128,7 @@ export class ModelQueue {
active,
waiting: state?.waiting.length ?? 0,
limit: limit.value,
+ ...(limit.ceiling === undefined ? {} : { ceiling: limit.ceiling }),
});
if (!change) return;
logAgentEvent("info", "model_queue.limit_changed", {
@@ -136,6 +137,7 @@ export class ModelQueue {
from: change.from,
to: change.to,
reason: change.reason,
+ ...(limit.ceiling === undefined ? {} : { ceiling: limit.ceiling }),
...(change.ratio === undefined
? {}
: { ratio: Math.round(change.ratio * 100) / 100 }),
diff --git a/apps/node-services/tests/agent/runner-gateway/model-queue.test.ts b/apps/node-services/tests/agent/runner-gateway/model-queue.test.ts
index f7e21112..b698bbc1 100644
--- a/apps/node-services/tests/agent/runner-gateway/model-queue.test.ts
+++ b/apps/node-services/tests/agent/runner-gateway/model-queue.test.ts
@@ -241,6 +241,52 @@ describe("ModelQueue limit", () => {
expect(d.value).toBeDefined();
});
+ it("keeps a limit it came down from as the ceiling and retries it later, waiting longer after each failed retry", async () => {
+ let now = 0;
+ const minutes = (count: number) => (now += count * 60_000);
+ const queue = new ModelQueue(
+ () => ({ initial: 1, max: 4 }),
+ () => now,
+ );
+ const a = await queue.acquire(codex, run("a"), signal);
+ for (let i = 0; i < 3; i++) queue.observe(codex, latency(500));
+ const b = track(queue.acquire(codex, run("b"), signal));
+ queue.observe(codex, latency(500));
+ await flush();
+ expect(b.value).toBeDefined();
+ for (let i = 0; i < 4; i++) queue.observe(codex, latency(1_500));
+ a.release(done);
+
+ // One unit running looks fast, but two were just too slow.
+ const c = track(queue.acquire(codex, run("c"), signal));
+ for (let i = 0; i < 4; i++) queue.observe(codex, latency(500));
+ await flush();
+ expect(c.value).toBeUndefined();
+ minutes(5);
+ queue.observe(codex, latency(500));
+ await flush();
+ expect(c.value).toBeDefined();
+
+ // The retry is slow too: the next one waits ten minutes.
+ for (let i = 0; i < 4; i++) queue.observe(codex, latency(1_500));
+ b.value!.release(done);
+ const d = track(queue.acquire(codex, run("d"), signal));
+ minutes(5);
+ for (let i = 0; i < 4; i++) queue.observe(codex, latency(500));
+ await flush();
+ expect(d.value).toBeUndefined();
+ minutes(5);
+ queue.observe(codex, latency(500));
+ await flush();
+ expect(d.value).toBeDefined();
+
+ // This retry holds: the ceiling is gone and the limit grows past it.
+ const e = track(queue.acquire(codex, run("e"), signal));
+ for (let i = 0; i < 4; i++) queue.observe(codex, latency(500));
+ await flush();
+ expect(e.value).toBeDefined();
+ });
+
it("keeps a learned limit while the endpoint is idle and starts over when its link changes", async () => {
let range = { initial: 1, max: 4 };
const queue = new ModelQueue(() => range);
diff --git a/docs/model-scheduling.md b/docs/model-scheduling.md
index 19decf5e..d282eacb 100644
--- a/docs/model-scheduling.md
+++ b/docs/model-scheduling.md
@@ -16,8 +16,11 @@ waits and requests come back about as fast as with one unit running, the
queue tries one more. When requests get clearly slower it gives one back, and
when the endpoint refuses work (rate limited, overloaded, or a call runs out
of time) it halves and holds off raising for a minute. Speed is compared per
-thousand new prompt tokens, so a long prompt does not look like a busy endpoint. What it
-learns lasts until the service restarts or the runtime's models change.
+thousand new prompt tokens, so a long prompt does not look like a busy
+endpoint. A number it had to come down from is not tried again for five
+minutes; if that retry is too slow as well, the wait doubles, up to half an
+hour. What it learns lasts until the service restarts or the runtime's models
+change.
Background work never takes the last place once there is more than one, so a
chat does not wait behind a wiki refresh.