Skip to content

test(renderer): pin the runtime-target selector identity stability that #18685 fixed - #18625

Merged
nwparker merged 1 commit into
mainfrom
nwparker/perf-ports-runtime-target-identity
Sep 4, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/perf-ports-runtime-target-identity

Conversation

@nwparker

@nwparker nwparker commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

ELI5

A hook that tells the ports UI which machine owns a workspace was building its answer object fresh inside the store subscription. React compares by identity, so a brand-new object always looks "changed" — and the always-mounted status-bar Ports segment re-rendered on every single write to the app store.

That fix has since landed on main via #18685. It landed without a test. This PR is that test.

What Changed

Test only. src/renderer/src/runtime/use-worktree-runtime-target.test.ts renders the hook, drives 50 unrelated store writes, and asserts two things: the consumer renders exactly once, and the returned target keeps its referential identity.

No production code — main already has the fix.

Why

I found and fixed this independently while auditing renderer render-churn; #18685 shipped the same change first (useAppStore(state => getExecutionHostIdForWorktree(...)) then useMemo), so I dropped my duplicate implementation and rebased this branch down to the regression guard.

Worth keeping, because the bug is invisible without one: the hook returns a structurally correct value either way. Only the identity differs, so nothing fails, nothing looks wrong, and the cost shows up as unexplained baseline render churn that scales with store traffic. A future refactor could reintroduce it silently. This test fails loudly instead — verified at 51 renders vs 1 against the pre-#18685 hook.

Measurements

before #18685 after
renders of a consumer across 50 unrelated store writes 51 (1 mount + 50) 1 (mount only)
returned target identity stable across those writes no yes

Negative user-facing trade-offs

None — this PR contains no production code. It adds one test file.

Merge risk

Very low. Test-only, on the current main, covering a fix that is already merged. The worst case is a flaky test, and it is not timing-based: it counts renders and compares object identity.

Linked Issue

No tracked issue — found by a repository-wide performance audit. Related: #18685.

Visual Proof

N/A — no production change.

Testing

  • I manually tested these changes locally
  • Automated tests added/updated

pnpm test src/renderer/src/runtime/use-worktree-runtime-target.test.ts passes on current main, and was confirmed to fail against the pre-#18685 hook.

Platforms: renderer-only, no platform-dependent behavior.

AI Disclosure

@coderabbitai

coderabbitai Bot commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: 46decd60-e693-4bbc-9e6b-561491f7a93b

📥 Commits

Reviewing files that changed from the base of the PR and between 62a9f29 and a3eb0e8036304caf2801b6b5f38e53557cc9931f.

📒 Files selected for processing (1)
  • src/renderer/src/runtime/use-worktree-runtime-target.test.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 6 remain after this review.


📝 Walkthrough

Walkthrough

The hook now selects the execution host ID and memoizes the derived runtime target by that ID. A new test performs repeated unrelated activeRepoId updates and verifies that the hook does not re-render and that the runtime target reference remains unchanged.

Merge Risk: 🔵 Low · up to ff98c

The runtime target change avoids rerenders from unrelated store updates, but the new test does not verify target identity when the hook itself rerenders. This is a bounded regression-coverage gap and should be addressed before relying on the test as protection for the memoization behavior.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description includes useful context, testing details, and visual-proof justification, but it is incomplete and conflicts with the changeset. It states that the PR is test-only and contains no prod… Update the description to accurately document both the production hook change and the regression test. Provide a valid linked issue or explain the required exception. Complete the Review, Agent skill upstream boundary, Notes, and Checklist …
✅ Passed checks (4 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 3 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly identifies a renderer test that protects runtime-target selector identity stability and references the related fix.
Full details: Description check

Explanation

The description includes useful context, testing details, and visual-proof justification, but it is incomplete and conflicts with the changeset. It states that the PR is test-only and contains no production code, while the summary shows production changes in use-worktree-runtime-target.ts. It also does not provide the required linked issue, Review section, Agent skill boundary checklist, Notes, or repository checklist.

Resolution

Update the description to accurately document both the production hook change and the regression test. Provide a valid linked issue or explain the required exception. Complete the Review, Agent skill upstream boundary, Notes, and Checklist sections, including the required validation commands and their results.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
⚔️ Resolve merge conflicts 💡
  • Resolve merge conflict in branch nwparker/perf-ports-runtime-target-identity
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Team

Run ID: 136d5dbb-0222-45b0-8a13-288b72467238

📥 Commits

Reviewing files that changed from the base of the PR and between 637dc30 and 62a9f29.

📒 Files selected for processing (2)
  • src/renderer/src/runtime/use-worktree-runtime-target-identity.test.ts
  • src/renderer/src/runtime/use-worktree-runtime-target.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 3 remain after this review.

Comment on lines +31 to +34
// A selector that built the target object inline re-rendered on every write.
expect(renders).toBe(rendersAfterMount)
expect(result.current).toBe(first)
})

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.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Add a direct rerender assertion for the memoization contract.

