Repository navigation
fix(desktop): hidden preview tabs free their memory after ten minutes #38399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1783,58 +1783,23 @@ const make = Effect.gen(function* () { | |
| const isVisible = (tab: ServerTab) => | ||
| tab.viewers.size > 0 || (tab.desktop !== null && desktopChannel.isShown(tab)); | ||
|
|
||
| const reclaimingTabs = new Set<ServerTab>(); | ||
| const idleAgentTabs = () => { | ||
| const cutoff = Date.now() - AGENT_TAB_IDLE_MS; | ||
| return [...tabs.values()].filter( | ||
| (tab) => | ||
| tab.control.agentId !== null && | ||
| !reclaimingTabs.has(tab) && | ||
| !isVisible(tab) && | ||
| tab.usedAt < cutoff, | ||
| (tab) => tab.control.agentId !== null && !isVisible(tab) && tab.usedAt < cutoff, | ||
| ); | ||
| }; | ||
|
|
||
| // The desktop app renders every server tab, so idle desktop agent tabs would otherwise | ||
| // hold the server's budget forever. Hidden ones give up their slots, longest idle first, | ||
| // only when an agent needs one and has room for it. | ||
| const freeIdleAgentSlot = async (agentSessionId: string) => { | ||
| if (!serverAtTabLimit() || agentAtTabLimit(agentSessionId)) return; | ||
| const longestIdle = idleAgentTabs().sort((left, right) => left.usedAt - right.usedAt)[0]; | ||
| if (longestIdle) { | ||
| reclaimingTabs.add(longestIdle); | ||
| try { | ||
| await runPromise( | ||
| manager.close({ threadId: longestIdle.threadId, tabId: longestIdle.tabId }), | ||
| ); | ||
| dropTab(longestIdle, false); | ||
| } finally { | ||
| reclaimingTabs.delete(longestIdle); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // A Playwright launch must settle before its capacity can be released. | ||
| const openTab = Effect.fn("ServerBrowser.openTab")(function* ( | ||
| input: Parameters<typeof manager.open>[0], | ||
| options: { readonly reclaimIdleSlot?: boolean } = {}, | ||
| ) { | ||
| let key: string = NodeCrypto.randomUUID(); | ||
| const reservation = { agentId: input.automationOwner ?? null }; | ||
| return yield* Effect.acquireUseRelease( | ||
| Effect.tryPromise({ | ||
| try: async () => { | ||
| if (input.automationOwner !== undefined) { | ||
| while ( | ||
| options.reclaimIdleSlot && | ||
| serverAtTabLimit() && | ||
| !agentAtTabLimit(input.automationOwner) && | ||
| idleAgentTabs().length > 0 | ||
| ) { | ||
| await freeIdleAgentSlot(input.automationOwner); | ||
| } | ||
| assertTabCapacity(input.automationOwner); | ||
| } | ||
| if (input.automationOwner !== undefined) assertTabCapacity(input.automationOwner); | ||
| pendingTabs.set(key, reservation); | ||
| }, | ||
| catch: ServerBrowserPage.toOperationError, | ||
|
|
@@ -1871,10 +1836,12 @@ const make = Effect.gen(function* () { | |
| ); | ||
| }, Effect.uninterruptible); | ||
|
|
||
| // Desktop browsing bypasses streamed viewers and server input tracking, | ||
| // so these idle signals only apply to headless tabs. | ||
| // Desktop browsing bypasses server input tracking, so a shown tab counts as used; the idle | ||
| // window then starts when it was last on screen, and hidden desktop guests do not live forever. | ||
| const closeIdleAgentTabs = () => { | ||
| for (const tab of idleAgentTabs()) if (tab.desktop === null) dropTab(tab, true); | ||
| const now = Date.now(); | ||
| for (const tab of tabs.values()) if (isVisible(tab)) tab.usedAt = now; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stamp the last-visible time when a desktop tab stops being shown.
Record the last-visible time when the desktop shown state changes from shown to hidden. Keep the sweep refresh for tabs that remain shown.
|
||
| for (const tab of idleAgentTabs()) dropTab(tab, true); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep active desktop recordings out of idle cleanup. If a hidden desktop-hosted agent tab records for more than thirty minutes without another request, Exclude active and starting recordings from desktop idle cleanup. Start their idle window after recording ends. The previous sweep excluded desktop tabs entirely.
|
||
| }; | ||
|
|
||
| const latestThreadTab = (threadId: string, agentSessionId?: string) => { | ||
|
|
@@ -2590,17 +2557,14 @@ const make = Effect.gen(function* () { | |
| const tab = | ||
| existing ?? | ||
| (await runPromise( | ||
| openTab( | ||
| { | ||
| threadId: request.threadId, | ||
| ...(url ? { url } : {}), | ||
| ...(profileId === undefined ? {} : { profileId }), | ||
| runtime: "server", | ||
| reveal: false, | ||
| automationOwner: request.agentSessionId, | ||
| }, | ||
| { reclaimIdleSlot: true }, | ||
| ), | ||
| openTab({ | ||
| threadId: request.threadId, | ||
| ...(url ? { url } : {}), | ||
| ...(profileId === undefined ? {} : { profileId }), | ||
| runtime: "server", | ||
| reveal: false, | ||
| automationOwner: request.agentSessionId, | ||
| }), | ||
| )); | ||
| if (existing?.dialog) | ||
| throw new BrowserControlInterrupted({ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Await a
Deferredfor each expected audio state instead of yielding once.The audio listener and the
destroyedhandler both start their work withrunFork, so that work runs in a detached fiber. A singleEffect.yieldNowafteremitAudioState(true)and afterdestroy()gives that fiber one scheduler turn. It does not show that the expected audio state was published. The publish path can need more than one turn, so these assertions can fail intermittently.Resolve a
Deferredfrom thesubscribeStateChangescallback when a snapshot with the expectedaudiblevalue arrives. Await thatDeferredbefore each assertion. The file already awaits aDeferredthis way for find results, and the testing rules require awaiting aDeferredinstead of yielding.