Skip to content

The agent that grows with you

fix(desktop): await page-owned promises before Electron IPC

Ömer Faruk Okumuşbbaf7af5c835
diff --git a/apps/desktop/src/app/chat/right-rail/preview-act.test.ts b/apps/desktop/src/app/chat/right-rail/preview-act.test.ts
index 2da0d2cecd9..81caf4405f9 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-act.test.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-act.test.ts
@@ -1,3 +1,5 @@
+import { types } from 'node:util'
+
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { $rightRailActiveTabId } from '@/store/layout'
@@ -100,6 +102,65 @@ describe('actOnActivePreview (drive_preview tool)', () => {
expect(injected).toContain('0 <= 0')
})
+ it('awaits page-owned thenables for inventories and settled actions before crossing Electron IPC', async () => {
+ // Zone.js replaces Promise with a non-native thenable. Electron awaits
+ // native V8 promises only; otherwise IPC delivers the object's state,
+ // losing its prototype and then() instead of delivering the result.
+ class PagePromise<T> {
+ private pending: Promise<T>
+
+ constructor(executor: ConstructorParameters<typeof Promise<T>>[0]) {
+ this.pending = new Promise(executor)
+ }
+
+ static resolve<T>(value: T) {
+ return new PagePromise<T>(resolve => resolve(value))
+ }
+
+ then(onFulfilled: (value: T) => unknown, onRejected?: (reason: unknown) => unknown) {
+ return new PagePromise((resolve, reject) => {
+ this.pending.then(onFulfilled, onRejected).then(resolve, reject)
+ })
+ }
+ }
+
+ document.body.innerHTML = '<button id="save">Save</button>'
+ const clicked = vi.fn()
+ document.getElementById('save')!.addEventListener('click', clicked)
+
+ const rect = vi.spyOn(Element.prototype, 'getBoundingClientRect').mockReturnValue({
+ bottom: 40,
+ height: 40,
+ left: 0,
+ right: 40,
+ top: 0,
+ width: 40,
+ x: 0,
+ y: 0,
+ toJSON: () => ({})
+ })
+
+ withRunner(async code => {
+ const raw = new Function('Promise', 'return ' + code)(PagePromise)
+
+ return types.isPromise(raw) ? await raw : JSON.parse(JSON.stringify(raw))
+ })
+
+ try {
+ const inventory = await actOnActivePreview({ kind: 'elements' })
+ expect(inventory.success).toBe(true)
+ const save = inventory.elements!.find(element => element.label === 'Save')!
+ expect(save).toBeDefined()
+ expect(await actOnActivePreview({ kind: 'click', ref: save.ref })).toMatchObject({ success: true })
+ expect(clicked).toHaveBeenCalledOnce()
+ expect(await actOnActivePreview({ kind: 'click', ref: 'missing-ref' })).toMatchObject({ success: false })
+ } finally {
+ rect.mockRestore()
+ document.body.replaceChildren()
+ delete (window as unknown as { __hermesActHolder?: unknown }).__hermesActHolder
+ }
+ })
+
it('reports a page that answers with nothing', async () => {
withRunner(async () => '')
diff --git a/apps/desktop/src/app/chat/right-rail/preview-act.ts b/apps/desktop/src/app/chat/right-rail/preview-act.ts
index 959e06927b0..070edef84aa 100644
--- a/apps/desktop/src/app/chat/right-rail/preview-act.ts
+++ b/apps/desktop/src/app/chat/right-rail/preview-act.ts
@@ -293,8 +293,11 @@ ${preamble()}
type Trip = { error: string; kind: 'failed' } | { kind: 'answered'; result: PreviewActResult } | { kind: 'silent' }
async function runJson(run: PreviewScriptRunner, code: string): Promise<Trip> {
+ // Pages such as Trendyol replace Promise with ZoneAwarePromise. Electron
+ // awaits native promises only, otherwise IPC clones the thenable's state.
+ // An async function adopts it into a native promise without changing the page.
const raw = await Promise.race([
- run(code).catch((error: unknown) => new Error(String(error))),
+ run(`(async () => (${code}))()`).catch((error: unknown) => new Error(String(error))),
new Promise<undefined>(resolve => setTimeout(resolve, ACT_TIMEOUT_MS))
])