Skip to content

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

Rust

return to agents view when leaving a subagent opened from it (#271)

ktf0b6c535c1e3
diff --git a/packages/coding-agent/src/modes/agents-view/agents-view-mode.ts b/packages/coding-agent/src/modes/agents-view/agents-view-mode.ts
index 0c56f1c007..bf89a69f16 100644
--- a/packages/coding-agent/src/modes/agents-view/agents-view-mode.ts
+++ b/packages/coding-agent/src/modes/agents-view/agents-view-mode.ts
@@ -92,10 +92,22 @@ export interface AgentsViewModeOptions {
verbose?: boolean;
}
-type AgentsViewRunResult = { type: "exit" } | { type: "open"; summary: SessionSummary; subagent?: SessionSummary };
+type AgentsViewRunResult =
+ | { type: "exit" }
+ | {
+ type: "open";
+ summary: SessionSummary;
+ subagent?: SessionSummary;
+ // Session ids of every ancestor that must be expanded to reveal the
+ // opened subagent, from the root agent down to its immediate parent.
+ subagentAncestorSessionIds?: string[];
+ };
type AgentsViewPersistentState = {
selectedRowIdentity?: string;
selectedSessionKey?: AgentsViewSelectionKey;
+ // Ancestor chain to re-expand on return to a subagent. Kept by sessionId, not
+ // row identity, so it survives a parent's active→persisted identity flip.
+ pendingExpandedAncestorSessionIds?: string[];
statusMessage?: string;
initialPromptsSent?: boolean;
// Gathered once and reused across agents-view instances so the notices survive
@@ -232,8 +244,17 @@ export async function runAgentsViewMode(options: AgentsViewModeOptions): Promise
if (result.type === "exit") {
return;
}
- persistentState.selectedRowIdentity = getSummaryIdentity(result.summary);
- persistentState.selectedSessionKey = getAgentsViewSelectionKey(result.summary);
+ if (result.subagent) {
+ // Returning from a subagent reopens the agents view with every ancestor
+ // list expanded and that subagent reselected.
+ persistentState.selectedRowIdentity = getSummaryIdentity(result.subagent);
+ persistentState.selectedSessionKey = getAgentsViewSelectionKey(result.subagent);
+ persistentState.pendingExpandedAncestorSessionIds = result.subagentAncestorSessionIds ?? [];
+ } else {
+ persistentState.selectedRowIdentity = getSummaryIdentity(result.summary);
+ persistentState.selectedSessionKey = getAgentsViewSelectionKey(result.summary);
+ persistentState.pendingExpandedAncestorSessionIds = undefined;
+ }
let opened: { connection: DaemonAgentConnection; summary: SessionSummary } | undefined;
try {
@@ -706,6 +727,33 @@ class AgentsViewMode implements Component, Focusable {
this.ui.requestRender();
}
+ // Resolve the persisted sessionId breadcrumb to live row identities once, so
+ // the rest of the expansion lifecycle stays uniformly identity-keyed.
+ private applyPendingAncestorExpansion(): void {
+ const sessionIds = this.persistentState.pendingExpandedAncestorSessionIds;
+ if (!sessionIds || sessionIds.length === 0) {
+ this.persistentState.pendingExpandedAncestorSessionIds = undefined;
+ return;
+ }
+ this.persistentState.pendingExpandedAncestorSessionIds = undefined;
+ const wanted = new Set(sessionIds);
+ // A nested ancestor's row only appears once its own parent is expanded, so
+ // expand-and-rebuild until a pass reveals nothing new.
+ let added = true;
+ while (added) {
+ added = false;
+ for (const row of this.rows) {
+ if (wanted.has(row.summary.sessionId) && !this.expandedSubagentParents.has(row.identity)) {
+ this.expandedSubagentParents.add(row.identity);
+ added = true;
+ }
+ }
+ if (added) {
+ this.rebuildRows();
+ }
+ }
+ }
+
/** Expanded subagent lists collapse back to their summary row once selection leaves them. */
private collapseSubagentListsOutsideSelection(): void {
if (this.expandedSubagentParents.size === 0) {
@@ -1061,7 +1109,27 @@ class AgentsViewMode implements Component, Focusable {
this.setStatusMessage("Cannot open subagent without its parent agent");
return;
}
- this.finish({ type: "open", summary: root.summary, subagent: row.summary });
+ this.finish({
+ type: "open",
+ summary: root.summary,
+ subagent: row.summary,
+ subagentAncestorSessionIds: this.collectSubagentAncestorSessionIds(row),
+ });
+ }
+
+ /** Session ids of every ancestor of a subagent row, root-most first. */
+ private collectSubagentAncestorSessionIds(row: AgentsViewRow): string[] {
+ const ancestors: string[] = [];
+ let parentIdentity = row.parentIdentity;
+ while (parentIdentity !== undefined) {
+ const parent = this.rows.find((candidate) => candidate.identity === parentIdentity);
+ if (!parent) {
+ break;
+ }
+ ancestors.unshift(parent.summary.sessionId);
+ parentIdentity = parent.parentIdentity;
+ }
+ return ancestors;
}
/**
@@ -1485,6 +1553,7 @@ class AgentsViewMode implements Component, Focusable {
this.expandedSubagentParents,
this.programShownParents,
);
+ this.applyPendingAncestorExpansion();
this.restoreSelection();
this.ui.requestRender();
} catch (error) {
diff --git a/packages/coding-agent/src/modes/interactive/components/child-agent-inspector.ts b/packages/coding-agent/src/modes/interactive/components/child-agent-inspector.ts
index 5ce108203b..43297c2c21 100644
--- a/packages/coding-agent/src/modes/interactive/components/child-agent-inspector.ts
+++ b/packages/coding-agent/src/modes/interactive/components/child-agent-inspector.ts
@@ -592,6 +592,7 @@ export class ChildAgentDetailComponent implements Component, Focusable {
private toolsExpanded = false;
private killConfirmExpiresAt = 0;
private killConfirmTimer: ReturnType<typeof setTimeout> | undefined;
+ private backHintLabel = "back to chat";
onCancel?: () => void;
onToggleToolsExpanded?: () => void;
@@ -613,6 +614,10 @@ export class ChildAgentDetailComponent implements Component, Focusable {
this.rebuildTranscriptComponents();
}
+ setBackHintLabel(label: string): void {
+ this.backHintLabel = label;
+ }
+
setToolsExpanded(expanded: boolean): void {
this.toolsExpanded = expanded;
for (const component of this.transcriptComponents) {
@@ -848,7 +853,7 @@ export class ChildAgentDetailComponent implements Component, Focusable {
? theme.fg("error", `${keyText("app.agents.delete", { primaryOnly: true }).trim()} again to stop`)
: keyAction("app.agents.delete", "stop", { primaryOnly: true });
return hintLine(
- [keyAction("app.agents.back", "back to chat", { primaryOnly: true }), expandAction, stopAction],
+ [keyAction("app.agents.back", this.backHintLabel, { primaryOnly: true }), expandAction, stopAction],
width,
);
}
diff --git a/packages/coding-agent/src/modes/interactive/interactive-mode.ts b/packages/coding-agent/src/modes/interactive/interactive-mode.ts
index 84a12523af..a8e06854d1 100644
--- a/packages/coding-agent/src/modes/interactive/interactive-mode.ts
+++ b/packages/coding-agent/src/modes/interactive/interactive-mode.ts
@@ -565,6 +565,7 @@ export class InteractiveMode {
private childAgentNodes: ChildAgentInspectorNode[] = [];
private childAgentDetailNodeId: string | undefined;
private childAgentPanelMode: "detail" | undefined;
+ private enteredSessionViaSubagentDetail = false;
// Tool output expansion state
private toolOutputExpanded = false;
@@ -686,7 +687,13 @@ export class InteractiveMode {
getHideThinkingBlock: () => this.hideThinkingBlock,
getHiddenThinkingLabel: () => this.hiddenThinkingLabel,
});
- this.childAgentDetail.onCancel = () => this.closeChildAgentPanel({ selectNodeId: this.childAgentDetailNodeId });
+ this.childAgentDetail.onCancel = () => {
+ if (this.options.returnToAgentsView && this.enteredSessionViaSubagentDetail) {
+ void this.returnToAgentsView();
+ return;
+ }
+ this.closeChildAgentPanel({ selectNodeId: this.childAgentDetailNodeId });
+ };
this.childAgentDetail.onToggleToolsExpanded = () => this.toggleToolOutputExpansion();
this.childAgentDetail.onKill = (nodeId) => void this.killChildAgent(nodeId);
this.widgetContainerAbove = new Container();
@@ -978,11 +985,18 @@ export class InteractiveMode {
// Jump straight into a subagent's read-only detail view when the agents
// view opened this session targeting one of its subagents.
- if (this.options.initialSubagentNodeId && !this.openChildAgentDetail(this.options.initialSubagentNodeId)) {
- // The subagent can finish and get released between the agents view
- // listing it and this session attaching; say so instead of silently
- // landing in the parent chat.
- this.showStatus("Subagent already finished; showing its parent session");
+ if (this.options.initialSubagentNodeId) {
+ if (this.openChildAgentDetail(this.options.initialSubagentNodeId)) {
+ if (this.options.returnToAgentsView) {
+ this.enteredSessionViaSubagentDetail = true;
+ this.childAgentDetail.setBackHintLabel("back to agents");
+ }
+ } else {
+ // The subagent can finish and get released between the agents view
+ // listing it and this session attaching; say so instead of silently
+ // landing in the parent chat.
+ this.showStatus("Subagent already finished; showing its parent session");
+ }
}
// Set up theme file watcher
@@ -3105,6 +3119,8 @@ export class InteractiveMode {
// Re-render queued previews cleared on panel entry; the queue may still hold messages.
this.updatePendingMessagesDisplay();
this.childAgentDetailNodeId = undefined;
+ this.enteredSessionViaSubagentDetail = false;
+ this.childAgentDetail.setBackHintLabel("back to chat");
this.childAgentDetail.setNode(undefined);
}
this.childAgentPanelMode = undefined;
@@ -4652,6 +4668,8 @@ export class InteractiveMode {
private closeChildAgentPanel(options: { selectNodeId?: string } = {}): void {
this.childAgentPanelMode = undefined;
this.childAgentDetailNodeId = undefined;
+ this.enteredSessionViaSubagentDetail = false;
+ this.childAgentDetail.setBackHintLabel("back to chat");
this.childAgentDetail.setNode(undefined);
this.childAgentSummary.setHidden(false);
this.restoreMainAgentView();
diff --git a/packages/coding-agent/test/agents-view-state.test.ts b/packages/coding-agent/test/agents-view-state.test.ts
index 7ecfc3e293..4a754f7fe8 100644
--- a/packages/coding-agent/test/agents-view-state.test.ts
+++ b/packages/coding-agent/test/agents-view-state.test.ts
@@ -193,6 +193,58 @@ describe("agents view state", () => {
expect(expanded.slice(1).every((row) => row.selectable && row.parentIdentity === parentIdentity)).toBe(true);
});
+ test("reveals a nested subagent only after its parent is also expanded", () => {
+ const summaries = [
+ makeSummary({
+ id: "grandchild-active",
+ activeSessionId: "grandchild-active",
+ sessionId: "grandchild-session",
+ sessionName: "Grandchild",
+ runtimeKind: "subagent",
+ parentActiveSessionId: "child-active",
+ parentSessionId: "child-session",
+ isStreaming: true,
+ activity: "working",
+ }),
+ makeSummary({
+ id: "child-active",
+ activeSessionId: "child-active",
+ sessionId: "child-session",
+ sessionName: "Child",
+ runtimeKind: "subagent",
+ parentActiveSessionId: "root-active",
+ parentSessionId: "root-session",
+ isStreaming: true,
+ activity: "working",
+ }),
+ makeSummary({
+ id: "root-active",
+ activeSessionId: "root-active",
+ sessionId: "root-session",
+ sessionName: "Root",
+ isStreaming: true,
+ activity: "working",
+ }),
+ ];
+
+ const rootIdentity = buildAgentsViewRows(summaries)[0]?.identity ?? "";
+ // Expanding only the root reveals the child but not the grandchild.
+ const oneLevel = buildAgentsViewRows(summaries, new Set([rootIdentity]));
+ expect(oneLevel.map((row) => [row.title, row.kind])).toEqual([
+ ["Root", "agent"],
+ ["Child", "subagent"],
+ ["1 subagent running", "subagent-summary"],
+ ]);
+
+ const childIdentity = oneLevel.find((row) => row.title === "Child")?.identity ?? "";
+ const twoLevel = buildAgentsViewRows(summaries, new Set([rootIdentity, childIdentity]));
+ expect(twoLevel.map((row) => [row.title, row.kind, row.depth])).toEqual([
+ ["Root", "agent", 0],
+ ["Child", "subagent", 1],
+ ["Grandchild", "subagent", 2],
+ ]);
+ });
+
test("keeps finished subagents reachable via the summary row", () => {
const rows = buildAgentsViewRows([
makeSummary({
diff --git a/packages/coding-agent/test/daemon-list-format.test.ts b/packages/coding-agent/test/daemon-list-format.test.ts
index c18b5a617e..66d5c58d43 100644
--- a/packages/coding-agent/test/daemon-list-format.test.ts
+++ b/packages/coding-agent/test/daemon-list-format.test.ts
@@ -10,9 +10,19 @@ describe("formatSessionListTable", () => {
const table = stripAnsi(
formatSessionListTable(
[
- makeSummary({ name: "sleep", id: "019e71ec-e08a-75a9-b573-fc10e9f8380f", lifecycle: "archived", activity: "idle" }),
+ makeSummary({
+ name: "sleep",
+ id: "019e71ec-e08a-75a9-b573-fc10e9f8380f",
+ lifecycle: "archived",
+ activity: "idle",
+ }),
makeSummary({ name: "tool", id: "ccccddddeeee", lifecycle: "live", activity: "working" }),
- makeSummary({ name: "crash", id: "019e71ec-e08a-75a9-b573-abcdef123456", lifecycle: "archived", activity: "idle" }),
+ makeSummary({
+ name: "crash",
+ id: "019e71ec-e08a-75a9-b573-abcdef123456",
+ lifecycle: "archived",
+ activity: "idle",
+ }),
makeSummary({ name: "idle", id: "bbbbccccdddd", lifecycle: "live", activity: "idle" }),
makeSummary({ name: "model", id: "ddddeeeeffff", lifecycle: "live", activity: "working" }),
makeSummary({