A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
[RSI, performance] Throttle event-flood hot paths in child updates, roster flushes, top bar, and summarizer (#2480)
Sebastian Müller031f12bcb223
diff --git a/packages/coding-agent/.changes/event-flood-throttle.md b/packages/coding-agent/.changes/event-flood-throttle.mdnew file mode 100644index 0000000000..6850abd697--- /dev/null+++ b/packages/coding-agent/.changes/event-flood-throttle.md@@ -0,0 +1 @@+- Reduced event-flood CPU on busy workers: child update previews now collapse only the trailing ~200 chars of the streaming message (O(L²) → O(200) per streamed child message) with per-child update emits capped at ~1/s, roster flushes coalesce into a 250ms window and recompose only trigger-marked sessions (lifecycle mutations still flush everything on the next tick), top-bar cost refreshes from session status are throttled to 1/s (agent_end and attach stay unthrottled), and recap summarizer model calls are capped at 4 concurrent fleet-wide with the most recently active session admitted first.diff --git a/packages/coding-agent/src/core/agent-session.ts b/packages/coding-agent/src/core/agent-session.tsindex 9ca4390f57..0384563214 100644--- a/packages/coding-agent/src/core/agent-session.ts+++ b/packages/coding-agent/src/core/agent-session.ts@@ -1079,6 +1079,8 @@ interface RlmChildRun {reportDeletionCleanupFailure?: (error: unknown) => Promise<void>;emitUpdate?: () => void;lastEmittedUpdate?: string;+ /** Monotonic time of the last streamed-delta emit; other event kinds still emit at once. */+ lastStreamedUpdateMonotonicAt?: number;unsubscribe?: () => void;}@@ -1095,6 +1097,8 @@ const KERNEL_STATE_LISTING_TIMEOUT_MS = 5000;const RLM_MAX_DEPTH_STATE_CUSTOM_TYPE = "rlm_max_depth_state";/** Minimum spacing between accepted progress notes from one child session. */const RLM_PROGRESS_NOTE_MIN_INTERVAL_MS = 10_000;+/** Minimum spacing between streamed-delta child update emits per child run. */+export const RLM_CHILD_UPDATE_MIN_INTERVAL_MS = 1_000;/** Bounded ring of progress notes kept per child run; the snapshot exposes the newest. */const RLM_CHILD_PROGRESS_NOTE_RING_MAX = 5;/** A running child with no tracked activity for this long reports activityStaleMs. */@@ -1376,6 +1380,24 @@ function readAssistantText(message: AssistantMessage): string {.join("");}+// Trailing window feeding the streaming answer preview: message_update fires per token delta, so+// rejoining the whole message costs O(length²); the preview shows the latest output, not the head.+// Equal to compactRlmText's cap, so compaction never cuts the newest characters.+const RLM_ANSWER_PREVIEW_TAIL_CHARS = 160;++function tailRlmAnswerPreview(message: AssistantMessage): string {+ const tail: string[] = [];+ let collected = 0;+ for (let index = message.content.length - 1; index >= 0 && collected < RLM_ANSWER_PREVIEW_TAIL_CHARS; index -= 1) {+ const block = message.content[index];+ if (block.type !== "text") continue;+ const remaining = RLM_ANSWER_PREVIEW_TAIL_CHARS - collected;+ tail.unshift(block.text.slice(-remaining));+ collected += Math.min(block.text.length, remaining);+ }+ return compactRlmText(tail.join(""));+}+function waitForPromiseOrAbort<T>(promise: Promise<T>,signal: AbortSignal | undefined,@@ -12539,17 +12561,25 @@ export class AgentSession {pendingChildUsage.set(origin, bucket);}}- const text = compactRlmText(readAssistantText(assistant));+ const text = tailRlmAnswerPreview(assistant);if (text) run.answerPreview = text;touchRlmChildActivity(run);emitChildUpdate();} else if (event.type === "message_start" || event.type === "message_update") {if (event.message.role === "assistant") {- const text = compactRlmText(readAssistantText(event.message as AssistantMessage));+ const text = tailRlmAnswerPreview(event.message as AssistantMessage);if (text) run.answerPreview = text;run.activity = { kind: "writing" };touchRlmChildActivity(run);- emitChildUpdate();+ // Snapshot+stringify per delta dominates streaming cost; message_end emits the final preview.+ const now = performance.now();+ if (+ run.lastStreamedUpdateMonotonicAt === undefined ||+ now - run.lastStreamedUpdateMonotonicAt >= RLM_CHILD_UPDATE_MIN_INTERVAL_MS+ ) {+ run.lastStreamedUpdateMonotonicAt = now;+ emitChildUpdate();+ }}} else if (event.type === "tool_execution_start") {flushPendingChildUsageIfStale();diff --git a/packages/coding-agent/src/modes/daemon/daemon-mode.ts b/packages/coding-agent/src/modes/daemon/daemon-mode.tsindex e8e751ac0a..1f459e6574 100644--- a/packages/coding-agent/src/modes/daemon/daemon-mode.ts+++ b/packages/coding-agent/src/modes/daemon/daemon-mode.ts@@ -623,6 +623,10 @@ export class AgentDaemon {snapshotPending: false,};private rosterFlushScheduled = false;+ private rosterFlushTimer?: ReturnType<typeof setTimeout>;+ private rosterLastFlushAt?: number;+ private readonly rosterDirtyAgentIds = new Set<string>();+ private rosterFlushFull = false;private rosterHeartbeatTimer?: ReturnType<typeof setInterval>;private rlmSpawnLedgerInstance?: RlmSpawnLedger;/** In-flight admission spawn appends, awaited (and consumed) by createRlmSubagentRuntime. */@@ -7300,7 +7304,7 @@ export class AgentDaemon {) {return;}- this.scheduleRosterFlush();+ this.scheduleRosterFlush(state);}private observeRosterChildUpdate(state: ActiveSessionState, child: AgentConnectionRlmChildAgentSnapshot): void {@@ -7311,7 +7315,7 @@ export class AgentDaemon {} else {this.rosterReporter.queuedChildren.delete(entry.agentId);}- this.scheduleRosterFlush();+ this.scheduleRosterFlush(state);}private hasSessionForRlmChild(parentState: ActiveSessionState, childId: string): boolean {@@ -7352,17 +7356,53 @@ export class AgentDaemon {return { agentId: rosterAgentIdForSummary(summary), queuedChild: true, summary };}- private scheduleRosterFlush(): void {- if (!this.options.worker || this.rosterFlushScheduled || this.shuttingDown) return;- this.rosterFlushScheduled = true;- setImmediate(() => {- this.rosterFlushScheduled = false;- try {- this.flushRoster();- } catch (error) {- this.log(`could not publish roster delta: ${String(error)}`);- }- });+ // Session-scoped triggers mark only their session dirty; other callers can affect any session+ // and schedule a full recompose on the next tick: the supervisor answers `list` from the+ // published roster, so a hydrated or closed session must be visible before the command's response.+ private scheduleRosterFlush(state?: ActiveSessionState): void {+ if (!this.options.worker || this.shuttingDown) return;+ if (state === undefined) {+ this.rosterFlushFull = true;+ } else {+ this.rosterDirtyAgentIds.add(this.rosterAgentIdForState(state));+ }+ if (this.rosterFlushScheduled) return;+ if (+ state === undefined ||+ this.rosterLastFlushAt === undefined ||+ Date.now() - this.rosterLastFlushAt >= ROSTER_FLUSH_MIN_INTERVAL_MS+ ) {+ // A full flush subsumes a pending trailing flush: it consumes its dirty marks.+ if (this.rosterFlushTimer !== undefined) {+ clearTimeout(this.rosterFlushTimer);+ this.rosterFlushTimer = undefined;+ }+ this.rosterFlushScheduled = true;+ setImmediate(() => {+ this.rosterFlushScheduled = false;+ this.runScheduledRosterFlush();+ });+ return;+ }+ if (this.rosterFlushTimer !== undefined) return;+ this.rosterFlushTimer = setTimeout(+ () => {+ this.rosterFlushTimer = undefined;+ this.runScheduledRosterFlush();+ },+ // Clamp the elapsed at zero: a backwards wall-clock step must not arm an over-long window.+ ROSTER_FLUSH_MIN_INTERVAL_MS - Math.max(0, Date.now() - this.rosterLastFlushAt),+ );+ this.rosterFlushTimer.unref?.();+ }++ private runScheduledRosterFlush(): void {+ this.rosterLastFlushAt = Date.now();+ try {+ this.flushRoster();+ } catch (error) {+ this.log(`could not publish roster delta: ${String(error)}`);+ }}private flushRoster(): void {@@ -7373,18 +7413,49 @@ export class AgentDaemon {// be reused instead of re-composed and re-stringified on every flush.const composedSources = new Map<string, SessionSummary>();const scheduledJobs = this.cronStore.list();- for (const summary of buildSessionList([...this.sessions.values()], [], scheduledJobs)) {- const agentId = rosterAgentIdForSummary(summary);- if (reporter.lastComposedSource.get(agentId) === summary) {- const entry = reporter.lastComposed.get(agentId);- if (entry) {- entries.set(agentId, entry);- composedSources.set(agentId, summary);- continue;+ // Compose only what a trigger marked dirty (plus never-composed or passivated rows): composing+ // is the fingerprint walk. A flush with no marks recomposes everything: untracked callers exist.+ const fullFlush = this.rosterFlushFull || this.rosterDirtyAgentIds.size === 0;+ this.rosterFlushFull = false;+ const dirtyAgentIds = new Set(this.rosterDirtyAgentIds);+ this.rosterDirtyAgentIds.clear();+ const targetStates: ActiveSessionState[] = [];+ for (const state of this.sessions.values()) {+ if (fullFlush) {+ targetStates.push(state);+ continue;+ }+ const agentId = this.rosterAgentIdForState(state);+ const previous = reporter.lastComposed.get(agentId);+ if (dirtyAgentIds.has(agentId) || previous === undefined || previous.summary.activeSessionId === undefined) {+ targetStates.push(state);+ }+ }+ const composedSummaries = new Map<string, SessionSummary>();+ for (const summary of buildSessionList(targetStates, [], scheduledJobs)) {+ composedSummaries.set(rosterAgentIdForSummary(summary), summary);+ }+ for (const state of this.sessions.values()) {+ const agentId = this.rosterAgentIdForState(state);+ const summary = composedSummaries.get(agentId);+ if (summary !== undefined) {+ if (reporter.lastComposedSource.get(agentId) === summary) {+ const entry = reporter.lastComposed.get(agentId);+ if (entry) {+ entries.set(agentId, entry);+ composedSources.set(agentId, summary);+ continue;+ }}+ entries.set(agentId, workerRosterEntryFromSummary(summary));+ composedSources.set(agentId, summary);+ continue;}- entries.set(agentId, workerRosterEntryFromSummary(summary));- composedSources.set(agentId, summary);+ const previous = reporter.lastComposed.get(agentId);+ if (previous === undefined) continue;+ entries.set(agentId, previous);+ const previousSource = reporter.lastComposedSource.get(agentId);+ if (previousSource !== undefined) composedSources.set(agentId, previousSource);}for (const [agentId, queued] of reporter.queuedChildren) {if (entries.has(agentId)) {@@ -7903,6 +7974,10 @@ export class AgentDaemon {clearInterval(this.rosterHeartbeatTimer);this.rosterHeartbeatTimer = undefined;}+ if (this.rosterFlushTimer) {+ clearTimeout(this.rosterFlushTimer);+ this.rosterFlushTimer = undefined;+ }this.log(`shutting down (exit ${exitCode}); closing ${this.sessions.size} active session(s)`);const closingReason = this.getShutdownClosingReason();for (const client of this.clients) {@@ -7962,6 +8037,9 @@ const ROSTER_SESSION_EVENT_TRIGGERS = new Set(["thinking_level_changed",]);+// Flushes coalesce into one window: streaming bursts fire several triggers per turn.+const ROSTER_FLUSH_MIN_INTERVAL_MS = 250;+/*** The transfer id must name the cursor observed at materialization, not the live session cursor:* events appended in between would let two different byte streams share one snapshot id.diff --git a/packages/coding-agent/src/modes/daemon/daemon-session-summarizer.ts b/packages/coding-agent/src/modes/daemon/daemon-session-summarizer.tsindex 7a20c2e199..2742e8a0ca 100644--- a/packages/coding-agent/src/modes/daemon/daemon-session-summarizer.ts+++ b/packages/coding-agent/src/modes/daemon/daemon-session-summarizer.ts@@ -7,6 +7,9 @@ import type { AgentStatus, AgentTaskState } from "../../core/session-manager.js"import type { ActiveSessionState } from "./active-session-state.js";const SWEEP_INTERVAL_MS = 25_000;+// Fleet-wide cap on concurrent model calls: bursty turn ends across many sessions must+// not fan out into unbounded generations. Over-cap sessions wait; the 25s sweep re-invokes them.+const MAX_CONCURRENT_SUMMARY_GENERATIONS = 4;// Collapse a tool-use loop's rapid turn_end bursts into one summarization.const SETTLE_DEBOUNCE_MS = 2_000;// Idle generations stop retrying (and paying) on unchanged content until the@@ -247,6 +250,7 @@ export class DaemonSessionSummarizer {private readonly inFlight = new Map<string, AbortController>();// Sessions requested while one was running; get one more pass on completion.private readonly rerunRequested = new Set<string>();+ private readonly waitingForSlot = new Map<string, ActiveSessionState>();// Failed idle generations per session, keyed to the settled content they saw.private readonly failedIdleGenerations = new Map<string,@@ -287,6 +291,7 @@ export class DaemonSessionSummarizer {controller.abort();}this.rerunRequested.clear();+ this.waitingForSlot.clear();this.failedIdleGenerations.clear();}@@ -299,6 +304,7 @@ export class DaemonSessionSummarizer {}this.inFlight.get(activeSessionId)?.abort();this.rerunRequested.delete(activeSessionId);+ this.waitingForSlot.delete(activeSessionId);this.failedIdleGenerations.delete(activeSessionId);}@@ -392,6 +398,11 @@ export class DaemonSessionSummarizer {const streaming = isWorking ? session.state.streamingMessage : undefined;const contextMessages = streaming ? [...messages, streaming] : messages;+ if (this.inFlight.size >= MAX_CONCURRENT_SUMMARY_GENERATIONS) {+ this.waitingForSlot.set(id, state);+ return;+ }+const controller = new AbortController();this.inFlight.set(id, controller);try {@@ -447,6 +458,7 @@ export class DaemonSessionSummarizer {);} finally {this.inFlight.delete(id);+ this.admitNextWaitingSession();// Re-debounce a request that arrived mid-pass instead of dropping it.if (this.rerunRequested.delete(id)) {this.notifyActivity(state);@@ -454,6 +466,31 @@ export class DaemonSessionSummarizer {}}+ /**+ * Admit the most recently active waiters while slots are free. summarize() claims its slot before+ * its first await, so a waiter that returns early without one leaves inFlight unchanged.+ */+ private admitNextWaitingSession(): void {+ while (this.inFlight.size < MAX_CONCURRENT_SUMMARY_GENERATIONS && this.waitingForSlot.size > 0) {+ let next: ActiveSessionState | undefined;+ let nextActivityAt = -1;+ for (const candidate of this.waitingForSlot.values()) {+ const activityAt = candidate.runtime.session.messages.at(-1)?.timestamp ?? 0;+ if (+ activityAt > nextActivityAt ||+ (activityAt === nextActivityAt &&+ (next === undefined || candidate.activeSessionId > next.activeSessionId))+ ) {+ next = candidate;+ nextActivityAt = activityAt;+ }+ }+ if (next === undefined) return;+ this.waitingForSlot.delete(next.activeSessionId);+ void this.summarize(next);+ }+ }+/*** Publish an in-memory status and persist a settled idle verdict that differs* from the latest persisted entry: sweeps must not grow the journal. `persist`diff --git a/packages/coding-agent/src/modes/interactive/interactive-mode.ts b/packages/coding-agent/src/modes/interactive/interactive-mode.tsindex 9b6f1a9047..f2881ea821 100644--- a/packages/coding-agent/src/modes/interactive/interactive-mode.ts+++ b/packages/coding-agent/src/modes/interactive/interactive-mode.ts@@ -348,6 +348,9 @@ interface PendingToolCallRenderInput {const HEARTBEAT_LEGACY_PROMPT_MIN_TOLERANCE_MS = 15_000;const HEARTBEAT_LEGACY_PROMPT_MAX_TOLERANCE_MS = 120_000;const MODEL_CATALOG_REFRESH_TTL_MS = 60_000;+// Status-driven top bar cost refreshes rebuild the full context tree; agent_end+// and attach/reconnect refresh unthrottled for per-turn and per-attach convergence.+const TOP_BAR_COST_REFRESH_MIN_INTERVAL_MS = 1_000;function isLabeledQueuedPreview(message: string): boolean {return (message.startsWith(`${HEARTBEAT_PROMPT_PREVIEW_LABEL}: `) ||@@ -1425,7 +1428,7 @@ export class InteractiveMode {/** Cached session spend (USD) for the top bar, keyed to the session it was fetched for. */private topBarCost: { sessionId?: string; total?: number } = {};/** Stale-discard state for top bar cost refreshes (mirrors contextUsageRefresh). */- private topBarCostRefresh = { generation: 0, lastSuccessGeneration: 0 };+ private topBarCostRefresh = { generation: 0, lastSuccessGeneration: 0, lastRefreshAt: 0 };private builtInHeader: Component | undefined = undefined;@@ -1862,6 +1865,13 @@ export class InteractiveMode {await this.updateAvailableProviderCount();}+ /** Leading-edge throttled refresh for status-driven call sites; direct refreshes stamp the window too. */+ private refreshTopBarCostThrottled(): void {+ const refresh = this.topBarCostRefresh ?? { lastRefreshAt: 0 };+ if (Date.now() - refresh.lastRefreshAt < TOP_BAR_COST_REFRESH_MIN_INTERVAL_MS) return;+ this.refreshTopBarCost();+ }+/*** Refresh the top bar's cached session spend from the context tree.* Results for a replaced session, or superseded by a newer successful@@ -1871,8 +1881,9 @@ export class InteractiveMode {// Partial-mode test harnesses skip the constructor, so the field// initializer may be absent there; the refresh is cosmetic and must// never crash a real flow on any `this`.- this.topBarCostRefresh ??= { generation: 0, lastSuccessGeneration: 0 };+ this.topBarCostRefresh ??= { generation: 0, lastSuccessGeneration: 0, lastRefreshAt: 0 };const refresh = this.topBarCostRefresh;+ refresh.lastRefreshAt = Date.now();const generation = ++refresh.generation;const connection = this.agentConnection;const sessionId = this.connectionState?.sessionId;@@ -5793,7 +5804,7 @@ export class InteractiveMode {this.sessionRecap = event.recap;this.patchConnectionState({ recap: event.recap });this.renderRecap();- this.refreshTopBarCost();+ this.refreshTopBarCostThrottled();} else if (event.type === "side_question_event") {this.handleSideQuestionEvent(event.event);} else if (event.type === "extension_ui_request") {@@ -6042,7 +6053,7 @@ export class InteractiveMode {case "session_info_changed":this.updateTerminalTitle();- this.refreshTopBarCost();+ this.refreshTopBarCostThrottled();this.footer.invalidate();this.ui.requestRender();break;diff --git a/packages/coding-agent/test/daemon-agent-roster.test.ts b/packages/coding-agent/test/daemon-agent-roster.test.tsindex 41b6feec25..fd9d857b57 100644--- a/packages/coding-agent/test/daemon-agent-roster.test.ts+++ b/packages/coding-agent/test/daemon-agent-roster.test.ts@@ -22,6 +22,7 @@ type RosterDelta = Extract<DaemonWorkerRosterOutbound, { type: "roster_delta" }>const tempDirs: string[] = [];afterEach(() => {+ vi.useRealTimers();for (const directory of tempDirs.splice(0)) rmSync(directory, { recursive: true, force: true });});@@ -31,6 +32,7 @@ interface WorkerReporterFixture {daemon: {sessions: Map<string, ActiveSessionState>;observeRosterEvent(state: ActiveSessionState, message: unknown): void;+ scheduleRosterFlush(state?: ActiveSessionState): void;flushRoster(): void;rosterReporter: {lastComposed: Map<string, WorkerRosterEntry>;@@ -63,6 +65,7 @@ function makeWorkerReporter(connected = true): WorkerReporterFixture {snapshotPending: false,},rosterFlushScheduled: false,+ rosterDirtyAgentIds: new Set<string>(),shuttingDown: false,hasAuthenticatedSupervisorClient: () => connection.connected,broadcastRosterFrame: (message: DaemonWorkerRosterOutbound) => {@@ -420,6 +423,7 @@ describe("worker roster reporter", () => {});it("flushes cron and model changes that have no session-event carrier", async () => {+ vi.useFakeTimers();const directory = mkdtempSync(join(tmpdir(), "prime-roster-cron-flush-"));tempDirs.push(directory);const daemon = new AgentDaemon(join(directory, "worker.sock"), {@@ -452,7 +456,7 @@ describe("worker roster reporter", () => {prompt: "check status",});expect(added.success).toBe(true);- await new Promise((resolveSettle) => setImmediate(resolveSettle));+ await vi.advanceTimersByTimeAsync(300);const agentId = "session-root-active";expect(internals.rosterReporter.lastComposed.get(agentId)?.summary.hasRegisteredCronJob).toBe(true);@@ -464,7 +468,7 @@ describe("worker roster reporter", () => {activeSessionId: "root-active",jobId,});- await new Promise((resolveSettle) => setImmediate(resolveSettle));+ await vi.advanceTimersByTimeAsync(300);expect(internals.rosterReporter.lastComposed.get(agentId)?.summary.hasRegisteredCronJob).toBeUndefined();// set_model has no session-event carrier either; its explicit flush publishes the new model.@@ -482,7 +486,7 @@ describe("worker roster reporter", () => {provider: "prov",modelId: "m2",});- await new Promise((resolveSettle) => setImmediate(resolveSettle));+ await vi.advanceTimersByTimeAsync(300);expect(internals.rosterReporter.lastComposed.get(agentId)?.summary.model).toMatchObject({ id: "m2" });});@@ -548,6 +552,50 @@ describe("worker roster reporter", () => {daemon.flushRoster();expect(sentDeltas.at(-1)?.entries[0]?.summary).toMatchObject({ isStreaming: false, activity: "idle" });});++ it("coalesces flush triggers into a 250ms window and scopes them to the dirty session", () => {+ vi.useFakeTimers();+ const { daemon, sentDeltas } = makeWorkerReporter();+ const quiet = makeState({ activeSessionId: "quiet-active" });+ const loud = makeState({ activeSessionId: "loud-active" });+ daemon.sessions.set(quiet.activeSessionId, quiet).set(loud.activeSessionId, loud);+ const statusFor = (state: ActiveSessionState) => ({+ type: "session_status",+ activeSessionId: state.activeSessionId,+ });+ const quietSummary = () => daemon.rosterReporter.lastComposed.get("session-quiet-active")?.summary.isStreaming;++ daemon.observeRosterEvent(loud, statusFor(loud));+ vi.advanceTimersByTime(0);+ expect(sentDeltas).toHaveLength(1);+ (loud.runtime.session as unknown as { isStreaming: boolean }).isStreaming = true;+ daemon.observeRosterEvent(loud, statusFor(loud));+ vi.advanceTimersByTime(100);+ expect(sentDeltas).toHaveLength(1);+ vi.advanceTimersByTime(150);+ expect(sentDeltas).toHaveLength(2);+ expect(+ sentDeltas.at(-1)?.entries.find((entry) => entry.summary.sessionName === "name-loud-active")?.summary+ .isStreaming,+ ).toBe(true);++ // quiet flips busy with no roster event of its own: loud-only triggers must not republish it.+ (quiet.runtime.session as unknown as { isStreaming: boolean }).isStreaming = true;+ daemon.observeRosterEvent(loud, statusFor(loud));+ vi.advanceTimersByTime(300);+ expect(quietSummary()).toBe(false);+ daemon.observeRosterEvent(quiet, statusFor(quiet));+ vi.advanceTimersByTime(300);+ expect(quietSummary()).toBe(true);++ // A lifecycle (stateless) flush inside the window breaks through the pending trailing+ // flush: the supervisor answers `list` from the published roster, so it must not lag.+ (loud.runtime.session as unknown as { isStreaming: boolean }).isStreaming = false;+ daemon.observeRosterEvent(loud, statusFor(loud));+ daemon.scheduleRosterFlush();+ vi.advanceTimersByTime(0);+ expect(sentDeltas).toHaveLength(4);+ });});// --- Supervisor-side roster ledger ---diff --git a/packages/coding-agent/test/daemon-session-summarizer.test.ts b/packages/coding-agent/test/daemon-session-summarizer.test.tsindex c60bf387e1..5c8716b88f 100644--- a/packages/coding-agent/test/daemon-session-summarizer.test.ts+++ b/packages/coding-agent/test/daemon-session-summarizer.test.ts@@ -7,11 +7,12 @@ import {type AgentStatusResult,buildStatusContext,DaemonSessionSummarizer,+ type GenerateAgentStatusParams,parseAgentStatusResponse,} from "../src/modes/daemon/daemon-session-summarizer.js";-function userMessage(text: string): AgentMessage {- return { role: "user", content: [{ type: "text", text }], timestamp: 0 } as unknown as AgentMessage;+function userMessage(text: string, timestamp = 0): AgentMessage {+ return { role: "user", content: [{ type: "text", text }], timestamp } as unknown as AgentMessage;}function assistantMessage(text: string, tools: string[] = []): AgentMessage {@@ -165,13 +166,14 @@ describe("daemon session summarizer", () => {function makeState(options: {messages: AgentMessage[];isSessionActive: boolean;+ activeSessionId?: string;summaryState?: AgentStatus;persistedStatus?: AgentStatus;appendAgentStatus?: (status: AgentStatus) => void;getLeafId?: () => string | null;}): ActiveSessionState {return {- activeSessionId: "active-1",+ activeSessionId: options.activeSessionId ?? "active-1",summaryState: options.summaryState,runtime: {session: {@@ -291,6 +293,31 @@ describe("daemon session summarizer", () => {expect(internal.failedIdleGenerations.size).toBe(0);});+ test("caps concurrent generations and admits the most recently active waiter first", async () => {+ const pendingGates: Array<() => void> = [];+ const generate = vi.fn((_params: GenerateAgentStatusParams) =>+ new Promise<void>((resolveGate) => pendingGates.push(resolveGate)).then(() => ({ summary: "done" })),+ );+ const states = ["s1", "s2", "s3", "s4", "s5", "s6"].map((id, index) =>+ makeState({ activeSessionId: id, isSessionActive: true, messages: [userMessage("hi", index + 1)] }),+ );+ const summarizer = new DaemonSessionSummarizer(() => states, undefined, generate);+ const internal = summarizer as unknown as { summarize(state: ActiveSessionState): Promise<void> };++ const running = states.slice(0, 4).map((state) => internal.summarize(state));+ states.slice(4).map((state) => internal.summarize(state));+ expect(generate).toHaveBeenCalledTimes(4);+ pendingGates[3]!();+ await running[3];+ expect(generate).toHaveBeenCalledTimes(5);+ expect(generate.mock.calls[4]?.[0]?.messages.at(-1)?.timestamp).toBe(6);+ summarizer.forget("s5");+ pendingGates[0]!();+ await running[0];+ expect(generate).toHaveBeenCalledTimes(5);+ for (const release of pendingGates) release();+ });+test("an idle re-settle matching the latest persisted status appends nothing", async () => {const appendAgentStatus = vi.fn();const persisted: AgentStatus = {diff --git a/packages/coding-agent/test/daemon-supervisor-monitor.test.ts b/packages/coding-agent/test/daemon-supervisor-monitor.test.tsindex e2b2ee5816..db31629250 100644--- a/packages/coding-agent/test/daemon-supervisor-monitor.test.ts+++ b/packages/coding-agent/test/daemon-supervisor-monitor.test.ts@@ -518,6 +518,8 @@ describe("daemon worker supervisor monitoring", () => {removedAgentIds: new Map(),snapshotPending: false,},+ rosterFlushScheduled: false,+ rosterDirtyAgentIds: new Set(),shuttingDown: false,clearSupervisorAvailabilityCheck: vi.fn(),scheduleSupervisorFenceCheck: vi.fn(),diff --git a/packages/coding-agent/test/interactive-mode-status.test.ts b/packages/coding-agent/test/interactive-mode-status.test.tsindex c4c898da71..4358bc3e57 100644--- a/packages/coding-agent/test/interactive-mode-status.test.ts+++ b/packages/coding-agent/test/interactive-mode-status.test.ts@@ -1,7 +1,7 @@import type { AgentMessage, ThinkingLevel } from "@earendil-works/pi-agent-core";import type { Api, Model, ServiceTier } from "@earendil-works/pi-ai";import { Container } from "@earendil-works/pi-tui";-import { beforeAll, describe, expect, test, vi } from "vitest";+import { afterEach, beforeAll, describe, expect, test, vi } from "vitest";import type { AgentSessionRuntime } from "../src/core/agent-session-runtime.js";import { AuthStorage } from "../src/core/auth-storage.js";import { emptyGoalState } from "../src/core/goals.js";@@ -234,6 +234,8 @@ describe("InteractiveMode.renderSessionContext", () => {describe("InteractiveMode connection events", () => {type ConnectionEventListener = (event: any) => Promise<void> | void;+ afterEach(() => vi.useRealTimers());+function createSubscribeHarness(overrides: Record<string, any> = {}): {fakeThis: Record<string, any>;emit: ConnectionEventListener;@@ -552,6 +554,44 @@ describe("InteractiveMode connection events", () => {expect(calls).toEqual(["replacement", "reset", "messages", "display", "loader"]);});++ test("throttles session_status top bar refreshes to one per second; direct refreshes reset the window", async () => {+ vi.useFakeTimers();+ const { fakeThis, emit } = createSubscribeHarness({+ patchConnectionState: vi.fn(),+ renderRecap: vi.fn(),+ isInitialized: true,+ footer: { invalidate: vi.fn() },+ activityTracker: { handleEvent: vi.fn() },+ updateWorkingLoaderMessage: vi.fn(),+ updateTerminalTitle: vi.fn(),+ });+ Object.setPrototypeOf(fakeThis, InteractiveMode.prototype);+ const getContextTree = vi.fn(async () => ({ totalUsage: { cost: { total: 5 } } }));+ fakeThis.agentConnection.getContextTree = getContextTree;++ await emit({ type: "session_status", recap: "working" });+ expect(getContextTree).toHaveBeenCalledOnce();+ await emit({ type: "session_status", recap: "still working" });+ expect(getContextTree).toHaveBeenCalledOnce();+ // session_info_changed routes the same throttled refresh through handleEvent's switch.+ const handleEvent = (+ InteractiveMode.prototype as unknown as {+ handleEvent(this: unknown, event: unknown): Promise<void>;+ }+ ).handleEvent;+ await handleEvent.call(fakeThis, { type: "session_info_changed" });+ expect(getContextTree).toHaveBeenCalledOnce();+ vi.advanceTimersByTime(1_100);+ await emit({ type: "session_status", recap: "done" });+ expect(getContextTree).toHaveBeenCalledTimes(2);+ (InteractiveMode.prototype as unknown as { refreshTopBarCost(this: unknown): void }).refreshTopBarCost.call(+ fakeThis,+ );+ expect(getContextTree).toHaveBeenCalledTimes(3);+ await emit({ type: "session_status", recap: "again" });+ expect(getContextTree).toHaveBeenCalledTimes(3);+ });});describe("InteractiveMode connection extension UI", () => {diff --git a/packages/coding-agent/test/rlm-progress-notes.test.ts b/packages/coding-agent/test/rlm-progress-notes.test.tsindex 737b6694ae..71f8c47f3b 100644--- a/packages/coding-agent/test/rlm-progress-notes.test.ts+++ b/packages/coding-agent/test/rlm-progress-notes.test.ts@@ -11,7 +11,7 @@ import {type Usage,} from "@earendil-works/pi-ai";import { afterEach, beforeEach, describe, expect, it } from "vitest";-import { AgentSession } from "../src/core/agent-session.js";+import { AgentSession, RLM_CHILD_UPDATE_MIN_INTERVAL_MS } from "../src/core/agent-session.js";import { AuthStorage } from "../src/core/auth-storage.js";import { convertToLlm } from "../src/core/messages.js";import { ModelRegistry } from "../src/core/model-registry.js";@@ -26,6 +26,7 @@ interface InspectableRlmRun {progressNotes: string[];lastActivityAt?: number;lastActivityMonotonicAt?: number;+ lastStreamedUpdateMonotonicAt?: number;status: string;activity?: { kind: string };session?: AgentSession;@@ -502,7 +503,7 @@ describe("rlm.progress.note child progress channel", () => {}});- it("does not churn child updates on streaming deltas once the preview saturates", async () => {+ it("throttles streamed child updates, still previews the latest tail, and emits real activity", async () => {const held = heldAnswerStream();session = makeSession(held.streamFn);const handle = await session.runRlmChild("slow task", { name: "worker-a" });@@ -526,9 +527,7 @@ describe("rlm.progress.note child progress channel", () => {// The agent turn starts asynchronously; wait for its activity signal so// no tracked event can race the injected streaming deltas.await waitFor(() => run.activity !== undefined);- // Streaming past the 160-character preview cap: like real token- // deltas, appending text past the cap leaves the capped preview- // unchanged, so only lastActivityAt still moves.+ // Streaming past the preview cap: the throttle coalesces the burst, so only lastActivityAt moves.const saturatedText = "saturation".repeat(40);const baseline = childUpdates.length;emitChild({ type: "message_update", message: assistantMessage(saturatedText) });@@ -538,9 +537,8 @@ describe("rlm.progress.note child progress channel", () => {// Saturated deltas must not re-emit (regression: the advancing// lastActivityAt used to defeat the snapshot dedup on every delta).const saturated = childUpdates.length;- const lastActivityBefore = run.lastActivityAt;+ const lastActivityMonotonicBefore = run.lastActivityMonotonicAt;for (let index = 0; index < 3; index += 1) {- await new Promise((resolve) => setTimeout(resolve, 2));emitChild({type: "message_update",message: assistantMessage(`${saturatedText}${"x".repeat(index + 1)}`),@@ -548,18 +546,23 @@ describe("rlm.progress.note child progress channel", () => {}expect(childUpdates.length).toBe(saturated);- // Staleness semantics survive: streaming still counts as activity.- expect(run.lastActivityAt).toBeGreaterThan(lastActivityBefore ?? 0);+ expect(run.lastActivityMonotonicAt).toBeGreaterThan(lastActivityMonotonicBefore ?? 0);const streamedSnapshot = session.getRlmChildSnapshots().find((candidate) => candidate.id === handle.rlm_child_id);expect(streamedSnapshot?.activityStaleMs).toBeUndefined();+ expect(streamedSnapshot?.answerPreview).toMatch(/saturationxxx$/);- // Real activity still emits: a changed preview, then a tool call.+ // Past the window a delta emits again; the preview follows the tail, never the head.+ run.lastStreamedUpdateMonotonicAt = performance.now() - RLM_CHILD_UPDATE_MIN_INTERVAL_MS - 1;const changed = childUpdates.length;- emitChild({ type: "message_update", message: assistantMessage(`changed ${saturatedText}`) });+ emitChild({+ type: "message_update",+ message: assistantMessage(`head-marker${"x".repeat(400)}changed tail${"y".repeat(60)}`),+ });await waitFor(() => childUpdates.length > changed);- expect(childUpdates.at(-1)?.preview).toContain("changed");+ expect(childUpdates.at(-1)?.preview).toMatch(/changed taily{60}$/);+ expect(childUpdates.at(-1)?.preview).not.toContain("head-marker");const beforeTool = childUpdates.length;emitChild({ type: "tool_execution_start", toolCallId: "tool-1", toolName: "ipython", args: {} });