Skip to content

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

Rust

[RSI, performance] perf(coding-agent): skip identical roster broadcasts and per-delta child-update serialization (#2481)

Sebastian Müllerc258a07f143a
diff --git a/packages/coding-agent/.changes/roster-delta-broadcast.md b/packages/coding-agent/.changes/roster-delta-broadcast.md
new file mode 100644
index 0000000000..1356efb7b7
--- /dev/null
+++ b/packages/coding-agent/.changes/roster-delta-broadcast.md
@@ -0,0 +1 @@
+- Cut redundant broadcast work under streaming and fleet load: the supervisor now compares roster rows against their last published serialization so repair pulls, snapshot applies, and staleness sweeps no longer re-broadcast identical entries to every subscriber; child update dedup compares fields instead of building and serializing a full snapshot per streamed delta (the prompt label is computed once per run); and queue updates serialize the snapshot once instead of twice per mutation.
diff --git a/packages/coding-agent/src/core/agent-session.ts b/packages/coding-agent/src/core/agent-session.ts
index d071bd4fc9..187bb087bc 100644
--- a/packages/coding-agent/src/core/agent-session.ts
+++ b/packages/coding-agent/src/core/agent-session.ts
@@ -406,6 +406,9 @@ export interface RlmChildAgentSnapshot {
error?: string;
}
+/** The observable state rlm_child_update events dedup on. */
+type RlmChildStableSnapshot = Omit<RlmChildAgentSnapshot, "lastActivityAt" | "activityStaleMs">;
+
export type CompactionReason = "manual" | "threshold" | "overflow" | "requested";
export type AgentSessionEvent =
@@ -1032,6 +1035,8 @@ type AutonomousRuntimeSnapshot = Pick<
interface RlmChildRun {
id: string;
prompt: string;
+ /** rlmChildLabel(prompt), computed once: the prompt never changes. */
+ label: string;
sessionName: string;
sessionDir: string;
model: Model<Api>;
@@ -1081,7 +1086,7 @@ interface RlmChildRun {
completeDeletion?: () => Promise<void>;
reportDeletionCleanupFailure?: (error: unknown) => Promise<void>;
emitUpdate?: () => void;
- lastEmittedUpdate?: string;
+ lastEmittedUpdate?: RlmChildStableSnapshot;
/** Monotonic time of the last streamed-delta emit; other event kinds still emit at once. */
lastStreamedUpdateMonotonicAt?: number;
unsubscribe?: () => void;
@@ -1337,6 +1342,39 @@ export function rlmChildLabel(prompt: string): string {
return prompt.replace(/\s+/g, " ").trim() || "child agent";
}
+/** `satisfies` makes a field without a compare entry a compile error, not a silently suppressed rlm_child_update. */
+const RLM_CHILD_STABLE_SNAPSHOT_KEYS = Object.keys({
+ id: true,
+ parentId: true,
+ activeSessionId: true,
+ sessionName: true,
+ model: true,
+ label: true,
+ status: true,
+ durationMs: true,
+ answerPreview: true,
+ toolUseCount: true,
+ tokenCount: true,
+ recap: true,
+ sessionDir: true,
+ activity: true,
+ repliedSinceTask: true,
+ progressNote: true,
+ error: true,
+} satisfies Record<keyof RlmChildStableSnapshot, true>) as (keyof RlmChildStableSnapshot)[];
+
+/** Fields are primitives except activity, compared by value because each delta assigns a fresh activity object. */
+function rlmChildStableFieldsEqual(a: RlmChildStableSnapshot, b: RlmChildStableSnapshot): boolean {
+ for (const key of RLM_CHILD_STABLE_SNAPSHOT_KEYS) {
+ if (key === "activity") {
+ if (a.activity?.kind !== b.activity?.kind || a.activity?.toolName !== b.activity?.toolName) return false;
+ } else if (a[key] !== b[key]) {
+ return false;
+ }
+ }
+ return true;
+}
+
/**
* Record a tracked child activity on both clocks: lastActivityAt stays
* wall-clock ms for snapshots, and its monotonic twin bounds staleness so a
@@ -1474,11 +1512,8 @@ export class AgentSession {
private _unsubscribeAgent?: () => void;
private _eventListeners: AgentSessionEventListener[] = [];
- private _lastSessionActionSnapshot: SessionActionSnapshot = {
- queuedCount: 0,
- steering: [],
- followUps: [],
- };
+ /** Serialized last-emitted queue snapshot, so a mutation pays one stringify, not two. */
+ private _lastSessionActionSnapshot = JSON.stringify({ queuedCount: 0, steering: [], followUps: [] });
private _agentEventQueue: Promise<void> = Promise.resolve();
/** Session-owned actions. Items are never fed into Agent.steer/followUp. */
@@ -2037,8 +2072,9 @@ export class AgentSession {
private _emitQueueUpdate(): void {
const actions = this.getSessionActionSnapshot();
- if (JSON.stringify(actions) === JSON.stringify(this._lastSessionActionSnapshot)) return;
- this._lastSessionActionSnapshot = actions;
+ const serialized = JSON.stringify(actions);
+ if (serialized === this._lastSessionActionSnapshot) return;
+ this._lastSessionActionSnapshot = serialized;
this._emit({ type: "session_action_update", actions });
}
@@ -11948,17 +11984,14 @@ export class AgentSession {
};
}
- private _rlmChildSnapshotForRun(
- run: RlmChildRun,
- child = run.session ?? this._rlmChildSessions.get(run.id)?.session,
- ): RlmChildAgentSnapshot {
+ private _rlmChildStableSnapshotForRun(run: RlmChildRun, child: AgentSession | undefined): RlmChildStableSnapshot {
const model = child?.model ?? run.model;
return {
id: run.id,
parentId: this._rlmParentNodeId,
sessionName: child?.sessionName ?? run.sessionName,
model: `${model.provider}/${model.id}`,
- label: rlmChildLabel(run.prompt),
+ label: run.label,
status: run.status,
durationMs: run.durationMs,
answerPreview: run.answerPreview,
@@ -11969,9 +12002,18 @@ export class AgentSession {
activity: run.activity,
repliedSinceTask: child?._repliedToParentSinceTask,
progressNote: run.progressNotes.at(-1),
+ error: run.error,
+ };
+ }
+
+ private _rlmChildSnapshotForRun(
+ run: RlmChildRun,
+ child = run.session ?? this._rlmChildSessions.get(run.id)?.session,
+ ): RlmChildAgentSnapshot {
+ return {
+ ...this._rlmChildStableSnapshotForRun(run, child),
lastActivityAt: run.lastActivityAt,
activityStaleMs: rlmActivityStaleMs(run.status, run.activity, run.lastActivityAt, run.lastActivityMonotonicAt),
- error: run.error,
};
}
@@ -12450,6 +12492,7 @@ export class AgentSession {
const run: RlmChildRun = {
id: childNodeId,
prompt,
+ label: rlmChildLabel(prompt),
sessionName,
sessionDir: childSessionDir,
model: modelSelection.model,
@@ -12472,17 +12515,18 @@ export class AgentSession {
this._activeRlmChildRuns.set(run.id, run);
this._unsettledRlmChildRuns.add(run);
const emitChildUpdate = () => {
- const child = this._rlmChildSnapshotForRun(run);
// Dedup compares observable child state, not clock-derived fields:
// lastActivityAt advances on every streamed token delta and
// activityStaleMs is recomputed on each snapshot build, so including
// either would re-emit on every delta once answerPreview saturates its
- // cap. Emitted snapshots still carry both fields fresh.
- const { lastActivityAt: _lastActivityAt, activityStaleMs: _activityStaleMs, ...stable } = child;
- const serialized = JSON.stringify(stable);
- if (serialized === run.lastEmittedUpdate) return;
- run.lastEmittedUpdate = serialized;
- this._emit({ type: "rlm_child_update", child });
+ // cap. Streamed deltas would otherwise pay a snapshot build plus a serialization
+ // each; only a detected change builds the fresh snapshot, which carries both clock fields.
+ const child = run.session ?? this._rlmChildSessions.get(run.id)?.session;
+ const next = this._rlmChildStableSnapshotForRun(run, child);
+ if (run.lastEmittedUpdate && rlmChildStableFieldsEqual(run.lastEmittedUpdate, next)) return;
+ // next holds run.activity by reference; safe because activity objects are replaced, never mutated.
+ run.lastEmittedUpdate = next;
+ this._emit({ type: "rlm_child_update", child: this._rlmChildSnapshotForRun(run, child) });
};
run.emitUpdate = emitChildUpdate;
emitChildUpdate();
@@ -14640,7 +14684,7 @@ export class AgentSession {
children: [],
}),
id: run.id,
- label: rlmChildLabel(run.prompt),
+ label: run.label,
status: run.status,
});
}
diff --git a/packages/coding-agent/src/modes/daemon/daemon-supervisor.ts b/packages/coding-agent/src/modes/daemon/daemon-supervisor.ts
index f291402560..bbf1fb89c6 100644
--- a/packages/coding-agent/src/modes/daemon/daemon-supervisor.ts
+++ b/packages/coding-agent/src/modes/daemon/daemon-supervisor.ts
@@ -790,6 +790,8 @@ export class DaemonSupervisor {
private readonly pendingRosterRemoved = new Set<string>();
/** Ids declared to subscribers: gates removals to once and keeps owned-only row ids private. */
private readonly publishedRosterIds = new Set<string>();
+ /** Last broadcast serialization per published id: identical rewrites stay off every subscriber's wire. */
+ private readonly publishedRosterJson = new Map<string, string>();
private rosterPushScheduled = false;
private rosterWatchdogTimer?: ReturnType<typeof setInterval>;
private rlmSpawnLedgerInstance?: RlmSpawnLedger;
@@ -4645,14 +4647,20 @@ export class DaemonSupervisor {
const removed: string[] = [];
for (const agentId of this.pendingRosterRemoved) {
if (this.publishedRosterIds.delete(agentId)) removed.push(agentId);
+ // The id leaves the client view: a re-added row republishes even when its content repeats the last broadcast.
+ this.publishedRosterJson.delete(agentId);
}
for (const agentId of this.pendingRosterChanged) {
const entry = this.roster().get(agentId);
if (!entry) continue;
if (this.isRosterEntryVisibleToClients(entry)) {
+ const json = JSON.stringify(entry);
+ if (this.publishedRosterJson.get(agentId) === json) continue;
+ this.publishedRosterJson.set(agentId, json);
changed.push(entry);
this.publishedRosterIds.add(agentId);
} else if (this.publishedRosterIds.delete(agentId)) {
+ this.publishedRosterJson.delete(agentId);
removed.push(agentId);
}
}
diff --git a/packages/coding-agent/test/agent-session-queue-mutation.test.ts b/packages/coding-agent/test/agent-session-queue-mutation.test.ts
index b23d4a5981..6ac6c46897 100644
--- a/packages/coding-agent/test/agent-session-queue-mutation.test.ts
+++ b/packages/coding-agent/test/agent-session-queue-mutation.test.ts
@@ -15,7 +15,7 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { AgentSession } from "../src/core/agent-session.js";
import { AuthStorage } from "../src/core/auth-storage.js";
import { ModelRegistry } from "../src/core/model-registry.js";
-import type { QueuedMessageMutation } from "../src/core/session-action-store.js";
+import type { QueuedMessageMutation, SessionActionSnapshot } from "../src/core/session-action-store.js";
import { SessionManager } from "../src/core/session-manager.js";
import { SettingsManager } from "../src/core/settings-manager.js";
import { createTestResourceLoader } from "./utilities.js";
@@ -292,4 +292,17 @@ describe("AgentSession queue mutation", () => {
await session.abort();
await running.catch(() => {});
});
+
+ it("skips session_action_update when a replace leaves the queue unchanged", async () => {
+ createSession();
+ const { running } = await blockSession();
+ await session.steer("s1");
+ const updates: SessionActionSnapshot[] = [];
+ session.subscribe((event) => event.type === "session_action_update" && updates.push(event.actions));
+ expect(mutate("steering", 0, "s1", { type: "replace", text: "s1", lane: "steering" })).toBe("applied");
+ expect(updates).toEqual([]);
+ mutate("steering", 0, "s1", { type: "replace", text: "s1 edited", lane: "steering" });
+ expect(updates).toMatchObject([{ steering: ["s1 edited"] }]);
+ await Promise.all([session.abort(), running.catch(() => {})]);
+ });
});
diff --git a/packages/coding-agent/test/agent-session-recursion.test.ts b/packages/coding-agent/test/agent-session-recursion.test.ts
index a49404f0c0..a047bdb10e 100644
--- a/packages/coding-agent/test/agent-session-recursion.test.ts
+++ b/packages/coding-agent/test/agent-session-recursion.test.ts
@@ -20,7 +20,12 @@ import {
formatAgentSessionNameUnavailable,
isAgentSessionMessage,
} from "../src/core/agent-messages.js";
-import { AgentSession } from "../src/core/agent-session.js";
+import {
+ AgentSession,
+ compactRlmText,
+ RLM_CHILD_UPDATE_MIN_INTERVAL_MS,
+ type RlmChildAgentSnapshot,
+} from "../src/core/agent-session.js";
import { AuthStorage } from "../src/core/auth-storage.js";
import type { LoadExtensionsResult } from "../src/core/extensions/index.js";
import { type HostRequestHandler, type HostRequestHandlers, ReplKernelManager } from "../src/core/kernel/index.js";
@@ -105,6 +110,7 @@ interface InspectableRlmRun {
error?: string;
abandonedForQuiescence?: boolean;
activity?: { kind: string };
+ lastStreamedUpdateMonotonicAt?: number;
progressNotes: string[];
emitUpdate?: () => void;
publication?: { promise: Promise<void>; resolve(): void; reject(error: Error): void };
@@ -993,6 +999,33 @@ describe("AgentSession rlm recursion", () => {
expect(terminalNotices(root)).toHaveLength(0);
});
+ it("skips rlm_child_update re-emission for streamed deltas that change no observable state", async () => {
+ const { root, hostedChild: child, releaseStartup } = createStartupGatedRoot();
+ child.agent.streamFn = () => createAssistantMessageEventStream(); // held open: no terminal event
+ await root.runRlmChild("stream one long answer", { name: "saturating-worker" });
+ releaseStartup();
+ const run = [...(root as unknown as InspectableRlmSession)._activeRlmChildRuns.values()][0]!;
+ await run.publication!.promise;
+ const childUpdates: RlmChildAgentSnapshot[] = [];
+ root.subscribe((event) => event.type === "rlm_child_update" && childUpdates.push(event.child));
+ const emit = (child as unknown as { _emit(event: unknown): void })._emit.bind(child);
+ for (const l of [50, 200, 210, 220]) {
+ // Hold the streamed-delta window open on every delta: this case pins the
+ // observable-state dedup, while coalescing inside one window is covered by
+ // the rlm-progress-notes cases.
+ run.lastStreamedUpdateMonotonicAt = performance.now() - RLM_CHILD_UPDATE_MIN_INTERVAL_MS - 1;
+ emit({ type: "message_start", message: assistantMessage("w".repeat(l)) });
+ }
+ // The preview keeps only the 160-char message tail, so both deltas past the cap
+ // reproduce the previous preview byte for byte and emit nothing.
+ expect(childUpdates.map((snapshot) => snapshot.answerPreview)).toEqual([
+ compactRlmText("w".repeat(50)),
+ compactRlmText("w".repeat(160)),
+ ]);
+ emit({ type: "agent_end", messages: [] }); // a real change (the activity) emits again
+ expect(childUpdates.at(-1)?.activity).toBeUndefined();
+ });
+
it("does not inject a terminal notice when a parent follow-up resets reply state after a reply", async () => {
const child = createSession({
depth: 1,
diff --git a/packages/coding-agent/test/agents-view-roster.test.ts b/packages/coding-agent/test/agents-view-roster.test.ts
index ac511eea29..c362722cc6 100644
--- a/packages/coding-agent/test/agents-view-roster.test.ts
+++ b/packages/coding-agent/test/agents-view-roster.test.ts
@@ -400,6 +400,7 @@ describe("subscriber push transitions", () => {
clients: new Set([subscriber]),
pendingRosterChanged: new Set(),
publishedRosterIds: new Set(),
+ publishedRosterJson: new Map(),
pendingRosterRemoved: new Set(),
rosterPushScheduled: false,
persistWorker: vi.fn(),
@@ -412,7 +413,7 @@ describe("subscriber push transitions", () => {
workerRosterEntries(worker: unknown): AgentRosterEntry[];
sweepRosterStaleness(now?: number): void;
promoteOwnedWorker(client: object, worker: unknown): Promise<void>;
- roster(): { delete(agentId: string): void };
+ roster(): { delete(agentId: string): void; get(agentId: string): AgentRosterEntry | undefined };
};
const settle = async () => {
await new Promise((resolve) => setImmediate(resolve));
@@ -544,13 +545,12 @@ describe("subscriber push transitions", () => {
worker,
Buffer.from(JSON.stringify({ type: "roster_delta", entries: [], snapshot: true })),
);
- await vi.waitFor(() => expect(pushes.length).toBeGreaterThan(0));
+ // The apply reseeds the deleted row byte-identically within one flush: no broadcast.
+ await (worker as { rosterApplyChain?: Promise<void> }).rosterApplyChain;
await settle();
- expect(pushes.some((push) => push.removed?.includes(childEntry.agentId))).toBe(false);
- expect(
- pushes.flatMap((push) => push.changed).find((entry) => entry.agentId === childEntry.agentId),
- ).toBeDefined();
+ expect(pushes).toEqual([]);
+ expect(supervisor.roster().get(childEntry.agentId)).toMatchObject({ agentId: childEntry.agentId });
});
it("sends one drain resync per loss gap even when the write reports backpressure", async () => {
diff --git a/packages/coding-agent/test/daemon-agent-roster.test.ts b/packages/coding-agent/test/daemon-agent-roster.test.ts
index fd9d857b57..124413f5f6 100644
--- a/packages/coding-agent/test/daemon-agent-roster.test.ts
+++ b/packages/coding-agent/test/daemon-agent-roster.test.ts
@@ -652,6 +652,7 @@ function makeWorker(workerId: string, overrides: Partial<WorkerFixture> = {}): W
interface SupervisorFixture {
workers: Map<string, WorkerFixture>;
+ flushRosterUpdates(): void;
consumeWorkerRosterDelta(worker: WorkerFixture, payload: Buffer): void;
handleList(
client: object,
@@ -667,6 +668,7 @@ interface SupervisorFixture {
get(agentId: string): AgentRosterEntry | undefined;
has(agentId: string): boolean;
values(): IterableIterator<AgentRosterEntry>;
+ delete(agentId: string): void;
};
refreshWorkerSummaries: ReturnType<typeof vi.fn>;
}
@@ -679,6 +681,7 @@ function makeSupervisor(workers: WorkerFixture[], extra: Record<string, unknown>
catalog: { list: vi.fn(async () => []) },
pendingRosterChanged: new Set(),
publishedRosterIds: new Set(),
+ publishedRosterJson: new Map(),
pendingRosterRemoved: new Set(),
rosterPushScheduled: false,
refreshWorkerSummaries: vi.fn(async () => {}),
@@ -727,6 +730,25 @@ function rosterDelta(entries: WorkerRosterEntry[], removedAgentIds?: string[], s
}
describe("supervisor roster ledger", () => {
+ it("broadcasts a roster row only when its published content changes", () => {
+ const write = vi.fn();
+ const client = { rosterSubscribed: true };
+ const supervisor = makeSupervisor([], { rosterPushScheduled: true, write, clients: new Set([client]) }); // flushes below run explicitly
+ const row = (a?: boolean) =>
+ workerRosterEntryFromSummary(summary({ id: "r", sessionId: "r", isSessionActive: a }));
+ const writeAndFlush = (a?: boolean) => supervisor.writeRosterEntry(row(a)) && supervisor.flushRosterUpdates();
+ writeAndFlush();
+ writeAndFlush();
+ expect(write.mock.calls).toHaveLength(1);
+ writeAndFlush(true);
+ expect(write.mock.calls).toHaveLength(2);
+ supervisor.roster().delete("r");
+ supervisor.flushRosterUpdates();
+ expect(write.mock.calls[2]?.[1]).toMatchObject({ removed: ["r"] });
+ writeAndFlush(true); // an identical re-add republishes: the removal dropped the baseline
+ expect(write.mock.calls).toHaveLength(4);
+ });
+
it("serves list from the ledger with zero worker round-trips and exact busy counts", async () => {
const visible = makeWorker("visible");
const owned = makeWorker("owned", {
diff --git a/packages/coding-agent/test/daemon-supervisor-monitor.test.ts b/packages/coding-agent/test/daemon-supervisor-monitor.test.ts
index 7598fbbcb5..07fd823b68 100644
--- a/packages/coding-agent/test/daemon-supervisor-monitor.test.ts
+++ b/packages/coding-agent/test/daemon-supervisor-monitor.test.ts
@@ -252,6 +252,7 @@ function createSupervisorSnapshotState() {
pendingReplacementSnapshots: new WeakMap<object, Map<string, unknown>>(),
pendingRosterChanged: new Set<string>(),
publishedRosterIds: new Set<string>(),
+ publishedRosterJson: new Map<string, string>(),
pendingRosterRemoved: new Set<string>(),
rosterPushScheduled: false,
};
diff --git a/packages/coding-agent/test/fixtures/roster-seed.ts b/packages/coding-agent/test/fixtures/roster-seed.ts
index 5645d59893..26f296fd21 100644
--- a/packages/coding-agent/test/fixtures/roster-seed.ts
+++ b/packages/coding-agent/test/fixtures/roster-seed.ts
@@ -17,6 +17,7 @@ export function seedSupervisorRoster(supervisor: object, ...workers: RosterWorke
pendingRosterChanged: new Set(),
pendingRosterRemoved: new Set(),
publishedRosterIds: new Set(),
+ publishedRosterJson: new Map(),
rosterPushScheduled: false,
clients: internals.clients ?? new Set(),
});