From 600c8b1c17b45cb4d58d355f1848d550364a30a9 Mon Sep 17 00:00:00 2001 From: Matt Toohey Date: Wed, 26 Aug 2026 21:50:24 +1000 Subject: [PATCH] refactor(desktop): ref the whole mention-wake options object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit useMentionWakePreflight destructured its ten options into locals and then reassembled them into an identical object twice — once as the useRef initializer, once as the per-render assignment. Both were hand-maintained mirrors of the parameter list: adding an option without updating them silently froze that field at its mount-time value, with no type error. Keep the options object as the parameter and ref it directly (contextRef -> optionsRef). There is no field list left to maintain — a new option is automatically fresh in optionsRef.current, so drift is impossible by construction. The hook also stops allocating a second context object per render on top of the caller's literal. The invalidation effect still destructures the subset it depends on, but that list is not a mirror: it is the deliberately narrower set of "which prop changes re-arm the preflight", excluding getManagedAgentsByPubkey and startManagedAgent as the existing biome-ignore documents. useEffectEvent would delete the ref entirely, but prepareMentionWake escapes the hook into MessageComposer's keystroke path, which violates its "call only from inside Effects" contract. The latest-ref pattern stays; it just no longer needs a hand-copied field list. The public parameter type is unchanged, so the call site and every existing test compile and pass untouched. Signed-off-by: Matt Toohey --- .../messages/ui/useMentionWakePreflight.ts | 63 +++++++------------ 1 file changed, 23 insertions(+), 40 deletions(-) diff --git a/desktop/src/features/messages/ui/useMentionWakePreflight.ts b/desktop/src/features/messages/ui/useMentionWakePreflight.ts index 3d0d7e9989..d83b77ba13 100644 --- a/desktop/src/features/messages/ui/useMentionWakePreflight.ts +++ b/desktop/src/features/messages/ui/useMentionWakePreflight.ts @@ -69,18 +69,7 @@ export function buildMentionWakePlan({ return { key: `${channelId}:${pubkeys.join(",")}`, pubkeys }; } -export function useMentionWakePreflight({ - channelId, - contentRef, - enabled, - expectedRelayUrl, - expectedSignerPubkey, - getDraftMentionRefs, - getManagedAgentsByPubkey, - isManagedAgentPubkey, - memberPubkeys, - startManagedAgent, -}: { +export function useMentionWakePreflight(options: { channelId: string | null; contentRef: React.MutableRefObject; enabled: boolean; @@ -94,30 +83,10 @@ export function useMentionWakePreflight({ }) { const timerRef = React.useRef | null>(null); const activePlanKeyRef = React.useRef(null); - const contextRef = React.useRef({ - channelId, - contentRef, - enabled, - expectedRelayUrl, - expectedSignerPubkey, - getDraftMentionRefs, - getManagedAgentsByPubkey, - isManagedAgentPubkey, - memberPubkeys, - startManagedAgent, - }); - contextRef.current = { - channelId, - contentRef, - enabled, - expectedRelayUrl, - expectedSignerPubkey, - getDraftMentionRefs, - getManagedAgentsByPubkey, - isManagedAgentPubkey, - memberPubkeys, - startManagedAgent, - }; + // Latest-ref over the whole options object: the stable callbacks below read + // fresh values without a hand-maintained field list that could drift. + const optionsRef = React.useRef(options); + optionsRef.current = options; const cancelMentionWake = React.useCallback(() => { if (timerRef.current !== null) clearTimeout(timerRef.current); @@ -126,7 +95,7 @@ export function useMentionWakePreflight({ }, []); const currentPlan = React.useCallback((content: string) => { - const context = contextRef.current; + const context = optionsRef.current; if ( !context.enabled || !context.expectedRelayUrl || @@ -157,6 +126,20 @@ export function useMentionWakePreflight({ : null; }, []); + // Not a mirror of the options object: this is the subset whose change should + // re-arm the preflight, deliberately excluding getManagedAgentsByPubkey and + // startManagedAgent (see the biome-ignore below). + const { + channelId, + contentRef, + enabled, + expectedRelayUrl, + expectedSignerPubkey, + getDraftMentionRefs, + isManagedAgentPubkey, + memberPubkeys, + } = options; + const prepareMentionWake = React.useCallback( (content: string) => { const plan = currentPlan(content); @@ -172,12 +155,12 @@ export function useMentionWakePreflight({ const beforeLookup = currentPlan(contentRef.current); if (beforeLookup?.key !== plan.key) return; const managedAgents = - await contextRef.current.getManagedAgentsByPubkey(); + await optionsRef.current.getManagedAgentsByPubkey(); if (activePlanKeyRef.current !== plan.key) return; const beforeWake = currentPlan(contentRef.current); if (beforeWake?.key !== plan.key) return; - const context = contextRef.current; + const context = optionsRef.current; await Promise.allSettled( beforeWake.pubkeys.flatMap((pubkey) => { const agent = managedAgents.get(pubkey); @@ -202,7 +185,7 @@ export function useMentionWakePreflight({ [cancelMentionWake, contentRef, currentPlan], ); - // biome-ignore lint/correctness/useExhaustiveDependencies: contextRef keeps callbacks fresh; these inputs are plan invalidation signals. + // biome-ignore lint/correctness/useExhaustiveDependencies: optionsRef keeps callbacks fresh; these inputs are plan invalidation signals. React.useEffect(() => { prepareMentionWake(contentRef.current); }, [