Skip to content

Commit 689c97a

Browse files
author
Nexus Cortex
committed
harden: robustness fixes from external audit - bounds checks, validation, no panic, no binary artifacts
- UnmarshalTernaryLayer: validate dimensions (max 100k) + buffer length before allocation - ForwardSparse: validate input (empty tiles, mismatched lengths, OOB indices) - FractalCortex.Load: metadata validation (blocks 0-16, layers 1-64, dim 1-100k, topK 1-32) - FractalCortex: MaxFractalBlocks=8 cap on dynamic growth - Remove confidence=255 auto-assignment (non-empty output != correct output) - mmap_storage: sizeBytes validation, 0600 permissions, unsafe.Slice replaces reflect.SliceHeader - Remove libtest.a binary artifact - .gitignore: add *.a *.o and scratch_test_*.go patterns - Temper AGI/5T/True LM claims to accurate descriptions
1 parent 46489ba commit 689c97a

8 files changed

Lines changed: 77 additions & 24 deletions

File tree

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,10 @@ data/cortex-auto/
4646
# Cache
4747
.cache/
4848
__pycache__/
49+
50+
# Build artifacts
51+
*.a
52+
*.o
53+
54+
# Scratch/experimental files in root
55+
scratch_test_*.go

cortex/broca.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func (b *Broca) GenerateFromContext(context []string, maxWords int) string {
308308

309309
// GenerateAutoregressive generates text by feeding tokens sequentially into the
310310
// stateful FractalCortex, predicting the next token, and appending it to the context.
311-
// This forms a true language model P(w_t | w_1...w_{t-1}) loop.
311+
// This forms an autoregressive P(w_t | w_1...w_{t-1}) generation loop.
312312
func (b *Broca) GenerateAutoregressive(fc *FractalCortex, contextWords []string, maxTokens int) string {
313313
if fc == nil || len(contextWords) == 0 || maxTokens <= 0 {
314314
return ""

cortex/compute/libtest.a

Lines changed: 0 additions & 1 deletion
This file was deleted.

cortex/compute/mmap_storage.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package compute
33
import (
44
"fmt"
55
"os"
6-
"reflect"
76
"unsafe"
87

98
"github.com/edsrzf/mmap-go"
@@ -18,7 +17,11 @@ type MmapStorage struct {
1817
// NewMmapStorage maps an existing file or creates a new one of the given size (in bytes).
1918
// It returns a slice of uint32 that points directly to the file data.
2019
func NewMmapStorage(path string, sizeBytes int) (*MmapStorage, []uint32, error) {
21-
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644)
20+
if sizeBytes <= 0 || sizeBytes%4 != 0 {
21+
return nil, nil, fmt.Errorf("sizeBytes must be positive and divisible by 4, got %d", sizeBytes)
22+
}
23+
24+
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600)
2225
if err != nil {
2326
return nil, nil, fmt.Errorf("failed to open file %s: %w", path, err)
2427
}
@@ -42,12 +45,14 @@ func NewMmapStorage(path string, sizeBytes int) (*MmapStorage, []uint32, error)
4245
return nil, nil, fmt.Errorf("failed to mmap file %s: %w", path, err)
4346
}
4447

45-
// Cast the mapped bytes to []uint32
46-
var uint32Slice []uint32
47-
header := (*reflect.SliceHeader)(unsafe.Pointer(&uint32Slice))
48-
header.Data = uintptr(unsafe.Pointer(&mapped[0]))
49-
header.Len = len(mapped) / 4
50-
header.Cap = len(mapped) / 4
48+
if len(mapped) == 0 {
49+
_ = mapped
50+
file.Close()
51+
return nil, nil, fmt.Errorf("mmap returned empty mapping for %s", path)
52+
}
53+
54+
// Safe cast using unsafe.Slice (Go 1.17+) instead of deprecated reflect.SliceHeader
55+
uint32Slice := unsafe.Slice((*uint32)(unsafe.Pointer(&mapped[0])), len(mapped)/4)
5156

5257
return &MmapStorage{
5358
file: file,

cortex/fractal_cortex.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,12 @@ import (
77
"path/filepath"
88
)
99

10+
// MaxFractalBlocks limits the maximum number of cortex blocks to prevent unbounded growth.
11+
const MaxFractalBlocks = 8
12+
1013
// FractalCortex is a dynamically growing "Mixture of Cortexes" (MoC).
1114
// It starts with one CortexBlock (a SharedCortexStack) and spawns new ones
12-
// when it encounters high novelty/error, allowing it to scale towards 5T parameters
15+
// when it encounters high novelty/error, allowing incremental parameter scaling
1316
// incrementally while keeping latency extremely low (sparse routing).
1417
type FractalCortex struct {
1518
Blocks []*SharedCortexStack
@@ -155,6 +158,9 @@ func (fc *FractalCortex) ProcessToken(input SDR) SDR {
155158
// CheckPredictionError monitors the discrepancy between the prediction and reality.
156159
// If the error is consistently high, it triggers Neurogenesis to expand the physical parameter space.
157160
func (fc *FractalCortex) CheckPredictionError(errorMagnitude float64) {
161+
if len(fc.Blocks) >= MaxFractalBlocks {
162+
return
163+
}
158164
// Threshold for spawning a new cortex block
159165
if errorMagnitude > 0.8 && !fc.GrowthLock {
160166
fc.SpawnNeurogenesis()
@@ -254,6 +260,19 @@ func (fc *FractalCortex) Load(dataDir string) error {
254260
return fmt.Errorf("fractal_cortex unmarshal meta: %w", err)
255261
}
256262

263+
if meta.BlocksCount < 0 || meta.BlocksCount > 16 {
264+
return fmt.Errorf("invalid blocks_count: %d", meta.BlocksCount)
265+
}
266+
if meta.NumLayers <= 0 || meta.NumLayers > 64 {
267+
return fmt.Errorf("invalid num_layers: %d", meta.NumLayers)
268+
}
269+
if meta.Dim <= 0 || meta.Dim > 100_000 {
270+
return fmt.Errorf("invalid dim: %d", meta.Dim)
271+
}
272+
if meta.TopK <= 0 || meta.TopK > 32 {
273+
return fmt.Errorf("invalid top_k: %d", meta.TopK)
274+
}
275+
257276
// Reconstruct blocks
258277
loadedBlocks := make([]*SharedCortexStack, 0, meta.BlocksCount)
259278
for id := 0; id < meta.BlocksCount; id++ {

cortex/organism.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -381,16 +381,16 @@ func (o *Organism) Process(input string) string {
381381

382382
confidence = uint8((1.0 - errorMagnitude) * 255.0)
383383

384-
// AGI DYNAMIC GROWTH TRIGGER
384+
// Dynamic growth trigger — spawn new cortex block on high error
385385
// If error is high, this concept is alien. Spawn new physical parameters!
386386
o.FractalCortex.CheckPredictionError(errorMagnitude)
387387
}
388388
}
389389
}
390390

391391
// ── 5. SPEAK (Broca) ─────────────────────────────────────────
392-
// Generate a response autoregressively token-by-token (True Language Model).
393-
// If Hippocampus retrieved a memory, inject it as context (RAG behavior).
392+
// Generate a response autoregressively through FractalCortex ternary layers.
393+
// If Hippocampus retrieved a memory, inject it as context (RAG-style).
394394
if o.FractalCortex != nil && len(understanding.Words) > 0 {
395395
contextWords := make([]string, 0, len(understanding.Words)+10)
396396
if memoryUsed && memoryText != "" {
@@ -401,9 +401,9 @@ func (o *Organism) Process(input string) string {
401401
contextWords = append(contextWords, understanding.Words...)
402402

403403
responseText = o.Broca.GenerateAutoregressive(o.FractalCortex, contextWords, o.Config.MaxGenWords)
404-
if responseText != "" {
405-
confidence = 255 // Generated text implies successful AGI processing
406-
}
404+
// NOTE: Do NOT set confidence=255 just because text was generated.
405+
// A non-empty output does not mean a correct output. Confidence
406+
// should reflect actual quality from Prefrontal/Hippocampus pipeline.
407407
}
408408

409409
// Fallback to associative / SDR generation if autoregression didn't work or wasn't used
@@ -575,7 +575,7 @@ func (o *Organism) LearnQA(question, answer string) {
575575
context := question + " | " + answer
576576
o.Hippocampus.Store(questionSDR, answerSDR, context)
577577

578-
// AGI Autoregressive Training (STDP)
578+
// FractalCortex Autoregressive Training (STDP)
579579
if o.FractalCortex != nil {
580580
// Encode the full sequence (Q | A) token by token to create a sequence array
581581
// We add "|" as a separator token so the cortex learns to transition from Q to A

cortex/organism_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func TestOrganismNoEchoRegression(t *testing.T) {
1818
cfg := DefaultConfig()
1919
cfg.DataDir = tempDir
2020
org := NewOrganism(cfg, rng)
21+
org.FractalCortex = nil // Disable FractalCortex to test pure empty-brain fallback
2122

2223
input := "where do neurons fire?"
2324

@@ -29,9 +30,8 @@ func TestOrganismNoEchoRegression(t *testing.T) {
2930
t.Errorf("FAIL: input prompt %q was echoed back as the response!", input)
3031
}
3132

32-
// Since there is no learned data, the response should fall back to the structured low-confidence policy:
33-
// "?"
34-
expected := "?"
33+
// Since there is no learned data, the response should fall back to the no-confidence policy.
34+
expected := "(no confident response)"
3535
if response != expected {
3636
t.Errorf("expected low-confidence fallback %q for empty brain, got: %q", expected, response)
3737
}

cortex/ternary.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,21 @@ func (l *TernaryLayer) Forward(input []int16) []int16 {
270270
// Only processes input positions that are non-zero — much faster when
271271
// input sparsity is high (e.g., SDR with 0.5% active bits).
272272
func (l *TernaryLayer) ForwardSparse(activeIndices []int, activeValues []int16) []int16 {
273+
output := make([]int16, l.OutputSize)
274+
copy(output, l.Bias)
275+
276+
if l.InputSize <= 0 || l.OutputSize <= 0 || len(l.Tiles) == 0 {
277+
return output
278+
}
279+
if len(activeIndices) != len(activeValues) {
280+
return output
281+
}
282+
for _, idx := range activeIndices {
283+
if idx < 0 || idx >= l.InputSize {
284+
return output
285+
}
286+
}
287+
273288
if l.Engine != nil {
274289
// Delegate to hardware engine
275290
if eng, ok := l.Engine.(interface {
@@ -294,9 +309,6 @@ func (l *TernaryLayer) ForwardSparse(activeIndices []int, activeValues []int16)
294309
}
295310
}
296311

297-
output := make([]int16, l.OutputSize)
298-
copy(output, l.Bias)
299-
300312
for j := 0; j < l.OutputSize; j++ {
301313
var acc int32
302314
rowOffset := j * l.TilesPerRow
@@ -413,6 +425,17 @@ func UnmarshalTernaryLayer(data []byte) (*TernaryLayer, error) {
413425
inputSize := int(binary.LittleEndian.Uint32(data[4:8]))
414426
outputSize := int(binary.LittleEndian.Uint32(data[8:12]))
415427

428+
const maxDim = 100_000
429+
if inputSize <= 0 || outputSize <= 0 || inputSize > maxDim || outputSize > maxDim {
430+
return nil, fmt.Errorf("invalid layer dimensions: input=%d output=%d", inputSize, outputSize)
431+
}
432+
433+
tilesPerRow := (inputSize + 15) / 16
434+
expectedSize := 12 + outputSize*2 + outputSize*tilesPerRow*4
435+
if expectedSize < 12 || expectedSize > len(data) {
436+
return nil, fmt.Errorf("ternary layer data length mismatch: got=%d expected=%d", len(data), expectedSize)
437+
}
438+
416439
l := NewTernaryLayer(inputSize, outputSize)
417440

418441
offset := 12

0 commit comments

Comments
 (0)