Skip to content

perf(website): split the two long tasks of a cold chat open - #37706

Merged
KiroCrewCaptain merged 1 commit into
mainfrom
crew/kiro-crew-rust/yield-url-open
Nov 16, 2026
Merged

KiroCrewCaptain merged 1 commit into
mainfrom
crew/kiro-crew-rust/yield-url-open

Conversation

@buluoray

@buluoray buluoray commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Opening the dashboard on a chat blocks input for less time: the longest main-thread task of the open gets shorter, and the transcript appears no later.

A cold open of /chat?sid=<key> runs two long main-thread tasks. The longest, about 150 ms, is the task the session list arrives in: it renders the sidebar, and the effect that opens the URL's session runs in the same task and mounts the transcript too. The second, about 115 ms, is the app's first render, run synchronously.

Why it matters

A click or a key press that lands during a long task waits until the task ends. The dashboard's own target for this page is no task over 100 ms.

Not a goal

  • Changing what the user sees while the page opens. The session still opens only once the session list names it. There is no new blank state and no panel that mounts later.
  • First paint time.
  • URL sync's record of an uncommitted write. The history tests reach a second push only by ordering router updates in a way the real router does not (see Tests).

What changed (motivation → approach → change)

The two long tasks are each two pieces of work in one task. Each piece is given a task of its own.

Part What it does
The URL open website/src/pages/chat/useChatPageSessionController.ts: once the effect that opens ?sid= finds its session in the list, it waits one turn of the main thread and then opens. The sidebar's render and the transcript's mount are separate tasks
yieldToMain() website/src/lib/yieldToMain.ts: scheduler.yield(), which resumes ahead of other queued tasks; a zero-delay timer in a browser without it
The first render website/src/main.tsx: startTransition(() => root.render(...)). React renders the app's first render in slices and yields between them

While the open waits, nothing differs from the wait for the session list: the link stays armed, the URL is not rewritten, and the "session not found" timer keeps its meaning. The turn opens only what a run of the effect that passed every guard left for it. A session the user picks meanwhile, a re-run that fails a guard, or an unmount takes that away, and the store is read once more at the turn so a pick that has not rendered yet wins too. After the first open the effect opens at once, as on main.

Measurements

All numbers are mine, from one shared Linux host. A node server on 127.0.0.1 serves the built dashboard and answers /api from scripts/lib/stub-dashboard-api.mjs with scripts/lib/perf-fixture.mjs (222 sessions, the 100-row chat): no gateway and no model are involved. chrome-headless-shell (Chromium 145), no DevTools client and no CPU throttling, opens /chat?sid=perf-long cold with a fresh profile. Every counted run rendered the target content, read from the DOM: 15 transcript rows, 28 session rows, the transcript scrolled to its end. Arms interleave in each round with rotating order, and a pair counts only when both start loads are under 15 and within 2 of each other.

Two runs of 10 rounds, load 6.5-14.9, 19 pairs. main is d470c46; "main again" is the same build as a second arm.

ms main this PR paired difference main again (A/A)
Longest task 151 94.5 −61 (lower in 19 of 19 pairs) +5
Second longest task 114 79.5 −35 +1
Sum of long tasks −41 +4
Session rows in the DOM 960 923 −58 +12
Transcript in the DOM 1178 1217 +16 −4
FCP 644 670 +16 +8
Renderer CPU, 6 s 5070 5040 +10 −40
  • The longest task is at or under 100 ms in 14 of 20 opens (range 79-122). On main it is in 0 of 40 (range 143-210).
  • The transcript, FCP and CPU differences are inside what main shows against itself, so this run does not show the open getting slower. The transcript's +16 ms is 1.4% of the open.
  • Each part alone, in one of those runs (9 pairs): the URL open's turn alone takes the longest task to 114 ms (−35, 0 of 10 opens at or under 100 ms); with the first render as a transition it is 92 ms (−55).

