Skip to content

fix(core): a retarget after a late first frame continues from the screen - #9401

Merged
cixzhang merged 1 commit into
meta:mainfrom
vjeux:uikit/f1-animator-late-first-frame
Nov 2, 2026
Merged

cixzhang merged 1 commit into
meta:mainfrom
vjeux:uikit/f1-animator-late-first-frame

Conversation

@vjeux

@vjeux vjeux commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

User impact

A follow-up to #9255 in the touch presentations' animator, which is internal and drives no shipped component yet, so the changeset is [chore]. Under CPU contention, a retarget made before Chromium reported a call's start could restart the curve from its first value while the screen already had it moving, and the screen stepped back. #9255 covers the same retarget when the call's first frame comes promptly; this covers a first frame that comes late with no block of the page's own.

Problem and solution fit

  • Why it steps back. Chromium's compositor starts a composited animation at the frame that commits it and tells the page that start a frame or more later. The animator's first frame after a call made in a task starts the call itself when no start has been reported, but only when that frame comes within LATE_START_MS (34 ms) of the call. After a longer gap that frame cannot tell a call made in a task from one a page's own frame callback made, so the animator waits for the browser's start. Under contention the first frame alone can come that late; until the start notice arrives, the call reads as not started, and a retarget then starts from the curve's first value.
  • The fix. A first frame that leaves the start unknown records the time it ran, for each channel whose animation is still running. Until the browser reports the start, reads and retargets of that channel take the curve from that time instead of from its first value, both at the current time and at a later one (a companion that samples ahead). The curve is dated as the browser's start would date it, so a curve that joined the call by an older clock (an event's timestamp) keeps its lead over the call's own time. The browser's start, once read, still decides, as before. Chromium starts a call made in a task at the frame it draws after the one that commits it, within about a frame of that time; a call an earlier frame committed (a page's own frame callback) started before it, by up to the gap between the two frames. Either way the read is closer to the screen than main's, which takes the curve's first value.
  • Rebuilds in that window. A rebase(), a set() or a retarget with a later clock that rebuilds one channel's animation after that frame makes a new pending animation, which the browser starts from its first keyframe at the frame that commits it. So that channel's recorded time is cleared before the rebuild reads it, its reads take the rebuilt animation's first value, and that next frame records its time again. The inline value and the hit-test origin the rebuild writes show that first value too. The call's other channels keep their own times. A retarget that joins the waiting call by its clock rebuilds the channel the same way. A retarget of the other axis made without a clock takes the channel's curve into its own call and starts it again from its first value; that rebuild also clears the channel's time on the call it takes the curve from, so its reads, inline value and hit-test origin show that first value.

Expected behavior and authority

spec:AST-094 FR6, as #9255 holds it: a retarget made before the page can read a call's start begins where the screen shows the element, and the screen never steps back.

Smallest restoration

Within the animator: the first-frame callback records latest, per channel, on a call it leaves waiting; valueAt and velocityAt read a waiting channel's curve from its latest once it is set, at any later time, dated as the browser's start would date it (datedFromLatest); and rebuild clears the channel's latest on each waiting call it restarts a curve from (a call it joins after that call's first frame, and a call whose curve it takes into its own) before it reads the value for the inline style and the hit-test origin, and records it again at the next frame. Nothing else changes: the first-frame start, the frame-callback and block exclusions, syncGroup and startAfterBlock are as on main.

