Skip to content

Commit 4b4e806

Browse files
fix(web): a device session reopened during a disconnect opens its preview
The device-session baseline key now includes openedAt, so a session that ended and reopened for the same thread and device while disconnected no longer looks like the old one and still opens its preview. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a8fa74d commit 4b4e806

2 files changed

Lines changed: 17 additions & 6 deletions

File tree

‎apps/web/src/components/preview/agentOpenedPreviews.test.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ const session = {
5353
} as const;
5454

5555
const deviceState = (input: {
56-
readonly sessions?: ReadonlyArray<typeof session>;
56+
readonly sessions?: ReadonlyArray<
57+
Omit<typeof session, "openedAt"> & { readonly openedAt: string }
58+
>;
5759
readonly devices?: ReadonlyArray<typeof device>;
5860
}) =>
5961
({
@@ -141,6 +143,16 @@ describe("applyDeviceSnapshot", () => {
141143
expect(miniPlayerSource()).toBeUndefined();
142144
});
143145

146+
it("opens a session that ended and reopened between two snapshots", () => {
147+
setup();
148+
applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [session] }), floating);
149+
expect(miniPlayerSource()).toBeUndefined();
150+
151+
const reopened = { ...session, openedAt: "2026-01-01T00:05:00.000Z" };
152+
applyDeviceSnapshot(ref.environmentId, deviceState({ sessions: [reopened] }), floating);
153+
expect(miniPlayerSource()).toMatchObject({ kind: "device", deviceId: "pixel" });
154+
});
155+
144156
it("opens a device for a thread that had no sessions when the baseline was taken", () => {
145157
setup();
146158
const otherRef = scopeThreadRef(ref.environmentId, ThreadId.make("thread-2"));

‎apps/web/src/components/preview/agentOpenedPreviews.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,10 @@ function followThread(threadKey: string, threadRef: ScopedThreadRef): () => void
118118
});
119119
}
120120

121-
const sessionKey = (session: {
122-
readonly threadId: string;
123-
readonly hostId: string;
124-
readonly deviceId: string;
125-
}) => `${session.threadId}:${session.hostId}:${session.deviceId}`;
121+
// `openedAt` tells a session apart from one that ended and reopened on the same device while
122+
// this client was disconnected.
123+
const sessionKey = (session: DeviceServiceState["sessions"][number]) =>
124+
`${session.threadId}:${session.hostId}:${session.deviceId}:${session.openedAt}`;
126125

127126
/**
128127
* Per environment: the device sessions seen in the last snapshot. The first

0 commit comments

Comments
 (0)