With 40 ms added to every response, on this revision's build against main @ 4dec84c (10 rounds, load 3.1-7.5, 10 pairs): the longest task goes from 142 to 92 ms (−52; A/A −1) and is at or under 100 ms in 7 of 10 opens. The transcript is at +6 ms (A/A +3), FCP at +10 ms (A/A −12), session rows at −74 ms (A/A −8).

The loopback table's build is this revision's code before two comment edits and one added test. Not measured: a real gateway, Safari and Firefox (neither has scheduler.yield() in every version, so they take the timer), a phone, a throttled CPU.

Backwards compatibility

Compatible. The session opens one main-thread turn later than on main; what opens, and every case in which the link is abandoned, is the same. Checked in Chromium on both builds: after the open history.length is the same, a session switch adds one entry, and Back and Forward land on the sessions they name.

In a hidden tab of a browser without scheduler.yield(), timers run about once a second, so the session can open up to a second later there.

Removes nothing.

Tests

  • website/src/lib/yieldToMain.test.ts (3 tests, new): scheduler.yield is used where it exists; the fallback settles in a later task and not as a microtask; a scheduler without yield takes the fallback.
  • website/src/test/ChatPage.sidOpenTurn.test.tsx (3 tests, new; a file of its own because ChatPage.sid.test.tsx is at the 2,000-line cap): the URL session is not open when mount returns with the list present, the URL is untouched meanwhile, and it opens after one timer turn; a session picked during that turn wins and the URL session never comes on screen; a page that unmounts during that turn opens nothing. With main's controller the first and third fail.
  • website/src/test/main.firstRender.test.ts (2 tests, new): main.tsx renders the root once, inside startTransition. A pin on the source, like the file's other pins: the module boots the app as it is imported.
  • Three existing tests change with the timing and keep what they assert:
    • website/src/test/ChatPage.backBeforePushCommits.test.tsx (5 tests): the helper openOrigin is documented to "let its URL settle on a committed entry", and waited for window.location. The open's slug replace is issued outside the mount's act, so the address bar shows it before the router commits it. The helper waits for the committed location, read in a layout effect. Each test's steps and assertions are unchanged.
    • ChatPage.sid.test.tsx, "does not declare a session missing while the slot list has not arrived": the fake clock advances inside act, so React runs the update the turn schedules.
  • Why those five tests failed before the helper waited for the commit: the helper's hold() kept back the router update of the next write while the open's slug replace was already delivered and uncommitted, so the replace committed alone and URL sync wrote the switch a second time. The real router does not produce that order. Both writes update one router state, and React commits them together. Checked on main's controller with the real <BrowserRouter>, React's own scheduling and no held history: a title change, then a switch 0 to 45 ms later while the replace's 30 ms transition renders, gives one commit and one pushState in 12 of 12 timings. In Chromium on the main build, 11 timings from 0 to 60 ms: one entry each, and one Back returns. So the second push is a state only the harness reaches, and the helper stays out of it. URL sync is not changed.
  • Determinism: the five changed or new files ran 5 times in order and 3 times with --sequence.shuffle (seeds 7919, 15838, 23757): 105 of 105 each time. test/test_flake_pattern_ratchet.py passes.
  • Ran: every test file that names ChatPage, a sid= URL, the controller or main.tsx (643 files, 12,006 tests); scripts/run_scoped_tests.py --surface frontend; tsc -p tsconfig.app.json; eslint on the changed files; scripts/check_file_line_cap.py; jscpd; the repository's semgrep rules on the diff; i18n:check, lint:i18n, gen:ui --check, the theme-colour, phantom-class and restyle gates. All pass.
  • Proof: not provable -- frontend test (prove.py runs Python only)

Manual verification

In the built dashboard (stubbed API, headless Chromium 145), main and this branch side by side: opened /chat?sid=perf-long, waited for the transcript, picked another session in the sidebar, pressed Back, pressed Forward. Both builds gave the same URL and the same history.length at each step (2, 3, 3, 3), the same 15 transcript rows after the open, and no page error. Not verified: Safari, Firefox, a phone, a real gateway.

Screenshots / video