Evidence

  • Reproduction before the change (main): traced at 6 workers in Chromium pinned to 2 cores, "a retarget made before the page can read a call's start begins where the screen shows the element" failed 4 of 36 runs. In each, the first frame came 57 to 74 ms after the call, the animation was still pending with no start time at the retarget, the page read 0, and the next frame stepped back from where the screen had it; every passing run had its first frame within 34 ms of the call.
  • Result after the change: with the first frame's delay recorded in every run, main's animator and this change alternated in rounds of 24 runs at 8 workers on the same 2-core pin, three rounds on this pull request's first version and three on its second (one recorded time per call; a single channel reads the same there as here). Main failed 28 of 72 and 18 of 72: every run whose first frame came more than 34 ms after the call (27 and 16 runs, 34 to 218 ms; the page read 0 and the screen stepped back), plus three runs that failed only the case's frame-count precondition (9 frames, prompt first frames). This change failed 0 of 72 and 2 of 72: one frame-count precondition (a prompt first frame) and one of its 27 late-first-frame runs, described under Known limit; the others read 70 to 91 px under way at the retarget.
  • Unit: nine new cases in animator.test.ts, each failing on main's animator (5a4e8fa) and passing here:
    • "a late first frame that leaves the start unread: a read and a retarget take the curve from that frame" ("…the browser's start, once read, still decides" passes on both);
    • "a rebase after a late first frame reads what the rebuilt animation shows, from the first value it starts at the frame that commits it", and the same for "a set of the other axis", which also fail on this pull request's first version. They also check that the inline value is the first keyframe and that the presented offset is zero, which fail on its third version;
    • "a retarget of the other axis without a clock after a late first frame reads the first value the adopted curve starts from" and "a retarget that joins a call by its clock after a late first frame reads the first value the rebuilt animation starts from" check the reads, the inline value and a zero presented offset; they fail on its fourth version;
    • "a curve that joins a call by an older clock reads, after a late first frame, where the compositor has it": {x: 300}, then {y: 50} joining with a clock 10 ms older; get('x') and get('y') equal the joined animation's keyframes 100 ms past the first frame. It fails on its fourth version, which read the joined curve 10 ms late;
    • "a rebase of one channel after a late first frame keeps the call's other channels reading from that frame", and the same for "a set of the other axis": a {x: 300, opacity: 0} call, then rebase({x: -16}) or set({y: 10}), where get('opacity') follows the untouched fade and an opacity retarget starts there. They fail on the second version and on a mutant that clears every channel's time;
    • "a late first frame that leaves the start unread: a read of a later time takes the curve from that frame, as the velocity does": get('x', 1186) and getVelocity('x', 1186) at 1170 take the curve from 1070. It fails where only reads up to now use the recorded time.
  • Known limit: the recorded time is near the compositor's start, not exact. Chromium starts a call made in a task at the frame it draws after the one that commits it, which the page cannot see from its frame callback. Measured against the browser's reported start in late first frames (8 workers on the 2-core pin), the callback's time was 3.2 ms after it once, and 2.1 and 15.8 ms before it in the other two; the frame's own timestamp was a whole frame or more early every time, so it is not used. So 1 of the 27 late-first-frame runs with this change still stepped back, by 3 px (first frame 34.5 ms after the call; read 82.9 px with 86 shown), where main stepped back to 0 in all 43. The browser's start, once reported, decides. Separately, a retarget of the other axis made without a clock after a late first frame still restarts the first axis from its first value at the next commit, so the screen steps back there, as on main; a follow-up carries that axis on from the screen. The same follow-up covers the presented offset a touch-down reads, which still takes a waiting curve's value at its own start (not the first keyframe, for a curve that joined by an older clock) in that window, as on main.
  • Representative unchanged path: every other case in the animator's unit file passes unchanged, and so do the animator's browser cases below: a call whose first frame comes promptly, or whose start the browser has reported, reads and retargets as on main.

Status at this head: build, type check, test:ui (17,806), the node suites (15,728, with 2 expected failures), check:repo, ASTRYX_STRICT_LINT=1 pnpm lint (0 errors), check:changesets and changeset coverage pass. The animator spec passes ×3 in Chromium pinned to 2 cores (54 of 54), the retarget case ×36 at 6 workers on that pin (36 of 36), and ×10 under the CI WebKit job's command (140 passed, 40 skipped by design). Merged with main, strict lint with the comment-provenance rule is clean on the changed files, and this change adds no provenance baseline entry.

Scope

  • One defect is restored.
  • Separable contradictory or unsettled tagalongs were removed or split.
  • No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
  • Public text and artifacts contain no internal Meta context.

Testing

pnpm exec vitest run --project ui packages/core/src/motion/animator.test.ts
pnpm test:interaction packages/core/src/motion/__tests__/animator.interaction.browser.spec.ts --project chromium --workers=1

For contention, pin Chromium to 2 cores (taskset -c 2,3) and run the retarget case at 6 workers. For WebKit, use the CI WebKit job's container command (internal/interaction-harness/scripts/webkit-container.mjs) with podman --cpus=4, CI=1, LP_NUM_THREADS=4 and 1 worker.

@vercel

vercel Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 2, 2026 9:01pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 1, 2026
@astracat-bot

astracat-bot Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 2, 2026, 9:14 PM UTC

@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · 1d1e2fa

