Skip to content

fix(personal-agent): a minimised window is not a visible one - #1430

Closed
gitsatra wants to merge 3 commits into
spacering-net:mainfrom
gitsatra:perf/face-attention
Closed

gitsatra wants to merge 3 commits into
spacering-net:mainfrom
gitsatra:perf/face-attention

Conversation

@gitsatra

@gitsatra gitsatra commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

The problem

A minimised Codeg goes on drawing its faces. Measured on this machine (20-core Windows 11, WebView2, an idle workspace with two faces in it), sampling the process over a minute:

window state CPU
visible 0.126 core
minimised 0.123 core

A window nobody can see costs 98% of what a window on screen costs. The animation work is not the biggest single item in an idle process — that remains the per-tab agent keepalive — but it is the one that should cost nothing, and it is the one a laptop on battery pays for while the lid is closed.

Why the existing gate does not catch it

bot-face-motion.ts already had a "nobody is looking" gate, and the stylesheet already knows how to hold a face still (data-hidden → animation-play-state: paused). The gate read document.visibilityState.

On Windows, minimising a WebView2 window leaves the document "visible". That was measured, not assumed: with the window minimised, document.visibilityState === "visible", document.hidden === false, and the faces were still play: running. So the gate was written against a signal that never fires on the platform most of these windows run on.

The change

The native window state is the honest signal there, so the first face that mounts starts following it:

  • isVisible() / isMinimized() from @tauri-apps/api/window, re-read on onResized and onFocusChanged (a minimised window reports a resize; restore reports another one).
  • One shared tracking promise and one listener set for every face, not one per face.
  • Any failure — no native window, a browser tab, a rejected IPC — leaves the previous answer standing, so this can never make a visible window go still.
  • The mark is removed on restore and on unmount, and data-hidden is what does the work: the stylesheet's pause rule was already there, this only feeds it.

Losing focus is deliberately not "unseen". A window sitting beside a terminal is fully on screen; a face that froze every time the person typed elsewhere would look broken, and this app is often used exactly that way. The predicate is !isVisible || isMinimized.

Verification

Measured on a locally built --no-bundle binary (Windows 11, 20 cores, WebView2), one minute of sampling per state, per-process CPU counters, the app's own processes only (codeg.exe + msedgewebview2.exe; its agent children counted separately):

window state app CPU agent CPU faces
visible 0.148 core 0.001 data-hidden absent, animation-play-state: running
minimised 0.018 core (0.12×) 0.004 data-hidden="", animation-play-state: paused
restored back to running — the mark is gone

An earlier binary measured the same way over the same workspace shape gave 0.123 → 0.094 core (0.76×); the shape of the saving depends on how many faces and tabs are on screen, so the invariant worth relying on is that minimised is strictly cheaper and nothing animates off screen. A CPU profile of a minimised window shows 99.2% idle in JS, i.e. what remains is the compositor and the browser process, not this app's timers.

