Skip to content

fix(window): tolerate DIP rounding in bsk window resize - #775

Merged
NianJiuZst merged 2 commits into
Tencent:mainfrom
wannT-1:fix/resize-dpi-tolerance
Nov 19, 2026
Merged

NianJiuZst merged 2 commits into
Tencent:mainfrom
wannT-1:fix/resize-dpi-tolerance

Conversation

@wannT-1

@wannT-1 wannT-1 commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

What

bsk window resize now accepts a committed resize whose reported bounds are within 2 DIP of the request on each axis, instead of requiring exact equality.

Fixes #771.

Why it failed

apps/extension/src/tools/window.ts (handleWindowResize) verified the resize with exact DIP equality in two places: the success condition of the 21 × 50 ms settling loop, and the window_resize_constrained branch that produces Window resize requested … but the browser reports ….

Windows stores a window rect in device pixels while chrome.windows.update takes DIP, so at a fractional display scale the DIP → physical → DIP round-trip lands 1 DIP off per axis (Windows 10 at 125%: 1100x700 → 1101x701). Exact equality cannot tell that rounding apart from a real window-manager constraint, so every such resize burned the full settling budget (~1 s, 21 reads) and then reported protocol_error / window_resize_constrained / exit 2 for a resize the browser had already committed.

The rule

Fixed 2 DIP per axis (WINDOW_ROUNDING_TOLERANCE_DIP), deliberately not a percentage:

  • the round-trip error is bounded by half a device pixel, i.e. ≤ ~1.5 DIP at any scale ≥ 100%;
  • a percentage would grow with the window (1% of 1400 px is 14 px) and swallow genuine work-area clamps;
  • 2 DIP is already the epsilon this module encodes for window bounds in viewportFollowed (Math.max(2, zoom)), so the constant now has a single home and the two comparisons agree.

Page zoom stays out of this comparison — it moves CSS viewports, not DIP bounds. effect_state: "committed", the error message, the unconfirmed settling guard and the window_resize_viewport_unchanged check are unchanged; a genuinely constrained window still reports the same reason.

Verification

  • window.test.ts: 37 passing before → 4 failing / 42 passing with the new tests against the unpatched code → 46/46 passing after the fix.
  • Full extension suite: 4110 passed / 1 failed / 8 skipped of 4119, versus 4101 passed / 1 failed / 8 skipped of 4110 on the untouched baseline — identical failure set, +9 tests. The single failure (human-loop "bounds dangerous URL regex checks in a child process", spawnSync … EPERM) reproduces on the baseline and is environmental.
  • tsc --noEmit clean; biome check clean on both changed files.

New cases cover: exact match, ±1 and ±2 DIP (accepted, without spending the settling budget), and 12/40 px deltas (still window_resize_constrained, message and payload unchanged), plus a rounding-sized resize with a stale viewport still reporting window_resize_viewport_unchanged.

Second observation in the issue (not fixed here)

The reporter also noted data.actual_viewport staying at 1178x702 across resizes. That value is geometrically impossible for a 1101×701 window — it matches the session-start viewport — so it is stale by at least one generation, below this handler (it re-reads Page.getLayoutMetrics every poll). In-extension candidates are a pinned emulate device-metrics override (resize deliberately does not clear it), a throttled/occluded unfocused Agent Window, or a wedged renderer.

Worth flagging plainly: if the reporter's viewport really is frozen, this change moves their error from window_resize_constrained (wrong) to window_resize_viewport_unchanged (accurate) rather than to success. Weakening that guard would hide the second bug and drop the existing browser regression, so it is out of scope here — happy to follow up separately if you want the staleness chased down.

Scope

apps/extension/src/tools/window.ts, its test file, both changelogs (the repo requires them updated in the same PR), and the window resize paragraph in crates/bsk-cli/skill/references/interaction-details.md. No protocol, CLI surface or message-shape changes.

