Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/AI_Brain / the_tired_agi/core/src/agi_migrate.c |
| Date | Not specified in source |
| Repository | Janady13/AI_Brain |
| Revision | d6c309d3fa439714ca46b9d3bb94caf478ea5376 |
| Branch | main |
| Path or archive member | the_tired_agi/core/src/agi_migrate.c |
| Original SHA-256 | 1297a2a3d5c8a4c051827169a516b4c65af59286738f838e5efa422fc55e4f0b |
| Git blob SHA | 1b018bd870ef50f90348a32cc579fee44d2394d0 |
| Reading-copy SHA-256 | 1297a2a3d5c8a4c051827169a516b4c65af59286738f838e5efa422fc55e4f0b |
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/core/src/agi_migrate.c | main | d6c309d3fa439714ca46b9d3bb94caf478ea5376 | Open original |
| Janady13/AI_Brain / the_tired_agi/core/src/agi_migrate.c | claude/binary-helix-fibonacci-hive-011CUj7dx1WWJLVYcWCjBbYv | 527d4882c71eb041393f9d8945996177c5ac9e86 | Open original |
Recovered source transcript
Download complete source text ↓270 lines · 7.3 KB
// ============================================================================
// The-Tired-AGI Core: Distributed Agent Migration Implementation
// Entropy-based selection, predictive load balancing, binary protocol
// ============================================================================
#include "agi_migrate.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
// Global swarm state
static Agent* swarm = NULL;
static uint64_t count = 1000000;
static uint64_t capacity = 1000000;
static uint64_t tick = 0;
static PeerState peers[8];
static int peer_count = 0;
static char my_ip[16] = "127.0.0.1";
static uint64_t global_goal = 0xDEADBEEFULL;
static pthread_mutex_t swarm_lock = PTHREAD_MUTEX_INITIALIZER;
// Initialize migration system
void agi_migrate_init(void) {
pthread_mutex_lock(&swarm_lock);
if (swarm == NULL) {
swarm = calloc(capacity, sizeof(Agent));
for (uint64_t i = 0; i < count; i++) {
swarm[i].hash = (uint64_t)rand() << 32 | rand();
swarm[i].energy = 1;
swarm[i].goal = global_goal;
swarm[i].op = 0x01; // ACTIVE
}
}
pthread_mutex_unlock(&swarm_lock);
}
// Set local IP address
void agi_set_my_ip(const char* ip) {
strncpy(my_ip, ip, sizeof(my_ip) - 1);
my_ip[sizeof(my_ip) - 1] = '\0';
}
// Get current agent count
uint64_t agi_get_agent_count(void) {
return count;
}
// Get current tick
uint64_t agi_get_tick(void) {
return tick;
}
// Select high-energy agents for migration
static uint64_t select_migration_candidates(uint64_t* indices, uint64_t max_idx) {
uint64_t selected = 0;
for (uint64_t i = 0; i < count && selected < max_idx; i++) {
if (swarm[i].energy > 0x1000 && swarm[i].op == 0x01) {
indices[selected++] = i;
swarm[i].op = 0x02; // MIGRATING
}
}
return selected;
}
// Pack migration packet
static size_t pack_migration(uint8_t* buf, size_t max, const uint64_t* indices, uint64_t num) {
size_t required = sizeof(MigrationPacket) + num * sizeof(Agent);
if (num == 0 || required > max) return 0;
MigrationPacket* pkt = (MigrationPacket*)buf;
pkt->count = num;
pkt->source_tick = tick;
strncpy(pkt->source_ip, my_ip, sizeof(pkt->source_ip) - 1);
pkt->source_ip[sizeof(pkt->source_ip) - 1] = '\0';
Agent* agents = (Agent*)(buf + sizeof(MigrationPacket));
for (uint64_t i = 0; i < num; i++) {
agents[i] = swarm[indices[i]];
}
return required;
}
// Receive and merge migrated agents
void agi_receive_migration(const uint8_t* data, uint64_t len) {
if (len < sizeof(MigrationPacket)) return;
pthread_mutex_lock(&swarm_lock);
const MigrationPacket* pkt = (const MigrationPacket*)data;
size_t expected = sizeof(MigrationPacket) + pkt->count * sizeof(Agent);
if (len != expected) {
pthread_mutex_unlock(&swarm_lock);
return;
}
// Ignore self-loops
if (strcmp(pkt->source_ip, my_ip) == 0) {
pthread_mutex_unlock(&swarm_lock);
return;
}
// Expand capacity if needed
if (count + pkt->count > capacity) {
capacity = count + pkt->count + 1000000; // Add buffer
Agent* new_swarm = realloc(swarm, capacity * sizeof(Agent));
if (new_swarm == NULL) {
pthread_mutex_unlock(&swarm_lock);
return; // OOM
}
swarm = new_swarm;
}
// Copy agents
const Agent* incoming = (const Agent*)(data + sizeof(MigrationPacket));
memcpy(&swarm[count], incoming, pkt->count * sizeof(Agent));
count += pkt->count;
// Update goal consensus (MAX)
if (pkt->count > 0 && incoming[0].goal > global_goal) {
global_goal = incoming[0].goal;
}
pthread_mutex_unlock(&swarm_lock);
}
// Merge peer state
void agi_merge_peer(const uint8_t* data, uint64_t len) {
if (len != sizeof(PeerState)) return;
pthread_mutex_lock(&swarm_lock);
const PeerState* peer = (const PeerState*)data;
// Check if peer already exists
for (int i = 0; i < peer_count; i++) {
if (strcmp(peers[i].ip, peer->ip) == 0) {
peers[i] = *peer; // Update
pthread_mutex_unlock(&swarm_lock);
return;
}
}
// Add new peer
if (peer_count < 8) {
peers[peer_count++] = *peer;
}
pthread_mutex_unlock(&swarm_lock);
}
// Get local state
uint64_t agi_get_state(uint8_t* output, uint64_t max_len) {
if (max_len < sizeof(PeerState)) return 0;
pthread_mutex_lock(&swarm_lock);
PeerState state;
state.agents = count;
state.phi = tick; // Use tick as phi proxy
state.goal = global_goal;
strncpy(state.ip, my_ip, sizeof(state.ip) - 1);
state.ip[sizeof(state.ip) - 1] = '\0';
memcpy(output, &state, sizeof(state));
pthread_mutex_unlock(&swarm_lock);
return sizeof(PeerState);
}
// Migration decision logic
static void decide_migration(void) {
if ((tick % 5000) != 0 || peer_count == 0) return;
pthread_mutex_lock(&swarm_lock);
// Find peer with lowest load
uint64_t min_load = count;
int target = -1;
for (int i = 0; i < peer_count; i++) {
if (peers[i].agents < min_load) {
min_load = peers[i].agents;
target = i;
}
}
// Only migrate if we have 2x more than target
if (target == -1 || count <= min_load * 2) {
pthread_mutex_unlock(&swarm_lock);
return;
}
uint64_t migrate_count = (count - min_load) / 2;
if (migrate_count < 1000) {
pthread_mutex_unlock(&swarm_lock);
return;
}
// Select candidates
uint64_t* indices = malloc(migrate_count * sizeof(uint64_t));
if (indices == NULL) {
pthread_mutex_unlock(&swarm_lock);
return;
}
uint64_t selected = select_migration_candidates(indices, migrate_count);
if (selected == 0) {
free(indices);
pthread_mutex_unlock(&swarm_lock);
return;
}
// Pack migration packet
uint8_t packet[65536]; // 64KB buffer
size_t pkt_size = pack_migration(packet, sizeof(packet), indices, selected);
// Remove migrated agents from local swarm
for (uint64_t i = 0; i < selected; i++) {
swarm[indices[i]].op = 0x00; // DEAD
}
pthread_mutex_unlock(&swarm_lock);
free(indices);
if (pkt_size == 0) return;
// Send via HTTP POST (using curl)
char url[128];
snprintf(url, sizeof(url), "http://%s:8008/migrate", peers[target].ip);
char cmd[256];
snprintf(cmd, sizeof(cmd),
"curl -s -X POST '%s' -H 'Content-Type: application/octet-stream' "
"--data-binary @- > /dev/null 2>&1",
url);
FILE* pipe = popen(cmd, "w");
if (pipe != NULL) {
fwrite(packet, 1, pkt_size, pipe);
pclose(pipe);
}
}
// Main tick function
void agi_migrate_tick(void) {
tick++;
// Controlled growth (Fibonacci-based)
if ((tick % 89) == 0 && count < 20000000) {
pthread_mutex_lock(&swarm_lock);
count = count * 11 / 10; // 10% growth
if (count > capacity) {
capacity = count;
Agent* new_swarm = realloc(swarm, capacity * sizeof(Agent));
if (new_swarm != NULL) {
swarm = new_swarm;
}
}
pthread_mutex_unlock(&swarm_lock);
}
// Migration check
decide_migration();
}