An algorithm-first reasoning agent designed to explore self-optimization. Intelligence through heuristics, not size.
rHAI is an experimental AI agent built entirely from scratch in JavaScript. Its core purpose is to serve as a testbed for meta-learning and self-optimization concepts. Unlike projects that rely on external APIs, rHAI features its own custom neural network library (slmnet) and a high-level "Meta-Controller" that uses an AI agent to analyze and attempt to improve its own underlying code.
This project demonstrates a complete, closed-loop system for AI self-improvement:
slmnet: A minimalist deep learning library written from the ground up. It includes Tensors, automatic differentiation, neural network layers (Dense,Embedding,GRU), and anAdamoptimizer. It is the "genome" of the system.- HeuristicEngine: A custom Gated Recurrent Unit (GRU) based language model built with
slmnet. This model serves as the "brain" or the "Coder Agent". - Orchestrator: The agent's control loop, implementing a Reason-Act (ReAct) pattern. It allows the Coder Agent to use tools to interact with its environment.
- Meta-Controller (
meta_train.js): The high-level supervisor that orchestrates the self-optimization cycle:- Benchmark: Measures the performance (speed and accuracy) of the current
slmnetlibrary. - Analyze & Decide: Presents the Coder Agent with a potential code improvement ("mutation") and asks it to decide if the change is worth testing.
- Apply & Test: If the agent agrees, the Meta-Controller applies the code change and runs the benchmark again.
- Evolve or Revert: If the benchmark shows improvement, the change is kept. If not, it is reverted.
- Benchmark: Measures the performance (speed and accuracy) of the current
This project's main achievement is the successful implementation of this complete, self-referential optimization loop.
- Self-Optimization Cycle: A working implementation of a meta-learning loop where an AI agent attempts to improve its own core library.
- AI as an Editor: A pragmatic approach where the AI's task is simplified from "code generation" to "code selection", making it feasible for a small, custom-trained model.
- Custom Neural Network Framework: No external ML dependencies. Every component is implemented from scratch for full transparency.
- CPU Parallelism: Optimized for Node.js using
worker_threadsto accelerate heavy computations. - Complete Training & Benchmarking Pipeline: Includes scripts for focused dataset creation, agent training, and performance evaluation.
- Node.js (v18.x or later)
- npm
- Clone the repo:
git clone https://github.com/your-username/reasoningHeuristicAI.git - Navigate into the directory:
cd reasoningHeuristicAI - Install dependencies:
npm install
The primary workflow is to first train the "Coder Agent" on a specialized dataset, and then run the Meta-Controller.
The agent is trained on a small, focused dataset of code-analysis examples located in data/coder_finetune.jsonl. This teaches it the basic structure of how to reason about code and respond in JSON format.
npm run trainThis command will run very quickly. It will create the necessary model file at models/rHAI_v1_final.json.
Now that the Coder Agent is trained, you can start the self-optimization process. The Meta-Controller will take over and begin its cycle of analyzing, modifying, and testing the slmnet library.
npm run meta-trainYou will see the system perform the following steps:
- Run an initial benchmark to establish baseline performance.
- Load a "mutation" (a potential code improvement) from the
core/slmnet_mutations/directory. - Ask the Coder Agent to decide if the mutation should be tested.
- Apply the change (if approved by the agent) and re-run the benchmark.
- Compare results and decide whether to keep or revert the change.
- Repeat for all available mutations.
reasoningHeuristicAI/
├── core/
│ ├── slmnet/ # The "genome": the core NN library to be optimized.
│ ├── slmnet_mutations/ # A library of potential code improvements.
│ ├── tools/ # Tools for the agent (read/write/modify code).
│ ├── orchestrator.js # The agent's control loop.
│ └── heuristic-engine.js # The "brain" of the agent.
├── data/
│ └── coder_finetune.jsonl # Specialized training data for the agent.
├── models/ # Where the trained Coder Agent is saved.
├── train.js # Script to train the Coder Agent.
├── benchmark.js # Script to measure slmnet performance.
└── meta_train.js # The main Meta-Controller script.
This project successfully demonstrates a working, end-to-end prototype of an AI system capable of reasoning about and modifying its own code to improve performance. While the base model's capabilities are limited, the foundational architecture for meta-learning is robust and complete.