@@ -4562,25 +4562,52 @@ function NodeCanvas() {
45624562 const nodeData = nodes.find(n => n.id === instanceId);
45634563 if (nodeData) {
45644564 const prototypeId = nodeData.prototypeId;
4565- if (nodeData.definitionGraphIds && nodeData.definitionGraphIds.length > 0) {
4565+ const currentState = useGraphStore.getState();
4566+ const prototypeData = currentState.nodePrototypes.get(prototypeId);
4567+
4568+ if (prototypeData?.definitionGraphIds && prototypeData.definitionGraphIds.length > 0) {
45664569 // Node has definitions - start hurtle animation to first one
4567- const graphIdToOpen = nodeData .definitionGraphIds[0];
4570+ const graphIdToOpen = prototypeData .definitionGraphIds[0];
45684571 startHurtleAnimation(instanceId, graphIdToOpen, prototypeId);
45694572 } else {
4570- // Node has no definitions - create one first, then start hurtle animation
4571- const sourceGraphId = activeGraphId; // Capture current graph before it changes
4572- storeActions.createAndAssignGraphDefinitionWithoutActivation(prototypeId);
4573-
4574- setTimeout(() => {
4575- const currentState = useGraphStore.getState();
4576- const updatedNodeData = currentState.nodePrototypes.get(prototypeId);
4577- if (updatedNodeData?.definitionGraphIds?.length > 0) {
4578- const newGraphId = updatedNodeData.definitionGraphIds[updatedNodeData.definitionGraphIds.length - 1];
4579- startHurtleAnimation(instanceId, newGraphId, prototypeId, sourceGraphId);
4580- } else {
4581-
4573+ // No definitions recorded. Self-heal: find any existing graph that defines this prototype
4574+ const sourceGraphId = activeGraphId;
4575+ let orphanGraphId = null;
4576+ try {
4577+ for (const [gId, g] of currentState.graphs.entries()) {
4578+ if (Array.isArray(g.definingNodeIds) && g.definingNodeIds.includes(prototypeId)) {
4579+ orphanGraphId = gId;
4580+ break;
4581+ }
45824582 }
4583- }, 50);
4583+ } catch (_) { }
4584+
4585+ if (orphanGraphId) {
4586+ console.log('[Expand] Found orphan definition graph. Repairing and opening.', {
4587+ prototypeId,
4588+ orphanGraphId
4589+ });
4590+ // Self-heal: add to prototype.definitionGraphIds
4591+ storeActions.updateNodePrototype(prototypeId, draft => {
4592+ draft.definitionGraphIds = Array.isArray(draft.definitionGraphIds) ? draft.definitionGraphIds : [];
4593+ if (!draft.definitionGraphIds.includes(orphanGraphId)) {
4594+ draft.definitionGraphIds.push(orphanGraphId);
4595+ }
4596+ });
4597+ startHurtleAnimation(instanceId, orphanGraphId, prototypeId, sourceGraphId);
4598+ } else {
4599+ // No existing definition anywhere - create one
4600+ storeActions.createAndAssignGraphDefinitionWithoutActivation(prototypeId);
4601+
4602+ setTimeout(() => {
4603+ const updatedState = useGraphStore.getState();
4604+ const updatedNodeData = updatedState.nodePrototypes.get(prototypeId);
4605+ if (updatedNodeData?.definitionGraphIds?.length > 0) {
4606+ const newGraphId = updatedNodeData.definitionGraphIds[updatedNodeData.definitionGraphIds.length - 1];
4607+ startHurtleAnimation(instanceId, newGraphId, prototypeId, sourceGraphId);
4608+ }
4609+ }, 50);
4610+ }
45844611 }
45854612 }
45864613 }
0 commit comments