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
48 changes: 48 additions & 0 deletions packages/webui/src/daemon/session/DaemonSessionProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8086,6 +8086,54 @@ describe('DaemonSessionProvider', () => {
expect(blocks).toEqual([]);
});

it('does not attach a session after its load watchdog expires', async () => {
Comment thread
yiliang114 marked this conversation as resolved.
const firstSession = createMockSession({ sessionId: 'session-a' });
const lateSession = createMockSession({ sessionId: 'session-b' });
const lateLoad = createDeferred<MockSession>();
sdkMocks.sessions.push(firstSession);
let actions: DaemonSessionActions | undefined;
let connection: DaemonConnectionState | undefined;

function Harness() {
actions = useDaemonActions();
connection = useDaemonConnection();
return null;
}

await renderWithProvider(<Harness />, {
autoConnect: true,
sessionId: 'session-a',
});
await act(async () => {
await flushPromises();
});
sdkMocks.MockDaemonSessionClient.load.mockImplementationOnce(
async () => lateLoad.promise,
);

vi.useFakeTimers();
try {
const loadPromise = requireActions(actions).loadSession('session-b');
const rejection = loadPromise.catch((error: unknown) => error);
await act(async () => {
await flushPromises();
await vi.advanceTimersByTimeAsync(75_000);
});
const loadError = await rejection;
expect(loadError).toBeInstanceOf(Error);
expect((loadError as Error).message).toContain('Session load timed out');
expect(connection).toMatchObject({ sessionId: undefined });

lateLoad.resolve(lateSession);
await act(async () => {
await flushPromises();
});
expect(connection).toMatchObject({ sessionId: undefined });
Comment thread
yiliang114 marked this conversation as resolved.
} finally {
vi.useRealTimers();
}
});

it('surfaces a restore 504 without treating the session as missing', async () => {
sdkMocks.MockDaemonSessionClient.load.mockRejectedValueOnce(
new DaemonHttpError(
Expand Down
5 changes: 4 additions & 1 deletion packages/webui/src/daemon/session/DaemonSessionProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,10 @@ export function DaemonSessionProvider(props: DaemonSessionProviderProps) {
| Awaited<ReturnType<DaemonClient['capabilities']>>
| undefined;
let reconnectSessionId = restoreSessionId;
let shouldCreateFreshSession = !restoreSessionId && newSessionNonce > 0;
let shouldCreateFreshSession =
!manualSessionClearRef.current &&
!restoreSessionId &&
newSessionNonce > 0;
Comment thread
yiliang114 marked this conversation as resolved.
let reconnectAttempt = 0;
let nextSseConnectReason: DaemonSseConnectReason | undefined;
let skipMetadataRefresh = false;
Expand Down
11 changes: 9 additions & 2 deletions packages/webui/src/daemon/session/actions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,13 @@ describe('createDaemonSessionActions', () => {
vi.useFakeTimers();
try {
const existingSession = createMockSession('session-a');
const manualSessionClearRef = { current: false };
const setRestoreSessionId = vi.fn();
const { actions, getConnection } = createActionsHarness({
connection: { status: 'connected', sessionId: 'session-a' },
manualSessionClearRef,
session: existingSession,
setRestoreSessionId,
});

const loadPromise = actions.loadSession('session-b');
Expand All @@ -541,10 +545,12 @@ describe('createDaemonSessionActions', () => {
await expect(loadPromise).rejects.toThrow('Session load timed out');
expect(getConnection()).toMatchObject({
status: 'disconnected',
sessionId: 'session-b',
sessionId: undefined,
Comment thread
yiliang114 marked this conversation as resolved.
loadingTranscript: undefined,
catchingUp: undefined,
});
expect(manualSessionClearRef.current).toBe(true);
expect(setRestoreSessionId).toHaveBeenLastCalledWith(undefined);
Comment thread
yiliang114 marked this conversation as resolved.
} finally {
vi.useRealTimers();
}
Expand Down Expand Up @@ -1024,6 +1030,7 @@ function createActionsHarness(
restartEventStream?: ReturnType<typeof vi.fn>;
session?: ReturnType<typeof createMockSession>;
setAttachSessionNonce?: ReturnType<typeof vi.fn>;
setRestoreSessionId?: ReturnType<typeof vi.fn>;
setRestoreWorkspaceCwd?: ReturnType<typeof vi.fn>;
} = {},
) {
Expand Down Expand Up @@ -1079,7 +1086,7 @@ function createActionsHarness(
connection = typeof update === 'function' ? update(connection) : update;
},
setPromptStatus: vi.fn(),
setRestoreSessionId: vi.fn(),
setRestoreSessionId: opts.setRestoreSessionId ?? vi.fn(),
setRestoreWorkspaceCwd: opts.setRestoreWorkspaceCwd ?? vi.fn(),
setRestoreMode: vi.fn(),
setRestoreSessionNonce: vi.fn(),
Expand Down
27 changes: 16 additions & 11 deletions packages/webui/src/daemon/session/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,22 @@ export function createDaemonSessionActions({
if (pendingSessionLoadRef.current?.id === loadId) {
pendingSessionLoadRef.current = undefined;
if (sessionRef.current?.sessionId !== sessionId) {
setConnection((current) =>
current.status === 'connecting' &&
current.sessionId === sessionId
? {
...current,
status: 'disconnected',
loadingTranscript: undefined,
catchingUp: undefined,
}
: current,
);
manualSessionClearRef.current = true;
setRestoreSessionId(undefined);
Comment thread
yiliang114 marked this conversation as resolved.
Comment thread
yiliang114 marked this conversation as resolved.
setRestoreWorkspaceCwd(undefined);
Comment thread
yiliang114 marked this conversation as resolved.
setConnection((current) => {
if (
current.status !== 'connecting' ||
current.sessionId !== sessionId
) {
return current;
}
return {
...getConnectionAfterSessionClear(current, sessionId),
status: 'disconnected',
sessionId: undefined,
};
});
}
reject(
dispatchActionError(
Expand Down
Loading