A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
fix queued messages stranded after agent end (#444)
kte98b5dc2e330
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.mdindex fd70e17f5b..939eb03ae4 100644--- a/packages/coding-agent/CHANGELOG.md+++ b/packages/coding-agent/CHANGELOG.md@@ -7,6 +7,7 @@- Changed shell commands to use discoverable agent, schedule, package, model, session, update, doctor, and full-shutdown verbs without exposing the background daemon hierarchy ([ENG-4538](https://linear.app/primeintellect/issue/ENG-4538/standardize-bash-command-conventions-and-improve-command-discovery)).- Fixed unsupported Node versions crashing before startup by requiring Node 22.8.0 or newer and showing upgrade guidance before loading the CLI ([ENG-4260](https://linear.app/primeintellect/issue/ENG-4260/incorrect-node-version-breaks-first-launch)).- Added `@` file-path autocomplete to new-agent and reply prompts in the Agents View.+- Fixed queued messages getting stranded when an agent turn ended ([ENG-4653](https://linear.app/primeintellect/issue/ENG-4653/queued-messages-can-get-stuck-with-heartbeats)).- Changed `/traces upload-all` to pace requests within the platform rate limit, honor bounded `Retry-After` responses, and support interruption.- Fixed resuming a daemon-resident session to attach the requesting client to its existing worker without disturbing other clients ([ENG-4656](https://linear.app/primeintellect/issue/ENG-4656/resuming-prime-agent-sessions-should-attach)).- Fixed daemon-owned updates terminating their updater before the daemon restart and session restore completed ([ENG-4606](https://linear.app/primeintellect/issue/ENG-4606/benign-error-on-prime-agent-update)).diff --git a/packages/coding-agent/src/core/agent-session.ts b/packages/coding-agent/src/core/agent-session.tsindex 0961cb2c98..c5c990338e 100644--- a/packages/coding-agent/src/core/agent-session.ts+++ b/packages/coding-agent/src/core/agent-session.ts@@ -454,6 +454,7 @@ interface InternalPromptOptions extends PromptOptions {skipPrePromptWork?: boolean;returnAfterAccepted?: boolean;agentMessageId?: string;+ resumeIfIdle?: boolean;}type QueuedAgentMessage = UserMessage | CustomMessage;@@ -791,6 +792,9 @@ export class AgentSession {private _unsubscribeAgent?: () => void;private _eventListeners: AgentSessionEventListener[] = [];private _agentEventQueue: Promise<void> = Promise.resolve();+ private _pendingMessageResumeQueue: Promise<void> = Promise.resolve();+ private _pendingMessageResumeEpoch = 0;+ private _pendingMessageResumeRequested = false;/** Tracks pending steering messages for UI display. Removed when delivered. */private _steeringMessages: QueuedSteeringMessage[] = [];@@ -1091,6 +1095,9 @@ export class AgentSession {}private _emitQueueUpdate(): void {+ if (this.pendingMessageCount === 0) {+ this._pendingMessageResumeRequested = false;+ }this._emit({type: "queue_update",steering: this._steeringMessages.map(queuedAgentMessagePreview),@@ -2379,6 +2386,7 @@ export class AgentSession {this._retryResolve();this._retryResolve = undefined;this._retryPromise = undefined;+ this._schedulePendingMessageResume();}}@@ -2921,6 +2929,7 @@ export class AgentSession {await this._promptInjectedMessage(job.prompt, message, {...options,followUpQueueKey: options?.followUpQueueKey ?? `heartbeat:${job.id}`,+ resumeIfIdle: true,});}@@ -2967,6 +2976,7 @@ export class AgentSession {queueKey: options.followUpQueueKey,previewLabel,suppressAutonomousContinuation: options.suppressAutonomousContinuation,+ resumeIfIdle: options.resumeIfIdle,},);if (!queued) {@@ -3061,6 +3071,7 @@ export class AgentSession {queueKey: options.followUpQueueKey,previewLabel,suppressAutonomousContinuation: options.suppressAutonomousContinuation,+ resumeIfIdle: options.resumeIfIdle,},);if (!queued) {@@ -3201,6 +3212,7 @@ export class AgentSession {agentMessageId: options.agentMessageId,suppressAutonomousContinuation: options.suppressAutonomousContinuation,customMessage: options.customMessage,+ resumeIfIdle: options.resumeIfIdle,},);if (!queued) {@@ -3394,6 +3406,7 @@ export class AgentSession {agentMessageId: options.agentMessageId,suppressAutonomousContinuation: options.suppressAutonomousContinuation,customMessage: options.customMessage,+ resumeIfIdle: options.resumeIfIdle,},);if (!queued) {@@ -3460,6 +3473,7 @@ export class AgentSession {) {this._acceptedAgentMessagePrompt = undefined;}+ this._schedulePendingMessageResume();}).catch(() => undefined);if (options?.returnAfterAccepted) {@@ -3549,7 +3563,7 @@ export class AgentSession {async steer(text: string,images?: ImageContent[],- options: { queueKey?: string; agentMessageId?: string } = {},+ options: { queueKey?: string; agentMessageId?: string; resumeIfIdle?: boolean } = {},): Promise<void> {// Check for extension commands (cannot be queued)if (text.startsWith("/")) {@@ -3563,6 +3577,7 @@ export class AgentSession {await this._queueSteer(expandedText, images, {queueKey: options.queueKey,agentMessageId: options.agentMessageId,+ resumeIfIdle: options.resumeIfIdle,});}@@ -3576,7 +3591,7 @@ export class AgentSession {async followUp(text: string,images?: ImageContent[],- options: { queueKey?: string; agentMessageId?: string } = {},+ options: { queueKey?: string; agentMessageId?: string; resumeIfIdle?: boolean } = {},): Promise<boolean> {// Check for extension commands (cannot be queued)if (text.startsWith("/")) {@@ -3590,6 +3605,7 @@ export class AgentSession {return this._queueFollowUp(expandedText, images, {queueKey: options.queueKey,agentMessageId: options.agentMessageId,+ resumeIfIdle: options.resumeIfIdle,});}@@ -3651,6 +3667,7 @@ export class AgentSession {agentMessageId?: string;customMessage?: CustomMessage;suppressAutonomousContinuation?: boolean;+ resumeIfIdle?: boolean;} = {},): Promise<boolean> {const pendingNextTurnMessages = this._pendingNextTurnMessages;@@ -3662,6 +3679,7 @@ export class AgentSession {agentMessageId: options.agentMessageId,message: options.customMessage,prefixMessages: pendingNextTurnMessages,+ resumeIfIdle: options.resumeIfIdle,});if (!queued) {this._pendingNextTurnMessages.unshift(...pendingNextTurnMessages);@@ -3674,6 +3692,7 @@ export class AgentSession {message: options.customMessage,prefixMessages: pendingNextTurnMessages,suppressAutonomousContinuation: options.suppressAutonomousContinuation,+ resumeIfIdle: options.resumeIfIdle,});return true;} catch (error) {@@ -3686,7 +3705,12 @@ export class AgentSession {text: string,message: CustomMessage,streamingBehavior: "steer" | "followUp",- options: { queueKey?: string; previewLabel?: string; suppressAutonomousContinuation?: boolean } = {},+ options: {+ queueKey?: string;+ previewLabel?: string;+ suppressAutonomousContinuation?: boolean;+ resumeIfIdle?: boolean;+ } = {},): Promise<boolean> {const pendingNextTurnMessages = this._pendingNextTurnMessages;this._pendingNextTurnMessages = [];@@ -3698,6 +3722,7 @@ export class AgentSession {prefixMessages: pendingNextTurnMessages,previewLabel: options.previewLabel,suppressAutonomousContinuation: options.suppressAutonomousContinuation,+ resumeIfIdle: options.resumeIfIdle,});if (!queued) {this._pendingNextTurnMessages.unshift(...pendingNextTurnMessages);@@ -3710,6 +3735,7 @@ export class AgentSession {previewLabel: options.previewLabel,queueKey: options.queueKey,suppressAutonomousContinuation: options.suppressAutonomousContinuation,+ resumeIfIdle: options.resumeIfIdle,});return true;} catch (error) {@@ -3732,6 +3758,7 @@ export class AgentSession {prefixMessages?: CustomMessage[];previewLabel?: string;suppressAutonomousContinuation?: boolean;+ resumeIfIdle?: boolean;} = {},): Promise<void> {const content = options.content ?? this._buildPromptContent(text, images);@@ -3755,6 +3782,9 @@ export class AgentSession {});this.agent.steer(options.prefixMessages?.length ? [...options.prefixMessages, message] : message);this._emitQueueUpdate();+ if (options.resumeIfIdle) {+ this._schedulePendingMessageResume(true);+ }}/**@@ -3771,6 +3801,7 @@ export class AgentSession {prefixMessages?: CustomMessage[];previewLabel?: string;suppressAutonomousContinuation?: boolean;+ resumeIfIdle?: boolean;} = {},): Promise<boolean> {if (options.queueKey && this._followUpMessages.some((message) => message.queueKey === options.queueKey)) {@@ -3797,9 +3828,94 @@ export class AgentSession {});this.agent.followUp(options.prefixMessages?.length ? [...options.prefixMessages, message] : message);this._emitQueueUpdate();+ if (options.resumeIfIdle) {+ this._schedulePendingMessageResume(true);+ }return true;}+ private _schedulePendingMessageResume(request = false): void {+ if (request) {+ this._pendingMessageResumeRequested = true;+ }+ if (this._disposed || this._disposing || this.pendingMessageCount === 0) {+ if (this.pendingMessageCount === 0) {+ this._pendingMessageResumeRequested = false;+ }+ return;+ }+ if (!this._pendingMessageResumeRequested) {+ return;+ }+ const epoch = this._pendingMessageResumeEpoch;+ const resume = () => this._resumePendingMessages(epoch);+ this._pendingMessageResumeQueue = this._pendingMessageResumeQueue.then(resume, resume);+ this._pendingMessageResumeQueue.catch(() => {});+ }++ private async _resumePendingMessages(epoch: number): Promise<void> {+ try {+ while (+ !this._disposed &&+ !this._disposing &&+ this._pendingMessageResumeRequested &&+ epoch === this._pendingMessageResumeEpoch &&+ this.pendingMessageCount > 0+ ) {+ await this.agent.waitForIdle();+ await this._agentEventQueue;+ await this._waitForRefineIdle();+ if (epoch !== this._pendingMessageResumeEpoch) {+ return;+ }++ const blockingOperations = [this._compactionOperation, this._branchSummaryOperation].filter(+ (operation): operation is Promise<void> => operation !== undefined,+ );+ if (blockingOperations.length > 0) {+ await Promise.allSettled(blockingOperations);+ continue;+ }+ if (this.isRetrying) {+ await this.waitForRetry();+ continue;+ }+ const acceptedPrompts = [...this._acceptedPromptCompletions];+ if (acceptedPrompts.length > 0) {+ await Promise.allSettled(acceptedPrompts);+ continue;+ }++ if (+ this._disposed ||+ this._disposing ||+ epoch !== this._pendingMessageResumeEpoch ||+ this.pendingMessageCount === 0+ ) {+ return;+ }+ if (this.isBashRunning || this.isCompacting || this.isRetrying || this.hasAcceptedPromptInFlight) {+ return;+ }++ await this.agent.waitForIdle();+ if (epoch !== this._pendingMessageResumeEpoch || this.pendingMessageCount === 0) {+ return;+ }+ this._flushPendingBashMessages();+ if (this._pendingNextTurnMessages.length > 0) {+ await this._drainQueuedMessagesAfterBash();+ } else {+ await this.agent.continue();+ }+ }+ } finally {+ if (epoch === this._pendingMessageResumeEpoch && this.pendingMessageCount === 0) {+ this._pendingMessageResumeRequested = false;+ }+ }+ }+/*** Throw an error if the text is an extension command.*/@@ -3895,11 +4011,12 @@ export class AgentSession {}// Use prompt() with expandPromptTemplates: false to skip command handling and template expansion- await this.prompt(text, {+ await this._prompt(text, {expandPromptTemplates: false,streamingBehavior: options?.deliverAs,images,source: "extension",+ resumeIfIdle: true,});}@@ -4070,6 +4187,8 @@ export class AgentSession {this.abortCompaction();this.abortBranchSummary();this.abortBash();+ this._pendingMessageResumeRequested = false;+ this._pendingMessageResumeEpoch++;this.agent.abort();}@@ -4096,6 +4215,8 @@ export class AgentSession {abortForUpdateRestart(): void {this.abortRetry();+ this._pendingMessageResumeRequested = false;+ this._pendingMessageResumeEpoch++;this._cancelActiveRlmChildRuns("Parent session aborted for update restart");this._goalAbortInProgress = this._goalState.status === "active";this.agent.abort();@@ -4588,6 +4709,7 @@ export class AgentSession {this._compactionOperation = undefined;}resolveCompactionOperation();+ this._schedulePendingMessageResume();if (didCompact) {this._discardPendingAutoRefine({ cancelPostCompactionContinue: true });if (hadPostCompactionContinue) {@@ -5031,6 +5153,7 @@ export class AgentSession {if (this._refineInFlight === settled) {this._refineInFlight = undefined;}+ this._schedulePendingMessageResume();}}@@ -5429,6 +5552,7 @@ export class AgentSession {return false;} finally {this._autoCompactionAbortController = undefined;+ this._schedulePendingMessageResume();}}@@ -7298,7 +7422,10 @@ export class AgentSession {) {return;}+ await this._promptPendingMessagesWithNextTurnContext();+ }+ private async _promptPendingMessagesWithNextTurnContext(): Promise<void> {const steeringMessages = [...this._steeringMessages];const followUpMessages = [...this._followUpMessages];const drainedSteeringMessages = steeringMessages.length > 0 ? steeringMessages : [];@@ -7700,6 +7827,7 @@ export class AgentSession {this._branchSummaryOperation = undefined;}resolveBranchSummaryOperation();+ this._schedulePendingMessageResume();}}diff --git a/packages/coding-agent/src/modes/agent-connection/in-process-agent-connection.ts b/packages/coding-agent/src/modes/agent-connection/in-process-agent-connection.tsindex 78ca04fe85..596ac593bf 100644--- a/packages/coding-agent/src/modes/agent-connection/in-process-agent-connection.ts+++ b/packages/coding-agent/src/modes/agent-connection/in-process-agent-connection.ts@@ -262,11 +262,11 @@ export class InProcessAgentConnection implements AgentConnection {}async steer(message: string, images?: ImageContent[]): Promise<void> {- await this.session.steer(message, images);+ await this.session.steer(message, images, { resumeIfIdle: true });}async followUp(message: string, images?: ImageContent[]): Promise<void> {- await this.session.followUp(message, images);+ await this.session.followUp(message, images, { resumeIfIdle: true });}async abort(): Promise<void> {diff --git a/packages/coding-agent/src/modes/daemon/daemon-mode.ts b/packages/coding-agent/src/modes/daemon/daemon-mode.tsindex 2c2969fbbc..45abcdfe83 100644--- a/packages/coding-agent/src/modes/daemon/daemon-mode.ts+++ b/packages/coding-agent/src/modes/daemon/daemon-mode.ts@@ -1198,7 +1198,7 @@ export class AgentDaemon {if (!this.isCronJobRunnableForState(runnableJob, state, requirePersistedJob)) {return "skipped";}- await session.followUp(runnableJob.prompt);+ await session.followUp(runnableJob.prompt, undefined, { resumeIfIdle: true });return;}const getRunnableJob = (): AgentCronJob | undefined => {@@ -2809,6 +2809,7 @@ export class AgentDaemon {await state.runtime.session.steer(command.message, command.images, {queueKey: command.queueKey,agentMessageId: command.agentMessageId,+ resumeIfIdle: true,});}this.recordWorkerRecoveryState(state, "steer_queued", true);@@ -2830,6 +2831,7 @@ export class AgentDaemon {queued = await state.runtime.session.followUp(command.message, command.images, {queueKey: command.queueKey,agentMessageId: command.agentMessageId,+ resumeIfIdle: true,});}if (queued) {diff --git a/packages/coding-agent/src/modes/rpc/rpc-mode.ts b/packages/coding-agent/src/modes/rpc/rpc-mode.tsindex ff7e93a336..2e64d14262 100644--- a/packages/coding-agent/src/modes/rpc/rpc-mode.ts+++ b/packages/coding-agent/src/modes/rpc/rpc-mode.ts@@ -401,12 +401,12 @@ export async function runRpcMode(runtimeHost: AgentSessionRuntime): Promise<neve}case "steer": {- await session.steer(command.message, command.images);+ await session.steer(command.message, command.images, { resumeIfIdle: true });return success(id, "steer");}case "follow_up": {- await session.followUp(command.message, command.images);+ await session.followUp(command.message, command.images, { resumeIfIdle: true });return success(id, "follow_up");}diff --git a/packages/coding-agent/test/daemon-mode.test.ts b/packages/coding-agent/test/daemon-mode.test.tsindex 8c75ce51b4..2507e82fb5 100644--- a/packages/coding-agent/test/daemon-mode.test.ts+++ b/packages/coding-agent/test/daemon-mode.test.ts@@ -4236,7 +4236,7 @@ describe("daemon mode helpers", () => {await internals.runCronJob(makeCronJob({ id: "cron-1", source: "cron", activeSessionId: state.activeSessionId }));- expect(followUp).toHaveBeenCalledWith("heartbeat prompt");+ expect(followUp).toHaveBeenCalledWith("heartbeat prompt", undefined, { resumeIfIdle: true });expect(prompt).not.toHaveBeenCalled();});@@ -4313,7 +4313,7 @@ describe("daemon mode helpers", () => {makeCronJob({ id: "cron-1", source: "cron", activeSessionId: state.activeSessionId }),);- expect(followUp).toHaveBeenCalledWith("heartbeat prompt");+ expect(followUp).toHaveBeenCalledWith("heartbeat prompt", undefined, { resumeIfIdle: true });expect(prompt).not.toHaveBeenCalled();});@@ -4343,7 +4343,7 @@ describe("daemon mode helpers", () => {makeCronJob({ id: "cron-1", source: "cron", activeSessionId: state.activeSessionId }),);- expect(followUp).toHaveBeenCalledWith("heartbeat prompt");+ expect(followUp).toHaveBeenCalledWith("heartbeat prompt", undefined, { resumeIfIdle: true });expect(prompt).not.toHaveBeenCalled();});@@ -4609,6 +4609,7 @@ describe("daemon mode helpers", () => {expect(steer).toHaveBeenCalledWith("queued heartbeat", undefined, {queueKey: "heartbeat:job-1",agentMessageId: "agentmsg_steer",+ resumeIfIdle: true,});expect(restoreSteeringMessage).not.toHaveBeenCalled();});diff --git a/packages/coding-agent/test/suite/regressions/4653-queued-steer-after-agent-end.test.ts b/packages/coding-agent/test/suite/regressions/4653-queued-steer-after-agent-end.test.tsnew file mode 100644index 0000000000..47d4d6c656--- /dev/null+++ b/packages/coding-agent/test/suite/regressions/4653-queued-steer-after-agent-end.test.ts@@ -0,0 +1,73 @@+import { fauxAssistantMessage } from "@earendil-works/pi-ai";+import { afterEach, describe, expect, it, vi } from "vitest";+import { createHarness, getUserTexts, type Harness } from "../harness.js";++describe("ENG-4653 queued messages after agent end", () => {+ const harnesses: Harness[] = [];++ afterEach(() => {+ while (harnesses.length > 0) {+ harnesses.pop()?.cleanup();+ }+ });++ async function waitForDelivery(harness: Harness, expectedCalls: number): Promise<void> {+ await vi.waitFor(() => expect(harness.faux.state.callCount).toBe(expectedCalls));+ await harness.session.agent.waitForIdle();+ await vi.waitFor(() => expect(harness.session.pendingMessageCount).toBe(0));+ }++ it("starts a new turn for steering queued from agent_end", async () => {+ const harness = await createHarness();+ harnesses.push(harness);+ harness.setResponses([fauxAssistantMessage("first turn complete"), fauxAssistantMessage("steering handled")]);++ let queued = false;+ const unsubscribe = harness.session.agent.subscribe(async (event) => {+ if (event.type !== "agent_end" || queued) return;+ queued = true;+ await harness.session.steer("stop heartbeat", undefined, { resumeIfIdle: true });+ });++ await harness.session.prompt("start");+ await waitForDelivery(harness, 2);+ unsubscribe();++ expect(getUserTexts(harness)).toEqual(["start", "stop heartbeat"]);+ expect(harness.eventsOfType("agent_start")).toHaveLength(2);+ expect(harness.eventsOfType("agent_end")).toHaveLength(2);+ });++ it("starts a turn for an explicit steering message accepted while idle", async () => {+ const harness = await createHarness();+ harnesses.push(harness);+ harness.setResponses([fauxAssistantMessage("idle steering handled")]);++ await harness.session.steer("recover stale routing", undefined, { resumeIfIdle: true });+ await waitForDelivery(harness, 1);++ expect(getUserTexts(harness)).toEqual(["recover stale routing"]);+ expect(harness.eventsOfType("agent_start")).toHaveLength(1);+ });++ it("starts a new turn for a follow-up queued from agent_end", async () => {+ const harness = await createHarness();+ harnesses.push(harness);+ harness.setResponses([fauxAssistantMessage("first turn complete"), fauxAssistantMessage("follow-up handled")]);++ let queued = false;+ const unsubscribe = harness.session.agent.subscribe(async (event) => {+ if (event.type !== "agent_end" || queued) return;+ queued = true;+ await harness.session.followUp("continue after end", undefined, { resumeIfIdle: true });+ });++ await harness.session.prompt("start");+ await waitForDelivery(harness, 2);+ unsubscribe();++ expect(getUserTexts(harness)).toEqual(["start", "continue after end"]);+ expect(harness.eventsOfType("agent_start")).toHaveLength(2);+ expect(harness.eventsOfType("agent_end")).toHaveLength(2);+ });+});