Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/AI_Brain / the_tired_agi/core/src/dna_api.cpp |
| Date | Not specified in source |
| Repository | Janady13/AI_Brain |
| Revision | d6c309d3fa439714ca46b9d3bb94caf478ea5376 |
| Branch | main |
| Path or archive member | the_tired_agi/core/src/dna_api.cpp |
| Original SHA-256 | 31ed2682b311124891889aedcd35e0b7037a80ae52386ff74a2b152803fdd14c |
| Git blob SHA | f662109c88a72d562d03ee7958353d6655c20afc |
| Reading-copy SHA-256 | 31ed2682b311124891889aedcd35e0b7037a80ae52386ff74a2b152803fdd14c |
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/dna_api.cpp | main | d6c309d3fa439714ca46b9d3bb94caf478ea5376 | Open original |
| Janady13/AI_Brain / the_tired_agi/core/src/dna_api.cpp | claude/binary-helix-fibonacci-hive-011CUj7dx1WWJLVYcWCjBbYv | 527d4882c71eb041393f9d8945996177c5ac9e86 | Open original |
Recovered source transcript
Download complete source text ↓438 lines · 12.9 KB
// ==============================================================================
// The-Tired-AGI Core: C API for CGo Bindings
// Exposes DNA Core functionality to Go services
// ==============================================================================
#include "helix.hpp"
#include "fgtp_engine.hpp"
#include "ingest_pipeline.hpp"
#include "agent_arrays.hpp"
#include <cstring>
#include <cstdlib>
#include <vector>
#include <mutex>
// ==============================================================================
// Global State (for CGo bindings)
// ==============================================================================
namespace {
tired_agi::HelixEngine* g_helix_engine = nullptr;
tired_agi::FGTPEngine* g_fgtp_engine = nullptr;
tired_agi::IngestPipeline* g_ingest_pipeline = nullptr;
tired_agi::AgentArrays* g_agent_arrays = nullptr;
std::mutex g_mutex;
}
// ==============================================================================
// C API Implementation
// ==============================================================================
extern "C" {
// ==============================================================================
// Initialization
// ==============================================================================
/// Initialize all DNA core engines
int dna_init() {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_helix_engine != nullptr) {
return 0; // Already initialized
}
g_helix_engine = new tired_agi::HelixEngine();
g_fgtp_engine = new tired_agi::FGTPEngine();
g_ingest_pipeline = new tired_agi::IngestPipeline();
g_agent_arrays = new tired_agi::AgentArrays();
return 1;
}
/// Shutdown and cleanup
void dna_shutdown() {
std::lock_guard<std::mutex> lock(g_mutex);
delete g_helix_engine;
delete g_fgtp_engine;
delete g_ingest_pipeline;
delete g_agent_arrays;
g_helix_engine = nullptr;
g_fgtp_engine = nullptr;
g_ingest_pipeline = nullptr;
g_agent_arrays = nullptr;
}
// ==============================================================================
// Agent Management
// ==============================================================================
/// Get agent count
unsigned long dna_get_agent_count() {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_agent_arrays == nullptr) {
return 0;
}
return g_agent_arrays->count_active();
}
/// Create agent from embedding
unsigned long dna_create_agent(const double* embedding, unsigned long dim, unsigned long tick) {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_agent_arrays == nullptr) {
return 0;
}
std::vector<double> emb(embedding, embedding + dim);
auto agent_id = g_agent_arrays->create_agent(emb, tick);
if (agent_id.has_value()) {
return agent_id.value();
}
return 0; // Failed
}
/// Get agent embedding
int dna_get_agent_embedding(unsigned long agent_id, double* out_embedding, unsigned long max_dim) {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_agent_arrays == nullptr) {
return 0;
}
const tired_agi::Agent* agent = g_agent_arrays->get_agent(agent_id);
if (agent == nullptr) {
return 0;
}
size_t copy_size = std::min(agent->embedding.size(), static_cast<size_t>(max_dim));
std::memcpy(out_embedding, agent->embedding.data(), copy_size * sizeof(double));
return static_cast<int>(copy_size);
}
/// Get agent phi value
double dna_get_agent_phi(unsigned long agent_id) {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_agent_arrays == nullptr) {
return 0.0;
}
const tired_agi::Agent* agent = g_agent_arrays->get_agent(agent_id);
if (agent == nullptr) {
return 0.0;
}
return agent->helix.phi;
}
/// Get agent energy
double dna_get_agent_energy(unsigned long agent_id) {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_agent_arrays == nullptr) {
return 0.0;
}
const tired_agi::Agent* agent = g_agent_arrays->get_agent(agent_id);
if (agent == nullptr) {
return 0.0;
}
return agent->energy.energy;
}
/// Get agent fitness
double dna_get_agent_fitness(unsigned long agent_id) {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_agent_arrays == nullptr) {
return 0.0;
}
const tired_agi::Agent* agent = g_agent_arrays->get_agent(agent_id);
if (agent == nullptr) {
return 0.0;
}
return agent->fitness;
}
// ==============================================================================
// Ingestion
// ==============================================================================
/// Process file and generate embeddings
unsigned long dna_ingest_file(const char* filepath) {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_ingest_pipeline == nullptr) {
return 0;
}
auto embeddings = g_ingest_pipeline->process_file(filepath);
// Push embeddings to ring buffer
if (g_agent_arrays != nullptr) {
for (const auto& emb : embeddings) {
g_agent_arrays->push_embedding(emb.vector);
}
}
return embeddings.size();
}
/// Process directory recursively
unsigned long dna_ingest_directory(const char* dirpath, int recursive) {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_ingest_pipeline == nullptr) {
return 0;
}
auto embeddings = g_ingest_pipeline->process_directory(dirpath, recursive != 0);
// Push embeddings to ring buffer
if (g_agent_arrays != nullptr) {
for (const auto& emb : embeddings) {
g_agent_arrays->push_embedding(emb.vector);
}
}
return embeddings.size();
}
/// Get ingestion statistics
void dna_get_ingest_stats(
unsigned long* files_processed,
unsigned long* chunks_generated,
unsigned long* embeddings_generated,
double* processing_rate_mbps
) {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_ingest_pipeline == nullptr) {
return;
}
auto stats = g_ingest_pipeline->get_statistics();
if (files_processed != nullptr) {
*files_processed = stats.total_files_processed;
}
if (chunks_generated != nullptr) {
*chunks_generated = stats.total_chunks_generated;
}
if (embeddings_generated != nullptr) {
*embeddings_generated = stats.total_embeddings_generated;
}
if (processing_rate_mbps != nullptr) {
*processing_rate_mbps = stats.processing_rate_mbps;
}
}
// ==============================================================================
// Helix Computations
// ==============================================================================
/// Compute phi from embedding
double dna_compute_phi(const double* embedding, unsigned long dim) {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_helix_engine == nullptr) {
return 0.0;
}
auto state = g_helix_engine->compute(embedding, dim, 0);
return state.phi;
}
/// Compute entropy from embedding
double dna_compute_entropy(const double* embedding, unsigned long dim) {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_helix_engine == nullptr) {
return 0.0;
}
auto state = g_helix_engine->compute(embedding, dim, 0);
return state.entropy;
}
/// Compute energy from embedding
double dna_compute_energy(const double* embedding, unsigned long dim) {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_helix_engine == nullptr || g_fgtp_engine == nullptr) {
return 0.0;
}
auto field = g_fgtp_engine->compute_energy_field(embedding, dim, 0);
return field.energy;
}
// ==============================================================================
// Statistics
// ==============================================================================
/// Get global statistics as JSON string (caller must free)
char* dna_get_statistics_json() {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_agent_arrays == nullptr) {
return nullptr;
}
auto agent_stats = g_agent_arrays->get_statistics();
auto fgtp_stats = g_fgtp_engine ? g_fgtp_engine->get_statistics() : tired_agi::FGTPEngine::Statistics{};
auto ingest_stats = g_ingest_pipeline ? g_ingest_pipeline->get_statistics() : tired_agi::IngestStatistics{};
// Build JSON string (simple formatting)
std::string json = "{\n";
json += " \"agents\": {\n";
json += " \"total\": " + std::to_string(agent_stats.total_agents) + ",\n";
json += " \"active\": " + std::to_string(agent_stats.active_agents) + ",\n";
json += " \"frozen\": " + std::to_string(agent_stats.frozen_agents) + ",\n";
json += " \"average_phi\": " + std::to_string(agent_stats.average_phi) + ",\n";
json += " \"average_energy\": " + std::to_string(agent_stats.average_energy) + ",\n";
json += " \"average_fitness\": " + std::to_string(agent_stats.average_fitness) + ",\n";
json += " \"fibonacci_index\": " + std::to_string(agent_stats.fibonacci_index) + "\n";
json += " },\n";
json += " \"fgtp\": {\n";
json += " \"total_computations\": " + std::to_string(fgtp_stats.total_computations) + ",\n";
json += " \"driver_applications\": " + std::to_string(fgtp_stats.total_driver_applications) + ",\n";
json += " \"violations\": " + std::to_string(fgtp_stats.total_violations) + "\n";
json += " },\n";
json += " \"ingest\": {\n";
json += " \"files_processed\": " + std::to_string(ingest_stats.total_files_processed) + ",\n";
json += " \"chunks_generated\": " + std::to_string(ingest_stats.total_chunks_generated) + ",\n";
json += " \"embeddings_generated\": " + std::to_string(ingest_stats.total_embeddings_generated) + ",\n";
json += " \"processing_rate_mbps\": " + std::to_string(ingest_stats.processing_rate_mbps) + "\n";
json += " }\n";
json += "}\n";
// Allocate C string
char* result = static_cast<char*>(malloc(json.size() + 1));
if (result != nullptr) {
std::memcpy(result, json.c_str(), json.size() + 1);
}
return result;
}
/// Free string allocated by dna_get_statistics_json
void dna_free_string(char* str) {
free(str);
}
// ==============================================================================
// Distributed Clustering Support (for gossip protocol)
// ==============================================================================
/// Get compression vector (binary state snapshot)
unsigned long dna_get_compression_vector(unsigned char* out_buffer, unsigned long max_size) {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_agent_arrays == nullptr) {
return 0;
}
// Get statistics and pack into binary format
auto stats = g_agent_arrays->get_statistics();
// Simple binary packing (can be enhanced)
size_t offset = 0;
// Pack agent count (8 bytes)
if (offset + 8 <= max_size) {
uint64_t count = stats.active_agents;
std::memcpy(out_buffer + offset, &count, 8);
offset += 8;
}
// Pack average phi (8 bytes)
if (offset + 8 <= max_size) {
std::memcpy(out_buffer + offset, &stats.average_phi, 8);
offset += 8;
}
// Pack average energy (8 bytes)
if (offset + 8 <= max_size) {
std::memcpy(out_buffer + offset, &stats.average_energy, 8);
offset += 8;
}
// Pack fibonacci index (4 bytes)
if (offset + 4 <= max_size) {
std::memcpy(out_buffer + offset, &stats.fibonacci_index, 4);
offset += 4;
}
return offset;
}
/// Merge peer binary state (MAX consensus)
void dna_merge_peer_binary(const unsigned char* peer_data, unsigned long size) {
std::lock_guard<std::mutex> lock(g_mutex);
if (g_agent_arrays == nullptr || size < 28) {
return;
}
// Unpack peer data
uint64_t peer_agent_count;
double peer_avg_phi;
double peer_avg_energy;
uint32_t peer_fib_index;
std::memcpy(&peer_agent_count, peer_data, 8);
std::memcpy(&peer_avg_phi, peer_data + 8, 8);
std::memcpy(&peer_avg_energy, peer_data + 16, 8);
std::memcpy(&peer_fib_index, peer_data + 24, 4);
// MAX consensus: take maximum values
// (In a real implementation, this would be more sophisticated)
// For now, just log the merge
// Real implementation would adjust local state based on peer state
}
/// Migrate agents to peer
void dna_migrate_agents_to(const unsigned char* peer_addr, unsigned long addr_len, unsigned long count) {
std::lock_guard<std::mutex> lock(g_mutex);
// Placeholder: In real implementation, this would:
// 1. Select agents to migrate (lowest fitness)
// 2. Serialize agent state
// 3. Send to peer via network
// 4. Deactivate migrated agents locally
// For now, just deactivate N agents
if (g_agent_arrays == nullptr) {
return;
}
auto agents = g_agent_arrays->get_active_agents();
size_t migrated = 0;
for (auto* agent : agents) {
if (migrated >= count) {
break;
}
// Deactivate agent (simulating migration)
g_agent_arrays->deactivate_agent(agent->agent_id);
migrated++;
}
}
} // extern "C"