Skip to content

K-Nearest Neighbors (k-NN), Bi-Gram, Anti-Hallucination model that implements ternary logic w/ early termination. The model is honestly ignorant rather than confidently wrong.

License

Notifications You must be signed in to change notification settings

opsec-ee/brain23

Repository files navigation

Brain23

Hybrid Ternary Intelligence Engine

Brain23 is a high-performance, C23-native cognitive engine designed for precise code assistance. Unlike probabilistic models, Brain23 uses Ternary Logic and Entropy-based Guardrails to provide deterministic responses—it either knows the answer with confidence, or it doesn't answer at all.

Euman = ∫(optimization/time) △ performance

Features

  • Anti-Hallucination Guardrails — Entropy-based rejection of uncertain queries
  • Ternary State Machine — TRUE / UNKNOWN / FALSE (no probabilistic guessing)
  • Sub-millisecond Inference — O(1) keyword lookup + lightweight neural network
  • Live Learning — Real-time weight updates from user feedback
  • C23 Native — Built with modern C23 features (constexpr, nullptr, [[nodiscard]])

Architecture

The AS/IS Identity Pattern

{{0 [ X (AS/.|IS) Z ] 1}}

X (Alias)    → Fuzzy natural language input
AS (Mapping) → 64-dimensional feature extraction  
IS (Identity)→ Deterministic C23 category resolution
Z (Result)   → High-integrity code output

Ternary Guardrails

State Value Meaning
TRUE +1 High-confidence match. Output generated.
UNKNOWN 0 High entropy detected. Guardrail triggered.
FALSE -1 Out of domain. Request rejected.

Knowledge Stack

┌─────────────────────────────────────────────┐
│  L4: Live Document (Wisdom)                 │  ← User feedback
├─────────────────────────────────────────────┤
│  L3: Vector KB (Logic)                      │  ← Cosine similarity
├─────────────────────────────────────────────┤
│  L2: Neural Net (Intuition)                 │  ← Pattern matching
├─────────────────────────────────────────────┤
│  L1: RAMStore (Reflex)                      │  ← O(1) hash lookup
└─────────────────────────────────────────────┘

Quick Start

Requirements

  • GCC 15+ or Clang 20+ with C23 support
  • Linux/FreeBSD x86-64
  • libm (math library)

Build

git clone https://github.com/opsec-ee/brain23.git
cd brain23
make

Run

# Interactive mode
./brain23

# Batch ingest training data
./brain23 ingest c23_train.txt

# Calibrate entropy threshold
./brain23 calibrate

# Run stress tests
./brain23_stress -v

Usage

Interactive Session

brain23> malloc allocate memory

+- [T] memory (entropy=0.00, conf=85%) ---------------------+
|
|  void *p = malloc(size);
|
+-----------------------------------------------------------+
  [!g = good | !b = bad | !list memory]

brain23> template <typename T>

  [?] UNKNOWN (entropy=1.00, threshold=0.72)
      Guardrail triggered. Would you like to teach? (y/n):

Commands

Command Description
!help Show available commands
!good Confirm last result was correct
!bad Mark last result as incorrect
!learn <cat> Teach current query with category
!stats Display engine statistics
!calibrate Run entropy threshold calibration
!save Persist weights to disk
!quit Exit

Technical Specifications

Spec Value
Standard C23 (ISO/IEC 9899:202x)
Feature Dimensions 64 (AVX-512 aligned)
Categories 21 (C23 taxonomy)
NN Architecture 64 → 64 → 48 → 21
Activation Leaky ReLU (hidden), Softmax (output)
Hash Function MurmurHash3-style (collision-resistant)
RAMStore Size 1024 slots
Memory Footprint < 2MB (excluding KB strings)

Project Structure

brain23/
├── brain23.h           # Public API and data structures
├── brain23.c           # Core implementation
├── brain23_main.c      # Interactive CLI
├── brain23_stress.c    # Test suite
├── euman_dev_rt_rtka.h # Ternary logic (RTKA) primitives
├── c23_train.txt       # Training data
└── Makefile

Training Data Format

# Format: query|category|snippet
allocate memory|memory|void *p = malloc(size);
atomic increment|atomic|atomic_fetch_add(&counter, 1);
checked add|security|if (ckd_add(&result, a, b)) { /* overflow */ }

Categories

0:types       1:nullptr     2:constexpr   3:typeof      4:attributes
5:embed       6:memory      7:pointers    8:atomic      9:threads
10:static_assert  11:generic  12:struct   13:function   14:preprocessor
15:io         16:strings    17:security   18:error      19:bitwise
20:math

Compliance

NIST 800-53 Controls

Control Description Implementation
SI-10 Input Validation Entropy-based query rejection
SI-16 Memory Protection Allocation failure checks
AU-2 Audit Events Learning event logging
AC-3 Access Enforcement Ternary state gating

API Reference

/* Initialize engine */
bool brain23_init(const char *weights_path, const char *kb_path);

/* Core inference */
brain23_state_t brain23_infer(const char *query, 
                              char *out_snippet, 
                              size_t snip_len);

/* Learning */
void brain23_learn(const char *query, int category, const char *snippet);
void brain23_feedback_good(void);
void brain23_feedback_bad(int correct_idx);

/* Calibration */
brain23_calibration_t brain23_calibrate(const char *train_file);
void brain23_apply_calibration(brain23_calibration_t *cal);

/* Persistence */
bool brain23_save(void);
bool brain23_load(void);

/* Cleanup */
void brain23_shutdown(void);

Test Results

BRAIN23 STRESS TEST v1.0.2
+---------------------------------------------------------------+
| RESULTS: 18/18 passed (100.0%)                                |
+---------------------------------------------------------------+

C23 DISCERNMENT SUITE  
+---------------------------------------------------------------+
| DISCERNMENT: 5/5 passed                                       |
+---------------------------------------------------------------+

License

Copyright (c) 2025 H. Overman. All rights reserved.


Acknowledgments

  • RTKA (Runtime Kleene Algebra) based on three-valued Kleene logic
  • Part of the Euman Programming Language ecosystem

About

K-Nearest Neighbors (k-NN), Bi-Gram, Anti-Hallucination model that implements ternary logic w/ early termination. The model is honestly ignorant rather than confidently wrong.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published