Skip to content

fix(mochi): restore Save as Preset focus after the close commits - #29959

Merged
KiroCrewCaptain merged 1 commit into
mainfrom
crew/bug-resolve/29897-mochi-focus
Nov 5, 2026
Merged

KiroCrewCaptain merged 1 commit into
mainfrom
crew/bug-resolve/29897-mochi-focus

Conversation

@CrysisDeu

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: Saving a Mochi color preset with Enter always puts focus back on the Save as Preset button, so MochiSavePresetFocus stops flaking.

Frontend Tests (3) failed once on PR #10838 (run 41864791468, job 126702263259): Enter saves and returns focus to the Save as Preset button expected the button to have focus and found <body>. That head already contained #28515, the earlier requestAnimationFrame -> setTimeout(0) attempt, so that fix did not close the race.

Why it matters

The flake reds unrelated PRs and costs reruns. The same race can strand keyboard focus on body for a real user after a slow save.

Not a goal

  • Not changing any other focus-return site.
  • Not changing the test file.

Intended? / Repro

Intended? No. The comment above closeSaveForm on main says every close hands focus back to the trigger (website/src/apps/mochi/src/renderer/ColorCustomizer.tsx:136-153). Focus left on body breaks that contract.

Repro: CI log above, commit 0e5052dfae3eea1aa5fd18ed284ceb84072b566b (608 commits ahead of #28515's d6d40e23a2, so it carries the setTimeout fix). Input: type Sunset in the name field, press Enter. Seen: focus on <body> instead of the Save as Preset button. Passed on rerun of the same job.

What changed (motivation → approach → change)

Symptom: focus sometimes lands on body after an Enter save.

Root cause: handleSaveAsPreset awaits presetsSaveCustom, then calls closeSaveForm. That update is outside any event handler, so React schedules its commit on a separate task. closeSaveForm queued its focus check with setTimeout(0), which is another separate task with no ordering guarantee against React's. When the timer ran first it saw the input still focused, skipped the restore, and then React unmounted the input and focus fell to body.

Change: closeSaveForm now sets a ref and closes the field. A useLayoutEffect keyed on showSaveForm runs right after the commit that remounts the trigger and does the same "only if focus is on body" restore. This is the pattern website/src/apps/crew-companion/ColorCustomizerPanel.tsx already uses on main.

Alternative considered: keep the timer and retry it until the trigger is mounted. Rejected: it is still timing-based and needs a cap, while the effect runs exactly once after the commit.

Backwards compatibility

Compatible: same focus rule (restore only when focus was dropped to body), now applied after the commit instead of on a timer.

Removes nothing: no symbol, prop or behavior is removed.

Tests

Proof: not provable -- frontend test (prove.py runs Python only)

website/src/test/MochiSavePresetFocus.test.tsx already covers every close path (Enter, Escape, confirm, cancel, slow save after the user moved away). It is unchanged; it was flaky only because the product code raced. Locally only static gates ran (tsc --noEmit, eslint); CI runs the tests.

Manual verification

N/A — unit coverage sufficient: the test drives every close path through the real component.

Screenshots / video

Why no screenshot: Only the moment focus is restored changes; nothing renders differently.

Related Issues

Fixes #29897

Sibling sweep

Swept: git grep -n "closeSaveForm" -- website/src
Sibling: website/src/apps/crew-companion/ColorCustomizerPanel.tsx -- not affected -- already restores focus from a layout effect on main
Sibling: website/src/apps/mochi/src/renderer/ColorCustomizer.tsx -- changed here

Pattern harvest

Rule candidate: review-prompt
Pattern: focus restore scheduled with setTimeout/requestAnimationFrame after an await races React's commit; restore from a layout effect keyed on the state that remounts the target.

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.

The Save as Preset field returned focus through setTimeout(0) after an
awaited save. React schedules that update on its own task, so the timer
could run first, see the input still focused, skip the restore, and
leave focus on body once the input unmounted. MochiSavePresetFocus
flaked on exactly that.

Restore focus from a layout effect that runs after the commit that
closes the field, the same pattern the crew-companion copy uses.

Fixes #29897
@CrysisDeu
CrysisDeu requested review from a team and KiroCrewCaptain as code owners November 4, 2026 22:03
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 4, 2026
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

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

Design-Verdict: PASS

Nothing blocks: the focus restore now runs in a layout effect after the commit that remounts the trigger, which removes the timer race at its root.

[DESIGN-REVIEWED] 5d4cfc3

Description read

[DESCRIPTION-READ] fb762bde5b4eafecbc27b65f8ede5ad4ac07d6b6a067cda02fbe7c6afddc393c

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 4, 2026

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ✅ PASS

UX-level review of 5d4cfc31ef725336f6da555886fe9aa396fd06f0 — 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 focus returns to the Save as Preset button. It adds or changes no visible control, label or state, so the missing blind read leaves no evidence gap.

[UX-REVIEWED] 5d4cfc3

Description read

[DESCRIPTION-READ] c591cba39ab7ea4e719bc255ebd07a9b5f83c26462cf3080dc8d9880b236e9e2

That sha256 names everything this verdict read: the PR description, plus the 3 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 4, 2026

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 5d4cfc31ef725336f6da555886fe9aa396fd06f0 — 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. The fix moves the focus restore onto React's commit, which is where the race comes from. The same timer-after-await pattern is still used in five other places.

Notes

  • Item 1 — symptom-level (point patch, 5 unfixed siblings): I grepped at === document.body under website/src. Five sites still restore focus with requestAnimationFrame instead of after the commit: apps/file-explorer/TabStrip.tsx:39, pages/knowledge/SourcesList.tsx:422, pages/knowledge/DetailView.tsx:100, pages/useArtifactDetailPageEditor.ts:182, pages/overview/VectorMemoryCard.tsx:258. SourcesList and DetailView explicitly say a close "can resolve after" an await, which is the race this PR names. The PR's "Not changing any other focus-return site" keeps them out of scope, so this is deferred, not demanded. Subtraction: when these are swept, replace each requestAnimationFrame restore with the layout-effect form, so each site has one restore path instead of a timer.

What this change ships

Inventory (2 items) — 1 justified

Intent: after saving a Mochi preset with Enter, keyboard focus reliably goes back to the Save as Preset button. This is a FIX (#29897, CI run linked).

  1. Focus goes back to Save as Preset after the close commits, not on a timer — symptom-level (5 sibling sites still use timers)
  2. Comment above the restore rewritten to describe the commit-ordered rule — justified

[FIRST-PRINCIPLES-REVIEWED] 5d4cfc3

Description read

[DESCRIPTION-READ] 706607c3a77519634556ba4a84261445c9f36802ba6710a374820bbd58475089

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 4, 2026

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] 5d4cfc3

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

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

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

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

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 5d4cfc3

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

@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 4, 2026
@KiroCrewCaptain
KiroCrewCaptain enabled auto-merge (squash) November 5, 2026 00:04

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

Tier 1 auto-approve: fix (1 file). Criteria: no conflict, no requested changes, security path denylist clean, design-doc gate clean, security checklist all-NO, no reviewer-lane findings, readiness signal fresh for this head SHA, declared category matches the diff. Category: Mochi Save as Preset focus is restored in a layout effect after the trigger remounts, replacing a timer that could fire before the commit.

@KiroCrewCaptain
KiroCrewCaptain merged commit d199728 into main Nov 5, 2026
150 of 154 checks passed
@KiroCrewCaptain
KiroCrewCaptain deleted the crew/bug-resolve/29897-mochi-focus branch November 5, 2026 00:05
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Nov 5, 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.

Flaky: MochiSavePresetFocus Enter saves and returns focus to the Save as Preset button

2 participants