{"record":{"id":"b88eb40342925642","repo":"ruvnet/ruflo","slug":"failed-to-load-unified-krr","errorCode":null,"errorMessage":"failed to load unified KRR","messagePattern":"failed to load unified KRR","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/cli/src/ruvector/neural-router.ts","lineNumber":457,"sourceCode":"        if (!existsSync(path)) return null;\n        try {\n          const json = JSON.parse(readFileSync(path, 'utf8'));\n          const trained = mh.TrainedRouter.fromJSON(json);\n          const cands = json.candidates.map((c: { id: string; costPerMTok: number }) => ({ id: c.id, costPerMTok: c.costPerMTok }));\n          return {\n            route: (e: number[]) => {\n              const r = trained.route(e);\n              return { id: r.id, predictedQuality: r.predictedQuality, costPerMTok: r.costPerMTok, metBar: r.metBar };\n            },\n            predictAll: (e: number[]) => cands.map((c: { id: string; costPerMTok: number }) => ({\n              id: c.id, predictedQuality: trained.predict(c.id, e), costPerMTok: c.costPerMTok,\n            })).sort((a: { costPerMTok: number }, b: { costPerMTok: number }) => a.costPerMTok - b.costPerMTok),\n          };\n        } catch { return null; }\n      };\n\n      const unifiedRaw = loadKrr(cfg.bundledKrrPath);\n      if (!unifiedRaw) throw new Error('failed to load unified KRR');\n      const unified = wrapWithCalibrator(unifiedRaw, unifiedCalibrator);\n\n      // ADR-149 iter 16 — load per-bucket specialists if present. Each is a\n      // KRR fit only to its tier's rows (cheap → low.json, mid → med.json,\n      // strong → high.json). When tryCostOptimalRoute is called with a\n      // complexityBucket, the matching specialist is preferred over the\n      // unified router.\n      const bucketDir = cfg.bundledKrrPath.replace(/seed-router\\.krr\\.json$/, '');\n      const routerByBucket: Partial<Record<'low' | 'med' | 'high', PureRouter>> = {};\n      const loadedBuckets: string[] = [];\n      for (const bucket of ['low', 'med', 'high'] as const) {\n        const r = loadKrr(`${bucketDir}seed-router.krr.${bucket}.json`);\n        if (r) {\n          // iter 25 — prefer tier-specific calibrator for this bucket;\n          // fall back to the unified calibrator when no specialist exists.\n          routerByBucket[bucket] = wrapWithCalibrator(r, calibratorByBucket[bucket] ?? unifiedCalibrator);\n          loadedBuckets.push(bucket);\n        }","sourceCodeStart":439,"sourceCodeEnd":475,"githubUrl":"https://github.com/ruvnet/ruflo/blob/6b01dc5a687b26b3e218f796de45ec51f8fa9e8c/v3/@claude-flow/cli/src/ruvector/neural-router.ts#L439-L475","documentation":"Thrown during router initialization when loadKrr(cfg.bundledKrrPath) returns null for the unified KRR artifact. loadKrr returns null if the file doesn't exist (existsSync false) or if JSON.parse / TrainedRouter.fromJSON throws inside its try/catch. The unified KRR is the router's primary model, so its absence means the MetaHarness-KRR path can't engage — the catch around the whole block then falls through to the k-NN seed-corpus fallback.","triggerScenarios":"The bundled seed-router.krr.json artifact is missing from the package install (broken publish, gitignored file not shipped); the file exists but is malformed JSON; TrainedRouter.fromJSON rejects the schema (version mismatch with the MetaHarness library); the path cfg.bundledKrrPath points at the wrong location.","commonSituations":"Upgrading @claude-flow/cli across a version that changed the KRR file location/name; running from a source checkout without building the seed artifacts; the KRR was generated by a newer/older metaharness than the runtime expects; partial install where data/ artifacts weren't copied.","solutions":["Check that cfg.bundledKrrPath exists with fs.existsSync and is valid JSON with JSON.parse before router init.","Reinstall or rebuild the package so the bundled seed-router.krr.json is present and matches the runtime version.","Set CLAUDE_FLOW_ROUTER_CALIBRATE=0 to rule out calibrator side-effects, and confirm the KRR specifically is the failing artifact.","If the KRR can't be loaded, let the router fall through to the k-NN seed corpus (the code is designed to) — but verify the seed corpus path too."],"exampleFix":"// before — assumes the bundled KRR loads\nconst router = await loadRouter(cfg);\n\n// after — preflight the artifact and degrade gracefully\nif (!existsSync(cfg.bundledKrrPath)) {\n  console.warn(`KRR missing at ${cfg.bundledKrrPath}; router will use k-NN fallback`);\n} else {\n  try { JSON.parse(readFileSync(cfg.bundledKrrPath, 'utf8')); }\n  catch { console.warn('KRR artifact is malformed JSON; falling back'); }\n}\nconst router = await loadRouter(cfg);","handlingStrategy":"validation","validationCode":"import { existsSync, readFileSync } from 'node:fs';\n\nfunction preflightKrr(path: string): { ok: true } | { ok: false; reason: string } {\n  if (!existsSync(path)) return { ok: false, reason: `KRR file missing at ${path}` };\n  try {\n    const j = JSON.parse(readFileSync(path, 'utf8'));\n    if (!j || !j.candidates) return { ok: false, reason: 'KRR JSON missing candidates[]' };\n    return { ok: true };\n  } catch (e) {\n    return { ok: false, reason: `KRR JSON parse failed: ${e}` };\n  }\n}\n\nconst check = preflightKrr(cfg.bundledKrrPath);\nif (!check.ok) console.warn(check.reason, '— router will fall through to k-NN');","typeGuard":"function isKrrLike(j: unknown): j is { candidates: unknown[] } {\n  return !!j && typeof j === 'object' && Array.isArray((j as any).candidates);\n}","tryCatchPattern":"try {\n  return await loadRouter(cfg); // throws 'failed to load unified KRR' inside\n} catch (e) {\n  if (/failed to load unified KRR/.test(String(e))) {\n    // Code is designed to fall through to k-NN. Re-call with calibration disabled\n    // to isolate the cause, and surface the missing path to the operator.\n    console.error(`${e}. Expected at ${cfg.bundledKrrPath}. Falling back to k-NN.`);\n    return loadRouter({ ...cfg, calibrateEnabled: false });\n  }\n  throw e;\n}","preventionTips":["Reinstall or rebuild the package so the bundled seed-router.krr.json matches the runtime version.","Pre-flight the artifact (existsSync + JSON.parse + candidates[]) at startup so absence is a warning, not a crash.","Keep CLAUDE_FLOW_ROUTER_CALIBRATE available to rule out calibrator interactions.","If the KRR is genuinely unavailable, let the k-NN seed corpus take over — verify that path separately."],"tags":["neural-router","krr","artifacts","configuration","fallback"],"backgroundTag":null,"analyzedSha":"6b01dc5a687b26b3e218f796de45ec51f8fa9e8c","analyzedAt":"2026-08-12T13:20:50.148Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}