Repository navigation
fix(studio): refresh profile stats and mode-aware activity summary - #9402
danielhanchen merged 6 commits into
Conversation
Update the token activity card subtitle when switching Daily, Weekly, or Cumulative views, and refetch profile stats when the tab regains focus or settings reopens so headline numbers stay current.
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
Headless browser check that the token activity subtitle updates across Daily, Weekly, and Cumulative modes against a mocked API.
|
Confirmed the subtitle in studio/frontend/src/features/profile/components/stats/token-activity-card.tsx does not follow the Daily/Weekly/Cumulative switch, and the helper plus locale updates look right. Could you drop the new puppeteer e2e files and their mock server and keep this to the card, the hook and the unit test in tests/profile-stats-format.test.ts? |
Remove the mock server and headless browser spec per review feedback. Keep the card, hook, locale updates, and profile-stats-format unit tests.
Thanks for the review, @danielhanchen I've dropped the puppeteer e2e harness and mock server. The PR now only includes the card, hook, helper, locale updates, and the unit test in [tests/profile-stats-format.test.ts Confirmed locally that the Token Activity subtitle updates across Daily / Weekly / Cumulative: Daily: 0 tokens over the last 43 weeks Let me know if anything else should change. |
|
To use Codex here, create a Codex account and connect to github. |
|
@danielhanchen Feedback addressed — ready for re-review. Dropped the puppeteer e2e harness and mock server per your Aug 22 note. This PR is now limited to the token activity card, hook, helper, locale updates, and Confirmed locally that the subtitle updates across Daily / Weekly / Cumulative. Rebased branch is current with |
The closed->open watcher in useProfileStats cannot fire. ProfileStatsContent sits inside the settings DialogContent, which is not force-mounted, so Radix unmounts the panel on close and the hook is gone before the store flips back to open. Measured on a real build: with the dialog closed the Token activity card is absent from the DOM, and reopening Settings costs exactly one /api/profile/stats request, from the mount effect. Removing the effect keeps that behaviour and drops a dependency from features/profile on features/settings. The focus/visibility refresh is the part that does work, and it stays. Verified on Chromium, Firefox and WebKit against two installs: a focus event triggers no refetch at the merge base and exactly one on this branch. Tests: - pin the refresh wiring, the unsubscribe contract, that a tab going hidden does not refetch, and that repeated mounts leave no listeners behind - assert daily and cumulative still equal the reduce this PR replaced, over 200 generated grids, so the number under every existing subtitle cannot drift - cover the shapes a real grid takes: all-padding columns and a short final week
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e5689db305
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // Reopening Settings needs no watcher of its own -- the dialog content is not | ||
| // force-mounted, so the panel unmounts on close and the effect above refetches | ||
| // when it mounts again. | ||
| useEffect(() => subscribeResidentStatusRefresh(load), [load]); |
There was a problem hiding this comment.
Coalesce duplicate foreground refreshes
When a backgrounded profile tab returns and the browser emits both visibilitychange and focus, this subscription calls load twice because subscribeResidentStatusRefresh deliberately registers both events (resident-status-refresh.ts:30-37). Although the second call aborts the first fetch, /api/profile/stats runs its potentially 1.2-second cold aggregation via asyncio.to_thread (profile_stats.py:48-53), so an already-started worker continues and two complete database/message scans can run concurrently whenever the fingerprint changed. Coalesce the paired events or use a deduplicated foreground subscription for this expensive endpoint.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
The mechanism is right, but this is duplicated work rather than a correctness problem, and it predates the PR.
Measured it on a live install rather than reasoning about it: firing the pair in one tick sends 2 requests to /api/profile/stats and completes 1 response. So yes, the losing fetch is aborted client side and the server thread keeps going, since Starlette only consumes http.disconnect for StreamingResponse and a thread handed work by asyncio.to_thread cannot be cancelled anyway (https://marcelotryle.com/blog/2024/06/06/understanding-client-disconnection-in-fastapi/, fastapi/fastapi#8805).
What that costs is bounded, though. compute_profile_stats memoises on (fingerprint, days, tz) with a 20s TTL, so the duplicate only redoes the fold in the window where history just changed, and both calls then write the same bytes. The endpoint is a read-only GET: same payload, same final state, nothing lost, nothing crashes.
The double subscribe is also a deliberate property of the shared helper, not something this PR introduces. resident-status-refresh.ts says it outright: focus alone misses a backgrounded tab, visibility alone a window that never hid, so a redundant pair of reads is cheaper than a missed switch. hub-page.tsx and use-chat-model-runtime.ts have both taken that pair since the helper was written. Coalescing inside the profile hook would make this one caller behave differently from the other two for a saving capped by a 20s memo, and coalescing in the helper is a change to Hub and chat behaviour that belongs in its own PR.
|
@codex security review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
27 comment lines to 5. Cuts narration that restated the code: what the helper replaced, what the effect below depends on, and a paragraph restating the body of the listener-leak test. Kept the four facts the code does not state: the dialog is not force-mounted, weekly is a max rather than a sum, null grid cells are normal padding, and dropping the source assertion silently restores the stale-stats bug.
|
Before/after for this change, shot from two isolated Studio installs: BEFORE is the merge base Weekly is the mode worth looking at. It is the only one where both the wording and the number move: The 300K in the new subtitle is the seeded peak week, and it matches the one tall bar in the same frame. Cumulative changes wording only, same number: Daily is deliberately not pictured. Its string is byte identical on both sides and Subtitle strings read straight off the running card:
Both halves of each image have the same mode pill selected and the same chart, and both installs reported identical data ( The refresh half of the PR is not something a screenshot can show, so I measured it instead, on all three engines against both installs. Returning focus to the tab triggers no refetch at the merge base and exactly one on this branch:
|





Fixes #9337
Problem
Changes
activitySummaryForMode()helper with unit tests; updated all 12 locale files.Testing
npm test -- tests/profile-stats-format.test.ts— all 1624 tests pass (includes newactivitySummaryForModecoverage)npx tsc --noEmit— no type errors