diff --git a/src/mobile-web/AGENTS.md b/src/mobile-web/AGENTS.md index b69ff0642c..ff532612fe 100644 --- a/src/mobile-web/AGENTS.md +++ b/src/mobile-web/AGENTS.md @@ -63,6 +63,7 @@ pnpm --dir src/mobile-web run test:session-model-browser # Per-session host mode pnpm --dir src/mobile-web run test:account-login # account login without an online desktop pnpm --dir src/mobile-web run test:account-browser # real Chrome tabs, persistence, migration, races; simulated Relay pnpm --dir src/mobile-web run test:images # image preparation and upload limits +pnpm --dir src/mobile-web run test:session-deletion # deleting the open session moves to its nearest surviving neighbor pnpm --dir src/mobile-web run test:workspace-identity # workspace ID wire projection, capability gate, legacy routing and cache records pnpm --dir src/mobile-web run test:remote-cache-browser # real IndexedDB read of pre-ID session cache records pnpm --dir src/mobile-web run type-check diff --git a/src/mobile-web/package.json b/src/mobile-web/package.json index fb4ebb8544..f3886e4126 100644 --- a/src/mobile-web/package.json +++ b/src/mobile-web/package.json @@ -14,6 +14,7 @@ "test:device-compatibility": "node --test tests/device-compatibility.test.mjs", "test:account-browser": "node --test --test-force-exit tests/browser-account.test.mjs", "test:images": "node --test tests/image-compressor.test.mjs", + "test:session-deletion": "node --test tests/session-deletion-navigation.test.mjs", "test:workspace-identity": "node --test tests/workspace-identity.test.mjs tests/workspace-routing.test.mjs", "test:remote-cache-browser": "node --test tests/remote-cache-browser.test.mjs", "type-check": "tsc --noEmit", diff --git a/src/mobile-web/src/App.tsx b/src/mobile-web/src/App.tsx index e356d3d772..d5536822f8 100644 --- a/src/mobile-web/src/App.tsx +++ b/src/mobile-web/src/App.tsx @@ -3,7 +3,7 @@ import { MobileBanner, MobileButton, MobileScrim, MobileStatus } from '@openbitf import PairingPage, { type BrowserAccountBinding } from './pages/PairingPage'; import WorkspacePage from './pages/WorkspacePage'; import DeviceToolsPage from './pages/DeviceToolsPage'; -import SessionListPage from './pages/SessionListPage'; +import SessionListPage, { type ActiveSessionReplacement } from './pages/SessionListPage'; import DevicesPage from './pages/DevicesPage'; import { ErrorBoundary } from './components/ErrorBoundary'; import { I18nProvider, useI18n } from './i18n'; @@ -308,6 +308,25 @@ const AppContent: React.FC = () => { navigateTo('chat', 'push'); }, [isWideLayout, navigateTo, page]); + // The open session was deleted from the session list. Swap the detail pane + // in place (no navigation entry, sidebar left as the user had it) so the + // chat page never keeps reading a session the host no longer has. + const handleActiveSessionDeleted = useCallback((next: ActiveSessionReplacement | null) => { + useMobileStore.getState().setError(null); + setChatAutoFocus(false); + if (next) { + setActiveSessionId(next.sessionId); + setActiveSessionName(next.sessionName || 'Session'); + setActiveSessionAgentType(next.agentType || 'Standard'); + setActiveSessionWorkspace(next.workspace); + return; + } + setActiveSessionId(null); + setActiveSessionName('Session'); + setActiveSessionAgentType('Standard'); + setActiveSessionWorkspace(undefined); + }, []); + const handleBackToSessions = useCallback(() => { navigateTo('sessions', 'pop'); setTimeout(() => { @@ -485,6 +504,7 @@ const AppContent: React.FC = () => { compact activeSessionId={activeSessionId} onSelectSession={handleSelectSession} + onActiveSessionDeleted={handleActiveSessionDeleted} onOpenWorkspace={handleOpenWorkspace} onOpenDeviceTools={handleOpenDeviceTools} onDisconnect={handleDisconnect} diff --git a/src/mobile-web/src/pages/SessionListPage.tsx b/src/mobile-web/src/pages/SessionListPage.tsx index 9ffddd770b..169dc93e75 100644 --- a/src/mobile-web/src/pages/SessionListPage.tsx +++ b/src/mobile-web/src/pages/SessionListPage.tsx @@ -62,6 +62,7 @@ import { createRemoteCacheScope, remoteCache } from '../services/RemoteCache'; import { describeRemoteError } from '../services/remoteErrorPresentation'; // Device-directory read failures share the device pages' relay-failure copy. import { deviceFailurePresentation } from '../services/deviceFailureCopy'; +import { nearestSurvivingSession, type SessionDeletionGroup } from '../services/sessionDeletionNavigation'; import { isClawAgent, sameWorkspace, @@ -101,6 +102,11 @@ interface SessionListPageProps { agentType?: string, workspace?: SessionWorkspaceContext, ) => void; + /** + * The open session was deleted. `next` is the nearest surviving session in + * the same list, or null when the detail pane should show the empty panel. + */ + onActiveSessionDeleted?: (next: ActiveSessionReplacement | null) => void; onOpenWorkspace: () => void; onOpenDeviceTools: () => void; onDisconnect: () => void; @@ -110,6 +116,13 @@ interface SessionListPageProps { type CompactDevice = RelayDeviceInfo; +export interface ActiveSessionReplacement { + sessionId: string; + sessionName: string; + agentType: string; + workspace?: SessionWorkspaceContext; +} + function compactSelectedDeviceIdForClient(client?: RelayHttpClient): string | null { if (!client) return null; @@ -290,6 +303,7 @@ const SessionListPage: React.FC = ({ compact = false, activeSessionId, onSelectSession, + onActiveSessionDeleted, onOpenWorkspace, onOpenDeviceTools, onDisconnect, @@ -534,6 +548,26 @@ const SessionListPage: React.FC = ({ try { await sessionMgr.deleteSession(deleteConfirmTarget.session_id); if (!isSessionListCurrent(targetEpoch)) return; + if (deleteConfirmTarget.session_id === activeSessionId) { + // Leave the deleted session before its chat page reads it again. + const groups: SessionDeletionGroup[] = [ + ...workspaceList.map((workspace) => ({ + owner: workspace, + sessions: compactWorkspaceSessions[compactWorkspaceKey(workspace)] + ?? sessions.filter((session) => sessionMatchesWorkspace(session, workspace, workspaceList)), + })), + { sessions }, + ]; + const next = nearestSurvivingSession(groups, deleteConfirmTarget.session_id); + onActiveSessionDeleted?.(next ? { + sessionId: next.session.session_id, + sessionName: next.session.name, + agentType: next.session.agent_type, + workspace: next.owner + ? sessionWorkspaceContext(next.session, workspaceContext(next.owner)) + : listedSessionWorkspace(next.session), + } : null); + } useMobileStore.getState().removeSession(deleteConfirmTarget.session_id); setCompactWorkspaceSessions((current) => Object.fromEntries( Object.entries(current).map(([key, workspaceSessions]) => [ @@ -554,7 +588,7 @@ const SessionListPage: React.FC = ({ } finally { if (isSessionListCurrent(targetEpoch)) setDeleting(false); } - }, [cacheScope, captureSessionListEpoch, deleteConfirmTarget, isSessionListCurrent, sessionMgr, showToast, t]); + }, [activeSessionId, cacheScope, captureSessionListEpoch, compactWorkspaceSessions, deleteConfirmTarget, isSessionListCurrent, listedSessionWorkspace, onActiveSessionDeleted, sessionMgr, sessions, showToast, t, workspaceList]); const [pullDistance, setPullDistance] = useState(0); const [refreshing, setRefreshing] = useState(false); diff --git a/src/mobile-web/src/services/sessionDeletionNavigation.ts b/src/mobile-web/src/services/sessionDeletionNavigation.ts new file mode 100644 index 0000000000..9d3fc5a2cd --- /dev/null +++ b/src/mobile-web/src/services/sessionDeletionNavigation.ts @@ -0,0 +1,34 @@ +/** + * Where to go after the open session is deleted. + * + * The chat page must not stay mounted on a deleted session: its host reads + * (queue, stream, model) then fail with "session unavailable" errors. The + * replacement is the nearest surviving row in the list the user deleted from: + * the next row, or the previous one when the deleted row was last. Rows from + * other workspaces are not candidates; with no neighbor the caller shows the + * empty home panel instead. + */ +export interface SessionDeletionGroup { + sessions: readonly TSession[]; + owner?: TOwner; +} + +export interface SessionDeletionTarget { + session: TSession; + owner?: TOwner; +} + +export function nearestSurvivingSession( + groups: readonly SessionDeletionGroup[], + deletedSessionId: string, +): SessionDeletionTarget | null { + for (const group of groups) { + const index = group.sessions.findIndex(session => session.session_id === deletedSessionId); + if (index === -1) continue; + const survivors = group.sessions.filter(session => session.session_id !== deletedSessionId); + if (survivors.length === 0) return null; + const session = survivors[Math.min(index, survivors.length - 1)]; + return { session, owner: group.owner }; + } + return null; +} diff --git a/src/mobile-web/tests/session-deletion-navigation.test.mjs b/src/mobile-web/tests/session-deletion-navigation.test.mjs new file mode 100644 index 0000000000..0ecf6bfe38 --- /dev/null +++ b/src/mobile-web/tests/session-deletion-navigation.test.mjs @@ -0,0 +1,56 @@ +import assert from 'node:assert/strict'; +import { readFile } from 'node:fs/promises'; +import test from 'node:test'; +import ts from 'typescript'; + +const load = async (path) => { + const code = ts.transpileModule(await readFile(new URL(path, import.meta.url), 'utf8'), { + compilerOptions: { module: ts.ModuleKind.ESNext, target: ts.ScriptTarget.ES2020 }, + }).outputText; + return import(`data:text/javascript;base64,${Buffer.from(code).toString('base64')}`); +}; +const { nearestSurvivingSession } = await load('../src/services/sessionDeletionNavigation.ts'); + +const row = (id) => ({ session_id: id, name: id }); +const project = { path: '/project' }; +const other = { path: '/other' }; + +test('deleting an open middle row selects the next row in the same workspace', () => { + const next = nearestSurvivingSession([ + { owner: other, sessions: [row('o1')] }, + { owner: project, sessions: [row('a'), row('b'), row('c')] }, + ], 'b'); + assert.equal(next.session.session_id, 'c'); + assert.equal(next.owner, project); +}); + +test('deleting the last row selects the previous row', () => { + const next = nearestSurvivingSession([{ owner: project, sessions: [row('a'), row('b')] }], 'b'); + assert.equal(next.session.session_id, 'a'); +}); + +test('the only session in its workspace leaves the empty panel instead of another workspace', () => { + assert.equal(nearestSurvivingSession([ + { owner: project, sessions: [row('a')] }, + { owner: other, sessions: [row('o1')] }, + ], 'a'), null); +}); + +test('an unlisted session falls through to the flat list, then to the empty panel', () => { + const flat = nearestSurvivingSession([ + { owner: project, sessions: [row('a')] }, + { sessions: [row('x'), row('y')] }, + ], 'x'); + assert.equal(flat.session.session_id, 'y'); + assert.equal(flat.owner, undefined); + assert.equal(nearestSurvivingSession([{ sessions: [row('a')] }], 'missing'), null); +}); + +test('the session list hands deletion of the open session to the app shell', async () => { + const page = await readFile(new URL('../src/pages/SessionListPage.tsx', import.meta.url), 'utf8'); + const app = await readFile(new URL('../src/App.tsx', import.meta.url), 'utf8'); + const handleDelete = page.slice(page.indexOf('const handleDelete = useCallback'), page.indexOf('const [pullDistance')); + assert.match(handleDelete, /deleteConfirmTarget\.session_id === activeSessionId/); + assert.match(handleDelete, /onActiveSessionDeleted\?\.\(/); + assert.match(app, /onActiveSessionDeleted=\{handleActiveSessionDeleted\}/); +});