{"record":{"id":"d29d2c19632be33f","repo":"ruvnet/ruflo","slug":"consensus-engine-not-initialized","errorCode":null,"errorMessage":"Consensus engine not initialized","messagePattern":"Consensus engine not initialized","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"v3/@claude-flow/swarm/src/consensus/index.ts","lineNumber":146,"sourceCode":"      this.emit('leader.elected', data);\n    });\n\n    this.emit('initialized', {\n      nodeId: this.nodeId,\n      algorithm: this.config.algorithm\n    });\n  }\n\n  async shutdown(): Promise<void> {\n    if (this.implementation) {\n      await this.implementation.shutdown();\n    }\n    this.emit('shutdown');\n  }\n\n  addNode(nodeId: string, options?: { isPrimary?: boolean }): void {\n    if (!this.implementation) {\n      throw new Error('Consensus engine not initialized');\n    }\n\n    if (this.implementation instanceof RaftConsensus) {\n      this.implementation.addPeer(nodeId);\n    } else if (this.implementation instanceof ByzantineConsensus) {\n      this.implementation.addNode(nodeId, options?.isPrimary);\n    } else if (this.implementation instanceof GossipConsensus) {\n      this.implementation.addNode(nodeId);\n    }\n  }\n\n  removeNode(nodeId: string): void {\n    if (!this.implementation) {\n      return;\n    }\n\n    if (this.implementation instanceof RaftConsensus) {\n      this.implementation.removePeer(nodeId);","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/swarm/src/consensus/index.ts#L128-L164","documentation":"ConsensusEngine creates its implementation lazily inside initialize(); every other method assumes it exists. addNode() throws 'Consensus engine not initialized' when this.implementation is still undefined — i.e. when addNode runs before await initialize() completes, or after initialize() rejected (for example on an unknown algorithm) leaving the engine empty.","triggerScenarios":"Calling engine.addNode(...) immediately after the constructor without awaiting initialize(); initialize() threw (invalid algorithm) and the caller ignored the rejection; tests constructing the engine and registering nodes in beforeEach without init.","commonSituations":"Missing await on initialize() in async boot code; fire-and-forget initialization; error from initialize swallowed so later calls fail confusingly with 'not initialized' instead of the root cause.","solutions":["await engine.initialize(config) before any addNode/propose/vote call.","If initialize() rejected, read and fix the original error (e.g. unknown algorithm), then initialize again before using the engine.","Structure boot as: construct → await initialize → register nodes → operate.","Wrap the engine in a facade that queues node registrations until initialization resolves."],"exampleFix":"// before\nconst engine = new ConsensusEngine('n1', { algorithm: 'raft' });\nengine.addNode('n2'); // throws: not initialized\n\n// after\nconst engine = new ConsensusEngine('n1', { algorithm: 'raft' });\nawait engine.initialize();\nengine.addNode('n2');","handlingStrategy":"validation","validationCode":"// every API is safe only after initialize() resolves\nlet ready = false;\nengine.on('initialized', () => { ready = true; });\nawait engine.initialize(config); // throws here on bad config, not later\nif (!ready) throw new Error('consensus engine failed to initialize');\nengine.addNode('n2');","typeGuard":null,"tryCatchPattern":"try {\n  engine.addNode(nodeId);\n} catch (e) {\n  if (e instanceof Error && e.message === 'Consensus engine not initialized') {\n    await engine.initialize(config); // then retry addNode\n    engine.addNode(nodeId);\n  } else {\n    throw e;\n  }\n}","preventionTips":["Treat initialize() as part of construction: construct → await initialize → register nodes.","Propagate initialize() failures — a swallowed rejection turns every later call into a confusing 'not initialized' error.","Wrap the engine in a facade that queues node registrations until init resolves."],"tags":["consensus","lifecycle","initialization","factory"],"backgroundTag":"not-initialized","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"}