FerrellSyntheticIntelligence commited on
Commit ·
8663f54
1
Parent(s): 41b685b
[VITALIS] Full FSI system — Tier 1-4 complete
Browse files- failure_report.json +1 -1
- memory_slot.json +1 -0
- project_ledger.json +1 -1
- src/brain/__pycache__/inference.cpython-311.pyc +0 -0
- src/brain/inference.py +22 -2
- src/hippocampus.py +14 -19
- src/ide_kernel/__pycache__/gateway.cpython-311.pyc +0 -0
- src/ide_kernel/__pycache__/kernel.cpython-311.pyc +0 -0
- src/ide_kernel/__pycache__/ledger.cpython-311.pyc +0 -0
- src/ide_kernel/gateway.py +3 -3
- src/ide_kernel/kernel.py +13 -17
- src/ide_kernel/ledger.py +29 -1
- src/router.py +5 -19
- test_module/__init__.py +1 -0
- test_module2/__init__.py +1 -0
- vitalis_ide/brain/__pycache__/bridge.cpython-311.pyc +0 -0
- vitalis_ide/brain/bridge.py +6 -16
- vitalis_ide/cli/__pycache__/main.cpython-311.pyc +0 -0
- vitalis_ide/math_core/__pycache__/kernel.cpython-311.pyc +0 -0
- vitalis_ide/math_core/kernel.py +110 -1
failure_report.json
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
{"intent": "auto_debug", "original_task": {"intent": "scaffold", "module_name": "
|
|
|
|
| 1 |
+
{"intent": "auto_debug", "original_task": {"intent": "scaffold", "module_name": "test_module", "file": null, "code": null}, "error_log": "/usr/bin/python3: No module named pytest\n"}
|
memory_slot.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"slot": 2}
|
project_ledger.json
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
{"scaffold": "
|
|
|
|
| 1 |
+
{"scaffold": "Completed", "test_action": "Completed"}
|
src/brain/__pycache__/inference.cpython-311.pyc
CHANGED
|
Binary files a/src/brain/__pycache__/inference.cpython-311.pyc and b/src/brain/__pycache__/inference.cpython-311.pyc differ
|
|
|
src/brain/inference.py
CHANGED
|
@@ -1,3 +1,23 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
sys.path.insert(0, os.path.expanduser("~/vitalis_devcore"))
|
| 5 |
+
from vitalis_ide.math_core.kernel import VitalisKernel
|
| 6 |
|
| 7 |
+
class InferenceEngine:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.kernel = VitalisKernel()
|
| 10 |
+
|
| 11 |
+
def reason(self, prompt: str) -> str:
|
| 12 |
+
tokens = prompt.strip().split()
|
| 13 |
+
vec = self.kernel.vectorize_tokens(tokens)
|
| 14 |
+
confidence = float(np.mean(np.abs(vec)))
|
| 15 |
+
if "scaffold" in prompt.lower():
|
| 16 |
+
return "scaffold"
|
| 17 |
+
elif "write" in prompt.lower() or "fix" in prompt.lower():
|
| 18 |
+
return "write"
|
| 19 |
+
else:
|
| 20 |
+
return f"[INFER] Confidence={confidence:.3f} | Input={prompt[:80]}"
|
| 21 |
+
|
| 22 |
+
def embed(self, text: str) -> np.ndarray:
|
| 23 |
+
return self.kernel.vectorize_tokens(text.strip().split())
|
src/hippocampus.py
CHANGED
|
@@ -2,25 +2,20 @@ import numpy as np
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
class Hippocampus:
|
| 5 |
-
def __init__(self,
|
| 6 |
-
self.
|
| 7 |
-
self.
|
| 8 |
-
self.
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
os.ftruncate(self.fd, self.size)
|
| 13 |
-
|
| 14 |
-
self.mem = np.memmap(self.filename, dtype=np.int8, mode="r+", shape=(capacity, dim))
|
| 15 |
|
| 16 |
-
def store(self,
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
self.mem[index] = vector
|
| 20 |
-
self.mem.flush()
|
| 21 |
|
| 22 |
-
def recall(self,
|
| 23 |
-
return self.
|
| 24 |
|
| 25 |
-
def
|
| 26 |
-
self.
|
|
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
class Hippocampus:
|
| 5 |
+
def __init__(self, path=None):
|
| 6 |
+
self.path = path or os.path.expanduser("~/.vitalis_workspace/hippocampus.npy")
|
| 7 |
+
os.makedirs(os.path.dirname(self.path), exist_ok=True)
|
| 8 |
+
if os.path.exists(self.path):
|
| 9 |
+
self.memory = np.load(self.path, allow_pickle=True).item()
|
| 10 |
+
else:
|
| 11 |
+
self.memory = {}
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
def store(self, slot, vector):
|
| 14 |
+
self.memory[slot] = vector
|
| 15 |
+
np.save(self.path, self.memory)
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
def recall(self, slot):
|
| 18 |
+
return self.memory.get(slot, None)
|
| 19 |
|
| 20 |
+
def all_slots(self):
|
| 21 |
+
return list(self.memory.keys())
|
src/ide_kernel/__pycache__/gateway.cpython-311.pyc
CHANGED
|
Binary files a/src/ide_kernel/__pycache__/gateway.cpython-311.pyc and b/src/ide_kernel/__pycache__/gateway.cpython-311.pyc differ
|
|
|
src/ide_kernel/__pycache__/kernel.cpython-311.pyc
CHANGED
|
Binary files a/src/ide_kernel/__pycache__/kernel.cpython-311.pyc and b/src/ide_kernel/__pycache__/kernel.cpython-311.pyc differ
|
|
|
src/ide_kernel/__pycache__/ledger.cpython-311.pyc
CHANGED
|
Binary files a/src/ide_kernel/__pycache__/ledger.cpython-311.pyc and b/src/ide_kernel/__pycache__/ledger.cpython-311.pyc differ
|
|
|
src/ide_kernel/gateway.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
-
from pydantic import BaseModel, ValidationError
|
| 5 |
from typing import Literal, Optional
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
|
@@ -17,9 +17,9 @@ class TaskPayload(BaseModel):
|
|
| 17 |
@app.route('/execute', methods=['POST'])
|
| 18 |
def execute_task():
|
| 19 |
try:
|
| 20 |
-
data = TaskPayload.
|
| 21 |
except ValidationError as exc:
|
| 22 |
return jsonify({"error": exc.errors()}), 400
|
| 23 |
with open(TASK_FILE, "w") as f:
|
| 24 |
-
json.dump(data.
|
| 25 |
return jsonify({"status": "Task Queued", "intent": data.intent}), 202
|
|
|
|
| 1 |
from flask import Flask, request, jsonify
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
+
from pydantic import BaseModel, ValidationError
|
| 5 |
from typing import Literal, Optional
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
|
|
|
| 17 |
@app.route('/execute', methods=['POST'])
|
| 18 |
def execute_task():
|
| 19 |
try:
|
| 20 |
+
data = TaskPayload.model_validate(request.json or {})
|
| 21 |
except ValidationError as exc:
|
| 22 |
return jsonify({"error": exc.errors()}), 400
|
| 23 |
with open(TASK_FILE, "w") as f:
|
| 24 |
+
json.dump(data.model_dump(), f)
|
| 25 |
return jsonify({"status": "Task Queued", "intent": data.intent}), 202
|
src/ide_kernel/kernel.py
CHANGED
|
@@ -1,26 +1,22 @@
|
|
| 1 |
import os
|
| 2 |
-
import ast
|
| 3 |
|
| 4 |
class SovereignKernel:
|
| 5 |
-
def __init__(self,
|
| 6 |
-
self.root = os.path.abspath(
|
| 7 |
|
| 8 |
-
def write_code(self, file_path,
|
| 9 |
full_path = os.path.join(self.root, file_path)
|
| 10 |
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
| 11 |
with open(full_path, 'w') as f:
|
| 12 |
-
f.write(
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
def scaffold_module(self, module_name):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
f
|
| 21 |
-
}
|
| 22 |
-
|
| 23 |
-
results = []
|
| 24 |
-
for path, content in files.items():
|
| 25 |
-
results.append(self.write_code(path, content))
|
| 26 |
-
return results
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
|
| 3 |
class SovereignKernel:
|
| 4 |
+
def __init__(self, workspace_path):
|
| 5 |
+
self.root = os.path.abspath(workspace_path)
|
| 6 |
|
| 7 |
+
def write_code(self, file_path, code):
|
| 8 |
full_path = os.path.join(self.root, file_path)
|
| 9 |
os.makedirs(os.path.dirname(full_path), exist_ok=True)
|
| 10 |
with open(full_path, 'w') as f:
|
| 11 |
+
f.write(code)
|
| 12 |
+
print(f"[KERNEL] Written: {full_path}")
|
| 13 |
+
return full_path
|
| 14 |
|
| 15 |
def scaffold_module(self, module_name):
|
| 16 |
+
module_dir = os.path.join(self.root, module_name)
|
| 17 |
+
os.makedirs(module_dir, exist_ok=True)
|
| 18 |
+
init_path = os.path.join(module_dir, "__init__.py")
|
| 19 |
+
with open(init_path, 'w') as f:
|
| 20 |
+
f.write(f"# {module_name} module\n")
|
| 21 |
+
print(f"[KERNEL] Scaffolded: {module_dir}")
|
| 22 |
+
return module_dir
|
|
|
|
|
|
|
|
|
|
|
|
src/ide_kernel/ledger.py
CHANGED
|
@@ -1,16 +1,44 @@
|
|
| 1 |
import json
|
| 2 |
import os
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class ProjectLedger:
|
| 5 |
def __init__(self, workspace_path):
|
| 6 |
self.ledger_path = os.path.join(workspace_path, "project_ledger.json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def update_state(self, action, status):
|
|
|
|
| 9 |
data = {}
|
| 10 |
if os.path.exists(self.ledger_path):
|
| 11 |
with open(self.ledger_path, 'r') as f:
|
| 12 |
data = json.load(f)
|
| 13 |
-
|
| 14 |
data[action] = status
|
| 15 |
with open(self.ledger_path, 'w') as f:
|
| 16 |
json.dump(data, f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
import os
|
| 3 |
+
import numpy as np
|
| 4 |
+
from src.hippocampus import Hippocampus
|
| 5 |
|
| 6 |
class ProjectLedger:
|
| 7 |
def __init__(self, workspace_path):
|
| 8 |
self.ledger_path = os.path.join(workspace_path, "project_ledger.json")
|
| 9 |
+
self.hippocampus = Hippocampus()
|
| 10 |
+
self.slot_path = os.path.join(workspace_path, "memory_slot.json")
|
| 11 |
+
|
| 12 |
+
def _next_slot(self):
|
| 13 |
+
if os.path.exists(self.slot_path):
|
| 14 |
+
with open(self.slot_path, 'r') as f:
|
| 15 |
+
data = json.load(f)
|
| 16 |
+
else:
|
| 17 |
+
data = {"slot": 0}
|
| 18 |
+
slot = data["slot"]
|
| 19 |
+
data["slot"] = slot + 1
|
| 20 |
+
with open(self.slot_path, 'w') as f:
|
| 21 |
+
json.dump(data, f)
|
| 22 |
+
return slot
|
| 23 |
|
| 24 |
def update_state(self, action, status):
|
| 25 |
+
# 1. Write to JSON ledger as before
|
| 26 |
data = {}
|
| 27 |
if os.path.exists(self.ledger_path):
|
| 28 |
with open(self.ledger_path, 'r') as f:
|
| 29 |
data = json.load(f)
|
|
|
|
| 30 |
data[action] = status
|
| 31 |
with open(self.ledger_path, 'w') as f:
|
| 32 |
json.dump(data, f)
|
| 33 |
+
|
| 34 |
+
# 2. If successful, imprint into Hippocampus
|
| 35 |
+
if "Completed" in status or "Recovered" in status:
|
| 36 |
+
try:
|
| 37 |
+
seed = sum(ord(c) for c in action)
|
| 38 |
+
np.random.seed(seed)
|
| 39 |
+
vector = np.random.choice([-1, 1], size=10000).astype(np.int8)
|
| 40 |
+
slot = self._next_slot()
|
| 41 |
+
self.hippocampus.store(slot, vector)
|
| 42 |
+
print(f"[LEDGER] Action '{action}' imprinted to memory slot {slot}.")
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"[LEDGER] Memory imprint failed: {e}")
|
src/router.py
CHANGED
|
@@ -1,22 +1,8 @@
|
|
| 1 |
-
|
| 2 |
-
from src.hippocampus import Hippocampus
|
| 3 |
-
import hdc_engine
|
| 4 |
|
| 5 |
class Router:
|
| 6 |
def __init__(self):
|
| 7 |
-
self.
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
# 1. Map input to a vector
|
| 12 |
-
# (Simplified encoding: using the sum of character codes as a seed)
|
| 13 |
-
seed = sum(ord(c) for c in raw_input)
|
| 14 |
-
np.random.seed(seed)
|
| 15 |
-
vector = np.random.choice([-1, 1], size=10000).astype(np.int8)
|
| 16 |
-
|
| 17 |
-
# 2. Routing logic
|
| 18 |
-
# For this prototype, we store everything as 'new learning'
|
| 19 |
-
self.brain.store(self.next_slot, vector)
|
| 20 |
-
print(f"Router: Imprinted '{raw_input}' into slot {self.next_slot}")
|
| 21 |
-
self.next_slot += 1
|
| 22 |
-
return vector
|
|
|
|
| 1 |
+
from src.brain.inference import InferenceEngine
|
|
|
|
|
|
|
| 2 |
|
| 3 |
class Router:
|
| 4 |
def __init__(self):
|
| 5 |
+
self.engine = InferenceEngine()
|
| 6 |
+
|
| 7 |
+
def route(self, prompt: str) -> str:
|
| 8 |
+
return self.engine.reason(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
test_module/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# test_module module
|
test_module2/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
# test_module2 module
|
vitalis_ide/brain/__pycache__/bridge.cpython-311.pyc
CHANGED
|
Binary files a/vitalis_ide/brain/__pycache__/bridge.cpython-311.pyc and b/vitalis_ide/brain/__pycache__/bridge.cpython-311.pyc differ
|
|
|
vitalis_ide/brain/bridge.py
CHANGED
|
@@ -1,19 +1,9 @@
|
|
| 1 |
-
|
| 2 |
-
Bridge Module — Autonomous confidence recovery.
|
| 3 |
-
If confidence is in the hypothesis zone, this module forces a RAG retrieval
|
| 4 |
-
to boost context and re-evaluate.
|
| 5 |
-
"""
|
| 6 |
-
|
| 7 |
-
from vitalis_ide.brain.rag import RAGEngine
|
| 8 |
|
| 9 |
class ConfidenceBridge:
|
| 10 |
-
def __init__(self
|
| 11 |
-
self.
|
| 12 |
-
|
| 13 |
-
def needs_augmentation(self, confidence: float) -> bool:
|
| 14 |
-
# Range 0.45 to 0.65 triggers the Bridge
|
| 15 |
-
return 0.45 <= confidence < 0.65
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
return
|
|
|
|
| 1 |
+
from src.brain.inference import InferenceEngine
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
class ConfidenceBridge:
|
| 4 |
+
def __init__(self):
|
| 5 |
+
self.engine = InferenceEngine()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
def evaluate(self, prompt: str) -> dict:
|
| 8 |
+
result = self.engine.reason(prompt)
|
| 9 |
+
return {"result": result, "status": "ok"}
|
vitalis_ide/cli/__pycache__/main.cpython-311.pyc
CHANGED
|
Binary files a/vitalis_ide/cli/__pycache__/main.cpython-311.pyc and b/vitalis_ide/cli/__pycache__/main.cpython-311.pyc differ
|
|
|
vitalis_ide/math_core/__pycache__/kernel.cpython-311.pyc
CHANGED
|
Binary files a/vitalis_ide/math_core/__pycache__/kernel.cpython-311.pyc and b/vitalis_ide/math_core/__pycache__/kernel.cpython-311.pyc differ
|
|
|
vitalis_ide/math_core/kernel.py
CHANGED
|
@@ -1,11 +1,120 @@
|
|
| 1 |
import numpy as np
|
| 2 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class VitalisKernel:
|
| 5 |
def __init__(self):
|
|
|
|
| 6 |
self.weights_path = Path.home() / ".vitalis_workspace" / "kernel.weights.npy"
|
|
|
|
|
|
|
| 7 |
self.bias = np.load(self.weights_path) if self.weights_path.exists() else np.array([0.0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def matmul(self, a, b):
|
| 10 |
-
|
| 11 |
return np.dot(a, b) + self.bias
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
from pathlib import Path
|
| 3 |
+
import ast
|
| 4 |
+
import hdc_engine
|
| 5 |
+
|
| 6 |
+
DIM = 10000
|
| 7 |
|
| 8 |
class VitalisKernel:
|
| 9 |
def __init__(self):
|
| 10 |
+
self.dim = DIM
|
| 11 |
self.weights_path = Path.home() / ".vitalis_workspace" / "kernel.weights.npy"
|
| 12 |
+
self.codebook_path = Path.home() / ".vitalis_workspace" / "codebook.npy"
|
| 13 |
+
self.codebook_index_path = Path.home() / ".vitalis_workspace" / "codebook_index.npy"
|
| 14 |
self.bias = np.load(self.weights_path) if self.weights_path.exists() else np.array([0.0])
|
| 15 |
+
self._load_codebook()
|
| 16 |
+
|
| 17 |
+
def _load_codebook(self):
|
| 18 |
+
"""Load or initialize the token codebook."""
|
| 19 |
+
if self.codebook_path.exists():
|
| 20 |
+
self.codebook = np.load(self.codebook_path, allow_pickle=True).item()
|
| 21 |
+
else:
|
| 22 |
+
self.codebook = {}
|
| 23 |
+
|
| 24 |
+
def _save_codebook(self):
|
| 25 |
+
self.codebook_path.parent.mkdir(parents=True, exist_ok=True)
|
| 26 |
+
np.save(self.codebook_path, self.codebook)
|
| 27 |
+
|
| 28 |
+
def _get_token_vector(self, token: str) -> np.ndarray:
|
| 29 |
+
"""Get or create a stable hypervector for a token."""
|
| 30 |
+
if token not in self.codebook:
|
| 31 |
+
self.codebook[token] = np.random.choice(
|
| 32 |
+
[-1, 1], size=self.dim
|
| 33 |
+
).astype(np.int8)
|
| 34 |
+
self._save_codebook()
|
| 35 |
+
return self.codebook[token]
|
| 36 |
+
|
| 37 |
+
def _get_position_vector(self, position: int) -> np.ndarray:
|
| 38 |
+
"""Generate a stable position vector by seeded random."""
|
| 39 |
+
rng = np.random.default_rng(seed=position)
|
| 40 |
+
return rng.choice([-1, 1], size=self.dim).astype(np.int8)
|
| 41 |
+
|
| 42 |
+
def vectorize_tokens(self, tokens: list) -> np.ndarray:
|
| 43 |
+
"""
|
| 44 |
+
Encode a list of tokens into a single hypervector.
|
| 45 |
+
Each token is bound with its position, then all are bundled.
|
| 46 |
+
"""
|
| 47 |
+
bundle = np.zeros(self.dim, dtype=np.int32)
|
| 48 |
+
for i, token in enumerate(tokens):
|
| 49 |
+
token_vec = self._get_token_vector(token)
|
| 50 |
+
pos_vec = self._get_position_vector(i)
|
| 51 |
+
bound = hdc_engine.bind(token_vec, pos_vec)
|
| 52 |
+
bundle += bound
|
| 53 |
+
# Binarize the bundle
|
| 54 |
+
result = np.sign(bundle).astype(np.int8)
|
| 55 |
+
result[result == 0] = 1
|
| 56 |
+
return result
|
| 57 |
+
|
| 58 |
+
def vectorize_source(self, source_code: str) -> np.ndarray:
|
| 59 |
+
"""
|
| 60 |
+
Map a source file string into a single hypervector.
|
| 61 |
+
Extracts AST-level tokens for semantic richness.
|
| 62 |
+
"""
|
| 63 |
+
tokens = self._extract_tokens(source_code)
|
| 64 |
+
return self.vectorize_tokens(tokens)
|
| 65 |
+
|
| 66 |
+
def vectorize_file(self, file_path: str) -> np.ndarray:
|
| 67 |
+
"""
|
| 68 |
+
Map a source file on disk into a hypervector.
|
| 69 |
+
"""
|
| 70 |
+
path = Path(file_path)
|
| 71 |
+
if not path.exists():
|
| 72 |
+
raise FileNotFoundError(f"Source file not found: {file_path}")
|
| 73 |
+
source = path.read_text(encoding="utf-8")
|
| 74 |
+
return self.vectorize_source(source)
|
| 75 |
+
|
| 76 |
+
def _extract_tokens(self, source_code: str) -> list:
|
| 77 |
+
"""
|
| 78 |
+
Extract meaningful tokens from source code via AST.
|
| 79 |
+
Falls back to whitespace splitting if parsing fails.
|
| 80 |
+
"""
|
| 81 |
+
tokens = []
|
| 82 |
+
try:
|
| 83 |
+
tree = ast.parse(source_code)
|
| 84 |
+
for node in ast.walk(tree):
|
| 85 |
+
# Function and class names
|
| 86 |
+
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef, ast.ClassDef)):
|
| 87 |
+
tokens.append(f"DEF:{node.name}")
|
| 88 |
+
# Variable names
|
| 89 |
+
elif isinstance(node, ast.Name):
|
| 90 |
+
tokens.append(f"NAME:{node.id}")
|
| 91 |
+
# String constants
|
| 92 |
+
elif isinstance(node, ast.Constant) and isinstance(node.value, str):
|
| 93 |
+
tokens.append(f"STR:{node.value[:32]}")
|
| 94 |
+
# Imports
|
| 95 |
+
elif isinstance(node, ast.Import):
|
| 96 |
+
for alias in node.names:
|
| 97 |
+
tokens.append(f"IMPORT:{alias.name}")
|
| 98 |
+
elif isinstance(node, ast.ImportFrom):
|
| 99 |
+
tokens.append(f"FROM:{node.module}")
|
| 100 |
+
except SyntaxError:
|
| 101 |
+
# Fallback for non-Python or malformed files
|
| 102 |
+
tokens = source_code.split()
|
| 103 |
+
return tokens if tokens else ["EMPTY"]
|
| 104 |
+
|
| 105 |
+
def similarity(self, vec_a: np.ndarray, vec_b: np.ndarray) -> float:
|
| 106 |
+
"""Cosine similarity between two hypervectors."""
|
| 107 |
+
a = vec_a.astype(np.float32)
|
| 108 |
+
b = vec_b.astype(np.float32)
|
| 109 |
+
denom = np.linalg.norm(a) * np.linalg.norm(b)
|
| 110 |
+
if denom == 0:
|
| 111 |
+
return 0.0
|
| 112 |
+
return float(np.dot(a, b) / denom)
|
| 113 |
|
| 114 |
def matmul(self, a, b):
|
| 115 |
+
"""Legacy math operation with resonant bias."""
|
| 116 |
return np.dot(a, b) + self.bias
|
| 117 |
+
|
| 118 |
+
def activation(self, x):
|
| 119 |
+
"""Simple sign activation."""
|
| 120 |
+
return np.sign(x)
|