A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
fix(coding-agent): park waitForIdle while the session-input pump is blocked (#2151)
Sebastian Müller8eaa00c37385
diff --git a/packages/coding-agent/.changes/res-1332-waitforidle-spin.md b/packages/coding-agent/.changes/res-1332-waitforidle-spin.mdnew file mode 100644index 0000000000..3046fbd963--- /dev/null+++ b/packages/coding-agent/.changes/res-1332-waitforidle-spin.md@@ -0,0 +1 @@+- Fixed a session worker wedging at 100% CPU: waiting for a session to go idle while queued input was blocked by a running bash command, compaction, or retry spun in microtasks without ever yielding to IO, freezing every session in the worker and starving daemon IPC.diff --git a/packages/coding-agent/src/core/agent-session.ts b/packages/coding-agent/src/core/agent-session.tsindex b9b06fa239..39f4bf3ce4 100644--- a/packages/coding-agent/src/core/agent-session.ts+++ b/packages/coding-agent/src/core/agent-session.ts@@ -6575,6 +6575,12 @@ export class AgentSession {}}+ /**+ * Busy inputs: compaction, retry, bash, plus (for "pump") disposal,+ * suspension, queued-work pauses, and branch-summary mutation. Waiters+ * parked on this predicate rely on every clear site notifying the+ * session-input checkpoint waiters.+ */private _isBusyForSessionInput(point: "preflight" | "pump"): boolean {const externalBusy = this.isCompacting || this.isRetrying || this.isBashRunning;if (point === "pump") {@@ -7566,7 +7572,10 @@ export class AgentSession {private async _waitForIdleOrSettlement(settlement?: PostCompactionContinuationSettlement): Promise<void> {while (settlement === undefined || this._postCompactionContinuationSettlement === settlement) {if (this._actionStore.queuedActions().length > 0) {- if (this._sessionInputPumpSuspended || this._queuedWorkPauses.size > 0) {+ // Park while the pump would refuse scheduling or selection: rescheduling+ // a blocked pump completes on already-resolved promises, so looping here+ // would spin on the microtask queue and starve the IO that ends the busy state.+ if (this._isBusyForSessionInput("pump")) {let wake = () => {};const changed = new Promise<void>((resolve) => {wake = resolve;diff --git a/packages/coding-agent/test/suite/agent-session-queue.test.ts b/packages/coding-agent/test/suite/agent-session-queue.test.tsindex c993bd6582..afe121da90 100644--- a/packages/coding-agent/test/suite/agent-session-queue.test.ts+++ b/packages/coding-agent/test/suite/agent-session-queue.test.ts@@ -29,6 +29,7 @@ import {saveHarnessState,} from "../../src/core/refinement/index.js";import { parseSessionSlashCommand } from "../../src/core/slash-commands.js";+import type { BashOperations } from "../../src/core/tools/bash.js";import {conversationMessages,createHarness,@@ -3631,4 +3632,50 @@ describe("AgentSession scheduler scenarios", () => {}}});++ it("waitForIdle parks instead of microtask-spinning while a running bash blocks queued input", async () => {+ const harness = await createHarness();+ harnesses.push(harness);+ const session = harness.session;+ let releaseBash!: () => void;+ const gate = new Promise<{ exitCode: number | null }>((resolve) => {+ releaseBash = () => resolve({ exitCode: 0 });+ });+ const operations: BashOperations = { exec: async () => await gate };+ const bashPromise = session.executeBash("blocked", undefined, { operations });+ await new Promise((resolve) => setTimeout(resolve, 0));+ expect(session.isBashRunning).toBe(true);++ harness.setResponses([fauxAssistantMessage("first done"), fauxAssistantMessage("second done")]);+ const firstPrompt = session.prompt("queued while bash runs");+ await new Promise((resolve) => setTimeout(resolve, 0));++ // Count pump scheduling from the idle wait. Unfixed, the wait loop respun the+ // blocked pump purely in microtasks, so the setImmediate below never fired and+ // only the 200th reschedule (the escape hatch) released the gate.+ const internals = session as unknown as { _scheduleSessionInputPump(): void };+ const originalSchedule = internals._scheduleSessionInputPump.bind(session);+ let scheduleCount = 0;+ let secondPrompt: Promise<void> | undefined;+ internals._scheduleSessionInputPump = () => {+ scheduleCount++;+ if (scheduleCount === 200) releaseBash();+ originalSchedule();+ };++ const idle = session.waitForIdle();+ setImmediate(() => {+ // An arrival during the park must not be lost once the busy state clears.+ secondPrompt = session.prompt("queued during park");+ secondPrompt.catch(() => undefined);+ releaseBash();+ });+ await idle;+ await bashPromise;+ await firstPrompt;+ await secondPrompt;++ expect(scheduleCount).toBeLessThan(200);+ expect(getAssistantTexts(harness)).toEqual(["first done", "second done"]);+ });});