Skip to content

The agent that grows with you

fix(webapp): share one installed-profiles read between concurrent callers

unsupportedpastelsce4bf05b749a
diff --git a/apps/desktop/src/lib/browser-connection.ts b/apps/desktop/src/lib/browser-connection.ts
index 34018a4d03b..7e0b4875666 100644
--- a/apps/desktop/src/lib/browser-connection.ts
+++ b/apps/desktop/src/lib/browser-connection.ts
@@ -118,9 +118,32 @@ export function createBrowserConnectionBridge({ api, bootstrap }: BrowserConnect
return [...new Set((result.profiles || []).map(profile => String(profile.name || '').trim()).filter(Boolean))]
}
- // Every installed profile, with the served set brought up to date for that list.
- const installedProfiles = async () => {
- const profiles = await getProfiles()
+ // The list read in flight. Both mounted pickers read the roster on the same list refresh, so
+ // concurrent callers share it; it is dropped once it settles, so no answer outlives its read.
+ let profilesRead: null | Promise<string[]> = null
+
+ const readProfiles = (join: boolean) => {
+ if (join && profilesRead) {
+ return profilesRead
+ }
+
+ const read = getProfiles()
+ const settle = () => {
+ if (profilesRead === read) {
+ profilesRead = null
+ }
+ }
+
+ profilesRead = read
+ read.then(settle, settle)
+
+ return read
+ }
+
+ // Every installed profile, with the served set brought up to date for that list. `join: false`
+ // starts a read of its own, for a caller that needs a list newer than the read in flight.
+ const installedProfiles = async (join = true) => {
+ const profiles = await readProfiles(join)
await served.refresh(profiles)
return profiles
@@ -247,7 +270,8 @@ export function createBrowserConnectionBridge({ api, bootstrap }: BrowserConnect
},
// A profile created since the last inventory read is missing from the cached set, though the
// multiplexer adopted it before its create returned. Selection reads the inventory once before
- // it routes, so the profile rides the tab's socket; dials keep reading the cache alone.
+ // it routes, so the profile rides the tab's socket; dials keep reading the cache alone. The read
+ // is its own: one already in flight may have gone out before the create.
refreshServedProfile: async (profile: string) => {
if (reachesSocket(profile)) {
return
@@ -256,7 +280,7 @@ export function createBrowserConnectionBridge({ api, bootstrap }: BrowserConnect
let timer: ReturnType<typeof setTimeout> | undefined
await Promise.race([
- installedProfiles().then(
+ installedProfiles(false).then(
() => undefined,
() => undefined
),
diff --git a/apps/desktop/src/lib/browser-desktop-bridge-served-profiles.test.ts b/apps/desktop/src/lib/browser-desktop-bridge-served-profiles.test.ts
index 4a124ba0360..f4229a2e37d 100644
--- a/apps/desktop/src/lib/browser-desktop-bridge-served-profiles.test.ts
+++ b/apps/desktop/src/lib/browser-desktop-bridge-served-profiles.test.ts
@@ -233,6 +233,47 @@ describe('the served set read', () => {
expect(statusReads()).toBe(4)
})
+ it('is one /api/profiles read for concurrent inventory reads, as both mounted pickers make on one refresh', async () => {
+ const fetchMock = stubGateway({ profiles: ['default', 'research', 'solo'], status: reports(['default', 'research']) })
+ const desktop = installServedBridge()
+
+ const [rail, statusbar] = await Promise.all([desktop.getAgentRoster?.(), desktop.getAgentRoster?.()])
+ expect(rail?.agents.map(agent => agent.profile)).toEqual(['default', 'research', 'solo'])
+ expect(statusbar?.agents).toEqual(rail?.agents)
+ expect(callsTo(fetchMock, '/api/profiles')).toBe(1)
+
+ // Once it settles, the next refresh reads the list again.
+ await desktop.getAgentRoster?.()
+ expect(callsTo(fetchMock, '/api/profiles')).toBe(2)
+ })
+
+ it('lets a pick read the list itself rather than join a read that predates a new profile', async () => {
+ const gateway: Gateway = { profiles: ['default'], status: reports(['default']) }
+ const fetchMock = stubGateway(gateway)
+ const desktop = installServedBridge()
+ await desktop.getProfileRoutes?.(['default'])
+
+ // The roster's list read answers with the list from before the create, and only once released.
+ let release!: () => void
+ const held = new Promise<void>(resolve => (release = resolve))
+ const answer = fetchMock.getMockImplementation()!
+ fetchMock.mockImplementationOnce(async (url, init) => {
+ const response = await answer(url, init)
+ await held
+
+ return response
+ })
+ const roster = desktop.getAgentRoster?.()
+
+ gateway.profiles = ['default', 'fresh']
+ gateway.status = reports(['default', 'fresh'])
+ await desktop.refreshServedProfile?.('fresh')
+ await expect(desktop.getConnection('fresh')).resolves.toMatchObject({ sharedPrimary: true })
+
+ release()
+ await roster
+ })
+
it('keeps the last answer when a later read fails', async () => {
const gateway: Gateway = { profiles: ['default', 'research', 'solo'], status: reports(['default', 'research']) }
stubGateway(gateway)