/** * CRDT-Based Agent State Sync * Conflict-Free Replicated Data Types for Agent Swarm Coordination * * Binlerce agent'ın tek bir mega-agent gibi çalışması: * Arbitrage, liquidity, risk ve mempool snapshot paylaşımı çakışmasız. * * Pipeline: * [Agent] → Local State * → CRDT Merge Engine * → QUIC Gossip Layer * → Peer Agents * * CRDT Türleri: * - G-Counter: latency counters (only increment) * - PN-Counter: bidirectional price movements * - LWW-Register: market state (last write wins) * - OR-Set: open positions (observed-remove set) */ import { EventEmitter } from 'events'; export type NodeId = string; export interface VectorClock { [nodeId: string]: number; } export interface CRDT { value(): T; merge(other: CRDT): CRDT; toJSON(): object; fromJSON(json: object): CRDT; } /** * G-Counter (Grow-only Counter) * Used for: latency counters, packet counts, event tracking */ export declare class GCounter implements CRDT { private nodeId; private counts; constructor(nodeId: NodeId); /** * Increment counter for this node */ increment(amount?: number): void; /** * Get total value across all nodes */ value(): number; /** * Get value for specific node */ valueFor(nodeId: NodeId): number; /** * Merge with another G-Counter (take max for each node) */ merge(other: GCounter): GCounter; toJSON(): object; fromJSON(json: any): GCounter; } /** * PN-Counter (Positive-Negative Counter) * Used for: bidirectional price movements, balance tracking */ export declare class PNCounter implements CRDT { private nodeId; private positive; private negative; constructor(nodeId: NodeId); /** * Increment counter */ increment(amount?: number): void; /** * Decrement counter */ decrement(amount?: number): void; /** * Get net value */ value(): number; /** * Merge with another PN-Counter */ merge(other: PNCounter): PNCounter; toJSON(): object; fromJSON(json: any): PNCounter; } /** * LWW-Register (Last-Writer-Wins Register) * Used for: market state, prices, configuration */ export declare class LWWRegister implements CRDT { private nodeId; private data; private timestamp; constructor(nodeId: NodeId, initialValue?: T); /** * Set value with current timestamp */ set(value: T): void; /** * Get current value */ value(): T | undefined; /** * Get timestamp */ getTimestamp(): number; /** * Merge with another LWW-Register (take value with higher timestamp) */ merge(other: LWWRegister): LWWRegister; toJSON(): object; fromJSON(json: any): LWWRegister; } /** * OR-Set (Observed-Remove Set) * Used for: open positions, active orders, peer lists */ export declare class ORSet implements CRDT> { private nodeId; private elements; private tombstones; constructor(nodeId: NodeId); /** * Add element to set */ add(element: T): void; /** * Remove element from set */ remove(element: T): void; /** * Check if element is in set */ has(element: T): boolean; /** * Get all elements in set */ value(): Set; /** * Get set size */ size(): number; /** * Merge with another OR-Set */ merge(other: ORSet): ORSet; private serialize; toJSON(): object; fromJSON(json: any): ORSet; } /** * Vector Clock for causal ordering */ export declare class CausalClock { private nodeId; private clock; constructor(nodeId: NodeId); /** * Increment local clock */ tick(): VectorClock; /** * Merge with another clock */ merge(other: VectorClock): void; /** * Check if this clock happened before other */ happensBefore(other: VectorClock): boolean; /** * Check if concurrent with other */ isConcurrent(other: VectorClock): boolean; private happensAfter; value(): VectorClock; } /** * CRDT State Manager * Manages all CRDTs for an agent */ export declare class CRDTStateManager extends EventEmitter { private nodeId; private gCounters; private pnCounters; private lwwRegisters; private orSets; private clock; constructor(nodeId?: NodeId); /** * Get or create a G-Counter */ gCounter(key: string): GCounter; /** * Get or create a PN-Counter */ pnCounter(key: string): PNCounter; /** * Get or create an LWW-Register */ register(key: string, initialValue?: T): LWWRegister; /** * Get or create an OR-Set */ set(key: string): ORSet; /** * Merge state from another node */ mergeFrom(state: CRDTStateSnapshot): void; /** * Export state snapshot for gossip */ snapshot(): CRDTStateSnapshot; /** * Get node ID */ getNodeId(): NodeId; /** * Tick the causal clock */ tick(): VectorClock; } export interface CRDTStateSnapshot { nodeId: NodeId; clock: VectorClock; gCounters: Record; pnCounters: Record; lwwRegisters: Record; orSets: Record; } /** * Gossip Protocol for CRDT State Sync */ export declare class CRDTGossipLayer extends EventEmitter { private stateManager; private transport; private config; private peers; private gossipInterval; constructor(stateManager: CRDTStateManager, transport: { broadcast: (msg: any) => Promise; onMessage: (handler: (msg: any, from: NodeId) => void) => void; }, config?: { gossipIntervalMs?: number; fanout?: number; maxAge?: number; }); /** * Start gossip protocol */ start(): void; /** * Stop gossip protocol */ stop(): void; /** * Perform one gossip round */ private gossipRound; /** * Handle incoming gossip message */ private handleMessage; /** * Get connected peers */ getPeers(): NodeId[]; /** * Get peer count */ getPeerCount(): number; } /** * High-level SDK API for CRDT State */ export declare class CRDTStateSDK { private stateManager; private gossipLayer; constructor(nodeId?: NodeId); /** * Register a new LWW state */ register(options: { key: string; type: 'lww'; initialValue?: T; }): LWWRegister; /** * Register a G-Counter */ counter(key: string): GCounter; /** * Register a PN-Counter */ bidirectionalCounter(key: string): PNCounter; /** * Register an OR-Set */ set(key: string): ORSet; /** * Enable gossip-based sync */ enableGossip(transport: any, config?: any): CRDTGossipLayer; /** * Get state manager */ getStateManager(): CRDTStateManager; /** * Get current state snapshot */ snapshot(): CRDTStateSnapshot; /** * Merge state from peer */ merge(state: CRDTStateSnapshot): void; } export declare const crdtState: CRDTStateSDK; //# sourceMappingURL=crdt.d.ts.map