wannT-1 and others added 2 commits November 19, 2026 09:51
Why:
At a fractional display scale (Windows 10 22H2 at 125%), `chrome.windows.get`
reports the outer window one DIP larger than requested on each axis because
Windows round-trips `chrome.windows.update`'s DIP request through device pixels
(1100x700 -> 1101x701). `handleWindowResize` verified the result with exact
equality in two places, so that rounding was indistinguishable from a real
window-manager constraint: every resize first burned the full 21 x 50 ms
settling budget and then reported `protocol_error` / `window_resize_constrained`
/ exit code 2 even though `effect_state` was `committed` (Tencent#771).

What:
- Add `WINDOW_ROUNDING_TOLERANCE_DIP = 2` and a `withinRounding(actual,
  requested)` helper for window bounds.
- Accept the resize when both axes land within that tolerance, both in the
  settling loop (a rounding-only resize returns immediately instead of
  dead-waiting ~1 s) and in the constrained branch (a DPI-rounded window is no
  longer mislabelled as constrained; it falls through to the accurate
  `window_resize_viewport_unchanged` diagnosis).
- Reuse the constant as the floor of `viewportFollowed`'s window-bounds epsilon,
  which already encoded "2 px of DIP rounding".

2 px fixed rather than scaled: the DIP round-trip error is bounded by half a
device pixel, i.e. <= ~1.5 DIP at any scale >= 100%, so it does not grow with
the DPI factor, while a percentage tolerance would grow with the window (1% of
1400 px = 14 px) and swallow real work-area constraints. The extension cannot
read the OS scale factor either (no `system.display` permission).

Unchanged on purpose: `effect_state: "committed"` still accompanies a genuine
`window_resize_constrained` (same message, same exit code), the `unconfirmed`
retry guard still compares exact sizes, and the Tencent#559
`window_resize_viewport_unchanged` guard is untouched.

Tests:
- `apps/extension/src/tools/__tests__/window.test.ts`: +9 cases covering exact /
  +1,+1 / -1,-1 / +2,-2 acceptance (asserting the settling budget is not spent),
  +40,+40 / -40,0 / 0,+12 still constrained, rounding-with-frozen-viewport
  reporting `window_resize_viewport_unchanged`, and a rejected
  `windows.update` never becoming success. 46/46 pass (4 were red before the fix).
- Full extension unit suite: 4110 passed / 1 failed / 8 skipped, versus
  4101 passed / 1 failed / 8 skipped on the untouched baseline (+9 tests, same
  failure); the failure is the sandbox's `spawnSync ... EPERM` in
  `human-loop.test.ts`.
- `tsc --noEmit` clean; `biome check` clean on the changed files.

Docs: bilingual CHANGELOG entries and the agent-facing resize paragraph in
`crates/bsk-cli/skill/references/interaction-details.md`.

Fixes Tencent#771
Why:
After accepting 2 px DIP rounding in handleWindowResize, keeping exact
sameSize() comparisons in the unconfirmed-retry guard allowed a second
identical resize request on a frozen viewport under fractional scaling
(e.g. requesting 1100x710 while the browser reports 1101x711) to bypass
the guard, clear unconfirmed via viewportFollowed(), and return false
success. The same mismatch occurred when a post-resize read failed after
windows.update committed DPI-rounded bounds (previousFailure.window held
the exact requested size while beforeWindow on retry held the rounded
bounds).

What:
- Compare beforeWindow against both requested and previousFailure.window
  with withinRounding() in the unconfirmed retry guard while keeping
  exact sameSize() for viewport comparison.
- Add regression tests in window.test.ts covering repeated identical
  resizes and failed post-resize reads with DPI-rounded bounds and a
  frozen viewport.
- Align CHANGELOG.zh-CN.md with the 2 px DIP rounding tolerance.

@NianJiuZst NianJiuZst left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thorough investigation and clean fix for #771 — using a fixed 2 DIP tolerance (WINDOW_ROUNDING_TOLERANCE_DIP) shared with viewportFollowed is the right rule here, avoiding the 1s settling penalty on fractional scaling without hiding genuine window-manager constraints.

During review, I caught one subtle interaction with the unconfirmed retry guard and pushed a small follow-up commit (f866dec3) directly to this branch:

  • Preserve the unconfirmed retry guard under DIP rounding (apps/extension/src/tools/window.ts): Previously, the previousFailure check still used exact sameSize(beforeWindow, requested) and sameSize(previousFailure.window, beforeWindow). When a viewport is frozen under fractional scaling (e.g. requesting 1100x710 while Windows commits 1101x711), the first call accurately returned window_resize_viewport_unchanged, but an immediate retry of 1100x710 bypassed the guard, cleared unconfirmed via viewportFollowed(), and reported a false success. The same mismatch also occurred when a post-resize read failed (window_resize_unconfirmed) after windows.update committed rounded bounds. Switched both window-bounds checks in that guard to withinRounding() while keeping exact sameSize() for the viewport comparison.
  • Regression coverage (apps/extension/src/tools/__tests__/window.test.ts): Added tests covering repeated identical resizes and failed post-resize reads with DPI-rounded bounds and a frozen viewport (plus recovery once the viewport updates).
  • Changelog wording (CHANGELOG.zh-CN.md): Aligned the Chinese entry to reflect the 2 px DIP rounding tolerance to match the code and CHANGELOG.md.

Verified locally with window.test.ts (47/47), real-Chrome window.browser.test.ts (1/1), tsc --noEmit, pnpm lint, and scripts/check-changelog-sync.py. Ready to merge once CI finishes green!

@NianJiuZst
NianJiuZst merged commit e186aa7 into Tencent:main Nov 19, 2026
12 checks passed
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.

[Bug]: bsk window resize fails on Windows 10 at 125% scaling due to 1 px size mismatch

2 participants