Storybook · Sandbox · built from this PR's head

Package size: @astryxdesign/core 5.77 MB packed (+1.3 kB); no new dependencies.

API changes: none in this pull request

Committed API report behind this build: core 1 row; run pnpm api:report.

Not run on this PR: add ci:run-visual, ci:run-a11y or ci:run-rtl to run them.


Generated by PR Enrichment workflow | View full report

@cixzhang cixzhang added the ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) label Nov 1, 2026
@cixzhang
cixzhang marked this pull request as ready for review November 1, 2026 23:49

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes requested: the late-first-frame bound fixes the direct retarget, but a rebase() or set() in the same window keeps latest on the rebuilt animation's group, so get() and the next retarget read ahead of the screen. In a unit fixture (first frame at 1070 ms with the start unread, then rebase({x: -16}) at 1170 ms), the rebuilt animation starts at 91.3 px while get('x') reads 204.3 px, and a retarget one frame later starts at 217.0 px. Please clear latest when commit adds a pending animation to a waiting group outside that group's own call, re-arm it from the rebuilt animation's first frame, and add a unit case for a rebase after a late first frame.

[Automated review]

Comment thread packages/core/src/motion/animator.ts Outdated
Comment on lines +1091 to +1097
const latest = segment.group?.latest;
if (latest == null || time <= latest) {
return segment.at(0);
}
return time - latest >= segment.duration
? segment.target
: segment.at(time - latest);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

After a late first frame, a rebase() (or a set() on the other axis) rebuilds the channel as a new pending animation in this same group, but latest still dates the cancelled animation's first frame. This then reads the rebuilt curve, which already starts at the value at the rebase, time - latest in. In a unit fixture (first frame at 1070 ms, rebase({x: -16}) at 1170 ms), the rebuilt animation starts at 91.3 px while get('x') reads 204.3 px, and a retarget one frame later starts at 217.0 px. Clearing latest when commit adds a pending animation to a waiting group outside its own call, and re-arming it from that animation's first frame, would keep reads on the screen.

@vjeux
vjeux force-pushed the uikit/f1-animator-late-first-frame branch from f63753a to 4d01619 Compare November 2, 2026 04:59
@vjeux

vjeux commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 4d01619: when rebuild adds a pending animation to a waiting call's group outside that call (a rebase, a set, another axis's retarget) after the call's first frame, it clears latest and records it again at the next frame, the one that commits the rebuilt animation. Until then, reads take the rebuilt animation's first value, which is what the screen shows. Two new unit cases, a rebase and a set of the other axis after a late first frame, fail on f63753a and on each mutant (keeping latest through the rebuild; clearing it without recording it again), and pass here.

While checking this, I measured Chromium's reported start against the recorded time. It is near the compositor's start, not exact, so I reworded the comments and the changeset that called it the latest possible start. The PR body's Known limit gives the numbers.

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes requested: the late-first-frame read and the same-channel rebuild path now hold, but latest is one value per call while rebuild replaces only one channel's animation. In a call that moves translate and opacity, a rebase({x: -16}) or set({y: 10}) after a late first frame clears latest for both (packages/core/src/motion/animator.ts:1464), so get('opacity') drops from 0.642 (0.643 on screen) to 1.0, reads 0.982 a frame later while 0.507 is on screen, and an opacity retarget there jumps back. Please keep the recorded time per channel, clear and re-arm only the rebuilt channel, and add a unit case for a two-channel call with a rebase of translate.

[Automated review]

Comment thread packages/core/src/motion/animator.ts Outdated
// frame that commits it, from its first keyframe, not where the old
// one had got to. Reads take its first value until that frame, which
// dates its start again.
waiting.latest = undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This clears the recorded time for every channel of the call, but rebuild replaces only channel's animation. In an {x: 300, opacity: 0} call with a late first frame, rebase({x: -16}) here makes get('opacity') read 1.0, then 0.982 after the next frame, while the untouched opacity animation shows 0.643, then 0.507, so an opacity retarget jumps back. Keeping latest per channel, cleared and re-armed only for channel, keeps the other channels' reads.

@vjeux

