Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/mobile-web/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/mobile-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 21 additions & 1 deletion src/mobile-web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(() => {
Expand Down Expand Up @@ -485,6 +504,7 @@ const AppContent: React.FC = () => {
compact
activeSessionId={activeSessionId}
onSelectSession={handleSelectSession}
onActiveSessionDeleted={handleActiveSessionDeleted}
onOpenWorkspace={handleOpenWorkspace}
onOpenDeviceTools={handleOpenDeviceTools}
onDisconnect={handleDisconnect}
Expand Down
36 changes: 35 additions & 1 deletion src/mobile-web/src/pages/SessionListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -290,6 +303,7 @@ const SessionListPage: React.FC<SessionListPageProps> = ({
compact = false,
activeSessionId,
onSelectSession,
onActiveSessionDeleted,
onOpenWorkspace,
onOpenDeviceTools,
onDisconnect,
Expand Down Expand Up @@ -534,6 +548,26 @@ const SessionListPage: React.FC<SessionListPageProps> = ({
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<SessionInfo, RecentWorkspaceEntry>[] = [
...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]) => [
Expand All @@ -554,7 +588,7 @@ const SessionListPage: React.FC<SessionListPageProps> = ({
} 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);
Expand Down
34 changes: 34 additions & 0 deletions src/mobile-web/src/services/sessionDeletionNavigation.ts
Original file line number Diff line number Diff line change
@@ -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<TSession extends { session_id: string }, TOwner> {
sessions: readonly TSession[];
owner?: TOwner;
}

export interface SessionDeletionTarget<TSession, TOwner> {
session: TSession;
owner?: TOwner;
}

export function nearestSurvivingSession<TSession extends { session_id: string }, TOwner>(
groups: readonly SessionDeletionGroup<TSession, TOwner>[],
deletedSessionId: string,
): SessionDeletionTarget<TSession, TOwner> | 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;
}
56 changes: 56 additions & 0 deletions src/mobile-web/tests/session-deletion-navigation.test.mjs
Original file line number Diff line number Diff line change
@@ -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\}/);
});
Loading