A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
preserve session selection when returning to agents view (#268)
kt8bd19857e140
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.tsindex 8fd2a214f5..d6eed13f94 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@@ -55,8 +55,11 @@ import {import {type AgentsViewRow,type AgentsViewSection,+ type AgentsViewSelectionKey,buildAgentsViewRows,+ getAgentsViewSelectionKey,getAgentsViewSummaryIdentity as getSummaryIdentity,+ resolveAgentsViewSelectionIndex,sectionTitle,shouldShowAgentsViewSession,} from "./agents-view-state.js";@@ -92,6 +95,7 @@ export interface AgentsViewModeOptions {type AgentsViewRunResult = { type: "exit" } | { type: "open"; summary: SessionSummary; subagent?: SessionSummary };type AgentsViewPersistentState = {selectedRowIdentity?: string;+ selectedSessionKey?: AgentsViewSelectionKey;statusMessage?: string;initialPromptsSent?: boolean;// Gathered once and reused across agents-view instances so the notices survive@@ -226,6 +230,7 @@ export async function runAgentsViewMode(options: AgentsViewModeOptions): Promisereturn;}persistentState.selectedRowIdentity = getSummaryIdentity(result.summary);+ persistentState.selectedSessionKey = getAgentsViewSelectionKey(result.summary);let opened: { connection: DaemonAgentConnection; summary: SessionSummary } | undefined;try {@@ -293,6 +298,7 @@ class AgentsViewMode implements Component, Focusable {private selectedIndex = 0;private selectedRowIdentity: string | undefined;private selectedActiveSessionId: string | undefined;+ private selectedSessionKey: AgentsViewSelectionKey | undefined;private replyActiveSessionId: string | undefined;private replyLastAssistantText: string | undefined;private replyLastAssistantTextLoading = false;@@ -313,6 +319,8 @@ class AgentsViewMode implements Component, Focusable {private readonly persistentState: AgentsViewPersistentState = {},) {this.selectedRowIdentity = persistentState.selectedRowIdentity;+ this.selectedSessionKey = persistentState.selectedSessionKey;+ this.selectedActiveSessionId = persistentState.selectedSessionKey?.activeSessionId;this.keybindings = KeybindingsManager.create();setKeybindings(this.keybindings);setRegisteredThemes(options.uiServices.getThemes());@@ -1400,7 +1408,9 @@ class AgentsViewMode implements Component, Focusable {await this.refreshSessions();this.selectedRowIdentity = getSummaryIdentity(summary);this.selectedActiveSessionId = activeSessionId;+ this.selectedSessionKey = getAgentsViewSelectionKey(summary);this.persistentState.selectedRowIdentity = this.selectedRowIdentity;+ this.persistentState.selectedSessionKey = this.selectedSessionKey;this.restoreSelection();return { summary, activeSessionId };} catch (error) {@@ -1504,20 +1514,7 @@ class AgentsViewMode implements Component, Focusable {this.selectedActiveSessionId = undefined;return;}- const selectedIdentity = this.selectedRowIdentity ?? this.persistentState.selectedRowIdentity;- let index =- selectedIdentity === undefined- ? -1- : this.rows.findIndex((row) => row.selectable && row.identity === selectedIdentity);- const selectedId = this.selectedActiveSessionId;- if (index < 0) {- index =- selectedId === undefined- ? -1- : this.rows.findIndex(- (row) => row.selectable && (row.summary.activeSessionId ?? row.summary.id) === selectedId,- );- }+ const index = this.findSelectedRowIndex();if (index >= 0) {this.selectedIndex = index;} else if (!this.rows[this.selectedIndex]?.selectable) {@@ -1528,6 +1525,12 @@ class AgentsViewMode implements Component, Focusable {this.syncSelectedRowState();}+ private findSelectedRowIndex(): number {+ const identity = this.selectedRowIdentity ?? this.persistentState.selectedRowIdentity;+ const key = this.selectedSessionKey ?? this.persistentState.selectedSessionKey;+ return resolveAgentsViewSelectionIndex(this.rows, identity, key);+ }+private getSelectableRowIndexes(): number[] {return this.rows.flatMap((row, index) => (row.selectable ? [index] : []));}@@ -1536,7 +1539,9 @@ class AgentsViewMode implements Component, Focusable {const row = this.rows[this.selectedIndex];this.selectedActiveSessionId = row?.selectable ? (row.summary.activeSessionId ?? row.summary.id) : undefined;this.selectedRowIdentity = getSelectedRowIdentity(row);+ this.selectedSessionKey = row?.selectable ? getAgentsViewSelectionKey(row.summary) : undefined;this.persistentState.selectedRowIdentity = this.selectedRowIdentity;+ this.persistentState.selectedSessionKey = this.selectedSessionKey;}private finish(result: AgentsViewRunResult): void {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 76aca7b6cc..e1889aac64 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@@ -76,6 +76,46 @@ export function getAgentsViewSummaryIdentity(summary: SessionSummary): string {return `session:${summary.sessionId}`;}+export interface AgentsViewSelectionKey {+ sessionId: string;+ activeSessionId?: string;+}++export function getAgentsViewSelectionKey(summary: SessionSummary): AgentsViewSelectionKey {+ return { sessionId: summary.sessionId, activeSessionId: summary.activeSessionId };+}++// Matches by identity, then activeSessionId, then sessionId: a row's identity+// changes when a session is persisted or re-attached, so the latter two keys+// re-find the same session across those transitions. Returns -1 when gone.+export function resolveAgentsViewSelectionIndex(+ rows: readonly AgentsViewRow[],+ identity: string | undefined,+ key: AgentsViewSelectionKey | undefined,+): number {+ const findSelectable = (predicate: (row: AgentsViewRow) => boolean): number =>+ rows.findIndex((row) => row.selectable && predicate(row));++ if (identity !== undefined) {+ const index = findSelectable((row) => row.identity === identity);+ if (index >= 0) {+ return index;+ }+ }+ if (key?.activeSessionId !== undefined) {+ const activeSessionId = key.activeSessionId;+ const index = findSelectable((row) => (row.summary.activeSessionId ?? row.summary.id) === activeSessionId);+ if (index >= 0) {+ return index;+ }+ }+ if (key?.sessionId !== undefined) {+ const sessionId = key.sessionId;+ return findSelectable((row) => row.summary.sessionId === sessionId);+ }+ return -1;+}+export function buildAgentsViewRows(summaries: readonly SessionSummary[],expandedSubagentParents: ReadonlySet<string> = new Set(),diff --git a/packages/coding-agent/src/modes/index.ts b/packages/coding-agent/src/modes/index.tsindex fbbd91e01e..2a1b1cd607 100644--- a/packages/coding-agent/src/modes/index.ts+++ b/packages/coding-agent/src/modes/index.ts@@ -28,8 +28,11 @@ export { type AgentsViewModeOptions, runAgentsViewMode } from "./agents-view/ageexport {type AgentsViewRow,type AgentsViewSection,+ type AgentsViewSelectionKey,buildAgentsViewRows,classifyAgentsViewSession,+ getAgentsViewSelectionKey,+ resolveAgentsViewSelectionIndex,sectionTitle,shouldShowAgentsViewSession,} from "./agents-view/agents-view-state.js";diff --git a/packages/coding-agent/test/agents-view-state.test.ts b/packages/coding-agent/test/agents-view-state.test.tsindex f425d1f6ba..039c1a4ede 100644--- a/packages/coding-agent/test/agents-view-state.test.ts+++ b/packages/coding-agent/test/agents-view-state.test.ts@@ -14,6 +14,8 @@ import {import {buildAgentsViewRows,classifyAgentsViewSession,+ getAgentsViewSelectionKey,+ resolveAgentsViewSelectionIndex,type SessionSummary,shouldShowAgentsViewSession,} from "../src/modes/index.js";@@ -456,6 +458,63 @@ describe("agents view state", () => {).resolves.toBe(sessionServices);expect(createUiServicesForSession).toHaveBeenCalledWith(summary);});++ describe("restores selection to the previously open session", () => {+ const opened = makeSummary({+ id: "active-open",+ activeSessionId: "active-open",+ sessionId: "session-open",+ sessionFile: "/tmp/project/open.jsonl",+ sessionName: "open",+ });+ const other = makeSummary({+ id: "active-other",+ activeSessionId: "active-other",+ sessionId: "session-other",+ sessionFile: "/tmp/project/other.jsonl",+ sessionName: "other",+ });+ const identity = `file:${opened.sessionFile}`;+ const key = getAgentsViewSelectionKey(opened);++ test("re-finds the session after the list reorders", () => {+ // Returning bumps the opened session's modified time so it sorts first.+ const rows = buildAgentsViewRows([+ { ...opened, modified: "2026-01-02T00:00:00Z" },+ { ...other, modified: "2026-01-01T00:00:00Z" },+ ]);+ expect(rows[0]?.summary.sessionId).toBe("session-open");+ expect(resolveAgentsViewSelectionIndex(rows, identity, key)).toBe(0);+ });++ test("falls back to activeSessionId when the row identity changed", () => {+ // Selected before the session had a file, so the stored identity is active:...+ const rows = buildAgentsViewRows([other, opened]);+ const staleIdentity = "active:active-open";+ expect(resolveAgentsViewSelectionIndex(rows, staleIdentity, key)).toBe(+ rows.findIndex((row) => row.summary.sessionId === "session-open"),+ );+ });++ test("falls back to sessionId after a daemon re-attach regenerates the active id", () => {+ // Re-attach gives a fresh activeSessionId, so only the sessionId still matches.+ const reattached = { ...opened, id: "active-open-2", activeSessionId: "active-open-2" };+ const rows = buildAgentsViewRows([other, reattached]);+ expect(resolveAgentsViewSelectionIndex(rows, identity, key)).toBe(+ rows.findIndex((row) => row.summary.sessionId === "session-open"),+ );+ });++ test("returns -1 when the session is gone so callers pick a default", () => {+ const rows = buildAgentsViewRows([other]);+ expect(resolveAgentsViewSelectionIndex(rows, identity, key)).toBe(-1);+ });++ test("returns -1 with no stored selection", () => {+ const rows = buildAgentsViewRows([opened, other]);+ expect(resolveAgentsViewSelectionIndex(rows, undefined, undefined)).toBe(-1);+ });+ });});function makeSummary(overrides: Partial<SessionSummary>): SessionSummary {