Cold open of /chat?sid=perf-long, main on the left and this branch on the right, played at half speed. The page looks the same in both; the readout above each side is that take's own longest main-thread task and the time its transcript reached the DOM.

Cold open of a chat, main and this branch side by side

recorded from 1a4bb63 (right), an earlier head of this branch with the same non-test code, and main @ 4dec84c (left) · production builds · stubbed API and fixture, no gateway and no model · Playwright's recorder attached, which slows both sides: the takes read 151 ms and 109 ms where the unattended runs above read 151 ms and 94.5 ms. Four takes a side, alternating order, load 7.6-8.7; the GIF shows the take at each side's median.

Related Issues

N/A. From the Chrome modern-web-guidance review of the dashboard (break-up-long-tasks, schedule-tasks-by-priority).

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.

@buluoray
buluoray requested review from a team and KiroCrewCaptain as code owners November 15, 2026 21:32
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 15, 2026
@buluoray

Copy link
Copy Markdown
Contributor Author

Before and after, a cold open of /chat?sid=perf-long, main on the left and this branch on the right, at half speed:

Cold open of a chat, main and this branch side by side

recorded from 1a4bb63 (right) and main @ 4dec84c (left) · production builds · stubbed API and fixture on 127.0.0.1, no gateway and no model · Playwright's recorder attached

The page looks the same on both sides, which is the point: nothing the user sees moves. What differs is the longest main-thread task of the open, printed above each side for that take (151 ms and 109 ms with the recorder attached; 151 ms and 94.5 ms in the unattended paired runs in the description).

Paired runs, longest task, ms:

Run main this PR paired A/A at or under 100 ms
Loopback, 19 pairs, load 6.5-14.9 151 94.5 −61 +5 14 of 20 opens
40 ms per response, 10 pairs, load 3.1-7.5 142 92 −52 −1 7 of 10 opens

Every counted open rendered 15 transcript rows and 28 session rows, read from the DOM.

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of a21683a1a421e553f24b0b6d2483acd71a1b1e49 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: both long tasks are split at their real seams, the deferred open is guarded against a re-run, a user pick and an unmount, and the change can be reverted cleanly.

Notes

  • yieldToMain's timer fallback can stall up to about a second in a hidden tab on Safari and Firefox. The doc comment already names MessageChannel as the fix. It is reasonable to switch now, because opening a link in a background tab is exactly the ?sid= cold-open case.

[DESIGN-REVIEWED] a21683a

Description read

[DESCRIPTION-READ] 4b14190d8d62fa4e616cf99911a298453f860205dd97784f8d1b5e5bcbb56f96

That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of a21683a1a421e553f24b0b6d2483acd71a1b1e49 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

First-Principles-Verdict: PASS

Nothing blocks: both timing changes rest on a paired measurement, and the one new helper has a single direct consumer.

Not checked: Safari and Firefox use the timer fallback, and that path is unmeasured.

What this change ships

Inventory (5 items) — 5 justified

Intent: shorten the longest main-thread task of a cold /chat?sid= open so input is not blocked, without delaying the transcript. FIX (performance).

  1. On a cold open, the URL's session opens one main-thread turn after the sidebar renders, instead of in the same task. — justified
  2. The app's first render runs as a React transition, in slices that yield to input. — justified
  3. A new yieldToMain() helper: scheduler.yield() where the browser has it, otherwise a zero-delay timer. It is distinct from pierreStaging.afterPaint, which waits for idle time. — justified
  4. In a hidden tab without scheduler.yield(), the session can open up to about 1 s later. — justified
  5. The test helper openOrigin now waits for the router's committed location, not window.location. — justified

[FIRST-PRINCIPLES-REVIEWED] a21683a

Description read

[DESCRIPTION-READ] ed5d3a4b428cbfc1f912b2c1073f9108265fcbf743893ad1bb809c3e3f05568e

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ✅ PASS

UX-level review of a21683a1a421e553f24b0b6d2483acd71a1b1e49 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

UX-Verdict: PASS

Nothing to check. The diff only changes when work runs on a cold chat open. It adds or changes no control, label, string or visible state.

