Skip to content

Commit 50cdc24

Browse files
Marve10smaria-rcks
andauthored
fix(server): browser profiles reported by one client survive another client's report (#21745)
Co-authored-by: maria-rcks <[email protected]>
1 parent ebd1265 commit 50cdc24

5 files changed

Lines changed: 106 additions & 22 deletions

File tree

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import {
1616
type PreviewAutomationStatus,
1717
} from "@t3tools/contracts";
1818
import * as Effect from "effect/Effect";
19+
import * as Exit from "effect/Exit";
20+
import * as Scope from "effect/Scope";
1921
import * as Fiber from "effect/Fiber";
2022
import * as FileSystem from "effect/FileSystem";
2123
import * as Layer from "effect/Layer";
@@ -614,8 +616,9 @@ it.live.each([
614616
Effect.scoped(
615617
Effect.gen(function* () {
616618
const { browser, broker, tabId } = yield* ready;
619+
const reportProfiles = yield* browser.profileReporter;
617620
for (const recordingFrameRate of reports) {
618-
yield* browser.reportProfiles({
621+
yield* reportProfiles({
619622
profiles: [],
620623
defaultProfileId: "default",
621624
...(recordingFrameRate === undefined ? {} : { recordingFrameRate }),
@@ -1453,7 +1456,8 @@ it.live(
14531456
const broker = yield* Broker.PreviewAutomationBroker;
14541457
const manager = yield* Manager.PreviewManager;
14551458
yield* Effect.yieldNow;
1456-
yield* browser.reportProfiles({
1459+
const reportProfiles = yield* browser.profileReporter;
1460+
yield* reportProfiles({
14571461
profiles: [{ id: "profile-work", name: "Work", kind: "persistent" }],
14581462
defaultProfileId: "profile-work",
14591463
});
@@ -1498,3 +1502,46 @@ it.live(
14981502
}),
14991503
).pipe(Effect.provide(layer)),
15001504
);
1505+
1506+
it.live("profiles reported by one client survive another client's report", () =>
1507+
Effect.scoped(
1508+
Effect.gen(function* () {
1509+
const { browser, broker } = yield* ready;
1510+
contextRequests.length = 0;
1511+
const work = { id: "profile-work", name: "Work", kind: "persistent" } as const;
1512+
const shared = { id: "profile-shared", name: "Shared", kind: "persistent" } as const;
1513+
const openWork = () =>
1514+
broker.invoke<PreviewAutomationStatus>({
1515+
scope,
1516+
operation: "open",
1517+
input: { reuseExistingTab: false, show: false, profileId: work.id },
1518+
});
1519+
const reportFromA = yield* browser.profileReporter;
1520+
const clientB = yield* Scope.make();
1521+
const reportFromB = yield* browser.profileReporter.pipe(
1522+
Effect.provideService(Scope.Scope, clientB),
1523+
);
1524+
yield* reportFromA({ profiles: [work, shared], defaultProfileId: work.id });
1525+
yield* reportFromB({ profiles: [shared], defaultProfileId: shared.id });
1526+
expect(yield* openWork()).toMatchObject({
1527+
defaultProfileId: shared.id,
1528+
profiles: [
1529+
{ id: "default", name: "Default" },
1530+
{ id: "incognito", name: "Incognito", incognito: true },
1531+
{ id: work.id, name: work.name },
1532+
{ id: shared.id, name: shared.name },
1533+
],
1534+
});
1535+
expect(contextRequests).toEqual([{ profileId: work.id, isolated: false }]);
1536+
yield* Scope.close(clientB, Exit.void);
1537+
expect(yield* openWork()).toMatchObject({ defaultProfileId: work.id });
1538+
yield* reportFromA({ profiles: [shared], defaultProfileId: shared.id });
1539+
const removed = yield* openWork().pipe(Effect.flip);
1540+
expect(removed).toMatchObject({
1541+
_tag: "PreviewAutomationExecutionError",
1542+
reason:
1543+
'No browser profile is named "profile-work". Use one of: Default (id default), Incognito (id incognito), Shared (id profile-shared).',
1544+
});
1545+
}),
1546+
).pipe(Effect.provide(layer)),
1547+
);

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ export interface ServerBrowserViewer {
210210
readonly input: (message: unknown) => Effect.Effect<void>;
211211
}
212212

213+
export type ProfileReporter = (input: PreviewReportProfilesInput) => Effect.Effect<void>;
214+
213215
export class ServerBrowser extends Context.Service<
214216
ServerBrowser,
215217
{
@@ -244,7 +246,7 @@ export class ServerBrowser extends Context.Service<
244246

245247
readonly clearProfile: (profileId: string) => Effect.Effect<void, PreviewClearProfileError>;
246248

247-
readonly reportProfiles: (input: PreviewReportProfilesInput) => Effect.Effect<void>;
249+
readonly profileReporter: Effect.Effect<ProfileReporter, never, Scope.Scope>;
248250
}
249251
>()("t3/preview/ServerBrowser") {}
250252

@@ -1384,6 +1386,8 @@ const make = Effect.gen(function* () {
13841386
? ("agent" as const)
13851387
: ("unclaimed" as const);
13861388

1389+
const reportsByClient = new Map<number, PreviewReportProfilesInput>();
1390+
let nextClient = 0;
13871391
let reportedProfiles: {
13881392
readonly profiles: ReadonlyArray<BrowserProfile>;
13891393
readonly defaultProfileId: string;
@@ -1392,16 +1396,45 @@ const make = Effect.gen(function* () {
13921396
// last report outlives the client, which lets agents record while no client is connected.
13931397
let recordingFrameRate = DEFAULT_BROWSER_RECORDING_FRAME_RATE;
13941398

1395-
const reportProfiles: ServerBrowser["Service"]["reportProfiles"] = (input) =>
1396-
Effect.sync(() => {
1397-
if (input.recordingFrameRate !== undefined) recordingFrameRate = input.recordingFrameRate;
1398-
const profiles = resolveBrowserProfiles(input.profiles);
1399-
reportedProfiles = {
1400-
profiles,
1401-
defaultProfileId:
1402-
findBrowserProfile(profiles, input.defaultProfileId)?.id ?? DEFAULT_BROWSER_PROFILE_ID,
1403-
};
1404-
});
1399+
// Each client reports only the profiles in its own settings, so the catalog is
1400+
// their union while they stay connected. The last catalog stays usable after
1401+
// every client disconnects, as a single report did before.
1402+
const rebuildReportedProfiles = () => {
1403+
const byId = new Map<string, BrowserProfile>();
1404+
let latest: PreviewReportProfilesInput | undefined;
1405+
for (const report of reportsByClient.values()) {
1406+
for (const profile of report.profiles) byId.set(profile.id, profile);
1407+
latest = report;
1408+
}
1409+
if (latest === undefined) return;
1410+
const profiles = resolveBrowserProfiles([...byId.values()]);
1411+
reportedProfiles = {
1412+
profiles,
1413+
defaultProfileId:
1414+
findBrowserProfile(profiles, latest.defaultProfileId)?.id ?? DEFAULT_BROWSER_PROFILE_ID,
1415+
};
1416+
};
1417+
1418+
const profileReporter: ServerBrowser["Service"]["profileReporter"] = Effect.acquireRelease(
1419+
Effect.sync(() => nextClient++),
1420+
(client) =>
1421+
Effect.sync(() => {
1422+
reportsByClient.delete(client);
1423+
rebuildReportedProfiles();
1424+
}),
1425+
).pipe(
1426+
Effect.map(
1427+
(client): ProfileReporter =>
1428+
(input) =>
1429+
Effect.sync(() => {
1430+
if (input.recordingFrameRate !== undefined)
1431+
recordingFrameRate = input.recordingFrameRate;
1432+
reportsByClient.delete(client);
1433+
reportsByClient.set(client, input);
1434+
rebuildReportedProfiles();
1435+
}),
1436+
),
1437+
);
14051438

14061439
const profileStatus = () => {
14071440
const profiles = reportedProfiles?.profiles ?? BUILT_IN_BROWSER_PROFILES;
@@ -2618,7 +2651,7 @@ const make = Effect.gen(function* () {
26182651
return ServerBrowser.of({
26192652
attachViewer,
26202653
clearProfile,
2621-
reportProfiles,
2654+
profileReporter,
26222655
openDownload,
26232656
answerFileChooser,
26242657
});

‎apps/server/src/preview/ServerBrowserStream.test.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ it.effect.each([
8888
});
8989
const browser = ServerBrowser.ServerBrowser.of({
9090
clearProfile: () => Effect.void,
91-
reportProfiles: () => Effect.void,
91+
profileReporter: Effect.succeed(() => Effect.void),
9292
openDownload: () => Effect.succeedNone,
9393
answerFileChooser: () => Effect.succeed(false),
9494
attachViewer: (input) =>
@@ -163,7 +163,7 @@ it.effect("sends replies ahead of stale frames and only the newest frame after a
163163
});
164164
const browser = ServerBrowser.ServerBrowser.of({
165165
clearProfile: () => Effect.void,
166-
reportProfiles: () => Effect.void,
166+
profileReporter: Effect.succeed(() => Effect.void),
167167
openDownload: () => Effect.succeedNone,
168168
answerFileChooser: () => Effect.succeed(false),
169169
attachViewer: () => Effect.succeed({ output, input: () => Effect.void }),
@@ -234,7 +234,7 @@ it.effect.each([
234234
let attachments = 0;
235235
const browser = ServerBrowser.ServerBrowser.of({
236236
clearProfile: () => Effect.void,
237-
reportProfiles: () => Effect.void,
237+
profileReporter: Effect.succeed(() => Effect.void),
238238
openDownload: () => Effect.succeedNone,
239239
answerFileChooser: () => Effect.succeed(false),
240240
attachViewer: () => {
@@ -275,7 +275,7 @@ it.effect("serves a tab's download only to an authorized session", () =>
275275
const requests: Array<unknown> = [];
276276
const browser = ServerBrowser.ServerBrowser.of({
277277
clearProfile: () => Effect.void,
278-
reportProfiles: () => Effect.void,
278+
profileReporter: Effect.succeed(() => Effect.void),
279279
openDownload: (input) =>
280280
Effect.sync(() => {
281281
requests.push(input);
@@ -323,7 +323,7 @@ it.effect("passes uploaded files to the page's open picker and needs operate sco
323323
const answers: Array<{ chooserId: string; files: Array<{ name: string; text: string }> }> = [];
324324
const browser = ServerBrowser.ServerBrowser.of({
325325
clearProfile: () => Effect.void,
326-
reportProfiles: () => Effect.void,
326+
profileReporter: Effect.succeed(() => Effect.void),
327327
openDownload: () => Effect.succeedNone,
328328
answerFileChooser: (input) =>
329329
Effect.sync(() => {
@@ -392,7 +392,7 @@ it.effect.each([
392392
Effect.gen(function* () {
393393
const browser = ServerBrowser.ServerBrowser.of({
394394
clearProfile: () => Effect.void,
395-
reportProfiles: () => Effect.void,
395+
profileReporter: Effect.succeed(() => Effect.void),
396396
openDownload: () => Effect.succeedNone,
397397
answerFileChooser: () => Effect.succeed(false),
398398
attachViewer: () => Effect.fail(new ServerBrowser.ServerBrowserLaunchError({ cause: error })),

‎apps/server/src/ws.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ it.effect(
659659
},
660660
{},
661661
yield* ServerBrowser.ServerBrowser,
662+
() => Effect.void,
662663
).pipe(Layer.provide(configRpcServices)),
663664
),
664665
);
@@ -826,6 +827,7 @@ it.effect("on-demand clients get provider config without workspace snapshots", (
826827
},
827828
{},
828829
yield* ServerBrowser.ServerBrowser,
830+
() => Effect.void,
829831
).pipe(Layer.provide(configRpcServices)),
830832
),
831833
);

‎apps/server/src/ws.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,7 @@ export const layerWsRpc = (
12221222
currentSession: EnvironmentAuth.AuthenticatedSession,
12231223
clientAnalyticsProps: Readonly<Record<string, unknown>>,
12241224
serverBrowser: ServerBrowser.ServerBrowser["Service"],
1225+
reportProfiles: ServerBrowser.ProfileReporter,
12251226
) =>
12261227
Layer.effectContext(
12271228
Effect.gen(function* () {
@@ -3032,7 +3033,7 @@ export const layerWsRpc = (
30323033
[WS_METHODS.previewClose]: (input) => previewManager.close(input),
30333034
[WS_METHODS.previewList]: (input) => previewManager.list(input),
30343035
[WS_METHODS.previewClearProfile]: (input) => serverBrowser.clearProfile(input.profileId),
3035-
[WS_METHODS.previewReportProfiles]: (input) => serverBrowser.reportProfiles(input),
3036+
[WS_METHODS.previewReportProfiles]: (input) => reportProfiles(input),
30363037
[WS_METHODS.previewReportStatus]: (input) => previewManager.reportStatus(input),
30373038
[WS_METHODS.subscribePreviewEvents]: (_input) => previewManager.events,
30383039
[WS_METHODS.deviceConfigure]: (input) => deviceService.configure(input),
@@ -3305,6 +3306,7 @@ export const layer = Layer.unwrap(
33053306
);
33063307
const clientAnalyticsProps = readClientAnalyticsProps(request);
33073308
yield* analytics.recordOwners(readClientOwners(request), "client");
3309+
const reportProfiles = yield* serverBrowser.profileReporter;
33083310
const rpcWebSocketHttpEffect = yield* Effect.gen(function* () {
33093311
const { protocol, httpEffect } = yield* RpcServer.makeProtocolWithHttpEffectWebsocket;
33103312
yield* RpcServer.make(ServerWsRpcGroup, WS_RPC_SERVER_OPTIONS).pipe(
@@ -3334,7 +3336,7 @@ export const layer = Layer.unwrap(
33343336
return httpEffect;
33353337
}).pipe(
33363338
Effect.provide(
3337-
layerWsRpc(session, clientAnalyticsProps, serverBrowser).pipe(
3339+
layerWsRpc(session, clientAnalyticsProps, serverBrowser, reportProfiles).pipe(
33383340
Layer.provideMerge(RpcSerialization.layerJson),
33393341
// Handler-context reporters see only request defects, not server-wide defects.
33403342
Layer.provide(DefectReporter.layer),

0 commit comments

Comments
 (0)