vjeux commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in ae3c1dc: the recorded time is kept per channel. The first frame records it for each moving channel of the call. A rebuild outside the call clears and re-arms only the rebuilt channel's, and valueAt/velocityAt read their own channel's. Two new unit cases cover it: a {x: 300, opacity: 0} call with the first frame at 1070 ms unread, then rebase({x: -16}) or set({y: 10}) at 1170 ms. In both, get('opacity') tracks what the untouched fade shows, then and a frame later, and an opacity retarget starts there. Both fail on 4d01619 and on a mutant that clears every channel's time, and pass here.

A read of a later time now takes the curve from the recorded time too, as the velocity already did: at 1170, get('x', 1186) and getVelocity('x', 1186) read the curve 116 ms in, not 186 ms. A new case covers it.

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes requested: the per-channel recorded time fixes the late-first-frame reads and both earlier rebuild findings, but rebuild still reads the old frame's time when it sets the hit-test origin and the inline translate (packages/core/src/motion/animator.ts:1311 and :1327), because the channel's time is cleared only later, at :1469. After animate({x: 300}), a first frame at 1070 ms with the start unread, and rebase({x: -16}) at 1170 ms, the inline translate is 204.3 px while the rebuilt animation starts at 91.3 px, and presentedOffsetOf reports −113 px, then 0 for the rest of the curve while the element is drawn 113 px from where its animation started. Please clear the rebuilt channel's time before those reads, and assert the inline value and a zero presented offset in the rebase and set cases at animator.test.ts:939 and :962.

[Automated review]

Comment thread packages/core/src/motion/animator.ts Outdated
// starts at the frame that commits it, from its first keyframe. Its
// reads take that first value until then, and that frame dates its
// start again. The call's other channels keep their own.
waiting.latest?.delete(channel);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This delete runs after rebuild has already read valueAt for hitTested (line 1311) and for the inline write (line 1327), so both come from the old frame's time instead of this animation's first keyframe. With a first frame at 1070 ms and the start unread, rebase({x: -16}) at 1170 ms writes 204.3 px inline while the first keyframe is 91.3 px, and presentedOffsetOf reports −113 px, then 0 for the rest of the curve while the element is drawn 113 px from where its animation started. Clearing this channel's time before those reads keeps them on the first keyframe.

Comment thread packages/core/src/motion/animator.ts Outdated
Comment on lines +1082 to +1083
* its start until its first frame runs. After that frame, the curve is read
* from that frame, at any time. Without that frame, a later time reads the

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking: "After that frame, the curve is read from that frame, at any time" does not hold for a channel a rebuild cleared, a paused animation, or a time at or before that frame. A shorter version that keeps only the reason: "Until the browser reports its start, a held curve shows its first value. A frame that ran without that start dates the curve. Without that frame, a later time reads from the provisional start, so a companion that samples ahead follows the curve's shape."

@vjeux
vjeux force-pushed the uikit/f1-animator-late-first-frame branch from ae3c1dc to 6c4fb4a Compare November 2, 2026 20:05
@vjeux

vjeux commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 6c4fb4a: rebuild now clears the rebuilt channel's recorded time before it reads the value for the hit-test origin and the inline style, so both show the rebuilt animation's first keyframe. The rebase and set cases now also assert that the inline translate is that first keyframe and that presentedOffsetOf reports 0 right after the rebuild; both fail on ae3c1dc and pass here. The valueAt comment takes your shorter wording.

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes requested: the late-first-frame reads, and the rebase and set paths with their inline value and hit-test origin, now hold, but a retarget of the other axis without a clock still reads the old call's recorded time. After animate({x: 300}), a first frame at 1070 ms with the start unread, and animate({y: 50}) at 1170 ms, rebuild writes x = 107.3 px as the inline value and the hit-test origin (packages/core/src/motion/animator.ts:1328 and :1344) before it adopts the x curve, whose rebuilt animation starts at 0, so presentedOffsetOf stays 107.3 px off for the rest of the curve where main reports it correctly. Please clear the rebuilt channel's time on the call the rebuild adopts or joins before those reads (the waiting !== callGroup condition at :1317 skips both), and add a unit case for the other-axis retarget that asserts the inline first keyframe and a zero presented offset.

[Automated review]

