A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
feat(coding-agent): shared agent-status classifier, honest worker visibility, subagents bar rename (#1895)
Sebastian Müller4e42fab2ce0c
diff --git a/packages/coding-agent/.changes/eng-5794-agent-status-classifier.md b/packages/coding-agent/.changes/eng-5794-agent-status-classifier.mdnew file mode 100644index 0000000000..a7fe277e44--- /dev/null+++ b/packages/coding-agent/.changes/eng-5794-agent-status-classifier.md@@ -0,0 +1,3 @@+- Fixed the agents view hiding running subagents whose worker is starting or recovering; the worker state now shows as the row's status label.+- Made spawned subagent sessions visible from creation, before their first message lands.+- Renamed the subagent summary bar label from "agents" to "subagents" and unified the status formula behind both surfaces.diff --git a/packages/coding-agent/src/modes/agents-view/agents-view-state.ts b/packages/coding-agent/src/modes/agents-view/agents-view-state.tsindex b29531360b..1e9fa13103 100644--- a/packages/coding-agent/src/modes/agents-view/agents-view-state.ts+++ b/packages/coding-agent/src/modes/agents-view/agents-view-state.ts@@ -103,12 +103,11 @@ export function classifyUnifiedSession(record: Pick<UnifiedSessionRecord, "daemoreturn classifyAgentsViewSession(record.daemon);}-// Live sessions only; drafts and archived stay out.export function shouldShowAgentsViewSession(summary: SessionSummary, manuallyInactive = false): boolean {if (manuallyInactive) {return false;}- return summary.lifecycle === "live" && (summary.workerState === undefined || summary.workerState === "ready");+ return summary.lifecycle === "live";}export function sectionTitle(section: AgentsViewSection): string {@@ -960,6 +959,10 @@ function getSessionSubtitle(summary: SessionSummary): string {}function getSessionStatusLabel(summary: SessionSummary, hasActiveHeartbeat = summary.hasActiveHeartbeat): string {+ // A non-ready worker cannot report fresh runtime flags; its state is the row's story.+ if (summary.workerState !== undefined && summary.workerState !== "ready") {+ return summary.workerState;+ }if (summary.isCompacting) {return "compacting";}diff --git a/packages/coding-agent/src/modes/daemon/agent-roster.ts b/packages/coding-agent/src/modes/daemon/agent-roster.tsnew file mode 100644index 0000000000..5c5e14ee84--- /dev/null+++ b/packages/coding-agent/src/modes/daemon/agent-roster.ts@@ -0,0 +1,18 @@+// One status formula shared by every agent surface; surfaces adapt their inputs and never reimplement it.+export type AgentRosterStatus = "running" | "idle" | "inactive";++export interface AgentStatusInput {+ /** A live runtime exists for the agent. */+ resident: boolean;+ /** Admitted child run whose session has not materialized yet. */+ queuedChild: boolean;+ /** Actively working: streaming, running tools/bash, or running children. */+ busy: boolean;+ hasActiveHeartbeat: boolean;+}++export function classifyAgentStatus(input: AgentStatusInput): AgentRosterStatus {+ if (input.queuedChild) return "running";+ if (!input.resident) return "inactive";+ return input.busy || input.hasActiveHeartbeat ? "running" : "idle";+}diff --git a/packages/coding-agent/src/modes/daemon/daemon-session-list.ts b/packages/coding-agent/src/modes/daemon/daemon-session-list.tsindex 8d6eeff5cc..85ae56b6ec 100644--- a/packages/coding-agent/src/modes/daemon/daemon-session-list.ts+++ b/packages/coding-agent/src/modes/daemon/daemon-session-list.ts@@ -9,6 +9,7 @@ import type { SessionActionSnapshot } from "../../core/session-action-store.js";import type { AgentTaskState, SessionInfo } from "../../core/session-manager.js";import type { AgentConnectionRlmChildAgentSnapshot } from "../agent-connection/types.js";import type { ActiveSessionState } from "./active-session-state.js";+import { type AgentRosterStatus, classifyAgentStatus } from "./agent-roster.js";// Durable lifecycle; decides agents-view visibility. Only "live" is shown.// "draft" = no message sent yet (discarded on close); "archived" = ctrl+x'd,@@ -18,7 +19,6 @@ export type SessionLifecycle = "draft" | "live" | "archived";// Heuristic activity of a live session. Classification-in-flight counts as// "working" so the view never sees an unlabeled idle session.export type SessionActivity = "working" | "idle";-export type SessionRosterStatus = "running" | "idle" | "inactive";// Upper bound on the spawn-code source carried in a session summary. Generous// enough for real spawn cells while keeping the daemon wire payload bounded.@@ -100,10 +100,13 @@ export function resolveAttachModelFallbackMessage(return summary.model ? undefined : startupModelFallbackMessage;}-export function classifySessionRosterStatus(summary: SessionSummary): SessionRosterStatus {- if (!summary.activeSessionId) return "inactive";- if (summary.hasActiveHeartbeat || summary.activity === "working" || isSessionSummaryBusy(summary)) return "running";- return "idle";+export function classifySessionRosterStatus(summary: SessionSummary): AgentRosterStatus {+ return classifyAgentStatus({+ resident: !!summary.activeSessionId,+ queuedChild: false,+ busy: summary.activity === "working" || isSessionSummaryBusy(summary),+ hasActiveHeartbeat: summary.hasActiveHeartbeat === true,+ });}export function isSessionSummaryBusy(summary: SessionSummary): boolean {@@ -431,6 +434,8 @@ export function inactiveLifecycleForSession(session: SessionInfo): SessionLifecy}export function activeLifecycleForSession(activeSession: ActiveSessionState): SessionLifecycle {+ // A resident subagent is a spawned worker, not a user draft; it is visible before its first message lands.+ if (activeSession.runtime.metadata?.kind === "subagent") return "live";// Lifecycle drives agents-view visibility and is message-based: a session// becomes live once a message is sent. A message-less session is a draft (hidden// from the view) even if the user changed its model/name first — that config isdiff --git a/packages/coding-agent/src/modes/interactive/components/subagent-summary-line.ts b/packages/coding-agent/src/modes/interactive/components/subagent-summary-line.tsindex 69af418950..73ac870d4b 100644--- a/packages/coding-agent/src/modes/interactive/components/subagent-summary-line.ts+++ b/packages/coding-agent/src/modes/interactive/components/subagent-summary-line.ts@@ -1,5 +1,6 @@import { type Component, type Focusable, getKeybindings, truncateToWidth, visibleWidth } from "@earendil-works/pi-tui";import type { AgentConnectionRlmChildAgentSnapshot } from "../../agent-connection/index.js";+import { type AgentRosterStatus, classifyAgentStatus } from "../../daemon/agent-roster.js";import { theme } from "../theme/theme.js";import { keyText } from "./keybinding-hints.js";@@ -10,29 +11,33 @@ export interface SubagentSummaryCounts {inactive: number;}+export function classifySubagentSnapshotStatus(+ child: AgentConnectionRlmChildAgentSnapshot,+ activeHeartbeatSessionIds: ReadonlySet<string>,+): AgentRosterStatus {+ // Activity implies a live session; the in-process connection never stamps activeSessionId.+ const resident = child.activeSessionId !== undefined || child.activity !== undefined;+ const busy = child.status === "running" || child.status === "queued" || child.activity !== undefined;+ return classifyAgentStatus({+ resident,+ queuedChild: !resident && busy,+ busy,+ hasActiveHeartbeat: child.activeSessionId !== undefined && activeHeartbeatSessionIds.has(child.activeSessionId),+ });+}+export function countDirectSubagentStatuses(children: Iterable<AgentConnectionRlmChildAgentSnapshot>,parentId: string | undefined,activeHeartbeatSessionIds: ReadonlySet<string>,): SubagentSummaryCounts {- let total = 0;- let running = 0;- let idle = 0;+ const counts: SubagentSummaryCounts = { total: 0, running: 0, idle: 0, inactive: 0 };for (const child of children) {if (child.parentId !== parentId || child.status === "cancelled") continue;- total += 1;- const isRunning =- child.status === "running" ||- child.status === "queued" ||- child.activity !== undefined ||- (child.activeSessionId !== undefined && activeHeartbeatSessionIds.has(child.activeSessionId));- if (isRunning) {- running += 1;- } else if ((child.status === "done" || child.status === "error") && child.activeSessionId !== undefined) {- idle += 1;- }+ counts.total += 1;+ counts[classifySubagentSnapshotStatus(child, activeHeartbeatSessionIds)] += 1;}- return { total, running, idle, inactive: total - running - idle };+ return counts;}/** One-line entry into the current session's scoped agents view. */@@ -86,9 +91,9 @@ export class SubagentSummaryLine implements Component, Focusable {if (width < 2) return lines;const safeWidth = width;const inner = safeWidth - 2;- const label = theme.fg("accent", "[1magents[22m");+ const label = theme.fg("accent", "[1msubagents[22m");const top = truncateToWidth(- `${theme.fg("border", "╭─ ")}${label}${theme.fg("border", ` ${"─".repeat(Math.max(0, inner - 9))}╮`)}`,+ `${theme.fg("border", "╭─ ")}${label}${theme.fg("border", ` ${"─".repeat(Math.max(0, inner - 3 - visibleWidth(label)))}╮`)}`,safeWidth,"…",);diff --git a/packages/coding-agent/test/agent-roster.test.ts b/packages/coding-agent/test/agent-roster.test.tsnew file mode 100644index 0000000000..0ea00f88e3--- /dev/null+++ b/packages/coding-agent/test/agent-roster.test.ts@@ -0,0 +1,70 @@+import { describe, expect, it } from "vitest";+import type { AgentConnectionRlmChildAgentSnapshot } from "../src/modes/agent-connection/types.js";+import { classifyAgentStatus } from "../src/modes/daemon/agent-roster.js";+import { classifySessionRosterStatus, type SessionSummary } from "../src/modes/daemon/daemon-session-list.js";+import { classifySubagentSnapshotStatus } from "../src/modes/interactive/components/subagent-summary-line.js";++function summaryFor(resident: boolean, busy: boolean, heartbeat: boolean): SessionSummary {+ return {+ id: "s-1",+ ...(resident ? { activeSessionId: "as-1" } : {}),+ lifecycle: "live",+ activity: "idle",+ isSessionActive: busy,+ ...(heartbeat ? { hasActiveHeartbeat: true } : {}),+ sessionId: "session-1",+ cwd: "/tmp/project",+ isStreaming: busy,+ isCompacting: false,+ attachedClients: 0,+ messageCount: 1,+ sessionActions: { queuedCount: 0, steering: [], followUps: [] },+ };+}++function childFor(resident: boolean, busy: boolean): AgentConnectionRlmChildAgentSnapshot {+ return {+ id: "child-1",+ label: "child-1",+ status: busy ? "running" : "done",+ sessionDir: "/tmp/child-1",+ ...(resident ? { activeSessionId: "as-1" } : {}),+ };+}++describe("classifyAgentStatus", () => {+ it("classifies once and both surface adapters agree with it", () => {+ // The formula's three defining rows: a queued child runs before any session+ // exists, nothing else resurrects a non-resident agent, residents split on work.+ expect(classifyAgentStatus({ resident: false, queuedChild: true, busy: false, hasActiveHeartbeat: false })).toBe(+ "running",+ );+ expect(classifyAgentStatus({ resident: false, queuedChild: false, busy: true, hasActiveHeartbeat: true })).toBe(+ "inactive",+ );+ expect(classifySubagentSnapshotStatus({ ...childFor(false, false), status: "queued" }, new Set())).toBe(+ "running",+ );+ for (const busy of [false, true]) {+ for (const heartbeat of [false, true]) {+ const expected = classifyAgentStatus({+ resident: true,+ queuedChild: false,+ busy,+ hasActiveHeartbeat: heartbeat,+ });+ expect(expected).toBe(busy || heartbeat ? "running" : "idle");+ const heartbeatIds = new Set(heartbeat ? ["as-1"] : []);+ expect(classifySessionRosterStatus(summaryFor(true, busy, heartbeat)), `busy=${busy} hb=${heartbeat}`).toBe(+ expected,+ );+ expect(+ classifySubagentSnapshotStatus(childFor(true, busy), heartbeatIds),+ `busy=${busy} hb=${heartbeat}`,+ ).toBe(expected);+ }+ }+ expect(classifySessionRosterStatus(summaryFor(false, false, false))).toBe("inactive");+ expect(classifySubagentSnapshotStatus(childFor(false, false), new Set())).toBe("inactive");+ });+});diff --git a/packages/coding-agent/test/agents-view-state.test.ts b/packages/coding-agent/test/agents-view-state.test.tsindex 1466f6e23d..799515b1ef 100644--- a/packages/coding-agent/test/agents-view-state.test.ts+++ b/packages/coding-agent/test/agents-view-state.test.ts@@ -816,6 +816,15 @@ describe("agents view state", () => {expect(shouldShowAgentsViewSession(makeSummary({ lifecycle: "live", activity: "idle" }), true)).toBe(false);});+ test("keeps sessions of non-ready workers visible and labels them with the worker state", () => {+ for (const workerState of ["starting", "recovering", "stopping", "failed"] as const) {+ const summary = makeSummary({ lifecycle: "live", workerState });+ expect(shouldShowAgentsViewSession(summary), workerState).toBe(true);+ expect(buildAgentsViewRows([summary])[0]?.statusLabel, workerState).toBe(workerState);+ }+ expect(buildAgentsViewRows([makeSummary({ workerState: "ready" })])[0]?.statusLabel).toBe("needs input");+ });+test("does not override saved session cwd when reopening inactive agents", () => {const config: AgentSessionRuntimeConfig = {cwd: "/tmp/dashboard",diff --git a/packages/coding-agent/test/daemon-session-list.test.ts b/packages/coding-agent/test/daemon-session-list.test.tsindex a6b52745af..7b6f9cc46b 100644--- a/packages/coding-agent/test/daemon-session-list.test.ts+++ b/packages/coding-agent/test/daemon-session-list.test.ts@@ -328,6 +328,24 @@ describe("buildSessionList", () => {expect(entries[0]!.sessionName).toBe("session active-1");});+ it("keeps a resident message-less subagent live while a top-level one stays a draft", () => {+ const entries = buildSessionList(+ [+ makeState({+ activeSessionId: "child",+ metadata: { kind: "subagent", createdAt: 1, rlmChildId: "child-1" },+ }),+ makeState({ activeSessionId: "top" }),+ ],+ [],+ );++ expect(entries.map((entry) => [entry.id, entry.lifecycle])).toEqual([+ ["child", "live"],+ ["top", "draft"],+ ]);+ });+it("treats a message-less on-disk active session as a hidden draft", () => {const emptyPath = resolve("/tmp/project/empty.jsonl");const usedPath = resolve("/tmp/project/used.jsonl");diff --git a/packages/coding-agent/test/subagent-summary-line.test.ts b/packages/coding-agent/test/subagent-summary-line.test.tsindex 71037d7a6b..467f393f0f 100644--- a/packages/coding-agent/test/subagent-summary-line.test.ts+++ b/packages/coding-agent/test/subagent-summary-line.test.ts@@ -31,7 +31,7 @@ describe("SubagentSummaryLine", () => {line.setSubagentCounts({ total: 1, running: 1, idle: 0, inactive: 0 });let rendered = line.render(120).map(stripAnsi);expect(rendered).toHaveLength(3);- expect(rendered[0]).toContain("╭─ agents ─");+ expect(rendered[0]).toContain("╭─ subagents ─");expect(rendered[1]).toContain("● 1 running ◐ 0 idle ○ 0 inactive");line.setSubagentCounts({ total: 2, running: 1, idle: 1, inactive: 0 });@@ -206,7 +206,7 @@ describe("SubagentSummaryLine", () => {Reflect.set(mode, "rlmNodeId", "me");seed.call(mode, [worker]);- expect(stripAnsi(line.render(100).join("\n"))).toContain("╭─ agents ─");+ expect(stripAnsi(line.render(100).join("\n"))).toContain("╭─ subagents ─");});it("clears a resident session id when a terminal update reports an evicted child", () => {