ruvnet/ruflo · error · Error

Training system not initialized

Error message

Training system not initialized

What it means

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.

Source

Thrown at v3/@claude-flow/cli/src/services/ruvector-training.ts:472

  MEMORY: 10,
  REASONING: 11,
  COORDINATION: 12,
  OPTIMIZATION: 13,
  SECURITY: 14,
  TESTING: 15,
  DEBUGGING: 16,
} as const;

/**
 * Train a pattern with MicroLoRA
 */
export async function trainPattern(
  embedding: Float32Array,
  gradient: Float32Array,
  operatorType?: number
): Promise<{ deltaNorm: number; adaptCount: bigint }> {
  if (!initialized || !microLoRA) {
    throw new Error('Training system not initialized');
  }

  // Use scoped LoRA if operator type specified
  if (operatorType !== undefined && scopedLoRA) {
    scopedLoRA.adapt_array(operatorType, gradient);
    return {
      deltaNorm: scopedLoRA.delta_norm(operatorType),
      adaptCount: scopedLoRA.adapt_count(operatorType),
    };
  }

  // Standard MicroLoRA adaptation
  microLoRA.adapt_array(gradient);
  totalAdaptations++;

  return {
    deltaNorm: microLoRA.delta_norm(),
    adaptCount: microLoRA.adapt_count(),

View on GitHub (pinned to fa13ee4ad6)

Solutions

  1. Await initializeTraining() once at startup (it always succeeds — WASM falls back to JS) before any trainPattern/forward/adaptWithReward calls.
  2. If you called cleanup() (e.g. between test suites), call initializeTraining() again — the module is re-initializable.
  3. Guard call sites with an ensureInitialized() wrapper that inits once and caches the promise to prevent concurrent double-init.
  4. For worker processes/threads, run initializeTraining inside each worker; parent-process init does not propagate.

Example fix

// before
import { trainPattern } from './services/ruvector-training.js';
await trainPattern(embedding, gradient); // throws 'Training system not initialized'

// after
import { initializeTraining, trainPattern } from './services/ruvector-training.js';
let ready: Promise<unknown> | null = null;
const ensureTraining = () => (ready ??= initializeTraining());
await ensureTraining();
await trainPattern(embedding, gradient);
Defensive patterns

Strategy: validation

Validate before calling

import { initializeTraining, trainPattern, type TrainingConfig } from './services/ruvector-training.js';

let ready: Promise<ReturnType<typeof initializeTraining>> | null = null;
export function ensureTraining(cfg?: TrainingConfig) {
  return (ready ??= initializeTraining(cfg));
}

// before any trainPattern call:
const init = await ensureTraining();
if (!init.success) throw new Error(`Training init failed: ${init.error ?? 'unknown'}`);
await trainPattern(embedding, gradient, OperatorType.GENERAL);

Try / catch

try {
  await trainPattern(embedding, gradient);
} catch (e) {
  if (e instanceof Error && e.message === 'Training system not initialized') {
    await initializeTraining(); // idempotent bootstrap, then retry once
    return trainPattern(embedding, gradient);
  }
  throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of ruvnet/ruflo@fa13ee4ad6 (2026-08-18). Data as JSON: /api/errors/c88c4960ededf92e. Report an issue: GitHub.