Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

fix: return to the most recently used chat after close

Cursor Agent375b1871f84e
diff --git a/apps/native/components/site/useChatClose.ts b/apps/native/components/site/useChatClose.ts
index 8d0ff067..04e5c25e 100644
--- a/apps/native/components/site/useChatClose.ts
+++ b/apps/native/components/site/useChatClose.ts
@@ -2,6 +2,7 @@
import { useEffect, useRef, useState, type Dispatch, type MutableRefObject, type SetStateAction } from "react";
import { disposeSession } from "@/lib/acp-client";
import { browserClose } from "@/lib/browser-client";
+import { focusAfterClose, rememberActivation } from "@/lib/chat-activation";
import { closeWorkerCount, readCloseDontAsk, shouldConfirmTabClose, writeCloseDontAsk } from "@/lib/close-confirm";
import { removeSession, type StoredSession } from "@/lib/sessions";
import type { AcpCommand } from "@/lib/acp";
@@ -39,11 +40,20 @@ export function useChatClose(props: Props) {
const { openChat, focusChat, openStored, newChat, refreshStored, unwatchIdle } = props;
// Closed tabs, oldest first, for the reopen shortcut.
const closedRef = useRef<{ session: string | null; cwd?: string; resumable: boolean }[]>([]);
+ // Activation order is independent of tab and split-pane order.
+ const activeHistoryRef = useRef<number[]>([activeId]);
const [dontAskAgain, setDontAskAgain] = useState(false);
const [pendingClose, setPendingClose] = useState<{ ids: number[]; workers: number; after?: () => void } | null>(null);
useEffect(() => {
setDontAskAgain(readCloseDontAsk(window.localStorage));
}, []);
+ useEffect(() => {
+ activeHistoryRef.current = rememberActivation(
+ activeHistoryRef.current,
+ activeId,
+ chatsRef.current.map(chat => chat.id),
+ );
+ }, [activeId, chatsRef]);
const dropChat = (id: number) => {
sessionsRef.current.delete(id);
@@ -76,9 +86,12 @@ export function useChatClose(props: Props) {
dropChat(id);
}
const remaining = chatsRef.current.filter(c => !ids.includes(c.id));
+ const live = remaining.map(chat => chat.id);
+ const recent = focusAfterClose(activeHistoryRef.current, live, visible, remaining);
+ activeHistoryRef.current = activeHistoryRef.current.filter(id => live.includes(id));
chatsRef.current = remaining; setChats(remaining);
if (!remaining.length) { openChat(++chatIdRef.current); return; }
- if (ids.includes(activeId)) focusChat(visible[0] ?? remaining[remaining.length - 1].id);
+ if (ids.includes(activeId) && recent != null) focusChat(recent);
};
const closeChats = (ids: number[], after?: () => void) => {
diff --git a/apps/native/electron/session-navigation-visual.cjs b/apps/native/electron/session-navigation-visual.cjs
index e03a48ea..a41778ed 100644
--- a/apps/native/electron/session-navigation-visual.cjs
+++ b/apps/native/electron/session-navigation-visual.cjs
@@ -17,6 +17,10 @@ async function runSessionNavigation({ win, output }) {
};
const active = () => js(`Number(document.querySelector('[data-chat][data-focused="true"]').dataset.chat)`);
const ids = () => js(`Array.from(document.querySelectorAll('[data-tab-id]'),e=>Number(e.dataset.tabId))`);
+ const shortcut = async (keyCode, modifiers) => {
+ for (const type of ['keyDown','keyUp']) await desktop.testInput(wc,{type,keyCode,modifiers});
+ await pause(80);
+ };
const capture = async name => {
await js('document.fonts.ready.then(()=>true)'); await pause(320);
fs.writeFileSync(path.join(output,name+'.png'),(await wc.capturePage()).toPNG());
@@ -41,8 +45,25 @@ async function runSessionNavigation({ win, output }) {
const drafted = await active();
for (const keyCode of 'Keep this draft') await desktop.testInput(wc,{type:'char',keyCode});
await click('[aria-label="Workspace navigation"] button[aria-label="New chat"]');
- const last = await active(), before = await ids();
+ let last = await active(), before = await ids();
assert.equal(before.length,3);
+ const first = before[0];
+ await click(`[data-tab-id="${drafted}"] button[aria-pressed]`);
+ await click(`[data-tab-id="${first}"] button[aria-pressed]`);
+ await click(`[data-tab-id="${first}"] button[aria-label="Close tab"]`);
+ await wait(`!document.querySelector('[data-tab-id="${first}"]')`);
+ assert.equal(await active(),drafted,'close control returns to the previously active chat');
+ await shortcut('t',['meta','shift']);
+ await wait(`document.querySelectorAll('[data-tab-id]').length===3`);
+ last = await active();
+ await click(`[data-tab-id="${drafted}"] button[aria-pressed]`);
+ await click(`[data-tab-id="${last}"] button[aria-pressed]`);
+ await shortcut('w',['meta']);
+ await wait(`!document.querySelector('[data-tab-id="${last}"]')`);
+ assert.equal(await active(),drafted,'Cmd+W returns to the previously active chat');
+ await shortcut('t',['meta','shift']);
+ await wait(`document.querySelectorAll('[data-tab-id]').length===3`);
+ last = await active(); before = await ids();
await click(`[data-tab-id="${drafted}"] button[aria-pressed]`);
assert.equal(await js(`document.querySelector('[data-chat="${drafted}"] textarea').value`),'Keep this draft');
await capture('sidebar-open');
diff --git a/apps/native/lib/chat-activation.test.ts b/apps/native/lib/chat-activation.test.ts
new file mode 100644
index 00000000..ce5bcc92
--- /dev/null
+++ b/apps/native/lib/chat-activation.test.ts
@@ -0,0 +1,21 @@
+import { describe, it } from "node:test";
+import assert from "node:assert/strict";
+import { focusAfterClose, rememberActivation } from "./chat-activation.ts";
+
+describe("chat activation history", () => {
+ it("records the focused chat without depending on display order", () => {
+ assert.deepEqual(rememberActivation([1, 3], 2, [1, 2, 3]), [1, 3, 2]);
+ assert.deepEqual(rememberActivation([1, 2, 3], 2, [1, 2, 3]), [1, 3, 2]);
+ assert.deepEqual(rememberActivation([1, 9, 3], 2, [1, 2, 3]), [1, 3, 2]);
+ });
+
+ it("closing the active chat returns to the most recently used remaining chat", () => {
+ assert.equal(focusAfterClose([1, 3, 2], [1, 3], [1, 3], [{ id: 1 }, { id: 3 }]), 3);
+ assert.equal(focusAfterClose([1, 3], [1], [1], [{ id: 1 }]), 1);
+ });
+
+ it("falls back to visible then remaining position when history is empty", () => {
+ assert.equal(focusAfterClose([], [4, 5], [5], [{ id: 4 }, { id: 5 }]), 5);
+ assert.equal(focusAfterClose([], [4, 5], [], [{ id: 4 }, { id: 5 }]), 5);
+ });
+});
diff --git a/apps/native/lib/chat-activation.ts b/apps/native/lib/chat-activation.ts
new file mode 100644
index 00000000..b168228e
--- /dev/null
+++ b/apps/native/lib/chat-activation.ts
@@ -0,0 +1,17 @@
+/** Activation order is independent of tab and split-pane display order. */
+
+export function rememberActivation(history: number[], activeId: number, live: Iterable<number>): number[] {
+ const keep = new Set(live);
+ return [...history.filter(id => id !== activeId && keep.has(id)), activeId];
+}
+
+/** Newest surviving activation, then the existing positional fallback. */
+export function focusAfterClose(
+ history: number[],
+ live: Iterable<number>,
+ visible: number[],
+ remaining: { id: number }[],
+): number | undefined {
+ const keep = new Set(live);
+ return [...history].reverse().find(id => keep.has(id)) ?? visible[0] ?? remaining.at(-1)?.id;
+}