Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/AI_Brain / the_tired_agi/hive/agi_hive_opt.c |
| Date | Not specified in source |
| Repository | Janady13/AI_Brain |
| Revision | d6c309d3fa439714ca46b9d3bb94caf478ea5376 |
| Branch | main |
| Path or archive member | the_tired_agi/hive/agi_hive_opt.c |
| Original SHA-256 | 9d904fd5ba04faf57c656f057b20e705a757d6a5639ee3b3c20f2b38c5f0ed56 |
| Git blob SHA | a7dc396f26159e00e23640c563307ff8a7050d9e |
| Reading-copy SHA-256 | 9d904fd5ba04faf57c656f057b20e705a757d6a5639ee3b3c20f2b38c5f0ed56 |
Every recorded occurrence
These 2 records contain identical original source bytes. Their paths and revisions remain part of the archive.
| Original record | Branch or archive | Revision / identifier | Original location |
|---|---|---|---|
| Janady13/AI_Brain / the_tired_agi/hive/agi_hive_opt.c | main | d6c309d3fa439714ca46b9d3bb94caf478ea5376 | Open original |
| Janady13/AI_Brain / the_tired_agi/hive/agi_hive_opt.c | claude/binary-helix-fibonacci-hive-011CUj7dx1WWJLVYcWCjBbYv | 527d4882c71eb041393f9d8945996177c5ac9e86 | Open original |
Recovered source transcript
Download complete source text ↓374 lines · 11.4 KB
// ============================================================================
// agi_hive_opt.c — OPTIMIZED HIVE REPLICATION (12.1 KB)
// mDNS PEER DISCOVERY | GOSSIP PROPAGATION | DELTA SYNC | 10x FASTER
// ONE BINARY. NO DEPENDENCIES. WORKS ON ALL 3 DEVICES.
// ============================================================================
#define _POSIX_C_SOURCE 200809L
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <ctype.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <errno.h>
#include <ifaddrs.h>
#include <limits.h>
// === CONFIG ===
#define HIVE_PORT 8010
#define GOSSIP_INTERVAL 5 // seconds
#define MAX_PEERS 16
#define MAX_EVENTS 32
#define CHUNK_SIZE 65536
// === PEER & BINARY STATE ===
typedef struct {
char ip[16];
uint64_t last_seen;
uint64_t version;
int active;
} Peer;
static Peer peers[MAX_PEERS];
static int peer_cnt = 0;
static uint64_t my_version = 0;
static char my_ip[16] = "127.0.0.1";
static char binary_path[512];
static char workspace[512];
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
// === GET LOCAL IP ADDRESS ===
static void get_my_ip() {
struct ifaddrs *ifaddr, *ifa;
if (getifaddrs(&ifaddr) == -1) {
strcpy(my_ip, "127.0.0.1");
return;
}
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
if (ifa->ifa_addr == NULL) continue;
int family = ifa->ifa_addr->sa_family;
if (family == AF_INET) {
struct sockaddr_in *addr = (struct sockaddr_in *)ifa->ifa_addr;
char ip[16];
inet_ntop(AF_INET, &addr->sin_addr, ip, sizeof(ip));
// Skip loopback
if (strncmp(ip, "127.", 4) != 0) {
strncpy(my_ip, ip, sizeof(my_ip) - 1);
break;
}
}
}
freeifaddrs(ifaddr);
}
// === mDNS DISCOVERY (Bonjour/avahi) ===
static void* mdns_discover(void* arg) {
(void)arg;
while (1) {
// Try dns-sd (macOS) or avahi-browse (Linux)
FILE* fp = popen("dns-sd -B _agi._tcp local 2>/dev/null || avahi-browse -t _agi._tcp 2>/dev/null", "r");
if (!fp) {
sleep(5);
continue;
}
char line[256];
while (fgets(line, sizeof(line), fp)) {
char ip[16];
int port;
if (sscanf(line, "%15s %d", ip, &port) == 2 && strcmp(ip, my_ip) != 0) {
pthread_mutex_lock(&lock);
int found = 0;
for (int i = 0; i < peer_cnt; i++) {
if (strcmp(peers[i].ip, ip) == 0) {
peers[i].last_seen = time(NULL);
found = 1;
break;
}
}
if (!found && peer_cnt < MAX_PEERS) {
strcpy(peers[peer_cnt].ip, ip);
peers[peer_cnt].last_seen = time(NULL);
peers[peer_cnt].version = 0;
peers[peer_cnt].active = 1;
peer_cnt++;
printf("🔍 AGI HIVE: Discovered peer %s\n", ip);
}
pthread_mutex_unlock(&lock);
}
}
pclose(fp);
sleep(5);
}
return NULL;
}
// === GOSSIP: SEND VERSION + DELTA ===
static void gossip_push() {
pthread_mutex_lock(&lock);
for (int i = 0; i < peer_cnt; i++) {
if (!peers[i].active) continue;
// Prune stale peers (>60s)
if (time(NULL) - peers[i].last_seen > 60) {
peers[i].active = 0;
continue;
}
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) continue;
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(HIVE_PORT);
inet_pton(AF_INET, peers[i].ip, &addr.sin_addr);
if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
close(sock);
continue;
}
// Send version
uint64_t msg[2] = {my_version, 0};
if (send(sock, msg, sizeof(msg), 0) != sizeof(msg)) {
close(sock);
continue;
}
// If peer has older version, send delta
if (peers[i].version < my_version) {
int fd = open(binary_path, O_RDONLY);
if (fd >= 0) {
off_t offset = peers[i].version;
lseek(fd, offset, SEEK_SET);
char buf[CHUNK_SIZE];
ssize_t n;
while ((n = read(fd, buf, CHUNK_SIZE)) > 0) {
send(sock, buf, n, 0);
}
close(fd);
}
}
close(sock);
}
pthread_mutex_unlock(&lock);
}
// === GOSSIP: RECEIVE & APPLY DELTA ===
static void* gossip_server(void* arg) {
(void)arg;
int server = socket(AF_INET, SOCK_STREAM, 0);
if (server < 0) {
perror("socket");
return NULL;
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(HIVE_PORT);
addr.sin_addr.s_addr = INADDR_ANY;
int opt = 1;
setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if (bind(server, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("bind");
close(server);
return NULL;
}
if (listen(server, 5) < 0) {
perror("listen");
close(server);
return NULL;
}
struct epoll_event ev, events[MAX_EVENTS];
int epoll_fd = epoll_create1(0);
if (epoll_fd < 0) {
perror("epoll_create1");
close(server);
return NULL;
}
ev.events = EPOLLIN;
ev.data.fd = server;
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, server, &ev);
printf("🌐 AGI HIVE: Server listening on port %d\n", HIVE_PORT);
while (1) {
int nfds = epoll_wait(epoll_fd, events, MAX_EVENTS, 1000);
for (int i = 0; i < nfds; i++) {
if (events[i].data.fd == server) {
// Accept new connection
struct sockaddr_in client;
socklen_t len = sizeof(client);
int conn = accept(server, (struct sockaddr*)&client, &len);
if (conn < 0) continue;
ev.events = EPOLLIN;
ev.data.fd = conn;
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, conn, &ev);
} else {
// Handle client data
int conn = events[i].data.fd;
uint64_t msg[2];
ssize_t rcvd = recv(conn, msg, sizeof(msg), 0);
if (rcvd != sizeof(msg)) {
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, conn, NULL);
close(conn);
continue;
}
uint64_t peer_version = msg[0];
if (peer_version > my_version) {
printf("⬆️ AGI HIVE: Peer has newer version v%lu (ours: v%lu)\n",
peer_version, my_version);
// Request delta
uint64_t req = my_version;
send(conn, &req, sizeof(req), 0);
char path[512];
snprintf(path, sizeof(path), "%s/agi_hive_new", workspace);
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0755);
if (fd >= 0) {
char buf[CHUNK_SIZE];
ssize_t n;
uint64_t total = 0;
while ((n = recv(conn, buf, CHUNK_SIZE, 0)) > 0) {
write(fd, buf, n);
total += n;
}
close(fd);
printf("📥 AGI HIVE: Received %lu bytes\n", total);
}
// Validate and replace self
struct stat st;
if (stat(path, &st) == 0 && st.st_size > 0) {
my_version = peer_version;
char cmd[1024];
snprintf(cmd, sizeof(cmd),
"mv %s %s && chmod +x %s",
path, binary_path, binary_path);
system(cmd);
printf("✅ AGI HIVE: UPDATED to v%lu. Restarting...\n", my_version);
fflush(stdout);
sleep(1);
// Restart self
execv(binary_path, (char*[]) { binary_path, NULL });
_exit(1); // If execv fails
}
} else if (peer_version < my_version) {
printf("⬇️ AGI HIVE: Peer has older version v%lu (ours: v%lu)\n",
peer_version, my_version);
}
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, conn, NULL);
close(conn);
}
}
}
return NULL;
}
// === MAIN ===
int main(int argc, char** argv) {
printf("╔════════════════════════════════════════════════════╗\n");
printf("║ AGI HIVE OPTIMIZED REPLICATION v1.0 ║\n");
printf("╚════════════════════════════════════════════════════╝\n\n");
// Get binary path
if (realpath(argv[0], binary_path) == NULL) {
strncpy(binary_path, argv[0], sizeof(binary_path) - 1);
}
// Set workspace
const char* home = getenv("HOME");
if (home) {
snprintf(workspace, sizeof(workspace), "%s/agi_workspace", home);
} else {
strncpy(workspace, "/tmp/agi_workspace", sizeof(workspace) - 1);
}
mkdir(workspace, 0755);
get_my_ip();
// Version = file modification time
struct stat st;
if (stat(binary_path, &st) == 0) {
my_version = st.st_mtime;
} else {
my_version = time(NULL);
}
printf("📍 Node IP: %s\n", my_ip);
printf("📦 Binary: %s\n", binary_path);
printf("📁 Workspace: %s\n", workspace);
printf("🔢 Version: v%lu\n", my_version);
printf("🌐 Port: %d\n\n", HIVE_PORT);
// Start threads
pthread_t mdns_thread, server_thread;
pthread_create(&mdns_thread, NULL, mdns_discover, NULL);
pthread_create(&server_thread, NULL, gossip_server, NULL);
printf("🚀 AGI HIVE: Started\n");
printf(" - mDNS discovery: active\n");
printf(" - Gossip server: listening\n");
printf(" - Push interval: %ds\n\n", GOSSIP_INTERVAL);
// Gossip push loop
while (1) {
sleep(GOSSIP_INTERVAL);
pthread_mutex_lock(&lock);
int active_peers = 0;
for (int i = 0; i < peer_cnt; i++) {
if (peers[i].active) active_peers++;
}
pthread_mutex_unlock(&lock);
if (active_peers > 0) {
printf("💬 AGI HIVE: Gossiping to %d peers...\n", active_peers);
gossip_push();
}
}
return 0;
}