Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): keep preview hosts that report a waitFor miss at the deadline #12899

ClosedmInrOz(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/12899/headopened View on GitHub

What Changed

PreviewAutomationInvokeInput gains an optional replyGraceMs. The broker waits timeoutMs + replyGraceMs before it treats a request as unanswered and evicts the host. The request sent to the host, the error context, and the reported timeoutMs do not change.

preview_wait_for passes 1500 ms of grace. No other operation changes: click, type, navigate, resize, evaluate, and the optional status lookup keep their current deadlines.

Why

preview_wait_for forwarded the same timeoutMs as the broker deadline and as the desktop operation timeout. The desktop waitFor polls until that deadline and only then reports the miss, so its reply landed after the broker had already evicted the host. One unmatched wait dropped a live desktop runtime. On released nightlies the next call returns PreviewAutomationNoAvailableHostError until the desktop reloads. On main with #12535 the host re-registers after one second, but the session loses its pinned tab and the honest miss still counts as a dead-host eviction.

#11381 states the contract: a host that replies with an operation-level timeout remains available. The existing broker test covers that only for an immediate reply. #12407 described this exact race and was accepted in triage; the reporter closed it without a fix.

The grace lives on the broker input rather than in the waitFor handler's timeoutMs so the error still reports the caller's deadline. Inflating timeoutMs made the message read timed out after 6500ms for a 5000 ms wait. A host that stays silent past timeoutMs + replyGraceMs is still evicted, so a frozen host is released as before.

Fixes #12898. Same race as #12407.

Validation

  • vp test run apps/server/src/mcp/McpHttpServer.test.ts apps/server/src/mcp/PreviewAutomationBroker.test.ts: 51 passed (2 new). Both new tests fail on main without the source change: the MCP test gets isError: true on the follow-up preview_status, the broker test gets PreviewAutomationNoAvailableHostError instead of the routed status.
  • New broker test: host answers the waitFor with an operation-level timeout at timeoutMs + 100 under the test clock; the error carries timeoutMs: 5000 and a later status routes to the same host.
  • New MCP test: preview_wait_for with timeoutMs: 5000 returns Preview automation waitFor timed out after 5000ms.; preview_status on the same host then succeeds.
  • vp run typecheck in apps/server: exit 0. vp lint and vp fmt --check on the four changed files: clean.
  • Not run: a live desktop reproduction against this branch. The failing sequence was observed on t3 v0.0.43-nightly.20260917.1866 with a macOS desktop client, from provider logs; the new tests reproduce the same timing through the real broker and MCP toolkit.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes (not applicable)
  • I included a video for animation/interaction changes (not applicable)

Model: Claude Fable 5.1. Harness: Claude Code in T3 Code.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved preview automation reliability when preview_wait_for responses arrive shortly after the caller’s deadline.
    • Preview hosts now remain available for subsequent status checks after an honest wait timeout.
    • Added coverage for delayed responses and continued routing to the correct preview host.
    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched

    2. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      Approvability

      Verdict: Not approved

      Macroscope's review found this PR not approvable — This is a focused, well-tested server fix that adds a 1.5-second grace period to the default preview_wait_for host deadline. Because that changes production default timing and host-retention behavior for all such calls, human review is required.

      No code changes detected at 9f9fdee. Prior analysis still applies.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review Change StackReview Change Stack

      Understand this PR’s impact

      Explore downstream dependencies and potential security impact with Blast Radius.

      View blast radius →

      Important

      Review skipped

      Review was skipped as selected files did not have any reviewable changes.

      ⚙️ Run configuration

      Configuration used: Repository: pingdotgg/t3code/.coderabbit.yaml

      Review profile: CHILL

      Plan: Advanced

      Run ID: 7c5593dc-5ad8-4f32-8515-38bf9343c268

      📥 Commits

      Reviewing files that changed from the base of the PR and between 49576f11105a65fe53db076bb592eb23bc617de4 and 9f9fdeeae1ff04dbf77e780b8108c2d182342da6.

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: Repository: pingdotgg/t3code/.coderabbit.yaml

      Review profile: CHILL

      Plan: Advanced

      Run ID: 37b67cb9-2fbe-46e5-9ba6-615d1c96f964

      📥 Commits

      Reviewing files that changed from the base of the PR and between 1de563c1491c7d82563e4553bf5bf689ce6adbb9 and 49576f11105a65fe53db076bb592eb23bc617de4.

      📒 Files selected for processing (4)
      • apps/server/src/mcp/McpHttpServer.test.ts
      • apps/server/src/mcp/PreviewAutomationBroker.test.ts
      • apps/server/src/mcp/PreviewAutomationBroker.ts
      • apps/server/src/mcp/toolkits/preview/handlers.ts

      Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


      📝 Walkthrough

      Walkthrough

      The broker now supports an optional reply grace period. preview_wait_for uses a 1,500 ms grace period, and tests verify that delayed timeout replies do not evict the preview host.

      Changes

      Preview wait grace handling

      Layer / File(s)Summary
      Broker reply grace handling
      apps/server/src/mcp/PreviewAutomationBroker.ts, apps/server/src/mcp/PreviewAutomationBroker.test.ts
      The broker accepts optional replyGraceMs metadata and waits for timeoutMs + replyGraceMs. Tests verify that a delayed waitFor timeout reply preserves host availability.
      Preview wait integration
      apps/server/src/mcp/toolkits/preview/handlers.ts, apps/server/src/mcp/McpHttpServer.test.ts
      Preview handlers forward the grace period, and preview_wait_for uses 1,500 ms. HTTP server tests verify that a later preview_status call reaches the same host after a timeout reply.

      Priority: ➖ Normal

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Bug fix · Severity of issue fixed: Medium

      Suggested reviewers: juliusmarminge

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title clearly and concisely describes the primary fix: retaining preview hosts after a waitFor miss at the deadline.
      Description check✅ PassedThe description includes complete What Changed and Why sections, explains the broker-only grace behavior, documents issue context and validation results, and addresses the non-applicable UI checklist …
      Linked Issues check✅ PassedIssue [#12898] requires an unsuccessful preview_wait_for to return PreviewAutomationTimeoutError, preserve the caller timeoutMs, keep the host and pinned session available, route later calls to …
      Out of Scope Changes check✅ PassedThe changes are limited to the broker timeout input, preview handler forwarding and configuration, and automated broker and MCP tests. These changes directly support issue [#12898] by adding broker-on…
      Docstring Coverage✅ PassedDocstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 4 files.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR

      Comment `@coderabbitai help` to get the list of available commands.
    4. juliusmarminge added labelsmacroscope-review

    5. I hit #12898 independently today and reproduced it on two Macs with server traces (my duplicate is #15335, and my closed PR #15338 took the same approach). The broker-side grace in this PR fixes the eviction I saw. Some findings from running the fix on real hardware:

      1. On a real desktop, an unmatched wait will read "failed on client", not "timed out". The tests here fake a host that replies _tag: "PreviewAutomationTimeoutError". A real desktop's reply loses that tag over Electron IPC (#15336, confirmed in triage), so the renderer reports PreviewAutomationExecutionError. With the grace in place, my after run on two Macs returned Preview automation waitFor failed on client preview-… for an unmatched 2 s wait. Before, the broker's own timeout produced "timed out", which hid this. The assertion that the message reads "timed out after 5000ms" passes only because of the fake host. It may be worth landing a #15336 fix alongside this, or at least noting the message change.
      2. The race isn't specific to waitFor. Navigate readiness, resize (waitForRenderedViewport), and open with a URL on a reused tab also spend the full timeoutMs on the host before answering. The 500 ms best-effort status lookup after click and type (#12273) evicts through the same broker rule. Applying the grace once in PreviewAutomationBroker.invoke for every request covers all of them without per-call opt-in. For the status lookup, bounding only the caller's wait (Effect.timeoutOption(500) around the invoke) keeps tool latency unchanged without evicting. #15338 has both, with regression tests.
      3. Minor: no test shows that a waitFor that never answers is still evicted once the grace expires. The existing eviction tests only cover operations with zero grace.

      Two-machine evidence, relevant to #13540: Mac A ran the thread and served a page on its own localhost, and Mac B had the same thread's Browser panel open. One unmatched preview_wait_for (2 s) went like this:

      • the broker evicted Mac A at exactly 2 s;
      • Mac A's own timeout reply arrived and was dropped;
      • Mac A reconnected a second later;
      • the next preview_navigate went to Mac B, which can't reach Mac A's localhost;
      • the session stayed on Mac B.

      With broker-wide grace, Mac A kept the session and navigate succeeded there. Before/after recordings and the trace timeline are on #15338.

    6. Note

      Grok responding on behalf of Julius.

      Thanks for digging into this and for the detailed write-up. The same broker deadline race was fixed in #17693, which just merged: the broker now gives the host a short reply allowance past the operation budget, so an honest waitFor miss no longer evicts a live host. That also closed #12898, so I'm closing this one as superseded. If you still see the eviction on a build that includes #17693, please open a fresh issue with the log.

    7. juliusmarminge closed this

Sign in to join this conversation.