Comment thread packages/core/src/motion/animator.ts Outdated
.find(group => group != null && !group.resolved) ??
null;
const rebuiltAfterFirstFrame =
waiting != null && waiting !== callGroup && waiting.framed === true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This skips both rebuilds that run inside a call: a retarget of the other axis without a clock (a new call, which adopts the old x curve at lines 1378-1396) and an explicit-clock join (callGroup === waiting). In the first, with a first frame at 1070 ms unread and animate({y: 50}) at 1170 ms, the hit-test origin and the inline x below read 107.3 px from the old call's time while the rebuilt animation starts at x = 0, so presentedOffsetOf stays 107.3 px off for the rest of the curve. Clearing this channel's time on every waiting call the rebuild takes a curve from, before those reads, keeps them on the first keyframe.

The animator's first frame after a call made in a task starts that call
itself when the browser has not reported a start yet, but only when that
frame comes within LATE_START_MS of the call: after a longer gap it cannot
tell a call made in a task from one a page's own frame callback made, and it
waits for the browser's start. Under CPU contention the first frame alone
can come late, with no block of the page's: Chromium's compositor starts
the call at that frame and tells the page a frame or more later, and a read
or a retarget in between read the call as not started. A retarget then
restarted the curve from its first value while the screen had it moving,
so the screen stepped back (the retarget proof failed 4 of 36 runs pinned
to 2 cores at 6 workers, each with a first frame 57 to 74 ms after the
call).

A first frame that leaves the start unknown now records, for each moving
channel, the time it ran. Chromium's compositor starts a call made in a
task at the frame it draws after the one that commits it, within about a
frame of that time, and one an earlier frame committed before it. Until the
browser reports its start, which still decides, reads and retargets of that
channel take the curve from that frame, at now and at later times, dated
as the browser's start would date it: a curve that joined the call by an
older clock keeps its lead over the call's own time. A
rebase, a set or a retarget with a later clock that rebuilds one channel's
animation after that frame starts a new animation at the frame that commits
it, from its first keyframe: that channel's reads take that first value
until then, and that frame dates its start again. The rebuild clears that
channel's time before it writes the inline value and the hit-test origin,
so both show that first keyframe too. The call's other channels keep their
own times.

A retarget that joins the waiting call by its clock rebuilds the channel
the same way. A retarget of the other axis made without a clock after a
late first frame takes the first axis's curve into its own call and still
restarts it from its first value at the next commit, as on main. In both,
the rebuild clears that channel's time on each waiting call it joins or
takes a curve from, so its reads, the inline value and the hit-test origin
show that first value.
@vjeux
vjeux force-pushed the uikit/f1-animator-late-first-frame branch from 6c4fb4a to 1d1e2fa Compare November 2, 2026 20:51
@vjeux

vjeux commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Fixed in 1d1e2fa: rebuild now clears the rebuilt channel's recorded time on every waiting call it restarts a curve from before it reads the value for the hit-test origin and the inline style: the call it joins after that call's first frame (the explicit-clock join included) and, for a call of its own, each waiting call whose curve it takes (the other-axis retarget without a clock). Two new cases cover the other-axis retarget and the explicit-clock join; each asserts the reads, the inline first keyframe and a zero presented offset, and both fail on 6c4fb4a.

Also in this head: a held curve read from the recorded time is now dated as the browser's start would date it, so a curve that joined the call by an older clock keeps its lead over the call's own time. A third new case covers that join; it fails on 6c4fb4a, which read the joined curve 10 ms late.

@astracat-bot astracat-bot Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ready to approve: after a late first frame, reads and same-axis retargets now continue from where the compositor has the element, as spec:AST-094 FR6 requires, and the rebuild-path issues raised on earlier revisions are fixed. Seven of the new unit cases fail against the base animator, and all 212 cases in the file pass at this head. Two optional touch-ups: trim the valueAt comment that repeats the datedFromLatest JSDoc, and correct the description's note that all nine listed cases fail on main (the other-axis and clock-join rebuild cases pass there, since they guard state this change adds).

[Automated review]

Comment on lines +1102 to +1104
// The compositor starts the call near its first frame. The curve is
// dated as the browser's start would date it. A curve that joined by an
// older clock keeps its lead over the call's own time.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Non-blocking: the second and third sentences repeat the datedFromLatest JSDoc a few lines up. Keeping only the platform reason reads as:

// The compositor starts the call near its first frame.

@cixzhang
cixzhang merged commit 4a7a6f2 into meta:main Nov 2, 2026
60 checks passed
@vjeux
vjeux deleted the uikit/f1-animator-late-first-frame branch November 2, 2026 21:35

This branch was successfully deployed

1 active deployment
Preview — 1d1e2fa1 Deployed Nov 2, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants