Skip to content

refactor(desktop): stabilize app-shell action factory identities - #1162

Merged
Astro-Han merged 1 commit into
mainfrom
refactor/1043-app-shell-stable-actions
Jul 17, 2026
Merged

Astro-Han merged 1 commit into
mainfrom
refactor/1043-app-shell-stable-actions

Conversation

@Astro-Han

@Astro-Han Astro-Han commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Refs #1043 (step 1 of the plan documented there).

The 9 createAppShell*Actions factories ran unmemoized in the AppShell render body, so every render allocated fresh handler identities. The one observable harm: the streaming-settle fallback effect lists settleAssistantStreaming in its deps, so its 1s timer tore down and re-armed on every render while armed — under continuous render churn the fallback could be postponed indefinitely.

Rather than patching the one effect, this fixes the defect class. The new useStableActions still runs the factory every render (closures always capture the latest deps, identical to calling it bare), but returns a once-created delegating facade whose method identities never change. Publication to the facade happens in useLayoutEffect, so only the latest committed render's actions are reachable — an interrupted or discarded concurrent render cannot leak its closures to event handlers, timers, or subscriptions. The settle timer now arms once by construction, and any future effect-dep or memoized-child consumer is safe by the same mechanism.

  • createDelegatingActions is pure and React-free so the behavior is testable through the repo's node:test chain; source contracts pin the wiring at all 9 call sites and forbid the render-phase publication this design rejected.
  • The hand-rolled sessionRowActionHandlersRef mirror is removed — superseded by the facade. The fail-soft contract moves to the facade pattern with unchanged semantics (dedup registry, no fire-and-forget void).
  • createAppShellStopAction deliberately stays unwrapped: it returns a bare function (no object to facade) and only feeds JSX props, never effect deps.

Not TDD for the timer behavior itself — the repo has no React-rendering test harness; identity stability is pinned by unit + source contracts and the streaming journey by the existing E2E below.

Verification

  • tsc -p tsconfig.main.json and tsconfig.renderer.json — clean.
  • node --test dist/main/**/*.test.js — 2650/2651. The one failure (menu-highlight-recipe-contract) fails identically on clean main; unrelated, not touched here.
  • Contract: app-shell-stable-actions-contract.test.ts (7 tests — facade delegation/identity, constant key set under shape change, wiring, commit-time publication).
  • npm run test:checks (console / a11y / copy) — clean; biome lint on changed files — clean.
  • Playwright e2e/send-message.spec.ts (fake-backend streaming journey through the settle path) — passes.

Review focus

An external review round (Codex) flagged that the first iteration published latestRef.current during render, which under concurrent React can expose closures from an uncommitted render. Fixed at the owner: publication moved to useLayoutEffect, render-phase writes limited to lazy initialization, and a source contract now forbids the rejected pattern. One known pre-existing instance of the old pattern remains at openSessionInChatRef (single-function mirror, not a factory) — out of scope here, candidate for a follow-up sweep.

The 9 createAppShell*Actions factories ran unmemoized in the AppShell
render body, so every render allocated fresh handler identities. The
one observable harm: the streaming-settle fallback effect lists
settleAssistantStreaming in its deps and therefore tore down and
re-armed its 1s timer on every render while armed (#1043).

Introduce useStableActions: the factory still runs every render (so
closures always capture the latest deps, identical to calling it
bare), but consumers receive a once-created delegating facade whose
method identities never change. Publication to the facade happens in
useLayoutEffect, so only the latest COMMITTED render's actions are
reachable — an interrupted or discarded concurrent render cannot leak
its closures to event handlers, timers, or subscriptions (render-phase
ref writes are limited to the lazy initialization React permits). The
settle timer now arms once by construction, and any future effect-dep
or memoized-child consumer is safe by the same mechanism.

- createDelegatingActions (pure, React-free) pinned by node:test unit
  tests; source-contract tests pin the wiring at all 9 call sites and
  forbid the render-phase publication this design rejected
- remove the hand-rolled sessionRowActionHandlersRef mirror superseded
  by the facade; the fail-soft contract moves to the facade pattern
  with unchanged semantics (dedup registry, no fire-and-forget void)
- createAppShellStopAction stays unwrapped: it returns a bare function
  and only feeds JSX props, never effect deps
@Astro-Han
Astro-Han force-pushed the refactor/1043-app-shell-stable-actions branch from 5427669 to 09534b4 Compare July 17, 2026 06:37
@Astro-Han
Astro-Han merged commit 1414a62 into main Jul 17, 2026
3 checks passed
@Astro-Han
Astro-Han deleted the refactor/1043-app-shell-stable-actions branch July 24, 2026 09:05
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