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): judge a model endpoint's limit on settled, comparable samples
LeoXingd32473902523
diff --git a/apps/node-services/src/agent/runner-gateway/adaptive-limit.ts b/apps/node-services/src/agent/runner-gateway/adaptive-limit.tsindex 0e27eaee..d0ee6a64 100644--- a/apps/node-services/src/agent/runner-gateway/adaptive-limit.ts+++ b/apps/node-services/src/agent/runner-gateway/adaptive-limit.ts@@ -20,6 +20,20 @@ export type ModelSignal =}| { readonly type: "overload" };+/** One latency sample beside what it is compared with, in ms per thousand+ * tokens; a median is missing until enough samples arrived. */+export interface LatencySample {+ readonly source: "run" | "call";+ readonly perThousand: number;+ readonly baseline?: number;+ readonly window?: number;+}++export interface Observation {+ readonly change?: LimitChange;+ readonly latency?: LatencySample;+}+export interface LimitChange {readonly from: number;readonly to: number;@@ -73,10 +87,12 @@ export class AdaptiveLimit {readonly saturated: boolean;readonly now: number;},- ): LimitChange | undefined {+ ): Observation {if (signal.type === "overload") {this.#cooldownUntil = endpoint.now + OVERLOAD_COOLDOWN_MS;- return this.#set(Math.max(1, Math.floor(this.#limit / 2)), "overload");+ return {+ change: this.#set(Math.max(1, Math.floor(this.#limit / 2)), "overload"),+ };}const perThousand =signal.latencyMs / (Math.max(signal.workTokens, MIN_WORK_TOKENS) / 1000);@@ -87,26 +103,43 @@ export class AdaptiveLimit {}if (endpoint.active <= 1)keepLast(latencies.baseline, perThousand, BASELINE_SAMPLES);- keepLast(latencies.window, perThousand, WINDOW);- if (- latencies.baseline.length < BASELINE_MIN_SAMPLES ||- latencies.window.length < WINDOW- )- return undefined;- const ratio = median(latencies.window) / median(latencies.baseline);+ // After a lower limit the units already running drain first; until+ // then requests still show the old limit's load.+ if (endpoint.active <= this.#limit)+ keepLast(latencies.window, perThousand, WINDOW);+ const baseline =+ latencies.baseline.length >= BASELINE_MIN_SAMPLES+ ? median(latencies.baseline)+ : undefined;+ const window =+ latencies.window.length >= WINDOW ? median(latencies.window) : undefined;+ const latency = {+ source: signal.source,+ perThousand,+ ...(baseline === undefined ? {} : { baseline }),+ ...(window === undefined ? {} : { window }),+ };+ if (baseline === undefined || window === undefined) return { latency };+ const ratio = window / baseline;if (ratio > LOWER_ABOVE)- return this.#set(Math.max(1, this.#limit - 1), "slower", ratio);+ return {+ latency,+ change: this.#set(Math.max(1, this.#limit - 1), "slower", ratio),+ };if (ratio <= RAISE_AT_MOST &&endpoint.saturated &&endpoint.now >= this.#cooldownUntil)- return this.#set(- Math.min(this.range.max, this.#limit + 1),- "faster",- ratio,- );- return undefined;+ return {+ latency,+ change: this.#set(+ Math.min(this.range.max, this.#limit + 1),+ "faster",+ ratio,+ ),+ };+ return { latency };}#set(diff --git a/apps/node-services/src/agent/runner-gateway/model-queue.ts b/apps/node-services/src/agent/runner-gateway/model-queue.tsindex 595f422a..32bf4c99 100644--- a/apps/node-services/src/agent/runner-gateway/model-queue.ts+++ b/apps/node-services/src/agent/runner-gateway/model-queue.ts@@ -106,11 +106,29 @@ export class ModelQueue {const key = endpointKey(endpoint);const limit = this.#limit(key, endpoint);const state = this.#endpoints.get(key);- const change = limit.observe(signal, {- active: state?.active ?? 0,- saturated: (state?.waiting.length ?? 0) > 0,+ const active = state?.active ?? 0;+ const saturated = (state?.waiting.length ?? 0) > 0;+ const { change, latency } = limit.observe(signal, {+ active,+ saturated,now: this.now(),});+ if (latency)+ logAgentEvent("debug", "model_queue.latency_sampled", {+ computer_id: endpoint.computerId,+ runtime_type: endpoint.runtimeType,+ source: latency.source,+ ms_per_thousand: Math.round(latency.perThousand),+ ...(latency.baseline === undefined+ ? {}+ : { baseline_ms_per_thousand: Math.round(latency.baseline) }),+ ...(latency.window === undefined+ ? {}+ : { window_ms_per_thousand: Math.round(latency.window) }),+ active,+ waiting: state?.waiting.length ?? 0,+ limit: limit.value,+ });if (!change) return;logAgentEvent("info", "model_queue.limit_changed", {computer_id: endpoint.computerId,diff --git a/apps/node-services/src/agent/runner-gateway/runner-operations.ts b/apps/node-services/src/agent/runner-gateway/runner-operations.tsindex 6f7b2936..e2becde4 100644--- a/apps/node-services/src/agent/runner-gateway/runner-operations.ts+++ b/apps/node-services/src/agent/runner-gateway/runner-operations.ts@@ -634,7 +634,9 @@ export class RunnerOperations {}/** Time to the first item covers queueing and prompt processing, which is- * where a busy endpoint shows; later items depend on what the model wrote. */+ * where a busy endpoint shows; later items depend on what the model wrote.+ * Work counts only fresh prompt tokens: a hosted model writes its prompt+ * cache at no visible cost, and local servers report no cache writes. */#observeModelResponse(operation: RunnerOperationState,response: ModelResponded,@@ -644,7 +646,7 @@ export class RunnerOperations {type: "latency",source: "run",latencyMs: response.first_item_at_unix_ms - response.started_at_unix_ms,- workTokens: response.usage.input + response.usage.cache_write,+ workTokens: response.usage.input,});}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.tsindex b495f739..f7e21112 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@@ -220,6 +220,27 @@ describe("ModelQueue limit", () => {expect(refused.value).toBeDefined();});+ it("judges a lower limit only once the units above it have finished", async () => {+ const queue = new ModelQueue(() => ({ initial: 1, max: 4 }));+ 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));+ 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).toBeDefined();++ // Three at once are slow: two. Three still run, so their slow requests+ // say nothing about two.+ for (let i = 0; i < 8; i++) queue.observe(codex, latency(1_500));+ a.release(done);+ b.value!.release(done);+ const d = track(queue.acquire(codex, run("d"), signal));+ await flush();+ expect(d.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.mdindex c0dc2c17..19decf5e 100644--- a/docs/model-scheduling.md+++ b/docs/model-scheduling.md@@ -16,7 +16,7 @@ waits and requests come back about as fast as with one unit running, thequeue tries one more. When requests get clearly slower it gives one back, andwhen the endpoint refuses work (rate limited, overloaded, or a call runs outof time) it halves and holds off raising for a minute. Speed is compared per-thousand tokens, so a long prompt does not look like a busy endpoint. What it+thousand new prompt tokens, so a long prompt does not look like a busy endpoint. What itlearns 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