RSS is unchanged in both states (~1206 MB for the app's processes in this session, ~454 MB in agent children), so nothing here trades CPU for memory.

  • bot-face-motion.test.ts: a faked native window is minimised → the face is marked data-hidden; restored → the mark is gone; focus alone → the face keeps drawing; document.visibilityState = "hidden" → marked even if the native window claims otherwise.
  • bot-avatar.test.tsx unchanged and passing (41/41 together with the new file), tsc --noEmit clean.

Not changed

The animation itself, its timing, and how it looks with a window on screen. This only decides whether the browser should spend frames on it, and it never pauses anything the owner can see.

CI

Frontend (lint + vitest + build) green (18m16s — 14,787 tests, all passing on the full suite), Rust desktop on windows-latest and Rust desktop on ubuntu-22.04 green, cli/server green on both. One lint failure on the way there was a prettier/prettier wrap in bot-face-motion.test.ts, fixed in f76349598.

Rust desktop on macos-latest reports cancelled after 54m51s with no uploaded log — the same on #1429 and on this branch, which touches no Rust file.

@xintaofei

Copy link
Copy Markdown
Collaborator

Verdict: the diagnosis is right, but the fix is in the wrong layer. I'd change direction rather than merge this.

The diagnosis holds. wry 0.57 handles WM_SIZE only when wparam != SIZE_MINIMIZED (webview2/mod.rs:1297). It calls ICoreWebView2Controller::SetIsVisible only at creation and in set_visible. So a minimised window never tells WebView2 it is hidden, and document.visibilityState stays "visible". Microsoft's WebView2 docs ask hosts to set IsVisible = false on minimise; doing so is what lets Chromium stop rendering and throttle the page.

Why fixing it for faces alone is not enough. The same false "visible" affects every part of the app that relies on visibilityState, not just the faces:

  • src/lib/document-visibility.ts (useDocumentVisible) pauses hidden transcript rendering and live elapsed timers (message-list-view.tsx, use-live-elapsed.ts).
  • The comment above the pause rules for hidden tabs in src/app/globals.css (around line 3122) states that nothing is paused for a hidden window because the engine produces no frames for a hidden page. That assumption is only true once WebView2 is told about the minimise.
  • The face's blink/glance timer and the rAF loop in bot-face-motion.ts.

This PR adds a second, face-only source of truth that polls the native window over IPC. All the other features stay wrong on Windows.

Suggested direction. In the desktop Rust side (Windows), track each window's minimise/hide/show state and call controller.SetIsVisible(shown && !minimised) through with_webview. Key points:

  • Shown/hidden events: tray hide/show sends no resize, so listen for those too.
  • Multiple windows: handle each window on its own; a secondary window must keep drawing while the main one is minimised.
  • Embedded browser tabs: leave their controllers and their own visibility alone.
  • wry's own set_visible: don't conflict with it.

Then document.visibilityState becomes truthful and every existing check works with no frontend change. It is also worth reporting upstream to wry. The CPU numbers should be measured again on a Windows build afterwards.

Other notes on the diff:

  • The branch is based on a commit before perf(personal-agent): keep face motion off the SVG so an idle window costs no layout #1424. On that base, holdFaceWhenUnseen has no caller and there is no data-hidden CSS rule, so the commit changes nothing at runtime. On current main, bot-face-motion.ts conflicts.
  • Tests:
    • The focus test fires no callbacks after the first test, because beforeEach clears the fake listener arrays but the shared tracking promise is never reset.
    • The minimise test sets visible=false and minimized=true together, so it still passes if the isMinimized check is removed.
  • Tracking:
    • Each resize or focus event triggers two IPC reads. Those reads can overlap and publish out of order.
    • The native listeners are never removed after the last face unmounts.

Not ready to merge as it stands.


🤖 Generated with Codeg

…cused

Minimising a WebView2 window leaves `document.visibilityState` at "visible",
so a minimised Codeg kept drawing its faces — 0.12 core of pure idle cost,
measured on this build with an idle workspace open. The native window state is
the honest signal: the first face that mounts follows `isVisible`/`isMinimized`
and marks itself `data-hidden`, which the stylesheet already holds still. The
mark is taken off again on restore, and the tracking is one shared listener for
every face rather than one per face.

Losing focus is deliberately NOT part of it. A window beside a terminal is on
screen; freezing a face every time the person types elsewhere would read as a
broken app rather than a thrifty one.
@gitsatra
gitsatra force-pushed the perf/face-attention branch from da5c200 to c0f629f Compare December 6, 2026 04:01
@gitsatra

gitsatra commented Dec 6, 2026

Copy link
Copy Markdown
Contributor Author

Closing this in favour of #1433, which takes the route you pointed at.

You were right that the root cause is below the page: wry 0.57 handles WM_SIZE only while wparam != SIZE_MINIMIZED, so neither a minimised window nor one hidden to the tray ever reaches ICoreWebView2Controller::SetIsVisible — document.visibilityState keeps saying "visible" and the page's own pause paths (useDocumentVisible, the animation-play-state rules in globals.css, the face's rAF loop and blink timer) run blind.

#1433 keeps each window's shown/minimised state and mirrors it into SetIsVisible(shown && !minimised) with with_webview, per window, from window setup, Resized, focus, the tray hide and show_and_focus_window. It touches no frontend file — the face work that landed in #1424 stays as you fixed it.

Measured on a build without it: 0.478 core on screen, 0.025 core minimised, 0.365 core restored, with visibilityState reporting "visible" in all three states.

Thanks for the precise diagnosis — it saved me from shipping the same fix in the wrong layer twice.

@gitsatra gitsatra closed this Dec 6, 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