A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
scope heartbeat display to agent sessions (#472)
kt1933216ff0e5
diff --git a/packages/coding-agent/CHANGELOG.md b/packages/coding-agent/CHANGELOG.mdindex 23327ba124..8e3ebc6895 100644--- a/packages/coding-agent/CHANGELOG.md+++ b/packages/coding-agent/CHANGELOG.md@@ -3,6 +3,7 @@## [Unreleased]- Fixed agents with heartbeat-owning subagents appearing completed and showing completion checkmarks in the Agents View.+- Fixed the heartbeat tray and manager showing heartbeats from unrelated sessions.- Fixed daemon backpressure triggering redundant catch-up snapshots for events already queued by the socket.- Fixed incompatible daemon builds crashing startup or respawning after shutdown, with capability negotiation, verified provenance, and convergent force shutdown ([ENG-4687](https://linear.app/primeintellect/issue/ENG-4687/make-daemon-version-mismatches-self-healing)).- Changed tool-result and announcement images to show compact metadata instead of terminal graphics ([#437](https://github.com/PrimeIntellect-ai/prime-agent/pull/437) by [@snimu](https://github.com/snimu)).diff --git a/packages/coding-agent/src/modes/interactive/heartbeat-scope.ts b/packages/coding-agent/src/modes/interactive/heartbeat-scope.tsnew file mode 100644index 0000000000..82d60d5176--- /dev/null+++ b/packages/coding-agent/src/modes/interactive/heartbeat-scope.ts@@ -0,0 +1,31 @@+import type { AgentConnectionHeartbeat, AgentConnectionRlmChildAgentSnapshot } from "../agent-connection/types.js";++interface HeartbeatSessionIdentity {+ activeSessionId?: string;+ sessionId: string;+}++export function scopeHeartbeatsToSession(+ heartbeats: readonly AgentConnectionHeartbeat[],+ session: HeartbeatSessionIdentity | undefined,+ children: Iterable<Pick<AgentConnectionRlmChildAgentSnapshot, "activeSessionId">>,+): AgentConnectionHeartbeat[] {+ if (!session) {+ return [];+ }++ const activeSessionIds = new Set<string>();+ if (session.activeSessionId) {+ activeSessionIds.add(session.activeSessionId);+ }+ for (const child of children) {+ if (child.activeSessionId) {+ activeSessionIds.add(child.activeSessionId);+ }+ }++ return heartbeats.filter(+ (heartbeat) =>+ heartbeat.job.sessionId === session.sessionId || activeSessionIds.has(heartbeat.job.activeSessionId),+ );+}diff --git a/packages/coding-agent/src/modes/interactive/interactive-mode.ts b/packages/coding-agent/src/modes/interactive/interactive-mode.tsindex e98c2e3a61..218b164dcf 100644--- a/packages/coding-agent/src/modes/interactive/interactive-mode.ts+++ b/packages/coding-agent/src/modes/interactive/interactive-mode.ts@@ -206,6 +206,7 @@ import { TreeSelectorComponent } from "./components/tree-selector.js";import { UserMessageComponent } from "./components/user-message.js";import { UserMessageSelectorComponent } from "./components/user-message-selector.js";import { FeatureHintDeck } from "./feature-hints.js";+import { scopeHeartbeatsToSession } from "./heartbeat-scope.js";import { collectMarkedImages, evictImagesToBudget, formatImageMarker, imageMarkerIds } from "./image-markers.js";import type {InteractiveModeLocalSessionHost,@@ -339,6 +340,7 @@ function childAgentSummaryChanged(const nextTokens = next.tokenCount === undefined ? undefined : formatTokenCount(next.tokenCount);return (previous.parentId !== next.parentId ||+ previous.activeSessionId !== next.activeSessionId ||previous.sessionName !== next.sessionName ||previous.model !== next.model ||previous.label !== next.label ||@@ -830,6 +832,7 @@ export class InteractiveMode {private connectionState: AgentConnectionState | undefined;private connectionResourceSnapshot: AgentConnectionResourceSnapshot | undefined;private sessionHasMessages = false;+ private heartbeatCatalog: AgentConnectionHeartbeat[] = [];private heartbeats: AgentConnectionHeartbeat[] = [];private heartbeatRefreshPromise: Promise<void> | undefined;private heartbeatRefreshRequested = false;@@ -2363,6 +2366,22 @@ export class InteractiveMode {}private applyHeartbeatCatalog(heartbeats: AgentConnectionHeartbeat[]): void {+ this.heartbeatCatalog = heartbeats;+ this.updateScopedHeartbeats();+ }++ private updateScopedHeartbeats(): void {+ const heartbeats = scopeHeartbeatsToSession(+ this.heartbeatCatalog,+ this.connectionState,+ this.childAgentSnapshots.values(),+ );+ if (+ heartbeats.length === this.heartbeats.length &&+ heartbeats.every((heartbeat, index) => heartbeat === this.heartbeats[index])+ ) {+ return;+ }this.heartbeats = heartbeats;this.heartbeatManager?.setHeartbeats(heartbeats);this.scheduleHeartbeatManagerRefresh();@@ -2373,6 +2392,7 @@ export class InteractiveMode {private applyConnectionStateSnapshot(state: AgentConnectionState): void {this.bindPromptStashSession(state.sessionId);this.connectionState = state;+ this.updateScopedHeartbeats();// Don't touch contextUsageTokenBaseline: a mid-stream snapshot reflects only completed// turns (the in-flight message isn't persisted yet), so the in-flight delta must keep// accumulating. The baseline is managed at turn end (refreshConnectionContextUsage) and@@ -5261,6 +5281,7 @@ export class InteractiveMode {private refreshChildAgentInspector(): void {this.childAgentNodes = this.buildChildAgentInspectorNodes();this.childAgentSummary.setNodes(this.childAgentNodes);+ this.updateScopedHeartbeats();this.updateWorkingPulse();this.syncWorkingLoader();this.updateWorkingLoaderMessage();@@ -5297,6 +5318,7 @@ export class InteractiveMode {this.childAgentSnapshots.clear();this.childAgentNodes = [];this.childAgentSummary.setNodes([]);+ this.updateScopedHeartbeats();this.childAgentSummary.setHidden(false);this.childAgentDetail.setNode(undefined);this.childAgentDetailNodeId = undefined;@@ -8911,7 +8933,7 @@ export class InteractiveMode {if (updated.source === "heartbeat" && updated.activeSessionId === this.connectionState?.activeSessionId) {this.patchConnectionState({ heartbeat: action === "stop" ? null : updated });}- const remaining = this.heartbeats.filter((entry) => entry.job.id !== updated.id);+ const remaining = this.heartbeatCatalog.filter((entry) => entry.job.id !== updated.id);this.applyHeartbeatCatalog(updated.status === "active" || updated.status === "paused"? [...remaining, { ...heartbeat, job: updated }]diff --git a/packages/coding-agent/test/interactive-heartbeat-management.test.ts b/packages/coding-agent/test/interactive-heartbeat-management.test.tsindex 5400640204..88bd8948ba 100644--- a/packages/coding-agent/test/interactive-heartbeat-management.test.ts+++ b/packages/coding-agent/test/interactive-heartbeat-management.test.ts@@ -1,9 +1,13 @@import { describe, expect, it, vi } from "vitest";import type { AgentCronJob, AgentHeartbeatManagementAction } from "../src/core/cron-jobs.js";-import type { AgentConnectionHeartbeat } from "../src/modes/agent-connection/types.js";+import type {+ AgentConnectionHeartbeat,+ AgentConnectionRlmChildAgentSnapshot,+} from "../src/modes/agent-connection/types.js";import { InteractiveMode } from "../src/modes/interactive/interactive-mode.js";interface HeartbeatManagementHarness {+ heartbeatCatalog: AgentConnectionHeartbeat[];heartbeats: AgentConnectionHeartbeat[];agentConnection: {manageHeartbeat(@@ -19,6 +23,25 @@ interface HeartbeatManagementHarness {manageHeartbeat(heartbeat: AgentConnectionHeartbeat, action: AgentHeartbeatManagementAction): Promise<void>;}+interface HeartbeatScopeHarness {+ heartbeatCatalog: AgentConnectionHeartbeat[];+ heartbeats: AgentConnectionHeartbeat[];+ connectionState: { activeSessionId: string; sessionId: string };+ childAgentSnapshots: Map<string, AgentConnectionRlmChildAgentSnapshot>;+ heartbeatManager: { setHeartbeats(heartbeats: AgentConnectionHeartbeat[]): void } | undefined;+ childAgentSummary: { invalidate(): void };+ ui: { requestRender(): void };+ scheduleHeartbeatManagerRefresh(): void;+ applyHeartbeatCatalog(heartbeats: AgentConnectionHeartbeat[]): void;+}++interface ChildIdentityUpdateHarness {+ childAgentSnapshots: Map<string, AgentConnectionRlmChildAgentSnapshot>;+ childAgentDetailNodeId: string | undefined;+ refreshChildAgentInspector(): void;+ updateChildAgentInspector(child: AgentConnectionRlmChildAgentSnapshot): void;+}+interface HeartbeatRefreshHarness {heartbeats: AgentConnectionHeartbeat[];heartbeatManager: object | undefined;@@ -27,7 +50,7 @@ interface HeartbeatRefreshHarness {scheduleHeartbeatManagerRefresh(): void;}-function heartbeat(): AgentCronJob {+function heartbeat(overrides: Partial<AgentCronJob> = {}): AgentCronJob {return {id: "heartbeat-1",status: "active",@@ -42,6 +65,7 @@ function heartbeat(): AgentCronJob {updatedAt: "2026-01-01T00:00:00.000Z",nextRunAt: "2026-01-01T00:05:00.000Z",runCount: 0,+ ...overrides,};}@@ -52,6 +76,7 @@ describe("interactive heartbeat management", () => {const patches: Array<{ heartbeat: AgentCronJob | null }> = [];const harness = Object.create(InteractiveMode.prototype) as HeartbeatManagementHarness;harness.heartbeats = [{ job: current }];+ harness.heartbeatCatalog = harness.heartbeats;harness.connectionState = { activeSessionId: current.activeSessionId };harness.agentConnection = {manageHeartbeat: vi.fn(async () => stopped),@@ -72,6 +97,7 @@ describe("interactive heartbeat management", () => {const paused = { ...current, status: "paused" as const, nextRunAt: undefined };const harness = Object.create(InteractiveMode.prototype) as HeartbeatManagementHarness;harness.heartbeats = [{ job: current, sessionName: "Primary session" }];+ harness.heartbeatCatalog = harness.heartbeats;harness.connectionState = { activeSessionId: current.activeSessionId };harness.agentConnection = { manageHeartbeat: vi.fn(async () => paused) };harness.patchConnectionState = vi.fn();@@ -88,6 +114,60 @@ describe("interactive heartbeat management", () => {expect(harness.refreshHeartbeatCatalog).toHaveBeenCalledOnce();});+ it("scopes the catalog to the current session and its subagents", () => {+ const own = { job: heartbeat() };+ const child = {+ job: heartbeat({ id: "heartbeat-2", activeSessionId: "active-2", sessionId: "session-2" }),+ };+ const unrelated = {+ job: heartbeat({ id: "heartbeat-3", activeSessionId: "active-3", sessionId: "session-3" }),+ };+ const harness = Object.create(InteractiveMode.prototype) as HeartbeatScopeHarness;+ harness.heartbeatCatalog = [];+ harness.heartbeats = [];+ harness.connectionState = { activeSessionId: "active-1", sessionId: "session-1" };+ harness.childAgentSnapshots = new Map([+ [+ "child-1",+ {+ id: "child-1",+ activeSessionId: "active-2",+ label: "child",+ status: "running",+ sessionDir: "/tmp/child-1",+ },+ ],+ ]);+ harness.heartbeatManager = { setHeartbeats: vi.fn() };+ harness.childAgentSummary = { invalidate: vi.fn() };+ harness.ui = { requestRender: vi.fn() };+ harness.scheduleHeartbeatManagerRefresh = vi.fn();++ harness.applyHeartbeatCatalog([own, child, unrelated]);++ expect(harness.heartbeatCatalog).toEqual([own, child, unrelated]);+ expect(harness.heartbeats).toEqual([own, child]);+ expect(harness.heartbeatManager.setHeartbeats).toHaveBeenCalledWith([own, child]);+ });++ it("refreshes heartbeat scope when a known subagent gains its active session id", () => {+ const existing: AgentConnectionRlmChildAgentSnapshot = {+ id: "child-1",+ label: "child",+ status: "running",+ sessionDir: "/tmp/child-1",+ };+ const harness = Object.create(InteractiveMode.prototype) as ChildIdentityUpdateHarness;+ harness.childAgentSnapshots = new Map([[existing.id, existing]]);+ harness.childAgentDetailNodeId = undefined;+ harness.refreshChildAgentInspector = vi.fn();++ harness.updateChildAgentInspector({ ...existing, activeSessionId: "active-2" });++ expect(harness.childAgentSnapshots.get(existing.id)?.activeSessionId).toBe("active-2");+ expect(harness.refreshChildAgentInspector).toHaveBeenCalledOnce();+ });+it("refreshes an open manager after the next scheduled run", async () => {vi.useFakeTimers();try {