t3code icon indicating copy to clipboard operation
t3code copied to clipboard

[Bug]: preview_evaluate rejects top-level await and has a fixed 15s timeout

Open Guria opened this issue 3 weeks ago • 1 comments

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

  1. Open any page with preview_open.
  2. Call preview_evaluate with expression: "await new Promise(r => setTimeout(r, 100)); location.pathname".
  3. Call preview_evaluate with 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:

Seen in an audit of ~170 Claude Code sessions in T3 Code on one project (Oct 18 – Nov 6 2026):

  • 3 sessions hit the SyntaxError with expressions like location.assign('/pr-189/editor'); await new Promise(r=>setTimeout(r,4000)); location.pathname. Agents write top-level await because 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: true to Runtime.evaluate. That is how devtools supports top-level await, and it keeps the last expression as the result. Alternatively, say in the tool description that await needs an async IIFE.
  • Add timeoutMs to PreviewAutomationEvaluateInput as preview_act has, 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.

Guria avatar Nov 06 '26 08:11 Guria

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 on window and 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!

santissimoo avatar Dec 01 '26 18:12 santissimoo