claude-flow
Version:
Ruflo - Enterprise AI agent orchestration for Claude Code. Deploy 60+ specialized agents in coordinated swarms with self-learning, fault-tolerant consensus, vector memory, and MCP integration
81 lines • 3.14 kB
TypeScript
/**
* V3 Raft Consensus Implementation
* Leader election and log replication for distributed coordination
*/
import { EventEmitter } from 'events';
import type { ConsensusTransport } from './transport.js';
import { ConsensusProposal, ConsensusVote, ConsensusResult, ConsensusConfig } from '../types.js';
export type RaftState = 'follower' | 'candidate' | 'leader';
export interface RaftNode {
id: string;
state: RaftState;
currentTerm: number;
votedFor?: string;
log: RaftLogEntry[];
commitIndex: number;
lastApplied: number;
}
export interface RaftLogEntry {
term: number;
index: number;
command: unknown;
timestamp: Date;
}
export interface RaftConfig extends Partial<ConsensusConfig> {
electionTimeoutMinMs?: number;
electionTimeoutMaxMs?: number;
heartbeatIntervalMs?: number;
/**
* ADR-095 G2 — optional pluggable transport. When set, RequestVote and
* AppendEntries RPCs go over it (request-response) and this node also
* answers inbound RequestVote / AppendEntries from peers using proper
* Raft receiver rules (term comparison, vote-once-per-term, log-up-to-date
* check). When unset, behavior is unchanged: the legacy in-process path
* mutates the local `peers` map directly (single-process).
*/
transport?: ConsensusTransport;
}
export declare class RaftConsensus extends EventEmitter {
private config;
private node;
private peers;
private proposals;
private electionTimeout?;
private heartbeatInterval?;
private proposalCounter;
private readonly transport?;
constructor(nodeId: string, config?: RaftConfig);
/** Index/term of the last entry in this node's log (Raft "up-to-date" comparison). */
private lastLogInfo;
/**
* ADR-095 G2 — Raft receiver. Answers inbound RequestVote / AppendEntries
* with proper Raft rules. Returns the RPC response the transport relays
* back to the caller (the transport's `send` resolves with it).
*/
private handleInboundRaftMessage;
initialize(): Promise<void>;
shutdown(): Promise<void>;
addPeer(peerId: string): void;
removePeer(peerId: string): void;
propose(value: unknown): Promise<ConsensusProposal>;
vote(proposalId: string, vote: ConsensusVote): Promise<void>;
awaitConsensus(proposalId: string): Promise<ConsensusResult>;
getState(): RaftState;
getTerm(): number;
isLeader(): boolean;
getLeaderId(): string | undefined;
private resetElectionTimeout;
private randomElectionTimeout;
private startElection;
private requestVote;
private becomeLeader;
private sendHeartbeats;
private appendEntries;
private replicateToFollowers;
private checkConsensus;
private createResult;
handleVoteRequest(candidateId: string, term: number, lastLogIndex: number, lastLogTerm: number): boolean;
handleAppendEntries(leaderId: string, term: number, entries: RaftLogEntry[], leaderCommit: number): boolean;
}
export declare function createRaftConsensus(nodeId: string, config?: RaftConfig): RaftConsensus;
//# sourceMappingURL=raft.d.ts.map