Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

fix(coding-agent): reap deleted subagent runtimes on compaction (#547)

Seth Kartenedf608534780
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.md
index ceb8223e4d..f4f210e7a6 100644
--- a/packages/coding-agent/CHANGELOG.md
+++ b/packages/coding-agent/CHANGELOG.md
@@ -3,6 +3,7 @@
## [Unreleased]
- Changed top-level CLI help and documentation to expose autonomous mode, quality gates, and their limits.
+- Fixed compaction retaining runtime resources after an explicitly deleted subagent had a transient cleanup failure.
- Fixed long-running thinking timers to display hours and days instead of unbounded minutes.
## [0.4.0] - 2026-08-01
diff --git a/packages/coding-agent/src/core/agent-session.ts b/packages/coding-agent/src/core/agent-session.ts
index dc7bde0a9a..6c1ff7e91b 100644
--- a/packages/coding-agent/src/core/agent-session.ts
+++ b/packages/coding-agent/src/core/agent-session.ts
@@ -6555,10 +6555,16 @@ export class AgentSession {
});
}
await this._notifyKernelStateAfterCompaction();
+ await this._reapDeletedRlmSubagentRuntimesAfterCompaction();
return { summary, firstKeptEntryId, tokensBefore, details };
}
+ private async _reapDeletedRlmSubagentRuntimesAfterCompaction(): Promise<void> {
+ const childIds = [...this._retryableRlmSubagentDeletions.keys()];
+ await Promise.allSettled(childIds.map((childId) => this.deleteRlmSubagent(childId)));
+ }
+
/**
* Cancel in-progress compaction (manual or auto).
*/
diff --git a/packages/coding-agent/test/agent-session-recursion.test.ts b/packages/coding-agent/test/agent-session-recursion.test.ts
index bccc80edcc..bc3da17c57 100644
--- a/packages/coding-agent/test/agent-session-recursion.test.ts
+++ b/packages/coding-agent/test/agent-session-recursion.test.ts
@@ -114,6 +114,7 @@ interface InspectableRlmSession {
_retainedRlmChildSessions: Map<string, AgentSession>;
_retainedRlmChildUnsubscribes: Map<string, () => void>;
_createKernelHostHandlers(): HostRequestHandlers;
+ _reapDeletedRlmSubagentRuntimesAfterCompaction(): Promise<void>;
}
interface KernelPumpTestApi {
@@ -1206,7 +1207,7 @@ describe("AgentSession rlm recursion", () => {
expect(deleteRuntime).toHaveBeenCalledTimes(1);
expect(disposeHostedChild).not.toHaveBeenCalled();
- await expect(root.deleteRlmSubagent("starting-worker")).resolves.toEqual({ subagent: starting });
+ await internals._reapDeletedRlmSubagentRuntimesAfterCompaction();
expect(deleteRuntime).toHaveBeenCalledTimes(2);
expect(disposeHostedChild).toHaveBeenCalledOnce();
expect(internals._retryableRlmSubagentDeletions.size).toBe(0);
diff --git a/packages/coding-agent/test/suite/agent-session-compaction.test.ts b/packages/coding-agent/test/suite/agent-session-compaction.test.ts
index 7b06cee50f..c159629dad 100644
--- a/packages/coding-agent/test/suite/agent-session-compaction.test.ts
+++ b/packages/coding-agent/test/suite/agent-session-compaction.test.ts
@@ -102,6 +102,38 @@ describe("AgentSession compaction characterization", () => {
expect(harness.session.messages[0]?.role).toBe("compactionSummary");
});
+ it("retries cleanup for explicitly deleted subagents after compaction", async () => {
+ const harness = await createHarness({
+ settings: { compaction: { keepRecentTokens: 1 } },
+ extensionFactories: [
+ (pi) => {
+ pi.on("session_before_compact", async (event) => ({
+ compaction: {
+ summary: "summary from extension",
+ firstKeptEntryId: event.preparation.firstKeptEntryId,
+ tokensBefore: event.preparation.tokensBefore,
+ details: {},
+ },
+ }));
+ },
+ ],
+ });
+ harnesses.push(harness);
+ await harness.session.prompt("one");
+ await harness.session.prompt("two");
+
+ const retryableDeletions = (
+ harness.session as unknown as { _retryableRlmSubagentDeletions: Map<string, unknown> }
+ )._retryableRlmSubagentDeletions;
+ retryableDeletions.set("deleted-child", {});
+ const retryDeletion = vi
+ .spyOn(harness.session, "deleteRlmSubagent")
+ .mockRejectedValue(new Error("cleanup still unavailable"));
+
+ await expect(harness.session.compact()).resolves.toMatchObject({ summary: "summary from extension" });
+ expect(retryDeletion).toHaveBeenCalledWith("deleted-child");
+ });
+
it("compacts through the model summarizer, persists metadata, emits events, and remains usable", async () => {
const harness = await createHarness({
settings: { compaction: { keepRecentTokens: 1 } },