A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
fix(coding-agent): attribute subagent terminal notices (#619)
Seth Kartenc2ec4d249b11
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.mdindex 3718bf73c6..e08c0b55a6 100644--- a/packages/coding-agent/CHANGELOG.md+++ b/packages/coding-agent/CHANGELOG.md@@ -7,6 +7,7 @@- Documented the global `idleEvictionMinutes` daemon setting, including its default, valid values, and eviction/passivation behavior ([#621](https://github.com/PrimeIntellect-ai/prime-agent/issues/621)).- Fixed top-level `--help` omitting `acp` from the supported `--mode` values ([#620](https://github.com/PrimeIntellect-ai/prime-agent/issues/620)).- Fixed `stop` and `rename` becoming prompts when `--daemon-socket` precedes the command ([#622](https://github.com/PrimeIntellect-ai/prime-agent/issues/622)).+- Fixed subagent terminal notices arriving as anonymous follow-up prompts instead of attributed agent messages, so a parent can now tell which child reported completion, failure, or cancellation, and a busy parent is steered at the next turn boundary rather than waiting to go idle ([#617](https://github.com/PrimeIntellect-ai/prime-agent/issues/617)).## [0.6.0] - 2026-08-04diff --git a/packages/coding-agent/src/core/agent-session.ts b/packages/coding-agent/src/core/agent-session.tsindex fedef724ff..bd49673813 100644--- a/packages/coding-agent/src/core/agent-session.ts+++ b/packages/coding-agent/src/core/agent-session.ts@@ -9690,7 +9690,22 @@ export class AgentSession {onSessionPublished: publishChildSession,};- const injectTerminalMessage = async (message: CustomMessage): Promise<void> => {+ const deliverTerminalMessageToParent = async (message: CustomMessage): Promise<void> => {+ const childController = childSession?._agentMessageController;+ if (childController) {+ try {+ // deliveryMode is deliberately omitted: the "auto" default steers into+ // a busy parent, whereas "follow_up" parks a child's last signal in the+ // when-run-idle lane, which drains only once the steer lane empties.+ await childController.sendAgentMessage({+ target: this.sessionId,+ message: message.content as string,+ });+ return;+ } catch {+ // An unattributed notice beats silence, so fall through to the injected path.+ }+ }await this._promptInjectedMessage(message.content as string, message, {streamingBehavior: "followUp",queueIfBusy: true,@@ -9803,7 +9818,7 @@ export class AgentSession {emitChildUpdate();if (!run.detachedDeletion && child._parentReplyCount === parentReplyCountBeforeRun) {const lastAssistantText = child.getLastAssistantText();- await injectTerminalMessage(+ await deliverTerminalMessageToParent(createRlmChildTerminalNoticeMessage({kind: "completed_without_reply",childId: run.id,@@ -9833,7 +9848,7 @@ export class AgentSession {emitChildUpdate();if (!run.detachedDeletion) {if (run.status === "error") {- await injectTerminalMessage(+ await deliverTerminalMessageToParent(createRlmChildFailureMessage({childId: run.id,sessionName,@@ -9841,7 +9856,7 @@ export class AgentSession {}),);} else if (run.status === "cancelled") {- await injectTerminalMessage(+ await deliverTerminalMessageToParent(createRlmChildTerminalNoticeMessage({kind: "cancelled",childId: run.id,diff --git a/packages/coding-agent/test/suite/harness.ts b/packages/coding-agent/test/suite/harness.tsindex 5e3ec41007..97bda0d282 100644--- a/packages/coding-agent/test/suite/harness.ts+++ b/packages/coding-agent/test/suite/harness.ts@@ -9,6 +9,7 @@ import type { AgentMessage, AgentTool } from "@earendil-works/pi-agent-core";import { Agent } from "@earendil-works/pi-agent-core";import type { FauxModelDefinition, FauxProviderRegistration, FauxResponseStep, Model } from "@earendil-works/pi-ai";import { registerFauxProvider } from "@earendil-works/pi-ai";+import type { AgentSessionMessageController } from "../../src/core/agent-messages.js";import type { AgentObserveController } from "../../src/core/agent-observe.js";import { AgentSession, type AgentSessionEvent, type AutoRefineReviewer } from "../../src/core/agent-session.js";import { AuthStorage } from "../../src/core/auth-storage.js";@@ -16,6 +17,7 @@ import type { AgentAutonomousConfig } from "../../src/core/autonomous.js";import type { ExtensionRunner } from "../../src/core/extensions/index.js";import { convertToLlm } from "../../src/core/messages.js";import { ModelRegistry } from "../../src/core/model-registry.js";+import type { SubagentRuntimeHost } from "../../src/core/rlm-runtime.js";import { SessionManager } from "../../src/core/session-manager.js";import type { Settings } from "../../src/core/settings-manager.js";import { SettingsManager } from "../../src/core/settings-manager.js";@@ -68,8 +70,11 @@ export interface HarnessOptions {extensionFactories?: Array<ExtensionFactory | CreateTestExtensionsResultInput>;withConfiguredAuth?: boolean;agentObserveController?: AgentObserveController;+ agentMessageController?: AgentSessionMessageController;+ subagentRuntimeHost?: SubagentRuntimeHost;persistSession?: boolean;rlmDepth?: number;+ rlmMaxDepth?: number;autonomous?: AgentAutonomousConfig;autoRefineReviewer?: AutoRefineReviewer;serializedRefine?: boolean;@@ -188,9 +193,12 @@ export async function createHarness(options: HarnessOptions = {}): Promise<HarnemodelRegistry,resourceLoader,agentObserveController: options.agentObserveController,+ agentMessageController: options.agentMessageController,+ subagentRuntimeHost: options.subagentRuntimeHost,baseToolsOverride: toolMap,extensionRunnerRef,rlmDepth: options.rlmDepth,+ rlmMaxDepth: options.rlmMaxDepth,autonomous: options.autonomous,autoRefineReviewer: options.autoRefineReviewer,serializedRefine: options.serializedRefine,diff --git a/packages/coding-agent/test/suite/regressions/617-subagent-terminal-agent-message.test.ts b/packages/coding-agent/test/suite/regressions/617-subagent-terminal-agent-message.test.tsnew file mode 100644index 0000000000..0a52367249--- /dev/null+++ b/packages/coding-agent/test/suite/regressions/617-subagent-terminal-agent-message.test.ts@@ -0,0 +1,179 @@+import { fauxAssistantMessage } from "@earendil-works/pi-ai";+import { afterEach, describe, expect, it } from "vitest";+import {+ AGENT_MESSAGE_CUSTOM_TYPE,+ type AgentSessionMessage,+ createAgentSessionMessage,+ resolveAgentSessionMessageStreamingBehavior,+} from "../../../src/core/agent-messages.js";+import { createHarness, type Harness } from "../harness.js";++function terminalMessage(messages: readonly unknown[]): AgentSessionMessage | undefined {+ return messages.find(+ (message): message is AgentSessionMessage =>+ typeof message === "object" &&+ message !== null &&+ "role" in message &&+ "customType" in message &&+ (message as { role?: unknown }).role === "custom" &&+ (message as { customType?: unknown }).customType === AGENT_MESSAGE_CUSTOM_TYPE,+ );+}++describe("#617 subagent terminal agent messages", () => {+ let parent: Harness | undefined;+ let child: Harness | undefined;++ afterEach(() => {+ child?.cleanup();+ parent?.cleanup();+ child = undefined;+ parent = undefined;+ });++ it("delivers a child completion without a reply as an attributed agent message", async () => {+ const childSessionName = "terminal-worker";+ child = await createHarness({+ agentMessageController: {+ listAgents: () => ({ agents: [] }),+ sendAgentMessage: async (input) => {+ expect(input).toMatchObject({+ target: parent!.session.sessionId,+ message: expect.stringContaining("completed without sending a reply"),+ });+ // Omitted, so the daemon applies the "auto" default.+ expect(input.deliveryMode).toBeUndefined();+ const message = createAgentSessionMessage({+ id: "agentmsg-terminal-completion",+ source: "agent_message",+ message: input.message,+ from: {+ activeSessionId: "child-active",+ sessionId: child!.session.sessionId,+ sessionName: childSessionName,+ },+ fromRelationship: "child",+ target: { activeSessionId: "parent-active", sessionId: parent!.session.sessionId },+ deliveryMode: input.deliveryMode ?? "auto",+ });+ await parent!.session.acceptAgentMessagePrompt(message.content, { customMessage: message });+ return {+ id: message.details.id,+ source: "agent_message",+ target: { activeSessionId: "parent-active", sessionId: parent!.session.sessionId },+ message: input.message,+ deliveryStatus: "delivered",+ deliveryMode: input.deliveryMode ?? "auto",+ };+ },+ },+ });+ parent = await createHarness({+ rlmDepth: 0,+ rlmMaxDepth: 1,+ subagentRuntimeHost: {+ createRlmSubagentRuntime: async () => ({ session: child!.session }),+ deleteRlmSubagentRuntime: async () => {},+ },+ });+ child.setResponses([fauxAssistantMessage("child completed")]);++ const spawned = await parent.session.runRlmChild("finish without replying", { name: childSessionName });++ await expect+ .poll(() => terminalMessage(parent!.session.messages))+ .toMatchObject({+ customType: AGENT_MESSAGE_CUSTOM_TYPE,+ details: {+ id: "agentmsg-terminal-completion",+ fromRelationship: "child",+ from: { sessionId: child.session.sessionId, sessionName: childSessionName },+ },+ content: expect.stringContaining(`[from child:${childSessionName}]`),+ });+ expect(parent.session.messages).not.toContainEqual(+ expect.objectContaining({ customType: "rlm_child_terminal_notice" }),+ );+ expect(terminalMessage(parent.session.messages)?.content).toContain(spawned.rlm_child_id);+ });+ it("steers a terminal notice into a busy parent instead of deferring it", async () => {+ // A streaming parent must resolve to a steer, not a deferred follow-up.+ const childSessionName = "busy-parent-worker";+ let resolvedBehavior: "steer" | "followUp" | undefined;+ child = await createHarness({+ agentMessageController: {+ listAgents: () => ({ agents: [] }),+ sendAgentMessage: async (input) => {+ expect(input.deliveryMode).toBeUndefined();+ resolvedBehavior = resolveAgentSessionMessageStreamingBehavior(true, input.deliveryMode);+ const message = createAgentSessionMessage({+ id: "agentmsg-terminal-busy",+ source: "agent_message",+ message: input.message,+ from: {+ activeSessionId: "child-active",+ sessionId: child!.session.sessionId,+ sessionName: childSessionName,+ },+ fromRelationship: "child",+ target: { activeSessionId: "parent-active", sessionId: parent!.session.sessionId },+ deliveryMode: input.deliveryMode ?? "auto",+ });+ return {+ id: message.details.id,+ source: "agent_message",+ target: { activeSessionId: "parent-active", sessionId: parent!.session.sessionId },+ message: input.message,+ deliveryStatus: "delivered",+ deliveryMode: input.deliveryMode ?? "auto",+ };+ },+ },+ });+ parent = await createHarness({+ rlmDepth: 0,+ rlmMaxDepth: 1,+ subagentRuntimeHost: {+ createRlmSubagentRuntime: async () => ({ session: child!.session }),+ deleteRlmSubagentRuntime: async () => {},+ },+ });+ child.setResponses([fauxAssistantMessage("child completed")]);++ await parent.session.runRlmChild("finish without replying", { name: childSessionName });++ await expect.poll(() => resolvedBehavior).toBe("steer");+ });++ it("falls back to an injected notice when the agent message cannot be delivered", async () => {+ // A controller that always rejects: the parent must still learn the child+ // finished. Losing the notice entirely is worse than losing attribution.+ child = await createHarness({+ agentMessageController: {+ listAgents: () => ({ agents: [] }),+ sendAgentMessage: async () => {+ throw new Error("delivery unavailable");+ },+ },+ });+ parent = await createHarness({+ subagentRuntimeHost: {+ createRlmSubagentRuntime: async () => ({ session: child!.session }),+ deleteRlmSubagentRuntime: async () => {},+ } as never,+ });+ child.setResponses([fauxAssistantMessage("done, no reply to parent")]);+ parent.setResponses([fauxAssistantMessage("parent ack")]);++ await parent.session.runRlmChild("do the work");+ await new Promise((resolve) => setTimeout(resolve, 200));++ // The notice arrives some way: either the agent message or the injected+ // fallback, but never silently dropped.+ const sawNotice = parent.session.messages.some((message) => {+ const content = (message as { content?: unknown }).content;+ return typeof content === "string" && content.includes("completed without sending a reply");+ });+ expect(sawNotice || terminalMessage(parent.session.messages) !== undefined).toBe(true);+ }, 60_000);+});