The current test only performs store writes. Because the selector now returns a primitive, those writes do not rerender the hook, so the identity assertion does not exercise useMemo. A regression that removes useMemo would still pass this test.

Trigger one explicit hook rerender and assert that result.current remains first before applying the store writes.

Proposed test adjustment
-  const { result } = renderHook(() => {
+  const { result, rerender } = renderHook(() => {
     renders += 1
     return useWorktreeRuntimeTarget('worktree-1')
   })

   const first = result.current
   const rendersAfterMount = renders

+  rerender()
+  expect(result.current).toBe(first)
+  const rendersAfterRerender = renders
+
   act(() => {
     for (let index = 0; index < 50; index += 1) {
       useAppStore.setState({ activeRepoId: `repo-${index}` })
@@
-  expect(renders).toBe(rendersAfterMount)
+  expect(renders).toBe(rendersAfterRerender)

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

✅ No new issues found.

Reviewed changes

  • use-worktree-runtime-target.ts — the selector now returns the primitive ExecutionHostId string and derives the target object via useMemo([hostId]), stabilizing identity across unrelated store writes. This mirrors the existing useAgentDetectionTargetForWorktree pattern.
  • use-worktree-runtime-target-identity.test.ts — a new guard that renders the hook, drives 50 unrelated activeRepoId writes, and asserts both the render count and the returned target identity are unchanged.

The fix is correct: runtimeTargetForExecutionHostId is a pure function of hostId, and getExecutionHostIdForWorktree returns a plain string (compared by value under Zustand's Object.is), so selecting the string is referentially stable and useMemo cannot go stale. I confirmed the new test passes against the fixed hook and fails against the pre-change implementation (the old selector trips getSnapshot should be cached / "Maximum update depth exceeded" under happy-dom).

The useMemo is load-bearing, not redundant: PortsStatusSegment and the sidebar port rows pass runtimeTarget into useCallback deps, so keeping it identity-stable across parent re-renders (not just store writes) is what keeps those callbacks asleep. The lint rule no-fresh-selector-result correctly stays silent here because the selector returns a call expression, and extending it is reasonably left out of scope.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) | 𝕏

#18685 landed the fix (select the host id, derive in useMemo) without a
regression guard. This is that guard: it drives 50 unrelated store writes and
asserts the consumer re-renders once and the returned target keeps its identity.
Confirmed to fail against the pre-#18685 hook (51 renders).
@nwparker
nwparker force-pushed the nwparker/perf-ports-runtime-target-identity branch from a3eb0e8 to ff98c61 Compare September 4, 2026 21:14
@nwparker nwparker changed the title perf(renderer): stop the ports runtime-target selector re-rendering on every store write test(renderer): pin the runtime-target selector identity stability that #18685 fixed Sep 4, 2026
@nwparker
nwparker merged commit f46823a into main Sep 4, 2026
27 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#18625)

stablyai#18685 landed the fix (select the host id, derive in useMemo) without a
regression guard. This is that guard: it drives 50 unrelated store writes and
asserts the consumer re-renders once and the returned target keeps its identity.
Confirmed to fail against the pre-stablyai#18685 hook (51 renders).
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.

1 participant