[UX-REVIEWED] a21683a

Description read

[DESCRIPTION-READ] 22f53f8cbf737571245c2d1cf8398d52b9d8d77d702bbf772deec056c39e2d29

That sha256 names everything this verdict read: the PR description, plus the 4 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed a21683a1a421e553f24b0b6d2483acd71a1b1e49 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] a21683a

Verdict parsed from the review's SHA-scoped output markers for commit a21683a1a421e553f24b0b6d2483acd71a1b1e49.

False positive or not applicable? A repository writer can comment:
/ai-review override fable a21683a1a421e553f24b0b6d2483acd71a1b1e49 false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of a21683a1a421e553f24b0b6d2483acd71a1b1e49 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] a21683a

False positive or not applicable? A repository writer can comment:
/ai-review override gpt a21683a1a421e553f24b0b6d2483acd71a1b1e49 false-positive: <reason, 20 to 500 characters>

@buluoray

Copy link
Copy Markdown
Contributor Author

Correction to this PR's description (no code change, head is still 1a4bb63).

The first version said the five history tests "exposed" a second pushState that is "on main too" and needs its own fix. That was wrong, and the description no longer says it.

What I had: with main's controller, the second push shows when the test harness releases its held router updates one at a time. What I had not checked: whether the real router can commit them in that order. It does not.

Check on main's controller Result
Harness, held updates released one at a time second pushState
Real <BrowserRouter>, React's own scheduling, no hold: title change, then a switch 0 to 45 ms later, mid-transition one commit, one pushState, 12 of 12 timings
Same, two switches 0 to 70 ms apart one pushState per switch, 12 of 12
Chromium on the main build: title frame, then a pick 0 to 60 ms after the slug replace one entry, one Back returns, 11 of 11

Both writes update one router state, and React commits pending transitions on one state together, so the earlier write never commits alone under a later one. The harness's hold() could split them only because it started between the two.

So the helper change in ChatPage.backBeforePushCommits.test.tsx stands as described: it waits for the committed location so each test starts outside a state the product does not reach. URL sync is untouched, and no follow-up fix is planned. The "Not a goal" bullet and the Tests bullet about this are reworded to match.

@buluoray
buluoray force-pushed the crew/kiro-crew-rust/yield-url-open branch from 1a4bb63 to 41290d1 Compare November 15, 2026 22:03
@buluoray

Copy link
Copy Markdown
Contributor Author

Red check on 1a4bb63, read from the job log: Frontend Ratchets & i18n failed at "Check file line cap". Mine: the three new tests took website/src/test/ChatPage.sid.test.tsx from 2,000 to 2,040 lines, over the 2,000-line cap. In 41290d1 they live in a new sibling, ChatPage.sidOpenTurn.test.tsx, and ChatPage.sid.test.tsx is back at 2,000 (its one changed line stays one line). scripts/check_file_line_cap.py passes locally. No product code changed between the two heads.

@buluoray

Copy link
Copy Markdown
Contributor Author
  • Deferred URL activation overrides a newer New chat request span=f0a681263df6

Rebutted: disproportionate, and not a path this PR opens. The same click wins or loses the same way on main for the whole wait for the session list: the URL-open effect's guards read only activeSlot, and createSlot.pending (website/src/store/chat/lifecycle.ts:552) leaves activeSlot unchanged, so a New chat pressed before the list arrives is followed by switchSlot(urlSlot) there too.
This PR adds one main-thread turn to that wait (scheduler.yield() or a zero-delay timer). A guard for that one turn would make it behave differently from the hundreds of ms before it, for a press that has to land inside a single task.
The new chat is still created and listed, so one click opens it. The adjudication on this head and on 1a4bb63 reached the same call.
The main comparison is from reading the two code paths, not from a test.

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Nov 15, 2026
@KiroCrewCaptain

KiroCrewCaptain commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: A person should decide

Why: the decider thinks the change is probably fine but estimates a 15% chance that this change introduces a defect, above the 12% limit for approving on its own.

