{"record":{"id":"c88c4960ededf92e","repo":"ruvnet/ruflo","slug":"training-system-not-initialized","errorCode":null,"errorMessage":"Training system not initialized","messagePattern":"Training system not initialized","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/cli/src/services/ruvector-training.ts","lineNumber":472,"sourceCode":"  MEMORY: 10,\n  REASONING: 11,\n  COORDINATION: 12,\n  OPTIMIZATION: 13,\n  SECURITY: 14,\n  TESTING: 15,\n  DEBUGGING: 16,\n} as const;\n\n/**\n * Train a pattern with MicroLoRA\n */\nexport async function trainPattern(\n  embedding: Float32Array,\n  gradient: Float32Array,\n  operatorType?: number\n): Promise<{ deltaNorm: number; adaptCount: bigint }> {\n  if (!initialized || !microLoRA) {\n    throw new Error('Training system not initialized');\n  }\n\n  // Use scoped LoRA if operator type specified\n  if (operatorType !== undefined && scopedLoRA) {\n    scopedLoRA.adapt_array(operatorType, gradient);\n    return {\n      deltaNorm: scopedLoRA.delta_norm(operatorType),\n      adaptCount: scopedLoRA.adapt_count(operatorType),\n    };\n  }\n\n  // Standard MicroLoRA adaptation\n  microLoRA.adapt_array(gradient);\n  totalAdaptations++;\n\n  return {\n    deltaNorm: microLoRA.delta_norm(),\n    adaptCount: microLoRA.adapt_count(),","sourceCodeStart":454,"sourceCodeEnd":490,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/cli/src/services/ruvector-training.ts#L454-L490","documentation":"Thrown by trainPattern() when the ruvector training module's module-level state is not ready: either initializeTraining() was never awaited in this process, or cleanup() has since run (it sets initialized=false and frees microLoRA). trainPattern needs both the initialized flag and a live MicroLoRA instance (WASM or JS-fallback). State is per-process — importing the module in a new process starts uninitialized again.","triggerScenarios":"Calling trainPattern() without a preceding await initializeTraining(); racing init (fire-and-forget initializeTraining() then immediately training); calling train after cleanup() in long-lived test harnesses; using the module from a worker process/thread where only the parent ran init.","commonSituations":"Tests that exercise trainPattern directly without a beforeAll init; refactors that moved the init call behind a lazy branch that didn't run; CLI commands that train but skip the init step under a fast path; init awaited in main but training called in a dynamically imported worker bundle.","solutions":["Await initializeTraining() once at startup (it always succeeds — WASM falls back to JS) before any trainPattern/forward/adaptWithReward calls.","If you called cleanup() (e.g. between test suites), call initializeTraining() again — the module is re-initializable.","Guard call sites with an ensureInitialized() wrapper that inits once and caches the promise to prevent concurrent double-init.","For worker processes/threads, run initializeTraining inside each worker; parent-process init does not propagate."],"exampleFix":"// before\nimport { trainPattern } from './services/ruvector-training.js';\nawait trainPattern(embedding, gradient); // throws 'Training system not initialized'\n\n// after\nimport { initializeTraining, trainPattern } from './services/ruvector-training.js';\nlet ready: Promise<unknown> | null = null;\nconst ensureTraining = () => (ready ??= initializeTraining());\nawait ensureTraining();\nawait trainPattern(embedding, gradient);","handlingStrategy":"validation","validationCode":"import { initializeTraining, trainPattern, type TrainingConfig } from './services/ruvector-training.js';\n\nlet ready: Promise<ReturnType<typeof initializeTraining>> | null = null;\nexport function ensureTraining(cfg?: TrainingConfig) {\n  return (ready ??= initializeTraining(cfg));\n}\n\n// before any trainPattern call:\nconst init = await ensureTraining();\nif (!init.success) throw new Error(`Training init failed: ${init.error ?? 'unknown'}`);\nawait trainPattern(embedding, gradient, OperatorType.GENERAL);","typeGuard":null,"tryCatchPattern":"try {\n  await trainPattern(embedding, gradient);\n} catch (e) {\n  if (e instanceof Error && e.message === 'Training system not initialized') {\n    await initializeTraining(); // idempotent bootstrap, then retry once\n    return trainPattern(embedding, gradient);\n  }\n  throw e;\n}","preventionTips":["Bootstrap with an awaited, memoized initializeTraining() before exposing any training APIs.","Treat cleanup() as terminal: re-initialize before any post-cleanup call in long-lived processes and test harnesses.","Initialize per process — worker threads and child processes do not inherit module state.","Assert on the init result (success/backend/features) at startup so failures surface at boot, not mid-training."],"tags":["initialization","ruvector","training","lora","lifecycle"],"backgroundTag":"component-not-initialized","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-08-21T13:17:26.733Z"}