{"record":{"id":"67c6ec8ddbd3bf59","repo":"ruvnet/ruflo","slug":"contrastive-loss-not-initialized","errorCode":null,"errorMessage":"Contrastive loss not initialized","messagePattern":"Contrastive loss not initialized","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/cli/src/services/ruvector-training.ts","lineNumber":636,"sourceCode":"  values: Float32Array[]\n): Float32Array {\n  if (!hyperbolicAttention) {\n    throw new Error('Hyperbolic attention not initialized');\n  }\n\n  return hyperbolicAttention.computeRaw(query, keys, values);\n}\n\n/**\n * Compute contrastive loss for training\n */\nexport function computeContrastiveLoss(\n  anchor: Float32Array,\n  positives: Float32Array[],\n  negatives: Float32Array[]\n): { loss: number; gradient: Float32Array } {\n  if (!contrastiveLoss) {\n    throw new Error('Contrastive loss not initialized');\n  }\n\n  const loss = contrastiveLoss.compute(anchor, positives, negatives);\n  const gradient = contrastiveLoss.backward(anchor, positives, negatives);\n\n  return { loss, gradient };\n}\n\n/**\n * Optimizer step\n */\nexport function optimizerStep(\n  params: Float32Array,\n  gradients: Float32Array\n): Float32Array {\n  if (!optimizer) {\n    throw new Error('Optimizer not initialized');\n  }","sourceCodeStart":618,"sourceCodeEnd":654,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/cli/src/services/ruvector-training.ts#L618-L654","documentation":"Thrown by computeContrastiveLoss() when contrastiveLoss is null. The InfoNCE loss (temperature 0.07) is constructed by initializeTraining() whenever the optional @ruvector/attention package imports — it has no config flag — so a null here means init never ran, the package was unavailable (init logged a warning and skipped ALL attention features), or cleanup() nulled it.","triggerScenarios":"Computing contrastive loss before awaiting initializeTraining(); running in an install where @ruvector/attention failed to resolve (pruned optional dependency, broken hoisting) while the WASM/JS LoRA core initialized fine, masking the gap; calling after cleanup().","commonSituations":"Training pipelines that only use LoRA (which works without @ruvector/attention) and later add a contrastive-loss step that silently requires it; CI caching node_modules from a --production install; upgrading package managers and losing optional deps.","solutions":["Await initializeTraining() and assert features includes 'InfoNCE Loss' before the training loop starts.","If the feature is missing, install/restore @ruvector/attention (npm install without --omit=optional) — the LoRA backend initializing successfully does NOT imply attention features are present.","Check init's console output for '[ruvector] @ruvector/attention unavailable' to distinguish missing-package from missing-init.","Re-initialize after cleanup()."],"exampleFix":"// before\nawait initializeTraining(); // may skip attention features silently\nconst { loss, gradient } = computeContrastiveLoss(anchor, pos, neg); // throws\n\n// after\nconst init = await initializeTraining();\nif (!init.features.includes('InfoNCE Loss')) {\n  throw new Error('Contrastive loss requires @ruvector/attention — reinstall deps');\n}\nconst { loss, gradient } = computeContrastiveLoss(anchor, pos, neg);","handlingStrategy":"validation","validationCode":"const init = await initializeTraining();\nif (!init.features.includes('InfoNCE Loss')) {\n  throw new Error('Contrastive loss requires @ruvector/attention — reinstall optional deps');\n}\nconst { loss, gradient } = computeContrastiveLoss(anchor, positives, negatives);","typeGuard":"async function contrastiveReady(): Promise<boolean> {\n  const init = await initializeTraining();\n  return init.features.includes('InfoNCE Loss');\n}","tryCatchPattern":"try {\n  return computeContrastiveLoss(anchor, pos, neg);\n} catch (e) {\n  if (e instanceof Error && e.message === 'Contrastive loss not initialized') {\n    const init = await initializeTraining();\n    if (!init.features.includes('InfoNCE Loss')) throw new Error('Missing @ruvector/attention');\n    return computeContrastiveLoss(anchor, pos, neg);\n  }\n  throw e;\n}","preventionTips":["Validate the full capability set (LoRA backend ≠ attention features) on the init result before training loops start.","Remember a successful JS-fallback LoRA init says nothing about InfoNCE — the packages are independent.","Keep @ruvector/attention in package.json (not just as a transitive optional) if you compute losses.","Fail fast at startup on missing features rather than mid-epoch."],"tags":["initialization","ruvector","contrastive-loss","infonce","optional-dependency"],"backgroundTag":"component-not-initialized","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}