Approve. The deferred ?sid= open waits in the same state as on main while the session list is arriving: initialSidRef stays set, so auto-select and the not-found timer behave the same, and popInFlightRef stays armed, so URL sync is held. Every effect re-run clears the queued open through its cleanup, StrictMode's run-twice reuses the one turn already asked for, and the store is read again when the turn runs. After the first open, later runs open at once. Rendering the root inside startTransition on React 18.3 changes nothing that depended on a synchronous commit, the index.html boot watchdog is reversible, and the existing source pin still matches. New tests cover the delay, a pick winning and an unmount during the wait, and CI is green.

An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit a21683a1a4 · 322 changed lines. If this recommendation is wrong, reply here: replies are how the approver is evaluated.

@github-actions github-actions Bot added the merge conflict Branch has merge conflicts with its base — author must resolve before merge label Nov 15, 2026
Opening the dashboard on /chat?sid=<key> ran two long main-thread tasks.
The longest was the one the session list arrived in: it rendered the
sidebar, and the effect that opens the URL's session ran in the same
task and mounted the transcript too. The second was the app's first
render, run synchronously.

The URL open waits one turn of the main thread once it finds its session
in the list (website/src/lib/yieldToMain.ts: scheduler.yield(), or a
zero-delay timer where the browser has none). While it waits the link
stays armed exactly as it was before the list arrived. The turn opens
only what a run of the effect that passed every guard left for it, so a
session picked meanwhile, a re-run or an unmount cancels it.

main.tsx hands the first render to React as a transition, so it is
rendered in slices.

Three tests change with the timing and keep their invariant. The history
tests in ChatPage.backBeforePushCommits wait for the location the router
committed, not for the address bar, before they start: the open's slug
replace is issued outside the mount's act. One fake-timer test in
ChatPage.sid advances its clock inside act.
@buluoray
buluoray force-pushed the crew/kiro-crew-rust/yield-url-open branch from 41290d1 to a21683a Compare November 15, 2026 23:35
@buluoray

Copy link
Copy Markdown
Contributor Author

Conflict cleared in a21683a. #37584 merged (fa4e40d) and added an import above the React import in website/src/main.tsx, the line this PR adds startTransition to. Both stay; nothing else in the diff changed. After the rebase: tsc, the line cap, the five test files of this PR and the four suites that pin main.tsx by source pass (138 tests).

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: passed Eligible automated validation passed for the current revision merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running labels Nov 15, 2026
@KiroCrewCaptain
KiroCrewCaptain enabled auto-merge (squash) November 16, 2026 01:17

@KiroCrewCaptain KiroCrewCaptain 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.

Approving. On this head the review traced every way the deferred ?sid= open can end: a re-run during the wait, a user pick, a disconnect, the list dropping the session, unmount, StrictMode double mount and embed mode. Each one holds, and the existing guards (URL sync hold, auto-select, mount refetch) behave as they do on main. It also checked that startTransition around the first render leaves the boot-failure panel alone, that the new tests use fake timers and fail when the deferral is reverted, and that the two edits to existing tests keep their assertions. Done well: paired, interleaved measurements with an A/A control, and the body says plainly what was not measured.
Optional:

  • useChatPageSessionController.ts:637 and :652: deleting the 'taken' branch or the store re-read at the turn fails no test. A disconnect-then-reconnect case and a pick dispatched outside act would pin them.
  • useChatPageSessionController.ts:641: the comment says only the cold open waits a turn. A warm in-app navigation to /chat?sid=X also waits one turn. That is harmless, but the comment could say so.
  • ChatPage.backBeforePushCommits.test.tsx:180: the reason for the stricter precondition is written down only in the PR body. One test using the real BrowserRouter that asserts a single pushState would pin it.

@KiroCrewCaptain
KiroCrewCaptain merged commit a96bb41 into main Nov 16, 2026
99 checks passed
@KiroCrewCaptain
KiroCrewCaptain deleted the crew/kiro-crew-rust/yield-url-open branch November 16, 2026 01:18
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Nov 16, 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