Skip to content

Self-improving AI Employees for everyone

perf(canvas): read node specs without re-rendering on every broadcast

trohitg0ba56c8f645e
diff --git a/client/src/contexts/WebSocketContext.tsx b/client/src/contexts/WebSocketContext.tsx
index 513f42e9..0eb9bf4e 100644
--- a/client/src/contexts/WebSocketContext.tsx
+++ b/client/src/contexts/WebSocketContext.tsx
@@ -34,7 +34,7 @@ import { startWebSocketHeartbeat } from '../lib/webSocketHeartbeat';
import { todoQueryKeyFromEvent } from '../lib/todoQuery';
import { AGENT_PHASE } from '../lib/agentPhases';
import { useCanvasDockStore } from '../stores/canvasDockStore';
-// Cycle note: lib/nodeSpec imports useWebSocket from this module. Safe in
+// Cycle note: lib/nodeSpec imports useWebSocketActions from this module. Safe in
// both evaluation orders because neither side calls the other's binding at
// module top level — `executionBudgetFor` only references it inside a body.
import { getCachedNodeSpec } from '../lib/nodeSpec';
diff --git a/client/src/lib/nodeSpec.ts b/client/src/lib/nodeSpec.ts
index a2346e9c..cccebea4 100644
--- a/client/src/lib/nodeSpec.ts
+++ b/client/src/lib/nodeSpec.ts
@@ -12,7 +12,7 @@
* See C:\\Users\\Tgroh\\.claude\\plans\\typed-splashing-crown.md.
*/
-import { useEffect, useSyncExternalStore } from 'react';
+import { useCallback, useEffect, useSyncExternalStore } from 'react';
import { hashKey, useQuery, type UseQueryResult } from '@tanstack/react-query';
import { nodeSpecToDescription, type NodeSpec } from '../adapters/nodeSpecToDescription';
import type { INodeTypeDescription } from '../types/INodeProperties';
@@ -20,7 +20,9 @@ import { featureFlags } from './featureFlags';
import { BRAND_STORAGE_KEYS, readAndMigrateStorageValue } from './brandStorage';
import { queryClient } from './queryClient';
import { GC_TIME, STALE_TIME } from './queryConfig';
-import { useWebSocket } from '../contexts/WebSocketContext';
+// The actions context, not useWebSocket(): the full context value changes on
+// every broadcast, and every canvas node and palette item reads a spec.
+import { useWebSocketActions } from '../contexts/WebSocketContext';
type SendRequest = (type: string, data?: any) => Promise<any>;
@@ -122,27 +124,30 @@ export function getCachedNodeSpec(nodeType: string): NodeSpec | null {
* than a long-lived observer.
*/
export function useNodeSpec(nodeType: string | undefined | null): NodeSpec | null {
- const { sendRequest, isReady } = useWebSocket();
- const key = nodeSpecQueryKey(nodeType ?? '__none__');
+ const { sendRequest, isReady } = useWebSocketActions();
+ const specType = nodeType ?? '__none__';
+ const targetHash = hashKey(nodeSpecQueryKey(specType));
- const targetHash = hashKey(key);
- const subscribe = (onChange: () => void) => {
- const unsub = queryClient.getQueryCache().subscribe((event) => {
+ // Stable per node type, so useSyncExternalStore does not unsubscribe and
+ // resubscribe on every render.
+ const subscribe = useCallback(
+ (onChange: () => void) => queryClient.getQueryCache().subscribe((event) => {
if (event.query.queryHash === targetHash) onChange();
- });
- return unsub;
- };
+ }),
+ [targetHash],
+ );
- const getSnapshot = () =>
- queryClient.getQueryData<NodeSpec | null>(key) ?? null;
+ const getSnapshot = useCallback(
+ () => queryClient.getQueryData<NodeSpec | null>(nodeSpecQueryKey(specType)) ?? null,
+ [specType],
+ );
const data = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
useEffect(() => {
if (!nodeType || !isReady) return;
- if (queryClient.getQueryData(key) !== undefined) return;
+ if (queryClient.getQueryData(nodeSpecQueryKey(nodeType)) !== undefined) return;
void fetchNodeSpec(nodeType, sendRequest);
- // eslint-disable-next-line react-hooks/exhaustive-deps -- key derives from nodeType; including it is redundant.
}, [nodeType, isReady, sendRequest]);
return data;
@@ -156,7 +161,7 @@ export function useNodeSpec(nodeType: string | undefined | null): NodeSpec | nul
* status-check pattern: check data first, error second, loading last.
*/
export function useNodeGroups(): UseQueryResult<Record<string, NodeGroupEntry>> {
- const { sendRequest, isReady } = useWebSocket();
+ const { sendRequest, isReady } = useWebSocketActions();
return useQuery<Record<string, NodeGroupEntry>>({
queryKey: nodeGroupsQueryKey,
queryFn: async () => {