Skip to content

Studio Code: Reload the preview in the app when the agent finishes, instead of asking the agent to - #5171

Open
chubes4 wants to merge 8 commits into
trunkfrom
studio-code/reload-preview-automatically
Open

chubes4 wants to merge 8 commits into
trunkfrom
studio-code/reload-preview-automatically

Conversation

@chubes4

@chubes4 chubes4 commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

Related issues

How AI was used in this PR

Claude (via the Pi coding agent in a Roadie session) wrote the change and its tests and ran the checks below. Opened as a draft for human review.

Proposed Changes

When the Studio app is attached, the prompt told Studio Code: "After a change that alters what the site renders …, call refresh_browser so the in-app preview shows the result." Keeping the preview current was the agent's job, and depended on it remembering a mechanical step.

The preview is a browser (an Electron <webview> with its own Refresh button). Like any browser, it shows a page as it loaded, whoever changed the site, whether the agent or someone running wp by hand. Keeping it current belongs to the app, and the app already knows when the agent finishes: it receives run.exited and run.interrupted for every request.

  • The app reloads the preview when an agent run ends. It reloads in place: same page, and a closed panel stays closed.
  • wp-admin is left alone. The person may be working there, perhaps with unsaved edits in the editor, which a reload would interrupt with "Leave page with unsaved changes?". After a run, the Refresh button shows the result.
  • Removed: the refresh_browser tool, its prompt rule, the chatArtifactsEnabled prompt option that only existed for that rule, and the preview.reload event, which nothing emits any more. The app keeps its refresh_browser icon and label so older sessions still display.

Trade-offs:

  • The preview no longer refreshes during a request, only at its end. None of the 47 local Studio Code sessions on the machine where this was written called refresh_browser, so mid-request refreshes look rarely used.
  • A request that changed nothing (a question) also reloads the front-end page, without visible effect.

Testing Instructions

  • Unit tests: npx vitest run apps/ui/src/hooks apps/cli/ai/tests/tools.test.ts apps/cli/ai/tests/system-prompt.test.ts. They check that the preview reloads when a run exits or is interrupted, not when it starts or for another session's run; that the reload keeps the current page and leaves a closed panel closed; and that wp-admin isn't reloaded.
  • By hand, in the Studio app: ask Studio Code to change a site's heading. When the agent finishes, the preview shows the new heading, without a refresh_browser call in the conversation. Open a post in the editor in the preview, ask a question, and check the editor isn't reloaded.

Pre-merge Checklist

  • Have you checked for TypeScript, React or other console errors?

…nstead of asking the agent to

The prompt told the agent to call refresh_browser after any change that alters what the
site renders. Studio now reloads the preview itself at the end of a turn in which a
file write, a WP-CLI or WordPress.com write, a theme scaffold, an import or a pull
succeeded. The refresh_browser tool and its prompt rule go.
A turn changed the preview when something under the active site's wp-content (themes,
plugins, uploads, the SQLite database) changed during it, whichever tool or command
changed it.
@chubes4 chubes4 changed the title Studio Code: Reload the preview after a turn that changed the site, instead of asking the agent to Studio Code: Reload the preview after a turn that changed the site's files, instead of asking the agent to Oct 17, 2026
@chubes4
chubes4 marked this pull request as ready for review October 17, 2026 15:18
@wpmobilebot

wpmobilebot commented Oct 17, 2026 •

Copy link
Copy Markdown
Collaborator

📊 Performance Test Results

Comparing add0bd4 vs trunk

app-size

Metric trunk add0bd4 Diff Change
App Size (Mac) 930.76 MB 930.76 MB 0.00 MB ⚪ 0.0%

site-editor

Metric trunk add0bd4 Diff Change
load 1240 ms 1212 ms 28 ms ⚪ 0.0%

site-startup

Metric trunk add0bd4 Diff Change
siteCreation 8573 ms 8578 ms +5 ms ⚪ 0.0%
siteStartup 5899 ms 5902 ms +3 ms ⚪ 0.0%

Results are median values from multiple test runs.

Legend: 🟢 Improvement (faster) | 🔴 Regression (slower) | ⚪ No change (<50ms diff)

WordPress writes to the database in the background: WP-Cron on the next request after
an event falls due (every minute with WooCommerce), and the block editor's post locks
and autosaves. Checked per turn, those writes reloaded the preview after nearly every
turn on such sites. Checked per request, they cause at most one reload, when the agent
finishes.
@chubes4 chubes4 changed the title Studio Code: Reload the preview after a turn that changed the site's files, instead of asking the agent to Studio Code: Reload the preview when the agent finishes a request that changed the site, instead of asking the agent to Oct 17, 2026
A turn's tool calls name the files they touch. At the end of a turn whose named file in
the site's wp-content changed since the last reload, the preview reloads, so a build
shows its theme and styles as they land. When the request ends, an asynchronous walk
that stops at the first changed file catches what tool paths cannot show: wp commands,
Bash and the database. The walk never blocks the CLI, and it only reloads if something
changed since the last reload.
@chubes4 chubes4 changed the title Studio Code: Reload the preview when the agent finishes a request that changed the site, instead of asking the agent to Studio Code: Reload the preview when the site's files change, instead of asking the agent to Oct 17, 2026
WordPress writes to its database in the background: WP-Cron on the next request after an
event falls due, and the block editor's post locks and autosaves. When a request ends, the
database now counts only if the request ran a command or a tool annotated as replacing
data, the calls that can write it without naming a file.

The reload event says whether a tool call changed the site or the request ended. The app
leaves a preview showing wp-admin alone on the second kind, so an editor with unsaved
changes is never reloaded under the person using it.
A reload after a tool's change recorded Date.now() as the last reload. File times can be
later than the process clock, as on Windows, so the check when the request ended found the
same file newer and reloaded again. The last reload is now the newest change it showed,
when that is later than the clock.
The preview is a browser: it shows a page as it loaded, whoever changed the site. The app
already knows when the agent finishes a request, so it reloads the preview then, in
place and without opening a closed panel. wp-admin is left alone, where the person may
have unsaved edits. This replaces the CLI's detection of site changes from tool paths
and file times, and the preview.reload event, which nothing emits any more.
The app's session commands reload the preview on run.exited and run.interrupted, in place
and without opening a closed panel, and leave wp-admin alone. The preview.reload event
and the chatArtifactsEnabled prompt option have no users left.
@chubes4 chubes4 changed the title Studio Code: Reload the preview when the site's files change, instead of asking the agent to Studio Code: Reload the preview in the app when the agent finishes, instead of asking the agent to Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants