[Bug]: preview_evaluate rejects top-level await and has a fixed 15s timeout
Before submitting
- [x] I searched existing issues and did not find a duplicate.
- [x] I included enough detail to reproduce or investigate the problem.
Area
apps/server
Steps to reproduce
- Open any page with
preview_open. - Call
preview_evaluatewithexpression: "await new Promise(r => setTimeout(r, 100)); location.pathname". - Call
preview_evaluatewith an async loop that waits for page state longer than 15 s, e.g.(async () => { while (!document.querySelector('#late')) await new Promise(r => setTimeout(r, 500)); return 'ok' })().
Expected behavior
Step 2 returns location.pathname, as a devtools console would. Step 3 either has a way to raise the limit, or the tool description states it.
Actual behavior
Step 2: Preview automation evaluate failed: SyntaxError: await is only valid in async functions and the top level bodies of modules.
Step 3: Preview automation evaluate timed out after 15000ms. The tool has no timeout parameter and its description does not mention the limit.
Cause, at 2756da402b:
ServerBrowserPage.evaluatesendsRuntime.evaluatewithawaitPromiseandreturnByValuebut notreplMode, so top-levelawaitis a syntax error.PreviewAutomationEvaluateInputhas notimeoutMs, and thepreview_evaluatehandler passesundefined, so the broker default of 15 000 ms always applies.preview_actandpreview_wait_foraccepttimeoutMs;preview_evaluatedoes not.
Seen in an audit of ~170 Claude Code sessions in T3 Code on one project (Oct 18 – Nov 6 2026):
- 3 sessions hit the
SyntaxErrorwith expressions likelocation.assign('/pr-189/editor'); await new Promise(r=>setTimeout(r,4000)); location.pathname. Agents write top-levelawaitbecause devtools consoles accept it. - 4 calls in 2 sessions hit the 15 s timeout on async loops waiting for page state.
Suggested fix
- Pass
replMode: truetoRuntime.evaluate. That is how devtools supports top-levelawait, and it keeps the last expression as the result. Alternatively, say in the tool description thatawaitneeds an async IIFE. - Add
timeoutMstoPreviewAutomationEvaluateInputaspreview_acthas, or at least state the 15 s limit in the description.
Impact
Minor bug or occasional failure
Version or commit
main @ 2756da402b
Environment
T3 Code with Claude Code as the provider
Workaround
Wrap the expression in (async () => { ...; return value })(). Split long waits across several calls or use preview_wait_for.
Hey! Adding some numbers here in case they're useful, since this one still comes up a lot for my agents.
Yesterday I counted 70 preview_evaluate timeouts across 41 agent sessions on my machine. The day before it was 25 across 19. Of the five I looked at closely, three were scripts that were always going to run past 15 seconds, like waiting on an animation or a slow page. The agents had no way to know about the limit before they hit it. This is on 0.1.14-nightly.20261130.5492.
I read the reasoning in #31925 for leaving the tool description alone and that makes sense to me. Would it work to put the hint in the timeout error instead? Something like:
Preview automation evaluate timed out after 15000ms. For a longer script pass
awaitPromise: false, store the result onwindowand read it with a second call.
That wouldn't grow anyone's tool list and the agent would get it right on the next call.
Thanks for all the work on this!