{"record":{"id":"42e84f4435084f75","repo":"ruvnet/ruflo","slug":"optimizer-not-initialized","errorCode":null,"errorMessage":"Optimizer not initialized","messagePattern":"Optimizer not initialized","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/cli/src/services/ruvector-training.ts","lineNumber":653,"sourceCode":"  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  }\n\n  return optimizer.step(params, gradients);\n}\n\n/**\n * Get curriculum difficulty for current step\n */\nexport function getCurriculumDifficulty(step: number): number {\n  if (!curriculum) {\n    return 1.0; // Full difficulty if no curriculum\n  }\n\n  return curriculum.getDifficulty(step);\n}\n\n/**\n * Mine hard negatives for better training","sourceCodeStart":635,"sourceCodeEnd":671,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/cli/src/services/ruvector-training.ts#L635-L671","documentation":"Thrown by optimizerStep() when the module-level optimizer is null. Like the InfoNCE loss, the AdamW optimizer (lr, 0.9, 0.999, 1e-8, 0.01) is created unconditionally whenever @ruvector/attention imports during initializeTraining() — there is no enabling flag. A null optimizer therefore means: init not awaited, the attention package unavailable (init warned and disabled attention features), or cleanup() ran.","triggerScenarios":"Calling optimizerStep(params, grads) in a training loop whose bootstrap never awaited initializeTraining(); running in a stripped install where @ruvector/attention is absent so the LoRA core came up on the JS fallback without attention/optimizer; post-cleanup usage in tests.","commonSituations":"Custom training loops written against the full feature set but deployed with an install that pruned optionalDependencies; long test files where cleanup() in one describe block poisons the next; monorepos where the package resolves in dev but not in the built artifact.","solutions":["Await initializeTraining() up front and assert features includes 'AdamW Optimizer' before entering the training loop.","Restore the optional @ruvector/attention dependency in the failing environment (check init warnings).","Pair optimizerStep with a computeContrastiveLoss/computeFlashAttention feature check — they all share the same root cause.","Re-initialize after cleanup() if the process continues."],"exampleFix":"// before\nfor (const batch of batches) {\n  params = optimizerStep(params, grads(batch)); // throws 'Optimizer not initialized'\n}\n\n// after\nconst init = await initializeTraining();\nif (!init.features.includes('AdamW Optimizer')) {\n  throw new Error('Optimizer requires @ruvector/attention');\n}\nfor (const batch of batches) {\n  params = optimizerStep(params, grads(batch));\n}","handlingStrategy":"validation","validationCode":"const init = await initializeTraining();\nif (!init.features.includes('AdamW Optimizer')) {\n  throw new Error('Optimizer requires @ruvector/attention — check optional deps');\n}\nparams = optimizerStep(params, gradients);","typeGuard":"async function optimizerReady(): Promise<boolean> {\n  const init = await initializeTraining();\n  return init.features.includes('AdamW Optimizer');\n}","tryCatchPattern":"try {\n  return optimizerStep(params, grads);\n} catch (e) {\n  if (e instanceof Error && e.message === 'Optimizer not initialized') {\n    const init = await initializeTraining();\n    if (!init.features.includes('AdamW Optimizer')) throw new Error('Missing @ruvector/attention');\n    return optimizerStep(params, grads);\n  }\n  throw e;\n}","preventionTips":["Assert 'AdamW Optimizer' in init.features once at boot; share the check with the loss/attention probes — same root cause.","Don't assume WASM LoRA success implies optimizer availability; they come from different packages.","Pair cleanup() with re-init in test harnesses that continue to optimize.","Fail the run early with a clear dependency message instead of throwing inside the training loop."],"tags":["initialization","ruvector","optimizer","adamw","optional-dependency"],"backgroundTag":"component-not-initialized","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}