{"record":{"id":"cbc0b6a5abbbdf09","repo":"ruvnet/ruflo","slug":"selfconsistency-n-must-be-a-positive-integer-got","errorCode":null,"errorMessage":"selfConsistency: N must be a positive integer, got ${config.N}","messagePattern":"selfConsistency: N must be a positive integer, got (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/neural/src/utils/self-consistency.ts","lineNumber":69,"sourceCode":"   * For 'majority': fraction of samples matching finalAnswer (0..1).\n   * For 'mean':    1 - normalized stddev (rough confidence proxy).\n   * For 'first':   always 1.\n   */\n  agreement: number;\n  /** The config used for this run. */\n  config: SelfConsistencyConfig;\n}\n\n/**\n * Run an operation N times and aggregate. The operation is awaited\n * sequentially — if the caller wants concurrency they can wrap it themselves.\n */\nexport async function selfConsistency<T>(\n  operation: () => Promise<T> | T,\n  config: SelfConsistencyConfig,\n): Promise<SelfConsistencyResult<T>> {\n  if (!Number.isInteger(config.N) || config.N <= 0) {\n    throw new Error(`selfConsistency: N must be a positive integer, got ${config.N}`);\n  }\n\n  const samples: T[] = [];\n  for (let i = 0; i < config.N; i++) {\n    samples.push(await operation());\n  }\n\n  const aggregator: SelfConsistencyAggregator = config.aggregator ?? 'majority';\n\n  let finalAnswer: T;\n  let agreement: number;\n\n  if (aggregator === 'majority') {\n    // Group by JSON-stringified value (handles primitives, plain objects,\n    // arrays). Float32Array does NOT JSON-encode losslessly by default —\n    // callers wanting f32 majority should pre-convert via Array.from.\n    const counts = new Map<string, { value: T; count: number }>();\n    for (const s of samples) {","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/neural/src/utils/self-consistency.ts#L51-L87","documentation":"selfConsistency() validates its sample count up front: config.N must be a positive integer (Number.isInteger true and > 0). Zero, negatives, fractional values like 2.5, NaN, and Infinity all fail before the operation is run even once, so no samples are wasted.","triggerScenarios":"Passing N: 0 because a config default was never set; computing N from a ratio that yields a float (e.g. 3 * 0.5); NaN from Number.parseInt(undefined) on a missing CLI/env flag; a negative result from a subtraction like budget - used.","commonSituations":"CLI- or env-driven sampling counts where a missing flag coerces to 0 or NaN; dynamic N = Math.round(quality * k) with quality 0; configs copied between callers with different N semantics.","solutions":["Default N explicitly at the call site: config.N ?? 3","Coerce computed values with Math.max(1, Math.round(n)) before building the config","Validate any user-supplied N (CLI arg, env var, API field) before it reaches selfConsistency","Fail fast in your own config loader with a clear message instead of letting the library reject it later"],"exampleFix":"// before\nawait selfConsistency(op, { N: ratio * 10, aggregator: 'mean' });\n// after\nconst N = Math.max(1, Math.round(ratio * 10));\nawait selfConsistency(op, { N, aggregator: 'mean' });","handlingStrategy":"validation","validationCode":"const N = Number.isInteger(cfg.N) && (cfg.N as number) > 0 ? cfg.N : 3;\nawait selfConsistency(op, { ...cfg, N });","typeGuard":"function isPositiveInteger(n: unknown): n is number {\n  return typeof n === 'number' && Number.isInteger(n) && n > 0;\n}","tryCatchPattern":null,"preventionTips":["Always set an explicit N default at the call site","Round and clamp computed counts: Math.max(1, Math.round(n))","Validate CLI/env-sourced counts in your config loader before use","Unit-test the config builder with missing inputs"],"tags":["validation","arguments","sampling"],"backgroundTag":"invalid-argument","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","contentChangedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}